<?php

/**
 * This is the model class for table "sidcai_cambio_tasa".
 *
 * The followings are the available columns in table 'sidcai_cambio_tasa':
 * @property integer $cata_codigo_pk
 * @property integer $camo_codigo_fk
 * @property string $cata_nombre
 * @property string $cata_simbolo
 * @property string $cata_fecha
 * @property string $cata_tasa
 * @property boolean $cata_habilitado
 */
class SidcaiCambioTasa extends CActiveRecord{
	/**
	 * @return string the associated database table name
	 */
	public function tableName(){
		return 'sidcai_cambio_tasa';
	}

	/**
	 * @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(
				'camo_codigo_fk, cata_fecha, cata_tasa', 
				'required',
				'message' => 'Este campo es requerido.'
			),
			array(
				'cata_nombre, cata_simbolo', 
				'filter', 
				'filter' => 'trim'
			),
			array(
				'cata_nombre', 
				'length', 
				'min' => 4,
				'tooShort' => 'Mínimo 4 caracteres.',
				'max' => 25,
				'tooLong' => 'Máximo 25 caracteres.',
			),
			array(
				'cata_simbolo', 
				'length', 
				'min' => 1,
				'tooShort' => 'Mínimo 1 caracteres.',
				'max' => 6,
				'tooLong' => 'Máximo 6 caracteres.',
			),
			array(
				'cata_tasa',
				'length',
				'max' => 22,
				'tooLong' => 'Ha superado el monto máximo permitido.',
			),
			array(
				'cata_fecha',
				'compare',
				'compareValue' => '00/00/0000',
				'operator' => '>',
				'message' => 'Debe seleccionar una fecha',
			),
			array(
				'cata_fecha',
				'existeTasa',
				'on' => 'agregar'
			),
			array(
				'cata_fecha',
				'existeTasaModificar',
				'on' => 'modificar'
			),
			array(
				'camo_nombre, camo_simbolo, cata_habilitado', 
				'safe'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels(){
		return array(
			'cata_codigo_pk' => 'Código',
			'camo_codigo_fk' => 'Código Moneda',
			'cata_nombre' => 'Nombre',
			'cata_simbolo' => 'Simbolo',
			'cata_fecha' => 'Fecha',
			'cata_tasa' => 'Tasa',
			'cata_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 SidcaiCambioTasa the static model class
	 */
	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public function existeTasa() {
		if($this->camo_codigo_fk != null && $this->camo_codigo_fk != '' && $this->cata_fecha != null && $this->cata_fecha != '') {
			if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $this->cata_fecha)) {
				$fecha = $this->cata_fecha;
			} else {
				Yii::import("application.controllers.FuncionesController");
				$fecha = FuncionesController::convertirFecha($this->cata_fecha, "yyyy-mm-dd");
			}

			$old = SidcaiCambioTasa::model()->find('camo_codigo_fk = :camo_codigo_fk AND cata_fecha = :cata_fecha', 
				array(
					':camo_codigo_fk' => $this->camo_codigo_fk,
					':cata_fecha' => $fecha
				)
			);

			if($old != null){
				$this->addError('cata_fecha', 'La tasa para la fecha ya se encuentra registrado.');
			}
		}
	}

	public function existeTasaModificar() {
		if($this->camo_codigo_fk != null && $this->camo_codigo_fk != '' && $this->cata_fecha != null && $this->cata_fecha != '') {
			if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $this->cata_fecha)) {
				$fecha = $this->cata_fecha;
			} else {
				Yii::import("application.controllers.FuncionesController");
				$fecha = FuncionesController::convertirFecha($this->cata_fecha, "yyyy-mm-dd");
			}

			$old = SidcaiCambioTasa::model()->find('camo_codigo_fk = :camo_codigo_fk AND cata_fecha = :cata_fecha', 
				array(
					':camo_codigo_fk' =>$this->camo_codigo_fk,
					':cata_fecha' =>$fecha
				)
			);

			if($old != null && $old->cata_codigo_pk != $this->cata_codigo_pk){
				$this->addError('cata_fecha', 'La tasa para la fecha ya se encuentra registrado.');
			}
		}
	}
}
