From 88be16559f033d038393f6c55a25a6d6d75f454a Mon Sep 17 00:00:00 2001 From: JoySystem_v Date: Thu, 8 Aug 2024 16:04:48 +0300 Subject: [PATCH] fix daily_paiment query - get latest record from EmployeePayment --- erp24/actions/motivation/GetSalaryAction.php | 18 +++++++++----- erp24/actions/motivation/SumSalaryAction.php | 9 ++++--- erp24/services/MotivationService.php | 26 +++++++++++++------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/erp24/actions/motivation/GetSalaryAction.php b/erp24/actions/motivation/GetSalaryAction.php index 5fa26fda..126279d6 100644 --- a/erp24/actions/motivation/GetSalaryAction.php +++ b/erp24/actions/motivation/GetSalaryAction.php @@ -24,7 +24,7 @@ class GetSalaryAction extends Action $year = $model->year; $month = $model->month; $records = []; - $vacationSum = 0; + $model = new DynamicModel(compact('store_id', 'month', 'year')); $model->addRule(['store_id', 'month', 'year'], 'required') @@ -59,10 +59,13 @@ class GetSalaryAction extends Action $weeklyRecords = MotivationService::getTimetableFactRecordsByDateAndStore($startDate, $endDate, $store_id); $weeklyVacationSum = MotivationService::getVacationsSum($startDate, $endDate, $store_id); - $weeklyTotalSalary = 0; + $weeklyTotalSalary = 0.0; $recordsWithAdditionalData = []; foreach ($weeklyRecords as $record) { - $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); + $payment = EmployeePayment::find() + ->where(['admin_id' => $record->admin_id]) + ->orderBy(['date' => SORT_DESC]) // Сортировка по полю date от самой поздней записи к самой ранней + ->one(); $dailyPayment = $payment ? $payment->daily_payment : null; $admin = Admin::findOne(['id' => $record->admin_id]); @@ -95,14 +98,17 @@ class GetSalaryAction extends Action $startOfMonth = sprintf("%s-%02d-01", $year, $month); $endOfMonth = (new DateTime("$year-$month-01"))->format('Y-m-t'); - $monthlyTotalSalary = 0; - $monthlyVacationSum = 0; + $monthlyTotalSalary = 0.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]); + $payment = EmployeePayment::find() + ->where(['admin_id' => $record->admin_id]) + ->orderBy(['date' => SORT_DESC]) // Сортировка по полю date от самой поздней записи к самой ранней + ->one(); $dailyPayment = $payment ? $payment->daily_payment : null; $monthlyTotalSalary += !empty($record->salary_shift) ? $record->salary_shift : $dailyPayment; diff --git a/erp24/actions/motivation/SumSalaryAction.php b/erp24/actions/motivation/SumSalaryAction.php index 60053002..f13af742 100644 --- a/erp24/actions/motivation/SumSalaryAction.php +++ b/erp24/actions/motivation/SumSalaryAction.php @@ -44,10 +44,13 @@ class SumSalaryAction extends \yii\base\Action $records = MotivationService::getTimetableFactRecordsByDateAndStore($startDate, $endDate, $store_id); $vacationSum = MotivationService::getVacationsSum($startDate, $endDate, $store_id); - $totalSalary = 0; + $totalSalary = 0.0; foreach ($records as $record) { - $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); - $dailyPayment = $payment ? $payment->daily_payment : null; + $payment = EmployeePayment::find() + ->where(['admin_id' => $record->admin_id]) + ->orderBy(['date' => SORT_DESC]) // Сортировка по полю date от самой поздней записи к самой ранней + ->one(); + $dailyPayment = $payment ? $payment->daily_payment : 0.0; // Если $record->salary_shift имеет значение, учитываем его и не добавляем dailyPayment if (!empty($record->salary_shift)) { diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 1acf981e..e1ae70c1 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -348,7 +348,7 @@ class MotivationService return $records; } - public static function getVacationsSum($startDate, $endDate, $store_id) + public static function getVacationsSum($startDate, $endDate, $store_id):float { // Делаем запрос к таблице Timetable для получения записей с slot_type_id = 2 $records = Timetable::find() @@ -360,15 +360,19 @@ class MotivationService // Проверяем, есть ли записи if (empty($records)) { - return 0; // Возвращаем 0, если записей нет + return 0.0; // Возвращаем 0, если записей нет } - $vacationsSum = 0; + $vacationsSum = 0.0; foreach ($records as $record) { - $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); - $dailyPayment = $payment ? $payment->daily_payment : null; + // Находим самую позднюю запись по admin_id + $payment = EmployeePayment::find() + ->where(['admin_id' => $record->admin_id]) + ->orderBy(['date' => SORT_DESC]) + ->one(); + $dailyPayment = $payment ? $payment->daily_payment : 0.0; $vacationsSum += $dailyPayment; @@ -480,15 +484,19 @@ class MotivationService } - public static function calculateTotalSalary($startDate, $endDate, $storeId) + public static function calculateTotalSalary($startDate, $endDate, $storeId):float { $records = self::getTimetableFactRecordsByDateAndStore($startDate, $endDate, $storeId); $vacationSum = self::getVacationsSum($startDate, $endDate, $storeId); - $totalSalary = 0; + $totalSalary = 0.0; foreach ($records as $record) { - $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); - $dailyPayment = $payment ? $payment->daily_payment : null; + // Находим самую позднюю запись по admin_id + $payment = EmployeePayment::find() + ->where(['admin_id' => $record->admin_id]) + ->orderBy(['date' => SORT_DESC]) + ->one(); + $dailyPayment = $payment ? $payment->daily_payment : 0.0; if (!empty($record->salary_shift)) { $totalSalary += $record->salary_shift; -- 2.39.5