Example #1
0
 /**
  * @param $node
  * @return string
  * @throws NotFoundHttpException
  */
 public function actionList($node)
 {
     /** @var Order $order */
     $order = Order::findOne($node);
     if (!$order) {
         throw new NotFoundHttpException();
     }
     $newProduct = new AddProductForm();
     if ($newProduct->load(\Yii::$app->request->post()) && $newProduct->validate()) {
         /** @var OrderData $existingLine */
         $existingLine = OrderData::find()->where(['product_id' => $newProduct->product_id, 'order_id' => $order->id])->one();
         if ($existingLine) {
             $existingLine->quantity += $newProduct->quantity;
             $existingLine->save();
         } else {
             /** @var Product $product */
             $product = Product::findOne($newProduct->product_id);
             if ($product) {
                 $line = new OrderData(['product_id' => $product->id, 'quantity' => $newProduct->quantity, 'price' => $product->price]);
                 $order->link('orderLines', $line);
             }
         }
     }
     return $this->render('list', ['order' => $order, 'newProduct' => $newProduct]);
 }
Example #2
0
 /**
  * @param $id
  * @return \yii\web\Response
  * @throws NotFoundHttpException
  * @throws \Exception
  */
 public function actionDelete($id)
 {
     /** @var Order $model */
     $model = Order::findOne($id);
     if (!$model) {
         throw new NotFoundHttpException();
     }
     $model->delete();
     return $this->redirect(['list']);
 }
Example #3
0
 /**
  * @param $params
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Order::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_ASC], 'attributes' => ['id', 'name', 'address', 'phone', 'country_id', 'status', 'created_at', 'updated_at']]]);
     $this->load($params);
     if (!$this->validate()) {
         $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['status' => $this->status, 'country_id' => $this->country_id]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     $query->andFilterWhere(['like', 'address', $this->address]);
     $query->andFilterWhere(['like', 'phone', $this->phone]);
     return $dataProvider;
 }
Example #4
0
 /**
  * @param $token
  * @return Order
  */
 protected function findOrder($token)
 {
     return Order::findOne(['token' => $token]);
 }
Example #5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getOrder()
 {
     return $this->hasOne(Order::className(), ['id' => 'order_id']);
 }