440 lines
18 KiB
Transact-SQL
440 lines
18 KiB
Transact-SQL
USE [db_olympo_web]
|
||
|
||
BEGIN
|
||
declare @par_id_convenio int = 0;
|
||
-- SET NOCOUNT ON added to prevent extra result sets from
|
||
-- interfering with SELECT statements.
|
||
SET NOCOUNT ON;
|
||
-- ANTES DE PROCESA EJECUTAR EL PROCEDIMIENTO EXEC agcm_2023_tram_actualiza_valores_claves_convenio
|
||
|
||
SET @par_id_convenio = (select id from db_olympo_web.dbo.convenios_cabecera where code in('002031'));
|
||
|
||
/*
|
||
select * from telus.dbo.agcm_2022_tram_liquidaciones_convenio where id_convenio in(@par_id_convenio)
|
||
GOTO SALIR
|
||
*/
|
||
|
||
declare @convenio_id int = 0--1008 --739 --1109--739
|
||
declare @fimain int =1, @tomain int =0
|
||
if OBJECT_ID('tempdb.dbo.#conmain') is not null
|
||
drop table #conmain
|
||
select ROW_NUMBER() over(order by a.id_convenio) as fila,a.id_convenio,count(*) Total
|
||
into #conmain
|
||
from telus.dbo.agcm_2022_tram_liquidaciones_convenio a
|
||
where (a.estado='ACTIVO' )
|
||
and a.id_convenio in(@par_id_convenio)
|
||
group by a.id_convenio
|
||
order by a.id_convenio
|
||
print @@ROWCOUNT
|
||
create index IX_LO_FI_con on #conmain(fila,id_convenio)
|
||
|
||
/*
|
||
select * from #conmain
|
||
GOTO SALIR
|
||
*/
|
||
|
||
set @tomain = (select count(*) from #conmain)
|
||
|
||
print CONVERT(varchar(20), GETDATE(), 131)
|
||
--
|
||
declare @estadopagos table(idpago int, mensaje varchar(max),lote_id int)
|
||
declare @cantidadxconvenio int =0
|
||
declare @cantidadxconvenioID varchar(max)='NO PROCESADO'
|
||
--
|
||
while @fimain <=@tomain
|
||
begin
|
||
set @convenio_id = (select a.id_convenio from #conmain a where a.fila=@fimain)
|
||
print '@convenio_id'
|
||
print @convenio_id
|
||
print CONVERT(varchar(20), GETDATE(), 131)
|
||
print '===================='
|
||
if OBJECT_ID('tempdb.dbo.#con') is not null
|
||
drop table #con
|
||
SELECT ROW_NUMBER() over(order by a.id) as fila,a.id as idpagCab, a.date,x.* into #con FROM convenios_pagos_cabecera a,convenios_pagos_detalle x
|
||
WHERE a.id = x.vouchers_id and x.agreement_id IN (
|
||
SELECT y.id FROM convenios_cabecera y where y.id=@convenio_id
|
||
) and x.tax_id=3 -- Impuesto Predial
|
||
and a.state='approved'
|
||
|
||
and x.id not in(
|
||
select xxy.idpago from agcm_2023_recaudacion_cuotas_detalle xxy
|
||
)
|
||
and a.id not in(
|
||
select xxyz.idpago from agcm_2023_recaudacion_cuotas xxyz
|
||
)
|
||
and a.id not in(select id_pago_olympo from telus.dbo.agcm_2023_tram_id_pagos_olympo_omitir) -- Se agreg<65>
|
||
order by a.id
|
||
|
||
/*
|
||
select * from #con
|
||
GOTO SALIR
|
||
*/
|
||
|
||
|
||
delete x from #con x, agcm_2023_recaudacion_cuotas y
|
||
where y.idpago=x.idpagCab and x.to_paid=y.valor_pagar_unitario
|
||
DECLARE @contador INT = 0;
|
||
UPDATE #con SET fila = @contador, @contador = @contador + 1;
|
||
|
||
|
||
|
||
/*
|
||
select * from #con
|
||
GOTO SALIR
|
||
*/
|
||
|
||
|
||
|
||
/*
|
||
SELECT ROW_NUMBER() over(order by a.id) as fila, a.* into #con FROM convenios_pagos_cabecera a
|
||
WHERE a.id IN (
|
||
SELECT x.vouchers_id FROM convenios_pagos_detalle x
|
||
WHERE x.agreement_id IN (
|
||
SELECT y.id FROM convenios_cabecera y where y.id=@convenio_id
|
||
) and x.tax_id=3 -- Impuesto Predial
|
||
) and a.state='approved'
|
||
and a.id not in(
|
||
select xx.idpago from db_olympo_web.dbo.agcm_2023_recaudacion_cuotas xx
|
||
)
|
||
and a.id not in(select id_pago_olympo from telus.dbo.agcm_2023_tram_id_pagos_olympo_omitir) -- Se agreg<65>
|
||
order by a.id
|
||
*/
|
||
|
||
print @@ROWCOUNT
|
||
-- select * from #con order by create_date
|
||
declare @fila int =1,@total int=0
|
||
declare @fecha date, @id int, @idliqui int
|
||
--
|
||
declare @valorpagado numeric(18,2)=0
|
||
declare @tbredis table(fila int identity(1,1),idpago int,fecha_pago date,lote_id int,valorconveniototal numeric(18,3),
|
||
valorconvenio numeric(18,3),factor numeric(18,8),valor_pagar_total numeric(18,2),valor_pagar_unitario numeric(18,2),
|
||
id_tranliqui int,id_convenio_olympo int,recaudacion_id_mg varchar(max),aniodesde int,aniohasta int)
|
||
declare @idfila int=0,@toidfila int =0
|
||
declare @resrecau int
|
||
declare @idusuariorec int
|
||
set @idusuariorec = (select top 1 a.usuario_id from telus.dbo.agcm_2023_usuarios_ocultos_recaudacion a)
|
||
--
|
||
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))
|
||
declare @deudafinal numeric(18,2)
|
||
declare @aniodesde int, @aniohasta int
|
||
declare @deudafinaldesdehasta numeric(18,2)
|
||
--
|
||
|
||
declare @msgproc varchar(100) = 'PROCESADO'
|
||
--
|
||
set @total = (select count(*) from #con)
|
||
while @fila<=@total
|
||
begin
|
||
BEGIN TRANSACTION;
|
||
Begin Try
|
||
declare
|
||
@par_valor_pagado numeric(11,2)=0,
|
||
@par_lote_id int=0,
|
||
@card varchar(max)=''
|
||
,@NumeroTransaccion varchar(max)=''
|
||
,@id_tranliqui int =0
|
||
select @fecha = a.date, @id=a.id from #con a where a.fila=@fila
|
||
select @valorpagado = a.to_paid, @idliqui = a.collection_id from convenios_pagos_detalle a where a.id = @id and a.tax_id=3
|
||
print '@valorpagado'
|
||
print @valorpagado
|
||
print '@idliqui'
|
||
print @idliqui
|
||
--delete from @tbredis
|
||
-- Insertar para recorrer Tabla de Lotes
|
||
insert into @tbredis
|
||
select @id,@fecha,a.lote_id,0,a.valor,0,@valorpagado,0,a.id,a.id_convenio,0,a.desde,a.hasta
|
||
from telus.dbo.agcm_2022_tram_liquidaciones_convenio a where a.id_convenio=@convenio_id and
|
||
a.id_liquidacion=@idliqui and (a.estado='ACTIVO' ) --or a.estado='PROCESARPAGO'
|
||
-- Actualizar valor de deuda seg<65>n la clave que tenga prioridad
|
||
/*
|
||
select * from @tbredis
|
||
Rollback;
|
||
goto SALIR
|
||
*/
|
||
print @@ROWCOUNT
|
||
set @idfila = (select count(*) from @tbredis a where a.idpago=@id)
|
||
if @idfila = 0
|
||
begin
|
||
print '========INACTIVO lINea 141==================='
|
||
set @msgproc = 'NO PROCESADO, CONVENIO INACTIVO: ' + cast(@convenio_id as varchar)+' Liquidacion: '+ cast(@idliqui as varchar)
|
||
goto CONTINUAR
|
||
end
|
||
set @idfila = (select min(a.fila) from @tbredis a where a.idpago=@id)
|
||
set @toidfila = (select max(a.fila) from @tbredis a where a.idpago=@id)
|
||
declare @fifima int = @idfila
|
||
declare @buslo int = 0
|
||
/*
|
||
update @tbredis set valorconvenio=163.14 where fila=1
|
||
update @tbredis set valorconvenio=200 where fila=2
|
||
update @tbredis set valor_pagar_total=400
|
||
*/
|
||
while @idfila<=@toidfila
|
||
begin
|
||
if @idfila=@fifima
|
||
begin
|
||
update @tbredis set valorconveniototal = (select sum(valorconvenio) from @tbredis where idpago=@id)
|
||
where idpago=@id
|
||
-- Prioritario
|
||
set @buslo = isnull((select top 1 a.lote_id from telus.dbo.agcm_2023_tram_lotes_prioridad a,@tbredis b
|
||
where a.lote_id=b.lote_id and a.id_convenio=b.id_convenio_olympo and a.estado='ACTIVO' order by a.lote_id),0)
|
||
print '@buslo'
|
||
print @buslo
|
||
print @par_lote_id
|
||
declare @filasafectadas int =0
|
||
update x set factor = x.valorconvenio / x. valorconveniototal
|
||
from @tbredis x where x.idpago=@id
|
||
--select * from @tbredis
|
||
end
|
||
if @buslo>0 and @idfila=@fifima
|
||
begin
|
||
declare @fatoresuno int =0
|
||
update x set x.factor = 1
|
||
from @tbredis x where x.idpago=@id and x.valor_pagar_total<=x.valorconvenio and x.lote_id in(
|
||
select a.lote_id from telus.dbo.agcm_2023_tram_lotes_prioridad a,@tbredis b
|
||
where a.lote_id=b.lote_id and a.id_convenio=b.id_convenio_olympo and a.estado='ACTIVO'
|
||
) and x.factor<>1
|
||
set @filasafectadas = @@ROWCOUNT
|
||
-- select * from @tbredis
|
||
print 'Filas afectadas'
|
||
print @filasafectadas
|
||
declare @tbcalcu table(lote_id int,id_convenio_olympo int,idpago int,fx numeric(18,8),fxd numeric(18,8))
|
||
if @filasafectadas=0
|
||
begin
|
||
declare @fx numeric(18,8)= 0
|
||
declare @fxdi numeric(18,8)= 0
|
||
declare @fxsum numeric(18,8)= 0
|
||
-- select * from @tbredis
|
||
insert into @tbcalcu (lote_id,id_convenio_olympo,idpago,fx)
|
||
select x.lote_id,x.id_convenio_olympo,x.idpago,( x.valorconvenio/x.valor_pagar_total) as fx
|
||
from @tbredis x,telus.dbo.agcm_2023_tram_lotes_prioridad y where x.idpago=@id and x.valor_pagar_total>x.valorconvenio and x.lote_id=y.lote_id and y.estado='ACTIVO'
|
||
and y.id_convenio=x.id_convenio_olympo and x.valor_pagar_total>0 order by y.lote_id
|
||
update @tbcalcu set fxd = 1 - (select sum(fx) from @tbcalcu where idpago=@id) where idpago=@id
|
||
set @fxsum = isnull((select sum(x.factor)
|
||
from @tbredis x where x.idpago=@id and x.lote_id not in (select lote_id from @tbcalcu)),0)
|
||
print '@fxsum'
|
||
print @fxsum
|
||
set @fxdi = (select max(fxd) from @tbcalcu where idpago=@id)
|
||
--select * from @tbcalcu
|
||
/*
|
||
set @fx = isnull((select ((100 * x.valorconvenio)/x.valor_pagar_total)/100
|
||
from @tbredis x,telus.dbo.agcm_2023_tram_lotes_prioridad y where x.idpago=@id and x.valor_pagar_total>x.valorconvenio and x.lote_id=y.lote_id and y.estado='ACTIVO' and y.id_convenio=x.id_convenio_olympo and x.valor_pagar_total>0),0)
|
||
*/
|
||
/*
|
||
print 'nsi'
|
||
set @fxdi = 1 - @fx
|
||
*/
|
||
update x set x.factor = y.fx from @tbredis x,@tbcalcu y where x.lote_id=y.lote_id and x.id_convenio_olympo= y.id_convenio_olympo and x.idpago=y.idpago and y.idpago=@id
|
||
update x set x.factor = (x.factor * @fxdi)/@fxsum from @tbredis x where x.lote_id not in(select lote_id from @tbcalcu) and x.idpago=@id
|
||
--select * from @tbcalcu
|
||
--select * from @tbredis
|
||
--GOTO SALIR
|
||
delete from @tbcalcu
|
||
end
|
||
else
|
||
begin
|
||
update x set x.factor=0 from @tbredis x where x.idpago=@id and x.factor<>1
|
||
set @filasafectadas = (select count(*) from @tbredis a where a.factor=1)
|
||
if @filasafectadas >1
|
||
begin
|
||
print 'Nuevo proceso'
|
||
update x set x.factor=0 from @tbredis x where x.idpago=@id and x.factor=1 and x.fila not in(
|
||
select min(y.fila) from @tbredis y where y.idpago=@id and y.factor=1
|
||
)
|
||
end
|
||
end
|
||
|
||
end -- FIn IF
|
||
update x set x.valor_pagar_unitario = x.factor * x.valor_pagar_total
|
||
from @tbredis x where x.idpago=@id
|
||
-- ===================================================
|
||
-- Procesar Pago
|
||
select @par_valor_pagado=a.valor_pagar_unitario,@par_lote_id=a.lote_id,
|
||
@card ='Recaudaci<EFBFBD>n Facilidad de Pago. Control #'+cast(a.id_convenio_olympo as varchar(30)) ,
|
||
@NumeroTransaccion = cast(a.idpago as varchar(30)),
|
||
@aniodesde=a.aniodesde,@aniohasta=a.aniohasta,@id_tranliqui=a.id_tranliqui
|
||
from @tbredis a
|
||
where a.fila=@idfila
|
||
-- Verificaci<63>n si existe Pago
|
||
declare @noproxy int = isnull((select x.fila from agcm_2023_recaudacion_cuotas_detalle x where x.lote_id=@par_lote_id and x.idpago=@id and x.fecha_pago=@fecha and x.recaudacion_id_mg<>'0'),0)
|
||
print '@noproxy'
|
||
print @noproxy
|
||
set @msgproc = 'PROCESADO'
|
||
if @noproxy>0
|
||
begin
|
||
print '========PAGO YA FUE PROCESADO==================='
|
||
set @msgproc = 'NO PROCESADO, PAGO EFECTUADO CON ANTERIORIDAD'
|
||
goto CONTINUARLOTE
|
||
end
|
||
if @par_valor_pagado=0
|
||
begin
|
||
print '========PAGO En CERO==================='
|
||
set @msgproc = 'NO PROCESADO, PAGO EN CERO'
|
||
goto CONTINUARLOTE
|
||
end
|
||
|
||
/*
|
||
select * from @tbredis
|
||
print 'VER VALORCITO'
|
||
print @par_valor_pagado
|
||
print @par_lote_id
|
||
print '===================FECHA=============='
|
||
print @fecha
|
||
select @msgproc
|
||
Rollback;
|
||
goto SALIR
|
||
*/
|
||
|
||
-- Ejecutar Pago
|
||
print '========PROCESANDO PAGO==================='
|
||
exec telus.dbo.agcm_2023_telus_recaudacion_registro_pago_convenios @idusuariorec,4,@par_valor_pagado,@par_valor_pagado,0,@par_lote_id,-2,null,null,null,@card ,@id,@fecha
|
||
print '========FIN PROCESANDO PAGO==================='
|
||
/*
|
||
select @idusuariorec,4,@par_valor_pagado,@par_valor_pagado,0,@par_lote_id,-2,null,null,null,@card ,@id,@fecha
|
||
Rollback;
|
||
--set @fila = 100
|
||
goto SALIR
|
||
*/
|
||
--
|
||
set @resrecau = isnull((select a.recaudacion_id from telus.dbo.agcm_2023_recaudacion_id_convenio a where a.pago_id_ol=@id and a.lote_id=@par_lote_id),0)
|
||
print 'XXXXXXXXXXXXXXXXXXXXXXXX'
|
||
print @resrecau
|
||
if @resrecau = 0
|
||
begin
|
||
print '========EL PAGO no FUE PROCESADO==================='
|
||
set @msgproc = 'NO PROCESADO, ERROR EN EL PAGO'
|
||
goto CONTINUARLOTE
|
||
end
|
||
set @msgproc = 'PROCESADO'
|
||
-- ==============================================
|
||
update @tbredis set recaudacion_id_mg=@resrecau where fila=@idfila
|
||
-- Final
|
||
declare @fefe date = getdate()
|
||
insert into @kardexDeu
|
||
exec telus.dbo.agcm_telus_avaluo_emision_consulta @par_lote_id,@fefe
|
||
set @deudafinal = isnull((select sum(a.saldo+a.valor_porcentaje) from @kardexDeu a where a.ano_id between @aniodesde and @aniohasta),0)
|
||
if @deudafinal=0
|
||
BEGIN
|
||
update x Set x.estado='INACTIVO',x.fecha_actualiza=getdate()
|
||
from telus.dbo.agcm_2022_tram_liquidaciones_convenio x
|
||
where x.id=@id_tranliqui
|
||
Update x set x.esta_convenio=0
|
||
from telus.dbo.avaluo_emision_anual_cabecera x
|
||
where x.esta_convenio=1 and x.ano_id between @aniodesde and @aniohasta
|
||
and x.lote_id=@par_lote_id
|
||
-- exec telus_avaluo_emision_anual_cabecera_actualizar_saldo
|
||
update x set x.estado_registro='INACTIVO'
|
||
from telus.dbo.rentas_estado x
|
||
where x.tipo_renta=0 and x.registro_lote_id=@par_lote_id and x.estado_registro='ACTIVO'
|
||
insert into telus.dbo.rentas_estado(registro_lote_id,tipo_renta,estado,usuario_id,ip,pcname,detalle,estado_registro,fecha)
|
||
select @par_lote_id,0,0,18,'127.0.0.1','localhost','Facilidad de Pago satisfactoria. '+@card,'ACTIVO',GETDATE()
|
||
if @buslo>0
|
||
begin
|
||
update telus.dbo.agcm_2023_tram_lotes_prioridad set estado='INACTIVO' where lote_id=@par_lote_id and estado='ACTIVO'
|
||
end
|
||
end
|
||
else
|
||
begin
|
||
update x Set x.valor=@deudafinal,x.fecha_actualiza=getdate()
|
||
from telus.dbo.agcm_2022_tram_liquidaciones_convenio x
|
||
where x.id=@id_tranliqui and x.lote_id=@par_lote_id
|
||
end
|
||
delete from @kardexDeu
|
||
set @cantidadxconvenioID = 'PROCESADO'
|
||
|
||
CONTINUARLOTE:
|
||
insert into @estadopagos
|
||
select @id,@msgproc+': lote_id=' + cast(@par_lote_id as varchar) + ' => ' + @card,@par_lote_id
|
||
--set @msgproc = 'FIN RECORRIDO DE LOTES'
|
||
print cast(@idfila as varchar) + ' de ' + cast(@toidfila as varchar) + ' lotes, lote_id = ' +cast(@par_lote_id as varchar)+ ' @id_tranliqui:'+cast(@id_tranliqui as varchar)
|
||
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||
/*
|
||
if @par_lote_id = 95989
|
||
begin
|
||
select * from @tbredis
|
||
select * from @estadopagos
|
||
print 'VER VALORCITO'
|
||
print @par_valor_pagado
|
||
print @par_lote_id
|
||
select @msgproc
|
||
Rollback;
|
||
goto SALIR
|
||
end
|
||
*/
|
||
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||
set @idfila = @idfila + 1
|
||
set @par_lote_id = 0
|
||
set @card =''
|
||
end --Fin While Recorre Claves
|
||
-- ===================================================
|
||
set @cantidadxconvenio = @cantidadxconvenio + 1
|
||
CONTINUAR:
|
||
print @fecha
|
||
print @valorpagado
|
||
print @idliqui
|
||
print 'FILA: ' + cast(@fila as varchar) + ' ID PAgo: '+cast(@id as varchar )
|
||
PRINT '====================================================='
|
||
PRINT '====================================================='
|
||
/*
|
||
insert into @estadopagos
|
||
select @id,@msgproc+': lote_id=' + cast(@par_lote_id as varchar) + ' => ' + @card,@par_lote_id
|
||
*/
|
||
COMMIT;-- TRAN Pago
|
||
End try
|
||
Begin Catch
|
||
Rollback;-- TRAN Pago
|
||
insert into @estadopagos
|
||
select @id,'NO PROCESADO: ' + ERROR_MESSAGE() + ' -> ' +ERROR_PROCEDURE() +' ->' +cast(ERROR_LINE() as varchar),0
|
||
End Catch
|
||
set @fila = @fila + 1
|
||
/*
|
||
IF @id=4681591
|
||
set @fila = 100
|
||
*/
|
||
end -- End While
|
||
--select * from @estadopagos
|
||
if OBJECT_ID('tempdb.dbo.#inser') is not null
|
||
drop table #inser
|
||
select *
|
||
into #inser
|
||
from @tbredis
|
||
--select * from #inser
|
||
|
||
delete x from #inser x,agcm_2023_recaudacion_cuotas_detalle y
|
||
where x.lote_id=y.lote_id and x.fecha_pago=y.fecha_pago and x.idpago=y.idpago
|
||
delete x from #inser x,@estadopagos y
|
||
where x.idpago=y.idpago and y.mensaje like 'NO PROCESADO%' and x.lote_id=y.lote_id
|
||
insert into agcm_2023_recaudacion_cuotas_detalle(idpago,fecha_pago,lote_id,valorconveniototal,valorconvenio,factor,valor_pagar_total,
|
||
valor_pagar_unitario,id_tranliqui,id_convenio_olympo,recaudacion_id_mg,aniodesde,aniohasta)
|
||
select idpago,fecha_pago,lote_id,valorconveniototal,valorconvenio,factor,valor_pagar_total,
|
||
valor_pagar_unitario,id_tranliqui,id_convenio_olympo,recaudacion_id_mg,aniodesde,aniohasta from #inser a
|
||
/*
|
||
drop table #con
|
||
drop table #inser
|
||
*/
|
||
print '****************************************************************************************'
|
||
print cast(@fimain as varchar) + ' de ' +cast(@tomain as varchar)
|
||
set @fimain = @fimain + 1
|
||
insert into tmp_proceso_convenios(convenio_id,cantidadpago,estado)
|
||
select @convenio_id,@cantidadxconvenio,@cantidadxconvenioID
|
||
set @cantidadxconvenioID = ''
|
||
set @cantidadxconvenio = 0
|
||
end-- Fin While General
|
||
print CONVERT(varchar(20), GETDATE(), 131)
|
||
insert into agcm_2023_log_estado_pagos(idpago,mensaje)
|
||
select idpago,mensaje from @estadopagos
|
||
/*
|
||
select * from @estadopagos
|
||
select * from agcm_2023_recaudacion_cuotas
|
||
*/
|
||
|
||
|
||
drop table #con
|
||
drop table #inser
|
||
drop table #conmain
|
||
|
||
SALIR:
|
||
print '----------------------------------------------------------------------'
|
||
print 'FINALIZADO'
|
||
print '----------------------------------------------------------------------'
|
||
END
|