Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
9| Name | Data Type | Length | Description |
|---|---|---|---|
| @p_cuit | varchar | 20 | |
| @p_id_padron | varchar | 50 | |
| @p_fecha_nacimiento | varchar | 20 | |
| @p_razon_social | varchar | 100 | |
| @p_fecha_fallecimiento | varchar | 20 | |
| @p_sexo | varchar | 1 | |
| @p_identificador | varchar | 10 | |
| @p_fecha_inscripcion | varchar | 20 | |
| @p_tipo_persona | varchar | 1 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec [dbo].[carga_datos_persona_afip]
'23080279019',
'20421617',
'1945-05-25',
'SERRANO SAMUEL ANTONIO',
'2021-10-10',
'',
'CUIL',
null,
'F'
*/
CREATE OR ALTER PROCEDURE dbo.carga_datos_persona_afip_ORIGINAL
@p_cuit varchar(20),
@p_id_padron varchar(50),
@p_fecha_nacimiento varchar(20),
@p_razon_social varchar(100),
@p_fecha_fallecimiento varchar(20),
@p_sexo varchar(1),
@p_identificador varchar(10),
@p_fecha_inscripcion varchar(20),
@p_tipo_persona varchar(1)
--select * from DATOS_PERSONAS where c01_cuit = '20177260909'
AS
BEGIN
--inicio DATOS PERSONA
DECLARE @fecha_nacimiento_anterior varchar(20)
DECLARE @fecha_fallecimiento_anterior varchar(20)
DECLARE @fecha_inscripcion_anterior varchar(20)
SET @fecha_nacimiento_anterior = (select top 1 c02_fecha_nac from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
SET @fecha_fallecimiento_anterior = (select top 1 fecha_defuncion from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
SET @fecha_inscripcion_anterior = (select top 1 c02_fecha_ins_rpc from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
IF ((select count(*) from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1) = 0 )
begin
begin try
insert into DATOS_PERSONAS (c01_cuit,fecha_defuncion,c02_fecha_nac,c02_fecha_ins_rpc,created)
values (@p_cuit,@p_fecha_fallecimiento,@p_fecha_nacimiento,@p_fecha_inscripcion,GETDATE())
select 'ok' as ok
end try
begin catch
select ERROR_MESSAGE() as error
end catch
end
--F. DEFUNCION
IF (@p_fecha_fallecimiento is not null and @fecha_fallecimiento_anterior is null)
begin
update DATOS_PERSONAS set
fecha_defuncion = @p_fecha_fallecimiento,
fecha_defuncion_modified = GETDATE(),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
IF (@p_fecha_fallecimiento is not null and
@fecha_fallecimiento_anterior is not null and
@fecha_fallecimiento_anterior != @p_fecha_fallecimiento)
begin
update DATOS_PERSONAS set
fecha_defuncion_ant = @fecha_fallecimiento_anterior,
fecha_defuncion_ant_modified = fecha_defuncion_modified,
fecha_defuncion = @p_fecha_fallecimiento,
fecha_defuncion_modified = GETDATE(),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
-- Si ahora aparece sin fecha de defuncion, y teniamos una fecha, la borramos.
IF (@p_fecha_fallecimiento is null and @fecha_fallecimiento_anterior is not null)
begin
update DATOS_PERSONAS set
fecha_defuncion_ant = @fecha_fallecimiento_anterior,
fecha_defuncion_ant_modified = fecha_defuncion_modified,
fecha_defuncion = null,
fecha_defuncion_modified = GETDATE(),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
--F. NACIMIENTO
IF(@p_fecha_nacimiento is not null) /*and (@fecha_nacimiento_anterior is null OR @fecha_nacimiento_anterior != @p_fecha_nacimiento) )*/
begin
update DATOS_PERSONAS set
c02_fecha_nac = iif (@p_fecha_nacimiento is not null, @p_fecha_nacimiento, @fecha_nacimiento_anterior),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
--F. INSCRIPCION
IF(@p_fecha_inscripcion is not null and (@fecha_inscripcion_anterior is null OR @fecha_inscripcion_anterior != @p_fecha_inscripcion) )
--select iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @p_fecha_inscripcion_anterior) as pepe
begin
update DATOS_PERSONAS set
c02_fecha_ins_rpc = iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @fecha_inscripcion_anterior),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
/*ELSE IF((@p_fecha_nacimiento is not null and (@p_fecha_nacimiento != @p_fecha_nacimiento_anterior)) OR (@p_fecha_fallecimiento is not null and (@p_fecha_fallecimiento != @p_fecha_fallecimiento_anterior)) OR (@p_fecha_inscripcion is not null and (@p_fecha_inscripcion != @p_fecha_inscripcion_anterior)))
begin
begin try
update DATOS_PERSONAS set
c02_fecha_nac = iif (@p_fecha_nacimiento is not null, @p_fecha_nacimiento, @p_fecha_nacimiento_anterior),
fecha_defuncion = iif (@p_fecha_fallecimiento is not null, @p_fecha_fallecimiento, @p_fecha_fallecimiento_anterior),
c02_fecha_ins_rpc = iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @p_fecha_inscripcion_anterior),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
select 'ok' as ok
end try
begin catch
select ERROR_MESSAGE() as error
end catch
end*/
-- fin DATOS PERSONA
/*
-- inicio PADRONES
DECLARE @nombre_anterior varchar(100)
DECLARE @identificador_anterior varchar(10)
DECLARE @tipo_persona_ant varchar(1)
DECLARE @sexo_ant varchar(1)
SET @nombre_anterior = (select top 1 nombre_persona from padrones where identificacion = @p_cuit and habilitado = 1 order by iscreated desc)
SET @identificador_anterior = (select top 1 identificador from padrones where identificacion = @p_cuit and habilitado = 1 order by iscreated desc)
SET @tipo_persona_ant = (select top 1 tipo_persona from padrones where identificacion = @p_cuit and habilitado = 1 order by iscreated desc)
SET @sexo_ant = (select top 1 sexo from padrones where identificacion = @p_cuit and habilitado = 1 order by iscreated desc)
IF(((@p_razon_social is not null and (@nombre_anterior != @p_razon_social)) or @nombre_anterior is null) OR (@p_identificador is not null and (@identificador_anterior != @p_identificador)) OR (@p_tipo_persona is not null and (isnull(@tipo_persona_ant,'') != @p_tipo_persona)) OR (@p_sexo is not null and (@sexo_ant != @p_sexo)))
begin
begin try
update padrones set
nombre_persona = iif (@p_razon_social is not null, @p_razon_social, @nombre_anterior),
sexo = iif (@p_sexo is not null, @p_sexo, @sexo_ant),
identificador = iif (@p_identificador is not null, @p_identificador, @identificador_anterior),
tipo_persona = @p_tipo_persona,
ismodified = GETDATE()
where (id_padron = @p_id_padron) and (habilitado = 1)
select 'ok' as ok
end try
begin catch
select ERROR_MESSAGE() as error
end catch
end
--fin PADRONES
*/
-- inicio PADRONES
DECLARE @nombre_anterior varchar(100)
DECLARE @identificador_anterior varchar(10)
DECLARE @tipo_persona_ant varchar(1)
DECLARE @sexo_ant varchar(1)
SET @nombre_anterior = (select top 1 nombre_persona from padrones where id_padron = @p_id_padron and habilitado = 1)
SET @identificador_anterior = (select top 1 identificador from padrones where id_padron = @p_id_padron and habilitado = 1)
SET @tipo_persona_ant = (select top 1 tipo_persona from padrones where id_padron = @p_id_padron and habilitado = 1)
SET @sexo_ant = (select top 1 sexo from padrones where id_padron = @p_id_padron and habilitado = 1)
IF (isnull(@p_razon_social,'') != isnull(@nombre_anterior,'') OR
isnull(@p_identificador,'') != isnull(@identificador_anterior,'') OR
isnull(@p_tipo_persona,'') != isnull(@tipo_persona_ant,'') OR
isnull(@p_sexo,'') != isnull(@sexo_ant,'') OR
isnull(@p_razon_social,'') = '') --> Este ultimo es para que cambie a habilitado 0, si el nombre está vacío
begin
begin try
/*
DECLARE @p_cuit varchar(30)='27208925658'
declare @p_tipo_persona varchar(1)='F'
declare @p_razon_social varchar(100)='SORIA MARIA INES'
declare @nombre_anterior varchar(100)='SORIA MARIA INES'
DECLARE @p_id_padron INT=6019247
*/
--SELECT * FROM PADRONES WHERE IDENTIFICACION = '27208925658'
--select count(*) from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1
update padrones set
nombre_persona = iif(isnull(@p_razon_social,'') != '', @p_razon_social, @nombre_anterior),
--sexo = iif (@p_sexo is not null, @p_sexo, @sexo_ant),
--identificador = iif (@p_identificador is not null, @p_identificador, @identificador_anterior),
tipo_persona = @p_tipo_persona,
ismodified = GETDATE(),
/*
habilitado = iif(isnull(@p_razon_social,'') != '', 1, 0) --> si el nombre viene vacio de AFIP, avisamos pero lo dejamos habilitado (31/7/2018)
*/
habilitado = iif(isnull(@p_razon_social,'') != '', 1, 1)
where (id_padron = @p_id_padron) and (habilitado = 1)
select 'ok' as ok
end try
begin catch
select ERROR_MESSAGE() as error
end catch
end
--fin PADRONES
END
GO
GO
/*
exec [dbo].[carga_datos_persona_afip]
'23080279019',
'20421617',
'1945-05-25',
'SERRANO SAMUEL ANTONIO',
'2021-10-10',
'',
'CUIL',
null,
'F'
*/
CREATE OR ALTER PROCEDURE dbo.carga_datos_persona_afip_ORIGINAL
@p_cuit varchar(20),
@p_id_padron varchar(50),
@p_fecha_nacimiento varchar(20),
@p_razon_social varchar(100),
@p_fecha_fallecimiento varchar(20),
@p_sexo varchar(1),
@p_identificador varchar(10),
@p_fecha_inscripcion varchar(20),
@p_tipo_persona varchar(1)
--select * from DATOS_PERSONAS where c01_cuit = '20177260909'
AS
BEGIN
--inicio DATOS PERSONA
DECLARE @fecha_nacimiento_anterior varchar(20)
DECLARE @fecha_fallecimiento_anterior varchar(20)
DECLARE @fecha_inscripcion_anterior varchar(20)
SET @fecha_nacimiento_anterior = (select top 1 c02_fecha_nac from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
SET @fecha_fallecimiento_anterior = (select top 1 fecha_defuncion from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
SET @fecha_inscripcion_anterior = (select top 1 c02_fecha_ins_rpc from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1 order by created desc)
IF ((select count(*) from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1) = 0 )
begin
begin try
insert into DATOS_PERSONAS (c01_cuit,fecha_defuncion,c02_fecha_nac,c02_fecha_ins_rpc,created)
values (@p_cuit,@p_fecha_fallecimiento,@p_fecha_nacimiento,@p_fecha_inscripcion,GETDATE())
select 'ok' as ok
end try
begin catch
select ERROR_MESSAGE() as error
end catch
end
--F. DEFUNCION
IF (@p_fecha_fallecimiento is not null and @fecha_fallecimiento_anterior is null)
begin
update DATOS_PERSONAS set
fecha_defuncion = @p_fecha_fallecimiento,
fecha_defuncion_modified = GETDATE(),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
IF (@p_fecha_fallecimiento is not null and
@fecha_fallecimiento_anterior is not null and
@fecha_fallecimiento_anterior != @p_fecha_fallecimiento)
begin
update DATOS_PERSONAS set
fecha_defuncion_ant = @fecha_fallecimiento_anterior,
fecha_defuncion_ant_modified = fecha_defuncion_modified,
fecha_defuncion = @p_fecha_fallecimiento,
fecha_defuncion_modified = GETDATE(),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
-- Si ahora aparece sin fecha de defuncion, y teniamos una fecha, la borramos.
IF (@p_fecha_fallecimiento is null and @fecha_fallecimiento_anterior is not null)
begin
update DATOS_PERSONAS set
fecha_defuncion_ant = @fecha_fallecimiento_anterior,
fecha_defuncion_ant_modified = fecha_defuncion_modified,
fecha_defuncion = null,
fecha_defuncion_modified = GETDATE(),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
--F. NACIMIENTO
IF(@p_fecha_nacimiento is not null) /*and (@fecha_nacimiento_anterior is null OR @fecha_nacimiento_anterior != @p_fecha_nacimiento) )*/
begin
update DATOS_PERSONAS set
c02_fecha_nac = iif (@p_fecha_nacimiento is not null, @p_fecha_nacimiento, @fecha_nacimiento_anterior),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
--F. INSCRIPCION
IF(@p_fecha_inscripcion is not null and (@fecha_inscripcion_anterior is null OR @fecha_inscripcion_anterior != @p_fecha_inscripcion) )
--select iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @p_fecha_inscripcion_anterior) as pepe
begin
update DATOS_PERSONAS set
c02_fecha_ins_rpc = iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @fecha_inscripcion_anterior),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
end
/*ELSE IF((@p_fecha_nacimiento is not null and (@p_fecha_nacimiento != @p_fecha_nacimiento_anterior)) OR (@p_fecha_fallecimiento is not null and (@p_fecha_fallecimiento != @p_fecha_fallecimiento_anterior)) OR (@p_fecha_inscripcion is not null and (@p_fecha_inscripcion != @p_fecha_inscripcion_anterior)))
begin
begin try
update DATOS_PERSONAS set
c02_fecha_nac = iif (@p_fecha_nacimiento is not null, @p_fecha_nacimiento, @p_fecha_nacimiento_anterior),
fecha_defuncion = iif (@p_fecha_fallecimiento is not null, @p_fecha_fallecimiento, @p_fecha_fallecimiento_anterior),
c02_fecha_ins_rpc = iif (@p_fecha_inscripcion is not null, @p_fecha_inscripcion, @p_fecha_inscripcion_anterior),
modified = GETDATE()
where (c01_cuit = @p_cuit) and (visible = 1)
select 'ok' as ok
end try
begin catch
select ERROR_MESSAGE() as error
end catch
end*/
-- fin DATOS PERSONA
/*
-- inicio PADRONES
DECLARE @nombre_anterior varchar(100)
DECLARE @identificador_anterior varchar(10)
DECLARE @tipo_persona_ant varchar(1)
DECLARE @sexo_ant varchar(1)
SET @nombre_anterior = (select top 1 nombre_persona from padrones where identificacion = @p_cuit and habilitado = 1 order by iscreated desc)
SET @identificador_anterior = (select top 1 identificador from padrones where identificacion = @p_cuit and habilitado = 1 order by iscreated desc)
SET @tipo_persona_ant = (select top 1 tipo_persona from padrones where identificacion = @p_cuit and habilitado = 1 order by iscreated desc)
SET @sexo_ant = (select top 1 sexo from padrones where identificacion = @p_cuit and habilitado = 1 order by iscreated desc)
IF(((@p_razon_social is not null and (@nombre_anterior != @p_razon_social)) or @nombre_anterior is null) OR (@p_identificador is not null and (@identificador_anterior != @p_identificador)) OR (@p_tipo_persona is not null and (isnull(@tipo_persona_ant,'') != @p_tipo_persona)) OR (@p_sexo is not null and (@sexo_ant != @p_sexo)))
begin
begin try
update padrones set
nombre_persona = iif (@p_razon_social is not null, @p_razon_social, @nombre_anterior),
sexo = iif (@p_sexo is not null, @p_sexo, @sexo_ant),
identificador = iif (@p_identificador is not null, @p_identificador, @identificador_anterior),
tipo_persona = @p_tipo_persona,
ismodified = GETDATE()
where (id_padron = @p_id_padron) and (habilitado = 1)
select 'ok' as ok
end try
begin catch
select ERROR_MESSAGE() as error
end catch
end
--fin PADRONES
*/
-- inicio PADRONES
DECLARE @nombre_anterior varchar(100)
DECLARE @identificador_anterior varchar(10)
DECLARE @tipo_persona_ant varchar(1)
DECLARE @sexo_ant varchar(1)
SET @nombre_anterior = (select top 1 nombre_persona from padrones where id_padron = @p_id_padron and habilitado = 1)
SET @identificador_anterior = (select top 1 identificador from padrones where id_padron = @p_id_padron and habilitado = 1)
SET @tipo_persona_ant = (select top 1 tipo_persona from padrones where id_padron = @p_id_padron and habilitado = 1)
SET @sexo_ant = (select top 1 sexo from padrones where id_padron = @p_id_padron and habilitado = 1)
IF (isnull(@p_razon_social,'') != isnull(@nombre_anterior,'') OR
isnull(@p_identificador,'') != isnull(@identificador_anterior,'') OR
isnull(@p_tipo_persona,'') != isnull(@tipo_persona_ant,'') OR
isnull(@p_sexo,'') != isnull(@sexo_ant,'') OR
isnull(@p_razon_social,'') = '') --> Este ultimo es para que cambie a habilitado 0, si el nombre está vacío
begin
begin try
/*
DECLARE @p_cuit varchar(30)='27208925658'
declare @p_tipo_persona varchar(1)='F'
declare @p_razon_social varchar(100)='SORIA MARIA INES'
declare @nombre_anterior varchar(100)='SORIA MARIA INES'
DECLARE @p_id_padron INT=6019247
*/
--SELECT * FROM PADRONES WHERE IDENTIFICACION = '27208925658'
--select count(*) from DATOS_PERSONAS where c01_cuit = @p_cuit and visible = 1
update padrones set
nombre_persona = iif(isnull(@p_razon_social,'') != '', @p_razon_social, @nombre_anterior),
--sexo = iif (@p_sexo is not null, @p_sexo, @sexo_ant),
--identificador = iif (@p_identificador is not null, @p_identificador, @identificador_anterior),
tipo_persona = @p_tipo_persona,
ismodified = GETDATE(),
/*
habilitado = iif(isnull(@p_razon_social,'') != '', 1, 0) --> si el nombre viene vacio de AFIP, avisamos pero lo dejamos habilitado (31/7/2018)
*/
habilitado = iif(isnull(@p_razon_social,'') != '', 1, 1)
where (id_padron = @p_id_padron) and (habilitado = 1)
select 'ok' as ok
end try
begin catch
select ERROR_MESSAGE() as error
end catch
end
--fin PADRONES
END
GO
Depends On
2Used By
No items found