]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-302 Редактирование букета
authormarina <m.zozirova@gmail.com>
Fri, 21 Feb 2025 07:09:42 +0000 (10:09 +0300)
committermarina <m.zozirova@gmail.com>
Fri, 21 Feb 2025 07:09:42 +0000 (10:09 +0300)
erp24/controllers/BouquetController.php
erp24/records/BouquetComposition.php
erp24/views/bouquet/_product_edit.php
erp24/views/bouquet/create.php
erp24/views/bouquet/update.php
erp24/web/js/bouquet/bouquet.js

index 13fda34e50fb11f186b74a993351885ae5b46d53..5c68af5cbea29dcaef6831ac0332c26f62e26481 100644 (file)
@@ -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),
+        ];
+    }
+
+
 }
index df6111100ba2b9d5a1213f5dd2ac62bfffc7b3c8..0d8e0c5e529ac9cf07f17d7c1fc92f4acaf82278 100644 (file)
@@ -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;
index 13fb28eac2c661d2afc1f5f4456cbaa7be38c7c3..1f3479cc03e40c6a0cf7438e256ededc3de2458a 100644 (file)
@@ -16,18 +16,25 @@ use yii\helpers\Html;
 ]) ?>
 <br>
 <div class="row">
-    <div class="col-md-3">
+    <div class="col-md-4">
         <div class="pt-2">
-            <p class="mb-1"><strong>Себестоимость:</strong> <span class="cost-value">0</span> ₽</p>
-            <p class="mb-1"><strong>Наценка:</strong> <span class="markup-value">0</span> %</p>
-            <p class="mb-0"><strong>Цена:</strong> <span class="price-value">0</span> ₽</p>
+            <p class="mb-1"><strong>Себестоимость:</strong> <span class="selfcost-value"><?= $selfCost ?? 0 ?></span> ₽</p>
+            <p class="mb-1"><strong>Наценка:</strong> <span class="markup-value"><?= $markUp?? 0 ?></span></p>
+            <p class="mb-0"><strong>Цена:</strong> <span class="cost-value"><?= $cost ?? 0 ?></span> ₽</p>
         </div>
     </div>
-    <div class="col-md-6"></div>
+<div class="col-md-4"></div>
     <?php if (!$isCreate) {?>
+        <div class="col-md-2 d-flex justify-content-end align-items-end px-3 w-100">
+            <?= Html::button('Рассчитать', ['class' => 'btn btn-warning calculate-btn w-100']) ?>
+        </div>
     <div class="col-md-2 d-flex justify-content-end align-items-end px-3 w-100">
         <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success w-100']) ?>
     </div>
-    <?php } ?>
+    <?php } else { ?>
+        <div class="col-md-4 d-flex justify-content-end align-items-end px-3 w-100">
+            <?= Html::button('Рассчитать', ['class' => 'btn btn-warning calculate-btn w-100']) ?>
+        </div>
+    <?php }  ?>
 
 </div>
index be17ce0d43278a49d70f10ed02bece16c648c8b2..2f88ad8de1b95cb8a2e88d1e41c1306fa5714c40 100644 (file)
@@ -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]);
 ?>
 
+
 <div class="bouquet-create border-bottom-4 p-6">
     <?= $this->render('_form', [
         'onlineStoresList' => $onlineStoresList,
index b83fde8772000bac9a6d4d313136f44503f7b8a3..6c9c7460ddd38f626936ff2053b549158662fdc8 100644 (file)
@@ -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
     <h2 class="d-inline"><strong><?= Html::encode($this->title) ?></strong></h2>
     <div class="row">
         <div class="col-md-2">
-            <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary ms-3 mt-4 w-45']) ?>
+            <?= Html::a('Назад', Url::to(['bouquet/view', 'id' => $model->id], true), ['class' => 'btn btn-primary ms-3 mt-4 w-45']) ?>
         </div>
     </div>
 
@@ -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(),
             ]); ?>
             <?php ActiveForm::end(); ?>
         </div>
\ No newline at end of file
index e724f7aab098c355f833e7117b500cf6b43a25ac..03c72a73c48f50487acdffc067576f2c5a86a36a 100644 (file)
@@ -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);
+        }
+    });
+});