<?php

/**
 * This is the model class for table "prestamos.gastos_administrativos_afianzadora".
 *
 * The followings are the available columns in table 'prestamos.gastos_administrativos_afianzadora':
 * @property integer $id
 * @property string $duracion
 * @property double $rango_uno
 * @property double $rango_dos
 * @property integer $garantia
 * @property integer $status
 * @property string $fecha_registro
 * @property boolean $blnborrado
 * @property integer $idafianzadora
 */
class GastosAdministrativosAfianzadora extends CActiveRecord
{
    public $rango_porcentaje = 1;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'prestamos.gastos_administrativos_afianzadora';
	}

	/**
	 * @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('duracion, duracion_hasta', 'required'),
			array('duracion', 'validarRangoDuracion'),
			array('rango_uno, rango_dos, garantia', 'required','on'=>'RangoValor'),
			array('porcentaje', 'required','on'=>'Procentaje'),
                        array('garantia,rango_uno, rango_dos,porcentaje', '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('garantia, status, idafianzadora', 'numerical', 'integerOnly'=>true),
			//array('rango_uno, rango_dos', 'numerical'),
			array('rango_uno, rango_dos', 'validacionRango','on'=>'RangoValor'),
			array('duracion,garantia,porcentaje,rango_porcentaje,rango_uno, rango_dos, duracion_hasta,garantia', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, duracion, rango_uno, rango_dos, garantia, status, fecha_registro, blnborrado, idafianzadora', '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(
                    'idAfianzadora' => array(self::BELONGS_TO, 'Afianzadora', 'idafianzadora'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'duracion' => 'Duración desde',
			'duracion_hasta' => 'Duración hasta',
			'rango_uno' => 'Rango Uno',
			'rango_dos' => 'Rango Dos',
			'garantia' => 'Garantia',
			'status' => 'Status',
			'fecha_registro' => 'Fecharegistro',
			'blnborrado' => 'Blnborrado',
			'idafianzadora' => 'Idafianzadora',
		);
	}
        
	public function validarRangoDuracion()
	{
            if(!$this->hasErrors('duracion')&& !$this->hasErrors('duracion_hasta')){
                if($this->duracion>=$this->duracion_hasta)
                    $this->addError('duracion','No puede ser mayor o igual al rango dos');
            }
	}
        
	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($id)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('duracion',$this->duracion,true);
		$criteria->compare('rango_uno',$this->rango_uno);
		$criteria->compare('rango_dos',$this->rango_dos);
		$criteria->compare('garantia',$this->garantia);
		$criteria->compare('status',$this->status);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('idafianzadora',$id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
         public function beforeSave(){      
            $this->fecha_registro=date('d-m-Y');
            //$this->id_usuario=Yii::app()->user->id;
            
            $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');
            /*if($this->porcentaje !='')
                $this->garantia = NULL;*/
            
            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 GastosAdministrativosAfianzadora the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
