feat: add payment stornation with history-preserving allocations and card layout fixes
This commit is contained in:
@@ -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)
|
||||
@@ -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(
|
||||
|
||||
@@ -62,10 +62,14 @@
|
||||
<td class="text-nowrap payments-actions">
|
||||
<span class="d-inline-flex gap-1 align-items-center">
|
||||
<a href="/projects/{{ project.id }}/payments/{{ row.payment.id }}" class="btn btn-sm btn-outline-primary">Просмотр</a>
|
||||
{% if row.payment.status != 'cancelled' %}
|
||||
<a href="/projects/{{ project.id }}/payments/{{ row.payment.id }}/edit" class="btn btn-sm btn-outline-secondary">Редактировать</a>
|
||||
<form method="post" action="/projects/{{ project.id }}/payments/{{ row.payment.id }}/delete" class="d-inline m-0">
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Удалить платёж?')">Удалить</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<span class="text-muted small">Действия недоступны</span>
|
||||
{% endif %}
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -3,50 +3,101 @@
|
||||
{% block title %}Платёж — {{ project.name }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container mt-4">
|
||||
<style>
|
||||
/*
|
||||
* Payment card layout only.
|
||||
* Alert must stay inside the container and wrap long text (incl. 480px).
|
||||
* min-width:0 lets flex/grid children shrink instead of overflowing.
|
||||
*/
|
||||
.payment-view {
|
||||
min-width: 0;
|
||||
max-width: 100%;
|
||||
}
|
||||
.payment-storno-alert {
|
||||
display: block;
|
||||
width: 100%;
|
||||
max-width: 100%;
|
||||
box-sizing: border-box;
|
||||
white-space: normal;
|
||||
overflow-wrap: anywhere;
|
||||
word-wrap: break-word;
|
||||
word-break: normal;
|
||||
}
|
||||
.payment-storno-alert p {
|
||||
margin: 0 0 .5rem;
|
||||
}
|
||||
.payment-storno-alert p:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
.payment-view .money-value,
|
||||
.payment-view .card-body {
|
||||
overflow-wrap: anywhere;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
.payment-view .card {
|
||||
max-width: 100%;
|
||||
}
|
||||
.payment-view .row {
|
||||
min-width: 0;
|
||||
}
|
||||
.payment-view .row > [class*="col"] {
|
||||
min-width: 0;
|
||||
}
|
||||
.payment-actions {
|
||||
justify-content: flex-start;
|
||||
align-items: center;
|
||||
}
|
||||
.payment-actions .btn {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
</style>
|
||||
<div class="payment-view">
|
||||
<nav class="mb-3">
|
||||
<a href="/projects/{{ project.id }}" class="btn btn-outline-secondary btn-sm">← К проекту</a>
|
||||
</nav>
|
||||
|
||||
<h2>Платёж{% if payment.document_number %} №{{ payment.document_number }}{% endif %} от {{ payment.date.strftime('%d.%m.%Y') }}</h2>
|
||||
<h2 class="h4 text-break">Платёж{% if payment.document_number %} №{{ payment.document_number }}{% endif %} от {{ payment.date.strftime('%d.%m.%Y') }}</h2>
|
||||
|
||||
{% if payment.status == 'cancelled' %}
|
||||
<div class="alert alert-danger" role="alert">
|
||||
Платёж сторнирован. Сумма не считается полученными деньгами, распределения исключены из агрегатов.
|
||||
<div class="alert alert-danger payment-storno-alert" role="alert">
|
||||
<p>Платёж сторнирован. Сумма не считается полученными деньгами, распределения исключены из агрегатов.</p>
|
||||
<p>Исторические распределения сохранены ниже.</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="card mb-3">
|
||||
<div class="card-body">
|
||||
<p><strong>Статус:</strong>
|
||||
<p class="mb-3"><strong>Статус:</strong>
|
||||
{% if payment.status == 'cancelled' %}
|
||||
<span class="badge bg-danger">Сторнирован</span>
|
||||
{% else %}
|
||||
<span class="badge bg-success">Активен</span>
|
||||
{% endif %}
|
||||
</p>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<p><strong>Сумма:</strong> <span class="money-value">{{ payment.amount | money(payment.currency) }}</span></p>
|
||||
<div class="row g-3">
|
||||
<div class="col-12 col-sm-6 col-lg-4">
|
||||
<div class="text-muted small mb-1">Сумма</div>
|
||||
<div class="fw-semibold money-value text-break">{{ payment.amount | money(payment.currency) }}</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<p><strong>Распределено:</strong> <span class="money-value">{{ allocated | money(payment.currency) }}</span></p>
|
||||
<div class="col-12 col-sm-6 col-lg-4">
|
||||
<div class="text-muted small mb-1">Распределено</div>
|
||||
<div class="fw-semibold money-value text-break">{{ allocated | money(payment.currency) }}</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<p><strong>Не распределено:</strong> <span class="money-value">{{ unallocated | money(payment.currency) }}</span></p>
|
||||
<div class="col-12 col-sm-6 col-lg-4">
|
||||
<div class="text-muted small mb-1">Не распределено</div>
|
||||
<div class="fw-semibold money-value text-break">{{ unallocated | money(payment.currency) }}</div>
|
||||
</div>
|
||||
</div>
|
||||
{% if payment.comment %}
|
||||
<p><strong>Комментарий:</strong> {{ payment.comment }}</p>
|
||||
<p class="mb-0 mt-3 text-break"><strong>Комментарий:</strong> {{ payment.comment }}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if payment.status != 'cancelled' %}
|
||||
<h4>Распределение по КС-3</h4>
|
||||
<h4 class="h5 text-break">{% if payment.status == 'cancelled' %}Исторические распределения по КС-3{% else %}Распределение по КС-3{% endif %}</h4>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped">
|
||||
<div class="table-responsive mb-3">
|
||||
<table class="table table-striped align-middle">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>№ КС-3</th>
|
||||
@@ -58,7 +109,7 @@
|
||||
{% for alloc in payment.allocations %}
|
||||
<tr>
|
||||
<td>{{ alloc.ks3.number if alloc.ks3 else alloc.ks3_id }}</td>
|
||||
<td>{{ alloc.ks3.date.strftime('%d.%m.%Y') if alloc.ks3 and alloc.ks3.date else '—' }}</td>
|
||||
<td class="text-nowrap">{{ alloc.ks3.date.strftime('%d.%m.%Y') if alloc.ks3 and alloc.ks3.date else '—' }}</td>
|
||||
<td class="text-end text-nowrap money-value">{{ alloc.amount | money(payment.currency) }}</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
@@ -67,9 +118,9 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="d-flex gap-2">
|
||||
<div class="d-flex flex-wrap gap-2 payment-actions">
|
||||
<a href="/projects/{{ project.id }}/payments" class="btn btn-secondary">Назад к списку</a>
|
||||
{% if payment.status != 'cancelled' %}
|
||||
<a href="/projects/{{ project.id }}/payments/{{ payment.id }}/edit" class="btn btn-primary">Редактировать</a>
|
||||
<form method="post" action="/projects/{{ project.id }}/payments/{{ payment.id }}/cancel" class="d-inline">
|
||||
@@ -80,8 +131,9 @@
|
||||
<button type="submit" class="btn btn-danger"
|
||||
onclick="return confirm('Удалить платёж?')">Удалить</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<span class="text-muted small align-self-center">Редактирование, удаление и повторное сторнирование недоступны.</span>
|
||||
{% endif %}
|
||||
<a href="/projects/{{ project.id }}/payments" class="btn btn-secondary">Назад к списку</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user