diff --git a/examples/voucherify_test.py b/examples/voucherify_test.py index 7b897ba8..6fef7a22 100644 --- a/examples/voucherify_test.py +++ b/examples/voucherify_test.py @@ -32,6 +32,24 @@ Get voucher ''' +pprint.pprint('=== Get voucher ===') +voucher = client.get(voucher['code']) +pprint.pprint(voucher) + ''' -Validate voucher +Utils ''' + +pprint.pprint('=== Calculate Discount Amount and Price after discount===') + +unit_price = 83.45 +items_count = 13 +base_price = unit_price * items_count + +discount = voucherify.utils.calculate_discount(base_price, voucher, unit_price) +new_price = voucherify.utils.calculate_price(base_price, voucher, unit_price) +pprint.pprint(unit_price) +pprint.pprint(items_count) +pprint.pprint(base_price) +pprint.pprint(new_price) +pprint.pprint(discount) diff --git a/voucherify/__init__.py b/voucherify/__init__.py index 0d509bfd..b4f556f7 100644 --- a/voucherify/__init__.py +++ b/voucherify/__init__.py @@ -1,2 +1,2 @@ from voucherify.client import Client -from voucherify.utils import calculatePrice, calculatePrice +import voucherify.utils as utils diff --git a/voucherify/utils.py b/voucherify/utils.py index 98fb6f95..552374ea 100644 --- a/voucherify/utils.py +++ b/voucherify/utils.py @@ -1,28 +1,73 @@ +import pprint + def round_money(value): - """@TODO""" - return None + if value is None or value < 0: + raise Exception('Invalid value, amount should be a number and higher than zero.') + return round(value, 2) def validate_percent_discount(discount): - """@TODO""" - return None + if discount is None or discount < 0 or discount > 100: + raise Exception('Invalid voucher, percent discount should be between 0-100.') + + +def validate_amount_discount(discount=None): + if discount is None or discount < 0: + raise Exception('Invalid voucher, amount discount must be higher than zero.') + + +def validate_unit_discount(discount=None): + if discount is None or discount < 0: + raise Exception('Invalid voucher, unit discount must be higher than zero.') + + +def calculate_price(base_price, voucher, unit_price): + e = 100 + + if voucher['discount']['type'] == 'PERCENT': + discount = voucher['discount']['percent_off'] + validate_percent_discount(discount) + price_discount = base_price * (discount / 100) + return round_money(base_price - price_discount) + + elif voucher['discount']['type'] == 'AMOUNT': + discount = voucher['discount']['amount_off'] / e + validate_amount_discount(discount) + new_price = base_price - discount + return round_money(new_price if new_price > 0 else 0) + + elif voucher['discount']['type'] == 'UNIT': + discount = voucher['discount']['unit_off'] + validate_unit_discount(discount) + new_price = base_price - unit_price * discount + return round_money(new_price if new_price > 0 else 0) + + else: + raise Exception('Unsupported voucher type.') -def validate_amount_discount(discount): - """@TODO""" - return None +def calculate_discount(base_price, voucher, unit_price): + e = 100 + if voucher['discount']['type'] == 'PERCENT': + discount = voucher['discount']['percent_off'] + validate_percent_discount(discount) + return round_money(base_price * (discount / 100)) -def validate_unit_discount(discount): - """@TODO""" - return None + elif voucher['discount']['type'] == 'AMOUNT': + discount = voucher['discount']['amount_off'] / e + validate_amount_discount(discount) + new_price = base_price - discount + return round_money(discount if new_price > 0 else base_price) + elif voucher['discount']['type'] == 'UNIT': + discount = voucher['discount']['unit_off'] + validate_unit_discount(discount) + price_discount = unit_price * discount + return round_money(base_price if price_discount > base_price else price_discount) -def calculatePrice(base_price, voucher, unit_price): - """@TODO""" - return 0 + else: + raise Exception('Unsupported voucher type.') -def calculateDiscount(base_price, voucher, unit_price): - """@TODO""" - return 0 +__all__ = ['calculate_price', 'calculate_discount']