OrderController.php 12.2 KB
<?php
    
    namespace artweb\artbox\ecommerce\controllers;
    
    use artweb\artbox\ecommerce\models\OrderSearch;
    use phpDocumentor\Reflection\Types\Null_;
    use Yii;
    use yii\helpers\ArrayHelper;
    use yii\helpers\Json;
    use yii\web\Controller;
    use yii\filters\VerbFilter;
    use yii\data\ActiveDataProvider;
    use yii\web\HttpException;
    use artweb\artbox\ecommerce\models\Order;
    use artweb\artbox\ecommerce\models\OrderProduct;
    use artweb\artbox\ecommerce\models\ProductVariant;
    use yii\web\NotFoundHttpException;
    use developeruz\db_rbac\behaviors\AccessBehavior;
    
    class OrderController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'access' => [
                    '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 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 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.');
            }
        }
    }