def export_tracking_info_to_magento(self): """ Export tracking info to magento for the specified shipment. :param shipment: Browse record of shipment :return: Shipment increment ID """ MagentoCarrier = Pool().get('magento.instance.carrier') Channel = Pool().get('sale.channel') Shipment = Pool().get('stock.shipment.out') channel = Channel.get_current_magento_channel() assert self.tracking_number assert self.carrier try: carrier, = MagentoCarrier.search([('channel', '=', channel.id), ('carrier', '=', self.carrier.id) ]) except ValueError: # No mapping carrier found use custom code, title = 'custom', self.carrier.rec_name else: code, title = carrier.get_magento_mapping() # Add tracking info to the shipment on magento with magento.Shipment(channel.magento_url, channel.magento_api_user, channel.magento_api_key) as shipment_api: shipment_increment_id = shipment_api.addtrack( self.magento_increment_id, code, title, self.tracking_number) Shipment.write([self], {'is_tracking_exported_to_magento': True}) return shipment_increment_id
def export_tracking_info_to_magento( self, cursor, user, shipment, context ): """ Export tracking info to magento for the specified shipment. :param cursor: Database cursor :param user: ID of current user :param shipment: Browse record of shipment :param context: Dictionary of application context :return: Shipment increment ID """ magento_carrier_obj = self.pool.get('magento.instance.carrier') instance_obj = self.pool.get('magento.instance') picking_obj = self.pool.get('stock.picking') instance = instance_obj.browse( cursor, user, context['magento_instance'], context ) carriers = magento_carrier_obj.search( cursor, user, [ ('instance', '=', instance.id), ('carrier', '=', shipment.carrier_id.id) ], context=context ) if not carriers: _logger.error( 'No matching carrier has been configured on instance %s' ' for the magento carrier/shipping method %s' % (instance.name, shipment.carrier_id.name) ) return carrier = magento_carrier_obj.browse( cursor, user, carriers[0], context ) # Add tracking info to the shipment on magento with magento.Shipment( instance.url, instance.api_user, instance.api_key ) as shipment_api: shipment_increment_id = shipment_api.addtrack( shipment.magento_increment_id, carrier.code, carrier.title, shipment.carrier_tracking_ref, ) picking_obj.write( cursor, user, shipment.id, { 'is_tracking_exported_to_magento': True }, context=context ) return shipment_increment_id
def export_tracking_info_to_magento(self): """ Export tracking info to magento for the specified shipment. :param shipment: Browse record of shipment :return: Shipment increment ID """ MagentoCarrier = Pool().get('magento.instance.carrier') Channel = Pool().get('sale.channel') Shipment = Pool().get('stock.shipment.out') channel = Channel.get_current_magento_channel() assert self.tracking_number assert self.carrier carriers = MagentoCarrier.search([('channel', '=', channel.id), ('carrier', '=', self.carrier.id)]) if not carriers: # The carrier linked to this shipment is not found mapped to a # magento carrier. return # Add tracking info to the shipment on magento with magento.Shipment(channel.magento_url, channel.magento_api_user, channel.magento_api_key) as shipment_api: shipment_increment_id = shipment_api.addtrack( self.magento_increment_id, carriers[0].code, carriers[0].title, self.tracking_number, ) Shipment.write([self], {'is_tracking_exported_to_magento': True}) return shipment_increment_id
def export_shipment_status_to_magento(self, cursor, user, store_view, context): """ Exports shipment status for shipments to magento, if they are shipped :param cursor: Database cursor :param user: ID of current user :param store_view: Browse record of Store View :param context: Dictionary of application context :return: List of browse record of shipment """ shipment_obj = self.pool.get('stock.picking') instance_obj = self.pool.get('magento.instance') instance = instance_obj.browse(cursor, user, context['magento_instance'], context) domain = [ ('sale_id', '!=', None), ('sale_id.magento_store_view', '=', store_view.id), ('state', '=', 'done'), ('sale_id.magento_id', '!=', None), ('is_tracking_exported_to_magento', '=', False), ] if store_view.last_shipment_export_time: domain.append( ('write_date', '>=', store_view.last_shipment_export_time)) if store_view.export_tracking_information: domain.extend([ ('carrier_tracking_ref', '!=', None), ('carrier_id', '!=', None), ]) shipment_ids = shipment_obj.search(cursor, user, domain, context=context) shipments = [] if not shipment_ids: raise osv.except_osv( _('Shipments Not Found!'), _('Seems like there are no shipments to be exported ' 'for the orders in this store view')) for shipment in shipment_obj.browse(cursor, user, shipment_ids, context): shipments.append(shipment) increment_id = shipment.sale_id.name[ len(instance.order_prefix):len(shipment.sale_id.name)] try: # FIXME This method expects the shipment to be made for all # products in one picking. Split shipments is not supported yet with magento.Shipment(instance.url, instance.api_user, instance.api_key) as shipment_api: shipment_increment_id = shipment_api.create( order_increment_id=increment_id, items_qty={}) shipment_obj.write( cursor, user, shipment.id, { 'magento_increment_id': shipment_increment_id, }, context=context) # Rebrowse the record shipment = shipment_obj.browse(cursor, user, shipment.id, context=context) if store_view.export_tracking_information: self.export_tracking_info_to_magento( cursor, user, shipment, context) except xmlrpclib.Fault, fault: if fault.faultCode == 102: # A shipment already exists for this order, log this # detail and continue _logger.info( 'Shipment for sale %s already exists on magento' % shipment.sale_id.name) continue
def export_shipment_status_to_magento(self): """ Exports shipment status for shipments to magento, if they are shipped :return: List of active record of shipment """ Shipment = Pool().get('stock.shipment.out') Sale = Pool().get('sale.sale') SaleLine = Pool().get('sale.line') self.validate_magento_channel() sale_domain = [ ('channel', '=', self.id), ('shipment_state', '=', 'sent'), ('magento_id', '!=', None), ('shipments', '!=', None), ] if self.last_shipment_export_time: sale_domain.append( ('write_date', '>=', self.last_shipment_export_time)) sales = Sale.search(sale_domain) self.last_shipment_export_time = datetime.utcnow() self.save() updated_sales = set([]) for sale in sales: # Get the increment id from the sale reference increment_id = sale.reference[len(self.magento_order_prefix ):len(sale.reference)] for shipment in sale.shipments: try: # Some checks to make sure that only valid shipments are # being exported if shipment.is_tracking_exported_to_magento or \ shipment.state != 'done' or \ shipment.magento_increment_id: continue updated_sales.add(sale) with magento.Shipment( self.magento_url, self.magento_api_user, self.magento_api_key) as shipment_api: item_qty_map = {} for move in shipment.outgoing_moves: if isinstance(move.origin, SaleLine) \ and move.origin.magento_id: # This is done because there can be multiple # lines with the same product and they need # to be send as a sum of quanitities item_qty_map.setdefault( str(move.origin.magento_id), 0) item_qty_map[str(move.origin.magento_id)] += \ move.quantity shipment_increment_id = shipment_api.create( order_increment_id=increment_id, items_qty=item_qty_map) Shipment.write( list(sale.shipments), { 'magento_increment_id': shipment_increment_id, }) if self.magento_export_tracking_information and ( hasattr(shipment, 'tracking_number') and hasattr(shipment, 'carrier') and shipment.tracking_number and shipment.carrier): with Transaction().set_context( current_channel=self.id): shipment.export_tracking_info_to_magento() except xmlrpclib.Fault, fault: if fault.faultCode == 102: # A shipment already exists for this order, # we cannot do anything about it. # Maybe it was already exported earlier or was created # separately on magento # Hence, just continue continue