diff --git a/backend/controllers/ProcurementController.php b/backend/controllers/ProcurementController.php index 31019fd..d5dd603 100644 --- a/backend/controllers/ProcurementController.php +++ b/backend/controllers/ProcurementController.php @@ -9,6 +9,8 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use common\models\Warehouse; +use common\models\Product; +use common\models\User; /** * ProcurementController implements the CRUD actions for Procurement model. @@ -33,12 +35,20 @@ class ProcurementController extends Controller */ public function actionIndex() { + + $warehouses = Warehouse::read(null); + $products = Product::read(null); + $users = User::read(null); + $searchModel = new ProcurementSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, + 'users' => $users, + 'products' => $products, + 'warehouses' => $warehouses, ]); } @@ -62,12 +72,42 @@ class ProcurementController extends Controller public function actionCreate() { $model = new Procurement(); + $model->scenario = 'create_general'; $model->id_user = Yii::$app->user->id; $warehouses = Warehouse::read(null); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ( count($warehouses) <= 0 ){ + throw new NotFoundHttpException( Yii::t('common/procurement' ,'No active warehouse found.' )); + } + + if ($model->load(Yii::$app->request->post()) && $model->validate()) { + + $connection = \Yii::$app->db; + + $transaction = $connection->beginTransaction(); + + try { + + $product = Product::findOne( $model->id_product ); + $model->stock = $product->stock; + $result = $model->save(false); + + $product->stock = $product->stock + $model->count; + $result &= $product->save(false); + + if ($result) { + $transaction->commit(); + } else { + $transaction->rollback(); + } + } catch (\Exception $e) { + $transaction->rollback(); + throw $e; + } + + return $this->redirect(['view', 'id' => $model->id_procurement]); } else { return $this->render('create', [ @@ -76,39 +116,64 @@ class ProcurementController extends Controller ]); } } - /** - * Updates an existing Procurement model. - * If update is successful, the browser will be redirected to the 'view' page. - * @param integer $id + * Creates a new Procurement model. + * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ - public function actionUpdate($id) + public function actionCreateProduct($id) { - $model = $this->findModel($id); + + $product = $this->findProduct($id); + + $model = new Procurement(); + + $warehouses = Warehouse::read(null); + + $model->id_user = Yii::$app->user->id; + $model->id_product = $product->id_product; + $model->stock = $product->stock; - if ($model->load(Yii::$app->request->post()) && $model->save()) { + $warehouses = Warehouse::read(null); + + if ( count($warehouses) <= 0 ){ + throw new NotFoundHttpException( Yii::t('common/procurement' ,'No active warehouse found.' )); + } + + if ($model->load(Yii::$app->request->post()) && $model->validate()) { + + $connection = \Yii::$app->db; + + $transaction = $connection->beginTransaction(); + + try { + + $result = $model->save(false); + + $product->stock = $product->stock + $model->count; + $result &= $product->save(false); + + if ($result) { + $transaction->commit(); + } else { + $transaction->rollback(); + } + } catch (\Exception $e) { + $transaction->rollback(); + throw $e; + } + + return $this->redirect(['view', 'id' => $model->id_procurement]); } else { - return $this->render('update', [ + return $this->render('create_product', [ 'model' => $model, + 'warehouses' =>$warehouses, + 'product' => $product ]); } } - /** - * Deletes an existing Procurement model. - * If deletion is successful, the browser will be redirected to the 'index' page. - * @param integer $id - * @return mixed - */ - public function actionDelete($id) - { - $this->findModel($id)->delete(); - - return $this->redirect(['index']); - } - /** * Finds the Procurement model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. @@ -124,4 +189,19 @@ class ProcurementController extends Controller throw new NotFoundHttpException('The requested page does not exist.'); } } + /** + * Finds the Product model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Procurement the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findProduct($id) + { + if (($model = Product::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } } diff --git a/backend/models/ProcurementSearch.php b/backend/models/ProcurementSearch.php index 203745b..82ab42b 100644 --- a/backend/models/ProcurementSearch.php +++ b/backend/models/ProcurementSearch.php @@ -12,14 +12,22 @@ use common\models\Procurement; */ class ProcurementSearch extends Procurement { + + public $date_start; + public $date_end; + + public $timestampStart; + public $timestampEnd; + /** * @inheritdoc */ public function rules() { return [ - [['id_procurement', 'id_warehouse', 'id_user', 'id_product', 'count', 'stock', 'purchase_price'], 'integer'], - [['description', 'created_at', 'updated_at'], 'safe'], + [[ 'id_warehouse', 'id_user', 'id_product', ], 'integer'], + [[ 'date_start', ], 'date' , 'timestampAttribute' => 'timestampStart' ], + [[ 'date_end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], ]; } @@ -56,18 +64,14 @@ class ProcurementSearch extends Procurement } $query->andFilterWhere([ - 'id_procurement' => $this->id_procurement, 'id_warehouse' => $this->id_warehouse, 'id_user' => $this->id_user, 'id_product' => $this->id_product, - 'count' => $this->count, - 'stock' => $this->stock, - 'purchase_price' => $this->purchase_price, - 'created_at' => $this->created_at, - 'updated_at' => $this->updated_at, ]); + + $query->andFilterWhere([ '>=', 'created_at', $this->date_start ] ); + $query->andFilterWhere([ '<', 'created_at', $this->timestampEnd ] ); - $query->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; } diff --git a/backend/views/procurement/_form_product.php b/backend/views/procurement/_form_product.php new file mode 100644 index 0000000..725492d --- /dev/null +++ b/backend/views/procurement/_form_product.php @@ -0,0 +1,54 @@ + + + +

+ $product, + 'attributes' =>[ + 'productCategoryName', + 'accountName', + 'product_number', + 'barcode', + 'stock', + ] +]); + +?> + +
+ + + + + field($model, 'id_warehouse')->dropDownList($warehouseOptions) ?> + + field($model, 'count')->textInput() ?> + + field($model, 'purchase_price')->textInput() ?> + + field($model, 'description')->textarea(['maxlength' => true]) ?> + +
+ isNewRecord ? Yii::t('common/procurement', 'Create') : Yii::t('common/procurement', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
diff --git a/backend/views/procurement/_search.php b/backend/views/procurement/_search.php index 85badf8..f5d80c8 100644 --- a/backend/views/procurement/_search.php +++ b/backend/views/procurement/_search.php @@ -2,12 +2,35 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; +use yii\helpers\ArrayHelper; +use kartik\widgets\DatePicker; /* @var $this yii\web\View */ /* @var $model backend\models\ProcurementSearch */ /* @var $form yii\widgets\ActiveForm */ ?> + Yii::t('common/product','All' ) ]; + +// $o[''] = Yii::t('common/product','All' ) ; + + $o = $all + $options; + + return $o; +} + +$products = mkOptions( ArrayHelper::map( $products ,'id_product','name') ); +$users = mkOptions( ArrayHelper::map( $users ,'id','username') ); +$warehouses = mkOptions( ArrayHelper::map( $warehouses ,'id_warehouse','name') ); + +?> + +