<?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_ticket_summary_for_department".
 *
 * The followings are the available columns in table 'v_report_ticket_summary_for_department':
 * @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 integer $quantity
 */
class VReportTicketSummaryForDepartment extends LsmsActiveRecord
{


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

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

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'v_report_ticket_summary_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('quantity', 'numerical', 'integerOnly' => true),
            array('id', 'length', 'max' => 25),
            array('department, accounting_unit,activity_code, user_name, person, pos, badge, schedule', 'length', 'max' => 255),
            array('date_time', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('number_of_tickets,location,from_date,to_date,id, department, date_time, accounting_unit,activity_code, user_name, person, pos, badge, schedule,ticket_type, quantity', '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(
            'ticket_type' => yii::t('default', 'Ticket Type'),
            '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'),
            'number_of_tickets' => yii::t('default', 'Number Of Tickets'),
        );
    }

    /**
     * @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('ticket_type', $this->ticket_type, true);
            $criteria->compare('date_time', $this->date_time, 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('person', $this->person, true);
            $criteria->compare('pos', $this->pos, true);
            $criteria->compare('badge', $this->badge, true);
            $criteria->compare('schedule', $this->schedule, 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 POSs Alocated in the Selected Location
                    // Adding Condition on POS for validate this :
                    //      	This report will consider only the tickets that have been generated using a POS located at the location of the user.
                    //      	This report will include the generated tickets for a person that is from any location/department if he used a POS at the location of the user.
                    $location = Location::model()->findByPk($this->location);
                    $poss_array = $location->getArrayOfPOSs();
                    $criteria->addInCondition('pos', $poss_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 . '"');
            }


            $criteria->group = 'department,user_name,person,pos,badge,schedule';
            $criteria->select = 'ticket_type,department,accounting_unit,activity_code,user_name,person,pos,badge,schedule,sum(quantity) as number_of_tickets ';


            if ($this->number_of_tickets != null)
            {
                $criteria->having = 'number_of_tickets=' . $this->number_of_tickets;
            }
        }

        $pageSize = $this->yiiparam('pageSize');
        return new CActiveDataProvider($this, array(
            'sort' => array(
                'attributes' => array(
                    '*',
                    'number_of_tickets' => array(
                        'asc' => 'number_of_tickets',
                        'desc' => 'number_of_tickets DESC',
                    ),
            )),
            'pagination' => array('pageSize' => $pageSize,),
            'criteria' => $criteria,
        ));
    }

}