From fd0b6619c2a17486daaa3eff4a707109dc3e5bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 21 Jun 2022 10:13:53 +0200 Subject: [PATCH] Use context date as default pattern issue11359 review417411003 --- product.py | 9 +++++++++ tests/test_module.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/product.py b/product.py index ddbdc2cada..fccdd7f35c 100644 --- a/product.py +++ b/product.py @@ -3,6 +3,7 @@ from trytond.model import ModelView, fields from trytond.pool import Pool, PoolMeta from trytond.pyson import Eval, If +from trytond.transaction import Transaction class PriceList(metaclass=PoolMeta): @@ -21,6 +22,14 @@ class PriceList(metaclass=PoolMeta): def open_lines(cls, price_lists): pass + def compute( + self, party, product, unit_price, quantity, uom, pattern=None): + context = Transaction().context + pattern = pattern.copy() if pattern is not None else {} + pattern.setdefault('date', context.get('date')) + return super().compute( + party, product, unit_price, quantity, uom, pattern=pattern) + class PriceListLine(metaclass=PoolMeta): __name__ = 'product.price_list.line' diff --git a/tests/test_module.py b/tests/test_module.py index 6386ec9c18..9a1cd198ae 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -8,6 +8,7 @@ from trytond.modules.company.tests import ( CompanyTestMixin, create_company, set_company) from trytond.pool import Pool from trytond.tests.test_tryton import ModuleTestCase, with_transaction +from trytond.transaction import Transaction class ProductPriceListDatesTestCase(CompanyTestMixin, ModuleTestCase): @@ -72,5 +73,25 @@ class ProductPriceListDatesTestCase(CompanyTestMixin, ModuleTestCase): None, None, Decimal(10), 1, None, pattern={'date': yesterday}), Decimal(9)) + @with_transaction() + def test_price_list_with_context_date(self): + "Test price list with context date" + pool = Pool() + Date = pool.get('ir.date') + + today = Date.today() + tomorrow = today + datetime.timedelta(days=1) + + price_list = self.create_price_list('start_date', tomorrow) + + with Transaction().set_context(date=today): + self.assertEqual( + price_list.compute(None, None, Decimal(10), 1, None), + Decimal(10)) + with Transaction().set_context(date=tomorrow): + self.assertEqual( + price_list.compute(None, None, Decimal(10), 1, None), + Decimal(9)) + del ModuleTestCase