dbo.obtener_cuadro_cheques

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@p_Cuit varchar 20

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE  dbo.obtener_cuadro_cheques
@p_Cuit varchar(20)
AS

BEGIN

DECLARE @p_5anios smalldatetime = getdate() - 1825
DECLARE @p_2anios smalldatetime = getdate() - 730
DECLARE @p_1anio  smalldatetime = getdate() - 365
DECLARE @total_cheques int = 0
DECLARE @tipo varchar(2)
SELECT 
     count(*) as cantidad, 'pago' as estado,sum(isnull(ChequesBCRA.importe,0)) as monto,'formales' as tipo
    FROM
      ChequesBCRA
      LEFT JOIN padrones AS pf ON (pf.identificacion = cuit_firmante and pf.habilitado = 1)    --info firmante
      INNER JOIN Cheques_causales ON (ChequesBCRA.id_causa = Cheques_causales.id_causa)
    WHERE
   (Cuit_firmante = @p_Cuit)
     AND id_cheque = (select max(id_cheque) from chequesBCRA b 
       where b.cuit_firmante = chequesBCRA.cuit_firmante 
         and b.nro_cheque = chequesBCRA.nro_cheque 
         and b.visible = 1) 
 and ChequesBCRA.id_causa = 1
 and fecha_levantamiento is not null
      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 >= (GETDATE() - 2))) 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_1anio)) 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) 
   )

  )

 union all
 SELECT 
     count(*) as cantidad, 'impago' as estado,sum(isnull(ChequesBCRA.importe,0)) as monto, 'formales' as tipo
    FROM
      ChequesBCRA
      LEFT JOIN padrones AS pf ON (pf.identificacion = cuit_firmante)    --info firmante
      INNER JOIN Cheques_causales ON (ChequesBCRA.id_causa = Cheques_causales.id_causa)
    WHERE
   (Cuit_firmante = @p_Cuit)
     AND id_cheque = (select max(id_cheque) from chequesBCRA b 
       where b.cuit_firmante = chequesBCRA.cuit_firmante 
         and b.nro_cheque = chequesBCRA.nro_cheque 
         and b.visible = 1) 
 and ChequesBCRA.id_causa = 1
 and fecha_levantamiento is null
      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 >= (GETDATE() - 2))) 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_1anio)) 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) 
   )

  )
union all
SELECT 
     count(*) as cantidad, 'pago' as estado,sum(isnull(ChequesBCRA.importe,0)) as monto, 'sin_fondos' as tipo
    FROM
      ChequesBCRA
      LEFT JOIN padrones AS pf ON (pf.identificacion = cuit_firmante)    --info firmante
      INNER JOIN Cheques_causales ON (ChequesBCRA.id_causa = Cheques_causales.id_causa)
    WHERE
   (Cuit_firmante = @p_Cuit)
     AND id_cheque = (select max(id_cheque) from chequesBCRA b 
       where b.cuit_firmante = chequesBCRA.cuit_firmante 
         and b.nro_cheque = chequesBCRA.nro_cheque 
         and b.visible = 1) 
 and ChequesBCRA.id_causa = 2
 and fecha_levantamiento is not null
      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 >= (GETDATE() - 2))) 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_1anio)) 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) 
   )

  )

 union all
 SELECT 
     count(*) as cantidad, 'impago' as estado,sum(isnull(ChequesBCRA.importe,0)) as monto, 'sin_fondos' as tipo
    FROM
      ChequesBCRA
      LEFT JOIN padrones AS pf ON (pf.identificacion = cuit_firmante)    --info firmante
      INNER JOIN Cheques_causales ON (ChequesBCRA.id_causa = Cheques_causales.id_causa)
    WHERE
   (Cuit_firmante = @p_Cuit)
     AND id_cheque = (select max(id_cheque) from chequesBCRA b 
       where b.cuit_firmante = chequesBCRA.cuit_firmante 
         and b.nro_cheque = chequesBCRA.nro_cheque 
         and b.visible = 1) 
 and ChequesBCRA.id_causa = 2
 and fecha_levantamiento is null
      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 >= (GETDATE() - 2))) 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_1anio)) 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) 
   )

  )
  order by estado asc,tipo desc
 end
GO

Used By

No items found