gke_arce_rupe2
.rupe
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
generar_datos_proveedor()
Parameters
Name
Type
Mode
Definition
declare /* variables auxiliares para actualizar los registros */ aux_prv_id integer; aux_prv_cod_fiscal varchar(255); aux_prv_crea_fecha timestamp; aux_prv_estado varchar(255); aux_prv_tipo_entidad varchar(255); aux_prv_pais varchar(255); aux_prv_tipo_documento varchar(255); aux_inciso_activador VARCHAR(255); aux_ue_activador VARCHAR(255); aux_cedula_activador VARCHAR(255); aux_fecha_activacion timestamp; aux_fecha_baja_voluntaria timestamp; aux_fecha_baja_dgi timestamp; aux_tiene_correos boolean; aux_tiene_documentos boolean; aux_tipo_usuario_activador VARCHAR(255); aux_cantidad_comunicaciones integer; aux_cantidad_documentos integer; tipo_usuario integer; unidad_ejecutora_id integer; inciso_id integer; proveedor_procesado integer; /* cursor que retorna las cuentas bancarias del banco BROU (no eliminadas y en estado: Pendiente Validar o Validada)*/ C_proveedor CURSOR FOR SELECT p.prv_id FROM rupe.rupe_proveedores p; reg_proveedor RECORD; BEGIN delete from rupe.exportar_proveedores_indicador; OPEN C_proveedor; LOOP FETCH C_proveedor INTO reg_proveedor; EXIT WHEN NOT FOUND; select id_interno from rupe.exportar_proveedores_indicador into proveedor_procesado where id_interno = reg_proveedor.prv_id; if not found then select p.prv_id, p.prv_cod_fiscal,p.prv_crea_fecha,e.eor_nombre , n.nju_nombre , pa.pai_nombre, case when tr.idr_codigo='' then tp.tip_codigo else tr.idr_codigo end as codigo_tipo_documento from rupe.rupe_proveedores p left outer join rupe.rupe_naturalezas_juridicas n on (p.prv_naturaleza_juridica_nju_id=n.nju_id) left outer join rupe.rupe_tipos_identificacion_persona tp on (p.prv_tipo_pf_unicidad=tp.tip_id), rupe.rupe_estados_proveedor e, rupe.rupe_paises pa, rupe.rupe_identificaciones_registro tr into aux_prv_id, aux_prv_cod_fiscal, aux_prv_crea_fecha, aux_prv_estado, aux_prv_tipo_entidad, aux_prv_pais, aux_prv_tipo_documento where p.prv_estado_eor_id=e.eor_id and p.prv_pais_nacionalidad_pai_id=pa.pai_id and p.prv_tipo_doc_registro=cast(tr.idr_id as varchar) and p.prv_id = reg_proveedor.prv_id; -- determino si tene correos aux_tiene_correos := false; select count(*) from rupe.rupe_datos_comunicacion_proveedor into aux_cantidad_comunicaciones where dco_proveedor_prv_id = aux_prv_id and dco_tipo_comunicacion_tcm_id = 6; if aux_cantidad_comunicaciones > 0 then aux_tiene_correos := true; end if; -- determino si tiene documentos aux_tiene_documentos := false; select count(*) from rupe.rupe_documentos_electronicos_proveedores into aux_cantidad_documentos where deo_proveedor_prv_id = aux_prv_id; if aux_cantidad_documentos > 0 then aux_tiene_documentos := true; end if; -- determino datos de activación aux_cedula_activador := null; aux_fecha_activacion := null; aux_ue_activador := null; aux_inciso_activador := null; inciso_id := null; unidad_ejecutora_id := null; tipo_usuario := null; select pa.prv_ultmod_fecha, pa.prv_ultmod_usu from audit.rupe_proveedores_aud pa into aux_fecha_activacion, aux_cedula_activador where pa.prv_id = aux_prv_id and pa.rev = (select min(rev) from audit.rupe_proveedores_aud r1 where r1.prv_estado_eor_id = 7 and aux_prv_id = r1.prv_id); aux_tipo_usuario_activador := null; -- detemino el organismo que activo al proveedor if found then select ua.usu_tipo,ua.usu_unidad_ejecutora_id from audit.rupe_usuarios_aud ua into tipo_usuario, unidad_ejecutora_id where ua.usu_codigo = aux_cedula_activador and ua.rev = (select max(rev) from audit.rupe_usuarios_aud r1 where r1.usu_ultmod_fecha < aux_fecha_activacion and aux_cedula_activador = r1.usu_codigo); aux_tipo_usuario_activador := 'ARCE'; -- usuario de punto de atención if tipo_usuario = 1 then aux_tipo_usuario_activador := 'ORGANISMO'; select uej_nombre,uej_inciso_inc_id from rupe.rupe_unidades_ejecutoras into aux_ue_activador, inciso_id where uej_id = unidad_ejecutora_id; select inc_nombre from rupe.rupe_incisos into aux_inciso_activador where inc_id = inciso_id; end if; end if; -- determino fecha de baja DGI si está en estado baja DGI aux_fecha_baja_dgi := null; if aux_prv_estado = 'BAJA DGI' then select pa.prv_ultmod_fecha from audit.rupe_proveedores_aud pa into aux_fecha_baja_dgi where pa.prv_id = aux_prv_id and pa.rev = (select min(rev) from audit.rupe_proveedores_aud r1 where r1.prv_estado_eor_id = 6 and aux_prv_id = r1.prv_id and not exists (select r2.rev from audit.rupe_proveedores_aud r2 where r2.rev > r1.rev and r2.prv_estado_eor_id != 6 and aux_prv_id = r2.prv_id)); end if; -- determino fecha de baja voluntaria si está en estado baja voluntaria aux_fecha_baja_voluntaria := null; if aux_prv_estado = 'BAJA VOLUNTARIA' then select pa.prv_ultmod_fecha from audit.rupe_proveedores_aud pa into aux_fecha_baja_voluntaria where pa.prv_id = aux_prv_id and pa.rev = (select min(rev) from audit.rupe_proveedores_aud r1 where r1.prv_estado_eor_id = 5 and aux_prv_id = r1.prv_id and not exists (select r2.rev from audit.rupe_proveedores_aud r2 where r2.rev > r1.rev and r2.prv_estado_eor_id != 5 and aux_prv_id = r2.prv_id)); end if; select id_interno from rupe.exportar_proveedores_indicador into proveedor_procesado where id_interno = aux_prv_id; if not found then insert into rupe.exportar_proveedores_indicador values (aux_prv_id, aux_prv_cod_fiscal,aux_prv_tipo_documento,aux_prv_pais,aux_prv_tipo_entidad, aux_prv_crea_fecha, aux_prv_estado, aux_inciso_activador, aux_ue_activador, aux_cedula_activador, aux_fecha_activacion, aux_fecha_baja_voluntaria, aux_fecha_baja_dgi, aux_tiene_correos, aux_tiene_documentos,aux_tipo_usuario_activador); end if; end if; END LOOP; CLOSE C_proveedor; Raise debug 'finalizo el proceso de generar tabla para exportar proveedores '; END;