]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-137] Прогноз за месяц для мотивации
authorAlexander Smirnov <fredeom@mail.ru>
Fri, 9 Aug 2024 12:09:17 +0000 (15:09 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Fri, 9 Aug 2024 12:09:17 +0000 (15:09 +0300)
erp24/actions/motivation/IndexAction.php
erp24/migrations/m240809_115844_insert_forecast_into_motivation_value_group.php [new file with mode: 0755]
erp24/services/MotivationService.php

index 5d800c6e97fa3f2cf0282e0f77c15d75e9bca58e..86bb32e79a6d1c3595db0a900234e1aee6b5a8cc 100644 (file)
@@ -166,8 +166,8 @@ class IndexAction extends Action
             }
         }
 
-
-
+        // Подсчитываем прогноз за месяц
+        MotivationService::calculateMonthForecast($model->store_id, $model->year, $model->month);
 
         return $this->controller->render(
             'index',
diff --git a/erp24/migrations/m240809_115844_insert_forecast_into_motivation_value_group.php b/erp24/migrations/m240809_115844_insert_forecast_into_motivation_value_group.php
new file mode 100755 (executable)
index 0000000..9498eb1
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m240809_115844_insert_forecast_into_motivation_value_group
+ */
+class m240809_115844_insert_forecast_into_motivation_value_group extends Migration
+{
+    const TABLE_NAME = 'motivation_value_group';
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        $this->insert(self::TABLE_NAME, [
+            'id' => 9,
+            'name' => 'Прогноз',
+            'alias' => 'forecast'
+        ]);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->delete(self::TABLE_NAME, ['alias' => 'forecast']);
+    }
+}
index 0ada3d5e52587793fd22291d954eac4179dee154..6c90600104bbf4952cdfe26528d00513100a0d9b 100644 (file)
@@ -21,6 +21,33 @@ use yii_app\records\SalesProducts;
 class MotivationService
 {
 
+    // 6. Создание массива дополнительных элементов
+    static $additionalItems = [
+            80 => ['name' => 'Выручка от реализации'],
+            90 => ['name' => 'Продажа товара'],
+            115 => ['name' => 'Прочие услуги'],
+            135 => ['name' => 'Прямые расходы на продажу'],
+            143 => ['name' => 'Услуги агентов (Расходы на закупку, хранение, доставку товара)'],
+            146 => ['name' => 'Брак, пересорт'],
+            192 => ['name' => 'Маржинальный доход'],
+            194 => ['name' => 'Операционные расходы (Себестоимость)'],
+            196 => ['name' => 'Оплата труда'],
+            205 => ['name' => 'Содержание помещения'],
+            245 => ['name' => 'Расходы по доставке'],
+            275 => ['name' => 'Содержание и обслуживание ОС и НМА'],
+            315 => ['name' => 'Услуги связи'],
+            325 => ['name' => 'Прочие операционные расходы'],
+            353 => ['name' => 'Валовая прибыль'],
+            355 => ['name' => 'Общехозяйственные расходы'],
+            357 => ['name' => 'Бухгалтерия и финансы'],
+            365 => ['name' => 'Юридическое сопровождение'],
+            375 => ['name' => 'HR- услуги'],
+            395 => ['name' => 'IT услуги'],
+            425 => ['name' => 'Чистая прибыль'],
+            427 => ['name' => 'Рентабельность по чистой прибыли, %'],
+            428 => ['name' => 'Минимальный порог Чистой прибыли, руб.'],
+            435 => ['name' => 'Расчет премии']
+        ];
 
     private function getMotivationValueGroupAliases()
     {
@@ -110,36 +137,8 @@ class MotivationService
         // 5. Сортировка результата по ключу (order)
         ksort($result);
 
-        // 6. Создание массива дополнительных элементов
-        $additionalItems = [
-            80 => ['name' => 'Выручка от реализации'],
-            90 => ['name' => 'Продажа товара'],
-            115 => ['name' => 'Прочие услуги'],
-            135 => ['name' => 'Прямые расходы на продажу'],
-            143 => ['name' => 'Услуги агентов (Расходы на закупку, хранение, доставку товара)'],
-            146 => ['name' => 'Брак, пересорт'],
-            192 => ['name' => 'Маржинальный доход'],
-            194 => ['name' => 'Операционные расходы (Себестоимость)'],
-            196 => ['name' => 'Оплата труда'],
-            205 => ['name' => 'Содержание помещения'],
-            245 => ['name' => 'Расходы по доставке'],
-            275 => ['name' => 'Содержание и обслуживание ОС и НМА'],
-            315 => ['name' => 'Услуги связи'],
-            325 => ['name' => 'Прочие операционные расходы'],
-            353 => ['name' => 'Валовая прибыль'],
-            355 => ['name' => 'Общехозяйственные расходы'],
-            357 => ['name' => 'Бухгалтерия и финансы'],
-            365 => ['name' => 'Юридическое сопровождение'],
-            375 => ['name' => 'HR- услуги'],
-            395 => ['name' => 'IT услуги'],
-            425 => ['name' => 'Чистая прибыль'],
-            427 => ['name' => 'Рентабельность по чистой прибыли, %'],
-            428 => ['name' => 'Минимальный порог Чистой прибыли, руб.'],
-            435 => ['name' => 'Расчет премии']
-        ];
-
         // 7. Объединение массивов
-        foreach ($additionalItems as $key => $item) {
+        foreach (self::$additionalItems as $key => $item) {
             if (!isset($result[$key])) {
                 $result[$key] = array_merge($item, [
                     'plan' => null,
@@ -519,4 +518,57 @@ class MotivationService
             }
         }
     }
+
+    public static function calculateMonthForecast($store_id, $year, $month) {
+        $motivationCostsItem = MotivationCostsItem::find()->all();
+        $motivationCostsItemCodes = ArrayHelper::getColumn($motivationCostsItem, 'code');
+        $additionalItemsKeys = array_keys(self::$additionalItems);
+
+        $items = array_merge($motivationCostsItemCodes, $additionalItemsKeys);
+
+        $motivationValueGroups = [];
+        foreach (range(1, 5) as $ind) {
+            $motivationValueGroups []= MotivationValueGroup::find()->where(['alias' => 'week' . $ind])->one();
+        }
+        /** @var $motivationValueGroups MotivationValueGroup[] */
+
+        $motivationValueGroupForecast = MotivationValueGroup::find()->where(['alias' => 'forecast'])->one();
+        /** @var $motivationValueGroupForecast MotivationValueGroup */
+
+        $motivation = Motivation::find()->where(['store_id' => $store_id, 'year' => $year, 'month' => $month])->one();
+        foreach ($items as $code) {
+            if ($motivation) {
+                $motivationValue = MotivationValue::find()->where(['motivation_id' => $motivation->id,
+                    'motivation_group_id' => $motivationValueGroupForecast->id, 'value_id' => $code])->one();
+                $sum = 0;
+                $sum_type = MotivationCostsItem::DATA_TYPE_INT;
+                foreach (range(1, 5) as $ind) {
+                    $mv = MotivationValue::find()->where(['motivation_id' => $motivation->id,
+                        'motivation_group_id' => $motivationValueGroups[$ind-1]->id, 'value_id' => $code])->one();
+                    /** @var $mv MotivationValue */
+                    if ($mv) {
+                        switch ($mv->value_type) {
+                            case MotivationCostsItem::DATA_TYPE_INT: { $sum += $mv->value_int; break; }
+                            default: { $sum += $mv->value_float; $sum_type = MotivationCostsItem::DATA_TYPE_FLOAT; break; }
+                        };
+                    }
+                }
+                if (!$motivationValue) {
+                    $motivationValue = new MotivationValue;
+                    $motivationValue->motivation_id = $motivation->id;
+                    $motivationValue->motivation_group_id = $motivationValueGroupForecast->id;
+                    $motivationValue->value_id = $code;
+                    $motivationValue->value_type = $sum_type;
+                }
+                switch ($sum_type) {
+                    case MotivationCostsItem::DATA_TYPE_INT: { $motivationValue->value_int = $sum; break; }
+                    default: { $motivationValue->value_float = $sum; break; }
+                }
+                $motivationValue->save();
+                if ($motivationValue->getErrors()) {
+                    throw new \Exception(Json::encode($motivationValue->getErrors()));
+                }
+            }
+        }
+    }
 }