diff --git a/product.py b/product.py index 003742f224..835fce4a9e 100644 --- a/product.py +++ b/product.py @@ -3,6 +3,7 @@ from trytond.model import ModelView, fields from trytond.pool import PoolMeta, Pool 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_product_price_list_dates.py b/tests/test_product_price_list_dates.py index 5d1b548e92..58613b500d 100644 --- a/tests/test_product_price_list_dates.py +++ b/tests/test_product_price_list_dates.py @@ -11,6 +11,7 @@ from trytond.tests.test_tryton import suite as test_suite from trytond.modules.company.tests import ( create_company, set_company, CompanyTestMixin) +from trytond.transaction import Transaction class ProductPriceListDatesTestCase(CompanyTestMixin, ModuleTestCase): @@ -75,6 +76,26 @@ 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)) + def suite(): suite = test_suite()