diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index afabb24..f838024 100644 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -266,13 +266,15 @@ class ParserController extends BaseController foreach (glob(Yii::getAlias('@temp_upload') . '/*.csv') as $server_file) { $file_name = basename($server_file,".csv"); - // rename( $server_file, Yii::getAlias('@auto_upload') . '/' . $file_name . '.csv'); copy( $server_file, Yii::getAlias('@auto_upload') . '/' . $file_name . '.csv' ); } + Yii::$app->session->setFlash( 'server-files', 'Файл успешно загружен' ); $this->redirect('server-files'); +// $csv = new \console\controllers\ParserController( 'parse-csv', $this->module ); +// $csv->actionParseCsv(); } diff --git a/backend/models/Importers.php b/backend/models/Importers.php index d6f7d2d..221b09e 100644 --- a/backend/models/Importers.php +++ b/backend/models/Importers.php @@ -2,6 +2,7 @@ namespace backend\models; +use common\components\CustomVarDamp; use Yii; use backend\components\base\BaseActiveRecord; @@ -93,6 +94,7 @@ class Importers extends BaseActiveRecord { // возьмем только поля описанные в fields() - там как раз наши настройки парсера $arr = $this->toArray(); + // отсортируем по ключам с учетом преобразования в число asort($arr, SORT_NUMERIC); // уберем нулевые колонки @@ -101,10 +103,21 @@ class Importers extends BaseActiveRecord }); // нам нужны именно ключи $arr = array_keys($arr); + return $arr; + } + + + public function getMultiply () + { + $arr['sign'] = $this->PARSER_FIELD_SIGN; + $arr['multiplier'] = $this->PARSER_FIELD_MULTIPLIER; + return $arr; } + + public function fields() { return [ @@ -119,5 +132,4 @@ class Importers extends BaseActiveRecord } - } diff --git a/common/components/parsers/CustomConverter.php b/common/components/parsers/CustomConverter.php index 4b2a084..07f8718 100644 --- a/common/components/parsers/CustomConverter.php +++ b/common/components/parsers/CustomConverter.php @@ -11,6 +11,9 @@ class CustomConverter extends Converter { * @param $key_array - ключи для вложенного массива * @return array - таблица с проименованными колонками */ + public static $sign; + public static $multiplier; + public static function convertToAssocArray ( array $value_arr, array $key_array, $key_prefix = '' ) { // очистка служебного префикса в массиве заголовков @@ -82,6 +85,40 @@ class CustomConverter extends Converter { return $row; } + public function ConvertToMultiply ( array $row ) + { + $PRICE = $row[ 'PRICE' ]; + $sign = self::$sign; + $multiplier = self::$multiplier; + //CustomVarDamp::dumpAndDie(self); + if (isset($sign)) { + if ($sign == '+') { + if ($multiplier > 0) { + $PRICE += $multiplier; + } + } + else if ($sign == '-') { + if ($multiplier > 0) { + $PRICE -= $multiplier; + } + } + else if ($sign == '*') { + if ($multiplier > 0) { + $PRICE *= $multiplier; + } + } + else if ($sign == '/') { + if ($multiplier > 0) { + $PRICE /= $multiplier; + } + } + } + + $row[ 'PRICE' ] = $PRICE; + + return $row; + + } } \ No newline at end of file diff --git a/common/components/parsers/CustomCsvParser.php b/common/components/parsers/CustomCsvParser.php index 72af4e7..6a7caf6 100644 --- a/common/components/parsers/CustomCsvParser.php +++ b/common/components/parsers/CustomCsvParser.php @@ -25,10 +25,12 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { if ($this->hasHeaderRow) { // если у файла есть заголовок, то в результате имеем ассоциативный массив $this->converter_conf['hasKey'] = 1; + } } - +// $this->converter = \Yii::createObject( $this->converter_conf ); +// CustomVarDamp::dumpAndDie($this->converter); } /** @@ -38,7 +40,7 @@ class CustomCsvParser extends \yii\multiparser\CsvParser { */ protected function convert($arr) { - $arr = \Yii::$app->multiparser->convertByConfiguration($arr, $this->converter_conf); + $arr = \Yii::$app->multiparser->convertByConfiguration( $arr, $this->converter_conf ); return $arr; diff --git a/common/components/parsers/config.php b/common/components/parsers/config.php index 1908466..4a423cb 100644 --- a/common/components/parsers/config.php +++ b/common/components/parsers/config.php @@ -4,18 +4,21 @@ ['web' => ['class' => 'common\components\parsers\CustomCsvParser', 'auto_detect_first_line' => true, - 'converter_conf' => ['class' => ' common\components\parsers\CustomConverter', + 'converter_conf' => [ + //'class' => ' common\components\parsers\CustomConverter', // @todo переделать на подключаемый behavior 'configuration' => ["string" => 'DESCR'],] ], 'console' => ['class' => 'common\components\parsers\CustomCsvParser', 'auto_detect_first_line' => true, 'hasHeaderRow' => true, - 'converter_conf' => ['class' => ' common\components\parsers\CustomConverter', + 'converter_conf' => [ + //'class' => ' common\components\parsers\CustomConverter', 'hasKey' => 1, 'configuration' => ["string" => 'DESCR', "float" => 'PRICE', "integer" => ['BOX','ADD_BOX'], + "multiply" => [], "details" => [] // @todo сделать отдельно конфигурирование валидации ] @@ -36,7 +39,8 @@ ['console' => ['class' => 'yii\multiparser\XmlParser', 'node' => 'Товар', - 'converter_conf' => ['class' => ' common\components\parsers\CustomConverter', + 'converter_conf' => [ + //'class' => ' common\components\parsers\CustomConverter', 'hasKey' => 1, 'configuration' => ["details" => [] ],], diff --git a/console/controllers/ParserController.php b/console/controllers/ParserController.php index 3d210ec..aa725c8 100644 --- a/console/controllers/ParserController.php +++ b/console/controllers/ParserController.php @@ -1,6 +1,7 @@ $file_name])->importer_id; - $keys = Importers::findOne(['id' => $importer_id])->keys; + $current_importer = Importers::findOne(['id' => $importer_id]); + $keys = $current_importer->keys; + $mult_array = $current_importer->multiply; + + // получим настройки ценообразования и передадим их отдельно в конвертер + $sign = ''; + $multiplier = ''; + extract( $mult_array ); + $config = ['record_id' => $file_name, 'importer_id' => $importer_id, 'parser_config' => ['keys' => $keys, + 'converter_conf' => + ['sign' => $sign, + 'multiplier' => $multiplier], 'mode' => 'console'] ]; if ($this->parseFileConsole($file_path, $config)) { @@ -48,7 +59,7 @@ class ParserController extends Controller $parser_config = $configuration['parser_config']; } $data = \Yii::$app->multiparser->parse( $file_path, $parser_config ); - if (! $data) { + if ( ! $data ) { // @todo переделать, что бы ошибка автоматически останавливала сценарий return false; } -- libgit2 0.21.4