<?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_pos_schedule".
 *
 * The followings are the available columns in table 'v_report_ticket_summary_pos_schedule':
 * @property string $id
 * @property string $date_time
 * @property string $pos
 * @property string $schedule
 * @property integer $quantity
 */
class VReportTicketSummaryPosSchedule extends LsmsActiveRecord
{


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

    /**
     * @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, date_time, pos, schedule, quantity', 'required'),
            array('quantity', 'numerical', 'integerOnly' => true),
            array('id', 'length', 'max' => 25),
            array('pos, schedule', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('number_of_tickets,id, date_time, pos, schedule, quantity ,from_date,to_date,location', '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(
            'date_time' => yii::t('default', 'Date Time'),
            'pos' => yii::t('default', 'POS'),
            '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('date_time', $this->date_time, true);
            $criteria->compare('pos', $this->pos, true);
            $criteria->compare('schedule', $this->schedule, true);
            $criteria->compare('quantity', $this->quantity);
//        $criteria->compare('number_of_tickets', $this->number_of_tickets);
            //Check if user select all location
            if ($this->location == '' || $this->location == null)
            {
                //Return All POSs Alocated in the geomarket wich user belong to it
                $pos_array = array_values(User::model()->getPOSInGeomarket());
                array_shift($pos_array);
                $criteria->addInCondition('pos', $pos_array);
            }
            else
            {
                //Return All POSs Alocated in the Selected Location
                $location = Location::model()->findByPk($this->location);
                $poss_array = $location->getArrayOfPOSs();
                $criteria->addInCondition('pos', $poss_array);
            }


            if (($this->from_date != null ) && ($this->to_date != null))
            {
                $criteria->addCondition('Date(date_time) BETWEEN "' . $this->from_date . '" AND "' . $this->to_date . '"');
            }

            $criteria->group = 'pos,schedule';
            $criteria->select = 'pos,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(
            'pagination' => array('pageSize' => $pageSize,),
            'criteria' => $criteria,
            'sort' => array(
                'attributes' => array(
                    '*',
                    'number_of_tickets' => array(
                        'asc' => 'number_of_tickets',
                        'desc' => 'number_of_tickets DESC',
                    ),
            )),
        ));
    }

}