OrderController.php 12.3 KB
<?php
    
    namespace artbox\order\controllers;

    use artbox\catalog\models\Product;
    use artbox\catalog\models\Variant;
    use artbox\order\models\Delivery;
    use artbox\order\models\Label;
    use artbox\order\models\OrderProduct;
    use artbox\order\models\Payment;
    use Yii;
    use artbox\order\models\Order;
    use artbox\order\models\OrderSearch;
    use yii\base\InvalidParamException;
    use yii\base\Model;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;

    /**
     * OrderController implements the CRUD actions for Order model.
     */
    class OrderController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'verbs' => [
                    'class'   => VerbFilter::className(),
                    'actions' => [
                        'delete' => [ 'POST' ],
                    ],
                ],
            ];
        }
    
        /**
         * Lists all Order models.
         *
         * @return mixed
         */
        public function actionIndex()
        {
            $searchModel = new OrderSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $labels = Label::find()
                ->select(
                    [
                        'title',
                        'id',
                    ]
                )
                ->joinWith('lang')
                ->andWhere(
                    [
                        'status' => true,
                    ]
                )
                ->indexBy('id')
                ->column();
        
            return $this->render(
                '@artbox/order/views/order/index',
                [
                    'searchModel'  => $searchModel,
                    'dataProvider' => $dataProvider,
                    'labels'       => $labels,
                ]
            );
        }
    
        /**
         * Displays a single Order model.
         *
         * @param integer $id
         *
         * @return mixed
         */
        public function actionView($id)
        {
            return $this->render(
                '@artbox/order/views/order/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()) {
                return $this->redirect(
                    [
                        'view',
                        'id' => $model->id,
                    ]
                );
            } 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,
                    ]
                );
            }
        }
    
        /**
         * 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()) {
                OrderProduct::saveItems(\Yii::$app->request->post('OrderProduct'), $id);
                return $this->redirect(
                    [
                        'view',
                        'id' => $model->id,
                    ]
                );
            } 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,
                    ]
                );
            }
        }
    
        /**
         * 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('lang', false)
                                   ->joinWith('product.lang', false)
                                   ->andWhere(
                                       [
                                           'like',
                                           'product_lang.title',
                                           $q,
                                       ]
                                   )
                                   ->orWhere(
                                       [
                                           'like',
                                           'variant_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()
        {
            $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
             */
            $variant = Variant::find()
                              ->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(
                    [
                        'order_id'   => $orderId,
                        'variant_id' => $id,
                        'sku'        => $variant->sku,
                        'price'      => $variant->price,
                        'count'      => $count,
                    ]
                );
            }
            $model->save();
        }
    
        /**
         * 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.');
            }
        }
    }