sign field instead of amount_type
This commit is contained in:
+24
-17
@@ -15,14 +15,11 @@ class Currency(str, Enum):
|
||||
GBP = "GBP"
|
||||
CHF = "CHF"
|
||||
|
||||
def __str__(self):
|
||||
return self.value
|
||||
|
||||
class AmountType(str, Enum):
|
||||
DBIT = "DBIT"
|
||||
CRDT = "CRDT"
|
||||
|
||||
@property
|
||||
def sign(self) -> int:
|
||||
return -1 if self == self.DBIT else +1
|
||||
def __repr__(self):
|
||||
return f"Currency.{self.name}"
|
||||
|
||||
|
||||
class B4wExport(BaseModel):
|
||||
@@ -32,17 +29,28 @@ class B4wExport(BaseModel):
|
||||
date_valid: date = Field(alias="ValDt")
|
||||
amount: Decimal = Field(alias="Amt")
|
||||
amount_currency: Currency = Field(alias="AmtCcy")
|
||||
amount_type: AmountType = Field(alias="CdtDbtInd")
|
||||
reference: str = Field(alias = "RmtInf")
|
||||
other_name: str = Field(alias = "RmtdNm")
|
||||
other_iban: str = Field(alias = "RmtdAcctIBAN")
|
||||
category: Category | None = Field(alias = "Category")
|
||||
notes: str = Field(alias = "Notes")
|
||||
|
||||
sign: int = Field(alias="CdtDbtInd")
|
||||
reference: str = Field(alias="RmtInf")
|
||||
other_name: str = Field(alias="RmtdNm")
|
||||
other_iban: str = Field(alias="RmtdAcctIBAN")
|
||||
category: Category | None = Field(alias="Category")
|
||||
notes: str = Field(alias="Notes")
|
||||
|
||||
@property
|
||||
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")
|
||||
@classmethod
|
||||
@@ -55,7 +63,6 @@ class B4wExport(BaseModel):
|
||||
return category
|
||||
|
||||
|
||||
|
||||
class Category(BaseModel):
|
||||
name: str = Field(pattern=r"[^:\r\n]+")
|
||||
parent: Category | None = None
|
||||
@@ -79,4 +86,4 @@ class Category(BaseModel):
|
||||
return self.to_str()
|
||||
|
||||
def __repr__(self):
|
||||
return f"Category.from_str(\"{self.__str__()}\")"
|
||||
return f'Category.from_str("{self.__str__()}")'
|
||||
|
||||
Reference in New Issue
Block a user