diff --git a/addons/sale/models/account_move.py b/addons/sale/models/account_move.py index 8c14faee2b5..caae347ee13 100644 --- a/addons/sale/models/account_move.py +++ b/addons/sale/models/account_move.py @@ -72,6 +72,8 @@ class AccountMoveLine(models.Model): For Vendor Bill flow, if the product has a 'erinvoice policy' and is a cost, then we will find the SO on which reinvoice the AAL """ self.ensure_one() + if self.sale_line_ids: + return False uom_precision_digits = self.env['decimal.precision'].precision_get('Product Unit of Measure') return float_compare(self.credit or 0.0, self.debit or 0.0, precision_digits=uom_precision_digits) != 1 and self.product_id.expense_policy not in [False, 'no'] diff --git a/addons/sale/tests/test_reinvoice.py b/addons/sale/tests/test_reinvoice.py index 6ba53f7e24b..477004a22c4 100644 --- a/addons/sale/tests/test_reinvoice.py +++ b/addons/sale/tests/test_reinvoice.py @@ -218,3 +218,44 @@ class TestReInvoice(TestSaleCommon): self.assertEqual(len(self.sale_order.order_line), 1, "No SO line should have been created (or removed) when validating vendor bill") self.assertTrue(invoice_a.mapped('line_ids.analytic_line_ids'), "Analytic lines should be generated") + + def test_not_reinvoicing_invoiced_so_lines(self): + """ Test that invoiced SO lines are not re-invoiced. """ + so_line1 = self.env['sale.order.line'].create({ + 'name': self.company_data['product_delivery_cost'].name, + 'product_id': self.company_data['product_delivery_cost'].id, + 'product_uom_qty': 1, + 'product_uom': self.company_data['product_delivery_cost'].uom_id.id, + 'price_unit': self.company_data['product_delivery_cost'].list_price, + 'discount': 100.00, + 'order_id': self.sale_order.id, + }) + so_line1.product_id_change() + so_line2 = self.env['sale.order.line'].create({ + 'name': self.company_data['product_delivery_sales_price'].name, + 'product_id': self.company_data['product_delivery_sales_price'].id, + 'product_uom_qty': 1, + 'product_uom': self.company_data['product_delivery_sales_price'].uom_id.id, + 'price_unit': self.company_data['product_delivery_sales_price'].list_price, + 'discount': 100.00, + 'order_id': self.sale_order.id, + }) + so_line2.product_id_change() + + self.sale_order.onchange_partner_id() + self.sale_order._compute_tax_id() + self.sale_order.action_confirm() + + for line in self.sale_order.order_line: + line.qty_delivered = 1 + # create invoice and validate it + invoice = self.sale_order._create_invoices() + invoice.action_post() + + so_line3 = self.sale_order.order_line.filtered(lambda sol: sol != so_line1 and sol.product_id == self.company_data['product_delivery_cost']) + so_line4 = self.sale_order.order_line.filtered(lambda sol: sol != so_line2 and sol.product_id == self.company_data['product_delivery_sales_price']) + + self.assertFalse(so_line3, "No re-invoicing should have created a new sale line with product #1") + self.assertFalse(so_line4, "No re-invoicing should have created a new sale line with product #2") + self.assertEqual(so_line1.qty_delivered, 1, "No re-invoicing should have impacted exising SO line 1") + self.assertEqual(so_line2.qty_delivered, 1, "No re-invoicing should have impacted exising SO line 2")