]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
исправление JS
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 25 Jun 2025 09:24:21 +0000 (12:24 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 25 Jun 2025 09:24:21 +0000 (12:24 +0300)
erp24/views/category-plan/index.php
erp24/web/js/category-plan/index.js

index b9afcce8b0ef0dab4b16be6bf265ddf34bb537ba..a5af77cc1ec0301fc9c5bab3e792147710ea36dd 100644 (file)
@@ -203,23 +203,23 @@ input[readonly] {
                             <th data-type><?= $type ?></th>
                             <?php
                             $data = $categoryPlan[$type]['offline'];
-                            $p1 = $offline_sale <= 0 ? 0 : round( $data / $offline_sale, 6) * 100;
+                            $p1 = $offline_sale <= 0 ? 0 : round( $data / $offline_sale, 2) * 100;
                             ?>
-                            <td data-p1-<?= $type ?> data-offline="<?= $offline_sale ?>" data-offline-type="<?= $type ?>"><?= Html::textInput('p1', number_format($p1, 2, '.', ''), ['type' => 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 1);']) ?>%</td>
-                            <td data-offline-type="<?= $type ?>"><?= Html::textInput('offline', number_format($categoryPlan[$type]['offline'], 0, '.', ''), ['type' => 'number', 'readonly' => true, 'onchange' => 'editField(this);']) ?></td>
+                            <td data-p1-<?= $type ?> data-offline="<?= $offline_sale ?>" data-offline-type="<?= $type ?>"><?= Html::textInput('p1', number_format($p1, 2, '.', ''), ['type' => 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this);']) ?>%</td>
+                            <td data-offline-type="<?= $type ?>"><?= Html::textInput('offline', number_format($categoryPlan[$type]['offline'], 0, '.', ''), ['type' => 'number', 'readonly' => true, ]) ?></td>
                             <?php
                             $data2 = $categoryPlan[$type]['internet_shop'];
-                            $p2 = $online_sale <= 0 ? 0 : round( $data2 / $online_sale, 6) * 100;
+                            $p2 = $online_sale <= 0 ? 0 : round( $data2 / $online_sale, 2) * 100;
                             ?>
-                            <td data-p2-<?= $type ?> data-online="<?= $online_sale ?>" data-online-type="<?= $type ?>"><?= Html::textInput('p2', number_format($p2, 2, '.', ''), ['type' => 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 2);']) ?>%</td>
-                            <td data-online-type="<?= $type ?>"><?= Html::textInput('internet_shop', number_format($categoryPlan[$type]['internet_shop'], 0, '.', ''), ['type' => 'number', 'readonly' => true, 'onchange' => 'editField(this);']) ?></td>
+                            <td data-p2-<?= $type ?> data-online="<?= $online_sale ?>" data-online-type="<?= $type ?>"><?= Html::textInput('p2', number_format($p2, 2, '.', ''), ['type' => 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this);']) ?>%</td>
+                            <td data-online-type="<?= $type ?>"><?= Html::textInput('internet_shop', number_format($categoryPlan[$type]['internet_shop'], 0, '.', ''), ['type' => 'number', 'readonly' => true, ]) ?></td>
                             <?php //<td></td><td></td> ?>
                             <?php
                             $data4 = $categoryPlan[$type]['write_offs'];
-                            $p3 = $write_offs <= 0 ? 0 : round( $data4 / $write_offs, 6) * 100;
+                            $p3 = $write_offs <= 0 ? 0 : round( $data4 / $write_offs, 2) * 100;
                             ?>
-                            <td data-p3-<?= $type ?> data-writeoffs="<?= $write_offs ?>" data-writeoffs-type="<?= $type ?>"><?= Html::textInput('p3', number_format($p3, 2, '.', ''), ['type' => 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this, 3);']) ?>%</td>
-                            <td data-writeoffs-type="<?= $type ?>"><?= Html::textInput('write_offs', number_format($categoryPlan[$type]['write_offs'], 0, '.', ''), ['type' => 'number',  'readonly' => true, 'onchange' => 'editField(this);']) ?></td>
+                            <td data-p3-<?= $type ?> data-writeoffs="<?= $write_offs ?>" data-writeoffs-type="<?= $type ?>"><?= Html::textInput('p3', number_format($p3, 2, '.', ''), ['type' => 'number', 'style' => 'max-width: 80px;', 'readonly' => !$isEditable, 'onchange' => 'editProcent(this);']) ?>%</td>
+                            <td data-writeoffs-type="<?= $type ?>"><?= Html::textInput('write_offs', number_format($categoryPlan[$type]['write_offs'], 0, '.', ''), ['type' => 'number',  'readonly' => true, ]) ?></td>
                         </tr>
                     <?php endforeach; ?>
                 </tbody>
index e930000a4ea0dea0ee8d17ccce192fd37192d05b..9532403b88efa40966fcdbefb02b6e1c9044a073 100644 (file)
@@ -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,