<?php

/**
 * This is the model class for table "sidcai_tasainteres".
 *
 * The followings are the available columns in table 'sidcai_tasainteres':
 * @property integer $tasa_codigo_pk
 * @property string $tasa_interes
 * @property string $tasa_gaceta
 * @property string $tasa_fechainicio
 * @property string $tasa_fechafin
 * @property integer $audit_usua
 */
class SidcaiTasainteres extends CActiveRecord{
	/**
	 * @return string the associated database table name
	 */
	public function tableName(){
		return 'sidcai_tasainteres';
	}

	/**
	 * @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(
				'tasa_interes, tasa_gaceta, tasa_fechainicio', 
				'required',
				'message' => 'Este campo es requerido.'
			),
			array(
				'tasa_interes, tasa_gaceta, tasa_fechainicio', 
				'filter',
				'filter' => 'trim'
			),
			array(
				'tasa_interes', 
				'length', 
				'max' => 9,
				'tooLong' => 'Máximo 7 digitos.'
			),
			array(
				'tasa_interes', 
				'compare', 
				'compareValue' => 0,
				'operator' => '!=', 
				'message' => 'El monto debe ser mayor a 0,00.',
			),
			array(
				'tasa_gaceta', 
				'length', 
				'max' => 150,
				'tooLong' => 'Máximo 150 caracteres.',
				'min' => 4,
				'tooShort' => 'Mínimo 4 caracteres.'
			),
			array(
				'tasa_codigo_pk, audit_usua', 
				'numerical', 
				'integerOnly' => true
			),
			array(
			    'tasa_fechainicio',
			    'compararFecha',
			),
			array(
				'tasa_fechafin', 
				'safe'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels(){
		return array(
			'tasa_codigo_pk' 	=> 'Código',
			'tasa_interes' 		=> 'Tasa de interés',
			'tasa_gaceta' 		=> 'Gaceta',
			'tasa_fechainicio' 	=> 'Fecha de inicio',
			'tasa_fechafin' 	=> 'Fecha fin',
			'audit_usua' 		=> 'Usuario Auditor',
		);
	}

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

	/* Comprueba que la fecha fin no sea mayor que la fecha de inicio. */
	public function compararFecha(){
		if($this->tasa_fechafin != "" && $this->tasa_fechainicio != ""){
			$fecha_inicio = strtotime(str_replace("/", "-", $this->tasa_fechainicio));
			$fecha_fin = strtotime(str_replace("/", "-", $this->tasa_fechafin));

			if($fecha_inicio > $fecha_fin)
				$this->addError('tasa_fechafin', 'La fecha de cierre no puede ser menor que '.$this->tasa_fechainicio);
		}
	}
}
