Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
2| Name | Data Type | Length | Description |
|---|---|---|---|
| @p_id_usuario | int | 4 | |
| @p_id_producto | int | 4 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: <Samuel Gonzalez>
-- Create date: <05 Dic 2017>
-- Description: <verificar si un usuario ha alcanzado su límite de consumos>
-- =============================================
CREATE OR ALTER PROCEDURE dbo.obtener_limite_de_consumo
-- Add the parameters for the stored procedure here
@p_id_usuario int,
@p_id_producto int
AS
BEGIN
SET NOCOUNT ON;
declare @cliente int, @limite_consumo_cliente int, @limite_consumo_usuario int, @bloquear_consumo int, @activo int, @cantidad_consumido int, @cli_codigo int, @consumido int
declare @fecha_desde date, @fecha_hasta date, @current_fecha date = getdate()
declare @habilitado int = 1, @condicion int = 0
declare @mensaje varchar(200) = 'Ok'
declare @aux varchar(50)
select top 1-- OBTENGO EL CLIENTE Y LIMITES DE CONSUMO DEL USUARIO
@cliente = cliente,
@limite_consumo_usuario = limite_consumo,
@bloquear_consumo = bloquear_consumo
from usuarios_productos
where (user_id = @p_id_usuario) and (id_producto = @p_id_producto) and (activo = 1)
order by modified desc, created desc
if (@cliente is not null)
begin
select top 1 -- OBTENGO LIMITES DE CONSUMO Y FECHAS DEL CLIENTE
@cli_codigo = cli_codigo,
@limite_consumo_cliente = limite_consumo,
@consumido = IIF (consumido = null, 0, consumido),
@fecha_desde = IIF (fecha_desde = null, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0), fecha_desde),
@fecha_hasta = IIF (fecha_hasta = null, EOMONTH (getdate()), fecha_hasta),
@bloquear_consumo = bloquear_consumo,
@activo = activo
from clientes_productos
where (cliente = @cliente) and (id_producto = @p_id_producto)
order by modified desc, created desc
if (@cli_codigo is not null)
begin
if ((@consumido < @limite_consumo_cliente) or (@limite_consumo_cliente = 0))
begin
if (@current_fecha < @fecha_desde) or (@current_fecha > @fecha_hasta)
begin
-- consumo fuera de la fecha permitida
set @habilitado = 0
set @condicion = 1
set @mensaje = 'El cliente está fuera del intérvalo de fecha asignado para el consumo'
end
if (@habilitado = 1)
begin
set @consumido = (select count(*) from
(
select distinct uc.cuit_consultado, convert(date, uc.created) as created, uc.user_id
from usuarios_consumos uc
where uc.user_id in
(
select up.user_id
from usuarios_productos up
where (up.cliente = @cliente) and (up.id_producto = @p_id_producto) and (up.activo = 1)
)
and (uc.created between @fecha_desde and @fecha_hasta)
) total
)
if ((@limite_consumo_cliente != 0) and (@consumido >= @limite_consumo_cliente))
begin
set @habilitado = 0
set @condicion = 2
set @mensaje = 'El total de consumos realizados por los usuarios alcanzó el límite dispuesto para el cliente'
end
end
if (@habilitado = 1)
begin
set @consumido = (select count(*) from
(
select distinct uc.cuit_consultado, convert(date, uc.created) as created, uc.user_id
from usuarios_consumos uc
where (uc.user_id = @p_id_usuario) and (uc.created between @fecha_desde and @fecha_hasta)
) total
)
if ((@limite_consumo_usuario != 0) and (@consumido >= @limite_consumo_usuario))
begin
set @habilitado = 0
set @condicion = 3
set @mensaje = 'El total de consumos realizados alcanzó el límite dispuesto para el usuario'
end
end
end
else
begin
set @habilitado = 0
set @condicion = 4
set @mensaje = 'El cliente ya superó el límite de consumos para este producto'
end
end
else
begin
set @habilitado = 0
set @condicion = 5
set @mensaje = 'El cliente no tiene asignado este producto'
end
end
else
begin
set @habilitado = 0
set @condicion = 6
set @mensaje = 'Usuario inactivo, bloqueado, sin cliente asignado, o sin acceso definido para este producto'
end
if ((@bloquear_consumo = 0) and (@condicion != 1) and (@condicion != 2) and (@condicion != 3))
begin
set @habilitado = 1
set @condicion = 0
set @mensaje = 'Ok'
end
select @habilitado as habilitado, @condicion as condicion, @mensaje as mensaje
END
GO
GO
-- =============================================
-- Author: <Samuel Gonzalez>
-- Create date: <05 Dic 2017>
-- Description: <verificar si un usuario ha alcanzado su límite de consumos>
-- =============================================
CREATE OR ALTER PROCEDURE dbo.obtener_limite_de_consumo
-- Add the parameters for the stored procedure here
@p_id_usuario int,
@p_id_producto int
AS
BEGIN
SET NOCOUNT ON;
declare @cliente int, @limite_consumo_cliente int, @limite_consumo_usuario int, @bloquear_consumo int, @activo int, @cantidad_consumido int, @cli_codigo int, @consumido int
declare @fecha_desde date, @fecha_hasta date, @current_fecha date = getdate()
declare @habilitado int = 1, @condicion int = 0
declare @mensaje varchar(200) = 'Ok'
declare @aux varchar(50)
select top 1-- OBTENGO EL CLIENTE Y LIMITES DE CONSUMO DEL USUARIO
@cliente = cliente,
@limite_consumo_usuario = limite_consumo,
@bloquear_consumo = bloquear_consumo
from usuarios_productos
where (user_id = @p_id_usuario) and (id_producto = @p_id_producto) and (activo = 1)
order by modified desc, created desc
if (@cliente is not null)
begin
select top 1 -- OBTENGO LIMITES DE CONSUMO Y FECHAS DEL CLIENTE
@cli_codigo = cli_codigo,
@limite_consumo_cliente = limite_consumo,
@consumido = IIF (consumido = null, 0, consumido),
@fecha_desde = IIF (fecha_desde = null, DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0), fecha_desde),
@fecha_hasta = IIF (fecha_hasta = null, EOMONTH (getdate()), fecha_hasta),
@bloquear_consumo = bloquear_consumo,
@activo = activo
from clientes_productos
where (cliente = @cliente) and (id_producto = @p_id_producto)
order by modified desc, created desc
if (@cli_codigo is not null)
begin
if ((@consumido < @limite_consumo_cliente) or (@limite_consumo_cliente = 0))
begin
if (@current_fecha < @fecha_desde) or (@current_fecha > @fecha_hasta)
begin
-- consumo fuera de la fecha permitida
set @habilitado = 0
set @condicion = 1
set @mensaje = 'El cliente está fuera del intérvalo de fecha asignado para el consumo'
end
if (@habilitado = 1)
begin
set @consumido = (select count(*) from
(
select distinct uc.cuit_consultado, convert(date, uc.created) as created, uc.user_id
from usuarios_consumos uc
where uc.user_id in
(
select up.user_id
from usuarios_productos up
where (up.cliente = @cliente) and (up.id_producto = @p_id_producto) and (up.activo = 1)
)
and (uc.created between @fecha_desde and @fecha_hasta)
) total
)
if ((@limite_consumo_cliente != 0) and (@consumido >= @limite_consumo_cliente))
begin
set @habilitado = 0
set @condicion = 2
set @mensaje = 'El total de consumos realizados por los usuarios alcanzó el límite dispuesto para el cliente'
end
end
if (@habilitado = 1)
begin
set @consumido = (select count(*) from
(
select distinct uc.cuit_consultado, convert(date, uc.created) as created, uc.user_id
from usuarios_consumos uc
where (uc.user_id = @p_id_usuario) and (uc.created between @fecha_desde and @fecha_hasta)
) total
)
if ((@limite_consumo_usuario != 0) and (@consumido >= @limite_consumo_usuario))
begin
set @habilitado = 0
set @condicion = 3
set @mensaje = 'El total de consumos realizados alcanzó el límite dispuesto para el usuario'
end
end
end
else
begin
set @habilitado = 0
set @condicion = 4
set @mensaje = 'El cliente ya superó el límite de consumos para este producto'
end
end
else
begin
set @habilitado = 0
set @condicion = 5
set @mensaje = 'El cliente no tiene asignado este producto'
end
end
else
begin
set @habilitado = 0
set @condicion = 6
set @mensaje = 'Usuario inactivo, bloqueado, sin cliente asignado, o sin acceso definido para este producto'
end
if ((@bloquear_consumo = 0) and (@condicion != 1) and (@condicion != 2) and (@condicion != 3))
begin
set @habilitado = 1
set @condicion = 0
set @mensaje = 'Ok'
end
select @habilitado as habilitado, @condicion as condicion, @mensaje as mensaje
END
GO
Used By
No items found