diff --git a/controllers/OrderController.php b/controllers/OrderController.php index 6319945..e243341 100755 --- a/controllers/OrderController.php +++ b/controllers/OrderController.php @@ -1,7 +1,7 @@ search(Yii::$app->request->queryParams); $labels = Label::find() - ->select( - [ - 'title', - 'id', - ] - ) - ->joinWith('lang') - ->andWhere( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); - + ->select( + [ + 'title', + 'id', + ] + ) + ->joinWith('lang') + ->andWhere( + [ + 'status' => true, + ] + ) + ->indexBy('id') + ->column(); + return $this->render( '@artbox/order/views/order/index', [ @@ -69,7 +70,7 @@ ] ); } - + /** * Displays a single Order model. * @@ -86,7 +87,7 @@ ] ); } - + /** * Creates a new Order model. * If creation is successful, the browser will be redirected to the 'view' page. @@ -96,8 +97,9 @@ public function actionCreate() { $model = new Order(); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { + + if ($model->load(Yii::$app->request->post()) && $this->hasProducts() && $model->save()) { + OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $model->id); return $this->redirect( [ 'view', @@ -105,63 +107,18 @@ ] ); } else { - $labels = Label::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); - $deliveries = Delivery::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); - $payments = Payment::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); return $this->render( '@artbox/order/views/order/create', [ 'model' => $model, - 'labels' => $labels, - 'payments' => $payments, - 'deliveries' => $deliveries, + 'labels' => $this->getLabels(), + 'payments' => $this->getPayments(), + 'deliveries' => $this->getDeliveries(), ] ); } } - + /** * Updates an existing Order model. * If update is successful, the browser will be redirected to the 'view' page. @@ -173,8 +130,8 @@ public function actionUpdate($id) { $model = $this->findModel($id); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { + + if ($model->load(Yii::$app->request->post()) && $this->hasProducts() && $model->save()) { OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $id); return $this->redirect( [ @@ -183,63 +140,18 @@ ] ); } else { - $labels = Label::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); - $deliveries = Delivery::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); - $payments = Payment::find() - ->joinWith('lang') - ->select( - [ - 'title', - 'id', - ] - ) - ->where( - [ - 'status' => true, - ] - ) - ->indexBy('id') - ->column(); return $this->render( '@artbox/order/views/order/update', [ 'model' => $model, - 'labels' => $labels, - 'payments' => $payments, - 'deliveries' => $deliveries, + 'labels' => $this->getLabels(), + 'payments' => $this->getPayments(), + 'deliveries' => $this->getDeliveries(), ] ); } } - + /** * Deletes an existing Order model. * If deletion is successful, the browser will be redirected to the 'index' page. @@ -252,14 +164,19 @@ { $this->findModel($id) ->delete(); - + return $this->redirect([ 'index' ]); } - - public function actionProductList($q = null, $id = null) + + /** + * @param string $q + * @param null $id + * + * @return array + */ + public function actionProductList(string $q, $id = null) { - $response = \Yii::$app->response; - $response->format = $response::FORMAT_JSON; + \Yii::$app->response->format = Response::FORMAT_JSON; $out = [ 'results' => [ 'id' => '', @@ -274,21 +191,22 @@ $variants = Variant::find() ->joinWith('lang', false) ->joinWith('product.lang', false) - ->andWhere( + ->where( [ - 'like', + 'ilike', 'product_lang.title', $q, ] ) ->orWhere( [ - 'like', + 'ilike', 'variant_lang.title', $q, ] ) ->orWhere([ 'variant.sku' => $q ]) + ->limit(20) ->all(); foreach ($variants as $variant) { $out[ 'results' ][] = [ @@ -311,57 +229,62 @@ } return $out; } - + + /** + * @return string + */ 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( + '@artbox/order/views/order/_order_product', + [ + 'orderProduct' => $orderProduct, + 'index' => rand(0, 10000), + 'variant' => $variant, + 'price' => $variant->price, + 'form' => $form, + ] + ); + + return [ + 'success' => true, + 'row' => $row, + ]; } - $model->save(); + + return [ + 'success' => false, + 'message' => \Yii::t('app', 'Product not found'), + ]; } - + /** * Finds the Order model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. @@ -379,4 +302,84 @@ throw new NotFoundHttpException('The requested page does not exist.'); } } + + /** + * @return bool + */ + 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/models/OrderSearch.php b/models/OrderSearch.php index 5fd8740..6137063 100755 --- a/models/OrderSearch.php +++ b/models/OrderSearch.php @@ -65,7 +65,12 @@ 'count(*)', ] ) - ->groupBy('order.id'); + ->groupBy('order.id') + ->orderBy( + [ + 'order.id' => SORT_DESC, + ] + ); // add conditions that should always apply here diff --git a/views/order/_form.php b/views/order/_form.php index 79eaf5f..3a1dd80 100755 --- a/views/order/_form.php +++ b/views/order/_form.php @@ -43,7 +43,7 @@ ->textInput([ 'maxlength' => true ]) ?> = $form->field($model, 'comment') - ->textInput([ 'maxlength' => true ]) ?> + ->textarea() ?> = $form->field($model, 'label_id') ->dropDownList($labels) ?> @@ -53,137 +53,108 @@ = $form->field($model, 'payment_id') ->dropDownList($payments) ?> - isNewRecord) { + +