fix: agreement cancellation FIFO semantics, modal rendering, and list status badges

This commit is contained in:
Арсентьев Александр
2026-09-30 22:50:11 +03:00
parent 39adbf32e2
commit bc69e52174
16 changed files with 2056 additions and 16 deletions
+50 -4
View File
@@ -314,7 +314,6 @@ def delete_draft_agreement(db, agreement):
def _closed_quantity_for_work_item(db, work_item_id: int) -> Decimal:
"""Sum of KS2ItemSource quantities for a work item, excluding cancelled KS-2."""
from app.models.ks2 import KS2, KS2Item, KS2ItemSource
from app.models.daily_report import DailyReportItem
total = db.query(func.coalesce(func.sum(KS2ItemSource.quantity), ZERO)).join(
KS2Item, KS2Item.id == KS2ItemSource.ks2_item_id
@@ -327,16 +326,63 @@ def _closed_quantity_for_work_item(db, work_item_id: int) -> Decimal:
return Decimal(str(total or 0))
def _agreement_item_used_volume(db, agreement, line: AgreementItem) -> Decimal:
"""
Return how much of *this* AgreementItem's positive delta is consumed by
non-cancelled KS-2 closings, using FIFO layering.
Layers are stacked bottom-up: base BOQ quantity first, then each approved
AgreementItem's quantity_delta in (Agreement.id, AgreementItem.id) order.
Closed volume fills from the bottom. For an increase / new_position line
the "used" part is the portion of closed volume that falls inside *that*
line's positive delta window.
For ``decrease`` lines the semantics of "used" are ambiguous (a decrease
only shrinks the plan), so we keep the pre-existing conservative rule:
any closed volume on the WorkItem blocks cancellation.
"""
if line.change_type == "decrease":
closed = _closed_quantity_for_work_item(db, line.work_item_id)
return closed if closed > ZERO else ZERO
item = db.query(boq.WorkItem).filter(boq.WorkItem.id == line.work_item_id).first()
base_qty = ZERO if line.change_type == "new_position" else (
Decimal(str(item.quantity)) if item and item.quantity is not None else ZERO
)
# All approved AgreementItems on this WorkItem, FIFO order.
all_lines = (
db.query(AgreementItem)
.join(Agreement, Agreement.id == AgreementItem.agreement_id)
.filter(
AgreementItem.work_item_id == line.work_item_id,
Agreement.status == "approved",
)
.order_by(Agreement.id, AgreementItem.id)
.all()
)
prior_delta = ZERO
for aline in all_lines:
if aline.id == line.id:
break
prior_delta += Decimal(str(aline.quantity_delta))
floor = base_qty + prior_delta
closed = _closed_quantity_for_work_item(db, line.work_item_id)
delta = Decimal(str(line.quantity_delta))
return max(ZERO, min(closed - floor, delta))
def agreement_cancel_block_reason(db, agreement) -> str | None:
"""Return why an approved agreement cannot be cancelled, or None."""
if agreement.status == "draft":
return "Черновик нельзя отменить. Сначала утвердите ДС."
if agreement.status == "cancelled":
return "ДС уже отменено."
# Conservative V1: block if any affected WorkItem has closed volume in non-cancelled KS-2
for line in agreement.items:
closed = _closed_quantity_for_work_item(db, line.work_item_id)
if closed > ZERO:
used = _agreement_item_used_volume(db, agreement, line)
if used > ZERO:
return "Нельзя отменить ДС: его объём уже использован в закрытых КС-2."
return None