224 lines
12 KiB
HTML
224 lines
12 KiB
HTML
{% extends "base.html" %}
|
||
|
||
{% block title %}{{ 'Редактирование' if document else 'Создание' }} КС-2{% endblock %}
|
||
|
||
{% block content %}
|
||
<div class="container mt-4">
|
||
<nav class="mb-3">
|
||
<a href="/projects/{{ project.id }}" class="btn btn-outline-secondary btn-sm">← К проекту</a>
|
||
</nav>
|
||
|
||
<div class="row">
|
||
<div class="col-12">
|
||
<h2>{{ 'Редактирование КС-2' if document else 'Создание КС-2' }}</h2>
|
||
|
||
{% if error %}
|
||
<div class="alert alert-danger" role="alert">
|
||
{{ error }}
|
||
</div>
|
||
{% endif %}
|
||
|
||
<form method="post" action="/projects/{{ project.id }}/ks2{% if document %}/{{ document.id }}/edit{% endif %}" id="ks2-form">
|
||
<div class="row mb-3">
|
||
<div class="col-md-3">
|
||
<label for="number" class="form-label">Номер КС-2</label>
|
||
<input type="text" class="form-control" id="number" name="number" required
|
||
value="{{ form_values.get('number', document.number if document else suggested_number) }}">
|
||
</div>
|
||
<div class="col-md-3">
|
||
<label for="document_date" class="form-label">Дата</label>
|
||
<input type="date" class="form-control" id="document_date" name="document_date" required
|
||
value="{{ form_values.get('document_date', document.date.isoformat() if document else '') }}">
|
||
</div>
|
||
<div class="col-md-3">
|
||
<label for="reporting_period_from" class="form-label">Период с</label>
|
||
<input type="date" class="form-control" id="reporting_period_from" name="reporting_period_from" required
|
||
value="{{ form_values.get('reporting_period_from', document.reporting_period_from.isoformat() if document and document.reporting_period_from else '') }}">
|
||
</div>
|
||
<div class="col-md-3">
|
||
<label for="reporting_period_to" class="form-label">Период по</label>
|
||
<input type="date" class="form-control" id="reporting_period_to" name="reporting_period_to" required
|
||
value="{{ form_values.get('reporting_period_to', document.reporting_period_to.isoformat() if document and document.reporting_period_to else '') }}">
|
||
</div>
|
||
</div>
|
||
<div class="row mb-3">
|
||
<div class="col-md-12">
|
||
<label for="comment" class="form-label">Комментарий</label>
|
||
<input type="text" class="form-control" id="comment" name="comment"
|
||
value="{{ form_values.get('comment', document.comment if document and document.comment else '') }}">
|
||
</div>
|
||
</div>
|
||
|
||
<h4>Объёмы к закрытию</h4>
|
||
|
||
<div class="d-flex flex-wrap gap-2 align-items-center mb-2">
|
||
<button type="button" class="btn btn-outline-success btn-sm" id="fill-available">Учесть весь объём</button>
|
||
<span class="form-text mb-0">Подставляет доступный остаток по всем позициям. Количество можно править вручную в любой момент.</span>
|
||
</div>
|
||
|
||
<div class="table-responsive">
|
||
<table class="table table-striped table-sm" style="table-layout: fixed;">
|
||
<colgroup>
|
||
<col style="width: 50px">
|
||
<col>
|
||
<col style="width: 40px">
|
||
<col style="width: 72px">
|
||
<col style="width: 72px">
|
||
<col style="width: 72px">
|
||
<col style="width: 72px">
|
||
<col style="width: 72px">
|
||
<col style="width: 96px">
|
||
<col style="width: 88px">
|
||
<col style="width: 100px">
|
||
</colgroup>
|
||
<thead>
|
||
<tr>
|
||
<th>№ поз.</th>
|
||
<th>Наименование</th>
|
||
<th>Ед.</th>
|
||
<th class="text-end text-nowrap">План</th>
|
||
<th class="text-end text-nowrap">Факт</th>
|
||
<th class="text-end text-nowrap">Закрыто</th>
|
||
<th class="text-end text-nowrap">Доступно</th>
|
||
<th class="text-end text-nowrap">Цена</th>
|
||
<th class="text-nowrap">Кол-во</th>
|
||
<th></th>
|
||
<th class="text-end text-nowrap">Сумма</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{% for balance in balances %}
|
||
{% set item_id = balance.item.id %}
|
||
{% set available = balance.available_quantity %}
|
||
{% set qty_value = form_values.get('quantity_' ~ item_id, selected.get(item_id, '')) %}
|
||
<tr data-available="{{ available }}" data-item-id="{{ item_id }}"{% if available <= 0 %} class="table-secondary"{% endif %}>
|
||
<td>{{ balance.item.source_number or '' }}</td>
|
||
<td style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{{ balance.item.name }}</td>
|
||
<td>{{ balance.item.unit or '' }}</td>
|
||
<td class="text-end text-nowrap">{{ balance.planned_quantity|quantity2 }}</td>
|
||
<td class="text-end text-nowrap">{{ balance.actual_quantity|quantity2 }}</td>
|
||
<td class="text-end text-nowrap">{{ balance.closed_quantity|quantity2 }}</td>
|
||
<td class="text-end text-nowrap">
|
||
{{ available|quantity2 }}
|
||
{% if available <= 0 %}
|
||
<span class="badge text-bg-secondary">нет остатка</span>
|
||
{% endif %}
|
||
</td>
|
||
<td class="text-end text-nowrap" data-price="{{ balance.item.unit_price or 0 }}">{{ balance.item.unit_price|money }}</td>
|
||
<td>
|
||
<input type="number" class="form-control form-control-sm"
|
||
name="quantity_{{ item_id }}"
|
||
value="{{ qty_value }}"
|
||
min="0" step="0.01"
|
||
placeholder="0.00"
|
||
style="width: 100%; box-sizing: border-box;"
|
||
data-item-id="{{ item_id }}">
|
||
</td>
|
||
<td>
|
||
<button type="button" class="btn btn-outline-success btn-sm fill-row"
|
||
data-item-id="{{ item_id }}"
|
||
{% if available <= 0 %}disabled{% endif %}
|
||
title="Подставить доступный остаток этой позиции"
|
||
aria-label="Весь объём позиции {{ balance.item.source_number or item_id }}">Весь объём</button>
|
||
</td>
|
||
<td class="text-end text-nowrap" id="amount_{{ item_id }}">—</td>
|
||
</tr>
|
||
{% endfor %}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<button type="submit" class="btn btn-primary">{{ 'Сохранить' if document else 'Создать' }}</button>
|
||
<a href="/projects/{{ project.id }}/ks2{% if document %}/{{ document.id }}{% endif %}" class="btn btn-secondary">Отмена</a>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
const form = document.getElementById('ks2-form');
|
||
|
||
function availableOf(row) {
|
||
return parseFloat(row.dataset.available) || 0;
|
||
}
|
||
|
||
function rows() {
|
||
return Array.from(document.querySelectorAll('tr[data-item-id]'));
|
||
}
|
||
|
||
function quantityOf(row) {
|
||
return row.querySelector('input[type="number"][data-item-id]');
|
||
}
|
||
|
||
function updateAmount(itemId, quantityInput) {
|
||
const amountCell = document.getElementById('amount_' + itemId);
|
||
const priceCell = quantityInput.closest('tr').querySelector('[data-price]');
|
||
const price = parseFloat(priceCell.dataset.price) || 0;
|
||
const qty = parseFloat(quantityInput.value) || 0;
|
||
const amount = price * qty;
|
||
amountCell.textContent = amount > 0
|
||
? amount.toLocaleString('ru-RU', {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' \u20BD'
|
||
: '—';
|
||
}
|
||
|
||
function fillRow(row) {
|
||
const available = availableOf(row);
|
||
if (available <= 0) return;
|
||
const input = quantityOf(row);
|
||
if (!input) return;
|
||
input.value = available.toFixed(2);
|
||
updateAmount(input.dataset.itemId, input);
|
||
}
|
||
|
||
// Кнопка строки: подставить доступный остаток только этой позиции.
|
||
document.querySelectorAll('.fill-row').forEach(function(btn) {
|
||
btn.addEventListener('click', function() {
|
||
const row = this.closest('tr[data-item-id]');
|
||
if (row) fillRow(row);
|
||
});
|
||
});
|
||
|
||
// Глобальная кнопка: доступный остаток по всем позициям формы.
|
||
const fillAllBtn = document.getElementById('fill-available');
|
||
if (fillAllBtn) {
|
||
fillAllBtn.addEventListener('click', function() {
|
||
rows().forEach(fillRow);
|
||
});
|
||
}
|
||
|
||
// Ручной ввод количества всегда доступен и не зависит от кнопок.
|
||
document.querySelectorAll('input[type="number"][data-item-id]').forEach(function(input) {
|
||
input.addEventListener('input', function() {
|
||
updateAmount(this.dataset.itemId, this);
|
||
});
|
||
if (input.value) {
|
||
updateAmount(input.dataset.itemId, input);
|
||
}
|
||
});
|
||
|
||
// Серверный контракт _entries_from_form ждёт include_<id>=1.
|
||
// Чекбоксов нет: поля include_* создаются на submit только для строк
|
||
// с количеством > 0. Это не «выбор», а протокол передачи закрываемых позиций.
|
||
if (form) {
|
||
form.addEventListener('submit', function() {
|
||
form.querySelectorAll('input[name^="include_"]').forEach(function(el) {
|
||
el.remove();
|
||
});
|
||
rows().forEach(function(row) {
|
||
const input = quantityOf(row);
|
||
if (!input) return;
|
||
const qty = parseFloat(input.value);
|
||
if (!(qty > 0)) return;
|
||
const hidden = document.createElement('input');
|
||
hidden.type = 'hidden';
|
||
hidden.name = 'include_' + input.dataset.itemId;
|
||
hidden.value = '1';
|
||
form.appendChild(hidden);
|
||
});
|
||
});
|
||
}
|
||
});
|
||
</script>
|
||
{% endblock %}
|