From 5b5a9e307c077e6582dd2304760d591ca79bf097 Mon Sep 17 00:00:00 2001 From: JoySystem_v Date: Thu, 8 Aug 2024 12:27:51 +0300 Subject: [PATCH] fix vacation method --- erp24/actions/motivation/GetSalaryAction.php | 75 +++++++++++++++----- erp24/models/SumSalaryForm.php | 2 +- erp24/services/MotivationService.php | 39 +++++----- erp24/views/motivation/error.php | 25 +++++++ erp24/views/motivation/records.php | 75 ++++++++++++++++---- erp24/views/motivation/run-sum-salary.php | 22 +++++- 6 files changed, 184 insertions(+), 54 deletions(-) create mode 100644 erp24/views/motivation/error.php diff --git a/erp24/actions/motivation/GetSalaryAction.php b/erp24/actions/motivation/GetSalaryAction.php index 858c37ca..6395ffd9 100644 --- a/erp24/actions/motivation/GetSalaryAction.php +++ b/erp24/actions/motivation/GetSalaryAction.php @@ -9,6 +9,7 @@ use yii_app\services\MotivationService; use yii_app\records\TimetableFactModel; use DateTime; use yii_app\records\EmployeePayment; +use yii_app\records\Admin; class GetSalaryAction extends Action { @@ -16,19 +17,17 @@ class GetSalaryAction extends Action public function run() { $request = Yii::$app->request; - $store_id = $request->get('store_id'); - $month = $request->get('month'); - $year = $request->get('year'); + $store_id = $request->get('store_id', 1); + $month = $request->get('month', 7); + $year = $request->get('year', 2024); $records = []; $vacationSum = 0; - // Создаем и валидируем модель - $model = DynamicModel::validateData(compact('store_id', 'month', 'year'), [ - [['store_id', 'month', 'year'], 'required'], - ['store_id', 'integer'], - ['month', 'integer', 'min' => 1, 'max' => 12], - ['year', 'integer', 'min' => 1900, 'max' => 2100], - ]); + $model = new DynamicModel(compact('store_id', 'month', 'year')); + $model->addRule(['store_id', 'month', 'year'], 'required') + ->addRule('store_id', 'integer') + ->addRule('month', 'integer', ['min' => 1, 'max' => 12]) + ->addRule('year', 'integer', ['min' => 2000, 'max' => 2100]); if ($model->hasErrors()) { return $this->controller->render('error', [ @@ -37,12 +36,16 @@ class GetSalaryAction extends Action ]); } + $store_id = (int)$store_id; + $month = (int)$month; + $year = (int)$year; + $weeks = [ ['start' => 1, 'end' => 7], ['start' => 8, 'end' => 14], ['start' => 15, 'end' => 21], ['start' => 22, 'end' => 28], - ['start' => 29, 'end' => (new DateTime("$year-$month-01"))->format('t')] + ['start' => 29, 'end' => (new DateTime("$year-$month-01"))->format('t')], ]; $results = []; @@ -54,29 +57,63 @@ class GetSalaryAction extends Action $weeklyVacationSum = MotivationService::getVacationsSum($startDate, $endDate, $store_id); $weeklyTotalSalary = 0; + $recordsWithAdditionalData = []; foreach ($weeklyRecords as $record) { $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); $dailyPayment = $payment ? $payment->daily_payment : null; - if (!empty($record->salary_shift)) { - $weeklyTotalSalary += $record->salary_shift; - } else { - $weeklyTotalSalary += $dailyPayment; - } + $admin = Admin::findOne(['id' => $record->admin_id]); + $adminName = $admin ? $admin->name : 'Unknown'; + + $weeklyTotalSalary += !empty($record->salary_shift) ? $record->salary_shift : $dailyPayment; + + $recordsWithAdditionalData[] = [ + 'admin_id' => $record->admin_id, + 'admin_name' => $adminName, + 'salary_shift' => $record->salary_shift, + 'daily_payment' => $dailyPayment, + 'date_shift' => $record->date_shift + ]; } $results[$weekIndex + 1] = [ 'totalSalary' => $weeklyTotalSalary + $weeklyVacationSum, - 'records' => $weeklyRecords, + 'records' => $recordsWithAdditionalData, 'vacationSum' => $weeklyVacationSum, 'startDate' => $startDate, - 'endDate' => $endDate + 'endDate' => $endDate, ]; + + } + // Сортировка по дате начала недели + ksort($results); + + // Вычисление месячных данных + $startOfMonth = sprintf("%s-%02d-01", $year, $month); + $endOfMonth = (new DateTime("$year-$month-01"))->format('Y-m-t'); + + $monthlyTotalSalary = 0; + $monthlyVacationSum = 0; + + $monthlyRecords = MotivationService::getTimetableFactRecordsByDateAndStore($startOfMonth, $endOfMonth, $store_id); + $monthlyVacationSum = MotivationService::getVacationsSum($startOfMonth, $endOfMonth, $store_id); + + foreach ($monthlyRecords as $record) { + $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); + $dailyPayment = $payment ? $payment->daily_payment : null; + + $monthlyTotalSalary += !empty($record->salary_shift) ? $record->salary_shift : $dailyPayment; } - return $this->controller->render('weekly-salary', [ + // Подсчет зарплаты без отпускных + $monthlySalaryWithoutVacation = $monthlyTotalSalary - $monthlyVacationSum; + + return $this->controller->render('records', [ 'model' => $model, 'results' => $results, + 'monthlyTotalSalary' => $monthlyTotalSalary, + 'monthlyVacationSum' => $monthlyVacationSum, + 'monthlySalaryWithoutVacation' => $monthlySalaryWithoutVacation, ]); } } diff --git a/erp24/models/SumSalaryForm.php b/erp24/models/SumSalaryForm.php index 55ee9c9f..72954f2b 100644 --- a/erp24/models/SumSalaryForm.php +++ b/erp24/models/SumSalaryForm.php @@ -13,7 +13,7 @@ class SumSalaryForm extends Model public function rules() { return [ - [['month', 'year'], 'required'], + [['store_id','month', 'year'], 'required'], ['store_id', 'integer'], ['month', 'integer', 'min' => 1, 'max' => 12], ['year', 'integer', 'min' => 1900, 'max' => 2100], diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 57f1684c..1acf981e 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -10,10 +10,11 @@ use yii_app\records\MotivationValueGroup; use yii_app\records\CityStore; use yii_app\records\MotivationCostsItem; use yii_app\records\TimetableFactModel; +use yii_app\records\Timetable; use DateTime; use yii\helpers\ArrayHelper; -use yii_app\records\Timetable; + use yii_app\records\EmployeePayment; @@ -336,14 +337,12 @@ class MotivationService public static function getTimetableFactRecordsByDateAndStore($startDate, $endDate, $store_id) { - // Преобразуем начальную и конечную дату в объекты DateTime - $startDateTime = new DateTime($startDate); - $endDateTime = new DateTime($endDate); + // Делаем запрос к TimetableFactModel $records = TimetableFactModel::find() ->where(['store_id' => $store_id]) - ->andWhere(['between', 'date_shift', $startDateTime->format('Y-m-d'), $endDateTime->format('Y-m-d')]) + ->andWhere(['between', 'date_shift', $startDate, $endDate]) ->all(); return $records; @@ -351,31 +350,33 @@ class MotivationService public static function getVacationsSum($startDate, $endDate, $store_id) { - // Преобразуем начальную и конечную дату в объекты DateTime - $startDateTime = new DateTime($startDate); - $endDateTime = new DateTime($endDate); - // Делаем запрос к таблице Timetable для получения записей с slot_type_id = 2 $records = Timetable::find() + ->where(['store_id' => $store_id]) - ->andWhere(['between', 'date', $startDateTime->format('Y-m-d'), $endDateTime->format('Y-m-d')]) + ->andWhere(['between', 'date', $startDate, $endDate]) ->andWhere(['slot_type_id' => 2]) ->all(); - // Получаем массив admin_id - $adminIds = ArrayHelper::getColumn($records, 'admin_id'); + // Проверяем, есть ли записи + if (empty($records)) { + return 0; // Возвращаем 0, если записей нет + } + - // Делаем запрос к таблице EmployeePayment для получения daily_payment - $payments = EmployeePayment::find() - ->where(['admin_id' => $adminIds]) - ->all(); - // Вычисляем сумму отпускных выплат $vacationsSum = 0; - foreach ($payments as $payment) { - $vacationsSum += $payment->daily_payment; + foreach ($records as $record) { + $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); + $dailyPayment = $payment ? $payment->daily_payment : null; + + + $vacationsSum += $dailyPayment; + } + + return $vacationsSum; } diff --git a/erp24/views/motivation/error.php b/erp24/views/motivation/error.php new file mode 100644 index 00000000..ddd11220 --- /dev/null +++ b/erp24/views/motivation/error.php @@ -0,0 +1,25 @@ +title = 'Ошибка валидации данных'; +?> + +
+

title) ?>

+

Произошли следующие ошибки:

+ +
diff --git a/erp24/views/motivation/records.php b/erp24/views/motivation/records.php index 218775cb..1a82cd6d 100644 --- a/erp24/views/motivation/records.php +++ b/erp24/views/motivation/records.php @@ -2,27 +2,74 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; + /* @var $this yii\web\View */ /* @var $model yii_app\models\SumSalaryForm */ +/* @var $results array */ +/* @var $monthlyTotalSalary float */ +/* @var $monthlyVacationSum float */ +/* @var $monthlySalaryWithoutVacation float */ -$this->title = 'Получение зарплат по неделям'; +$this->title = 'Результаты расчета зарплат по неделям'; ?> -
+

title) ?>

- 'get', - 'action' => ['motivation/get-salary'], - ]); ?> +

Магазин: store_id) ?>, Месяц: month) ?>, Год: year) ?>

+ + + +

Итого за месяц

+

Общая зарплата с отпускными:

+

Сумма отпускных:

+

Сумма зарплат без отпускных:

- field($model, 'store_id')->textInput() ?> - field($model, 'month')->textInput() ?> - field($model, 'year')->textInput() ?> + $data): ?> +

Неделя (с по )

+

Общая зарплата с отпускными:

+

Общая зарплата без отпускных:

+

Сумма отпускных:

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

Записи:

+ + + + + + + + + + + + + + + + + + + + + +
Admin IDAdmin NameSalary ShiftDaily PaymentDate Shift
+ +
- -
\ 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 b6bf6d26..554db402 100644 --- a/erp24/views/motivation/run-sum-salary.php +++ b/erp24/views/motivation/run-sum-salary.php @@ -13,7 +13,7 @@ $this->title = 'Запуск экшена SumSalary';

Введите номер месяца и год для расчета зарплат.

['motivation/sum-salary-by-month'], 'method' => 'post']); ?> - + field($model, 'store_id')->input('number', ['min' => 1, 'max' => 12]) ?> field($model, 'month')->input('number', ['min' => 1, 'max' => 12]) ?> field($model, 'year')->input('number', ['min' => 2000, 'max' => 2100]) ?> @@ -33,4 +33,24 @@ $this->title = 'Запуск экшена SumSalary'; 'btn btn-primary']) ?> + + -- 2.39.5