From 6368971ba0bebe336f4157f9bea4ff71fb6f9638 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Tue, 7 May 2019 12:50:52 +0000 Subject: [PATCH] [FIX] sale: invoice multiple SO: right sequence Have sections order lines on each Sale Order Have multiple Sale Orders on which the action "invoice order" is triggered (to make one invoice for all of them) Before this commit, the real invoice lines were not under their respective section line This was because we kept the sequence of the order line when making the order line which makes little sense in a case where we merge the orders After this commit, the invoice lines are under their respective section OPW 1985080 closes odoo/odoo#33219 Signed-off-by: Lucas Perais (lpe) --- addons/sale/models/sale.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index 660f621ff7e..cc06fbddf41 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -516,6 +516,9 @@ class SaleOrder(models.Model): invoices_origin = {} invoices_name = {} + # Keep track of the sequences of the lines + # To keep lines under their section + inv_line_sequence = 0 for order in self: group_key = order.id if grouped else (order.partner_invoice_id.id, order.currency_id.id) @@ -524,6 +527,7 @@ class SaleOrder(models.Model): # Create lines in batch to avoid performance problems line_vals_list = [] + # sequence is the natural order of order_lines for line in order.order_line: if line.display_type == 'line_section': pending_section = line @@ -545,14 +549,21 @@ class SaleOrder(models.Model): if line.qty_to_invoice > 0 or (line.qty_to_invoice < 0 and final): if pending_section: - line_vals_list.extend(pending_section.invoice_line_create_vals( + section_invoice = pending_section.invoice_line_create_vals( invoices[group_key].id, pending_section.qty_to_invoice - )) + ) + inv_line_sequence += 1 + section_invoice[0]['sequence'] = inv_line_sequence + line_vals_list.extend(section_invoice) pending_section = None - line_vals_list.extend(line.invoice_line_create_vals( + + inv_line_sequence += 1 + inv_line = line.invoice_line_create_vals( invoices[group_key].id, line.qty_to_invoice - )) + ) + inv_line[0]['sequence'] = inv_line_sequence + line_vals_list.extend(inv_line) if references.get(invoices.get(group_key)): if order not in references[invoices[group_key]]: