-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Voucherify util methods to Python SDK
- Loading branch information
Jakub Reczko
committed
Jun 3, 2016
1 parent
831141a
commit 20121f6
Showing
3 changed files
with
81 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from voucherify.client import Client | ||
from voucherify.utils import calculatePrice, calculatePrice | ||
import voucherify.utils as utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] |