From 01ad82e911e6e490fd4a1ae5c8d536c71035b690 Mon Sep 17 00:00:00 2001 From: Martin Quinteros Date: Thu, 7 Sep 2023 12:00:56 -0300 Subject: [PATCH] [IMP] now after modifying a fixed tax through the wizard, it keeps the total amount of the tax groups even if we change the information of the invoice lines. --- account_invoice_tax/__init__.py | 1 + account_invoice_tax/__manifest__.py | 5 ++ account_invoice_tax/models/__init__.py | 2 + account_invoice_tax/models/account_move.py | 21 +++++++++ account_invoice_tax/models/account_tax.py | 47 +++++++++++++++++++ .../static/src/xml/tax_totals.xml | 13 +++++ .../views/account_move_view.xml | 2 +- .../wizards/account_invoice_tax.py | 13 ++++- 8 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 account_invoice_tax/models/__init__.py create mode 100644 account_invoice_tax/models/account_move.py create mode 100644 account_invoice_tax/models/account_tax.py create mode 100644 account_invoice_tax/static/src/xml/tax_totals.xml diff --git a/account_invoice_tax/__init__.py b/account_invoice_tax/__init__.py index 0fa2dde6..0c98ede6 100644 --- a/account_invoice_tax/__init__.py +++ b/account_invoice_tax/__init__.py @@ -1,2 +1,3 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from . import wizards +from . import models diff --git a/account_invoice_tax/__manifest__.py b/account_invoice_tax/__manifest__.py index ac760cec..7c784c85 100644 --- a/account_invoice_tax/__manifest__.py +++ b/account_invoice_tax/__manifest__.py @@ -11,6 +11,11 @@ 'views/account_move_view.xml', 'security/ir.model.access.csv', ], + 'assets': { + 'web.assets_backend': [ + 'account_invoice_tax/static/src/xml/**/*', + ], + }, 'license': 'AGPL-3', 'installable': True, 'auto_install': False, diff --git a/account_invoice_tax/models/__init__.py b/account_invoice_tax/models/__init__.py new file mode 100644 index 00000000..1680d728 --- /dev/null +++ b/account_invoice_tax/models/__init__.py @@ -0,0 +1,2 @@ +from . import account_move +from . import account_tax diff --git a/account_invoice_tax/models/account_move.py b/account_invoice_tax/models/account_move.py new file mode 100644 index 00000000..f7cf6275 --- /dev/null +++ b/account_invoice_tax/models/account_move.py @@ -0,0 +1,21 @@ +############################################################################## +# For copyright and license notices, see __manifest__.py file in module root +# directory +############################################################################## +from odoo import models, fields, api +import logging + +_logger = logging.getLogger(__name__) + + +class AccountMove(models.Model): + + _inherit = "account.move" + + + def _compute_tax_totals(self): + """ Computed field used for custom widget's rendering. + Only set on invoices. + """ + for move in self: + super(AccountMove, move.with_context(tax_total_origin=move._origin.tax_totals))._compute_tax_totals() diff --git a/account_invoice_tax/models/account_tax.py b/account_invoice_tax/models/account_tax.py new file mode 100644 index 00000000..7194cf4a --- /dev/null +++ b/account_invoice_tax/models/account_tax.py @@ -0,0 +1,47 @@ +############################################################################## +# For copyright and license notices, see __manifest__.py file in module root +# directory +############################################################################## +from odoo import models, fields, api +from odoo.tools.misc import formatLang + +import logging + +_logger = logging.getLogger(__name__) + + +class AccountTax(models.Model): + + _inherit = "account.tax" + + + @api.model + def _prepare_tax_totals(self, base_lines, currency, tax_lines=None): + totals = super()._prepare_tax_totals(base_lines, currency, tax_lines) + ##recorrer totals y si comple on la condicion y esta en self.env._context.get('tax_total_origin') + tax_total_origin = self.env.context.get('tax_total_origin') + if tax_total_origin: + for subtotals_order in totals['subtotals_order']: + for subtotals_index in range(0,len(totals['groups_by_subtotal'][subtotals_order])): + id_fixed = all([x.amount_type == 'fixed' and x.type_tax_use == 'purchase' for x in self.env['account.tax'].search([('tax_group_id' ,'=', totals['groups_by_subtotal'][subtotals_order][subtotals_index]['tax_group_id'])])]) + exist = [x for x in tax_total_origin['groups_by_subtotal'][subtotals_order] if x['tax_group_id'] == totals['groups_by_subtotal'][subtotals_order][subtotals_index]['tax_group_id']] + + if id_fixed and exist: + totals['groups_by_subtotal'][subtotals_order][subtotals_index] = exist[0] + + + subtotals = [] + amount_tax = 0 + for subtotal_title in totals['subtotals_order']: + amount_total = totals['amount_untaxed'] + amount_tax + subtotals.append({ + 'name': subtotal_title, + 'amount': amount_total, + 'formatted_amount': formatLang(self.env, amount_total, currency_obj=currency), + }) + amount_tax += sum(x['tax_group_amount'] for x in totals['groups_by_subtotal'][subtotal_title]) + + amount_total = totals['amount_untaxed'] + amount_tax + + totals['amount_tax'] = amount_tax + return totals diff --git a/account_invoice_tax/static/src/xml/tax_totals.xml b/account_invoice_tax/static/src/xml/tax_totals.xml new file mode 100644 index 00000000..18165d99 --- /dev/null +++ b/account_invoice_tax/static/src/xml/tax_totals.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/account_invoice_tax/views/account_move_view.xml b/account_invoice_tax/views/account_move_view.xml index 2bf6f035..4e070025 100644 --- a/account_invoice_tax/views/account_move_view.xml +++ b/account_invoice_tax/views/account_move_view.xml @@ -8,7 +8,7 @@
-
diff --git a/account_invoice_tax/wizards/account_invoice_tax.py b/account_invoice_tax/wizards/account_invoice_tax.py index 10bb66f4..6fb7d986 100644 --- a/account_invoice_tax/wizards/account_invoice_tax.py +++ b/account_invoice_tax/wizards/account_invoice_tax.py @@ -24,7 +24,16 @@ def default_get(self, fields): def onchange_move_id(self): taxes = self.env['account.tax'].search([]) if self.type_operation == 'add' else self.move_id.mapped( 'invoice_line_ids.tax_ids') - return {'domain': {'tax_id': [('id', 'in', taxes.ids), ('company_id', '=', self.move_id.company_id.id)]}} + leaf = [('id', 'in', taxes.ids), ('type_tax_use', '=', 'purchase') ,('company_id', '=', self.move_id.company_id.id)] + if self.env.context.get('group_id'): + leaf += [('tax_group_id', '=', self.env.context.get('group_id'))] + return {'domain': {'tax_id': leaf}} + + @api.onchange('tax_id') + def onchange_tax_id(self): + tax_line = self.move_id.line_ids.filtered(lambda x: x.tax_line_id and x.tax_line_id.id == self.tax_id.id) + if tax_line: + self.amount = tax_line.balance def _get_amount_updated_values(self): debit = credit = 0 @@ -71,7 +80,6 @@ def add_tax(self): 'debit': line.debit, 'credit': line.credit, } for line in move.line_ids.filtered(lambda x: x.tax_repartition_line_id.tax_id.amount_type == 'fixed')} - # al crear la linea de impuesto no queda balanceado porque no recalcula las lineas AP/AR # por eso pasamos check_move_validity container = {'records': move} @@ -109,3 +117,4 @@ def remove_tax(self): for tax_line in move_id.line_ids.filtered( lambda x: x.tax_repartition_line_id.tax_id in fixed_taxes_bu and x.tax_repartition_line_id.tax_id.amount_type == 'fixed'): tax_line.write(fixed_taxes_bu.get(tax_line.tax_line_id)) +