<?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_tickets_details_for_department".
 *
 * The followings are the available columns in table 'v_report_tickets_details_for_department':
 * @property string $user_name
 * @property string $id
 * @property string $department
 * @property string $accounting_unit
 * @property string $pos
 * @property string $schedule
 * @property string $date_time
 * @property string $reason
 * @property integer $quantity
 */
class VReportTicketsDetailsForDepartment extends LsmsActiveRecord
{


    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return VReportTicketsDetailsForDepartment the static model class
     */
    public $from_date;
    public $to_date;
    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_tickets_details_for_department';
    }

    /**
     * @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('id, department, pos, schedule, date_time, quantity', 'required'),
            array('quantity', 'numerical', 'integerOnly' => true),
            array('user_name, department, accounting_unit,activity_code, pos, schedule', 'length', 'max' => 255),
            array('id', 'length', 'max' => 25),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('location,from_date,to_date,user_name, id, department, accounting_unit,activity_code, pos, schedule, date_time,reason, quantity,cancelled', '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(
            'department' => yii::t('default', 'Department'),
            'date_time' => yii::t('default', 'Date Time'),
            'accounting_unit' => yii::t('default', 'Accounting Unit'),
            'activity_code' => yii::t('default', 'Activity Code'),
            'user_name' => yii::t('default', 'System User'),
            'person' => yii::t('default', 'Person'),
            'pos' => yii::t('default', 'POS'),
            'badge' => yii::t('default', 'Badge'),
            'schedule' => yii::t('default', 'Schedule'),
            'reason' => yii::t('default', 'Reason'),
            'quantity' => yii::t('default', 'Quantity'),
            'cancelled' => yii::t('default', 'Cancelled'),
        );
    }

    /**
     * @return array customized attribute labels Tooltips (name=>label tooltips)
     */
    public function attributeLabelsPopover()
    {
        return array();
    }

    /**
     * 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('id', -1);
        }
        else
        {
            $criteria = new CDbCriteria;
            $criteria->compare('id', $this->id, true);
            $criteria->compare('accounting_unit', $this->accounting_unit, true);
            $criteria->compare('activity_code', $this->activity_code, true);
            $criteria->compare('user_name', $this->user_name, true);
            $criteria->compare('pos', $this->pos, true);
            $criteria->compare('schedule', $this->schedule, true);
            $criteria->compare('date_time', $this->date_time, true);
            $criteria->compare('reason', $this->reason, true);
            $criteria->compare('cancelled', $this->cancelled, true);
            $criteria->compare('quantity', $this->quantity);


            // 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);
                }
                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);
                }
            }

            if (($this->from_date != null ) && ($this->to_date != null))
            {
                $criteria->addCondition('Date(date_time) BETWEEN "' . $this->from_date . '" AND "' . $this->to_date . '"');
            }
        }
        $pageSize = $this->yiiparam('pageSize');
        return new CActiveDataProvider($this, array(
            'pagination' => array('pageSize' => $pageSize,),
            'criteria' => $criteria,
        ));
    }

}