|
| 1 | +# Copyright 2023 Akretion France (http://www.akretion.com/) |
| 2 | +# @author: Alexis de Lattre <alexis.delattre@akretion.com> |
| 3 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 4 | + |
| 5 | +from odoo import _, api, fields, models |
| 6 | +from odoo.exceptions import ValidationError |
| 7 | + |
| 8 | + |
| 9 | +class AccountJournal(models.Model): |
| 10 | + _inherit = "account.journal" |
| 11 | + |
| 12 | + donation_debit_order_account_id = fields.Many2one( |
| 13 | + "account.account", |
| 14 | + check_company=True, |
| 15 | + copy=False, |
| 16 | + ondelete="restrict", |
| 17 | + domain="[('reconcile', '=', True), ('deprecated', '=', False), " |
| 18 | + "('company_id', '=', company_id), " |
| 19 | + "('account_type', '=', 'asset_receivable'), " |
| 20 | + "('id', 'not in', (default_account_id, suspense_account_id))]", |
| 21 | + string="Donation by Debit Order Account", |
| 22 | + help="Transfer account for donations by debit order. " |
| 23 | + "Leave empty if you don't handle donations by debit order on this bank account." |
| 24 | + "This account must be a receivable account, otherwise the debit order will not work.", |
| 25 | + ) |
| 26 | + |
| 27 | + @api.constrains("donation_debit_order_account_id") |
| 28 | + def _check_donation_accounts(self): |
| 29 | + acc_type2label = dict( |
| 30 | + self.env["account.account"].fields_get("account_type", "selection")[ |
| 31 | + "account_type" |
| 32 | + ]["selection"] |
| 33 | + ) |
| 34 | + for journal in self: |
| 35 | + ddo_account = journal.donation_debit_order_account_id |
| 36 | + if ddo_account: |
| 37 | + if not ddo_account.reconcile: |
| 38 | + raise ValidationError( |
| 39 | + _( |
| 40 | + "The Donation by Debit Order Account of journal " |
| 41 | + "'%(journal)s' must be reconciliable, but the account " |
| 42 | + "'%(account)s' is not reconciliable.", |
| 43 | + journal=journal.display_name, |
| 44 | + account=ddo_account.display_name, |
| 45 | + ) |
| 46 | + ) |
| 47 | + if ddo_account.account_type != "asset_receivable": |
| 48 | + raise ValidationError( |
| 49 | + _( |
| 50 | + "The Donation by Debit Order Account of journal " |
| 51 | + "'%(journal)s' must be a receivable account, " |
| 52 | + "but the account '%(account)s' is configured with " |
| 53 | + "account type '%(account_type)s'.", |
| 54 | + journal=journal.display_name, |
| 55 | + account=ddo_account.display_name, |
| 56 | + account_type=acc_type2label[ddo_account.account_type], |
| 57 | + ) |
| 58 | + ) |
0 commit comments