Import: Handle deposits, withdrawals and expenses
This commit is contained in:
@@ -76,7 +76,14 @@ def import_csv(filepath: str):
|
|||||||
)
|
)
|
||||||
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
if row["typ"].lower() not in ["essen", "essen kind"]:
|
transaction_type = row["typ"].lower()
|
||||||
|
if transaction_type not in [
|
||||||
|
"essen",
|
||||||
|
"essen kind",
|
||||||
|
"einzahlung",
|
||||||
|
"einkauf",
|
||||||
|
"auszahlung",
|
||||||
|
]:
|
||||||
# TODO: Handle other types
|
# TODO: Handle other types
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -91,36 +98,55 @@ def import_csv(filepath: str):
|
|||||||
db.add(account)
|
db.add(account)
|
||||||
db.flush()
|
db.flush()
|
||||||
|
|
||||||
if row["stueck"] == 0:
|
if transaction_type == "essen" or transaction_type == "essen kind":
|
||||||
# if no quantity is given, assume it's 1 for non-zero amounts
|
if row["stueck"] == 0:
|
||||||
quantity = row["betrag"] / (3 if row["typ"].lower() == "essen" else 1.5)
|
# if no quantity is given, assume it's 1 for non-zero amounts
|
||||||
else:
|
quantity = row["betrag"] / (3 if transaction_type == "essen" else 1.5)
|
||||||
quantity = row["stueck"]
|
else:
|
||||||
|
quantity = row["stueck"]
|
||||||
|
|
||||||
timestamp = datetime.combine(row["datum"], time(18, 30, 0))
|
timestamp = datetime.combine(row["datum"], time(18, 30, 0))
|
||||||
|
|
||||||
order = Order(user_id=1)
|
order = Order(user_id=1)
|
||||||
|
|
||||||
order.items.append(
|
order.items.append(
|
||||||
OrderItem(
|
OrderItem(
|
||||||
order=order,
|
order=order,
|
||||||
product=(
|
product=(
|
||||||
meal_product if row["typ"].lower() == "essen" else kids_meal_product
|
meal_product
|
||||||
),
|
if transaction_type == "essen"
|
||||||
|
else kids_meal_product
|
||||||
|
),
|
||||||
|
total_amount=row["betrag"],
|
||||||
|
quantity=quantity,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
transaction = Transaction(
|
||||||
|
type="order",
|
||||||
|
timestamp=timestamp,
|
||||||
total_amount=row["betrag"],
|
total_amount=row["betrag"],
|
||||||
quantity=quantity,
|
quantity=quantity,
|
||||||
|
account_id=account.id,
|
||||||
|
order=order,
|
||||||
)
|
)
|
||||||
)
|
db.add(transaction)
|
||||||
|
elif transaction_type == "einzahlung" or transaction_type == "einkauf":
|
||||||
transaction = Transaction(
|
transaction = Transaction(
|
||||||
type="order",
|
type="deposit",
|
||||||
timestamp=timestamp,
|
timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
|
||||||
total_amount=row["betrag"],
|
total_amount=row["betrag"],
|
||||||
quantity=quantity,
|
account_id=account.id,
|
||||||
account_id=account.id,
|
)
|
||||||
order=order,
|
db.add(transaction)
|
||||||
)
|
elif transaction_type == "auszahlung":
|
||||||
db.add(transaction)
|
transaction = Transaction(
|
||||||
|
type="withdrawal",
|
||||||
|
timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
|
||||||
|
total_amount=row["betrag"],
|
||||||
|
account_id=account.id,
|
||||||
|
)
|
||||||
|
db.add(transaction)
|
||||||
db.flush()
|
db.flush()
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user