|
| 1 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 2 | + |
| 3 | +from collections import defaultdict |
| 4 | + |
| 5 | +from odoo import _, exceptions, fields, models |
| 6 | + |
| 7 | + |
| 8 | +class AccountMove(models.Model): |
| 9 | + _inherit = "account.move" |
| 10 | + |
| 11 | + ecotax_move_id = fields.Many2one("account.move", readonly=True, copy=False) |
| 12 | + |
| 13 | + def _create_prepare_ecotax_move_vals(self): |
| 14 | + self.ensure_one() |
| 15 | + ecotax_journal_id = self.company_id.ecotax_journal_id.id |
| 16 | + if not ecotax_journal_id: |
| 17 | + raise exceptions.UserError( |
| 18 | + _( |
| 19 | + "Please configure the relative ecotax journal on the company settings" |
| 20 | + ) |
| 21 | + ) |
| 22 | + return { |
| 23 | + "journal_id": ecotax_journal_id, |
| 24 | + "move_type": "entry", |
| 25 | + "ref": _("Ecotaxe entry for %s") % self.name, |
| 26 | + "date": self.date, |
| 27 | + "currency_id": self.currency_id.id, |
| 28 | + "company_id": self.company_id.id, |
| 29 | + } |
| 30 | + |
| 31 | + def _manage_ecotax_isolation(self): |
| 32 | + self.ensure_one() |
| 33 | + ecotax_lines = self.invoice_line_ids.ecotax_line_ids |
| 34 | + account_ecotax_mapping = defaultdict(float) |
| 35 | + product_account_mapping = defaultdict(float) |
| 36 | + for ecotax_line in ecotax_lines: |
| 37 | + account = ( |
| 38 | + ecotax_line.classification_id.ecotax_account_id |
| 39 | + or self.company_id.ecotax_account_id |
| 40 | + ) |
| 41 | + if not account: |
| 42 | + continue |
| 43 | + account_ecotax_mapping[account] += ecotax_line.amount_total |
| 44 | + product_account_mapping[ |
| 45 | + ecotax_line.account_move_line_id.account_id |
| 46 | + ] += ecotax_line.amount_total |
| 47 | + if not account_ecotax_mapping: |
| 48 | + return self.env["account.move"] |
| 49 | + ecotax_move = self.ecotax_move_id |
| 50 | + if ecotax_move: |
| 51 | + if ecotax_move.state != "cancel": |
| 52 | + raise exceptions.ValidationError( |
| 53 | + _("The linked ecotax entry should be canceled.") |
| 54 | + ) |
| 55 | + ecotax_move.button_draft() |
| 56 | + ecotax_move.line_ids.unlink() |
| 57 | + else: |
| 58 | + vals = self._create_prepare_ecotax_move_vals() |
| 59 | + ecotax_move = self.create(vals) |
| 60 | + self.write({"ecotax_move_id": ecotax_move.id}) |
| 61 | + line_vals_list = [] |
| 62 | + for account, amount_curr in account_ecotax_mapping.items(): |
| 63 | + line_vals = { |
| 64 | + "name": "ecotax for %s" % self.name, |
| 65 | + "account_id": account.id, |
| 66 | + "currency_id": self.currency_id.id, |
| 67 | + "amount_currency": self.move_type == "out_invoice" |
| 68 | + and -amount_curr |
| 69 | + or amount_curr, |
| 70 | + "move_id": ecotax_move.id, |
| 71 | + # We never want tax lines here even if the account has a default |
| 72 | + # tax set |
| 73 | + "tax_ids": False, |
| 74 | + } |
| 75 | + line_vals_list.append(line_vals) |
| 76 | + for account, amount_curr in product_account_mapping.items(): |
| 77 | + line_vals = { |
| 78 | + "account_id": account.id, |
| 79 | + "currency_id": self.currency_id.id, |
| 80 | + "amount_currency": self.move_type == "out_invoice" |
| 81 | + and amount_curr |
| 82 | + or -amount_curr, |
| 83 | + "move_id": ecotax_move.id, |
| 84 | + # We never want tax lines here even if the account has a default |
| 85 | + # tax set |
| 86 | + "tax_ids": False, |
| 87 | + } |
| 88 | + line_vals_list.append(line_vals) |
| 89 | + ecotax_move.write({"line_ids": [(0, 0, vals) for vals in line_vals_list]}) |
| 90 | + ecotax_move.action_post() |
| 91 | + return ecotax_move |
| 92 | + |
| 93 | + def _post(self, soft=True): |
| 94 | + res = super()._post(soft=soft) |
| 95 | + for move in self: |
| 96 | + if move.move_type in ("out_invoice", "out_refund"): |
| 97 | + move._manage_ecotax_isolation() |
| 98 | + return res |
| 99 | + |
| 100 | + def button_draft(self): |
| 101 | + res = super().button_draft() |
| 102 | + if self.ecotax_move_id: |
| 103 | + self.ecotax_move_id.button_cancel() |
| 104 | + return res |
| 105 | + |
| 106 | + def button_cancel(self): |
| 107 | + res = super().button_cancel() |
| 108 | + if self.ecotax_move_id: |
| 109 | + self.ecotax_move_id.button_cancel() |
| 110 | + return res |
0 commit comments