<?php

/**
 * This is the model class for table "prestamos.tipo_credito_gastos_administrativo".
 *
 * The followings are the available columns in table 'prestamos.tipo_credito_gastos_administrativo':
 * @property integer $id
 * @property string $id_tipo_credito
 * @property string $id_rango_monto
 * @property string $porcentaje
 * @property string $fecha_registro
 * @property string $id_usuario
 * @property boolean $publicar
 * @property boolean $blnborrado
 * @property double $rango_uno
 * @property double $rango_dos
 * @property integer $garantia
 *
 * The followings are the available model relations:
 * @property TipoCredito $idTipoCredito
 */
class TipoCreditoGastosAdministrativo extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'prestamos.tipo_credito_gastos_administrativo';
	}

	/**
	 * @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('rango_uno, rango_dos, garantia', 'required', 'on'=>'RangoValor'),
			array('porcentaje', 'required', 'on'=>'Porcentaje'),
			array('porcentaje', 'length', 'max'=>5),
                        array('porcentaje', 'match',  //PARA VALIDAR CAMPOS CON FORMATO MONEDA
					'pattern' => '/^[0-9]\d{0,2}(\.[0-9]\d{2,2})*(\,\d{1,3})?$/',
 					'message' => 'El valor del campo debe tener un formato 100.000,000',),
                        array('rango_uno, rango_dos, garantia', 'match',  //PARA VALIDAR CAMPOS CON FORMATO MONEDA
					'pattern' => '/^[0-9]\d{0,2}(\.[0-9]\d{2,2})*(\,\d{1,2})?$/',
 					'message' => 'El valor del campo debe tener un formato 100.000,00',),
                        array('rango_uno, rango_dos', 'validacionRango', 'on'=>'RangoValor'),
                        array('id_tipo_credito, id_rango_monto, fecha_registro, id_usuario, publicar, blnborrado,rango_uno, rango_dos, garantia', 'safe'),
                        // The following rule is used by search().
                        // @todo Please remove those attributes that should not be searched.
                        array('id, id_tipo_credito, id_rango_monto, porcentaje, fecha_registro, id_usuario, publicar, blnborrado, rango_uno, rango_dos, garantia', '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(
			'idTipoCredito' => array(self::BELONGS_TO, 'TipoCredito', 'id_tipo_credito'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_tipo_credito' => 'Id Tipo Credito',
			'id_rango_monto' => 'Id Rango Monto',
			'porcentaje' => 'Porcentaje',
			'fecha_registro' => 'Fecha Registro',
			'id_usuario' => 'Id Usuario',
			'publicar' => 'Publicar',
			'blnborrado' => 'Blnborrado',
		);
	}

        public function validacionRango()
	{
            if(!$this->hasErrors('rango_uno')&& !$this->hasErrors('rango_dos')){
                //valida que el rango 1 no sea mayor que el rango 2
                $rango_uno =  str_replace('.', '', $this->rango_uno);
                $rango_uno=str_replace(',', '.', $rango_uno);

                $rango_dos =  str_replace('.', '', $this->rango_dos);
                $rango_dos=str_replace(',', '.', $rango_dos);
                
                if($rango_uno>=$rango_dos)
                    $this->addError('rango_uno','No puede ser mayor o igual al rango dos');
            }
	}
	/**
	 * 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('id_tipo_credito',$this->id_tipo_credito,true);
		$criteria->compare('id_rango_monto',$this->id_rango_monto,true);
		$criteria->compare('porcentaje',$this->porcentaje,true);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('id_usuario',$this->id_usuario,true);
		$criteria->compare('publicar',$this->publicar);
		$criteria->compare('blnborrado',$this->blnborrado);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        public function beforeSave(){
            $this->rango_uno =  str_replace('.', '', $this->rango_uno);
            $this->rango_uno=str_replace(',', '.', $this->rango_uno);
            
            $this->rango_dos =  str_replace('.', '', $this->rango_dos);
            $this->rango_dos=str_replace(',', '.', $this->rango_dos);
            
            $this->garantia =  str_replace('.', '', $this->garantia);
            $this->garantia=str_replace(',', '.', $this->garantia);
            
            $this->porcentaje =  str_replace('.', '', $this->porcentaje);
            $this->porcentaje=str_replace(',', '.', $this->porcentaje);
            
            $this->fecha_registro=date('d-m-Y');
            $this->id_usuario=Yii::app()->user->id;
            if($this->rango_uno =='')$this->rango_uno= NULL;
            if($this->rango_dos =='')$this->rango_dos= NULL;
            if($this->garantia =='')$this->garantia= NULL;
            if($this->porcentaje =='')$this->porcentaje= NULL;
//            echo "<pre>";print_r($this->attributes);exit;
//            $this->porcentaje_tope =  str_replace('.', '', $this->porcentaje_tope);
//            $this->porcentaje_tope=str_replace(',', '.', $this->porcentaje_tope);

            if($this->descontar =='')
                $this->descontar = false;
            
                
            return parent::beforeSave();
        }

	/**
	 * 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 TipoCreditoGastosAdministrativo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
