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 $token
  * @return Order
  */
 protected function findOrder($token)
 {
     return Order::findOne(['token' => $token]);
 }