diff --git a/CatalogUrlManager.php b/CatalogUrlManager.php index c9161a4..6e1616e 100755 --- a/CatalogUrlManager.php +++ b/CatalogUrlManager.php @@ -64,29 +64,30 @@ throw new HttpException(404, 'Page not found'); } } - } elseif ($paths[ 0 ] == 'product') { - - if (!empty( $paths[ 3 ] )) { - throw new HttpException(404, 'Page not found'); - } - $product = Product::find() - ->joinWith('lang') - ->where([ 'product_lang.alias' => $paths[ 1 ] ]) - ->one(); - $variant = ProductVariant::find() - ->joinWith('lang') - ->where([ 'sku' => $paths[ 2 ] ]) - ->one(); - - if (empty( $variant->id ) || empty( $product->id )) { - throw new HttpException(404, 'Page not found'); - } - $route = 'catalog/product'; - $params = [ - 'product' => $product, - 'variant' => $variant, - ]; } +// elseif ($paths[ 0 ] == 'product') { +// +// if (!empty( $paths[ 3 ] )) { +// throw new HttpException(404, 'Page not found'); +// } +// $product = Product::find() +// ->joinWith('lang') +// ->where([ 'product_lang.alias' => $paths[ 1 ] ]) +// ->one(); +// $variant = ProductVariant::find() +// ->joinWith('lang') +// ->where([ 'sku' => $paths[ 2 ] ]) +// ->one(); +// +// if (empty( $variant->id ) || empty( $product->id )) { +// throw new HttpException(404, 'Page not found'); +// } +// $route = 'catalog/product'; +// $params = [ +// 'product' => $paths[1], +// 'variant' => $variant, +// ]; +// } return [ $route, diff --git a/controllers/OrderController.php b/controllers/OrderController.php index bb72098..72db562 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -3,7 +3,9 @@ namespace artweb\artbox\ecommerce\controllers; use artweb\artbox\ecommerce\models\OrderSearch; + use phpDocumentor\Reflection\Types\Null_; use Yii; + use yii\helpers\ArrayHelper; use yii\helpers\Json; use yii\web\Controller; use yii\filters\VerbFilter; @@ -103,43 +105,99 @@ $model->save(); } - public function actionDelete() + public function actionDelete($id) { - $model = Order::findOne($_GET[ 'id' ]); - $model->delete(); - return Yii::$app->response->redirect([ '/order/index' ]); + $model = OrderProduct::findOne($id); + $orderId = $model->order_id; + if ($model->delete()) { + return $this->actionUpdate($orderId); + } } public function actionAdd() { - $model = new OrderProduct(); - if ($model->load(Yii::$app->request->post())) { - /** - * @var ProductVariant $modelMod - */ - if (!$modelMod = ProductVariant::find() - ->with('product.lang') - ->with('lang') - ->where([ 'sku' => $model->sku ]) - ->one() - ) { - throw new HttpException(404, 'Данного артикля не существует!'); + if (!empty( \Yii::$app->request->post() )) { + $id = \Yii::$app->request->post('OrderProduct')[ 'id' ]; + $order_id = \Yii::$app->request->post('OrderProduct')[ 'order_id' ]; + if(!empty(\Yii::$app->request->post('OrderProduct')[ 'count' ])) { + $count = \Yii::$app->request->post('OrderProduct')[ 'count' ]; + } + else { + $count = 1; + } + $productVariant = ProductVariant::findOne($id); + + $model = OrderProduct::find() + ->where( + [ + 'order_id' => $order_id, + ] + ) + ->andWhere( + [ + 'product_variant_id' => $id, + ] + ) + ->one(); + + if (!empty( $model )) { + $model->count += $count; + } else { + $model = new OrderProduct(); + + $model->order_id = $order_id; + $model->product_variant_id = $productVariant->id; + $model->product_name = $productVariant->product->lang->title; + $model->name = $productVariant->lang->title; + $model->sku = $productVariant->sku; + $model->price = $productVariant->price; + $model->count = $count; } - $model->product_name = $modelMod->product->lang->title; - $model->name = $modelMod->lang->title; - $model->sku = $modelMod->sku; - $model->price = $modelMod->price; - $model->sum_cost = $model->count * $modelMod->price; - $model->product_variant_id = $modelMod->id; - $model->save(); - //return Yii::$app->response->redirect(['/admin/order/show','id'=>$_GET['order_id']]); + \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + + if ($model->save()) { + return [ 'status' => 'success' ]; + } else { + return [ 'status' => 'fail' ]; + } + + } else { + throw new NotFoundHttpException(); } - - //return $this->render('add',['model'=>$model]); } public function actionCreate() { + if (\Yii::$app->request->post('hasEditable')) { + $orderProductId = \Yii::$app->request->post('editableKey'); + $orderProduct = OrderProduct::findOne($orderProductId); + $out = Json::encode( + [ + 'output' => '', + 'message' => '', + ] + ); + + $posted = current(\Yii::$app->request->post('OrderProduct')); + $post = [ 'OrderProduct' => $posted ]; + + if ($orderProduct->load($post)) { + $orderProduct->save(); + $output = ''; + if (isset( $posted[ 'count' ] )) { + $output = Yii::$app->formatter->asDecimal($orderProduct->count, 0); + } + $out = Json::encode( + [ + 'output' => $output, + 'message' => '', + ] + ); + } + + return $out; + } + $model = new Order(); $dataProvider = new ActiveDataProvider( @@ -163,25 +221,33 @@ public function actionUpdate($id) { - if (\Yii::$app->request->post('hasEditable')) - { + if (\Yii::$app->request->post('hasEditable')) { $orderProductId = \Yii::$app->request->post('editableKey'); $orderProduct = OrderProduct::findOne($orderProductId); - $out = Json::encode(['output'=>'', 'message'=>'']); - + $out = Json::encode( + [ + 'output' => '', + 'message' => '', + ] + ); + $posted = current(\Yii::$app->request->post('OrderProduct')); - $post = ['OrderProduct' => $posted]; + $post = [ 'OrderProduct' => $posted ]; - if ($orderProduct->load($post)) - { + if ($orderProduct->load($post)) { $orderProduct->save(); $output = ''; - if (isset($posted['count'])) { + if (isset( $posted[ 'count' ] )) { $output = Yii::$app->formatter->asDecimal($orderProduct->count, 0); } - $out = Json::encode(['output'=>$output, 'message'=>'']); + $out = Json::encode( + [ + 'output' => $output, + 'message' => '', + ] + ); } - + return $out; } @@ -206,17 +272,46 @@ } } - public function actionDeleteProduct($id, $order_id) + public function actionFindProduct($q = NULL, $id = NULL) { - $model = OrderProduct::findOne($id); - $model->delete(); \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; - return [ - 'result' => 'success', - 'id' => $id, + $out = [ + 'results' => [ + 'id' => '', + 'text' => '', + ], ]; + if (!is_null($q)) { + $result = ProductVariant::find() + ->select('id, sku') + ->where( + [ + 'like', + 'sku', + $q, + ] + ) + ->limit(20) + ->asArray() + ->all(); + + $out[ 'results' ] = $result; + } + + return $out; } + // public function actionDeleteProduct($id, $order_id) + // { + // $model = OrderProduct::findOne($id); + // $model->delete(); + // \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + // return [ + // 'result' => 'success', + // 'id' => $id, + // ]; + // } + protected function findModel($id) { if (( $model = Order::findOne($id) ) !== NULL) { diff --git a/helpers/ProductHelper.php b/helpers/ProductHelper.php index 2962c6a..a688d30 100755 --- a/helpers/ProductHelper.php +++ b/helpers/ProductHelper.php @@ -177,7 +177,7 @@ * * @return int */ - public static function getLastCategory(): int + public static function getLastCategory() { return \Yii::$app->session->get('last_category_id'); } diff --git a/models/CategoryLang.php b/models/CategoryLang.php index 03354aa..4d8cd42 100755 --- a/models/CategoryLang.php +++ b/models/CategoryLang.php @@ -18,6 +18,7 @@ * @property string $seo_text * @property string $h1 * @property string $category_synonym + * @property string $alias * @property Category $category * @property Language $language */ @@ -121,7 +122,7 @@ 'meta_description' => Yii::t('app', 'Meta Desc'), 'seo_text' => Yii::t('app', 'Seo Text'), 'h1' => Yii::t('app', 'H1'), - 'category_synonym' => Yii::t('app', 'Synonym') + 'category_synonym' => Yii::t('app', 'Synonym'), ]; } diff --git a/models/OrderSearch.php b/models/OrderSearch.php index 3d9bb9f..94364de 100755 --- a/models/OrderSearch.php +++ b/models/OrderSearch.php @@ -10,6 +10,9 @@ */ class OrderSearch extends Order { + public $date_from; + public $date_to; + public $date_range; /** * @inheritdoc @@ -32,6 +35,9 @@ 'name', 'email', 'phone', + 'date_from', + 'date_to', + 'date_range', ], 'safe', ], @@ -67,7 +73,7 @@ ); $this->load($params); - + if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); @@ -107,6 +113,9 @@ ] ); + $query->andFilterWhere(['>=', 'date_time', $this->date_from]); + $query->andFilterWhere(['<=', 'date_time', $this->date_to]); + return $dataProvider; } } diff --git a/views/order/_form.php b/views/order/_form.php index 04c7271..9e07a01 100755 --- a/views/order/_form.php +++ b/views/order/_form.php @@ -1,14 +1,17 @@ registerJs($js, View::POS_READY); ?> @@ -103,14 +128,16 @@ - + +
$dataProvider, + 'layout' => '{items}{pager}', 'columns' => [ 'id', 'product_name', @@ -128,25 +155,97 @@ ], ], ], - 'hAlign' => 'right', - 'vAlign' => 'middle', - 'width' => '7%', 'format' => [ 'decimal', - 2, + 0, ], - 'pageSummary' => true, + 'pageSummary' => false, ], [ - 'class' => 'yii\grid\ActionColumn', + 'class' => 'yii\grid\ActionColumn', 'template' => '{delete}', ], ], 'responsive' => true, 'hover' => true, + 'pjax' => true, + 'pjaxSettings' => [ + 'options' => [ + 'scrollTo' => 'false', + 'id' => 'order-products-grid', + ], + ], ] ); ?>
+
+
+ yii\helpers\Url::to([ 'add' ]), + 'id' => 'add-product-form', + ] + ); + $newOrderProduct = new OrderProduct(); + ?> +
+ field($newOrderProduct, 'id') + ->widget( + Select2::className(), + [ + 'options' => [ 'placeholder' => 'Search for a product ...' ], + 'pluginOptions' => [ + 'allowClear' => true, + 'disabled' => $model->isNewRecord ? true : false, + 'minimumInputLength' => 3, + 'language' => [ + 'errorLoading' => new JsExpression( + "function () { return 'Waiting for results...'; }" + ), + ], + 'ajax' => [ + 'url' => \yii\helpers\Url::to([ 'find-product' ]), + 'dataType' => 'json', + 'data' => new JsExpression( + 'function(params) { return {q:params.term}; }' + ), + ], + 'escapeMarkup' => new JsExpression( + 'function (markup) { return markup; }' + ), + 'templateResult' => new JsExpression( + 'function(data) { return data.sku; }' + ), + 'templateSelection' => new JsExpression( + 'function (data) { return data.sku; }' + ), + ], + ] + ); + + ?> +
+
+ field($newOrderProduct, 'count')->input('number'); ?> +
+
+ $model->isNewRecord ? 'btn btn-primary disabled' : 'btn btn-primary', + ] + ) ?> +
+ field($newOrderProduct, 'order_id') + ->hiddenInput( + [ + 'value' => $model->id, + ] + ) + ->label(false) ?> + +
+
diff --git a/views/order/index.php b/views/order/index.php index 549b7fd..c30d445 100755 --- a/views/order/index.php +++ b/views/order/index.php @@ -1,107 +1,51 @@ title = 'Заказы'; $this->params[ 'breadcrumbs' ][] = $this->title; ?> -

Заказы

-

- 'btn btn-success' ]) ?> -

- +

Заказы

+

+ 'btn btn-success' ]) ?> +

+ $dataProvider, 'filterModel' => $searchModel, 'columns' => [ - - [ - 'attribute' => 'id', - 'format' => 'raw', - 'options' => [ 'class' => 'btn btn-warning' ], - 'value' => function ($model) { - return Html::button($model->id, - [ - 'id' => $model->id, - 'class' => 'btn btn-warning', - ] - ); - - }, - - ], + 'id', [ 'attribute' => 'date_time', - 'value' => 'date_time', - ], - - [ - 'attribute' => 'name', - 'value' => 'name', - 'format' => 'raw', - ], - [ - 'attribute' => 'phone', - 'value' => 'phone', - ], - // [ - // 'attribute' => 'total', - // 'value'=>'total', - // ], - // [ - // 'filter' => yii\helpers\ArrayHelper::map(Label::find()->orderBy('id')->asArray()->all(), 'id', 'label'), - // 'attribute' => 'label', - // 'value' => function ($model, $key, $index, $column) { - // return Html::activeDropDownList($model, 'label', - // yii\helpers\ArrayHelper::map(Label::find()->orderBy('id')->asArray()->all(), 'id', 'label'), - // [ - // 'prompt' => 'Нет', - // 'onchange' => "$.ajax({ - // url: \"/admin/order/label-update\", - // type: \"post\", - // data: { order_id: $model->id, label_id : this.value}, - // });" - // ] - // - // ); - // }, - // 'format' => 'raw', - // ], - // [ - // 'attribute' => 'pay', - // 'filter' => [ - // 0 => 'Нет',1=>'Да' - // ], - // 'value' => function ($model, $key, $index, $column) { - // return Html::activeDropDownList($model, 'pay',[0 => 'Нет',1=>'Да'], - // [ - // 'onchange' => "$.ajax({ - // url: \"/admin/order/pay-update\", - // type: \"post\", - // data: { order_id: $model->id, pay_id : this.value}, - // });" - // ] - // - // ); - // }, - // 'format' => 'raw', - // ], - [ - 'attribute' => 'status', - 'value' => 'status', - 'contentOptions' => [ 'style' => 'width: 5px;' ], + 'filter' => DateRangePicker::widget( + [ + 'name' => 'OrderSearch[date_range]', + 'model' => $searchModel, + 'convertFormat' => false, + 'pluginOptions' => [ + 'format' => 'YYYY-MM-DD', + 'opens' => 'left', + ], + ] + ), ], + 'name', + 'phone', + 'status', [ - 'class' => 'yii\grid\ActionColumn', -// 'template' => '{delete}', - 'contentOptions' => [ 'style' => 'width: 70px;' ], + 'class' => 'yii\grid\ActionColumn', ], ], ] ) ?> - \ No newline at end of file -- libgit2 0.21.4