<?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".
 *
 * The followings are the available columns in table 'pos':
 * @property integer $id
 * @property integer $location_id
 * @property string $name
 * @property string $description
 * @property string $mac_address
 * @property string $installation_date
 * @property integer $user_id
 * @property string $status
 * @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 PersonPos[] $personPoses
 * @property Location $location
 * @property PosSchedule[] $posSchedules
 * @property Ticket[] $tickets
 */
class Pos extends LsmsActiveRecord
{


    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Pos 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 '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('location_id, name, installation_date, user_id, status', 'required'),
            array('name', 'unique', 'message' => yii::t('dafault', 'POS name ({value}) already exists.')),
            array('location_id, user_id', 'numerical', 'integerOnly' => true),
            array('name, description, mac_address', 'length', 'max' => 255),
            array('status', 'length', 'max' => 8),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, location_id, name, description, mac_address, installation_date, user_id, status, 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(
            'personPoses' => array(self::HAS_MANY, 'PersonPos', 'pos_id'),
            'location' => array(self::BELONGS_TO, 'Location', 'location_id'),
            'posSchedules' => array(self::HAS_MANY, 'PosSchedule', 'pos_id'),
            'tickets' => array(self::HAS_MANY, 'Ticket', 'pos_id'),
            'user' => array(self::BELONGS_TO, 'User', 'user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'location_id' => yii::t('deafult', 'Location'),
            'name' => yii::t('deafult', 'Name'),
            'description' => yii::t('deafult', 'Description'),
            'mac_address' => yii::t('deafult', 'Mac Address'),
            'installation_date' => yii::t('deafult', 'Installation Date'),
            'user_id' => yii::t('deafult', 'Installed By'),
            'status' => yii::t('deafult', 'Status'),
        );
    }

    /**
     * @return array customized attribute labels Tooltips (name=>label tooltips)
     */
    public function attributeLabelsPopover()
    {
        return array(
            'location_id' => '',
            'name' => '',
            'description' => '',
            'mac_address' => '',
            'installation_date' => '',
            'user_id' => '',
            'status' => '',
        );
    }

    /**
     * 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('location_id', $this->location_id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('mac_address', $this->mac_address, true);
        $criteria->compare('installation_date', $this->installation_date, true);
        $criteria->compare('user_id', $this->user_id);
        $criteria->compare('status', $this->status, false);

        $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 Pos::model()->active()->findAll(array('order' => 'name'));
        }
        else
        {
            return Pos::model()->findAll(array('order' => 'name'));
        }
    }

    // The following function is responsible for getting all free (unassigned ) POS  which we can assign it to selected person it to the
    // Input :
    //         1- $personId : the person which edited and assign or unassign POS to him,
    // Output:
    //         List of unassigned POS models to the person
    public static function getFreePerson($personId=null)
    {
        // Get all POS
        $all_POS = Pos::model()->active()->findAll(array('order' => 'name'));
        if ($personId != '' && $personId != null && $all_POS != null)
        {
            // Get all Assigned schedules
            $assigned_POS = PersonPos::model()->findAllByAttributes(array('person_id' => $personId));
            if ($assigned_POS != null)
            {
                //remove duplicated or assigned schedule from all schedules
                foreach ($assigned_POS as $pos)
                {
                    $key = array_search($pos->pos, $all_POS);
                    if ($key !== null)
                    {
                        unset($all_POS[$key]);
                    }
                }
            }
        }
        return $all_POS;
    }

    public static function getAllAsArray()
    {
        $result = array();
        $result[''] = yii::t('default', 'Select POS');
        $POS_array = Pos::model()->findAll();
        foreach ($POS_array as $pos)
        {
            $result[$pos->id] = $pos->name;
        }
        return $result;
    }

    /*
     *  return all schedules wich assigned with this POS and return it in specific format for viewing
     */

    public function getAllSchedules($status=null)
    {
        if (isset($this->posSchedules) && (count($this->posSchedules) > 0))
        {
            $schedules = $this->posSchedules;
            $retruvalue = "<br>";
            foreach ($schedules as $value)
            {
                $retruvalue .= CHtml::link($value->schedule->name, array('schedule/view', 'id' => $value->schedule->id)) . ": (" . $value->schedule->getDays() . " ) (" . $value->schedule->start_time . "," . $value->schedule->end_time . ")";
                $retruvalue .= "<br>";
            }
        }
        else
        {
            // there is zero assigned schedule
            $retruvalue = "";
        }
        return $retruvalue;
    }

}