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); - ?>
=$sum?>