From 543a6adb504a8f9f6dc83bf812e40569b4de7076 Mon Sep 17 00:00:00 2001 From: stes Date: Fri, 12 Jan 2018 18:02:24 +0200 Subject: [PATCH] order --- controllers/OrderController.php | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------- views/order/_form.php | 348 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ views/order/_order_product.php | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ web/js/order.js | 59 ++++++++++++++++++++++------------------------------------- 4 files changed, 302 insertions(+), 333 deletions(-) create mode 100644 views/order/_order_product.php diff --git a/controllers/OrderController.php b/controllers/OrderController.php index 0f7a3b1..3982a23 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -10,11 +10,12 @@ use Yii; use artbox\order\models\Order; use artbox\order\models\OrderSearch; - use yii\base\InvalidParamException; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\filters\AccessControl; + use yii\web\Response; + use yii\widgets\ActiveForm; /** * OrderController implements the CRUD actions for Order model. @@ -353,71 +354,54 @@ public function actionAddToOrder() { - $id = \Yii::$app->request->post('id'); - $count = \Yii::$app->request->post('count'); - $orderId = \Yii::$app->request->post('order'); - if (empty($id) || empty($count)) { - throw new InvalidParamException(\Yii::t('order', 'Set id and count')); - } - $order = Order::find() - ->where([ 'id' => $orderId ]) - ->one(); - if (empty($order)) { - throw new NotFoundHttpException(\Yii::t('order', 'Order not found')); - } /** - * @var Variant $variant + * @var Variant $variant + * @var ActiveForm $form + * @var OrderProduct $orderProduct */ + \Yii::$app->response->format = Response::FORMAT_JSON; + $id = \Yii::$app->request->post('id'); + $count = \Yii::$app->request->post('count') ? \Yii::$app->request->post('count') : 1; + $variant = Variant::find() - ->where([ 'id' => $id ]) + ->with('product.lang') + ->where( + [ + 'id' => $id, + ] + ) ->one(); - if (empty($variant)) { - throw new NotFoundHttpException(\Yii::t('order', 'Variant not found')); - } - /** - * @var OrderProduct $model - */ - $model = OrderProduct::find() - ->where( - [ - 'order_id' => $orderId, - 'variant_id' => $id, - ] - ) - ->one(); - if ($model) { - $model->count += $count; - } else { - $model = new OrderProduct( + + if ($variant) { + $form = new ActiveForm(); + $orderProduct = new OrderProduct( [ - 'order_id' => $orderId, - 'variant_id' => $id, - 'sku' => $variant->sku, - 'price' => $variant->price, 'count' => $count, + 'variant_id' => $variant->id, ] ); + + $row = $this->renderPartial( + '_order_product', + [ + 'orderProduct' => $orderProduct, + 'index' => rand(0, 10000), + 'variant' => $variant, + 'price' => $variant->price, + 'form' => $form, + ] + ); + + return [ + 'success' => true, + 'row' => $row, + ]; } - $model->save(); - $sum = $count * $variant->price; - - return $sum; - } - public function actionDeleteFromOrder() - { - $id = \Yii::$app->request->post('id'); - $variant = \Yii::$app->request->post('variant'); - $product = OrderProduct::find() - ->where( - [ - 'order_id' => $id, - 'variant_id' => $variant, - ] - ) - ->one() - ->delete(); - return 'ok'; + return [ + 'success' => false, + 'message' => \Yii::t('app', 'Product not found'), + ]; } /** @@ -445,4 +429,81 @@ 'model' => $model ]); } + + protected function hasProducts() + { + if (empty(\Yii::$app->request->post('OrderProduct'))) { + \Yii::$app->session->setFlash('error', \Yii::t('app', 'Заказ не может быть без товаров')); + + return false; + } + + return true; + } + + /** + * @return array + */ + protected function getLabels() + { + return Label::find() + ->joinWith('lang') + ->select( + [ + 'title', + 'id', + ] + ) + ->where( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); + } + + /** + * @return array + */ + protected function getPayments() + { + return Payment::find() + ->joinWith('lang') + ->select( + [ + 'title', + 'id', + ] + ) + ->where( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); + } + + /** + * @return array + */ + protected function getDeliveries() + { + return Delivery::find() + ->joinWith('lang') + ->select( + [ + 'title', + 'id', + ] + ) + ->where( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); + } } diff --git a/views/order/_form.php b/views/order/_form.php index 1ad2399..5714204 100755 --- a/views/order/_form.php +++ b/views/order/_form.php @@ -4,7 +4,6 @@ use yii\bootstrap\Html; use yii\helpers\Url; use yii\web\JsExpression; - use yii\web\View; use yii\widgets\ActiveForm; /** @@ -15,42 +14,6 @@ * @var array $payments * @var ActiveForm $form */ - - $js = <<< JS -$(document).on('click', '#create-add', function(e) { - e.preventDefault(); - var variant_id = $('#add-to-order').val(); - var count = $('#count-to-order').val(); - - $.ajax({ - url: '/admin/order-product/add', - type: "GET", - data: { - variant_id: variant_id, - count: count - }, - success: function(data) { - if (data.id != 0) { - var row = $('#products #' + data.id); - if (row.length !== 0) { - row.find('.count-input').val(function(i, oldVal) { - return parseInt(oldVal, 10) + data.count; - }); - } else { - $('#products').append(data.row); - } - } - } - }); -}); -$(document).on('click', '.delete-product-row', function(e) { - e.preventDefault(); - $(this).parent().parent().remove(); -}) -JS; - - $this->registerJs($js, View::POS_READY); - ?>
@@ -58,15 +21,18 @@ JS; isNewRecord) { - echo $form->field($model, 'user_id') - ->textInput(); - } + // if ($model->isNewRecord) { + // echo $form->field($model, 'user_id') + // ->textInput(); + // } ?> field($model, 'name') ->textInput([ 'maxlength' => true ]) ?> + field($model, 'secondname') + ->textInput([ 'maxlength' => true ]) ?> + field($model, 'phone') ->textInput([ 'maxlength' => true ]) ?> @@ -80,7 +46,7 @@ JS; ->textInput([ 'maxlength' => true ]) ?> field($model, 'comment') - ->textInput([ 'maxlength' => true ]) ?> + ->textarea() ?> field($model, 'label_id') ->dropDownList($labels) ?> @@ -90,210 +56,112 @@ JS; field($model, 'payment_id') ->dropDownList($payments) ?> - isNewRecord) { + +
+ +
+
+
+ 'add-to-order', + 'options' => [ + 'placeholder' => \Yii::t('order', 'Select product'), + ], + 'pluginOptions' => [ + 'allowClear' => true, + 'minimumInputLength' => 3, + 'language' => [ + 'errorLoading' => new JsExpression( + "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }" + ), + ], + 'ajax' => [ + 'url' => Url::to([ 'product-list' ]), + 'dataType' => 'json', + 'data' => new JsExpression('function(params) { return {q:params.term}; }'), + ], + 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), + 'templateResult' => new JsExpression('function(city) { return city.text; }'), + 'templateSelection' => new JsExpression('function (city) { return city.text; }'), + ], + 'id' => 'add-to-order', + ] + ); + ?> +
+
+ 'form-control', + 'id' => 'count-to-order', + ] + ); + ?> +
+
+ 'fa fa-', + ] + ), + '#', + [ + 'class' => 'variant-to-order', + ] + ); + ?> +
+
+
+
+
+
+ -
-
-
-
- -
-
- -
-
- -
-
- - orderProducts as $index => $orderProduct) { - $sum += ($orderProduct->count * $orderProduct->price) - ?> -
-
- field($orderProduct, "[$index]variant_id") - ->hiddenInput() - ->label(false); - echo $orderProduct->variant->product->lang->title . '(' . $orderProduct->variant->sku . ')'; - ?> -
-
- price; ?> -
-
- field($orderProduct, "[$index]count") - ->textInput(['class' => 'count']) - ->label(false); - ?> -
-
- 'fa fa-', - ] - ), - '#', - [ - 'class' => 'remove-order-product', - 'data' => [ - 'variant' => $orderProduct->variant->id, - 'id' => $model->id, - ], - ] - ) - ?> -
-
- -
-
-
- 'add-to-order', - 'options' => [ - 'placeholder' => \Yii::t('order', 'Select product'), - ], - 'pluginOptions' => [ - 'allowClear' => true, - 'minimumInputLength' => 3, - 'language' => [ - 'errorLoading' => new JsExpression( - "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }" - ), - ], - 'ajax' => [ - 'url' => Url::to([ 'product-list' ]), - 'dataType' => 'json', - 'data' => new JsExpression('function(params) { return {q:params.term}; }'), - ], - 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), - 'templateResult' => new JsExpression('function(city) { return city.text; }'), - 'templateSelection' => new JsExpression('function (city) { return city.text; }'), - ], - 'id' => 'add-to-order', - ] - ); - ?> -
-
- 'form-control', - 'id' => 'count-to-order', - ] - ); - ?> -
-
- 'fa fa-', - ] - ), - '#', - [ - 'class' => 'variant-to-order', - 'data-id' => $model->id, - ] - ); - ?> -
-
-
+
+
-
-
- 'add-to-order', - 'options' => [ - 'placeholder' => \Yii::t('order', 'Select product'), - ], - 'pluginOptions' => [ - 'allowClear' => true, - 'minimumInputLength' => 3, - 'language' => [ - 'errorLoading' => new JsExpression( - "function() {return '" . \Yii::t('order', 'Waiting for results') . "'; }" - ), - ], - 'ajax' => [ - 'url' => Url::to([ 'product-list' ]), - 'dataType' => 'json', - 'data' => new JsExpression('function(params) { return {q:params.term}; }'), - ], - 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), - 'templateResult' => new JsExpression('function(city) { return city.text; }'), - 'templateSelection' => new JsExpression('function (city) { return city.text; }'), - ], - 'id' => 'add-to-order', - ] - ); - ?> -
-
- 'form-control', - 'id' => 'count-to-order', - ] - ); - ?> -
-
- ', - '#', - [ - 'class' => 'btn btn-success', - 'id' => 'create-add', - ] - ); - ?> -
-
- -
- +
+
+ echo Html::tag('strong', \Yii::t('order', 'Count')); + ?> +
+
+
+ isNewRecord) { + foreach ($model->orderProducts as $index => $orderProduct) { + echo $this->render( + '_order_product', + [ + 'orderProduct' => $orderProduct, + 'price' => $orderProduct->price, + 'index' => $index, + 'variant' => $orderProduct->variant, + 'form' => $form, + ] + ); + } + } + ?> +
+
+
-
Всего:

+
isNewRecord ? Yii::t('order', 'Create') : Yii::t('order', 'Update'), diff --git a/views/order/_order_product.php b/views/order/_order_product.php new file mode 100644 index 0000000..26bdf6e --- /dev/null +++ b/views/order/_order_product.php @@ -0,0 +1,55 @@ + + +
+
+ field($orderProduct, "[$index]variant_id") + ->hiddenInput() + ->label(false); + echo $variant->product->lang->title . '(' . $variant->sku . ')'; + ?> +
+
+ +
+
+ field($orderProduct, "[$index]count") + ->textInput() + ->label(false); + ?> +
+
+ 'fa fa-', + ] + ), + '#', + [ + 'class' => 'remove-order-product', + ] + ) + ?> +
+
diff --git a/web/js/order.js b/web/js/order.js index 940e4d1..493feec 100755 --- a/web/js/order.js +++ b/web/js/order.js @@ -8,32 +8,19 @@ $(function() { .data('id'); var variant = $(this) .data('variant'); - var selector = '#order-product-pjax'; - $.post('/admin/order/delete-from-order', { - id: id, - variant: variant - }, function(data) { - var total_price = parseInt($(".sum_all") - .children('p') - .text()); - total_price = total_price - (current_price * number); - console.log(number); - $(".sum_all") - .children('p') - .text(total_price); - $.pjax.reload(selector, { - timeout: 5000, - fragment: selector - }); - - }); - - - - - - + $(this) + .parents('.row-order-product') + .remove(); + var total_price = parseInt($(".sum_all") + .children('p') + .text()); + total_price = total_price - (current_price * number); + console.log(number); + $(".sum_all") + .children('p') + .text(total_price); }); + $(document) .on('click', '.variant-to-order', function(e) { e.preventDefault(); @@ -43,25 +30,23 @@ $(function() { .data('id'); if (id.val() && count.val()) { var selector = '#order-product-pjax'; - showLoader(selector); $.post('/admin/order/add-to-order', { id: id.val(), count: count.val(), order: order }, function(data) { - $.pjax.reload(selector, { - timeout: 5000, - fragment: selector - }); - - var sum = $(".sum_all").children('p').text(); - $(".sum_all").children('p').html(parseInt(sum)+parseInt(data)); + if (data.success) { + $('#product-rows') + .append(data.row); + var sum = $(".sum_all") + .children('p') + .text(); + $(".sum_all") + .children('p') + .html(parseInt(sum) + parseInt(data)); + } }); - id.val(null) - .trigger('change'); - count.val(null) - .trigger('change'); } }); $(document).on('change', '.count', function() { -- libgit2 0.21.4