feat: enforce KS-2/KS-3 stornation integrity
This commit is contained in:
@@ -220,4 +220,47 @@ def project_ks2_totals(db: Session, project_id: int):
|
||||
for document in documents:
|
||||
rows.append({"document": document, "item_count": len(document.items),
|
||||
"amount": sum((_decimal(line.amount) for line in document.items), ZERO)})
|
||||
return rows
|
||||
return rows
|
||||
|
||||
|
||||
def ks2_cancel_block_reason(db: Session, project_id: int, ks2_id: int) -> str | None:
|
||||
"""Return why KS-2 cannot be cancelled, or None if cancellation is allowed."""
|
||||
from app.models.ks3 import KS3, KS3KS2
|
||||
|
||||
document = db.query(KS2).filter(KS2.id == ks2_id, KS2.project_id == project_id).first()
|
||||
if document is None:
|
||||
return "КС-2 не найден"
|
||||
if document.status == "cancelled":
|
||||
return "КС-2 уже сторнирован"
|
||||
active_link = db.query(KS3KS2).join(KS3, KS3.id == KS3KS2.ks3_id).filter(
|
||||
KS3KS2.ks2_id == ks2_id,
|
||||
KS3.project_id == project_id,
|
||||
KS3.status == "issued",
|
||||
).first()
|
||||
if active_link is not None:
|
||||
ks3_doc = db.query(KS3).filter(KS3.id == active_link.ks3_id).first()
|
||||
number = ks3_doc.number if ks3_doc else active_link.ks3_id
|
||||
return f"КС-2 включён в действующий КС-3 №{number}. Сначала сторнируйте КС-3."
|
||||
return None
|
||||
|
||||
|
||||
def cancel_ks2(db: Session, project_id: int, ks2_id: int):
|
||||
"""Cancel a KS-2 atomically. Blocked when linked to an issued KS-3."""
|
||||
try:
|
||||
reason = ks2_cancel_block_reason(db, project_id, ks2_id)
|
||||
if reason is not None:
|
||||
raise KS2ValidationError(reason)
|
||||
document = db.query(KS2).filter(
|
||||
KS2.id == ks2_id, KS2.project_id == project_id
|
||||
).with_for_update().first()
|
||||
if document is None:
|
||||
raise KS2ValidationError("КС-2 не найден")
|
||||
if document.status == "cancelled":
|
||||
raise KS2ValidationError("КС-2 уже сторнирован")
|
||||
document.status = "cancelled"
|
||||
db.commit()
|
||||
db.refresh(document)
|
||||
return document
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise
|
||||
@@ -1,6 +1,7 @@
|
||||
from datetime import date
|
||||
from decimal import Decimal, InvalidOperation
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
|
||||
from app.models.ks2 import KS2, KS2Item
|
||||
@@ -176,4 +177,49 @@ def project_ks3_totals(db: Session, project_id: int):
|
||||
"ks2_count": len(doc.links),
|
||||
"amount": total,
|
||||
})
|
||||
return rows
|
||||
return rows
|
||||
|
||||
|
||||
def ks3_cancel_block_reason(db: Session, project_id: int, ks3_id: int) -> str | None:
|
||||
"""Return why KS-3 cannot be cancelled, or None if cancellation is allowed."""
|
||||
from decimal import Decimal as D
|
||||
|
||||
from app.models.payment import Payment, PaymentAllocation
|
||||
|
||||
document = db.query(KS3).filter(KS3.id == ks3_id, KS3.project_id == project_id).first()
|
||||
if document is None:
|
||||
return "КС-3 не найден"
|
||||
if document.status == "cancelled":
|
||||
return "КС-3 уже сторнирован"
|
||||
paid = db.query(func.coalesce(func.sum(PaymentAllocation.amount), ZERO)).join(
|
||||
Payment, Payment.id == PaymentAllocation.payment_id
|
||||
).filter(
|
||||
PaymentAllocation.ks3_id == ks3_id,
|
||||
Payment.status != "cancelled",
|
||||
PaymentAllocation.amount > 0,
|
||||
).scalar()
|
||||
if D(str(paid or 0)) > 0:
|
||||
return "На КС-3 распределены платежи. Сначала отмените распределения платежей."
|
||||
return None
|
||||
|
||||
|
||||
def cancel_ks3(db: Session, project_id: int, ks3_id: int):
|
||||
"""Cancel a KS-3 atomically. Blocked when payment allocations exist."""
|
||||
try:
|
||||
reason = ks3_cancel_block_reason(db, project_id, ks3_id)
|
||||
if reason is not None:
|
||||
raise KS3ValidationError(reason)
|
||||
document = db.query(KS3).filter(
|
||||
KS3.id == ks3_id, KS3.project_id == project_id
|
||||
).with_for_update().first()
|
||||
if document is None:
|
||||
raise KS3ValidationError("КС-3 не найден")
|
||||
if document.status == "cancelled":
|
||||
raise KS3ValidationError("КС-3 уже сторнирован")
|
||||
document.status = "cancelled"
|
||||
db.commit()
|
||||
db.refresh(document)
|
||||
return document
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise
|
||||
Reference in New Issue
Block a user