|
1 | | -from odoo import models, fields, api |
| 1 | +from odoo import api, fields, models |
2 | 2 |
|
3 | 3 |
|
4 | 4 | class AccountMove(models.Model): |
5 | | - |
6 | 5 | _inherit = "account.move" |
7 | 6 |
|
| 7 | + tax_override_ids = fields.One2many( |
| 8 | + "account.move.tax.override", |
| 9 | + "move_id", |
| 10 | + string="Tax Overrides", |
| 11 | + copy=False, |
| 12 | + ) |
| 13 | + |
| 14 | + # ------------------------------------------------------------------ |
| 15 | + # Tax-totals widget: inject override amounts so the widget reflects |
| 16 | + # the manually-set values instead of the recomputed ones. |
| 17 | + # ------------------------------------------------------------------ |
| 18 | + |
8 | 19 | def _compute_tax_totals(self): |
9 | 20 | for move in self: |
10 | | - tax_context = {'inverse_invoice_currency_rate': move.inverse_invoice_currency_rate} |
11 | | - for line in move.line_ids.filtered(lambda x: x.tax_fixed_amount_in_currency): |
12 | | - #tax_context[line.tax_line_id.id] = line.tax_fixed_amount_in_currency |
13 | | - tax_context[line.tax_line_id.id] = { |
14 | | - 'fixed_amount':line.tax_fixed_amount_in_currency, |
15 | | - 'rate': line.tax_fixed_amount / line.tax_fixed_amount_in_currency |
16 | | - } |
17 | | - super(AccountMove, move.with_context(tax_context=tax_context))._compute_tax_totals() |
18 | | - |
19 | | - # def button_draft(self): |
20 | | - # for move in self: |
21 | | - # tax_context = {'inverse_invoice_currency_rate': move.inverse_invoice_currency_rate} |
22 | | - # for line in move.line_ids.filtered(lambda x: x.tax_fixed_amount_in_currency): |
23 | | - # tax_context[line.tax_line_id.id] = line.tax_fixed_amount_in_currency |
24 | | - # super(AccountMove, move.with_context(tax_context=tax_context)).button_draft() |
| 21 | + # Only fixed-amount tax overrides affect the totals widget; |
| 22 | + # percentage taxes are always recomputed. |
| 23 | + overrides = { |
| 24 | + o.tax_id.id: o |
| 25 | + for o in move.tax_override_ids |
| 26 | + if o.tax_id.amount_type == "fixed" |
| 27 | + } |
| 28 | + if not overrides: |
| 29 | + super(AccountMove, move)._compute_tax_totals() |
| 30 | + continue |
| 31 | + |
| 32 | + rate = getattr(move, "inverse_invoice_currency_rate", None) or 1.0 |
| 33 | + is_company_currency = move.currency_id == move.company_currency_id |
| 34 | + tax_context = { |
| 35 | + "inverse_invoice_currency_rate": rate, |
| 36 | + "tax_context": { |
| 37 | + tax_id: { |
| 38 | + # If the invoice is in company currency, amount_company_currency |
| 39 | + # is not stored (always 0); use `amount` directly with rate=1.0. |
| 40 | + # If the invoice is in a foreign currency, use amount_company_currency |
| 41 | + # as fixed_amount and derive the rate from both fields. |
| 42 | + "fixed_amount": ( |
| 43 | + override.amount |
| 44 | + if is_company_currency |
| 45 | + else override.amount_company_currency |
| 46 | + ), |
| 47 | + "rate": ( |
| 48 | + 1.0 |
| 49 | + if is_company_currency |
| 50 | + else ( |
| 51 | + override.amount_company_currency / override.amount |
| 52 | + if override.amount |
| 53 | + else 1.0 |
| 54 | + ) |
| 55 | + ), |
| 56 | + } |
| 57 | + for tax_id, override in overrides.items() |
| 58 | + }, |
| 59 | + } |
| 60 | + super(AccountMove, move.with_context(**tax_context))._compute_tax_totals() |
| 61 | + |
| 62 | + # ------------------------------------------------------------------ |
| 63 | + # Re-apply overrides after every tax-line recomputation so that the |
| 64 | + # amounts set through the wizard survive any subsequent edits on the |
| 65 | + # invoice lines. |
| 66 | + # ------------------------------------------------------------------ |
| 67 | + |
| 68 | + def _recompute_tax_lines( |
| 69 | + self, |
| 70 | + recompute_tax_base_amount=False, |
| 71 | + tax_rep_lines_to_recompute=None, |
| 72 | + ): |
| 73 | + """Restore manually-set tax amounts after each recomputation.""" |
| 74 | + super()._recompute_tax_lines( |
| 75 | + recompute_tax_base_amount=recompute_tax_base_amount, |
| 76 | + tax_rep_lines_to_recompute=tax_rep_lines_to_recompute, |
| 77 | + ) |
| 78 | + for move in self: |
| 79 | + move._apply_tax_overrides() |
| 80 | + |
| 81 | + def _apply_tax_overrides(self): |
| 82 | + """Re-write values from ``tax_override_ids`` onto the matching tax lines. |
| 83 | +
|
| 84 | + Only overrides for fixed-amount taxes are applied; percentage-based |
| 85 | + taxes must always reflect their recomputed values. |
| 86 | + """ |
| 87 | + if not self.tax_override_ids: |
| 88 | + return |
| 89 | + overrides = { |
| 90 | + o.tax_id: o |
| 91 | + for o in self.tax_override_ids |
| 92 | + if o.tax_id.amount_type == "fixed" |
| 93 | + } |
| 94 | + for line in self.line_ids.filtered(lambda l: l.tax_line_id in overrides): |
| 95 | + override = overrides[line.tax_line_id] |
| 96 | + line.write(override._get_line_values(self)) |
0 commit comments