dbo.Carga_Led_Inicio_Periodo

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

CREATE OR ALTER PROCEDURE dbo.Carga_Led_Inicio_Periodo
AS

BEGIN
 SET NOCOUNT ON;

DECLARE @ERRORNUMBER SMALLINT 



SELECT 'NO USAR'
/*

DECLARE @StartOfMonth DATE
DECLARE @ActualDate DATE
DECLARE @periodo INT = NULL
DECLARE @IDCliente int = NULL


SET @periodo = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())),2)



SELECT @StartOfMonth = convert(date,DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) 
select @ActualDate = getdate()
if @ActualDate = @StartOfMonth
begin

/*Averiguamos la lista de clientes a procesar */

DECLARE LedClientesActivos Cursor
FOR

SELECT        led_contratos.cliente as IDCliente
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

Open LedClientesActivos


Fetch NEXT FROM LedClientesActivos INTO @IDCliente

While (@@FETCH_STATUS <> -1)
BEGIN


BEGIN TRY
/* Popular las tablas de periodo */


select @IDCliente as IDCliente,@StartOfMonth as StartOfMonth,@ActualDate as ActualDate,@periodo as periodo

/* Renovamos el periodo de los contratistas directos del cliente*/


insert into led_contratista_periodo (contratista,periodo)
select contratista,@periodo 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 = @periodo and cliente = @IDCliente)

/* Renovamos el periodo de los sub contratistas del cliente*/


insert into led_subcontratista_periodo (subcontratista,periodo,contratista,contratista_1)
select contratista as subcontratista,@periodo 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 contratista from led_subcontratista_periodo where periodo = @periodo and cliente = @IDCliente)



/* Renovamos el periodo de los empleados del cliente*/

insert into led_empleados_periodo (empleado,periodo,contratista,contratista_1)
select empleado,@periodo 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 = @periodo and cliente = @IDCliente)
/*
and contratista <> contratista_1
*/



/* Renovamos el periodo de los vehiculos del cliente*/

insert into led_vehiculos_periodo (vehiculo,periodo,contratista,contratista_1)
select vehiculo,@periodo 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 = @periodo and cliente = @IDCliente)

END TRY

BEGIN CATCH

SET @ERRORNUMBER = 1

END CATCH

Fetch NEXT FROM LedClientesActivos INTO @IDCliente

END

CLOSE LedClientesActivos
DEALLOCATE LedClientesActivos
end
*/

END

GO

Depends On

No items found

Used By

No items found