changeset a04f626934c1 in modules/sale:default
details: https://hg.tryton.org/modules/sale?cmd=changeset;node=a04f626934c1
description:
        Use sale_uom as fallback uom to compute sale price

        issue7855
        review45821002
diffstat:

 CHANGELOG          |   2 +
 product.py         |   5 +++-
 tests/test_sale.py |  55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 60 insertions(+), 2 deletions(-)

diffs (104 lines):

diff -r 39472bd8e3cc -r a04f626934c1 CHANGELOG
--- a/CHANGELOG Sun Nov 18 12:11:19 2018 +0100
+++ b/CHANGELOG Sat Dec 22 00:16:15 2018 +0100
@@ -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 -r 39472bd8e3cc -r a04f626934c1 product.py
--- a/product.py        Sun Nov 18 12:11:19 2018 +0100
+++ b/product.py        Sat Dec 22 00:16:15 2018 +0100
@@ -94,7 +94,7 @@
         '''
         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 @@
             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 -r 39472bd8e3cc -r a04f626934c1 tests/test_sale.py
--- a/tests/test_sale.py        Sun Nov 18 12:11:19 2018 +0100
+++ b/tests/test_sale.py        Sat Dec 22 00:16:15 2018 +0100
@@ -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()

Reply via email to