<?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 "department".
 *
 * The followings are the available columns in table 'department':
 * @property integer $id
 * @property string $name
 * @property string $accounting_unit
 * @property string $activity_code
 * @property integer $location_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 Location $location
 * @property Person[] $people
 * @property Ticket[] $tickets
 */
class Department extends LsmsActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Department 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 '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('name, location_id, status', 'required'),
	    array('name', 'unique', 'message' => yii::t('dafault', 'Department name ({value}) already exists.')),
	    array('location_id', 'numerical', 'integerOnly' => true), //created_by, last_edited_by'
	    array('name, activity_code ,accounting_unit', '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, name, accounting_unit,activity_code, location_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(
	    'location' => array(self::BELONGS_TO, 'Location', 'location_id'),
	    'people' => array(self::HAS_MANY, 'Person', 'department_id'),
	    'tickets' => array(self::HAS_MANY, 'Ticket', 'department_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => yii::t('default', 'ID'),
	    'name' => yii::t('default', 'Name'),
	    'accounting_unit' => yii::t('default', 'Accounting Unit'),
	    'activity_code' => yii::t('default', 'Activity Code'),
	    'location_id' => yii::t('default', 'Location'),
	    'status' => yii::t('default', 'Status'),
	);
    }

    /**
     * @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('name', $this->name, true);
	$criteria->compare('accounting_unit', $this->accounting_unit, true);
	$criteria->compare('activity_code', $this->activity_code, true);
	$criteria->compare('location_id', $this->location_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 Department::model()->active()->findAll(array('order' => 'name'));
	} else {
	    return Department::model()->findAll(array('order' => 'name'));
	}
    }

    public static function getAllInGeomarket($status = null) {
	$result = array();
	if (Yii::app()->user->checkAccess('Super Admin')) {
	    if ($status == "Active") {
		$result = Department::model()->active()->findAll(array('order' => 'name'));
	    } else {
		$result = Department::model()->findAll(array('order' => 'name'));
	    }
	} else {
	    //when user is not super admin he can see all Departments wich located at locations in his geomarket
	    $location_array = User::model()->getLocationsInGeomarket();
	    foreach ($location_array as $key => $value) {
		$department_array = Department::model()->findAllByAttributes(array('location_id' => $key, 'status' => $status));
		$result = array_merge($result, $department_array);
	    }
	}
	return $result;
    }

    public static function getDepartmentID($department_name) {
	$department = Department::model()->findByAttributes(array('name' => $department_name));
	if ($department != null) {
	    return $department->id;
	} else {
	    return null;
	}
    }

    public static function getDepartmentName($department_id) {
	$department = Department::model()->findByPk($department_id);
	if ($department != null)
	    return $department->name;

	return null;
    }

    public static function getDepartmentsInLocation($location_id, $set_all_option = false) {
	$result = array();
	if ($set_all_option)
	    $result[''] = yii::t('default', 'All Departments');

	if (Yii::app()->user->checkAccess('Food Manager')) {
	    $user = User::model()->findByPk(Yii::app()->user->getId());
	    $person_model = Person::model()->findByAttributes(array('alias' => $user->username));
	    if ($person_model->is_food_manager == 1)
		$result[$person_model->department->id] = $person_model->department->name;
	} else {
	    $department_array = Department::model()->findAllByAttributes(array('location_id' => $location_id, 'status' => 'Active'));

	    if ($department_array != null) {
		foreach ($department_array as $department) {
		    $result[$department->id] = $department->name;
		}
	    }
	}
	return $result;
    }

    public static function checkInsideLocation($department_id, $location_id) {
	$department = Department::model()->findByAttributes(array('location_id' => $location_id, 'id' => $department_id, 'status' => 'Active'));
	if ($department != null) {
	    return true;
	}
	return false;
    }

}
