From d7c9c01743479bb17cb86be22e6ff1777174ba2c Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Thu, 25 Jul 2024 15:28:09 +0300 Subject: [PATCH] =?utf8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B0?= =?utf8?q?=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D0=B0=20=D0=BF=D0=BB?= =?utf8?q?=D0=B0=D0=BD=D0=B0=20=D0=B2=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/actions/motivation/IndexAction.php | 74 +----------- erp24/records/Motivation.php | 54 +++++++++ erp24/records/MotivationValue.php | 60 ++++++++++ erp24/records/MotivationValueGroup.php | 46 ++++++++ erp24/services/MotivationService.php | 138 +++++++++++++++++++++++ 5 files changed, 301 insertions(+), 71 deletions(-) create mode 100644 erp24/records/Motivation.php create mode 100644 erp24/records/MotivationValue.php create mode 100644 erp24/records/MotivationValueGroup.php create mode 100644 erp24/services/MotivationService.php diff --git a/erp24/actions/motivation/IndexAction.php b/erp24/actions/motivation/IndexAction.php index bed7da14..c3f3794b 100644 --- a/erp24/actions/motivation/IndexAction.php +++ b/erp24/actions/motivation/IndexAction.php @@ -11,6 +11,7 @@ use yii\helpers\ArrayHelper; use yii\web\UploadedFile; use yii_app\records\CityStore; use yii_app\records\MotivationCostsItem; +use yii_app\services\MotivationService; class IndexAction extends Action { @@ -21,78 +22,9 @@ class IndexAction extends Action $path = Yii::getAlias('@uploads') . '/template_plan.xslx'; $file->saveAs($path); - $motivationCostsItems = MotivationCostsItem::find()->indexBy('code')->all(); - $spreadsheets = IOFactory::load($path); - $sheets = []; - $errors = []; - foreach ($spreadsheets->getAllSheets() as $spreadSheet) { - $rows = []; - $finish = false; - $storeStr = true; - $error = ''; - foreach ($spreadSheet->getRowIterator() as $ind => $spreadSheetRow) { - $row = []; - foreach ($spreadSheetRow->getCellIterator() as $spreadSheetRowCell) { - $value = $spreadSheetRowCell->getValue(); - if ($value == '###') { - $finish = true; - break; - } - $row []= $value; - } - if ($finish) { - break; - } - if ($storeStr) { - $store = CityStore::find()->where(['id' => $row[0] ?? -1])->one(); - if (!$store) { - $error = "Не найден магазин с таким индексом [0,0]"; - break; - } elseif ($store->name != ($row[1] ?? 'NO_NAME')) { - $error = "Не найден магазин с таким названием [1,0]" . $row[1] . ' ' . $store->name; - break; - } - $year = ((int)$row[2]) ?? -1; - $month = ((int)$row[3]) ?? -1; - if ($year > 2030 || $year < 2023) { - $error = "Не корректно указан год [2,0]"; - break; - } - if ($month < 1 || $month > 12) { - $error = "Не корректно указан месяц [3,0]"; - break; - } - $storeStr = false; - } else { - if (!isset($motivationCostsItems[$row[0] ?? -1])) { - $error = "Не корректен код элемента " . ($row[0] ?? '') . "[$ind,0]"; - break; - } - /** @var $motivationCostsItems MotivationCostsItem[] */ - if ($motivationCostsItems[$row[0]]->name != ($row[1] ?? 'NO_NAME')) { - $error = "Не корректно название элемента " . ($row[1] ?? '') . "[$ind,1]"; - break; - } - if (($row[2] ?? 'NO_NAME') == 'NO_NAME') { - $error = "Не корректно значение элемента [$ind,2]"; - break; - } - switch ($motivationCostsItems[$row[0]]->data_type) { - case MotivationCostsItem::DATA_TYPE_INT: { if (is_int($row[2])) { $value = (int)$row[2]; } else { $error = "Не инт [$ind,2]"; }; break; } - case MotivationCostsItem::DATA_TYPE_FLOAT: { if (is_int($row[2]) || is_float($row[2])) { $value = (float)$row[2]; } else {$error = "Не флот [$ind,2]"; } break; } - case MotivationCostsItem::DATA_TYPE_STRING: { $value = $row[2]; break; } - } - if (!empty($error)) { - break; - } - } - $rows []= $row; - } - $errors []= empty($error) ? '' : $error; - $sheets []= $rows; - } + $data = MotivationService::uploadTemplatePlan($path); - return implode('
', $errors); + return implode('
', $data['errors']); } else { return 'not ok'; } diff --git a/erp24/records/Motivation.php b/erp24/records/Motivation.php new file mode 100644 index 00000000..321ca3dd --- /dev/null +++ b/erp24/records/Motivation.php @@ -0,0 +1,54 @@ + null], + [['store_id', 'year', 'month'], 'integer'], + [['updated_at', 'created_at'], 'safe'], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'store_id' => 'Store ID', + 'year' => 'Year', + 'month' => 'Month', + 'updated_at' => 'Updated At', + 'created_at' => 'Created At', + ]; + } +} diff --git a/erp24/records/MotivationValue.php b/erp24/records/MotivationValue.php new file mode 100644 index 00000000..da5dae07 --- /dev/null +++ b/erp24/records/MotivationValue.php @@ -0,0 +1,60 @@ + null], + [['motivation_id', 'motivation_group_id', 'value_id', 'value_int'], 'integer'], + [['value_float'], 'number'], + [['value_type'], 'string', 'max' => 10], + [['value_string'], 'string', 'max' => 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'motivation_id' => 'Motivation ID', + 'motivation_group_id' => 'Motivation Group ID', + 'value_id' => 'Value ID', + 'value_type' => 'Value Type', + 'value_int' => 'Value Int', + 'value_float' => 'Value Float', + 'value_string' => 'Value String', + ]; + } +} diff --git a/erp24/records/MotivationValueGroup.php b/erp24/records/MotivationValueGroup.php new file mode 100644 index 00000000..d4667e53 --- /dev/null +++ b/erp24/records/MotivationValueGroup.php @@ -0,0 +1,46 @@ + 80], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'name' => 'Name', + 'alias' => 'Alias', + ]; + } +} diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php new file mode 100644 index 00000000..56ca1733 --- /dev/null +++ b/erp24/services/MotivationService.php @@ -0,0 +1,138 @@ +indexBy('code')->all(); + $spreadsheets = IOFactory::load($path); + $errors = []; + foreach ($spreadsheets->getAllSheets() as $spreadSheet) { + $rows = []; + $finish = false; + $storeStr = true; + $error = ''; + $motivation = null; + foreach ($spreadSheet->getRowIterator() as $ind => $spreadSheetRow) { + $row = []; + foreach ($spreadSheetRow->getCellIterator() as $spreadSheetRowCell) { + $value = $spreadSheetRowCell->getValue(); + if ($value == '###') { + $finish = true; + break; + } + $row []= $value; + } + if ($finish) { + break; + } + if ($storeStr) { + $store = CityStore::find()->where(['id' => $row[0] ?? -1])->one(); + if (!$store) { + $error = "Не найден магазин с таким индексом [0,0]"; + break; + } elseif ($store->name != ($row[1] ?? 'NO_NAME')) { + $error = "Не найден магазин с таким названием [1,0]" . $row[1] . ' ' . $store->name; + break; + } + $year = ((int)$row[2]) ?? -1; + $month = ((int)$row[3]) ?? -1; + if ($year > 2030 || $year < 2023) { + $error = "Не корректно указан год [2,0]"; + break; + } + if ($month < 1 || $month > 12) { + $error = "Не корректно указан месяц [3,0]"; + break; + } + + $motivation = Motivation::find()->where(['store_id' => $store->id, 'year' => $year, 'month' => $month])->one(); + /** @var $motivation Motivation */ + if ($motivation) { + $motivation->updated_at = date('Y-m-d H:i:s'); + } else { + $motivation = new Motivation; + $motivation->store_id = $store->id; + $motivation->year = $year; + $motivation->month = $month; + $motivation->created_at = date('Y-m-d H:i:s'); + $motivation->updated_at = date('Y-m-d H:i:s'); + } + + $storeStr = false; + } else { + if (!isset($motivationCostsItems[$row[0] ?? -1])) { + $error = "Не корректен код элемента " . ($row[0] ?? '') . "[$ind,0]"; + break; + } + /** @var $motivationCostsItems MotivationCostsItem[] */ + if ($motivationCostsItems[$row[0]]->name != ($row[1] ?? 'NO_NAME')) { + $error = "Не корректно название элемента " . ($row[1] ?? '') . "[$ind,1]"; + break; + } + if (($row[2] ?? 'NO_NAME') == 'NO_NAME') { + $error = "Не корректно значение элемента [$ind,2]"; + break; + } + switch ($motivationCostsItems[$row[0]]->data_type) { + case MotivationCostsItem::DATA_TYPE_INT: { if (is_int($row[2])) { $value = (int)$row[2]; } else { $error = "Не инт [$ind,2]"; }; break; } + case MotivationCostsItem::DATA_TYPE_FLOAT: { if (is_int($row[2]) || is_float($row[2])) { $value = (float)$row[2]; } else {$error = "Не флот [$ind,2]"; } break; } + case MotivationCostsItem::DATA_TYPE_STRING: { $value = $row[2]; break; } + } + if (!empty($error)) { + break; + } + } + $rows []= $row; + } + if ($motivation && empty($error)) { + $motivation->save(); + if ($motivation->getErrors()) { + $error = json_encode($motivation->getErrors()); + } + } else { + $error = 'Не указан магазин, год и месяц [0,0]'; + } + if (empty($error)) { + $motivationValueGroupPlan = MotivationValueGroup::find()->where(['alias' => 'plan'])->one(); + /** @var $motivationValueGroupPlan MotivationValueGroup */ + foreach ($rows as $row) { + $motivationValue = MotivationValue::find()->where([ + 'motivation_id' => $motivation->id, + 'motivation_group_id' => $motivationValueGroupPlan->id, + 'value_id' => $row[0] + ])->one(); + /** @var $motivationValue MotivationValue */ + if (!$motivationValue) { + $motivationValue = new MotivationValue; + $motivationValue->motivation_id = $motivation->id; + $motivationValue->motivation_group_id = $motivationValueGroupPlan->id; + $motivationValue->value_id = $row[0]; + } + $motivationValue->value_type = $motivationCostsItems[$row[0]]->data_type; + switch ($motivationValue->value_type) { + case MotivationCostsItem::DATA_TYPE_INT: { $motivationValue->value_int = (int)$row[2]; break; } + case MotivationCostsItem::DATA_TYPE_FLOAT: { $motivationValue->value_float = (float)$row[2]; break; } + case MotivationCostsItem::DATA_TYPE_STRING: { $motivationValue->value_string = '' . $row[2]; break; } + } + $motivationValue->save(); + if ($motivationValue->getErrors()) { + $error = json_encode($motivationValue->getErrors()); + break; + } + } + } + $errors []= empty($error) ? '' : $error; + } + + return compact('errors'); + } +} \ No newline at end of file -- 2.39.5