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


    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return VReportTicketsDetailsForPerson the static model class
     */
    public $from_date;
    public $to_date;

    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_person';
    }

    /**
     * @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, date_time, person, pos, badge, schedule, quantity', 'required'),
            array('quantity', 'numerical', 'integerOnly' => true),
            array('id', 'length', 'max' => 25),
            array('department, accounting_unit,activity_code, user_name, person, pos, badge, schedule', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('from_date,to_date,id, department, date_time, accounting_unit,activity_code, user_name, person, pos, badge, schedule, 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(
            'id' => yii::t('default', 'Ticket ID'),
            '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'),
            'quantity' => yii::t('default', 'Number Of Tickets'),
            '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('pos', $this->pos, true);
            $criteria->compare('badge', $this->badge, true);
            $criteria->compare('schedule', $this->schedule, true);
            $criteria->compare('quantity', $this->quantity, true);
            $criteria->compare('department', $this->department, true);
            $criteria->compare('date_time', $this->date_time, true);
            $criteria->compare('cancelled', $this->cancelled, true);
            $criteria->compare('user_name', $this->user_name, true);


            // when user choose all Person should not be compare condition
            if ($this->person == yii::t('default', 'All Persons'))
            {
                $criteria->compare('person', null, true);
            }
            else
            {
                $person_identifier = $this->person;
                $arr = explode("-", $person_identifier);
                $number='-'.$arr[count($arr)-1];
                $person_name = str_replace($number,'',$person_identifier);
                $criteria->compare('person', $person_name, false);
            }


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

}