transactions now use their orders total_amount
This commit is contained in:
52
alembic/versions/5ea1426af0d0_transaction_refact.py
Normal file
52
alembic/versions/5ea1426af0d0_transaction_refact.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
"""transaction refact
|
||||||
|
|
||||||
|
Revision ID: 5ea1426af0d0
|
||||||
|
Revises: 3b61a6fbc6b2
|
||||||
|
Create Date: 2026-05-17 11:34:08.045462
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = "5ea1426af0d0"
|
||||||
|
down_revision: Union[str, Sequence[str], None] = "3b61a6fbc6b2"
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
"""Upgrade schema."""
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column(
|
||||||
|
"aps_transaction",
|
||||||
|
sa.Column("_total_amount", sa.Numeric(precision=10, scale=2), nullable=True),
|
||||||
|
)
|
||||||
|
# copy data from total_amount to _total_amount
|
||||||
|
conn = op.get_bind()
|
||||||
|
transactions = conn.execute(
|
||||||
|
sa.text("SELECT id, total_amount FROM aps_transaction")
|
||||||
|
).fetchall()
|
||||||
|
for transaction in transactions:
|
||||||
|
conn.execute(
|
||||||
|
sa.text(
|
||||||
|
"UPDATE aps_transaction SET _total_amount = :total_amount WHERE id = :id"
|
||||||
|
),
|
||||||
|
{"total_amount": transaction.total_amount, "id": transaction.id},
|
||||||
|
)
|
||||||
|
op.drop_column("aps_transaction", "total_amount")
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
"""Downgrade schema."""
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column(
|
||||||
|
"aps_transaction",
|
||||||
|
sa.Column("total_amount", sa.NUMERIC(precision=10, scale=2), nullable=False),
|
||||||
|
)
|
||||||
|
op.drop_column("aps_transaction", "_total_amount")
|
||||||
|
# ### end Alembic commands ###
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
from collections import defaultdict
|
||||||
import csv
|
import csv
|
||||||
from datetime import datetime, time
|
from datetime import datetime, time
|
||||||
|
from decimal import Decimal
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from sqlalchemy import select
|
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."
|
"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:
|
for row in rows:
|
||||||
transaction_type = row["typ"].lower()
|
transaction_type = row["typ"].lower()
|
||||||
if transaction_type not in [
|
if transaction_type not in [
|
||||||
@@ -107,7 +113,7 @@ def import_csv(filepath: str):
|
|||||||
|
|
||||||
timestamp = datetime.combine(row["datum"], time(18, 30, 0))
|
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(
|
order.items.append(
|
||||||
OrderItem(
|
OrderItem(
|
||||||
@@ -121,21 +127,22 @@ def import_csv(filepath: str):
|
|||||||
quantity=quantity,
|
quantity=quantity,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
if not order.transaction:
|
||||||
transaction = Transaction(
|
transaction = Transaction(
|
||||||
type="order",
|
type="order",
|
||||||
timestamp=timestamp,
|
timestamp=timestamp,
|
||||||
total_amount=row["betrag"],
|
quantity=quantity,
|
||||||
quantity=quantity,
|
account_id=account.id,
|
||||||
account_id=account.id,
|
order=order,
|
||||||
order=order,
|
)
|
||||||
)
|
db.add(transaction)
|
||||||
db.add(transaction)
|
else:
|
||||||
|
order.transaction._total_amount = Decimal(order.total_amount)
|
||||||
elif transaction_type == "einzahlung" or transaction_type == "einkauf":
|
elif transaction_type == "einzahlung" or transaction_type == "einkauf":
|
||||||
transaction = Transaction(
|
transaction = Transaction(
|
||||||
type="deposit",
|
type="deposit",
|
||||||
timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
|
timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
|
||||||
total_amount=row["betrag"],
|
_total_amount=row["betrag"],
|
||||||
account_id=account.id,
|
account_id=account.id,
|
||||||
)
|
)
|
||||||
db.add(transaction)
|
db.add(transaction)
|
||||||
@@ -143,7 +150,7 @@ def import_csv(filepath: str):
|
|||||||
transaction = Transaction(
|
transaction = Transaction(
|
||||||
type="withdrawal",
|
type="withdrawal",
|
||||||
timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
|
timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
|
||||||
total_amount=row["betrag"],
|
_total_amount=row["betrag"],
|
||||||
account_id=account.id,
|
account_id=account.id,
|
||||||
)
|
)
|
||||||
db.add(transaction)
|
db.add(transaction)
|
||||||
|
|||||||
@@ -281,8 +281,8 @@ class Transaction(Base):
|
|||||||
timestamp: Mapped[datetime.datetime] = mapped_column(
|
timestamp: Mapped[datetime.datetime] = mapped_column(
|
||||||
nullable=False, default=datetime.datetime.now
|
nullable=False, default=datetime.datetime.now
|
||||||
)
|
)
|
||||||
total_amount: Mapped[decimal.Decimal] = mapped_column(
|
_total_amount: Mapped[decimal.Decimal] = mapped_column(
|
||||||
Numeric(10, 2), nullable=False
|
Numeric(10, 2), nullable=True
|
||||||
)
|
)
|
||||||
|
|
||||||
order_id: Mapped[int] = mapped_column(
|
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_id: Mapped[int] = mapped_column(ForeignKey(TABLE_PREFIX + "account.id"))
|
||||||
account: Mapped["Account"] = relationship("Account", back_populates="transactions")
|
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 %}">
|
<span class="fs-5 fw-bold {% if transaction.total_amount < 0 %}text-danger{% else %}text-success{% endif %}">
|
||||||
{{ transaction.total_amount | format_number }} €
|
{{ transaction.total_amount | format_number }} €
|
||||||
</span>
|
</span>
|
||||||
{% if transaction.quantity %}
|
|
||||||
<div class="small text-muted">{{ transaction.quantity }}</div>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|||||||
Reference in New Issue
Block a user