use telus; /*verificar convenios antes del 2024*/ if OBJECT_ID('tempdb.dbo.#convenios_revisar') is not null drop table #convenios_revisar select id, create_date into #convenios_revisar from db_olympo_web.dbo.convenios_cabecera where code in('002068'); declare @cuotas_pagadas int, @total_lotes int, @fecha_convenio date --print @agreement_id; set @cuotas_pagadas = (select count(*) from db_olympo_web.dbo.convenios_detalle de where agreement_id in(select id from #convenios_revisar) and all_paid = 1); print 'Cuotas pagadas: ' + CAST(@cuotas_pagadas as VARCHAR(MAX)); set @fecha_convenio = (select MIN(create_date) from #convenios_revisar); if OBJECT_ID('tempdb.dbo.#lotes_convenio_re') is not null drop table #lotes_convenio_re select --top 10 *, cast('0.00' as numeric(18,2)) AS saldo_kardex, 0 AS revisar, 0 AS anio_min, 0 AS anio_max, 0 AS anio_max_recaudo into #lotes_convenio_re from agcm_2022_tram_liquidaciones_convenio lc where lc.id_convenio in(select id from #convenios_revisar) and lc.estado = 'ACTIVO'; set @total_lotes = (select COUNT(*) from #lotes_convenio_re); print 'Claves catastrales: ' + CAST(@total_lotes as VARCHAR(MAX)); DECLARE @id INT, @lote_id INT, @desde INT, @hasta INT, @valor NUMERIC(18,2), @valorinicial NUMERIC(18,2), @saldo NUMERIC(18,2) DECLARE procesar_cursor CURSOR FOR select id, lote_id, desde, hasta, valor, valorinicial from #lotes_convenio_re r where estado = 'ACTIVO' OPEN procesar_cursor FETCH NEXT FROM procesar_cursor INTO @id, @lote_id, @desde, @hasta, @valor, @valorinicial WHILE @@FETCH_STATUS = 0 BEGIN print @lote_id; declare @anio_max_recaudado int; declare @anio_max_kardex int; declare @anio_min_kardex int; declare @revisar int = 0; declare @date date = '2024-12-04'; declare @kardexDeu table (emision_id int, lote_id int,rubro_id int,ano_id int,base_imponible numeric (25,2),rubro varchar (max), saldo numeric (11,2) ,valor_porcentaje numeric (11,2)) insert into @kardexDeu exec telus.dbo.agcm_telus_avaluo_emision_consulta @lote_id,@date set @anio_max_kardex = (select MAX(ano_id) from @kardexDeu); set @anio_min_kardex = (select MIN(ano_id) from @kardexDeu); set @anio_max_recaudado = isnull((select MAX(pl.ano_id) from recaudacion_registro_pago as rp inner join recaudacion_registro_pago_lote as pl on rp.recaudacion_id = pl.recaudacion_id where lote_id = @lote_id),0); /*Para verificar si hay un anio pagado*/ if @anio_max_recaudado >= @anio_max_kardex set @revisar = 1 IF(@desde = @hasta) SET @saldo = isnull((select sum(saldo + valor_porcentaje) from @kardexDeu as deu where deu.ano_id = @hasta and lote_id = @lote_id), 0) else SET @saldo = isnull((select sum(saldo + valor_porcentaje) from @kardexDeu as deu where deu.ano_id between @desde and @hasta and lote_id = @lote_id), 0) if @revisar = 1 update #lotes_convenio_re set revisar = 1, saldo_kardex = @saldo, anio_min = @anio_min_kardex, anio_max = @anio_max_kardex , anio_max_recaudo = @anio_max_recaudado where id = @id else update #lotes_convenio_re set saldo_kardex = @saldo, anio_min = @anio_min_kardex, anio_max = @anio_max_kardex , anio_max_recaudo = @anio_max_recaudado where id = @id update agcm_2022_tram_liquidaciones_convenio set valor = @saldo where id = @id FETCH NEXT FROM procesar_cursor INTO @id, @lote_id, @desde, @hasta, @valor, @valorinicial END CLOSE procesar_cursor DEALLOCATE procesar_cursor; select sum(valor) from #lotes_convenio_re where estado ='ACTIVO' select * from agcm_2022_tram_liquidaciones_convenio where lote_id = 94136 select da.ClaveCatastral,conv.* from #lotes_convenio_re as conv inner join telus_procesos.dbo.agcm_datos_claves_avaluos as da on conv.lote_id = da.lote_id where valor = saldo_kardex GOTO SALIR select lc.id, lc.lote_id, lc.valor, lc.desde, lc.hasta, r.lote_id, r.saldo_kardex, r.desde, r.hasta from agcm_2022_tram_liquidaciones_convenio as lc inner join #lotes_convenio_re as r on lc.lote_id = r.lote_id where lc.id_convenio = 2296 and lc.valor <> r.saldo_kardex select sum(lc.valor), sum(r.saldo_kardex) from agcm_2022_tram_liquidaciones_convenio as lc inner join #lotes_convenio_re as r on lc.lote_id = r.lote_id /*2260 id convenio con valores similares*/ where lc.id_convenio = 2296 and lc.valor = r.saldo_kardex SALIR: