Created
May 19, 2021 06:27
-
-
Save victoraguilarc/560b9194ff54b4686e4456217ed718d7 to your computer and use it in GitHub Desktop.
Calculate Total and IVA
This file contains hidden or 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
from decimal import Decimal | |
from pprint import pprint | |
def cast_int(amount: Decimal) -> int: | |
return int(amount * 100) | |
def cast_decimal(amount: int) -> Decimal: | |
return Decimal(amount / 100).quantize(Decimal('1.00')) | |
def calculate_iva(amount: Decimal) -> int: | |
iva = amount * Decimal(0.16) | |
return cast_int(iva) | |
def calculate_monthly_payouts(initial_amount: Decimal, num_months: int) -> dict: | |
subtotal_amount = cast_int(initial_amount) | |
iva_amount = calculate_iva(initial_amount) | |
total_amount = subtotal_amount + iva_amount | |
monthly_payment = total_amount // num_months | |
unexpected_total = monthly_payment * num_months | |
last_payment = monthly_payment - (unexpected_total - total_amount) | |
plan = [cast_decimal(monthly_payment) for x in range(num_months - 1)] + [cast_decimal(last_payment)] | |
return { | |
'subtotal': cast_decimal(subtotal_amount), | |
'iva': cast_decimal(iva_amount), | |
'total': cast_decimal(total_amount), | |
'payments': plan, | |
} | |
def sum_items(items): | |
total_sum = 0 | |
for item in items: | |
total_sum += item | |
return total_sum | |
subtotal = Decimal(10585) | |
result = calculate_monthly_payouts(subtotal, 7) | |
pprint(result) | |
# Tests | |
assert sum_items(result['payments']) == result['total'] | |
# Specific value Tests | |
assert isinstance(result, dict) | |
assert result['subtotal'] == Decimal('10585.00') | |
assert result['iva'] == Decimal('1693.60') | |
assert result['total'] == Decimal('12278.60') | |
assert sum_items(result['payments']) == Decimal('12278.60') | |
print('Ok!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment