dbo.monitoreos_batch_comex_24012025

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@yesterday date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec [monitoreos_batch_comex] '2024-09-17'
*/

-- select * from productos_secciones where producto = 12
-- seccion 11 = Comex
CREATE OR ALTER PROCEDURE dbo.monitoreos_batch_comex_24012025
@yesterday date = null

AS
BEGIN
    SET NOCOUNT ON;

 declare @importaciones varchar(50), @importaciones_moneda varchar(50),   
   @exportaciones varchar(50), @exportaciones_moneda varchar(50),
   @total bigint,
   @tipo_operacion varchar(50),
   @periodo varchar(7), 
   @html varchar(max),
   @fecha varchar(7),
   @created date 
        
 if (@yesterday is null) begin set @yesterday = GETDATE() - 1 end 
 
 
 declare @r_cuit varchar(20),@r_seccion_mantenimiento int = 11
 DECLARE bn CURSOR FOR
  select --'30550815997'
  distinct cuit 
   from monitoreos 
   where visible = 1 
   and isnull(seccion_mantenimiento,11) = @r_seccion_mantenimiento
   and (fecha_vigencia_mantenimiento is null or fecha_vigencia_mantenimiento >= @yesterday)
   and (fecha_desde is null or fecha_desde <= @yesterday)  
 OPEN bn 
 Fetch next from bn INTO  @r_cuit 

 WHILE (@@FETCH_STATUS <> -1 )

  BEGIN

   DECLARE mod_comex CURSOR FOR

    select 'IMPO' as tipo_operacion, convert(varchar(7),fecha) as periodo, convert(date, created), 
           sum(ROUND (fobusd,0)) as total
      from comex_importaciones 
      where cuit = @r_cuit  
    and (convert(date,created) = @yesterday and created is not null)
    group by convert(varchar(7),fecha),convert(date,created)

    union all 

    select 'EXPO' as tipo_operacion, convert(varchar(7),fecha) as periodo, convert(date, created), 
           sum(ROUND (fobusd,0)) as total
      from comex_exportaciones 
     where cuit = @r_cuit  
    and (convert(date,created) = @yesterday and created is not null)
    group by convert(varchar(7),fecha),convert(date,created)

   order by convert(varchar(7),fecha)

   declare @periodo_ant varchar(7) = ''
   OPEN mod_comex 
   
   FETCH next from mod_comex INTO  @tipo_operacion, @fecha, @created, @total

   WHILE (@@FETCH_STATUS <> -1 )
    BEGIN
     
     set @periodo = SUBSTRING(@fecha,6,2) + '/' + SUBSTRING(@fecha,1,4)
     if (@periodo !=  @periodo_ant) 
     begin
      set @importaciones = ''
      set @exportaciones = ''
      set @importaciones_moneda = ''
      set @exportaciones_moneda = ''

      if (@periodo_ant != '') 
      begin
       set @html = '<td>'+@r_cuit+'</td><td>'+@periodo_ant+'</td>'+
          '<td align=right>'+@importaciones+'</td>'+
          '<td align=right>'+@importaciones_moneda+'</td>'+
          '<td align=right>'+@exportaciones+'</td>'+
          '<td align=right>'+@exportaciones_moneda+'</td>'

       insert into monitoreos_resultados (id_tabla,tabla,cuit,seccion_mantenimiento,fecha,novedad,tipo_novedad,html)
                     values(null, 'comex', @r_cuit, 11, @created, 1,'comex', @html)
       update monitoreos 
         set fecha_ultimo_procesamiento = @yesterday,
          fecha_modificacion = @yesterday
       where cuit = @r_cuit 
         and isnull(seccion_mantenimiento,@r_seccion_mantenimiento) = @r_seccion_mantenimiento
         and (fecha_vigencia_mantenimiento is null or fecha_vigencia_mantenimiento >= @yesterday)
         and (fecha_desde is null or fecha_desde <= @yesterday) 
      end 
     end 
     set @periodo_ant = @periodo
     if(@tipo_operacion = 'IMPO')
     begin
      set @importaciones = format(@total,'#,#')
      set @importaciones_moneda = 'Dolar'
     end 
     else 
     begin
      set @exportaciones = format(@total,'#,#')
      set @exportaciones_moneda = 'Dolar'
     end
     FETCH next from mod_comex INTO  @tipo_operacion, @fecha, @periodo, @total
          
    END
   
    CLOSE mod_comex
    DEALLOCATE mod_comex 
    
    if (@periodo_ant != '') 
    begin
     set @html = '<td>'+@r_cuit+'</td><td>'+@periodo_ant+'</td>'+
        '<td align=right>'+@importaciones+'</td>'+
        '<td align=right>'+@importaciones_moneda+'</td>'+
        '<td align=right>'+@exportaciones+'</td>'+
        '<td align=right>'+@exportaciones_moneda+'</td>' 
     insert into monitoreos_resultados (id_tabla,tabla,cuit,seccion_mantenimiento,fecha,novedad,tipo_novedad,html)
                values(null, 'comex', @r_cuit, 11, @created, 1,'comex', @html)
     update monitoreos 
       set fecha_ultimo_procesamiento = @yesterday,
        fecha_modificacion = @yesterday
     where cuit = @r_cuit 
      and isnull(seccion_mantenimiento,@r_seccion_mantenimiento) = @r_seccion_mantenimiento
      and (fecha_vigencia_mantenimiento is null or fecha_vigencia_mantenimiento >= @yesterday)
      and (fecha_desde is null or fecha_desde <= @yesterday) 
    end 
  
    FETCH next from bn INTO  @r_cuit 

  END
  CLOSE bn
  DEALLOCATE bn 
END 

/*
 select 'IMPO' as tipo_operacion, convert(varchar(7),fecha) as periodo, convert(date, created), sum(ROUND (fobusd,0)) as total_anual
   from comex_importaciones 
    where cuit = '30550815997'
and (convert(date,created) = '2024-09-17' and created is not null)
group by convert(varchar(7),fecha),convert(date,created)
 union all 

 select 'EXPO' as tipo_operacion, convert(varchar(7),fecha) as periodo, convert(date, created), sum(ROUND (fobusd,0)) as total_anual
   from comex_exportaciones 
  where cuit = '30550815997'  
and (convert(date,created) = '2024-09-17' and created is not null)
group by convert(varchar(7),fecha),convert(date,created)
*/

GO

Used By

No items found