Skip to content

Commit

Permalink
[FIX] When evaluating risk, convert the amount to correct currency
Browse files Browse the repository at this point in the history
Detailed description of the issue: OCA#314
  • Loading branch information
maciej-wichowski committed Sep 18, 2023
1 parent ea6758a commit 6acf559
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sale_financial_risk/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def evaluate_risk_message(self, partner):
self.ensure_one()
risk_amount = self.currency_id._convert(
self.amount_total,
self.company_id.currency_id,
partner.risk_currency_id,
self.company_id,
self.date_order
and self.date_order.date()
Expand Down Expand Up @@ -124,7 +124,7 @@ def _compute_risk_amount(self):
risk_amount = line.price_reduce_taxinc * risk_qty
line.risk_amount = line.order_id.currency_id._convert(
risk_amount,
line.company_id.currency_id,
line.order_id.partner_id.risk_currency_id,
line.company_id,
line.order_id.date_order
and line.order_id.date_order.date()
Expand Down
73 changes: 71 additions & 2 deletions sale_financial_risk/tests/test_partner_sale_risk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2016-2018 Tecnativa - Carlos Dauden
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields
from odoo.tests.common import SavepointCase


Expand All @@ -19,10 +20,26 @@ def setUpClass(cls):
cls.product_pricelist = cls.env["product.pricelist"].create(
{"name": "pricelist for sale_financial_risk test"}
)
cls.sale_order = cls.env["sale.order"].create(
cls.main_currency = cls.env.company.currency_id
cls.EUR = cls.env.ref("base.EUR")
cls.other_company = cls.env["res.company"].create(
{"name": "Company 2", "currency_id": cls.EUR.id}
)
cls.sale_order = cls.create_sale_order()
cls.env.user.lang = "en_US"

@classmethod
def create_sale_order(cls, currency=None, company=None):
if not currency:
currency = cls.main_currency
if not company:
company = cls.env.company
return cls.env["sale.order"].create(
{
"partner_id": cls.partner.id,
"pricelist_id": cls.product_pricelist.id,
"currency_id": currency.id,
"company_id": company.id,
"order_line": [
(
0,
Expand All @@ -33,12 +50,12 @@ def setUpClass(cls):
"product_uom_qty": 1,
"product_uom": cls.product.uom_id.id,
"price_unit": 100.0,
"company_id": company.id,
},
)
],
}
)
cls.env.user.lang = "en_US"

def test_sale_order(self):
self.sale_order.action_confirm()
Expand Down Expand Up @@ -157,3 +174,55 @@ def test_open_risk_pivot_info(self):
self.assertEqual(action["res_model"], "sale.order.line")
self.assertTrue(action["view_id"])
self.assertTrue(action["domain"])

def test_manual_currency_risk_not_exceeded(self):
self.product_pricelist.currency_id = self.EUR
self.partner.write(
{
"risk_sale_order_limit": 99,
"credit_currency": "manual",
"manual_credit_currency_id": self.main_currency.id,
}
)
self.env["res.currency.rate"].create(
{
"currency_id": self.main_currency.id,
"name": fields.Date.today(),
"rate": 0.5,
"company_id": self.other_company.id,
}
)
sale_order = self.create_sale_order(
currency=self.EUR, company=self.other_company
)
result = sale_order.action_confirm()

# Limit not exceeded
self.assertEqual(result, True)

def test_manual_currency_risk_exceeded(self):
self.product_pricelist.currency_id = self.EUR
self.partner.write(
{
"risk_sale_order_limit": 99,
"credit_currency": "manual",
"manual_credit_currency_id": self.main_currency.id,
}
)
self.product_pricelist.currency_id = self.EUR
self.env["res.currency.rate"].create(
{
"currency_id": self.main_currency.id,
"name": fields.Date.today(),
"rate": 1.5,
"company_id": self.other_company.id,
}
)
sale_order = self.create_sale_order(
currency=self.EUR, company=self.other_company
)
result = sale_order.action_confirm()

# Limit exceeded
self.assertNotEquals(result, True)
self.assertEqual(result["res_model"], "partner.risk.exceeded.wiz")

0 comments on commit 6acf559

Please sign in to comment.