fix: payments validation and in-place allocation edits

Close the KS-2 -> KS-3 -> Payment -> allocation -> aggregates chain for V1.
Two real defects fixed:

- NaN/Infinity payment or allocation amounts raised InvalidOperation (500)
  or were silently stored; they are now finite-value validation errors (400)
  via _parse_money / is_finite checks.
- Editing an allocation to the same KS-3 hit uq_payment_ks3 because
  clear() deferred the DELETE past the new INSERT; allocations are now
  updated in place.

Financial semantics unchanged: full active payment counts as received,
storno keeps historical allocations while excluding the payment from
active aggregates. Decimal arithmetic preserved.
This commit is contained in:
Арсентьев Александр
2026-10-08 20:24:48 +03:00
parent e7abc7c86c
commit 1af2a70406
5 changed files with 780 additions and 8 deletions
+10 -6
View File
@@ -11,8 +11,8 @@ 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, cancel_payment, delete_payment,
project_payments, save_payment,
PaymentValidationError, _parse_money, available_ks3_for_payment, cancel_payment,
delete_payment, project_payments, save_payment,
)
from app.utils.formatters import format_money
@@ -84,14 +84,16 @@ async def payment_create(request: Request, project_id: int,
if k.startswith("alloc_") and v.strip():
try:
ks3_id = int(k.removeprefix("alloc_"))
alloc_amount = Decimal(v.strip().replace(",", "."))
alloc_amount = _parse_money(v, f"Распределение КС-3 {ks3_id}")
if alloc_amount > 0:
allocations.append({"ks3_id": ks3_id, "amount": alloc_amount})
except PaymentValidationError:
raise
except (ValueError, TypeError):
pass
try:
parsed_date = date.fromisoformat(payment_date)
parsed_amount = Decimal(amount.strip().replace(",", "."))
parsed_amount = _parse_money(amount, "Сумма платежа")
payment = save_payment(
db, project_id, parsed_date, parsed_amount, currency, comment,
allocations, document_number=document_number or None,
@@ -136,14 +138,16 @@ async def payment_update(request: Request, project_id: int, payment_id: int,
if k.startswith("alloc_") and v.strip():
try:
ks3_id = int(k.removeprefix("alloc_"))
alloc_amount = Decimal(v.strip().replace(",", "."))
alloc_amount = _parse_money(v, f"Распределение КС-3 {ks3_id}")
if alloc_amount > 0:
allocations.append({"ks3_id": ks3_id, "amount": alloc_amount})
except PaymentValidationError:
raise
except (ValueError, TypeError):
pass
try:
parsed_date = date.fromisoformat(payment_date)
parsed_amount = Decimal(amount.strip().replace(",", "."))
parsed_amount = _parse_money(amount, "Сумма платежа")
payment = save_payment(
db, project_id, parsed_date, parsed_amount, currency, comment,
allocations, document_number=document_number or None,