Commit 2ad658238fc5c331352bc39e89f0190e0d70fea8

Authored by Alexey Boroda
1 parent c240a19c

-Added date range widget to grid view, stopped on search model

CatalogUrlManager.php
... ... @@ -64,29 +64,30 @@
64 64 throw new HttpException(404, 'Page not found');
65 65 }
66 66 }
67   - } elseif ($paths[ 0 ] == 'product') {
68   -
69   - if (!empty( $paths[ 3 ] )) {
70   - throw new HttpException(404, 'Page not found');
71   - }
72   - $product = Product::find()
73   - ->joinWith('lang')
74   - ->where([ 'product_lang.alias' => $paths[ 1 ] ])
75   - ->one();
76   - $variant = ProductVariant::find()
77   - ->joinWith('lang')
78   - ->where([ 'sku' => $paths[ 2 ] ])
79   - ->one();
80   -
81   - if (empty( $variant->id ) || empty( $product->id )) {
82   - throw new HttpException(404, 'Page not found');
83   - }
84   - $route = 'catalog/product';
85   - $params = [
86   - 'product' => $product,
87   - 'variant' => $variant,
88   - ];
89 67 }
  68 +// elseif ($paths[ 0 ] == 'product') {
  69 +//
  70 +// if (!empty( $paths[ 3 ] )) {
  71 +// throw new HttpException(404, 'Page not found');
  72 +// }
  73 +// $product = Product::find()
  74 +// ->joinWith('lang')
  75 +// ->where([ 'product_lang.alias' => $paths[ 1 ] ])
  76 +// ->one();
  77 +// $variant = ProductVariant::find()
  78 +// ->joinWith('lang')
  79 +// ->where([ 'sku' => $paths[ 2 ] ])
  80 +// ->one();
  81 +//
  82 +// if (empty( $variant->id ) || empty( $product->id )) {
  83 +// throw new HttpException(404, 'Page not found');
  84 +// }
  85 +// $route = 'catalog/product';
  86 +// $params = [
  87 +// 'product' => $paths[1],
  88 +// 'variant' => $variant,
  89 +// ];
  90 +// }
90 91  
91 92 return [
92 93 $route,
... ...
controllers/OrderController.php
... ... @@ -3,7 +3,9 @@
3 3 namespace artweb\artbox\ecommerce\controllers;
4 4  
5 5 use artweb\artbox\ecommerce\models\OrderSearch;
  6 + use phpDocumentor\Reflection\Types\Null_;
6 7 use Yii;
  8 + use yii\helpers\ArrayHelper;
7 9 use yii\helpers\Json;
8 10 use yii\web\Controller;
9 11 use yii\filters\VerbFilter;
... ... @@ -103,43 +105,99 @@
103 105 $model->save();
104 106 }
105 107  
106   - public function actionDelete()
  108 + public function actionDelete($id)
107 109 {
108   - $model = Order::findOne($_GET[ 'id' ]);
109   - $model->delete();
110   - return Yii::$app->response->redirect([ '/order/index' ]);
  110 + $model = OrderProduct::findOne($id);
  111 + $orderId = $model->order_id;
  112 + if ($model->delete()) {
  113 + return $this->actionUpdate($orderId);
  114 + }
111 115 }
112 116  
113 117 public function actionAdd()
114 118 {
115   - $model = new OrderProduct();
116   - if ($model->load(Yii::$app->request->post())) {
117   - /**
118   - * @var ProductVariant $modelMod
119   - */
120   - if (!$modelMod = ProductVariant::find()
121   - ->with('product.lang')
122   - ->with('lang')
123   - ->where([ 'sku' => $model->sku ])
124   - ->one()
125   - ) {
126   - throw new HttpException(404, 'Данного артикля не существует!');
  119 + if (!empty( \Yii::$app->request->post() )) {
  120 + $id = \Yii::$app->request->post('OrderProduct')[ 'id' ];
  121 + $order_id = \Yii::$app->request->post('OrderProduct')[ 'order_id' ];
  122 + if(!empty(\Yii::$app->request->post('OrderProduct')[ 'count' ])) {
  123 + $count = \Yii::$app->request->post('OrderProduct')[ 'count' ];
  124 + }
  125 + else {
  126 + $count = 1;
  127 + }
  128 + $productVariant = ProductVariant::findOne($id);
  129 +
  130 + $model = OrderProduct::find()
  131 + ->where(
  132 + [
  133 + 'order_id' => $order_id,
  134 + ]
  135 + )
  136 + ->andWhere(
  137 + [
  138 + 'product_variant_id' => $id,
  139 + ]
  140 + )
  141 + ->one();
  142 +
  143 + if (!empty( $model )) {
  144 + $model->count += $count;
  145 + } else {
  146 + $model = new OrderProduct();
  147 +
  148 + $model->order_id = $order_id;
  149 + $model->product_variant_id = $productVariant->id;
  150 + $model->product_name = $productVariant->product->lang->title;
  151 + $model->name = $productVariant->lang->title;
  152 + $model->sku = $productVariant->sku;
  153 + $model->price = $productVariant->price;
  154 + $model->count = $count;
127 155 }
128   - $model->product_name = $modelMod->product->lang->title;
129   - $model->name = $modelMod->lang->title;
130   - $model->sku = $modelMod->sku;
131   - $model->price = $modelMod->price;
132   - $model->sum_cost = $model->count * $modelMod->price;
133   - $model->product_variant_id = $modelMod->id;
134   - $model->save();
135   - //return Yii::$app->response->redirect(['/admin/order/show','id'=>$_GET['order_id']]);
  156 + \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  157 +
  158 + if ($model->save()) {
  159 + return [ 'status' => 'success' ];
  160 + } else {
  161 + return [ 'status' => 'fail' ];
  162 + }
  163 +
  164 + } else {
  165 + throw new NotFoundHttpException();
136 166 }
137   -
138   - //return $this->render('add',['model'=>$model]);
139 167 }
140 168  
141 169 public function actionCreate()
142 170 {
  171 + if (\Yii::$app->request->post('hasEditable')) {
  172 + $orderProductId = \Yii::$app->request->post('editableKey');
  173 + $orderProduct = OrderProduct::findOne($orderProductId);
  174 + $out = Json::encode(
  175 + [
  176 + 'output' => '',
  177 + 'message' => '',
  178 + ]
  179 + );
  180 +
  181 + $posted = current(\Yii::$app->request->post('OrderProduct'));
  182 + $post = [ 'OrderProduct' => $posted ];
  183 +
  184 + if ($orderProduct->load($post)) {
  185 + $orderProduct->save();
  186 + $output = '';
  187 + if (isset( $posted[ 'count' ] )) {
  188 + $output = Yii::$app->formatter->asDecimal($orderProduct->count, 0);
  189 + }
  190 + $out = Json::encode(
  191 + [
  192 + 'output' => $output,
  193 + 'message' => '',
  194 + ]
  195 + );
  196 + }
  197 +
  198 + return $out;
  199 + }
  200 +
143 201 $model = new Order();
144 202  
145 203 $dataProvider = new ActiveDataProvider(
... ... @@ -163,25 +221,33 @@
163 221  
164 222 public function actionUpdate($id)
165 223 {
166   - if (\Yii::$app->request->post('hasEditable'))
167   - {
  224 + if (\Yii::$app->request->post('hasEditable')) {
168 225 $orderProductId = \Yii::$app->request->post('editableKey');
169 226 $orderProduct = OrderProduct::findOne($orderProductId);
170   - $out = Json::encode(['output'=>'', 'message'=>'']);
171   -
  227 + $out = Json::encode(
  228 + [
  229 + 'output' => '',
  230 + 'message' => '',
  231 + ]
  232 + );
  233 +
172 234 $posted = current(\Yii::$app->request->post('OrderProduct'));
173   - $post = ['OrderProduct' => $posted];
  235 + $post = [ 'OrderProduct' => $posted ];
174 236  
175   - if ($orderProduct->load($post))
176   - {
  237 + if ($orderProduct->load($post)) {
177 238 $orderProduct->save();
178 239 $output = '';
179   - if (isset($posted['count'])) {
  240 + if (isset( $posted[ 'count' ] )) {
180 241 $output = Yii::$app->formatter->asDecimal($orderProduct->count, 0);
181 242 }
182   - $out = Json::encode(['output'=>$output, 'message'=>'']);
  243 + $out = Json::encode(
  244 + [
  245 + 'output' => $output,
  246 + 'message' => '',
  247 + ]
  248 + );
183 249 }
184   -
  250 +
185 251 return $out;
186 252 }
187 253  
... ... @@ -206,17 +272,46 @@
206 272 }
207 273 }
208 274  
209   - public function actionDeleteProduct($id, $order_id)
  275 + public function actionFindProduct($q = NULL, $id = NULL)
210 276 {
211   - $model = OrderProduct::findOne($id);
212   - $model->delete();
213 277 \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
214   - return [
215   - 'result' => 'success',
216   - 'id' => $id,
  278 + $out = [
  279 + 'results' => [
  280 + 'id' => '',
  281 + 'text' => '',
  282 + ],
217 283 ];
  284 + if (!is_null($q)) {
  285 + $result = ProductVariant::find()
  286 + ->select('id, sku')
  287 + ->where(
  288 + [
  289 + 'like',
  290 + 'sku',
  291 + $q,
  292 + ]
  293 + )
  294 + ->limit(20)
  295 + ->asArray()
  296 + ->all();
  297 +
  298 + $out[ 'results' ] = $result;
  299 + }
  300 +
  301 + return $out;
218 302 }
219 303  
  304 + // public function actionDeleteProduct($id, $order_id)
  305 + // {
  306 + // $model = OrderProduct::findOne($id);
  307 + // $model->delete();
  308 + // \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  309 + // return [
  310 + // 'result' => 'success',
  311 + // 'id' => $id,
  312 + // ];
  313 + // }
  314 +
220 315 protected function findModel($id)
221 316 {
222 317 if (( $model = Order::findOne($id) ) !== NULL) {
... ...
helpers/ProductHelper.php
... ... @@ -177,7 +177,7 @@
177 177 *
178 178 * @return int
179 179 */
180   - public static function getLastCategory(): int
  180 + public static function getLastCategory()
181 181 {
182 182 return \Yii::$app->session->get('last_category_id');
183 183 }
... ...
models/CategoryLang.php
... ... @@ -18,6 +18,7 @@
18 18 * @property string $seo_text
19 19 * @property string $h1
20 20 * @property string $category_synonym
  21 + * @property string $alias
21 22 * @property Category $category
22 23 * @property Language $language
23 24 */
... ... @@ -121,7 +122,7 @@
121 122 'meta_description' => Yii::t('app', 'Meta Desc'),
122 123 'seo_text' => Yii::t('app', 'Seo Text'),
123 124 'h1' => Yii::t('app', 'H1'),
124   - 'category_synonym' => Yii::t('app', 'Synonym')
  125 + 'category_synonym' => Yii::t('app', 'Synonym'),
125 126 ];
126 127 }
127 128  
... ...
models/OrderSearch.php
... ... @@ -10,6 +10,9 @@
10 10 */
11 11 class OrderSearch extends Order
12 12 {
  13 + public $date_from;
  14 + public $date_to;
  15 + public $date_range;
13 16  
14 17 /**
15 18 * @inheritdoc
... ... @@ -32,6 +35,9 @@
32 35 'name',
33 36 'email',
34 37 'phone',
  38 + 'date_from',
  39 + 'date_to',
  40 + 'date_range',
35 41 ],
36 42 'safe',
37 43 ],
... ... @@ -67,7 +73,7 @@
67 73 );
68 74  
69 75 $this->load($params);
70   -
  76 +
71 77 if (!$this->validate()) {
72 78 // uncomment the following line if you do not want to return any records when validation fails
73 79 // $query->where('0=1');
... ... @@ -107,6 +113,9 @@
107 113 ]
108 114 );
109 115  
  116 + $query->andFilterWhere(['>=', 'date_time', $this->date_from]);
  117 + $query->andFilterWhere(['<=', 'date_time', $this->date_to]);
  118 +
110 119 return $dataProvider;
111 120 }
112 121 }
... ...
views/order/_form.php
1 1 <?php
2 2  
3 3 use artweb\artbox\ecommerce\models\Order;
  4 + use artweb\artbox\ecommerce\models\OrderProduct;
4 5 use frontend\models\OrderFrontend;
5 6 use kartik\grid\GridView;
  7 + use kartik\widgets\Select2;
6 8 use yii\data\ActiveDataProvider;
7 9 use yii\helpers\Html;
8 10 use yii\bootstrap\ActiveForm;
9 11 use yii\helpers\ArrayHelper;
10 12 use artweb\artbox\ecommerce\models\Delivery;
11 13 use yii\web\View;
  14 + use yii\web\JsExpression;
12 15  
13 16 /**
14 17 * @var View $this
... ... @@ -16,6 +19,28 @@
16 19 * @var ActiveForm $form
17 20 * @var ActiveDataProvider $dataProvider
18 21 */
  22 +
  23 + $js = <<< JS
  24 +$(document).on('submit', '#add-product-form', function(e) {
  25 + e.preventDefault();
  26 + var addFormData = $(this).serializeArray();
  27 + var addFormAction = this.action;
  28 + $.ajax({
  29 + url: addFormAction,
  30 + type: "POST",
  31 + data: addFormData,
  32 + success: function (data) {
  33 + if (data.status === "success") {
  34 + $.pjax.reload({container:"#order-products-grid"}); //Reload GridView
  35 + }
  36 + },
  37 + error: function () {
  38 + }
  39 + });
  40 +});
  41 +JS;
  42 +
  43 + $this->registerJs($js, View::POS_READY);
19 44 ?>
20 45  
21 46 <?php $form = ActiveForm::begin(); ?>
... ... @@ -103,14 +128,16 @@
103 128 </div>
104 129 </div>
105 130 </div>
106   -
107 131 <?php ActiveForm::end(); ?>
  132 +
  133 +
108 134 <div class="container">
109 135 <div class="row">
110 136 <?php
111 137 echo GridView::widget(
112 138 [
113 139 'dataProvider' => $dataProvider,
  140 + 'layout' => '{items}{pager}',
114 141 'columns' => [
115 142 'id',
116 143 'product_name',
... ... @@ -128,25 +155,97 @@
128 155 ],
129 156 ],
130 157 ],
131   - 'hAlign' => 'right',
132   - 'vAlign' => 'middle',
133   - 'width' => '7%',
134 158 'format' => [
135 159 'decimal',
136   - 2,
  160 + 0,
137 161 ],
138   - 'pageSummary' => true,
  162 + 'pageSummary' => false,
139 163 ],
140 164 [
141   - 'class' => 'yii\grid\ActionColumn',
  165 + 'class' => 'yii\grid\ActionColumn',
142 166 'template' => '{delete}',
143 167 ],
144 168 ],
145 169 'responsive' => true,
146 170 'hover' => true,
  171 + 'pjax' => true,
  172 + 'pjaxSettings' => [
  173 + 'options' => [
  174 + 'scrollTo' => 'false',
  175 + 'id' => 'order-products-grid',
  176 + ],
  177 + ],
147 178 ]
148 179 );
149 180 ?>
150 181 </div>
151 182 </div>
152 183  
  184 +<div class="container">
  185 + <div class="row">
  186 + <?php $newProductForm = ActiveForm::begin(
  187 + [
  188 + 'action' => yii\helpers\Url::to([ 'add' ]),
  189 + 'id' => 'add-product-form',
  190 + ]
  191 + );
  192 + $newOrderProduct = new OrderProduct();
  193 + ?>
  194 + <div class="col-md-8">
  195 + <?php echo $newProductForm->field($newOrderProduct, 'id')
  196 + ->widget(
  197 + Select2::className(),
  198 + [
  199 + 'options' => [ 'placeholder' => 'Search for a product ...' ],
  200 + 'pluginOptions' => [
  201 + 'allowClear' => true,
  202 + 'disabled' => $model->isNewRecord ? true : false,
  203 + 'minimumInputLength' => 3,
  204 + 'language' => [
  205 + 'errorLoading' => new JsExpression(
  206 + "function () { return 'Waiting for results...'; }"
  207 + ),
  208 + ],
  209 + 'ajax' => [
  210 + 'url' => \yii\helpers\Url::to([ 'find-product' ]),
  211 + 'dataType' => 'json',
  212 + 'data' => new JsExpression(
  213 + 'function(params) { return {q:params.term}; }'
  214 + ),
  215 + ],
  216 + 'escapeMarkup' => new JsExpression(
  217 + 'function (markup) { return markup; }'
  218 + ),
  219 + 'templateResult' => new JsExpression(
  220 + 'function(data) { return data.sku; }'
  221 + ),
  222 + 'templateSelection' => new JsExpression(
  223 + 'function (data) { return data.sku; }'
  224 + ),
  225 + ],
  226 + ]
  227 + );
  228 +
  229 + ?>
  230 + </div>
  231 + <div class="col-md-2">
  232 + <?php echo $newProductForm->field($newOrderProduct, 'count')->input('number'); ?>
  233 + </div>
  234 + <div class="col-md-2">
  235 + <?php echo Html::submitButton(
  236 + 'Add',
  237 + [
  238 + 'class' => $model->isNewRecord ? 'btn btn-primary disabled' : 'btn btn-primary',
  239 + ]
  240 + ) ?>
  241 + </div>
  242 + <?php echo $newProductForm->field($newOrderProduct, 'order_id')
  243 + ->hiddenInput(
  244 + [
  245 + 'value' => $model->id,
  246 + ]
  247 + )
  248 + ->label(false) ?>
  249 + <?php ActiveForm::end(); ?>
  250 + </div>
  251 +</div>
... ...
views/order/index.php
1 1 <?php
  2 + /**
  3 + * @var ActiveDataProvider $dataProvider
  4 + * @var OrderSearch $searchModel
  5 + * @var View $this
  6 + */
  7 +
  8 + use artweb\artbox\ecommerce\models\OrderSearch;
  9 + use kartik\daterange\DateRangePicker;
  10 + use yii\data\ActiveDataProvider;
2 11 use yii\helpers\Html;
3   - use yii\grid\GridView;
  12 + use kartik\grid\GridView;
  13 + use yii\web\View;
4 14  
5 15 $this->title = 'Заказы';
6 16 $this->params[ 'breadcrumbs' ][] = $this->title;
7 17 ?>
8   - <h1>Заказы</h1>
9   - <p>
10   - <?= Html::a('Add order', [ 'create' ], [ 'class' => 'btn btn-success' ]) ?>
11   - </p>
12   -<?php \yii\widgets\Pjax::begin(
13   - [
14   -
15   - ]
16   -); ?>
  18 +<h1>Заказы</h1>
  19 +<p>
  20 + <?= Html::a('Add order', [ 'create' ], [ 'class' => 'btn btn-success' ]) ?>
  21 +</p>
  22 +
17 23 <?= GridView::widget(
18 24 [
19 25 'dataProvider' => $dataProvider,
20 26 'filterModel' => $searchModel,
21 27 'columns' => [
22   -
23   - [
24   - 'attribute' => 'id',
25   - 'format' => 'raw',
26   - 'options' => [ 'class' => 'btn btn-warning' ],
27   - 'value' => function ($model) {
28   - return Html::button($model->id,
29   - [
30   - 'id' => $model->id,
31   - 'class' => 'btn btn-warning',
32   - ]
33   - );
34   -
35   - },
36   -
37   - ],
  28 + 'id',
38 29 [
39 30 'attribute' => 'date_time',
40   - 'value' => 'date_time',
41   - ],
42   -
43   - [
44   - 'attribute' => 'name',
45   - 'value' => 'name',
46   - 'format' => 'raw',
47   - ],
48   - [
49   - 'attribute' => 'phone',
50   - 'value' => 'phone',
51   - ],
52   - // [
53   - // 'attribute' => 'total',
54   - // 'value'=>'total',
55   - // ],
56   - // [
57   - // 'filter' => yii\helpers\ArrayHelper::map(Label::find()->orderBy('id')->asArray()->all(), 'id', 'label'),
58   - // 'attribute' => 'label',
59   - // 'value' => function ($model, $key, $index, $column) {
60   - // return Html::activeDropDownList($model, 'label',
61   - // yii\helpers\ArrayHelper::map(Label::find()->orderBy('id')->asArray()->all(), 'id', 'label'),
62   - // [
63   - // 'prompt' => 'Нет',
64   - // 'onchange' => "$.ajax({
65   - // url: \"/admin/order/label-update\",
66   - // type: \"post\",
67   - // data: { order_id: $model->id, label_id : this.value},
68   - // });"
69   - // ]
70   - //
71   - // );
72   - // },
73   - // 'format' => 'raw',
74   - // ],
75   - // [
76   - // 'attribute' => 'pay',
77   - // 'filter' => [
78   - // 0 => 'Нет',1=>'Да'
79   - // ],
80   - // 'value' => function ($model, $key, $index, $column) {
81   - // return Html::activeDropDownList($model, 'pay',[0 => 'Нет',1=>'Да'],
82   - // [
83   - // 'onchange' => "$.ajax({
84   - // url: \"/admin/order/pay-update\",
85   - // type: \"post\",
86   - // data: { order_id: $model->id, pay_id : this.value},
87   - // });"
88   - // ]
89   - //
90   - // );
91   - // },
92   - // 'format' => 'raw',
93   - // ],
94   - [
95   - 'attribute' => 'status',
96   - 'value' => 'status',
97   - 'contentOptions' => [ 'style' => 'width: 5px;' ],
  31 + 'filter' => DateRangePicker::widget(
  32 + [
  33 + 'name' => 'OrderSearch[date_range]',
  34 + 'model' => $searchModel,
  35 + 'convertFormat' => false,
  36 + 'pluginOptions' => [
  37 + 'format' => 'YYYY-MM-DD',
  38 + 'opens' => 'left',
  39 + ],
  40 + ]
  41 + ),
98 42 ],
  43 + 'name',
  44 + 'phone',
  45 + 'status',
99 46 [
100   - 'class' => 'yii\grid\ActionColumn',
101   -// 'template' => '{delete}',
102   - 'contentOptions' => [ 'style' => 'width: 70px;' ],
  47 + 'class' => 'yii\grid\ActionColumn',
103 48 ],
104 49 ],
105 50 ]
106 51 ) ?>
107   -<?php \yii\widgets\Pjax::end(); ?>
108 52 \ No newline at end of file
... ...