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()
{
// 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,
}
}
}
+
+ 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()));
+ }
+ }
+ }
+ }
}