Commit 1a17e5eb3925c91997cd8a08972bbd39293d5386

Authored by Yarik
1 parent 036d7990

Import test

common/modules/product/models/Export.php
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 namespace common\modules\product\models; 3 namespace common\modules\product\models;
4 4
5 use common\modules\rubrication\models\TaxGroup; 5 use common\modules\rubrication\models\TaxGroup;
  6 + use common\modules\rubrication\models\TaxOption;
6 use yii\base\Model; 7 use yii\base\Model;
7 8
8 class Export extends Model 9 class Export extends Model
@@ -101,14 +102,19 @@ @@ -101,14 +102,19 @@
101 return $result; 102 return $result;
102 103
103 } 104 }
104 - 105 +
  106 + /**
  107 + * @param TaxOption[] $filters
  108 + *
  109 + * @return string
  110 + */
105 public function convertFilterToString($filters) 111 public function convertFilterToString($filters)
106 { 112 {
107 if(!empty($filters)){ 113 if(!empty($filters)){
108 $fittersArray = []; 114 $fittersArray = [];
109 foreach($filters as $filter) { 115 foreach($filters as $filter) {
110 if($filter->taxGroup instanceof TaxGroup){ 116 if($filter->taxGroup instanceof TaxGroup){
111 - $fittersArray[ $filter->taxGroup->alias ][] = $filter->value; 117 + $fittersArray[ $filter->taxGroup->alias ][] = $filter->value . '(#' . $filter->alias . '#)';
112 } 118 }
113 119
114 } 120 }
common/modules/product/models/Import.php
@@ -317,9 +317,9 @@ @@ -317,9 +317,9 @@
317 $result_items[] = "Не указано наименование товара (строка $j)"; 317 $result_items[] = "Не указано наименование товара (строка $j)";
318 continue; 318 continue;
319 } 319 }
320 - if(preg_match('/^.+\(#(\d+)#\)$/', trim($product_name), $remote_id)) {  
321 - if(!empty($remote_id[1])) {  
322 - $remote_id = $remote_id[1]; 320 + if (preg_match('/^.+\(#(\d+)#\)$/', trim($product_name), $remote_id)) {
  321 + if (!empty( $remote_id[ 1 ] )) {
  322 + $remote_id = $remote_id[ 1 ];
323 $product_name = substr($product_name, 0, strpos($product_name, '(#' . $remote_id . '#)')); 323 $product_name = substr($product_name, 0, strpos($product_name, '(#' . $remote_id . '#)'));
324 } 324 }
325 } 325 }
@@ -368,13 +368,16 @@ @@ -368,13 +368,16 @@
368 $product_image = explode('=', $data[ 14 ]); 368 $product_image = explode('=', $data[ 14 ]);
369 $product_image = @$product_image[ 3 ]; 369 $product_image = @$product_image[ 3 ];
370 370
371 - if(!empty($remote_id)) {  
372 - if(($_product = Product::find()->filterWhere(['product_id' => $remote_id])->one()) === null) { 371 + if (!empty( $remote_id )) {
  372 + if (( $_product = Product::find()
  373 + ->filterWhere([ 'product_id' => $remote_id ])
  374 + ->one() ) === null
  375 + ) {
373 $_product = new Product(); 376 $_product = new Product();
374 } 377 }
375 } elseif (( $_product = Product::find() 378 } elseif (( $_product = Product::find()
376 - ->filterWhere([ 'name' => trim($product_name) ])  
377 - ->one() ) === null 379 + ->filterWhere([ 'name' => trim($product_name) ])
  380 + ->one() ) === null
378 ) { 381 ) {
379 $_product = new Product(); 382 $_product = new Product();
380 } 383 }
@@ -630,22 +633,31 @@ @@ -630,22 +633,31 @@
630 $filters_options = explode(',', $filter[ 2 ][ 0 ]); 633 $filters_options = explode(',', $filter[ 2 ][ 0 ]);
631 634
632 foreach ($filters_options as $filter_options) { 635 foreach ($filters_options as $filter_options) {
633 - $value = TaxValueString::find()  
634 - ->innerJoinWith('taxOption')  
635 - ->andWhere([ 'value' => $filter_options ])  
636 - ->andWhere([ 'tax_option.tax_group_id' => $taxGroup->tax_group_id ])  
637 - ->one(); 636 + $parsed_filter = $this->parseFilter($filter_options);
  637 + $value = null;
  638 + if ($parsed_filter !== false) {
  639 + $value = TaxValueString::find()
  640 + ->innerJoinWith('taxOption')
  641 + ->andWhere([ 'tax_option.alias' => $parsed_filter[ 2 ] ])
  642 + ->andWhere([ 'tax_option.tax_group_id' => $taxGroup->tax_group_id ])
  643 + ->one();
  644 + }
638 645
639 if (!$value instanceof TaxValueString) { 646 if (!$value instanceof TaxValueString) {
640 // Create option 647 // Create option
641 $option = new TaxOption(); 648 $option = new TaxOption();
642 $option->tax_group_id = $taxGroup->tax_group_id; 649 $option->tax_group_id = $taxGroup->tax_group_id;
643 - $option->name = $filter_options; 650 + if($parsed_filter !== false) {
  651 + $option->name = $parsed_filter[1];
  652 + $option->alias = $parsed_filter[2];
  653 + } else {
  654 + $option->name = $filter_options;
  655 + }
644 $option->save(); 656 $option->save();
645 657
646 $value = new TaxValueString(); 658 $value = new TaxValueString();
647 $value->tax_option_id = $option->tax_option_id; 659 $value->tax_option_id = $option->tax_option_id;
648 - $value->value = $filter_options; 660 + $value->value = $option->name;
649 $value->save(); 661 $value->save();
650 662
651 $option->default_value = $value->tax_value_id; 663 $option->default_value = $value->tax_value_id;
@@ -659,4 +671,14 @@ @@ -659,4 +671,14 @@
659 671
660 return $options; 672 return $options;
661 } 673 }
  674 +
  675 + private function parseFilter($filter)
  676 + {
  677 + $regex = '/^(.+)\(#(.+)#\)$/';
  678 + if (preg_match($regex, $filter, $result)) {
  679 + return $result;
  680 + } else {
  681 + return false;
  682 + }
  683 + }
662 } 684 }
663 \ No newline at end of file 685 \ No newline at end of file