dbo.ChequesBCRA_estadisticas_

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@paso int 4
@desde datetime 8
@hasta datetime 8

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec ChequesBCRA_estadisticas_ 1, '2020-01-01 00:00:00', '2020-12-31 23:59:59' 
select * from  chequesBCRA_estadisticas

select year(fecha_rechazo), count(*) from chequesBCRA group by year(fecha_rechazo)

exec ChequesBCRA_estadisticas_ 1,'2017-01-01','2017-12-31'

exec ChequesBCRA_estadisticas_ 2

select top 10000 * from chequesBCRA where fecha_rechazo >= '2024-01-01'

*/

CREATE OR ALTER PROCEDURE dbo.ChequesBCRA_estadisticas_  
/* Siempre se borra los datos que ya teniamos en la tabla, porque puede ser que cheques que teniamos de 
antes hayan pasado a estar levantados, o por disposision del BCRA hayan sido eliminados */
 
@paso int ,
@desde datetime = '2016-01-01 00:00:00',
@hasta datetime = '2020-06-29 23:59:59' 
AS
BEGIN
    SET NOCOUNT ON;
 
 if (@paso = 1) 
 begin 

 DECLARE @p_5anios smalldatetime = getdate() - 1825
 DECLARE @p_2anios smalldatetime = getdate() - 730
 DECLARE @p_1anio  smalldatetime = getdate() - 365

 --delete from chequesBCRA_estadisticas

 /* Cheques de dic-2019 a mar-2020, 
  cuando es un cheque de empresa, lo tomo una sola vez... no cada uno de los firmantes (por eso cuit_relacionado NULL)
  tomamos causal 1 y 2
  visibles */


 insert into chequesBCRA_estadisticas

 select cuit_firmante, cuit_relacionado, nro_cheque, importe, id_causa, fecha_rechazo, fecha_levantamiento, fecha_pago 
  from ChequesBCRA 
  WHERE 
    (id_cheque = (select max(id_cheque) from chequesBCRA b 
        where (b.cuit_firmante = chequesBCRA.cuit_firmante)
            and (b.nro_cheque = chequesBCRA.nro_cheque)
         and (isnull(b.Cuit_relacionado,'')  = isnull(chequesBCRA.Cuit_relacionado,''))
         and (b.visible = 1)))
   AND (id_causa in (1,2))
   AND fecha_rechazo between @desde and @hasta  
   AND isnull(Cuit_relacionado,'') = ''
   AND (visible = 1)
   /*AND (
    (
    (
    (
    (fecha_levantamiento IS NULL) 
    AND (fecha_rechazo >= @p_5anios)
    ) 
    OR 
    (
    (
    (fecha_levantamiento >= @p_5anios) 
    AND ((datediff(day,fecha_rechazo,fecha_levantamiento)) <= 15)
    ) 
    AND 
    (
    (
    (fecha_pago IS NOT NULL) 
    AND (fecha_levantamiento >= (GETDATE() - 2))
    ) 
    OR 
    (
    (
    (fecha_pago IS NULL) 
    AND (fecha_levantamiento >= @p_2anios)
    ) 
    AND (fecha_rechazo >= @p_5anios)
    ) 
    ) 
    ) 
    ) 
    OR 
    (
    (
    (fecha_levantamiento >= @p_5anios) 
    AND 
    (
    ((datediff(day,fecha_rechazo,fecha_levantamiento)) >= 16 ) 
    AND ((datediff(day,fecha_rechazo,fecha_levantamiento)) <= 90)
    ) 
    ) 
    AND 
    (
    (
    (fecha_pago IS NOT NULL) 
    AND (fecha_levantamiento >= @p_1anio)
    )
    OR 
    (
    (
    (fecha_pago IS NULL) 
    AND (fecha_levantamiento >= @p_2anios)
    ) 
    AND 
    (fecha_rechazo >= @p_5anios)
    )
    ) 
    ) 
    )
    OR 
    (
    (
    (
    (fecha_levantamiento >= @p_5anios) 
    AND ((datediff(day,fecha_rechazo,fecha_levantamiento)) > 90)
    ) 
    AND 
    (fecha_levantamiento >= @p_2anios) 
    ) 
    AND (fecha_rechazo >= @p_5anios) 
    )
    )*/

 end
 
 if (@paso = 2)
 begin
  select DATEPART(year,fecha_rechazo) 'Año', datepart(month,fecha_rechazo) 'Mes', 
  case id_causa
   when 2 then 'SIN FONDOS'
   when 1 then 'DEFECTOS FORMALES'
   else NULL
  end as 'Causal', 
  'NO LEVANTADOS' as Levantados,
  count(*) Cantidad, sum(isnull(importe,0)) 'Importe total'
  from chequesBCRA_estadisticas 
  where fecha_levantamiento is null
  group by DATEPART(year,fecha_rechazo) , datepart(month,fecha_rechazo), id_causa
  union all
    select DATEPART(year,fecha_rechazo) 'Año', datepart(month,fecha_rechazo) 'Mes', 
  case id_causa
   when 2 then 'SIN FONDOS'
   when 1 then 'DEFECTOS FORMALES'
   else NULL
  end as 'Causal', 
  'LEVANTADOS' as  Levantados,
  count(*) Cantidad, sum(isnull(importe,0)) 'Importe total'
  from chequesBCRA_estadisticas 
  where fecha_levantamiento is not null
  group by DATEPART(year,fecha_rechazo) , datepart(month,fecha_rechazo), id_causa

  order by 1,2,3,4
 end 
END 

GO

Used By

No items found