From 718f2aede4f433c11209bb75f5c6665265c16a35 Mon Sep 17 00:00:00 2001 From: oco-odoo Date: Tue, 16 Jun 2020 15:19:42 +0000 Subject: [PATCH] [FIX] account: cash basis taxes: Make cash basis taxes work with the new tax report in any case. Before that, since https://github.com/odoo/odoo/commit/71ee252dc43fd0e2b86ba8079c31915544e528eb , negative invoice lines were not supported by thoses taxes. Nor were miscellaneous entries. To make them work, we change the way tags are assigned to cash basis taxes : instead of copying them from the invoice, sign included, we now change their sign when appropriate, so that the resulting cash basis move can be treated like any regular miscellaneous operation by the tax report (so, always applying a factor of 1 to its lines' balance). Doing that also allows us to re-simplify the code used in the tax report and tax audit computation to manage cash basis entries, and makes the cash basis entry more standard. This is also a usability improvement: now the user does not have to search for the original invoice to understand how the CABA entries are going to impact the report: it's always managed like a misc. operation, so he can just trust the signs of the tags. v13 version of https://github.com/odoo/odoo/pull/53085 closes odoo/odoo#54058 X-original-commit: 20850301f5025ffd1a05e0594f6d26d2a61759ef Related: odoo/enterprise#11652 Signed-off-by: Laurent Smet Signed-off-by: oco-odoo --- addons/account/models/account_move.py | 67 +++++++++++++-------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 554dcc68b94..3b387975d5b 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -3429,43 +3429,13 @@ class AccountMoveLine(models.Model): audit_str = '' for tag in record.tax_tag_ids: - # In case of cash basis entries, we need to consider the original invoice, not the current move if record.move_id.tax_cash_basis_rec_id: - reconciled_amls = record.move_id.tax_cash_basis_rec_id.debit_move_id + record.move_id.tax_cash_basis_rec_id.credit_move_id - invoice_aml = reconciled_amls.filtered(lambda x: x.journal_id.type in ('sale', 'purchase')) # To exclude the payment - - if len(invoice_aml) > 1: - - caba_origin_inv_journal_type = invoice_aml.mapped('journal_id.type')[0] - type_prefixes = {'sale': 'out', 'purchase': 'in'} - - if record.tax_repartition_line_id: - # If tax_repartition_line_id is set, we know for sure we are on a tax line. - # We can then simply check whether the repartition line is intended for invoices or refunds - type_postfix = record.tax_repartition_line_id.invoice_tax_id and 'invoice' or 'refund' - caba_origin_inv_type = "%s_%s" % (type_prefixes[caba_origin_inv_journal_type], type_postfix) - - elif record.tax_ids: - # If it's a base line, we rely on debit/credit to guess the type of the CABA origin invoice. - if (caba_origin_inv_journal_type == 'sale' and record.credit) \ - or (caba_origin_inv_journal_type == 'purchase' and record.debit): - caba_origin_inv_type = type_prefixes[caba_origin_inv_journal_type] + '_invoice' - else: - caba_origin_inv_type = type_prefixes[caba_origin_inv_journal_type] + '_refund' - - else: - # Default type for non-tax related lines is invoice. (in/out depending of the journal) - caba_origin_inv_type = type_prefixes[caba_origin_inv_journal_type] + '_invoice' - - if caba_origin_inv_type not in invoice_aml.mapped('move_id.move_type'): - caba_origin_inv_type = type_prefixes[caba_origin_inv_journal_type] + '_invoice' - else: - caba_origin_inv_type = invoice_aml.move_id.move_type - + # Cash basis entries are always treated as misc operations, applying the tag sign directly to the balance + type_multiplicator = 1 else: - caba_origin_inv_type = record.move_id.move_type + type_multiplicator = (record.journal_id.type == 'sale' and -1 or 1) * (record.move_id.move_type in ('in_refund', 'out_refund') and -1 or 1) - tag_amount = (tag.tax_negate and -1 or 1) * (caba_origin_inv_type in record.move_id.get_inbound_types() and -1 or 1) * record.balance + tag_amount = type_multiplicator * (tag.tax_negate and -1 or 1) * record.balance if tag.tax_report_line_ids: #Then, the tag comes from a report line, and hence has a + or - sign (also in its name) @@ -4716,3 +4686,32 @@ class AccountMoveLine(models.Model): if self.payment_id: domains.append([('res_model', '=', 'account.payment'), ('res_id', '=', self.payment_id.id)]) return domains + + def _convert_tags_for_cash_basis(self, tags): + """ Cash basis entries are managed by the tax report just like misc operations. + So it means that the tax report will not apply any additional multiplicator + to the balance of the cash basis lines. + + For invoices move lines whose multiplicator would have been -1 (if their + taxes had not CABA), it will hence cause sign inversion if we directly copy + the tags from those lines. Instead, we need to invert all the signs from these + tags (if they come from tax report lines; tags created in data for financial + reports will stay onchanged). + """ + self.ensure_one() + tax_multiplicator = (self.journal_id.type == 'sale' and -1 or 1) * (self.move_id.move_type in ('in_refund', 'out_refund') and -1 or 1) + if tax_multiplicator == -1: + # Take the opposite tags instead + rslt = self.env['account.account.tag'] + for tag in tags: + if tag.tax_report_line_ids: + # tag created by an account.tax.report.line + new_tag = tag.tax_report_line_ids[0].tag_ids.filtered(lambda x: x.tax_negate != tag.tax_negate) + rslt += new_tag + else: + # tag created in data for use by an account.financial.html.report.line + rslt += tag + + return rslt + + return tags