summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--addons/l10n_latam_invoice_document/models/account_move.py33
-rw-r--r--addons/l10n_latam_invoice_document/models/account_move_line.py8
2 files changed, 41 insertions, 0 deletions
diff --git a/addons/l10n_latam_invoice_document/models/account_move.py b/addons/l10n_latam_invoice_document/models/account_move.py
index f3479d90c7f..d993288cb37 100644
--- a/addons/l10n_latam_invoice_document/models/account_move.py
+++ b/addons/l10n_latam_invoice_document/models/account_move.py
@@ -4,12 +4,45 @@ from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError
import re
from odoo.tools.misc import formatLang
+from odoo.tools.sql import column_exists, create_column
class AccountMove(models.Model):
_inherit = "account.move"
+ def _auto_init(self):
+ # Skip the computation of the field `l10n_latam_document_type_id` at the module installation
+ # Without this, at the module installation,
+ # it would call `_compute_l10n_latam_document_type` on all existing records
+ # which can take quite a while if you already have a lot of moves. It can even fail with a MemoryError.
+ # In addition, it sets `_compute_l10n_latam_document_type = False` on all records
+ # because this field depends on the many2many `l10n_latam_available_document_type_ids`,
+ # which relies on having records for the model `l10n_latam.document.type`
+ # which only happens once the according localization module is loaded.
+ # The localization module is loaded afterwards, because the localization module depends on this module,
+ # (e.g. `l10n_cl` depends on `l10n_latam_invoice_document`, and therefore `l10n_cl` is loaded after)
+ # and therefore there are no records for the model `l10n_latam.document.type` at the time this fields
+ # gets computed on installation. Hence, all records' `_compute_l10n_latam_document_type` are set to `False`.
+ # In addition, multiple localization module depends on this module (e.g. `l10n_cl`, `l10n_ar`)
+ # So, imagine `l10n_cl` gets installed first, and then `l10n_ar` is installed next,
+ # if `l10n_latam_document_type_id` needed to be computed on install,
+ # the install of `l10n_cl` would call the compute method,
+ # because `l10n_latam_invoice_document` would be installed at the same time,
+ # but then `l10n_ar` would miss it, because `l10n_latam_invoice_document` would already be installed.
+ # Besides, this field is computed only for drafts invoices, as stated in the compute method:
+ # `for rec in self.filtered(lambda x: x.state == 'draft'):`
+ # So, if we want this field to be computed on install, it must be done only on draft invoices, and only once
+ # the localization modules are loaded.
+ # It should be done in a dedicated post init hook,
+ # filtering correctly the invoices for which it must be computed.
+ # Though I don't think this is needed.
+ # In practical, it's very rare to already have invoices (draft, in addition)
+ # for a Chilian or Argentian company (`res.company`) before installing `l10n_cl` or `l10n_ar`.
+ if not column_exists(self.env.cr, "account_move", "l10n_latam_document_type_id"):
+ create_column(self.env.cr, "account_move", "l10n_latam_document_type_id", "int4")
+ return super()._auto_init()
+
l10n_latam_amount_untaxed = fields.Monetary(compute='_compute_l10n_latam_amount_and_taxes')
l10n_latam_tax_ids = fields.One2many(compute="_compute_l10n_latam_amount_and_taxes", comodel_name='account.move.line')
l10n_latam_available_document_type_ids = fields.Many2many('l10n_latam.document.type', compute='_compute_l10n_latam_available_document_types')
diff --git a/addons/l10n_latam_invoice_document/models/account_move_line.py b/addons/l10n_latam_invoice_document/models/account_move_line.py
index 4e7db32a5b7..a0dbeb75179 100644
--- a/addons/l10n_latam_invoice_document/models/account_move_line.py
+++ b/addons/l10n_latam_invoice_document/models/account_move_line.py
@@ -1,12 +1,20 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models, api, fields
+from odoo.tools.sql import column_exists, create_column
class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
+ def _auto_init(self):
+ # Skip the computation of the field `l10n_latam_document_type_id` at the module installation
+ # See `_auto_init` in `l10n_latam_invoice_document/models/account_move.py` for more information
+ if not column_exists(self.env.cr, "account_move_line", "l10n_latam_document_type_id"):
+ create_column(self.env.cr, "account_move_line", "l10n_latam_document_type_id", "int4")
+ return super()._auto_init()
+
l10n_latam_document_type_id = fields.Many2one(
related='move_id.l10n_latam_document_type_id', auto_join=True, store=True, index=True)
l10n_latam_price_unit = fields.Monetary(compute='compute_l10n_latam_prices_and_taxes')