From 221a8d9e77c4d959e39ad54838c22cd97889224f Mon Sep 17 00:00:00 2001 From: JoySystem_v Date: Wed, 7 Aug 2024 17:32:45 +0300 Subject: [PATCH] added form for calculate weeks of custom mounth --- .../motivation/SumSalaryByMonthAction.php | 107 ++++++++++++++++++ erp24/controllers/MotivationController.php | 5 +- erp24/models/SumSalaryForm.php | 18 +++ erp24/views/motivation/run-sum-salary.php | 15 ++- 4 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 erp24/actions/motivation/SumSalaryByMonthAction.php create mode 100644 erp24/models/SumSalaryForm.php diff --git a/erp24/actions/motivation/SumSalaryByMonthAction.php b/erp24/actions/motivation/SumSalaryByMonthAction.php new file mode 100644 index 00000000..4979500e --- /dev/null +++ b/erp24/actions/motivation/SumSalaryByMonthAction.php @@ -0,0 +1,107 @@ +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 diff --git a/erp24/controllers/MotivationController.php b/erp24/controllers/MotivationController.php index e56fe240..df9a3d3f 100644 --- a/erp24/controllers/MotivationController.php +++ b/erp24/controllers/MotivationController.php @@ -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 index 00000000..686eb002 --- /dev/null +++ b/erp24/models/SumSalaryForm.php @@ -0,0 +1,18 @@ + 'php:Y-m-d'], + ]; + } +} \ No newline at end of file diff --git a/erp24/views/motivation/run-sum-salary.php b/erp24/views/motivation/run-sum-salary.php index 373fd020..f07645cb 100644 --- a/erp24/views/motivation/run-sum-salary.php +++ b/erp24/views/motivation/run-sum-salary.php @@ -2,13 +2,26 @@ use yii\helpers\Html; /* @var $this yii\web\View */ - +/* @var $model yii_app\models\SumSalaryForm */ $this->title = 'Запуск экшена SumSalary'; ?>

title) ?>

+

Введите дату и нажмите кнопку ниже, чтобы запустить экшн SumSalaryByMonth и вычислить суммы зарплат за каждую неделю выбранного месяца.

+ + ['motivation/sum-salary-by-month'], + 'method' => 'post', + ]); ?> + + field($model, 'date')->input('date') ?> + + 'btn btn-primary']) ?> + + +

Нажмите кнопку ниже, чтобы запустить экшн SumSalary и вычислить суммы зарплат за текущую неделю.

-- 2.39.5