]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-130] Добавление подсчёта брака
authorAlexander Smirnov <fredeom@mail.ru>
Tue, 6 Aug 2024 13:28:29 +0000 (16:28 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Tue, 6 Aug 2024 13:28:29 +0000 (16:28 +0300)
erp24/actions/motivation/IndexAction.php
erp24/records/MotivationCostsItem.php
erp24/records/WriteOffsErp.php
erp24/services/MotivationService.php

index f745fa062253454a944aac23ccc3b4ceb772d390..9a6c2e1475f1ba350d7b982a878e82a4a91c7eb5 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace yii_app\actions\motivation;
 
-use PhpOffice\PhpSpreadsheet\IOFactory;
-use PhpOffice\PhpSpreadsheet\Spreadsheet;
 use Yii;
 use yii\base\Action;
 use yii\base\DynamicModel;
@@ -11,7 +9,6 @@ use yii\helpers\ArrayHelper;
 use yii\web\UploadedFile;
 use yii_app\records\CityStore;
 use yii_app\records\Motivation;
-use yii_app\records\MotivationCostsItem;
 use yii_app\services\MotivationService;
 
 class IndexAction extends Action
@@ -87,6 +84,9 @@ class IndexAction extends Action
             $model->month = intval($model->month);
         }
 
+        // Подсчитываем стоимость брака
+        MotivationService::calculateDefectCost($model->store_id, $model->year, $model->month);
+
         $showTable = false;
         $motivationData = [];
         $daysInMonth = null;
index 98bd45f602deadfacdbceab75bb7f48e36fdf125..9492f9c6e2c01a28419d9205a76c5a20f2e120b6 100644 (file)
@@ -24,6 +24,21 @@ class MotivationCostsItem extends ActiveRecord
     const DATA_TYPE_FLOAT = 'float';
     const DATA_TYPE_STRING = 'string';
 
+    const ITEM_WRITE_OFF_OF_ILLIQUID_GOODS_SPOILAGE_EXPIRATION_OF_SHELF_LIFE = 'Списание неликвидного товара: порча, истечение срока годности';
+    const ITEM_DEFECTIVE_DELIVERY = 'Брак с поставки';
+    const ITEM_DEFECT_DUE_TO_EQUIPMENT_FAILURE = 'Брак из-за поломки оборудования';
+    const ITEM_REGRADING = 'Пересорт';
+
+    public static function writeOffsToMotivationItemMap($itemType) {
+        switch ($itemType) {
+            case WriteOffsErp::WRITE_OFFS_TYPE_BRAK: return self::ITEM_WRITE_OFF_OF_ILLIQUID_GOODS_SPOILAGE_EXPIRATION_OF_SHELF_LIFE;
+            case WriteOffsErp::WRITE_OFFS_TYPE_DELIVERY_BRAK: return self::ITEM_DEFECTIVE_DELIVERY;
+            case WriteOffsErp::WRITE_OFFS_TYPE_DUE_TO_EQUIPMENT_FAILURE_BRAK: return self::ITEM_DEFECT_DUE_TO_EQUIPMENT_FAILURE;
+            case WriteOffsErp::WRITE_OFFS_TYPE_RESORTING_DOES_NOT_COUNT_TOWARDS_COST: return self::ITEM_REGRADING;
+            default: return self::ITEM_WRITE_OFF_OF_ILLIQUID_GOODS_SPOILAGE_EXPIRATION_OF_SHELF_LIFE;
+        }
+    }
+
     /**
      * {@inheritdoc}
      */
index 33182e8dfca9c1d2efee13431012d034fbe86a46..5c2b3b63759bd4b0c06444189b6b1c88e3e0d1fd 100644 (file)
@@ -119,6 +119,9 @@ class WriteOffsErp extends \yii\db\ActiveRecord
     const STATUS_DISABLE = 5;
     const WRITE_OFFS_TYPE_BRAK = "Брак";
     const WRITE_OFFS_TYPE_RETURN_KALUGA = "Возврат нереализованного товара (Калуга)";
+    const WRITE_OFFS_TYPE_DELIVERY_BRAK = 'Брак с поставки';
+    const WRITE_OFFS_TYPE_DUE_TO_EQUIPMENT_FAILURE_BRAK = 'Брак из-за поломки оборудования';
+    const WRITE_OFFS_TYPE_RESORTING_DOES_NOT_COUNT_TOWARDS_COST = 'Пересорт, не идет в затраты';
 
     public function custom_function_validation($attribute, $params)
     {
index 06e071aa8c8fedcbb9658b000d963de733d1dc08..f94585cc59c8ba6e86eba60113d8d0ae46c2244f 100644 (file)
@@ -3,11 +3,13 @@
 namespace yii_app\services;
 
 use PhpOffice\PhpSpreadsheet\IOFactory;
+use yii_app\records\ExportImportTable;
 use yii_app\records\Motivation;
 use yii_app\records\MotivationValue;
 use yii_app\records\MotivationValueGroup;
 use yii_app\records\CityStore;
 use yii_app\records\MotivationCostsItem;
+use yii_app\records\WriteOffs;
 
 
 class MotivationService
@@ -326,4 +328,55 @@ class MotivationService
 
         return compact('errors');
     }
+
+    public static function calculateDefectCost($store_id, $year, $month) {
+        $monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1'));
+        $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1'));
+
+        $motivation = Motivation::find()->where(['store_id' => $store_id, 'year' => $year, 'month' => $month])->one();
+
+        foreach (range(1, 5) as $ind) {
+            $weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($monthStart)));
+            $weekEnd = date("Y-m-d 23:50:50", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($monthStart)));
+            if ($weekEnd > $monthEnd) {
+                $weekEnd = $monthEnd;
+            }
+
+            $motivationValueGroup = MotivationValueGroup::find()->where(['alias' => 'week' . $ind])->one();
+
+            $eit = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $store_id, 'export_id' => 1])->one();
+            /** @var $eit ExportImportTable */
+            if ($eit) {
+                $writeOffs = WriteOffs::find()->select(['sum(summ) as total', 'type'])
+                    ->where(['between', 'date', $weekStart, $weekEnd])
+                    ->andWhere(['store_id' => $eit->export_val])
+                    ->groupBy(['type'])
+                    ->indexBy('type')
+                    ->asArray()->all();
+
+                foreach ($writeOffs as $key => $data) {
+                    $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key);
+                    $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one();
+                    /** @var $motivationCostsItem MotivationCostsItem */
+                    if ($motivation) {
+                        $motivationValue = MotivationValue::find()->where(['motivation_id' => $motivation->id,
+                            'motivation_group_id' => $motivationValueGroup->id, 'value_id' => $motivationCostsItem->code])->one();
+                        if (!$motivationValue) {
+                            $motivationValue = new MotivationValue;
+                            $motivationValue->motivation_id = $motivation->id;
+                            $motivationValue->motivation_group_id = $motivationValueGroup->id;
+                            $motivationValue->value_id = $motivationCostsItem->code;
+                            $motivationValue->value_type = $motivationCostsItem->data_type;
+                        }
+                        $motivationValue->value_float = $data['total'];
+                        $motivationValue->save();
+                        if ($motivationValue->getErrors()) {
+                            var_dump($motivationValue->getErrors());
+                            die;
+                        }
+                    }
+                }
+            }
+        }
+    }
 }