{
$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 = [];
// Создаем и валидируем модель
<?php
namespace yii_app\actions\motivation;
+
use Yii;
use yii\base\Action;
use yii\db\Expression;
use yii_app\records\Motivation;
use yii_app\records\MotivationValue;
use yii_app\records\MotivationValueGroup;
+use yii_app\records\Admin;
+use yii_app\records\EmployeePayment;
use yii_app\services\MotivationService;
use DateTime;
$totalSalary = 0;
foreach ($records as $record) {
- $admin = Admin::findOne($record->admin_id);
+
$payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]);
$dailyPayment = $payment ? $payment->daily_payment : null;
}
// Определяем начало недели и номер недели в месяце
- $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
+}
{
// Преобразуем дату в объект 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;
}
}