Skip to content

Commit 03f00e5

Browse files
committed
[IMP] account_product_fiscal_classification: update views by python code.
rational: In odoo, some module (like hr_expense) redefines product views. For each case, we need a glue module to insert fiscal_classification_id in the view. With that changes, insertion is done for all tree and form view. Closes: #505 [FIX] display field also for product.produt form view.
1 parent 25ad504 commit 03f00e5

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

account_product_fiscal_classification/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from . import product_product
12
from . import product_template
23
from . import account_product_fiscal_classification_template
34
from . import account_product_fiscal_classification
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (C) 2014-Today GRAP (http://www.grap.coop)
2+
# Copyright (C) 2016-Today La Louve (<http://www.lalouve.net/>)
3+
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
4+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
5+
6+
import logging
7+
8+
from odoo import api, models
9+
10+
_logger = logging.getLogger(__name__)
11+
12+
13+
class ProductProduct(models.Model):
14+
_inherit = "product.product"
15+
16+
@api.model
17+
def _get_view(self, view_id=None, view_type="form", **options):
18+
arch, view = super()._get_view(view_id=view_id, view_type=view_type, **options)
19+
self.env["product.template"]._alter_view_fiscal_classification(arch, view_type)
20+
return arch, view

account_product_fiscal_classification/models/product_template.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
44
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
55

6-
import json
76
import logging
87

9-
from lxml import etree
8+
from lxml import builder
109

1110
from odoo import api, fields, models
1211

@@ -16,9 +15,9 @@
1615
class ProductTemplate(models.Model):
1716
_inherit = "product.template"
1817

19-
taxes_id = fields.Many2many(default=False)
18+
taxes_id = fields.Many2many(readonly=True, default=False)
2019

21-
supplier_taxes_id = fields.Many2many(default=False)
20+
supplier_taxes_id = fields.Many2many(readonly=True, default=False)
2221

2322
fiscal_classification_id = fields.Many2one(
2423
comodel_name="account.product.fiscal.classification",
@@ -65,20 +64,33 @@ def _onchange_fiscal_classification_id(self):
6564
self.taxes_id = self.fiscal_classification_id.sale_tax_ids.ids
6665

6766
@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")
8294

8395
# Custom Section
8496
def _fiscal_classification_update_taxes(self, vals):

account_product_fiscal_classification/views/view_product_template.xml

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,6 @@
1616
</field>
1717
</record>
1818

19-
<record id="view_product_template_tree_account" model="ir.ui.view">
20-
<field name="model">product.template</field>
21-
<field name="inherit_id" ref="account.product_template_view_tree" />
22-
<field name="arch" type="xml">
23-
<field name="taxes_id" position="attributes">
24-
<attribute name="invisible">1</attribute>
25-
</field>
26-
<field name="supplier_taxes_id" position="attributes">
27-
<attribute name="invisible">1</attribute>
28-
</field>
29-
<field name="supplier_taxes_id" position="after">
30-
<field name="fiscal_classification_id" />
31-
</field>
32-
</field>
33-
</record>
34-
35-
<record id="view_product_template_form_account" model="ir.ui.view">
36-
<field name="model">product.template</field>
37-
<field name="inherit_id" ref="account.product_template_form_view" />
38-
<field name="arch" type="xml">
39-
<field name="taxes_id" position="before">
40-
<field name="fiscal_classification_id" quick_create="false" />
41-
</field>
42-
<field name="taxes_id" position="attributes">
43-
<attribute name="readonly">True</attribute>
44-
</field>
45-
<field name="supplier_taxes_id" position="attributes">
46-
<attribute name="readonly">True</attribute>
47-
</field>
48-
</field>
49-
</record>
50-
5119
<record
5220
id="action_template_list_by_fiscal_classification"
5321
model="ir.actions.act_window"

0 commit comments

Comments
 (0)