diff --git a/addons/purchase/stock.py b/addons/purchase/stock.py index a966ac8308f..12a18984361 100644 --- a/addons/purchase/stock.py +++ b/addons/purchase/stock.py @@ -83,6 +83,14 @@ class stock_move(osv.osv): class stock_picking(osv.osv): _inherit = 'stock.picking' + def _get_partner_to_invoice(self, cr, uid, picking, context=None): + """ Inherit the original function of the 'stock' module + We select the partner of the purchase order as the partner of the invoice + """ + if picking.move_lines and picking.move_lines[0] and picking.move_lines[0].purchase_line_id: + return picking.move_lines[0].purchase_line_id.order_id.partner_id and picking.move_lines[0].purchase_line_id.order_id.partner_id.id + return super(stock_picking, self)._get_partner_to_invoice(cr, uid, picking, context=context) + def _get_to_invoice(self, cr, uid, ids, name, args, context=None): res = {} for picking in self.browse(cr, uid, ids, context=context): diff --git a/addons/sale_stock/sale_stock.py b/addons/sale_stock/sale_stock.py index 9e409f8be31..f2c6f529dcc 100644 --- a/addons/sale_stock/sale_stock.py +++ b/addons/sale_stock/sale_stock.py @@ -396,6 +396,17 @@ class stock_location_route(osv.osv): class stock_picking(osv.osv): _inherit = "stock.picking" + def _get_partner_to_invoice(self, cr, uid, picking, context=None): + """ Inherit the original function of the 'stock' module + We select the partner of the sales order as the partner of the customer invoice + """ + saleorder_ids = self.pool['sale.order'].search(cr, uid, [('procurement_group_id' ,'=', picking.group_id.id)], context=context) + saleorders = self.pool['sale.order'].browse(cr, uid, saleorder_ids, context=context) + if saleorders and saleorders[0]: + saleorder = saleorders[0] + return saleorder.partner_invoice_id.id + return super(stock_picking, self)._get_partner_to_invoice(cr, uid, picking, context=context) + def _get_sale_id(self, cr, uid, ids, name, args, context=None): sale_obj = self.pool.get("sale.order") res = {} diff --git a/addons/stock_account/stock.py b/addons/stock_account/stock.py index 05f0fe3f6ce..e6a26bd9d64 100644 --- a/addons/stock_account/stock.py +++ b/addons/stock_account/stock.py @@ -190,6 +190,14 @@ class stock_picking(osv.osv): invoice_obj = self.pool.get('account.invoice') return invoice_obj.create(cr, uid, vals, context=context) + def _get_partner_to_invoice(self, cr, uid, picking, context=None): + """ Gets the partner that will be invoiced + Note that this function is inherited in the sale and purchase modules + @param picking: object of the picking for which we are selecting the partner to invoice + @return: object of the partner to invoice + """ + return picking.partner_id and picking.partner_id.id + def action_invoice_create(self, cr, uid, ids, journal_id, group=False, type='out_invoice', context=None): """ Creates invoice based on the invoice state selected for picking. @param journal_id: Id of journal @@ -197,20 +205,14 @@ class stock_picking(osv.osv): @param type: Type invoice to be created @return: Ids of created invoices for the pickings """ - saleorder_model = self.pool['sale.order'] context = context or {} todo = {} for picking in self.browse(cr, uid, ids, context=context): + partner = self._get_partner_to_invoice(cr, uid, picking, context) #grouping is based on the invoiced partner if group: - saleorder_ids = saleorder_model.search(cr, uid, [('procurement_group_id' ,'=', picking.group_id.id)], context=context) - saleorders = saleorder_model.browse(cr, uid, saleorder_ids, context=context) - if saleorders and saleorders[0]: - saleorder = saleorders[0] - key = "%s" % saleorder.partner_invoice_id.id - else: - key = "Pick %s" % picking.id + key = partner else: key = picking.id for move in picking.move_lines: