[ 'class' => AccessBehavior::className(), 'rules' => [ 'site' => [ [ 'actions' => [ 'login', 'error', ], 'allow' => true, ], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => [ 'POST' ], ], ], ]; } public function actionIndex() { $searchModel = new OrderSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render( 'index', [ 'dataProvider' => $dataProvider, 'searchModel' => $searchModel, ] ); } public function actionShow($id) { $model = $this->findModel((int) $id); $dataProvider = new ActiveDataProvider( [ 'query' => OrderProduct::find() ->where([ 'order_id' => (int) $id ]), 'pagination' => [ 'pageSize' => 20, ], ] ); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect([ 'index' ]); } else { $model_orderproduct = new OrderProduct(); return $this->renderAjax( 'show', [ 'model' => $model, 'model_orderproduct' => $model_orderproduct, 'dataProvider' => $dataProvider, ] ); } } public function actionLabelUpdate() { $model = Order::findOne($_POST[ 'order_id' ]); $model->label = $_POST[ 'label_id' ]; $model->save(); } public function actionView($id) { $model = $this->findModel($id); $dataProvider = new ActiveDataProvider([ 'query' => $model->getProducts(), ]); return $this->render( 'view', [ 'model' => $model, 'products' => $dataProvider, ] ); } public function actionPayUpdate() { $model = Order::findOne($_POST[ 'order_id' ]); $model->pay = $_POST[ 'pay_id' ]; $model->save(); } public function actionDelete($id) { $model = OrderProduct::findOne($id); $orderId = $model->order_id; if ($model->delete()) { return $this->actionUpdate($orderId); } } public function actionAdd() { if (!empty( \Yii::$app->request->post() )) { $id = \Yii::$app->request->post('OrderProduct')[ 'id' ]; $order_id = \Yii::$app->request->post('OrderProduct')[ 'order_id' ]; if (!empty( \Yii::$app->request->post('OrderProduct')[ 'count' ] )) { $count = \Yii::$app->request->post('OrderProduct')[ 'count' ]; } else { $count = 1; } $productVariant = ProductVariant::findOne($id); $model = OrderProduct::find() ->where( [ 'order_id' => $order_id, ] ) ->andWhere( [ 'product_variant_id' => $id, ] ) ->one(); if (!empty( $model )) { $model->count += $count; } else { $model = new OrderProduct(); $model->order_id = $order_id; $model->product_variant_id = $productVariant->id; $model->product_name = $productVariant->product->lang->title; $model->name = $productVariant->lang->title; $model->sku = $productVariant->sku; $model->price = $productVariant->price; $model->count = $count; } \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if ($model->save()) { return [ 'status' => 'success' ]; } else { return [ 'status' => 'fail' ]; } } else { throw new NotFoundHttpException(); } } public function actionCreate() { if (\Yii::$app->request->post('hasEditable')) { $orderProductId = \Yii::$app->request->post('editableKey'); $orderProduct = OrderProduct::findOne($orderProductId); $out = Json::encode( [ 'output' => '', 'message' => '', ] ); $posted = current(\Yii::$app->request->post('OrderProduct')); $post = [ 'OrderProduct' => $posted ]; if ($orderProduct->load($post)) { $orderProduct->save(); $output = ''; if (isset( $posted[ 'count' ] )) { $output = Yii::$app->formatter->asDecimal($orderProduct->count, 0); } $out = Json::encode( [ 'output' => $output, 'message' => '', ] ); } return $out; } $model = new Order(); $dataProvider = new ActiveDataProvider( [ 'query' => $model->getProducts() ->joinWith('productVariant'), ] ); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect([ 'index' ]); } else { return $this->render( 'create', [ 'model' => $model, 'dataProvider' => $dataProvider, ] ); } } public function actionPrint() { $orderId = Yii::$app->request->get("order_id"); if (!empty( $orderId )) { $order = $this->findModel($orderId); return $this->renderPartial( '@frontend/views/cabinet/order_print', [ 'order' => $order, ] ); } else { throw new NotFoundHttpException('The requested page does not exist.'); } } public function actionUpdate($id) { if (\Yii::$app->request->post('hasEditable')) { $orderProductId = \Yii::$app->request->post('editableKey'); $orderProduct = OrderProduct::findOne($orderProductId); $out = Json::encode( [ 'output' => '', 'message' => '', ] ); $posted = current(\Yii::$app->request->post('OrderProduct')); $post = [ 'OrderProduct' => $posted ]; if ($orderProduct->load($post)) { $orderProduct->save(); $output = ''; if (isset( $posted[ 'count' ] )) { $output = Yii::$app->formatter->asDecimal($orderProduct->count, 0); } $out = Json::encode( [ 'output' => $output, 'message' => '', ] ); } return $out; } $model = $this->findModel($id); $dataProvider = new ActiveDataProvider( [ 'query' => $model->getProducts() ->joinWith('productVariant.product.brand'), ] ); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect([ 'index' ]); } else { return $this->render( 'update', [ 'model' => $model, 'dataProvider' => $dataProvider, ] ); } } public function actionFindProduct($q = NULL, $id = NULL) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $out = [ 'results' => [ 'id' => '', 'sku' => '', ], ]; if (!is_null($q)) { $result = ProductVariant::find()// ->select( // [ // 'id', // 'sku', // 'product_lang.title AS name' // ] // ) ->joinWith('product.lang') ->where( [ 'like', 'sku', $q, ] ) ->limit(20) ->asArray() ->all(); $out[ 'results' ] = $result; } return $out; } public function actionSendSms() { $phone = \Yii::$app->request->post('phone'); $content = \Yii::$app->request->post('content'); $sender = \Yii::$app->sender; $result = $sender->send($phone, $content); return $phone . $content . $result; } // public function actionDeleteProduct($id, $order_id) // { // $model = OrderProduct::findOne($id); // $model->delete(); // \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; // return [ // 'result' => 'success', // 'id' => $id, // ]; // } protected function findModel($id) { if (( $model = Order::findOne($id) ) !== NULL) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }