--- /dev/null
+<?php
+
+namespace yii_app\actions\motivation;
+
+use Yii;
+use yii\base\Action;
+use yii\base\DynamicModel;
+use yii_app\services\MotivationService;
+use yii_app\records\TimetableFactModel;
+use DateTime;
+
+
+class GetSalaryAction extends Action
+{
+
+public function run()
+{
+ $request = Yii::$app->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,
+ ]);
+}
+}
--- /dev/null
+<?php
+
+namespace yii_app\actions\motivation;
+use Yii;
+use yii\base\Action;
+use yii\db\Expression;
+use yii_app\records\Motivation;
+use yii_app\records\MotivationValue;
+use yii_app\records\MotivationValueGroup;
+use yii_app\services\MotivationService;
+use DateTime;
+
+class SumSalaryAction extends \yii\base\Action
+{
+ public function run()
+ {
+ $currentDate = new DateTime();
+ $currentYear = $currentDate->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
'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 [
use yii_app\records\MotivationValueGroup;
use yii_app\records\CityStore;
use yii_app\records\MotivationCostsItem;
-
+use yii_app\records\TimetableFactModel;
+use DateTime;
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;
+ }
}
--- /dev/null
+<?php
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+use yii_app\records\Admin;
+use yii_app\records\EmployeePayment;
+use yii\base\DynamicModel;
+
+/** @var $model DynamicModel */
+/** @var $records array */
+/** @var $startOfWeek string */
+/** @var $endOfWeek string */
+/** @var $weekNumberInMonth int */
+$this->title = 'Записи';
+?>
+
+<h1><?= Html::encode($this->title) ?></h1>
+
+<div class="motivation-form">
+ <?php $form = ActiveForm::begin([
+ 'method' => 'get',
+ 'action' => ['motivation/get-salary'],
+ ]); ?>
+
+ <?= $form->field($model, 'store_id')->textInput(['value' => $model->store_id]) ?>
+ <?= $form->field($model, 'date')->input('date', ['value' => $model->date]) ?>
+
+ <div class="form-group">
+ <?= Html::submitButton('Запросить', ['class' => 'btn btn-primary']) ?>
+ </div>
+
+ <?php ActiveForm::end(); ?>
+</div>
+
+<?php if (!empty($records)): ?>
+ <?php
+ $totalSalary = 0;
+ $weekNumber = (new DateTime($model->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;
+ }
+ }
+ ?>
+
+ <h2>Смены</h2>
+ <table class="table table-bordered">
+ <thead>
+ <tr>
+ <th>Store ID</th>
+ <th>Admin ID</th>
+ <th>Date Shift</th>
+ <th>Salary Shift</th>
+ <th>Name</th>
+ <th>Group</th>
+ <th>ЗП за смену</th>
+ </tr>
+ </thead>
+ <tbody>
+ <?php foreach ($records as $record): ?>
+ <?php
+ $admin = Admin::findOne($record->admin_id);
+ $payment = EmployeePayment::findOne(['admin_id' => $record->admin_id]);
+ $dailyPayment = $payment ? $payment->daily_payment : null;
+ ?>
+ <tr>
+ <td><?= Html::encode($record->store_id) ?></td>
+ <td><?= Html::encode($record->admin_id) ?></td>
+ <td><?= Html::encode($record->date_shift) ?></td>
+ <td><?= Html::encode($record->salary_shift) ?></td>
+ <td><?= Html::encode($admin->name) ?></td>
+ <td><?= Html::encode($admin->group_name) ?></td>
+ <td><?= Html::encode($dailyPayment) ?></td>
+ </tr>
+ <?php endforeach; ?>
+ </tbody>
+ </table>
+
+ <p>Всего смен: <?= count($records) ?></p>
+ <p>ЗП за весь период: <?= Html::encode($totalSalary) ?></p>
+ <p>Период: с <?= Html::encode($startOfWeek) ?> по <?= Html::encode($endOfWeek) ?></p>
+ <p>Номер недели в месяце: <?= Html::encode($weekNumberInMonth) ?></p>
+<?php endif; ?>
--- /dev/null
+<?php
+use yii\helpers\Html;
+
+/* @var $this yii\web\View */
+
+$this->title = 'Запуск экшена SumSalary';
+?>
+
+<div class="sum-salary-action">
+ <h1><?= Html::encode($this->title) ?></h1>
+
+ <p>Нажмите кнопку ниже, чтобы запустить экшн SumSalary и вычислить суммы зарплат.</p>
+
+ <?= Html::beginForm(['motivation/sum-salary'], 'post') ?>
+ <?= Html::submitButton('Запустить SumSalary', ['class' => 'btn btn-primary']) ?>
+ <?= Html::endForm() ?>
+</div>