<?php

/**
 * This is the model class for table "sidcai_unidadtributaria".
 *
 * The followings are the available columns in table 'sidcai_unidadtributaria':
 * @property integer $unid_codigo_pk
 * @property string $unid_fechacomienzo
 * @property string $unid_fechafin
 * @property string $unid_habilitado
 * @property string $unid_valor
 * @property integer $audit_usua
 */
class SidcaiUnidadtributaria extends CActiveRecord{
	/**
	 * @return string the associated database table name
	 */
	public function tableName(){
		return 'sidcai_unidadtributaria';
	}

	/**
	 * @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(
				'unid_valor, unid_fechacomienzo', 
				'required',
				'message' => 'Este campo es requerido.',	
			),
			array(
				'unid_fechacomienzo, unid_valor',
				'filter',
				'filter' => 'trim'
			),
			array(
				'unid_valor', 
				'compare', 
				'compareValue' => 0,
				'operator' => '!=', 
				'message' => 'El valor tiene que ser mayor a 1.'
			),
			array(
			    'unid_fechafin',
			    'compararFecha',
			), 
			array(
				'audit_usua', 
				'numerical', 
				'integerOnly' => true,
				'message' => 'Solo se permite números enteros.'
			),
			array(
				'unid_fechafin, 
				unid_habilitado', 
				'safe'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels(){
		return array(
			'unid_codigo_pk' => 'Código',
			'unid_valor' => 'Valor',
			'unid_fechacomienzo' => 'Fecha Inicio',
			'unid_fechafin' => 'Fecha Fin',
			'unid_habilitado' => 'Habilitado',
			'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 SidcaiUnidadtributaria 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->unid_fechafin != "" && $this->unid_fechacomienzo != ""){
			$fecha_inicio = strtotime(str_replace("/", "-", $this->unid_fechacomienzo));
			$fecha_fin = strtotime(str_replace("/", "-", $this->unid_fechafin));

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