From 77c2c31e2e577766951d95ffb7f9a0a8cf28801a Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Tue, 6 Aug 2024 16:28:29 +0300 Subject: [PATCH] =?utf8?q?[ERP-130]=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?utf8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=B4=D1=81=D1=87=D1=91?= =?utf8?q?=D1=82=D0=B0=20=D0=B1=D1=80=D0=B0=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/actions/motivation/IndexAction.php | 6 +-- erp24/records/MotivationCostsItem.php | 15 +++++++ erp24/records/WriteOffsErp.php | 3 ++ erp24/services/MotivationService.php | 53 ++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/erp24/actions/motivation/IndexAction.php b/erp24/actions/motivation/IndexAction.php index f745fa06..9a6c2e14 100644 --- a/erp24/actions/motivation/IndexAction.php +++ b/erp24/actions/motivation/IndexAction.php @@ -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; diff --git a/erp24/records/MotivationCostsItem.php b/erp24/records/MotivationCostsItem.php index 98bd45f6..9492f9c6 100644 --- a/erp24/records/MotivationCostsItem.php +++ b/erp24/records/MotivationCostsItem.php @@ -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} */ diff --git a/erp24/records/WriteOffsErp.php b/erp24/records/WriteOffsErp.php index 33182e8d..5c2b3b63 100644 --- a/erp24/records/WriteOffsErp.php +++ b/erp24/records/WriteOffsErp.php @@ -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) { diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 06e071aa..f94585cc 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -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; + } + } + } + } + } + } } -- 2.39.5