Commit 934cd82b264a318a8696263e1dd7d320d5e2da2b
1 parent
5baec053
stock
Showing
4 changed files
with
209 additions
and
2 deletions
Show diff stats
controllers/VariantCountController.php
... | ... | @@ -11,6 +11,8 @@ |
11 | 11 | use yii\filters\VerbFilter; |
12 | 12 | use artbox\catalog\models\Variant; |
13 | 13 | use yii\web\Response; |
14 | + use PHPExcel_IOFactory; | |
15 | + use yii\helpers\ArrayHelper; | |
14 | 16 | |
15 | 17 | /** |
16 | 18 | * VariantCountController implements the CRUD actions for VariantToShop model. |
... | ... | @@ -241,4 +243,97 @@ |
241 | 243 | } |
242 | 244 | return $out; |
243 | 245 | } |
246 | + | |
247 | + | |
248 | + public function actionImport($shop_id){ | |
249 | + return $this->render( | |
250 | + 'import', | |
251 | + [ | |
252 | + "shop_id" => $shop_id, | |
253 | + ] | |
254 | + ); | |
255 | + } | |
256 | + | |
257 | + public function actionUpload($shop_id) | |
258 | + { | |
259 | + \Yii::$app->response->format = Response::FORMAT_JSON; | |
260 | + | |
261 | + $error = false; | |
262 | + $files = []; | |
263 | + | |
264 | + $uploaddir = \Yii::getAlias('@storage/'); | |
265 | + foreach ($_FILES as $file) { | |
266 | + if (move_uploaded_file($file[ 'tmp_name' ], $uploaddir . 'import_stock.xlsx')) { | |
267 | + $files[] = $uploaddir . $file[ 'name' ]; | |
268 | + chmod($uploaddir . 'import_stock.xlsx', 0777); | |
269 | + } else { | |
270 | + $error = true; | |
271 | + } | |
272 | + } | |
273 | + | |
274 | + $data = ( $error ) ? [ 'error' => 'There was an error uploading your files' ] : [ 'files' => $files ]; | |
275 | + | |
276 | + $this->populateImportTable($shop_id); | |
277 | + | |
278 | + return $data; | |
279 | + } | |
280 | + | |
281 | + | |
282 | + protected function populateImportTable($shop_id) | |
283 | + { | |
284 | + $xlsx = PHPExcel_IOFactory::load(\Yii::getAlias('@storage/import_stock.xlsx')); | |
285 | + $xlsx->setActiveSheetIndex(0); | |
286 | + $sheet = $xlsx->getActiveSheet(); | |
287 | + $rowIterator = $sheet->getRowIterator(); | |
288 | + $j = 0; | |
289 | + $count = []; $sku = []; | |
290 | + foreach ($rowIterator as $row) { | |
291 | + $j++; | |
292 | + $cellIterator = $row->getCellIterator(); | |
293 | + $row = []; | |
294 | + $i = 0; | |
295 | + foreach ($cellIterator as $cell) { | |
296 | + /** | |
297 | + * @var \PHPExcel_Cell $cell | |
298 | + */ | |
299 | + $i++; | |
300 | + $row[ $i ] = $cell->getValue(); | |
301 | + if ($i > 1) { | |
302 | + break; | |
303 | + } | |
304 | + } | |
305 | + $sku[] = $row[1]; | |
306 | + if (is_numeric($row[2])){ | |
307 | + $count[] = $row[2]; | |
308 | + } | |
309 | + | |
310 | + | |
311 | + if (empty($row[ 1 ])) { | |
312 | + break; | |
313 | + } | |
314 | + | |
315 | + } | |
316 | + $ids = Variant::find()->select('id')->where(['in', 'sku', $sku])->asArray()->column(); | |
317 | + $insert = []; | |
318 | + foreach ($ids as $key => $val){ | |
319 | + $insert[$key] = [$val,$count[$key], $shop_id]; | |
320 | + } | |
321 | + VariantToShop::deleteAll(['shop_id' => $shop_id]); | |
322 | + | |
323 | + $db = \Yii::$app->db; | |
324 | + | |
325 | + | |
326 | + $db->createCommand() | |
327 | + ->batchInsert( | |
328 | + 'variant_to_shop', | |
329 | + [ | |
330 | + 'variant_id', | |
331 | + 'count', | |
332 | + 'shop_id' | |
333 | + ], | |
334 | + $insert | |
335 | + ) | |
336 | + ->execute(); | |
337 | + | |
338 | + } | |
244 | 339 | } | ... | ... |
messages/ru/stock.php
1 | +<?php | |
2 | + | |
3 | + /** | |
4 | + * @var $sheet PHPExcel_Worksheet | |
5 | + * @var $this View | |
6 | + * @var $shop_id integer | |
7 | + */ | |
8 | + | |
9 | + | |
10 | + use kartik\file\FileInput; | |
11 | + use yii\helpers\Html; | |
12 | + use yii\web\View; | |
13 | + use yii\widgets\ActiveForm; | |
14 | + use yiister\gentelella\widgets\Panel; | |
15 | + | |
16 | + $js = <<< JS | |
17 | +$(document).on('submit', '#my-form', function(e) { | |
18 | + var formData = new FormData(this); | |
19 | + var form = $(this); | |
20 | + | |
21 | + $(document.body).append('<div class="animated yt-loader"></div>'); | |
22 | + | |
23 | + $.ajax({ | |
24 | + url: "/admin/variant-count/upload?shop_id="+$(".shop").val(), | |
25 | + data: formData, | |
26 | + type: "POST", | |
27 | + success: function(data) { | |
28 | + console.log(data); | |
29 | + $('.yt-loader').remove(); | |
30 | + new PNotify({ | |
31 | + title: "Success", | |
32 | + text: "File updated", | |
33 | + type: "success", | |
34 | + styling: "bootstrap3", | |
35 | + icon: "glyphicon glyphicon-exclamation-sign" | |
36 | + }); | |
37 | + }, | |
38 | + cache: false, | |
39 | + contentType: false, | |
40 | + processData: false | |
41 | + }); | |
42 | + | |
43 | + e.preventDefault(); | |
44 | +}); | |
45 | +JS; | |
46 | + | |
47 | + $this->registerJs($js, View::POS_READY); | |
48 | + | |
49 | + $this->params[ 'breadcrumbs' ][] = \Yii::t('stock', 'Import'); | |
50 | +?> | |
51 | + | |
52 | +<?php $panel = Panel::begin() ?> | |
53 | + | |
54 | +<?php $form = ActiveForm::begin( | |
55 | + [ | |
56 | + 'options' => [ | |
57 | + 'enctype' => 'multipart/form-data', | |
58 | + ], | |
59 | + 'id' => 'my-form', | |
60 | + 'action' => 'import/upload', | |
61 | + ] | |
62 | +) ?> | |
63 | + | |
64 | +<?php | |
65 | + echo '<label class="control-label">' . \Yii::t('catalog', 'Upload Document') . '</label>'; | |
66 | + echo FileInput::widget( | |
67 | + [ | |
68 | + 'name' => 'attachment_3', | |
69 | + ] | |
70 | + ); | |
71 | +?> | |
72 | + | |
73 | +<?php echo Html::hiddenInput('shop_id', $shop_id, [ | |
74 | + "class" => "shop" | |
75 | + ]) | |
76 | +?> | |
77 | + | |
78 | +<?= Html::submitButton( | |
79 | + \Yii::t('catalog', 'Send'), | |
80 | + [ | |
81 | + 'class' => 'btn btn-success', | |
82 | + ] | |
83 | +) ?> | |
84 | + | |
85 | + | |
86 | + | |
87 | +<div id="progress-container"> | |
88 | + | |
89 | +</div> | |
90 | + | |
91 | +<?php $form::end() ?> | |
92 | + | |
93 | +<?php $panel::end() ?> | |
94 | + | ... | ... |
views/variant-count/index.php
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 | use yii\helpers\Html; |
4 | 4 | use yii\grid\GridView; |
5 | 5 | use yiister\gentelella\widgets\Panel; |
6 | + use yii\helpers\Url; | |
6 | 7 | |
7 | 8 | /* @var $this yii\web\View */ |
8 | 9 | /* @var $dataProvider yii\data\ActiveDataProvider */ |
... | ... | @@ -29,6 +30,14 @@ |
29 | 30 | ], |
30 | 31 | [ 'class' => 'btn btn-success' ] |
31 | 32 | ) ?> |
33 | + | |
34 | + <?= Html::a(\Yii::t('stock', 'Import'), | |
35 | + [ | |
36 | + 'import', | |
37 | + 'shop_id' => $shop_id, | |
38 | + ], | |
39 | + [ 'class' => 'btn btn-success' ] | |
40 | + ) ?> | |
32 | 41 | </p> |
33 | 42 | <?= GridView::widget( |
34 | 43 | [ |
... | ... | @@ -45,7 +54,16 @@ |
45 | 54 | ], |
46 | 55 | 'count', |
47 | 56 | |
48 | - [ 'class' => 'yii\grid\ActionColumn' ], | |
57 | + [ 'class' => 'yii\grid\ActionColumn', | |
58 | + 'template' => '{view} {update} {delete}{link}', | |
59 | + 'buttons' => [ | |
60 | + 'link' => function ($url,$model,$key) { | |
61 | + return Html::a('<span class="fa fa-download transfer" title="Перенести на другой склад"> </span>'); | |
62 | + }, | |
63 | + 'icon' => 'gift', | |
64 | + ], | |
65 | + | |
66 | + ], | |
49 | 67 | ], |
50 | 68 | ] |
51 | 69 | ); ?> | ... | ... |