<?php

/**
 * This is the model class for table "datos_txt_funerario".
 *
 * The followings are the available columns in table 'datos_txt_funerario':
 * @property integer $id
 * @property string $nmb_archivo
 * @property string $descripcion
 * @property boolean $aprobado
 * @property string $fecha_aprobacion
 * @property string $fecha_registro
 * @property integer $id_usuario
 * @property string $num_comprobante
 * @property string $total_aporte
 * @property integer $cant_aportantes
 * @property boolean $valido
 * @property string $fecha_aporte
 * @property integer $idunidad
 */
class DatosTxtFunerario extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'datos_txt_funerario';
	}


	/**
	 * @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('nmb_archivo, fecha_registro', 'required'),
			array('id_usuario, cant_aportantes, idunidad', 'numerical', 'integerOnly'=>true),
			array('nmb_archivo', 'length', 'max'=>20),
                     
			array('descripcion', 'length', 'max'=>300),
			array('num_comprobante', 'length', 'max'=>30),
			array('aprobado,  pagado,fecha_aprobacion, total_aporte, valido, fecha_aporte,blnborrado', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, nmb_archivo, descripcion, aprobado, pagado, fecha_aprobacion, fecha_registro, id_usuario, num_comprobante, total_aporte, cant_aportantes, valido, fecha_aporte, idunidad', '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(
                    'unidad' => array(self::BELONGS_TO, 'Unidad', 'idunidad'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nmb_archivo' => 'Nmb Archivo',
			'descripcion' => 'Descripcion',
			'aprobado' => 'Aprobado',
			'fecha_aprobacion' => 'Fecha Aprobacion',
			'fecha_registro' => 'Fecha Registro',
			'id_usuario' => 'Usuario',
			'num_comprobante' => 'Num Comprobante',
			'total_aporte' => 'Pago Funerario',
			'cant_aportantes' => 'Cant Aportantes',
			'valido' => 'Valido',
			'fecha_aporte' => 'Fecha Aporte',
			'idunidad' => 'Unidad',
			'pagado' => 'Pagado',
		);
	}

	/**
	 * 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;
                                $filtro="";
		if (Yii::app()->user->checkAccess('usuario_asociado')){
		$id=Yii::app()->user->id;		                
                $filtro="id_usuario='$id' and blnborrado=false";                                   
		}
                if (Yii::app()->user->checkAccess('usuario_administrador')){		
		$filtro="  valido= true and blnborrado=false";		
		}
                $criteria->condition = $filtro;
		$criteria->compare('id',$this->id);
		$criteria->compare('nmb_archivo',$this->nmb_archivo,true);
		$criteria->compare('descripcion',$this->descripcion,true);
		$criteria->compare('aprobado',$this->aprobado);
		$criteria->compare('pagado',$this->pagado);
		$criteria->compare('fecha_aprobacion',$this->fecha_aprobacion,true);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('id_usuario',$this->id_usuario);
		$criteria->compare('num_comprobante',$this->num_comprobante,true);
		$criteria->compare('total_aporte',$this->total_aporte,true);
		$criteria->compare('cant_aportantes',$this->cant_aportantes);
		$criteria->compare('valido',$this->valido);
		$criteria->compare('fecha_aporte',$this->fecha_aporte,true);
		$criteria->compare('idunidad',$this->idunidad);
		$_SESSION['datos_filtrados'] = new CActiveDataProvider($this, array(
                      'criteria'=>$criteria,
                      'sort'=>array( 'defaultOrder'=>'id DESC', ),
                      'pagination'=>false,
              ));
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array( 'defaultOrder'=>'pagado ASC, id DESC', ),
		));
	}

	/**
	 * 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 DatosTxtFunerario 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',
	    );
	}
}
