--- /dev/null
+<?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
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') ?>