From: JoySystem_v Date: Thu, 8 Aug 2024 04:16:13 +0000 (+0300) Subject: change weekly X-Git-Tag: 1.4~35^2~13 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=0dbcb4ca9e51fcfc51f34b800b50922d42a3b2f1;p=erp24_rep%2Fyii-erp24%2F.git change weekly --- diff --git a/erp24/actions/motivation/GetSalaryAction.php b/erp24/actions/motivation/GetSalaryAction.php index e03c43b7..858c37ca 100644 --- a/erp24/actions/motivation/GetSalaryAction.php +++ b/erp24/actions/motivation/GetSalaryAction.php @@ -8,43 +8,75 @@ use yii\base\DynamicModel; use yii_app\services\MotivationService; use yii_app\records\TimetableFactModel; use DateTime; - +use yii_app\records\EmployeePayment; class GetSalaryAction extends Action { -public function run() -{ - $request = Yii::$app->request; - $store_id = $request->get('store_id', 2); - $date = $request->get('date', '2024-08-05'); - $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::getTimetableFactRecordsByDateAndStore($date, $store_id); - $vacationSum = MotivationService::getVacationsSum($date, $store_id); + public function run() + { + $request = Yii::$app->request; + $store_id = $request->get('store_id'); + $month = $request->get('month'); + $year = $request->get('year'); + $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], + ]); + + if ($model->hasErrors()) { + return $this->controller->render('error', [ + 'model' => $model, + 'errors' => $model->errors, + ]); + } + + $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')] + ]; + + $results = []; + foreach ($weeks as $weekIndex => $week) { + $startDate = sprintf("%s-%02d-%02d", $year, $month, $week['start']); + $endDate = sprintf("%s-%02d-%02d", $year, $month, $week['end']); + + $weeklyRecords = MotivationService::getTimetableFactRecordsByDateAndStore($startDate, $endDate, $store_id); + $weeklyVacationSum = MotivationService::getVacationsSum($startDate, $endDate, $store_id); + + $weeklyTotalSalary = 0; + 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; + } + } + + $results[$weekIndex + 1] = [ + 'totalSalary' => $weeklyTotalSalary + $weeklyVacationSum, + 'records' => $weeklyRecords, + 'vacationSum' => $weeklyVacationSum, + 'startDate' => $startDate, + 'endDate' => $endDate + ]; + } + + return $this->controller->render('weekly-salary', [ + 'model' => $model, + 'results' => $results, + ]); } -// Определяем начало недели и номер недели в месяце - $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, - 'vacation'=> $vacationSum, - ]); -} } diff --git a/erp24/actions/motivation/SumPreviousMonthSalaryAction.php b/erp24/actions/motivation/SumPreviousMonthSalaryAction.php index 7225f69e..1ac1fa5c 100644 --- a/erp24/actions/motivation/SumPreviousMonthSalaryAction.php +++ b/erp24/actions/motivation/SumPreviousMonthSalaryAction.php @@ -16,15 +16,13 @@ class SumPreviousMonthSalaryAction extends Action $currentDate = new DateTime(); $previousMonthDate = (clone $currentDate)->modify('first day of last month'); $year = $previousMonthDate->format('Y'); - $mounth = $previousMonthDate->format('m'); + $month = $previousMonthDate->format('m'); - // Определяем даты начала и конца предыдущего месяца - $startOfPreviousMonth = new DateTime("$year-$mounth-01"); + $startOfPreviousMonth = new DateTime("$year-$month-01"); $endOfPreviousMonth = new DateTime($startOfPreviousMonth->format('Y-m-t')); - // Найти все записи по предыдущему месяцу и году $motivations = Motivation::find() - ->where(['year' => $year, 'month' => $mounth]) + ->where(['year' => $year, 'month' => $month]) ->all(); if (empty($motivations)) { @@ -34,25 +32,8 @@ class SumPreviousMonthSalaryAction extends Action foreach ($motivations as $motivation) { $store_id = $motivation->store_id; + $monthlyTotalSalary = MotivationService::calculateTotalSalary($startOfPreviousMonth->format('Y-m-d'), $endOfPreviousMonth->format('Y-m-d'), $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); } diff --git a/erp24/actions/motivation/SumSalaryByMonthAction.php b/erp24/actions/motivation/SumSalaryByMonthAction.php index 8936ffaa..a83c58f3 100644 --- a/erp24/actions/motivation/SumSalaryByMonthAction.php +++ b/erp24/actions/motivation/SumSalaryByMonthAction.php @@ -5,6 +5,7 @@ namespace yii_app\actions\motivation; use Yii; use yii\base\Action; use DateTime; +use yii_app\models\SumSalaryForm; use yii_app\records\Motivation; use yii_app\records\MotivationValue; use yii_app\records\EmployeePayment; @@ -14,94 +15,65 @@ class SumSalaryByMonthAction extends Action { public function run() { - $request = Yii::$app->request; - $inputDate = $request->post('date'); // Получаем дату из формы - $selectedDate = new DateTime($inputDate); - $year = $selectedDate->format('Y'); - $month = $selectedDate->format('m'); + $model = new SumSalaryForm(); - // Найти все записи по выбранному месяцу и году - $motivations = Motivation::find() - ->where(['year' => $year, 'month' => $month]) - ->all(); + if ($model->load(Yii::$app->request->post()) && $model->validate()) { + $year = $model->year; + $month = $model->month; - 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"))] // последняя неделя до конца месяца - ]; + $motivations = Motivation::find() + ->where(['year' => $year, 'month' => $month]) + ->all(); - 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; + if (empty($motivations)) { + Yii::warning("Не найдено ни одной записи для выбранного месяца и года."); + return $this->controller->redirect(['motivation/index']); + } - $records = MotivationService::getTimetableFactRecordsByDateAndStore($startDate, $endDate, $store_id); - $vacationSum = MotivationService::getVacationsSum($startDate, $endDate, $store_id); + $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')] // последняя неделя до конца месяца + ]; + + foreach ($weeks as $weekIndex => $week) { + $startDate = sprintf("%s-%02d-%02d", $year, $month, $week['start']); + $endDate = sprintf("%s-%02d-%02d", $year, $month, $week['end']); + + foreach ($motivations as $motivation) { + $store_id = $motivation->store_id; + $totalSalary = MotivationService::calculateTotalSalary($startDate, $endDate, $store_id); + + $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'; + } - $totalSalary = 0; - foreach ($records as $record) { - $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); - $dailyPayment = $payment ? $payment->daily_payment : null; + $motivationValue->value_float = $totalSalary; + $motivationValue->value_int = null; + $motivationValue->value_string = null; - // Если $record->salary_shift имеет значение, учитываем его и не добавляем dailyPayment - if (!empty($record->salary_shift)) { - $totalSalary += $record->salary_shift; - } else { - $totalSalary += $dailyPayment; + if (!$motivationValue->save()) { + Yii::error("Не удалось записать данные для store_id: $store_id"); } } - - $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']); + } else { + Yii::warning("Неправильный ввод данных."); + return $this->controller->redirect(['motivation/index']); } - - return $this->controller->redirect(['motivation/index']); } } \ No newline at end of file diff --git a/erp24/models/SumSalaryForm.php b/erp24/models/SumSalaryForm.php index 238d0690..55ee9c9f 100644 --- a/erp24/models/SumSalaryForm.php +++ b/erp24/models/SumSalaryForm.php @@ -6,13 +6,17 @@ use yii\base\Model; class SumSalaryForm extends Model { - public $date; + public $store_id; + public $month; + public $year; public function rules() { return [ - [['date'], 'required'], - [['date'], 'date', 'format' => 'php:Y-m-d'], + [['month', 'year'], 'required'], + ['store_id', 'integer'], + ['month', 'integer', 'min' => 1, 'max' => 12], + ['year', 'integer', 'min' => 1900, 'max' => 2100], ]; } } \ No newline at end of file diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 30b3b785..57f1684c 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -477,4 +477,25 @@ class MotivationService return new DateTime("$year-$month-01"); } } + + + public static function calculateTotalSalary($startDate, $endDate, $storeId) + { + $records = self::getTimetableFactRecordsByDateAndStore($startDate, $endDate, $storeId); + $vacationSum = self::getVacationsSum($startDate, $endDate, $storeId); + + $totalSalary = 0; + foreach ($records as $record) { + $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]); + $dailyPayment = $payment ? $payment->daily_payment : null; + + if (!empty($record->salary_shift)) { + $totalSalary += $record->salary_shift; + } else { + $totalSalary += $dailyPayment; + } + } + + return $totalSalary + $vacationSum; + } } diff --git a/erp24/views/motivation/records.php b/erp24/views/motivation/records.php index 5c014acc..218775cb 100644 --- a/erp24/views/motivation/records.php +++ b/erp24/views/motivation/records.php @@ -1,92 +1,28 @@ title = 'Записи'; +/* @var $this yii\web\View */ +/* @var $model yii_app\models\SumSalaryForm */ + +$this->title = 'Получение зарплат по неделям'; ?> -

title) ?>

+
+

title) ?>

-
'get', 'action' => ['motivation/get-salary'], ]); ?> - field($model, 'store_id')->textInput(['value' => $model->store_id]) ?> - field($model, 'date')->input('date', ['value' => $model->date]) ?> + field($model, 'store_id')->textInput() ?> + field($model, 'month')->textInput() ?> + field($model, 'year')->textInput() ?>
- 'btn btn-primary']) ?> + '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) ?>
- -

Всего смен:

-

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

-

Отпускные:

-

Период: с по

-

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

- +
\ 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 f35a5140..b6bf6d26 100644 --- a/erp24/views/motivation/run-sum-salary.php +++ b/erp24/views/motivation/run-sum-salary.php @@ -10,14 +10,12 @@ $this->title = 'Запуск экшена SumSalary';

title) ?>

-

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

+

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

- ['motivation/sum-salary-by-month'], - 'method' => 'post', - ]); ?> + ['motivation/sum-salary-by-month'], 'method' => 'post']); ?> - field($model, 'date')->input('date') ?> + field($model, 'month')->input('number', ['min' => 1, 'max' => 12]) ?> + field($model, 'year')->input('number', ['min' => 2000, 'max' => 2100]) ?> 'btn btn-primary']) ?>