sign field instead of amount_type

This commit is contained in:
2026-09-02 23:07:41 +02:00
parent 3c5cd68553
commit c23c87d5f6
+19 -12
View File
@@ -15,14 +15,11 @@ class Currency(str, Enum):
GBP = "GBP" GBP = "GBP"
CHF = "CHF" CHF = "CHF"
def __str__(self):
return self.value
class AmountType(str, Enum): def __repr__(self):
DBIT = "DBIT" return f"Currency.{self.name}"
CRDT = "CRDT"
@property
def sign(self) -> int:
return -1 if self == self.DBIT else +1
class B4wExport(BaseModel): class B4wExport(BaseModel):
@@ -32,17 +29,28 @@ class B4wExport(BaseModel):
date_valid: date = Field(alias="ValDt") date_valid: date = Field(alias="ValDt")
amount: Decimal = Field(alias="Amt") amount: Decimal = Field(alias="Amt")
amount_currency: Currency = Field(alias="AmtCcy") amount_currency: Currency = Field(alias="AmtCcy")
amount_type: AmountType = Field(alias="CdtDbtInd") sign: int = Field(alias="CdtDbtInd")
reference: str = Field(alias="RmtInf") reference: str = Field(alias="RmtInf")
other_name: str = Field(alias="RmtdNm") other_name: str = Field(alias="RmtdNm")
other_iban: str = Field(alias="RmtdAcctIBAN") other_iban: str = Field(alias="RmtdAcctIBAN")
category: Category | None = Field(alias="Category") category: Category | None = Field(alias="Category")
notes: str = Field(alias="Notes") notes: str = Field(alias="Notes")
@property @property
def amount_signed(self) -> Decimal: def amount_signed(self) -> Decimal:
return self.amount * self.amount_type.sign return self.amount * self.sign
@field_validator("sign", mode="before")
@classmethod
def get_sign(cls, input_value: str) -> int:
if input_value == "DBIT":
return -1
elif input_value == "CRDT":
return 1
else:
raise ValidationError(
f"Invalid amount type: {input_value}. Expected DBIT or CRDT"
)
@field_validator("category", mode="before") @field_validator("category", mode="before")
@classmethod @classmethod
@@ -55,7 +63,6 @@ class B4wExport(BaseModel):
return category return category
class Category(BaseModel): class Category(BaseModel):
name: str = Field(pattern=r"[^:\r\n]+") name: str = Field(pattern=r"[^:\r\n]+")
parent: Category | None = None parent: Category | None = None
@@ -79,4 +86,4 @@ class Category(BaseModel):
return self.to_str() return self.to_str()
def __repr__(self): def __repr__(self):
return f"Category.from_str(\"{self.__str__()}\")" return f'Category.from_str("{self.__str__()}")'