From ea183854f8c92201c2e1ac79173fe3c05ac73868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=81=D0=B5=D0=BD=D1=82=D1=8C=D0=B5=D0=B2=20?= =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Sun, 27 Sep 2026 20:58:19 +0300 Subject: [PATCH] feat: add payment stornation with history-preserving allocations and card layout fixes --- app/routers/payments.py | 16 +- app/services/payment_service.py | 31 +- app/templates/payments/list.html | 4 + app/templates/payments/view.html | 96 ++++-- .../024-fix-payment-view-layout.md | 85 +++++ .../024-fix2-payment-view-layout.md | 104 ++++++ docs/implementation/024-payment-stornation.md | 117 +++++++ docs/tasks/024-fix-payment-view-layout.md | 153 +++++++++ docs/tasks/024-fix2-payment-view-layout.md | 143 ++++++++ docs/tasks/024-payment-stornation.md | 219 +++++++++++++ tests/test_task023fix2_layout.py | 7 +- tests/test_task024_payment_stornation.py | 310 ++++++++++++++++++ tests/test_task024fix_payment_view_layout.py | 114 +++++++ 13 files changed, 1365 insertions(+), 34 deletions(-) create mode 100644 docs/implementation/024-fix-payment-view-layout.md create mode 100644 docs/implementation/024-fix2-payment-view-layout.md create mode 100644 docs/implementation/024-payment-stornation.md create mode 100644 docs/tasks/024-fix-payment-view-layout.md create mode 100644 docs/tasks/024-fix2-payment-view-layout.md create mode 100644 docs/tasks/024-payment-stornation.md create mode 100644 tests/test_task024_payment_stornation.py create mode 100644 tests/test_task024fix_payment_view_layout.py diff --git a/app/routers/payments.py b/app/routers/payments.py index 96afff0..abde26a 100644 --- a/app/routers/payments.py +++ b/app/routers/payments.py @@ -11,7 +11,7 @@ from app.database.database import get_db from app.models.project import Project from app.models.payment import Payment, PaymentAllocation from app.services.payment_service import ( - PaymentValidationError, available_ks3_for_payment, delete_payment, + PaymentValidationError, available_ks3_for_payment, cancel_payment, delete_payment, project_payments, save_payment, ) from app.utils.formatters import format_money @@ -117,6 +117,8 @@ async def payment_view(request: Request, project_id: int, payment_id: int, db: S async def payment_edit(request: Request, project_id: int, payment_id: int, db: Session = Depends(get_db)): project = _project(db, project_id) payment = _payment(db, project_id, payment_id) + if payment.status == "cancelled": + return RedirectResponse(f"/projects/{project_id}/payments/{payment_id}", status_code=303) return _render_form(request, db, project, payment) @@ -165,12 +167,8 @@ async def payment_delete(project_id: int, payment_id: int, db: Session = Depends @router.post("/{project_id}/payments/{payment_id}/cancel") async def payment_cancel(project_id: int, payment_id: int, db: Session = Depends(get_db)): - from app.models.payment import Payment - doc = db.query(Payment).filter(Payment.id == payment_id, Payment.project_id == project_id).first() - if doc is None: - raise HTTPException(status_code=404, detail="Payment not found") - if doc.status == "cancelled": - raise HTTPException(status_code=400, detail="Payment already cancelled") - doc.status = "cancelled" - db.commit() + try: + cancel_payment(db, project_id, payment_id) + except PaymentValidationError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from exc return RedirectResponse(f"/projects/{project_id}/payments/{payment_id}", status_code=303) \ No newline at end of file diff --git a/app/services/payment_service.py b/app/services/payment_service.py index 195a799..aca42ad 100644 --- a/app/services/payment_service.py +++ b/app/services/payment_service.py @@ -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( diff --git a/app/templates/payments/list.html b/app/templates/payments/list.html index c8838c9..c730807 100644 --- a/app/templates/payments/list.html +++ b/app/templates/payments/list.html @@ -62,10 +62,14 @@ Просмотр + {% if row.payment.status != 'cancelled' %} Редактировать
+ {% else %} + Действия недоступны + {% endif %}
diff --git a/app/templates/payments/view.html b/app/templates/payments/view.html index 5c41a87..07a491b 100644 --- a/app/templates/payments/view.html +++ b/app/templates/payments/view.html @@ -3,50 +3,101 @@ {% block title %}Платёж — {{ project.name }}{% endblock %} {% block content %} -
+ +
-

Платёж{% if payment.document_number %} №{{ payment.document_number }}{% endif %} от {{ payment.date.strftime('%d.%m.%Y') }}

+

Платёж{% if payment.document_number %} №{{ payment.document_number }}{% endif %} от {{ payment.date.strftime('%d.%m.%Y') }}

{% if payment.status == 'cancelled' %} -