Odoo v10 在采购树和表单视图中以公司货币显示金额

Odoo v10 display amount in company currency in purchase tree and form view

我正在使用 Odoo v10。

正如我们在 purchase.order 中所知,有一个基本(货币)字段 amount_total,其中包含基于(自身)currency_id 的采购订单总额的价值。

现在,我在 purchase.order 中创建了一个新的浮动字段 home_currency_amount_total 。

home_currency_amount_total = fields.Float(string='Total Amount in company currency', store=True)

我怎样才能在这个字段中获得基于公司货币的值?即我想在公司基础货币中有一个相应的值,并且可以在我的树和表单视图中使用。

我是 Odoo 的新手,我想知道是否有 "shortcut"(例如内置计算方法)而不是我必须编写相关代码。

有一个内置的货币转换方法。

例如

  @api.model
  def _compute(self, from_currency, to_currency, from_amount, round=True):
     if (to_currency == from_currency):
        amount = to_currency.round(from_amount) if round else from_amount
     else:
        rate = self._get_conversion_rate(from_currency, to_currency)
        amount = to_currency.round(from_amount * rate) if round else from_amount * rate
     return amount

所以,如果你想计算转换你可以使用这个方法。

此方法有 3 个参数,第一个来自货币,第二个是货币和您要转换的金额作为第三个参数。

例如

self.env['res.currency']._compute(order.currency_id,order.company_id.currency_id,order.amount_total)

更新:

像这样创建你的字段。

home_currency_amount_total = fields.Float(string='Total Amount in company currency', compute="_compute", store=True)

@api.depends('order_lines.price_subtotal','company_id','currency_id')
def _compute(self);
    for order in self:
        home_currency_amount_total =  self.env['res.currency']._compute(order.currency_id,order.company_id.currency_id,order.amount_total)

您可以使用以下方法

class PurchaseOrder(models.Model):
    _inherit = "purchase.order"

    @api.multi
    @api.depends('amount_total')
    def get_amount_in_company_currency(self):
        for purchase_order in self:
            if purchase_order.currency_id.id!=purchase_order.company_id.currency_id.id:
                currency_id = purchase_order.currency_id.with_context(date=purchase_order.date_order)
                purchase_order.home_currency_amount_total = currency_id.compute(purchase_order.amount_total, purchase_order.company_id.currency_id)            
            else:
                purchase_order.home_currency_amount_total=purchase_order.amount_total
    home_currency_amount_total = fields.Float(string='Total Amount in company currency',compute="get_amount_in_company_currency",store=True)

在上面的代码中我们创建了一个计算字段 store True,这意味着值将是store 在数据库中。

amount_total发生变化时系统会计算本币金额.

在方法中我们检查了公司货币采购订单货币是否不同然后系统将计算货币金额.

在 odoo 基础模块中,方法可用于计算货币,您可以在其中传递上下文中的日期

purchase_order.currency_id.with_context(date=purchase_order.date_order)

Based on context date system will take currency rate, if you not pass any date then system will take current date rate.

这对你有帮助。