fitness-web/common/models/InventoryItem.php

157 lines
4.0 KiB
PHP

<?php
namespace common\models;
use Yii;
use yii\helpers\ArrayHelper;
use common\components\UserAwareBehavior;
use common\components\Helper;
use common\components\ProductAwareBehavior;
/**
* This is the model class for table "inventory_item".
*
* @property integer $id_inventory_item
* @property integer $id_inventory
* @property integer $count_in
* @property integer $count_sold
* @property integer $count
* @property integer $count_prev
* @property integer $count_system
* @property integer $type
* @property integer $id_product
* @property integer $id_inventory_group
* @property integer $id_user
* @property integer $id_inventory_item_prev
* @property integer $id_user
* @property string $created_at
* @property string $updated_at
*/
class InventoryItem extends BaseFitnessActiveRecord
{
public static $TYPE_PRODUCT = 10;
public static $TYPE_INVENTORY_GROUP = 20;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'inventory_item';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['count'],'integer'] ,
[['count'],'required'] ,
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id_inventory_item' => Yii::t('common/inventory_item', 'Id Inventory Item'),
'id_inventory' => Yii::t('common/inventory_item', 'Id Inventory'),
'count_in' => Yii::t('common/inventory_item', 'Count In'),
'count_sold' => Yii::t('common/inventory_item', 'Count Sold'),
'count' => Yii::t('common/inventory_item', 'Leltározott mennyiség'),
'type' => Yii::t('common/inventory_item', 'Type'),
'id_product' => Yii::t('common/inventory_item', 'Id Product'),
'id_inventory_group' => Yii::t('common/inventory_item', 'Id Inventory Group'),
'id_user' => Yii::t('common/inventory_item', 'Id User'),
'created_at' => Yii::t('common/inventory_item', 'Created At'),
'updated_at' => Yii::t('common/inventory_item', 'Updated At'),
];
}
public function behaviors()
{
return ArrayHelper::merge( [
[
'class' => UserAwareBehavior::className(),
],
[
'class' => ProductAwareBehavior::className(),
]
], parent::behaviors());
}
public static function creqteQueryInventoryItems($id_inventory ) {
$query = InventoryItem::find();
$query->andWhere(['inventory_item.id_inventory' => $id_inventory]);
return $query;
}
public function getProduct(){
return $this->hasOne( Product::className() , [ 'id_product' => 'id_product' ]);
}
public function getInventoryGroup(){
return $this->hasOne( Product::className() , [ 'id_inventory_group' => 'id_inventory_group' ]);
}
public function getProductName(){
$result = "";
$product = $this->product;
if ( isset($product)){
$result = $product->name;
}
return $result;
}
public function getInventoryGroupName(){
$result = "";
$product = $this->inventoryGroup;
if ( isset($product)){
$result = $product->name;
}
return $result;
}
public function getName(){
$name = "";
if ( isset($this->id_product )){
$name = $this->getProductName();
}else{
$name = $this->getInventoryGroupName();
}
return $name;
}
public function getDifference(){
$diff = $this->count - ( $this->count_prev + $this->count_in - $this->count_sold );
return $diff;
}
public function afterSave($insert, $changedAttributes){
if ( !$insert ){
if ( $this->type == 'product'){
$product = $this->product;
$product->stock = $this->count;
if ( !$product->save(false) ){
\Yii::error("Failed to save product stock");
throw new \Exception("A leltár elem mentése nem sikerült");
}
}
}
}
}