|
3 | 3 | # @author: Sylvain LE GAL (https://twitter.com/legalsylvain) |
4 | 4 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
5 | 5 |
|
6 | | -import json |
7 | 6 | import logging |
8 | 7 |
|
9 | | -from lxml import etree |
| 8 | +from lxml import builder |
10 | 9 |
|
11 | 10 | from odoo import api, fields, models |
12 | 11 |
|
|
16 | 15 | class ProductTemplate(models.Model): |
17 | 16 | _inherit = "product.template" |
18 | 17 |
|
19 | | - taxes_id = fields.Many2many(default=False) |
| 18 | + taxes_id = fields.Many2many(readonly=True, default=False) |
20 | 19 |
|
21 | | - supplier_taxes_id = fields.Many2many(default=False) |
| 20 | + supplier_taxes_id = fields.Many2many(readonly=True, default=False) |
22 | 21 |
|
23 | 22 | fiscal_classification_id = fields.Many2one( |
24 | 23 | comodel_name="account.product.fiscal.classification", |
@@ -65,20 +64,33 @@ def _onchange_fiscal_classification_id(self): |
65 | 64 | self.taxes_id = self.fiscal_classification_id.sale_tax_ids.ids |
66 | 65 |
|
67 | 66 | @api.model |
68 | | - def get_view(self, view_id=None, view_type="form", **options): |
69 | | - """Set fiscal_classification_id required on all views. |
70 | | - We don't set the field required by field definition to avoid |
71 | | - incompatibility with other modules, errors on import, etc...""" |
72 | | - result = super().get_view(view_id=view_id, view_type=view_type, **options) |
73 | | - doc = etree.fromstring(result["arch"]) |
74 | | - nodes = doc.xpath("//field[@name='fiscal_classification_id']") |
75 | | - if nodes: |
76 | | - for node in nodes: |
77 | | - modifiers = json.loads(node.get("modifiers", "{}")) |
78 | | - modifiers["required"] = True |
79 | | - node.set("modifiers", json.dumps(modifiers)) |
80 | | - result["arch"] = etree.tostring(doc, encoding="unicode").replace("\t", "") |
81 | | - return result |
| 67 | + def _get_view(self, view_id=None, view_type="form", **options): |
| 68 | + arch, view = super()._get_view(view_id=view_id, view_type=view_type, **options) |
| 69 | + self._alter_view_fiscal_classification(arch, view_type) |
| 70 | + return arch, view |
| 71 | + |
| 72 | + @api.model |
| 73 | + def _alter_view_fiscal_classification(self, arch, view_type): |
| 74 | + if view_type in ("form", "tree"): |
| 75 | + node_taxes_id = arch.xpath("//field[@name='taxes_id']") |
| 76 | + node_supplier_taxes_id = arch.xpath("//field[@name='supplier_taxes_id']") |
| 77 | + node_tax_field = node_taxes_id or node_supplier_taxes_id |
| 78 | + if node_tax_field: |
| 79 | + # append fiscal_classification_id just before taxes fields |
| 80 | + node_tax_field = node_tax_field[0] |
| 81 | + classification_node = builder.E.field( |
| 82 | + name="fiscal_classification_id", required="1" |
| 83 | + ) |
| 84 | + node_tax_field.getparent().insert( |
| 85 | + node_tax_field.getparent().index(node_tax_field), |
| 86 | + classification_node, |
| 87 | + ) |
| 88 | + |
| 89 | + if view_type == "tree": |
| 90 | + if node_taxes_id: |
| 91 | + node_taxes_id[0].set("optional", "hide") |
| 92 | + if node_supplier_taxes_id: |
| 93 | + node_supplier_taxes_id[0].set("optional", "hide") |
82 | 94 |
|
83 | 95 | # Custom Section |
84 | 96 | def _fiscal_classification_update_taxes(self, vals): |
|
0 commit comments