CREATE OR REPLACE PROCEDURE DASHBOARD_FIN_COUNT AS  
BEGIN
    DELETE FROM FINPDCCONT;

    FOR COUNT_FIN IN (
        SELECT
            IDFINPDCCONT,
            NRORG,
            CDEMPRESA,
            COUNT(*) NRCOUNT
        FROM
            (
                SELECT
                    'VENCIDO_REC' IDFINPDCCONT,
                    NRORG,
                    CDEMPRESA
                FROM
                    BOLETRECEBER BR
                WHERE
                    TRUNC(BR.DTVENBOLREC) < TRUNC(SYSDATE)
                    AND BR.IDSTATBREC IN (
                        'A',
                        'E'
                    )
                UNION ALL
                SELECT
                    'ABERTO_DIA_REC' IDFINPDCCONT,
                    NRORG,
                    CDEMPRESA
                FROM
                    BOLETRECEBER BR
                WHERE
                    TRUNC(BR.DTVENBOLREC) = TRUNC(SYSDATE)
                    AND BR.IDSTATBREC = 'A'
                UNION ALL
                SELECT
                    'VENCIDO_PAG' IDFINPDCCONT,
                    NRORG,
                    CDEMPRESA
                FROM
                    BOLETPAGAR BR
                WHERE
                    TRUNC(BR.DTVENBOLPAG) < TRUNC(SYSDATE)
                    AND BR.IDSTATBPAG IN (
                        'A',
                        'E'
                    )
                UNION ALL
                SELECT
                    'ABERTO_DIA_PAG' IDFINPDCCONT,
                    NRORG,
                    CDEMPRESA
                FROM
                    BOLETPAGAR BR
                WHERE
                    TRUNC(BR.DTVENBOLPAG) = TRUNC(SYSDATE)
                    AND BR.IDSTATBPAG = 'A'
                UNION ALL
                SELECT
                    'BAIXADO_REC' IDFINPDCCONT,
                    BA.NRORG,
                    BA.CDEMPRESA
                FROM
                    BOLETRECEBER BR,
                    BAIXARECEBIDA BA
                WHERE
                    BR.IDSTATBREC = 'B'
                    AND BR.NRORG = BA.NRORG
                    AND BR.CDEMPRESA = BA.CDEMPRESA
                    AND BR.NRBAIXAREC = BA.NRBAIXAREC
                    AND ( ( TRUNC(BR.DTVENBOLREC) < TRUNC(SYSDATE) )
                          OR ( TRUNC(BR.DTVENBOLREC) = TRUNC(SYSDATE) ) )
                    AND TRUNC(BA.DTBAIXAREC) = TRUNC(SYSDATE)
                UNION ALL
                SELECT
                    'BAIXADO_PAG' IDFINPDCCONT,
                    BA.NRORG,
                    BA.CDEMPRESA
                FROM
                    BOLETPAGAR BR,
                    BAIXAPAGA BA
                WHERE
                    BR.IDSTATBPAG = 'B'
                    AND BR.NRORG = BR.NRORG
                    AND BR.CDEMPRESA = BA.CDEMPRESA
                    AND BR.NRBAIXAPAG = BA.NRBAIXAPAG
                    AND ( ( TRUNC(BR.DTVENBOLPAG) < TRUNC(SYSDATE) )
                          OR ( TRUNC(BR.DTVENBOLPAG) = TRUNC(SYSDATE) ) )
                    AND TRUNC(BA.DTBAIXAPAG) = TRUNC(SYSDATE)
            )
        GROUP BY
            IDFINPDCCONT,
            NRORG,
            CDEMPRESA
        ORDER BY
            CDEMPRESA
    ) LOOP
        INSERT INTO FINPDCCONT (
            IDFINPDCCONT,
            NRORG,
            CDEMPRESA,
            NRCOUNT
        ) VALUES (
            COUNT_FIN.IDFINPDCCONT,
            COUNT_FIN.NRORG,
            COUNT_FIN.CDEMPRESA,
            COUNT_FIN.NRCOUNT
        );

    END LOOP;

END;
/