4

I am using Odoo v10 .

As we know in purchase.order, there is a base (Monetary) field amount_total which contains value of total amount of a Purchase Order based on (self) currency_id .

Now, I create a new float field home_currency_amount_total in purchase.order .

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

How can i have a value in this field based on company currency? i.e. I want to have a corresponding value in company base currency and can be used in my tree & form views.

I am new to Odoo and I wonder if there is a "shortcut" (e.g. a built-in compute method) instead of I have to write up related codes.

cyliinhk
  • 75
  • 8
  • Are you using multi-company structure? – Chavada Viki Jul 08 '17 at 06:13
  • My purpose is which company's currency_id you want to use in the purchase to calculate the new total field? is it from the purchase order itself or current company in which user currently logged in? – Chavada Viki Jul 08 '17 at 06:17
  • No. Just a single company in Odoo. each order has its own invoice currency but i need a number of company currency (i.e. the current company of the whole platform) in the same PO tree or form view – cyliinhk Jul 09 '17 at 00:46

2 Answers2

4

There is a built-in method for conversion of currency.

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 calculate the conversion you can use this method.

This method takes 3 arguments, first from currency, second to currency and amount which you want to convert as a third argument.

Eg.

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

Update :

Create you field like this.

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)
Chavada Viki
  • 1,514
  • 1
  • 12
  • 22
  • tks Viki. is it I can do in purchase.order? – cyliinhk Jul 09 '17 at 01:00
  • will this work ? home_currency_amount_total = fields.Float(string='Total Amount in company currency', compute="_compute(order.currency_id,order.company_id.currency_id,order.amount_total)", True) – cyliinhk Jul 09 '17 at 01:04
  • i have inserted the updated code into purchase.py. I get the internal server error. What will be the problem? – cyliinhk Jul 10 '17 at 06:25
  • hi i stop the odoo and start it. Access the url and obtain the error log here – cyliinhk Jul 10 '17 at 07:18
  • 1
    File "/usr/lib/python2.7/dist-packages/odoo/addons/purchase/models/__init__.py", line 5, in import purchase File "/usr/lib/python2.7/dist-packages/odoo/addons/purchase/models/purchase.py", line 171 home_currency_amount_total = fields.Float(string='Total Amount in company currency', compute="_compute", store=True) ^ IndentationError: unexpected indent – cyliinhk Jul 10 '17 at 07:20
  • Ohh.. my friend you just need to properly indent your code. – Chavada Viki Jul 10 '17 at 07:23
  • 2
    Thanks Viki. I got solved. your updated expression works. Thanks a lot. – cyliinhk Jul 10 '17 at 14:29
  • 1
    So, can you please accept and upvote the answer? Thank you. – Chavada Viki Jul 11 '17 at 04:04
0

You can do it using 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 above code we have create one compute field store True, it means value will be store in the database.

When amount_total will change at that time system will calculate home currency amount.

In method we have checked if company currency & purchase order currency is different then system will compute currency amount.

In odoo base module on method is available for compute currency, in which you can pass date in the context.

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.

This will help you.

  • thanks. I am going to use the code and test it. Before that I wonder if amount_total will change during the process. I want to keep it and so finally I have two values : one in invoice currency and one in company currency. – cyliinhk Jul 09 '17 at 00:53
  • Hi, i have put the code into purchase.py but it looks get wrong with error. should i create a new model to do so? if so how could i show in tree and form view? – cyliinhk Jul 09 '17 at 15:21
  • I copied the above code " @api.multi ... ,store=True) into purchase.py. when i click a record of PO Form, Odoo error shows ...... /lib/python2.7/dist-packages/odoo/sql_db.py", line 141, in wrapper return f(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/odoo/sql_db.py", line 218, in execute res = self._obj.execute(query, params) ProgrammingError: column purchase_order.home_currency_amount_total does not exist LINE 1: ...ef","purchase_order"."company_id" as "company_id","purchase_... ^ – cyliinhk Jul 10 '17 at 06:37
  • it looks i cannot create home_currency_amount_total – cyliinhk Jul 10 '17 at 06:37
  • I think you have forget to upgrade module, you should restart your odoo server & upgrade your module. – Emipro Technologies Pvt. Ltd. Jul 10 '17 at 07:26
  • thanks. my problem is solved. thanks for your effort. – cyliinhk Jul 10 '17 at 14:30