transactions now use their orders total_amount
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import argparse
|
||||
from collections import defaultdict
|
||||
import csv
|
||||
from datetime import datetime, time
|
||||
from decimal import Decimal
|
||||
import sys
|
||||
|
||||
from sqlalchemy import select
|
||||
@@ -75,6 +77,10 @@ def import_csv(filepath: str):
|
||||
"Error: 'Essen Kind' product not found in database. Please create it before importing."
|
||||
)
|
||||
|
||||
orders_by_account_and_date = defaultdict(
|
||||
lambda: defaultdict(lambda: Order(user_id=1))
|
||||
)
|
||||
|
||||
for row in rows:
|
||||
transaction_type = row["typ"].lower()
|
||||
if transaction_type not in [
|
||||
@@ -107,7 +113,7 @@ def import_csv(filepath: str):
|
||||
|
||||
timestamp = datetime.combine(row["datum"], time(18, 30, 0))
|
||||
|
||||
order = Order(user_id=1)
|
||||
order = orders_by_account_and_date[account.id][row["datum"]]
|
||||
|
||||
order.items.append(
|
||||
OrderItem(
|
||||
@@ -121,21 +127,22 @@ def import_csv(filepath: str):
|
||||
quantity=quantity,
|
||||
)
|
||||
)
|
||||
|
||||
transaction = Transaction(
|
||||
type="order",
|
||||
timestamp=timestamp,
|
||||
total_amount=row["betrag"],
|
||||
quantity=quantity,
|
||||
account_id=account.id,
|
||||
order=order,
|
||||
)
|
||||
db.add(transaction)
|
||||
if not order.transaction:
|
||||
transaction = Transaction(
|
||||
type="order",
|
||||
timestamp=timestamp,
|
||||
quantity=quantity,
|
||||
account_id=account.id,
|
||||
order=order,
|
||||
)
|
||||
db.add(transaction)
|
||||
else:
|
||||
order.transaction._total_amount = Decimal(order.total_amount)
|
||||
elif transaction_type == "einzahlung" or transaction_type == "einkauf":
|
||||
transaction = Transaction(
|
||||
type="deposit",
|
||||
timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
|
||||
total_amount=row["betrag"],
|
||||
_total_amount=row["betrag"],
|
||||
account_id=account.id,
|
||||
)
|
||||
db.add(transaction)
|
||||
@@ -143,7 +150,7 @@ def import_csv(filepath: str):
|
||||
transaction = Transaction(
|
||||
type="withdrawal",
|
||||
timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
|
||||
total_amount=row["betrag"],
|
||||
_total_amount=row["betrag"],
|
||||
account_id=account.id,
|
||||
)
|
||||
db.add(transaction)
|
||||
|
||||
@@ -281,8 +281,8 @@ class Transaction(Base):
|
||||
timestamp: Mapped[datetime.datetime] = mapped_column(
|
||||
nullable=False, default=datetime.datetime.now
|
||||
)
|
||||
total_amount: Mapped[decimal.Decimal] = mapped_column(
|
||||
Numeric(10, 2), nullable=False
|
||||
_total_amount: Mapped[decimal.Decimal] = mapped_column(
|
||||
Numeric(10, 2), nullable=True
|
||||
)
|
||||
|
||||
order_id: Mapped[int] = mapped_column(
|
||||
@@ -292,3 +292,9 @@ class Transaction(Base):
|
||||
|
||||
account_id: Mapped[int] = mapped_column(ForeignKey(TABLE_PREFIX + "account.id"))
|
||||
account: Mapped["Account"] = relationship("Account", back_populates="transactions")
|
||||
|
||||
@property
|
||||
def total_amount(self):
|
||||
if self.type == "order" and self.order is not None:
|
||||
return self.order.total_amount
|
||||
return self._total_amount
|
||||
|
||||
@@ -50,9 +50,6 @@
|
||||
<span class="fs-5 fw-bold {% if transaction.total_amount < 0 %}text-danger{% else %}text-success{% endif %}">
|
||||
{{ transaction.total_amount | format_number }} €
|
||||
</span>
|
||||
{% if transaction.quantity %}
|
||||
<div class="small text-muted">{{ transaction.quantity }}</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
Reference in New Issue
Block a user