From 934cd82b264a318a8696263e1dd7d320d5e2da2b Mon Sep 17 00:00:00 2001
From: stes
Date: Tue, 1 Aug 2017 14:13:41 +0300
Subject: [PATCH] stock
---
controllers/VariantCountController.php | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
messages/ru/stock.php | 2 +-
views/variant-count/import.php | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
views/variant-count/index.php | 20 +++++++++++++++++++-
4 files changed, 209 insertions(+), 2 deletions(-)
create mode 100644 views/variant-count/import.php
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',
+ ]
+) ?>
+
+
+
+
+
+
+
+
+
+
+
diff --git a/views/variant-count/index.php b/views/variant-count/index.php
index 90dcea8..7ab51c0 100644
--- a/views/variant-count/index.php
+++ b/views/variant-count/index.php
@@ -3,6 +3,7 @@
use yii\helpers\Html;
use yii\grid\GridView;
use yiister\gentelella\widgets\Panel;
+ use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */
@@ -29,6 +30,14 @@
],
[ 'class' => 'btn btn-success' ]
) ?>
+
+ = Html::a(\Yii::t('stock', 'Import'),
+ [
+ 'import',
+ 'shop_id' => $shop_id,
+ ],
+ [ 'class' => 'btn btn-success' ]
+ ) ?>
= GridView::widget(
[
@@ -45,7 +54,16 @@
],
'count',
- [ 'class' => 'yii\grid\ActionColumn' ],
+ [ 'class' => 'yii\grid\ActionColumn',
+ 'template' => '{view} {update} {delete}{link}',
+ 'buttons' => [
+ 'link' => function ($url,$model,$key) {
+ return Html::a(' ');
+ },
+ 'icon' => 'gift',
+ ],
+
+ ],
],
]
); ?>
--
libgit2 0.21.4