work on inventory

This commit is contained in:
2016-03-07 20:54:32 +01:00
parent 603508eb70
commit 1d3c476476
24 changed files with 1357 additions and 0 deletions

View File

@@ -0,0 +1,268 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use common\models\Card;
use common\models\Customer;
use common\models\Ticket;
use common\models\Account;
use yii\web\UploadedFile;
use common\models\Inventory;
use yii\base\Exception;
use common\models\Product;
use common\models\InventoryGroup;
use common\models\InventoryItem;
use yii\db\Query;
use common\models\Procurement;
use common\models\Transfer;
/**
* ContactForm is the model behind the contact form.
* @property \Yii\web\UploadedFile $file
*/
class InventoryItemForm extends Model{
/**Form data*/
public $count;
public $id_product;
public $type;
public $name;
/**common data*/
public $inventory;
public $lastInventory;
public $id_inventory;
public $products;
public $inventoryGroups;
public $productOptions;
/**save data*/
public $product;
public $inventoryGroup;
public $prevInventoryItem;
/** internal stuff*/
public $product_in;
public $product_out;
public $product_stock;
public function rules(){
return [
[[ 'name', 'count'], 'required'],
[['id_product','type','name'], 'string'],
[['count'], 'integer'],
['type','validateType','skipOnEmpty' => false, 'skipOnError' => false],
['id_product','validateId','skipOnEmpty' => false, 'skipOnError' => false]
];
}
public function validateType($attribute,$params){
if ( array_search($this->type, ['product','group']) === false ){
\Yii::error("Típus nem megfelelő: " . $this->type);
$this->addError("name","Nincs termék vagy termék csoport kiválasztva");
}
}
public function validateId($attribute,$params){
if ( !$this->hasErrors('type')){
if ( $this->type == 'group'){
$this->inventoryGroup = InventoryGroup::findOne($this->id_product);
if ( !isset($this->inventoryGroup)){
\Yii::error("Termék csoport nem található");
$this->addError("name","Nincs termék vagy termék csoport kiválasztva");
}
}else{
$this->product = Product::findOne($this->id_product);
if ( !isset($this->product )){
\Yii::error("Termék nem található");
$this->addError("name","Nincs termék vagy termék csoport kiválasztva");
}
}
}
}
public function save(){
if ( $this->validate()) {
$this->loadLastInventoryItem();
$this->loadProductIn();
$this->loadProductSold();
$this->loadProductStock();
$item = new InventoryItem();
$item->count_in = $this->product_in;
$item->count_sold = $this->product_out;
$item->count = $this->product_stock;
if ( isset( $this->lastInventoryItem ) ){
$item->id_inventory_item_prev = $this->lastInventoryItem->id_inventory_item;
$item->count_prev = $this->lastInventoryItem->count;
}else{
$item->count_prev = 0;
}
$item->id_user = \Yii::$app->user->id;
if ( $this->type == 'product'){
$item->id_product = $this->product->id_product;
}else{
$item->id_inventory_group = $this->inventoryGroup->id_inventory_group;
}
if ( !$item->save() ){
throw new \Exception("Nem sikerült a leltár végrehajtása");
}
}
return false;
}
protected function loadProductIn(){
$query = new Query();
$query->select(['sum(procurement.count) as total_in']);
$query->from(Procurement::tableName());
$query->innerJoin(Product::tableName(),"product.id_product = procurement.id_product");
if ( isset($this->lastInventoryItem ) ){
$query->andWhere([ '>', 'procurement.created_at' ,$this->lastInventoryItem->created_at]);
}
if ( $this->type == 'product') {
$query->andWhere(['product.id_product' => $this->product->id_product]);
}else{
$query->andWhere(['product.id_inventory_group' => $this->inventoryGroup->id_inventory_group]);
}
$this->product_in = $query->scalar();
}
protected function loadProductSold(){
$query = new Query();
$query->select(['sum(transfer.count) as total_sold']);
$query->from(Transfer::tableName());
$query->innerJoin(Product::tableName(),"product.id_product = transfer.id_object and transfer.type = " .Transfer::TYPE_PRODUCT);
$query->andWhere(['in', 'transfer.status' ,[Transfer::STATUS_PAID ,Transfer::STATUS_NOT_PAID ] ]);
$query->andWhere(['transfer.type' => Transfer::TYPE_PRODUCT]);
if ( $this->type == 'product') {
$query->andWhere(['product.id_product' => $this->product->id_product]);
}else{
$query->andWhere(['product.id_inventory_group' => $this->inventoryGroup->id_inventory_group]);
}
$this->product_out = $query->scalar();
}
protected function loadProductStock(){
$query = new Query();
$query->select(['sum(product.stock) as total_stock']);
$query->from(Product::tableName());
if ( $this->type == 'product') {
$query->andWhere(['product.id_product' => $this->product->id_product]);
}else{
$query->andWhere(['product.id_inventory_group' => $this->inventoryGroup->id_inventory_group]);
}
$this->product_stock = $query->scalar();
}
public function addProductItem(){
}
public function addGroupItem(){
}
/**
* Load previous inventory item, if exists
* */
protected function loadLastInventoryItem( ){
if ( isset( $this->lastInventory ) ){
$query = InventoryItem::find();
$query->andWhere(['id_inventory' => $this->lastInventory->id_inventory]);
if ( $this->type == 'product'){
$query->andWhere(['id_product' => $this->product->id_product]);
}else{
$query->andWhere(['id_inventory_group' => $this->inventoryGroup->id_inventory_group]);
}
$this->lastInventoryItem = $query->one();
}
}
public function read(){
$this->loadInventory();
$this->loadLastInventory();
$this->loadProducts();
$this->loadInventoryGroups();
$this->buildProductList();
}
public function loadInventory(){
$this->inventory = Inventory::findOne($this->id_inventory);
if ( !isset( $this->inventory) ){
throw new Exception("Nem található a leltár objektum");
}
}
public function loadLastInventory(){
$query = Inventory::find();
$query->andWhere('<', 'inventory.created_at' , $this->inventory->created_at);
$query->orderBy(['created_at' => SORT_DESC ]);
//$this->lastInventory =
$query->limit(1)->all();
}
public function loadProducts(){
$query = Product::find();
$query->andWhere("id_inventory_group is null");
$query->andWhere(['status' => Product::STATUS_ACTIVE]);
$this->products = $query->all();
}
public function loadInventoryGroups(){
$query = InventoryGroup::find();
$query->andWhere(['status' => Product::STATUS_ACTIVE]);
$this->inventoryGroups = $query->all();
}
public function buildProductList(){
$this->productOptions = [];
foreach ($this->products as $product ){
$this->productOptions[]= [ 'type' =>'product', 'id' => $product->id_product ,'name' => $product->name . " (Termék/" .$product->productCategoryName ."/" . $product->accountName .")" ];
}
foreach ($this->inventoryGroups as $inventoryGroup ){
$this->productOptions[] = ['type' =>'group', 'id' =>$inventoryGroup->id_inventory_group ,'name' =>$inventoryGroup->name ."/Termékcsoport" ];
}
}
public function attributeLabels()
{
return [
'count' => 'Mennyiség'
];
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\InventoryItem;
/**
* InventoryItemSearch represents the model behind the search form about `common\models\InventoryItem`.
*/
class InventoryItemSearch extends InventoryItem
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_inventory_item', 'id_inventory', 'count_in', 'count_sold', 'count', 'type', 'id_product', 'id_inventory_group', 'id_user'], 'integer'],
[['last_inventroy_at', 'created_at', 'updated_at'], 'safe'],
];
}
/**
* @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 = InventoryItem::find();
$dataProvider = new ActiveDataProvider([
'query' => $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_inventory_item' => $this->id_inventory_item,
'id_inventory' => $this->id_inventory,
'count_in' => $this->count_in,
'count_sold' => $this->count_sold,
'count' => $this->count,
'type' => $this->type,
'id_product' => $this->id_product,
'id_inventory_group' => $this->id_inventory_group,
'id_user' => $this->id_user,
'last_inventroy_at' => $this->last_inventroy_at,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
return $dataProvider;
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Inventory;
/**
* InventorySearch represents the model behind the search form about `common\models\Inventory`.
*/
class InventorySearch extends Inventory
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_inventory', 'id_user'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
];
}
/**
* @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 = Inventory::find();
$dataProvider = new ActiveDataProvider([
'query' => $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_inventory' => $this->id_inventory,
'id_user' => $this->id_user,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
return $dataProvider;
}
}