Skip to content
Snippets Groups Projects
Commit 66eb732c authored by hote's avatar hote Committed by Horacio Tellez (hote)
Browse files

[FIX] sale: currency conversions and rates


Before this commit the reports in sales and associated app suffered from a confusion between
multiplicative and divisive rates for currency conversion. After this commit this will no longer be
the case.

There are several models with their own currencies that are involved in computing the amount of a
sale order:
- the order's company currency `order.company_id.currency_id`
- the order's pricelist currency `order.pricelist_id.currency_id`
- the product's currency for each product in the sale order `product.currency_id`

In the case of a report we need also to take into account the current user's company currency:
`self.env.company.currency_id`.

The `sale_order.currency_rate` is the **multiplicative <u>rate</u>** to convert from the company
currency into the SO currency.

Any product added to a sale order follow the next conversion. Let `conversion_rate` be the conversion
rate between the product's currency and the order's company currency, then:
```python
product_amount_converted = product.list_price / conversion_rate

order.amount_total +=  product_amount_converted * order.currency_rate
```

The rates returned by `_get_query_currency_table()`, on the other hand, are computed as the
**multiplicative <u>ratio</u>** to convert from each company currency into the current user's
company currency.

Any order that is to be analysed on a *report* follow the next conversion. Let `current_company_currency_rate`
be the conversion rate between the order's company currency and the current user's company, then:
```python
order_amount_in_cmp_currency = order.amount_total / order.currency_rate

report.order_id.price_total = order_amount_in_cmp_currency * current_company_currency_rate
```

What follow is a numerical example. Suppose you have the following multi-company and
multi-currency environment.

|order.id|order.company  |line.subtotal|order.currency|order.currency_rate|
| ------ | -----------   | ----------- | ------------ | ----------------- |
|1      |BE Company (EUR)|1000         |INR           |89.46              |
|2      |BE Company (EUR)|2000         |USD           |1.09               |
|3      |BE Company (EUR)|3000         |EUR           |1.0                |
|4      |US Company (USD)|4000         |USD           |1.0                |

If we try to convert all of this for reporting in the US company, the currency table returned by
`_get_query_currency_table()` will give this (company currencies vs USD):

|currency_table.company|currency_table.rate|
| -------------------- | ------------------ |
|BE Company (EUR)      |1.09                |
|US Company (USD)      |1.0                 |

Finally, in order to convert each amount into USD, applying the formulae listed above should give
the following result:

|order.id|order.company|display_price_usd           |
| ------ | ----------- | -------------------------- |
|1       |BE Company   |1000 / 89.46 * 1.09 = 12.18 |
|2       |BE Company   |2000 / 1.09 * 1.09 = 2000.00|
|3       |BE Company   |3000 / 1.0 * 1.09 = 3270.00 |
|4       |US Company   |4000 / 1.0 * 1.0 = 4000.00  |
|Total (USD): 9282.18|

Task - 3277006

closes odoo/odoo#119608

Signed-off-by: default avatarVictor Feyens (vfe) <vfe@odoo.com>
parent aac4a1e9
Branches
Tags
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment