dbo.Imperium_Carga_Docs_Vehiculos_Contratista

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

4
Name Data Type Length Description
@periodo int 4
@IDCliente int 4
@IDContratista int 4
@ModalidadForzada varchar 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec Imperium_Carga_Docs_Vehiculos_Contratista 202202,7048064,3561,'A'
*/

CREATE OR ALTER PROCEDURE dbo.Imperium_Carga_Docs_Vehiculos_Contratista
@periodo int,
@IDCliente int,
@IDContratista int,
@ModalidadForzada varchar(1)

AS

BEGIN
 SET NOCOUNT ON;


DECLARE @ERRORNUMBER SMALLINT 
declare  @periodoAnt int 

DECLARE @PeriodoModalidad int  
DECLARE @Modalidad VARCHAR(1)


DECLARE @periodoFecha date


/*
declare @periodo int = 201701
DECLARE @IDCliente INT = 5546348
*/

DECLARE @periodoV INT = NULL
DECLARE @periodoVAnt INT = NULL
DECLARE @periodoA INT = NULL
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)

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()


if @Periodo =  @periodoA
 BEGIN
  SET @Modalidad = 'A'
 END
ELSE
 BEGIN
  SET @Modalidad = 'V'
 END




/*
if (@IDCliente = 9335603)
begin
set @IDCliente = 0
end
*/


select @periodoV as periodoV,@periodoA as periodoA,@periodoVAnt as periodoVANT,@PERIODO AS PERIODO,@periodoYear as periodoYear,@periodoMonth as periodoMonth,@Modalidad AS Modalidad,@PeriodoModalidad AS PeriodoModalidad


DECLARE @LastDay_AnyMonth DATETIME
--Last Day of Any Month and Year
DECLARE @dtDate DATETIME, @nextMonth DATETIME, @LastDayNextMonth DATETIME
SET @dtDate = substring(CONVERT(varchar(6),@periodo),1,4) + '-' + substring(CONVERT(varchar(6),@periodo),5,2) + '-01'
SET @nextMonth = DATEADD(mm, 1, @dtDate)

set @LastDay_AnyMonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0)) 
set @LastDayNextMonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@nextMonth)+1,0)) 


DECLARE @tipo_doc INT,@estado_doc VARCHAR(1),@led_seccion VARCHAR(1),@cliente INT,@contratista INT,@empleado INT,@vehiculo INT
DECLARE @contratista_1 INT,@insertado DATETIME,@fecha_tolerancia DATETIME,@compliance_doc VARCHAR(1),@fecha_vigencia DATETIME,@doc_log VARCHAR(150),@grupo_doc INT
DECLARE @doc_id_lista INT

DECLARE LedCargaDocs Cursor
FOR

SELECT  distinct led_tipos_doc.tipo_doc,
/*
iif(LDAnt.fecha_vigencia > GETDATE(),LDAnt.estado_doc,'N') as estado_doc,
*/

iif(LDAnt.fecha_vigencia > GETDATE(),LDAnt.estado_doc,led_cliente_vehiculo.estado_inicial) as estado_doc,

/*
iif(LDAnt.fecha_vigencia > GETDATE() and led_tipos_doc.periodicidad = 'E',LDAnt.estado_doc,'N') as estado_doc,
*/

led_tipos_doc.led_seccion,
led_vehiculos.cliente,
led_vehiculos.contratista,  
null as empleado,
led_vehiculos.vehiculo as vehiculo,
/*
convert(varchar(4),datepart(year,getdate())) + convert(varchar(2),datepart(month,getdate())) as periodo,
*/

@periodo AS periodo,
led_vehiculos.contratista_1,
GETDATE() as insertado,
dateadd(ms,-3,Convert(varchar(10),isnull(led_cliente_vehiculo.dias_tolerancia,0) + getdate(),111)) as fecha_tolerancia,
/*
iif(LDAnt.fecha_vigencia > GETDATE(),LDAnt.compliance_doc,'B') as compliance_doc,
*/

iif(LDAnt.fecha_vigencia > GETDATE(),LDAnt.compliance_doc,iif(led_cliente_vehiculo.estado_inicial='N','B',iif(led_cliente_vehiculo.estado_inicial='R','R',iif(led_cliente_vehiculo.estado_inicial='V','V',iif(led_cliente_vehiculo.estado_inicial='I','A','B'))))) as compliance_doc,


iif(LDAnt.fecha_vigencia > getdate(),LDAnt.fecha_vigencia,NULL) as fecha_vigencia,
/*
iif(LDAnt.fecha_vigencia > GETDATE() and led_tipos_doc.periodicidad = 'E',LDAnt.compliance_doc,'B') as compliance_doc,
iif(LDAnt.fecha_vigencia > getdate() and led_tipos_doc.periodicidad = 'E',LDAnt.fecha_vigencia,NULL) as fecha_vigencia,

*/

convert(varchar(16),getdate(),120) + ' GENERACION AUTOMATICA DE DOCUMENTOS' AS doc_log,
led_grupo_tipo_doc.grupo_doc,led_tipos_doc.tipo_doc_lista as doc_id_lista
FROM            led_vehiculos INNER JOIN
                         led_cliente_vehiculo ON led_vehiculos.cliente = led_cliente_vehiculo.cliente AND led_vehiculos.tipo_vehiculo = led_cliente_vehiculo.tipo_vehiculo INNER JOIN
                         clientes ON led_vehiculos.cliente = clientes.cliente INNER JOIN
                         led_tipos_doc ON led_cliente_vehiculo.tipo_doc = led_tipos_doc.tipo_doc INNER JOIN
                         led_grupo_tipo_doc ON led_cliente_vehiculo.tipo_doc = led_grupo_tipo_doc.tipo_doc
       inner join led_grupos_doc on led_grupo_tipo_doc.grupo_doc = led_grupos_doc.grupo_doc 
       INNER JOIN led_vehiculos_periodo on (led_vehiculos.vehiculo = led_vehiculos_periodo.vehiculo and led_vehiculos_periodo.periodo = @periodo and led_vehiculos_periodo.tipo_baja is null) 
 left JOIN LED_DOCS LDAnt ON (LDAnt.tipo_doc = led_tipos_doc.tipo_doc AND LDAnt.led_seccion = led_tipos_doc.led_seccion AND LDAnt.vehiculo = led_vehiculos.vehiculo 
 AND LDAnt.periodo = @periodoVAnt AND LDAnt.grupo_doc = led_grupo_tipo_doc.grupo_doc)
WHERE        (clientes.cliente = @IDCliente) AND (led_cliente_vehiculo.activo = 1)
/*
and ((led_tipos_doc.doc_alta = 0 OR led_tipos_doc.doc_alta IS NULL)
and (led_tipos_doc.doc_baja = 0 OR led_tipos_doc.doc_baja IS NULL) or led_tipos_doc.doc_alta = 1 and (LDAnt.fecha_vigencia > getdate() OR LDAnt.fecha_vigencia IS NULL))
*/

and ( ISNULL(led_tipos_doc.doc_alta,0) = 0 
  or (led_tipos_doc.doc_alta = 1 /*and 
   (LDAnt.fecha_vigencia > getdate() OR LDAnt.fecha_vigencia IS NULL)*/

  ) )

and ISNULL(led_tipos_doc.doc_baja,0) = 0 
and led_grupos_doc.cliente = @IDCliente
and led_vehiculos.estado_vehiculo = 'A'
and led_tipos_doc.modalidad = @ModalidadForzada /*@Modalidad*/
and led_VEHICULOS.contratista = @IDContratista


DECLARE @dbDocID BIGINT = 0

Open LedCargaDocs


Fetch NEXT FROM LedCargaDocs INTO @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista

While @@FETCH_STATUS = 0
BEGIN
/*
SELECT @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc
*/


SET @dbDocID = 0


DELETE from led_docs
where tipo_doc = @tipo_doc
and periodo = @periodo
and led_seccion = @led_seccion
and contratista = @contratista
and vehiculo = @vehiculo
AND estado_doc NOT IN ('N','I','V','R')

select @dbDocID = doc_id from led_docs
where tipo_doc = @tipo_doc
and periodo = @periodo
and led_seccion = @led_seccion
and contratista = @contratista
and vehiculo = @vehiculo
/*
SELECT @dbDocID, @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc
*/


IF (@dbDocID = 0)
/*
select @dbDocID as dbDocID
*/


BEGIN

BEGIN TRY

insert into led_docs (
tipo_doc,
estado_doc,
led_seccion,
cliente,
contratista,  
empleado,
vehiculo,
periodo,
contratista_1,
insertado,
fecha_tolerancia,
compliance_doc,
fecha_vigencia,
doc_log,
grupo_doc,
doc_id_lista
)
values
(@tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista)

END TRY

BEGIN CATCH

SET @ERRORNUMBER = 1

END CATCH
END 
Fetch NEXT FROM LedCargaDocs INTO @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista

END

CLOSE LedCargaDocs
DEALLOCATE LedCargaDocs



END

GO