From 6acf559529722a61619de63dc47d27763d4f3200 Mon Sep 17 00:00:00 2001 From: Maciej Date: Mon, 18 Sep 2023 16:39:27 +0200 Subject: [PATCH] [FIX] When evaluating risk, convert the amount to correct currency Detailed description of the issue: https://github.com/OCA/credit-control/issues/314 --- sale_financial_risk/models/sale.py | 4 +- .../tests/test_partner_sale_risk.py | 73 ++++++++++++++++++- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/sale_financial_risk/models/sale.py b/sale_financial_risk/models/sale.py index e50f60052..90bfc8ed7 100644 --- a/sale_financial_risk/models/sale.py +++ b/sale_financial_risk/models/sale.py @@ -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() @@ -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() diff --git a/sale_financial_risk/tests/test_partner_sale_risk.py b/sale_financial_risk/tests/test_partner_sale_risk.py index 93e24df46..c3e2af4e9 100644 --- a/sale_financial_risk/tests/test_partner_sale_risk.py +++ b/sale_financial_risk/tests/test_partner_sale_risk.py @@ -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 @@ -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, @@ -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() @@ -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")