From d0e5fa18b9f06d7155e6e5ccc213c4f5eab574a6 Mon Sep 17 00:00:00 2001 From: fomichev Date: Wed, 25 Jun 2025 12:24:21 +0300 Subject: [PATCH] =?utf8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?utf8?q?=D0=BD=D0=B8=D0=B5=20JS?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/views/category-plan/index.php | 18 ++--- erp24/web/js/category-plan/index.js | 107 +++++++++------------------- 2 files changed, 41 insertions(+), 84 deletions(-) diff --git a/erp24/views/category-plan/index.php b/erp24/views/category-plan/index.php index b9afcce8..a5af77cc 100644 --- a/erp24/views/category-plan/index.php +++ b/erp24/views/category-plan/index.php @@ -203,23 +203,23 @@ input[readonly] { - data-offline="" data-offline-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 1);']) ?>% - 'number', 'readonly' => true, 'onchange' => 'editField(this);']) ?> + data-offline="" data-offline-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this);']) ?>% + 'number', 'readonly' => true, ]) ?> - data-online="" data-online-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 2);']) ?>% - 'number', 'readonly' => true, 'onchange' => 'editField(this);']) ?> + data-online="" data-online-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this);']) ?>% + 'number', 'readonly' => true, ]) ?> ?> - data-writeoffs="" data-writeoffs-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 3);']) ?>% - 'number', 'readonly' => true, 'onchange' => 'editField(this);']) ?> + data-writeoffs="" data-writeoffs-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this);']) ?>% + 'number', 'readonly' => true, ]) ?> diff --git a/erp24/web/js/category-plan/index.js b/erp24/web/js/category-plan/index.js index e930000a..9532403b 100644 --- a/erp24/web/js/category-plan/index.js +++ b/erp24/web/js/category-plan/index.js @@ -31,21 +31,35 @@ function updateStores() { }); } -function editField(zis) { - const tr = zis.closest('tr'); - const store_id = document.querySelector('#selected-store').value; - const type = tr.querySelector('th[data-type]').textContent.trim(); - // Инпуты с исходными значениями - const offlineInput = tr.querySelector(`td[data-offline-type="${type}"] input[name="offline"]`); - const internetInput = tr.querySelector(`td[data-online-type="${type}"] input[name="internet_shop"]`); - const writeoffsInput = tr.querySelector(`td[data-writeoffs-type="${type}"] input[name="write_offs"]`); +function editProcent(zis) { + const tr = zis.closest('tr'); + const store_id = $('#selected-store').val(); + const type = tr.querySelector('th[data-type]').textContent.trim(); - const offlineVal = offlineInput.value; - const internetVal = internetInput.value; - const writeoffsVal = writeoffsInput.value; + const map = { + p1: { dataKey: 'offline', inputName: 'offline' }, + p2: { dataKey: 'online', inputName: 'internet_shop' }, + p3: { dataKey: 'writeoffs', inputName: 'write_offs' }, + }[zis.name]; + + if (!map) return; + + const cell = tr.querySelector( + `td[data-${map.dataKey}][data-${map.dataKey}-type="${type}"]` + ); + const baseValue = +cell.dataset[map.dataKey]; + const pct = parseFloat(zis.value) || 0; + const newVal = Math.round(pct / 100 * baseValue); + const numInput = tr.querySelector( + `td[data-${map.dataKey}-type="${type}"] input[name="${map.inputName}"]` + ); + numInput.value = newVal; + + const offlineVal = +tr.querySelector(`input[name="offline"]`).value; + const internetVal = +tr.querySelector(`input[name="internet_shop"]`).value; + const writeoffsVal = +tr.querySelector(`input[name="write_offs"]`).value; - // валидация [offlineVal, internetVal, writeoffsVal].forEach(v => { if (!isNumeric(v)) { alert(v + ' не число'); @@ -53,80 +67,23 @@ function editField(zis) { } }); - // ячейки с процентами по тому же type - const p1Cell = tr.querySelector(`td[data-offline][data-offline-type="${type}"]`); - const p2Cell = tr.querySelector(`td[data-online][data-online-type="${type}"]`); - const p3Cell = tr.querySelector(`td[data-writeoffs][data-writeoffs-type="${type}"]`); - - const p1Input = p1Cell.querySelector('input'); - const p2Input = p2Cell.querySelector('input'); - const p3Input = p3Cell.querySelector('input'); - - // пересчитываем % - p1Input.value = p1Cell.dataset.offline > 0 - ? (100 * offlineVal / +p1Cell.dataset.offline).toFixed(2) - : 0; - console.log(p1Input.value); - - console.log(Math.round(p1Input.value)); - - p2Input.value = p2Cell.dataset.online > 0 - ? (100 * internetVal / +p2Cell.dataset.online) - : 0; - p3Input.value = p3Cell.dataset.writeoffs > 0 - ? (100 * writeoffsVal / +p3Cell.dataset.writeoffs) - : 0; - - // сохраняем на сервере $.ajax({ method: "POST", url: '/category-plan/save-fields', data: { - year: document.querySelector('#dynamicmodel-year').value, - month: document.querySelector('#dynamicmodel-month').value, + year: $('#dynamicmodel-year').val(), + month: $('#dynamicmodel-month').val(), store_id, type, - offline: offlineVal, - internet_shop: internetVal, - write_offs: writeoffsVal, - [param26]: token26 + offline: offlineVal, + internet_shop: internetVal, + write_offs: writeoffsVal, + [param26]: token26 }, dataType: "text" }); } -function editProcent(zis, num) { - const tr = zis.closest('tr'); - const type = tr.querySelector('th[data-type]').textContent.trim(); - - const p1Cell = tr.querySelector(`td[data-offline][data-offline-type="${type}"]`); - const p2Cell = tr.querySelector(`td[data-online][data-online-type="${type}"]`); - const p3Cell = tr.querySelector(`td[data-writeoffs][data-writeoffs-type="${type}"]`); - - switch (num) { - case 1: { - const pct = +p1Cell.querySelector('input').value; - const newVal = (pct / 100 * +p1Cell.dataset.offline); - tr.querySelector(`td[data-offline-type="${type}"] input[name="offline"]`).value = newVal; - break; - } - case 2: { - const pct = +p2Cell.querySelector('input').value; - const newVal = (pct / 100 * +p2Cell.dataset.online); - tr.querySelector(`td[data-online-type="${type}"] input[name="internet_shop"]`).value = newVal; - break; - } - case 3: { - const pct = +p3Cell.querySelector('input').value; - const newVal = (pct / 100 * +p3Cell.dataset.writeoffs); - tr.querySelector(`td[data-writeoffs-type="${type}"] input[name="write_offs"]`).value = newVal; - break; - } - } - - editField(zis); -} - $(document).ready(() => { $('#categoryPlan').DataTable({ sorting: false, -- 2.39.5