From bdef3505bd7ab27ace8599f5beecaa2dcd189241 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Wed, 19 Feb 2020 09:42:43 +0000 Subject: [PATCH] [FIX] sale: avoid adding account_id into section lines - create a SO with a section; - create an invoice from the SO. Before this commit, an SQL constraint was raised : 'Forbidden unit price, account and quantity on non-accountable invoice line'. This error occurs, because the default_get function adds a default account_id to all the lines (including section lines) if a journal_id is passed in the context. The journal_id is added in the context since : 03353118d3176463fbf779471cc272d31fb11b2e Now, the default_get function only adds a default account_id if the line is not a section line or a note line. This avoids the SQL constraint to be raised, and the invoice is correctly created. opw-2199356 opw-2199017 opw-2198781 opw-2198650 closes odoo/odoo#45809 X-original-commit: d71830f5d9f9a717194aaa8ec993d99739fe09e7 Signed-off-by: Nicolas Martinelli (nim) --- addons/sale/models/sale.py | 5 ++- addons/sale/tests/test_sale_to_invoice.py | 49 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index 0f537039544..4b8399d5f44 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -1412,7 +1412,7 @@ class SaleOrderLine(models.Model): :param qty: float quantity to invoice """ self.ensure_one() - return { + res = { 'display_type': self.display_type, 'sequence': self.sequence, 'name': self.name, @@ -1426,6 +1426,9 @@ class SaleOrderLine(models.Model): 'analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)], 'sale_line_ids': [(4, self.id)], } + if self.display_type: + res['account_id'] = False + return res def _prepare_procurement_values(self, group_id=False): """ Prepare specific key for moves or other components that will be created from a stock rule diff --git a/addons/sale/tests/test_sale_to_invoice.py b/addons/sale/tests/test_sale_to_invoice.py index b58e1ec6c20..1991c31559d 100644 --- a/addons/sale/tests/test_sale_to_invoice.py +++ b/addons/sale/tests/test_sale_to_invoice.py @@ -215,3 +215,52 @@ class TestSaleToInvoice(TestCommonSaleNoChart): self.assertEqual(line.qty_invoiced, 2.0, "The ordered (serv) sale line are totally invoiced (qty invoiced = the invoice lines)") self.assertEqual(line.untaxed_amount_to_invoice, line.price_unit * line.qty_to_invoice, "Amount to invoice is now set as qty to invoice * unit price since no price change on invoice, for ordered products") self.assertEqual(line.untaxed_amount_invoiced, line.price_unit * line.qty_invoiced, "Amount invoiced is now set as qty invoiced * unit price since no price change on invoice, for ordered products") + + def test_invoice_with_sections(self): + """ Test create and invoice with sections from the SO, and check qty invoice/to invoice, and the related amounts """ + + sale_order = self.env['sale.order'].with_context(tracking_disable=True).create({ + 'partner_id': self.partner_customer_usd.id, + 'partner_invoice_id': self.partner_customer_usd.id, + 'partner_shipping_id': self.partner_customer_usd.id, + 'pricelist_id': self.pricelist_usd.id, + }) + + SaleOrderLine = self.env['sale.order.line'].with_context(tracking_disable=True) + SaleOrderLine.create({ + 'name': 'Section', + 'display_type': 'line_section', + 'order_id': sale_order.id, + }) + sol_prod_deliver = SaleOrderLine.create({ + 'name': self.product_order.name, + 'product_id': self.product_order.id, + 'product_uom_qty': 5, + 'product_uom': self.product_order.uom_id.id, + 'price_unit': self.product_order.list_price, + 'order_id': sale_order.id, + 'tax_id': False, + }) + + # Confirm the SO + sale_order.action_confirm() + + sol_prod_deliver.write({'qty_delivered': 5.0}) + + # Context + self.context = { + 'active_model': 'sale.order', + 'active_ids': [sale_order.id], + 'active_id': sale_order.id, + 'default_journal_id': self.journal_sale.id, + } + + # Let's do an invoice with invoiceable lines + payment = self.env['sale.advance.payment.inv'].with_context(self.context).create({ + 'advance_payment_method': 'delivered' + }) + payment.create_invoices() + + invoice = sale_order.invoice_ids[0] + + self.assertEqual(invoice.line_ids[0].display_type, 'line_section')