lunes, 20 de junio de 2016

Creacion del sp masivo diario

19 de junio de 8am - 12md y de 2pm a 8pm
20 de junio de 8am - 12md y de 1pm a 7pm
Se crea el procedimiento almacenado diario, tuvimos algunos problemas en la implementacion, algunos errores minimos, otros no tanto.

La duracion fue extensa ya que la complejidad del algoritmo para hacer todos los calculos era muy complejo, nos tomo horas pensar algunos métodos

Por ejemplo en las fechas nos ayudaron varias funciones como por ejemplo DATEADD(month, 1,@fecha)  sum() DATEDIFF(minute,@fecha1,@fecha2) las cuales se explicaran en el informe final mas detalladamente.

tuvimos que crear funciones tambien que nos ayudaran a calcular la cantidad de minutos que duraba una llamada por ejemplo, tambien el costo, si era familiar, extrangera, especial, etc, al igual que con los mensajes y demás.



use telefonia
go
create procedure sp_diario
@fecha_proceso datetime
as
begin
begin try
declare @temp table(
sec int primary key identity,
id int,
id_cuenta int,
id_factura int,
fk_tipo_movimientos int,
fecha datetime,
tipo int,
numero_telefono bigint,
costo money,
minutos float
)

declare @cantida int,@ultimo int

insert @temp (id,id_cuenta,fecha,tipo,numero_telefono)
select llamada.id,cuenta.id,llamada.fecha,llamada.fk_tipos_operaciones,llamada.numero_telefono
from llamada inner join cuenta on cuenta.telefono=llamada.numero_telefono
where day(fecha)=day(@fecha_proceso) and month(fecha)=month(@fecha_proceso) and year(fecha)=year(@fecha_proceso)

insert @temp (id,id_cuenta,fecha,tipo,numero_telefono)
select llamadadXcobrar.id,cuenta.id,llamadadXcobrar.fecha,llamadadXcobrar.fk_tipos_operaciones,llamadadXcobrar.numero_telefono
from llamadadXcobrar inner join cuenta on cuenta.telefono=llamadadXcobrar.numero_telefono
where day(fecha)=day(@fecha_proceso) and month(fecha)=month(@fecha_proceso) and year(fecha)=year(@fecha_proceso)

insert @temp (id,id_cuenta,fecha,tipo,numero_telefono)
select conexion_internet.id,cuenta.id,conexion_internet.fecha,conexion_internet.fk_tipos_operaciones,conexion_internet.numero_telefono
from conexion_internet inner join cuenta on cuenta.telefono=conexion_internet.numero_telefono
where day(fecha)=day(@fecha_proceso) and month(fecha)=month(@fecha_proceso) and year(fecha)=year(@fecha_proceso)
.
.
.
.
.
update @temp set fk_tipo_movimientos=dbo.calculo_tipo_movimiento(tipo,id,id_cuenta),costo=dbo.calculo_costo(tipo,id,id_cuenta),minutos=dbo.calculo_minutos(tipo,id,id_cuenta)

set transaction isolation level read uncommitted
begin transaction

insert cuenta(fk_tipo_contrato,dia_corte,telefono)
select ingreso.tipo_contrato,ingreso.fecha_corte,ingreso.numero_telefono
from ingreso where day(fecha)=day(@fecha_proceso) and month(fecha)=month(@fecha_proceso) and year(fecha)=year(@fecha_proceso)
set @cantida=(select count(1) from ingreso where day(fecha)=day(@fecha_proceso) and month(fecha)=month(@fecha_proceso) and year(fecha)=year(@fecha_proceso))


insert factura(fk_cuenta,mes,fecha_maxima_pago,cm_normales,cm_reducidos,cm_internet,cm_gratuita,cm_mensajes,cd_pago_factura,total,estado)

select cuenta.id,MONTH(DATEADD(month, 1,@fecha_proceso)),DATEADD(month, 1,DATEADD(day,(-1*DAY(@fecha_proceso))+cuenta.dia_corte+15,@fecha_proceso)),0,0,0,0,0,0,0,'facturando' from cuenta order by id  DESC OFFSET 0 ROWS FETCH NEXT @cantida ROWS ONLY

update @temp set id_factura=(select factura.id from factura join @temp on  id_cuenta=factura.fk_cuenta order by id  DESC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY)

    insert movimientos (fk_cuenta,fk_factura,fk_tipo_movimientos,fecha_hora,costo,minutos,postin,postby,postDate)
select id_cuenta,id_factura,fk_tipo_movimientos,fecha,costo,minutos,'localhost','localhost',GETDATE() from @temp

commit
return 1
end try
begin catch

if @@TRANCOUNT > 0
begin

rollback
end
return -1*@@error
end catch
end

No hay comentarios:

Publicar un comentario