<?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 "asset_status".
 *
 * The followings are the available columns in table 'asset_status':
 * @property integer $id
 * @property integer $asset_id
 * @property string $vehicle_book_expiry_date
 * @property string $vehicle_book_status
 * @property integer $vehicle_book_expired_days
 * @property double $fine
 * @property string $radioactive_license_expiry_date
 * @property string $radioactive_license_status
 * @property integer $radioactive_license_expired_days
 * @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 Asset $asset
 * @property DahsboardAsset[] $dahsboardAssets
 *
 * @author Amaney
 * @package application.models
 *
 */
class AssetStatus extends TidansActiveRecord {

    /**
     * @return String the old status of the asset
     */
    public $old_status;
     /**
     * @return String the plate number followed by "-" followed by asset number
     */
    public $plate_number_auto_complete;
    /**
     * @return String the license number followed by "-" followed by asset number
     */
    public $radioactive_license_number_auto_complete;



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

    /**
     * @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('asset_id', 'required'),
	    array('asset_id, vehicle_book_expired_days, radioactive_license_expired_days, created_by, last_edited_by', 'numerical', 'integerOnly' => true),
	    array('fine', 'numerical'),
	    array('vehicle_book_status, radioactive_license_status', 'length', 'max' => 7),
	    array('vehicle_book_expiry_date, radioactive_license_expiry_date, created_date, last_edited_date', 'safe'),
	    array('vehicle_book_expiry_date', 'required', 'on' => 'vehicle'),
	    array('radioactive_license_expiry_date', 'required', 'on' => 'radio'),
	    array('plate_number_auto_complete', 'validatePlate'),
	    array('radioactive_license_number_auto_complete', 'ValidateRadioactive'),
	    // The following rule is used by search().
	    // @todo Please remove those attributes that should not be searched.
	    array('id, asset_id, vehicle_book_expiry_date, vehicle_book_status, vehicle_book_expired_days, fine, radioactive_license_expiry_date, radioactive_license_status, radioactive_license_expired_days, 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(
	    'asset' => array(self::BELONGS_TO, 'Asset', 'asset_id'),
	    'dahsboardAssets' => array(self::HAS_MANY, 'DahsboardAsset', 'asset_status_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => 'ID',
	    'asset_id' => 'Asset Number',
	    'vehicle_book_expiry_date' => 'Vehicle Book Expiry Date',
	    'vehicle_book_status' => 'Vehicle Book Status',
	    'vehicle_book_expired_days' => 'Vehicle Book Expired Days',
	    'fine' => 'Fine',
	    'radioactive_license_expiry_date' => 'Radioactive License Expiry Date',
	    'radioactive_license_status' => 'Radioactive License Status',
	    'radioactive_license_expired_days' => 'Radioactive License Expired Days',
	    'created_by' => 'Created By',
	    'created_date' => 'Created Date',
	    'last_edited_by' => 'Last Edited By',
	    'last_edited_date' => 'Last Edited Date',
	);
    }

    /**
     * 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 = new CDbCriteria;

	$criteria->compare('id', $this->id);
	$criteria->compare('asset_id', $this->asset_id);
	$criteria->compare('vehicle_book_expiry_date', $this->vehicle_book_expiry_date, true);
	$criteria->compare('vehicle_book_status', $this->vehicle_book_status, true);
	$criteria->compare('vehicle_book_expired_days', $this->vehicle_book_expired_days);
	$criteria->compare('fine', $this->fine);
	$criteria->compare('radioactive_license_expiry_date', $this->radioactive_license_expiry_date, true);
	$criteria->compare('radioactive_license_status', $this->radioactive_license_status, true);
	$criteria->compare('radioactive_license_expired_days', $this->radioactive_license_expired_days);
	$criteria->compare('created_by', $this->created_by);
	$criteria->compare('created_date', $this->created_date, true);
	$criteria->compare('last_edited_by', $this->last_edited_by);
	$criteria->compare('last_edited_date', $this->last_edited_date, true);

	return new CActiveDataProvider($this, array(
	    'criteria' => $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 AssetStatus the static model class
     */
    public static function model($className=__CLASS__) {
	return parent::model($className);
    }

    /**
     * Save Asset status information
     * @param string $expiry_date either vehicle expiry date or radioactive expiry date
     * @return string the status of the asset
     */
    public function setAssetStatus($expiry_date) {
	$settings_array = Setting::getSettingsArray();
	$grace_period = $settings_array['grace_period'];
	$grace_expire_date = date("Y-m-d", strtotime(date("Y-m-d", strtotime(date("Y-m-d"))) . " +$grace_period day"));
	$current_date = date("Y-m-d", strtotime(date("Y-m-d")));
	if ($expiry_date <= $current_date)
	    return 'Expired';
	else
	if ($expiry_date <= $grace_expire_date)
	    return 'Grace';
	else
	    return 'Valid';
    }

    protected function beforeSave() {
	if ($this->asset != null) {
	    $status = $this->asset->getLastStatus();
	    $this->old_status = ($status == null) ? null : $status->getStatusValue();
	}
	return parent::beforeSave();
    }

    /**
     * Get asset status value
     * @return string the value of status of the asset
     */
    public function getStatusValue() {
	return ($this->asset->type_of_asset_id == Setting::getVehicleTypeOfAsset()) ? $this->vehicle_book_status : $this->radioactive_license_status;
    }

        /**
     * The following validation is responsible for checking if tid identifier is in auto complete list or not
     */
    function validatePlate($attribute, $params) {

	if (!AssetStatus::checkIdentifierInsideArrayOfIdentifiers($this->plate_number_auto_complete, array_values(Asset::getPlateNumbersForDropDown()))) {
	    $this->addError('plate_number_auto_complete', Yii::t('default', "The plate number (plate_number_auto_complete) is not valid, just start typing the plate number/asset number then select the required asset.", array('plate_number_auto_complete' => $this->plate_number_auto_complete)));
	}
    }

    /**
     * The following validation is responsible for checking if tid identifier is in auto complete list or not
     */
    function validateRadioactive($attribute, $params) {

	if (!AssetStatus::checkIdentifierInsideArrayOfIdentifiers($this->radioactive_license_number_auto_complete, array_values(Asset::getLicenseNumbersForDropDown()))) {
	    $this->addError('radioactive_license_number_auto_complete', Yii::t('default', "The radio active license (radioactive_license_number_auto_complete) is not valid, just start typing the radio active liecense number/asset number then select the required asset.", array('radioactive_license_number_auto_complete' => $this->radioactive_license_number_auto_complete)));
	}
    }

    /**
     * The Following function is responsible for check if identifier input is from autocomplete list (plate number or radioactive license with the asset number)
     * @param string $identifier the identifier to check
     * @param string $array_to_check the array to check that identifier inside it or not.
     * @return boolean true if identifier inside autocomplete list, false otherwise.
     */
    public static function checkIdentifierInsideArrayOfIdentifiers($identifier, $array_to_check) {

	//$tids = array_values(tid::getNumbersForDropDown());
	if ($identifier != null && $identifier != "") {
	    if (!in_array($identifier, $array_to_check))
		return false;
	}
	return true;
    }

}

