From: Alexander Smirnov Date: Thu, 5 Dec 2024 11:58:12 +0000 (+0300) Subject: [ERP-258] массовая загрузка групп X-Git-Tag: 1.7~189^2~17 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=f5fe83bd2fb208140bc21af178ec9e796546bea2;p=erp24_rep%2Fyii-erp24%2F.git [ERP-258] массовая загрузка групп --- diff --git a/erp24/controllers/ShiftTransferController.php b/erp24/controllers/ShiftTransferController.php index 36362a9c..68cfeca2 100644 --- a/erp24/controllers/ShiftTransferController.php +++ b/erp24/controllers/ShiftTransferController.php @@ -51,6 +51,39 @@ class ShiftTransferController extends Controller return $this->render('index', compact('shiftTransfers', 'storeNameById', 'admins')); } + public function buildLoadDataShiftRemains($groups, $storeGuid) { + $productsClass = ProductsClass::find()->select(['category_id'])->where(['tip' => $groups])->column(); + $products = ArrayHelper::map(Products1c::find()->select(['id', 'name']) + ->where(['parent_id' => $productsClass])->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'); + $balance = ArrayHelper::map(Balances::find()->select(['quantity', 'product_id']) + ->where(['store_id' => $storeGuid])->all(), 'product_id', 'quantity'); + $productGuids = []; + foreach ($products as $key => $name) { + if (isset($balance[$key])) { + $productGuids[$key] = $name . ' (' . $balance[$key] . ' шт.)'; + } + else { + $productGuids[$key] = $name; + } + } + $price = ArrayHelper::map(Prices::find()->select(['product_id', 'price'])->where(['product_id' => array_keys($products)])->all(), 'product_id', 'price'); + $storeEIT = ExportImportTable::find()->select(['entity_id'])->where(['entity' => 'city_store', 'export_val' => $storeGuid, 'export_id' => 1])->one(); + $selfCost = ArrayHelper::map(SelfCostProduct::find()->select(['product_guid', 'price'])->where(['product_guid' => + array_keys($products), 'store_id' => $storeEIT->entity_id])->all(), 'product_guid', 'price'); + + $loadDataShiftRemains = []; + foreach ($productGuids as $key => $name) { + $loadDataShiftRemains[]= [ + 'product_guid' => $key, 'retail_price' => $price[$key] ?? '', + 'self_cost' => $selfCost[$key] ?? '', + 'remains_summ' => isset($price[$key]) && isset($balance[$key]) ? $price[$key] * $balance[$key]: '', + 'remains_count' => $balance[$key] ?? '', + 'fact_and_1c_diff' => 0, 'remains_1c' => $balance[$key] ?? '' + ]; + } + return compact('loadDataShiftRemains', 'productGuids'); + } + public function actionCreate() { $shiftTransfer = new ShiftTransfer; @@ -58,18 +91,30 @@ class ShiftTransferController extends Controller $shiftTransfer->date_start = $shiftTransfer->date; $shiftTransfer->status_id = ShiftTransfer::STATUS_ID_INPUT_FACT_REMAINS; $shiftTransfer->end_shift_admin_id = Yii::$app->user->id; + $shiftTransfer->groups2 = [ProductsClass::HINT_OTHER_ITEMS]; $isCreate = true; $storeNameById = TaskService::getEntitiesByAlias('store'); $storeGuids = AdminStores::find()->select(['store_guid'])->where(['admin_id' => Yii::$app->user->id])->column(); + $firstId = null; foreach ($storeNameById as $id => $name) { if (!in_array($id, $storeGuids)) { unset($storeNameById[$id]); + continue; + } + if (!$firstId) { + $firstId = $id; } } - return $this->render('update', compact('shiftTransfer', 'isCreate', 'storeNameById')); + $data = self::buildLoadDataShiftRemains($shiftTransfer->groups2, $firstId); + $loadDataShiftRemains = $data['loadDataShiftRemains']; + $productGuids = $data['productGuids']; + + $shiftTransfer->shiftRemainsCopy = $loadDataShiftRemains ?? $shiftTransfer->shiftRemains; + + return $this->render('update', compact('shiftTransfer', 'isCreate', 'storeNameById', 'productGuids')); } public function actionUpdate($id = null) @@ -82,39 +127,48 @@ class ShiftTransferController extends Controller $loadDataShiftRemains = null; if ($shiftTransfer->load(Yii::$app->request->post())) { $postShiftTransfer = Yii::$app->request->post('ShiftTransfer'); - $loadDataShiftRemains = ArrayHelper::getValue($postShiftTransfer, 'shiftRemainsCopy'); - - if ($shiftTransfer->id || ($shiftTransfer->validate() && $shiftTransfer->save())) { - ShiftRemains::deleteAll(['shift_transfer_id' => $shiftTransfer->id]); - $modelsShiftRemains = MultipleModel::createMultipleModel(ShiftRemains::class, - 'ShiftTransfer', 'shiftRemainsCopy'); - if (!empty($loadDataShiftRemains)) { - MultipleModel::loadMultipleFromArray($modelsShiftRemains, $loadDataShiftRemains, '', []); - } + $action = Yii::$app->request->post('action'); + if ($action == 'applyGroups') { + $data = self::buildLoadDataShiftRemains(array_merge(empty($postShiftTransfer['groups1']) ? [] : $postShiftTransfer['groups1'], + empty($postShiftTransfer['groups2']) ? [] : $postShiftTransfer['groups2']), $postShiftTransfer['store_guid']); + $loadDataShiftRemains = $data['loadDataShiftRemains']; + $productGuids = $data['productGuids']; + } else { + $loadDataShiftRemains = ArrayHelper::getValue($postShiftTransfer, 'shiftRemainsCopy'); + + if ($shiftTransfer->id || ($shiftTransfer->validate() && $shiftTransfer->save())) { + ShiftRemains::deleteAll(['shift_transfer_id' => $shiftTransfer->id]); + $modelsShiftRemains = MultipleModel::createMultipleModel(ShiftRemains::class, + 'ShiftTransfer', 'shiftRemainsCopy'); + if (!empty($loadDataShiftRemains)) { + MultipleModel::loadMultipleFromArray($modelsShiftRemains, $loadDataShiftRemains, '', []); + } - foreach ($modelsShiftRemains as $modelsShiftRemain) { - $modelsShiftRemain->shift_transfer_id = $shiftTransfer->id; - if ($modelsShiftRemain->validate()) { - $modelsShiftRemain->save(); - } else { - var_dump($modelsShiftRemain->getErrors()); - die; + foreach ($modelsShiftRemains as $modelsShiftRemain) { + $modelsShiftRemain->shift_transfer_id = $shiftTransfer->id; + if ($modelsShiftRemain->validate()) { + $modelsShiftRemain->save(); + } else { + var_dump($modelsShiftRemain->getErrors()); + die; + } } - } - if ($shiftTransfer->validate()) { - $shiftTransfer->goods_transfer_summ = array_sum(ArrayHelper::getColumn($shiftTransfer->shiftRemains, 'remains_summ')); - $shiftTransfer->goods_transfer_count = array_sum(ArrayHelper::getColumn($shiftTransfer->shiftRemains, 'remains_count')); - $shiftTransfer->discrepancy_pieces = array_sum(ArrayHelper::getColumn($shiftTransfer->shiftRemains, 'fact_and_1c_diff')); - $shiftTransfer->discrepancy_rubles = array_sum(ArrayHelper::getColumn($shiftTransfer->shiftRemains, 'remains_1c')); - $shiftTransfer->status_id = ShiftTransfer::STATUS_ID_TRANSFER_ACTIONS; - $shiftTransfer->save(); + if ($shiftTransfer->validate()) { + $shiftTransfer->goods_transfer_summ = array_sum(ArrayHelper::getColumn($shiftTransfer->shiftRemains, 'remains_summ')); + $shiftTransfer->goods_transfer_count = array_sum(ArrayHelper::getColumn($shiftTransfer->shiftRemains, 'remains_count')); + $shiftTransfer->discrepancy_pieces = array_sum(ArrayHelper::getColumn($shiftTransfer->shiftRemains, 'fact_and_1c_diff')); + $shiftTransfer->discrepancy_rubles = array_sum(ArrayHelper::getColumn($shiftTransfer->shiftRemains, 'remains_1c')); + $shiftTransfer->status_id = ShiftTransfer::STATUS_ID_TRANSFER_ACTIONS; + $shiftTransfer->setProductGroups(); + $shiftTransfer->save(); - Yii::$app->session->setFlash('info', 'Документ передачи смены успешно сохранён. Сейчас документ находится в статусе Действия по замене.
' . - 'Для продолжения оформления документа в таблице возможных замен добавьте замену недостающего товара.'); + Yii::$app->session->setFlash('info', 'Документ передачи смены успешно сохранён. Сейчас документ находится в статусе Действия по замене.
' . + 'Для продолжения оформления документа в таблице возможных замен добавьте замену недостающего товара.'); - return $this->redirect(['/shift-transfer/view', 'id' => $shiftTransfer->id]); - } + return $this->redirect(['/shift-transfer/view', 'id' => $shiftTransfer->id]); + } + } } } @@ -130,7 +184,7 @@ class ShiftTransferController extends Controller $shiftTransfer->shiftRemainsCopy = $loadDataShiftRemains ?? $shiftTransfer->shiftRemains; - return $this->render('update', compact('shiftTransfer', 'isCreate', 'storeNameById')); + return $this->render('update', compact('shiftTransfer', 'isCreate', 'storeNameById', 'productGuids')); } diff --git a/erp24/migrations/m241205_080132_alter_table_shift_transfer_product_groups.php b/erp24/migrations/m241205_080132_alter_table_shift_transfer_product_groups.php new file mode 100755 index 00000000..cfee66ef --- /dev/null +++ b/erp24/migrations/m241205_080132_alter_table_shift_transfer_product_groups.php @@ -0,0 +1,26 @@ +addColumn(self::TABLE_NAME, 'product_groups', $this->text()->null()->comment('Список alias выбранных чекбоксов, через запятую')); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropColumn(self::TABLE_NAME, 'product_groups'); + } +} diff --git a/erp24/records/ShiftTransfer.php b/erp24/records/ShiftTransfer.php index 05867dd4..e46a5413 100644 --- a/erp24/records/ShiftTransfer.php +++ b/erp24/records/ShiftTransfer.php @@ -21,10 +21,13 @@ use Yii; * @property float|null $discrepancy_rubles Расхождение факта, руб. * @property string|null $comment Комментарий принимающей стороны * @property string|null $report в формате HTML или TXT или JSON внешний вид всех данных при приеме + * @property string|null $product_groups Список alias выбранных чекбоксов, через запятую */ class ShiftTransfer extends \yii\db\ActiveRecord { public $shiftRemainsCopy; + public $groups1; + public $groups2; const STATUS_ID_INPUT_FACT_REMAINS = 1; const STATUS_ID_TRANSFER_ACTIONS = 2; @@ -57,9 +60,9 @@ class ShiftTransfer extends \yii\db\ActiveRecord [['status_id', 'date', 'date_start', 'store_guid', 'end_shift_admin_id'], 'required'], [['status_id', 'end_shift_admin_id', 'start_shift_admin_id'], 'default', 'value' => null], [['status_id', 'end_shift_admin_id', 'start_shift_admin_id'], 'integer'], - [['date_start', 'date_end'], 'safe'], + [['date_start', 'date_end', 'groups1', 'groups2'], 'safe'], [['goods_transfer_summ', 'goods_transfer_count', 'discrepancy_pieces', 'discrepancy_rubles'], 'number'], - [['comment', 'report'], 'string'], + [['comment', 'report', 'product_groups'], 'string'], [['date', 'store_guid'], 'string', 'max' => 36], ]; } @@ -84,6 +87,9 @@ class ShiftTransfer extends \yii\db\ActiveRecord 'discrepancy_rubles' => 'Расхождение факта, руб.', 'comment' => 'Комментарий', 'report' => 'Отчёт', + 'product_groups' => 'Продуктовые группы', + 'groups1' => 'Другая группа', + 'groups2' => 'Основная группа' ]; } @@ -98,4 +104,15 @@ class ShiftTransfer extends \yii\db\ActiveRecord { return $this->hasMany(EqualizationRemains::class, ['shift_id' => 'id']); } + + public function setGroups() { + function cb($a) { return $a != 'other_items'; } + function cb2($a) { return $a == 'other_items'; } + $this->groups1 = array_filter(explode(',', $this->product_groups ?? ''), 'cb'); + $this->groups2 = array_filter(explode(',', $this->product_groups ?? ''), 'cb2'); + } + + public function setProductGroups() { + $this->product_groups = implode(',', array_merge(empty($this->groups1) ? [] : $this->groups1, empty($this->groups2) ? [] : $this->groups2)); + } } diff --git a/erp24/views/shift-transfer/update.php b/erp24/views/shift-transfer/update.php index a5b33e3d..3e85ea99 100644 --- a/erp24/views/shift-transfer/update.php +++ b/erp24/views/shift-transfer/update.php @@ -16,6 +16,7 @@ use yii_app\records\ProductsClass; /* @var $isCreate bool */ /* @var $shiftTransfer ShiftTransfer */ /* @var $storeNameById array */ +/* @var $productGuids array */ $this->registerJsFile('/js/shift-transfer/update.js', ['position' => \yii\web\View::POS_END]); @@ -48,7 +49,9 @@ $this->registerCss('
- field($shiftTransfer, 'store_guid')->dropDownList($storeNameById, ['onchange' => 'updateProductsWithBalanceAll();']) ?> + field($shiftTransfer, 'store_guid')->dropDownList($storeNameById, ['onchange' + => 'const inp = document.createElement("input"); inp.setAttribute("type", "hidden"); inp.setAttribute("name", "action");' . + ' inp.setAttribute("value", "applyGroups"); this.form.append(inp); this.form.submit();']) ?>
@@ -65,6 +68,23 @@ $this->registerCss(' +
+
+ Другие +
+ + field($shiftTransfer, 'groups1')->checkboxList($hints1, ['checked' => 1]) ?> +
+
+
+ 'Номенклатура 1%'] ?> + field($shiftTransfer, 'groups2')->checkboxList($hints2, ['checked' => 1]) ?> +
+
+ 'btn btn-success', 'name' => 'action', 'value' => 'applyGroups']) ?> +
+
+
field($shiftTransfer, 'shiftRemainsCopy')->widget(MultipleInput::class, [ @@ -76,8 +96,9 @@ $this->registerCss(' 'title' => 'Продукт', 'type' => Select2::class, 'options' => [ - 'data' => ArrayHelper::map(Products1c::find()->select(['id', 'name']) - ->where(['tip' => 'products'])->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'), + 'data' => $productGuids, +// ArrayHelper::map(Products1c::find()->select(['id', 'name']) +// ->where(['tip' => 'products'])->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'), ], 'value' => function($data) { return $data['product_guid'] ?? ''; @@ -86,7 +107,7 @@ $this->registerCss(' 'style' => 'width: 70px;', ] ], - [ + /*[ 'name' => 'group_label', 'title' => 'Название группы', 'type' => Select2::class, @@ -100,74 +121,74 @@ $this->registerCss(' 'headerOptions' => [ 'style' => 'width: 70px;', ] - ], + ], */ [ - 'name' => 'retail_price', - 'title' => 'Розничная цена, руб', + 'name' => 'remains_count', + 'title' => 'Фактические остатки кол-во, шт', 'type' => BaseColumn::TYPE_TEXT_INPUT, - 'options' => ['type' => 'number', 'step' => 0.01, 'readonly' => true], + 'options' => ['type' => 'number', 'step' => 1/*, 'min' => 0*/], 'value' => function($data) { - return $data['retail_price'] ?? ''; + return $data['remains_count'] ?? ''; }, 'headerOptions' => [ 'style' => 'width: 70px;', ] ], [ - 'name' => 'self_cost', - 'title' => 'Себестоимость, руб', + 'name' => 'fact_and_1c_diff', + 'title' => 'Разница факт и по программе 1с, шт', 'type' => BaseColumn::TYPE_TEXT_INPUT, 'options' => ['type' => 'number', 'step' => 0.01, 'readonly' => true], 'value' => function($data) { - return $data['self_cost'] ?? ''; + return $data['fact_and_1c_diff'] ?? ''; }, 'headerOptions' => [ 'style' => 'width: 70px;', ] ], [ - 'name' => 'remains_summ', - 'title' => 'Сумма остатков (недостача или излишек), руб', + 'name' => 'remains_1c', + 'title' => 'Остатки по 1с, шт', 'type' => BaseColumn::TYPE_TEXT_INPUT, 'options' => ['type' => 'number', 'step' => 0.01, 'readonly' => true], 'value' => function($data) { - return $data['remains_summ'] ?? ''; + return $data['remains_1c'] ?? ''; }, 'headerOptions' => [ 'style' => 'width: 70px;', ] ], [ - 'name' => 'remains_count', - 'title' => 'Фактические остатки кол-во, шт', + 'name' => 'retail_price', + 'title' => 'Розничная цена, руб', 'type' => BaseColumn::TYPE_TEXT_INPUT, - 'options' => ['type' => 'number', 'step' => 1, 'min' => 0], + 'options' => ['type' => 'number', 'step' => 0.01, 'readonly' => true], 'value' => function($data) { - return $data['remains_count'] ?? ''; + return $data['retail_price'] ?? ''; }, 'headerOptions' => [ 'style' => 'width: 70px;', ] ], [ - 'name' => 'fact_and_1c_diff', - 'title' => 'Разница факт и по программе 1с, шт', + 'name' => 'self_cost', + 'title' => 'Себестоимость, руб', 'type' => BaseColumn::TYPE_TEXT_INPUT, 'options' => ['type' => 'number', 'step' => 0.01, 'readonly' => true], 'value' => function($data) { - return $data['fact_and_1c_diff'] ?? ''; + return $data['self_cost'] ?? ''; }, 'headerOptions' => [ 'style' => 'width: 70px;', ] ], [ - 'name' => 'remains_1c', - 'title' => 'Остатки по 1с, шт', + 'name' => 'remains_summ', + 'title' => 'Сумма остатков (недостача или излишек), руб', 'type' => BaseColumn::TYPE_TEXT_INPUT, 'options' => ['type' => 'number', 'step' => 0.01, 'readonly' => true], 'value' => function($data) { - return $data['remains_1c'] ?? ''; + return $data['remains_summ'] ?? ''; }, 'headerOptions' => [ 'style' => 'width: 70px;', diff --git a/erp24/web/js/shift-transfer/update.js b/erp24/web/js/shift-transfer/update.js index aea5b7e0..ee787388 100644 --- a/erp24/web/js/shift-transfer/update.js +++ b/erp24/web/js/shift-transfer/update.js @@ -4,39 +4,39 @@ const param24 = $('meta[name=csrf-param]').attr('content'); const token24 = $('meta[name=csrf-token]').attr('content'); -function updateProductsWithBalance(currentIndex, row) { - const store = $('#shifttransfer-store_guid'); - const productGuid = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-product_guid'); - productGuid.empty(); - $.ajax({ - type: 'POST', - url: '/shift-transfer/get-products-with-remains', - data: { storeGuid: store.val(), [param24]: token24 }, - dataType: 'json', - success: (data) => { - console.log(data); - $.each(data, (key, el) => { - console.log(key, el) - const opt = document.createElement('option'); - opt.text = el; - opt.value = key; - productGuid.append(opt); - }) - } - }); -} +// function updateProductsWithBalance(currentIndex, row) { +// const store = $('#shifttransfer-store_guid'); +// const productGuid = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-product_guid'); +// productGuid.empty(); +// $.ajax({ +// type: 'POST', +// url: '/shift-transfer/get-products-with-remains', +// data: { storeGuid: store.val(), [param24]: token24 }, +// dataType: 'json', +// success: (data) => { +// console.log(data); +// $.each(data, (key, el) => { +// console.log(key, el) +// const opt = document.createElement('option'); +// opt.text = el; +// opt.value = key; +// productGuid.append(opt); +// }) +// } +// }); +// } -function updateProductsWithBalanceAll() { - const rows = $('.multiple-input-list__item'); - for (let currentIndex = 0; currentIndex < rows.length; currentIndex++) { - updateProductsWithBalance(currentIndex + 1, rows[currentIndex]); - } -} +// function updateProductsWithBalanceAll() { +// const rows = $('.multiple-input-list__item'); +// for (let currentIndex = 0; currentIndex < rows.length; currentIndex++) { +// updateProductsWithBalance(currentIndex + 1, rows[currentIndex]); +// } +// } function checkInputRow(currentIndex, row, errorContainer) { const productGuid = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-product_guid'); const productName = $(row).find('#select2-shifttransfer-shiftremainscopy-' + currentIndex + '-product_guid-container').text(); - const groupLabel = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-group_label'); + //const groupLabel = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-group_label'); const retailPrice = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-retail_price'); const selfCost = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-self_cost'); const remains1c = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-remains_1c'); @@ -49,7 +49,7 @@ function checkInputRow(currentIndex, row, errorContainer) { } return checkField(productGuid, currentIndex, productName, 'Не выбран продукт.', errorContainer) && - checkField(groupLabel, currentIndex, productName, 'Пустое название группы.', errorContainer) && + // checkField(groupLabel, currentIndex, productName, 'Пустое название группы.', errorContainer) && checkField(retailPrice, currentIndex, productName, 'Не назначена розничная цена.', errorContainer) && checkField(selfCost, currentIndex, productName, 'Отсутствует себестоимость.', errorContainer) && checkField(remains1c, currentIndex, productName, 'Отсутствует информация об остатках в 1с.', errorContainer) && @@ -63,7 +63,7 @@ function checkInputs() { let noError = true; const errorContainer = []; for (let currentIndex = 0; currentIndex < rows.length; currentIndex++) { - noError = checkInputRow(currentIndex + 1, rows[currentIndex], errorContainer) && noError; + noError = checkInputRow(currentIndex, rows[currentIndex], errorContainer) && noError; } if (errorContainer.length > 0) { alert(errorContainer.join('\n')); @@ -72,9 +72,9 @@ function checkInputs() { } function setMultipleInputHandlers(currentIndex, row) { - updateProductsWithBalance(currentIndex, row); + // updateProductsWithBalance(currentIndex, row); const store = $('#shifttransfer-store_guid'); - const groupLabel = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-group_label'); + // const groupLabel = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-group_label'); const productGuid = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-product_guid'); const retailPrice = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-retail_price'); const selfCost = $(row).find('#shifttransfer-shiftremainscopy-' + currentIndex + '-self_cost'); @@ -95,7 +95,7 @@ function setMultipleInputHandlers(currentIndex, row) { data: { productGuid: e.target.value, storeGuid: store.val(), [param24]: token24 }, dataType: 'json', success: (data) => { - if (data.groupLabel) { groupLabel.val(data.groupLabel); groupLabel.select2('destroy'); groupLabel.select2(); } + // if (data.groupLabel) { groupLabel.val(data.groupLabel); groupLabel.select2('destroy'); groupLabel.select2(); } if (data.price) { retailPrice.val(data.price); } if (data.selfCost) { selfCost.val(data.selfCost); } if (data.quantity) { remains1c.val(data.quantity); }