  procedure SCRIE_JV_2007(tnAn    JV2007.AN%TYPE,
                          tnLuna  JV2007.LUNA%TYPE,
                          tnScrie number) IS

    lcSql                    VARCHAR2(32767);
    lcContTvaDeductibil      ACT.SCD%TYPE := '4426';
    lcContTvaColectat        ACT.SCD%TYPE := '4427';
    lcContTvaNeexigibil      ACT.SCD%TYPE := '4428';
    lcContDebitRegularizare  VARCHAR2(100) := '4111,461,4511,4118';
    lcContCreditRegularizare VARCHAR2(100) := '418';
    lnIdMinJtvaColoana       JTVA_COLOANE.ID_JTVA_COLOANA%TYPE := 1;
    lnIdMaxJtvaColoana       JTVA_COLOANE.ID_JTVA_COLOANA%TYPE := 100;
    lcTabelSursa             VARCHAR2(100);
    lcCondSucursala          VARCHAR2(1000);
  begin
    -- tnScrie: 0 - scriere; 1 - refacere; 2 - stergere
    -- 0,2 - selectie din act_temp
    -- 1 - selectie din act

    if pack_contafin.get_id_sucursala() is not null then
      lcCondSucursala := ' AND ALIAS.ID_SUCURSALA = ' ||
                         pack_contafin.GET_ID_SUCURSALA();
    end if;

    if tnScrie in (pack_contafin.nScriere, pack_contafin.nStergere) then
      lcTabelSursa := 'act_temp'; -- scriere, stergere
    else
      lcTabelSursa := 'act'; -- refacere
    end if;

    pack_contafin.completeaza_jv_2007(tnAn,
                                      tnLuna,
                                      lcTabelSursa,
                                      lcCondSucursala);

      MERGE INTO JV2007 J
      USING (SELECT AN,
                    LUNA,
                    ID_FDOC,
                    ID_FACT,
                    NRACT,
                    SERIE_ACT,
                    DATAACT,
                    DATAIREG,
                    ID_PART,
                    COD,
                    (CASE
                      WHEN NOTA_TVA_EX = 1 THEN
                       0
                      WHEN REGULARIZARE > 0 THEN
                       TOTCTVA -
                       DECODE(RO24NB, 0, 0, RO24B) -
                       DECODE(RO20NB, 0, 0, RO20B) -
                       DECODE(RO21NB, 0, 0, RO21B) -
                       DECODE(RO11NB, 0, 0, RO11B) -
                       DECODE(RO19NB, 0, 0, RO19B) -
                       DECODE(RO9NB, 0, 0, RO9B) -
                       DECODE(RO5NB, 0, 0, RO5B) -
                       DECODE(RO24NT, 0, 0, RO24T) -
                       DECODE(RO20NT, 0, 0, RO20T) -
                       DECODE(RO21NT, 0, 0, RO21T) -
                       DECODE(RO11NT, 0, 0, RO11T) -
                       DECODE(RO19NT, 0, 0, RO19T) -
                       DECODE(RO9NT, 0, 0, RO9T) -
                       DECODE(RO5NT, 0, 0, RO5T) -
                       DECODE(RO24NTR, 0, RO24TR, RO24NTR) -
                       DECODE(RO20NTR, 0, RO20TR, RO20NTR) -
                       DECODE(RO21NTR, 0, RO21TR, RO21NTR) -
                       DECODE(RO11NTR, 0, RO11TR, RO11NTR) -
                       DECODE(RO19NTR, 0, RO19TR, RO19NTR) -
                       DECODE(RO9NTR, 0, RO9TR, RO9NTR) -
                       DECODE(RO5NTR, 0, RO5TR, RO5NTR)
                      ELSE
                       TOTCTVA -
                       DECODE(RO24NB, 0, 0, RO24B) -
                       DECODE(RO20NB, 0, 0, RO20B) -
                       DECODE(RO21NB, 0, 0, RO21B) -
                       DECODE(RO11NB, 0, 0, RO11B) -
                       DECODE(RO19NB, 0, 0, RO19B) -
                       DECODE(RO9NB, 0, 0, RO9B) -
                       DECODE(RO5NB, 0, 0, RO5B) -
                       DECODE(RO24NT, 0, 0, RO24T) -
                       DECODE(RO20NT, 0, 0, RO20T) -
                       DECODE(RO21NT, 0, 0, RO21T) -
                       DECODE(RO11NT, 0, 0, RO11T) -
                       DECODE(RO19NT, 0, 0, RO19T) -
                       DECODE(RO9NT, 0, 0, RO9T) -
                       DECODE(RO5NT, 0, 0, RO5T)
                    END) AS TOTCTVA,
                    (CASE
                      WHEN NOTA_TVA_EX = 1 THEN
                       0
                      WHEN REGULARIZARE > 0 THEN
                       DECODE(RO24NB, 0, RO24B, RO24NB) +
                       DECODE(RO20NB, 0, RO20B, RO20NB) +
                       DECODE(RO21NB, 0, RO21B, RO21NB) +
                       DECODE(RO11NB, 0, RO11B, RO11NB) +
                       DECODE(RO19NB, 0, RO19B, RO19NB) +
                       DECODE(RO9NB, 0, RO9B, RO9NB) +
                       DECODE(RO5NB, 0, RO5B, RO5NB) -
                       DECODE(RO24NB, 0, RO24TR, RO24NTR) -
                       DECODE(RO20NB, 0, RO20TR, RO20NTR) -
                       DECODE(RO21NB, 0, RO21TR, RO21NTR) -
                       DECODE(RO11NB, 0, RO11TR, RO11NTR) -
                       DECODE(RO19NB, 0, RO19TR, RO19NTR) -
                       DECODE(RO9NB, 0, RO9TR, RO9NTR) -
                       DECODE(RO5NB, 0, RO5TR, RO5NTR)
                      ELSE
                       DECODE(RO24NB, 0, RO24B, RO24NB) +
                       DECODE(RO20NB, 0, RO20B, RO20NB) +
                       DECODE(RO21NB, 0, RO21B, RO21NB) +
                       DECODE(RO11NB, 0, RO11B, RO11NB) +
                       DECODE(RO19NB, 0, RO19B, RO19NB) +
                       DECODE(RO9NB, 0, RO9B, RO9NB) +
                       DECODE(RO5NB, 0, RO5B, RO5NB)
                    END) AS TOTFTVATAX,
                    DECODE(NOTA_TVA_EX,
                           1,
                           0,
                           DECODE(RO24NT, 0, RO24T, RO24NT) +
                           DECODE(RO20NT, 0, RO20T, RO20NT) +
                           DECODE(RO21NT, 0, RO21T, RO21NT) +
                           DECODE(RO11NT, 0, RO11T, RO11NT) +
                           DECODE(RO19NT, 0, RO19T, RO19NT) +
                           DECODE(RO9NT, 0, RO9T, RO9NT) +
                           DECODE(RO5NT, 0, RO5T, RO5NT)) AS TOTTVATAX,
                    ROTI + CESCDD1 + CESCDD2 + CEOPTR + CESVDD + CESVFDD +
                    CESVFS + WRSCDD + WRSCDDAB + WRSCDDCD + FODD + FOFDD +
                    WRSCFDD + WRN AS TOTNETAX,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       DECODE(RO24B,
                              0,
                              DECODE(RO24T, 0, 0, ROUND(RO24T / 0.24, 2)),
                              RO24B - RO24TR)
                      ELSE
                       DECODE(RO24B,
                              0,
                              DECODE(RO24T, 0, 0, ROUND(RO24T / 0.24, 2)),
                              RO24B)
                    END) AS RO24B,
                    RO24T,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       DECODE(RO20B,
                              0,
                              DECODE(RO20T, 0, 0, ROUND(RO20T / 0.20, 2)),
                              RO20B - RO20TR)
                      ELSE
                       DECODE(RO20B,
                              0,
                              DECODE(RO20T, 0, 0, ROUND(RO20T / 0.20, 2)),
                              RO20B)
                    END) AS RO20B,
                    RO20T,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       DECODE(RO21B,
                              0,
                              DECODE(RO21T, 0, 0, ROUND(RO21T / 0.21, 2)),
                              RO21B - RORTD21)
                      ELSE
                       DECODE(RO21B,
                              0,
                              DECODE(RO21T, 0, 0, ROUND(RO21T / 0.21, 2)),
                              RO21B)
                    END) AS RO21B,
                    RO21T,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       DECODE(RO11B,
                              0,
                              DECODE(RO11T, 0, 0, ROUND(RO11T / 0.11, 2)),
                              RO11B - RORTD11)
                      ELSE
                       DECODE(RO11B,
                              0,
                              DECODE(RO11T, 0, 0, ROUND(RO11T / 0.11, 2)),
                              RO11B)
                    END) AS RO11B,
                    RO11T,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       DECODE(RO19B,
                              0,
                              DECODE(RO19T, 0, 0, ROUND(RO19T / 0.19, 2)),
                              RO19B - RO19TR)
                      ELSE
                       DECODE(RO19B,
                              0,
                              DECODE(RO19T, 0, 0, ROUND(RO19T / 0.19, 2)),
                              RO19B)
                    END) AS RO19B,
                    RO19T,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       DECODE(RO9B,
                              0,
                              DECODE(RO9T, 0, 0, ROUND(RO9T / 0.09, 2)),
                              RO9B - RO9TR)
                      ELSE
                       DECODE(RO9B,
                              0,
                              DECODE(RO9T, 0, 0, ROUND(RO9T / 0.09, 2)),
                              RO9B)
                    END) AS RO9B,
                    RO9T,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       DECODE(RO5B,
                              0,
                              DECODE(RO5T, 0, 0, ROUND(RO5T / 0.05, 2)),
                              RO5B - RO5TR)
                      ELSE
                       DECODE(RO5B,
                              0,
                              DECODE(RO5T, 0, 0, ROUND(RO5T / 0.05, 2)),
                              RO5B)
                    END) AS RO5B,
                    RO5T,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       RO24NB - RO24NTR
                      ELSE
                       RO24NB
                    END) AS RO24NB,
                    RO24NT,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       RO20NB - RO20NTR
                      ELSE
                       RO20NB
                    END) AS RO20NB,
                    RO20NT,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       RO21NB - RO21NTR
                      ELSE
                       RO21NB
                    END) AS RO21NB,
                    RO21NT,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       RO11NB - RO11NTR
                      ELSE
                       RO11NB
                    END) AS RO11NB,
                    RO11NT,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       RO19NB - RO19NTR
                      ELSE
                       RO19NB
                    END) AS RO19NB,
                    RO19NT,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       RO9NB - RO9NTR
                      ELSE
                       RO9NB
                    END) AS RO9NB,
                    RO9NT,
                    (CASE
                      WHEN REGULARIZARE > 0 THEN
                       RO5NB - RO5NTR
                      ELSE
                       RO5NB
                    END) AS RO5NB,
                    RO5NT,
                    ROTI,
                    CESCDD1,
                    CESCDD2,
                    CEOPTR,
                    CESVDD,
                    CESVFDD,
                    CESVFS,
                    WRSCDD,
                    WRSCDDAB,
                    WRSCDDCD,
                    FODD,
                    FOFDD,
                    WRSCFDD,
                    WRN,
                    RORTC24,
                    RORTC20,
                    RORTC21,
                    RORTC11,
                    RORTC19,
                    RORTC9,
                    RORTC5,
                    ID_SUCURSALA
               FROM (SELECT JV.AN,
                            JV.LUNA,
                            MAX(JV.ID_FDOC) AS ID_FDOC,
                            JV.ID_FACT,
                            JV.NRACT,
                            JV.SERIE_ACT,
                            JV.DATAACT,
                            JV.DATAIREG,
                            MAX(JV.ID_PART) AS ID_PART,
                            JV.ID_SUCURSALA,
                            MIN(JV.COD) AS COD,
                            MIN(JV.NOTA_TVA_EX) AS NOTA_TVA_EX,
                            sum(CASE
                                  WHEN NVL(JV.ID_JTVA_COLOANA, 0) BETWEEN 1 AND 100 THEN
                                   JV.suma
                                  ELSE
                                   0
                                END) TOTCTVA,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 15, JV.suma, 0)) RO24B,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 16, JV.suma, 0)) RO24T,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 27, JV.suma, 0)) RO20B,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 28, JV.suma, 0)) RO20T,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 35, JV.suma, 0)) RO21B,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 36, JV.suma, 0)) RO21T,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 1, JV.suma, 0)) RO19B,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 2, JV.suma, 0)) RO19T,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 3, JV.suma, 0)) RO9B,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 4, JV.suma, 0)) RO9T,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 13, JV.suma, 0)) RO5B,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 14, JV.suma, 0)) RO5T,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 39, JV.suma, 0)) RO11B,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 40, JV.suma, 0)) RO11T,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 5, JV.suma, 0)) ROTI,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 6, JV.suma, 0)) CESCDD1,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 7, JV.suma, 0)) CESCDD2,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 17, JV.suma, 0)) CEOPTR,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 18, JV.suma, 0)) CESVDD,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 19, JV.suma, 0)) CESVFDD,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 20, JV.suma, 0)) CESVFS,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 8, JV.suma, 0)) WRSCDD,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 33, JV.suma, 0)) WRSCDDAB,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 34, JV.suma, 0)) WRSCDDCD,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 9, JV.suma, 0)) FODD,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 10, JV.suma, 0)) FOFDD,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 11, JV.suma, 0)) WRSCFDD,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 12, JV.suma, 0)) WRN,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 1006, JV.suma, 0)) RORTC24,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 1007, JV.suma, 0)) RORTC20,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 1016, JV.suma, 0)) RORTC21,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 1017, JV.suma, 0)) RORTC11,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 1008, JV.suma, 0)) RORTC19,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 1009, JV.suma, 0)) RORTC9,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 1010, JV.suma, 0)) RORTC5,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 21, JV.suma, 0)) RO24NB,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 22, JV.suma, 0)) RO24NT,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 29, JV.suma, 0)) RO20NB,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 30, JV.suma, 0)) RO20NT,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 37, JV.suma, 0)) RO21NB,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 38, JV.suma, 0)) RO21NT,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 41, JV.suma, 0)) RO11NB,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 42, JV.suma, 0)) RO11NT,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 31, JV.suma, 0)) RO19NB,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 32, JV.suma, 0)) RO19NT,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 23, JV.suma, 0)) RO9NB,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 24, JV.suma, 0)) RO9NT,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 25, JV.suma, 0)) RO5NB,
                            sum(DECODE(JV.ID_JTVA_COLOANA, 26, JV.suma, 0)) RO5NT,
                            SUM(JV.REGULARIZARE) AS REGULARIZARE,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 16 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RO24TR,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 28 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RO20TR,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 36 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RO21TR,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 40 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RO11TR,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 2 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RO19TR,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 4 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RO9TR,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 14 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RO5TR,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 1016 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RORTD21,
                            SUM(CASE
                                  WHEN JV.ID_JTVA_COLOANA = 1017 AND JV.SCD = '4428' THEN
                                   JV.SUMA
                                  ELSE
                                   0
                                END) RORTD11,
                            SUM(CASE
                                  WHEN ID_JTVA_COLOANA = 22 AND SCD = '4428' THEN
                                   SUMA
                                  ELSE
                                   0
                                END) RO24NTR,
                            SUM(CASE
                                  WHEN ID_JTVA_COLOANA = 30 AND SCD = '4428' THEN
                                   SUMA
                                  ELSE
                                   0
                                END) RO20NTR,
                            SUM(CASE
                                  WHEN ID_JTVA_COLOANA = 32 AND SCD = '4428' THEN
                                   SUMA
                                  ELSE
                                   0
                                END) RO19NTR,
                            SUM(CASE
                                  WHEN ID_JTVA_COLOANA = 24 AND SCD = '4428' THEN
                                   SUMA
                                  ELSE
                                   0
                                END) RO9NTR,
                            SUM(CASE
                                  WHEN ID_JTVA_COLOANA = 26 AND SCD = '4428' THEN
                                   SUMA
                                  ELSE
                                   0
                                END) RO5NTR,
                            SUM(CASE
                                  WHEN ID_JTVA_COLOANA = 38 AND SCD = '4428' THEN
                                   SUMA
                                  ELSE
                                   0
                                END) RO21NTR,
                            SUM(CASE
                                  WHEN ID_JTVA_COLOANA = 42 AND SCD = '4428' THEN
                                   SUMA
                                  ELSE
                                   0
                                END) RO11NTR
                       FROM (SELECT A.AN,
                                    A.LUNA,
                                    (CASE
                                      WHEN A.NRACT <> D.NRACT or
                                           A.DATAACT <> D.DATAACT THEN
                                       1
                                      ELSE
                                       0
                                    END) AS NOTA_TVA_EX,
                                    (CASE
                                      WHEN A.NRACT <> D.NRACT or
                                           A.DATAACT <> D.DATAACT OR
                                           A.ID_SET <> D.ID_SET OR
                                           NVL(A.SERIE_ACT, 'X') <>
                                           NVL(D.SERIE_ACT, 'X') THEN
                                       NULL
                                      ELSE
                                       A.ID_FDOC
                                    END) AS ID_FDOC,
                                    A.ID_FACT,
                                    (CASE
                                      WHEN A.NRACT <> D.NRACT THEN
                                       D.NRACT
                                      ELSE
                                       A.NRACT
                                    END) AS NRACT,
                                    D.SERIE_ACT,
                                    (CASE
                                      WHEN A.NRACT <> D.NRACT or
                                           A.DATAACT <> D.DATAACT THEN
                                       TRUNC(D.DATAACT)
                                      ELSE
                                       TRUNC(A.DATAACT)
                                    END) AS DATAACT,
                                    (CASE
                                      WHEN A.NRACT <> D.NRACT or
                                           NVL(A.DATAIREG, A.DATAACT) <>
                                           NVL(D.DATAIREG, D.DATAACT) THEN
                                       TRUNC(NVL(D.DATAIREG, D.DATAACT))
                                      ELSE
                                       TRUNC(NVL(A.DATAIREG, A.DATAACT))
                                    END) AS DATAIREG,
                                    A.COD,
                                    A.SCD,
                                    A.SCC,
                                    -- 4426 = 4427 sau 4428 = 4427 nu au parteneri
                                    MAX(DECODE(E.EXCEPTIE,
                                               1,
                                               A.ID_PARTC,
                                               (CASE
                                               -- ESTE POSIBIL SA FIE DOAR 5121 = 419 FARA 4111 = 4427, TREBUIE PARTENERUL 419
                                                 WHEN A.SCC = '419' THEN
                                                  A.ID_PARTC
                                                 WHEN A.SCD IN (lcContTvaDeductibil,
                                                                lcContTvaNeexigibil) OR
                                                      SUBSTR(A.SCD, 1, 1) = '5' THEN
                                                  -9999999999
                                                 ELSE
                                                  A.ID_PARTD
                                               END))) OVER(PARTITION BY A.AN, A.LUNA, A.ID_FDOC, A.NRACT, A.DATAACT, A.COD) AS ID_PART,
                                    -- INVERSARE SUME LA EXCEPTII
                                    DECODE(A.ID_SET, 10616, -1, 1) *
                                    DECODE(E.EXCEPTIE, 1, -A.SUMA, A.SUMA) AS SUMA,
                                    -- MARCA REGULARIZARE FACTURI NEINTOCMITE
                                    (CASE
                                      WHEN INSTR(lcContDebitRegularizare, A.scd) > 0 and
                                           INSTR(lcContCreditRegularizare, A.scc) > 0 THEN
                                       1
                                      ELSE
                                       0
                                    END) AS REGULARIZARE,
                                    A.ID_JTVA_COLOANA,
                                    A.ID_SUCURSALA
                               FROM (
                                SELECT AN,
                                       LUNA,
                                       ID_FDOC,
                                       ID_FACT,
                                       NRACT,
                                       SERIE_ACT,
                                       DATAACT,
                                       DATAIREG,
                                       COD,
                                       SCD,
                                       SCC,
                                       ID_PARTC,
                                       ID_PARTD,
                                       ID_SET,
                                       SUMA,
                                       ID_JTVA_COLOANA,
                                       ID_SUCURSALA,
                                       STERS
                                FROM ACT_TEMP
                                WHERE lcTabelSursa = 'act_temp'
                                  AND AN = tnAn
                                  AND LUNA = tnLuna
                                  AND STERS = 0
                                  AND (ID_JTVA_COLOANA BETWEEN lnIdMinJtvaColoana AND lnIdMaxJtvaColoana
                                       OR ID_JTVA_COLOANA BETWEEN 1006 AND 1010)
                                  AND ID_SET <> 10621
                                UNION ALL
                                SELECT AN,
                                       LUNA,
                                       ID_FDOC,
                                       ID_FACT,
                                       NRACT,
                                       SERIE_ACT,
                                       DATAACT,
                                       DATAIREG,
                                       COD,
                                       SCD,
                                       SCC,
                                       ID_PARTC,
                                       ID_PARTD,
                                       ID_SET,
                                       SUMA,
                                       ID_JTVA_COLOANA,
                                       ID_SUCURSALA,
                                       STERS
                                FROM ACT
                                WHERE lcTabelSursa = 'act'
                                  AND AN = tnAn
                                  AND LUNA = tnLuna
                                  AND STERS = 0
                                  AND (ID_JTVA_COLOANA BETWEEN lnIdMinJtvaColoana AND lnIdMaxJtvaColoana
                                       OR ID_JTVA_COLOANA BETWEEN 1006 AND 1010)
                                  AND ID_SET <> 10621
                               ) A
                               LEFT JOIN (SELECT DISTINCT 1 AS EXCEPTIE,
                                                         DECODE(DEBIT,
                                                                1,
                                                                CONT_C,
                                                                CONT) AS SCD,
                                                         DECODE(DEBIT,
                                                                1,
                                                                CONT,
                                                                CONT_C) AS SCC
                                           FROM EXCEPTII_IREG
                                          WHERE INVERS = 1
                                            AND IN_TVA = 1) E
                                 ON A.SCD = E.SCD
                                AND A.SCC = E.SCC
                               LEFT JOIN DOCUMENTE D
                                 ON A.ID_FACT = D.ID_DOC
                              WHERE A.AN = tnAn
                                and A.LUNA = tnLuna
                                and A.STERS = 0
                                AND (A.ID_JTVA_COLOANA between lnIdMinJtvaColoana and
                                    lnIdMaxJtvaColoana OR
                                    A.ID_JTVA_COLOANA BETWEEN 1006 AND 1010)
                                   -- IN JURNALUL DE VANZARI NU INTRA FACTURILE NEINTOCMITE + INCASARILE
                                   -- SAU COMPENSARI 419 CU EXPLICATIE TVA
                                AND A.ID_SET <> 10621
                                AND NOT (TRIM(NVL(A.SCD, 'X')) = '418' OR
                                     (TRIM(NVL(A.SCD, 'X')) not in
                                     ('4111', '461', '4511', '4118') AND
                                     TRIM(NVL(A.SCC, 'X')) = '418'))
                                AND nvl2(pack_contafin.GET_ID_SUCURSALA(),
                                         A.ID_SUCURSALA,
                                         0) =
                                    nvl(pack_contafin.GET_ID_SUCURSALA(), 0)) JV
                      GROUP BY JV.AN,
                               JV.LUNA,
                               JV.ID_FACT,
                               JV.NRACT,
                               JV.SERIE_ACT,
                               JV.DATAACT,
                               JV.DATAIREG,
                               JV.ID_SUCURSALA)) S
      ON (J.AN = S.AN AND J.LUNA = S.LUNA AND J.ID_FACT = S.ID_FACT AND NVL(J.ID_SUCURSALA, -99) = NVL(S.ID_SUCURSALA, -99))
      WHEN MATCHED THEN
        UPDATE
           SET J.TOTCTVA    = J.TOTCTVA + (CASE
                                WHEN EXTRACT(MONTH FROM J.DATAACT) = J.LUNA AND
                                     EXTRACT(YEAR FROM J.DATAACT) = J.AN THEN
                                 S.TOTCTVA
                                ELSE
                                 0
                              END),
               J.TOTFTVATAX = J.TOTFTVATAX + (CASE
                                WHEN EXTRACT(MONTH FROM J.DATAACT) = J.LUNA AND
                                     EXTRACT(YEAR FROM J.DATAACT) = J.AN THEN
                                 S.TOTFTVATAX
                                ELSE
                                 0
                              END),
               J.TOTTVATAX  = J.TOTTVATAX + (CASE
                                WHEN EXTRACT(MONTH FROM J.DATAACT) = J.LUNA AND
                                     EXTRACT(YEAR FROM J.DATAACT) = J.AN THEN
                                 S.TOTTVATAX
                                ELSE
                                 0
                              END),
               J.TOTNETAX   = J.TOTNETAX + (CASE
                                WHEN EXTRACT(MONTH FROM J.DATAACT) = J.LUNA AND
                                     EXTRACT(YEAR FROM J.DATAACT) = J.AN THEN
                                 S.TOTNETAX
                                ELSE
                                 0
                              END),
               J.RO24B      = J.RO24B + NVL(S.RO24B, 0) + (CASE
                                WHEN NVL(S.RO24B, 0) <> 0 AND NVL(J.RO24NT, 0) <> 0 AND
                                     NVL(J.RO24NT, 0) - (NVL(J.RO24T, 0) + NVL(S.RO24T, 0)) = 0 THEN
                                 NVL(J.RO24NB, 0) - (NVL(J.RO24B, 0) + NVL(S.RO24B, 0))
                                ELSE
                                 0
                              END),
               J.RO24T      = J.RO24T + S.RO24T,
               J.RO20B      = J.RO20B + NVL(S.RO20B, 0) + (CASE
                                WHEN NVL(S.RO20B, 0) <> 0 AND NVL(J.RO20NT, 0) <> 0 AND
                                     NVL(J.RO20NT, 0) - (NVL(J.RO20T, 0) + NVL(S.RO20T, 0)) = 0 THEN
                                 NVL(J.RO20NB, 0) - (NVL(J.RO20B, 0) + NVL(S.RO20B, 0))
                                ELSE
                                 0
                              END),
               J.RO20T      = J.RO20T + S.RO20T,
               J.RO21B      = J.RO21B + NVL(S.RO21B, 0) + (CASE
                                WHEN NVL(S.RO21B, 0) <> 0 AND NVL(J.RO21NT, 0) <> 0 AND
                                     NVL(J.RO21NT, 0) - (NVL(J.RO21T, 0) + NVL(S.RO21T, 0)) = 0 THEN
                                 NVL(J.RO21NB, 0) - (NVL(J.RO21B, 0) + NVL(S.RO21B, 0))
                                ELSE
                                 0
                              END),
               J.RO21T      = J.RO21T + S.RO21T,
               J.RO11B      = J.RO11B + NVL(S.RO11B, 0) + (CASE
                                WHEN NVL(S.RO11B, 0) <> 0 AND NVL(J.RO11NT, 0) <> 0 AND
                                     NVL(J.RO11NT, 0) - (NVL(J.RO11T, 0) + NVL(S.RO11T, 0)) = 0 THEN
                                 NVL(J.RO11NB, 0) - (NVL(J.RO11B, 0) + NVL(S.RO11B, 0))
                                ELSE
                                 0
                              END),
               J.RO11T      = J.RO11T + S.RO11T,
               J.RO19B      = J.RO19B + NVL(S.RO19B, 0) + (CASE
                                WHEN NVL(S.RO19B, 0) <> 0 AND NVL(J.RO19NT, 0) <> 0 AND
                                     NVL(J.RO19NT, 0) - (NVL(J.RO19T, 0) + NVL(S.RO19T, 0)) = 0 THEN
                                 NVL(J.RO19NB, 0) - (NVL(J.RO19B, 0) + NVL(S.RO19B, 0))
                                ELSE
                                 0
                              END),
               J.RO19T      = J.RO19T + S.RO19T,
               J.RO9B       = J.RO9B + NVL(S.RO9B, 0) + (CASE
                                WHEN NVL(S.RO9B, 0) <> 0 AND NVL(J.RO9NT, 0) <> 0 AND
                                     NVL(J.RO9NT, 0) - (NVL(J.RO9T, 0) + NVL(S.RO9T, 0)) = 0 THEN
                                 NVL(J.RO9NB, 0) - (NVL(J.RO9B, 0) + NVL(S.RO9B, 0))
                                ELSE
                                 0
                              END),
               J.RO9T       = J.RO9T + S.RO9T,
               J.RO5B       = J.RO5B + NVL(S.RO5B, 0) + (CASE
                                WHEN NVL(S.RO5B, 0) <> 0 AND NVL(J.RO5NT, 0) <> 0 AND
                                     NVL(J.RO5NT, 0) - (NVL(J.RO5T, 0) + NVL(S.RO5T, 0)) = 0 THEN
                                 NVL(J.RO5NB, 0) - (NVL(J.RO5B, 0) + NVL(S.RO5B, 0))
                                ELSE
                                 0
                              END),
               J.RO5T       = J.RO5T + S.RO5T,
               J.RO24NB     = J.RO24NB + S.RO24NB,
               J.RO24NT     = J.RO24NT + S.RO24NT,
               J.RO20NB     = J.RO20NB + S.RO20NB,
               J.RO20NT     = J.RO20NT + S.RO20NT,
               J.RO21NB     = J.RO21NB + S.RO21NB,
               J.RO21NT     = J.RO21NT + S.RO21NT,
               J.RO11NB     = J.RO11NB + S.RO11NB,
               J.RO11NT     = J.RO11NT + S.RO11NT,
               J.RO19NB     = J.RO19NB + S.RO19NB,
               J.RO19NT     = J.RO19NT + S.RO19NT,
               J.RO9NB      = J.RO9NB + S.RO9NB,
               J.RO9NT      = J.RO9NT + S.RO9NT,
               J.RO5NB      = J.RO5NB + S.RO5NB,
               J.RO5NT      = J.RO5NT + S.RO5NT,
               J.ROTI       = J.ROTI + S.ROTI,
               J.CESCDD1    = J.CESCDD1 + S.CESCDD1,
               J.CESCDD2    = J.CESCDD2 + S.CESCDD2,
               J.CEOPTR     = J.CEOPTR + S.CEOPTR,
               J.CESVDD     = J.CESVDD + S.CESVDD,
               J.CESVFDD    = J.CESVFDD + S.CESVFDD,
               J.CESVFS     = J.CESVFS + S.CESVFS,
               J.WRSCDD     = J.WRSCDD + S.WRSCDD,
               J.WRSCDDAB   = J.WRSCDDAB + S.WRSCDDAB,
               J.WRSCDDCD   = J.WRSCDDCD + S.WRSCDDCD,
               J.FODD       = J.FODD + S.FODD,
               J.FOFDD      = J.FOFDD + S.FOFDD,
               J.WRSCFDD    = J.WRSCFDD + S.WRSCFDD,
               J.WRN        = J.WRN + S.WRN,
               J.RORTC24    = J.RORTC24 + S.RORTC24,
               J.RORTC20    = J.RORTC20 + S.RORTC20,
               J.RORTC21    = J.RORTC21 + S.RORTC21,
               J.RORTC11    = J.RORTC11 + S.RORTC11,
               J.RORTC19    = J.RORTC19 + S.RORTC19,
               J.RORTC9     = J.RORTC9 + S.RORTC9,
               J.RORTC5     = J.RORTC5 + S.RORTC5 DELETE
         WHERE ' || tnScrie || ' = ' ||
               pack_contafin.nStergere || '
           and J.TOTCTVA = 0
           AND J.TOTFTVATAX = 0
           AND J.TOTTVATAX = 0
           AND J.TOTNETAX = 0
           AND J.RO24B = 0
           AND J.RO24T = 0
           AND J.RO20B = 0
           AND J.RO20T = 0
           AND J.RO19B = 0
           AND J.RO19T = 0
           AND J.RO9B = 0
           AND J.RO9T = 0
           AND J.RO5B = 0
           AND J.RO5T = 0
           AND J.RO24NB = 0
           AND J.RO24NT = 0
           AND J.RO20NB = 0
           AND J.RO20NT = 0
           AND J.RO19NB = 0
           AND J.RO19NT = 0
           AND J.RO9NB = 0
           AND J.RO9NT = 0
           AND J.RO5NB = 0
           AND J.RO5NT = 0
           AND J.ROTI = 0
           AND J.CESCDD1 = 0
           AND J.CESCDD2 = 0
           AND J.CEOPTR = 0
           AND J.CESVDD = 0
           AND J.CESVFDD = 0
           AND J.CESVFS = 0
           AND J.WRSCDD = 0
           AND J.WRSCDDAB = 0
           AND J.WRSCDDCD = 0
           AND J.FODD = 0
           AND J.FOFDD = 0
           AND J.WRSCFDD = 0
           AND J.WRN = 0
           AND J.RORTC24 = 0
           AND J.RORTC20 = 0
           AND J.RORTC19 = 0
           AND J.RORTC9 = 0
           AND J.RORTC5 = 0
         WHEN NOT MATCHED THEN
           INSERT(AN, LUNA, ID_FACT, NRACT, SERIE_ACT, ID_FDOC, DATAACT, DATAIREG, ID_PART, TOTCTVA, TOTFTVATAX, TOTTVATAX, TOTNETAX, RO24B, RO24T, RO20B, RO20T, RO21B, RO21T, RO11B, RO11T, RO19B, RO19T, RO9B, RO9T, RO5B, RO5T, RO24NB, RO24NT, RO20NB, RO20NT, RO21NB, RO21NT, RO11NB, RO11NT, RO19NB, RO19NT, RO9NB, RO9NT, RO5NB, RO5NT, ROTI, CESCDD1, CESCDD2, CEOPTR, CESVDD, CESVFDD, CESVFS, WRSCDD, WRSCDDAB, WRSCDDCD, FODD, FOFDD, WRSCFDD, WRN, RORTC24, RORTC20, RORTC21, RORTC11, RORTC19, RORTC9, RORTC5, COD, ID_SUCURSALA) VALUES(S.AN, S.LUNA, S.ID_FACT, S.NRACT, S.SERIE_ACT, S.ID_FDOC, S.DATAACT, S.DATAIREG, S.ID_PART, S.TOTCTVA, S.TOTFTVATAX, S.TOTTVATAX, S.TOTNETAX,
           (CASE
          WHEN NVL(S.RO24B, 0) <> 0 AND NVL(S.RO24NT, 0) <> 0 AND
               NVL(S.RO24NT, 0) = NVL(S.RO24T, 0) THEN
           NVL(S.RO24NB, 0)
          ELSE
           NVL(S.RO24B, 0)
        END), S.RO24T,
        (CASE
          WHEN NVL(S.RO20B, 0) <> 0 AND NVL(S.RO20NT, 0) <> 0 AND
               NVL(S.RO20NT, 0) = NVL(S.RO20T, 0) THEN
           NVL(S.RO20NB, 0)
          ELSE
           NVL(S.RO20B, 0)
        END), S.RO20T,
        (CASE
          WHEN NVL(S.RO21B, 0) <> 0 AND NVL(S.RO21NT, 0) <> 0 AND
               NVL(S.RO21NT, 0) = NVL(S.RO21T, 0) THEN
           NVL(S.RO21NB, 0)
          ELSE
           NVL(S.RO21B, 0)
        END), S.RO21T,(CASE
          WHEN NVL(S.RO11B, 0) <> 0 AND NVL(S.RO11NT, 0) <> 0 AND
               NVL(S.RO11NT, 0) = NVL(S.RO11T, 0) THEN
           NVL(S.RO11NB, 0)
          ELSE
           NVL(S.RO11B, 0)
        END), S.RO11T,
        (CASE
          WHEN NVL(S.RO19B, 0) <> 0 AND NVL(S.RO19NT, 0) <> 0 AND
               NVL(S.RO19NT, 0) = NVL(S.RO19T, 0) THEN
           NVL(S.RO19NB, 0)
          ELSE
           NVL(S.RO19B, 0)
        END), S.RO19T,(CASE
          WHEN NVL(S.RO9B, 0) <> 0 AND NVL(S.RO9NT, 0) <> 0 AND
               NVL(S.RO9NT, 0) = NVL(S.RO9T, 0) THEN
           NVL(S.RO9NB, 0)
          ELSE
           NVL(S.RO9B, 0)
        END), S.RO9T,(CASE
          WHEN NVL(S.RO5B, 0) <> 0 AND NVL(S.RO5NT, 0) <> 0 AND
               NVL(S.RO5NT, 0) = NVL(S.RO5T, 0) THEN
           NVL(S.RO5NB, 0)
          ELSE
           NVL(S.RO5B, 0)
        END), S.RO5T, S.RO24NB, S.RO24NT, S.RO20NB, S.RO20NT, S.RO21NB, S.RO21NT, S.RO11NB, S.RO11NT, S.RO19NB, S.RO19NT, S.RO9NB, S.RO9NT, S.RO5NB, S.RO5NT, S.ROTI, S.CESCDD1, S.CESCDD2, S.CEOPTR, S.CESVDD, S.CESVFDD, S.CESVFS, S.WRSCDD, S.WRSCDDAB, S.WRSCDDCD, S.FODD, S.FOFDD, S.WRSCFDD, S.WRN, S.RORTC24, S.RORTC20, S.RORTC21, S.RORTC11, S.RORTC19, S.RORTC9, S.RORTC5, S.COD, S.ID_SUCURSALA);

  END SCRIE_JV_2007;