|
| 1 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 2 | + |
| 3 | +from odoo import _, api, exceptions, fields, models |
| 4 | + |
| 5 | + |
| 6 | +class AccountMoveLine(models.Model): |
| 7 | + _inherit = "account.move.line" |
| 8 | + |
| 9 | + analytic_account_id = fields.Many2one( |
| 10 | + "account.analytic.account", string="Analytic Account", index="btree" |
| 11 | + ) |
| 12 | + |
| 13 | + def _check_analytic_simple_required_msg(self): |
| 14 | + self.ensure_one() |
| 15 | + company_cur = self.company_currency_id |
| 16 | + if company_cur.is_zero(self.debit) and company_cur.is_zero(self.credit): |
| 17 | + return None |
| 18 | + analytic_policy = self.account_id._get_analytic_simple_policy() |
| 19 | + if analytic_policy == "always" and not self.analytic_account_id: |
| 20 | + return _( |
| 21 | + "Analytic policy is set to 'Always' with account " |
| 22 | + "'%(account)s' but the analytic account is missing in " |
| 23 | + "the account move line with label '%(move)s'." |
| 24 | + ) % { |
| 25 | + "account": self.account_id.display_name, |
| 26 | + "move": self.name or "", |
| 27 | + } |
| 28 | + elif analytic_policy == "never" and (self.analytic_account_id): |
| 29 | + analytic_account = self.analytic_account_id |
| 30 | + return _( |
| 31 | + "Analytic policy is set to 'Never' with account " |
| 32 | + "'%(account)s' but the account move line with label '%(move)s' " |
| 33 | + "has an analytic account '%(analytic_account)s'." |
| 34 | + ) % { |
| 35 | + "account": self.account_id.display_name, |
| 36 | + "move": self.name or "", |
| 37 | + "analytic_account": analytic_account.name, |
| 38 | + } |
| 39 | + elif ( |
| 40 | + analytic_policy == "posted" |
| 41 | + and not self.analytic_account_id |
| 42 | + and self.move_id.state == "posted" |
| 43 | + ): |
| 44 | + return _( |
| 45 | + "Analytic policy is set to 'Posted moves' with " |
| 46 | + "account '%(account)s' but the analytic account is missing " |
| 47 | + "in the account move line with label '%(move)s'." |
| 48 | + ) % { |
| 49 | + "account": self.account_id.display_name, |
| 50 | + "move": self.name or "", |
| 51 | + } |
| 52 | + return None |
| 53 | + |
| 54 | + @api.constrains("analytic_account_id", "account_id", "debit", "credit") |
| 55 | + def _check_analytic_simple_required(self): |
| 56 | + for rec in self: |
| 57 | + message = rec._check_analytic_simple_required_msg() |
| 58 | + if message: |
| 59 | + raise exceptions.ValidationError(message) |
0 commit comments