]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
added form for calculate weeks of custom mounth
authorJoySystem_v <fvv2011@gmail.com>
Wed, 7 Aug 2024 14:32:45 +0000 (17:32 +0300)
committerJoySystem_v <fvv2011@gmail.com>
Wed, 7 Aug 2024 14:32:45 +0000 (17:32 +0300)
erp24/actions/motivation/SumSalaryByMonthAction.php [new file with mode: 0644]
erp24/controllers/MotivationController.php
erp24/models/SumSalaryForm.php [new file with mode: 0644]
erp24/views/motivation/run-sum-salary.php

diff --git a/erp24/actions/motivation/SumSalaryByMonthAction.php b/erp24/actions/motivation/SumSalaryByMonthAction.php
new file mode 100644 (file)
index 0000000..4979500
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+
+namespace yii_app\actions\motivation;
+
+use Yii;
+use yii\base\Action;
+use DateTime;
+use yii_app\records\Motivation;
+use yii_app\records\MotivationValue;
+use yii_app\records\EmployeePayment;
+use yii_app\services\MotivationService;
+
+class SumSalaryByMonthAction extends Action
+{
+    public function run()
+    {
+        $request = Yii::$app->request;
+        $inputDate = $request->post('date'); // Получаем дату из формы
+        $selectedDate = new DateTime($inputDate);
+        $year = $selectedDate->format('Y');
+        $month = $selectedDate->format('m');
+
+        // Найти все записи по выбранному месяцу и году
+        $motivations = Motivation::find()
+            ->where(['year' => $year, 'month' => $month])
+            ->all();
+
+        if (empty($motivations)) {
+            Yii::warning("No motivation records found for the selected month and year.");
+            return $this->controller->redirect(['motivation/index']);
+        }
+
+        $weeks = [
+            ['start' => '01', 'end' => '07'],
+            ['start' => '08', 'end' => '14'],
+            ['start' => '15', 'end' => '21'],
+            ['start' => '22', 'end' => '28'],
+            ['start' => '29', 'end' => date('t', strtotime("$year-$month-01"))] // последняя неделя до конца месяца
+        ];
+
+        foreach ($weeks as $weekIndex => $week) {
+            $startDate = "$year-$month-" . $week['start'];
+            $endDate = "$year-$month-" . $week['end'];
+
+            $results = [];
+            foreach ($motivations as $motivation) {
+                $store_id = $motivation->store_id;
+
+                $records = MotivationService::getRecordsByDateAndStore($startDate, $endDate, $store_id);
+                $vacationSum = MotivationService::getVacationsSum($startDate, $endDate, $store_id);
+
+                $totalSalary = 0;
+                foreach ($records as $record) {
+                    $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]);
+                    $dailyPayment = $payment ? $payment->daily_payment : null;
+
+                    // Если $record->salary_shift имеет значение, учитываем его и не добавляем dailyPayment
+                    if (!empty($record->salary_shift)) {
+                        $totalSalary += $record->salary_shift;
+                    } else {
+                        $totalSalary += $dailyPayment;
+                    }
+                }
+
+                $results[$store_id] = $totalSalary + $vacationSum;
+            }
+
+            // Записываем информацию в таблицу MotivationValue за текущую неделю
+            foreach ($results as $store_id => $totalSalary) {
+                $motivation = Motivation::findOne(['store_id' => $store_id, 'year' => $year, 'month' => $month]);
+
+                // Если не найдено соответствующее мотивационное значение, пропустить этот store_id
+                if ($motivation === null) {
+                    continue;
+                }
+
+                // Проверка наличия записи в MotivationValue за текущую неделю
+                $motivationValue = MotivationValue::findOne([
+                    'motivation_id' => $motivation->id,
+                    'motivation_group_id' => $weekIndex + 1, // Номер недели
+                    'value_id' => 11
+                ]);
+
+                if ($motivationValue === null) {
+                    // Если записи нет, создаем новую
+                    $motivationValue = new MotivationValue();
+                    $motivationValue->motivation_id = $motivation->id;
+                    $motivationValue->motivation_group_id = $weekIndex + 1;
+                    $motivationValue->value_id = 11;
+                    $motivationValue->value_type = 'float';
+                }
+
+                // Обновляем значение
+                $motivationValue->value_float = $totalSalary;
+                $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 e56fe2402572fd98013213a43ccfea2bb4a810b4..df9a3d3fd46233dea01b1f78211bb1b977aee216 100644 (file)
@@ -19,12 +19,15 @@ class MotivationController extends Controller
             'get-salary' => \yii_app\actions\motivation\GetSalaryAction::class,
             'sum-salary' => \yii_app\actions\motivation\SumSalaryAction::class,
             'mounth-salary' => \yii_app\actions\motivation\SumPreviousMonthSalaryAction::class,
+            'sum-salary-by-month' => \yii_app\actions\motivation\SumSalaryByMonthAction::class,
         ];
     }
 
     public function actionRunSumSalary()
     {
-        return $this->render('run-sum-salary');
+        $model = new SumSalaryForm();
+        return $this->render('run-sum-salary', ['model' => $model]);
+
     }
 
     public function behaviors()
diff --git a/erp24/models/SumSalaryForm.php b/erp24/models/SumSalaryForm.php
new file mode 100644 (file)
index 0000000..686eb00
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+namespace app\models;
+
+use yii\base\Model;
+
+class SumSalaryForm extends Model
+{
+    public $date;
+
+    public function rules()
+    {
+        return [
+            [['date'], 'required'],
+            [['date'], 'date', 'format' => 'php:Y-m-d'],
+        ];
+    }
+}
\ No newline at end of file
index 373fd020f61eb0628c9b1a02cb428e1c986124f1..f07645cbe2e942c9e0167865f84c2759bd24956f 100644 (file)
@@ -2,13 +2,26 @@
 use yii\helpers\Html;
 
 /* @var $this yii\web\View */
-
+/* @var $model yii_app\models\SumSalaryForm */
 $this->title = 'Запуск экшена SumSalary';
 ?>
 
 <div class="sum-salary-action">
     <h1><?= Html::encode($this->title) ?></h1>
 
+    <p>Введите дату и нажмите кнопку ниже, чтобы запустить экшн SumSalaryByMonth и вычислить суммы зарплат за каждую неделю выбранного месяца.</p>
+
+    <?php $form = ActiveForm::begin([
+        'action' => ['motivation/sum-salary-by-month'],
+        'method' => 'post',
+    ]); ?>
+
+    <?= $form->field($model, 'date')->input('date') ?>
+
+    <?= Html::submitButton('Запустить', ['class' => 'btn btn-primary']) ?>
+
+    <?php ActiveForm::end(); ?>
+
     <p>Нажмите кнопку ниже, чтобы запустить экшн SumSalary и вычислить суммы зарплат за текущую неделю.</p>
 
     <?= Html::beginForm(['motivation/sum-salary'], 'post') ?>