[FIX] point_of_sale: fix loading of chart of account with default pos.config

With `generic_coa` now installed by default, the default pos.config are
automatically assigned a sale and an invoice journal, and trying to install
afterward a country specific chart template result in the following error:

2023-04-03 16:29:27,642 9344 ERROR _testdb-162820-testdb-162820-point-of-sale odoo.sql_db: bad query: DELETE FROM "account_journal" WHERE id IN (1, 2, 3, 8, 4, 6, 9, 7, 5, 10)
ERROR: update or delete on table "account_journal" violates foreign key constraint "pos_config_journal_id_fkey" on table "pos_config"
DETAIL:  Key (id)=(10) is still referenced from table "pos_config".

Traceback (most recent call last):
  ...
  File "/home/odoo/src/odoo/saas-16.2/odoo/modules/loading.py", line 481, in load_modules
    processed_modules += load_marked_modules(env, graph,
  File "/home/odoo/src/odoo/saas-16.2/odoo/modules/loading.py", line 365, in load_marked_modules
    loaded, processed = load_module_graph(
  File "/home/odoo/src/odoo/saas-16.2/odoo/modules/loading.py", line 297, in load_module_graph
    module.write({'state': 'installed', 'latest_version': ver})
  File "/home/odoo/src/odoo/saas-16.2/addons/account/models/ir_module.py", line 67, in write
    self.env['account.chart.template'].try_loading(
  File "/home/odoo/src/odoo/saas-16.2/addons/account/models/chart_template.py", line 141, in try_loading
    return self._load(template_code, company, install_demo)
  File "/home/odoo/src/odoo/saas-16.2/addons/point_of_sale/models/chart_template.py", line 16, in _load
    result = super()._load(template_code, company, install_demo)
  File "/home/odoo/src/odoo/saas-16.2/addons/account/models/chart_template.py", line 176, in _load
    self.env[model].search([('company_id', '=', company.id)]).with_context({MODULE_UNINSTALL_FLAG: True}).unlink()
  File "/home/odoo/src/odoo/saas-16.2/addons/account/models/account_journal.py", line 524, in unlink
    ret = super(AccountJournal, self).unlink()
  File "/home/odoo/src/odoo/saas-16.2/addons/mail/models/mail_thread.py", line 326, in unlink
    res = super(MailThread, self).unlink()
  File "/home/odoo/src/odoo/saas-16.2/addons/mail/models/mail_activity_mixin.py", line 246, in unlink
    result = super(MailActivityMixin, self).unlink()
  File "/home/odoo/src/odoo/saas-16.2/odoo/models.py", line 3635, in unlink
    cr.execute(query, (sub_ids,))
  File "/home/odoo/src/odoo/saas-16.2/odoo/sql_db.py", line 311, in execute
    res = self._obj.execute(query, params)
psycopg2.errors.ForeignKeyViolation: update or delete on table "account_journal" violates foreign key constraint "pos_config_journal_id_fkey" on table "pos_config"
DETAIL:  Key (id)=(10) is still referenced from table "pos_config".

This commit also reset the journals on the existing pos.config so that
`post_install_pos_localisation()` assign the right ones once the new
chart template is loaded.

We also only reset payment methods and journals in case we load a new
chart of accounts (this fix the "reload" of the CoA when PoS is
installed)

closes odoo/odoo#117651

X-original-commit: 7a91c57f6bb47e40f0486ca06f0e1916b98db1f4
Signed-off-by: Joseph Caburnay (jcb) <jcb@odoo.com>
Signed-off-by: Xavier Alt (xal) <xal@odoo.com>
This commit is contained in:
Xavier ALT
2023-04-04 17:03:32 +02:00
parent f91782279a
commit 739aa00a49
+10 -4
View File
@@ -5,14 +5,20 @@ from odoo import models
class AccountChartTemplate(models.AbstractModel):
_inherit = 'account.chart.template'
def _load(self, template_code=False, company=False, install_demo=True):
"""Remove the payment methods that are created for the company before installing the chart of accounts.
def _load(self, template_code, company, install_demo):
"""Remove the payment methods that are created for the company and unset journals before installing the chart of accounts.
Keeping these existing pos.payment.method records interferes with the installation of chart of accounts
Keeping these existing pos.payment.method records and pos.config journals interferes with the installation of chart of accounts
because pos.payment.method model has fields linked to account.journal and account.account records that are
deleted during the loading of chart of accounts.
"""
self.env['pos.payment.method'].search([('company_id', '=', company.id)]).unlink()
reload_template = template_code == company.chart_template
if not reload_template:
self.env['pos.payment.method'].search([('company_id', '=', company.id)]).unlink()
self.env["pos.config"].search([('company_id', '=', company.id)]).write({
'journal_id': False,
'invoice_journal_id': False,
})
result = super()._load(template_code, company, install_demo)
self.env['pos.config'].post_install_pos_localisation(companies=company)
return result