Commit 9af12423 authored by Jérome Perrin's avatar Jérome Perrin

trade: prevent ZeroDivisionError for 0 quantity when using base_price_per_slice

parent b2e93a3f
Pipeline #38757 passed with stage
in 0 seconds
...@@ -24,16 +24,19 @@ result = context.getPriceParameterDict(context=movement, **kw) ...@@ -24,16 +24,19 @@ result = context.getPriceParameterDict(context=movement, **kw)
if result["slice_base_price"]: if result["slice_base_price"]:
total_price = 0. total_price = 0.
quantity = movement.getQuantity() quantity = movement.getQuantity()
sliced_base_price_list = zip(result["slice_base_price"], result["slice_quantity_range"]) if quantity:
for slice_price, slice_range in sliced_base_price_list: sliced_base_price_list = zip(result["slice_base_price"], result["slice_quantity_range"])
slice_min, slice_max = slice_range for slice_price, slice_range in sliced_base_price_list:
if slice_max is None: slice_min, slice_max = slice_range
slice_max = quantity + 1 if slice_max is None:
if slice_min == 0: slice_max = quantity + 1
slice_min = 1 if slice_min == 0:
priced_quantity = min(slice_max - 1, quantity) - (slice_min - 1) slice_min = 1
total_price += priced_quantity * slice_price priced_quantity = min(slice_max - 1, quantity) - (slice_min - 1)
result["base_price"] = total_price / quantity total_price += priced_quantity * slice_price
result["base_price"] = total_price / quantity
else:
result["base_price"] = 0.
base_price = result["base_price"] base_price = result["base_price"]
if base_price in (None, ""): if base_price in (None, ""):
......
...@@ -1338,6 +1338,7 @@ class TestResource(ERP5TypeTestCase): ...@@ -1338,6 +1338,7 @@ class TestResource(ERP5TypeTestCase):
{'quantity': 21, 'price': 198./21, 'total_price': 198.}, {'quantity': 21, 'price': 198./21, 'total_price': 198.},
{'quantity': 22, 'price': 206./22, 'total_price': 206.}, {'quantity': 22, 'price': 206./22, 'total_price': 206.},
{'quantity': 25, 'price': 230./25, 'total_price': 230.}, {'quantity': 25, 'price': 230./25, 'total_price': 230.},
{'quantity': 0., 'price': 0., 'total_price': 0.},
]: ]:
_test(**case) _test(**case)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment