[ '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) { if (\Yii::$app->user->identity->isAdmin()) { $this->findModel($id) ->delete(); } return $this->redirect([ 'index' ]); } public function actionDeleteProduct($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); /** * @var User $user */ $user = \Yii::$app->user->identity; if ($model->isBlocked() && $model->edit_id !== \Yii::$app->user->id) { if (!$user->isAdmin()) { throw new ForbiddenHttpException(); } } $dataProvider = new ActiveDataProvider( [ 'query' => $model->getProducts() ->joinWith('productVariant.product.brand'), ] ); if (empty( $model->manager_id )) { $model->manager_id = \Yii::$app->user->id; } if ($model->load(Yii::$app->request->post()) && $model->save()) { $this->unblockOrder($model->id); 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.'); } } public function actionExitOrder($id) { $this->unblockOrder($id); return $this->redirect('index'); } public function actionBlockOrder() { if (!empty( \Yii::$app->request->post() )) { \Yii::$app->response->format = Response::FORMAT_JSON; $model = $this->findModel(\Yii::$app->request->post('id')); $model->edit_time = time(); $model->edit_id = \Yii::$app->user->id; //$date = new \DateTime("NOW"/*date('D, d M Y H:i:s', $model->edit_time)*/, new \DateTimeZone('Europe/Kiev')); $date = \Yii::$app->formatter->asDatetime($model->edit_time + 7200, 'php:G : i'); if ($model->save()) { return [ 'time' => $date, ]; } else { return [ 'success' => false, 'errors' => $model->errors, ]; } } } protected function unblockOrder($id) { $model = $this->findModel($id); $model->edit_time = 0; $model->edit_id = 0; $model->save(); } }