From db3e40396232f41d86ec0c5f90e783e77f3e9510 Mon Sep 17 00:00:00 2001 From: Nicolas Bayet Date: Tue, 24 Jul 2018 15:56:22 +0200 Subject: [PATCH] [IMP] account: Try to increment account code on duplication Purpose ======= Instead of adding 'copy' on the code, we could try to increment the code by 10 if possible. Specification ============= On an account duplication, if the code represents an Integer and if there are no other account with the code 'code + 10', then increment the account code by 10. Otherwise, use the default behavior (adding a 'copy' on the code label). TaskID: 1868221 --- addons/account/models/account.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/addons/account/models/account.py b/addons/account/models/account.py index 9c8bdc6bfeb..868853bf2f4 100644 --- a/addons/account/models/account.py +++ b/addons/account/models/account.py @@ -224,7 +224,18 @@ class AccountAccount(models.Model): @api.returns('self', lambda value: value.id) def copy(self, default=None): default = dict(default or {}) - default.setdefault('code', _("%s (copy)") % (self.code or '')) + if default.get('code', False): + return super(AccountAccount, self).copy(default) + try: + default['code'] = (str(int(self.code) + 10) or '') + default.setdefault('name', _("%s (copy)") % (self.name or '')) + if self.env['account.account'].search([('code', '=', default['code']), + ('company_id', '=', default.get('company_id', False) or self.company_id.id)], limit=1): + default['code'] = _("%s (copy)") % (self.code or '') + default['name'] = _("%s (copy)") % (self.name or '') + except ValueError: + default['code'] = _("%s (copy)") % (self.code or '') + default['name'] = self.name return super(AccountAccount, self).copy(default) @api.model