<?php

namespace common\models;

use Yii;
use common\components\UserAwareBehavior;
use yii\helpers\ArrayHelper;
use yii\db\Query;
use backend\models\InventoryItemForm;
use common\components\AccountAwareBehavior;
use common\components\Helper;

/**
 * This is the model class for table "inventory".
 *
 * @property integer $id_inventory
 * @property integer $id_user
 * @property integer $id_account
 * @property integer $status
 * @property string $name
 * @property string $created_at
 * @property string $updated_at
 */
class Inventory extends  \common\models\BaseFitnessActiveRecord
{
  
	public static $STATUS_OPEN = 10;
	public static $STATUS_CLOSED = 20;
	
	
	/**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'inventory';
    }
    

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        	[['name'], 'string'],
        	[['id_account'], 'integer'],
        	[['name'], 'validateOnlyClosed'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_inventory' => Yii::t('common/inventory', 'Leltár azonosító'),
            'name' => Yii::t('common/inventory', 'Megnevezés'),
            'status' => Yii::t('common/inventory', 'Státusz'),
            'id_user' => Yii::t('common/inventory', 'Felhasználó'),
            'id_account' => Yii::t('common/inventory', 'Kassza'),
            'created_at' => Yii::t('common/inventory', 'Létrehozás'),
            'updated_at' => Yii::t('common/inventory', 'Módosítás'),
        ];
    }
    
    
    public function validateOnlyClosed($attribute,$params){
    	$opened = Inventory::find()->andWhere(['status' => Inventory::$STATUS_OPEN])->all();
    	if ( count($opened) > 0 ){
    		$this->addError("name","Kérem előbb zárjon le minden másik leltárt");
    	}
    }
    
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
    	return ArrayHelper::merge(  [
    			[
    					'class' => UserAwareBehavior::className(),
    			],
    			[
    			'class' => AccountAwareBehavior::className()
    				
    			]
    			], parent::behaviors());
    }
    
    public function afterSave($insert, $changedAttributes){
    	if ( $insert ){
    		
    		$query = Product::find();
    		if ( isset($this->id_account) && is_numeric($this->id_account)){
    			$query->andWhere(['id_account' => $this->id_account]);
    		
    		}
//     		$query->andWhere("product.id_inventory_group is null");
    		$products = $query->all();
    		
    		//echo "Products found: " . count($products);
    		
    		$inventoryGroups = InventoryGroup::find()->all();
    		
    		
    		foreach ($products as $product){
    			$form = new InventoryItemForm(
    				[
    					'inventory'	=> $this,
    					'id_product' =>  $product->id_product,
    					'product' => $product,
    					'type' => 'product'
    				]
    					);
    			$form->save();
    		}
    		
    		foreach ($inventoryGroups as $group){
    			$form = new InventoryItemForm(
    					[
    							'inventory'	=> $this,
    							'id_product' =>  $group->id_inventory_group,
    							'inventoryGroup' => $group,
    							'type' => 'group'
    					]
    					);
    			$form->save();
    		}
    		
    		
    		
    	}
    }
    
    
    public static function statuses(){
    	return [
    		Inventory::$STATUS_CLOSED => "Lezárva",
    		Inventory::$STATUS_OPEN => "Nyitva"
    	];
    }
    
    public   function getStatusHuman(){
    	return Helper::getArrayValue(self::statuses(), $this->status, "NA");
    }
    
    public function isOpen(){
    	return $this->status == self::$STATUS_OPEN;
    }
    
    
}
