<?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_person_pos".
 *
 * The followings are the available columns in table 'v_report_person_pos':
 * @property string $person_name
 * @property string $person_number
 * @property string $person_department_name
 * @property string $person_location_code
 * @property string $person_geomarket_name
 * @property string $pos_name
 * @property string $pos_location_code
 * @property string $pos_geomraket_name
 */
class VReportPersonPos extends LsmsActiveRecord
{


    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return VReportPersonPos the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

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

    /**
     * @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('person_name, person_number, person_department_name, person_location_code, person_geomarket_name, pos_name, pos_location_code, pos_geomraket_name', 'required'),
            array('person_name, person_number, person_department_name, person_location_code, person_geomarket_name, pos_name, pos_location_code, pos_geomraket_name', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('person_name, person_number, person_department_name, person_location_code, person_geomarket_name, pos_name, pos_location_code, pos_geomraket_name', '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(
            'person_name' => yii::t('default', 'Person Name'),
            'person_number' => yii::t('default', 'Person Number'),
            'person_department_name' => yii::t('default', 'Person Department'),
            'person_location_code' => yii::t('default', 'Person Location'),
            'person_geomarket_name' => yii::t('default', 'Person Geomarket'),
            'pos_name' => yii::t('default', 'POS Name'),
            'pos_location_code' => yii::t('default', 'POS Location'),
            'pos_geomraket_name' => yii::t('default', 'POS Geomraket'),
        );
    }

    /**
     * @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('person_number', -1);
        }
        else
        {

            $criteria = new CDbCriteria;

//            $criteria->compare('person_name', $this->person_name, true);
            $criteria->compare('person_number', $this->person_number, true);
            $criteria->compare('person_department_name', $this->person_department_name, true);
            $criteria->compare('person_location_code', $this->person_location_code, true);
            $criteria->compare('person_geomarket_name', $this->person_geomarket_name, true);
            $criteria->compare('pos_name', $this->pos_name, true);
            $criteria->compare('pos_location_code', $this->pos_location_code, true);
            $criteria->compare('pos_geomraket_name', $this->pos_geomraket_name, true);


            // when user choose all Person should not be compare condition
            if ($this->person_name == yii::t('default', 'All Persons'))
            {
                $criteria->compare('person_name', null, true);
            }
            else
            {
                $person_identifier = $this->person_name;
                $arr = explode("-", $person_identifier);
                $person_name = $arr[0];
                $criteria->compare('person_name', $person_name, true);
            }
        }
        $pageSize = $this->yiiparam('pageSize');
        return new CActiveDataProvider($this, array(
            'pagination' => array('pageSize' => $pageSize,),
            'criteria' => $criteria,
        ));
    }

}