diff --git a/addons/sale/models/account_move.py b/addons/sale/models/account_move.py index e944314bc5e..8c14faee2b5 100644 --- a/addons/sale/models/account_move.py +++ b/addons/sale/models/account_move.py @@ -16,9 +16,12 @@ class AccountMove(models.Model): for line in line_ids: try: line.sale_line_ids.tax_id = line.tax_ids - #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 + 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 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 67ca71b3024..06247d827ce 100644 --- a/addons/sale/tests/test_sale_to_invoice.py +++ b/addons/sale/tests/test_sale_to_invoice.py @@ -100,6 +100,36 @@ class TestSaleToInvoice(TestSaleCommon): self.assertEqual(len(invoice.invoice_line_ids.filtered(lambda l: not (l.display_type == 'line_section' and l.name == "Down Payments"))), len(self.sale_order.order_line), 'All lines should be invoiced') self.assertEqual(invoice.amount_total, self.sale_order.amount_total - downpayment_line.price_unit, 'Downpayment should be applied') + def test_downpayment_percentage_tax_icl(self): + """ Test invoice with a percentage downpayment and an included tax + Check the total amount of invoice is correct and equal to a respective sale order's total amount + """ + # Confirm the SO + self.sale_order.action_confirm() + tax_downpayment = self.company_data['default_tax_sale'].copy({'price_include': True}) + # Let's do an invoice for a deposit of 100 + product_id = self.env['ir.config_parameter'].sudo().get_param('sale.default_deposit_product_id') + product_id = self.env['product.product'].browse(int(product_id)).exists() + product_id.taxes_id = tax_downpayment.ids + payment = self.env['sale.advance.payment.inv'].with_context(self.context).create({ + 'advance_payment_method': 'percentage', + 'amount': 50, + 'deposit_account_id': self.company_data['default_account_revenue'].id, + }) + payment.create_invoices() + + self.assertEqual(len(self.sale_order.invoice_ids), 1, 'Invoice should be created for the SO') + downpayment_line = self.sale_order.order_line.filtered(lambda l: l.is_downpayment) + self.assertEqual(len(downpayment_line), 1, 'SO line downpayment should be created on SO') + self.assertEqual(downpayment_line.price_unit, self.sale_order.amount_total/2, 'downpayment should have the correct amount') + + invoice = self.sale_order.invoice_ids[0] + downpayment_aml = invoice.line_ids.filtered(lambda l: not (l.display_type == 'line_section' and l.name == "Down Payments"))[0] + self.assertEqual(downpayment_aml.price_total, self.sale_order.amount_total/2, 'downpayment should have the correct amount') + self.assertEqual(downpayment_aml.price_unit, self.sale_order.amount_total/2, 'downpayment should have the correct amount') + invoice.action_post() + self.assertEqual(downpayment_line.price_unit, self.sale_order.amount_total/2, 'downpayment should have the correct amount') + def test_invoice_with_discount(self): """ Test invoice with a discount and check discount applied on both SO lines and an invoice lines """ # Update discount and delivered quantity on SO lines