From d52faff8cc4b893da841ef407f441589c3f89e88 Mon Sep 17 00:00:00 2001 From: marina Date: Fri, 21 Feb 2025 10:09:42 +0300 Subject: [PATCH] =?utf8?q?ERP-302=20=D0=A0=D0=B5=D0=B4=D0=B0=D0=BA=D1=82?= =?utf8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B1=D1=83?= =?utf8?q?=D0=BA=D0=B5=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/controllers/BouquetController.php | 14 ++++++++ erp24/records/BouquetComposition.php | 43 ++++++++++++++----------- erp24/views/bouquet/_product_edit.php | 19 +++++++---- erp24/views/bouquet/create.php | 3 ++ erp24/views/bouquet/update.php | 6 +++- erp24/web/js/bouquet/bouquet.js | 42 ++++++++++++++++++++++-- 6 files changed, 100 insertions(+), 27 deletions(-) diff --git a/erp24/controllers/BouquetController.php b/erp24/controllers/BouquetController.php index 13fda34e..5c68af5c 100644 --- a/erp24/controllers/BouquetController.php +++ b/erp24/controllers/BouquetController.php @@ -386,4 +386,18 @@ class BouquetController extends Controller 'marketplace' => $marketplaceList, ]; } + public function actionGetCalculates() + { + Yii::$app->response->format = Response::FORMAT_JSON; + $data = json_decode(Yii::$app->request->getRawBody(), true); + $model = new BouquetComposition(); + + return [ + 'selfcost' => round($model->getSelfCost($data), 2), + 'cost' => round($model->getCost($data), 2), + 'markup' => $model->getMarkUp($data), + ]; + } + + } diff --git a/erp24/records/BouquetComposition.php b/erp24/records/BouquetComposition.php index df611110..0d8e0c5e 100644 --- a/erp24/records/BouquetComposition.php +++ b/erp24/records/BouquetComposition.php @@ -103,57 +103,63 @@ class BouquetComposition extends ActiveRecord ->andWhere(['entity' => self::VIDEO_BUILD_PROCESS]); } - public function getCost() + public function getCost($data = null) { $cost = 0; $compositionProducts = $this->bouquetCompositionProducts; if (!$compositionProducts) { - return $cost; + $compositionProducts = $data; + if (empty($data)) { + return $cost; + } } foreach ($compositionProducts as $item) { - if (!$item || !isset($item->product_guid)) { + if (!$item || !isset($item['product_guid'])) { continue; } $priceModel = PricesDynamic::find() - ->where(['product_id' => $item->product_guid]) + ->where(['product_id' => $item['product_guid']]) ->andWhere(['=', 'active', PricesDynamic::ACTIVE]) ->one(); if ($priceModel) { - $cost += $priceModel->price * $item->count; + $cost += $priceModel->price * $item['count']; } } return $cost; } - public function getMarkUp() + public function getMarkUp($data = null) { - if ($this->getSelfCost() == 0 || $this->getCost() == 0) + if ($this->getSelfCost($data) == 0 || $this->getCost($data) == 0) return 0; return sprintf("+%.2f%% / +%.2f", - (self::getCost() / self::getSelfCost() - 1) * 100, - self::getCost() - self::getSelfCost() + (self::getCost($data) / self::getSelfCost($data) - 1) * 100, + self::getCost($data) - self::getSelfCost($data) ); } - public function getSelfCost() + public function getSelfCost($data = null) { $selfCost = 0; $compositionProducts = $this->bouquetCompositionProducts; if (!$compositionProducts) { - return $selfCost; + $compositionProducts = $data; + if (empty($data)) { + return $selfCost; + } } - - $productGuids = array_filter(array_column($compositionProducts, 'product_guid')); - + + $productGuids = array_filter(array_column($compositionProducts, 'product_guid')); + if (empty($productGuids)) { return $selfCost; } @@ -172,14 +178,15 @@ class BouquetComposition extends ActiveRecord } foreach ($compositionProducts as $item) { - if (!$item || !isset($item->product_guid)) { + if (!$item || !isset($item['product_guid'])) { continue; } - $prices = $pricesByProduct[$item->product_guid] ?? []; + $prices = $pricesByProduct[$item['product_guid']] ?? []; $count = count($prices); - if ($count === 0) { + + if ($count == 0) { continue; } @@ -189,7 +196,7 @@ class BouquetComposition extends ActiveRecord ? ($prices[$middle - 1] + $prices[$middle]) / 2 : $prices[$middle]; - $selfCost += $median * $item->count; + $selfCost += $median * $item['count']; } return $selfCost; diff --git a/erp24/views/bouquet/_product_edit.php b/erp24/views/bouquet/_product_edit.php index 13fb28ea..1f3479cc 100644 --- a/erp24/views/bouquet/_product_edit.php +++ b/erp24/views/bouquet/_product_edit.php @@ -16,18 +16,25 @@ use yii\helpers\Html; ]) ?>
-
+
-

Себестоимость: 0 ₽

-

Наценка: 0 %

-

Цена: 0 ₽

+

Себестоимость: ₽

+

Наценка:

+

Цена: ₽

-
+
+
+ 'btn btn-warning calculate-btn w-100']) ?> +
'btn btn-success w-100']) ?>
- + +
+ 'btn btn-warning calculate-btn w-100']) ?> +
+
diff --git a/erp24/views/bouquet/create.php b/erp24/views/bouquet/create.php index be17ce0d..2f88ad8d 100644 --- a/erp24/views/bouquet/create.php +++ b/erp24/views/bouquet/create.php @@ -15,8 +15,11 @@ use yii\helpers\Url; $this->title = 'Состав букета'; $this->params['breadcrumbs'][] = ['label' => 'Букеты', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; + +$this->registerJsFile('/js/bouquet/bouquet.js', ['position' => \yii\web\View::POS_END]); ?> +
render('_form', [ 'onlineStoresList' => $onlineStoresList, diff --git a/erp24/views/bouquet/update.php b/erp24/views/bouquet/update.php index b83fde87..6c9c7460 100644 --- a/erp24/views/bouquet/update.php +++ b/erp24/views/bouquet/update.php @@ -3,6 +3,7 @@ use app\widgets\DualList; use yii\helpers\ArrayHelper; use yii\helpers\Html; +use yii\helpers\Url; use yii\widgets\ActiveForm; use yii_app\records\Products1c; use yii_app\records\Products1cNomenclature; @@ -24,7 +25,7 @@ $this->registerJsFile('/js/bouquet/bouquet.js', ['position' => \yii\web\View::PO

title) ?>

- 'btn btn-primary ms-3 mt-4 w-45']) ?> + $model->id], true), ['class' => 'btn btn-primary ms-3 mt-4 w-45']) ?>
@@ -58,6 +59,9 @@ $this->registerJsFile('/js/bouquet/bouquet.js', ['position' => \yii\web\View::PO 'selectedItems' => $selectedItems, 'isCreate' => false, 'listContainerSize' => [], + 'selfCost' => $model->getSelfCost(), + 'cost' => $model->getCost(), + 'markUp' => $model->getMarkUp(), ]); ?>
\ No newline at end of file diff --git a/erp24/web/js/bouquet/bouquet.js b/erp24/web/js/bouquet/bouquet.js index e724f7aa..03c72a73 100644 --- a/erp24/web/js/bouquet/bouquet.js +++ b/erp24/web/js/bouquet/bouquet.js @@ -25,12 +25,10 @@ $(document).ready(function () { data: { year: year, month: month, id: id }, dataType: 'json', success: function(response) { - console.log("Ответ сервера:", response); if (response) { ['offline', 'online', 'marketplace'].forEach(type => { $.each(response[type], function(_, value) { let inputSelector = `input[name='BouquetForecast[type_sales_value][${type}][${value.id}]']`; - console.log(`Поиск инпута: ${inputSelector}, установка значения: ${value.value}`); $(inputSelector).val(value.value); }); }); @@ -43,3 +41,43 @@ $(document).ready(function () { }); } }); + +$('.calculate-btn').on('click', function () { + let data = []; + + $('.selected-item').each(function () { + let id = $(this).data('id'); + let quantity = $(this).find('.quantity-input').val(); + + if (id && quantity) { + data.push({ + product_guid: id, + count: quantity + }); + } + }); + + $.ajax({ + url: '/bouquet/get-calculates', + type: 'POST', + contentType: 'application/json', + data: JSON.stringify(data), + dataType: 'json', + success: function(response) { + if (response) { + if (response.cost !== undefined) { + $('.price-value').text(response.cost); + } + if (response.selfcost !== undefined) { + $('.selfcost-value').text(response.selfcost); + } + if (response.markup !== undefined) { + $('.markup-value').text(response.markup); + } + } + }, + error: function(xhr, status, error) { + console.error("Ошибка загрузки данных:", status, error); + } + }); +}); -- 2.39.5