Skip to content

Commit

Permalink
[IMP]product_harmonized_system: various improvements
Browse files Browse the repository at this point in the history
- extract `name_get` logic in another method to be inherited
- remove _sql_constraints and change it for an api constrains method to have a better control and so that there no global duplicates (those that are not company specific)
- replaced `replace` method for strip()
  • Loading branch information
GuillemCForgeFlow committed Oct 2, 2023
1 parent 6b7d414 commit e2716c1
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions product_harmonized_system/models/hs_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# @author Luc de Meyer <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class HSCode(models.Model):
Expand Down Expand Up @@ -74,32 +75,41 @@ def _compute_product_tmpl_count(self):
for code in self:
code.product_tmpl_count = len(code.product_tmpl_ids)

@api.depends("local_code", "description")
def _get_name(self):
self.ensure_one()
name = self.local_code
if self.description:
name += " " + self.description
return len(name) > 55 and name[:55] + "..." or name

def name_get(self):
res = []
for this in self:
name = this.local_code
if this.description:
name += " " + this.description
name = len(name) > 55 and name[:55] + "..." or name
name = this._get_name()
res.append((this.id, name))
return res

_sql_constraints = [
(
"local_code_company_uniq",
"unique(local_code, company_id)",
"This code already exists for this company !",
)
]
@api.constrains("local_code", "company_id")
def _check_duplicate_hs_code(self):
for rec in self:
domain = [("local_code", "=", rec.local_code), ("id", "!=", rec.id)]
if rec.company_id:
domain += [("company_id", "=", rec.company_id.id)]
dup = self.search_count(domain)
if dup:
msg = (
"There is already an existing H.S. Code with the specified Code: %s."
% rec.local_code
)
raise ValidationError(_(msg))

@api.model
def create(self, vals):
if vals.get("local_code"):
vals["local_code"] = vals["local_code"].replace(" ", "")
vals["local_code"] = vals["local_code"].strip()
return super().create(vals)

def write(self, vals):
if vals.get("local_code"):
vals["local_code"] = vals["local_code"].replace(" ", "")
vals["local_code"] = vals["local_code"].strip()
return super().write(vals)

0 comments on commit e2716c1

Please sign in to comment.