<?php

/**
 * This is the model class for table "{{inventory}}".
 *
 * The followings are the available columns in table '{{inventory}}':
 * @property integer $id
 * @property integer $successor_id
 * @property integer $product_category_id
 * @property string $gmc_code
 * @property string $model_no
 * @property string $ymgf_fob
 * @property string $cbm
 * @property string $base_code
 * @property string $is_active
 * @property integer $created_by
 * @property string $created_date_time
 * @property integer $updated_by
 * @property string $updated_date_time
 */
class Inventory extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
//	public $tooSmall = null;
	
	public function tableName()
	{
		return '{{inventory}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
            
            //goods_in_end_month,goods_in_end_year,backlog_end_month,backlog_end_year adeed  by rahul on 19-5-22
		return array(
			array('product_category_id, gmc_code, model_no, cbm, base_code,goods_in_month, goods_in_end_month,goods_in_year,goods_in_end_year,backlog_month,
			backlog_end_month,backlog_year,backlog_end_year,new_till_month,new_till_year', 'required'),//,base_category_code,base_category_name,description,origin,category_type_id
			array('product_category_id,successor_id,category_type_id,future_category_type_id,created_by, updated_by', 'numerical', 'integerOnly'=>true),
			array('gmc_code, model_no,barcode,gmc_colour,sub_category,spiral_sub_category,psi_series,order_lot_to_ycj,cable_modeification,other_1,other_2,other_3,other_4', 'length', 'max'=>255),
			array('goods_in_month', 'length', 'max'=>2),
            array('goods_in_end_month,discontinuing_month', 'length', 'max'=>2),
			array('goods_in_year', 'length', 'max'=>4),
            array('goods_in_end_year,discontinuing_year', 'length', 'max'=>4),
			array('gmc_code', 'unique'),
			array('ymgf_fob, base_code,inventory_type', 'length', 'max'=>25),
			array('length, width,height,weight', 'length', 'max'=>15),
			array('cbm', 'numerical', 'min' => 0,'tooSmall' =>'Negative value not accepted'),
			array('is_active,is_toplevel,is_confidential', 'length', 'max'=>1),
		
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, product_category_id,successor_id,gmc_code, model_no, ymgf_fob, cbm, base_code,base_category_code,base_category_name,description,category_type_id, is_active,origin,barcode,gmc_colour,sub_category,spiral_sub_category,psi_series,order_lot_to_ycj,cable_modeification,other_1,other_2,other_3,other_4,length, width,height,weight, created_by, created_date_time, updated_by, updated_date_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'category_det' => array(self::BELONGS_TO, 'MasProductCategory', 'product_category_id'),
                    'successor_det' => array(self::BELONGS_TO, 'Inventory', 'successor_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'product_category_id' => 'Product Category',
			'gmc_code' => 'Gmc Code',
			'model_no' => 'Model No',
			'ymgf_fob' => 'Ymgf Fob',
			'cbm' => 'Cbm',
			'base_code' => 'Base Code',
			'goods_in_month' => 'Goods In Start Month',
			'goods_in_end_month' => 'Goods In End Month',
			'backlog_month' => 'Backlog Start Month',
			'backlog_end_month' => 'Backlog End Month',
			'backlog_year' => 'Backlog Start Year',
			'backlog_end_year'=> 'Backlog End Year',
			'goods_in_year' => 'Goods In Start Year',
			'goods_in_end_year'=> 'Goods In End Year',
			'new_till_year' => 'New Till Year',
			'new_till_month'=> 'New Till Month',
			'is_active' => 'Is Active',
			'created_by' => 'Created By',
			'created_date_time' => 'Created Date Time',
			'updated_by' => 'Updated By',
			'updated_date_time' => 'Updated Date Time',
			'spiral_sub_category' => 'Series Qty wise',
			'psi_series' => 'Series Amt wise'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('product_category_id',$this->product_category_id);
		$criteria->compare('gmc_code',$this->gmc_code,true);
		$criteria->compare('model_no',$this->model_no,true);
		$criteria->compare('ymgf_fob',$this->ymgf_fob,true);
		$criteria->compare('cbm',$this->cbm,true);
		$criteria->compare('base_code',$this->base_code,true);
		$criteria->compare('is_active',$this->is_active,true);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('created_date_time',$this->created_date_time,true);
		$criteria->compare('updated_by',$this->updated_by);
		$criteria->compare('updated_date_time',$this->updated_date_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	
	
	
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Inventory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	public static function AddOneGoodsInMonth($product_id){
	    $mod_inv = Inventory::model()->findByPK((int)$product_id);
	    if((int)$mod_inv->goods_in_month==12){
	        $mod_inv->goods_in_month=1;
	        $mod_inv->goods_in_year=$mod_inv->goods_in_year+1;
	    }else{
	        $mod_inv->goods_in_month=$mod_inv->goods_in_month+1;
	    } 
	    if((int)$mod_inv->goods_in_end_month==12){
	        $mod_inv->goods_in_end_month=1;
	        $mod_inv->goods_in_end_year=$mod_inv->goods_in_end_year+1;
	    }else{
	        $mod_inv->goods_in_end_month=$mod_inv->goods_in_end_month+1;
	    }
	    $mod_inv->save(false);
	}
	public static function DeductOneGoodsInMonth($product_id){
	    $mod_inv = Inventory::model()->findByPK((int)$product_id);
	    if((int)$mod_inv->goods_in_month==1){
	        $mod_inv->goods_in_month=12;
	        $mod_inv->goods_in_year=$mod_inv->goods_in_year-1;
	    }else{
	        $mod_inv->goods_in_month=$mod_inv->goods_in_month-1;
	    } 
	    if((int)$mod_inv->goods_in_end_month==1){
	        $mod_inv->goods_in_end_month=12;
	        $mod_inv->goods_in_end_year=$mod_inv->goods_in_end_year-1;
	    }else{
	        $mod_inv->goods_in_end_month=$mod_inv->goods_in_end_month-1;
	    }
	    $mod_inv->save(false);
	}
	public static function AddOneBacklogMonth($product_id){
	    $mod_inv = Inventory::model()->findByPK((int)$product_id);
	    if((int)$mod_inv->backlog_month==12){
	        $mod_inv->backlog_month=1;
	        $mod_inv->backlog_year=$mod_inv->backlog_year+1;
	    }else{
	        $mod_inv->backlog_month=$mod_inv->backlog_month+1;
	    } 
	    if((int)$mod_inv->backlog_end_month==12){
	        $mod_inv->backlog_end_month=1;
	        $mod_inv->backlog_end_year=$mod_inv->backlog_end_year+1;
	    }else{
	        $mod_inv->backlog_end_month=$mod_inv->backlog_end_month+1;
	    }
	    $mod_inv->save(false);
	}
	public static function DeductOneBacklogMonth($product_id){
	    $mod_inv = Inventory::model()->findByPK((int)$product_id);
	    if((int)$mod_inv->backlog_month==1){
	        $mod_inv->backlog_month=12;
	        $mod_inv->backlog_year=$mod_inv->backlog_year-1;
	    }else{
	        $mod_inv->backlog_month=$mod_inv->backlog_month-1;
	    } 
	    if((int)$mod_inv->backlog_end_month==1){
	        $mod_inv->backlog_end_month=12;
	        $mod_inv->backlog_end_year=$mod_inv->backlog_end_year-1;
	    }else{
	        $mod_inv->backlog_end_month=$mod_inv->backlog_end_month-1;
	    }
	    $mod_inv->save(false);
	}
	
	public static function AddOneETAMonth($product_id){
	    $mod_inv = Inventory::model()->findByPK((int)$product_id);
	    if((int)$mod_inv->eta_month==12){
	        $mod_inv->eta_month=1;
	        $mod_inv->eta_year=$mod_inv->eta_year+1;
	    }else{
	        $mod_inv->eta_month=$mod_inv->eta_month+1;
	    }
	    $mod_inv->save(false);
	}
	public static function DeductOneETAMonth($product_id){
	    $mod_inv = Inventory::model()->findByPK((int)$product_id);
	    if((int)$mod_inv->eta_month==1){
	        $mod_inv->eta_month=12;
	        $mod_inv->eta_year=$mod_inv->eta_year-1;
	    }else{
	        $mod_inv->eta_month=$mod_inv->eta_month-1;
	    } 
	    $mod_inv->save(false);
	}
	public static function getmonthyear($year,$month,$mtype){
        $arr =[];
        $no=(int)(explode('-',$mtype)[1]);
        $date =(int)$year.'-'.(int)$month.'-01';
        $new=date('Y-m-d',strtotime(' -'.$no.' months',strtotime($date)));
        $arr['month_taken'] =((int)date('m',strtotime($new)));
        $arr['year_taken'] =((int)date('Y',strtotime($new)));
        return $arr;
    }
    public static function getmonth_type()
    {
        $arr_month=[];
        $arr_month= array(
            'CM'=>'Current Month',
            'CM-1'=>'Current Month-1',
            'CM-2'=>'Current Month-2',
            'CM-3'=>'Current Month-3',
            'CM-4'=>'Current Month-4',
        );
        return $arr_month;
    }
    public static function getActiveGMCs() {
        $condition = 'is_active =  "Y"';
        $model = self::model()->findAll(array('condition' => $condition, 'order' => 'product_category_id,gmc_sort_order ASC'));
        return CHtml::listData($model, 'id', 'gmc_code');
    }
    public static function getAllGMCs() {
        $condition = 'is_active =  "Y"';
        $model = self::model()->findAll(array('condition' => $condition, 'order' => 'product_category_id,gmc_sort_order ASC'));
        return CHtml::listData($model, 'id', 'gmc_code');
    }
    public static function getActiveCategorywiseGMCs($cat_id) {
        $condition = 'is_active =  "Y"';
        if((int)$cat_id>0){
            $condition .= ' AND product_category_id = '.$cat_id;
        }
        $model = self::model()->findAll(array('condition' => $condition, 'order' => 'product_category_id,gmc_sort_order ASC'));
        return CHtml::listData($model, 'id', 'gmc_code');
    }
    
     public static function getGMCWithoutKit() {
        $condition = 'is_active =  "Y" AND gmc_type != "K"';
        $model = self::model()->findAll(array('condition' => $condition, 'order' => 'product_category_id,gmc_sort_order ASC'));
        return CHtml::listData($model, 'id', 'gmc_code');
    }
    
    public static function getinvtype($pdt_id,$month=0,$year=0)
    {
        $model = self::model()->findByPK((int)$pdt_id);
        $new_dis = '';
        //Inventory_new_till_month,Inventory_new_till_year
        if((int)$month!=0 && (int)$year!=0){
            $date= $year.'-'.$month.'-01';
            $new_till_date =$model->new_till_year.'-'.$model->new_till_month.'-01';
            if(strtotime($date) < strtotime($new_till_date)){
                $new_dis = 'New';
            }
            if($model->is_active!='Y'){
                $new_dis .= ' Discontinued';
            }
        }
        
        
        return $new_dis;
    }
}
