· 7 years ago · Feb 10, 2019, 08:16 AM
1drop function if exists mcom.fn_ofertas_referente(integer [], integer, integer, integer, integer);
2--crear mcom.fn_ofertas_referente
3create or replace function mcom.fn_ofertas_referente(metodo integer [], id_centro_inatec integer,
4 idreferente integer, anio_oferta integer, mes integer default null)
5 returns TABLE(
6 id_oferta bigint,
7 grupo_oferta character varying,
8 id_referente bigint,
9 nombre_referente text,
10 cupo_oferta_beca_nacional integer,
11 cupo_oferta_fondos_propios integer,
12 metodo_evaluacion smallint
13 )
14language plpgsql
15as $$
16DECLARE CONSULTA VARCHAR;
17BEGIN
18
19 CONSULTA := 'SELECT o.id as id_oferta,
20 o.grupo as grupo_oferta,
21 ef.id,
22 UPPER(C.descripcion) AS nombre_referente,
23 P.cupo AS cupo_beca_nacional,
24 P.cupofp AS cupo_fondos_propios,
25 P.metodo_evaluacion
26 FROM registro_cobranza.rg_oferta o
27 INNER JOIN sac.acuerdos_detalles acd ON (acd.ID = o.id_acuerdo_deta)
28 INNER JOIN sac.acuerdos ac ON (acd.acuerdoid = ac.acuerdoid)
29 INNER JOIN registro_cobranza.cu_curso_clasificacion cl ON (
30 acd.id_curso_clasificacion = cl.ID
31 )
32 INNER JOIN registro_cobranza.cu_estructura_formativa ef ON (
33 ef.ID = cl.id_estructura_formativa
34 )
35 INNER JOIN registro_cobranza.cu_cat_curso C ON cl.id_curso = C.ID
36 LEFT JOIN registro_cobranza.indicadores_vis ON registro_cobranza.indicadores_vis.ID = o.id_indicador_detalle
37 INNER JOIN registro_cobranza.reg_catalogo_turno ON o.id_turno =
38 registro_cobranza.reg_catalogo_turno.id_turno
39 INNER JOIN registro_cobranza.cu_det_rama_familia drf ON C
40 .id_det_rama_familia = drf.ID
41 INNER JOIN registro_cobranza.cu_det_sector_rama dsr ON drf.id_sector_rama = dsr.ID
42 INNER JOIN PUBLIC.tb_sectores s ON s.id_sector = dsr.id_sector
43 INNER JOIN registro_cobranza.rg_programacion P ON P.ID = o.id_programacion
44 WHERE C.id_tipo_evento = 4
45 AND o.activo IN (1) and';
46
47 IF id_centro_inatec != -1
48 THEN
49 CONSULTA := CONSULTA || ' o.id_centro = ' || id_centro_inatec || ' AND';
50 end if;
51
52 IF idreferente != -1
53 THEN
54 CONSULTA := CONSULTA || ' ef.ID = ' || idreferente || ' AND';
55 end if;
56
57 CONSULTA := CONSULTA || ' o.anio_ofertado = ' || anio_oferta || ' AND';
58
59 CONSULTA := CONSULTA || ' P.metodo_evaluacion in (' || (select * from array_to_string(ARRAY [$1], ',')) ||
60 ')';
61 if $5 is not null
62 then
63 CONSULTA := CONSULTA || ' and o.mes_ofertado=' || $5;
64 end if;
65
66 consulta := CONSULTA || 'ORDER BY o.finicio';
67 RETURN QUERY EXECUTE CONSULTA;
68END;
69$$;