dbo.obtener_control_consumos_MIS

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

4
Name Data Type Length Description
@id_cliente1 varchar 20
@user_id1 varchar 20
@prod int 4
@hoy date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec obtener_control_consumos 3283427, 994710161, 12, '2023-01-17'
select * from users where user_id=258502429

obtener_control_consumos 2254875, 0,47

SELECT * from clientes where cli_codigo = 5978


select clientes.cli_codigo,* from clientes_productos
inner join clientes on clientes.cliente = clientes_productos.cliente
where id_producto = 10 and modalidad_pago = 'PP' and clientes_productos.activo = 1

select * from ln_contrapartes  
inner join ln_gestiones on ln_gestiones.contraparte = ln_contrapartes.contraparte
where ln_contrapartes.cliente = 4390310 and desde between '2021-09-29' and '2024-02-27' and ln_contrapartes.servicio_contratado = 10

[obtener_control_consumos] 4390310,0,10


*/

CREATE OR ALTER PROCEDURE dbo.obtener_control_consumos_MIS
@id_cliente1 varchar(20),
@user_id1 varchar(20), 
@prod int, 
@hoy date = null 
AS

BEGIN
    SET NOCOUNT ON;
 declare @limite_cliente int = null,   
      @fecha_desde date = null,   
   @fecha_hasta date = null,   
   @tipo_paquete char(1) = null,   
   @bloquear_consumo_cliente int = null,   
   @activo bit = 0,   
   @limite_usuario int = null , 
   @consumo_usuario int = 0, 
   @consumo_cliente int = 0

 if (@hoy is null) set @hoy = getdate()
 set @id_cliente1  = RTRIM(@id_cliente1) 
 set @user_id1     = RTRIM(@user_id1)
 if (@id_cliente1  = '') begin set @id_cliente1 = null end 
 if (@user_id1  = '') begin set @user_id1 = null end 

 declare @id_cliente int   = convert(int, @id_cliente1)
 declare @user_id int      = convert(int, @user_id1)

-- 0: Chequeamos cliente / usuario 
 if (not exists (select * from clientes where cliente = @id_cliente and isnull(estado_cliente,'A') = 'A'))
 begin 
    SELECT 1, 0 as permitir, 'Cliente no activo' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
    , 0 as consumido_cliente, 0 as consumido_usuario
  return
 end 
 if (@user_id != 0)
 begin
  if (not exists (select * from users where user_id = @user_id and isnull(active_user,'Y') = 'Y'))
  begin 
     SELECT 2, 0 as permitir, 'Usuario no activo' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
     , 0 as consumido_cliente, 0 as consumido_usuario
   return
  end 

  if (not exists (select * from usuarios_productos where cliente = @id_cliente and user_id = @user_id)) 
  begin 
     SELECT 3, 0 as permitir, 'Usuario no habilitado para el cliente' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
     , 0 as consumido_cliente, 0 as consumido_usuario
   return
  end 
 end 


-- 1: Vemos si el cliente tiene el producto activo 

 if not exists (select * from clientes_productos 
     where cliente = @id_cliente
       and id_producto = @prod)
 begin 
    SELECT 4, 0 as permitir, 'El cliente no está suscripto al producto' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
    , 0 as consumido_cliente, 0 as consumido_usuario
  return
 end  
 declare @inicio_mes date = (SELECT DATEADD(DAY,1,EOMONTH(getdate(),-1)))
 select  @limite_cliente = isnull(limite_consumo,0), 
   @fecha_desde = iif(tipo_paquete='F',@inicio_mes,fecha_desde),
      --@fecha_desde = fecha_desde, 
   @fecha_hasta = fecha_hasta, 
   @tipo_paquete = tipo_paquete,
   @bloquear_consumo_cliente = bloquear_consumo, 
   @activo = isnull(activo,0)
 from clientes_productos 
  where cliente = @id_cliente
    and id_producto = @prod 

 if (isnull(@activo,0) = 0) 
 begin
  SELECT 5, 0 as permitir, 'El cliente no tiene el producto activo' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
  , 0 as consumido_cliente, 0 as consumido_usuario
  return 
 end 

 if (isnull(@fecha_desde,@hoy) > @hoy)
 begin
  SELECT 6, 0 as permitir, 
  'El cliente tendrá activo el producto a partir de ' + convert(varchar, @fecha_desde, 103) as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
  , 0 as consumido_cliente, 0 as consumido_usuario
  return 
 end

 if (isnull(@fecha_hasta,@hoy) < @hoy)
 begin
  SELECT 0 as permitir, 
  'El cliente no tiene activo el producto desde ' + convert(varchar, @fecha_hasta, 103) as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
    , 0 as consumido_cliente, 0 as consumido_usuario
  return 
 end

-- 2: Vemos si el usuario tiene el producto activo 
 if (@user_id != 0)
 begin
  if not exists (select * from usuarios_productos 
      where user_id = @user_id
        and id_producto = @prod)
  begin 
   if (@user_id1!=358502429)
   begin
      SELECT 7, 0 as permitir, 
      'El usuario no tiene el producto activo' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
         , 0 as consumido_cliente, 0 as consumido_usuario
     end
   if (@user_id1=358502429)
   begin
      SELECT 8, 1 as permitir, 
      'El usuario no tiene el producto activo' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
         , 0 as consumido_cliente, 0 as consumido_usuario
     end

   return
  end 

  select top 1 @limite_usuario = isnull(limite_consumo,0),
      @activo = isnull(activo,0)
  from usuarios_productos 
   where user_id = @user_id
     and id_producto = @prod 
     order by activo, limite_consumo 
 
  if (isnull(@activo,0) = 0) 
  begin
    if (@user_id1!=358502429)
   begin
    SELECT 9, 0 as permitir, 
    'El usuario no tiene el producto activo' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
    , 0 as consumido_cliente, 0 as consumido_usuario
     end
   if (@user_id1=358502429)
   begin
    SELECT 10, 1 as permitir, 
    'El usuario no tiene el producto activo' as mensaje, 0 as 'limite_cliente', 0 as 'limite_usuario'
    , 0 as consumido_cliente, 0 as consumido_usuario
     end

   return 
  end 
 end
 

-- 3: Vemos los consumos entre las fechas 
 -- si esta vacía, toma mañana como fecha hasta (a las 0 horas) 
 if (@fecha_hasta is null) set @fecha_hasta = @hoy 
 
 -- Le suma un dia a la fecha hasta para pasarle ese dia a las 0 horas como datetime 
 set @fecha_hasta = DATEADD(day, +1, @fecha_hasta)

 -- si esta vacía, toma el primer día del mes como fecha desde
 declare @dias int = convert (int, day(@hoy) * (-1)) 
 if (@fecha_desde is null) set @fecha_desde = DATEADD(day, @dias, @fecha_hasta) 

 declare @fecha_desde_comp datetime = @fecha_desde 
 declare @fecha_hasta_comp datetime = @fecha_hasta 

 /* Para Inquiro IV / LED se ven las contrapartes con gestion entre esas fechas */
 if (@prod = 10 or @prod = 47)
 begin
  set @consumo_cliente = (select count(*) from ln_contrapartes 
         inner join ln_gestiones on ln_gestiones.contraparte = ln_contrapartes.contraparte 
         where ln_contrapartes.cliente = @id_cliente 
           and ln_contrapartes.servicio_contratado = @prod
           and ln_gestiones.desde between @fecha_desde_comp and @fecha_hasta_comp)
    
  set @consumo_usuario = @consumo_cliente
  set @limite_usuario  = @limite_cliente
 end 
 else
 begin
  -- Vemos los consumos del usuario y cliente entre esas fechas 
  if (@user_id != 0)
  begin
   set @consumo_usuario = (select count(*) from 
    (select distinct cuit_consultado, convert(date, usuarios_consumos.created) fecha 
    from usuarios_consumos 
    where id_producto = @prod
    and usuarios_consumos.user_id = @user_id
    and usuarios_consumos.created between @fecha_desde_comp and @fecha_hasta_comp) as sbq)
  end

  set @consumo_cliente = (select count(*) from 
    (select distinct cuit_consultado, convert(date, usuarios_consumos.created) fecha 
      from usuarios_consumos 
      where id_producto = @prod
     and usuarios_consumos.user_id in(select distinct user_id from usuarios_productos where cliente = @id_cliente) 
     and usuarios_consumos.created between @fecha_desde_comp and @fecha_hasta_comp) as sbq)
  if (@user_id = 0)
  begin
   set @limite_usuario  = @limite_cliente
   set @consumo_cliente = @consumo_cliente
  end 
 end 
 if (@limite_usuario != 0 and @consumo_usuario >= @limite_usuario and @bloquear_consumo_cliente = 1) 
 begin
  SELECT 0 as permitir, 
  'El usuario ya ha consumido el límite permitido' as mensaje, @limite_cliente as 'limite_cliente', @limite_usuario as 'limite_usuario',
  @consumo_cliente as consumido_cliente, @consumo_usuario as consumido_usuario
  return 
 end 

 -- Si el usuario no llego a su limite vemos las condiciones del cliente 
 if (@bloquear_consumo_cliente = 0) --> No se bloquea puede seguir consumiendo 
 begin
  select 1 as permitir, 
  'Cliente autorizado' as mensaje, @limite_cliente as 'limite_cliente', @limite_usuario as 'limite_usuario',
  @consumo_cliente as consumido_cliente, @consumo_usuario as consumido_usuario, 
 @fecha_desde_comp desde, 
 @fecha_hasta_comp hasta
  return  
 end 

 if (@limite_cliente != 0 and @consumo_cliente >= @limite_cliente and @bloquear_consumo_cliente = 1) 
 begin
  SELECT 0 as permitir, 
  'El cliente ya ha consumido el límite permitido' as mensaje, @limite_cliente as 'limite_cliente', @limite_usuario as 'limite_usuario',
  @consumo_cliente as consumido_cliente, @consumo_usuario as consumido_usuario
  return 
 end 

 select 1 as permitir, 
 'Usuario autorizado' as mensaje, @limite_cliente as 'limite_cliente', @limite_usuario as 'limite_usuario',
 @consumo_cliente as consumido_cliente, @consumo_usuario as consumido_usuario, 
 @fecha_desde_comp desde, 
 @fecha_hasta_comp hasta

 
END

GO

Used By

No items found