--- /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 SumPreviousMonthSalaryAction extends Action
+{
+ public function run()
+ {
+ $currentDate = new DateTime();
+ $previousMonthDate = (clone $currentDate)->modify('first day of last month');
+ $year = $previousMonthDate->format('Y');
+ $mounth = $previousMonthDate->format('m');
+
+ // Определяем даты начала и конца предыдущего месяца
+ $startOfPreviousMonth = new DateTime("$year-$mounth-01");
+ $endOfPreviousMonth = new DateTime($startOfPreviousMonth->format('Y-m-t'));
+
+ // Найти все записи по предыдущему месяцу и году
+ $motivations = Motivation::find()
+ ->where(['year' => $year, 'month' => $mounth])
+ ->all();
+
+ if (empty($motivations)) {
+ Yii::warning("Записи для текущего месяца и года таблицы мотивации не найдены.");
+ return $this->controller->redirect(['motivation/index']);
+ }
+
+ foreach ($motivations as $motivation) {
+ $store_id = $motivation->store_id;
+
+ $monthlyRecords = MotivationService::getTimetableFactRecordsByDateAndStore($startOfPreviousMonth->format('Y-m-d'), $endOfPreviousMonth->format('Y-m-d'), $store_id);
+ $monthlyVacationSum = MotivationService::getVacationsSum($startOfPreviousMonth->format('Y-m-d'), $endOfPreviousMonth->format('Y-m-d'), $store_id);
+
+ $monthlyTotalSalary = 0;
+ foreach ($monthlyRecords 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)) {
+ $monthlyTotalSalary += $record->salary_shift;
+ } else {
+ $monthlyTotalSalary += $dailyPayment;
+ }
+ }
+
+ $monthlyTotalSalary += $monthlyVacationSum;
+ //TODO Проверка записи
+ $idMotivation = MotivationService::saveOrUpdateMotivationValue($motivation->id, 6, 11, "float", $monthlyTotalSalary);
+ }
+
+ return $this->controller->redirect(['motivation/index']);
+ }
+}
->all();
if (empty($motivations)) {
- Yii::warning("No motivation records found for the current month and year.");
+ Yii::warning("Записи для текущего месяца и года таблицы мотивации не найдены.");
return $this->controller->redirect(['motivation/index']);
}
}
}
- // Определяем даты начала и конца текущего месяца
- $startOfMonth = new DateTime("$currentYear-$currentMonth-01");
- $endOfMonth = new DateTime($startOfMonth->format('Y-m-t'));
-
- // Записываем информацию в таблицу MotivationValue за текущий месяц
- foreach ($motivations as $motivation) {
- $store_id = $motivation->store_id;
-
- $monthlyRecords = MotivationService::getTimetableFactRecordsByDateAndStore($startOfMonth->format('Y-m-d'), $endOfMonth->format('Y-m-d'), $store_id);
- $monthlyVacationSum = MotivationService::getVacationsSum($startOfMonth->format('Y-m-d'), $endOfMonth->format('Y-m-d'), $store_id);
-
- $monthlyTotalSalary = 0;
- foreach ($monthlyRecords 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)) {
- $monthlyTotalSalary += $record->salary_shift;
- } else {
- $monthlyTotalSalary += $dailyPayment;
- }
- }
-
- $monthlyTotalSalary += $monthlyVacationSum;
-
- // Проверка наличия записи в MotivationValue за текущий месяц
- $motivationValue = MotivationValue::findOne([
- 'motivation_id' => $motivation->id,
- 'motivation_group_id' => 6, // 6 для обозначения всего месяца
- 'value_id' => 11
- ]);
-
- if ($motivationValue === null) {
- // Если записи нет, создаем новую
- $motivationValue = new MotivationValue();
- $motivationValue->motivation_id = $motivation->id;
- $motivationValue->motivation_group_id = 6;
- $motivationValue->value_id = 11;
- $motivationValue->value_type = 'float';
- }
-
- // Обновляем значение
- $motivationValue->value_float = $monthlyTotalSalary;
- $motivationValue->value_int = null;
- $motivationValue->value_string = null;
-
- // Сохраняем запись
- if (!$motivationValue->save()) {
- Yii::error("Не удалось записать данные для store_id: $store_id");
- }
- }
-
return $this->controller->redirect(['motivation/index']);
}
}
'delete-value' => \yii_app\actions\motivation\DeleteValueAction::class,
'get-salary' => \yii_app\actions\motivation\GetSalaryAction::class,
'sum-salary' => \yii_app\actions\motivation\SumSalaryAction::class,
+ 'mounth-salary' => \yii_app\actions\motivation\SumPreviousMonthSalaryAction::class,
];
}
namespace yii_app\services;
+use Yii;
use PhpOffice\PhpSpreadsheet\IOFactory;
use yii_app\records\Motivation;
use yii_app\records\MotivationValue;
$motivationValue->value_string = $value;
break;
default:
- throw new \InvalidArgumentException("Invalid value type: $value_type");
+ throw new \InvalidArgumentException("Неправильное значение типа: $value_type");
}
// Сохранить запись и вернуть id или false
if ($motivationValue->save()) {
return $motivationValue->id;
} else {
- Yii::error("Failed to save MotivationValue: " . json_encode($motivationValue->errors));
+ Yii::error("Не удалось сохранить значение: " . json_encode($motivationValue->errors));
return false;
}
}
<div class="sum-salary-action">
<h1><?= Html::encode($this->title) ?></h1>
- <p>Нажмите кнопку ниже, чтобы запустить экшн SumSalary и вычислить суммы зарплат.</p>
+ <p>Нажмите кнопку ниже, чтобы запустить экшн SumSalary и вычислить суммы зарплат за текущую неделю.</p>
<?= Html::beginForm(['motivation/sum-salary'], 'post') ?>
- <?= Html::submitButton('Запустить SumSalary', ['class' => 'btn btn-primary']) ?>
+ <?= Html::submitButton('Запустить', ['class' => 'btn btn-primary']) ?>
<?= Html::endForm() ?>
+
+ <p>Нажмите кнопку ниже, чтобы запустить экшн SumPreviousMonthSalary и вычислить суммы зарплат за предыдущий месяц.</p>
+
+ <?= Html::beginForm(['motivation/mounth-salary'], 'post') ?>
+ <?= Html::submitButton('Запустить', ['class' => 'btn btn-primary']) ?>
+ <?= Html::endForm() ?>
+
</div>