From 094fb63f9c0dd2e58f848a7dbf4858570d1af546 Mon Sep 17 00:00:00 2001 From: JoySystem_v Date: Tue, 6 Aug 2024 10:19:55 +0300 Subject: [PATCH] add sum salary action --- erp24/actions/motivation/GetSalaryAction.php | 48 +++++++++++ erp24/actions/motivation/SumSalaryAction.php | 70 +++++++++++++++ erp24/controllers/MotivationController.php | 7 ++ erp24/services/MotivationService.php | 43 +++++++++- erp24/views/motivation/records.php | 90 ++++++++++++++++++++ erp24/views/motivation/run-sum-salary.php | 17 ++++ 6 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 erp24/actions/motivation/GetSalaryAction.php create mode 100644 erp24/actions/motivation/SumSalaryAction.php create mode 100644 erp24/views/motivation/records.php create mode 100644 erp24/views/motivation/run-sum-salary.php diff --git a/erp24/actions/motivation/GetSalaryAction.php b/erp24/actions/motivation/GetSalaryAction.php new file mode 100644 index 00000000..8ebecc42 --- /dev/null +++ b/erp24/actions/motivation/GetSalaryAction.php @@ -0,0 +1,48 @@ +request; + $store_id = $request->get('store_id', 1); + $date = $request->get('date', '2024-07-06'); + $records = []; + + // Создаем и валидируем модель + $model = DynamicModel::validateData(compact('store_id', 'date'), [ + [['store_id', 'date'], 'required'], + ['store_id', 'integer'], + ['date', 'date', 'format' => 'php:Y-m-d'], + ]); + + if (!$model->hasErrors()) { + $records = MotivationService::getRecordsByDateAndStore($date, $store_id); + } +// Определяем начало недели и номер недели в месяце + $currentDate = new DateTime($model->date); + $startOfWeek = $currentDate->modify('monday this week')->format('Y-m-d'); + $endOfWeek = $currentDate->format('Y-m-d'); + + $weekNumberInMonth = ceil($currentDate->format('j') / 7); + + return $this->controller->render('records', [ + 'model' => $model, + 'records' => $records, + 'startOfWeek' => $startOfWeek, + 'endOfWeek' => $endOfWeek, + 'weekNumberInMonth' => $weekNumberInMonth, + ]); +} +} diff --git a/erp24/actions/motivation/SumSalaryAction.php b/erp24/actions/motivation/SumSalaryAction.php new file mode 100644 index 00000000..9eb16fe4 --- /dev/null +++ b/erp24/actions/motivation/SumSalaryAction.php @@ -0,0 +1,70 @@ +format('Y'); + $currentMonth = $currentDate->format('m'); + + // Найти все записи по текущему месяцу и году + $motivations = Motivation::find() + ->where(['year' => $currentYear, 'month' => $currentMonth]) + ->all(); + + $results = []; + foreach ($motivations as $motivation) { + $store_id = $motivation->store_id; + $date = $currentDate->format('Y-m-d'); + $records = MotivationService::getRecordsByDateAndStore($date, $store_id); + + $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; + + // Если $record->salary_shift имеет значение, учитываем его и не добавляем dailyPayment + if (!empty($record->salary_shift)) { + $totalSalary += $record->salary_shift; + } else { + $totalSalary += $dailyPayment; + } + } + + $results[$store_id] = $totalSalary; + } + + // Определяем начало недели и номер недели в месяце + $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'; + $motivationValue->value_float = $totalSalary; + $motivationValue->save(); + } + + 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 f81c287b..412b0905 100644 --- a/erp24/controllers/MotivationController.php +++ b/erp24/controllers/MotivationController.php @@ -16,9 +16,16 @@ class MotivationController extends Controller 'create-value' => \yii_app\actions\motivation\CreateValueAction::class, 'update-value' => \yii_app\actions\motivation\UpdateValueAction::class, 'delete-value' => \yii_app\actions\motivation\DeleteValueAction::class, + 'get-salary' => \yii_app\actions\motivation\GetSalaryAction::class, + 'sum-salary' => \yii_app\actions\motivation\SumSalaryAction::class, ]; } + public function actionRunSumSalary() + { + return $this->render('run-sum-salary'); + } + public function behaviors() { return [ diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 51ab681c..583850f0 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -8,7 +8,8 @@ use yii_app\records\MotivationValue; use yii_app\records\MotivationValueGroup; use yii_app\records\CityStore; use yii_app\records\MotivationCostsItem; - +use yii_app\records\TimetableFactModel; +use DateTime; class MotivationService { @@ -325,4 +326,44 @@ class MotivationService return compact('errors'); } + + + public static function getRecordsByDateAndStore($date, $store_id) + { + // Преобразуем дату в объект 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'); + + // Делаем запрос к TimetableFactModel + $records = TimetableFactModel::find() + ->where(['store_id' => $store_id]) + ->andWhere(['between', 'date_shift', $startOfWeek->format('Y-m-d'), $dateTime->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')); + } + + return $weekOfYear - $firstWeekOfYear + 1; + } } diff --git a/erp24/views/motivation/records.php b/erp24/views/motivation/records.php new file mode 100644 index 00000000..54416c16 --- /dev/null +++ b/erp24/views/motivation/records.php @@ -0,0 +1,90 @@ +title = 'Записи'; +?> + +

title) ?>

+ +
+ 'get', + 'action' => ['motivation/get-salary'], + ]); ?> + + field($model, 'store_id')->textInput(['value' => $model->store_id]) ?> + field($model, 'date')->input('date', ['value' => $model->date]) ?> + +
+ 'btn btn-primary']) ?> +
+ + +
+ + + date))->format("W"); + + foreach ($records as $record) { + $admin = Admin::findOne($record->admin_id); + $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; + } + } + ?> + +

Смены

+ + + + + + + + + + + + + + + admin_id); + $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); + $dailyPayment = $payment ? $payment->daily_payment : null; + ?> + + + + + + + + + + + +
Store IDAdmin IDDate ShiftSalary ShiftNameGroupЗП за смену
store_id) ?>admin_id) ?>date_shift) ?>salary_shift) ?>name) ?>group_name) ?>
+ +

Всего смен:

+

ЗП за весь период:

+

Период: с по

+

Номер недели в месяце:

+ diff --git a/erp24/views/motivation/run-sum-salary.php b/erp24/views/motivation/run-sum-salary.php new file mode 100644 index 00000000..9ef37c43 --- /dev/null +++ b/erp24/views/motivation/run-sum-salary.php @@ -0,0 +1,17 @@ +title = 'Запуск экшена SumSalary'; +?> + +
+

title) ?>

+ +

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

+ + + 'btn btn-primary']) ?> + +
-- 2.39.5