feat: Daily Report two-zone picker with pre-add quantity entry
Split the Daily Report form into a top position picker (system filter, live search) and a bottom added-items list grouped by system, with quantity edit and remove. Switching systems no longer drops the added list; A1->B1->A2 keeps all three. Quantity can be entered in the picker before pressing Добавить and is carried into the selected list; removing a position returns it to the picker with the typed quantity intact. Draft quantities of never-added positions stay out of POST via a submit guard, so they never become Daily Report facts. Server rows are always rendered client-filtered; POST contract and apply_quantities unchanged.
This commit is contained in:
@@ -85,7 +85,7 @@ def _render_form(request, db, project_obj, system_id=None, report=None, errors=N
|
||||
).order_by(boq.System.sort_order, boq.System.id).all()
|
||||
unclassified_selected = token == "unclassified" or (report is not None and report.system_id is None and token is None)
|
||||
selected_system = None
|
||||
rows = []
|
||||
selected_id = None
|
||||
if token is not None:
|
||||
if token == "unclassified":
|
||||
if not unclassified:
|
||||
@@ -99,13 +99,15 @@ def _render_form(request, db, project_obj, system_id=None, report=None, errors=N
|
||||
selected_system = next((row for row in systems if row.id == selected_id), None)
|
||||
if selected_system is None:
|
||||
raise HTTPException(status_code=400, detail="System does not belong to this project effective plan")
|
||||
rows = _report_rows(db, project_obj.id, selected_id, report.id if report else None,
|
||||
all_systems=True)
|
||||
# DOC-C10: always render every effective position — the picker filters by
|
||||
# system client-side and switching systems must not drop already-added rows.
|
||||
rows = _report_rows(db, project_obj.id, selected_id, report.id if report else None,
|
||||
all_systems=True)
|
||||
current = {line.work_item_id: line.completed_quantity for line in report.items} if report else {}
|
||||
for row in rows:
|
||||
row["current_quantity"] = current.get(row["item"].id)
|
||||
row["over_plan"] = row["accumulated"] + Decimal(str(row["current_quantity"] or 0)) > row["planned_quantity"]
|
||||
row["in_selection"] = row["item"].system_id == selected_id
|
||||
row["in_selection"] = token is not None and row["item"].system_id == selected_id
|
||||
return request.app.templates.TemplateResponse("projects/daily_report_form.html", {
|
||||
"request": request, "project": project_obj, "systems": systems,
|
||||
"has_unclassified_items": bool(unclassified), "unclassified_selected": unclassified_selected,
|
||||
|
||||
@@ -4,6 +4,13 @@
|
||||
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{ 'Редактирование' if report else 'Новый' }} Daily Report — {{ project.name }}</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<style>
|
||||
#picker-scroll { max-height: 420px; overflow-x: auto; overflow-y: auto; }
|
||||
.item-action { white-space: nowrap; }
|
||||
.table-compact { width: 100%; table-layout: fixed; }
|
||||
.table-compact th, .table-compact td { padding-left: .35rem; padding-right: .35rem; overflow-wrap: anywhere; }
|
||||
.sel-group td { background: #f8f9fa; }
|
||||
</style>
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<main class="container py-4">
|
||||
@@ -11,87 +18,203 @@
|
||||
<h1>{{ 'Редактировать Daily Report' if report else 'Новый Daily Report' }}</h1>
|
||||
{% for error in errors %}<div class="alert alert-danger">{{ error }}</div>{% endfor %}
|
||||
|
||||
<form method="get" action="{% if report %}/projects/{{ project.id }}/daily-reports/{{ report.id }}/edit{% else %}/projects/{{ project.id }}/daily-reports/new{% endif %}" class="card card-body mb-3">
|
||||
<label for="system-picker" class="form-label">Система</label>
|
||||
<div class="d-flex gap-2"><select id="system-picker" name="system_id" class="form-select" required>
|
||||
<option value="">Выберите систему</option>
|
||||
{% for system in systems %}<option value="{{ system.id }}" {% if selected_system and selected_system.id == system.id %}selected{% endif %}>{{ system.name }}</option>{% endfor %}
|
||||
{% if has_unclassified_items %}<option value="unclassified" {% if unclassified_selected %}selected{% endif %}>Проектные позиции без системы</option>{% endif %}
|
||||
</select><button class="btn btn-outline-primary" type="submit">Показать позиции</button></div>
|
||||
<div class="form-text">Выберите систему и нажмите «Показать позиции». Позиции разных систем можно добавлять в один отчёт — уже введённые количества сохраняются.</div>
|
||||
</form>
|
||||
|
||||
{% if selected_system or unclassified_selected %}
|
||||
<form method="post" action="{% if report %}/projects/{{ project.id }}/daily-reports/{{ report.id }}/edit{% else %}/projects/{{ project.id }}/daily-reports{% endif %}">
|
||||
<input type="hidden" name="system_id" value="{{ selected_system.id if selected_system else 'unclassified' }}">
|
||||
<input type="hidden" name="system_id" id="hidden-system-id" value="{{ selected_system.id if selected_system else ('unclassified' if unclassified_selected else '') }}">
|
||||
<div class="card card-body mb-3">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-4"><label for="report-date" class="form-label">Дата отчёта</label><input id="report-date" class="form-control" type="date" name="report_date" required value="{{ report.report_date.isoformat() if report else '' }}"></div>
|
||||
<div class="col-md-8"><label for="notes" class="form-label">Комментарий</label><input id="notes" class="form-control" name="notes" value="{{ report.notes if report and report.notes else '' }}"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="card-header"><strong><span id="current-system-name">{{ selected_system.name if selected_system else 'Проектные позиции без системы' }}</span></strong> — укажите фактическое количество хотя бы для одной позиции</div>
|
||||
<div class="p-3 border-bottom">
|
||||
<label class="form-label" for="position-search">Поиск позиций</label>
|
||||
<input id="position-search" class="form-control" type="search" autocomplete="off" placeholder="Номер, наименование, модель или производитель">
|
||||
<div id="position-search-empty" class="form-text text-muted" hidden>Совпадений нет.</div>
|
||||
|
||||
<!-- ============ Зона 1: подбор позиций ============ -->
|
||||
<div class="card mb-3" id="picker-zone">
|
||||
<div class="card-header"><strong>Подбор позиций</strong></div>
|
||||
<div class="card-body py-2">
|
||||
<div class="row g-2">
|
||||
<div class="col-12 col-md-4">
|
||||
<label for="system-picker" class="form-label">Система</label>
|
||||
<select id="system-picker" class="form-select">
|
||||
<option value="">Выберите систему</option>
|
||||
{% for system in systems %}<option value="{{ system.id }}" {% if selected_system and selected_system.id == system.id %}selected{% endif %}>{{ system.name }}</option>{% endfor %}
|
||||
{% if has_unclassified_items %}<option value="unclassified" {% if unclassified_selected %}selected{% endif %}>Проектные позиции без системы</option>{% endif %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-12 col-md-8">
|
||||
<label for="position-search" class="form-label">Поиск</label>
|
||||
<input id="position-search" class="form-control" type="search" autocomplete="off" placeholder="Номер, наименование, модель или производитель">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-text">Выберите систему, при необходимости введите количество и нажмите «Добавить». Позиции разных систем можно добавлять в один отчёт — уже добавленные при смене системы сохраняются.</div>
|
||||
</div>
|
||||
<div id="picker-scroll" class="table-responsive">
|
||||
<table class="table table-sm table-hover align-middle mb-0 table-compact">
|
||||
<thead class="table-light"><tr><th class="d-none d-md-table-cell">Раздел</th><th>№</th><th>Рабочая позиция</th><th>Ед.</th><th style="width: 118px">Количество</th><th class="text-end" style="width: 104px" aria-label="Действие"></th></tr></thead>
|
||||
<tbody id="picker-body">
|
||||
{% for row in rows %}{% if row.current_quantity is none %}
|
||||
<tr class="position-row" data-number="{{ row.item.source_number or '' }}" data-name="{{ row.item.name or '' }}" data-model="{{ row.item.model or '' }}" data-manufacturer="{{ row.item.manufacturer or '' }}" data-system="{{ row.item.system_id if row.item.system_id is not none else 'unclassified' }}" data-system-name="{{ row.item.system.name if row.item.system else 'Без системы' }}" data-section="{{ row.item.section.name if row.item.section else '—' }}" {% if not row.in_selection %}hidden{% endif %}>
|
||||
<td class="d-none d-md-table-cell">{{ row.item.section.name if row.item.section else '—' }}</td>
|
||||
<td>{{ row.item.source_number or '' }}</td>
|
||||
<td>{{ row.item.name }}<div class="small text-muted">План: {{ row.planned_quantity if row.planned_quantity is not none else '—' }} · Накоплено: {{ row.accumulated }}</div></td>
|
||||
<td>{{ row.item.unit or '' }}</td>
|
||||
<td><input class="form-control form-control-sm picker-qty" type="number" min="0" step="0.0001" name="quantity_{{ row.item.id }}" value="" data-item-id="{{ row.item.id }}" aria-label="Выполнено: {{ row.item.name }}"></td>
|
||||
<td class="text-end"><button type="button" class="btn btn-sm btn-outline-success item-action" data-action="add">Добавить</button></td>
|
||||
</tr>
|
||||
{% endif %}{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div id="picker-empty" class="card-footer text-muted" hidden>Совпадений нет.</div>
|
||||
</div>
|
||||
|
||||
<!-- ============ Зона 2: добавленные позиции ============ -->
|
||||
<div class="card mb-3" id="selected-zone">
|
||||
<div class="card-header"><strong>Добавленные позиции</strong> — укажите фактическое количество хотя бы для одной</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-sm align-middle mb-0">
|
||||
<thead class="table-light"><tr><th class="d-none d-md-table-cell">Раздел</th><th>№</th><th>Наименование</th><th>Ед.</th><th style="width: 118px">Количество</th><th class="text-end" style="width: 96px" aria-label="Действие"></th></tr></thead>
|
||||
<tbody id="selected-body">
|
||||
{% for row in rows %}{% if row.current_quantity is not none %}
|
||||
<tr class="position-row" data-number="{{ row.item.source_number or '' }}" data-name="{{ row.item.name or '' }}" data-model="{{ row.item.model or '' }}" data-manufacturer="{{ row.item.manufacturer or '' }}" data-system="{{ row.item.system_id if row.item.system_id is not none else 'unclassified' }}" data-system-name="{{ row.item.system.name if row.item.system else 'Без системы' }}" data-section="{{ row.item.section.name if row.item.section else '—' }}">
|
||||
<td class="d-none d-md-table-cell">{{ row.item.section.name if row.item.section else '—' }}</td>
|
||||
<td>{{ row.item.source_number or '' }}</td>
|
||||
<td>{{ row.item.name }}</td>
|
||||
<td>{{ row.item.unit or '' }}</td>
|
||||
<td><input class="form-control form-control-sm" type="number" min="0" step="0.0001" name="quantity_{{ row.item.id }}" value="{{ row.current_quantity }}" aria-label="Выполнено: {{ row.item.name }}"></td>
|
||||
<td class="text-end"><button type="button" class="btn btn-sm btn-outline-danger item-action" data-action="remove">Удалить</button></td>
|
||||
</tr>
|
||||
{% endif %}{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div id="selected-empty" class="card-footer text-muted" hidden>Пока ничего не добавлено.</div>
|
||||
<div class="card-footer d-flex justify-content-between">
|
||||
<a class="btn btn-outline-secondary" href="/projects/{{ project.id }}/daily-reports">Отмена</a>
|
||||
<button class="btn btn-primary" type="submit">{{ 'Сохранить изменения' if report else 'Создать отчёт' }}</button>
|
||||
</div>
|
||||
<div class="table-responsive"><table class="table table-sm table-hover align-middle mb-0">
|
||||
<thead><tr><th>Раздел</th><th>№</th><th>Рабочая позиция</th><th>Ед.</th><th class="text-end">План</th><th class="text-end">Накоплено до отчёта</th><th style="width: 180px">Выполнено за дату</th></tr></thead>
|
||||
<tbody>
|
||||
{% for row in rows %}
|
||||
<tr class="position-row {% if row.over_plan %}table-warning{% endif %}" data-number="{{ row.item.source_number or '' }}" data-name="{{ row.item.name or '' }}" data-model="{{ row.item.model or '' }}" data-manufacturer="{{ row.item.manufacturer or '' }}" data-system="{{ row.item.system_id if row.item.system_id is not none else 'unclassified' }}" {% if not row.in_selection and row.current_quantity is none %}hidden{% endif %}>
|
||||
<td>{{ row.item.section.name if row.item.section else '—' }}<div class="small text-muted">{{ row.item.system.name if row.item.system else 'Без системы' }}</div></td><td>{{ row.item.source_number or '' }}</td><td>{{ row.item.name }}</td><td>{{ row.item.unit or '' }}</td>
|
||||
<td class="text-end">{{ row.planned_quantity if row.planned_quantity is not none else '—' }}</td><td class="text-end">{{ row.accumulated }}</td>
|
||||
<td><input class="form-control form-control-sm" type="number" min="0" step="0.0001" name="quantity_{{ row.item.id }}" value="{{ row.current_quantity if row.current_quantity is not none else '' }}" aria-label="Выполнено: {{ row.item.name }}"></td>
|
||||
</tr>
|
||||
{% else %}<tr><td colspan="7" class="text-center text-muted py-4">В ведомости нет рабочих позиций.</td></tr>{% endfor %}
|
||||
</tbody>
|
||||
</table></div>
|
||||
<div class="card-footer d-flex justify-content-between"><a class="btn btn-outline-secondary" href="/projects/{{ project.id }}/daily-reports">Отмена</a><button class="btn btn-primary" type="submit">{{ 'Сохранить изменения' if report else 'Создать отчёт' }}</button></div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
(() => {
|
||||
const pickerBody = document.getElementById('picker-body');
|
||||
const selectedBody = document.getElementById('selected-body');
|
||||
const systemPicker = document.getElementById('system-picker');
|
||||
const search = document.getElementById('position-search');
|
||||
if (!search) return;
|
||||
const rows = [...document.querySelectorAll('.position-row')];
|
||||
const empty = document.getElementById('position-search-empty');
|
||||
const picker = document.getElementById('system-picker');
|
||||
const pickerForm = picker ? picker.form : null;
|
||||
const hiddenSystem = document.querySelector('form[method="post"] input[name="system_id"]');
|
||||
const systemName = document.getElementById('current-system-name');
|
||||
const pickerEmpty = document.getElementById('picker-empty');
|
||||
const selectedEmpty = document.getElementById('selected-empty');
|
||||
const hiddenSystem = document.getElementById('hidden-system-id');
|
||||
if (!pickerBody || !selectedBody) return;
|
||||
|
||||
const normalize = value => (value || '').toLocaleLowerCase('ru-RU').trim();
|
||||
const applyFilters = () => {
|
||||
const selected = picker ? picker.value : '';
|
||||
const query = normalize(search.value);
|
||||
|
||||
const applyPickerFilter = () => {
|
||||
const selected = systemPicker ? systemPicker.value : '';
|
||||
const query = normalize(search ? search.value : '');
|
||||
let visible = 0;
|
||||
for (const row of rows) {
|
||||
const filled = [...row.querySelectorAll('input')].some(input => input.value.trim() !== '');
|
||||
const inSystem = !selected || row.dataset.system === selected || filled;
|
||||
for (const row of pickerBody.querySelectorAll('tr.position-row')) {
|
||||
const inSystem = selected !== '' && row.dataset.system === selected;
|
||||
const haystack = normalize(['number', 'name', 'model', 'manufacturer']
|
||||
.map(key => row.dataset[key]).join(' '));
|
||||
row.hidden = !(inSystem && haystack.includes(query));
|
||||
if (!row.hidden) visible++;
|
||||
const show = inSystem && haystack.includes(query);
|
||||
row.hidden = !show;
|
||||
if (show) visible++;
|
||||
}
|
||||
empty.hidden = visible !== 0;
|
||||
if (pickerEmpty) pickerEmpty.hidden = visible !== 0;
|
||||
};
|
||||
if (pickerForm && picker) {
|
||||
pickerForm.addEventListener('submit', event => {
|
||||
event.preventDefault();
|
||||
if (hiddenSystem) hiddenSystem.value = picker.value;
|
||||
if (systemName && picker.selectedOptions.length) {
|
||||
systemName.textContent = picker.selectedOptions[0].textContent;
|
||||
|
||||
const setAddedState = (row, added) => {
|
||||
const input = row.querySelector('input[name^="quantity_"]');
|
||||
const btn = row.querySelector('.item-action');
|
||||
if (input) {
|
||||
// DOC-C11: the same input travels with the row, so the typed
|
||||
// quantity survives add / remove / filter / system switch.
|
||||
input.disabled = false;
|
||||
}
|
||||
if (btn) {
|
||||
btn.dataset.action = added ? 'remove' : 'add';
|
||||
btn.textContent = added ? 'Удалить' : 'Добавить';
|
||||
btn.classList.toggle('btn-outline-success', !added);
|
||||
btn.classList.toggle('btn-outline-danger', added);
|
||||
}
|
||||
};
|
||||
|
||||
const rebuildSelected = () => {
|
||||
const rows = [...selectedBody.querySelectorAll('tr.position-row')];
|
||||
rows.sort((a, b) => {
|
||||
const bySystem = (a.dataset.systemName || '').localeCompare(b.dataset.systemName || '', 'ru');
|
||||
if (bySystem !== 0) return bySystem;
|
||||
return (a.dataset.number || '').localeCompare(b.dataset.number || '', 'ru', { numeric: true });
|
||||
});
|
||||
selectedBody.textContent = '';
|
||||
let lastSystem = null;
|
||||
for (const row of rows) {
|
||||
const systemName = row.dataset.systemName || 'Без системы';
|
||||
if (systemName !== lastSystem) {
|
||||
const group = document.createElement('tr');
|
||||
group.className = 'sel-group';
|
||||
const cell = document.createElement('td');
|
||||
cell.colSpan = 6;
|
||||
const strong = document.createElement('strong');
|
||||
strong.textContent = systemName;
|
||||
cell.appendChild(strong);
|
||||
group.appendChild(cell);
|
||||
selectedBody.appendChild(group);
|
||||
lastSystem = systemName;
|
||||
}
|
||||
row.hidden = false;
|
||||
selectedBody.appendChild(row);
|
||||
}
|
||||
if (selectedEmpty) selectedEmpty.hidden = rows.length !== 0;
|
||||
};
|
||||
|
||||
const moveToSelected = row => {
|
||||
setAddedState(row, true);
|
||||
selectedBody.appendChild(row);
|
||||
rebuildSelected();
|
||||
applyPickerFilter();
|
||||
};
|
||||
|
||||
const moveToPicker = row => {
|
||||
setAddedState(row, false);
|
||||
pickerBody.appendChild(row);
|
||||
rebuildSelected();
|
||||
applyPickerFilter();
|
||||
};
|
||||
|
||||
document.addEventListener('click', event => {
|
||||
const btn = event.target.closest('.item-action');
|
||||
if (!btn) return;
|
||||
const row = btn.closest('tr.position-row');
|
||||
if (!row) return;
|
||||
if (btn.dataset.action === 'add') moveToSelected(row);
|
||||
else moveToPicker(row);
|
||||
});
|
||||
|
||||
// DOC-C11: quantities typed in the picker but never submitted with
|
||||
// "Добавить" must not reach POST / become Daily Report facts.
|
||||
const form = pickerBody.closest('form');
|
||||
if (form) {
|
||||
form.addEventListener('submit', () => {
|
||||
for (const row of pickerBody.querySelectorAll('tr.position-row')) {
|
||||
const input = row.querySelector('input[name^="quantity_"]');
|
||||
if (input) input.disabled = true;
|
||||
}
|
||||
applyFilters();
|
||||
});
|
||||
}
|
||||
search.addEventListener('input', applyFilters);
|
||||
applyFilters();
|
||||
|
||||
if (systemPicker) {
|
||||
systemPicker.addEventListener('change', () => {
|
||||
if (hiddenSystem) hiddenSystem.value = systemPicker.value;
|
||||
applyPickerFilter();
|
||||
});
|
||||
}
|
||||
if (search) search.addEventListener('input', applyPickerFilter);
|
||||
|
||||
rebuildSelected();
|
||||
applyPickerFilter();
|
||||
})();
|
||||
</script>
|
||||
{% endif %}
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user