<?php
/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */
 

/**
 * This is the model class for table "v_report_expired_badges_within_period".
 *
 * The followings are the available columns in table 'v_report_expired_badges_within_period':
 * @property string $badge
 * @property string $person
 * @property string $department
 * @property string $activation_start_date
 * @property string $activation_end_date
 */
class VReportExpiredBadgesWithinPeriod extends LsmsActiveRecord
{


    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return VReportExpiredBadgesWithinPeriod the static model class
     */
    public $period_days;
    public $location;

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'v_report_expired_badges_within_period';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('badge, person, department', 'required'),
            array('badge, person, department', 'length', 'max' => 255),
            array('activation_start_date, activation_end_date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('location,period_days,badge, person, department, activation_start_date, activation_end_date', '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(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'badge' => yii::t('default', 'Badge'),
            'person' => yii::t('default', 'Person'),
            'department' => yii::t('default', 'Department'),
            'activation_start_date' => yii::t('default', 'Activation Start Date'),
            'activation_end_date' => yii::t('default', 'Activation End Date'),
        );
    }

    /**
     * @return array customized attribute labels Tooltips (name=>label tooltips)
     */
    public function attributeLabelsPopover()
    {
        return array(
            'period_days' => yii::t('default', 'tooltip_period_days'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($is_empty=FALSE)
    {
        if ($is_empty)
        {
            $criteria = new CDbCriteria;
            $criteria->compare('badge', -1);
        }
        else
        {

            $criteria = new CDbCriteria;

            $criteria->compare('badge', $this->badge, true);
            $criteria->compare('person', $this->person, true);
            $criteria->compare('activation_start_date', $this->activation_start_date, true);
            $criteria->compare('activation_end_date', $this->activation_end_date, true);

            // Check if user choose All Location
            if ($this->location == null || $this->location == '')
            {
                //select all department in Geomarket wich user belong to it
                $department_array = array_values(User::model()->getDepartmentInGeomarket());
                array_shift($department_array);
                $criteria->addInCondition('department', $department_array);
                // we add compare for filtering department when user select all location
                $criteria->compare('department', $this->department, true);
            }
            else
            {
                // Check if user choose All Department
                if ($this->department == '' || $this->department == null)
                {
                    //Return All Department Alocated in the Selected Location
                    $location = Location::model()->findByPk($this->location);
                    $department_array = $location->getArrayOfDepartments();
                    $criteria->addInCondition('department', $department_array);
                    // we add compare for filtering department when user select all location
                    $criteria->compare('department', $this->department, true);
                }
                else
                {
                    //Return Selected department name
                    if (is_numeric($this->department))
                    {
                        $department_model = Department::model()->findByPK($this->department);
                        if ($department_model != null)
                        {
                            $this->department = $department_model->name;
                        }
                    }

                    $criteria->compare('department', $this->department, true);
                }
            }

            // remove all badge have been expired
            $now_date = date("Y-m-d");
            $criteria->addCondition(" DATEDIFF(activation_end_date,'$now_date') >=0");

            // Add condition for filter badge within period days to expired
            if ($this->period_days != null)
            {
                $criteria->addCondition(" DATEDIFF(activation_end_date,'$now_date') <=" . $this->period_days);
            }
        }
        $pageSize = $this->yiiparam('pageSize');
        return new CActiveDataProvider($this, array(
            'pagination' => array('pageSize' => $pageSize,),
            'criteria' => $criteria,
        ));
    }

}