]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
add sum salary action
authorJoySystem_v <fvv2011@gmail.com>
Tue, 6 Aug 2024 07:19:55 +0000 (10:19 +0300)
committerJoySystem_v <fvv2011@gmail.com>
Tue, 6 Aug 2024 07:19:55 +0000 (10:19 +0300)
erp24/actions/motivation/GetSalaryAction.php [new file with mode: 0644]
erp24/actions/motivation/SumSalaryAction.php [new file with mode: 0644]
erp24/controllers/MotivationController.php
erp24/services/MotivationService.php
erp24/views/motivation/records.php [new file with mode: 0644]
erp24/views/motivation/run-sum-salary.php [new file with mode: 0644]

diff --git a/erp24/actions/motivation/GetSalaryAction.php b/erp24/actions/motivation/GetSalaryAction.php
new file mode 100644 (file)
index 0000000..8ebecc4
--- /dev/null
@@ -0,0 +1,48 @@
+<?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,
+    ]);
+}
+}
diff --git a/erp24/actions/motivation/SumSalaryAction.php b/erp24/actions/motivation/SumSalaryAction.php
new file mode 100644 (file)
index 0000000..9eb16fe
--- /dev/null
@@ -0,0 +1,70 @@
+<?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
index f81c287b1ea0a236d5b7568d2d636b96200be6f7..412b0905589338a18710452af8af431afa0fb5b6 100644 (file)
@@ -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 [
index 51ab681c0663fb10e0aa9d03890ab87f295f1f82..583850f0db3b429069d946868ceefa1c32e66871 100644 (file)
@@ -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 (file)
index 0000000..54416c1
--- /dev/null
@@ -0,0 +1,90 @@
+<?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; ?>
diff --git a/erp24/views/motivation/run-sum-salary.php b/erp24/views/motivation/run-sum-salary.php
new file mode 100644 (file)
index 0000000..9ef37c4
--- /dev/null
@@ -0,0 +1,17 @@
+<?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>