<?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 "contract".
 *
 * The followings are the available columns in table 'contract':
 * @property integer $id
 * @property string $name
 * @property string $reference
 * @property string $start_date
 * @property string $expiry_date
 * @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 TidStatus[] $tidStatuses
 * 
 * @author Amaney
 * @package application.models
 */
class Contract extends TidansActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return 'contract';
    }

    /**
     * @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, reference, start_date, expiry_date, status', 'required'),
	    array('name, reference', 'length', 'max' => 30),
	    array('name', 'unique'),
	    array('reference', 'unique'),
	    array('expiry_date', 'validateExpiryDate'),
	    array('status', 'length', 'max' => 8),
	    // The following rule is used by search().
	    // @todo Please remove those attributes that should not be searched.
	    array('id, name, reference, start_date, expiry_date, status', '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(
	    'tidStatuses' => array(self::HAS_MANY, 'TidStatus', 'contract_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'name' => Yii::t('default', 'Name'),
	    'reference' => Yii::t('default', 'Reference'),
	    'start_date' => Yii::t('default', 'Start Date'),
	    'expiry_date' => Yii::t('default', 'Expiry Date'),
	    'status' => Yii::t('default', 'Status'),
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
	// @todo Please modify the following code to remove attributes that should not be searched.

	$criteria = $this->ModelCriteria();

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

    /**
     * Get the query used to get the current model information to be used in export to excel action
     * @return CDbCriteria the criteria used to get the current model information
     */
    public function ModelCriteria() { // @todo Please modify the following code to remove attributes that should not be searched.
	$criteria = new CDbCriteria;

	//$criteria->compare('id',$this->id);
	$criteria->compare('id', $this->id);
	$criteria->compare('name', $this->name, true);
	$criteria->compare('reference', $this->reference, true);
	$criteria->compare('start_date', $this->start_date, true);
	$criteria->compare('expiry_date', $this->expiry_date, true);
	$criteria->compare('status', $this->status);


	return $criteria;
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Contract the static model class
     */
    public static function model($className=__CLASS__) {
	return parent::model($className);
    }

    /**
     * Check If the expiry date is less than the start date
     */
    public function validateExpiryDate($attribute, $params) {
	if ($this->expiry_date < $this->start_date) {
	    $this->addError($attribute, Yii::t('default', 'The Expiry Date can’t be smaller than Start Date.'));
	}
    }

     /**
     * The Following function is responsible for return array of all Contracts
     *
     * @return Contract[] array of Contract model .
     */
    public static function getNamesForView() {
	$result = Contract::getNameLists();
	$names = array();
	foreach ($result as $res) {
	    $names[$res] = $res;
	}
	return $names;
    }

    /**
     * The Following function is responsible for return array of all Contracts
     *
     * @return Contract[] array of Contract model .
     */
    public static function getNameLists() {
	$model = Contract::model();
	$criteria = new CDbCriteria;
	$criteria->select = 'reference';
        $criteria->order = 'reference';
	$criteria->condition = "status = 'Active'";
	return $model->commandBuilder->createFindCommand($model->tableName(), $criteria)->queryColumn();
    }

    /**
     * The Following function is responsible for return array of all Contracts to display in drop dwon
     *
     * @return Contract[] array of Contract model .
     */
    public static function getNamesForDropDown() {
	$model = Contract::model();
	$criteria = new CDbCriteria;
	$criteria->select = 'id,reference';
        $criteria->order = 'reference';
	$criteria->condition = "status = 'Active'";
	$result = $model->commandBuilder->createFindCommand($model->tableName(), $criteria)->queryAll();
	$names = array();
	foreach ($result as $res) {
	    $names[$res['id']] = $res['reference'];
	}
	return $names;
    }

}
