[ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => [ 'POST' ], ], ], 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => [ 'login', 'error', ], 'allow' => true, ], [ 'allow' => true, 'roles' => [ '@' ], ], ], ], ]; } public function actions() { return [ 'index' => [ 'class' => Index::className(), 'columns' => [ 'id' => [ 'type' => Index::NUMBER_COL, ], 'name' => [ 'type' => Index::ACTION_COL, 'columnConfig' => [ 'buttonsTemplate' => '{edit}{delete}', ], ], 'phone' => [ 'type' => Index::STRING_COL, ], 'email' => [ 'type' => Index::STRING_COL, ], 'label' => [ 'type' => Index::RELATION_COL, 'columnConfig' => [ 'relationField' => 'title', ], ], 'created_at' => [ 'type' => Index::DATETIME_COL, ], ], 'model' => Order::className(), 'hasLanguage' => false, 'enableMassDelete' => false, 'modelPrimaryKey' => 'id', ], ]; } /** * Displays a single Order model. * * @param integer $id * * @return mixed */ public function actionView($id) { return $this->render( 'view', [ 'model' => $this->findModel($id), ] ); } /** * Creates a new Order model. * If creation is successful, the browser will be redirected to the 'view' page. * * @return mixed */ public function actionCreate() { $model = new Order(); if ($model->load(Yii::$app->request->post()) && $model->save()) { if (!empty(\Yii::$app->request->post('Product'))) { foreach (\Yii::$app->request->post('Product') as $id => $count) { /** * @var Variant $variant */ $variant = Variant::findOne($id); if ($variant) { $orderProduct = new OrderProduct( [ 'variant_id' => $variant->id, 'order_id' => $model->id, 'sku' => $variant->sku, 'price' => $variant->price, 'count' => $count, ] ); $orderProduct->save(); } } } return $this->redirect( [ 'view', 'id' => $model->id, ] ); } else { $labels = Label::find() ->joinWith('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); $deliveries = Delivery::find() ->joinWith('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); $payments = Payment::find() ->joinWith('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); return $this->render( 'create', [ 'model' => $model, 'labels' => $labels, 'payments' => $payments, 'deliveries' => $deliveries, ] ); } } /** * Updates an existing Order model. * If update is successful, the browser will be redirected to the 'view' page. * * @param integer $id * * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save() && !empty( \Yii::$app->request->post( 'OrderProduct' ) )) { //print_r(\Yii::$app->request->post('OrderProduct')); die(); OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $id); return $this->redirect( [ 'view', 'id' => $model->id, ] ); } else { $labels = Label::find() ->joinWith('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); $deliveries = Delivery::find() ->joinWith('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); $payments = Payment::find() ->joinWith('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); return $this->render( 'update', [ 'model' => $model, 'labels' => $labels, 'payments' => $payments, 'deliveries' => $deliveries, ] ); } } /** * Deletes an existing Order model. * If deletion is successful, the browser will be redirected to the 'index' page. * * @param integer $id * * @return mixed */ public function actionDelete($id) { $this->findModel($id) ->delete(); return $this->redirect([ 'index' ]); } public function actionProductList($q = null, $id = null) { $response = \Yii::$app->response; $response->format = $response::FORMAT_JSON; $out = [ 'results' => [ 'id' => '', 'text' => '', ], ]; if (!is_null($q)) { $out[ 'results' ] = []; /** * @var Variant[] $variants */ $variants = Variant::find() ->joinWith('product.lang', false) ->where( [ 'like', 'product_lang.title', $q, ] ) ->orWhere([ 'variant.sku' => $q ]) ->all(); foreach ($variants as $variant) { $out[ 'results' ][] = [ 'id' => $variant->id, 'text' => $variant->product->lang->title, ]; } } elseif ($id > 0) { /** * @var Variant $variant */ $variant = Variant::find() ->with('lang', 'product.lang') ->where([ 'id' => $id ]) ->one(); $out[ 'results' ] = [ 'id' => $id, 'text' => $variant->product->lang->title, ]; } return $out; } public function actionAddToOrder() { /** * @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() ->with('product.lang') ->where( [ 'id' => $id, ] ) ->one(); if ($variant) { $form = new ActiveForm(); $orderProduct = new OrderProduct( [ '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, 'price' => $variant->price * $count, ]; } 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. * * @param integer $id * * @return Order the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (( $model = Order::findOne($id) ) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } public function actionPrint($id) { $this->layout = false; $model = $this->findModel($id); return $this->renderPartial( '@backend/views/layouts/print', [ '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('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); } /** * @return array */ protected function getPayments() { return Payment::find() ->joinWith('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); } /** * @return array */ protected function getDeliveries() { return Delivery::find() ->joinWith('language') ->select( [ 'title', 'id', ] ) ->where( [ 'status' => true, ] ) ->indexBy('id') ->column(); } }