/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = PurchaseOrderDetail::find(); if (!empty($params['purchase_order_id'])) { $query->where(['purchase_order_id' => $params['purchase_order_id']]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'purchase_order_id' => $this->purchase_order_id, 'purchase_order_id' => $this->purchase_order_id, 'unit_price' => $this->unit_price, 'quantity' => $this->quantity, 'discount_percent' => $this->discount_percent, 'is_offer' => $this->is_offer]); $query->andFilterWhere(['like', 'product_name', $this->product_name])->andFilterWhere(['like', 'product_code', $this->product_code])->andFilterWhere(['like', 'color_code', $this->color_code])->andFilterWhere(['like', 'customer_note', $this->customer_note])->andFilterWhere(['like', 'user_note', $this->user_note]); return $dataProvider; }
/** * Updates an existing PurchaseOrder model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $username = Yii::$app->user->identity->username; if ($model = $this->findModel($id)) { $this->purchase_order_id = $model->id; $old_status = $model->status; if (Yii::$app->request->isPost && $model->update2(Yii::$app->request->post())) { $new_status = $model->status; $details = PurchaseOrderDetail::find()->where(['purchase_order_id' => $model->id])->all(); switch ($old_status) { case PurchaseOrder::STATUS_NEW: case PurchaseOrder::STATUS_PENDING: switch ($new_status) { case PurchaseOrder::STATUS_SUCCESS: foreach ($details as $item) { if ($product = Product::findOne($item->product_id)) { $product->order_quantity -= $item->quantity; $product->sold_quantity += $item->quantity; $product->total_revenue += $item->quantity * $item->unit_price; $product->save(); } } break; case PurchaseOrder::STATUS_REJECT: case PurchaseOrder::STATUS_CANCELLED: case PurchaseOrder::STATUS_FAIL: foreach ($details as $item) { if ($product = Product::findOne($item->product_id)) { $product->order_quantity -= $item->quantity; $product->available_quantity += $item->quantity; $product->save(); } } default: } break; case PurchaseOrder::STATUS_SUCCESS: switch ($new_status) { case PurchaseOrder::STATUS_NEW: case PurchaseOrder::STATUS_PENDING: foreach ($details as $item) { if ($product = Product::findOne($item->product_id)) { $product->sold_quantity -= $item->quantity; $product->order_quantity += $item->quantity; $product->total_revenue -= $item->quantity * $item->unit_price; $product->save(); } } break; case PurchaseOrder::STATUS_REJECT: case PurchaseOrder::STATUS_CANCELLED: case PurchaseOrder::STATUS_FAIL: foreach ($details as $item) { if ($product = Product::findOne($item->product_id)) { $product->sold_quantity -= $item->quantity; $product->available_quantity += $item->quantity; $product->total_revenue -= $item->quantity * $item->unit_price; $product->save(); } } default: } break; case PurchaseOrder::STATUS_REJECT: case PurchaseOrder::STATUS_CANCELLED: case PurchaseOrder::STATUS_FAIL: switch ($new_status) { case PurchaseOrder::STATUS_NEW: case PurchaseOrder::STATUS_PENDING: foreach ($details as $item) { if ($product = Product::findOne($item->product_id)) { $product->available_quantity -= $item->quantity; $product->order_quantity += $item->quantity; $product->save(); } } break; case PurchaseOrder::STATUS_SUCCESS: foreach ($details as $item) { if ($product = Product::findOne($item->product_id)) { $product->available_quantity -= $item->quantity; $product->sold_quantity += $item->quantity; $product->total_revenue += $item->quantity * $item->unit_price; $product->save(); } } default: } break; default: } return $this->goBack(Url::previous()); } else { return $this->render('update', ['username' => $username, 'model' => $model]); } } else { throw new NotFoundHttpException(); } }