我有这个python文件
class sale_order_line(models.Model):
_inherit='sale.order.line'
@api.multi
@api.depends('product_id')
def _get_cost_price_currency(self):
for lot in self:
so_line_ids = self.env['product.product'].search([('id','=',lot.product_id.id)])
for rec in so_line_ids:
if rec.date_hpp_start:
converted_date = datetime.datetime.strptime(rec.date_hpp_start, '%Y-%m-%d').date()
rec.days_difference = (datetime.date.today() - converted_date).days
if rec.days_difference > 0:
rec.days_difference = rec.days_difference
else:
rec.days_difference = 0
rec.cost_price_set = rec.cost_price_set
rec.cost_price_currency = rec.cost_price_currency
if rec.currency_rate_cost2:
lot.currency_rate_cost = rec.currency_rate_cost2
else:
lot.currency_rate_cost = rec.currency_rate_cost
lot.hpp_non_interest = rec.hpp_non_interest
lot.date_hpp_start = rec.date_hpp_start
lot.days_difference = rec.days_difference
lot.interest_per_year = rec.interest_per_year
lot.interest_per_day = rec.interest_per_year/365
lot.interest_by_today = lot.days_difference*lot.interest_per_day
if lot.hpp_non_interest and rec.currency_rate_cost2:
lot.cost_price_currency = lot.hpp_non_interest + (rec.hpp_non_interest*rec.interest_by_today/100)
lot.cost_price_set = lot.cost_price_currency * lot.currency_rate_cost
elif lot.hpp_non_interest:
lot.cost_price_currency = '0'
lot.cost_price_set = rec.hpp_non_interest + (rec.hpp_non_interest*rec.interest_by_today/100)
else:
lot.cost_price_currency = rec.cost_price_currency
lot.cost_price_set = rec.cost_price_set
currency_rate_cost = fields.Float(string='Tax Rate',compute='_get_cost_price_currency', digits_compute=dp.get_precision('Custom1') )
hpp_non_interest = fields.Float(string='HPP Non Interest',compute='_get_cost_price_currency', digits_compute=dp.get_precision('Custom3'))
date_hpp_start = fields.Date(string='HPP Date start',compute='_get_cost_price_currency')
days_difference = fields.Integer(string='Interest Age (days)', compute=_get_cost_price_currency)
interest_per_year = fields.Float(string='Interest(%) Per Year', compute=_get_cost_price_currency)
interest_per_day = fields.Float(string='Interest(%) Per Day', compute=_get_cost_price_currency)
interest_by_today = fields.Float(string='Today Interest', compute=_get_cost_price_currency)
cost_price_currency = fields.Float(string='HPP USD',compute='_get_cost_price_currency', digits_compute=dp.get_precision('Custom3') )
cost_price_set = fields.Float(string='Cost Price Public',compute='_get_cost_price_currency', digits_compute=dp.get_precision('Custom1') )
和这个.xml
<record model="ir.ui.view" id="sale_cost_sale_order_line_form">
<field name="name">sale.order.line.tree.cost.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/tree//field[@name='price_unit']" position="after">
<field name="cost_price_set" />
<field name="cost_price_currency" />
<field name="currency_rate_cost" />
</xpath>
</field>
</record>
我想存储cost_price_set、cost_price_current和currency_rate_cost的值。问题是:
- 如果在这3个字段中使用
store=True
,则该字段不会按应有的方式更新 - 如果我不使用
store=True
,则字段会更新,但单击保存时该值不会存储到DB中
我该怎么喊?
您需要使用store=True
,以便将其存储在数据库中。但它不会一直计算。
根据您当前的代码,它将计算@api.depends
中提到的字段何时发生任何更改,只有当产品id发生更改时,它才会发生更改。
所以你也可以提到其他领域。
@api.depends('product_id', 'other_fields', 'product_id.product_field1')
def _get_cost_price_currency(self):
# this is an example of how you can mention fields into depends.
您还可以从以下位置获取参考:https://www.odoo.com/documentation/15.0/developer/howtos/rdtraining/09_compute_onchange.html
希望这能有所帮助!