From 28caac03dc8980e17c465525c61e2189c90fafde Mon Sep 17 00:00:00 2001 From: rocho Date: Fri, 25 Sep 2015 08:57:05 +0200 Subject: [PATCH] add changes to procurement --- backend/components/AdminMenuStructure.php | 1 + backend/controllers/ProcurementController.php | 127 ++++++++++++++++++ backend/models/ProcurementSearch.php | 74 ++++++++++ backend/views/procurement/_form.php | 38 ++++++ backend/views/procurement/_search.php | 45 +++++++ backend/views/procurement/create.php | 23 ++++ backend/views/procurement/index.php | 49 +++++++ backend/views/procurement/update.php | 23 ++++ backend/views/procurement/view.php | 44 ++++++ common/models/Procurement.php | 122 +++++++++++++++++ common/models/Warehouse.php | 16 +++ .../m150925_055052_add__table_procurement.php | 47 +++++++ 12 files changed, 609 insertions(+) create mode 100644 backend/controllers/ProcurementController.php create mode 100644 backend/models/ProcurementSearch.php create mode 100644 backend/views/procurement/_form.php create mode 100644 backend/views/procurement/_search.php create mode 100644 backend/views/procurement/create.php create mode 100644 backend/views/procurement/index.php create mode 100644 backend/views/procurement/update.php create mode 100644 backend/views/procurement/view.php create mode 100644 common/models/Procurement.php create mode 100644 console/migrations/m150925_055052_add__table_procurement.php diff --git a/backend/components/AdminMenuStructure.php b/backend/components/AdminMenuStructure.php index 153b0ff..47b26a8 100644 --- a/backend/components/AdminMenuStructure.php +++ b/backend/components/AdminMenuStructure.php @@ -41,6 +41,7 @@ class AdminMenuStructure{ $items[] = ['label' => 'Termék kategóriák', 'url' => ['/product-category/index'] ]; $items[] = ['label' => 'Bérlet típusok', 'url' => ['/ticket-type/index'] ]; $items[] = ['label' => 'Termékek', 'url' => ['/product/index'] ]; + $items[] = ['label' => 'Beszerzések', 'url' => ['/procurement/index'] ]; if ( count($items) > 0 ){ $userMainMenu = ['label' => 'Beállítások', 'url' => null, diff --git a/backend/controllers/ProcurementController.php b/backend/controllers/ProcurementController.php new file mode 100644 index 0000000..31019fd --- /dev/null +++ b/backend/controllers/ProcurementController.php @@ -0,0 +1,127 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['post'], + ], + ], + ]; + } + + /** + * Lists all Procurement models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new ProcurementSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Procurement model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Procurement model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Procurement(); + + $model->id_user = Yii::$app->user->id; + + $warehouses = Warehouse::read(null); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id_procurement]); + } else { + return $this->render('create', [ + 'model' => $model, + 'warehouses' =>$warehouses + ]); + } + } + + /** + * Updates an existing Procurement model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id_procurement]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * 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. + * @param integer $id + * @return Procurement the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Procurement::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 new file mode 100644 index 0000000..203745b --- /dev/null +++ b/backend/models/ProcurementSearch.php @@ -0,0 +1,74 @@ + $query, + ]); + + $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_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(['like', 'description', $this->description]); + + return $dataProvider; + } +} diff --git a/backend/views/procurement/_form.php b/backend/views/procurement/_form.php new file mode 100644 index 0000000..b402e8a --- /dev/null +++ b/backend/views/procurement/_form.php @@ -0,0 +1,38 @@ + + +
+ + + + field($model, 'productIdentifier')->textInput()->hint(Yii::t('common/procurement', "Product name, product number or barcode")) ?> + + 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 new file mode 100644 index 0000000..85badf8 --- /dev/null +++ b/backend/views/procurement/_search.php @@ -0,0 +1,45 @@ + + + diff --git a/backend/views/procurement/create.php b/backend/views/procurement/create.php new file mode 100644 index 0000000..415e4aa --- /dev/null +++ b/backend/views/procurement/create.php @@ -0,0 +1,23 @@ +title = Yii::t('common/procurement', 'Create Procurement'); +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/procurement', 'Procurements'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + 'warehouses' => $warehouses + ]) ?> + +
diff --git a/backend/views/procurement/index.php b/backend/views/procurement/index.php new file mode 100644 index 0000000..c2c7b85 --- /dev/null +++ b/backend/views/procurement/index.php @@ -0,0 +1,49 @@ +title = Yii::t('common/procurement', 'Procurements'); +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ render('_search', ['model' => $searchModel]); ?> + +

+ 'btn btn-success']) ?> +

+ + $dataProvider, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + [ + 'attribute' => 'id_warehouse', + 'value' => 'wareHouseName' + + ], + 'id_user', + 'id_product', + 'count', + 'stock', + 'purchase_price', + // 'description', + 'created_at:datetime', + 'updated_at:datetime', + + [ + 'class' => 'yii\grid\ActionColumn', + 'template' => '{view}' + + ], + ], + ]); ?> + +
diff --git a/backend/views/procurement/update.php b/backend/views/procurement/update.php new file mode 100644 index 0000000..6e0d44e --- /dev/null +++ b/backend/views/procurement/update.php @@ -0,0 +1,23 @@ +title = Yii::t('common/procurement', 'Update {modelClass}: ', [ + 'modelClass' => 'Procurement', +]) . ' ' . $model->id_procurement; +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/procurement', 'Procurements'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->id_procurement, 'url' => ['view', 'id' => $model->id_procurement]]; +$this->params['breadcrumbs'][] = Yii::t('common/procurement', 'Update'); +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/views/procurement/view.php b/backend/views/procurement/view.php new file mode 100644 index 0000000..2366b40 --- /dev/null +++ b/backend/views/procurement/view.php @@ -0,0 +1,44 @@ +title = $model->id_procurement; +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/procurement', 'Procurements'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ $model->id_procurement], ['class' => 'btn btn-primary']) ?> + $model->id_procurement], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => Yii::t('common/procurement', 'Are you sure you want to delete this item?'), + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id_procurement', + 'id_warehouse', + 'id_user', + 'id_product', + 'count', + 'stock', + 'purchase_price', + 'description', + 'created_at', + 'updated_at', + ], + ]) ?> + +
diff --git a/common/models/Procurement.php b/common/models/Procurement.php new file mode 100644 index 0000000..ae04358 --- /dev/null +++ b/common/models/Procurement.php @@ -0,0 +1,122 @@ + 255], + [['productIdentifier'] ,'validateProductIdentifier', 'on' => 'create_general'] + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id_procurement' => Yii::t('common/procurement', 'Id Procurement'), + 'id_warehouse' => Yii::t('common/procurement', 'Id Warehouse'), + 'id_user' => Yii::t('common/procurement', 'Id User'), + 'id_product' => Yii::t('common/procurement', 'Id Product'), + 'count' => Yii::t('common/procurement', 'Count'), + 'stock' => Yii::t('common/procurement', 'Stock'), + 'purchase_price' => Yii::t('common/procurement', 'Purchase Price'), + 'description' => Yii::t('common/procurement', 'Description'), + 'created_at' => Yii::t('common/procurement', 'Created At'), + 'updated_at' => Yii::t('common/procurement', 'Updated At'), + ]; + } + + + public function validateProductIdentifier($attribute,$params){ + $product = null; + + if ( isset($this->attribute)){ + $id = $this->$attribute; + $conditionProductName = ['name' =>$id]; + $conditionProductNumber = ['product_number' =>$id]; + $conditionBarcode= ['barcode' =>$id]; + $product = Product::findOne(['or',[ $conditionProductName,$conditionProductNumber,$conditionBarcode ]]); + } + + if ( $product == null ){ + $this->addError('productIdentifier' , Yii::t("common/procurement", "Invalid product")); + } + + } + + //////////////////////////////// + // PRODUCT + //////////////////////////////// + public function getProduct() { + return $this->hasOne ( Product::className (), [ + 'id_product' => 'id_product' + ] ); + } + public function getProductName() { + return $this->product->name; + } + + //////////////////////////////// + // WAREHOUSE + //////////////////////////////// + public function getWarehouse() { + return $this->hasOne ( Warehouse::className (), [ + 'id_warehouse' => 'id_warehouse' + ] ); + } + + public function getWarehouseName(){ + return $this->warehouse->name; + } + //////////////////////////////// + // USER + //////////////////////////////// + public function getUser() { + return $this->hasOne ( User::className (), [ + 'id_user' => 'id_user' + ] ); + } + + public function getUserName(){ + return $this->user->username; + } + +} diff --git a/common/models/Warehouse.php b/common/models/Warehouse.php index db5006b..bf40ca9 100644 --- a/common/models/Warehouse.php +++ b/common/models/Warehouse.php @@ -82,5 +82,21 @@ class Warehouse extends \yii\db\ActiveRecord } return $result; } + + + /** + * $param int $forceIncludeAccount id warehouse, that should be included in list, even if it is inactive + * */ + public static function read($forceIncludeObjectWithId = null){ + $warehouses = null; + + if ( $forceIncludeObjectWithId == null){ + $warehouses = Warehouse::find()->andWhere(['status' => Warehouse::STATUS_ACTIVE])->all(); + }else{ + $warehouses = Warehouse::find()->andWhere( ['or', ['status' => Warehouse::STATUS_ACTIVE], ['id_warehouse' => $forceIncludeObjectWithId ] ])->all(); + } + + return $warehouses; + } } diff --git a/console/migrations/m150925_055052_add__table_procurement.php b/console/migrations/m150925_055052_add__table_procurement.php new file mode 100644 index 0000000..e27047a --- /dev/null +++ b/console/migrations/m150925_055052_add__table_procurement.php @@ -0,0 +1,47 @@ +db->driverName === 'mysql') { + // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('{{%procurement}}', [ + 'id_procurement' => $this->primaryKey(), + 'id_warehouse' => $this->integer()->notNull(), + 'id_user' => $this->integer()->notNull(), + 'id_product' => $this->integer()->notNull(), + 'count' => $this->integer()->notNull(), + 'stock' => $this->integer(), + 'purchase_price' => $this->integer(), + 'description' => $this->string(255), + 'created_at' => $this->timestamp()->notNull(), + 'updated_at' => $this->timestamp()->notNull(), + ], $tableOptions); + } + + public function down() + { + echo "m150925_055052_add__table_procurement cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +}