diff --git a/controllers/VariantCountController.php b/controllers/VariantCountController.php new file mode 100644 index 0000000..351b95f --- /dev/null +++ b/controllers/VariantCountController.php @@ -0,0 +1,244 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => [ 'POST' ], + ], + ], + ]; + } + + /** + * Lists all VariantToShop models. + * @param int|null $shop_id + * @return mixed + */ + public function actionIndex($shop_id) + { + $dataProvider = new ActiveDataProvider( + [ + 'query' => VariantToShop::find() + ->with( + [ + 'variant', + 'variant.lang', + 'shop', + 'shop.lang', + ] + ) + ->where([ 'shop_id' => $shop_id ]), + ] + ); + + return $this->render( + 'index', + [ + 'dataProvider' => $dataProvider, + 'shop_id' => $shop_id, + ] + ); + } + + /** + * Displays a single VariantToShop model. + * + * @param integer $variant_id + * @param integer $shop_id + * + * @return mixed + */ + public function actionView($variant_id, $shop_id) + { + return $this->render( + 'view', + [ + 'model' => $this->findModel($variant_id, $shop_id), + ] + ); + } + + /** + * Creates a new VariantToShop model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @param int|null $shop_id + * @return mixed + */ + public function actionCreate($shop_id) + { + $model = new VariantToShop(); + $shop = Shop::find() + ->with([ 'lang' ]) + ->where([ 'id' => $shop_id ]) + ->one(); + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect( + [ + 'view', + 'variant_id' => $model->variant_id, + 'shop_id' => $model->shop_id, + ] + ); + } else { + return $this->render( + 'create', + [ + 'model' => $model, + 'shop' => $shop, + ] + ); + } + } + + /** + * Updates an existing VariantToShop model. + * If update is successful, the browser will be redirected to the 'view' page. + * + * @param integer $variant_id + * @param integer $shop_id + * + * @return mixed + */ + public function actionUpdate($variant_id, $shop_id) + { + $model = $this->findModel($variant_id, $shop_id); + $shop = Shop::find() + ->with([ 'lang' ]) + ->where([ 'id' => $shop_id ]) + ->one(); + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect( + [ + 'view', + 'variant_id' => $model->variant_id, + 'shop_id' => $model->shop_id, + ] + ); + } else { + return $this->render( + 'update', + [ + 'model' => $model, + 'shop' => $shop, + ] + ); + } + } + + /** + * Deletes an existing VariantToShop model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * + * @param integer $variant_id + * @param integer $shop_id + * + * @return mixed + */ + public function actionDelete($variant_id, $shop_id) + { + $this->findModel($variant_id, $shop_id) + ->delete(); + + return $this->redirect([ 'index' ]); + } + + /** + * Finds the VariantToShop model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * + * @param integer $variant_id + * @param integer $shop_id + * + * @return VariantToShop the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($variant_id, $shop_id) + { + if (( $model = VariantToShop::findOne( + [ + 'variant_id' => $variant_id, + 'shop_id' => $shop_id, + ] + ) ) !== null + ) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } + + /** + * @param string $q + * @param int|null $shop_id + * + * @return array + */ + + public function actionList(string $q = null, $shop_id) + { + \Yii::$app->response->format = Response::FORMAT_JSON; + $out = [ + 'results' => [ + 'id' => '', + 'text' => '', + ], + ]; + if (!is_null($q)) { + $not = VariantToShop::find() + ->select('variant_id') + ->where([ 'shop_id' => $shop_id ]); + $out[ 'results' ] = Variant::find() + ->select( + [ + 'variant.id as id', + 'variant.sku as text', + ] + ) + ->where( + [ + 'like', + 'variant.sku', + $q, + ] + ) + ->where( + [ + 'NOT IN', + 'variant.id', + $not, + ] + ) + ->limit(20) + ->asArray() + ->all(); + } + return $out; + } + } diff --git a/messages/ru/stock.php b/messages/ru/stock.php index 3bf82bf..26b50ca 100644 --- a/messages/ru/stock.php +++ b/messages/ru/stock.php @@ -1,25 +1,31 @@ 'Города', - 'Shops' => 'Магазины/склады', - 'Title' => 'Название', - 'Sort' => 'Сортировка', - 'Status' => 'Статус', - 'Description' => 'Описание', - 'Create City' => 'Добавить город', - 'Alias' => 'Алиас', - 'Update' => 'Обновить', - 'Address' => 'Адрес', - 'Create Shop' => 'Добавить магазин', - 'Mode' => "Расписание работы", - 'Delete' => 'Удалить', - 'Create' => "Добавить", - 'Mon' => 'Пн', - 'Tue' => 'Вт', - 'Wed' => 'Ср', - 'Thu' => 'Чт', - 'Fri' => 'Пт', - 'Sat' => 'Сб', - 'Sun' => 'Вс', + 'Cities' => 'Города', + 'Shops' => 'Магазины/склады', + 'Title' => 'Название', + 'Sort' => 'Сортировка', + 'Status' => 'Статус', + 'Description' => 'Описание', + 'Create City' => 'Добавить город', + 'Alias' => 'Алиас', + 'Update' => 'Обновить', + 'Address' => 'Адрес', + 'Create Shop' => 'Добавить магазин', + 'Mode' => "Расписание работы", + 'Delete' => 'Удалить', + 'Create' => "Добавить", + 'Mon' => 'Пн', + 'Tue' => 'Вт', + 'Wed' => 'Ср', + 'Thu' => 'Чт', + 'Fri' => 'Пт', + 'Sat' => 'Сб', + 'Sun' => 'Вс', + 'Import' => 'Импорт', + 'Variant Count' => 'Количество товаров', + 'Sku' => 'Код товара', + 'Count' => 'Количество', + 'Add Product' => 'Добавить товар', + 'Update Count' => 'Обновить количество', ] ?> \ No newline at end of file diff --git a/migrations/m170727_081025_create_variant_to_shop_table.php b/migrations/m170727_081025_create_variant_to_shop_table.php new file mode 100644 index 0000000..6bea1c2 --- /dev/null +++ b/migrations/m170727_081025_create_variant_to_shop_table.php @@ -0,0 +1,70 @@ +createTable('variant_to_shop', [ + 'variant_id' => $this->integer() + ->notNull(), + 'shop_id' => $this->integer() + ->notNull(), + 'count' => $this->integer()->defaultValue(0), + ]); + + + $this->addPrimaryKey( + 'variant_to_shop_pk', + 'variant_to_shop', + [ + 'variant_id', + 'shop_id', + ] + ); + + $this->addForeignKey( + 'variant_to_shop_variant_id_to_variant_fk', + 'variant_to_shop', + 'variant_id', + 'variant', + 'id', + 'CASCADE', + 'CASCADE' + ); + + $this->addForeignKey( + 'variant_to_shop_shop_id_to_shop_fk', + 'variant_to_shop', + 'shop_id', + 'shop', + 'id', + 'CASCADE', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropForeignKey( + 'variant_to_shop_variant_id_to_variant_fk', + 'variant_to_shop' + ); + $this->dropForeignKey( + 'variant_to_shop_shop_id_to_shop_fk', + 'variant_to_shop' + ); + $this->dropTable('variant_to_shop'); + + } +} diff --git a/models/VariantToShop.php b/models/VariantToShop.php new file mode 100644 index 0000000..2939a0c --- /dev/null +++ b/models/VariantToShop.php @@ -0,0 +1,93 @@ + true, + 'targetClass' => Shop::className(), + 'targetAttribute' => [ 'shop_id' => 'id' ], + ], + [ + [ 'variant_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Variant::className(), + 'targetAttribute' => [ 'variant_id' => 'id' ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'variant_id' => 'Variant ID', + 'shop_id' => 'Shop ID', + 'count' => \Yii::t('stock', 'Count'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getShop() + { + return $this->hasOne(Shop::className(), [ 'id' => 'shop_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getVariant() + { + return $this->hasOne(Variant::className(), [ 'id' => 'variant_id' ]); + } + + } diff --git a/views/shop/index.php b/views/shop/index.php index ae2c1bb..9d3e530 100644 --- a/views/shop/index.php +++ b/views/shop/index.php @@ -3,6 +3,8 @@ 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 */ @@ -37,7 +39,18 @@ 'sort', 'status:boolean', - [ 'class' => 'yii\grid\ActionColumn' ], + [ 'class' => 'yii\grid\ActionColumn', + 'template' => '{view} {update} {delete}{link}', + 'buttons' => [ + 'link' => function ($url,$model,$key) { + return Html::a('Товары', Url::to([ + '/variant-count/index', + 'shop_id' => $model->id + ])); + }, + 'icon' => 'gift', + ], + ], ], ] ); ?> diff --git a/views/variant-count/_form.php b/views/variant-count/_form.php new file mode 100644 index 0000000..1e83246 --- /dev/null +++ b/views/variant-count/_form.php @@ -0,0 +1,82 @@ + + +
+ + + + 'VariantToShop[variant_id]', + 'value' => ( isset($model->variant->id) ) ? array_keys([ $model->variant->id ]) : [], + 'data' => ( isset($model->variant->sku) ) ? [ $model->variant->sku ] : [], + 'options' => [ + 'placeholder' => 'Выберите товар по коду ...', + 'multiple' => false, + 'disabled' => ( isset($model->variant->id) and isset($model->variant->sku) ) ? true : false, + ], + 'pluginOptions' => [ + 'allowClear' => true, + 'ajax' => [ + 'url' => Url::to([ '/variant-count/list' ]), + 'dataType' => 'json', + 'data' => new JsExpression( + 'function(params) { + return { + q:params.term,shop_id:' . $shop->id . ' + }; + }' + ), + ], + 'escapeMarkup' => new JsExpression( + 'function (markup) { + return markup; + }' + ), + 'templateResult' => new JsExpression( + 'function (variant) { + return variant.text; + }' + ), + 'templateSelection' => new JsExpression( + 'function (variant) { + return variant.text; + }' + ), + ], + ] + ); ?> + + field($model, 'shop_id') + ->hiddenInput( + [ + 'value' => $shop->id, + 'id' => 'shop_id', + ] + ) + ->label(false); ?> + + field($model, 'count') + ->textInput() ?> + +
+ isNewRecord ? \Yii::t('stock', 'Create') : \Yii::t('stock', 'Update'), + [ 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary' ] + ) ?> +
+ + + +
diff --git a/views/variant-count/create.php b/views/variant-count/create.php new file mode 100644 index 0000000..75988da --- /dev/null +++ b/views/variant-count/create.php @@ -0,0 +1,39 @@ +title = \Yii::t('stock', 'Add Product') . ': ' . $shop->lang->address; + $this->params[ 'breadcrumbs' ][] = [ 'label' => \Yii::t('stock', 'Variant Count'), + 'url' => [ + 'index', + 'shop_id' => $shop->id, + ], + ]; + $this->params[ 'breadcrumbs' ][] = $this->title; +?> +
+ \Yii::t('stock', Html::encode($this->title)), + ] + ); + ?> +

title) ?>

+ + render( + '_form', + [ + 'model' => $model, + 'shop' => $shop, + ] + ) ?> + +
diff --git a/views/variant-count/index.php b/views/variant-count/index.php new file mode 100644 index 0000000..90dcea8 --- /dev/null +++ b/views/variant-count/index.php @@ -0,0 +1,55 @@ +title = \Yii::t('stock', 'Variant Count'); + $this->params[ 'breadcrumbs' ][] = $this->title; +?> +
+ \Yii::t('stock', Html::encode($this->title)), + ] + ); + ?> +

title) ?>

+ +

+ $shop_id, + ], + [ 'class' => 'btn btn-success' ] + ) ?> +

+ $dataProvider, + 'columns' => [ + [ 'class' => 'yii\grid\SerialColumn' ], + [ + 'attribute' => \Yii::t('stock', 'Address'), + 'value' => 'shop.lang.address', + ], + [ + 'attribute' => \Yii::t('stock', 'Sku'), + 'value' => 'variant.sku', + ], + 'count', + + [ 'class' => 'yii\grid\ActionColumn' ], + ], + ] + ); ?> + +
diff --git a/views/variant-count/update.php b/views/variant-count/update.php new file mode 100644 index 0000000..cb65e50 --- /dev/null +++ b/views/variant-count/update.php @@ -0,0 +1,36 @@ +title = \Yii::t('stock', "Update Count") . ': ' . $model->variant->sku; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => 'Variant To Shops', + 'url' => [ 'index' ], + ]; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => $model->variant->sku, + 'url' => [ + 'view', + 'variant_id' => $model->variant_id, + 'shop_id' => $model->shop_id, + ], + ]; + $this->params[ 'breadcrumbs' ][] = \Yii::t('stock', 'Update'); +?> +
+ +

title) ?>

+ + render( + '_form', + [ + 'model' => $model, + 'shop' => $shop, + ] + ) ?> + +
diff --git a/views/variant-count/view.php b/views/variant-count/view.php new file mode 100644 index 0000000..87b5eac --- /dev/null +++ b/views/variant-count/view.php @@ -0,0 +1,70 @@ +title = $model->variant->sku; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => \Yii::t('stock', 'Variant Count'), + 'url' => [ + 'index', + 'shop_id' => $model->shop_id, + ], + ]; + $this->params[ 'breadcrumbs' ][] = $this->title; +?> +
+ \Yii::t('stock', Html::encode($this->title)), + ] + ); + ?> +

title) ?>

+ +

+ $model->variant_id, + 'shop_id' => $model->shop_id, + ], + [ 'class' => 'btn btn-primary' ] + ) ?> + $model->variant_id, + 'shop_id' => $model->shop_id, + ], + [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ] + ) ?> +

+ + $model, + 'attributes' => [ + 'variant.sku', + 'shop.lang.address', + 'count', + ], + ] + ) ?> + +
-- libgit2 0.21.4