|
| 1 | +from contextlib import contextmanager |
| 2 | + |
| 3 | +from odoo import fields, models |
| 4 | + |
| 5 | + |
| 6 | +class AccountMove(models.Model): |
| 7 | + _inherit = "account.move" |
| 8 | + |
| 9 | + # Stores manually-overridden tax amounts keyed by tax id (as string). |
| 10 | + # Structure: { "<tax_id>": {"amount": float, "rate": float} } |
| 11 | + # Only fixed-amount taxes are stored here; percentage taxes are always |
| 12 | + # recomputed automatically. |
| 13 | + tax_override_data = fields.Json() |
| 14 | + |
| 15 | + def sync_tax_override_from_tax_totals(self): |
| 16 | + """Rebuild ``tax_override_data`` from the current ``tax_totals`` widget value. |
| 17 | +
|
| 18 | + Iterates every tax group in ``tax_totals`` and, for each fixed-amount |
| 19 | + tax found, stores the displayed amounts as an override so they survive |
| 20 | + future recomputations. |
| 21 | +
|
| 22 | + ``tax_totals`` structure (relevant fields):: |
| 23 | +
|
| 24 | + { |
| 25 | + "subtotals": [{ |
| 26 | + "tax_groups": [{ |
| 27 | + "involved_tax_ids": [<tax_id>, ...], |
| 28 | + "tax_amount": <float>, # company currency |
| 29 | + }, ...] |
| 30 | + }, ...] |
| 31 | + } |
| 32 | +
|
| 33 | + ``amount`` (invoice-currency) → ``tax_amount_currency`` when the |
| 34 | + invoice is in a foreign currency, otherwise ``tax_amount``. |
| 35 | + ``amount_company_currency`` → ``tax_amount`` (always company currency), |
| 36 | + or 0.0 when the invoice is in company currency (field unused in that |
| 37 | + case, see ``_apply_tax_overrides``). |
| 38 | + """ |
| 39 | + for rec in self.filtered(lambda m: m.move_type in ("in_invoice", "in_refund", "in_receipt")): |
| 40 | + tax_totals = rec.tax_totals |
| 41 | + if not tax_totals: |
| 42 | + continue |
| 43 | + |
| 44 | + is_company_currency = rec.currency_id == rec.company_currency_id |
| 45 | + new_overrides = {} |
| 46 | + |
| 47 | + for subtotal in tax_totals.get("subtotals", []): |
| 48 | + for tax_group in subtotal.get("tax_groups", []): |
| 49 | + tax_amount = tax_group.get("tax_amount", 0.0) |
| 50 | + tax_amount_currency = tax_group.get("tax_amount_currency", 0.0) |
| 51 | + |
| 52 | + for tax_id in tax_group.get("involved_tax_ids", []): |
| 53 | + tax = rec.env["account.tax"].browse(tax_id) |
| 54 | + if not tax.exists() or tax.amount_type != "fixed": |
| 55 | + continue |
| 56 | + |
| 57 | + if is_company_currency: |
| 58 | + amount = tax_amount |
| 59 | + rate = 1 |
| 60 | + else: |
| 61 | + amount = tax_amount_currency |
| 62 | + rate = rec.invoice_currency_rate or 1.0 |
| 63 | + |
| 64 | + new_overrides[str(tax_id)] = { |
| 65 | + "amount": amount, |
| 66 | + "rate": rate, |
| 67 | + } |
| 68 | + |
| 69 | + rec.tax_override_data = new_overrides or False |
| 70 | + |
| 71 | + # ------------------------------------------------------------------ |
| 72 | + # Tax-totals widget: inject override amounts so the widget reflects |
| 73 | + # the manually-set values instead of the recomputed ones. |
| 74 | + # ------------------------------------------------------------------ |
| 75 | + |
| 76 | + def _compute_tax_totals(self): |
| 77 | + for move in self: |
| 78 | + overrides = move.tax_override_data or {} |
| 79 | + if not overrides: |
| 80 | + super(AccountMove, move)._compute_tax_totals() |
| 81 | + continue |
| 82 | + |
| 83 | + tax_context = { |
| 84 | + "tax_context": { |
| 85 | + int(tax_id): { |
| 86 | + "fixed_amount": (vals["amount"]), |
| 87 | + "rate": (move.invoice_currency_rate or 1.0), |
| 88 | + } |
| 89 | + for tax_id, vals in overrides.items() |
| 90 | + }, |
| 91 | + } |
| 92 | + super(AccountMove, move.with_context(**tax_context))._compute_tax_totals() |
| 93 | + |
| 94 | + # ------------------------------------------------------------------ |
| 95 | + # Re-apply overrides after every tax-line recomputation so that the |
| 96 | + # amounts set through the wizard survive any subsequent edits on the |
| 97 | + # invoice lines. |
| 98 | + # ------------------------------------------------------------------ |
| 99 | + |
| 100 | + @contextmanager |
| 101 | + def _sync_tax_lines(self, container): |
| 102 | + """Restore manually-set tax amounts after the core recomputes tax lines.""" |
| 103 | + with super()._sync_tax_lines(container): |
| 104 | + yield |
| 105 | + for move in container.get("records", self): |
| 106 | + move._apply_tax_overrides() |
| 107 | + |
| 108 | + def _apply_tax_overrides(self): |
| 109 | + """Re-write values from ``tax_override_data`` onto the matching tax lines. |
| 110 | +
|
| 111 | + Only overrides for fixed-amount taxes are applied; percentage-based |
| 112 | + taxes must always reflect their recomputed values. |
| 113 | + """ |
| 114 | + overrides = self.tax_override_data or {} |
| 115 | + if not overrides: |
| 116 | + return |
| 117 | + |
| 118 | + move_currency = self.currency_id |
| 119 | + company_currency = self.company_currency_id |
| 120 | + not_company_currency = move_currency and move_currency != company_currency |
| 121 | + for line in self.line_ids.filtered(lambda l: l.tax_line_id and str(l.tax_line_id.id) in overrides): |
| 122 | + vals = overrides[str(line.tax_line_id.id)] |
| 123 | + rate = line.move_id.invoice_currency_rate or 1.0 |
| 124 | + amount = vals.get("amount", 0.0) |
| 125 | + amount_cc = amount / rate |
| 126 | + debit = credit = debit_cc = credit_cc = 0.0 |
| 127 | + if self.move_type in ("in_invoice", "in_receipt"): |
| 128 | + if amount > 0: |
| 129 | + debit, debit_cc = amount, amount_cc |
| 130 | + elif amount < 0: |
| 131 | + credit, credit_cc = -amount, -amount_cc |
| 132 | + else: |
| 133 | + if amount > 0: |
| 134 | + credit, credit_cc = amount, amount_cc |
| 135 | + elif amount < 0: |
| 136 | + debit, debit_cc = -amount, -amount_cc |
| 137 | + |
| 138 | + line_vals = { |
| 139 | + "debit": debit_cc if not_company_currency else debit, |
| 140 | + "credit": credit_cc if not_company_currency else credit, |
| 141 | + "balance": ((amount_cc if not_company_currency else amount) * (1 if debit or debit_cc else -1)), |
| 142 | + } |
| 143 | + if not_company_currency and amount: |
| 144 | + line_vals["amount_currency"] = amount |
| 145 | + line.write(line_vals) |
0 commit comments