<?php

/**
 * This is the model class for table "prestamos.tipo_credito_cuotas_especiales".
 *
 * The followings are the available columns in table 'prestamos.tipo_credito_cuotas_especiales':
 * @property string $id_tipo_credito
 * @property integer $numero_cuota
 * @property string $fecha_cuota
 * @property string $id_usuario
 * @property string $fecha_registro
 * @property boolean $blnborrado
 */
class TipoCreditoCuotasEspeciales extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'prestamos.tipo_credito_cuotas_especiales';
	}

	/**
	 * @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('fecha_cuota, porcentaje', 'required'),
			array('numero_cuota', 'numerical', 'integerOnly'=>true),
                        array('porcentaje', 'length', 'max'=>6),
                        array('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,00',),
			array('fecha_cuota, id_usuario, fecha_registro, blnborrado, porcentaje', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_tipo_credito, numero_cuota, fecha_cuota, id_usuario, fecha_registro, blnborrado', '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(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_tipo_credito' => 'Id Tipo Credito',
			'numero_cuota' => 'Numero Cuota',
			'fecha_cuota' => 'Fecha Cuota',
			'id_usuario' => 'Id Usuario',
			'fecha_registro' => 'Fecha Registro',
			'blnborrado' => 'Blnborrado',
		);
	}

	/**
	 * 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()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_tipo_credito',$this->id_tipo_credito,true);
		$criteria->compare('numero_cuota',$this->numero_cuota);
		$criteria->compare('fecha_cuota',$this->fecha_cuota,true);
		$criteria->compare('id_usuario',$this->id_usuario,true);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('blnborrado',$this->blnborrado);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        public function beforeSave(){            
            $this->porcentaje =  str_replace('.', '', $this->porcentaje);
            $this->porcentaje=str_replace(',', '.', $this->porcentaje);           
            $this->id_usuario=Yii::app()->user->id;
            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 TipoCreditoCuotasEspeciales the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
