/** * @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]); }
/** * @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']); }
/** * @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; }
/** * @param $token * @return Order */ protected function findOrder($token) { return Order::findOne(['token' => $token]); }
/** * @return \yii\db\ActiveQuery */ public function getOrder() { return $this->hasOne(Order::className(), ['id' => 'order_id']); }