]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-140] Test факт мотивации
authorAlexander Smirnov <fredeom@mail.ru>
Wed, 14 Aug 2024 07:37:12 +0000 (10:37 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Wed, 14 Aug 2024 07:37:12 +0000 (10:37 +0300)
erp24/actions/motivation/TestFactAction.php [new file with mode: 0644]
erp24/controllers/MotivationController.php
erp24/services/MotivationService.php
erp24/views/motivation/test-fact.php [new file with mode: 0644]

diff --git a/erp24/actions/motivation/TestFactAction.php b/erp24/actions/motivation/TestFactAction.php
new file mode 100644 (file)
index 0000000..87f915e
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+
+namespace yii_app\actions\motivation;
+
+use Yii;
+use yii\base\Action;
+use yii\base\DynamicModel;
+use yii\helpers\ArrayHelper;
+use yii_app\records\Motivation;
+use yii_app\services\MotivationService;
+
+class TestFactAction extends Action
+{
+    public function run() {
+
+        // Получаем текущую дату
+        $currentDate = new \DateTime();
+
+        $model = DynamicModel::validateData([
+            'year' => $currentDate->format('Y'), // Текущий год
+            'month' => $currentDate->format('n') // Текущий месяц (1-12)
+        ], [
+            [['year', 'month'], 'safe']
+        ]);
+
+        $motivations = Motivation::find()->all();
+        $possibleYears = ArrayHelper::getColumn($motivations, 'year');
+        $years = array_filter(range(2023, 20100), function ($k) use ($possibleYears) {
+            return in_array($k, $possibleYears);
+        });
+        $years = array_combine($years, $years);
+        $possibleMonth = ArrayHelper::getColumn($motivations, 'month');
+        $months = array_filter([1 => 'Январь', 2 => 'Февраль', 3 => 'Март', 4 => 'Апрель', 5 => 'Май', 6 => 'Июнь', 7 => 'Июль', 8 => 'Август', 9 => 'Сентябрь', 10 => 'Октябрь', 11 => 'Ноябрь', 12 => 'Декабрь'], function ($k, $v) use ($possibleMonth) {
+            return in_array($v, $possibleMonth);
+        }, ARRAY_FILTER_USE_BOTH);
+
+        // Загружаем данные из GET-запроса, если они есть
+        if (Yii::$app->request->get()) {
+            $model->load(Yii::$app->request->get());
+
+            if ($model->year === '') {
+                $model->year = $currentDate->format('Y');
+            } else {
+                $model->year = intval($model->year);
+                // Проверяем, что год находится в допустимом диапазоне
+                if (!in_array($model->year, $years)) {
+                    $model->year = $currentDate->format('Y');
+                }
+            }
+
+            $model->month = intval($model->month);
+
+            MotivationService::calculateMonthSales($model->year, $model->month);
+
+            MotivationService::calculateMonthServices($model->year, $model->month);
+
+            MotivationService::calculateMonthDefect($model->year, $model->month);
+        }
+
+        return $this->controller->render('test-fact', compact('model', 'years', 'months'));
+    }
+}
\ No newline at end of file
index f81c287b1ea0a236d5b7568d2d636b96200be6f7..c6cfd50e5a55016d7f242e04c1d391ba426ea7b2 100644 (file)
@@ -16,6 +16,7 @@ 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,
+            'test-fact' => \yii_app\actions\motivation\TestFactAction::class,
         ];
     }
 
index 66a665ca31a3e9497b7a2b149a3fe20e9347881d..5e499e6da99213b51c456286fa587758bc14528f 100644 (file)
@@ -533,20 +533,20 @@ class MotivationService
         $returnSales = Sales::find()->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all();
         $returnSalesIds = ArrayHelper::getColumn($returnSales, 'sales_check');
 
-        $salesOffline = Sales::find()->select(['SUM(summ) as total'])
+        $salesOffline = Sales::find()->select(['SUM(summ) as total', 'store_id'])
             ->where(['between', 'date', $monthStart, $monthEnd])
             ->andWhere(['operation' => Sales::OPERATION_SALE])
             ->andWhere(['order_id' => ['', '0']])
-            ->andWhere(['NOT IN', 'check_id', $returnSalesIds])
+            ->andWhere(['NOT IN', 'id', $returnSalesIds])
             ->groupBy(['store_id'])
             ->indexBy('store_id')
             ->asArray()->all();
 
-        $salesOnline = Sales::find()->select(['SUM(summ) as total'])
+        $salesOnline = Sales::find()->select(['SUM(summ) as total', 'store_id'])
             ->where(['between', 'date', $monthStart, $monthEnd])
             ->andWhere(['operation' => Sales::OPERATION_SALE])
             ->andWhere(['NOT IN', 'order_id', ['', '0']])
-            ->andWhere(['NOT IN', 'check_id', $returnSalesIds])
+            ->andWhere(['NOT IN', 'id', $returnSalesIds])
             ->groupBy(['store_id'])
             ->indexBy('store_id')
             ->asArray()->all();
@@ -642,14 +642,14 @@ class MotivationService
 
         // Ищем продукты из категории services
         $salesProduct = SalesProducts::find()->alias('p')
-            ->select(['SUM(summ) as total'])
+            ->select(['SUM(p.summ) as total', 'store_id'])
             ->leftJoin('sales s', 's.id = p.check_id')
             ->where(['check_id' => $salesIds])
             ->andWhere(['NOT IN', 'check_id', $returnSalesIds])
             ->andWhere(['product_id' => array_keys($products1c)])
-            ->groupBy(['s.store_id'])
-            ->indexBy('s.store_id')
-            ->asArray()->one();
+            ->groupBy(['store_id'])
+            ->indexBy('store_id')
+            ->asArray()->all();
 
         // Ищем продажи по продуктовым гуидам по каталог-гуидам категории services_delivery
         $salesDelivery = Sales::find()->alias('s')
@@ -666,14 +666,14 @@ class MotivationService
 
         // Ищем продукты из категории services_delivery
         $salesProductDelivery = SalesProducts::find()->alias('p')
-            ->select(['SUM(summ) as total'])
+            ->select(['SUM(p.summ) as total', 'store_id'])
             ->leftJoin('sales s', 's.id = p.check_id')
             ->where(['check_id' => $salesIdsDelivery])
             ->andWhere(['NOT IN', 'check_id', $returnSalesIdsDelivery])
             ->andWhere(['product_id' => array_keys($products1cDelivery)])
-            ->groupBy(['s.store_id'])
-            ->indexBy('s.store_id')
-            ->asArray()->one();
+            ->groupBy(['store_id'])
+            ->indexBy('store_id')
+            ->asArray()->all();
 
         $motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all();
 
@@ -728,26 +728,25 @@ class MotivationService
 
         $motivations = Motivation::find()->where(['year' => $year, 'month' => $month])->indexBy('store_id')->all();
         $motivationValueGroup = MotivationValueGroup::find()->where(['alias' => 'fact'])->one();
-        $exportImportTables = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store',
+        $exportImportTables = ExportImportTable::find()->select(['export_val', 'entity_id'])->where(['entity' => 'city_store',
             'entity_id' => array_keys($motivations), 'export_id' => 1])->indexBy('entity_id')->all();
         /** @var $exportImportTables ExportImportTable[] */
 
         foreach ($exportImportTables as $store_id => $store_guid) {
-            $writeOffs = WriteOffs::find()->select(['sum(summ) as total', 'type'])
-                ->where(['between', 'date', $monthStart, $monthEnd])
-                ->andWhere(['store_id' => $store_guid])
-                ->groupBy(['type'])
-                ->indexBy('type')
-                ->asArray()->all();
-
-            foreach ($writeOffs as $key => $data) {
-                $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key);
-                if (empty($motivationItemType)) {
-                    continue;
-                }
-                $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one();
-                /** @var $motivationCostsItem MotivationCostsItem */
-                if (isset($motivations[$store_id])) {
+            if (isset($motivations[$store_id])) {
+                $writeOffs = WriteOffs::find()->select(['sum(summ) as total', 'type'])
+                    ->where(['between', 'date', $monthStart, $monthEnd])
+                    ->andWhere(['store_id' => $store_guid])
+                    ->groupBy(['type'])
+                    ->indexBy('type')
+                    ->asArray()->all();
+                foreach ($writeOffs as $key => $data) {
+                    $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key);
+                    if (empty($motivationItemType)) {
+                        continue;
+                    }
+                    $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one();
+                    /** @var $motivationCostsItem MotivationCostsItem */
                     $motivationValue = MotivationValue::find()->where(['motivation_id' => $motivations[$store_id]->id,
                         'motivation_group_id' => $motivationValueGroup->id, 'value_id' => $motivationCostsItem->code])->one();
                     if (!$motivationValue) {
diff --git a/erp24/views/motivation/test-fact.php b/erp24/views/motivation/test-fact.php
new file mode 100644 (file)
index 0000000..e95249e
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+use kartik\select2\Select2;
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+use yii\base\DynamicModel;
+
+/** @var $model DynamicModel */
+/** @var $years array */
+/** @var $months array */
+
+
+?>
+
+<div class="motivationTestFact m-5">
+
+    <?php $form = ActiveForm::begin(['method' => 'GET', 'action' => '/motivation/test-fact']) ?>
+
+    <div class="row">
+        <div class="col-4 d-flex justify-content-between align-items-center gap-4">
+            <div class="d-flex justify-content-around align-items-center gap-2">
+                <div class="mb-3">Год:</div>
+                <div style="display: inline-block">
+                    <?= $form->field($model, 'year')->widget(Select2::class, [
+                        'data' => $years,
+                        'language' => 'ru',
+                        'options' => ['placeholder' => 'Год...'],
+                        'pluginOptions' => [
+                            'allowClear' => true
+                        ],
+                    ])->label(false) ?>
+                </div>
+                <div class="d-flex justify-content-around align-items-center gap-2">
+                    <div class="mb-3">Месяц:</div>
+                    <div style="display: inline-block">
+                        <?= $form->field($model, 'month')->widget(Select2::class, [
+                            'data' => $months,
+                            'language' => 'ru',
+                            'options' => ['placeholder' => 'Месяц...'],
+                            'pluginOptions' => [
+                                'allowClear' => true
+                            ],
+                        ])->label(false) ?>
+                    </div>
+                </div>
+                <div class="d-flex justify-content-around align-items-center gap-2">
+                    <div class="mb-3"><?= Html::submitButton('Применить', ['class' => 'btn btn-secondary btn-sm']) ?></div>
+                </div>
+            </div>
+        </div>
+
+        <?php ActiveForm::end() ?>
+
+
+</div>