<?php

/**
 * This is the model class for table "contable.reintegro".
 *
 * The followings are the available columns in table 'contable.reintegro':
 * @property integer $idreintegro
 * @property integer $idtrabajador
 * @property integer $idtipocredito
 * @property integer $idcredito
 * @property integer $mvm_cuota
 * @property double $monto_reintegro
 * @property string $fecha_solicitud
 * @property string $fecha_aprobacion
 * @property boolean $aprobado
 * @property boolean $blnborrado
 *
 * The followings are the available model relations:
 * @property Credito $idcredito
 * @property TipoCredito $idtipocredito
 * @property Asociado $idtrabajador
 * @property TipoMovimientocuota $mvmCuota
 */
class Reintegro extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'contable.reintegro';
	}

	/**
	 * @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('idtrabajador, idcredito_pago, idcredito, mvm_cuota, fecha_solicitud', 'required'),
			array('idtrabajador, idcredito_pago, idcredito, mvm_cuota', 'numerical', 'integerOnly'=>true),
			array('monto_reintegro', 'numerical'),
			array('fecha_aprobacion, aprobado, blnborrado', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idreintegro, idtrabajador, idcredito_pago, idcredito, mvm_cuota, monto_reintegro, fecha_solicitud, fecha_aprobacion, aprobado, 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(
			'credito' => array(self::BELONGS_TO, 'Credito', 'idcredito'),
			'credito_pago' => array(self::BELONGS_TO, 'CreditoPagos', 'idcredito_pago'),
			'trabajador' => array(self::BELONGS_TO, 'Asociado', 'idtrabajador'),
			'mvmCuota' => array(self::BELONGS_TO, 'TipoMovimientocuota', 'mvm_cuota'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idreintegro' => 'Idreintegro',
			'idtrabajador' => 'Idtrabajador',
			'idcredito_pago' => 'Credito Pago',
			'idcredito' => 'Idcredito',
			'mvm_cuota' => 'Mvm Cuota',
			'monto_reintegro' => 'Monto Reintegro',
			'fecha_solicitud' => 'Fecha Solicitud',
			'fecha_aprobacion' => 'Fecha Aprobacion',
			'aprobado' => 'Aprobado',
			'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('idreintegro',$this->idreintegro);
		//$criteria->compare('idtrabajador',$this->idtrabajador);
		$criteria->compare('idcredito_pago',$this->idcredito_pago);
		$criteria->compare('idcredito',$this->idcredito);
		$criteria->compare('mvm_cuota',$this->mvm_cuota);
		$criteria->compare('monto_reintegro',$this->monto_reintegro);
		$criteria->compare('fecha_solicitud',$this->fecha_solicitud,true);
		$criteria->compare('fecha_aprobacion',$this->fecha_aprobacion,true);
		$criteria->compare('aprobado',$this->aprobado);
		$criteria->compare('blnborrado',$this->blnborrado);
                $criteria->addSearchCondition('trabajador.nombre || trabajador.apellidos || trabajador.cedula',  strtoupper($this->idtrabajador),true);
                $criteria->with = 'trabajador';
       

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

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

      public function behaviors()
	{
	    return array(
		    // Classname => path to Class
		    'ActiveRecordLogableBehavior'=>
		    	'application.behaviors.ActiveRecordLogableBehavior',
	    );
	}
}
