From 4ad329de55a96d0ff31bc13850945aacb89d0e30 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Fri, 20 Feb 2015 12:26:31 +0100 Subject: [PATCH] [FIX] delivery: shipping invoice line depending on amount total MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The delivery module overrode the method _create_invoice_from_picking to add the shipping costs according to the invoice and the shipping. The issue is that the method _create_invoice_from_picking creates an empty invoice, without any line. The lines are added afterwards, using the method _create_invoice_line_from_vals, within the method _invoice_create_line. So, after having called _create_invoice_from_picking, the invoice is indeed created, but without lines, and therefore without the amount total value. Adding the shipping costs there will thus prevent the shipping costs based on the total price of the invoice (e.g. free for an order of 500€) This rev. overrides the method _invoice_create_line instead, as, after the call to this method, the invoice will be completely set, with all its lines, and with a correct amount total. The side effect is that we need to recompute the taxes a second time, using button_compute. This is not the cleanest way to solve this issue. Indeed, a cleaner patch would be to change the method _create_invoice_from_picking so it creates the invoice along with its invoice lines, using the ORM command (0, _, values) to creates the lines directly within the invoice creation: invoice['invoice_line'] = [(0, _, values) for values in invoice_lines_vals]. Nevertheless, this is a bigger change, that will probably require API changes, and therefore should be done in master. opw-626226 opw-628517 --- addons/delivery/stock.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/addons/delivery/stock.py b/addons/delivery/stock.py index 3d6a552245a..4e0071e066d 100644 --- a/addons/delivery/stock.py +++ b/addons/delivery/stock.py @@ -119,15 +119,22 @@ class stock_picking(osv.osv): 'invoice_line_tax_id': [(6, 0, taxes_ids)], } - def _create_invoice_from_picking(self, cr, uid, picking, vals, context=None): + def _invoice_create_line(self, cr, uid, moves, journal_id, inv_type='out_invoice', context=None): invoice_obj = self.pool.get('account.invoice') invoice_line_obj = self.pool.get('account.invoice.line') - invoice_id = super(stock_picking, self)._create_invoice_from_picking(cr, uid, picking, vals, context=context) - invoice = invoice_obj.browse(cr, uid, invoice_id, context=context) - invoice_line = self._prepare_shipping_invoice_line(cr, uid, picking, invoice, context=context) - if invoice_line: - invoice_line_obj.create(cr, uid, invoice_line) - return invoice_id + invoice_ids = super(stock_picking, self)._invoice_create_line(cr, uid, moves, journal_id, inv_type=inv_type, context=context) + delivey_invoices = {} + for move in moves: + for invoice in move.picking_id.sale_id.invoice_ids: + if invoice.id in invoice_ids: + delivey_invoices[invoice] = move.picking_id + if delivey_invoices: + for invoice, picking in delivey_invoices.items(): + invoice_line = self._prepare_shipping_invoice_line(cr, uid, picking, invoice, context=context) + if invoice_line: + invoice_line_obj.create(cr, uid, invoice_line) + invoice_obj.button_compute(cr, uid, [invoice.id], context=context, set_total=(inv_type in ('in_invoice', 'in_refund'))) + return invoice_ids def _get_default_uom(self, cr, uid, context=None): uom_categ_id = self.pool.get('ir.model.data').xmlid_to_res_id(cr, uid, 'product.product_uom_categ_kgm')