· 5 years ago · Sep 26, 2020, 05:40 PM
1-- Function: public.generar_citi_compras(bigint)
2
3-- DROP FUNCTION public.generar_citi_compras(bigint);
4-- Function: public.validar_cuit(bigint)
5
6-- DROP FUNCTION public.validar_cuit(bigint);
7
8CREATE OR REPLACE FUNCTION public.validar_cuit(bigint)
9 RETURNS boolean AS
10$BODY$
11DECLARE
12 RES BIGINT;
13 DIG BIGINT;
14 NUM BIGINT;
15 CUIT ALIAS FOR $1;
16
17BEGIN
18 IF LENGTH(CUIT :: text) != 11 OR SUBSTR(CUIT:: text, 1, 2) = '00' THEN
19 RETURN 0;
20 END IF;
21 RES = 0;
22 FOR I IN 1..10 LOOP
23 NUM := (SUBSTR(CUIT:: text, I, 1));
24 IF (I = 1 OR I = 7) THEN RES := RES + NUM * 5;
25 ELSIF (I = 2 OR I = 8) THEN RES := RES + NUM * 4;
26 ELSIF (I = 3 OR I = 9) THEN RES := RES + NUM * 3;
27 ELSIF (I = 4 OR I = 10) THEN RES := RES + NUM * 2;
28 ELSIF (I = 5) THEN RES := RES + NUM * 7;
29 ELSIF (I = 6) THEN RES := RES + NUM * 6;
30 END IF;
31 END LOOP;
32 DIG := 11 - MOD(RES,11);
33 IF DIG = 11 THEN
34 DIG := 0;
35 END IF;
36
37 IF DIG = (SUBSTR(CUIT:: text,11,1)) :: int THEN
38 RETURN 1;
39 ELSE
40 RETURN 0;
41 END IF;
42END;
43$BODY$
44 LANGUAGE plpgsql VOLATILE
45 COST 100;
46ALTER FUNCTION public.validar_cuit(bigint)
47 OWNER TO postgres;
48
49
50CREATE OR REPLACE FUNCTION public.generar_citi_compras(p_periodoabono_id bigint)
51 RETURNS SETOF record AS
52$BODY$
53DECLARE
54BEGIN
55
56 drop table if exists cmp;
57 create temp table cmp as
58 select distinct comp.id
59 from ccpiva c
60 join comprobante comp
61 on comp.id = c.comprobante_id
62 where comp.periodocontable_id = p_periodoabono_id
63 and comp.anulado = false ;
64
65 drop table if exists trib;
66 create temp table trib
67 as
68 select comprobante_id ,
69 tipototalafip,
70 codigooperacion,
71 datos[1] tasaivaid,
72 datos[2] importeneto,
73 datos[3] importeiva,
74 datos[4] importeexento,
75 datos[5] impuestosNacionales,
76 datos[6] impuestosProvinciales,
77 datos[7] impuestosMunicipales,
78 datos[8] impuestosInternos
79 from (
80 select
81 comp.id comprobante_id,
82 TipoTotalAfip ,
83 case tasaiva.TipoTotalAfip
84 when 'I' then
85 array[TasaIva.TipoIvaAfipId, coalesce(c.importeneto, 0.00), c.ImporteIva, 0, 0, 0, 0, 0]
86 -- importeTotal = importeTotal.add(facturaIVA.getImporteNeto()).add(facturaIVA.getImporteIva());
87 --when 'E' then
88 else
89 case coalesce(tasaiva.OtrosTributosAfipId, 0)
90 when 0 then array[TasaIva.TipoIvaAfipId, 0.00, 0.00, importeexento, 0.00, 0.00, 0.00, 0.00]
91 when 1 then array[TasaIva.TipoIvaAfipId, 0.00, 0.00, 0.00, importeexento, 0.00, 0.00, 0.00]
92 when 2 then array[TasaIva.TipoIvaAfipId, 0.00, 0.00, 0.00, 0.00, importeexento, 0.00, 0.00]
93 when 3 then array[TasaIva.TipoIvaAfipId, 0.00, 0.00, 0.00, 0.00, 0.00, importeexento, 0.00]
94 when 4 then array[TasaIva.TipoIvaAfipId, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, importeexento]
95 end
96 end datos,
97 case when tasaiva.TipoTotalAfip = 'E' then 'E' else null end codigooperacion
98 from ccpiva c
99 inner join comprobante comp
100 on comp.id = c.comprobante_id
101 inner join tasaiva
102 on tasaiva.id = c.tasaiva_id
103 inner join tipocomprobante tc
104 on tc.id = comp.tipocomprobante_id
105 where c.comprobante_id in (select id from cmp)
106 ) v;
107
108 -- 0 tasaivaid
109 -- 1 neto
110 -- 2 importeiva
111 -- 3 importeexento
112 -- 4 impuestosNacionales
113 -- 5 impuestosProvinciales
114 -- 6 impuestosMunicipales
115 -- 7 impuestosInternos
116
117 drop table if exists cbte;
118 create temp table cbte
119 as
120 select
121 comp.id comprobante_id,
122 case substring(comp.nrocomprobante, 1, 1)
123 when 'A' then
124 case tc.tipocomprobantefiscal
125 when 'F' then 1
126 when 'T' then 1
127 when 'C' then 3
128 when 'D' then 2
129 end
130 when 'B' then
131 case tc.tipocomprobantefiscal
132 when 'F' then 6
133 when 'T' then 6
134 when 'C' then 8
135 when 'D' then 7
136 end
137 when 'C' then
138 case tc.tipocomprobantefiscal
139 when 'F' then 11
140 when 'T' then 11
141 when 'C' then 13
142 when 'D' then 12
143 end
144 when 'M' then
145 case tc.tipocomprobantefiscal
146 when 'F' then 51
147 when 'T' then 51
148 when 'C' then 53
149 when 'D' then 52
150 end
151 end tipocomprobante,
152 fecha fechacomprobante,
153 substring(comp.nrocomprobante, 2, 4) puntoventa,
154 substring(comp.nrocomprobante, 6, 8) nrocomprobantedesde,
155 substring(comp.nrocomprobante, 6, 8) nrocomprobantehasta,
156 case
157 when not prov.cuit is null and length(prov.Cuit) > 0 and prov.Cuit <> '00000000000' then 80
158 else case when prov.Dni is null or prov.dni = 0 then 99 else 96 end
159 end TipoDocEmisor,
160 case
161 when not prov.cuit is null and length(prov.Cuit) > 0 and prov.Cuit <> '00000000000' then replace(prov.Cuit :: text, '-', '')
162 else prov.dni ::text
163 end nrodocEmisor,
164 prov.Nombre nombreEmisor,
165 max(codigooperacion) codigooperacion,
166 --cbte.setImporteTotal(importeTotal.add(importeConceptos).add(importeExento));
167 sum(importeNeto) importeNeto,
168 sum(importeIva) importeIva,
169 0 importeConceptos,
170 0 ImportePercepcionNoCategorizados,
171 sum(importeExento) importeExento,
172 sum(impuestosNacionales) ImporteImpuestosNacionales,
173 sum(impuestosProvinciales) ImporteIngBrutos,
174 sum(impuestosMunicipales) ImporteImpuestosMunicipales,
175 sum(impuestosInternos) ImpuestosInterno,
176 'PES' :: varchar(3) CodigoMoneda,
177 1 TipoCambio,
178 --count(case trib.*) cantidadAlicuotas,
179 --count(trib.*) filter (where tasaivaid in (1,4,5,6,7)) as cantidadAlicuotas,
180 count(trib.*) filter (where tipototalafip = 'I' ) as cantidadAlicuotas,
181 0 ImporteOtrosTributos,
182 null :: date FechaVencimiento
183 from comprobante comp
184 inner join tipocomprobante tc
185 on tc.id = comp.tipocomprobante_id
186 inner join proveedor prov
187 on prov.id = comp.proveedor_id
188 left join trib
189 on trib.comprobante_id = comp.id
190 where comp.id in (select id from cmp)
191 group by comp.id , tc.tipocomprobantefiscal, prov.cuit, prov.Dni , prov.nombre;
192
193 drop table if exists er;
194 create temp table er
195 as
196 select concat('CUIT VENDEDOR INVALIDO ', nombreEmisor)
197 from cbte
198 where validar_cuit(nroDocEmisor :: bigint) = false
199 union
200 select concat('NUMERO COMPROBANTE INVALIDO: ', nombreEmisor, ' NRO ', puntoventa, nrocomprobantedesde )
201 from cbte
202 where tipocomprobante is null or puntoventa :: int < 1 or nrocomprobantedesde :: int < 1
203 union
204 select concat('COMPROBANTE DUPLICADO - PROV ', nombreemisor, ' NRO ', nrocomprobantedesde)
205 from cbte
206 group by nombreemisor, puntoventa, nrocomprobantedesde
207 having count(*) > 1
208 union
209 SELECT concat('ALICUOTA DUPLICADA: - PROV ', nombreemisor, ' NRO ', nrocomprobantedesde)
210 from trib
211 inner join cbte
212 on cbte.comprobante_id = trib.comprobante_id
213 group by trib.comprobante_id, trib.tasaivaid, cbte.nombreEmisor, cbte.nrocomprobantedesde
214 having count(*) > 1
215 ;
216
217copy er to '/tmp/ERRORES_CITI_COMPRAS.txt';
218
219 return query
220 select * from (
221 select
222 concat(
223 'CBTE|',
224 lpad(to_char(fechacomprobante, 'yyyymmdd'), 8, ' ') ,
225 lpad(coalesce(tipoComprobante :: text, ''), 3, '0') ,
226 lpad(puntoVenta:: text, 5, '0') ,
227 lpad(nroComprobanteDesde:: text, 20, '0') ,
228 rpad('', 16, ' ') ,
229 lpad(tipoDocEmisor :: text, 2, '0') ,
230 lpad(coalesce(nroDocEmisor :: text, ''), 20, '0') ,
231 rpad(nombreEmisor, 30, ' ') ,
232 lpad(round(
233 (importeneto + importeiva + importeConceptos + importePercepcionNoCategorizados + importeExento + importeImpuestosNacionales + importeIngBrutos + importeImpuestosMunicipales + impuestosInterno) * 100,
234 0) :: text, 15, '0' ) ,
235 lpad(round(importeConceptos * 100, 0) :: text, 15, '0' ) ,
236 lpad((case when tipoComprobante in (6,7,8,11,12,13) then 0 else round(importeExento * 100, 0) end):: text, 15, '0' ) ,
237 lpad(round(importePercepcionNoCategorizados * 100, 0) :: text, 15, '0' ) ,
238 lpad(round(importeImpuestosNacionales * 100, 0) :: text, 15, '0' ) ,
239 lpad(round(importeIngBrutos * 100, 0) :: text, 15, '0' ) ,
240 lpad(round(importeImpuestosMunicipales * 100, 0) :: text, 15, '0' ) ,
241 lpad(round(impuestosInterno * 100, 0) :: text, 15, '0' ) ,
242 rpad(codigoMoneda, 3, ' ') ,
243 lpad(round(tipoCambio * 1000000, 0) :: text, 10, '0') ,
244 --lpad((case when tipoComprobante in (6,7,8,11,12,13) then 0 else round(cantidadAlicuotas, 0) end) :: text, 1, '0' ) ,
245 --lpad((case when codigoOperacion = 'E' then round(cantidadAlicuotas - 1, 0) else round(cantidadAlicuotas, 0) end) :: text, 1, '0' ) ,
246 lpad(round(cantidadAlicuotas, 0) :: text, 1, '0' ) ,
247 rpad(coalesce(codigoOperacion, ''), 1, ' ') ,
248 lpad(round(importeIva * 100, 0) :: text, 15, '0' ) ,
249 lpad(round(importeOtrosTributos * 100, 0) :: text, 15, '0' ) ,
250 rpad('', 11, '0'),
251 rpad('', 30, ' '),
252 lpad(round(0 * 100, 0) :: text, 15, '0' )
253 )
254 from cbte
255 order by
256 lpad(to_char(fechacomprobante, 'yyyymmdd'), 8, ' ') ,
257 lpad(coalesce(tipoComprobante :: text, ''), 3, '0'),
258 lpad(puntoVenta:: text, 5, '0') ,
259 lpad(nroComprobanteDesde:: text, 20, '0')
260 ) a
261
262 union all
263
264 select *
265 from (
266 select
267 concat (
268 'ALICUOTAS|',
269 lpad(coalesce(tipoComprobante :: text, ''), 3, '0'),
270 lpad(puntoventa:: text, 5, '0'),
271 lpad(nrocomprobantedesde:: text, 20, '0'),
272 lpad(tipoDocEmisor :: text, 2, '0') ,
273 lpad(nroDocEmisor :: text, 20, '0') ,
274 lpad(round(trib.importeneto * 100, 0) :: text, 15, '0' ) ,
275 lpad(tasaivaid:: text, 4, '0'),
276 lpad(round(trib.importeiva * 100, 0) :: text, 15, '0' )
277 )
278 from trib
279 inner join cbte
280 on cbte.comprobante_id = trib.comprobante_id
281 where tipototalafip = 'I'
282 order by lpad(to_char(fechacomprobante, 'yyyymmdd'), 8, ' ') ,
283 lpad(coalesce(tipoComprobante :: text, ''), 3, '0') ,
284 lpad(puntoVenta:: text, 5, '0') ,
285 lpad(nroComprobanteDesde:: text, 20, '0')
286
287 ) b;
288
289end $BODY$
290 LANGUAGE plpgsql VOLATILE
291 COST 100
292 ROWS 1000;
293ALTER FUNCTION public.generar_citi_compras(bigint)
294 OWNER TO postgres;
295
296-- select * from periodocontable where sistema = 'P' order by id desc limit 10
297
298 select * from generar_citi_compras(10211) as (texto text)
299
300
301