Пример #1
0
 /**
  * Creates a new Price model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreateByPo($id)
 {
     if (($purchase = Purchase::findOne($id)) === null) {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
     $products = [];
     foreach ($purchase->getPurchaseDtls()->with('product.category')->all() as $detail) {
         $product = new ProductPrice(['id' => $detail->product_id, 'name' => $detail->product->name, 'category' => $detail->product->category->name, 'price' => $detail->price]);
         $products[$detail->product_id] = $product;
     }
     if (ProductPrice::loadMultiple($products, Yii::$app->request->post()) && ProductPrice::validateMultiple($products)) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $success = true;
             foreach ($products as $product) {
                 if (!$product->save(false)) {
                     $success = false;
                     break;
                 }
             }
             if ($success) {
                 $transaction->commit();
                 return $this->redirect(['list-po']);
             } else {
                 $transaction->rollBack();
             }
         } catch (\Exception $exc) {
             $transaction->rollBack();
             throw $exc;
         }
     }
     return $this->render('create_by_po', ['purchase' => $purchase, 'products' => $products, 'categories' => PriceCategory::find()->all()]);
 }
Пример #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = PurchaseModel::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         $query->where('1=0');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'supplier_id' => $this->supplier_id, 'branch_id' => $this->branch_id, 'date' => $this->date, 'value' => $this->value, 'discount' => $this->discount, 'status' => $this->status, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]);
     $query->andFilterWhere(['like', 'number', $this->number]);
     return $dataProvider;
 }
Пример #3
0
 public function getPurchase()
 {
     return $this->hasOne(Purchase::className(), ['id' => 'purchase_id']);
 }
Пример #4
0
 /**
  * Finds the Purchase model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Purchase the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Purchase::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }