From c5d9f4ee146f95f1d1698a0e2f5f255226cd09b0 Mon Sep 17 00:00:00 2001 From: "Valentin Vallaeys (vava)" Date: Mon, 27 Feb 2023 14:52:45 +0000 Subject: [PATCH] [FIX] sale: compute price unit on downpayment line For the moment, the price unit of the down payment line on the sale order is not always correctly computed. If the price unit is updated on the down payment invoice, it is not updated on the sale order; or if a regular invoice is created, the price unit of the down payment is set to zero on the sale order. This commit corrects the behavior by using the unit price of the invoice line. opw-3140740 opw-3160406 opw-3160420 closes odoo/odoo#114304 X-original-commit: 755c517fcab89df51e8b0ee4529894372b4d4ec1 Signed-off-by: Vallaeys Valentin (vava) --- addons/sale/models/account_move.py | 7 +--- addons/sale/tests/test_sale_to_invoice.py | 42 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/addons/sale/models/account_move.py b/addons/sale/models/account_move.py index a687578cc35..467eeea5084 100644 --- a/addons/sale/models/account_move.py +++ b/addons/sale/models/account_move.py @@ -69,12 +69,7 @@ class AccountMove(models.Model): try: line.sale_line_ids.tax_id = line.tax_ids - if all(line.tax_ids.mapped('price_include')): - line.sale_line_ids.price_unit = line.price_unit - else: - #To keep positive amount on the sale order and to have the right price for the invoice - #We need the - before our untaxed_amount_to_invoice - line.sale_line_ids.price_unit = -line.sale_line_ids.untaxed_amount_to_invoice + line.sale_line_ids.price_unit = line.price_unit except UserError: # a UserError here means the SO was locked, which prevents changing the taxes # just ignore the error - this is a nice to have feature and should not be blocking diff --git a/addons/sale/tests/test_sale_to_invoice.py b/addons/sale/tests/test_sale_to_invoice.py index dea992216d3..43a93344ef6 100644 --- a/addons/sale/tests/test_sale_to_invoice.py +++ b/addons/sale/tests/test_sale_to_invoice.py @@ -154,6 +154,48 @@ class TestSaleToInvoice(TestSaleCommon): # Validate invoice self.sale_order.invoice_ids.action_post() + def test_downpayment_line_remains_on_SO(self): + """ Test downpayment's SO line is created and remains unchanged even if everything is invoiced + """ + # Create the SO with one line + sale_order = self.env['sale.order'].with_context(tracking_disable=True).create({ + 'partner_id': self.partner_a.id, + 'partner_invoice_id': self.partner_a.id, + 'partner_shipping_id': self.partner_a.id, + 'pricelist_id': self.company_data['default_pricelist'].id, + 'order_line': [Command.create({ + 'product_id': self.company_data['product_order_no'].id, + 'product_uom_qty': 5, + 'tax_id': False, + }),] + }) + # Confirm the SO + sale_order.action_confirm() + # Update delivered quantity of SO line + sale_order.order_line.write({'qty_delivered': 5.0}) + context = { + 'active_model': 'sale.order', + 'active_ids': [sale_order.id], + 'active_id': sale_order.id, + 'default_journal_id': self.company_data['default_journal_sale'].id, + } + # Let's do an invoice for a down payment of 50 + downpayment = self.env['sale.advance.payment.inv'].with_context(context).create({ + 'advance_payment_method': 'fixed', + 'fixed_amount': 50, + 'deposit_account_id': self.company_data['default_account_revenue'].id + }) + downpayment.create_invoices() + # Let's do the invoice + payment = self.env['sale.advance.payment.inv'].with_context(context).create({ + 'deposit_account_id': self.company_data['default_account_revenue'].id + }) + payment.create_invoices() + # Confirm all invoices + for invoice in sale_order.invoice_ids: + invoice.action_post() + downpayment_line = sale_order.order_line.filtered(lambda l: l.is_downpayment and not l.display_type) + self.assertEqual(downpayment_line[0].price_unit, 50, 'The down payment unit price should not change on SO') def test_downpayment_percentage_tax_icl(self): """ Test invoice with a percentage downpayment and an included tax