<?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 "badge_assignment_rule".
  *
  * The followings are the available columns in table 'badge_assignment_rule':
  * @property integer $id
  * @property integer $person_type_id
  * @property integer $badge_type_id
  * @property integer $only_manager
  * @property integer $only_food_manager
  * @property integer $no_limit_on_badges_number
  * @property integer $max_badges_allowed
  * @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 BadgeType $badgeType
  * @property PersonType $personType
  */
 class BadgeAssignmentRule extends LsmsActiveRecord
     {


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


     /**
      * @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_type_id, badge_type_id, only_manager, only_food_manager, no_limit_on_badges_number', 'required'),
             array('person_type_id, badge_type_id, only_manager, only_food_manager, no_limit_on_badges_number, max_badges_allowed', 'numerical', 'integerOnly' => true),
             //uniqe validation person type and badge type
             array('badge_type_id', 'unique', 'criteria' => array(
                     'condition' => '`person_type_id`=:person_type and `only_manager`=:only_manager and `only_food_manager`=:only_food_manager  ',
                     'params' => array(
                         ':person_type' => $this->person_type_id,
                         ':only_manager' => $this->only_manager,
                         ':only_food_manager' => $this->only_food_manager
                     )
                 )
                 , 'message' => Yii::t('default', 'The assignment rule already exists')
             ),
             //Check if max_badges_allowed  greater than zero and not blank when no_limit_on_badges_number value is true
             array('max_badges_allowed', 'MaxBadgesAllowedValidate'),
             // The following rule is used by search().
             // Please remove those attributes that should not be searched.
             array('id, person_type_id, badge_type_id, only_manager, only_food_manager, no_limit_on_badges_number, max_badges_allowed', '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(
             'badgeType' => array(self::BELONGS_TO, 'BadgeType', 'badge_type_id'),
             'personType' => array(self::BELONGS_TO, 'PersonType', 'person_type_id'),
         );
     }


     //Check if max_badges_allowed  greater than zero and not blank when no_limit_on_badges_number value is true
     public function MaxBadgesAllowedValidate($attribute, $params)
     {
         if ($this->no_limit_on_badges_number == 0)
         {
             if ((isset($this->max_badges_allowed) && ($this->max_badges_allowed == '')) || ( ! isset($this->max_badges_allowed)))
             {
                 $this->addError($attribute, Yii::t('default', 'The Max Badges Allwoed can’t be blank'));
             }
             else
             if (isset($this->max_badges_allowed) && ($this->max_badges_allowed <= 0))
             {
                 $this->addError($attribute, Yii::t('default', 'The Max Badges Allwoed can’t be less than one day'));
             }
         }
     }


     /**
      * @return array customized attribute labels (name=>label)
      */
     public function attributeLabels()
     {
         return array(
             'person_type_id' => yii::t('default', 'Person Type'),
             'badge_type_id' => yii::t('default', 'Badge Type'),
             'only_manager' => yii::t('default', 'Only Manager'),
             'only_food_manager' => yii::t('default', 'Only Food Manager'),
             'no_limit_on_badges_number' => yii::t('default', 'Limited Badges'),
             'max_badges_allowed' => yii::t('default', 'Max Badges Allowed'),
         );
     }


     /**
      * @return array customized attribute labels Tooltips (name=>label tooltips)
      */
     public function attributeLabelsPopover()
     {
         return array(
             'person_type_id' => yii::t('default', 'tooltip_person_type_id'),
             'badge_type_id' => yii::t('default', 'tooltip_badge_type_id'),
             'only_manager' => yii::t('default', 'tooltip_only_manager'),
             'only_food_manager' => yii::t('default', 'tooltip_only_food_manager'),
             'no_limit_on_badges_number' => yii::t('default', 'tooltip_no_limit_on_badges_number'),
             'max_badges_allowed' => yii::t('default', 'tooltip_max_badges_allowed'),
         );
     }


     /**
      * 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('person_type_id', $this->person_type_id);
         $criteria->compare('badge_type_id', $this->badge_type_id);
         $criteria->compare('only_manager', $this->only_manager);
         $criteria->compare('only_food_manager', $this->only_food_manager);
         $criteria->compare('no_limit_on_badges_number', $this->no_limit_on_badges_number);
         $criteria->compare('max_badges_allowed', $this->max_badges_allowed, true);

         $pageSize = yii::app()->params['pageSize'];
         return new CActiveDataProvider($this, array(
             'pagination' => array('pageSize' => $pageSize,),
             'criteria' => $criteria,
         ));
     }
     /*
      * The Following function responsible for calculating the Max Badges number can the person get
      * Input: $person the person Object which contains his type and is_manager and is_food manager which will be in focus
      *        $badge_type_id The type of badge which will be assigned to person
      * Output: The number (Max Badges number can the person get)
      * if output is :  -1 = there is no rule for this type of assignment
      *                  0 = this type of badge assignment rule  don't have limits on badge number
      *              other = the number of badges can be assigned for person
      */


     public static function getMaxBadgeAllowed($person=null, $badge_type_id=null)
     {
         //there is no rule for this type of assignment default value
         $returnvalue = -1;
         if (isset($person) && ($person != null))
         {
             $assignmentRule_array = BadgeAssignmentRule::getPersonRules($person, $badge_type_id);

             if (isset($assignmentRule_array) && count($assignmentRule_array) > 0)
             {
                 foreach ($assignmentRule_array as $rule)
                 {
                     if ($rule->no_limit_on_badges_number == 1)
                     {
                         //this type of badge assignment rule  don't have limits on badge number
                         $returnvalue = 0;
                         return $returnvalue;
                     }
                     else
                     {
                         if ($rule->max_badges_allowed > $returnvalue)
                         {
                             // the number of badges can be assigned for person
                             $returnvalue = $rule->max_badges_allowed;
                         }
                     }
                 }
             }
         }
         return $returnvalue;
     }
     /*
      * The Following function responsible for get all assignment rule wich person can access to it
      *  for mor info refer to the file "Assignment badge to person process.docx"
      */


     public static function getPersonRules($person=null, $badge_type_id=null)
     {
         $assignmentRule_array = array();
         //Public rules
         if (($person->is_manager == 0) && ($person->is_food_manager == 0))
         {
             $assignmentRule_array = BadgeAssignmentRule::model()->findAllByAttributes(array(
                         'person_type_id' => $person->type_id,
                         'badge_type_id' => $badge_type_id,
                         'only_manager' => $person->is_manager,
                         'only_food_manager' => $person->is_food_manager,
                     ));
         }
         //Manager or Food Manager and public rules
         else
         {
             $manager_FoodManager_assignmentRules = BadgeAssignmentRule::model()->findAllByAttributes(array(
                         'person_type_id' => $person->type_id,
                         'badge_type_id' => $badge_type_id,
                         'only_food_manager' => 1,
                         'only_manager' => 1,
                     ));

             // Manager and public rules
             if ($person->is_manager == 1)
             {
                 $assignmentRule_array = BadgeAssignmentRule::model()->findAllByAttributes(array(
                             'person_type_id' => $person->type_id,
                             'badge_type_id' => $badge_type_id,
                             'only_food_manager' => 0,
                         ));
             }
             //Food Manager and public rules
             else if ($person->is_food_manager == 1)
             {
                 $assignmentRule_array = BadgeAssignmentRule::model()->findAllByAttributes(array(
                             'person_type_id' => $person->type_id,
                             'badge_type_id' => $badge_type_id,
                             'only_manager' => 0,
                         ));
             }
             $assignmentRule_array = array_merge($assignmentRule_array, $manager_FoodManager_assignmentRules);
         }
         return $assignmentRule_array;
     }
     }