Merge branch 'morfo'
This commit is contained in:
@@ -60,9 +60,10 @@ class AmountCollection(Collection):
|
||||
|
||||
class Amount:
|
||||
def __init__(
|
||||
self, amount: typing.Union[int, float, str, "Amount"],
|
||||
currency: Currency = Currency('COP')):
|
||||
|
||||
self, amount: int or float or str,
|
||||
currency: Currency = Currency('COP'),
|
||||
precision=DECIMAL_PRECISION):
|
||||
self.precision = precision
|
||||
# DIAN 1.7.-2020: 1.2.3.1
|
||||
if isinstance(amount, Amount):
|
||||
if amount < Amount(0.0):
|
||||
@@ -76,9 +77,10 @@ class Amount:
|
||||
|
||||
self.amount = Decimal(
|
||||
amount, decimal.Context(
|
||||
prec=DECIMAL_PRECISION,
|
||||
prec=self.precision,
|
||||
# DIAN 1.7.-2020: 1.2.1.1
|
||||
rounding=decimal.ROUND_HALF_EVEN))
|
||||
rounding=decimal.ROUND_HALF_EVEN)
|
||||
)
|
||||
self.currency = currency
|
||||
|
||||
def fromNumber(self, val):
|
||||
@@ -96,20 +98,24 @@ class Amount:
|
||||
def __lt__(self, other):
|
||||
if not self.is_same_currency(other):
|
||||
raise AmountCurrencyError()
|
||||
return round(self.amount, DECIMAL_PRECISION) < round(other, 2)
|
||||
return round(self.amount, self.precision) < round(other, 2)
|
||||
|
||||
def __eq__(self, other):
|
||||
if not self.is_same_currency(other):
|
||||
raise AmountCurrencyError()
|
||||
return round(self.amount, DECIMAL_PRECISION) == round(
|
||||
other.amount, DECIMAL_PRECISION)
|
||||
return round(self.amount, self.precision) == round(
|
||||
other.amount, self.precision)
|
||||
|
||||
def _cast(self, val):
|
||||
if type(val) in [int, float]:
|
||||
return self.fromNumber(val)
|
||||
if isinstance(val, Amount):
|
||||
return val
|
||||
raise TypeError("cant cast to amount")
|
||||
|
||||
if isinstance(val, Decimal):
|
||||
return self.fromNumber(float(val))
|
||||
|
||||
raise TypeError("cant cast %s to amount" % (type(val)))
|
||||
|
||||
def __add__(self, rother):
|
||||
other = self._cast(rother)
|
||||
|
||||
Reference in New Issue
Block a user