From bc69e5217443cb9f0c6e598d7af4b24c109776ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=81=D0=B5=D0=BD=D1=82=D1=8C=D0=B5=D0=B2=20?= =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Wed, 30 Sep 2026 22:50:11 +0300 Subject: [PATCH] fix: agreement cancellation FIFO semantics, modal rendering, and list status badges --- app/services/agreement_items.py | 54 ++- app/templates/agreements/detail.html | 6 +- app/templates/agreements/list.html | 2 +- .../AGR-02-FIX-free-to-close.md | 136 +++++++ .../UI-034-FIX-agreement-cancellation.md | 63 +++ docs/implementation/UI-034-ds-fixes.md | 48 +++ .../UI-035-cancelled-agreement-status.md | 68 ++++ docs/tasks/AGR-02-FIX-free-to-close.md | 358 +++++++++++++++++ .../UI-034-FIX-agreement-cancellation.md | 180 +++++++++ docs/tasks/UI-034-ds-fixes.md | 133 +++++++ .../UI-035-cancelled-agreement-status.md | 96 +++++ tests/test_agr02_agreement_cancellation.py | 14 +- tests/test_agr02fix_free_to_close.py | 376 ++++++++++++++++++ tests/test_task029_delete_draft_agreement.py | 3 +- tests/test_ui034_ds_fixes.py | 351 ++++++++++++++++ tests/test_ui035_cancelled_status.py | 184 +++++++++ 16 files changed, 2056 insertions(+), 16 deletions(-) create mode 100644 docs/implementation/AGR-02-FIX-free-to-close.md create mode 100644 docs/implementation/UI-034-FIX-agreement-cancellation.md create mode 100644 docs/implementation/UI-034-ds-fixes.md create mode 100644 docs/implementation/UI-035-cancelled-agreement-status.md create mode 100644 docs/tasks/AGR-02-FIX-free-to-close.md create mode 100644 docs/tasks/UI-034-FIX-agreement-cancellation.md create mode 100644 docs/tasks/UI-034-ds-fixes.md create mode 100644 docs/tasks/UI-035-cancelled-agreement-status.md create mode 100644 tests/test_agr02fix_free_to_close.py create mode 100644 tests/test_ui034_ds_fixes.py create mode 100644 tests/test_ui035_cancelled_status.py diff --git a/app/services/agreement_items.py b/app/services/agreement_items.py index c351cc0..a7cadc1 100644 --- a/app/services/agreement_items.py +++ b/app/services/agreement_items.py @@ -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 diff --git a/app/templates/agreements/detail.html b/app/templates/agreements/detail.html index 39226eb..09eef9c 100644 --- a/app/templates/agreements/detail.html +++ b/app/templates/agreements/detail.html @@ -11,12 +11,12 @@ {% for row in line_rows %}{% if row.line.change_type == 'new_position' %}Новая{% elif row.line.change_type == 'decrease' %}Уменьшение{% else %}Увеличение{% endif %}
{{ row.item.source_number or '' }} {{ row.item.name }}
{{ row.item.model or '' }}{% if row.item.manufacturer %} · {{ row.item.manufacturer }}{% endif %}{{ row.item.unit or '—' }}{{ row.before_quantity }}{{ row.line.quantity_delta }}{{ row.after_quantity }}{{ row.amount_delta|money(project.currency) }}{% if agreement.status == 'draft' %}
{% endif %} {% if agreement.status == 'draft' %}
{% if row.line.change_type == 'new_position' %}
{% endif %}
{% endif %} {% else %}Позиции ещё не добавлены.{% endfor %} -{% if agreement.status == 'draft' %}

Добавить существующую позицию

Позиция не выбрана
Для уменьшения укажите отрицательное число. Сумма рассчитается по цене позиции.
{% if not selectable_items %}
В этой области нет недобавленных позиц.
{% endif %}
+{% if agreement.status == 'draft' %}

Добавить существующую позицию

Позиция не выбрана
Для уменьшения укажите отрицательное число. Сумма рассчитается по цене позиции.
{% if not selectable_items %}
В этой области нет недобавленных позиц.
{% endif %}

Добавить новую позицию

- -Удалить ДС
{% else %}
ДС утверждено. Изменения недоступны.
{% endif %} + +