From d262dfac6914663bcef51141586b41c9c545659d Mon Sep 17 00:00:00 2001 From: Nicolas Lempereur Date: Mon, 24 Jul 2023 11:24:47 +0000 Subject: [PATCH] [FIX] sale: fix amount to invoice multiple SO one invoice line From an internal feedback, someone reported that the code added: e2ec9e6c86949f5781d03a7dbb1065ef8af3196a might cause an issue in case of several sale order linked to the same invoice line. With this changeset, in this case we will have the same behavior as before, eg: - one sale order line (SOL1) has amount_total 700 - one sale order line (SOL2) has amount_total 1400 if the invoice line linked to both line has price_total: * 700 => amount_to_invoice of: SOL1 will be 0, SOL2 will be 700 * 1400 => amount_to_invoice of: SOL1 will be -700, SOL2 will be 0 * 2100 => amount_to_invoice of: SOL1 will be -1400, SOL2 will be -700 opw-3437237 closes odoo/odoo#133498 X-original-commit: 15a90c9f6cbf5a1750fafee994fda4b6ec2ebc14 Signed-off-by: Nicolas Lempereur (nle) --- addons/sale/models/sale_order.py | 2 +- addons/sale/tests/test_sale_to_invoice.py | 41 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/addons/sale/models/sale_order.py b/addons/sale/models/sale_order.py index 464da51139a..2f7de9ec508 100644 --- a/addons/sale/models/sale_order.py +++ b/addons/sale/models/sale_order.py @@ -604,7 +604,7 @@ class SaleOrder(models.Model): order.amount_to_invoice = order.amount_total for invoice in order.invoice_ids.filtered(lambda x: x.state == 'posted'): - prices = sum(invoice.line_ids.filtered(lambda x: x.sale_line_ids.order_id.id == order.id).mapped('price_total')) + prices = sum(invoice.line_ids.filtered(lambda x: order in x.sale_line_ids.order_id).mapped('price_total')) invoice_amount_currency = invoice.currency_id._convert( prices * -invoice.direction_sign, order.currency_id, diff --git a/addons/sale/tests/test_sale_to_invoice.py b/addons/sale/tests/test_sale_to_invoice.py index 5b5755ae46d..b1cf0b04ee2 100644 --- a/addons/sale/tests/test_sale_to_invoice.py +++ b/addons/sale/tests/test_sale_to_invoice.py @@ -989,3 +989,44 @@ class TestSaleToInvoice(TestSaleCommon): self.assertEqual(sale_order_1.amount_to_invoice, 0.0) self.assertEqual(sale_order_2.amount_to_invoice, 0.0) + + def test_amount_to_invoice_one_line_multiple_so(self): + """ Testing creating two SOs linked to the same invoice line. Drawback: the substracted + amount to the amount_total will take both sale order into account. + """ + sale_order_1 = self.env['sale.order'].create({ + 'partner_id': self.partner_a.id, + 'order_line': [ + Command.create({ + 'product_id': self.company_data['product_delivery_no'].id, + 'product_uom_qty': 10, + }), + ], + }) + sale_order_2 = self.env['sale.order'].create({ + 'partner_id': self.partner_a.id, + 'order_line': [ + Command.create({ + 'product_id': self.company_data['product_delivery_no'].id, + 'product_uom_qty': 20, + }), + ], + }) + + sale_order_1.action_confirm() + sale_order_2.action_confirm() + sale_order_1.order_line.qty_delivered = 10 + sale_order_2.order_line.qty_delivered = 20 + + self.env['sale.advance.payment.inv'].create({ + 'advance_payment_method': 'delivered', + 'sale_order_ids': [Command.set((sale_order_2).ids)], + }).create_invoices() + + sale_order_1.invoice_ids = sale_order_2.invoice_ids + sale_order_1.invoice_ids.line_ids.sale_line_ids += sale_order_1.order_line + + sale_order_1.invoice_ids.action_post() + + self.assertEqual(sale_order_1.amount_to_invoice, -700.0) + self.assertEqual(sale_order_2.amount_to_invoice, 0.0)