<?php

/**
 * This is the model class for table "prestamos.datos_txt_credito".
 *
 * The followings are the available columns in table 'prestamos.datos_txt_credito':
 * @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_descuento
 * @property integer $cant_aportantes
 * @property boolean $valido
 * @property string $fecha_descuento
 * @property integer $idunidad
 * @property integer $pagado
 * @property string $fecha_pagado
 * @property string $observaciones
 * @property boolean $migrado
 * @property boolean $aprob_migrad
 * @property boolean $pago_m
 * @property boolean $pago_migrado
 */
class CreditoTxt extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
        public $fecha_registro_range = array(); 
         public $banco; 
         public $referencia; 
         public $observacion;
	public function tableName()
	{
		return 'prestamos.datos_txt_credito';
	}

	/**
	 * @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, pagado', 'numerical', 'integerOnly'=>true),
			array('nmb_archivo', 'length', 'max'=>20),
                        array('banco,referencia', 'required', 'on' => 'pagar_form'),    
			array('descripcion', 'length', 'max'=>300),
			array('num_comprobante', 'length', 'max'=>30),
			array('aprobado,banco,referencia,observacion,fecha_aprobacion, total_descuento, valido, fecha_descuento, fecha_pagado, observaciones, migrado, aprob_migrad, pago_m, pago_migrado', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, nmb_archivo,fecha_registro_range, descripcion, aprobado, fecha_aprobacion, fecha_registro, id_usuario, num_comprobante, total_descuento, cant_aportantes, valido, fecha_descuento, idunidad, pagado, fecha_pagado, observaciones, migrado, aprob_migrad, pago_m, pago_migrado', '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(
                'unidades' => 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' => 'Id Usuario',
			'num_comprobante' => 'Num Comprobante',
			'total_descuento' => 'Total Descuento',
			'cant_aportantes' => 'Cant Aportantes',
			'valido' => 'Valido',
			'fecha_descuento' => 'Fecha Descuento',
			'idunidad' => 'Idunidad',
			'pagado' => 'Pagado',
			'fecha_pagado' => 'Fecha Pagado',
			'observaciones' => 'Observaciones',
			'migrado' => 'Migrado',
			'aprob_migrad' => 'Aprob Migrad',
			'pago_m' => 'Pago M',
			'pago_migrado' => 'Pago Migrado',
		);
	}

	/**
	 * 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_administrador'))){
		$id=Yii::app()->user->id;		                
                $filtro="id_usuario='$id'";                                   
		}
                if (Yii::app()->user->checkAccess('usuario_administrador')){		
		$filtro="  valido= true";		
		}
                $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('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_descuento',$this->total_descuento,true);
		$criteria->compare('cant_aportantes',$this->cant_aportantes);
		$criteria->compare('valido',$this->valido);
		$criteria->compare('fecha_descuento',$this->fecha_descuento,true);
		$criteria->compare('idunidad',$this->idunidad);
		$criteria->compare('pagado',$this->pagado);
		$criteria->compare('fecha_pagado',$this->fecha_pagado,true);
		$criteria->compare('observaciones',$this->observaciones,true);
		$criteria->compare('migrado',$this->migrado);
		$criteria->compare('aprob_migrad',$this->aprob_migrad);
		$criteria->compare('pago_m',$this->pago_m);
		$criteria->compare('pago_migrado',$this->pago_migrado);
             
            $from = $to = '';
             if (count($this->fecha_registro_range)>=1) {
              if (isset($this->fecha_registro_range['from'])) {
                  $from = $this->fecha_registro_range['from'];
              }
              if (isset($this->fecha_registro_range['to'])) {
                  $to= $this->fecha_registro_range['to'];
              }
          }
          if ($from!='' || $to !='') {
              if ($from!='' && $to!='') {
                  $from = date("d-m-Y", strtotime($from));
                  $to = date("d-m-Y", strtotime($to));
                  $criteria->compare('fecha_registro',">= $from",false);
                  $criteria->compare('fecha_registro',"<= $to",false);
              }
              else {
                  if ($from!='') $creation_time = $from;
                  if ($to != '') $creation_time = $to;
                  $creation_time = date("d-m-Y", strtotime($creation_time));
                  $criteria->compare('fecha_registro', "$creation_time" ,false);
              }
          }
                
                
		$_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'=>'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 CreditoTxt the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
