-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX]hs_code_link: properly declare OCA hs_code field on product.temp…
…late Before the change, the field was not being updated after the value of the H.S. Code was changed on a Product Template. The related stored field was not working fine with the company dependent field. As the `hs_code_id` field is company dependent, the related `hs_code` value should also be like this and sync the value based on the previous one.
- Loading branch information
1 parent
6b7d414
commit 70bb56b
Showing
7 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright 2023 ForgeFlow <http://www.forgeflow.com> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
import logging | ||
|
||
from openupgradelib import openupgrade | ||
|
||
from odoo import SUPERUSER_ID | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def _fill_columns(env): | ||
_logger.info("Filling hs_code Company Property") | ||
field_name = "hs_code" | ||
template_model = env["ir.model"].search( | ||
[("model", "=", "product.template")], limit=1 | ||
) | ||
field = env["ir.model.fields"].search( | ||
[("name", "=", field_name), ("model_id", "=", template_model.id)], limit=1 | ||
) | ||
if not field: | ||
_logger.info("Field %s not found." % (field_name)) | ||
return False | ||
insert_query = """ | ||
WITH query AS ( | ||
SELECT | ||
ip.company_id AS company_id, | ||
pt.id AS product_tmpl_id, | ||
SUBSTRING(hc.local_code, 0, 7) AS value_text | ||
FROM ir_property ip | ||
JOIN product_template pt ON | ||
CAST(SUBSTRING(ip.res_id, 18, 30) AS INTEGER) = pt.id | ||
JOIN hs_code hc ON | ||
CAST(SUBSTRING(ip.value_reference, 9, 20) AS INTEGER) = hc.id | ||
WHERE | ||
ip.name = 'hs_code_id' | ||
AND ip.res_id ILIKE 'product.template,%' | ||
) | ||
INSERT INTO ir_property ( | ||
name, | ||
res_id, | ||
company_id, | ||
fields_id, | ||
value_text, | ||
type, | ||
create_date, | ||
write_date, | ||
create_uid, | ||
write_uid | ||
) | ||
SELECT | ||
'hs_code', | ||
'product.template,' || CAST(product_tmpl_id AS VARCHAR), | ||
company_id, | ||
{}, | ||
value_text, | ||
'char', | ||
now(), | ||
now(), | ||
{}, | ||
{} | ||
FROM query; | ||
""".format( | ||
field.id, SUPERUSER_ID, SUPERUSER_ID | ||
) | ||
env.cr.execute(insert_query) | ||
|
||
|
||
@openupgrade.migrate() | ||
def migrate(env, version): | ||
_fill_columns(env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from . import product | ||
from . import ir_property | ||
from . import hs_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright 2023 ForgeFlow <http://www.forgeflow.com> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
from odoo import api, models | ||
|
||
|
||
class HSCode(models.Model): | ||
_inherit = "hs.code" | ||
|
||
@api.model | ||
def _get_fields_to_sync_from_hs_code_id(self): | ||
return ["hs_code"] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2023 ForgeFlow <http://www.forgeflow.com> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). | ||
|
||
from odoo import api, models | ||
|
||
|
||
class IrProperty(models.Model): | ||
_inherit = "ir.property" | ||
|
||
@api.model | ||
def set_multi(self, name, model, values, default_value=None): | ||
""" | ||
When the H.S. Code instance is updated on the Product Template, we also update | ||
the H.S. Code (6 digits code) value | ||
""" | ||
result = super().set_multi(name, model, values, default_value=default_value) | ||
if name == "hs_code_id" and model == "product.template": | ||
HSCode = self.env["hs.code"] | ||
fields_to_sync = HSCode._get_fields_to_sync_from_hs_code_id() | ||
values = dict() | ||
for field_name in fields_to_sync: | ||
for rec_id, rec_val in values.items(): | ||
val = False | ||
if isinstance(rec_val, int): | ||
val = ( | ||
HSCode.browse(rec_val).exists() | ||
and HSCode.browse(rec_val)[field_name] | ||
) | ||
elif ( | ||
isinstance(rec_val, models.BaseModel) | ||
and rec_val._name == "hs.code" | ||
): | ||
val = rec_val[field_name] | ||
values.update({rec_id: val}) | ||
self.set_multi("hs_code", model, values) | ||
return result | ||
|
||
def unlink(self): | ||
to_delete_ids = self.ids | ||
for p_id in self.ids: | ||
p = self.browse(p_id) | ||
if p.exists() and p.name == "hs_code_id" and "product.template" in p.res_id: | ||
r_p = self.search( | ||
[("res_id", "=", p.res_id), ("name", "=", "hs_code")], limit=1 | ||
) | ||
to_delete_ids.append(r_p.id) | ||
self = self.browse(to_delete_ids) | ||
return super(IrProperty, self).unlink() | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
* Denis Leemann <[email protected]> | ||
* Guillem Casassas <[email protected]> |