diff --git a/controllers/VariantCountController.php b/controllers/VariantCountController.php index 351b95f..38c09b0 100644 --- a/controllers/VariantCountController.php +++ b/controllers/VariantCountController.php @@ -11,6 +11,8 @@ use yii\filters\VerbFilter; use artbox\catalog\models\Variant; use yii\web\Response; + use PHPExcel_IOFactory; + use yii\helpers\ArrayHelper; /** * VariantCountController implements the CRUD actions for VariantToShop model. @@ -241,4 +243,97 @@ } return $out; } + + + public function actionImport($shop_id){ + return $this->render( + 'import', + [ + "shop_id" => $shop_id, + ] + ); + } + + public function actionUpload($shop_id) + { + \Yii::$app->response->format = Response::FORMAT_JSON; + + $error = false; + $files = []; + + $uploaddir = \Yii::getAlias('@storage/'); + foreach ($_FILES as $file) { + if (move_uploaded_file($file[ 'tmp_name' ], $uploaddir . 'import_stock.xlsx')) { + $files[] = $uploaddir . $file[ 'name' ]; + chmod($uploaddir . 'import_stock.xlsx', 0777); + } else { + $error = true; + } + } + + $data = ( $error ) ? [ 'error' => 'There was an error uploading your files' ] : [ 'files' => $files ]; + + $this->populateImportTable($shop_id); + + return $data; + } + + + protected function populateImportTable($shop_id) + { + $xlsx = PHPExcel_IOFactory::load(\Yii::getAlias('@storage/import_stock.xlsx')); + $xlsx->setActiveSheetIndex(0); + $sheet = $xlsx->getActiveSheet(); + $rowIterator = $sheet->getRowIterator(); + $j = 0; + $count = []; $sku = []; + foreach ($rowIterator as $row) { + $j++; + $cellIterator = $row->getCellIterator(); + $row = []; + $i = 0; + foreach ($cellIterator as $cell) { + /** + * @var \PHPExcel_Cell $cell + */ + $i++; + $row[ $i ] = $cell->getValue(); + if ($i > 1) { + break; + } + } + $sku[] = $row[1]; + if (is_numeric($row[2])){ + $count[] = $row[2]; + } + + + if (empty($row[ 1 ])) { + break; + } + + } + $ids = Variant::find()->select('id')->where(['in', 'sku', $sku])->asArray()->column(); + $insert = []; + foreach ($ids as $key => $val){ + $insert[$key] = [$val,$count[$key], $shop_id]; + } + VariantToShop::deleteAll(['shop_id' => $shop_id]); + + $db = \Yii::$app->db; + + + $db->createCommand() + ->batchInsert( + 'variant_to_shop', + [ + 'variant_id', + 'count', + 'shop_id' + ], + $insert + ) + ->execute(); + + } } diff --git a/messages/ru/stock.php b/messages/ru/stock.php index 4097824..6573a77 100644 --- a/messages/ru/stock.php +++ b/messages/ru/stock.php @@ -27,6 +27,6 @@ 'Count' => 'Количество', 'Add Product' => 'Добавить товар', 'Update Count' => 'Обновить количество', - 'Shop' => 'Магазин (показывать на сайте)' + 'Shop' => 'Магазин (показывать на сайте)', ] ?> \ No newline at end of file diff --git a/views/variant-count/import.php b/views/variant-count/import.php new file mode 100644 index 0000000..2499fd7 --- /dev/null +++ b/views/variant-count/import.php @@ -0,0 +1,94 @@ +'); + + $.ajax({ + url: "/admin/variant-count/upload?shop_id="+$(".shop").val(), + data: formData, + type: "POST", + success: function(data) { + console.log(data); + $('.yt-loader').remove(); + new PNotify({ + title: "Success", + text: "File updated", + type: "success", + styling: "bootstrap3", + icon: "glyphicon glyphicon-exclamation-sign" + }); + }, + cache: false, + contentType: false, + processData: false + }); + + e.preventDefault(); +}); +JS; + + $this->registerJs($js, View::POS_READY); + + $this->params[ 'breadcrumbs' ][] = \Yii::t('stock', 'Import'); +?> + + + + [ + 'enctype' => 'multipart/form-data', + ], + 'id' => 'my-form', + 'action' => 'import/upload', + ] +) ?> + +' . \Yii::t('catalog', 'Upload Document') . ''; + echo FileInput::widget( + [ + 'name' => 'attachment_3', + ] + ); +?> + + "shop" + ]) +?> + += Html::submitButton( + \Yii::t('catalog', 'Send'), + [ + 'class' => 'btn btn-success', + ] +) ?> + + + +