]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
change week start counting
authorvladfo <fvv2011@gmail.com>
Tue, 6 Aug 2024 10:57:46 +0000 (13:57 +0300)
committervladfo <fvv2011@gmail.com>
Tue, 6 Aug 2024 10:57:46 +0000 (13:57 +0300)
erp24/actions/motivation/GetSalaryAction.php
erp24/actions/motivation/SumSalaryAction.php
erp24/services/MotivationService.php

index 8ebecc4267e29b8aab45a737a9e13e30e87250fb..4c238a882da71fca219a41ac17206f4996b81fcd 100644 (file)
@@ -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 = [];
 
     // Создаем и валидируем модель
index 9eb16fe42fa0306d8b00d57ddb1f8dc3b06f21ca..3cef88ffc16daedc9c03e092cbb56c0ff3920d24 100644 (file)
@@ -1,12 +1,15 @@
 <?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;
 
@@ -31,7 +34,7 @@ class SumSalaryAction extends \yii\base\Action
 
             $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;
 
@@ -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
+}
index 583850f0db3b429069d946868ceefa1c32e66871..c897569fab5ff033f4b0149f961a2ed9127dcc4a 100644 (file)
@@ -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;
     }
 }