Commit 8b1e29a64e54e138b7df1c0a74538d8ac673ad81

Authored by Alexey Boroda
1 parent 7e0d12f1

-Orders fixed

controllers/OrderController.php
1 <?php 1 <?php
2 2
3 namespace artbox\order\controllers; 3 namespace artbox\order\controllers;
4 - 4 +
5 use artbox\catalog\models\Variant; 5 use artbox\catalog\models\Variant;
6 use artbox\order\models\Delivery; 6 use artbox\order\models\Delivery;
7 use artbox\order\models\Label; 7 use artbox\order\models\Label;
@@ -10,11 +10,12 @@ @@ -10,11 +10,12 @@
10 use Yii; 10 use Yii;
11 use artbox\order\models\Order; 11 use artbox\order\models\Order;
12 use artbox\order\models\OrderSearch; 12 use artbox\order\models\OrderSearch;
13 - use yii\base\InvalidParamException;  
14 use yii\web\Controller; 13 use yii\web\Controller;
15 use yii\web\NotFoundHttpException; 14 use yii\web\NotFoundHttpException;
16 use yii\filters\VerbFilter; 15 use yii\filters\VerbFilter;
17 - 16 + use yii\web\Response;
  17 + use yii\widgets\ActiveForm;
  18 +
18 /** 19 /**
19 * OrderController implements the CRUD actions for Order model. 20 * OrderController implements the CRUD actions for Order model.
20 */ 21 */
@@ -34,7 +35,7 @@ @@ -34,7 +35,7 @@
34 ], 35 ],
35 ]; 36 ];
36 } 37 }
37 - 38 +
38 /** 39 /**
39 * Lists all Order models. 40 * Lists all Order models.
40 * 41 *
@@ -45,21 +46,21 @@ @@ -45,21 +46,21 @@
45 $searchModel = new OrderSearch(); 46 $searchModel = new OrderSearch();
46 $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 47 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
47 $labels = Label::find() 48 $labels = Label::find()
48 - ->select(  
49 - [  
50 - 'title',  
51 - 'id',  
52 - ]  
53 - )  
54 - ->joinWith('lang')  
55 - ->andWhere(  
56 - [  
57 - 'status' => true,  
58 - ]  
59 - )  
60 - ->indexBy('id')  
61 - ->column();  
62 - 49 + ->select(
  50 + [
  51 + 'title',
  52 + 'id',
  53 + ]
  54 + )
  55 + ->joinWith('lang')
  56 + ->andWhere(
  57 + [
  58 + 'status' => true,
  59 + ]
  60 + )
  61 + ->indexBy('id')
  62 + ->column();
  63 +
63 return $this->render( 64 return $this->render(
64 '@artbox/order/views/order/index', 65 '@artbox/order/views/order/index',
65 [ 66 [
@@ -69,7 +70,7 @@ @@ -69,7 +70,7 @@
69 ] 70 ]
70 ); 71 );
71 } 72 }
72 - 73 +
73 /** 74 /**
74 * Displays a single Order model. 75 * Displays a single Order model.
75 * 76 *
@@ -86,7 +87,7 @@ @@ -86,7 +87,7 @@
86 ] 87 ]
87 ); 88 );
88 } 89 }
89 - 90 +
90 /** 91 /**
91 * Creates a new Order model. 92 * Creates a new Order model.
92 * If creation is successful, the browser will be redirected to the 'view' page. 93 * If creation is successful, the browser will be redirected to the 'view' page.
@@ -96,8 +97,9 @@ @@ -96,8 +97,9 @@
96 public function actionCreate() 97 public function actionCreate()
97 { 98 {
98 $model = new Order(); 99 $model = new Order();
99 -  
100 - if ($model->load(Yii::$app->request->post()) && $model->save()) { 100 +
  101 + if ($model->load(Yii::$app->request->post()) && $this->hasProducts() && $model->save()) {
  102 + OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $model->id);
101 return $this->redirect( 103 return $this->redirect(
102 [ 104 [
103 'view', 105 'view',
@@ -105,63 +107,18 @@ @@ -105,63 +107,18 @@
105 ] 107 ]
106 ); 108 );
107 } else { 109 } else {
108 - $labels = Label::find()  
109 - ->joinWith('lang')  
110 - ->select(  
111 - [  
112 - 'title',  
113 - 'id',  
114 - ]  
115 - )  
116 - ->where(  
117 - [  
118 - 'status' => true,  
119 - ]  
120 - )  
121 - ->indexBy('id')  
122 - ->column();  
123 - $deliveries = Delivery::find()  
124 - ->joinWith('lang')  
125 - ->select(  
126 - [  
127 - 'title',  
128 - 'id',  
129 - ]  
130 - )  
131 - ->where(  
132 - [  
133 - 'status' => true,  
134 - ]  
135 - )  
136 - ->indexBy('id')  
137 - ->column();  
138 - $payments = Payment::find()  
139 - ->joinWith('lang')  
140 - ->select(  
141 - [  
142 - 'title',  
143 - 'id',  
144 - ]  
145 - )  
146 - ->where(  
147 - [  
148 - 'status' => true,  
149 - ]  
150 - )  
151 - ->indexBy('id')  
152 - ->column();  
153 return $this->render( 110 return $this->render(
154 '@artbox/order/views/order/create', 111 '@artbox/order/views/order/create',
155 [ 112 [
156 'model' => $model, 113 'model' => $model,
157 - 'labels' => $labels,  
158 - 'payments' => $payments,  
159 - 'deliveries' => $deliveries, 114 + 'labels' => $this->getLabels(),
  115 + 'payments' => $this->getPayments(),
  116 + 'deliveries' => $this->getDeliveries(),
160 ] 117 ]
161 ); 118 );
162 } 119 }
163 } 120 }
164 - 121 +
165 /** 122 /**
166 * Updates an existing Order model. 123 * Updates an existing Order model.
167 * If update is successful, the browser will be redirected to the 'view' page. 124 * If update is successful, the browser will be redirected to the 'view' page.
@@ -173,8 +130,8 @@ @@ -173,8 +130,8 @@
173 public function actionUpdate($id) 130 public function actionUpdate($id)
174 { 131 {
175 $model = $this->findModel($id); 132 $model = $this->findModel($id);
176 -  
177 - if ($model->load(Yii::$app->request->post()) && $model->save()) { 133 +
  134 + if ($model->load(Yii::$app->request->post()) && $this->hasProducts() && $model->save()) {
178 OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $id); 135 OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $id);
179 return $this->redirect( 136 return $this->redirect(
180 [ 137 [
@@ -183,63 +140,18 @@ @@ -183,63 +140,18 @@
183 ] 140 ]
184 ); 141 );
185 } else { 142 } else {
186 - $labels = Label::find()  
187 - ->joinWith('lang')  
188 - ->select(  
189 - [  
190 - 'title',  
191 - 'id',  
192 - ]  
193 - )  
194 - ->where(  
195 - [  
196 - 'status' => true,  
197 - ]  
198 - )  
199 - ->indexBy('id')  
200 - ->column();  
201 - $deliveries = Delivery::find()  
202 - ->joinWith('lang')  
203 - ->select(  
204 - [  
205 - 'title',  
206 - 'id',  
207 - ]  
208 - )  
209 - ->where(  
210 - [  
211 - 'status' => true,  
212 - ]  
213 - )  
214 - ->indexBy('id')  
215 - ->column();  
216 - $payments = Payment::find()  
217 - ->joinWith('lang')  
218 - ->select(  
219 - [  
220 - 'title',  
221 - 'id',  
222 - ]  
223 - )  
224 - ->where(  
225 - [  
226 - 'status' => true,  
227 - ]  
228 - )  
229 - ->indexBy('id')  
230 - ->column();  
231 return $this->render( 143 return $this->render(
232 '@artbox/order/views/order/update', 144 '@artbox/order/views/order/update',
233 [ 145 [
234 'model' => $model, 146 'model' => $model,
235 - 'labels' => $labels,  
236 - 'payments' => $payments,  
237 - 'deliveries' => $deliveries, 147 + 'labels' => $this->getLabels(),
  148 + 'payments' => $this->getPayments(),
  149 + 'deliveries' => $this->getDeliveries(),
238 ] 150 ]
239 ); 151 );
240 } 152 }
241 } 153 }
242 - 154 +
243 /** 155 /**
244 * Deletes an existing Order model. 156 * Deletes an existing Order model.
245 * If deletion is successful, the browser will be redirected to the 'index' page. 157 * If deletion is successful, the browser will be redirected to the 'index' page.
@@ -252,14 +164,19 @@ @@ -252,14 +164,19 @@
252 { 164 {
253 $this->findModel($id) 165 $this->findModel($id)
254 ->delete(); 166 ->delete();
255 - 167 +
256 return $this->redirect([ 'index' ]); 168 return $this->redirect([ 'index' ]);
257 } 169 }
258 -  
259 - public function actionProductList($q = null, $id = null) 170 +
  171 + /**
  172 + * @param string $q
  173 + * @param null $id
  174 + *
  175 + * @return array
  176 + */
  177 + public function actionProductList(string $q, $id = null)
260 { 178 {
261 - $response = \Yii::$app->response;  
262 - $response->format = $response::FORMAT_JSON; 179 + \Yii::$app->response->format = Response::FORMAT_JSON;
263 $out = [ 180 $out = [
264 'results' => [ 181 'results' => [
265 'id' => '', 182 'id' => '',
@@ -274,21 +191,22 @@ @@ -274,21 +191,22 @@
274 $variants = Variant::find() 191 $variants = Variant::find()
275 ->joinWith('lang', false) 192 ->joinWith('lang', false)
276 ->joinWith('product.lang', false) 193 ->joinWith('product.lang', false)
277 - ->andWhere( 194 + ->where(
278 [ 195 [
279 - 'like', 196 + 'ilike',
280 'product_lang.title', 197 'product_lang.title',
281 $q, 198 $q,
282 ] 199 ]
283 ) 200 )
284 ->orWhere( 201 ->orWhere(
285 [ 202 [
286 - 'like', 203 + 'ilike',
287 'variant_lang.title', 204 'variant_lang.title',
288 $q, 205 $q,
289 ] 206 ]
290 ) 207 )
291 ->orWhere([ 'variant.sku' => $q ]) 208 ->orWhere([ 'variant.sku' => $q ])
  209 + ->limit(20)
292 ->all(); 210 ->all();
293 foreach ($variants as $variant) { 211 foreach ($variants as $variant) {
294 $out[ 'results' ][] = [ 212 $out[ 'results' ][] = [
@@ -311,57 +229,62 @@ @@ -311,57 +229,62 @@
311 } 229 }
312 return $out; 230 return $out;
313 } 231 }
314 - 232 +
  233 + /**
  234 + * @return string
  235 + */
315 public function actionAddToOrder() 236 public function actionAddToOrder()
316 { 237 {
317 - $id = \Yii::$app->request->post('id');  
318 - $count = \Yii::$app->request->post('count');  
319 - $orderId = \Yii::$app->request->post('order');  
320 - if (empty($id) || empty($count)) {  
321 - throw new InvalidParamException(\Yii::t('order', 'Set id and count'));  
322 - }  
323 - $order = Order::find()  
324 - ->where([ 'id' => $orderId ])  
325 - ->one();  
326 - if (empty($order)) {  
327 - throw new NotFoundHttpException(\Yii::t('order', 'Order not found'));  
328 - }  
329 /** 238 /**
330 - * @var Variant $variant 239 + * @var Variant $variant
  240 + * @var ActiveForm $form
  241 + * @var OrderProduct $orderProduct
331 */ 242 */
  243 + \Yii::$app->response->format = Response::FORMAT_JSON;
  244 + $id = \Yii::$app->request->post('id');
  245 + $count = \Yii::$app->request->post('count') ? \Yii::$app->request->post('count') : 1;
  246 +
332 $variant = Variant::find() 247 $variant = Variant::find()
333 - ->where([ 'id' => $id ]) 248 + ->with('product.lang')
  249 + ->where(
  250 + [
  251 + 'id' => $id,
  252 + ]
  253 + )
334 ->one(); 254 ->one();
335 - if (empty($variant)) {  
336 - throw new NotFoundHttpException(\Yii::t('order', 'Variant not found'));  
337 - }  
338 - /**  
339 - * @var OrderProduct $model  
340 - */  
341 - $model = OrderProduct::find()  
342 - ->where(  
343 - [  
344 - 'order_id' => $orderId,  
345 - 'variant_id' => $id,  
346 - ]  
347 - )  
348 - ->one();  
349 - if ($model) {  
350 - $model->count += $count;  
351 - } else {  
352 - $model = new OrderProduct( 255 +
  256 + if ($variant) {
  257 + $form = new ActiveForm();
  258 + $orderProduct = new OrderProduct(
353 [ 259 [
354 - 'order_id' => $orderId,  
355 - 'variant_id' => $id,  
356 - 'sku' => $variant->sku,  
357 - 'price' => $variant->price,  
358 'count' => $count, 260 'count' => $count,
  261 + 'variant_id' => $variant->id,
359 ] 262 ]
360 ); 263 );
  264 +
  265 + $row = $this->renderPartial(
  266 + '@artbox/order/views/order/_order_product',
  267 + [
  268 + 'orderProduct' => $orderProduct,
  269 + 'index' => rand(0, 10000),
  270 + 'variant' => $variant,
  271 + 'price' => $variant->price,
  272 + 'form' => $form,
  273 + ]
  274 + );
  275 +
  276 + return [
  277 + 'success' => true,
  278 + 'row' => $row,
  279 + ];
361 } 280 }
362 - $model->save(); 281 +
  282 + return [
  283 + 'success' => false,
  284 + 'message' => \Yii::t('app', 'Product not found'),
  285 + ];
363 } 286 }
364 - 287 +
365 /** 288 /**
366 * Finds the Order model based on its primary key value. 289 * Finds the Order model based on its primary key value.
367 * If the model is not found, a 404 HTTP exception will be thrown. 290 * If the model is not found, a 404 HTTP exception will be thrown.
@@ -379,4 +302,84 @@ @@ -379,4 +302,84 @@
379 throw new NotFoundHttpException('The requested page does not exist.'); 302 throw new NotFoundHttpException('The requested page does not exist.');
380 } 303 }
381 } 304 }
  305 +
  306 + /**
  307 + * @return bool
  308 + */
  309 + protected function hasProducts()
  310 + {
  311 + if (empty(\Yii::$app->request->post('OrderProduct'))) {
  312 + \Yii::$app->session->setFlash('error', \Yii::t('app', 'ะ—ะฐะบะฐะท ะฝะต ะผะพะถะตั‚ ะฑั‹ั‚ัŒ ะฑะตะท ั‚ะพะฒะฐั€ะพะฒ'));
  313 +
  314 + return false;
  315 + }
  316 +
  317 + return true;
  318 + }
  319 +
  320 + /**
  321 + * @return array
  322 + */
  323 + protected function getLabels()
  324 + {
  325 + return Label::find()
  326 + ->joinWith('lang')
  327 + ->select(
  328 + [
  329 + 'title',
  330 + 'id',
  331 + ]
  332 + )
  333 + ->where(
  334 + [
  335 + 'status' => true,
  336 + ]
  337 + )
  338 + ->indexBy('id')
  339 + ->column();
  340 + }
  341 +
  342 + /**
  343 + * @return array
  344 + */
  345 + protected function getPayments()
  346 + {
  347 + return Payment::find()
  348 + ->joinWith('lang')
  349 + ->select(
  350 + [
  351 + 'title',
  352 + 'id',
  353 + ]
  354 + )
  355 + ->where(
  356 + [
  357 + 'status' => true,
  358 + ]
  359 + )
  360 + ->indexBy('id')
  361 + ->column();
  362 + }
  363 +
  364 + /**
  365 + * @return array
  366 + */
  367 + protected function getDeliveries()
  368 + {
  369 + return Delivery::find()
  370 + ->joinWith('lang')
  371 + ->select(
  372 + [
  373 + 'title',
  374 + 'id',
  375 + ]
  376 + )
  377 + ->where(
  378 + [
  379 + 'status' => true,
  380 + ]
  381 + )
  382 + ->indexBy('id')
  383 + ->column();
  384 + }
382 } 385 }
models/OrderSearch.php
@@ -65,7 +65,12 @@ @@ -65,7 +65,12 @@
65 'count(*)', 65 'count(*)',
66 ] 66 ]
67 ) 67 )
68 - ->groupBy('order.id'); 68 + ->groupBy('order.id')
  69 + ->orderBy(
  70 + [
  71 + 'order.id' => SORT_DESC,
  72 + ]
  73 + );
69 74
70 // add conditions that should always apply here 75 // add conditions that should always apply here
71 76
views/order/_form.php
@@ -43,7 +43,7 @@ @@ -43,7 +43,7 @@
43 ->textInput([ 'maxlength' => true ]) ?> 43 ->textInput([ 'maxlength' => true ]) ?>
44 44
45 <?= $form->field($model, 'comment') 45 <?= $form->field($model, 'comment')
46 - ->textInput([ 'maxlength' => true ]) ?> 46 + ->textarea() ?>
47 47
48 <?= $form->field($model, 'label_id') 48 <?= $form->field($model, 'label_id')
49 ->dropDownList($labels) ?> 49 ->dropDownList($labels) ?>
@@ -53,137 +53,108 @@ @@ -53,137 +53,108 @@
53 53
54 <?= $form->field($model, 'payment_id') 54 <?= $form->field($model, 'payment_id')
55 ->dropDownList($payments) ?> 55 ->dropDownList($payments) ?>
56 - <?php  
57 - if (!$model->isNewRecord) { 56 +
  57 + <div class="order-product-container">
  58 + <div id="order-product-pjax" style="position: relative;">
  59 + <div class="row strong">
  60 + <div class="col-md-4">
  61 + <?php
  62 + echo Html::tag('strong', \Yii::t('order', 'Product'));
58 ?> 63 ?>
59 - <div class="order-product-container">  
60 - <div id="order-product-pjax" style="position: relative;">  
61 - <div class="row strong">  
62 - <div class="col-md-4">  
63 - <?php  
64 - echo Html::tag('strong', \Yii::t('order', 'Product'));  
65 - ?>  
66 - </div>  
67 - <div class="col-md-4">  
68 - <?php  
69 - echo Html::tag('strong', \Yii::t('order', 'Price'));  
70 - ?>  
71 - </div>  
72 - <div class="col-md-4">  
73 - <?php  
74 - echo Html::tag('strong', \Yii::t('order', 'Count'));  
75 - ?>  
76 - </div>  
77 - </div>  
78 -  
79 - <?php  
80 - foreach ($model->orderProducts as $index => $orderProduct) {  
81 - ?>  
82 - <div class="row row-order-product">  
83 - <div class="col-md-4">  
84 - <?php  
85 - echo $form->field($orderProduct, "[$index]variant_id")  
86 - ->hiddenInput()  
87 - ->label(false);  
88 - echo $orderProduct->variant->product->lang->title . '(' . $orderProduct->variant->sku . ')';  
89 - ?>  
90 - </div>  
91 - <div class="col-md-4">  
92 - <?php echo $orderProduct->price; ?>  
93 - </div>  
94 - <div class="col-md-3">  
95 - <?php  
96 - echo $form->field($orderProduct, "[$index]count")  
97 - ->textInput()  
98 - ->label(false);  
99 - ?>  
100 - </div>  
101 - <div class="col-md-1">  
102 - <?php  
103 - echo Html::a(  
104 - Html::icon(  
105 - 'trash-o',  
106 - [  
107 - 'prefix' => 'fa fa-',  
108 - ]  
109 - ),  
110 - '#',  
111 - [  
112 - 'class' => 'remove-order-product',  
113 - ]  
114 - )  
115 - ?>  
116 - </div>  
117 - </div>  
118 - <?php  
119 - }  
120 - ?>  
121 - </div>  
122 - <div class="row">  
123 - <div class="col-md-8">  
124 - <?php  
125 - echo Select2::widget(  
126 - [  
127 - 'name' => 'add-to-order',  
128 - 'options' => [  
129 - 'placeholder' => \Yii::t('order', 'Select product'),  
130 - ],  
131 - 'pluginOptions' => [  
132 - 'allowClear' => true,  
133 - 'minimumInputLength' => 3,  
134 - 'language' => [  
135 - 'errorLoading' => new JsExpression(  
136 - "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }"  
137 - ),  
138 - ],  
139 - 'ajax' => [  
140 - 'url' => Url::to([ 'product-list' ]),  
141 - 'dataType' => 'json',  
142 - 'data' => new JsExpression('function(params) { return {q:params.term}; }'),  
143 - ],  
144 - 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),  
145 - 'templateResult' => new JsExpression('function(city) { return city.text; }'),  
146 - 'templateSelection' => new JsExpression('function (city) { return city.text; }'),  
147 - ],  
148 - 'id' => 'add-to-order',  
149 - ]  
150 - );  
151 - ?>  
152 - </div>  
153 - <div class="col-md-3">  
154 - <?php  
155 - echo Html::textInput(  
156 - 'count-to-order',  
157 - null,  
158 - [  
159 - 'class' => 'form-control',  
160 - 'id' => 'count-to-order',  
161 - ]  
162 - );  
163 - ?>  
164 - </div>  
165 - <div class="col-md-1">  
166 - <?php  
167 - echo Html::a(  
168 - Html::icon(  
169 - 'plus-circle',  
170 - [  
171 - 'prefix' => 'fa fa-',  
172 - ]  
173 - ),  
174 - '#', 64 + </div>
  65 + <div class="col-md-4">
  66 + <?php
  67 + echo Html::tag('strong', \Yii::t('order', 'Price'));
  68 + ?>
  69 + </div>
  70 + <div class="col-md-4">
  71 + <?php
  72 + echo Html::tag('strong', \Yii::t('order', 'Count'));
  73 + ?>
  74 + </div>
  75 + </div>
  76 + <div id="product-rows">
  77 + <?php
  78 + if (!$model->isNewRecord) {
  79 + foreach ($model->orderProducts as $index => $orderProduct) {
  80 + echo $this->render(
  81 + '_order_product',
175 [ 82 [
176 - 'class' => 'variant-to-order',  
177 - 'data-id' => $model->id, 83 + 'orderProduct' => $orderProduct,
  84 + 'price' => $orderProduct->price,
  85 + 'index' => $index,
  86 + 'variant' => $orderProduct->variant,
  87 + 'form' => $form,
178 ] 88 ]
179 ); 89 );
180 - ?>  
181 - </div>  
182 - </div>  
183 - </div>  
184 - <?php  
185 - }  
186 - ?> 90 + }
  91 + }
  92 + ?>
  93 + </div>
  94 + </div>
  95 + <div class="row">
  96 + <div class="col-md-8">
  97 + <?php
  98 + echo Select2::widget(
  99 + [
  100 + 'name' => 'add-to-order',
  101 + 'options' => [
  102 + 'placeholder' => \Yii::t('order', 'Select product'),
  103 + ],
  104 + 'pluginOptions' => [
  105 + 'allowClear' => true,
  106 + 'minimumInputLength' => 3,
  107 + 'language' => [
  108 + 'errorLoading' => new JsExpression(
  109 + "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }"
  110 + ),
  111 + ],
  112 + 'ajax' => [
  113 + 'url' => Url::to([ 'product-list' ]),
  114 + 'dataType' => 'json',
  115 + 'data' => new JsExpression('function(params) { return {q:params.term}; }'),
  116 + ],
  117 + 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
  118 + 'templateResult' => new JsExpression('function(city) { return city.text; }'),
  119 + 'templateSelection' => new JsExpression('function (city) { return city.text; }'),
  120 + ],
  121 + 'id' => 'add-to-order',
  122 + ]
  123 + );
  124 + ?>
  125 + </div>
  126 + <div class="col-md-3">
  127 + <?php
  128 + echo Html::textInput(
  129 + 'count-to-order',
  130 + null,
  131 + [
  132 + 'class' => 'form-control',
  133 + 'id' => 'count-to-order',
  134 + ]
  135 + );
  136 + ?>
  137 + </div>
  138 + <div class="col-md-1">
  139 + <?php
  140 + echo Html::a(
  141 + Html::icon(
  142 + 'plus-circle',
  143 + [
  144 + 'prefix' => 'fa fa-',
  145 + ]
  146 + ),
  147 + '#',
  148 + [
  149 + 'class' => 'variant-to-order',
  150 + ]
  151 + );
  152 + ?>
  153 + </div>
  154 + </div>
  155 + </div>
  156 +
  157 + <div class="ln_solid"></div>
187 158
188 <div class="form-group"> 159 <div class="form-group">
189 <?= Html::submitButton( 160 <?= Html::submitButton(
views/order/_order_product.php 0 โ†’ 100644
  1 +<?php
  2 +
  3 + use artbox\catalog\models\Variant;
  4 + use artbox\order\models\OrderProduct;
  5 + use yii\widgets\ActiveForm;
  6 + use yii\bootstrap\Html;
  7 + use yii\web\View;
  8 +
  9 + /**
  10 + * @var View $this
  11 + * @var OrderProduct $orderProduct
  12 + * @var int $index
  13 + * @var Variant $variant
  14 + * @var float $price
  15 + * @var ActiveForm $form
  16 + */
  17 +
  18 +?>
  19 +
  20 +<div class="row row-order-product">
  21 + <div class="col-md-4">
  22 + <?php
  23 + echo $form->field($orderProduct, "[$index]variant_id")
  24 + ->hiddenInput()
  25 + ->label(false);
  26 + echo $variant->product->lang->title . '(' . $variant->sku . ')';
  27 + ?>
  28 + </div>
  29 + <div class="col-md-4">
  30 + <?php echo $price; ?>
  31 + </div>
  32 + <div class="col-md-3">
  33 + <?php
  34 + echo $form->field($orderProduct, "[$index]count")
  35 + ->textInput()
  36 + ->label(false);
  37 + ?>
  38 + </div>
  39 + <div class="col-md-1">
  40 + <?php
  41 + echo Html::a(
  42 + Html::icon(
  43 + 'trash-o',
  44 + [
  45 + 'prefix' => 'fa fa-',
  46 + ]
  47 + ),
  48 + '#',
  49 + [
  50 + 'class' => 'remove-order-product',
  51 + ]
  52 + )
  53 + ?>
  54 + </div>
  55 +</div>
views/order/create.php
1 <?php 1 <?php
2 2
3 - use yii\helpers\Html;  
4 use yiister\gentelella\widgets\Panel; 3 use yiister\gentelella\widgets\Panel;
5 4
6 /** 5 /**
@@ -26,7 +25,6 @@ @@ -26,7 +25,6 @@
26 ] 25 ]
27 ); 26 );
28 ?> 27 ?>
29 - <h1><?= Html::encode($this->title) ?></h1>  
30 28
31 <?= $this->render( 29 <?= $this->render(
32 '_form', 30 '_form',
1 -$(function() {  
2 - $(document)  
3 - .on('click', '.remove-order-product', function(e) {  
4 - e.preventDefault();  
5 - $(this)  
6 - .parents('.row-order-product')  
7 - .remove();  
8 - });  
9 - $(document)  
10 - .on('click', '.variant-to-order', function(e) {  
11 - e.preventDefault();  
12 - var id = $('#add-to-order');  
13 - var count = $('#count-to-order');  
14 - var order = $(this)  
15 - .data('id');  
16 - if (id.val() && count.val()) {  
17 - var selector = '#order-product-pjax';  
18 - showLoader(selector);  
19 - $.post('/admin/order/add-to-order', {  
20 - id: id.val(),  
21 - count: count.val(),  
22 - order: order  
23 - }, function() {  
24 - $.pjax.reload(selector, {  
25 - timeout: 5000,  
26 - fragment: selector  
27 - });  
28 - });  
29 - id.val(null)  
30 - .trigger('change');  
31 - count.val(null)  
32 - .trigger('change');  
33 - }  
34 - });  
35 -});  
36 -function showLoader(container) {  
37 - $(container)  
38 - .prepend('<div class="loader-wrapper"></div>');  
39 -}  
40 \ No newline at end of file 1 \ No newline at end of file
  2 +$(
  3 + function() {
  4 + $(document)
  5 + .on(
  6 + 'click', '.remove-order-product', function(e) {
  7 + e.preventDefault();
  8 + $(this)
  9 + .parents('.row-order-product')
  10 + .remove();
  11 + }
  12 + );
  13 + $(document)
  14 + .on(
  15 + 'click', '.variant-to-order', function(e) {
  16 + e.preventDefault();
  17 + var id = $('#add-to-order');
  18 + var count = $('#count-to-order');
  19 + if (id.val()) {
  20 + $.ajax(
  21 + {
  22 + url: '/admin/order/add-to-order',
  23 + type: "POST",
  24 + data: {
  25 + 'id': id.val(),
  26 + 'count': count.val()
  27 + },
  28 + success: function(data) {
  29 + if (data.success) {
  30 + $('#product-rows')
  31 + .append(data.row);
  32 + }
  33 + }
  34 + }
  35 + );
  36 + }
  37 + }
  38 + );
  39 + }
  40 +);