Commit 543a6adb504a8f9f6dc83bf812e40569b4de7076

Authored by Anastasia
1 parent b6db17e5

order

controllers/OrderController.php
... ... @@ -10,11 +10,12 @@
10 10 use Yii;
11 11 use artbox\order\models\Order;
12 12 use artbox\order\models\OrderSearch;
13   - use yii\base\InvalidParamException;
14 13 use yii\web\Controller;
15 14 use yii\web\NotFoundHttpException;
16 15 use yii\filters\VerbFilter;
17 16 use yii\filters\AccessControl;
  17 + use yii\web\Response;
  18 + use yii\widgets\ActiveForm;
18 19  
19 20 /**
20 21 * OrderController implements the CRUD actions for Order model.
... ... @@ -353,71 +354,54 @@
353 354  
354 355 public function actionAddToOrder()
355 356 {
356   - $id = \Yii::$app->request->post('id');
357   - $count = \Yii::$app->request->post('count');
358   - $orderId = \Yii::$app->request->post('order');
359   - if (empty($id) || empty($count)) {
360   - throw new InvalidParamException(\Yii::t('order', 'Set id and count'));
361   - }
362   - $order = Order::find()
363   - ->where([ 'id' => $orderId ])
364   - ->one();
365   - if (empty($order)) {
366   - throw new NotFoundHttpException(\Yii::t('order', 'Order not found'));
367   - }
368 357 /**
369   - * @var Variant $variant
  358 + * @var Variant $variant
  359 + * @var ActiveForm $form
  360 + * @var OrderProduct $orderProduct
370 361 */
  362 + \Yii::$app->response->format = Response::FORMAT_JSON;
  363 + $id = \Yii::$app->request->post('id');
  364 + $count = \Yii::$app->request->post('count') ? \Yii::$app->request->post('count') : 1;
  365 +
371 366 $variant = Variant::find()
372   - ->where([ 'id' => $id ])
  367 + ->with('product.lang')
  368 + ->where(
  369 + [
  370 + 'id' => $id,
  371 + ]
  372 + )
373 373 ->one();
374   - if (empty($variant)) {
375   - throw new NotFoundHttpException(\Yii::t('order', 'Variant not found'));
376   - }
377   - /**
378   - * @var OrderProduct $model
379   - */
380   - $model = OrderProduct::find()
381   - ->where(
382   - [
383   - 'order_id' => $orderId,
384   - 'variant_id' => $id,
385   - ]
386   - )
387   - ->one();
388   - if ($model) {
389   - $model->count += $count;
390   - } else {
391   - $model = new OrderProduct(
  374 +
  375 + if ($variant) {
  376 + $form = new ActiveForm();
  377 + $orderProduct = new OrderProduct(
392 378 [
393   - 'order_id' => $orderId,
394   - 'variant_id' => $id,
395   - 'sku' => $variant->sku,
396   - 'price' => $variant->price,
397 379 'count' => $count,
  380 + 'variant_id' => $variant->id,
398 381 ]
399 382 );
  383 +
  384 + $row = $this->renderPartial(
  385 + '_order_product',
  386 + [
  387 + 'orderProduct' => $orderProduct,
  388 + 'index' => rand(0, 10000),
  389 + 'variant' => $variant,
  390 + 'price' => $variant->price,
  391 + 'form' => $form,
  392 + ]
  393 + );
  394 +
  395 + return [
  396 + 'success' => true,
  397 + 'row' => $row,
  398 + ];
400 399 }
401   - $model->save();
402   - $sum = $count * $variant->price;
403   -
404   - return $sum;
405   - }
406 400  
407   - public function actionDeleteFromOrder()
408   - {
409   - $id = \Yii::$app->request->post('id');
410   - $variant = \Yii::$app->request->post('variant');
411   - $product = OrderProduct::find()
412   - ->where(
413   - [
414   - 'order_id' => $id,
415   - 'variant_id' => $variant,
416   - ]
417   - )
418   - ->one()
419   - ->delete();
420   - return 'ok';
  401 + return [
  402 + 'success' => false,
  403 + 'message' => \Yii::t('app', 'Product not found'),
  404 + ];
421 405 }
422 406  
423 407 /**
... ... @@ -445,4 +429,81 @@
445 429 'model' => $model
446 430 ]);
447 431 }
  432 +
  433 + protected function hasProducts()
  434 + {
  435 + if (empty(\Yii::$app->request->post('OrderProduct'))) {
  436 + \Yii::$app->session->setFlash('error', \Yii::t('app', 'ะ—ะฐะบะฐะท ะฝะต ะผะพะถะตั‚ ะฑั‹ั‚ัŒ ะฑะตะท ั‚ะพะฒะฐั€ะพะฒ'));
  437 +
  438 + return false;
  439 + }
  440 +
  441 + return true;
  442 + }
  443 +
  444 + /**
  445 + * @return array
  446 + */
  447 + protected function getLabels()
  448 + {
  449 + return Label::find()
  450 + ->joinWith('lang')
  451 + ->select(
  452 + [
  453 + 'title',
  454 + 'id',
  455 + ]
  456 + )
  457 + ->where(
  458 + [
  459 + 'status' => true,
  460 + ]
  461 + )
  462 + ->indexBy('id')
  463 + ->column();
  464 + }
  465 +
  466 + /**
  467 + * @return array
  468 + */
  469 + protected function getPayments()
  470 + {
  471 + return Payment::find()
  472 + ->joinWith('lang')
  473 + ->select(
  474 + [
  475 + 'title',
  476 + 'id',
  477 + ]
  478 + )
  479 + ->where(
  480 + [
  481 + 'status' => true,
  482 + ]
  483 + )
  484 + ->indexBy('id')
  485 + ->column();
  486 + }
  487 +
  488 + /**
  489 + * @return array
  490 + */
  491 + protected function getDeliveries()
  492 + {
  493 + return Delivery::find()
  494 + ->joinWith('lang')
  495 + ->select(
  496 + [
  497 + 'title',
  498 + 'id',
  499 + ]
  500 + )
  501 + ->where(
  502 + [
  503 + 'status' => true,
  504 + ]
  505 + )
  506 + ->indexBy('id')
  507 + ->column();
  508 + }
448 509 }
... ...
views/order/_form.php
... ... @@ -4,7 +4,6 @@
4 4 use yii\bootstrap\Html;
5 5 use yii\helpers\Url;
6 6 use yii\web\JsExpression;
7   - use yii\web\View;
8 7 use yii\widgets\ActiveForm;
9 8  
10 9 /**
... ... @@ -15,42 +14,6 @@
15 14 * @var array $payments
16 15 * @var ActiveForm $form
17 16 */
18   -
19   - $js = <<< JS
20   -$(document).on('click', '#create-add', function(e) {
21   - e.preventDefault();
22   - var variant_id = $('#add-to-order').val();
23   - var count = $('#count-to-order').val();
24   -
25   - $.ajax({
26   - url: '/admin/order-product/add',
27   - type: "GET",
28   - data: {
29   - variant_id: variant_id,
30   - count: count
31   - },
32   - success: function(data) {
33   - if (data.id != 0) {
34   - var row = $('#products #' + data.id);
35   - if (row.length !== 0) {
36   - row.find('.count-input').val(function(i, oldVal) {
37   - return parseInt(oldVal, 10) + data.count;
38   - });
39   - } else {
40   - $('#products').append(data.row);
41   - }
42   - }
43   - }
44   - });
45   -});
46   -$(document).on('click', '.delete-product-row', function(e) {
47   - e.preventDefault();
48   - $(this).parent().parent().remove();
49   -})
50   -JS;
51   -
52   - $this->registerJs($js, View::POS_READY);
53   -
54 17 ?>
55 18  
56 19 <div class="order-form">
... ... @@ -58,15 +21,18 @@ JS;
58 21 <?php $form = ActiveForm::begin(); ?>
59 22  
60 23 <?php
61   - if ($model->isNewRecord) {
62   - echo $form->field($model, 'user_id')
63   - ->textInput();
64   - }
  24 + // if ($model->isNewRecord) {
  25 + // echo $form->field($model, 'user_id')
  26 + // ->textInput();
  27 + // }
65 28 ?>
66 29  
67 30 <?= $form->field($model, 'name')
68 31 ->textInput([ 'maxlength' => true ]) ?>
69 32  
  33 + <?= $form->field($model, 'secondname')
  34 + ->textInput([ 'maxlength' => true ]) ?>
  35 +
70 36 <?= $form->field($model, 'phone')
71 37 ->textInput([ 'maxlength' => true ]) ?>
72 38  
... ... @@ -80,7 +46,7 @@ JS;
80 46 ->textInput([ 'maxlength' => true ]) ?>
81 47  
82 48 <?= $form->field($model, 'comment')
83   - ->textInput([ 'maxlength' => true ]) ?>
  49 + ->textarea() ?>
84 50  
85 51 <?= $form->field($model, 'label_id')
86 52 ->dropDownList($labels) ?>
... ... @@ -90,210 +56,112 @@ JS;
90 56  
91 57 <?= $form->field($model, 'payment_id')
92 58 ->dropDownList($payments) ?>
93   - <?php
94   - $sum = 0;
95   - if (!$model->isNewRecord) {
  59 +
  60 + <div class="ln_solid"></div>
  61 +
  62 + <div class="order-product-container">
  63 + <div class="row">
  64 + <div class="col-md-8">
  65 + <?php
  66 + echo Select2::widget(
  67 + [
  68 + 'name' => 'add-to-order',
  69 + 'options' => [
  70 + 'placeholder' => \Yii::t('order', 'Select product'),
  71 + ],
  72 + 'pluginOptions' => [
  73 + 'allowClear' => true,
  74 + 'minimumInputLength' => 3,
  75 + 'language' => [
  76 + 'errorLoading' => new JsExpression(
  77 + "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }"
  78 + ),
  79 + ],
  80 + 'ajax' => [
  81 + 'url' => Url::to([ 'product-list' ]),
  82 + 'dataType' => 'json',
  83 + 'data' => new JsExpression('function(params) { return {q:params.term}; }'),
  84 + ],
  85 + 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
  86 + 'templateResult' => new JsExpression('function(city) { return city.text; }'),
  87 + 'templateSelection' => new JsExpression('function (city) { return city.text; }'),
  88 + ],
  89 + 'id' => 'add-to-order',
  90 + ]
  91 + );
  92 + ?>
  93 + </div>
  94 + <div class="col-md-3">
  95 + <?php
  96 + echo Html::textInput(
  97 + 'count-to-order',
  98 + null,
  99 + [
  100 + 'class' => 'form-control',
  101 + 'id' => 'count-to-order',
  102 + ]
  103 + );
  104 + ?>
  105 + </div>
  106 + <div class="col-md-1">
  107 + <?php
  108 + echo Html::a(
  109 + Html::icon(
  110 + 'plus-circle',
  111 + [
  112 + 'prefix' => 'fa fa-',
  113 + ]
  114 + ),
  115 + '#',
  116 + [
  117 + 'class' => 'variant-to-order',
  118 + ]
  119 + );
  120 + ?>
  121 + </div>
  122 + </div>
  123 + <div class="ln_solid"></div>
  124 + <div id="order-product-pjax" style="position: relative;">
  125 + <div class="row strong">
  126 + <div class="col-md-4">
  127 + <?php
  128 + echo Html::tag('strong', \Yii::t('order', 'Product'));
96 129 ?>
97   - <div class="order-product-container">
98   - <div id="order-product-pjax" style="position: relative;">
99   - <div class="row strong">
100   - <div class="col-md-4">
101   - <?php
102   - echo Html::tag('strong', \Yii::t('order', 'Product'));
103   - ?>
104   - </div>
105   - <div class="col-md-4">
106   - <?php
107   - echo Html::tag('strong', \Yii::t('order', 'Price'));
108   - ?>
109   - </div>
110   - <div class="col-md-4">
111   - <?php
112   - echo Html::tag('strong', \Yii::t('order', 'Count'));
113   - ?>
114   - </div>
115   - </div>
116   -
117   - <?php
118   -
119   - foreach ($model->orderProducts as $index => $orderProduct) {
120   - $sum += ($orderProduct->count * $orderProduct->price)
121   - ?>
122   - <div class="row row-order-product" data-price="<?=$orderProduct->price?>">
123   - <div class="col-md-4">
124   - <?php
125   - echo $form->field($orderProduct, "[$index]variant_id")
126   - ->hiddenInput()
127   - ->label(false);
128   - echo $orderProduct->variant->product->lang->title . '(' . $orderProduct->variant->sku . ')';
129   - ?>
130   - </div>
131   - <div class="col-md-4">
132   - <?php echo $orderProduct->price; ?>
133   - </div>
134   - <div class="col-md-3">
135   - <?php
136   - echo $form->field($orderProduct, "[$index]count")
137   - ->textInput(['class' => 'count'])
138   - ->label(false);
139   - ?>
140   - </div>
141   - <div class="col-md-1">
142   - <?php
143   - echo Html::a(
144   - Html::icon(
145   - 'trash-o',
146   - [
147   - 'prefix' => 'fa fa-',
148   - ]
149   - ),
150   - '#',
151   - [
152   - 'class' => 'remove-order-product',
153   - 'data' => [
154   - 'variant' => $orderProduct->variant->id,
155   - 'id' => $model->id,
156   - ],
157   - ]
158   - )
159   - ?>
160   - </div>
161   - </div>
162   - <?php
163   - }
164   - ?>
165   - </div>
166   - <div class="row">
167   - <div class="col-md-8">
168   - <?php
169   - echo Select2::widget(
170   - [
171   - 'name' => 'add-to-order',
172   - 'options' => [
173   - 'placeholder' => \Yii::t('order', 'Select product'),
174   - ],
175   - 'pluginOptions' => [
176   - 'allowClear' => true,
177   - 'minimumInputLength' => 3,
178   - 'language' => [
179   - 'errorLoading' => new JsExpression(
180   - "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }"
181   - ),
182   - ],
183   - 'ajax' => [
184   - 'url' => Url::to([ 'product-list' ]),
185   - 'dataType' => 'json',
186   - 'data' => new JsExpression('function(params) { return {q:params.term}; }'),
187   - ],
188   - 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
189   - 'templateResult' => new JsExpression('function(city) { return city.text; }'),
190   - 'templateSelection' => new JsExpression('function (city) { return city.text; }'),
191   - ],
192   - 'id' => 'add-to-order',
193   - ]
194   - );
195   - ?>
196   - </div>
197   - <div class="col-md-3">
198   - <?php
199   - echo Html::textInput(
200   - 'count-to-order',
201   - null,
202   - [
203   - 'class' => 'form-control',
204   - 'id' => 'count-to-order',
205   - ]
206   - );
207   - ?>
208   - </div>
209   - <div class="col-md-1">
210   - <?php
211   - echo Html::a(
212   - Html::icon(
213   - 'plus-circle',
214   - [
215   - 'prefix' => 'fa fa-',
216   - ]
217   - ),
218   - '#',
219   - [
220   - 'class' => 'variant-to-order',
221   - 'data-id' => $model->id,
222   - ]
223   - );
224   - ?>
225   - </div>
226   - </div>
227   - </div>
  130 + </div>
  131 + <div class="col-md-4">
228 132 <?php
229   - } else {
  133 + echo Html::tag('strong', \Yii::t('order', 'Price'));
230 134 ?>
231   - <div class="row">
232   - <div class="col-md-8">
233   - <?php
234   - echo Select2::widget(
235   - [
236   - 'name' => 'add-to-order',
237   - 'options' => [
238   - 'placeholder' => \Yii::t('order', 'Select product'),
239   - ],
240   - 'pluginOptions' => [
241   - 'allowClear' => true,
242   - 'minimumInputLength' => 3,
243   - 'language' => [
244   - 'errorLoading' => new JsExpression(
245   - "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }"
246   - ),
247   - ],
248   - 'ajax' => [
249   - 'url' => Url::to([ 'product-list' ]),
250   - 'dataType' => 'json',
251   - 'data' => new JsExpression('function(params) { return {q:params.term}; }'),
252   - ],
253   - 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
254   - 'templateResult' => new JsExpression('function(city) { return city.text; }'),
255   - 'templateSelection' => new JsExpression('function (city) { return city.text; }'),
256   - ],
257   - 'id' => 'add-to-order',
258   - ]
259   - );
260   - ?>
261   - </div>
262   - <div class="col-md-3">
263   - <?php
264   - echo Html::input(
265   - 'number',
266   - 'count-to-order',
267   - null,
268   - [
269   - 'class' => 'form-control',
270   - 'id' => 'count-to-order',
271   - ]
272   - );
273   - ?>
274   - </div>
275   - <div class="col-md-1">
276   - <?php
277   - echo Html::a(
278   - '<i class="fa fa-plus"></i>',
279   - '#',
280   - [
281   - 'class' => 'btn btn-success',
282   - 'id' => 'create-add',
283   - ]
284   - );
285   - ?>
286   - </div>
287   - </div>
288   -
289   - <div id="products"></div>
290   -
  135 + </div>
  136 + <div class="col-md-4">
291 137 <?php
292   - }
293   - ?>
  138 + echo Html::tag('strong', \Yii::t('order', 'Count'));
  139 + ?>
  140 + </div>
  141 + </div>
  142 + <div id="product-rows">
  143 + <?php
  144 + if (!$model->isNewRecord) {
  145 + foreach ($model->orderProducts as $index => $orderProduct) {
  146 + echo $this->render(
  147 + '_order_product',
  148 + [
  149 + 'orderProduct' => $orderProduct,
  150 + 'price' => $orderProduct->price,
  151 + 'index' => $index,
  152 + 'variant' => $orderProduct->variant,
  153 + 'form' => $form,
  154 + ]
  155 + );
  156 + }
  157 + }
  158 + ?>
  159 + </div>
  160 + </div>
  161 + </div>
294 162  
295 163 <div class="ln_solid"></div>
296   - <div class="form-group sum_all">ะ’ัะตะณะพ: <p><?=$sum?></p></div>
  164 +
297 165 <div class="form-group">
298 166 <?= Html::submitButton(
299 167 $model->isNewRecord ? Yii::t('order', 'Create') : Yii::t('order', 'Update'),
... ...
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>
... ...
web/js/order.js
... ... @@ -8,32 +8,19 @@ $(function() {
8 8 .data('id');
9 9 var variant = $(this)
10 10 .data('variant');
11   - var selector = '#order-product-pjax';
12   - $.post('/admin/order/delete-from-order', {
13   - id: id,
14   - variant: variant
15   - }, function(data) {
16   - var total_price = parseInt($(".sum_all")
17   - .children('p')
18   - .text());
19   - total_price = total_price - (current_price * number);
20   - console.log(number);
21   - $(".sum_all")
22   - .children('p')
23   - .text(total_price);
24   - $.pjax.reload(selector, {
25   - timeout: 5000,
26   - fragment: selector
27   - });
28   -
29   - });
30   -
31   -
32   -
33   -
34   -
35   -
  11 + $(this)
  12 + .parents('.row-order-product')
  13 + .remove();
  14 + var total_price = parseInt($(".sum_all")
  15 + .children('p')
  16 + .text());
  17 + total_price = total_price - (current_price * number);
  18 + console.log(number);
  19 + $(".sum_all")
  20 + .children('p')
  21 + .text(total_price);
36 22 });
  23 +
37 24 $(document)
38 25 .on('click', '.variant-to-order', function(e) {
39 26 e.preventDefault();
... ... @@ -43,25 +30,23 @@ $(function() {
43 30 .data('id');
44 31 if (id.val() && count.val()) {
45 32 var selector = '#order-product-pjax';
46   - showLoader(selector);
47 33 $.post('/admin/order/add-to-order', {
48 34 id: id.val(),
49 35 count: count.val(),
50 36 order: order
51 37 }, function(data) {
52   - $.pjax.reload(selector, {
53   - timeout: 5000,
54   - fragment: selector
55   - });
56   -
57   - var sum = $(".sum_all").children('p').text();
58   - $(".sum_all").children('p').html(parseInt(sum)+parseInt(data));
  38 + if (data.success) {
  39 + $('#product-rows')
  40 + .append(data.row);
  41 + var sum = $(".sum_all")
  42 + .children('p')
  43 + .text();
  44 + $(".sum_all")
  45 + .children('p')
  46 + .html(parseInt(sum) + parseInt(data));
  47 + }
59 48  
60 49 });
61   - id.val(null)
62   - .trigger('change');
63   - count.val(null)
64   - .trigger('change');
65 50 }
66 51 });
67 52 $(document).on('change', '.count', function() {
... ...