From: vladfo Date: Tue, 6 Aug 2024 10:57:46 +0000 (+0300) Subject: change week start counting X-Git-Tag: 1.4~35^2~20 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=8f8bedc86bf498b687bd345f254c09e396409352;p=erp24_rep%2Fyii-erp24%2F.git change week start counting --- diff --git a/erp24/actions/motivation/GetSalaryAction.php b/erp24/actions/motivation/GetSalaryAction.php index 8ebecc42..4c238a88 100644 --- a/erp24/actions/motivation/GetSalaryAction.php +++ b/erp24/actions/motivation/GetSalaryAction.php @@ -17,7 +17,7 @@ public function run() { $request = Yii::$app->request; $store_id = $request->get('store_id', 1); - $date = $request->get('date', '2024-07-06'); + $date = $request->get('date', '2024-08-05'); $records = []; // Создаем и валидируем модель diff --git a/erp24/actions/motivation/SumSalaryAction.php b/erp24/actions/motivation/SumSalaryAction.php index 9eb16fe4..3cef88ff 100644 --- a/erp24/actions/motivation/SumSalaryAction.php +++ b/erp24/actions/motivation/SumSalaryAction.php @@ -1,12 +1,15 @@ admin_id); + $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); $dailyPayment = $payment ? $payment->daily_payment : null; @@ -47,24 +50,46 @@ class SumSalaryAction extends \yii\base\Action } // Определяем начало недели и номер недели в месяце - $startOfWeek = $currentDate->modify('monday this week')->format('Y-m-d'); - $endOfWeek = $currentDate->format('Y-m-d'); + $weekNumberInMonth = ceil($currentDate->format('j') / 7); + // Записываем информацию в таблицу MotivationValue foreach ($results as $store_id => $totalSalary) { $motivation = Motivation::findOne(['store_id' => $store_id, 'year' => $currentYear, 'month' => $currentMonth]); - $motivationGroup = MotivationValueGroup::findOne(['week_number' => $weekNumberInMonth]); - $motivationValue = new MotivationValue(); - $motivationValue->motivation_id = $motivation->id; - $motivationValue->motivation_group_id = $motivationGroup->id; - $motivationValue->value_id = 11; - $motivationValue->value_type = 'float'; + // Если не найдено соответствующее мотивационное значение, пропустить этот store_id + if ($motivation === null) { + continue; + } + + // Проверка наличия записи в MotivationValue + $motivationValue = MotivationValue::findOne([ + 'motivation_id' => $motivation->id, + 'motivation_group_id' => $weekNumberInMonth, + 'value_id' => 11 + ]); + + if ($motivationValue === null) { + // Если записи нет, создаем новую + $motivationValue = new MotivationValue(); + $motivationValue->motivation_id = $motivation->id; + $motivationValue->motivation_group_id = $weekNumberInMonth; + $motivationValue->value_id = 11; + $motivationValue->value_type = 'float'; + } + + // Обновляем значение $motivationValue->value_float = $totalSalary; - $motivationValue->save(); + $motivationValue->value_int = null; + $motivationValue->value_string = null; + + // Сохраняем запись + if (!$motivationValue->save()) { + Yii::error("Не удалось записать данные для store_id: $store_id"); + } } return $this->controller->redirect(['motivation/index']); } -} \ No newline at end of file +} diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 583850f0..c897569f 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -332,38 +332,57 @@ class MotivationService { // Преобразуем дату в объект DateTime $dateTime = new DateTime($date); - - // Получаем номер недели в месяце + + // Получаем номер недели в месяце и начало недели $weekOfMonth = self::getWeekOfMonth($dateTime); - - // Определяем начало текущей недели (понедельник) - $startOfWeek = clone $dateTime; - $startOfWeek->modify(('Monday' == $startOfWeek->format('l')) ? 'this Monday' : 'last Monday'); - - // Определяем конец текущей недели (воскресенье) - $endOfWeek = clone $startOfWeek; - $endOfWeek->modify('next Sunday'); - + $startOfWeek = self::getStartOfWeek($dateTime, $weekOfMonth); + + // Определяем конец текущей недели (до текущей даты) + $endOfWeek = clone $dateTime; + // Делаем запрос к TimetableFactModel $records = TimetableFactModel::find() ->where(['store_id' => $store_id]) - ->andWhere(['between', 'date_shift', $startOfWeek->format('Y-m-d'), $dateTime->format('Y-m-d')]) + ->andWhere(['between', 'date_shift', $startOfWeek->format('Y-m-d'), $endOfWeek->format('Y-m-d')]) ->all(); - + return $records; } - + private static function getWeekOfMonth($dateTime) { - $firstDayOfMonth = new DateTime($dateTime->format('Y-m-01')); - $weekOfYear = intval($dateTime->format('W')); - $firstWeekOfYear = intval($firstDayOfMonth->format('W')); - - // Если первая неделя года - это конец предыдущего года - if ($firstWeekOfYear == 1 && $firstDayOfMonth->format('m') == '12') { - $firstWeekOfYear = intval((new DateTime($dateTime->format('Y-01-01')))->format('W')); + $dayOfMonth = intval($dateTime->format('j')); + if ($dayOfMonth <= 7) { + return 1; + } elseif ($dayOfMonth <= 14) { + return 2; + } elseif ($dayOfMonth <= 21) { + return 3; + } elseif ($dayOfMonth <= 28) { + return 4; + } else { + return 5; + } + } + + private static function getStartOfWeek($dateTime, $weekOfMonth) + { + $year = $dateTime->format('Y'); + $month = $dateTime->format('m'); + + switch ($weekOfMonth) { + case 1: + return new DateTime("$year-$month-01"); + case 2: + return new DateTime("$year-$month-08"); + case 3: + return new DateTime("$year-$month-15"); + case 4: + return new DateTime("$year-$month-22"); + case 5: + return new DateTime("$year-$month-29"); + default: + return new DateTime("$year-$month-01"); } - - return $weekOfYear - $firstWeekOfYear + 1; } }