|
4 | 4 | # @author Luc de Meyer <info@noviat.com> |
5 | 5 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
6 | 6 |
|
7 | | -from textwrap import shorten |
8 | 7 |
|
9 | 8 | from odoo import api, fields, models |
10 | 9 |
|
11 | 10 |
|
12 | 11 | class HSCode(models.Model): |
13 | 12 | _name = "hs.code" |
14 | 13 | _description = "H.S. Code" |
15 | | - _order = "local_code" |
16 | | - _rec_name = "local_code" |
| 14 | + _order = "hs_code" |
| 15 | + _rec_name = "hs_code" |
17 | 16 |
|
18 | 17 | hs_code = fields.Char( |
19 | 18 | string="H.S. Code", |
20 | | - compute="_compute_hs_code", |
21 | | - store=True, |
22 | | - precompute=True, |
23 | 19 | help="Harmonized System code (6 digits). Full list is " |
24 | 20 | "available from the World Customs Organisation, see " |
25 | 21 | "http://www.wcoomd.org", |
26 | 22 | ) |
27 | | - description = fields.Char( |
28 | | - translate=True, help="Short text description of the H.S. category" |
29 | | - ) |
30 | | - local_code = fields.Char( |
31 | | - required=True, |
32 | | - help="Code used for the national Import/Export declaration. " |
33 | | - "The national code starts with the 6 digits of the H.S. and often " |
34 | | - "has a few additional digits to extend the H.S. code.", |
| 23 | + local_code_ids = fields.One2many( |
| 24 | + comodel_name="local.code", |
| 25 | + inverse_name="hs_code_id", |
| 26 | + string="Local Codes", |
35 | 27 | ) |
36 | 28 | active = fields.Boolean(default=True) |
37 | | - company_id = fields.Many2one( |
38 | | - "res.company", |
39 | | - string="Company", |
40 | | - default=lambda self: self._default_company_id(), |
41 | | - ) |
42 | 29 | product_categ_ids = fields.One2many( |
43 | 30 | comodel_name="product.category", |
44 | | - inverse_name="hs_code_id", |
| 31 | + inverse_name="local_code_id", |
45 | 32 | string="Product Categories", |
46 | 33 | readonly=True, |
47 | 34 | ) |
48 | 35 | product_tmpl_ids = fields.One2many( |
49 | 36 | comodel_name="product.template", |
50 | | - inverse_name="hs_code_id", |
| 37 | + inverse_name="local_code_id", |
51 | 38 | string="Products", |
52 | 39 | readonly=True, |
53 | 40 | ) |
54 | 41 | product_categ_count = fields.Integer(compute="_compute_product_categ_count") |
55 | 42 | product_tmpl_count = fields.Integer(compute="_compute_product_tmpl_count") |
56 | 43 |
|
57 | | - @api.model |
58 | | - def _default_company_id(self): |
59 | | - return False |
60 | | - |
61 | | - @api.depends("local_code") |
62 | | - def _compute_hs_code(self): |
63 | | - for this in self: |
64 | | - this.hs_code = this.local_code and this.local_code[:6] |
65 | | - |
66 | | - @api.depends("product_categ_ids") |
| 44 | + @api.depends("local_code_ids") |
67 | 45 | def _compute_product_categ_count(self): |
68 | | - rg_res = self.env["product.category"].read_group( |
69 | | - [("hs_code_id", "in", self.ids)], ["hs_code_id"], ["hs_code_id"] |
70 | | - ) |
71 | | - mapped_data = {x["hs_code_id"][0]: x["hs_code_id_count"] for x in rg_res} |
72 | | - for code in self: |
73 | | - code.product_categ_count = mapped_data.get(code.id, 0) |
| 46 | + ProductCategory = self.env["product.category"] |
| 47 | + for hs in self: |
| 48 | + local_code_ids = hs.local_code_ids.ids |
| 49 | + if local_code_ids: |
| 50 | + count = ProductCategory.search_count( |
| 51 | + [("local_code_id", "in", local_code_ids)] |
| 52 | + ) |
| 53 | + hs.product_categ_count = count |
| 54 | + else: |
| 55 | + hs.product_categ_count = 0 |
74 | 56 |
|
75 | | - @api.depends("product_tmpl_ids") |
| 57 | + @api.depends("local_code_ids") |
76 | 58 | def _compute_product_tmpl_count(self): |
77 | | - rg_res = self.env["product.template"].read_group( |
78 | | - [("hs_code_id", "in", self.ids)], ["hs_code_id"], ["hs_code_id"] |
79 | | - ) |
80 | | - mapped_data = {x["hs_code_id"][0]: x["hs_code_id_count"] for x in rg_res} |
81 | | - for code in self: |
82 | | - code.product_tmpl_count = mapped_data.get(code.id, 0) |
83 | | - |
84 | | - @api.depends("local_code", "description") |
85 | | - def name_get(self): |
86 | | - res = [] |
87 | | - for this in self: |
88 | | - name = this.local_code |
89 | | - if this.description: |
90 | | - name += " " + this.description |
91 | | - name = shorten(name, 55) |
92 | | - res.append((this.id, name)) |
93 | | - return res |
94 | | - |
95 | | - _sql_constraints = [ |
96 | | - ( |
97 | | - "local_code_company_uniq", |
98 | | - "unique(local_code, company_id)", |
99 | | - "This code already exists for this company !", |
100 | | - ) |
101 | | - ] |
102 | | - |
103 | | - @api.model_create_multi |
104 | | - def create(self, vals_list): |
105 | | - for vals in vals_list: |
106 | | - if vals.get("local_code"): |
107 | | - vals["local_code"] = vals["local_code"].replace(" ", "") |
108 | | - return super().create(vals_list) |
109 | | - |
110 | | - def write(self, vals): |
111 | | - if vals.get("local_code"): |
112 | | - vals["local_code"] = vals["local_code"].replace(" ", "") |
113 | | - return super().write(vals) |
| 59 | + ProductTemplate = self.env["product.template"] |
| 60 | + for hs in self: |
| 61 | + local_code_ids = hs.local_code_ids.ids |
| 62 | + if local_code_ids: |
| 63 | + count = ProductTemplate.search_count( |
| 64 | + [("local_code_id", "in", local_code_ids)] |
| 65 | + ) |
| 66 | + hs.product_tmpl_count = count |
| 67 | + else: |
| 68 | + hs.product_tmpl_count = 0 |
0 commit comments