From: Raphael Michel Date: Wed, 3 Aug 2022 10:26:04 +0000 (+0200) Subject: Fix usage of CurrencyField vs DecimalField X-Git-Tag: 2.2.0~1^2~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ff9fa3d5d78ecc656487a867f2fccf2bc3d93f4f;p=thirdparty%2Fpython-drafthorse.git Fix usage of CurrencyField vs DecimalField --- diff --git a/drafthorse/models/accounting.py b/drafthorse/models/accounting.py index 640556f..fe22161 100644 --- a/drafthorse/models/accounting.py +++ b/drafthorse/models/accounting.py @@ -2,14 +2,13 @@ from . import BASIC, COMFORT, EXTENDED, NS_RAM from .elements import Element from .fields import ( CurrencyField, - Field, DateTimeField, DecimalField, IndicatorField, MultiField, QuantityField, StringField, - MultiDecimalField, + MultiCurrencyField, ) @@ -170,17 +169,20 @@ class MonetarySummation(Element): profile=BASIC, _d="Gesamtbetrag der Abschläge", ) - tax_basis_total = DecimalField( + tax_basis_total = CurrencyField( NS_RAM, "TaxBasisTotalAmount", required=True, profile=BASIC, _d="Steuerbasisbetrag", ) - tax_total = DecimalField( + tax_total = CurrencyField( NS_RAM, "TaxTotalAmount", required=True, profile=BASIC, _d="Steuergesamtbetrag" ) - grand_total = DecimalField( + tax_total_other_currency = MultiCurrencyField( + NS_RAM, "TaxTotalAmount", profile=EXTENDED, _d="Steuergesamtbetrag" + ) + grand_total = CurrencyField( NS_RAM, "GrandTotalAmount", required=True, profile=BASIC, _d="Bruttosumme" ) prepaid_total = DecimalField( diff --git a/drafthorse/models/container.py b/drafthorse/models/container.py index 4daa638..857da8d 100644 --- a/drafthorse/models/container.py +++ b/drafthorse/models/container.py @@ -61,7 +61,10 @@ class CurrencyContainer(SimpleContainer): el.currency = child[1] def add_from_etree(self, root): - self.add((root.text, root.attrib["currencyID"])) + if root.attrib.get("currencyID"): + self.add((root.text, root.attrib["currencyID"])) + else: + self.add(root.text) class IDContainer(SimpleContainer): diff --git a/drafthorse/models/elements.py b/drafthorse/models/elements.py index 884b135..a8aebe6 100644 --- a/drafthorse/models/elements.py +++ b/drafthorse/models/elements.py @@ -181,7 +181,7 @@ class QuantityElement(StringElement): class CurrencyElement(StringElement): - def __init__(self, namespace, tag, amount="", currency="EUR"): + def __init__(self, namespace, tag, amount="", currency=None): super().__init__(namespace, tag) self.amount = amount self.currency = currency @@ -189,12 +189,15 @@ class CurrencyElement(StringElement): def to_etree(self): node = self._etree_node() node.text = str(self.amount) - node.attrib["currencyID"] = self.currency + if self.currency is not None: + node.attrib["currencyID"] = self.currency + elif "currencyID" in node.attrib: + del node.attrib["currencyID"] return node def from_etree(self, root): self.amount = Decimal(root.text) - self.currency = root.attrib.get("currencyID", "EUR") + self.currency = root.attrib.get("currencyID") or None self.set_on_input = True return self diff --git a/drafthorse/models/fields.py b/drafthorse/models/fields.py index fb0a39e..5e9ea5a 100644 --- a/drafthorse/models/fields.py +++ b/drafthorse/models/fields.py @@ -132,6 +132,8 @@ class CurrencyField(Field): if instance._data.get(self.name, None) is None: instance._data[self.name] = self.initialize() + if isinstance(value, (int, float, Decimal)): + value = (value, None) if not isinstance(value, (tuple, list)): raise TypeError("Please pass a 2-tuple of including amount and currency.") @@ -288,7 +290,7 @@ class MultiStringField(Field): return self.cls(child_type=str, namespace=self.namespace, tag=self.tag) -class MultiDecimalField(Field): +class MultiCurrencyField(Field): def __init__( self, namespace, tag, default=False, required=False, profile=BASIC, _d=None ): diff --git a/drafthorse/models/payment.py b/drafthorse/models/payment.py index 353aa97..f97ea3b 100644 --- a/drafthorse/models/payment.py +++ b/drafthorse/models/payment.py @@ -5,12 +5,12 @@ from .fields import ( DateTimeField, DecimalField, Field, - MultiDecimalField, MultiStringField, QuantityField, StringField, DirectDateTimeField, IDField, + MultiCurrencyField, ) @@ -184,7 +184,7 @@ class PaymentTerms(Element): debit_mandate_id = StringField( NS_RAM, "DirectDebitMandateID", required=False, profile=BASIC ) - partial_amount = MultiDecimalField( + partial_amount = MultiCurrencyField( NS_RAM, "PartialPaymentAmount", profile=EXTENDED,