Add waste

This commit is contained in:
2016-05-08 08:56:42 +02:00
parent a6fdfb1c83
commit f8e1f90a8e
28 changed files with 1075 additions and 102 deletions

View File

@@ -0,0 +1,102 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
use common\models\Product;
use common\models\Waste;
use yii\web\HttpException;
use common\models\Log;
use common\components\Helper;
/**
* ContactForm is the model behind the contact form.
* @property \Yii\web\UploadedFile $file
*/
class WasteCreateForm extends Model{
public $productIdentifier;
public $id_product;
public $comment;
public $count;
private $_product;
public function rules(){
return [
[['count', 'id_product', ], 'integer'],
[['count', ], 'required'],
[['comment'], 'string', 'max' => 255],
[['productIdentifier'], 'string', 'max' => 128],
[['id_product'] ,'validateProduct','skipOnEmpty' => false, 'skipOnError' => false]
];
}
public function validateProduct($attribute,$params){
$this->_product = null;
if ( isset($this->productIdentifier)){
$this->_product = Product::findOne($this->id_product);
}
if ( $this->_product == null ){
$this->addError('productIdentifier' , Yii::t("common/procurement", "Invalid product"));
}
}
public function attributeLabels( ) {
return [
'count' => "Mennyiség",
'comment' => "Megjegyzés"
];
}
public function save(){
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
$model = new Waste();
$model->id_product = $this->id_product;
$model->count = $this->count;
$model->stock_before = $this->_product->stock;
$model->stock_after = $this->_product->stock - $this->count;
$model->id_user = \Yii::$app->user->id;
$model->comment = $this->comment;
if ( !$model->save(false)){
throw new HttpException("Nem sikerült menteni a selejtet");
}
$product = $this->_product;
$product->stock = $product->stock - $this->count;
if ( !$product->save(false) ){
throw new HttpException("Nem sikerült menteni a terméket");
}
Log::log([
'type' =>Log::$TYPE_WASTE,
'message' => "Selejt létrehozva. Termék:".$product->name ." Mennyiség: ". $this->count ,
'id_product' => $product->id_product
]);
$transaction->commit();
Helper::flash('success', Yii::t('backend/procurement', 'Selejtezés mentve'));
} catch (\Exception $e) {
$transaction->rollback();
throw $e;
}
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Waste;
use yii\db\Query;
use common\components\Helper;
/**
* WasteSearch represents the model behind the search form about `common\models\Waste`.
*/
class WasteSearch extends Waste
{
public $date_start;
public $date_end;
public $timestampStart;
public $timestampEnd;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_waste', 'count', 'id_product', 'stock_before', 'stock_after', 'id_user'], 'integer'],
[['comment', 'created_at', 'updated_at'], 'safe'],
[[ 'date_start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
[[ 'date_end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
// $query = Waste::find();
$query = new Query();
$query->select([
"p.name as product_name",
'w.count as waste_count',
'w.created_at as waste_created_at',
'w.comment as waste_comment',
'w.stock_before as waste_stock_before',
'w.stock_after as waste_stock_after',
'u.username as user_username',
]);
$query->from("waste as w");
$query->innerJoin("product as p","p.id_product = w.id_product");
$query->innerJoin("user as u","u.id = w.id_user");
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' =>[
'defaultOrder' => [
'waste_created_at' => SORT_DESC
],
'attributes' => Helper::mkYiiSortItems([
['product_name'],
['waste_count'],
['waste_created_at'],
['waste_comment'],
['waste_stock_before'],
['waste_stock_after'],
['user_username'],
])
]]);
$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([
'w.id_user' => $this->id_user,
'w.id_product' => $this->id_product,
]);
if ( empty($this->date_start) ){
$this->timestampStart = '';
}
if ( empty($this->date_end) ){
$this->timestampEnd = '';
}
$query->andFilterWhere([ '>=', 'w.created_at', $this->timestampStart ] );
$query->andFilterWhere([ '<', 'w.created_at', $this->timestampEnd ] );
$query->andFilterWhere(['like', 'w.comment', $this->comment]);
return $dataProvider;
}
}