Odoo v10 displays the amount in company currency in the procurement tree and form
I am using Odoo v10.
As you know in buy.order, there is a base (cash) field amount_total which contains the value of the total purchase order amount based on (self) currency_id.
Now I am creating a new float field home_currency_amount_total in buy.order.
home_currency_amount_total = fields.Float(string='Total Amount in company currency', store=True)
How can I get the value in this field based on the company currency? that is, I want to have a corresponding value in the base currency of the company and can be used in tree and form views.
I'm new to Odoo and I'm wondering if there is a "shortcut" (like a built-in calculation method) instead of writing related codes.
source to share
There is a built-in currency conversion method.
Eg.
@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
So, if you want to compute the transformation, you can use this method.
This method takes 3 arguments, first from currency, second to currency and the amount you want to convert as the third argument.
Eg.
self.env['res.currency']._compute(order.currency_id,order.company_id.currency_id,order.amount_total)
Update:
Create this field as follows.
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)
source to share
You can do this using the following method .
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)
In the above code, we create one computational field store True , which means that the value will be stored in the database.
When the amount_total changes, at this time the system will calculate the amount of the local currency .
In the method, we checked if the company currency and the purchase order currency are different , then the system will calculate the currency amount .
In the base odoo module, it is available for the method to calculate the currency in which you can pass the date in the context .
purchase_order.currency_id.with_context (date = purchase_order.date_order)
Based on the context date system, the currency rate will be used , if you do not pass any date, then the system will accept the current baud rate .
This will help you.
source to share