fix: prevent KS-2 reuse across issued KS-3

This commit is contained in:
Арсентьев Александр
2026-10-16 12:10:07 +03:00
parent 9b67ace944
commit a6449a7d70
3 changed files with 231 additions and 6 deletions
+4 -1
View File
@@ -52,7 +52,10 @@ def _ks2_total(ks2):
def _render_form(request, db, project, document=None, error=None, status_code=200,
suggested_number=None, form_values=None):
available = available_ks2(db, project.id)
available = available_ks2(
db, project.id,
exclude_ks3_id=document.id if document is not None else None,
)
if document is not None:
selected_ids = {link.ks2_id for link in document.links}
elif form_values:
+56 -3
View File
@@ -36,14 +36,58 @@ def _ks2_total(ks2):
return sum(_decimal(item.amount) for item in ks2.items)
def available_ks2(db: Session, project_id: int):
"""Return issued KS-2 documents for the project with their totals."""
def _taken_ks2_ids(db: Session, project_id: int, exclude_ks3_id: int | None = None) -> set[int]:
"""KS-2 ids already linked to an issued KS-3 of this project.
Draft and cancelled KS-3 do not claim KS-2. exclude_ks3_id keeps the
current document's own selection available while editing.
"""
query = (
db.query(KS3KS2.ks2_id)
.join(KS3, KS3.id == KS3KS2.ks3_id)
.filter(KS3.project_id == project_id, KS3.status == "issued")
)
if exclude_ks3_id is not None:
query = query.filter(KS3.id != exclude_ks3_id)
return {row[0] for row in query.all()}
def _reject_if_taken_by_other_issued_ks3(db: Session, project_id: int, ks2_id: int,
ks2_number: str, exclude_ks3_id: int | None = None) -> None:
query = (
db.query(KS3KS2, KS3)
.join(KS3, KS3.id == KS3KS2.ks3_id)
.filter(
KS3KS2.ks2_id == ks2_id,
KS3.project_id == project_id,
KS3.status == "issued",
)
)
if exclude_ks3_id is not None:
query = query.filter(KS3.id != exclude_ks3_id)
taken = query.first()
if taken is not None:
_, other = taken
raise KS3ValidationError(
f"КС-2 №{ks2_number} уже включён в проведённый КС-3 №{other.number}"
)
def available_ks2(db: Session, project_id: int, exclude_ks3_id: int | None = None):
"""Return issued KS-2 documents available for inclusion in a KS-3.
KS-2 already linked to another issued KS-3 is excluded. Draft/cancelled
KS-3 do not block. exclude_ks3_id keeps the document's own KS-2 visible.
"""
taken = _taken_ks2_ids(db, project_id, exclude_ks3_id=exclude_ks3_id)
docs = db.query(KS2).options(joinedload(KS2.items)).filter(
KS2.project_id == project_id,
KS2.status == "issued",
).order_by(KS2.date, KS2.id).all()
result = []
for doc in docs:
if doc.id in taken:
continue
result.append({
"id": doc.id,
"number": doc.number,
@@ -117,9 +161,15 @@ def save_draft(db: Session, project_id: int, number: str, document_date: date,
raise KS3ValidationError(f"КС-2 №{ks2.number} не принадлежит этому проекту")
if ks2.status != "issued":
raise KS3ValidationError(f"КС-2 №{ks2.number} не проведён")
# Server-side guard: a KS-2 can belong to only one issued KS-3.
_reject_if_taken_by_other_issued_ks3(
db, project_id, ks2_id, ks2.number, exclude_ks3_id=ks3_id,
)
# Replace links
# Replace links: delete old rows first so re-selecting the same KS-2
# does not hit the unique constraint during the autoflush below.
document.links.clear()
db.flush()
for ks2_id in seen_ks2:
document.links.append(KS3KS2(ks2_id=ks2_id))
@@ -161,6 +211,9 @@ def issue_ks3(db: Session, project_id: int, ks3_id: int):
raise KS3ValidationError("Один из выбранных КС-2 не найден в проекте")
if ks2.status != "issued":
raise KS3ValidationError(f"КС-2 №{ks2.number} не проведён")
_reject_if_taken_by_other_issued_ks3(
db, project_id, ks2.id, ks2.number, exclude_ks3_id=document.id,
)
document.status = "issued"