def export_inventory_to_amazon(cls, products): """Export inventory of the products to the Amazon account in context :param products: List of active records of products """ MWSAccount = Pool().get('amazon.mws.account') mws_account = MWSAccount(Transaction().context['amazon_mws_account']) NS = "http://www.w3.org/2001/XMLSchema-instance" location_attribute = '{%s}noNamespaceSchemaLocation' % NS inventory_xml = [] for product in products: with Transaction().set_context( {'locations': [mws_account.warehouse.id]}): quantity = product.quantity if not quantity: continue if mws_account in [acc.account for acc in product.mws_accounts]: inventory_xml.append( E.Message( E.MessageID(str(product.id)), E.OperationType('Update'), E.Inventory( E.SKU(product.code), E.Quantity(str(round(quantity))), E.FulfillmentLatency( str(product.template.delivery_time)), ))) envelope_xml = E.AmazonEnvelope( E.Header(E.DocumentVersion('1.01'), E.MerchantIdentifier(mws_account.merchant_id)), E.MessageType('Inventory'), E.PurgeAndReplace('false'), *(inv_xml for inv_xml in inventory_xml)) envelope_xml.set(location_attribute, 'amznenvelope.xsd') feeds_api = mws.Feeds(mws_account.access_key, mws_account.secret_key, mws_account.merchant_id) response = feeds_api.submit_feed( etree.tostring(envelope_xml), feed_type='_POST_INVENTORY_AVAILABILITY_DATA_', marketplaceids=[mws_account.marketplace_id]) return response.parsed
def export_pricing_to_amazon(cls, products): """Export prices of the products to the Amazon account in context :param products: List of active records of products """ MWSAccount = Pool().get('amazon.mws.account') mws_account = MWSAccount(Transaction().context['amazon_mws_account']) NS = "http://www.w3.org/2001/XMLSchema-instance" location_attribute = '{%s}noNamespaceSchemaLocation' % NS pricing_xml = [] for product in products: if mws_account in [acc.account for acc in product.mws_accounts]: pricing_xml.append( E.Message( E.MessageID(str(product.id)), E.OperationType('Update'), E.Price( E.SKU(product.code), E.StandardPrice( # TODO: Use a pricelist str(product.template.list_price), currency=mws_account.company.currency.code), ))) envelope_xml = E.AmazonEnvelope( E.Header(E.DocumentVersion('1.01'), E.MerchantIdentifier(mws_account.merchant_id)), E.MessageType('Price'), E.PurgeAndReplace('false'), *(price_xml for price_xml in pricing_xml)) envelope_xml.set(location_attribute, 'amznenvelope.xsd') feeds_api = mws.Feeds(mws_account.access_key, mws_account.secret_key, mws_account.merchant_id) response = feeds_api.submit_feed( etree.tostring(envelope_xml), feed_type='_POST_PRODUCT_PRICING_DATA_', marketplaceids=[mws_account.marketplace_id]) return response.parsed
def _get_amazon_envelop(self, message_type, xml_list): """ Returns amazon envelop for xml given """ NS = "http://www.w3.org/2001/XMLSchema-instance" location_attribute = '{%s}noNamespaceSchemaLocation' % NS envelope_xml = E.AmazonEnvelope( E.Header( E.DocumentVersion('1.01'), E.MerchantIdentifier(self.amazon_merchant_id) ), E.MessageType(message_type), E.PurgeAndReplace('false'), *(xml for xml in xml_list) ) envelope_xml.set(location_attribute, 'amznenvelope.xsd') return envelope_xml
def export_to_amazon(cls, products): """Export the products to the Amazon account in context :param products: List of active records of products """ MWSAccount = Pool().get('amazon.mws.account') mws_account = MWSAccount(Transaction().context['amazon_mws_account']) NS = "http://www.w3.org/2001/XMLSchema-instance" location_attribute = '{%s}noNamespaceSchemaLocation' % NS products_xml = [] for product in products: if not product.code: cls.raise_user_error('missing_product_code', {'product': product.template.name}) if not product.codes: cls.raise_user_error('missing_product_codes', {'product': product.template.name}) # Get the product's code to be set as standard ID to amazon product_standard_id = (product.asin or product.ean or product.upc or product.isbn or product.gtin) products_xml.append( E.Message( E.MessageID(str(product.id)), E.OperationType('Update'), E.Product( E.SKU(product.code), E.StandardProductID( E.Type(product_standard_id.code_type.upper()), E.Value(product_standard_id.code), ), E.DescriptionData( E.Title(product.template.name), E.Description(product.description), ), # Amazon needs this information so as to place the product # under a category. # FIXME: Either we need to create all that inside our # system or figure out a way to get all that via API E.ProductData( E.Miscellaneous(E.ProductType('Misc_Other'), ), ), ))) envelope_xml = E.AmazonEnvelope( E.Header(E.DocumentVersion('1.01'), E.MerchantIdentifier(mws_account.merchant_id)), E.MessageType('Product'), E.PurgeAndReplace('false'), *(product_xml for product_xml in products_xml)) envelope_xml.set(location_attribute, 'amznenvelope.xsd') feeds_api = mws.Feeds(mws_account.access_key, mws_account.secret_key, mws_account.merchant_id) response = feeds_api.submit_feed( etree.tostring(envelope_xml), feed_type='_POST_PRODUCT_DATA_', marketplaceids=[mws_account.marketplace_id]) cls.write( products, { 'mws_accounts': [('create', [{ 'product': product.id, 'account': mws_account.id, } for product in products])] }) return response.parsed