]> git.ipfire.org Git - thirdparty/python-drafthorse.git/commitdiff
Fix usage of CurrencyField vs DecimalField
authorRaphael Michel <michel@rami.io>
Wed, 3 Aug 2022 10:26:04 +0000 (12:26 +0200)
committerRaphael Michel <michel@rami.io>
Wed, 3 Aug 2022 10:26:04 +0000 (12:26 +0200)
drafthorse/models/accounting.py
drafthorse/models/container.py
drafthorse/models/elements.py
drafthorse/models/fields.py
drafthorse/models/payment.py

index 640556f86a2b1a7eac6715befd2ffd810de0fb19..fe22161cda686ef9ec6010594340cb939c5becdd 100644 (file)
@@ -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(
index 4daa638b87f27095421f510dd6619d358772fe96..857da8d9ee095d6e218f23f5af40db70fe679e32 100644 (file)
@@ -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):
index 884b135ec62bca056ea0b1b66cb8781dabfc6f75..a8aebe6cd06b26c293a37bae7e6236e4e842476c 100644 (file)
@@ -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
 
index fb0a39ec5078e1e603467958b9b04a1e5b7c7be0..5e9ea5a3984183c4b58f60281370e24541bd1676 100644 (file)
@@ -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
     ):
index 353aa9777fdf91fa3dee49d3454e9ff78290d8a3..f97ea3b8e0fc972127ebcfb344da2784e0e83078 100644 (file)
@@ -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,