From 01b066f8f7779b643a5c0a3b4e0d3e268449c01c Mon Sep 17 00:00:00 2001 From: fomichev Date: Wed, 25 Jun 2025 10:07:28 +0300 Subject: [PATCH] =?utf8?q?=D0=A0=D0=B0=D1=81=D1=87=D0=B5=D1=82=20=D0=B4?= =?utf8?q?=D0=BE=D0=BB=D0=B5=D0=B9=20=D0=B8=20=D0=BF=D1=80=D0=BE=D1=86?= =?utf8?q?=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/controllers/CategoryPlanController.php | 2 +- erp24/views/category-plan/index.php | 12 +- erp24/web/js/category-plan/index.js | 133 +++++++++++-------- 3 files changed, 87 insertions(+), 60 deletions(-) diff --git a/erp24/controllers/CategoryPlanController.php b/erp24/controllers/CategoryPlanController.php index c2e0f5cd..ce61ae5b 100644 --- a/erp24/controllers/CategoryPlanController.php +++ b/erp24/controllers/CategoryPlanController.php @@ -47,7 +47,7 @@ class CategoryPlanController extends Controller { $service = new AutoPlannogrammaService(); $isEditable = date($model->year . '-' . $model->month . '-d') > date('Y-m-d') && ( - (date('d') < 25) || (date('Y-m-d', strtotime('-1 month', strtotime(date($model->year . '-' . $model->month . '-d')))) > date('Y-m-d'))); + (date('d') < 27) || (date('Y-m-d', strtotime('-1 month', strtotime(date($model->year . '-' . $model->month . '-d')))) > date('Y-m-d'))); $categoryPlan = CategoryPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->indexBy('category')->asArray()->all(); $types = []; diff --git a/erp24/views/category-plan/index.php b/erp24/views/category-plan/index.php index 7f63250e..b9afcce8 100644 --- a/erp24/views/category-plan/index.php +++ b/erp24/views/category-plan/index.php @@ -205,21 +205,21 @@ input[readonly] { $data = $categoryPlan[$type]['offline']; $p1 = $offline_sale <= 0 ? 0 : round( $data / $offline_sale, 6) * 100; ?> - 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 1);']) ?>% - 'number', 'readonly' => !$isEditable, 'onchange' => 'editField(this);']) ?> + data-offline="" data-offline-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 1);']) ?>% + 'number', 'readonly' => true, 'onchange' => 'editField(this);']) ?> - 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 2);']) ?>% - 'number', 'readonly' => !$isEditable, 'onchange' => 'editField(this);']) ?> + data-online="" data-online-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 2);']) ?>% + 'number', 'readonly' => true, 'onchange' => 'editField(this);']) ?> ?> - 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 3);']) ?>% - 'number', 'readonly' => !$isEditable, 'onchange' => 'editField(this);']) ?> + data-writeoffs="" data-writeoffs-type=""> 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 3);']) ?>% + 'number', 'readonly' => true, 'onchange' => 'editField(this);']) ?> diff --git a/erp24/web/js/category-plan/index.js b/erp24/web/js/category-plan/index.js index df3c96d1..d0c24865 100644 --- a/erp24/web/js/category-plan/index.js +++ b/erp24/web/js/category-plan/index.js @@ -32,67 +32,94 @@ function updateStores() { } function editField(zis) { - const tr = zis.parentNode.parentNode; - const store_id = document.querySelector("#selected-store").value; - const type = tr.querySelector("[data-type]").textContent; - const offline = tr.querySelector("input[name=offline]").value; - const internet_shop = tr.querySelector("input[name=internet_shop]").value; - const write_offs = tr.querySelector("input[name=write_offs]").value; - - const editFields = [ - offline, - internet_shop, - write_offs - ]; - - let succ = true; - editFields.forEach((el) => { - if (!isNumeric(el)) { - succ = false; - alert(el + " не число"); - console.log(el); + 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"]`); + + const offlineVal = offlineInput.value; + const internetVal = internetInput.value; + const writeoffsVal = writeoffsInput.value; + + // валидация + [offlineVal, internetVal, writeoffsVal].forEach(v => { + if (!isNumeric(v)) { + alert(v + ' не число'); + throw new Error('Некорректное число'); } }); - if (succ) { - const p1 = tr.querySelector("[data-p1]"); - const p2 = tr.querySelector("[data-p2]"); - const p3 = tr.querySelector("[data-p3]"); - p1.querySelector('input').value = (p1.dataset.offline > 0 ? Math.round(100.0 * offline / +p1.dataset.offline) : 0); - p2.querySelector('input').value = (p2.dataset.online > 0 ? Math.round(100.0 * internet_shop / +p2.dataset.online) : 0); - p3.querySelector('input').value = (p3.dataset.writeoffs > 0 ? Math.round(100.0 * write_offs / +p3.dataset.writeoffs) : 0); - - $.ajax({ - method: "POST", - url: '/category-plan/save-fields', - data: { - year: document.querySelector('#dynamicmodel-year').value, - month: document.querySelector('#dynamicmodel-month').value, - store_id, - type, - offline, - internet_shop, - write_offs, - [param26]: token26 - }, - dataType: "text", - success: function(data) { - console.log(data); - }, - }); - } + // ячейки с процентами по тому же 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; + p2Input.value = p2Cell.dataset.online > 0 + ? (100 * internetVal / +p2Cell.dataset.online).toFixed(2) + : 0; + p3Input.value = p3Cell.dataset.writeoffs > 0 + ? (100 * writeoffsVal / +p3Cell.dataset.writeoffs).toFixed(2) + : 0; + + // сохраняем на сервере + $.ajax({ + method: "POST", + url: '/category-plan/save-fields', + data: { + year: document.querySelector('#dynamicmodel-year').value, + month: document.querySelector('#dynamicmodel-month').value, + store_id, + type, + offline: offlineVal, + internet_shop: internetVal, + write_offs: writeoffsVal, + [param26]: token26 + }, + dataType: "text" + }); } function editProcent(zis, num) { - const tr = zis.parentNode.parentNode; - const p1 = tr.querySelector("[data-p1]"); - const p2 = tr.querySelector("[data-p2]"); - const p3 = tr.querySelector("[data-p3]"); + 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: tr.querySelector("input[name=offline]").value = Math.round(p1.querySelector('input').value / 100.0 * (+p1.dataset.offline)); break; - case 2: tr.querySelector("input[name=internet_shop]").value = Math.round(p2.querySelector('input').value / 100.0 * (+p2.dataset.online)); break; - case 3: tr.querySelector("input[name=write_offs]").value = Math.round(p3.querySelector('input').value / 100.0 * (+p3.dataset.writeoffs)); break; + case 1: { + const pct = +p1Cell.querySelector('input').value; + const newVal = Math.round(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 = Math.round(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 = Math.round(pct / 100 * +p3Cell.dataset.writeoffs); + tr.querySelector(`td[data-writeoffs-type="${type}"] input[name="write_offs"]`).value = newVal; + break; + } } + editField(zis); } -- 2.39.5