diff --git a/CHANGELOG b/CHANGELOG index 1326b7a3c9..8e5e6ccfbd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Use sale_uom as default for Product.get_sale_price + Version 5.0.0 - 2018-10-01 * Bug fixes (see mercurial logs for details) * Add configured delay for processing sale diff --git a/product.py b/product.py index c44a70b39f..5904776d66 100644 --- a/product.py +++ b/product.py @@ -94,7 +94,7 @@ class Product(metaclass=PoolMeta): ''' Return the sale price for products and quantity. It uses if exists from the context: - uom: the unit of measure + uom: the unit of measure or the sale uom of the product currency: the currency id for the returned price ''' pool = Pool() @@ -123,6 +123,9 @@ class Product(metaclass=PoolMeta): if uom and product.default_uom.category == uom.category: prices[product.id] = Uom.compute_price( product.default_uom, prices[product.id], uom) + else: + prices[product.id] = Uom.compute_price( + product.default_uom, prices[product.id], product.sale_uom) if currency and user.company: if user.company.currency != currency: date = Transaction().context.get('sale_date') or today diff --git a/tests/test_sale.py b/tests/test_sale.py index 4716161c95..2406410828 100644 --- a/tests/test_sale.py +++ b/tests/test_sale.py @@ -2,15 +2,68 @@ # this repository contains the full copyright notices and license terms. import unittest import doctest +from decimal import Decimal + import trytond.tests.test_tryton -from trytond.tests.test_tryton import ModuleTestCase +from trytond.tests.test_tryton import ModuleTestCase, with_transaction from trytond.tests.test_tryton import doctest_teardown, doctest_checker +from trytond.transaction import Transaction +from trytond.pool import Pool + +from trytond.modules.company.tests import create_company, set_company +from trytond.modules.account.tests import create_chart class SaleTestCase(ModuleTestCase): 'Test Sale module' module = 'sale' + @with_transaction() + def test_sale_price(self): + "Test sale price" + pool = Pool() + Account = pool.get('account.account') + Template = pool.get('product.template') + Product = pool.get('product.product') + Uom = pool.get('product.uom') + + company = create_company() + with set_company(company): + create_chart(company) + + receivable, = Account.search([ + ('kind', '=', 'receivable'), + ('company', '=', company.id), + ]) + payable, = Account.search([ + ('kind', '=', 'payable'), + ('company', '=', company.id), + ]) + + kg, = Uom.search([('name', '=', 'Kilogram')]) + g, = Uom.search([('name', '=', 'Gram')]) + pound, = Uom.search([('name', '=', 'Pound')]) + + template, = Template.create([{ + 'name': 'Product', + 'default_uom': g.id, + 'sale_uom': kg.id, + 'list_price': Decimal(5), + 'products': [('create', [{}])], + }]) + product, = template.products + + prices = Product.get_sale_price([product], quantity=100) + self.assertEqual(prices, {product.id: Decimal(5000)}) + prices = Product.get_sale_price([product], quantity=1500) + self.assertEqual(prices, {product.id: Decimal(5000)}) + + with Transaction().set_context(uom=pound.id): + prices = Product.get_sale_price([product], quantity=0.5) + self.assertEqual(prices, {product.id: Decimal('2267.96185')}) + prices = Product.get_sale_price([product], quantity=1.5) + self.assertEqual(prices, {product.id: Decimal('2267.96185')}) + def suite(): suite = trytond.tests.test_tryton.suite()