def export_inventory_to_magento(self, cursor, user, website, context): """ Exports stock data of products from openerp to magento for this website :param cursor: Database cursor :param user: ID of current user :param website: Browse record of website :param context: Application context :return: List of products """ products = [] instance = website.instance for magento_product in website.magento_products: products.append(magento_product.product) is_in_stock = '1' if magento_product.product.qty_available > 0 \ else '0' product_data = { 'qty': magento_product.product.qty_available, 'is_in_stock': is_in_stock, } # Update stock information to magento with magento.Inventory(instance.url, instance.api_user, instance.api_key) as inventory_api: inventory_api.update(magento_product.magento_id, product_data) return products
def export_inventory_to_magento(self): """ Exports stock data of products from tryton to magento for this channel :return: List of product templates """ Location = Pool().get('stock.location') self.validate_magento_channel() products = [] locations = Location.search([('type', '=', 'storage')]) for listing in self.product_listings: product = listing.product products.append(product) with Transaction().set_context({'locations': map(int, locations)}): product_data = { 'qty': product.quantity, 'is_in_stock': '1' if listing.product.quantity > 0 else '0', } # Update stock information to magento with magento.Inventory( self.magento_url, self.magento_api_user, self.magento_api_key ) as inventory_api: inventory_api.update( listing.product_identifier, product_data ) return products
def export_inventory_to_magento(self): """ Exports stock data of products from openerp to magento for this website :return: List of product templates """ Location = Pool().get('stock.location') product_templates = [] instance = self.instance locations = Location.search([('type', '=', 'storage')]) for magento_product_template in self.magento_product_templates: product_template = magento_product_template.template product_templates.append(product_template) with Transaction().set_context({'locations': map(int, locations)}): product_data = { 'qty': product_template.quantity, 'is_in_stock': '1' if product_template.quantity > 0 else '0', } # Update stock information to magento with magento.Inventory(instance.url, instance.api_user, instance.api_key) as inventory_api: inventory_api.update(magento_product_template.magento_id, product_data) return product_templates
def export_inventory(self): """ Export inventory of this listing """ if self.channel.source != 'magento': return super(ProductSaleChannelListing, self).export_inventory() Date = Pool().get('ir.date') channel, product = self.channel, self.product with Transaction().set_context(locations=[channel.warehouse.id], stock_date_end=Date.today(), stock_assign=True): product_data = { 'qty': product.quantity, } if self.magento_product_type == 'simple': # Only send inventory for simple products product_data['is_in_stock'] = '1' \ if product.quantity > 0 else '0' else: # configurable, bundle and everything else product_data['is_in_stock'] = '1' # Update stock information to magento # TODO: Figure out a way to do a bulk update and see the # result. At the moment this sucks because each update # takes forever on the slow magento API with magento.Inventory(channel.magento_url, channel.magento_api_user, channel.magento_api_key) as inventory_api: try: inventory_api.update(self.product_identifier, product_data) except xmlrpclib.Fault, e: if e.faultCode == 101: # Product does not exists self.state = 'disabled' self.save() # TODO: Notify human else: raise
def export_bulk_inventory(cls, listings): """ Bulk export inventory to magento. Do not rely on the return value from this method. """ SaleChannelListing = Pool().get('product.product.channel_listing') if not listings: # Nothing to update return non_magento_listings = cls.search([ ('id', 'in', map(int, listings)), ('channel.source', '!=', 'magento'), ]) if non_magento_listings: super(ProductSaleChannelListing, cls).export_bulk_inventory( non_magento_listings ) magento_listings = filter( lambda l: l not in non_magento_listings, listings ) log.info( "Fetching inventory of %d magento listings" % len(magento_listings) ) inventory_channel_map = defaultdict(list) for listing in magento_listings: channel = listing.channel product_data = { 'qty': listing.quantity, } # TODO: Get this from availability used if listing.magento_product_type == 'simple': # Only send inventory for simple products product_data['is_in_stock'] = '1' \ if listing.quantity > 0 else '0' else: # configurable, bundle and everything else product_data['is_in_stock'] = '1' # group inventory xml by channel inventory_channel_map[channel].append([ listing.product_identifier, product_data ]) for channel, product_data_list in inventory_channel_map.iteritems(): with magento.Inventory( channel.magento_url, channel.magento_api_user, channel.magento_api_key) as inventory_api: for product_data_batch in batch(product_data_list, 50): log.info( "Pushing inventory of %d products to magento" % len(product_data_batch) ) response = inventory_api.update_multi(product_data_batch) # Magento bulk API will not raise Faults. # Instead the response contains the faults as a dict for i, result in enumerate(response): if result is not True: if result.get('isFault') is True and \ result['faultCode'] == '101': listing, = SaleChannelListing.search([ ('product_identifier', '=', product_data_batch[i][0]), # noqa ('channel', '=', channel.id), ]) listing.state = 'disabled' listing.save() else: cls.raise_user_error( 'multi_inventory_update_fail', (result['faultCode'], result['faultMessage']) # noqa )