From 10dc2a4b9ae643a4a5d2a6d360ff57677a871520 Mon Sep 17 00:00:00 2001 From: Maximilien La Barre Date: Mon, 5 Sep 2022 13:54:47 +0000 Subject: [PATCH] [ADD] account: Unicity constraint on reco models 1- throw an error when creating a model with a name already taken 2- Duplicate action button create a new model with (copy) append to then name task_id: 2971488 closes odoo/odoo#99581 Signed-off-by: Olivier Colson (oco) --- .../account/models/account_reconcile_model.py | 13 +++++ addons/account/models/chart_template.py | 50 +++++++++---------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/addons/account/models/account_reconcile_model.py b/addons/account/models/account_reconcile_model.py index 0d2d67f8e39..33df0fd263f 100644 --- a/addons/account/models/account_reconcile_model.py +++ b/addons/account/models/account_reconcile_model.py @@ -226,6 +226,8 @@ class AccountReconcileModel(models.Model): _order = 'sequence, id' _check_company_auto = True + _sql_constraints = [('name_unique', 'unique(name, company_id)', 'A reconciliation model already bears this name.')] + # Base fields. active = fields.Boolean(default=True) name = fields.Char(string='Name', required=True) @@ -412,6 +414,17 @@ class AccountReconcileModel(models.Model): elif record.payment_tolerance_type == 'fixed_amount' and record.payment_tolerance_param < 0: raise ValidationError(_("A payment tolerance defined as an amount should always be higher than 0")) + @api.returns('self', lambda value: value.id) + def copy(self, default=None): + default = default or {} + if default.get('name'): + return super(AccountReconcileModel, self).copy(default) + name = _("%s (copy)", self.name) + while self.env['account.reconcile.model'].search([('name', '=', name)], limit=1): + name = _("%s (copy)", name) + default['name'] = name + return super(AccountReconcileModel, self).copy(default) + #################################################### # RECONCILIATION PROCESS #################################################### diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index 0a7280d0362..9c9066eb54a 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -805,32 +805,32 @@ class AccountChartTemplate(models.Model): self.create_record_with_xmlid(company, account_reconcile_model, 'account.reconcile.model', vals) # Create default rules for the reconciliation widget matching invoices automatically. + if not self.parent_id: + self.env['account.reconcile.model'].sudo().create({ + "name": _('Invoices/Bills Perfect Match'), + "sequence": '1', + "rule_type": 'invoice_matching', + "auto_reconcile": True, + "match_nature": 'both', + "match_same_currency": True, + "allow_payment_tolerance": True, + "payment_tolerance_type": 'percentage', + "payment_tolerance_param": 0, + "match_partner": True, + "company_id": company.id, + }) - self.env['account.reconcile.model'].sudo().create({ - "name": _('Invoices/Bills Perfect Match'), - "sequence": '1', - "rule_type": 'invoice_matching', - "auto_reconcile": True, - "match_nature": 'both', - "match_same_currency": True, - "allow_payment_tolerance": True, - "payment_tolerance_type": 'percentage', - "payment_tolerance_param": 0, - "match_partner": True, - "company_id": company.id, - }) - - self.env['account.reconcile.model'].sudo().create({ - "name": _('Invoices/Bills Partial Match if Underpaid'), - "sequence": '2', - "rule_type": 'invoice_matching', - "auto_reconcile": False, - "match_nature": 'both', - "match_same_currency": True, - "allow_payment_tolerance": False, - "match_partner": True, - "company_id": company.id, - }) + self.env['account.reconcile.model'].sudo().create({ + "name": _('Invoices/Bills Partial Match if Underpaid'), + "sequence": '2', + "rule_type": 'invoice_matching', + "auto_reconcile": False, + "match_nature": 'both', + "match_same_currency": True, + "allow_payment_tolerance": False, + "match_partner": True, + "company_id": company.id, + }) return True