From 7235c5116453b1ff653d269b71852c1af2ed3fc5 Mon Sep 17 00:00:00 2001 From: Laurent Smet Date: Fri, 18 Oct 2019 08:02:07 +0000 Subject: [PATCH] [FIX] account: Fix tax lines generation with analytic The analytic account/tags must be set on taxes only when the 'analytic' box is ticked. closes odoo/odoo#39088 --task: 2089557 X-original-commit: 0ed224048d2d5f7e6c37af42fb8865237b874b94 Signed-off-by: Quentin De Paoli (qdp) --- addons/account/models/account_move.py | 4 +- .../tests/test_account_move_out_invoice.py | 142 ++++++++++++++++++ 2 files changed, 144 insertions(+), 2 deletions(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index e6387212f34..0a207efbab6 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -411,8 +411,8 @@ class AccountMove(models.Model): 'tax_repartition_line_id': tax_vals['tax_repartition_line_id'], 'account_id': account.id, 'currency_id': base_line.currency_id.id, - 'analytic_tag_ids': [(6, 0, base_line.analytic_tag_ids.ids)], - 'analytic_account_id': base_line.analytic_account_id.id, + 'analytic_tag_ids': [(6, 0, tax_vals['analytic'] and base_line.analytic_tag_ids.ids or [])], + 'analytic_account_id': tax_vals['analytic'] and base_line.analytic_account_id.id, 'tax_ids': [(6, 0, tax_vals['tax_ids'])], 'tag_ids': [(6, 0, tax_vals['tag_ids'])], } diff --git a/addons/account/tests/test_account_move_out_invoice.py b/addons/account/tests/test_account_move_out_invoice.py index 93fe3b1baa0..38a54508910 100644 --- a/addons/account/tests/test_account_move_out_invoice.py +++ b/addons/account/tests/test_account_move_out_invoice.py @@ -522,6 +522,148 @@ class TestAccountMoveOutInvoiceOnchanges(InvoiceTestCommon): 'amount_total': 1730.0, }) + def test_out_invoice_line_onchange_analytic(self): + self.env.user.groups_id += self.env.ref('analytic.group_analytic_accounting') + self.env.user.groups_id += self.env.ref('analytic.group_analytic_tags') + + analytic_tag = self.env['account.analytic.tag'].create({ + 'name': 'test_analytic_tag', + }) + + analytic_account = self.env['account.analytic.account'].create({ + 'name': 'test_analytic_account', + 'partner_id': self.invoice.partner_id.id, + 'code': 'TEST' + }) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.analytic_account_id = analytic_account + line_form.analytic_tag_ids.add(analytic_tag) + move_form.save() + + # The tax is not flagged as an analytic one. It should change nothing on the taxes. + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'analytic_account_id': analytic_account.id, + 'analytic_tag_ids': analytic_tag.ids, + }, + { + **self.product_line_vals_2, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.tax_line_vals_1, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.tax_line_vals_2, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.term_line_vals_1, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.analytic_account_id = self.env['account.analytic.account'] + line_form.analytic_tag_ids.clear() + move_form.save() + + # Enable the analytic + self.tax_sale_a.analytic = True + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.analytic_account_id = analytic_account + line_form.analytic_tag_ids.add(analytic_tag) + move_form.save() + + # The tax is flagged as an analytic one. + # A new tax line must be generated. + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'analytic_account_id': analytic_account.id, + 'analytic_tag_ids': analytic_tag.ids, + }, + { + **self.product_line_vals_2, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.tax_line_vals_1, + 'price_unit': 150.0, + 'price_subtotal': 150.0, + 'price_total': 150.0, + 'credit': 150.0, + 'analytic_account_id': analytic_account.id, + 'analytic_tag_ids': analytic_tag.ids, + }, + { + **self.tax_line_vals_1, + 'price_unit': 30.0, + 'price_subtotal': 30.0, + 'price_total': 30.0, + 'credit': 30.0, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.tax_line_vals_2, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.term_line_vals_1, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + ], self.move_vals) + + move_form = Form(self.invoice) + with move_form.invoice_line_ids.edit(0) as line_form: + line_form.analytic_account_id = self.env['account.analytic.account'] + line_form.analytic_tag_ids.clear() + move_form.save() + + # The tax line has been removed. + self.assertInvoiceValues(self.invoice, [ + { + **self.product_line_vals_1, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.product_line_vals_2, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.tax_line_vals_1, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.tax_line_vals_2, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + { + **self.term_line_vals_1, + 'analytic_account_id': False, + 'analytic_tag_ids': [], + }, + ], self.move_vals) + def test_out_invoice_line_onchange_cash_rounding_1(self): move_form = Form(self.invoice) # Add a cash rounding having 'add_invoice_line'.