Revised to allow amounts in decimal numbers in the accounting application.

This commit is contained in:
依瑪貓
2020-08-21 10:00:59 +08:00
parent 50bc6fb0b1
commit 567a610e90
11 changed files with 42 additions and 34 deletions

View File

@ -20,6 +20,7 @@
"""
import datetime
import re
from decimal import Decimal
from typing import Optional, List, Dict
from django import forms
@ -57,12 +58,14 @@ class RecordForm(forms.Form):
error_messages={
"max_length": _("This summary is too long (max. 128 characters)."),
})
amount = forms.IntegerField(
min_value=1,
amount = forms.DecimalField(
max_digits=18,
decimal_places=2,
min_value=0.01,
error_messages={
"required": _("Please fill in the amount."),
"invalid": _("Please fill in a number."),
"min_value": _("The amount must be at least 1."),
"min_value": _("The amount must be more than 0."),
})
def __init__(self, *args, **kwargs):
@ -436,22 +439,22 @@ class TransactionForm(forms.Form):
return errors[0].message
return None
def debit_total(self) -> int:
def debit_total(self) -> Decimal:
"""Returns the total amount of the debit records.
Returns:
The total amount of the credit records.
"""
return sum([int(x.data["amount"]) for x in self.debit_records
return sum([Decimal(x.data["amount"]) for x in self.debit_records
if "amount" in x.data and "amount" not in x.errors])
def credit_total(self) -> int:
def credit_total(self) -> Decimal:
"""Returns the total amount of the credit records.
Returns:
The total amount of the credit records.
"""
return sum([int(x.data["amount"]) for x in self.credit_records
return sum([Decimal(x.data["amount"]) for x in self.credit_records
if "amount" in x.data and "amount" not in x.errors])