[FIX] account: match journal by type and name

When upgrading and loading data from templates, there could
be journals that match our target but the code is modified by
the users. The code may be different due to translations or simply
a user choice, but the journal can be the standard one. And the
noupdate=True that is on the xmlids of the created journals
confirm that it is possible for the users to keep their
customizations on the journals.
Matching the journals by type and name can be an alternative
to creating a duplicate journal, which will cause more conflict
down the line (for ex. the unique constraint on mail aliases)

closes odoo/odoo#128139

X-original-commit: 069714ee8e7175ab92a4d28b75e3725ea0be1b0f
Signed-off-by: William André (wan) <wan@odoo.com>
Signed-off-by: Jinane Maksoud (maji) <maji@odoo.com>
This commit is contained in:
Jinane Maksoud
2023-07-13 02:21:45 +02:00
parent 6533c081fb
commit a4a94fa133
+13 -5
View File
@@ -218,11 +218,19 @@ class AccountChartTemplate(models.AbstractModel):
for xmlid, journal_data in list(data.get('account.journal', {}).items()):
if self.ref(xmlid, raise_if_not_found=False):
del data['account.journal'][xmlid]
elif 'code' in journal_data:
journal = self.env['account.journal'].with_context(active_test=False).search([
('code', '=', journal_data['code']),
('company_id', '=', company.id),
])
else:
if 'code' in journal_data:
journal = self.env['account.journal'].with_context(active_test=False).search([
('code', '=', journal_data['code']),
('company_id', '=', company.id),
])
# Try to match by journal name to avoid conflict in the unique constraint on the mail alias
if not journal and 'name' in journal_data and 'type' in journal_data:
journal = self.env['account.journal'].with_context(active_test=False).search([
('type', '=', journal_data['type']),
('name', '=', journal_data['name']),
('company_id', '=', company.id),
], limit=1)
if journal:
del data['account.journal'][xmlid]
self.env['ir.model.data']._update_xmlids([{