I am working in Odoo13 I am calculating some taxes while creating my sale order and while doing this I am successfully calculating my respective taxes as following.
But after saving my sale order or by confirming it all my custom tax fields disappear like:
Here is my Python File code:
class SaleOrder(models.Model):
_inherit = ['sale.order']
federal_tax = fields.Float('Federal Tax', readonly=True)
state_tax = fields.Float('State Tax', readonly=True)
county_tax = fields.Float('County Tax', readonly=True)
city_tax = fields.Float('City Tax', readonly=True)
unincorporated_tax = fields.Float('Unincorporated Tax', store=True, readonly=True)
And here is my XML File:
<xpath expr="/form/sheet/notebook/page/group/group/field[@name='amount_untaxed']" position="after">
<field name="federal_tax" attrs="{'invisible':[('federal_tax','==', 0.00)]}"/>
<field name="state_tax" attrs="{'invisible':[('state_tax','==', 0.00)]}"/>
<field name="county_tax" attrs="{'invisible':[('county_tax','==', 0.00)]}"/>
<field name="city_tax" attrs="{'invisible':[('city_tax','==', 0.00)]}"/>
<field name="unincorporated_tax" attrs="{'invisible':[('unincorporated_tax','==', 0.00)]}"/>
</xpath>
Note: After confirming my order or by saving it my custom fields value turn to 0 that is why they are disappearing but why they are turning to zero?
You can put readonly field attribute on view instead of model, for example:
class SaleOrder(models.Model):
_inherit = ['sale.order']
federal_tax = fields.Float('Federal Tax',)
state_tax = fields.Float('State Tax',)
county_tax = fields.Float('County Tax',)
city_tax = fields.Float('City Tax',)
unincorporated_tax = fields.Float('Unincorporated Tax',)
<xpath expr="/form/sheet/notebook/page/group/group/field[@name='amount_untaxed']" position="after">
<field name="federal_tax" attrs="{'invisible':[('federal_tax','==', 0.00)]}" readonly="True"/>
<field name="state_tax" attrs="{'invisible':[('state_tax','==', 0.00)]}" readonly="True"/>
<field name="county_tax" attrs="{'invisible':[('county_tax','==', 0.00)]}" readonly="True"/>
<field name="city_tax" attrs="{'invisible':[('city_tax','==', 0.00)]}" readonly="True"/>
<field name="unincorporated_tax" attrs="{'invisible':[('unincorporated_tax','==', 0.00)]}" readonly="True"/>
</xpath>
Now the main question, why your data is not being stored, it is because on the model definition you defined the fields as readonly, by default Odoo doesn't store form data from fields for which readonly
attribute is set to True
. I am assuming that you are calculating those readonly fields on the form view from onchange
function, so the data is only calculated on the fly, but not yet saved on database, but as this are readonly, Odoo is ignoring those fields value and hence you are always getting ) even after pressing save. You can override the Odoo default behavior using force_save
attribute, which overrides Odoo default form behavior to submit fields value even if readonly.
<xpath expr="/form/sheet/notebook/page/group/group/field[@name='amount_untaxed']" position="after">
<field name="federal_tax" attrs="{'invisible':[('federal_tax','==', 0.00)]}" readonly="True" force_save="1"/>
<field name="state_tax" attrs="{'invisible':[('state_tax','==', 0.00)]}" readonly="True" force_save="1"/>
<field name="county_tax" attrs="{'invisible':[('county_tax','==', 0.00)]}" readonly="True" force_save="1"/>
<field name="city_tax" attrs="{'invisible':[('city_tax','==', 0.00)]}" readonly="True" force_save="1"/>
<field name="unincorporated_tax" attrs="{'invisible':[('unincorporated_tax','==', 0.00)]}" readonly="True" force_save="1"/>
</xpath>
Odoo follows same behavior for invisible
fields also, so you have to use force_save
attribute in invisible
fields also.