<?php

namespace common\models;

use Yii;
use common\components\Helper;

/**
 * This is the model class for table "procurement".
 *
 * @property integer $id_procurement
 * @property integer $id_warehouse
 * @property integer $id_user
 * @property integer $id_product
 * @property integer $count
 * @property integer $stock
 * @property integer $purchase_price
 * @property string $description
 * @property string $created_at
 * @property string $updated_at
 */
class Procurement extends \common\models\BaseFitnessActiveRecord 
{
	
	public $productIdentifier;
	public $id_account;
	
	public $_product;
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'procurement';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_warehouse', 'count', 'purchase_price' ], 'required'],
            [['id_warehouse', 'count', 'productIdentifier', 'purchase_price' ], 'required'  , 'on' => 'create_general'],
            [['id_warehouse', 'id_user', 'id_product', 'count', 'stock', 'purchase_price' ], 'integer'],
            [['description'], 'string', 'max' => 255],
            [['productIdentifier'], 'string', 'max' => 128],
        	[['id_product']  ,'validateProduct', 'on' => 'create_general','skipOnEmpty' => false, 'skipOnError' => false]
        ];
    }

    /**
     * @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 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"));
    	}
    	
    }
    
    ////////////////////////////////
    // 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' => 'id_user'
    	] );
    }
    
    public function getUserName(){
    	return $this->user->username;
    }
    
}
