Fix floating-point issue on bills (Fix #31)
This commit is contained in:
parent
444d6ded28
commit
74e2168529
2 changed files with 8 additions and 1 deletions
|
@ -1,9 +1,12 @@
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
import decimal
|
||||||
|
|
||||||
# Define DecimalField properties, used to represent amounts of money.
|
# Define DecimalField properties, used to represent amounts of money.
|
||||||
AMOUNT_MAX_DIGITS=10
|
AMOUNT_MAX_DIGITS=10
|
||||||
AMOUNT_DECIMALS=2
|
AMOUNT_DECIMALS=2
|
||||||
|
|
||||||
|
decimal.getcontext().prec = AMOUNT_DECIMALS
|
||||||
|
|
||||||
# http://xml.coverpages.org/country3166.html
|
# http://xml.coverpages.org/country3166.html
|
||||||
COUNTRIES = (
|
COUNTRIES = (
|
||||||
('AD', _('Andorra')),
|
('AD', _('Andorra')),
|
||||||
|
|
|
@ -494,6 +494,10 @@ class BillRecord():
|
||||||
else:
|
else:
|
||||||
self.one_time_price = 0
|
self.one_time_price = 0
|
||||||
|
|
||||||
|
# Set decimal context for amount computations.
|
||||||
|
# XXX: understand why we need +1 here.
|
||||||
|
decimal.getcontext().prec = AMOUNT_DECIMALS + 1
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def recurring_count(self):
|
def recurring_count(self):
|
||||||
# Compute billing delta.
|
# Compute billing delta.
|
||||||
|
@ -531,7 +535,7 @@ class BillRecord():
|
||||||
(_, days_in_month) = monthrange(
|
(_, days_in_month) = monthrange(
|
||||||
self.bill.starting_date.year,
|
self.bill.starting_date.year,
|
||||||
self.bill.starting_date.month)
|
self.bill.starting_date.month)
|
||||||
return days / days_in_month
|
return round(days / days_in_month, AMOUNT_DECIMALS)
|
||||||
elif self.recurring_period == RecurringPeriod.PER_WEEK:
|
elif self.recurring_period == RecurringPeriod.PER_WEEK:
|
||||||
weeks = ceil(billed_delta / timedelta(week=1))
|
weeks = ceil(billed_delta / timedelta(week=1))
|
||||||
return weeks
|
return weeks
|
||||||
|
|
Loading…
Reference in a new issue