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' %} -