diff --git a/facho/fe/fe.py b/facho/fe/fe.py index 054740c..2cc3b47 100644 --- a/facho/fe/fe.py +++ b/facho/fe/fe.py @@ -183,14 +183,14 @@ class DianXMLExtensionCUFE(DianXMLExtensionCUDFE): '%s' % build_vars['NumFac'], '%s' % build_vars['FecFac'], '%s' % build_vars['HoraFac'], - '%.02f' % round(build_vars['ValorBruto'], 2), + form.Amount(build_vars['ValorBruto']).format('%.02f'), '%02d' % CodImpuesto1, - '%.02f' % round(build_vars['ValorImpuestoPara'].get(CodImpuesto1, 0.0), 2), + form.Amount(build_vars['ValorImpuestoPara'].get(CodImpuesto1, 0.0)).format('%.02f'), '%02d' % CodImpuesto2, - '%.02f' % round(build_vars['ValorImpuestoPara'].get(CodImpuesto2, 0.0), 2), + form.Amount(build_vars['ValorImpuestoPara'].get(CodImpuesto2, 0.0)).format('%.02f'), '%02d' % CodImpuesto3, - '%.02f' % round(build_vars['ValorImpuestoPara'].get(CodImpuesto3, 0.0), 2), - '%.02f' % round(build_vars['ValorTotalPagar'], 2), + form.Amount(build_vars['ValorImpuestoPara'].get(CodImpuesto3, 0.0)).format('%.02f'), + form.Amount(build_vars['ValorTotalPagar']).format('%.02f'), '%s' % build_vars['NitOFE'], '%s' % build_vars['NumAdq'], '%s' % build_vars['ClTec'], @@ -220,14 +220,14 @@ class DianXMLExtensionCUDE(DianXMLExtensionCUDFE): '%s' % build_vars['NumFac'], '%s' % build_vars['FecFac'], '%s' % build_vars['HoraFac'], - '%.02f' % round(build_vars['ValorBruto'], 2), + form.Amount(build_vars['ValorBruto']).format('%.02f'), '%02d' % CodImpuesto1, - '%.02f' % round(build_vars['ValorImpuestoPara'].get(CodImpuesto1, 0.0), 2), + form.Amount(build_vars['ValorImpuestoPara'].get(CodImpuesto1, 0.0)).format('%.02f'), '%02d' % CodImpuesto2, - '%.02f' % round(build_vars['ValorImpuestoPara'].get(CodImpuesto2, 0.0), 2), + form.Amount(build_vars['ValorImpuestoPara'].get(CodImpuesto2, 0.0)).format('%.02f'), '%02d' % CodImpuesto3, - '%.02f' % round(build_vars['ValorImpuestoPara'].get(CodImpuesto3, 0.0), 2), - '%.02f' % round(build_vars['ValorTotalPagar'], 2), + form.Amount(build_vars['ValorImpuestoPara'].get(CodImpuesto3, 0.0)).format('%.02f'), + form.Amount(build_vars['ValorTotalPagar']).format('%.02f'), '%s' % build_vars['NitOFE'], '%s' % build_vars['NumAdq'], '%s' % build_vars['Software-PIN'], diff --git a/facho/fe/form/__init__.py b/facho/fe/form/__init__.py index 21a9749..56e8148 100644 --- a/facho/fe/form/__init__.py +++ b/facho/fe/form/__init__.py @@ -56,24 +56,33 @@ class Amount: def __init__(self, amount: int or float or Amount, currency: Currency = Currency('COP')): #DIAN 1.7.-2020: 1.2.3.1 - if amount < 0: - raise ValueError('amount must be positive >= 0') - if isinstance(amount, Amount): + if amount < Amount(0.0): + raise ValueError('amount must be positive >= 0') + self.amount = amount.amount self.currency = amount.currency else: + if amount < 0: + raise ValueError('amount must be positive >= 0') + self.amount = Decimal(amount, decimal.Context(prec=DECIMAL_PRECISION, #DIAN 1.7.-2020: 1.2.1.1 rounding=decimal.ROUND_HALF_EVEN )) self.currency = currency + def __round__(self, prec): return round(self.amount, prec) def __str__(self): return '%.06f' % self.amount + def __lt__(self, other): + if not self.is_same_currency(other): + raise AmountCurrencyError() + return round(self.amount, DECIMAL_PRECISION) < round(other, 2) + def __eq__(self, other): if not self.is_same_currency(other): raise AmountCurrencyError() @@ -97,6 +106,12 @@ class Amount: def is_same_currency(self, other): return self.currency == other.currency + def format(self, formatter): + return formatter % self.float() + + def float(self): + return round(self.amount, DECIMAL_PRECISION) + class Quantity(Amount): pass