<?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 "pos_schedule".
 *
 * The followings are the available columns in table 'pos_schedule':
 * @property integer $id
 * @property integer $schedule_id
 * @property integer $pos_id
 * @property integer $created_by
 * @property string $created_date
 * @property integer $last_edited_by
 * @property string $last_edited_date
 *
 * The followings are the available model relations:
 * @property Pos $pos
 * @property Schedule $schedule
 */
class PosSchedule extends LsmsActiveRecord
    {

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

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

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '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('schedule_id, pos_id', 'required'),
            array('schedule_id, pos_id', 'numerical', 'integerOnly' => true),
            array('schedule_id', 'scheduleValidate'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, schedule_id, pos_id, created_by, created_date, last_edited_by, last_edited_date', '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(
            'pos' => array(self::BELONGS_TO, 'Pos', 'pos_id'),
            'schedule' => array(self::BELONGS_TO, 'Schedule', 'schedule_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'schedule_id' => yii::t("default", 'Schedule'),
            'pos_id' => yii::t("default", 'Pos'),
            'assigned_schedules' => yii::t("default", 'Assigned Schedules'),
        );
    }

    /**
     * @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()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('schedule_id', $this->schedule_id);
        $criteria->compare('pos_id', $this->pos_id);

        $pageSize=$this->yiiparam('pageSize');
        return new CActiveDataProvider($this, array(
            'pagination' => array('pageSize' => $pageSize,),
            'criteria' => $criteria,
        ));
    }

    public static function getAll( $status=null )
    {
        if ($status == "Active")
        {
            return PosSchedule::model()->active()->findAll(array('order' => 'name'));
        }
        else
        {
            return PosSchedule::model()->findAll(array('order' => 'name'));
        }
    }

    public function scheduleValidate( $attribute, $params )
    {
        if (isset($this->pos_id) && ($this->pos_id != null))
        {
            $newSchedule = Schedule::model()->findByPk($this->schedule_id);
            $temp = PosSchedule::model()->findAllByAttributes(array("pos_id" => $this->pos_id));
            foreach ($temp as $value)
            {
                if ($newSchedule->checkConflict($value->schedule_id))
                {
                    // there is no conflict in schedules
                }
                else
                {
                    $addedSchedule = Schedule::model()->findByPk($value->schedule_id);
                    $this->addError($attribute, Yii::t('default', 'There are a conflict between schedule  (first_schedule) and (second_schedule) ',array('first_schedule'=>$addedSchedule->name,'second_schedule'=>$newSchedule->name)));
                    break;
                }
            }
        }
    }

    }