<?php

/**
 * This is the model class for table "sidcai_cambio_moneda".
 *
 * The followings are the available columns in table 'sidcai_cambio_moneda':
 * @property integer $act_codigo_pk
 * @property string $act_nombre
 * @property string $act_porcentaje
 * @property boolean $act_habilitado
 */
class SidcaiActividadEconomica extends CActiveRecord{
	/**
	 * @return string the associated database table name
	 */
	public function tableName(){
		return 'sidcai_actividad_economica';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
{
    return array(
        array('act_nombre', 'required', 'message' => 'Este campo es requerido.'),
        array('act_nombre', 'filter', 'filter' => 'trim'),
        
        // Reglas modificadas para act_porcentaje
        array('act_porcentaje', 'numerical', 'min'=>0, 'max'=>100,
              'integerOnly'=>false, 'message'=>'Debe ser un valor decimal entre 0 y 100'),
        array('act_porcentaje', 'match', 'pattern'=>'/^\d{1,3}(\.\d{1,2})?$/',
              'message'=>'Formato inválido (ej. 0.5 o 1.25)'),
        
        array('act_nombre', 'length', 
              'min' => 4, 'tooShort' => 'Mínimo 4 caracteres.',
              'max' => 25, 'tooLong' => 'Máximo 25 caracteres.'),
        
        array('act_nombre', 'unique', 
              'attributeName' => 'act_nombre',
              'className' => 'SidcaiActividadEconomica',
              'message' => '{value} ya se encuentra registrado.',
              'on' => 'agregar'),
        
        array('act_nombre', 'existeNombre', 'on' => 'modificar'),
        array('act_habilitado', 'safe'),
    );
}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels(){
		return array(
			'act_codigo_pk' => 'Código',
			'act_nombre' => 'Nombre',
			'act_porcentaje' => 'Porcentaje Alicuota',
			'act_habilitado' => 'Habilitado',
		);
	}

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

	/* Si el nombre que está guardado en la Base de Datos es distinto al que se quiere editar que compruebe que no exista otro registro con el mismo nombre. */
	public function existeNombre(){
		$original = SidcaiActividadEconomica::model()->findByPk($this->act_codigo_pk);
		$nuevo = SidcaiActividadEconomica::model()->find('act_nombre = :act_nombre', array(':act_nombre' =>$this->act_nombre ));

		if($nuevo != null){
			if($nuevo->act_codigo_pk != $original->act_codigo_pk)
				$this->addError('act_nombre', $this->act_nombre. ' ya se encuentra registrado.');
		}
	}



	public static function getActividadEcon(){
		$actividad_econ = SidcaiActividadEconomica::model()->findAll('act_habilitado = :act_habilitado', array(':act_habilitado' => true));

		return CHtml::listData($actividad_econ, 'act_codigo_pk', 'act_nombre');
	}
}
