Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
11| Name | Data Type | Length | Description |
|---|---|---|---|
| @accion | varchar | 50 | |
| @cliente | int | 4 | |
| @cli_cod_interno | int | 4 | |
| @nombre | varchar | 500 | |
| @cuit | varchar | 100 | |
| @direccion | varchar | 500 | |
| @localidad | varchar | 500 | |
| @tel | varchar | 500 | |
| varchar | 500 | ||
| @fecha_vigencia_desde | date | 3 | |
| @activo | bit | 1 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE OR ALTER PROCEDURE dbo.ITerceros_contrapartes
@accion varchar(50),
@cliente int,
@cli_cod_interno int = null,
@nombre varchar(500) = null,
@cuit varchar(100) = null,
@direccion varchar(500) = null,
@localidad varchar(500) = null,
@tel varchar(500) = null,
@email varchar(500) = null,
@fecha_vigencia_desde date= null, -- esta la ponemos en la tabla ln_contrapartes
@activo bit = null
AS
BEGIN
SET NOCOUNT ON;
declare @fecha_vigencia_hasta date = '2099-12-31'
declare @vigencia_desde date = getdate()
declare @vigencia_hasta date = '2099-12-31'
declare @idpadron int = null
declare @clave varchar(10) = 'JHSYTRFS'
declare @Userid int = 15342812
declare @modalidad varchar(10)= 'CC'
declare @p_contraparte int = null
declare @idContra int = null
declare @familia int
declare @gestion int = null
declare @paisservicio varchar(2) = 'AR'
declare @id_trazabilidad bigint = null
declare @activo_ant bit = null
declare @tel_ant varchar(500) = null
declare @email_ant varchar(500) = null
declare @detalle varchar(max) = ''
declare @detalle_update varchar(max) = ''
declare @error bit = null
if (@accion = 'ALTA')
begin
BEGIN TRY
select top 1 @idpadron = id_padron
from padrones
where habilitado = 1 and identificacion = @cuit
and identificador in (select identificador from identificadores where country_code = 'AR')
if (@idpadron is null)
begin
--select 'NO EXISTE EN PADRON'
set @detalle = 'CUIT inexistente'
set @error = 1
insert into Iterceros_trazabilidad ( cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
@accion,
@Userid,
0,
@detalle)
select @error as error, @detalle as detalle
return
end
-- hago top 1, pos si llegase a estar mas de una vez, pero no deberia
select top 1 @familia = familia from LN_familias where cliente = @cliente and LN_familias.visible = 1 and LN_familias.servicio_contratado = 47
if (@familia is null)
begin
set @detalle = 'No existen familias definidas para el cliente'
set @error = 1
insert into Iterceros_trazabilidad ( cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
@accion,
@Userid,
0,
@detalle)
select @error as error, @detalle as detalle
return
end
-- hago top 1, pos si llegase a estar mas de una vez, pero no deberia
select top 1 @idContra = contraparte, @activo_ant = activo, @tel_ant = isnull(telefono_contacto,'-'), @email_ant = isnull(email_contacto,'-')
from LN_contrapartes where cliente = @cliente and servicio_contratado = 47 and id_padron = @idpadron order by activo desc
if (@idContra is null)
begin
EXEC LN_contrapartes_ 'INSERT', @idContra OUTPUT, @idpadron, @cliente, null, @activo, null, @email, @tel,
NULL, NULL, @clave, @fecha_vigencia_desde, @fecha_vigencia_hasta, @Userid, 0, NULL, @modalidad,null,null, @paisservicio
-- Guardar trazabilidad INSERT OK
set @detalle = 'Registro dado de alta'
set @error = 0
insert into Iterceros_trazabilidad (cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle,
contraparte)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
'INSERT',
@Userid,
1,
@detalle,
@idContra)
end
else
begin
update LN_contrapartes
set email_contacto = iif(@email is not null, @email, email_contacto),
telefono_contacto = iif(@tel is not null, @tel, telefono_contacto),
vigencia_desde = IIF(@fecha_vigencia_desde is not null, @fecha_vigencia_desde, vigencia_desde),
activo = @activo,
modified = GETDATE(), modified_by = @Userid
where contraparte = @idContra
-- Guardar trazabilidad UPDATE OK
-- Solo guardamos cuando hay algun cambio
set @detalle = 'UPDATE OK'
set @error = 0
set @detalle_update = ''
if(@activo_ant != @activo)
set @detalle_update = @detalle_update + ' - Se cambio el campo activo de ' + convert(char(1),@activo_ant) + ' a ' + convert(char(1),@activo)
if(isnull(@email,'') != '' and @email_ant != @email)
set @detalle_update = @detalle_update + ' - Se cambio el campo email_contacto de ' + @email_ant + ' a ' + @email
if(isnull(@tel,'') != '' and @tel_ant != @tel)
set @detalle_update = @detalle_update + ' - Se cambio el campo telefono_contacto de ' + @tel_ant + ' a ' + @tel
set @detalle_update = ltrim(@detalle_update)
if (@detalle_update != '') -- Hubo cambios
insert into Iterceros_trazabilidad (cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle,
contraparte)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
'UPDATE',
@Userid,
1,
@detalle_update,
@idContra)
end
if not exists (select * from LN_contraparte_familias where contraparte = @idContra)
begin
EXEC LN_contraparte_familias_ 'INSERT', @idContra, @familia, NULL, 'N', 1, @Userid
end
else
begin
update LN_contraparte_familias
set familia = iif (@familia is not null,@familia,familia),
activo = 1,
modified = GETDATE(), modified_by = @Userid
where contraparte = @idContra
end
if not exists (select * from LN_gestiones where contraparte = @idContra and vigente = 1 and desde <= GETDATE() and hasta >= GETDATE())
begin
if exists (select * from LN_gestiones where contraparte = @idContra and vigente = 1 and (desde > GETDATE() or hasta < GETDATE()))
-- No deberia existir otra gestion
begin
update LN_gestiones
set vigente = 0,
modified = GETDATE(), modified_by = @Userid
where contraparte = @idContra and vigente = 1 and (desde > GETDATE() or hasta < GETDATE())
end
EXEC LN_gestiones_ 'INSERT', @gestion OUTPUT, NULL, @idContra, @vigencia_desde, @vigencia_hasta, NULL, @Userid
end
END TRY
BEGIN CATCH
-- Guardar trazabilidad que dio error el alta
set @detalle = 'Error al procesar el registro'
set @error = 1
insert into Iterceros_trazabilidad (cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle,
contraparte)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
'UPDATE',
@Userid,
0,
@detalle,
@idContra)
END CATCH
end
if (@accion = 'FIN')
-- Todos los registros que no se tocaron en la ultima hora, pasan a inactivos
begin
BEGIN TRY
/*
update LN_contrapartes
set activo = 0, modified = GETDATE(), modified_by = @Userid
where cliente = @cliente and activo = 1 and servicio_contratado = 47 and modified < dateadd(hour,-1,GETDATE())
*/
-- Lo hacemos de a uno, para poder guardar la trazabilidad individual
DECLARE C_Cursor CURSOR FOR
select contraparte
from LN_contrapartes
where cliente = @cliente and activo = 1 and servicio_contratado = 47
and modified < dateadd(hour,-1,GETDATE())
OPEN C_Cursor
FETCH NEXT FROM C_Cursor INTO @idContra
WHILE @@fetch_status = 0
BEGIN
update LN_contrapartes
set activo = 0, modified = GETDATE(), modified_by = @Userid
where contraparte = @idContra
-- Guardar trazabilidad de contraparte inactivada
set @error = 0
set @detalle = 'Registro inactivado porque no vino en el archivo'
insert into Iterceros_trazabilidad ( cliente,
accion,
contraparte,
realizado,
detalle,
user_id)
values ( @cliente,
'INACTIVAR',
@idContra,
1,
@detalle,
@Userid)
FETCH NEXT FROM C_Cursor INTO @idContra
END
CLOSE C_Cursor
DEALLOCATE C_Cursor
END TRY
BEGIN CATCH
-- Guardar trazabilidad que dio error el fin
set @detalle = 'ERROR al querer pasar los registros inactivos'
set @error = 1
insert into ITerceros_trazabilidad (cliente,
accion,
realizado,
detalle,
user_id)
values ( @cliente,
'FIN CLIENTE',
0,
@detalle,
@Userid)
END CATCH
end
select @error as error, @detalle as detalle
END
GO
GO
CREATE OR ALTER PROCEDURE dbo.ITerceros_contrapartes
@accion varchar(50),
@cliente int,
@cli_cod_interno int = null,
@nombre varchar(500) = null,
@cuit varchar(100) = null,
@direccion varchar(500) = null,
@localidad varchar(500) = null,
@tel varchar(500) = null,
@email varchar(500) = null,
@fecha_vigencia_desde date= null, -- esta la ponemos en la tabla ln_contrapartes
@activo bit = null
AS
BEGIN
SET NOCOUNT ON;
declare @fecha_vigencia_hasta date = '2099-12-31'
declare @vigencia_desde date = getdate()
declare @vigencia_hasta date = '2099-12-31'
declare @idpadron int = null
declare @clave varchar(10) = 'JHSYTRFS'
declare @Userid int = 15342812
declare @modalidad varchar(10)= 'CC'
declare @p_contraparte int = null
declare @idContra int = null
declare @familia int
declare @gestion int = null
declare @paisservicio varchar(2) = 'AR'
declare @id_trazabilidad bigint = null
declare @activo_ant bit = null
declare @tel_ant varchar(500) = null
declare @email_ant varchar(500) = null
declare @detalle varchar(max) = ''
declare @detalle_update varchar(max) = ''
declare @error bit = null
if (@accion = 'ALTA')
begin
BEGIN TRY
select top 1 @idpadron = id_padron
from padrones
where habilitado = 1 and identificacion = @cuit
and identificador in (select identificador from identificadores where country_code = 'AR')
if (@idpadron is null)
begin
--select 'NO EXISTE EN PADRON'
set @detalle = 'CUIT inexistente'
set @error = 1
insert into Iterceros_trazabilidad ( cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
@accion,
@Userid,
0,
@detalle)
select @error as error, @detalle as detalle
return
end
-- hago top 1, pos si llegase a estar mas de una vez, pero no deberia
select top 1 @familia = familia from LN_familias where cliente = @cliente and LN_familias.visible = 1 and LN_familias.servicio_contratado = 47
if (@familia is null)
begin
set @detalle = 'No existen familias definidas para el cliente'
set @error = 1
insert into Iterceros_trazabilidad ( cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
@accion,
@Userid,
0,
@detalle)
select @error as error, @detalle as detalle
return
end
-- hago top 1, pos si llegase a estar mas de una vez, pero no deberia
select top 1 @idContra = contraparte, @activo_ant = activo, @tel_ant = isnull(telefono_contacto,'-'), @email_ant = isnull(email_contacto,'-')
from LN_contrapartes where cliente = @cliente and servicio_contratado = 47 and id_padron = @idpadron order by activo desc
if (@idContra is null)
begin
EXEC LN_contrapartes_ 'INSERT', @idContra OUTPUT, @idpadron, @cliente, null, @activo, null, @email, @tel,
NULL, NULL, @clave, @fecha_vigencia_desde, @fecha_vigencia_hasta, @Userid, 0, NULL, @modalidad,null,null, @paisservicio
-- Guardar trazabilidad INSERT OK
set @detalle = 'Registro dado de alta'
set @error = 0
insert into Iterceros_trazabilidad (cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle,
contraparte)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
'INSERT',
@Userid,
1,
@detalle,
@idContra)
end
else
begin
update LN_contrapartes
set email_contacto = iif(@email is not null, @email, email_contacto),
telefono_contacto = iif(@tel is not null, @tel, telefono_contacto),
vigencia_desde = IIF(@fecha_vigencia_desde is not null, @fecha_vigencia_desde, vigencia_desde),
activo = @activo,
modified = GETDATE(), modified_by = @Userid
where contraparte = @idContra
-- Guardar trazabilidad UPDATE OK
-- Solo guardamos cuando hay algun cambio
set @detalle = 'UPDATE OK'
set @error = 0
set @detalle_update = ''
if(@activo_ant != @activo)
set @detalle_update = @detalle_update + ' - Se cambio el campo activo de ' + convert(char(1),@activo_ant) + ' a ' + convert(char(1),@activo)
if(isnull(@email,'') != '' and @email_ant != @email)
set @detalle_update = @detalle_update + ' - Se cambio el campo email_contacto de ' + @email_ant + ' a ' + @email
if(isnull(@tel,'') != '' and @tel_ant != @tel)
set @detalle_update = @detalle_update + ' - Se cambio el campo telefono_contacto de ' + @tel_ant + ' a ' + @tel
set @detalle_update = ltrim(@detalle_update)
if (@detalle_update != '') -- Hubo cambios
insert into Iterceros_trazabilidad (cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle,
contraparte)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
'UPDATE',
@Userid,
1,
@detalle_update,
@idContra)
end
if not exists (select * from LN_contraparte_familias where contraparte = @idContra)
begin
EXEC LN_contraparte_familias_ 'INSERT', @idContra, @familia, NULL, 'N', 1, @Userid
end
else
begin
update LN_contraparte_familias
set familia = iif (@familia is not null,@familia,familia),
activo = 1,
modified = GETDATE(), modified_by = @Userid
where contraparte = @idContra
end
if not exists (select * from LN_gestiones where contraparte = @idContra and vigente = 1 and desde <= GETDATE() and hasta >= GETDATE())
begin
if exists (select * from LN_gestiones where contraparte = @idContra and vigente = 1 and (desde > GETDATE() or hasta < GETDATE()))
-- No deberia existir otra gestion
begin
update LN_gestiones
set vigente = 0,
modified = GETDATE(), modified_by = @Userid
where contraparte = @idContra and vigente = 1 and (desde > GETDATE() or hasta < GETDATE())
end
EXEC LN_gestiones_ 'INSERT', @gestion OUTPUT, NULL, @idContra, @vigencia_desde, @vigencia_hasta, NULL, @Userid
end
END TRY
BEGIN CATCH
-- Guardar trazabilidad que dio error el alta
set @detalle = 'Error al procesar el registro'
set @error = 1
insert into Iterceros_trazabilidad (cliente,
cli_cod,
cli_razsoc,
cli_cuit,
cli_direc,
cli_loc,
cli_tel,
cli_email,
cli_fecmod,
cli_habilitado,
accion,
user_id,
realizado,
detalle,
contraparte)
values ( @cliente,
@cli_cod_interno,
@nombre,
@cuit,
@direccion,
@localidad,
@tel,
@email,
@fecha_vigencia_desde,
@activo,
'UPDATE',
@Userid,
0,
@detalle,
@idContra)
END CATCH
end
if (@accion = 'FIN')
-- Todos los registros que no se tocaron en la ultima hora, pasan a inactivos
begin
BEGIN TRY
/*
update LN_contrapartes
set activo = 0, modified = GETDATE(), modified_by = @Userid
where cliente = @cliente and activo = 1 and servicio_contratado = 47 and modified < dateadd(hour,-1,GETDATE())
*/
-- Lo hacemos de a uno, para poder guardar la trazabilidad individual
DECLARE C_Cursor CURSOR FOR
select contraparte
from LN_contrapartes
where cliente = @cliente and activo = 1 and servicio_contratado = 47
and modified < dateadd(hour,-1,GETDATE())
OPEN C_Cursor
FETCH NEXT FROM C_Cursor INTO @idContra
WHILE @@fetch_status = 0
BEGIN
update LN_contrapartes
set activo = 0, modified = GETDATE(), modified_by = @Userid
where contraparte = @idContra
-- Guardar trazabilidad de contraparte inactivada
set @error = 0
set @detalle = 'Registro inactivado porque no vino en el archivo'
insert into Iterceros_trazabilidad ( cliente,
accion,
contraparte,
realizado,
detalle,
user_id)
values ( @cliente,
'INACTIVAR',
@idContra,
1,
@detalle,
@Userid)
FETCH NEXT FROM C_Cursor INTO @idContra
END
CLOSE C_Cursor
DEALLOCATE C_Cursor
END TRY
BEGIN CATCH
-- Guardar trazabilidad que dio error el fin
set @detalle = 'ERROR al querer pasar los registros inactivos'
set @error = 1
insert into ITerceros_trazabilidad (cliente,
accion,
realizado,
detalle,
user_id)
values ( @cliente,
'FIN CLIENTE',
0,
@detalle,
@Userid)
END CATCH
end
select @error as error, @detalle as detalle
END
GO
Depends On
9Used By
No items found