<?php

/**
 * This is the model class for table "aporte_extra".
 *
 * The followings are the available columns in table 'aporte_extra':
 * @property integer $id
 * @property integer $id_trabajador
 * @property double $monto_pagar
 * @property integer $tipo_pago
 * @property double $monto_cuota
 * @property double $monto_faltante
 * @property string $fecha_fin
 * @property string $fecha_registro
 * @property boolean $blbnborrado
 *
 * The followings are the available model relations:
 * @property Asociado $idTrabajador
 */
class AporteExtra extends CActiveRecord
{
    public $cedula;
    public $unidad;
           /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'aporte_extra';
	}

	/**
	 * @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('id_trabajador,fecha_inicio,fecha_fin,tipo_pago,monto_cuota,tipo_pago', 'required'),
			array('id_trabajador, tipo_pago', 'numerical', 'integerOnly'=>true),
			array('monto_pagar, monto_cuota, monto_faltante', 'numerical'),
			array('empresa,tipotrabajador,fecha_inicio,id_trabajador,fecha_fin, blnborrado', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('cedula,empresa,id,tipotrabajador, unidad ,id_trabajador, monto_pagar, tipo_pago, monto_cuota, monto_faltante, fecha_fin, 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(
		'trabajador' => array(self::BELONGS_TO, 'Asociado','id_trabajador'),
                'empresas' => array(self::BELONGS_TO, 'LugarTrabajo', 'empresa'),
		'tipopago' => array(self::BELONGS_TO, 'Tipopago', 'tipo_pago'),
                 'tipo' => array(self::BELONGS_TO, 'Tipotrabajador', 'tipotrabajador'),   
		);
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_trabajador' => 'Trabajador',
                        'tipotrabajador' => 'Tipo Trabajador',
			'monto_pagar' => 'Monto Pagar',
			'tipo_pago' => 'Tipo Pago',
			'monto_cuota' => 'Monto Cuota',
			'monto_faltante' => 'Monto Faltante',
			'fecha_fin' => 'Fecha Fin',
			'fecha_registro' => 'Fecha Registro',
                        'empresa' => 'Empresa',
			'blnborrado' => 'Blnborrado',
                        'unidad' => 'unidad',
		);
	}

	/**
	 * 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="";
		$user=Yii::app()->user->id;
        	$sql = "select * from cruge_user where iduser= $user and state= 1";
		$resp = Yii::app()->db->createCommand($sql)->queryAll();
        	$cedula= $resp[0]['cedula'];
		if (Yii::app()->user->checkAccess('usuario_asociado')){
                $fecha = Asociado::model()->findByAttributes(array('cedula'=>$cedula),'blnborrado=:status',array(':status'=>false));           				
                if($fecha!=null){
			$idtrabajador=$fecha->idasociado;
		$filtro="and id_trabajador='$idtrabajador'";
		}				
		}
                $sort=new CSort();
                $criteria->condition = "t.blnborrado=false $filtro";
		$sort->defaultOrder='id_trabajador DESC';                
		$criteria->compare('id',$this->id);
                $criteria->compare('tipotrabajador',$this->tipotrabajador);
                $criteria->compare('empresa',$this->empresa);
		$criteria->compare('monto_pagar',$this->monto_pagar);
		$criteria->compare('tipo_pago',$this->tipo_pago);
		$criteria->compare('monto_cuota',$this->monto_cuota);
		$criteria->compare('monto_faltante',$this->monto_faltante);
		$criteria->compare('fecha_fin',$this->fecha_fin,true);
		$criteria->compare('fecha_registro',$this->fecha_registro);
		$criteria->compare('blnborrado',$this->blnborrado);
                $criteria->addSearchCondition('trabajador.nombre || trabajador.apellidos || trabajador.cedula',  strtoupper($this->id_trabajador),true);
                $criteria->addSearchCondition('trabajador.idunidad::text',$this->unidad,false);
                $criteria->with = 'trabajador';               
          $_SESSION['datos_filtrados'] = new CActiveDataProvider($this, array(
                      'criteria'=>$criteria,                      
                      'pagination'=>false,
              ));
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
		         

	}
		public function getDropdown()
	{
	    $values = array(
	    0 => 'Seleccione',
		//1 => 'Visualizar',
		2 => 'Actualizar',		
		4 => 'Imprimir Planilla',
	    );
	    return CHtml::dropDownlist('acc','acc',$values, array(
		'class'     => 'values',
		'data-id'   => $this->id,
		'onChange'=>'ValidarRuta('.$this->id.')',
		
	    ));
	}

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