feat: add payment stornation with history-preserving allocations and card layout fixes

This commit is contained in:
Арсентьев Александр
2026-09-27 20:58:19 +03:00
parent 94571f9684
commit ea183854f8
13 changed files with 1365 additions and 34 deletions
+30 -1
View File
@@ -165,6 +165,8 @@ def save_payment(db: Session, project_id: int, payment_date: date,
).first()
if payment is None:
raise PaymentValidationError("Платёж не найден")
if payment.status == "cancelled":
raise PaymentValidationError("Сторнированный платёж нельзя изменить")
else:
payment = Payment(project_id=project_id)
db.add(payment)
@@ -188,13 +190,15 @@ def save_payment(db: Session, project_id: int, payment_date: date,
def delete_payment(db: Session, project_id: int, payment_id: int):
"""Delete a payment and its allocations atomically."""
"""Delete a payment and its allocations atomically. Blocked for cancelled payments."""
try:
payment = db.query(Payment).filter(
Payment.id == payment_id, Payment.project_id == project_id
).first()
if payment is None:
raise PaymentValidationError("Платёж не найден")
if payment.status == "cancelled":
raise PaymentValidationError("Сторнированный платёж нельзя удалить")
db.delete(payment)
db.commit()
except Exception:
@@ -202,6 +206,31 @@ def delete_payment(db: Session, project_id: int, payment_id: int):
raise
def cancel_payment(db: Session, project_id: int, payment_id: int):
"""
Storno a payment atomically.
The payment is kept in history with status=cancelled. Its allocations are
preserved as historical records but excluded from active payment aggregates
(received / distributed / unallocated / outstanding) via status filters.
"""
try:
payment = db.query(Payment).filter(
Payment.id == payment_id, Payment.project_id == project_id
).first()
if payment is None:
raise PaymentValidationError("Платёж не найден")
if payment.status == "cancelled":
raise PaymentValidationError("Платёж уже сторнирован")
payment.status = "cancelled"
db.commit()
db.refresh(payment)
return payment
except Exception:
db.rollback()
raise
def project_payments(db: Session, project_id: int):
"""Return all payments for a project with summary."""
payments = db.query(Payment).options(