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
/*
EXEC Imperium_Inicio_Periodo
*/
CREATE OR ALTER PROCEDURE dbo.Imperium_Inicio_Periodo
/*
@periodo int,
@IDCliente int,
@led_seccion varchar(1)
*/
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ERRORNUMBER SMALLINT
/*
BEGIN
EXEC Carga_Led_Inicio_Periodo
END
*/
DECLARE @StartOfMonth DATE
DECLARE @ActualDate DATE
DECLARE @periodoV INT = NULL
DECLARE @periodoVAnt INT = NULL
DECLARE @periodoA INT = NULL
DECLARE @IDCliente int = NULL
DECLARE @Modalidad varchar(1)
DECLARE @NombreCliente varchar(200)
DECLARE @periodoYear INT = NULL
DECLARE @periodoMonth INT = NULL
SET @periodoYear = convert(varchar(4),datepart(year,getdate()))
SET @periodoMonth = convert(varchar(2),datepart(month,getdate()))
SET @periodoYear = datepart(year,getdate())
SET @periodoMonth = datepart(month,getdate())
SET @periodoA = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())),2)
SeT @StartOfMonth = convert(date,DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
set @ActualDate = getdate()
/*
select @StartOfMonth,@ActualDate,@periodoV,@periodoA,@periodoVAnt,@periodoYear as periodoYear,@periodoMonth as periodoMonth
*/
/*
set @periodoMonth = 1
*/
if (@periodoMonth = 1)
begin
set @periodoV = convert(varchar(4),@periodoYear-1) + RIGHT('00' +convert(varchar(2),12),2)
set @periodoVAnt = convert(varchar(4),@periodoYear-1) + RIGHT('00' +convert(varchar(2),11),2)
end
if (@periodoMonth > 1)
begin
set @periodoV = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())-1),2)
set @periodoVAnt = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())-2),2)
end
set @periodoVAnt = dbo.PeriodoMenosDos()
set @periodoV = dbo.PeriodoVencido()
set @periodoA = dbo.PeriodoAdelantado()
select @StartOfMonth as startofmonth,@ActualDate as actualdate,@periodoV as periodoV,@periodoA as periodoA,@periodoVAnt as periodoVANT,@periodoYear as periodoYear,@periodoMonth as periodoMonth
if @ActualDate = @StartOfMonth
begin
delete led_docs where periodo = @periodoA and estado_doc NOT IN ('N','I','R','V')
select @periodoV as periodoV,@periodoA as periodoA
/*Averiguamos la lista de clientes a procesar */
DECLARE LedClientesActivos Cursor
FOR
SELECT Led_contratos.cliente as IDCliente,IIF(clientes.acceso_ci = 1,'A','V') as modalidad,
clientes.nombre_cliente
FROM clientes INNER JOIN
led_contratos ON clientes.cliente = led_contratos.cliente
WHERE clientes.estado_cliente = 'A' and
(clientes.acceso_led = 1 or clientes.acceso_ci = 1)
and getdate() between led_contratos.vigencia_desde
and led_contratos.vigencia_hasta
order by modalidad asc
/*
and led_contratos.cliente in (9936068,1564570,1875643264,3915936)
UNION ALL
SELECT Led_contratos.cliente as IDCliente,IIF(clientes.acceso_ci = 1,'A','V') as modalidad,
clientes.nombre_cliente
FROM clientes INNER JOIN
led_contratos ON clientes.cliente = led_contratos.cliente
WHERE clientes.estado_cliente = 'A' and
(clientes.acceso_led = 1 or clientes.acceso_ci = 1)
and getdate() between led_contratos.vigencia_desde
and led_contratos.vigencia_hasta
and led_contratos.cliente in (9936068,1564570,1875643264,3915936)
*/
/*
and led_contratos.cliente not in (9335603)
*/
Open LedClientesActivos
Fetch NEXT FROM LedClientesActivos INTO @IDCliente,@Modalidad,@NombreCliente
While @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
/* Popular las tablas de periodo */
/*
select @IDCliente as IDCliente,@StartOfMonth as StartOfMonth,@ActualDate as ActualDate,@periodoV as periodo
*/
/* Renovamos el periodo de los contratistas directos del cliente*/
select @IDCliente as cliente
if (@Modalidad = 'A')
BEGIN
insert into led_contratista_periodo (contratista,periodo)
select contratista,@periodoA as periodo from led_contratistas
WHERE (cliente = @IDCliente)
and estado_contratista = 'A'
and contratista = contratista_1
and contratista not in (select contratista from led_contratista_periodo where periodo = @periodoA and cliente = @IDCliente)
and contratista in (select contratista from led_contratista_periodo where periodo = (@periodoV) and cliente = @IDCliente)
END
insert into led_contratista_periodo (contratista,periodo)
select contratista,@periodoV as periodo from led_contratistas
WHERE (cliente = @IDCliente)
and estado_contratista = 'A'
and contratista = contratista_1
and contratista not in (select contratista from led_contratista_periodo where periodo = @periodoV and cliente = @IDCliente)
and contratista in (select contratista from led_contratista_periodo where periodo = (@periodoVAnt) and cliente = @IDCliente)
/* Renovamos el periodo de los sub contratistas del cliente*/
if (@Modalidad = 'A')
BEGIN
insert into led_subcontratista_periodo (subcontratista,periodo,contratista,contratista_1)
select contratista as subcontratista,@periodoA as periodo,
subcontratista_de as contratista,contratista_1
from led_contratistas
WHERE (cliente = @IDCliente)
and estado_contratista = 'A'
and contratista <> contratista_1
and contratista not in (select subcontratista from led_subcontratista_periodo where periodo = @periodoA and cliente = @IDCliente)
and contratista in (select subcontratista from led_subcontratista_periodo where periodo = (@periodoV) and cliente = @IDCliente)
END
insert into led_subcontratista_periodo (subcontratista,periodo,contratista,contratista_1)
select contratista as subcontratista,@periodov as periodo,
subcontratista_de as contratista,contratista_1
from led_contratistas
WHERE (cliente = @IDCliente)
and estado_contratista = 'A'
and contratista <> contratista_1
and contratista not in (select subcontratista from led_subcontratista_periodo where periodo = @periodov and cliente = @IDCliente )
and contratista in (select subcontratista from led_subcontratista_periodo where periodo = (@periodoVAnt) and cliente = @IDCliente)
/* Renovamos el periodo de los empleados del cliente*/
if (@Modalidad = 'A')
BEGIN
insert into led_empleados_periodo (empleado,periodo,contratista,contratista_1)
select empleado,@periodoA as periodo,
contratista,contratista_1
from led_empleados
WHERE (cliente = @IDCliente)
and estado_empleado = 'A'
and empleado not in (select empleado from led_empleados_periodo where periodo = @periodoA and cliente = @IDCliente)
and empleado in (select empleado from led_empleados_periodo where periodo = (@periodoV) and cliente = @IDCliente)
END
insert into led_empleados_periodo (empleado,periodo,contratista,contratista_1)
select empleado,@periodoV as periodo,
contratista,contratista_1
from led_empleados
WHERE (cliente = @IDCliente)
and estado_empleado = 'A'
and empleado not in (select empleado from led_empleados_periodo where periodo = @periodoV and cliente = @IDCliente)
and empleado in (select empleado from led_empleados_periodo where periodo = (@periodoVAnt) and cliente = @IDCliente)
/*
and contratista <> contratista_1
*/
/* Renovamos el periodo de los vehiculos del cliente*/
if (@Modalidad = 'A')
BEGIN
insert into led_vehiculos_periodo (vehiculo,periodo,contratista,contratista_1)
select vehiculo,@periodoA as periodo,
contratista,contratista_1
from led_vehiculos
WHERE (cliente = @IDCliente)
and estado_vehiculo = 'A'
and vehiculo not in (select vehiculo from led_vehiculos_periodo where periodo = @periodoA and cliente = @IDCliente)
and vehiculo in (select vehiculo from led_vehiculos_periodo where periodo = (@periodoV) and cliente = @IDCliente)
END
insert into led_vehiculos_periodo (vehiculo,periodo,contratista,contratista_1)
select vehiculo,@periodoV as periodo,
contratista,contratista_1
from led_vehiculos
WHERE (cliente = @IDCliente)
and estado_vehiculo = 'A'
and vehiculo not in (select vehiculo from led_vehiculos_periodo where periodo = @periodoV and cliente = @IDCliente)
and vehiculo in (select vehiculo from led_vehiculos_periodo where periodo = (@periodoVAnt) and cliente = @IDCliente)
END TRY
BEGIN CATCH
SET @ERRORNUMBER = 1
END CATCH
Fetch NEXT FROM LedClientesActivos INTO @IDCliente,@Modalidad,@NombreCliente
END
CLOSE LedClientesActivos
DEALLOCATE LedClientesActivos
/*
EXEC Imperium_Inicio_Periodo_Docs
*/
end
END
GO
GO
/*
EXEC Imperium_Inicio_Periodo
*/
CREATE OR ALTER PROCEDURE dbo.Imperium_Inicio_Periodo
/*
@periodo int,
@IDCliente int,
@led_seccion varchar(1)
*/
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ERRORNUMBER SMALLINT
/*
BEGIN
EXEC Carga_Led_Inicio_Periodo
END
*/
DECLARE @StartOfMonth DATE
DECLARE @ActualDate DATE
DECLARE @periodoV INT = NULL
DECLARE @periodoVAnt INT = NULL
DECLARE @periodoA INT = NULL
DECLARE @IDCliente int = NULL
DECLARE @Modalidad varchar(1)
DECLARE @NombreCliente varchar(200)
DECLARE @periodoYear INT = NULL
DECLARE @periodoMonth INT = NULL
SET @periodoYear = convert(varchar(4),datepart(year,getdate()))
SET @periodoMonth = convert(varchar(2),datepart(month,getdate()))
SET @periodoYear = datepart(year,getdate())
SET @periodoMonth = datepart(month,getdate())
SET @periodoA = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())),2)
SeT @StartOfMonth = convert(date,DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
set @ActualDate = getdate()
/*
select @StartOfMonth,@ActualDate,@periodoV,@periodoA,@periodoVAnt,@periodoYear as periodoYear,@periodoMonth as periodoMonth
*/
/*
set @periodoMonth = 1
*/
if (@periodoMonth = 1)
begin
set @periodoV = convert(varchar(4),@periodoYear-1) + RIGHT('00' +convert(varchar(2),12),2)
set @periodoVAnt = convert(varchar(4),@periodoYear-1) + RIGHT('00' +convert(varchar(2),11),2)
end
if (@periodoMonth > 1)
begin
set @periodoV = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())-1),2)
set @periodoVAnt = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())-2),2)
end
set @periodoVAnt = dbo.PeriodoMenosDos()
set @periodoV = dbo.PeriodoVencido()
set @periodoA = dbo.PeriodoAdelantado()
select @StartOfMonth as startofmonth,@ActualDate as actualdate,@periodoV as periodoV,@periodoA as periodoA,@periodoVAnt as periodoVANT,@periodoYear as periodoYear,@periodoMonth as periodoMonth
if @ActualDate = @StartOfMonth
begin
delete led_docs where periodo = @periodoA and estado_doc NOT IN ('N','I','R','V')
select @periodoV as periodoV,@periodoA as periodoA
/*Averiguamos la lista de clientes a procesar */
DECLARE LedClientesActivos Cursor
FOR
SELECT Led_contratos.cliente as IDCliente,IIF(clientes.acceso_ci = 1,'A','V') as modalidad,
clientes.nombre_cliente
FROM clientes INNER JOIN
led_contratos ON clientes.cliente = led_contratos.cliente
WHERE clientes.estado_cliente = 'A' and
(clientes.acceso_led = 1 or clientes.acceso_ci = 1)
and getdate() between led_contratos.vigencia_desde
and led_contratos.vigencia_hasta
order by modalidad asc
/*
and led_contratos.cliente in (9936068,1564570,1875643264,3915936)
UNION ALL
SELECT Led_contratos.cliente as IDCliente,IIF(clientes.acceso_ci = 1,'A','V') as modalidad,
clientes.nombre_cliente
FROM clientes INNER JOIN
led_contratos ON clientes.cliente = led_contratos.cliente
WHERE clientes.estado_cliente = 'A' and
(clientes.acceso_led = 1 or clientes.acceso_ci = 1)
and getdate() between led_contratos.vigencia_desde
and led_contratos.vigencia_hasta
and led_contratos.cliente in (9936068,1564570,1875643264,3915936)
*/
/*
and led_contratos.cliente not in (9335603)
*/
Open LedClientesActivos
Fetch NEXT FROM LedClientesActivos INTO @IDCliente,@Modalidad,@NombreCliente
While @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
/* Popular las tablas de periodo */
/*
select @IDCliente as IDCliente,@StartOfMonth as StartOfMonth,@ActualDate as ActualDate,@periodoV as periodo
*/
/* Renovamos el periodo de los contratistas directos del cliente*/
select @IDCliente as cliente
if (@Modalidad = 'A')
BEGIN
insert into led_contratista_periodo (contratista,periodo)
select contratista,@periodoA as periodo from led_contratistas
WHERE (cliente = @IDCliente)
and estado_contratista = 'A'
and contratista = contratista_1
and contratista not in (select contratista from led_contratista_periodo where periodo = @periodoA and cliente = @IDCliente)
and contratista in (select contratista from led_contratista_periodo where periodo = (@periodoV) and cliente = @IDCliente)
END
insert into led_contratista_periodo (contratista,periodo)
select contratista,@periodoV as periodo from led_contratistas
WHERE (cliente = @IDCliente)
and estado_contratista = 'A'
and contratista = contratista_1
and contratista not in (select contratista from led_contratista_periodo where periodo = @periodoV and cliente = @IDCliente)
and contratista in (select contratista from led_contratista_periodo where periodo = (@periodoVAnt) and cliente = @IDCliente)
/* Renovamos el periodo de los sub contratistas del cliente*/
if (@Modalidad = 'A')
BEGIN
insert into led_subcontratista_periodo (subcontratista,periodo,contratista,contratista_1)
select contratista as subcontratista,@periodoA as periodo,
subcontratista_de as contratista,contratista_1
from led_contratistas
WHERE (cliente = @IDCliente)
and estado_contratista = 'A'
and contratista <> contratista_1
and contratista not in (select subcontratista from led_subcontratista_periodo where periodo = @periodoA and cliente = @IDCliente)
and contratista in (select subcontratista from led_subcontratista_periodo where periodo = (@periodoV) and cliente = @IDCliente)
END
insert into led_subcontratista_periodo (subcontratista,periodo,contratista,contratista_1)
select contratista as subcontratista,@periodov as periodo,
subcontratista_de as contratista,contratista_1
from led_contratistas
WHERE (cliente = @IDCliente)
and estado_contratista = 'A'
and contratista <> contratista_1
and contratista not in (select subcontratista from led_subcontratista_periodo where periodo = @periodov and cliente = @IDCliente )
and contratista in (select subcontratista from led_subcontratista_periodo where periodo = (@periodoVAnt) and cliente = @IDCliente)
/* Renovamos el periodo de los empleados del cliente*/
if (@Modalidad = 'A')
BEGIN
insert into led_empleados_periodo (empleado,periodo,contratista,contratista_1)
select empleado,@periodoA as periodo,
contratista,contratista_1
from led_empleados
WHERE (cliente = @IDCliente)
and estado_empleado = 'A'
and empleado not in (select empleado from led_empleados_periodo where periodo = @periodoA and cliente = @IDCliente)
and empleado in (select empleado from led_empleados_periodo where periodo = (@periodoV) and cliente = @IDCliente)
END
insert into led_empleados_periodo (empleado,periodo,contratista,contratista_1)
select empleado,@periodoV as periodo,
contratista,contratista_1
from led_empleados
WHERE (cliente = @IDCliente)
and estado_empleado = 'A'
and empleado not in (select empleado from led_empleados_periodo where periodo = @periodoV and cliente = @IDCliente)
and empleado in (select empleado from led_empleados_periodo where periodo = (@periodoVAnt) and cliente = @IDCliente)
/*
and contratista <> contratista_1
*/
/* Renovamos el periodo de los vehiculos del cliente*/
if (@Modalidad = 'A')
BEGIN
insert into led_vehiculos_periodo (vehiculo,periodo,contratista,contratista_1)
select vehiculo,@periodoA as periodo,
contratista,contratista_1
from led_vehiculos
WHERE (cliente = @IDCliente)
and estado_vehiculo = 'A'
and vehiculo not in (select vehiculo from led_vehiculos_periodo where periodo = @periodoA and cliente = @IDCliente)
and vehiculo in (select vehiculo from led_vehiculos_periodo where periodo = (@periodoV) and cliente = @IDCliente)
END
insert into led_vehiculos_periodo (vehiculo,periodo,contratista,contratista_1)
select vehiculo,@periodoV as periodo,
contratista,contratista_1
from led_vehiculos
WHERE (cliente = @IDCliente)
and estado_vehiculo = 'A'
and vehiculo not in (select vehiculo from led_vehiculos_periodo where periodo = @periodoV and cliente = @IDCliente)
and vehiculo in (select vehiculo from led_vehiculos_periodo where periodo = (@periodoVAnt) and cliente = @IDCliente)
END TRY
BEGIN CATCH
SET @ERRORNUMBER = 1
END CATCH
Fetch NEXT FROM LedClientesActivos INTO @IDCliente,@Modalidad,@NombreCliente
END
CLOSE LedClientesActivos
DEALLOCATE LedClientesActivos
/*
EXEC Imperium_Inicio_Periodo_Docs
*/
end
END
GO