From: Alexander Smirnov Date: Fri, 9 Aug 2024 12:09:17 +0000 (+0300) Subject: [ERP-137] Прогноз за месяц для мотивации X-Git-Tag: 1.4~36^2~15 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=a150a35571ab36fb74d6a1858e8d48cc1b4e6904;p=erp24_rep%2Fyii-erp24%2F.git [ERP-137] Прогноз за месяц для мотивации --- diff --git a/erp24/actions/motivation/IndexAction.php b/erp24/actions/motivation/IndexAction.php index 5d800c6e..86bb32e7 100644 --- a/erp24/actions/motivation/IndexAction.php +++ b/erp24/actions/motivation/IndexAction.php @@ -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 index 00000000..9498eb1b --- /dev/null +++ b/erp24/migrations/m240809_115844_insert_forecast_into_motivation_value_group.php @@ -0,0 +1,30 @@ +insert(self::TABLE_NAME, [ + 'id' => 9, + 'name' => 'Прогноз', + 'alias' => 'forecast' + ]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->delete(self::TABLE_NAME, ['alias' => 'forecast']); + } +} diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 0ada3d5e..6c906001 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -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())); + } + } + } + } }