dbo.consultas_para_felix

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

/*
select * from monitoreos where cliente = 74310378 and fecha_vigencia_mantenimiento >= getdate()
select * from clientes_productos where cliente = 950905458
select * from clientes where cli_codigo = 4785
select * from tipos_paquetes

select * from users where username = 'fbordese@arcor.com'
obtener_Registro_Consumos 806641810,0,'2020-07-01','2021-06-30',12,'C'
 
*/

CREATE OR ALTER PROCEDURE dbo.consultas_para_felix
AS
BEGIN
    SET NOCOUNT ON; 
 
 delete from  consultas_0720_0621

 declare @consumos table (nombre_persona varchar(200), cuit_consultado varchar(20), fecha varchar(10), fecha_dmy varchar(10),
                          nombre_producto varchar(100), username varchar(100))
 declare @id_cliente int, @cli_codigo int, @nombre_cliente varchar(200), @cuit varchar(20)

 DECLARE C_clientes CURSOR FOR
   select cliente, cli_codigo, nombre_cliente, cuit
    from clientes
    where cli_codigo in (4568,
       4493,
       4624,
       2582,
       4415,
       4104,
       4563,
       1791,
       4511,
       4417,
       4095,
       4219,
       3792,
       4451,
       654,
       4475,
       2041,
       4503,
       4568,
       5397,
       20001,
       2023,
       4411,
       5084,
       5598,
       4943,
       4785,
       1271,
       1986,
       5753,
       4854,
       1534,
       118,
       2768,
       2048,
       4015,
       2802,
       1574,
       3865,
       5327,
       2738,
       3915,
       4189,
       3952,
       1311,
       3884,
       4100,
       1226,
       1842,
       1418,
       3964,
       2017,
       2754,
       1511,
       4384,
       3886,
       2745,
       4867,
       5661,
       4600,
       3876,
       3959,
       2362,
       5374,
       4564,
       1575,
       4630,
       5637,
       4399,
       5599,
       378,
       4238,
       4386
       )
 OPEN C_clientes
 FETCH NEXT FROM C_clientes INTO @id_cliente, @cli_codigo , @nombre_cliente, @cuit
 
 WHILE @@fetch_status = 0
 BEGIN
  delete from @consumos 
  insert into @consumos 
  exec obtener_Registro_Consumos @id_cliente, 0,'2020-07-01','2021-06-30',12,'C'
  if (select count(*) from @consumos) = 0
  begin
  insert into consultas_0720_0621(cliente, cli_codigo, nombre_cliente, cuit, fecha, cuit_consultado) 
  values (@id_cliente, @cli_codigo, @nombre_cliente, @cuit, '2020-07', null) 
  end 
  else 
  begin
  insert into consultas_0720_0621(cliente, cli_codigo, nombre_cliente, cuit, fecha, cuit_consultado) 
   select @id_cliente, @cli_codigo, @nombre_cliente, @cuit, convert(char(7),fecha), cuit_consultado
   from @consumos
  end 
  FETCH NEXT FROM C_clientes INTO @id_cliente, @cli_codigo , @nombre_cliente, @cuit
 
 END 
 CLOSE C_clientes
 DEALLOCATE C_clientes

 
 DECLARE C_cursor CURSOR FOR
   select cliente, nombre_cliente, cuit, cli_codigo, fecha, count(*) 
    from consultas_0720_0621
    where cuit_consultado is not null 
    group by cliente, nombre_cliente, cuit, cli_codigo, fecha
   union 
   select cliente, nombre_cliente, cuit, cli_codigo, fecha, 0
    from consultas_0720_0621
    where cuit_consultado is null 
   order by cliente, fecha
 declare @nombre varchar(200), @fecha varchar(7), @cantidad int, @monitoreos int
 declare @resultado table (cliente varchar(200), cuit varchar(20), cli_codigo varchar(10), id_cliente int, 
 mes1 int,
 mes2 int,
 mes3 int,
 mes4 int,
 mes5 int,
 mes6 int,
 mes7 int,
 mes8 int,
 mes9 int,
 mes10 int,
 mes11 int,
 mes12 int, 
 monitoreos int)
 OPEN C_cursor
 FETCH NEXT FROM C_cursor INTO @id_cliente, @nombre, @cuit, @cli_codigo, @fecha, @cantidad
 WHILE @@fetch_status = 0
 BEGIN
  if not exists (select * from @resultado where cli_codigo = @cli_codigo)
  begin
   set @monitoreos = 0 
   select @monitoreos = COUNT(distinct a.cuit) 
   from monitoreos a
   where cliente = @id_cliente 
    and (a.fecha_vigencia_mantenimiento is null or a.fecha_vigencia_mantenimiento >= getdate())
     and isnull(a.cuit,'') != ''
           and a.visible = 1
     and exists (select * from clientes_productos where cliente = a.cliente and id_producto = 16) 

   insert into @resultado(cliente, cuit, cli_codigo, monitoreos, id_cliente) values (@nombre, @cuit, @cli_codigo,@monitoreos,@id_cliente) 
  end 
  if (@fecha = '2020-07') update @resultado set mes1  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2020-08') update @resultado set mes2  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2020-09') update @resultado set mes3  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2020-10') update @resultado set mes4  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2020-11') update @resultado set mes5  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2020-12') update @resultado set mes6  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2021-01') update @resultado set mes7  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2021-02') update @resultado set mes8  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2021-03') update @resultado set mes9  = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2021-04') update @resultado set mes10 = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2021-05') update @resultado set mes11 = @cantidad where id_cliente = @id_cliente
  if (@fecha = '2021-06') update @resultado set mes12 = @cantidad where id_cliente = @id_cliente

  FETCH NEXT FROM C_cursor INTO @id_cliente, @nombre, @cuit, @cli_codigo, @fecha, @cantidad
 
 END 

 CLOSE C_cursor
 DEALLOCATE C_cursor

 select res.cli_codigo, res.id_cliente, res.cliente, res.cuit,
 isnull(mes1,0) as '2020-07',
 isnull(mes2,0) as '2020-08',
 isnull(mes3,0) as '2020-09',
 isnull(mes4,0) as '2020-10',
 isnull(mes5,0) as '2020-11',
 isnull(mes6,0) as '2020-12',
 isnull(mes7,0) as '2021-01',
 isnull(mes8,0) as '2021-02',
 isnull(mes9,0) as '2021-03',
 isnull(mes10,0) as '2021-04',
 isnull(mes11,0) as '2021-05',
 isnull(mes12,0) as '2021-06', 
 monitoreos as 'Total Monitoreos', 
 nombre_paquete as 'Tipo Paquete', 
 cp12.limite_consumo as Pack_Consultas, cp12.fecha_desde as Desde_consultas, cp12.fecha_hasta as Hasta_Consultas, cp12.bloquear_consumo as 'Bloquea Consultas',
 cp16.limite_consumo as Pack_Monitoreos, cp12.fecha_desde as Desde_Monitoreos, cp12.fecha_hasta as Hasta_Monitoreos, cp16.bloquear_consumo as 'Bloquea Monitoreos'

 from @resultado res
 left join clientes_productos cp12 on (cp12.cliente = res.id_cliente and cp12.id_producto = 12) 
 left join clientes_productos cp16 on (cp16.cliente = res.id_cliente and cp16.id_producto = 16) 
 left join tipo_paquetes_productos tp on (tp.id_paquete = cp12.tipo_paquete and tp.producto = 12) 


END 
GO

Used By

No items found