CREATE OR REPLACE PACKAGE "PACK_CONTAFIN" is

  -- Author  : GEORGIANA.VOICU
  -- Created : 14.10.2004 11:35:49
  -- Purpose : SCRIE,STERGE DIN FISIERE

  -- Public type declarations
  --type <TypeName> is <Datatype>;

  -- Public constant declarations
  --<ConstantName> constant <Datatype> := <Value>;

  -- Public variable declarations
  --<VariableName> <Datatype>;

  -- Public function and procedure declarations
  --function <FunctionName>(<Parameter> <Datatype>) return <Datatype>;
  type tip_ref_cursor is ref cursor;

  nCod                     act_temp.cod%Type;
  dDataOra                 act_temp.dataora%Type;
  nIdFact                  act_temp.id_fact%Type;
  nIdUtil                  act_temp.id_util%Type;
  nAnRon                   act_temp.an%Type;
  nLunaRon                 act_temp.luna%Type;
  nScriere                 number(1) := 0;
  nRefacere                number(1) := 1;
  nStergere                number(1) := 2;
  nIdSucursala             act_temp.id_sucursala%type;
  nIdFirma                 act_temp.id_sucursala%type;
  nSuprascriereCod         number(1) := 1;
  nSuprascriereAnLuna      number(1) := 1;
  nDateCompletate          number(1) := 0; -- se suprascriu cod, an, luna
  nDateCompletateCod       number(1) := 1; -- nu se suprascrie cod
  nDateCompletateCodAnLuna number(1) := 2; -- nu se suprascrie cod, an, luna
  nScriereStoc             number(1) := 1; -- se scrie in stoc, rul

  nTipValidareAutomata number(1) := 1;
  nTipValidareRefacere number(1) := 2;
  nTipValidareManuala  number(1) := 3;

  FUNCTION GENEREAZA_COD(V_GCS VARCHAR2) RETURN ACT_TEMP.COD%TYPE;
  PROCEDURE SET_COD(V_GCS IN VARCHAR2);
  FUNCTION GET_COD return ACT_TEMP.COD%TYPE;

  PROCEDURE SET_ID_SUCURSALA(tnIdSucursala act_temp.id_sucursala%type);
  FUNCTION GET_ID_SUCURSALA RETURN act_temp.id_sucursala%type;

  PROCEDURE SET_ID_FIRMA(tnIdFirma act_temp.id_sucursala%type);
  FUNCTION GET_ID_FIRMA RETURN act_temp.id_sucursala%type;
  /*  PROCEDURE SET_IDFACT(tdDataAct   ACT_TEMP.DATAACT%TYPE,
  tcSerie_Act ACT_TEMP.SERIE_ACT%TYPE,
  tnNrAct     ACT_TEMP.NRACT%TYPE,
  tnId_Ctr    ACT_TEMP.ID_CTR%TYPE);*/
  PROCEDURE SET_IDFACT(V_GCS IN VARCHAR2);
  FUNCTION GET_IDFACT return ACT_TEMP.ID_FACT%TYPE;

  PROCEDURE SET_DATAORA(V_DATAORA IN ACT_TEMP.DATAORA%TYPE);
  FUNCTION GET_DATAORA RETURN ACT_TEMP.DATAORA%TYPE;

  PROCEDURE SET_IDUTIL(tnIdUtil act_temp.id_util%Type);
  FUNCTION GET_IDUTIL RETURN ACT_TEMP.ID_UTIL%TYPE;

  PROCEDURE SET_DATA_RON(V_GCS VARCHAR2);
  FUNCTION GET_ANRON RETURN ACT_TEMP.AN%TYPE;
  FUNCTION GET_LUNARON RETURN ACT_TEMP.LUNA%TYPE;

  function getCotaTVAStandard(V_LUNA IN NUMBER, V_AN IN NUMBER) return number;

  PROCEDURE SETARE_FACTURA(tcS VARCHAR2); --RETURN NUMBER;

  FUNCTION LISTA_CAMPURI(V_GCS VARCHAR2, tcTableName IN VARCHAR2)
    RETURN VARCHAR2;

  FUNCTION SCRIE_IN_FISIERE RETURN NUMBER;

  PROCEDURE SCRIE_IN_ACT(V_GCS VARCHAR2);

  FUNCTION GETVALIDARE(tcTabel      tabele_validare.tabel%type DEFAULT NULL,
                       tnAn         tabele_validare.an%type DEFAULT NULL,
                       tnLuna       tabele_validare.luna%type DEFAULT NULL,
                       tcCont       tabele_validare.cont%type DEFAULT NULL,
                       tnIdGestiune tabele_validare.id_gestiune%type DEFAULT NULL)
    RETURN NUMBER;

  PROCEDURE VALIDARE_INVALIDARE(tnTipValidare tabele_validare.tip_validare%type DEFAULT NULL,
                                tcTabel       tabele_validare.tabel%type DEFAULT NULL,
                                tnAn          tabele_validare.an%type DEFAULT NULL,
                                tnLuna        tabele_validare.luna%type DEFAULT NULL,
                                tcCont        tabele_validare.cont%type DEFAULT NULL,
                                tnIdGestiune  tabele_validare.id_gestiune%type DEFAULT NULL);

  PROCEDURE VALIDAREINREGISTRARI(tnAn     IN act.an%type,
                                 tnLuna   act.luna%type,
                                 tnIdUtil act.id_Utilv%type,
                                 tnCod    act.cod%type);

  procedure EXECUTA_SCRIE_TVA(V_GCS         VARCHAR2,
                              tcNumeTabel   varchar2,
                              tnProcTvaStR  number,
                              tnProcTvaRedR number,
                              tcCorespDTva  varchar2,
                              tcCorespDBaza varchar2,
                              tcCorespCTva  varchar2,
                              tcCorespCBaza varchar2,
                              tcContTva     varchar2,
                              tcExceptii    varchar2,
                              tnAn          number,
                              tnLuna        number,
                              tnScrie       number);

  procedure SCRIE2_TVA(V_GCS         VARCHAR2,
                       tcNumeTabel   varchar2,
                       tnProcTvaStR  number,
                       tnProcTvaRedR number,
                       tcCorespDTva  varchar2,
                       tcCorespDBaza varchar2,
                       tcCorespCTva  varchar2,
                       tcCorespCBaza varchar2,
                       tcContTva     varchar2,
                       tcExceptii    varchar2,
                       tnAn          number,
                       tnLuna        number,
                       tnScrie       number,
                       tnIdFact      number,
                       tnNrAct       number,
                       tdDataAct     date);

  PROCEDURE STERGE_DIN_ACT(V_GCS      VARCHAR2,
                           tnAn       in number,
                           tnLuna     in number,
                           tnCod      in number,
                           tnId_utils in number,
                           tnTip      IN NUMBER);

  PROCEDURE STERGE_DIN_RUL(V_GCS      VARCHAR2,
                           tnAn       in number,
                           tnLuna     in number,
                           tnCod      in number,
                           tnId_utils in number);

  PROCEDURE STERGE_DIN_RUL_OBINV(V_GCS      VARCHAR2,
                                 tnAn       in number,
                                 tnLuna     in number,
                                 tnCod      in number,
                                 tnId_utils in number);

  PROCEDURE SCRIE_IN_RUL(tcS IN VARCHAR2);

  PROCEDURE SCRIE_IN_RUL_OBINV(tcS IN VARCHAR2);

  PROCEDURE SCRIE_IN_STOC_OLD(tcS        IN VARCHAR2,
                              tnAn       in number,
                              tnLuna     in number,
                              tnScrie    number,
                              tcTabel    in varchar2,
                              tcIdTabel  in varchar2,
                              tcCondSpec in varchar2);

  PROCEDURE SCRIE_IN_STOC(tcS        IN VARCHAR2,
                          tnAn       in number,
                          tnLuna     in number,
                          tnScrie    number,
                          tcTabel    in varchar2,
                          tcIdTabel  in varchar2,
                          tcCondSpec in varchar2);

  PROCEDURE SCRIE_IN_STOC(tnAn         in number,
                          tnLuna       in number,
                          tnScrie      number,
                          tnIdGestiune IN RUL.ID_GESTIUNE%TYPE DEFAULT NULL);

  PROCEDURE SCRIE_IN_STOC_OBINV_OLD(tcS        IN VARCHAR2,
                                    tnAn       in number,
                                    tnLuna     in number,
                                    tnScrie    number,
                                    tcTabel    in varchar2,
                                    tcIdTabel  in varchar2,
                                    tcCondSpec in varchar2);

  PROCEDURE SCRIE_IN_STOC_OBINV(tcS        IN VARCHAR2,
                                tnAn       in number,
                                tnLuna     in number,
                                tnScrie    number,
                                tcTabel    in varchar2,
                                tcIdTabel  in varchar2,
                                tcCondSpec in varchar2);

  PROCEDURE SCRIE_IN_STOC_OBINV(tnAn    in number,
                                tnLuna  in number,
                                tnScrie number);

  PROCEDURE set_suprascriereCod(V_VALOARE IN NUMBER);

  PROCEDURE set_suprascriereAnLuna(V_VALOARE IN NUMBER);

  PROCEDURE RESET_VARIABLES;

  procedure SCRIE_JV_2007(tnAn    JV2007.AN%TYPE,
                          tnLuna  JV2007.LUNA%TYPE,
                          tnScrie number);
  procedure SCRIE_JC_2007(tnAn    JC2007.AN%TYPE,
                          tnLuna  JC2007.LUNA%TYPE,
                          tnScrie number);
  Procedure COMPLETEAZA_JV_2007(tnAn            IN JV2007.AN%TYPE,
                                tnLuna          IN JV2007.LUNA%TYPE,
                                tcTabelSursa    IN VARCHAR2,
                                tcCondSucursala IN VARCHAR2);
  Procedure COMPLETEAZA_JC_2007(tnAn            IN JC2007.AN%TYPE,
                                tnLuna          IN JC2007.LUNA%TYPE,
                                tcTabelSursa    IN VARCHAR2,
                                tcCondSucursala IN VARCHAR2);
  Procedure COMPLETEAZA_IREG_PARTENERI(tnAn            in IREG_PARTENERI.AN%TYPE,
                                       tnLuna          in IREG_PARTENERI.LUNA%TYPE,
                                       tcTabelSursa    IN VARCHAR2,
                                       tcCondSucursala IN VARCHAR2,
                                       tcCondInPlus    IN VARCHAR2,
                                       tcCont          IN VARCHAR2,
                                       tnIdPart        IN NUMBER);

  FUNCTION VERIFICA_NOTE_CONTABILE(tlVerificAnalitic IN NUMBER) RETURN NUMBER;

  PROCEDURE SCRIE_IN_BALANTA(tcS     in varchar2,
                             tnAn    in BAL.AN%TYPE,
                             tnLuna  in BAL.LUNA%TYPE,
                             tnScrie in number);

  PROCEDURE SCRIE_IN_BALANTA_PARTENERI(tcs      in varchar2,
                                       tnAn     in BALANTA_PARTENERI.AN%TYPE,
                                       tnLuna   in BALANTA_PARTENERI.LUNA%TYPE,
                                       tcCont   in BALANTA_PARTENERI.CONT%TYPE,
                                       tnScrie  in number,
                                       tnIdPart in BALANTA_PARTENERI.ID_PART%TYPE DEFAULT NULL);

  PROCEDURE SCRIE_IN_IREG_PARTENERI(tnAn     in IREG_PARTENERI.AN%TYPE,
                                    tnLuna   in IREG_PARTENERI.LUNA%TYPE,
                                    tcCont   in IREG_PARTENERI.CONT%TYPE,
                                    tnScrie  in number,
                                    tnIdPart in IREG_PARTENERI.ID_PART%TYPE DEFAULT NULL);

  PROCEDURE EXECUTA_SCRIE_IN_IREG(tcS        VARCHAR2,
                                  tnAn       in number,
                                  tnLuna     in number,
                                  tcCont     in varchar2,
                                  tnScrie    number,
                                  tnVenChelt number);

  PROCEDURE STERGE_TEMP_ACTRUL;

  Procedure INIT_SCRIERE_ACT_RUL_LOCAL(tnIdUtil          SYN_UTILIZATORI.ID_UTIL%TYPE,
                                       tnAn              ACT.AN%TYPE,
                                       tnLuna            ACT.LUNA%TYPE,
                                       tnSuprascriereCod NUMBER,
                                       tnScrieSterge     NUMBER,
                                       tnIdSucursala     ACT.ID_SUCURSALA%TYPE);

  PROCEDURE INITIALIZEAZA_SCRIERE_ACT_RUL(tnIdUtil             ACT.ID_UTIL%TYPE,
                                          tdDataOra            ACT.DATAORA%TYPE,
                                          tnAn                 ACT.AN%TYPE,
                                          tnLuna               ACT.LUNA%TYPE,
                                          tnSuprascriereCod    NUMBER,
                                          tnSuprascriereAnLuna NUMBER,
                                          tnScrieSterge        NUMBER,
                                          tnIdSucursala        ACT.ID_SUCURSALA%TYPE);

  procedure sterge_document(tnCod    in number,
                            tnAn     in number,
                            tnLuna   in number,
                            tnIdUtil in number);
  procedure sterge_document(V_cod in number);

  procedure finalizeaza_document_verif(tnAn                 IN NUMBER,
                                       tnLuna               IN NUMBER,
                                       tnScrieSterge        IN number,
                                       tnDateCompletate     IN number,
                                       tnScadereStoc        IN number,
                                       tnCod                IN ACT.COD%type,
                                       tnIdUtil             IN ACT.ID_UTIL%TYPE,
                                       tdDataOra            IN ACT.DATAORA%TYPE,
                                       tnIdSucursala        IN ACT.ID_SUCURSALA%TYPE,
                                       tcSirModificariAcont IN VARCHAR2,
                                       tcSirModificariPart  IN VARCHAR2,
                                       CURSOR_VERIFICARE    OUT PACK_TYPES.tip_cursor);

  procedure finalizeaza_document_compl(V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                       V_SIR_MODIFICARI_PART  IN VARCHAR2);

  PROCEDURE cursor_verificare(V_CURSOR_VERIFICARE OUT PACK_TYPES.tip_cursor);

  Procedure final_scriere_act_rul_local(tnIdUtil            SYN_UTILIZATORI.ID_UTIL%TYPE,
                                        tnAn                ACT.AN%TYPE,
                                        tnLuna              ACT.LUNA%TYPE,
                                        tnCod               ACT.COD%TYPE,
                                        tnScrieSterge       NUMBER,
                                        tnModificareNota    NUMBER,
                                        tcMesajRefacereNota OUT VARCHAR2);

  procedure finalizeaza_scriere_act_rul;

  procedure finalizeaza_scriere_act_rul(tnIdUtil            SYN_UTILIZATORI.ID_UTIL%TYPE,
                                        tnCod               ACT.COD%TYPE,
                                        tnScrieSterge       NUMBER, -- 0 = scriere, 1 = refacere, 2 = stergere
                                        tnModificareNota    NUMBER, -- 0 = modificare, 1 = stergere
                                        tnScrieCumpVanz     NUMBER, -- 0 = cump + vanz, 1 = cumparari, 2 = vanzari
                                        tcMesajRefacereNota OUT VARCHAR2);

  PROCEDURE finalizeaza_modificare_nota(tnLuna    IN NUMBER,
                                        tnAn      IN NUMBER,
                                        tdDataOra IN DATE,
                                        tnIdSet   IN NUMBER,
                                        tnCod     IN NUMBER,
                                        tnIdFact  IN NUMBER,
                                        tnIdFactD IN NUMBER,
                                        tnIdUtil  IN NUMBER);

  PROCEDURE finalizeaza_stergere_nota(tnLuna    IN NUMBER,
                                      tnAn      IN NUMBER,
                                      tdDataOra IN DATE,
                                      tnIdSet   IN NUMBER,
                                      tnCod     IN NUMBER,
                                      tnIdFact  IN NUMBER,
                                      tnIdFactD IN NUMBER,
                                      tnIdUtil  IN NUMBER);

  PROCEDURE completeaza_analitice;
  PROCEDURE completeaza_analitice_rul;
  PROCEDURE completeaza_parteneri_gestiuni;
  PROCEDURE sterge_analitice_gresite;
  PROCEDURE sterge_analitice_gresite_rul;

  FUNCTION verifica_id_documente(lcSirIdFact IN VARCHAR2) RETURN VARCHAR2;

  function GetRulajAuxiliarFurnizor(tnIdFurnizor rul_auxiliar.id_furnizor%type)
    return rul_auxiliar.id_rul_aux%type;
  procedure GetRulajAuxiliarFurnizor(tnIdFurnizor IN rul_auxiliar.id_furnizor%type,
                                     tnIdRulAux   OUT rul_auxiliar.id_rul_aux%type);

end PACK_CONTAFIN;
/
CREATE OR REPLACE PACKAGE BODY "PACK_CONTAFIN" is
  -- 11.02.2010
  -- marius.mutu
  -- scrie_in_ireg_parteneri
  -- nu se mai ia in considerare la selectia de facturi nota 419 = 4111 cu documente pereche (regularizare avans)
  -- nota se ia in considerare la incasari/plati

  -- 23.02.2010
  -- marius.mutu
  -- scrie_in_rul, scrie_in_rul_obinv
  -- nu se mai suprascrie id_sucursala pe inregistrarile deja completate

  -- 11.03.2010
  -- marius.mutu
  -- scrie_in_ireg_parteneri
  -- la incasari/plati se lua max(curs) pentru reevaluarea soldurilor in valuta (id_set 90501)
  -- am considerat cursul 0 pentru operatiile <> 90501, cursul din 90501 putea sa fie mai mic decat cursul de pe o operatie din luna

  -- 17.05.2010
  -- marius.mutu
  -- REFAC_STOC, SCRIE_IN_STOC - REFACERE PENTRU FIECARE GESTIUNE IN PARTE

  -- 15.07.2010
  -- marius.mutu
  -- SCRIE_JV_2007, SCRIE_JC_2007 - COTA TVA 24%

  -- 30.06.2011
  -- marius.mutu
  -- SCRIE_IN_BALANTA_PARTENERI, SCRIE_IN_IREG_PARTENERI - parametru ID_PART (scriere/refacere pentru un singur partener)

  -- 08.11.2012
  -- marius.mutu
  -- SCRIE_JC_2007
  -- s-a introdus coloana ROTN (limitare deducere TVA 4426 id_jtva_coloana = 1001)
  -- partenerul pentru nota x = 4426 id_jtva_coloana = 1001, introdusa in afara pachetului de note de factura achizitie,
  -- se ia din factura cu max(id_part) dupa nract, dataact, id_fdoc in loc de nract, dataact, id_fdoc, cod

  -- 12.02.2013
  -- marius.mutu
  -- SCRIE_JV_2007, SCRIE_JC_2007
  -- TVA_INCASARE - CORECTIE SUMA PLATITA

  -- 13.02.2013
  -- marius.mutu
  -- SCRIE_IN_IREG_PARTENERI
  -- LA INREGISTRAREA 408 AM ID_JTVA_COLOANA RO09BFB, RO09BVB...., IAR LA REGULARIZARE 408 POT SA AM RO09NB (TVA INCASARE NEEXIGIBIL)
  -- SI NU SE MAI PUPA INREGISTRARILE SI DUPA ID_JTVA_COLOANA
  -- INCERC SA LE PUP DOAR DUPA PROC_TVA (STIU CA POATE SA SARA IN AER) :)

  -- 26.03.2013
  -- marius.mutu
  -- SCRIE_IN_IREG_PARTENERI
  -- LA INREGISTRAREA 408 POT SA AM O 2 REGULARIZARI 408 = 401 CU ACELASI PROC_TVA, DAR ID_JTVA_COLOANA DIFERITE (EX: ACH INT 24, ACH INT NEEX 24)
  -- NU STIU DE CE AR EXISTA SITUATIA, DAR S-A INTAMPLAT
  -- DA EROARE LA MERGE (NU SE POATE OBTINE UN SET DE RANDURI STABILE IN TABELA SURSA)
  -- AM FACUT GRUPAREA DOAR DUPA PROC_TVA, IAR ID_JTVA_COLOANA IL FAC MAX(ID_JTVA_COLOANA)

  -- 18.04.2013
  -- marius.mutu
  -- + procedura finalizeaza_document_verif (initializeaza_act_rul + finalizeaza_document + cursor_verificare)
  -- + cursor_verificare
  -- + completeaza_analitice
  -- + completeaza_analitice_rul
  -- + completeaza_parteneri_gestiuni
  -- + sterge_analitice_gresite
  -- + sterge_analitice_gresite_rul

  -- 09.08.2013
  -- marius.mutu
  -- STERGE_DIN_ACT
  -- Nu mai sterg din DOCUMENTE
  -- Cand se sterge o plata cu o inregistrare TVA incasare (ID_FACT TVA INCASARE = ID_FACT FACTURA ORIGINALA)
  -- se marcheaza documentul sters si nu se mai pot face plati ulterioare pe factura respectiva
  -- mesaj: Documentul nu exista in baza de date

  -- 12.11.2013
  -- marius.mutu
  -- STERGE_DIN_ACT - nu se sterg din documente id_set 90501 reevaluare sold in valuta la 31.LL

  -- 05.12.2013
  -- marius.mutu
  -- SCRIE_IN_STOC - s-a reparat datain, dataout - lua max(r.dataact) in loc de max(r.datain) si max(r.dataout)

  -- 27.02.2014
  -- marius.mutu
  -- SCRIE_IN_ACT - nu completez ID_FACTD, ID_FACTC cu ID_FACT-ul documentului curent
  -- la factura + chitanta cu acelasi NRACT, se completa ID_FACTC cu ID_FACT chitanta

  -- 12.03.2014
  -- marius.atanasiu
  -- SCRIE_IN_ACT : daca am completat id_factd / id_factc pe o inregistrare noua ( cu id_act = -1 ),
  -- il suprascriu in caz ca gasesc un alt document cu acelasi numar, dar cu serie document diferita

  -- 04.06.2014
  -- marius.mutu
  -- SCRIE_JC_2007 : s-a introdus contul 4511,462 in lista conturilor creditoare pentru regularizarea 408 = 401/404/4511/462

  -- 05.06.2014
  -- marius.mutu
  -- SCRIE_JV_2007 : s-a introdus contul 461,4511,4118 in lista conturilor creditoare pentru regularizarea 4111/461/4511/4118 = 418

  -- 26.11.2014
  -- marius.mutu
  -- SET_DATA_RON - tratare no_data_found

  -- 17.04.2015
  -- marius.mutu
  -- finalizeaza_scriere_act_rul: Optiune SETURI_SITUATII_NU_SCRIE: LISTA ID_SET PENTRU CARE NU SE APELEAZA SCRIEREA IN SITUATII (25052|BV,BP,IP,TVA,STOC)

  -- 08.10.2015
  -- marius.mutu
  -- scrie_jc2007, scrie_jv2007 -- nu se iau in considerare operatiile 10621, 10622 - compensari 419, 409

  -- 15.12.2015
  -- marius.mutu
  -- getCotaTVAStandard - 20% din 01/2016
  -- SCRIE_JC_2007, SCRIE_JV_2007 cota tva 20%

  -- 11.03.2016
  -- marius.mutu
  -- SCRIE_JV_2007 - corectie JV2007.RO5NB/RO5NT - se suprascria eronat cu RO5B/RO5NT din luna curenta in loc de RO5NB/RO5NT, pentru facturile preluate din luna precedenta

  -- 21.04.2016
  -- marius.mutu
  -- completeaza analitice
  -- La vanzarea de marfuri la pret de vanzare 4428 = 371 are completata ID_GESTOUT
  -- functioneaza pentru contul 371 pe credit, dar nu pentru contul 4428 pe debit
  -- mai repet o data completarea analiticelor si cu ID_GESTIN/ID_GESTOUT pentru analiticele ramase necompletate

  -- 16.06.2016
  -- marius.mutu
  -- + VALIDARE_INVALIDARE - intretinere invalidare tabel (BAL, BALANTA_PARTENERI, IREG_PARTENERI, JV2007, JC2007, +STOC, + STOC_OBINV) an, luna, cont
  -- pentru a se da refaceri/redeschideri automate doar la situatiile invalidate
  -- ex: la CONPRESS se face redeschidere de luna la ultimele 4 luni deschise. Nu este nevoie de refaceri la toate conturile si la toate 4 lunile daca nu s-a modificat nimic in lunile anterioare
  -- + GETVALIDARE - intoarce situatia validarii unei luni

  -- 11.07.2016
  -- marius.mutu
  -- VALIDARE_INVALIDARE - se invalideaza si situatiile din luna operatiei, daca ID_SET nu se scrie in anumite tabele (bv, bp, ip, tva), ex: bon fiscal conpress
  -- daca nu se marcheaza situatiile invalidate, nu se refac, desi nu sunt scrise

  -- 27.10.2016
  -- marius.muut
  -- COMPLETEAZA_JC_2007, COMPLETEAZA_JV_2007, SCRIE_JC_2007, SCRIE_JV_2007 + DATAIREG

  -- 03.11.2016
  -- marius.mutu
  -- SCRIE_JC_2007, SCRIE_JV_2007 - DOCUMENTE.DATAIREG

  -- 29.12.2016
  -- marius.mutu
  -- getCotaTVAStandard - 19% din 01/2017
  -- SCRIE_JC_2007, SCRIE_JV_2007 - TVA NEEX. 19% de la 01/2017

  -- 25.01.2017
  -- marius.mutu
  -- SCRIE_JC_2007 +CE19SB, CE19ST

  -- 07.02.2017
  -- marius.mutu
  -- SCRIE_JC_2007, SCRIE_JV_2007 - ID_FDOC act/documente - si dupa coloanele id_set, serie_act
  -- s-a intamplat sa am tva exigibilizat la plata cu acelasi nr. doc si data document ca la factura
  -- si sa puna id_fdoc de la nota tva exigibilizat (ordin plata) in loc de factura

  -- 09.02.2017
  -- marius.mutu
  -- scrie_jc_2007 - calcul baza exigibila 19% pentru exigibilizare TVA incasare

  -- 23.03.2017
  -- marius.mutu
  -- scrie_jc_2007 - calcul totctva din regularizare 408 = 401 cu TVA incasare 19%

  -- scrie_jv_2007 - calcul totctva din regularizare 4111 = 418 cu TVA incasare si exigibilizare TVA - se scadea TVA de 3 ori

  -- 24.06.2017
  -- marius.mutu
  -- SCRIE_IN_IREG_PARTENERI: ACONT SE IA IN FUNCTIE DE CONTUL DEBITOR-ACTIV/CREDITOR-PASIV + EXCEPTIE
  -- NU TRATA CORECT NOTA 462.3 = 462.9. PUNEA ANALITICUL 3 IN LOC DE 9

  -- sterge_document. actualizat procedura cu pack_contafin.finalizeaza_stergere_nota
  -- pentru folosire in ROAGEST corectie NIR/Bon

  -- 28.09.2017
  -- marius.mutu
  -- SCRIE_IN_RUL
  -- Completare RUL.ID_RUL_AUX SI RUL_AUX.ID_FURNIZOR la intrari daca este setata optiunea

  -- 06.10.2017
  -- marius.mutu
  -- + procedure GetRulajAuxiliarFurnizor

  -- 11.01.2018
  -- marius.mutu
  -- STERGE_DIN_ACT - hint IDX_COD

  -- 22.05.2018
  -- marius.mutu
  -- SCRIE_JC_2007: corectare totftvatax pentru 19% regularizare 408=401

  -- 20.06.2018
  -- marius.mutu
  -- SCRIE_JV_2007: corectare totctva pentru 19% regularizare 4111=418

  -- 20.07.2018
  -- marius.mutu
  -- COMPLETEAZA_IREG_PARTENERI - se iau din ireg_parteneri ultima inregistrare din luna < decat luna curenta, ultima luna diferita de luna curenta

  -- 23.01.2020
  -- marius.mutu
  -- STERGE_DIN_ACT - nu mai sterg din documente pentru id_Set = 90021 inchidere 461 = 542, pentru ca are acelasi id_fact ca si 542.id_factc si se sterg id_docurile 542

  -- 23.06.2020
  -- marius.mutu
  -- SCRIE_JC_2007, SCRIE_JV_2007 nvl(scd/scc) la selectia din act.
  -- este posibil ca scd/scc sa fie null si sa aiba id_jtva_coloana completat si nu sunt luate in considerare

  -- 09.07.2020
  -- marius.mutu
  -- VALIDARE_INVALIDARE + ID_UTIL_VALIDARE, DATAORA_VALIDARE

  -- 08.02.2021
  --  SCRIE_IN_STOC_OBINV - se pot scrie si alte conturi in afara de 8039 in stoc_obinv

  -- 08.06.2021
  -- SCRIE_IN_STOC_OBINV - corectie selectie MERGE GROUP BY nvl(id_rul_aux)

  -- 25.01.2022
  -- SCRIE_JC_2007 - + xx19ti, ROTN50, ROTN100

  -- 06.03.2022
  -- SCRIE_JC_2007 + CE19CTB, CE19CTT,CE9CTB, CE9CTT,CE5CTB, CE5CTT, RORTD24..RORTD5
  -- SCRIE_JV_2007 + RORTC24..RORTC5

  -- 24.03.2022
  -- SCRIE_IN_ACT. ID_SUCURSALA, TAXCODE, PAYMENTCODE = NULL IN LOC DE 0

  -- 05.04.2022
  -- SCRIE_JV_2007. TRATARE REGULARIZARE 461 = 418

  -- 18.07.2023
  -- SCRIE_IN_STOC_OBINV + stoc_obinv.lot

  -- 31.08.2023
  -- SCRIE_JV_2007 + WRSCDDAB, WRSCDDCD

  -- 12.05.2025
  -- SCRIE_JC_2007 + XX19TIB

  -- 01.08.2025
  -- getCotaTVAStandard - obtinere cota TVA STANDARD din COTE_TVA, COTA 21% din 08/2025

  -- 05.08.2025
  -- SCRIE_JC_2007, SCRIE_JV_2007 COTE TVA 21% SI 11% din 08/2025

  -- 19.08.2025
  -- SCRIE_JC_2007 - corectie TOTAL CU TVA

  -- 01.09.2025
  -- getCotaTVAStandard - corectat pentru deschidere de luna

  -- 23.09.2025
  -- SCRIE_JC_2007
  -- BUG ORACLE XE 11: [Oracle][ODBC][Ora]ORA-00600: cod de eroare intern?, argumente: [13013], [5001], [80451], [21241702], [11], [21241702], [17], [], [], [], [], []
  -- Pentru incasarile/platile cu TVA la incasare
  -- NO_QUERY_TRANSFORMATION dezactiveaza TOATE transformarile (view merging, subquery unnesting, query rewrite, etc.)

  -- 01.10.2025
  -- SCRIE_JC_2007
  -- NO_QUERY_TRANSFORMATION nu stiu de ce nu facea corect (nu scadea valoarea bazei) in cazul discount-urilor 401 = 767
  -- MERGE replaced with FORALL BULK operations to avoid Oracle XE bugs and improve performance

  -- 17.10.2025
  -- SCRIE_JV_2007 - CORECTARE RO21B nu se scadea TVA regularizare 418

  -- 28.10.2025
  -- SCRIE_IN_RUL COMPLETARE ID_FACT PENTRU ACHIZITIE DIN IMPORT SI ACHIZITIE+PLATA
  -- LA ACHIZITIE DIN IMPORT SE PUNEA MIN(ID_FACT) SI NU ERA INTOTDEAUNA FACTURA DE ACHIZITIE

  ---------------------------------------------------------------
  FUNCTION SCRIE_IN_FISIERE RETURN NUMBER IS
    V_RETURN NUMBER := 0;

  BEGIN
    RETURN V_RETURN;
  END;
  ---------------------------------------------------------------
  PROCEDURE SCRIE_IN_ACT(V_GCS VARCHAR2) IS
    LN_COD          NUMBER(20) := 0;
    lnIdFact        NUMBER(20) := 0;
    LD_DATAORA      DATE;
    LC_INSERT       VARCHAR2(10000) := '';
    V_LISTA_CAMPURI VARCHAR2(2000) := '';
    lnID_Util       act_temp.id_util%Type := 0;
    LNAN            ACT_TEMP.AN%TYPE;
    LNLUNA          ACT_TEMP.LUNA%TYPE;
    lnAutoValidare  number(10) := 0;
    lnBlocatROACONT NUMBER(1) := 0;
    lnBlocatROAGEST NUMBER(1) := 0;
    lnInchis        number(1) := 0;
    lnIdSucursala   act_temp.id_sucursala%type;
    lnIdFirma       act_temp.id_sucursala%type;
    lnTvaIncasare   act_temp.tva_incasare%type;
  BEGIN
    V_LISTA_CAMPURI := pack_contafin.LISTA_CAMPURI(V_GCS, 'ACT');
    LN_COD          := pack_contafin.GET_COD();
    LD_DATAORA      := pack_contafin.GET_DATAORA();
    LNID_UTIL       := pack_contafin.GET_IDUTIL();
    LNAN            := pack_sesiune.GETAN();
    LNLUNA          := pack_sesiune.GETLUNA();
    lnIdSucursala   := pack_contafin.GET_ID_SUCURSALA();
    lnIdFirma       := pack_contafin.GET_ID_FIRMA();

    -- VERIFIC DACA LUNA ESTE BLOCATA
    PACK_FIRMA.IS_LUNA_BLOCATA(user,
                               pack_sesiune.nIdeROACONT,
                               lnAn,
                               lnLuna,
                               lnBlocatROACONT);
    --    PACK_FIRMA.IS_LUNA_BLOCATA(tcS, pack_sesiune.nIdeROAGEST, tnAn, tnLuna,  lnBlocatROAGEST);
    lnInchis := lnBlocatROACONT + lnBlocatROAGEST;

    if lnInchis = 1 then
      RAISE_APPLICATION_ERROR(-20000, 'LUNA CONTABILA ESTE BLOCATA');
    end if;

    -- SCRIE IN GCS.ACT_TEMP  ---------------------------
    PACK_CONTAFIN.SETARE_FACTURA(V_GCS);

    -- 12.03.2014 : pun ID_ACT -1 ca sa stiu care sunt inregistrarile noi atunci cand completez id_factd si id_factc
    update ACT_TEMP set ID_ACT = -1 where id_fact = -1;

    -- cursor crsFacturi is  select distinct nract, dataact, dataireg from act_temp where id_fact = -1 ;

    -- 03.10.2007
    -- MARIUS.MUTU
    -- selectez nract, dataact in ordinea fizica (sper!!!!!) - astfel incat id_fact-urile generate sa fie in aceeasi ordine
    -- pentru ca min(id_fact) se scrie in rull ( vezi nir din import, factura+chitanta)
    /*select distinct nract, dataact
     from act_temp
    where id_fact = -1*/
    -- 12.02.2009
    -- MARIUS.ATANASIU
    -- am adaugat si id_ctr ca sa pot da id_fact diferit pentru fiecare contract,
    -- in caz ca emit o factura pentru mai multe contracte
    for itemfact in (select nract,
                            dataact,
                            dataireg,
                            serie_act,
                            min(rn),
                            id_ctr,
                            min(id_ctr2) as id_ctr2,
                            max(tva_incasare) as tva_incasare,
                            id_set
                       from (select nract,
                                    dataact,
                                    dataireg,
                                    serie_act,
                                    rownum as rn,
                                    decode(NVL(id_ctr, 0),
                                           0,
                                           max(id_ctr) over(partition by nract,
                                                dataact,
                                                dataireg,
                                                serie_act),
                                           id_ctr) as id_ctr,
                                    NVL(id_ctr, 0) as id_ctr2,
                                    tva_incasare,
                                    id_set
                               from act_temp
                              where id_fact = -1)
                      group by nract,
                               dataact,
                               dataireg,
                               serie_act,
                               id_ctr,
                               id_set
                      order by 4) loop
      pack_contafin.set_idfact(V_GCS);
      /* 25.02.2013 : se face cumularea id_fact pe listarea din RC/RJ
      PACK_CONTAFIN.SET_IDFACT(itemfact.dataact,
      itemfact.serie_act,
      itemfact.nract,
      itemfact.id_ctr);*/
      lnIdFact := get_idFact();
      if itemfact.id_ctr = itemfact.id_ctr2 then
        update act_temp
           set id_fact = lnIdFact
         where nract = itemfact.nract
           and dataact = itemfact.dataact
           and dataireg = itemfact.dataireg
           and NVL(serie_act, '+_') = NVL(itemfact.serie_act, '+_')
           and id_ctr = itemfact.id_ctr
           and id_fact = -1;

        -- 12.03.2014 : completez id_factd sau il suprascriu ( in cazul in care l-am completat deja si
        -- mai gasesc un document pereche care are serie_act diferita de a documentului )
        update act_temp
           set id_factd = lnIdFact
         where pereched = itemfact.nract
           and (id_factd = -1 or
               (id_factd <> -1 and id_act = -1 and
               NVL(serie_act, '+_') <> NVL(itemfact.serie_act, '+_')))
           and id_ctr = itemfact.id_ctr;

        -- 12.03.2014 : completez id_factc sau il suprascriu ( in cazul in care l-am completat deja si
        -- mai gasesc un document pereche care are serie_act diferita de a documentului )
        update act_temp
           set id_factc = lnIdFact
         where perechec = itemfact.nract
           and (id_factc = -1 or
               (id_factc <> -1 and id_act = -1 and
               NVL(serie_act, '+_') <> NVL(itemfact.serie_act, '+_')))
           and id_ctr = itemfact.id_ctr;
      else
        update act_temp
           set id_fact = lnIdFact
         where nract = itemfact.nract
           and dataact = itemfact.dataact
           and dataireg = itemfact.dataireg
           and NVL(serie_act, '+_') = NVL(itemfact.serie_act, '+_')
           and NVL(id_ctr, 0) in (itemfact.id_ctr, 0)
           and id_fact = -1;

        -- 12.03.2014 : completez id_factd sau il suprascriu ( in cazul in care l-am completat deja si
        -- mai gasesc un document pereche care are serie_act diferita de a documentului )
        update act_temp
           set id_factd = lnIdFact
         where pereched = itemfact.nract
           and (id_factd = -1 or
               (id_factd <> -1 and id_act = -1 and
               NVL(serie_act, '+_') <> NVL(itemfact.serie_act, '+_')))
           and NVL(id_ctr, 0) in (itemfact.id_ctr, 0);

        -- 12.03.2014 : completez id_factc sau il suprascriu ( in cazul in care l-am completat deja si
        -- mai gasesc un document pereche care are serie_act diferita de a documentului )
        update act_temp
           set id_factc = lnIdFact
         where perechec = itemfact.nract
           and (id_factc = -1 or
               (id_factc <> -1 and id_act = -1 and
               NVL(serie_act, '+_') <> NVL(itemfact.serie_act, '+_')))
           and NVL(id_ctr, 0) in (itemfact.id_ctr, 0);
      end if;

      -- SCRIE IN DOCUMENTE
      lnTvaIncasare := case
                         when itemfact.tva_incasare > 0 then
                          1
                         else
                          0
                       end;
      -- 25.02.2013 : am repus insertul pentru ca se face cumularea id_fact pe listarea din RC/RJ
      INSERT INTO DOCUMENTE
        (ID_DOC,
         ID_UTIL,
         DATAORA,
         TVA_INCASARE,
         SERIE_ACT,
         NRACT,
         DATAACT,
         DATAIREG,
         ID_CTR,
         ID_SET)
      VALUES
        (lnIdFact,
         lnID_Util,
         LD_DATAORA,
         lnTvaIncasare,
         itemfact.serie_act,
         itemfact.nract,
         itemfact.dataact,
         itemfact.dataireg,
         decode(itemfact.id_ctr, 0, NULL, itemfact.id_ctr),
         itemfact.id_set);
      /*
      -- modificare ROACONT v 2.4.0 ( 27.12.2012 ) : am adaugat serie_act, nract, dataact
      -- modificare 21.02.2013 : am adaugat id_ctr
      MERGE INTO DOCUMENTE A
      USING (SELECT lnIdFact as ID_DOC,
                    itemfact.serie_act as SERIE_ACT,
                    itemfact.nract as NRACT,
                    itemfact.dataact as DATAACT,
                    decode(itemfact.id_ctr, 0, NULL, itemfact.id_ctr) AS ID_CTR
               FROM DUAL) B
      ON (A.ID_DOC = B.ID_DOC)
      WHEN NOT MATCHED THEN
        INSERT
          (ID_DOC,
           ID_UTIL,
           DATAORA,
           TVA_INCASARE,
           SERIE_ACT,
           NRACT,
           DATAACT,
           ID_CTR)
        VALUES
          (B.ID_DOC,
           lnID_Util,
           LD_DATAORA,
           lnTvaIncasare,
           B.SERIE_ACT,
           B.NRACT,
           B.DATAACT,
           B.ID_CTR);*/

    end loop;

    update act_temp set id_factd = 0 where id_factd = -1;
    update act_temp set id_factc = 0 where id_factc = -1;
    --  modificare 21.02.2013 : daca s-au modificat serie_act/nract/dataact, atunci trebuie sa modific
    -- si in tabela DOCUMENTE ; am pus merge-ul inainte de completare id_fact pentru notele cu id_fact = -5
    -- ( notele cu 4426-4428/4428-4427 ) deoarece acolo pot sa am serie act/nr.act/data act de la doc. de
    -- plata/incasare si id_fact de la factura si atunci mi-ar pune datele documentului de plata/incasare
    -- pe id_fact-ul facturii in DOCUMENTE
    MERGE INTO DOCUMENTE A
    USING (SELECT DISTINCT ID_FACT AS ID_DOC,
                           SERIE_ACT,
                           NRACT,
                           DATAACT,
                           DATAIREG,
                           MAX(DECODE(ID_CTR, 0, NULL, ID_CTR)) AS ID_CTR
             FROM ACT_TEMP
            WHERE STERS = 0
              AND ID_FACT <> -5
              AND NOT (SCD = '4426' AND SCC = '4428')
              AND NOT (SCD = '4428' AND SCC = '4427')
              AND ID_SET <> 90501
            GROUP BY ID_FACT, SERIE_ACT, NRACT, DATAACT, DATAIREG) B
    ON (A.ID_DOC = B.ID_DOC)
    WHEN MATCHED THEN
      UPDATE
         SET SERIE_ACT = B.SERIE_ACT,
             NRACT     = B.NRACT,
             DATAACT   = B.DATAACT,
             DATAIREG  = B.DATAIREG,
             ID_CTR    = B.ID_CTR,
             STERS     = 0;

    -- modificare ROACONT v 2.4.0 ( 24.12.2012 ) : pe notele 4426-4428 / 4428-4427
    -- care le fac o data cu factura pun id_fact = id_fact_factura
    merge into act_temp a
    using (select distinct nract, id_fact
             from act_temp
            where id_fact <> -5
              and id_factc = 0
              and id_factd = 0) b
    on (decode(a.pereched, 0, a.perechec, a.pereched) = b.nract and ((a.scd = '4426' and a.scc = '4428') or (a.scd = '4428' and a.scc = '4427')))
    when matched then
      update set id_fact = decode(a.id_fact, -5, b.id_fact, a.id_fact);

    -- SCRIE IN ACT_TEMP ----------------
    if nvl(pack_contafin.nSuprascriereCod, 1) = 1 then
      UPDATE ACT_TEMP
         SET COD     = LN_COD,
             DATAORA = LD_DATAORA,
             ID_UTIL = LNID_UTIL,
             AN      = LNAN,
             LUNA    = LNLUNA;
    else
      if NVL(pack_contafin.nSuprascriereAnLuna, 1) = 1 then
        UPDATE ACT_TEMP
           SET DATAORA = LD_DATAORA,
               ID_UTIL = LNID_UTIL,
               AN      = LNAN,
               LUNA    = LNLUNA;
      else
        UPDATE ACT_TEMP SET DATAORA = LD_DATAORA, ID_UTIL = LNID_UTIL;
      end if;
    end if;

    -- 04.04.2008
    -- Nu se suprascrie id_sucursala completat deja in act_temp
    UPDATE ACT_TEMP
       SET ID_SUCURSALA = lnIdSucursala
     WHERE NVL(ID_SUCURSALA, 0) = 0;
    -- 04.04.2008 ^

    -- COMPLETEZ MONEDA NATIONALA DACA ID_VALUTA = 0
    UPDATE ACT_TEMP
       SET ID_VALUTA = pack_def.GetIdMonedaNationala()
     where id_valuta = 0;

    -- Completez NULL campurile care sunt NULL in act
    update act_temp
       set id_sucursala = DECODE(id_sucursala, 0, null, id_sucursala),
           taxcode      = decode(taxcode, 0, null, taxcode),
           paymentcode  = decode(paymentcode, 0, null, paymentcode);

    -- 02.06.2008 USER - PACK_CONTAFIN.ID_FIRMA
    -- 16.11.2007 MARIUS.MUTU
    -- AUTOVALIDARE INREGISTRARI
    /*    SELECT CONTAFIN_ORACLE.PACK_DREPTURI.UtilHasValidare(USER,
                                                       PACK_CONTAFIN.GET_IDUTIL(),
                                                       'AUTOVALIDARE')
    INTO lnAutoValidare
    FROM DUAL;*/

    SELECT CONTAFIN_ORACLE.PACK_DREPTURI.UtilHasValidare(lnIdFirma,
                                                         PACK_CONTAFIN.GET_IDUTIL(),
                                                         'AUTOVALIDARE')
      INTO lnAutoValidare
      FROM DUAL;

    if nvl(lnAutoValidare, 0) > 0 then
      UPDATE ACT_TEMP
         SET VALIDAT  = 1,
             ID_UTILV = PACK_CONTAFIN.GET_IDUTIL(),
             DATAORAV = SYSDATE;
    end if;
    -- 16.11.2007 MARIUS.MUTU ^

    -- SCRIE IN ACT
    LC_INSERT := 'INSERT INTO ' || V_GCS || '.ACT(' || V_LISTA_CAMPURI ||
                 ') (SELECT ' || V_LISTA_CAMPURI || ' FROM ACT_TEMP)';
    EXECUTE IMMEDIATE LC_INSERT;

    -- ACTUALIZARE TABELE_VALIDARE
    PACK_CONTAFIN.VALIDARE_INVALIDARE(nTipValidareAutomata,
                                      NULL,
                                      lnAn,
                                      lnLuna);

  END SCRIE_IN_ACT;
  ---------------------------------------------------------------------------------------

  -- INTOARCE STAREA VALIDARII UNEI SITUATII INTR-O LUNA
  -- PENTRU REFACEREA SITUATIEI RESPECTIVE IN PROCEDURA DESCHIDERE_LUNA
  -- DACA NU GASESC INREGISTRAREA PENTRU LUNA RESPECTIVA, CONSIDER LUNA INVALIDATA
  -- RETURN: 1 = VALIDAT; 0 = INVALIDAT
  FUNCTION GETVALIDARE(tcTabel      tabele_validare.tabel%type DEFAULT NULL,
                       tnAn         tabele_validare.an%type DEFAULT NULL,
                       tnLuna       tabele_validare.luna%type DEFAULT NULL,
                       tcCont       tabele_validare.cont%type DEFAULT NULL,
                       tnIdGestiune tabele_validare.id_gestiune%type DEFAULT NULL)
    return number is
    ldInvalidat tabele_validare.dinvalidat%type;
  begin
    begin
      select DINVALIDAT
        INTO ldInvalidat
        from TABELE_VALIDARE
       WHERE TABEL = tcTabel
         and an = tnAn
         and luna = tnLuna
         and nvl(cont, 'x') = nvl(tcCont, 'x')
         and nvl(id_gestiune, 0) = nvl(tnIdGestiune, 0);
    exception
      when NO_DATA_FOUND then
        ldInvalidat := sysdate; -- daca nu gasesc inregistrari consider luna invalidata
    end;

    return case when ldInvalidat is null then 1 else 0 end;

  end;

  -- ACTUALIZARE TABELE_VALIDARE (BAL, BALANTA_PARTENERI, IREG_PARTENERI, JV2007, JC2007)
  -- INVALIDARE LUNI URMATOARE
  -- SE FOLOSESTE PENTRU REFACEREA DOAR A SITUATIILOR DIN LUNILE INVALIDATE
  PROCEDURE VALIDARE_INVALIDARE(tnTipValidare tabele_validare.tip_validare%type DEFAULT NULL,
                                tcTabel       tabele_validare.tabel%type DEFAULT NULL,
                                tnAn          tabele_validare.an%type DEFAULT NULL,
                                tnLuna        tabele_validare.luna%type DEFAULT NULL,
                                tcCont        tabele_validare.cont%type DEFAULT NULL,
                                tnIdGestiune  tabele_validare.id_gestiune%type DEFAULT NULL) IS
    ldDataAnt           date;
    lnAnAnt             tabele_validare.an%type;
    lnLunaAnt           tabele_validare.luna%type;
    ldInvalidatAnterior tabele_validare.dinvalidat%type;
    lnIdUtil            tabele_validare.id_util_validare%type := pack_contafin.GET_IDUTIL();

    lnAn   act.an%type := tnAn;
    lnLuna act.luna%type := tnLuna;

    lcSetNuScrie      optiuni.varvalue%type;
    lnIdSet           act.id_set%type;
    lcSituatiiNuScrie optiuni.varvalue%type;
    lnScrieBV         number(1) := 1;
    lnScrieBP         number(1) := 1;
    lnScrieIP         number(1) := 1;
    lnScrieTVA        number(1) := 1;
    lnScrieSTOC       number(1) := 1;
  BEGIN

    -- tip_validare: 1 = Validare automata (Operatia este facuta in luna curenta. Situatia este validata automat), 2 = Validare prin refacere (Situatia se valideaza in cazul unei refaceri), 3 = Validare manuala
    IF tnTipValidare = nTipValidareRefacere THEN
      -- se verifica daca luna anterioara este valida: DINVALIDAT = NULL
      -- luna cu refacerea se valideaza doar daca luna anterioara este valida
      ldDataAnt := TO_DATE(to_char(tnAn) || lpad(tnLuna, 2, '0'), 'yyyymm') - 1;
      lnAnAnt   := extract(year from ldDataAnt);
      lnLunaAnt := extract(month from ldDataAnt);
      select max(DINVALIDAT)
        INTO ldInvalidatAnterior
        from TABELE_VALIDARE
       WHERE TABEL = tcTabel
         and an = lnAnAnt
         and luna = lnLunaAnt
         and nvl(cont, 'x') = nvl(tcCont, 'x')
         and nvl(id_gestiune, 0) = nvl(tnIdGestiune, 0);

      -- validare prin refacere
      UPDATE TABELE_VALIDARE
         SET DULTIMA_REFACERE   = SYSDATE,
             DINVALIDAT = case
                            when ldInvalidatAnterior is null then
                             NULL
                            else
                             DINVALIDAT
                          end,
             TIP_VALIDARE = case
                              when ldInvalidatAnterior is null then
                               tnTipValidare
                              else
                               TIP_VALIDARE
                            end,
             ID_UTIL_INVALIDARE = case
                                    when ldInvalidatAnterior is null then
                                     NULL
                                    else
                                     ID_UTIL_INVALIDARE
                                  end,
             EXPLICATIE = case
                            when ldInvalidatAnterior is null then
                             NULL
                            else
                             EXPLICATIE
                          end,
             ID_UTIL_VALIDARE   = lnIdUtil,
             DATAORA_VALIDARE   = SYSDATE
       WHERE TABEL = tcTabel
         and an = tnAn
         and luna = tnLuna
         and nvl(cont, 'x') = nvl(tcCont, 'x')
         and nvl(id_gestiune, 0) = nvl(tnIdGestiune, 0);
    ELSIF tnTipValidare = nTipValidareManuala THEN
      -- validare manuala - nu se face nici o verificare a validarii lunii anterioare pentru ca doresc sa fortez validarea
      UPDATE TABELE_VALIDARE
         SET DINVALIDAT         = NULL,
             TIP_VALIDARE       = tnTipValidare,
             ID_UTIL_INVALIDARE = NULL,
             EXPLICATIE         = NULL,
             ID_UTIL_VALIDARE   = lnIdUtil,
             DATAORA_VALIDARE   = SYSDATE
       WHERE TABEL = tcTabel
         and an = tnAn
         and luna = tnLuna
         and nvl(cont, 'x') = nvl(tcCont, 'x')
         and nvl(id_gestiune, 0) = nvl(tnIdGestiune, 0);
    ELSE
      -- INVALIDARE SITUATII PE BAZA LA INREGISTRARILE DIN ACT_TEMP (SCRIERE/MODIFICARE/STERGERE)
      -- se valideaza automat luna operatiei doar daca operatia este in ultima luna deschisa si daca luna anterioara este validata

      -- LISTA DE SETURI PENTRU CARE NU SE APELEAZA SCRIEREA balanta, balanta parteneri, inregistrari parteneri, tva, stoc
      -- Bonurile fiscale de la chioscuri
      -- Se refac situatiile doar prin refaceri/redeschidere de luna FORTAT PENTRU LUNA DOCUMENTELOR
      lcSetNuScrie := PACK_SESIUNE.getOptiuneFirma('SETURI_SITUATII_NU_SCRIE'); -- '25052|BV,BP,IP,TVA;'
      select max(id_set) into lnIdSet from act_temp;
      IF lcSetNuScrie IS NOT NULL and lnIdSet is not null THEN
        SELECT MAX(UPPER(SITUATII))
          into lcSituatiiNuScrie
          FROM (SELECT GETWORDNUM(SET_SITUATII, 1, '|') AS ID_SET,
                       GETWORDNUM(SET_SITUATII, 2, '|') AS SITUATII
                  FROM (SELECT getwordnum(lcSetNuScrie, B.RAND, ';') AS SET_SITUATII
                          FROM DUAL A,
                               (SELECT ROWNUM AS RAND
                                  FROM USER_OBJECTS
                                 WHERE ROWNUM < 100) B)
                 WHERE SET_SITUATII IS NOT NULL)
         WHERE ID_SET = to_char(lnIdSet);
        if lcSituatiiNuScrie LIKE '%BV%' then
          lnScrieBV := 0;
        end if;
        if lcSituatiiNuScrie LIKE '%BP%' then
          lnScrieBP := 0;
        end if;
        if lcSituatiiNuScrie LIKE '%IP%' then
          lnScrieIP := 0;
        end if;
        if lcSituatiiNuScrie LIKE '%TVA%' then
          lnScrieTVA := 0;
        end if;
        if lcSituatiiNuScrie LIKE '%STOC%' then
          lnScrieSTOC := 0;
        end if;
      END IF;

      MERGE INTO TABELE_VALIDARE A
      USING (select c.tabel,
                    lnAn as an,
                    lnLuna as luna,
                    cal.an as can,
                    cal.luna as cluna,
                    c.cont,
                    c.id_gestiune,
                    case
                      when lnAn = cal.an and lnLuna = cal.luna then
                       sysdate
                      else
                       cast(null as date)
                    end as dultima_operatie,
                    case
                      when (lnAn = cal.an and lnLuna = cal.luna) and
                           (TABEL = 'BAL' AND lnScrieBV = 1) THEN
                       cast(null as date)
                      when (lnAn = cal.an and lnLuna = cal.luna) and
                           (TABEL = 'BALANTA_PARTENERI' AND lnScrieBP = 1) THEN
                       cast(null as date)
                      when (lnAn = cal.an and lnLuna = cal.luna) and
                           (TABEL = 'IREG_PARTENERI' AND lnScrieIP = 1) THEN
                       cast(null as date)
                      when (lnAn = cal.an and lnLuna = cal.luna) and
                           (TABEL = 'JV2007' AND lnScrieTVA = 1) THEN
                       cast(null as date)
                      when (lnAn = cal.an and lnLuna = cal.luna) and
                           (TABEL = 'JC2007' AND lnScrieTVA = 1) THEN
                       cast(null as date)
                      when (lnAn = cal.an and lnLuna = cal.luna) and
                           (TABEL = 'STOC' AND lnScrieSTOC = 1) THEN
                       cast(null as date)
                      when (lnAn = cal.an and lnLuna = cal.luna) and
                           (TABEL = 'STOC_OBINV' AND lnScrieSTOC = 1) THEN
                       cast(null as date)
                      else
                       sysdate
                    end as dinvalidat
               from (WITH cContAct as (SELECT SCD AS CONT, ID_JTVA_COLOANA
                                         FROM ACT_TEMP
                                       UNION
                                       SELECT SCC AS CONT, ID_JTVA_COLOANA
                                         FROM ACT_TEMP), cContStoc as (SELECT DISTINCT '' AS CONT,
                                                                                       ID_GESTIUNE
                                                                         FROM RUL_TEMP), cContStocO as (SELECT DISTINCT '' AS CONT,
                                                                                                                        ID_GESTIUNE
                                                                                                          FROM Rul_Temp_Obinv)
                      SELECT DISTINCT 'BAL' AS tabel,
                                      '' as CONT,
                                      CAST(NULL AS NUMBER) AS ID_GESTIUNE
                        FROM cContAct B
                      UNION
                      SELECT DISTINCT 'BALANTA_PARTENERI' AS tabel,
                                      B.CONT,
                                      CAST(NULL AS NUMBER) AS ID_GESTIUNE
                        FROM cContAct B
                       where b.cont in (select cont from config_cont_ireg)
                      UNION
                      SELECT DISTINCT 'IREG_PARTENERI' AS tabel,
                                      B.CONT,
                                      CAST(NULL AS NUMBER) AS ID_GESTIUNE
                        FROM cContAct B
                       where b.cont in (select cont
                                          from config_cont_ireg
                                         where cu_inregistrari = 1)
                      UNION
                      SELECT DISTINCT 'JV2007' AS tabel,
                                      '' AS CONT,
                                      CAST(NULL AS NUMBER) AS ID_GESTIUNE
                        FROM cContAct B
                       WHERE ID_JTVA_COLOANA IN
                             (SELECT ID_JTVA_COLOANA
                                FROM JTVA_COLOANE
                               WHERE JV = 1)
                      UNION
                      SELECT DISTINCT 'JC2007' AS tabel,
                                      '' AS CONT,
                                      CAST(NULL AS NUMBER) AS ID_GESTIUNE
                        FROM cContAct B
                       WHERE ID_JTVA_COLOANA IN
                             (SELECT ID_JTVA_COLOANA
                                FROM JTVA_COLOANE
                               WHERE JC = 1)
                      UNION
                      SELECT DISTINCT 'STOC' AS tabel,
                                      '' as CONT,
                                      B.ID_GESTIUNE
                        FROM cContStoc B
                      UNION
                      SELECT DISTINCT 'STOC_OBINV' AS tabel,
                                      '' as CONT,
                                      CAST(NULL AS NUMBER) AS ID_GESTIUNE
                        FROM cContStocO B) c
                        join calendar cal
                          on cal.an * 12 + cal.luna >= lnAn * 12 + lnLuna
             ) D
      ON (A.TABEL = d.TABEL AND A.AN = D.cAN AND A.LUNA = D.cLUNA AND nvl(A.CONT, 'X') = NVL(D.CONT, 'X') AND NVL(A.ID_GESTIUNE, 0) = NVL(D.ID_GESTIUNE, 0))
      WHEN NOT MATCHED THEN
        INSERT
          (tabel,
           an,
           luna,
           cont,
           id_gestiune,
           dultima_operatie,
           dinvalidat,
           id_util_invalidare,
           explicatie)
        values
          (d.tabel,
           d.can,
           d.cluna,
           d.cont,
           d.id_gestiune,
           d.dultima_operatie,
           d.dinvalidat,
           lnIdUtil,
           lpad(tnLuna, 2, '0') || '/' || tnAn)
      WHEN MATCHED THEN
        UPDATE
           set a.dultima_operatie   = nvl(d.dultima_operatie,
                                          a.dultima_operatie),
               a.dinvalidat         = nvl(d.dinvalidat, a.dinvalidat),
               a.id_util_invalidare = lnIdUtil,
               a.explicatie         = lpad(tnLuna, 2, '0') || '/' || tnAn;
    END IF; -- tcTabel is not null
  END VALIDARE_INVALIDARE;

  -- MARCHEAZA INREGISTRARILE CA VALIDATE
  PROCEDURE VALIDAREINREGISTRARI(tnAn     IN act.an%type,
                                 tnLuna   act.luna%type,
                                 tnIdUtil act.id_Utilv%type,
                                 tnCod    act.cod%type) IS
    lnDreptValidare number(10) := 0;
    lnBlocatROACONT NUMBER(1) := 0;
    lnBlocatROAGEST NUMBER(1) := 0;
    lnInchis        number(1) := 0;
  BEGIN

    PACK_FIRMA.IS_LUNA_BLOCATA(user,
                               pack_sesiune.nIdeROACONT,
                               tnAn,
                               tnLuna,
                               lnBlocatROACONT);
    --    PACK_FIRMA.IS_LUNA_BLOCATA(tcS, pack_sesiune.nIdeROAGEST, tnAn, tnLuna,  lnBlocatROAGEST);
    lnInchis := lnBlocatROACONT + lnBlocatROAGEST;

    if lnInchis = 1 then
      goto sfarsit;
    end if;

    SELECT CONTAFIN_ORACLE.PACK_DREPTURI.UtilHasValidare(pack_contafin.GET_ID_FIRMA(),
                                                         tnIdUtil,
                                                         'DREPTVALIDARE')
      INTO lnDreptValidare
      FROM DUAL;

    if nvl(lnDreptValidare, 0) > 0 then
      UPDATE ACT
         SET VALIDAT = 1, ID_UTILV = tnIdUtil, DATAORAV = SYSDATE
       WHERE AN = tnAn
         and LUNA = tnLuna
         and COD = tnCod
         and NVL(STERS, 0) = 0;
    else
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu aveti drepturi pentru validarea inregistrarilor');
    end if;

    <<sfarsit>>
    null;
  END VALIDAREINREGISTRARI;
  ---------------------------------------------------------------------------------------
  procedure EXECUTA_SCRIE_TVA(V_GCS         VARCHAR2,
                              tcNumeTabel   varchar2,
                              tnProcTvaStR  number,
                              tnProcTvaRedR number,
                              tcCorespDTva  varchar2,
                              tcCorespDBaza varchar2,
                              tcCorespCTva  varchar2,
                              tcCorespCBaza varchar2,
                              tcContTva     varchar2,
                              tcExceptii    varchar2,
                              tnAn          number,
                              tnLuna        number,
                              tnScrie       number) IS

    cursor c_DOCUMENTE is
      select distinct nvl(ID_FACT, 0) as id_fact, nract, dataact
        from act_temp;
    cLinieD c_DOCUMENTE%rowtype;

    tnProcTvaSt     act_temp.proc_tva%Type := tnProcTvaStR;
    tnProcTvaRed    act_temp.proc_tva%Type := tnProcTvaRedR;
    v_sumaTVA       act_temp.suma%type := 0;
    V_LISTA_CAMPURI varchar2(5000);

  BEGIN
    V_LISTA_CAMPURI := pack_contafin.LISTA_CAMPURI(USER, 'ACT_temp_ireg');
    -- citesc cota tva standard si redus din cote_tva
    BEGIN
      EXECUTE IMMEDIATE 'SELECT MAX(proc_tva) from ' || V_GCS ||
                        '.cote_tva
                 where an = :1 and luna = :2 and descriere like :3'
        INTO tnProcTvaSt
        USING tnAn, tnLuna, '%STANDARD%';
    EXCEPTION
      WHEN OTHERS THEN
        tnProcTvaSt := tnProcTvaStR;
    END;
    IF tnProcTvaSt IS NULL THEN
      tnProcTvaSt := tnProcTvaStR;
    END IF;

    BEGIN
      EXECUTE IMMEDIATE 'SELECT MAX(proc_tva) from ' || V_GCS ||
                        '.cote_tva
                 where an = :1 and luna = :2 and descriere like :3'
        INTO tnProcTvaRed
        USING tnAn, tnLuna, '%REDUS%';
    EXCEPTION
      WHEN OTHERS THEN
        tnProcTvaRed := tnProcTvaRedR;
    END;
    IF tnProcTvaRed IS NULL THEN
      tnProcTvaRed := tnProcTvaRedR;
    END IF;

    open c_DOCUMENTE;
    fetch c_DOCUMENTE
      into cLinieD;

    while c_DOCUMENTE%found loop

      delete from act_temp_ireg;

      execute immediate 'INSERT INTO ACT_TEMP_IREG (' || v_lista_campuri ||
                        ') SELECT ' || v_lista_campuri ||
                        ' from act_temp where id_fact = :1 and nract = :2 and dataact = :3'
        using cLinieD.id_fact, cLinieD.nract, cLinieD.dataact;

      -- 07.12.2006
      -- MARIUS.MUTU
      -- AM CALCULAT SUMA TVA PENTRU FIECARE COTA TVA IN AFARA DE 0 SAU 1 (NEIMPOZABIL)
      -- APOI SCAD SUMA DIN PRIMA SUMA CU TVA CU ACEA COTA DE TVA
      -- temporar, pt regularizare facturi nesosite, neintocmite
      FOR cota_tva in (select distinct proc_tva
                         from act_temp_ireg
                        where proc_tva not in (0, 1)) LOOP
        begin
          select suma
            INTO v_sumaTVA
            from act_temp_ireg
           where proc_tva = cota_tva.proc_tva
             and ((scd = '4426' and scc = '4428') or
                 (scd = '4428' and scc = '4427'));

        EXCEPTION
          WHEN OTHERS THEN
            v_sumaTVA := 0;
        end;
        -- 22.11.2006
        -- MARIUS.MUTU
        -- ROWNUM = 1 SA NU SE SCADA TVA-UL DE MAI MULTE ORI
        -- dbms_output.put_line('suma TVA : '||cLinieD.nract || ', ' || v_sumaTVA);
        UPDATE act_temp_ireg
           set suma = suma - v_sumaTVA
         where rownum = 1
           and ((scd = '4111' and scc = '418') or
               (scd = '408' and scc = '401'))
           and proc_tva = cota_tva.proc_tva;
      END LOOP;

      -----

      pack_contafin.SCRIE2_TVA(V_GCS,
                               tcNumeTabel,
                               tnProcTvaSt,
                               tnProcTvaRed,
                               tcCorespDTva,
                               tcCorespDBaza,
                               tcCorespCTva,
                               tcCorespCBaza,
                               tcContTva,
                               tcExceptii,
                               tnAn,
                               tnLuna,
                               tnScrie,
                               cLinieD.id_fact,
                               cLinieD.nract,
                               cLinieD.dataact);
      fetch c_DOCUMENTE
        into cLinieD;
    end loop;
    close c_DOCUMENTE;

    execute immediate 'delete from act_temp_ireg';

  END EXECUTA_SCRIE_TVA;
  ---------------------------------------------------------------
  procedure SCRIE2_TVA(V_GCS         VARCHAR2,
                       tcNumeTabel   varchar2,
                       tnProcTvaStR  number,
                       tnProcTvaRedR number,
                       tcCorespDTva  varchar2,
                       tcCorespDBaza varchar2,
                       tcCorespCTva  varchar2,
                       tcCorespCBaza varchar2,
                       tcContTva     varchar2,
                       tcExceptii    varchar2,
                       tnAn          number,
                       tnLuna        number,
                       tnScrie       number,
                       tnIdFact      number,
                       tnNrAct       number,
                       tdDataAct     date) IS

    lnAct      number(20);
    lnCumpVanz number(10);

    lnSumaNeimpozab number(24, 4);
    lnSumaTvam      number(24, 4);
    lnSumaTvai      number(24, 4);
    lnSumaTotalm    number(24, 4);
    lnSumaTotali    number(24, 4);

    lcAn      varchar2(4);
    lcLuna    varchar2(2);
    lcIdTabel varchar2(20);

    lnScrie_tva    number(1);
    V_APARITII_scd number(2) := 0;
    V_APARITII_scc number(2) := 0;
    V_APARITII     number(2) := 0;

    lcContCoresp   varchar2(3);
    lcCont         varchar2(3);
    lcContulCoresp varchar2(4);

    lcContul        varchar2(4);
    lcCorespTva     varchar2(1000);
    lcCorespBaza    varchar2(1000);
    lcCorespondente varchar2(1000);
    ldDataact       date;
    ldDataireg      date;
    lnNract         number(20);
    lnId_fdoc       number(5);
    lnCod           number(20);
    lnId_part       number(10);
    lcScd           varchar2(4);
    lcScc           varchar2(4);
    lnNeimpozab     number(24, 4);
    ignore          number(1);
    lnGasit         number(1) := 0;
    lnGasitPart     number(1) := 0;
    lnIdRand        number(35);

    cursor c_act_temp is
      select *
        from act_temp_ireg
       where id_fact = tnIdFact
         and nract = tnNrAct
         and dataact = tdDataAct
       order by id_act;

    cLinie c_act_temp%rowtype;

    tnProcTvaSt  act_temp.proc_tva%Type := tnProcTvaStR;
    tnProcTvaRed act_temp.proc_tva%Type := tnProcTvaRedR;

  begin
    lcAn        := to_char(tnAn);
    lcLuna      := TO_CHAR(tnLuna);
    lnScrie_tva := 0;

    /*    -- citesc cota tva standard si redus din cote_tva
    BEGIN
      EXECUTE IMMEDIATE 'SELECT MAX(proc_tva) from ' || V_GCS ||
                        '.cote_tva
                 where an = :1 and luna = :2 and descriere like :3'
        INTO tnProcTvaSt
        USING tnAn, tnLuna, '%STANDARD%';
    EXCEPTION
      WHEN OTHERS THEN
        tnProcTvaSt := tnProcTvaStR;
    END;
    IF tnProcTvaSt IS NULL THEN
      tnProcTvaSt := tnProcTvaStR;
    END IF;*/

    /*    BEGIN
      EXECUTE IMMEDIATE 'SELECT MAX(proc_tva) from ' || V_GCS ||
                        '.cote_tva
                 where an = :1 and luna = :2 and descriere like :3'
        INTO tnProcTvaRed
        USING tnAn, tnLuna, '%REDUS%';
    EXCEPTION
      WHEN OTHERS THEN
        tnProcTvaRed := tnProcTvaRedR;
    END;
    IF tnProcTvaRed IS NULL THEN
      tnProcTvaRed := tnProcTvaRedR;
    END IF;*/

    lnAct      := dbms_sql.open_cursor;
    lnCumpVanz := dbms_sql.open_cursor;
    lnId_part  := 0;

    if tcContTva = '4426' then
      execute immediate 'SELECT COUNT(*) FROM ACT_TEMP_ireg WHERE (SCD = :1 OR (proc_tva <> 0 and to_number(scc) <> 4427 and to_number(scc) in (' ||
                        tcCorespDBaza || ') and to_number(scd) not in (' ||
                        tcCorespDBaza ||
                        ')) OR (proc_tva<>0 and to_number(scd) in (' ||
                        tcCorespDBaza || ') and to_number(scc) in (' ||
                        tcExceptii ||
                        '))) and id_fact = :2 and nract = :3 and dataact = :4'
        into V_APARITII_scd
        using tcContTVA, tnIdFact, tnNrAct, tdDataAct;
    else
      V_APARITII_scd := 0;
    end if;
    if tcContTva = '4427' then
      execute immediate 'SELECT COUNT(*) FROM ACT_TEMP_ireg WHERE (SCC = :1 OR (proc_tva <> 0 and to_number(scd) <> 4426 and to_number(scd) in (' ||
                        tcCorespCBaza || ') and to_number(scc) not in (' ||
                        tcCorespCBaza ||
                        ')) OR (proc_tva<>0 and to_number(scc) in (' ||
                        tcCorespCBaza || ') and to_number(scd) in (' ||
                        tcExceptii ||
                        '))) and id_fact = :2 and nract = :3 and dataact = :4'
        into V_APARITII_scc
        using tcContTVA, tnIdFact, tnNrAct, tdDataAct;
    else
      V_APARITII_scc := 0;
    end if;

    if V_APARITII_scd > 0 then
      -- 4426
      lcContCoresp := 'scc';
      lcCont       := 'scd';
      lnScrie_tva  := 1;
      lcCorespTVA  := tcCorespDTVA;
      LcCorespBaza := tcCorespDBaza;
    end if;

    if V_APARITII_scc > 0 then
      -- 4427
      lcContCoresp := 'scd';
      lcCont       := 'scc';
      lnScrie_tva  := 1;
      lcCorespTVA  := tcCorespCTVA;
      lcCorespBaza := tcCorespCBaza;
    end if;

    if lnScrie_tva = 1 then
      open c_act_temp;
      fetch c_act_temp
        into cLinie;
      while c_act_temp%found loop

        if lcCont = 'scd' then
          -- 4426
          lcCorespondente := tcCorespDBaza || ',' || tcExceptii;
          lcContul        := cLinie.scd;
          lcContulCoresp  := cLinie.scc;
        else
          -- 4427
          lcCorespondente := tcCorespCBaza || ',' || tcExceptii;
          lcContul        := cLinie.scc;
          lcContulCoresp  := cLinie.scd;
        end if;
        lnNeimpozab := cLinie.proc_tva;

        --        dbms_output.put_line(cLinie.nract || ',' || lnNeimpozab || ',' ||
        --                             lcContulCoresp || ',' || lcCorespondente);

        if (lcContul = tcContTVA OR
           instr(lcCorespondente, lcContulCoresp) > 0) and lnNeimpozab <> 0 then
          --          dbms_output.put_line(cLinie.nract || ' a intrat');

          if lnGasit = 0 then
            lnGasit := 1;
            /*            lnNract    := cLinie.nract;
            ldDataact  := cLinie.dataact;*/
            lcScd      := cLinie.scd;
            lcScc      := cLinie.scc;
            lnCod      := cLinie.cod;
            ldDataireg := cLinie.dataireg;
            lnId_fdoc  := cLinie.id_fdoc;
          end if;

          if lcCont = 'scd' then
            -- 4426
            if instr(tcCorespDBaza, lcContulCoresp) > 0 /*and lcContul <> tcContTva*/
             then
              lnId_part   := cLinie.id_partc;
              lnGasitPart := 1;
            elsif instr(tcCorespDBaza, lcContul) > 0 and
                  instr(tcExceptii, lcContulCoresp) > 0 then
              lnId_part   := cLinie.id_partd;
              lnGasitPart := 1;

            end if;
          else
            --4427
            if instr(tcCorespCBaza, lcContulCoresp) > 0 /*and lcContul <> tcContTva*/
             then
              if instr(tcExceptii, lcContul) > 0 then
                -- 419
                lcScd       := lcContul;
                lnId_part   := cLinie.id_partc;
                lnGasitPart := 1;
              else
                lnId_part   := cLinie.id_partd;
                lnGasitPart := 1;
              end if;
            elsif instr(tcCorespCBaza, lcContul) > 0 and
                  instr(tcExceptii, lcContulCoresp) > 0 then
              lnId_part   := cLinie.id_partc;
              lnGasitPart := 1;
            end if;

          end if;

        end if;
        if lnGasit = 1 and lnGasitPart = 1 then
          exit;
        end if;
        fetch c_act_temp
          into cLinie;

      end loop;
      close c_act_temp;

      if lnGasit <> 1 then
        /*        lnNract    := cLinie.nract;
        ldDataact  := cLinie.dataact;*/
        lcScd      := cLinie.scd;
        lcScc      := cLinie.scc;
        lnCod      := cLinie.cod;
        ldDataireg := cLinie.dataireg;
        lnId_fdoc  := cLinie.id_fdoc;
      end if;

      lnNract   := tnNrAct;
      ldDataact := tdDataAct;
      lcIdTabel := 'Id_' || tcNumeTabel;

      execute immediate 'SELECT COUNT(*), min(' || lcIdTabel || ') FROM ' ||
                        V_GCS || '.' || tcNumeTabel ||
                        ' WHERE an = :1 and luna = :2 and ID_PART = :3 AND NRACT =:4 AND DATAACT =:5'
        into v_aparitii, lnIdRand
        using tnAn, tnLuna, lnId_part, lnNract, ldDataact;

      -- UPDATE Cump, Vanz
      if V_APARITII = 0 and tnscrie <> 2 then
        EXECUTE IMMEDIATE 'INSERT INTO ' || V_GCS || '.' || tcNumeTabel ||
                          '(LUNA, AN, scd, scc, dataact, ID_PART, nract, cod, dataireg, id_fdoc)
                             VALUES (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10)'
          USING tnLuna, tnAn, lcScd, LcScc, ldDataact, lnId_part, lnNract, lnCod, ldDataireg, lnId_fdoc;

        if tnScrie = 2 then
          return;
        end if;

      end if;

      DBMS_SQL.PARSE(lnCumpVanz,
                     'update ' || v_gcs || '.' || tcNumeTabel ||
                     ' set neimpozab = neimpozab + :lnSumaNeimpozab, tvam = tvam + :lnSumaTvaM, tvai = tvai + :lnSumaTvaI,' ||
                     'totftvam = totftvam + :lnSumaTotalM - :lnSumaTvaM, totftvai = totftvai + :lnSumaTotalI - :lnSumaTvaI  ' ||
                     'where an = ' || lcAn || ' and luna = ' || lcLuna ||
                     'and dataact = :ldDataact and nract = :lnNract and id_part = :lnId_part',
                     DBMS_SQL.native);
      -- 'totctva = totftvam + totftvai + tvam + tvai + neimpozab' ||
      DBMS_SQL.PARSE(lnAct,
                     'select sum(suma) as neimpozab, sum(0.0000) as tvam, sum(0.0000) as tvai, sum(0.0000) as Totctvam, sum(0.0000) as Totctvai
                     from act_temp_ireg where to_number(' ||
                     lcContCoresp || ') in (' || lcCorespBaza ||
                     ') and not to_number(' || lcCont || ') in (' ||
                     lcCorespBaza || ') and proc_tva = 1 and id_fact = ' ||
                     tnIdFact || ' group by an UNION ' ||
                     'select sum(-suma) as neimpozab, sum(0.0000) as tvam, sum(0.0000) as tvai, sum(0.0000) as Totctvam, sum(0.0000) as Totctvai
                     from act_temp_ireg where to_number(' ||
                     lcContCoresp || ') in (' || tcExceptii ||
                     ') and to_number(' || lcCont || ') in (' ||
                     lcCorespBaza || ') and proc_tva = 1 and id_fact = ' ||
                     tnIdFact || ' group by an ' ||
                     'UNION select sum(0.0000) as neimpozab, sum(suma) as tvam, sum(0.0000) as tvai, sum(0.0000) as Totctvam, sum(0.0000) as Totctvai
                      from act_temp_ireg where to_number(' ||
                     lcCont || ') = ' || tcContTva || ' and proc_tva = ' ||
                     tnProcTvaSt || ' and id_fact = ' || tnIdFact ||
                     ' group by an UNION ' ||
                     'select sum(0.0000) as neimpozab, sum(0.0000) as tvam, sum(suma) as tvai, sum(0.0000) as Totctvam, sum(0.0000) as Totctvai
                     from act_temp_ireg where to_number(' ||
                     lcCont || ') = ' || tcContTva || ' and proc_tva = ' ||
                     tnProcTvaRed || ' and id_fact = ' || tnIdFact ||
                     ' group by an UNION ' ||
                     'select sum(0.0000) as neimpozab, sum(0.0000) as tvam, sum(0.0000) as tvai, sum(suma) as Totctvam, sum(0.0000) as Totctvai
                      from act_temp_ireg where ((to_number(' ||
                     lcContCoresp || ') in (' || lcCorespBaza ||
                     ') and not to_number(' || lcCont || ') in (' ||
                     lcCorespBaza || ')) OR ' || lcCont || '= ' ||
                     tcContTva || ') and proc_tva = ' || tnProcTvaSt ||
                     ' and id_fact = ' || tnIdFact || ' group by an UNION ' ||
                     'select sum(0.0000) as neimpozab, sum(0.0000) as tvam, sum(0.0000) as tvai, sum(-suma) as Totctvam, sum(0.0000) as Totctvai
                      from act_temp_ireg where to_number(' ||
                     lcContCoresp || ') in (' || tcExceptii ||
                     ') and to_number(' || lcCont || ') in (' ||
                     lcCorespBaza || ') and proc_tva = ' || tnProcTvaSt ||
                     ' and id_fact = ' || tnIdFact || ' group by an UNION ' ||
                     'select sum(0.0000) as neimpozab, sum(0.0000) as tvam, sum(0.0000) as tvai, sum(0.0000) as Totctvam, sum(suma) as Totctvai
                     from act_temp_ireg where ((to_number(' ||
                     lcContCoresp || ') in (' || lcCorespBaza ||
                     ') and not to_number(' || lcCont || ') in (' ||
                     lcCorespBaza || ')) OR ' || lcCont || '= ' ||
                     tcContTva || ') and proc_tva = ' || tnProcTvaRed ||
                     ' and id_fact = ' || tnIdFact || ' group by an UNION ' ||
                     'select sum(0.0000) as neimpozab, sum(0.0000) as tvam, sum(0.0000) as tvai, sum(0.0000) as Totctvam, sum(-suma) as Totctvai
                     from act_temp_ireg where to_number(' ||
                     lcContCoresp || ') in (' || tcExceptii ||
                     ') and to_number(' || lcCont || ') in (' ||
                     lcCorespBaza || ') and proc_tva = ' || tnProcTvaRed ||
                     ' group by an ',
                     DBMS_SQL.native);

      DBMS_SQL.DEFINE_COLUMN(lnAct, 1, lnSumaNeimpozab);
      DBMS_SQL.DEFINE_COLUMN(lnAct, 2, lnSumaTvam);
      DBMS_SQL.DEFINE_COLUMN(lnAct, 3, lnSumaTvai);
      DBMS_SQL.DEFINE_COLUMN(lnAct, 4, lnSumaTotalm);
      DBMS_SQL.DEFINE_COLUMN(lnAct, 5, lnSumaTotali);
      ignore := DBMS_SQL.EXECUTE(lnAct);

      LOOP
        IF DBMS_SQL.FETCH_ROWS(lnAct) > 0 THEN
          -- get column values of the row
          DBMS_SQL.COLUMN_VALUE(lnAct, 1, lnSumaNeimpozab);
          DBMS_SQL.COLUMN_VALUE(lnAct, 2, lnSumaTvam);
          DBMS_SQL.COLUMN_VALUE(lnAct, 3, lnSumaTvai);
          DBMS_SQL.COLUMN_VALUE(lnAct, 4, lnSumaTotalm);
          DBMS_SQL.COLUMN_VALUE(lnAct, 5, lnSumaTotali);

          DBMS_SQL.BIND_VARIABLE(lnCumpVanz,
                                 ':lnSumaNeimpozab',
                                 lnSumaNeimpozab);
          DBMS_SQL.BIND_VARIABLE(lnCumpVanz, ':lnSumaTvam', lnSumaTvam);
          DBMS_SQL.BIND_VARIABLE(lnCumpVanz, ':lnSumaTvai', lnSumaTvai);
          DBMS_SQL.BIND_VARIABLE(lnCumpVanz, ':lnSumaTotalm', lnSumaTotalm);
          DBMS_SQL.BIND_VARIABLE(lnCumpVanz, ':lnSumaTotali', lnSumaTotali);
          DBMS_SQL.BIND_VARIABLE(lnCumpVanz, ':ldDataact', ldDataact);
          DBMS_SQL.BIND_VARIABLE(lnCumpVanz, ':lnNract', lnNract);
          DBMS_SQL.BIND_VARIABLE(lnCumpVanz, ':lnId_part', lnId_part);

          ignore := DBMS_SQL.EXECUTE(lnCumpVanz);

          execute immediate 'update ' || v_gcs || '.' || tcNumeTabel ||
                            ' set totctva = totftvam + totftvai + tvam + tvai + neimpozab ' ||
                            'where an = ' || lcAn || ' and luna = ' ||
                            lcLuna ||
                            'and dataact = :1 and nract = :2 and id_part = :3'
            using ldDataact, lnNract, lnId_part;

          -- if tnscrie = 2 then
          -- sterg inregistrarile cu suma 0
          execute immediate 'delete from ' || v_gcs || '.' || tcNumeTabel ||
                            ' where totctva = 0 and totftvam = 0 and totftvai = 0 and tvam = 0 and tvai = 0 and neimpozab = 0 and ' ||
                            lcIdTabel || ' = :1'
            using lnIdRand;
          -- end if;

        else

          exit;

        end if;

      end loop;

    end if; -- a gasit TVA

    dbms_sql.close_cursor(lnAct);
    dbms_sql.close_cursor(lnCumpVanz);

  end scrie2_tva;

  ---------------------------------------------------------------
  PROCEDURE STERGE_DIN_ACT(V_GCS      VARCHAR2,
                           tnAn       in number,
                           tnLuna     in number,
                           tnCod      in number,
                           tnId_utils in number,
                           tnTip      IN NUMBER) is

    -- tnTip : 0 = modificare ; 1 = stergere
    LD_DATAORA ACT_TEMP.DATAORA%TYPE;
    LC_UPDATE  VARCHAR2(10000) := '';
    LNID_UTIL  ACT_TEMP.ID_UTIL%TYPE;
    lnTip      number(1) := 0;
    lnId_Fact  act_temp.id_fact%Type := 0;
  BEGIN
    if tnTip is not null then
      lnTip := tnTip;
    end if;
    LD_DATAORA := PACK_CONTAFIN.GET_DATAORA();
    LNID_UTIL  := PACK_CONTAFIN.GET_IDUTIL();

    UPDATE /*+ index(ACT IDX_COD) */ ACT
       SET STERS = 1, DATAORAS = LD_DATAORA, ID_UTILS = lnId_util
     WHERE COD = tnCod
       and an = tnAn
       and luna = tnLuna;

    IF lnTip = 1 THEN
      UPDATE DOCUMENTE
         SET STERS = 1, ID_UTILS = LNID_UTIL, DATAORAS = LD_DATAORA
       WHERE ID_DOC IN
             (SELECT /*+ index(ACT IDX_COD) */
              DISTINCT ID_FACT
                FROM ACT
               WHERE COD = tnCod
                 and an = tnAn
                 and luna = tnLuna
                 AND ID_FACT <> 0
                 and ID_SET not in (90501, 90021)
                 AND NOT (SCD = '4426' AND SCC = '4428')
                 AND NOT (SCD = '4428' AND SCC = '4427'));
    END IF;

    update act_temp set suma = -suma, suma_val = -suma_val;

  END STERGE_DIN_ACT;
  ---------------------------------------------------------------
  PROCEDURE STERGE_DIN_RUL(V_GCS      VARCHAR2,
                           tnAn       in number,
                           tnLuna     in number,
                           tnCod      in number,
                           tnId_utils in number) is
    LD_DATAORA RUL_TEMP.DATAORA%TYPE := PACK_CONTAFIN.GET_DATAORA();
  BEGIN
    UPDATE RUL
       SET STERS = 1, ID_UTILS = tnId_utils, DATAORAS = LD_DATAORA
     WHERE COD = tnCod
       and an = tnAn
       and luna = tnLuna;

    update rul_temp set cant = -cant, cante = -cante; -- ??

  END STERGE_DIN_RUL;
  ---------------------------------------------------------------
  PROCEDURE STERGE_DIN_RUL_OBINV(V_GCS      VARCHAR2,
                                 tnAn       in number,
                                 tnLuna     in number,
                                 tnCod      in number,
                                 tnId_utils in number) is
    LD_DATAORA RUL_TEMP.DATAORA%TYPE := PACK_CONTAFIN.GET_DATAORA();
  BEGIN
    UPDATE RUL_OBINV
       SET STERS = 1, ID_UTILS = tnId_utils, DATAORAS = LD_DATAORA
     WHERE COD = tnCod
       and an = tnAn
       and luna = tnLuna;

    update rul_temp_OBINV set cant = -cant, cante = -cante; -- ??

  END STERGE_DIN_RUL_OBINV;
  ---------------------------------------------------------------
  ------------------------------------------------------------------------------------
  PROCEDURE SCRIE_IN_RUL(tcS IN VARCHAR2) IS
    V_LISTA_CAMPURI VARCHAR2(2000) := '';

    LN_COD            NUMBER(20) := 0;
    lnIdFact          NUMBER(20) := 0;
    lnCuFacturi       NUMBER(5) := 0;
    LD_DATAORA        DATE;
    LC_UPDATE         VARCHAR2(100) := '';
    LC_INSERT         VARCHAR2(10000) := '';
    lnId_Util         act_temp.id_util%Type;
    LNAN              ACT_TEMP.AN%TYPE;
    LNLUNA            ACT_TEMP.LUNA%TYPE;
    lnIdSucursala     act.id_sucursala%type;
    lnProcTvaStandard act_temp.proc_tva%TYPE;
    lcRulajFurnizor   OPTIUNI.VARVALUE%TYPE;
    lnRulajFurnizor   NUMBER(1) := 0;
    lnIdFurnizor      nom_parteneri.id_part%type;
    lnIdRulAux        rul_auxiliar.id_rul_aux%type;
  BEGIN
    lnIdSucursala := PACK_CONTAFIN.GET_ID_SUCURSALA();

    V_LISTA_CAMPURI := pack_contafin.LISTA_CAMPURI(tcS, 'RUL');

    LN_COD            := pack_contafin.GET_COD();
    LD_DATAORA        := pack_contafin.GET_DATAORA();
    lnId_Util         := pack_contafin.GET_IDUTIL();
    LNAN              := pack_sesiune.GETAN();
    LNLUNA            := pack_sesiune.GETLUNA();
    lnProcTvaStandard := pack_contafin.getCotaTVAStandard(LNLUNA, LNAN);

    lcRulajFurnizor := PACK_SESIUNE.getOptiuneFirma('RULAJFURNIZOR');
    IF lcRulajFurnizor IS NULL THEN
      lnRulajFurnizor := 0;
    ELSE
      lnRulajFurnizor := to_number(lcRulajFurnizor);
    END IF;

    -- 25.10.2006
    -- mutu.marius
    -- in cazul factura achizitie + plata - se generau 2 id_fact, iar in rulaj se ducea ultimul id_fact (plata)
    -- probleme la listare - iau doar inregistrarile fara trezorerie ('5')
    --    lnIdFact   := get_idFact();
    select min(id_fact)
      into lnIdFact
      from act_temp
     where substr(scd, 1, 1) <> '5'
       and substr(scc, 1, 1) <> '5';

    if nvl(pack_contafin.nSuprascriereCod, 1) = 1 then
      UPDATE RUL_TEMP
         SET COD     = LN_COD,
             DATAORA = LD_DATAORA,
             ID_UTIL = lnId_Util,
             AN      = lnAn,
             LUNA    = lnLuna;

      -- LA ACHIZITIE DIN IMPORT SE PUNEA ALT ID_FACT DECAT LA PRIMA FACTURA CARE AVEA NNIR <> 0
      MERGE INTO RUL_TEMP A
      USING (SELECT DISTINCT NRACT, COD, ID_FACT, NNIR
               FROM ACT_TEMP
              WHERE NVL(ID_FACT, 0) <> 0
                AND substr(scd, 1, 1) <> '5'
                and substr(scc, 1, 1) <> '5') B
      ON (A.NRACT = B.NRACT AND A.COD = B.COD AND A.NNIR = B.NNIR)
      WHEN MATCHED THEN
        UPDATE SET A.ID_FACT = B.ID_FACT;

      UPDATE RUL_TEMP SET ID_FACT = lnIdFact WHERE NVL(ID_FACT,0) = 0;

      /*if lnIdFact <> 0 and lnIdFact is not null then
        -- la modificare nu setez id_fact
        -- UPDATE RUL_TEMP SET ID_FACT = lnIdFact;
      end if;*/

    else
      if nvl(pack_contafin.nSuprascriereAnLuna, 1) = 1 then
        UPDATE RUL_TEMP
           SET DATAORA = LD_DATAORA,
               ID_UTIL = lnId_Util,
               AN      = lnAn,
               LUNA    = lnLuna;
      else
        UPDATE RUL_TEMP SET DATAORA = LD_DATAORA, ID_UTIL = lnId_Util;
      end if;

      MERGE INTO RUL_TEMP A
      USING (SELECT DISTINCT NRACT, COD, ID_FACT, NNIR FROM ACT_TEMP) B
      ON (A.NRACT = B.NRACT AND A.COD = B.COD AND A.NNIR = B.NNIR)
      WHEN MATCHED THEN
        UPDATE SET A.ID_FACT = B.ID_FACT;
    end if;

    -- 23.02.2010
    -- Nu se suprascrie id_sucursala completat deja in act_temp
    UPDATE RUL_TEMP
       SET ID_SUCURSALA = lnIdSucursala
     WHERE NVL(ID_SUCURSALA, 0) = 0;
    -- 23.02.2010 ^

    UPDATE RUL_TEMP SET DATAIN = dataact WHERE CANT <> 0;
    UPDATE RUL_TEMP SET DATAOUT = dataact WHERE CANTE <> 0;
    UPDATE RUL_TEMP SET ID_RUL_AUX = NULL WHERE ID_RUL_AUX = 0;

    -- Completare RUL.ID_RUL_AUX SI RUL_AUX.ID_FURNIZOR la intrari daca este setata optiunea
    if lnRulajFurnizor = 1 then
      SELECT max(id_partc)
        into lnIdFurnizor
        from act_temp
       where scc in ('401', '404', '408', '462')
         and id_fact in (select id_fact from rul_temp where cant <> 0);

      if lnIdFurnizor is not null then
        lnIdRulAux := GetRulajAuxiliarFurnizor(lnIdFurnizor);
        update rul_temp set id_rul_aux = lnIdRulAux;
      end if;
    end if;

    LC_INSERT := 'INSERT INTO ' || tcS || '.RUL (' || V_LISTA_CAMPURI ||
                 ') (SELECT ' || V_LISTA_CAMPURI || ' FROM RUL_TEMP)';

    --    LC_INSERT := 'INSERT INTO ' || tcS || '.RUL (SELECT ' || V_LISTA_CAMPURI || ' FROM RUL_TEMP)';
    EXECUTE IMMEDIATE LC_INSERT;

  END SCRIE_IN_RUL;

  ------------------------------------------------------------------------------------
  ------------------------------------------------------------------------------------
  PROCEDURE SCRIE_IN_STOC_OLD(tcS        IN VARCHAR2,
                              tnAn       in number,
                              tnLuna     in number,
                              tnScrie    number,
                              tcTabel    in varchar2,
                              tcIdTabel  in varchar2,
                              tcCondSpec in varchar2) IS

    CURSOR CRS_TEMP IS
      SELECT R.*, 00 AS TIPG FROM RUL_TEMP R WHERE 1 = 2;
    pCursor      tip_ref_cursor;
    lnId_Stoc    Act_Temp.Id_Act%Type;
    lcSelect     varchar2(1000);
    lcSearcha    varchar2(1000);
    lcSearchv    varchar2(1000);
    lnStoca      number;
    lnStocv      number;
    item         crs_temp%Rowtype;
    ignore       number := 0;
    lnAn         act_temp.an%Type := tnAn;
    lnLuna       act_temp.luna%Type := tnLuna;
    lcTabel      varchar2(50) := tcTabel;
    lcIdTabel    varchar2(50) := tcIdTabel;
    lcCondSpec   varchar2(100) := tcCondSpec;
    lcUpdateStoc varchar2(1000);

    lnIdSucursala    act.id_sucursala%type;
    lcWhereSucursale varchar2(1000);

  BEGIN
    lnIdSucursala := PACK_CONTAFIN.GET_ID_SUCURSALA();
    if lnIdSucursala is not null then
      lcWhereSucursale := ' and r.id_sucursala = ' || lnIdSucursala;
    end if;

    if lcTabel is null then
      lcTabel := 'Stoc';
    end if;
    if lcIdTabel is null then
      lcIdTabel := 'id_Stoc';
    end if;
    if tcCondSpec is null then
      lcCondSpec := '2=2';
    end if;

    lcSearcha := 'select ' || lcIdTabel || ' from ' || tcs || '.' ||
                 lcTabel ||
                 ' where an = :1 and luna = :2 and id_articol = :3 and id_gestiune = :4
                 and cont = :5 and nvl(acont,''XXXX'') = :6 and pret = :7
                 and NVL(id_rul_aux,0) = :8 and NVL(serie,''+_'') = :9
                 and NVL(pretd,0) = :10 and NVL(id_valuta,0) = :11
                 and NVL(LOT,''+_'') = :12
                 AND NVL(ADATA_EXPIRARE, to_date(''1900-01-01'',''YYYY-MM-DD'')) = :13 and NVL(id_sucursala, 0) = :14
                 AND NVL(ID_LUCRARE_REZ,0) = :15 AND NVL(ID_PART_REZ,0) = :16 AND ' ||
                 lcCondSpec;
    lcSearchv := 'select ' || lcIdTabel || ' from ' || tcs || '.' ||
                 lcTabel ||
                 ' where an = :1 and luna = :2 and id_articol = :3 and id_gestiune = :4
                 and cont = :5 and NVL(acont,''XXXX'') = :6 and pret = :7
                 and NVL(id_rul_aux,0) = :8 and NVL(serie,''+_'') = :9
                 and pretv = :10 and tvav = :11 and proc_tvav = :12
                 and NVL(pretd,0) = :13 and NVL(id_valuta,0) = :14
                 and NVL(LOT,''+_'') = :15
                 AND NVL(ADATA_EXPIRARE, to_date(''1900-01-01'',''YYYY-MM-DD'')) = :16 and NVL(id_sucursala, 0) = :17
                 AND NVL(ID_LUCRARE_REZ,0) = :18 AND NVL(ID_PART_REZ,0) = :19 AND ' ||
                 lcCondSpec;
    IF tnScrie = 1 THEN
      -- REFACERE
      lnAn   := tnAn;
      lnLuna := tnLuna;

      lcSelect := 'select r.*,decode(nvl(g.nr_pag,00),6,1,7,1,0) as tipg
                 from rul r
                 left join vnom_gestiuni g on r.id_gestiune = g.id_gestiune
                   where r.an = :1 and r.luna = :2 and substr(r.cont,1,2)<>''80'' and r.sters=0 ' ||
                  lcWhereSucursale;
    ELSE
      -- SCRIERE
      /*      lnAn   := pack_contafin.GET_AN();
      lnLuna := pack_contafin.GET_LUNA();*/
      lnAn   := tnAn;
      lnLuna := tnLuna;

      lcSelect := 'select r.*,decode(nvl(g.nr_pag,00),6,1,7,1,0) as tipg
                 from rul_Temp r
                 left join vnom_gestiuni g on r.id_gestiune = g.id_gestiune
                   where r.an = :1 and r.luna = :2 and substr(r.cont,1,2)<>''80'' ';
      -- lcSelect  := 'select * from Xrul_temp';
    END IF;

    lnStoca := dbms_sql.open_cursor;
    lnStocv := dbms_sql.open_cursor;
    DBMS_SQL.PARSE(lnStoca, lcSearcha, DBMS_SQL.native);
    DBMS_SQL.PARSE(lnStocv, lcSearchv, DBMS_SQL.native);
    DBMS_SQL.DEFINE_COLUMN(lnStoca, 1, lnId_Stoc);
    DBMS_SQL.DEFINE_COLUMN(lnStocv, 1, lnId_Stoc);

    open pCursor for lcSelect
      USING lnAn, lnLuna;
    loop
      fetch pCursor
        into item;
      exit when pCursor%notfound;

      item.acont := NVL(item.acont, 'XXXX');
      -- pinfo('item.tva ' || item.tva);

      if item.tipg = 0 then
        -- pinfo('tipg = 0 item.tva ' || item.tva);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':1', item.an);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':2', item.luna);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':3', item.id_articol);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':4', item.id_gestiune);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':5', item.cont);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':6', item.acont);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':7', item.pret);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':8', NVL(item.id_rul_aux, 0));
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':9', NVL(item.serie, '+_'), 100);
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':10', NVL(item.pretd, 0));
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':11', NVL(item.id_valuta, 0));
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':12', NVL(item.lot, '+_'), 100);
        DBMS_SQL.BIND_VARIABLE(lnStoca,
                               ':13',
                               NVL(item.adata_expirare,
                                   TO_DATE('1900-01-01', 'YYYY-MM-DD')));
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':14', NVL(item.id_sucursala, 0));
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':15', NVL(item.id_lucrare_rez, 0));
        DBMS_SQL.BIND_VARIABLE(lnStoca, ':16', NVL(item.id_part_rez, 0));

        ignore := DBMS_SQL.EXECUTE(lnStoca);

        if item.acont = 'XXXX' then
          item.acont := '';
        END IF;
        IF DBMS_SQL.FETCH_ROWS(lnStoca) > 0 THEN
          DBMS_SQL.COLUMN_VALUE(lnStoca, 1, lnId_Stoc);
          -- pinfo('tipg = 0, update stoc.id_stoc ' || lnId_Stoc);
          -- modificare
          lcUpdateStoc := 'UPDATE ' || tcs || '.' || lcTabel ||
                          ' set cant = cant + :1, cante = cante + :2, dataora = :3';

          if item.dataout is not null and item.cante > 0 then
            lcUpdateStoc := lcUpdateStoc || ',dataout = to_date(''' ||
                            to_char(item.dataout, 'DDMMYYYY') ||
                            ''',''DDMMYYYY'')';
          end if;

          if item.datain is not null and item.cant > 0 then
            lcUpdateStoc := lcUpdateStoc || ',datain = to_date(''' ||
                            to_char(item.datain, 'DDMMYYYY') ||
                            ''',''DDMMYYYY'')';
          end if;

          lcUpdateStoc := lcUpdateStoc || ' where ' || lcIdTabel || ' = :4';

          EXECUTE IMMEDIATE lcUpdateStoc
            USING item.cant, item.cante, item.dataora, lnId_Stoc;
          -- modificare ^
        ELSE
          -- pinfo('tipg = 0, insert stoc tva ' || item.tva);
          EXECUTE IMMEDIATE 'INSERT INTO ' || tcs || '.' || lcTabel ||
                            ' (an, luna, id_articol, pret, cant, cante, cont, acont, id_gestiune,
                            dataora, datain, dataout, id_rul_aux,serie,tva,pretd,id_valuta,lot,adata_expirare,id_sucursala,id_lucrare_rez,id_part_rez)
                                VALUES (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13,:14,:15,:16,:17,:18,:19,:20,:21,:22)'
            USING item.an, item.luna, item.id_articol, item.pret, item.cant, item.cante, item.cont, item.acont, item.id_gestiune, item.dataora, item.datain, item.dataout, item.id_rul_aux, item.serie, item.tva, item.pretd, item.id_valuta, item.lot, item.adata_expirare, item.id_sucursala, item.id_lucrare_rez, item.id_part_rez;
        END IF;
      else
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':1', item.an);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':2', item.luna);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':3', item.id_articol);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':4', item.id_gestiune);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':5', item.cont);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':6', item.acont);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':7', item.pret);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':8', NVL(item.id_rul_aux, 0));
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':9', NVL(item.serie, '+_'), 100);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':10', item.pretv);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':11', item.tvav);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':12', item.proc_tvav);
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':13', NVL(item.pretd, 0));
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':14', NVL(item.id_valuta, 0));
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':15', NVL(item.lot, '+_'), 100);
        DBMS_SQL.BIND_VARIABLE(lnStocv,
                               ':16',
                               NVL(item.adata_expirare,
                                   TO_DATE('1900-01-01', 'YYYY-MM-DD')));
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':17', NVL(item.id_sucursala, 0));
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':18', NVL(item.id_lucrare_rez, 0));
        DBMS_SQL.BIND_VARIABLE(lnStocv, ':19', NVL(item.id_part_rez, 0));

        ignore := DBMS_SQL.EXECUTE(lnStocv);

        if item.acont = 'XXXX' then
          item.acont := '';
        END IF;
        -- pinfo('tipg = 1 item.tva ' || item.tva);
        IF DBMS_SQL.FETCH_ROWS(lnStocv) > 0 THEN
          DBMS_SQL.COLUMN_VALUE(lnStocv, 1, lnId_Stoc);
          -- pinfo('UPDATE STOC.ID ' || lnId_Stoc);
          -- modificare
          lcUpdateStoc := 'UPDATE ' || tcs || '.' || lcTabel ||
                          ' set cant = cant + :1, cante = cante + :2, dataora = :3';

          if item.dataout is not null then
            lcUpdateStoc := lcUpdateStoc || ',dataout = to_date(''' ||
                            to_char(item.dataout, 'DDMMYYYY') ||
                            ''',''DDMMYYYY'')';
          end if;

          if item.datain is not null then
            lcUpdateStoc := lcUpdateStoc || ',datain = to_date(''' ||
                            to_char(item.datain, 'DDMMYYYY') ||
                            ''',''DDMMYYYY'')';
          end if;

          lcUpdateStoc := lcUpdateStoc || ' where ' || lcIdTabel || ' = :4';

          EXECUTE IMMEDIATE lcUpdateStoc
            USING item.cant, item.cante, item.dataora, lnId_Stoc;
          -- modificare ^
        ELSE
          -- pinfo('INSERT INTO STOC.TVA ' || item.tva);
          EXECUTE IMMEDIATE 'INSERT INTO ' || tcs || '.' || lcTabel ||
                            ' (an, luna, id_articol, pret, pretv, tvav, proc_tvav, cant, cante, cont, acont, id_gestiune,
                            dataora, datain, dataout, id_rul_aux, serie, tva, pretd, id_valuta,lot,adata_expirare,id_sucursala,id_lucrare_rez,id_part_rez)
                                VALUES (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13, :14, :15,:16,:17,:18,:19,:20,:21,:22,:23,:24,:25)'
            USING item.an, item.luna, item.id_articol, item.pret, item.pretv, item.tvav, item.proc_tvav, item.cant, item.cante, item.cont, item.acont, item.id_gestiune, item.dataora, item.datain, item.dataout, item.id_rul_aux, item.serie, item.tva, item.pretd, item.id_valuta, item.lot, item.adata_expirare, item.id_sucursala, item.id_lucrare_rez, item.id_part_rez;
        END IF;
      end if;

    end loop;
    close pCursor;

    DBMS_SQL.close_cursor(lnStoca);
    DBMS_SQL.close_cursor(lnStocv);

  END SCRIE_IN_STOC_OLD;
  --------------------------------------------------------------------------------
  PROCEDURE SCRIE_IN_STOC(tcS        IN VARCHAR2,
                          tnAn       in number,
                          tnLuna     in number,
                          tnScrie    number,
                          tcTabel    in varchar2,
                          tcIdTabel  in varchar2,
                          tcCondSpec in varchar2) IS
  BEGIN
    SCRIE_IN_STOC(tnAn, tnLuna, tnScrie);
  END;

  PROCEDURE SCRIE_IN_STOC(tnAn         in number,
                          tnLuna       in number,
                          tnScrie      number,
                          tnIdGestiune IN RUL.ID_GESTIUNE%TYPE DEFAULT NULL) IS

    lnAn   act_temp.an%Type := tnAn;
    lnLuna act_temp.luna%Type := tnLuna;

    lnIdSucursala    act.id_sucursala%type;
    lcWhereSucursale varchar2(1000);
    lcFrom           varchar2(1000);
    lcFromV          varchar2(1000);
    lcSql            varchar2(32000);
    lcWhereGestiune  varchar2(1000);
  BEGIN
    --pinfo('SCRIE_IN_STOC 1');
    lnIdSucursala := PACK_CONTAFIN.GET_ID_SUCURSALA();
    if lnIdSucursala is not null then
      lcWhereSucursale := ' and r.id_sucursala = ' || lnIdSucursala;
    end if;

    -- SE REFACE FIECARE GESTIUNE
    if tnIdGestiune is not null then
      lcWhereGestiune := ' and r.id_gestiune = ' || tnIdGestiune;
    end if;

    lnAn   := tnAn;
    lnLuna := tnLuna;

    IF tnScrie = 1 THEN
      -- REFACERE
      lcFromV := 'rul r
                 join vnom_gestiuni g on r.id_gestiune = g.id_gestiune
                 where r.an = ' || lnAn || ' and r.luna = ' ||
                 lnLuna ||
                 ' and substr(r.cont,1,2)<>''80'' and r.sters=0 and g.nr_pag in (6,7) ' ||
                 lcWhereGestiune || lcWhereSucursale;

      lcFrom := 'rul r
                 join vnom_gestiuni g on r.id_gestiune = g.id_gestiune
                 where r.an = ' || lnAn || ' and r.luna = ' ||
                lnLuna ||
                ' and substr(r.cont,1,2)<>''80'' and r.sters=0 and g.nr_pag not in (6,7)' ||
                lcWhereGestiune || lcWhereSucursale;
    ELSE
      -- SCRIERE
      lcFromV := 'rul_temp r
                 join vnom_gestiuni g on r.id_gestiune = g.id_gestiune
                 where r.an = ' || lnAn || ' and r.luna = ' ||
                 lnLuna ||
                 ' and substr(r.cont,1,2)<>''80'' and g.nr_pag in (6,7)';

      lcFrom := 'rul_temp r
                 join vnom_gestiuni g on r.id_gestiune = g.id_gestiune
                 where r.an = ' || lnAn || ' and r.luna = ' ||
                lnLuna ||
                ' and substr(r.cont,1,2)<>''80'' and g.nr_pag not in (6,7)';
    END IF;

    -- MARFA LA PRET DE VANZARE (nom_gestiuni.nr_pag in (6,7))
    -- are in plus coloanele pretv, tvav, proc_tvav
    -- nu grupez si nu fac merge dupa "tva", doar in bag in stoc - pentru ca nici in procedura veche nu cautam dupa tva

    lcSql := 'merge into stoc s1
      using (select r.an,
                r.luna,
                r.id_articol,
                r.pret,
                max(r.tva) as tva,
                r.pretv,
                r.tvav,
                r.proc_tvav,
                r.pretd,
                r.cont,
                r.acont,
                r.id_gestiune,
                decode(NVL(r.id_rul_aux, 0), 0, NULL, r.id_rul_aux) as id_rul_aux,
                r.serie,
                decode(NVL(r.id_valuta, 0), 0, NULL, r.id_valuta) as id_valuta,
                r.lot,
                r.adata_expirare,
                decode(NVL(r.id_sucursala, 0), 0, NULL, r.id_sucursala) as id_sucursala,
                decode(NVL(r.ID_LUCRARE_REZ, 0), 0, NULL, r.ID_LUCRARE_REZ) as id_lucrare_rez,
                decode(NVL(r.ID_PART_REZ, 0), 0, NULL, r.ID_PART_REZ) as id_part_rez,
                0 AS CANTS,
                SUM(r.CANT) as cant,
                SUM(r.CANTE) as cante,
                MAX(r.dataora) as dataora,
                MAX(nvl(r.datain,r.dataact)) AS datain,
                MAX(nvl(r.dataout, r.dataact)) AS dataout
               from ' || lcFromV || '
               GROUP BY
                 r.an,
                 r.luna,
                 r.id_articol,
                 r.pret,
                 r.pretv,
                 r.tvav,
                 r.proc_tvav,
                 r.pretd,
                 r.cont,
                 r.acont,
                 r.id_gestiune,
                 decode(NVL(r.id_rul_aux, 0), 0, NULL, r.id_rul_aux),
                 r.serie,
                 decode(NVL(r.id_valuta, 0), 0, NULL, r.id_valuta),
                 r.LOT,
                 r.ADATA_EXPIRARE,
                 decode(NVL(r.id_sucursala, 0), 0, NULL, r.id_sucursala),
                 decode(NVL(r.ID_LUCRARE_REZ, 0), 0, NULL, r.ID_LUCRARE_REZ),
                 decode(NVL(r.ID_PART_REZ, 0), 0, NULL, r.ID_PART_REZ)) s0
          ON (s0.an = s1.an and
          s0.luna = s1.luna and
          s0.id_articol = s1.id_articol and
          s0.pret = s1.pret and
          s0.pretv = s1.pretv and
          s0.tvav = s1.tvav and
          s0.proc_tvav = s1.proc_tvav and
          NVL(s0.pretd, 0) = NVL(s1.pretd, 0) and
          s0.cont = s1.cont and
          NVL(s0.acont, ''XXXX'') = NVL(s1.acont, ''XXXX'') and
          s0.id_gestiune = s1.id_gestiune and
          NVL(s0.id_rul_aux, 0) = NVL(s1.id_rul_aux, 0) and
          NVL(s0.serie, ''+_'') = NVL(s1.serie, ''+_'') and
          NVL(s0.id_valuta, 0) = NVL(s1.id_valuta, 0) and
          NVL(s0.LOT, ''+_'') = NVL(s1.LOT, ''+_'') AND
          NVL(s0.ADATA_EXPIRARE, to_date(''1900-01-01'', ''YYYY-MM-DD'')) = NVL(s1.ADATA_EXPIRARE, to_date(''1900-01-01'', ''YYYY-MM-DD'')) and
          NVL(s0.id_sucursala, 0) = NVL(s1.id_sucursala, 0) AND
          NVL(s0.ID_LUCRARE_REZ, 0) = NVL(s1.ID_LUCRARE_REZ, 0) AND
          NVL(s0.ID_PART_REZ, 0) = NVL(s1.ID_PART_REZ, 0))
      WHEN MATCHED THEN
        UPDATE set s1.cant = s1.cant + s0.cant, s1.cante = s1.cante + s0.cante, s1.dataora = s0.dataora, s1.datain = s0.datain, s1.dataout = s0.dataout, s1.tva = s0.tva
      WHEN NOT MATCHED THEN
        insert
          (an,
           luna,
           id_articol,
           pret,
           tva,
           pretv,
           tvav,
           proc_tvav,
           pretd,
           cant,
           cante,
           cont,
           acont,
           id_gestiune,
           dataora,
           datain,
           dataout,
           id_rul_aux,
           serie,
           id_valuta,
           lot,
           adata_expirare,
           id_sucursala,
           id_part_rez,
           id_lucrare_rez)
        values
          (s0.an,
           s0.luna,
           s0.id_articol,
           s0.pret,
           s0.tva,
           s0.pretv,
           s0.tvav,
           s0.proc_tvav,
           s0.pretd,
           s0.cant,
           s0.cante,
           s0.cont,
           s0.acont,
           s0.id_gestiune,
           s0.dataora,
           s0.datain,
           s0.dataout,
           s0.id_rul_aux,
           s0.serie,
           s0.id_valuta,
           s0.lot,
           s0.adata_expirare,
           s0.id_sucursala,
           s0.id_part_rez,
           s0.id_lucrare_rez)';
    --  pinfo(lcSql);
    -- pinfo('SCRIE_IN_STOC 2 ' || lcSql);
    execute immediate lcSql;
    -- pinfo('SCRIE_IN_STOC 2 ^');

    -- MATERII PRIME/MARFA LA PRET DE ACHIZITIE (nom_gestiuni.nr_pag not in (6,7))
    lcSql := 'merge into stoc s1
      using (select r.an,
                r.luna,
                r.id_articol,
                r.pret,
                max(r.tva) as tva,
                0 as pretv,
                0 as tvav,
                0 as proc_tvav,
                r.pretd,
                r.cont,
                r.acont,
                r.id_gestiune,
                decode(NVL(r.id_rul_aux, 0), 0, NULL, r.id_rul_aux) as id_rul_aux,
                r.serie,
                decode(NVL(r.id_valuta, 0), 0, NULL, r.id_valuta) as id_valuta,
                r.lot,
                r.adata_expirare,
                decode(NVL(r.id_sucursala, 0), 0, NULL, r.id_sucursala) as id_sucursala,
                decode(NVL(r.ID_LUCRARE_REZ, 0), 0, NULL, r.ID_LUCRARE_REZ) as id_lucrare_rez,
                decode(NVL(r.ID_PART_REZ, 0), 0, NULL, r.ID_PART_REZ) as id_part_rez,
                0 AS CANTS,
                SUM(r.CANT) as cant,
                SUM(r.CANTE) as cante,
                MAX(r.dataora) as dataora,
                MAX(nvl(r.datain,r.dataact)) AS datain,
                MAX(nvl(r.dataout, r.dataact)) AS dataout
               from ' || lcFrom || '
               GROUP BY
                 r.an,
                 r.luna,
                 r.id_articol,
                 r.pret,
                 r.pretd,
                 r.cont,
                 r.acont,
                 r.id_gestiune,
                 decode(NVL(r.id_rul_aux, 0), 0, NULL, r.id_rul_aux),
                 r.serie,
                 decode(NVL(r.id_valuta, 0), 0, NULL, r.id_valuta),
                 r.LOT,
                 r.ADATA_EXPIRARE,
                 decode(NVL(r.id_sucursala, 0), 0, NULL, r.id_sucursala),
                 decode(NVL(r.ID_LUCRARE_REZ, 0), 0, NULL, r.ID_LUCRARE_REZ),
                 decode(NVL(r.ID_PART_REZ, 0), 0, NULL, r.ID_PART_REZ)) s0
          ON (s0.an = s1.an and
          s0.luna = s1.luna and
          s0.id_articol = s1.id_articol and
          s0.pret = s1.pret and
          NVL(s0.pretd, 0) = NVL(s1.pretd, 0) and
          s0.cont = s1.cont and
          NVL(s0.acont, ''XXXX'') = NVL(s1.acont, ''XXXX'') and
          s0.id_gestiune = s1.id_gestiune and
          NVL(s0.id_rul_aux, 0) = NVL(s1.id_rul_aux, 0) and
          NVL(s0.serie, ''+_'') = NVL(s1.serie, ''+_'') and
          NVL(s0.id_valuta, 0) = NVL(s1.id_valuta, 0) and
          NVL(s0.LOT, ''+_'') = NVL(s1.LOT, ''+_'') AND
          NVL(s0.ADATA_EXPIRARE, to_date(''1900-01-01'', ''YYYY-MM-DD'')) = NVL(s1.ADATA_EXPIRARE, to_date(''1900-01-01'', ''YYYY-MM-DD'')) and
          NVL(s0.id_sucursala, 0) = NVL(s1.id_sucursala, 0) AND
          NVL(s0.ID_LUCRARE_REZ, 0) = NVL(s1.ID_LUCRARE_REZ, 0) AND
          NVL(s0.ID_PART_REZ, 0) = NVL(s1.ID_PART_REZ, 0))
      WHEN MATCHED THEN
        UPDATE set s1.cant = s1.cant + s0.cant, s1.cante = s1.cante + s0.cante, s1.dataora = s0.dataora, s1.datain = s0.datain, s1.dataout = s0.dataout, s1.tva = s0.tva
      WHEN NOT MATCHED THEN
        insert
          (an,
           luna,
           id_articol,
           pret,
           tva,
           pretv,
           tvav,
           proc_tvav,
           pretd,
           cant,
           cante,
           cont,
           acont,
           id_gestiune,
           dataora,
           datain,
           dataout,
           id_rul_aux,
           serie,
           id_valuta,
           lot,
           adata_expirare,
           id_sucursala,
           id_part_rez,
           id_lucrare_rez)
        values
          (s0.an,
           s0.luna,
           s0.id_articol,
           s0.pret,
           s0.tva,
           s0.pretv,
           s0.tvav,
           s0.proc_tvav,
           s0.pretd,
           s0.cant,
           s0.cante,
           s0.cont,
           s0.acont,
           s0.id_gestiune,
           s0.dataora,
           s0.datain,
           s0.dataout,
           s0.id_rul_aux,
           s0.serie,
           s0.id_valuta,
           s0.lot,
           s0.adata_expirare,
           s0.id_sucursala,
           s0.id_part_rez,
           s0.id_lucrare_rez)';

    --pinfo('SCRIE_IN_STOC 3 ' || lcSql);
    execute immediate lcSql;
    -- pinfo('SCRIE_IN_STOC 3 ^');

  END SCRIE_IN_STOC;
  ------------------------------------------------------------------------------------
  PROCEDURE SCRIE_IN_RUL_OBINV(tcS IN VARCHAR2) IS
    V_LISTA_CAMPURI VARCHAR2(2000) := '';

    LN_COD        NUMBER(20) := 0;
    lnIdFact      NUMBER(20) := 0;
    lnCuFacturi   NUMBER(5) := 0;
    LD_DATAORA    DATE;
    LC_UPDATE     VARCHAR2(100) := '';
    LC_INSERT     VARCHAR2(10000) := '';
    lnId_Util     act_temp.id_util%Type;
    LNAN          ACT_TEMP.AN%TYPE;
    LNLUNA        ACT_TEMP.LUNA%TYPE;
    lnIdSucursala act.id_sucursala%type;

  BEGIN

    lnIdSucursala   := PACK_CONTAFIN.GET_ID_SUCURSALA();
    V_LISTA_CAMPURI := pack_contafin.LISTA_CAMPURI(tcS, 'RUL_OBINV');

    LN_COD     := pack_contafin.GET_COD();
    LD_DATAORA := pack_contafin.GET_DATAORA();
    lnId_Util  := pack_contafin.GET_IDUTIL();
    lnIdFact   := get_idFact();
    LNAN       := pack_sesiune.GETAN();
    LNLUNA     := pack_sesiune.GETLUNA();

    if nvl(pack_contafin.nSuprascriereCod, 1) = 1 then
      UPDATE RUL_TEMP_OBINV
         SET COD     = LN_COD,
             DATAORA = LD_DATAORA,
             ID_UTIL = lnId_Util,
             AN      = lnAn,
             LUNA    = lnLuna;
    else
      if nvl(pack_contafin.nSuprascriereAnLuna, 1) = 1 then
        UPDATE RUL_TEMP_OBINV
           SET DATAORA = LD_DATAORA,
               ID_UTIL = lnId_Util,
               AN      = lnAn,
               LUNA    = lnLuna;
      else
        UPDATE RUL_TEMP_OBINV
           SET DATAORA = LD_DATAORA, ID_UTIL = lnId_Util;
      end if;
    end if;

    if lnIdFact <> 0 and lnIdFact is not null then
      -- la modificare nu setez id_fact
      UPDATE RUL_TEMP SET ID_FACT = lnIdFact;
    end if;

    -- 23.02.2010
    -- Nu se suprascrie id_sucursala completat deja in act_temp
    UPDATE RUL_TEMP_OBINV
       SET ID_SUCURSALA = lnIdSucursala
     WHERE NVL(ID_SUCURSALA, 0) = 0;
    -- 23.02.2010 ^

    UPDATE RUL_TEMP_OBINV SET DATAIN = dataact WHERE CANT <> 0;

    UPDATE RUL_TEMP_OBINV SET DATAOUT = dataact WHERE CANTE <> 0;

    LC_INSERT := 'INSERT INTO ' || tcS || '.RUL_OBINV (' || V_LISTA_CAMPURI ||
                 ') (SELECT ' || V_LISTA_CAMPURI || ' FROM RUL_TEMP_OBINV)';

    --    LC_INSERT := 'INSERT INTO ' || tcS || '.RUL (SELECT ' || V_LISTA_CAMPURI || ' FROM RUL_TEMP)';
    EXECUTE IMMEDIATE LC_INSERT;

    --    DBMS_OUTPUT.PUT_LINE('INREGISTRARI INSERATE: ' || SQL%ROWCOUNT);

  END SCRIE_IN_RUL_OBINV;

  ------------------------------------------------------------------------------------
  PROCEDURE SCRIE_IN_STOC_OBINV(tcS        IN VARCHAR2,
                                tnAn       in number,
                                tnLuna     in number,
                                tnScrie    number,
                                tcTabel    in varchar2,
                                tcIdTabel  in varchar2,
                                tcCondSpec in varchar2) IS
  BEGIN
    SCRIE_IN_STOC_OBINV(tnAn, tnLuna, tnScrie);
  END SCRIE_IN_STOC_OBINV;
  ------------------------------------------------------------------------------------
  PROCEDURE SCRIE_IN_STOC_OBINV(tnAn    in number,
                                tnLuna  in number,
                                tnScrie number) IS

    lcSql  varchar2(32000);
    lcFrom varchar2(1000);

    lnAn        act_temp.an%Type := tnAn;
    lnLuna      act_temp.luna%Type := tnLuna;
    lcContObInv stoc_obinv.cont%Type;

    lnIdSucursala    act.id_sucursala%type;
    lcWhereSucursale varchar2(1000);
  BEGIN
    lnIdSucursala := PACK_CONTAFIN.GET_ID_SUCURSALA();
    if lnIdSucursala is not null then
      lcWhereSucursale := ' and id_sucursala = ' || lnIdSucursala;
    end if;

    lcContObInv := pack_sesiune.getOptiuneFirma(USER, 'CONTOBINV');
    if lcContObinv is null then
      lcContObinv := '8039';
    end if;

    IF tnScrie = 1 THEN
      -- REFACERE
      lcFrom := 'rul_obinv where sters = 0  and an = ' || lnAn ||
                ' and luna = ' || lnLuna || lcWhereSucursale;
    ELSE
      -- SCRIERE
      lcFrom := 'rul_temp_obinv where an = ' || lnAn || ' and luna = ' ||
                lnLuna;
    END IF;

    lcSql := 'merge into stoc_obinv s1
      using (select an,
               luna,
               id_articol,
               pret,
               max(tva) as tva,
               max(pretv) as pretv,
               max(tvav) as tvav,
               max(proc_tvav) as proc_tvav,
               max(pretd) as pretd,
               0 as cants,
               sum(cant) as cant,
               sum(cante) as cante,
               (case when substr(cont,1,2) = ''80'' then ''' ||
             lcContObInv || ''' else cont end) as cont,
               acont,
               id_gestiune,
               max(dataora) as dataora,
               max(datain) as datain,
               max(dataout) as dataout,
               DATAPIF,
               DNS_LUNI,
               decode(NVL(id_responsabil, 0), 0, NULL, id_responsabil) as id_responsabil,
               decode(NVL(id_rul_aux, 0), 0, NULL, id_rul_aux) as id_rul_aux,
               serie,
               lot,
               decode(NVL(id_sucursala, 0), 0, NULL, id_sucursala) as id_sucursala
          from ' || lcFrom || '
         group by an,
                  luna,
                  id_articol,
                  pret,
                  case when substr(cont,1,2) = ''80'' then ''' ||
             lcContObInv || ''' else cont end,
                  acont,
                  id_gestiune,
                  DATAPIF,
                  DNS_LUNI,
                  decode(NVL(id_responsabil, 0), 0, NULL, id_responsabil),
                  decode(NVL(id_rul_aux, 0), 0, NULL, id_rul_aux),
                  serie,
                  lot,
                  decode(NVL(id_sucursala, 0), 0, NULL, id_sucursala)) s0
          ON (s0.an = s1.an and
              s0.luna = s1.luna and
              s0.id_articol = s1.id_articol and
              s0.pret = s1.pret and
              s0.cont = s1.cont and
              NVL(s0.acont, ''XXXX'') = NVL(s1.acont, ''XXXX'') and
              s0.id_gestiune = s1.id_gestiune and
              NVL(s0.DATAPIF, to_date(''1900-01-01'', ''YYYY-MM-DD'')) = NVL(s1.DATAPIF, to_date(''1900-01-01'', ''YYYY-MM-DD'')) and
              NVL(s0.dns_luni, 0) = NVL(s1.dns_luni, 0) AND
              NVL(s0.id_responsabil, 0) = NVL(s1.id_responsabil, 0) and
              NVL(s0.id_rul_aux, 0) = NVL(s1.id_rul_aux, 0) and
              NVL(s0.serie, ''+_'') = NVL(s1.serie, ''+_'') and
              NVL(s0.lot, ''+_'') = NVL(s1.lot, ''+_'') and
              NVL(s0.id_sucursala, 0) = NVL(s1.id_sucursala, 0))
      WHEN MATCHED THEN
        UPDATE set s1.cant = s1.cant + s0.cant, s1.cante = s1.cante + s0.cante, s1.dataora = s0.dataora, s1.datain = s0.datain, s1.dataout = s0.dataout,
        s1.tva = s0.tva, s1.pretv = s0.pretv, s1.tvav = s0.tvav, s1.proc_tvav = s0.proc_tvav, s1.pretd = s0.pretd
      WHEN NOT MATCHED THEN
        insert
          (an,
           luna,
           id_articol,
           pret,
           tva,
           pretv,
           tvav,
           proc_tvav,
           pretd,
           cants,
           cant,
           cante,
           cont,
           acont,
           id_gestiune,
           dataora,
           datain,
           dataout,
           DATAPIF,
           DNS_LUNI,
           id_responsabil,
           id_rul_aux,
           serie,
           lot,
           id_sucursala)
        values
          (s0.an,
           s0.luna,
           s0.id_articol,
           s0.pret,
           s0.tva,
           s0.pretv,
           s0.tvav,
           s0.proc_tvav,
           s0.pretd,
           s0.cants,
           s0.cant,
           s0.cante,
           s0.cont,
           s0.acont,
           s0.id_gestiune,
           s0.dataora,
           s0.datain,
           s0.dataout,
           s0.DATAPIF,
           s0.DNS_LUNI,
           s0.id_responsabil,
           s0.id_rul_aux,
           s0.serie,
           s0.lot,
           s0.id_sucursala)';
    -- PINFO(lcSql);
    execute immediate lcSql;

  END SCRIE_IN_STOC_OBINV;
  ------------------------------------------------------------------------------------
  PROCEDURE SCRIE_IN_STOC_OBINV_OLD(tcS        IN VARCHAR2,
                                    tnAn       in number,
                                    tnLuna     in number,
                                    tnScrie    number,
                                    tcTabel    in varchar2,
                                    tcIdTabel  in varchar2,
                                    tcCondSpec in varchar2) IS

    CURSOR CRS_TEMP IS
      SELECT an,
             luna,
             id_articol,
             id_gestiune,
             cont,
             acont,
             pret,
             id_responsabil,
             datapif,
             dns_luni,
             cant,
             cante,
             dataora,
             dataout,
             datain,
             id_rul_aux,
             serie,
             tva,
             id_sucursala
        FROM RUL_TEMP_OBINV
       WHERE 1 = 2;

    pCursor   tip_ref_cursor;
    lnId_Stoc Act_Temp.Id_Act%Type;
    lcSelect  varchar2(1000);
    lcSearcha varchar2(1000);
    lnStoca   number;
    item      CRS_TEMP%Rowtype;
    ignore    number := 0;
    V_DATAPIF DATE;
    v_acont   varchar2(4) := '';
    --    V_LISTA_CAMPURI VARCHAR2(2000) := '';
    lnAn         act_temp.an%Type := tnAn;
    lnLuna       act_temp.luna%Type := tnLuna;
    lcTabel      varchar2(50) := tcTabel;
    lcIdTabel    varchar2(50) := tcIdTabel;
    lcCondSpec   varchar2(100) := tcCondSpec;
    lcContObInv  stoc_obinv.cont%Type;
    lcUpdateStoc varchar2(1000);

    lnIdSucursala    act.id_sucursala%type;
    lcWhereSucursale varchar2(1000);
  BEGIN
    lnIdSucursala := PACK_CONTAFIN.GET_ID_SUCURSALA();
    if lnIdSucursala is not null then
      lcWhereSucursale := ' and id_sucursala = ' || lnIdSucursala;
    end if;

    lcContObInv := pack_sesiune.getOptiuneFirma(tcS, 'CONTOBINV');
    if lcContObinv is null then
      lcContObinv := '8039';
    end if;
    if lcTabel is null then
      lcTabel := 'Stoc_obinv';
    end if;
    if lcIdTabel is null then
      lcIdTabel := 'id_Stoc';
    end if;
    if tcCondSpec is null then
      lcCondSpec := '2=2';
    end if;
    /*    V_LISTA_CAMPURI := pack_contafin.LISTA_CAMPURI('contafin_oracle',
    'RUL_TEMP_OBINV');*/

    lcSearcha := 'select ' || lcIdTabel || ' from ' || tcs || '.' ||
                 lcTabel ||
                 ' where an = :1 and luna = :2 and id_articol = :3 and id_gestiune = :4 and cont = :5 and nvl(acont,''XXXX'') = :6 and pret = :7 and NVL(id_responsabil,0) = NVL(:8,0) and nvl(datapif,to_date(''1900-01-01'',''YYYY-MM-DD'')) = :9 and dns_luni = :10 and NVL(id_rul_aux,0) = :11 and NVL(serie,''+_'') = :12 and nvl(id_sucursala,0) = :13 and ' ||
                 lcCondSpec;
    IF tnScrie = 1 THEN
      -- REFACERE
      lnAn   := tnAn;
      lnLuna := tnLuna;

      lcSelect := 'select an, luna, id_articol, id_gestiune, cont, acont, pret, id_responsabil, datapif, dns_luni, cant, cante, dataora, dataout, datain, id_rul_aux, serie, tva, id_sucursala
                  from ' || tcS ||
                  '.rul_obinv
                   where an = :1 and luna = :2 and sters = 0 ' ||
                  lcWhereSucursale;
    ELSE
      -- SCRIERE
      /*      lnAn   := pack_contafin.GET_AN();
      lnLuna := pack_contafin.GET_LUNA();*/
      lnAn     := tnAn;
      lnLuna   := tnLuna;
      lcSelect := 'select an, luna, id_articol, id_gestiune, cont, acont, pret, id_responsabil, datapif, dns_luni, cant, cante, dataora, dataout, datain, id_rul_aux, serie, tva, id_sucursala
                  from rul_Temp_OBINV
                    where an = :1 and luna = :2 ';
    END IF;

    lnStoca := dbms_sql.open_cursor;
    DBMS_SQL.PARSE(lnStoca, lcSearcha, DBMS_SQL.native);
    DBMS_SQL.DEFINE_COLUMN(lnStoca, 1, lnId_Stoc);

    open pCursor for lcSelect
      USING lnAn, lnLuna;
    loop
      fetch pCursor
        into item;

      exit when pCursor%notfound;

      if item.acont is null then
        v_acont := 'XXXX';
      else
        v_acont := item.acont;
      END IF;

      if item.DATAPIF is null then
        V_DATAPIF := TO_DATE('1900-01-01', 'YYYY-MM-DD');
      ELSE
        V_DATAPIF := item.DATAPIF;
      END IF;

      DBMS_SQL.BIND_VARIABLE(lnStoca, ':1', item.an);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':2', item.luna);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':3', item.id_articol);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':4', item.id_gestiune);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':5', item.cont);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':6', V_ACONT);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':7', item.pret);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':8', item.id_responsabil);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':9', V_DATAPIF);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':10', item.dns_luni);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':11', NVL(item.id_rul_aux, 0));
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':12', NVL(item.serie, '+_'), 100);
      DBMS_SQL.BIND_VARIABLE(lnStoca, ':13', NVL(item.id_sucursala, 0));

      ignore := DBMS_SQL.EXECUTE(lnStoca);

      IF DBMS_SQL.FETCH_ROWS(lnStoca) > 0 THEN
        DBMS_SQL.COLUMN_VALUE(lnStoca, 1, lnId_Stoc);

        -- modificare
        lcUpdateStoc := 'UPDATE ' || tcs || '.' || lcTabel ||
                        ' set cant = cant + :1, cante = cante + :2, dataora = :3';

        if item.dataout is not null and item.cante > 0 then
          lcUpdateStoc := lcUpdateStoc || ',dataout = to_date(''' ||
                          to_char(item.dataout, 'DDMMYYYY') ||
                          ''',''DDMMYYYY'')';
        end if;

        if item.datain is not null and item.cant > 0 then
          lcUpdateStoc := lcUpdateStoc || ',datain = to_date(''' ||
                          to_char(item.datain, 'DDMMYYYY') ||
                          ''',''DDMMYYYY'')';
        end if;

        lcUpdateStoc := lcUpdateStoc || ' where ' || lcIdTabel || ' = :4';

        EXECUTE IMMEDIATE lcUpdateStoc
          USING item.cant, item.cante, item.dataora, lnId_Stoc;
        -- modificare ^
      ELSE

        EXECUTE IMMEDIATE 'INSERT INTO ' || tcs || '.' || lcTabel ||
                          ' (an, luna, id_articol, pret, cant, cante, cont, acont, id_gestiune, dataora, datain, dataout, id_responsabil, datapif, dns_luni,id_rul_aux, serie,tva,id_sucursala)
                                VALUES (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13, :14, :15, :16, :17,:18,:19)'
          USING item.an, item.luna, item.id_articol, item.pret, item.cant, item.cante, item.cont, item.acont, item.id_gestiune, item.dataora, item.datain, item.dataout, item.id_responsabil, item.datapif, item.dns_luni, item.id_rul_aux, item.serie, item.tva, item.id_sucursala;
      END IF;

    end loop;
    close pCursor;

    DBMS_SQL.close_cursor(lnStoca);

  END SCRIE_IN_STOC_OBINV_OLD;
  ------------------------------------------------------------------------------------
  PROCEDURE SET_DATAORA(V_DATAORA IN ACT_TEMP.DATAORA%TYPE) IS
  BEGIN
    if V_DATAORA IS NULL THEN
      pack_contafin.dDataOra := SYSDATE;
    else
      pack_contafin.dDataOra := V_DATAORA;
    end if;
  END SET_DATAORA;

  ------------------------------------------------------------------------------------
  FUNCTION GET_DATAORA RETURN ACT_TEMP.DATAORA%TYPE IS
  BEGIN
    RETURN PACK_CONTAFIN.dDataOra;
  END GET_DATAORA;

  ------------------------------------------------------------------------------------
  /* -- 25.02.2013 : am comentat pentru ca se face unirea id_fact pe listarea din JC/JV
    PROCEDURE SET_IDFACT(tdDataAct   ACT_TEMP.DATAACT%TYPE,
                       tcSerie_Act ACT_TEMP.SERIE_ACT%TYPE,
                       tnNrAct     ACT_TEMP.NRACT%TYPE,
                       tnId_Ctr    ACT_TEMP.ID_CTR%TYPE) IS
    V_ID_FACT DOCUMENTE.ID_DOC%TYPE;
  BEGIN
    BEGIN
      SELECT ID_DOC
        into pack_contafin.nIdFact
        FROM DOCUMENTE
       WHERE NRACT = tnNrAct
         AND NVL(SERIE_ACT, '+-') = NVL(tcSerie_Act, '+-')
         AND DATAACT = tdDataAct
         AND NVL(ID_CTR, 0) = NVL(tnId_Ctr, 0)
         AND STERS = 0;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        SELECT SEQ_IdFact.NEXTVAL into pack_contafin.nIdFact FROM DUAL;
    END;
  END SET_IDFACT;*/
  ------------------------------------------------------------------------------------
  PROCEDURE SET_IDFACT(V_GCS IN VARCHAR2) IS
  BEGIN
    SELECT SEQ_IdFact.NEXTVAL into pack_contafin.nIdFact FROM DUAL;
  END SET_IDFACT;

  ---------------------------------------------------------------
  FUNCTION GET_IDFACT return act_temp.id_fact%Type as
  BEGIN
    return pack_contafin.nIdFact;
  END GET_IDFACT;

  ---------------------------------------------------------------
  FUNCTION GENEREAZA_COD(V_GCS VARCHAR2) RETURN ACT_TEMP.COD%TYPE IS
  BEGIN
    SELECT SEQ_COD.NEXTVAL INTO PACK_CONTAFIN.nCod FROM DUAL;
    return PACK_CONTAFIN.nCod;
  END GENEREAZA_COD;
  ---------------------------------------------------------------
  PROCEDURE SET_COD(V_GCS VARCHAR2) IS
  BEGIN
    SELECT SEQ_COD.NEXTVAL INTO PACK_CONTAFIN.nCod FROM DUAL;
  END;

  ---------------------------------------------------------------
  FUNCTION GET_COD return ACT_TEMP.COD%TYPE as
  BEGIN
    return PACK_CONTAFIN.nCod;
  END GET_COD;

  ---------------------------------------------------------------
  PROCEDURE SET_ID_SUCURSALA(tnIdSucursala act_temp.id_sucursala%type) IS
  BEGIN
    PACK_CONTAFIN.nIdSucursala := tnIdSucursala;
  END SET_ID_SUCURSALA;

  ---------------------------------------------------------------
  FUNCTION GET_ID_SUCURSALA RETURN act_temp.id_sucursala%type is
  BEGIN
    RETURN PACK_CONTAFIN.nIdSucursala;
  END GET_ID_SUCURSALA;

  ---------------------------------------------------------------
  PROCEDURE SET_ID_FIRMA(tnIdFirma act_temp.id_sucursala%type) IS
  BEGIN
    PACK_CONTAFIN.nIdFirma := tnIdFirma;
  END SET_ID_FIRMA;

  ---------------------------------------------------------------
  FUNCTION GET_ID_FIRMA RETURN act_temp.id_sucursala%type is
  BEGIN
    RETURN PACK_CONTAFIN.nIdFirma;
  END GET_ID_FIRMA;

  ---------------------------------------------------------------

  PROCEDURE SET_DATA_RON(V_GCS VARCHAR2) IS
    lcDataRon varchar2(50);

  BEGIN
    BEGIN
      SELECT varvalue
        into lcDataRon
        from optiuni
       where upper(varname) = 'DATARON';
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        NULL;
    END;

    if lcDataRon is not null then
      pack_contafin.nAnRON   := to_number(substr(lcDataRon, 4, 4));
      pack_contafin.nLunaRON := to_number(substr(lcDataRon, 1, 2));
    else
      pack_contafin.nAnRON   := 2005;
      pack_contafin.nLunaRON := 7;
    end if;
  END;

  ---------------------------------------------------------------
  FUNCTION GET_ANRON RETURN ACT_TEMP.AN%TYPE IS
  BEGIN
    return pack_contafin.nAnRON;
  END GET_ANRON;

  ---------------------------------------------------------------

  FUNCTION GET_LUNARON RETURN ACT_TEMP.LUNA%TYPE IS
  BEGIN
    return pack_contafin.nLunaRon;
  END GET_LUNARON;
  ---------------------------------------------------------------
  function getCotaTVAStandard(V_LUNA IN NUMBER, V_AN IN NUMBER) return number is
    V_COTA_TVA NUMBER(10, 3);
  begin
    --    Daca fac select max(proc_tva) from cote_tva, da eroare la deschidere de luna cand fac INSERT INTO cote_tva

    /*    select MAX(PROC_TVA)
      INTO V_COTA_TVA
      from cote_tva
     where an = V_AN
       and luna = V_LUNA
       and descriere = 'STANDARD';

    IF NVL(V_COTA_TVA, 0) = 0 THEN*/
    if V_AN * 12 + V_LUNA >= 2025 * 12 + 8 then
      V_COTA_TVA := 1.21;
    elsif (V_AN * 12 + V_LUNA >= 2017 * 12 + 1) then
      V_COTA_TVA := 1.19;
    elsif (V_AN * 12 + V_LUNA >= 2016 * 12 + 1) then
      V_COTA_TVA := 1.20;
    elsif (V_AN * 12 + V_LUNA >= 2015 * 12 + 1) then
      V_COTA_TVA := 1.24;
    else
      V_COTA_TVA := 1.21;
    end if;
    /*    END IF;*/
    return V_COTA_TVA;
  end getCotaTVAStandard;
  ---------------------------------------------------------------
  PROCEDURE SET_IDUTIL(tnIdUtil act_temp.id_util%Type) IS
  BEGIN
    pack_contafin.nIdUtil := tnIdUtil;
  END SET_IDUTIL;
  ---------------------------------------------------------------
  FUNCTION GET_IDUTIL RETURN ACT_TEMP.ID_UTIL%TYPE IS
  BEGIN
    return pack_contafin.nIdUtil;
  END GET_IDUTIL;
  ---------------------------------------------------------------
  PROCEDURE SETARE_FACTURA(tcs VARCHAR2) --RETURN NUMBER IS
   IS
    --   E_FACTURA NUMBER(1) := 0;
    lcFromD  varchar2(1000);
    lcFromC  varchar2(1000);
    lcSelect varchar2(1000);
    --    lcWhere   varchar2(1000);
    lcUnion varchar2(1000);
    --    lcFrom    varchar2(1000);
    lcJoin varchar2(1000);
    lnCont pls_integer; --number(10);
    --    lcS        varchar2(100) := tcS;
    lcCont     varchar2(4);
    lcExceptie varchar2(4);
    lnPozitie  number(1);
    lnFelCont  number(1);
    lnInvers   number(1);
    lcScd      varchar2(4);
    lcScc      varchar2(4);
    ignore     number(1);

  BEGIN

    lcSelect := 'SELECT DISTINCT C.CONT, D.FEL_CONT, E.CONT_C AS EXCEPTIE, E.INVERS, E.DEBIT FROM ';
    lcFromD  := '(select distinct cont from ' || tcs ||
                '.vconfig_cont_ireg where cu_inregistrari = 1 INTERSECT select distinct scd ' ||
                'from act_temp) C
                left join ' || tcs ||
                '.vconfig_cont_ireg D on C.CONT = D.CONT
                left join ' || tcs ||
                '.exceptii_ireg E on C.CONT = E.CONT AND E.INVERS = 1 ';
    lcFromC  := '(select distinct cont from ' || tcs ||
                '.vconfig_cont_ireg where cu_inregistrari = 1 INTERSECT select distinct scc ' ||
                'from act_temp) C
                left join ' || tcs ||
                '.vconfig_cont_ireg D on C.CONT = D.CONT
                left join ' || tcs ||
                '.exceptii_ireg E on C.CONT = E.CONT AND E.INVERS = 1';
    lcUnion  := ' UNION ';

    -- 12.10.2006
    -- MARIUS.MUTU
    -- CURATARE -1 DE PE ID_FACT
    UPDATE ACT_TEMP SET ID_FACTD = 0 WHERE ID_FACTD = -1;
    UPDATE ACT_TEMP SET ID_FACTC = 0 WHERE ID_FACTC = -1;

    lnCont := dbms_sql.open_cursor;
    -- selectez toate conturile din ACT_TEMP care au INREGISTRARI + exceptiile lor
    DBMS_SQL.PARSE(lnCont,
                   lcSelect || lcFromD || lcJoin || lcUnion || lcSelect ||
                   lcFromC || lcJoin || ' order by 1',
                   DBMS_SQL.native);

    DBMS_SQL.DEFINE_COLUMN(lnCont, 1, lcCont, 4);
    DBMS_SQL.DEFINE_COLUMN(lnCont, 2, lnFelCont);
    DBMS_SQL.DEFINE_COLUMN(lnCont, 3, lcExceptie, 4);
    DBMS_SQL.DEFINE_COLUMN(lnCont, 4, lnInvers);
    DBMS_SQL.DEFINE_COLUMN(lnCont, 5, lnPozitie);

    ignore := DBMS_SQL.EXECUTE(lnCont);

    LOOP
      IF DBMS_SQL.FETCH_ROWS(lnCont) > 0 THEN
        -- get column values of the row
        DBMS_SQL.COLUMN_VALUE(lnCont, 1, lcCont);
        DBMS_SQL.COLUMN_VALUE(lnCont, 2, lnFelCont);
        DBMS_SQL.COLUMN_VALUE(lnCont, 3, lcExceptie);
        DBMS_SQL.COLUMN_VALUE(lnCont, 4, lnInvers);
        DBMS_SQL.COLUMN_VALUE(lnCont, 5, lnPozitie);

        lnInvers   := nvl(lnInvers, -1);
        lnPozitie  := nvl(lnPozitie, -1);
        lcExceptie := nvl(lcExceptie, 'XXXX');
        /*
        TODO: owner="marius.mutu" category="Fix" priority="1 - High" created="30.10.2006"
        text="ID_FACTD,ID_FACTC = -1 UNDE CONT_EXCEPTIE <> TOATE CONTURILE DE EXCEPTIE"
        */
        if lnFelCont = 0 then
          EXECUTE IMMEDIATE 'UPDATE ACT_TEMP SET id_fact = -1 where scd = :1 and (id_fact = 0 or id_fact is null) '
            USING lcCont;
          EXECUTE IMMEDIATE 'UPDATE ACT_TEMP SET id_factc = -1 where scc = :1 and scd <> :2 and (id_factc = 0 or id_factc is null) '
            USING lcCont, lcExceptie;
        else
          EXECUTE IMMEDIATE 'UPDATE ACT_TEMP SET id_fact = -1 where scc = :1 and (id_fact = 0 or id_fact is null)'
            USING lcCont;
          EXECUTE IMMEDIATE 'UPDATE ACT_TEMP SET id_factd = -1 where scd = :1 and scc <> :2 and (id_factd = 0 or id_factd is null) '
            USING lcCont, lcExceptie;
        end if;

        if lnInvers = 1 then
          if lnPozitie = 0 then
            lcScd := lcCont;
            lcScc := lcExceptie;
            EXECUTE IMMEDIATE 'UPDATE ACT_TEMP SET id_fact = -1 where scd = :1 and scc = :2 and (id_fact = 0 or id_fact is null)'
              USING lcScd, lcScc;
          end if;

          if lnPozitie = 1 then
            lcScc := lcCont;
            lcScD := lcExceptie;
            EXECUTE IMMEDIATE 'UPDATE ACT_TEMP SET id_fact = -1 where scd = :1 and scc = :2 and (id_fact = 0 or id_fact is null)'
              USING lcScd, lcScc;
          end if;
        end if;
      else
        exit;
      end if;
    end loop;

    dbms_sql.close_cursor(lnCont);
    -- 05.04.2005 -- MARIUS, AM PUS ID_FACT = -1 AI SA GENEREZE ID_FACT LA ORICE DOCUMENT
    EXECUTE IMMEDIATE 'UPDATE ACT_TEMP SET id_fact = -1 where (id_fact = 0 or id_fact is null) ';
    --  RETURN E_FACTURA;
  END SETARE_FACTURA;

  ---------------------------------------------------------------
  FUNCTION LISTA_CAMPURI(V_GCS VARCHAR2, tcTableName IN VARCHAR2)
    RETURN VARCHAR2 IS
    V_LISTA_CAMPURI VARCHAR2(2000) := '';
    --    I               NUMBER;
    lcTableName varchar2(100) := upper(tcTableName);
  BEGIN
    --    I := 0;
    FOR item in (select column_name
                   from all_tab_columns t
                  where t.TABLE_NAME = lcTableName
                    AND t.OWNER = upper(V_GCS)) LOOP
      --                    I := I + 1;
      --                    DBMS_OUTPUT.put_line(I || ', ' || ITEM.COLUMN_NAME);
      V_LISTA_CAMPURI := V_LISTA_CAMPURI || ITEM.COLUMN_NAME || ',';
    END LOOP;
    IF LENGTH(V_LISTA_CAMPURI) > 1 THEN
      V_LISTA_CAMPURI := SUBSTR(V_LISTA_CAMPURI,
                                1,
                                LENGTH(V_LISTA_CAMPURI) - 1);
    END IF;
    RETURN V_LISTA_CAMPURI;

  END LISTA_CAMPURI;

  ------------------------------------------------------------------------------
  PROCEDURE set_suprascriereCod(V_VALOARE IN NUMBER) IS
  BEGIN
    pack_contafin.nSuprascriereCod := V_VALOARE;
  END set_suprascriereCod;
  ------------------------------------------------------------------------------
  PROCEDURE set_suprascriereAnLuna(V_VALOARE IN NUMBER) IS
  BEGIN
    pack_contafin.nSuprascriereAnLuna := V_VALOARE;
  END set_suprascriereAnLuna;
  ------------------------------------------------------------------------------
  PROCEDURE RESET_VARIABLES IS
  BEGIN
    nCod                := 0;
    nIdFact             := 0;
    nIdUtil             := 0;
    dDataOra            := SYSDATE;
    nSuprascriereCod    := 1;
    nSuprascriereAnLuna := 1;
  END RESET_VARIABLES;

  --------------------------
  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 - RO21TR)
                    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 - RO11TR)
                    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;
  -------------------------------------------------------------------
  procedure SCRIE_JC_2007(tnAn    JC2007.AN%TYPE,
                          tnLuna  JC2007.LUNA%TYPE,
                          tnScrie number) IS

    lcSql                    clob;
    lcContTvaDeductibil      ACT.SCD%TYPE := '4426';
    lcContTvaColectat        ACT.SCD%TYPE := '4427';
    lcContTvaNeexigibil      ACT.SCD%TYPE := '4428';
    lcContDebitRegularizare  VARCHAR2(100) := '408';
    lcContCreditRegularizare VARCHAR2(100) := '401,404,4511,462';
    lnIdMinJtvaColoana       JTVA_COLOANE.ID_JTVA_COLOANA%TYPE := 101;
    lnIdMaxJtvaColoana       JTVA_COLOANE.ID_JTVA_COLOANA%TYPE := 500;
    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;

    -- COMPLETARE JC2007 CU ID_SET 10616 STORNARE PLATA/INCASARE
    pack_contafin.completeaza_jc_2007(tnAn,
                                      tnLuna,
                                      lcTabelSursa,
                                      lcCondSucursala);
    -- MERGE replaced with FORALL BULK operations to avoid Oracle XE bugs and improve performance
    DECLARE
      CURSOR c_source IS
        SELECT ID_SUCURSALA,
               AN,
               LUNA,
               ID_FACT,
               ID_FDOC,
               NRACT,
               SERIE_ACT,
               DATAACT,
               DATAIREG,
               ID_PART,
               COD,
               (CASE
                 WHEN NOTA_TVA_EX = 1 THEN
                  0
                 WHEN REGULARIZARE > 0 THEN
                  TOTCTVA - RO24TR - RO20TR - RO21TR - RO11TR - RO19BCTR -
                  RO19BVTR - RO19BFTR - RO09BCTR - RO09BVTR - RO09BFTR -
                  RO05TR - FO21TR - FO24TR - FO20TR - FO19BCTR - FO19BVTR -
                  FO19BFTR - FO09BCTR - FO09BVTR - FO09BFTR - CE24TR -
                  CE20TR - CE5STR - CE9STR - CE21STR - CE24STR - CE20STR -
                  CE19STR - CEBCTR - CEBVTR - CEBFTR - TI21TR - TI11TR -
                  TI24TR - TI20TR - TI19BCTR - TI19BVTR - TI19BFTR -
                  TI09BVTR - TI09BFTR - RO24NTR - RO20NTR - RO21NTR -
                  RO11NTR - RO19NTR - RO09NTR - RO05NTR
                 ELSE
                  TOTCTVA - DECODE(RO21NB, 0, 0, RO21B) -
                  DECODE(RO11NB, 0, 0, RO11B) - DECODE(RO24NB, 0, 0, RO24B) -
                  DECODE(RO20NB, 0, 0, RO20B) -
                  DECODE(RO19NB, 0, 0, RO19BCB + RO19BVB + RO19BFB) -
                  DECODE(RO9NB, 0, 0, RO09BCB + RO09BVB + RO09BFB) -
                  DECODE(RO5NB, 0, 0, RO05B) - DECODE(RO21NT, 0, 0, RO21T) -
                  DECODE(RO11NT, 0, 0, RO11T) - DECODE(RO24NT, 0, 0, RO24T) -
                  DECODE(RO20NT, 0, 0, RO20T) -
                  DECODE(RO19NT, 0, 0, RO19BCT + RO19BVT + RO19BFT) -
                  DECODE(RO9NT, 0, 0, RO09BCT + RO09BVT + RO09BFT) -
                  DECODE(RO5NT, 0, 0, RO05T)
               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, RO19BCB + RO19BVB + RO19BFB, RO19NB) +
                  DECODE(RO9NB, 0, RO09BCB + RO09BVB + RO09BFB, RO9NB) +
                  DECODE(RO5NB, 0, RO05B, RO5NB) -
                  DECODE(RO24NB, 0, RO24TR, RO24NTR) -
                  DECODE(RO20NB, 0, RO20TR, RO20NTR) -
                  DECODE(RO21NB, 0, RO21TR, RO21NTR) -
                  DECODE(RO11NB, 0, RO11TR, RO11NTR) -
                  DECODE(RO19NB, 0, RO19BCTR + RO19BVTR + RO19BFTR, RO19NTR) -
                  DECODE(RO9NB, 0, RO09BCTR + RO09BVTR + RO09BFTR, RO09NTR) -
                  DECODE(RO5NB, 0, RO05TR, RO05NTR) - FO21TR - FO24TR -
                  FO20TR - FO19BCTR - FO19BVTR - FO19BFTR - FO09BCTR -
                  FO09BVTR - FO09BFTR - CE24TR - CE20TR - CE5STR - CE9STR -
                  CE21STR - CE24STR - CE20STR - CE19STR - CEBCTR - CEBVTR -
                  CEBFTR - TI21TR - TI11TR - TI24TR - TI20TR - TI19BCTR -
                  TI19BVTR - TI19BFTR - TI09BVTR - TI09BFTR

                 ELSE
                  DECODE(RO24NB, 0, RO24B, RO24NB) +
                  DECODE(RO20NB, 0, RO20B, RO20NB) +
                  DECODE(RO21NB, 0, RO21B, RO21NB) +
                  DECODE(RO11NB, 0, RO11B, RO11NB) +
                  DECODE(RO19NB, 0, RO19BCB + RO19BVB + RO19BFB, RO19NB) +
                  DECODE(RO9NB, 0, RO09BCB + RO09BVB + RO09BFB, RO9NB) +
                  DECODE(RO5NB, 0, RO05B, 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, RO19BCT + RO19BVT + RO19BFT, RO19NT) +
                      DECODE(RO9NT, 0, RO09BCT + RO09BVT + RO09BFT, RO9NT) +
                      DECODE(RO5NT, 0, RO05T, RO5NT)) AS TOTTVATAX,
               ROSCN + FOSCN + (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CEBCB - CEBCTR
                 ELSE
                  CEBCB
               END) + CEBCT + (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CEBVB - CEBVTR
                 ELSE
                  CEBVB
               END) + CEBVT + CEBVSC + CEBVN + (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CEBFB - CEBFTR
                 ELSE
                  CEBFB
               END) + CEBFT + CEBFSC + CEBFN + ROTN + ROTN50 + ROTN100 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 - RO21TR)
                 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 - RO11TR)

                 ELSE
                  DECODE(RO11B,
                         0,
                         DECODE(RO11T, 0, 0, ROUND(RO11T / 0.11, 2)),
                         RO11B)
               END) AS RO11B,
               RO11T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  DECODE(RO19BCB,
                         0,
                         DECODE(RO19BCT, 0, 0, ROUND(RO19BCT / 0.19, 2)),
                         RO19BCB - RO19BCTR)
                 ELSE
                  DECODE(RO19BCB,
                         0,
                         DECODE(RO19BCT, 0, 0, ROUND(RO19BCT / 0.19, 2)),
                         RO19BCB)
               END) AS RO19BCB,
               RO19BCT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  DECODE(RO19BVB,
                         0,
                         DECODE(RO19BVT, 0, 0, ROUND(RO19BVT / 0.19, 2)),
                         RO19BVB - RO19BVTR)
                 ELSE
                  DECODE(RO19BVB,
                         0,
                         DECODE(RO19BVT, 0, 0, ROUND(RO19BVT / 0.19, 2)),
                         RO19BVB)
               END) AS RO19BVB,
               RO19BVT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  DECODE(RO19BFB,
                         0,
                         DECODE(RO19BFT, 0, 0, ROUND(RO19BFT / 0.19, 2)),
                         RO19BFB - RO19BFTR)
                 ELSE
                  DECODE(RO19BFB,
                         0,
                         DECODE(RO19BFT, 0, 0, ROUND(RO19BFT / 0.19, 2)),
                         RO19BFB)
               END) AS RO19BFB,
               RO19BFT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  DECODE(RO09BCB,
                         0,
                         DECODE(RO09BCT, 0, 0, ROUND(RO09BCT / 0.09, 2)),
                         RO09BCB - RO09BCTR)
                 ELSE
                  DECODE(RO09BCB,
                         0,
                         DECODE(RO09BCT, 0, 0, ROUND(RO09BCT / 0.09, 2)),
                         RO09BCB)
               END) AS RO09BCB,
               RO09BCT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  DECODE(RO09BVB,
                         0,
                         DECODE(RO09BVT, 0, 0, ROUND(RO09BVT / 0.09, 2)),
                         RO09BVB - RO09BVTR)
                 ELSE
                  DECODE(RO09BVB,
                         0,
                         DECODE(RO09BVT, 0, 0, ROUND(RO09BVT / 0.09, 2)),
                         RO09BVB)
               END) AS RO09BVB,
               RO09BVT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  DECODE(RO09BFB,
                         0,
                         DECODE(RO09BFT, 0, 0, ROUND(RO09BFT / 0.09, 2)),
                         RO09BFB - RO09BFTR)
                 ELSE
                  DECODE(RO09BFB,
                         0,
                         DECODE(RO09BFT, 0, 0, ROUND(RO09BFT / 0.09, 2)),
                         RO09BFB)
               END) AS RO09BFB,
               RO09BFT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  DECODE(RO05B,
                         0,
                         DECODE(RO05T, 0, 0, ROUND(RO05T / 0.05, 2)),
                         RO05B - RO05TR)
                 ELSE
                  DECODE(RO05B,
                         0,
                         DECODE(RO05T, 0, 0, ROUND(RO05T / 0.05, 2)),
                         RO05B)
               END) AS RO05B,
               RO05T,
               ROSCN,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO21B - FO21TR
                 ELSE
                  FO21B
               END) AS FO21B,
               FO21T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO24B - FO24TR
                 ELSE
                  FO24B
               END) AS FO24B,
               FO24T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO20B - FO20TR
                 ELSE
                  FO20B
               END) AS FO20B,
               FO20T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO19BCB - FO19BCTR
                 ELSE
                  FO19BCB
               END) AS FO19BCB,
               FO19BCT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO19BVB - FO19BVTR
                 ELSE
                  FO19BVB
               END) AS FO19BVB,
               FO19BVT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO19BFB - FO19BFTR
                 ELSE
                  FO19BFB
               END) AS FO19BFB,
               FO19BFT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  XX21TIB - XX21TITR
                 ELSE
                  XX21TIB
               END) AS XX21TIB,
               XX21TIT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  XX11TIB - XX11TITR
                 ELSE
                  XX11TIB
               END) AS XX11TIB,
               XX11TIT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  XX19TIB - XX19TITR
                 ELSE
                  XX19TIB
               END) AS XX19TIB,
               XX19TIT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  XX9TIB - XX9TITR
                 ELSE
                  XX9TIB
               END) AS XX9TIB,
               XX9TIT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO09BCB - FO09BCTR
                 ELSE
                  FO09BCB
               END) AS FO09BCB,
               FO09BCT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO09BVB - FO09BVTR
                 ELSE
                  FO09BVB
               END) AS FO09BVB,
               FO09BVT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  FO09BFB - FO09BFTR
                 ELSE
                  FO09BFB
               END) AS FO09BFB,
               FO09BFT,
               FOSCN,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE24B - CE24TR
                 ELSE
                  CE24B
               END) AS CE24B,
               CE24T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE20B - CE20TR
                 ELSE
                  CE20B
               END) AS CE20B,
               CE20T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE5SB - CE5STR
                 ELSE
                  CE5SB
               END) AS CE5SB,
               CE5ST,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE9SB - CE9STR
                 ELSE
                  CE9SB
               END) AS CE9SB,
               CE9ST,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE24SB - CE24STR
                 ELSE
                  CE24SB
               END) AS CE24SB,
               CE24ST,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE20SB - CE20STR
                 ELSE
                  CE20SB
               END) AS CE20SB,
               CE20ST,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE21SB - CE21STR
                 ELSE
                  CE21SB
               END) AS CE21SB,
               CE21ST,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE19SB - CE19STR
                 ELSE
                  CE19SB
               END) AS CE19SB,
               CE19ST,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CEBCB - CEBCTR
                 ELSE
                  CEBCB
               END) AS CEBCB,
               CEBCT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CEBVB - CEBVTR
                 ELSE
                  CEBVB
               END) AS CEBVB,
               CEBVT,
               CEBVSC,
               CEBVN,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CEBFB - CEBFTR
                 ELSE
                  CEBFB
               END) AS CEBFB,
               CEBFT,
               CEBFSC,
               CEBFN,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE21CTB - CE21CTTR
                 ELSE
                  CE21CTB
               END) AS CE21CTB,
               CE21CTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE11CTB - CE11CTTR
                 ELSE
                  CE11CTB
               END) AS CE11CTB,
               CE11CTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE19CTB - CE19CTTR
                 ELSE
                  CE19CTB
               END) AS CE19CTB,
               CE19CTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE9CTB - CE9CTTR
                 ELSE
                  CE9CTB
               END) AS CE9CTB,
               CE9CTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE5CTB - CE5CTTR
                 ELSE
                  CE5CTB
               END) AS CE5CTB,
               CE5CTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE21FTB - CE21FTTR
                 ELSE
                  CE21FTB
               END) AS CE21FTB,
               CE21FTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE11FTB - CE11FTTR
                 ELSE
                  CE11FTB
               END) AS CE11FTB,
               CE11FTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE19FTB - CE19FTTR
                 ELSE
                  CE19FTB
               END) AS CE19FTB,
               CE19FTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE9FTB - CE9FTTR
                 ELSE
                  CE9FTB
               END) AS CE9FTB,
               CE9FTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  CE5FTB - CE5FTTR
                 ELSE
                  CE5FTB
               END) AS CE5FTB,
               CE5FTT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI24B - TI24TR
                 ELSE
                  TI24B
               END) AS TI24B,
               TI24T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI20B - TI20TR
                 ELSE
                  TI20B
               END) AS TI20B,
               TI20T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI21B - TI21TR
                 ELSE
                  TI21B
               END) AS TI21B,
               TI21T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI11B - TI11TR
                 ELSE
                  TI11B
               END) AS TI11B,
               TI11T,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI19BCB - TI19BCTR
                 ELSE
                  TI19BCB
               END) AS TI19BCB,
               TI19BCT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI19BVB - TI19BVTR
                 ELSE
                  TI19BVB
               END) AS TI19BVB,
               TI19BVT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI19BFB - TI19BFTR
                 ELSE
                  TI19BFB
               END) AS TI19BFB,
               TI19BFT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI09BVB - TI09BVTR
                 ELSE
                  TI09BVB
               END) AS TI09BVB,
               TI09BVT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  TI09BFB - TI09BFTR
                 ELSE
                  TI09BFB
               END) AS TI09BFB,
               TI09BFT,
               (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 - RO09NTR
                 ELSE
                  RO9NB
               END) AS RO9NB,
               RO9NT,
               (CASE
                 WHEN REGULARIZARE > 0 THEN
                  RO5NB - RO05NTR
                 ELSE
                  RO5NB
               END) AS RO5NB,
               RO5NT,
               ROTN,
               ROTN50,
               ROTN100,
               RORTD24,
               RORTD20,
               RORTD19,
               RORTD9,
               RORTD5,
               RORTD21,
               RORTD11
          FROM (SELECT JC.ID_SUCURSALA,
                       JC.AN,
                       JC.LUNA,
                       JC.ID_FACT,
                       MAX(JC.ID_FDOC) AS ID_FDOC,
                       JC.NRACT,
                       JC.SERIE_ACT,
                       JC.DATAACT,
                       JC.DATAIREG,
                       MAX(JC.ID_PART) AS ID_PART,
                       MIN(JC.COD) AS COD,
                       MIN(JC.NOTA_TVA_EX) AS NOTA_TVA_EX,
                       sum(CASE
                             WHEN NVL(JC.ID_JTVA_COLOANA, 0) > 100 and
                                  JC.ID_JTVA_COLOANA NOT IN (1001, 1004, 1005) THEN
                              JC.suma
                             ELSE
                              0
                           END) TOTCTVA,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA In (156,
                                                      176,
                                                      208,
                                                      212,
                                                      107,
                                                      109,
                                                      111,
                                                      154,
                                                      170,
                                                      172,
                                                      174,
                                                      178,
                                                      210,
                                                      214,
                                                      101,
                                                      103,
                                                      105,
                                                      188) THEN
                              JC.suma
                             ELSE
                              0
                           END) As totftvatax,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA In (157,
                                                      177,
                                                      209,
                                                      213,
                                                      108,
                                                      110,
                                                      112,
                                                      155,
                                                      171,
                                                      173,
                                                      175,
                                                      179,
                                                      211,
                                                      215,
                                                      102,
                                                      104,
                                                      106,
                                                      189) THEN
                              JC.suma
                             ELSE
                              0
                           END) As tottvatax,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 156, JC.suma, 0)) RO24B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 157, JC.suma, 0)) RO24T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 176, JC.suma, 0)) RO20B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 177, JC.suma, 0)) RO20T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 208, JC.suma, 0)) RO21B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 209, JC.suma, 0)) RO21T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 101, JC.suma, 0)) RO19BCB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 102, JC.suma, 0)) RO19BCT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 103, JC.suma, 0)) RO19BVB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 104, JC.suma, 0)) RO19BVT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 105, JC.suma, 0)) RO19BFB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 106, JC.suma, 0)) RO19BFT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 107, JC.suma, 0)) RO09BCB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 108, JC.suma, 0)) RO09BCT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 109, JC.suma, 0)) RO09BVB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 110, JC.suma, 0)) RO09BVT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 111, JC.suma, 0)) RO09BFB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 112, JC.suma, 0)) RO09BFT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 154, JC.suma, 0)) RO05B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 155, JC.suma, 0)) RO05T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 212, JC.suma, 0)) RO11B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 213, JC.suma, 0)) RO11T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 113, JC.suma, 0)) ROSCN,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 158, JC.suma, 0)) FO24B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 159, JC.suma, 0)) FO24T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 180, JC.suma, 0)) FO20B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 181, JC.suma, 0)) FO20T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 220, JC.suma, 0)) FO21B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 221, JC.suma, 0)) FO21T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 114, JC.suma, 0)) FO19BCB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 115, JC.suma, 0)) FO19BCT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 116, JC.suma, 0)) FO19BVB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 117, JC.suma, 0)) FO19BVT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 118, JC.suma, 0)) FO19BFB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 119, JC.suma, 0)) FO19BFT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 192, JC.suma, 0)) XX19TIB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 193, JC.suma, 0)) XX19TIT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 224, JC.suma, 0)) XX21TIB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 225, JC.suma, 0)) XX21TIT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 230, JC.suma, 0)) XX11TIB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 231, JC.suma, 0)) XX11TIT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 206, JC.suma, 0)) XX9TIB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 207, JC.suma, 0)) XX9TIT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 120, JC.suma, 0)) FO09BCB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 121, JC.suma, 0)) FO09BCT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 122, JC.suma, 0)) FO09BVB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 123, JC.suma, 0)) FO09BVT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 124, JC.suma, 0)) FO09BFB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 125, JC.suma, 0)) FO09BFT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 126, JC.suma, 0)) FOSCN,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 160, JC.suma, 0)) CE24B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 161, JC.suma, 0)) CE24T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 182, JC.suma, 0)) CE20B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 183, JC.suma, 0)) CE20T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 164, JC.suma, 0)) CE5SB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 165, JC.suma, 0)) CE5ST,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 166, JC.suma, 0)) CE9SB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 167, JC.suma, 0)) CE9ST,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 168, JC.suma, 0)) CE24SB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 169, JC.suma, 0)) CE24ST,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 184, JC.suma, 0)) CE20SB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 185, JC.suma, 0)) CE20ST,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 190, JC.suma, 0)) CE19SB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 191, JC.suma, 0)) CE19ST,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 222, JC.suma, 0)) CE21SB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 223, JC.suma, 0)) CE21ST,
                       sum(DECODE(JC.ID_JTVA_COLOANA,
                                  127,
                                  JC.suma,
                                  148,
                                  JC.suma,
                                  0)) CEBCB,
                       sum(DECODE(JC.ID_JTVA_COLOANA,
                                  128,
                                  JC.suma,
                                  149,
                                  JC.suma,
                                  0)) CEBCT,
                       sum(DECODE(JC.ID_JTVA_COLOANA,
                                  129,
                                  JC.suma,
                                  150,
                                  JC.suma,
                                  0)) CEBVB,
                       sum(DECODE(JC.ID_JTVA_COLOANA,
                                  130,
                                  JC.suma,
                                  151,
                                  JC.suma,
                                  0)) CEBVT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 131, JC.suma, 0)) CEBVSC,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 132, JC.suma, 0)) CEBVN,
                       sum(DECODE(JC.ID_JTVA_COLOANA,
                                  133,
                                  JC.suma,
                                  152,
                                  JC.suma,
                                  0)) CEBFB,
                       sum(DECODE(JC.ID_JTVA_COLOANA,
                                  134,
                                  JC.suma,
                                  153,
                                  JC.suma,
                                  0)) CEBFT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 135, JC.suma, 0)) CEBFSC,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 136, JC.suma, 0)) CEBFN,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 194, JC.suma, 0)) CE19CTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 195, JC.suma, 0)) CE19CTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 226, JC.suma, 0)) CE21CTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 227, JC.suma, 0)) CE21CTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 232, JC.suma, 0)) CE11CTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 233, JC.suma, 0)) CE11CTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 196, JC.suma, 0)) CE9CTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 197, JC.suma, 0)) CE9CTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 198, JC.suma, 0)) CE5CTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 199, JC.suma, 0)) CE5CTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 200, JC.suma, 0)) CE19FTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 201, JC.suma, 0)) CE19FTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 228, JC.suma, 0)) CE21FTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 229, JC.suma, 0)) CE21FTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 234, JC.suma, 0)) CE11FTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 235, JC.suma, 0)) CE11FTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 202, JC.suma, 0)) CE9FTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 203, JC.suma, 0)) CE9FTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 204, JC.suma, 0)) CE5FTB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 205, JC.suma, 0)) CE5FTT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 162, JC.suma, 0)) TI24B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 163, JC.suma, 0)) TI24T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 186, JC.suma, 0)) TI20B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 187, JC.suma, 0)) TI20T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 216, JC.suma, 0)) TI21B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 217, JC.suma, 0)) TI21T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 218, JC.suma, 0)) TI11B,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 219, JC.suma, 0)) TI11T,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 137, JC.suma, 0)) TI19BCB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 138, JC.suma, 0)) TI19BCT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 139, JC.suma, 0)) TI19BVB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 140, JC.suma, 0)) TI19BVT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 141, JC.suma, 0)) TI19BFB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 142, JC.suma, 0)) TI19BFT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 143, JC.suma, 0)) TI09BVB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 144, JC.suma, 0)) TI09BVT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 145, JC.suma, 0)) TI09BFB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 146, JC.suma, 0)) TI09BFT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 170, JC.suma, 0)) RO24NB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 171, JC.suma, 0)) RO24NT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 178, JC.suma, 0)) RO20NB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 179, JC.suma, 0)) RO20NT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 210, JC.suma, 0)) RO21NB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 211, JC.suma, 0)) RO21NT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 214, JC.suma, 0)) RO11NB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 215, JC.suma, 0)) RO11NT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 188, JC.suma, 0)) RO19NB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 189, JC.suma, 0)) RO19NT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 172, JC.suma, 0)) RO9NB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 173, JC.suma, 0)) RO9NT,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 174, JC.suma, 0)) RO5NB,
                       sum(DECODE(JC.ID_JTVA_COLOANA, 175, JC.suma, 0)) RO5NT,
                       SUM(JC.REGULARIZARE) AS REGULARIZARE,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 209 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO21TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 213 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO11TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 157 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO24TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 177 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO20TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 102 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO19BCTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 104 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO19BVTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 106 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO19BFTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 108 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO09BCTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 110 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO09BVTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 112 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO09BFTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 155 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) RO05TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 159 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO24TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 181 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO20TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 221 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO21TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 115 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO19BCTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 117 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO19BVTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 119 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO19BFTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 225 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) XX21TITR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 231 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) XX11TITR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 193 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) XX19TITR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 207 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) XX9TITR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 121 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO09BCTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 123 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO09BVTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 125 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) FO09BFTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 161 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE24TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 183 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE20TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 165 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE5STR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 167 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE9STR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 169 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE24STR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 185 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE20STR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 223 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE21STR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 191 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE19STR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA in (128, 149) AND
                                  JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CEBCTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA in (130, 151) AND
                                  JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CEBVTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA in (134, 153) AND
                                  JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CEBFTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 227 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE21CTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 233 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE11CTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 195 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE19CTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 197 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE9CTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 199 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE5CTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 229 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE21FTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 235 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE11FTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 201 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE19FTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 203 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE9FTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 205 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) CE5FTTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 217 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI21TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 219 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI11TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 163 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI24TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 187 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI20TR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 138 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI19BCTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 140 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI19BVTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 142 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI19BFTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 144 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI09BVTR,
                       SUM(CASE
                             WHEN JC.ID_JTVA_COLOANA = 146 AND JC.SCC = '4428' THEN
                              JC.suma
                             ELSE
                              0
                           END) TI09BFTR,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 171 AND SCC = '4428' THEN
                              SUMA
                             ELSE
                              0
                           END) RO24NTR,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 179 AND SCC = '4428' THEN
                              SUMA
                             ELSE
                              0
                           END) RO20NTR,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 211 AND SCC = '4428' THEN
                              SUMA
                             ELSE
                              0
                           END) RO21NTR,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 215 AND SCC = '4428' THEN
                              SUMA
                             ELSE
                              0
                           END) RO11NTR,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 189 AND SCC = '4428' THEN
                              SUMA
                             ELSE
                              0
                           END) RO19NTR,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 173 AND SCC = '4428' THEN
                              SUMA
                             ELSE
                              0
                           END) RO09NTR,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 175 AND SCC = '4428' THEN
                              SUMA
                             ELSE
                              0
                           END) RO05NTR,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1001 THEN
                              SUMA
                             ELSE
                              0
                           END) ROTN,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1004 THEN
                              SUMA
                             ELSE
                              0
                           END) ROTN50,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1005 THEN
                              SUMA
                             ELSE
                              0
                           END) ROTN100,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1011 THEN
                              SUMA
                             ELSE
                              0
                           END) RORTD24,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1012 THEN
                              SUMA
                             ELSE
                              0
                           END) RORTD20,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1013 THEN
                              SUMA
                             ELSE
                              0
                           END) RORTD19,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1014 THEN
                              SUMA
                             ELSE
                              0
                           END) RORTD9,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1015 THEN
                              SUMA
                             ELSE
                              0
                           END) RORTD5,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1018 THEN
                              SUMA
                             ELSE
                              0
                           END) RORTD21,
                       SUM(CASE
                             WHEN ID_JTVA_COLOANA = 1019 THEN
                              SUMA
                             ELSE
                              0
                           END) RORTD11
                  FROM (SELECT A.ID_SUCURSALA,
                               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_PARTD,
                                          (CASE
                                            WHEN A.SCC IN
                                                 (lcContTvaColectat, lcContTvaNeexigibil) OR
                                                 SUBSTR(A.SCC, 1, 1) = '5' OR
                                                 ID_JTVA_COLOANA in (1001, 1004, 1005) THEN
                                             -9999999999
                                            ELSE
                                             A.ID_PARTC
                                          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 NESOSITE
                               (CASE
                                 WHEN INSTR(lcContDebitRegularizare, A.scd) > 0 and
                                      INSTR(lcContCreditRegularizare, A.scc) > 0 THEN
                                  1
                                 ELSE
                                  0
                               END) AS REGULARIZARE,
                               ID_JTVA_COLOANA,
                               ROW_NUMBER() OVER(PARTITION BY a.nract ORDER BY a.nract) AS RN
                          FROM (SELECT ID_SUCURSALA,
                                       AN,
                                       LUNA,
                                       ID_FDOC,
                                       ID_FACT,
                                       NRACT,
                                       SERIE_ACT,
                                       DATAACT,
                                       DATAIREG,
                                       COD,
                                       SCD,
                                       SCC,
                                       ID_PARTD,
                                       ID_PARTC,
                                       ID_SET,
                                       SUMA,
                                       ID_JTVA_COLOANA,
                                       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 in
                                       (1001,
                                         1004,
                                         1005,
                                         1011,
                                         1012,
                                         1013,
                                         1014,
                                         1015,
                                         1018,
                                         1019))
                                   AND ID_SET <> 10622
                                UNION ALL
                                SELECT ID_SUCURSALA,
                                       AN,
                                       LUNA,
                                       ID_FDOC,
                                       ID_FACT,
                                       NRACT,
                                       SERIE_ACT,
                                       DATAACT,
                                       DATAIREG,
                                       COD,
                                       SCD,
                                       SCC,
                                       ID_PARTD,
                                       ID_PARTC,
                                       ID_SET,
                                       SUMA,
                                       ID_JTVA_COLOANA,
                                       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 in
                                       (1001,
                                         1004,
                                         1005,
                                         1011,
                                         1012,
                                         1013,
                                         1014,
                                         1015,
                                         1018,
                                         1019))
                                   AND ID_SET <> 10622) 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 in
                               (1001,
                                 1004,
                                 1005,
                                 1011,
                                 1012,
                                 1013,
                                 1014,
                                 1015,
                                 1018,
                                 1019))
                              -- IN JURNALUL DE CUMPARARI NU INTRA FACTURILE NESOSITE + INCASARILE
                              -- SAU COMPENSARI 409 CU EXPLICATIE TVA
                           AND A.ID_SET <> 10622
                           AND NOT (TRIM(NVL(A.SCC, 'X')) =
                                lcContDebitRegularizare OR
                                (TRIM(NVL(A.SCD, 'X')) =
                                lcContDebitRegularizare AND
                                INSTR(lcContCreditRegularizare,
                                           TRIM(NVL(A.SCC, 'X'))) = 0))
                           AND nvl2(pack_contafin.GET_ID_SUCURSALA(),
                                    A.ID_SUCURSALA,
                                    0) =
                               nvl(pack_contafin.GET_ID_SUCURSALA(), 0)) JC
                 GROUP BY JC.AN,
                          JC.LUNA,
                          JC.ID_FACT,
                          JC.NRACT,
                          JC.SERIE_ACT,
                          JC.DATAACT,
                          JC.DATAIREG,
                          JC.ID_SUCURSALA);

      TYPE t_source_tab IS TABLE OF c_source%ROWTYPE;
      S t_source_tab; -- Renamed from l_data for better readability

      -- Dedicated collections for INSERT and DELETE operations
      SI t_source_tab := t_source_tab(); -- Source Insert
      SD t_source_tab := t_source_tab(); -- Source Delete
    BEGIN
      -- Bulk load all data into PGA
      OPEN c_source;
      FETCH c_source BULK COLLECT
        INTO S;
      CLOSE c_source;

      -- Exit if no data
      IF S.COUNT = 0 THEN
        RETURN;
      END IF;

      -- BULK UPDATE all rows at once using FORALL
      FORALL i IN 1 .. S.COUNT
        UPDATE JC2007 J
           SET J.TOTCTVA    = J.TOTCTVA + (CASE
                                WHEN EXTRACT(MONTH FROM J.DATAACT) = J.LUNA AND
                                     EXTRACT(YEAR FROM J.DATAACT) = J.AN THEN
                                 S(i).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(i).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(i).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(i).TOTNETAX
                                ELSE
                                 0
                              END),
               J.RO21B      = J.RO21B + NVL(S(i).RO21B, 0) + (CASE
                                WHEN NVL(S(i).RO21B, 0) <> 0 AND NVL(J.RO21NT, 0) <> 0 AND
                                     NVL(J.RO21NT, 0) - (NVL(J.RO21T, 0) + NVL(S(i).RO21T, 0)) = 0 THEN
                                 NVL(J.RO21NB, 0) - (NVL(J.RO21B, 0) + NVL(S(i).RO21B, 0))
                                ELSE
                                 0
                              END),
               J.RO21T      = J.RO21T + S(i).RO21T,
               J.RO11B      = J.RO11B + NVL(S(i).RO11B, 0) + (CASE
                                WHEN NVL(S(i).RO11B, 0) <> 0 AND NVL(J.RO11NT, 0) <> 0 AND
                                     NVL(J.RO11NT, 0) - (NVL(J.RO11T, 0) + NVL(S(i).RO11T, 0)) = 0 THEN
                                 NVL(J.RO11NB, 0) - (NVL(J.RO11B, 0) + NVL(S(i).RO11B, 0))
                                ELSE
                                 0
                              END),
               J.RO11T      = J.RO11T + S(i).RO11T,
               J.RO24B      = J.RO24B + NVL(S(i).RO24B, 0) + (CASE
                                WHEN NVL(S(i).RO24B, 0) <> 0 AND NVL(J.RO24NT, 0) <> 0 AND
                                     NVL(J.RO24NT, 0) - (NVL(J.RO24T, 0) + NVL(S(i).RO24T, 0)) = 0 THEN
                                 NVL(J.RO24NB, 0) - (NVL(J.RO24B, 0) + NVL(S(i).RO24B, 0))
                                ELSE
                                 0
                              END),
               J.RO24T      = J.RO24T + S(i).RO24T,
               J.RO20B      = J.RO20B + NVL(S(i).RO20B, 0) + (CASE
                                WHEN NVL(S(i).RO20B, 0) <> 0 AND NVL(J.RO20NT, 0) <> 0 AND
                                     NVL(J.RO20NT, 0) - (NVL(J.RO20T, 0) + NVL(S(i).RO20T, 0)) = 0 THEN
                                 NVL(J.RO20NB, 0) - (NVL(J.RO20B, 0) + NVL(S(i).RO20B, 0))
                                ELSE
                                 0
                              END),
               J.RO20T      = J.RO20T + S(i).RO20T,
               J.RO19BCB    = J.RO19BCB + NVL(S(i).RO19BCB, 0) + (CASE
                                WHEN NVL(S(i).RO19BCB, 0) <> 0 AND NVL(J.RO19NT, 0) <> 0 AND
                                     NVL(S(i).RO19BVB, 0) = 0 AND NVL(S(i).RO19BFB, 0) = 0 AND
                                     NVL(J.RO19NT, 0) - (NVL(J.RO19BCT, 0) + NVL(S(i).RO19BCT, 0)) = 0 THEN
                                 NVL(J.RO19NB, 0) - (NVL(J.RO19BCB, 0) + NVL(S(i).RO19BCB, 0))
                                ELSE
                                 0
                              END),
               J.RO19BCT    = J.RO19BCT + S(i).RO19BCT,
               J.RO19BVB    = J.RO19BVB + NVL(S(i).RO19BVB, 0) + (CASE
                                WHEN NVL(S(i).RO19BVB, 0) <> 0 AND NVL(J.RO19NT, 0) <> 0 AND
                                     NVL(S(i).RO19BFB, 0) = 0 AND
                                     NVL(J.RO19NT, 0) - (NVL(J.RO19BVT, 0) + NVL(S(i).RO19BVT, 0)) = 0 THEN
                                 NVL(J.RO19NB, 0) - (NVL(J.RO19BVB, 0) + NVL(S(i).RO19BVB, 0))
                                ELSE
                                 0
                              END),
               J.RO19BVT    = J.RO19BVT + S(i).RO19BVT,
               J.RO19BFB    = J.RO19BFB + NVL(S(i).RO19BFB, 0) + (CASE
                                WHEN NVL(S(i).RO19BFB, 0) <> 0 AND NVL(J.RO19NT, 0) <> 0 AND
                                     NVL(J.RO19NT, 0) - (NVL(J.RO19BFT, 0) + NVL(S(i).RO19BFT, 0)) = 0 THEN
                                 NVL(J.RO19NB, 0) - (NVL(J.RO19BFB, 0) + NVL(S(i).RO19BFB, 0))
                                ELSE
                                 0
                              END),
               J.RO19BFT    = J.RO19BFT + S(i).RO19BFT,
               J.RO09BCB    = J.RO09BCB + NVL(S(i).RO09BCB, 0) + (CASE
                                WHEN NVL(S(i).RO09BCB, 0) <> 0 AND NVL(J.RO9NT, 0) <> 0 AND
                                     NVL(S(i).RO09BVB, 0) = 0 AND NVL(S(i).RO09BFB, 0) = 0 AND
                                     NVL(J.RO9NT, 0) - (NVL(J.RO09BCT, 0) + NVL(S(i).RO09BCT, 0)) = 0 THEN
                                 NVL(J.RO9NB, 0) - (NVL(J.RO09BCB, 0) + NVL(S(i).RO09BCB, 0))
                                ELSE
                                 0
                              END),
               J.RO09BCT    = J.RO09BCT + S(i).RO09BCT,
               J.RO09BVB    = J.RO09BVB + NVL(S(i).RO09BVB, 0) + (CASE
                                WHEN NVL(S(i).RO09BVB, 0) <> 0 AND NVL(J.RO9NT, 0) <> 0 AND
                                     NVL(S(i).RO09BFB, 0) = 0 AND
                                     NVL(J.RO9NT, 0) - (NVL(J.RO09BVT, 0) + NVL(S(i).RO09BVT, 0)) = 0 THEN
                                 NVL(J.RO9NB, 0) - (NVL(J.RO09BVB, 0) + NVL(S(i).RO09BVB, 0))
                                ELSE
                                 0
                              END),
               J.RO09BVT    = J.RO09BVT + S(i).RO09BVT,
               J.RO09BFB    = J.RO09BFB + NVL(S(i).RO09BFB, 0) + (CASE
                                WHEN NVL(S(i).RO09BFB, 0) <> 0 AND NVL(J.RO9NT, 0) <> 0 AND
                                     NVL(J.RO9NT, 0) - (NVL(J.RO09BFT, 0) + NVL(S(i).RO09BFT, 0)) = 0 THEN
                                 NVL(J.RO9NB, 0) - (NVL(J.RO09BFB, 0) + NVL(S(i).RO09BFB, 0))
                                ELSE
                                 0
                              END),
               J.RO09BFT    = J.RO09BFT + S(i).RO09BFT,
               J.RO05B      = J.RO05B + NVL(S(i).RO05B, 0) + (CASE
                                WHEN NVL(S(i).RO05B, 0) <> 0 AND NVL(J.RO5NB, 0) <> 0 AND
                                     NVL(J.RO5NT, 0) - (NVL(J.RO05T, 0) + NVL(S(i).RO05T, 0)) = 0 THEN
                                 NVL(J.RO5NB, 0) - (NVL(J.RO05B, 0) + NVL(S(i).RO05B, 0))
                                ELSE
                                 0
                              END),
               J.RO05T      = J.RO05T + S(i).RO05T,
               J.RO21NB     = J.RO21NB + S(i).RO21NB,
               J.RO21NT     = J.RO21NT + S(i).RO21NT,
               J.RO11NB     = J.RO11NB + S(i).RO11NB,
               J.RO11NT     = J.RO11NT + S(i).RO11NT,
               J.RO24NB     = J.RO24NB + S(i).RO24NB,
               J.RO24NT     = J.RO24NT + S(i).RO24NT,
               J.RO19NB     = J.RO19NB + S(i).RO19NB,
               J.RO19NT     = J.RO19NT + S(i).RO19NT,
               J.RO20NB     = J.RO20NB + S(i).RO20NB,
               J.RO20NT     = J.RO20NT + S(i).RO20NT,
               J.RO9NB      = J.RO9NB + S(i).RO9NB,
               J.RO9NT      = J.RO9NT + S(i).RO9NT,
               J.RO5NB      = J.RO5NB + S(i).RO5NB,
               J.RO5NT      = J.RO5NT + S(i).RO5NT,
               J.ROSCN      = J.ROSCN + S(i).ROSCN,
               J.FO21B      = J.FO21B + S(i).FO21B,
               J.FO21T      = J.FO21T + S(i).FO21T,
               J.FO24B      = J.FO24B + S(i).FO24B,
               J.FO24T      = J.FO24T + S(i).FO24T,
               J.FO20B      = J.FO20B + S(i).FO20B,
               J.FO20T      = J.FO20T + S(i).FO20T,
               J.FO19BCB    = J.FO19BCB + S(i).FO19BCB,
               J.FO19BCT    = J.FO19BCT + S(i).FO19BCT,
               J.FO19BVB    = J.FO19BVB + S(i).FO19BVB,
               J.FO19BVT    = J.FO19BVT + S(i).FO19BVT,
               J.FO19BFB    = J.FO19BFB + S(i).FO19BFB,
               J.FO19BFT    = J.FO19BFT + S(i).FO19BFT,
               J.XX21TIB    = J.XX21TIB + S(i).XX21TIB,
               J.XX21TIT    = J.XX21TIT + S(i).XX21TIT,
               J.XX11TIB    = J.XX11TIB + S(i).XX11TIB,
               J.XX11TIT    = J.XX11TIT + S(i).XX11TIT,
               J.XX19TIB    = J.XX19TIB + S(i).XX19TIB,
               J.XX19TIT    = J.XX19TIT + S(i).XX19TIT,
               J.XX9TIB     = J.XX9TIB + S(i).XX9TIB,
               J.XX9TIT     = J.XX9TIT + S(i).XX9TIT,
               J.FO09BCB    = J.FO09BCB + S(i).FO09BCB,
               J.FO09BCT    = J.FO09BCT + S(i).FO09BCT,
               J.FO09BVB    = J.FO09BVB + S(i).FO09BVB,
               J.FO09BVT    = J.FO09BVT + S(i).FO09BVT,
               J.FO09BFB    = J.FO09BFB + S(i).FO09BFB,
               J.FO09BFT    = J.FO09BFT + S(i).FO09BFT,
               J.FOSCN      = J.FOSCN + S(i).FOSCN,
               J.CE24B      = J.CE24B + S(i).CE24B,
               J.CE24T      = J.CE24T + S(i).CE24T,
               J.CE20B      = J.CE20B + S(i).CE20B,
               J.CE20T      = J.CE20T + S(i).CE20T,
               J.CE5SB      = J.CE5SB + S(i).CE5SB,
               J.CE5ST      = J.CE5ST + S(i).CE5ST,
               J.CE9SB      = J.CE9SB + S(i).CE9SB,
               J.CE9ST      = J.CE9ST + S(i).CE9ST,
               J.CE21SB     = J.CE21SB + S(i).CE21SB,
               J.CE21ST     = J.CE21ST + S(i).CE21ST,
               J.CE24SB     = J.CE24SB + S(i).CE24SB,
               J.CE24ST     = J.CE24ST + S(i).CE24ST,
               J.CE20SB     = J.CE20SB + S(i).CE20SB,
               J.CE20ST     = J.CE20ST + S(i).CE20ST,
               J.CE19SB     = J.CE19SB + S(i).CE19SB,
               J.CE19ST     = J.CE19ST + S(i).CE19ST,
               J.CEBCB      = J.CEBCB + S(i).CEBCB,
               J.CEBCT      = J.CEBCT + S(i).CEBCT,
               J.CEBVB      = J.CEBVB + S(i).CEBVB,
               J.CEBVT      = J.CEBVT + S(i).CEBVT,
               J.CEBVSC     = J.CEBVSC + S(i).CEBVSC,
               J.CEBVN      = J.CEBVN + S(i).CEBVN,
               J.CEBFB      = J.CEBFB + S(i).CEBFB,
               J.CEBFT      = J.CEBFT + S(i).CEBFT,
               J.CEBFSC     = J.CEBFSC + S(i).CEBFSC,
               J.CEBFN      = J.CEBFN + S(i).CEBFN,
               J.CE21CTB    = J.CE21CTB + S(i).CE21CTB,
               J.CE21CTT    = J.CE21CTT + S(i).CE21CTT,
               J.CE11CTB    = J.CE11CTB + S(i).CE11CTB,
               J.CE11CTT    = J.CE11CTT + S(i).CE11CTT,
               J.CE19CTB    = J.CE19CTB + S(i).CE19CTB,
               J.CE19CTT    = J.CE19CTT + S(i).CE19CTT,
               J.CE9CTB     = J.CE9CTB + S(i).CE9CTB,
               J.CE9CTT     = J.CE9CTT + S(i).CE9CTT,
               J.CE5CTB     = J.CE5CTB + S(i).CE5CTB,
               J.CE5CTT     = J.CE5CTT + S(i).CE5CTT,
               J.CE21FTB    = J.CE21FTB + S(i).CE21FTB,
               J.CE21FTT    = J.CE21FTT + S(i).CE21FTT,
               J.CE11FTB    = J.CE11FTB + S(i).CE11FTB,
               J.CE11FTT    = J.CE11FTT + S(i).CE11FTT,
               J.CE19FTB    = J.CE19FTB + S(i).CE19FTB,
               J.CE19FTT    = J.CE19FTT + S(i).CE19FTT,
               J.CE9FTB     = J.CE9FTB + S(i).CE9FTB,
               J.CE9FTT     = J.CE9FTT + S(i).CE9FTT,
               J.CE5FTB     = J.CE5FTB + S(i).CE5FTB,
               J.CE5FTT     = J.CE5FTT + S(i).CE5FTT,
               J.TI21B      = J.TI21B + S(i).TI21B,
               J.TI21T      = J.TI21T + S(i).TI21T,
               J.TI11B      = J.TI11B + S(i).TI11B,
               J.TI11T      = J.TI11T + S(i).TI11T,
               J.TI24B      = J.TI24B + S(i).TI24B,
               J.TI24T      = J.TI24T + S(i).TI24T,
               J.TI20B      = J.TI20B + S(i).TI20B,
               J.TI20T      = J.TI20T + S(i).TI20T,
               J.TI19BCB    = J.TI19BCB + S(i).TI19BCB,
               J.TI19BCT    = J.TI19BCT + S(i).TI19BCT,
               J.TI19BVB    = J.TI19BVB + S(i).TI19BVB,
               J.TI19BVT    = J.TI19BVT + S(i).TI19BVT,
               J.TI19BFB    = J.TI19BFB + S(i).TI19BFB,
               J.TI19BFT    = J.TI19BFT + S(i).TI19BFT,
               J.TI09BVB    = J.TI09BVB + S(i).TI09BVB,
               J.TI09BVT    = J.TI09BVT + S(i).TI09BVT,
               J.TI09BFB    = J.TI09BFB + S(i).TI09BFB,
               J.TI09BFT    = J.TI09BFT + S(i).TI09BFT,
               J.ROTN       = J.ROTN + S(i).ROTN,
               J.ROTN50     = J.ROTN50 + S(i).ROTN50,
               J.ROTN100    = J.ROTN100 + S(i).ROTN100,
               J.RORTD21    = J.RORTD21 + S(i).RORTD21,
               J.RORTD11    = J.RORTD11 + S(i).RORTD11,
               J.RORTD24    = J.RORTD24 + S(i).RORTD24,
               J.RORTD20    = J.RORTD20 + S(i).RORTD20,
               J.RORTD19    = J.RORTD19 + S(i).RORTD19,
               J.RORTD9     = J.RORTD9 + S(i).RORTD9,
               J.RORTD5     = J.RORTD5 + S(i).RORTD5
         WHERE J.AN = S(i).AN
           AND J.LUNA = S(i).LUNA
           AND J.ID_FACT = S(i).ID_FACT
           AND NVL(J.ID_SUCURSALA, -99) = NVL(S(i).ID_SUCURSALA, -99);

      -- Populate SI and SD collections for INSERT and DELETE
      FOR i IN 1 .. S.COUNT LOOP
        IF SQL%BULK_ROWCOUNT(i) = 0 THEN
          -- No row was updated -> need to INSERT
          SI.EXTEND;
          SI(SI.COUNT) := S(i);
        ELSIF tnScrie = pack_contafin.nStergere THEN
          -- Row was updated and it's a deletion operation -> candidate for DELETE
          SD.EXTEND;
          SD(SD.COUNT) := S(i);
        END IF;
      END LOOP;

      -- BULK INSERT for new rows
      IF SI.COUNT > 0 THEN
        FORALL i IN 1 .. SI.COUNT
          INSERT INTO JC2007
            (AN,
             LUNA,
             ID_FACT,
             NRACT,
             SERIE_ACT,
             ID_FDOC,
             DATAACT,
             DATAIREG,
             ID_PART,
             COD,
             TOTCTVA,
             TOTFTVATAX,
             TOTTVATAX,
             TOTNETAX,
             RO21B,
             RO21T,
             RO11B,
             RO11T,
             RO24B,
             RO24T,
             RO20B,
             RO20T,
             RO19BCB,
             RO19BCT,
             RO19BVB,
             RO19BVT,
             RO19BFB,
             RO19BFT,
             RO09BCB,
             RO09BCT,
             RO09BVB,
             RO09BVT,
             RO09BFB,
             RO09BFT,
             RO05B,
             RO05T,
             RO21NB,
             RO21NT,
             RO11NB,
             RO11NT,
             RO24NB,
             RO24NT,
             RO20NB,
             RO20NT,
             RO19NB,
             RO19NT,
             RO9NB,
             RO9NT,
             RO5NB,
             RO5NT,
             ROSCN,
             FO21B,
             FO21T,
             FO24B,
             FO24T,
             FO20B,
             FO20T,
             FO19BCB,
             FO19BCT,
             FO19BVB,
             FO19BVT,
             FO19BFB,
             FO19BFT,
             XX21TIB,
             XX21TIT,
             XX11TIB,
             XX11TIT,
             XX19TIB,
             XX19TIT,
             XX9TIB,
             XX9TIT,
             FO09BCB,
             FO09BCT,
             FO09BVB,
             FO09BVT,
             FO09BFB,
             FO09BFT,
             FOSCN,
             CE24B,
             CE24T,
             CE20B,
             CE20T,
             CE5SB,
             CE5ST,
             CE9SB,
             CE9ST,
             CE21SB,
             CE21ST,
             CE24SB,
             CE24ST,
             CE20SB,
             CE20ST,
             CE19SB,
             CE19ST,
             CEBCB,
             CEBCT,
             CEBVB,
             CEBVT,
             CEBVSC,
             CEBVN,
             CEBFB,
             CEBFT,
             CEBFSC,
             CEBFN,
             CE21CTB,
             CE21CTT,
             CE11CTB,
             CE11CTT,
             CE19CTB,
             CE19CTT,
             CE9CTB,
             CE9CTT,
             CE5CTB,
             CE5CTT,
             CE21FTB,
             CE21FTT,
             CE11FTB,
             CE11FTT,
             CE19FTB,
             CE19FTT,
             CE9FTB,
             CE9FTT,
             CE5FTB,
             CE5FTT,
             TI21B,
             TI21T,
             TI11B,
             TI11T,
             TI24B,
             TI24T,
             TI20B,
             TI20T,
             TI19BCB,
             TI19BCT,
             TI19BVB,
             TI19BVT,
             TI19BFB,
             TI19BFT,
             TI09BVB,
             TI09BVT,
             TI09BFB,
             TI09BFT,
             ROTN,
             ROTN50,
             ROTN100,
             RORTD21,
             RORTD11,
             RORTD24,
             RORTD20,
             RORTD19,
             RORTD9,
             RORTD5,
             ID_SUCURSALA)
          VALUES
            (SI(i).AN,
             SI(i).LUNA,
             SI(i).ID_FACT,
             SI(i).NRACT,
             SI(i).SERIE_ACT,
             SI(i).ID_FDOC,
             SI(i).DATAACT,
             SI(i).DATAIREG,
             SI(i).ID_PART,
             SI(i).COD,
             SI(i).TOTCTVA,
             SI(i).TOTFTVATAX,
             SI(i).TOTTVATAX,
             SI(i).TOTNETAX,
             (CASE
               WHEN NVL(SI(i).RO21B, 0) <> 0 AND NVL(SI(i).RO21NT, 0) <> 0 AND
                    NVL(SI(i).RO21NT, 0) = NVL(SI(i).RO21T, 0) THEN
                NVL(SI(i).RO21NB, 0)
               ELSE
                NVL(SI(i).RO21B, 0)
             END),
             SI(i).RO21T,
             (CASE
               WHEN NVL(SI(i).RO11B, 0) <> 0 AND NVL(SI(i).RO11NT, 0) <> 0 AND
                    NVL(SI(i).RO11NT, 0) = NVL(SI(i).RO11T, 0) THEN
                NVL(SI(i).RO11NB, 0)
               ELSE
                NVL(SI(i).RO11B, 0)
             END),
             SI(i).RO11T,
             (CASE
               WHEN NVL(SI(i).RO24B, 0) <> 0 AND NVL(SI(i).RO24NT, 0) <> 0 AND
                    NVL(SI(i).RO24NT, 0) = NVL(SI(i).RO24T, 0) THEN
                NVL(SI(i).RO24NB, 0)
               ELSE
                NVL(SI(i).RO24B, 0)
             END),
             SI(i).RO24T,
             (CASE
               WHEN NVL(SI(i).RO20B, 0) <> 0 AND NVL(SI(i).RO20NT, 0) <> 0 AND
                    NVL(SI(i).RO20NT, 0) = NVL(SI(i).RO20T, 0) THEN
                NVL(SI(i).RO20NB, 0)
               ELSE
                NVL(SI(i).RO20B, 0)
             END),
             SI(i).RO20T,
             (CASE
               WHEN NVL(SI(i).RO19BCB, 0) <> 0 AND NVL(SI(i).RO19BVB, 0) = 0 AND
                    NVL(SI(i).RO19BFB, 0) = 0 AND NVL(SI(i).RO19NT, 0) <> 0 AND
                    NVL(SI(i).RO19NT, 0) = NVL(SI(i).RO19BCT, 0) THEN
                NVL(SI(i).RO19NB, 0)
               ELSE
                NVL(SI(i).RO19BCB, 0)
             END),
             SI(i).RO19BCT,
             (CASE
               WHEN NVL(SI(i).RO19BVB, 0) <> 0 AND NVL(SI(i).RO19BFB, 0) = 0 AND
                    NVL(SI(i).RO19NT, 0) <> 0 AND
                    NVL(SI(i).RO19NT, 0) = NVL(SI(i).RO19BVT, 0) THEN
                NVL(SI(i).RO19NB, 0)
               ELSE
                NVL(SI(i).RO19BVB, 0)
             END),
             SI(i).RO19BVT,
             (CASE
               WHEN NVL(SI(i).RO19BFB, 0) <> 0 AND NVL(SI(i).RO19NT, 0) <> 0 AND
                    NVL(SI(i).RO19NT, 0) = NVL(SI(i).RO19BFT, 0) THEN
                NVL(SI(i).RO19NB, 0)
               ELSE
                NVL(SI(i).RO19BFB, 0)
             END),
             SI(i).RO19BFT,
             (CASE
               WHEN NVL(SI(i).RO09BCB, 0) <> 0 AND NVL(SI(i).RO09BVB, 0) = 0 AND
                    NVL(SI(i).RO09BFB, 0) = 0 AND NVL(SI(i).RO9NT, 0) <> 0 AND
                    NVL(SI(i).RO9NT, 0) = NVL(SI(i).RO09BCT, 0) THEN
                NVL(SI(i).RO9NB, 0)
               ELSE
                NVL(SI(i).RO09BCB, 0)
             END),
             SI(i).RO09BCT,
             (CASE
               WHEN NVL(SI(i).RO09BVB, 0) <> 0 AND NVL(SI(i).RO09BFB, 0) = 0 AND
                    NVL(SI(i).RO9NT, 0) <> 0 AND
                    NVL(SI(i).RO9NT, 0) = NVL(SI(i).RO09BVT, 0) THEN
                NVL(SI(i).RO9NB, 0)
               ELSE
                NVL(SI(i).RO09BVB, 0)
             END),
             SI(i).RO09BVT,
             (CASE
               WHEN NVL(SI(i).RO09BFB, 0) <> 0 AND NVL(SI(i).RO9NT, 0) <> 0 AND
                    NVL(SI(i).RO9NT, 0) = NVL(SI(i).RO09BFT, 0) THEN
                NVL(SI(i).RO9NB, 0)
               ELSE
                NVL(SI(i).RO09BFB, 0)
             END),
             SI(i).RO09BFT,
             (CASE
               WHEN NVL(SI(i).RO05B, 0) <> 0 AND NVL(SI(i).RO5NT, 0) <> 0 AND
                    NVL(SI(i).RO5NT, 0) = NVL(SI(i).RO05T, 0) THEN
                NVL(SI(i).RO5NB, 0)
               ELSE
                NVL(SI(i).RO05B, 0)
             END),
             SI(i).RO05T,
             SI(i).RO21NB,
             SI(i).RO21NT,
             SI(i).RO11NB,
             SI(i).RO11NT,
             SI(i).RO24NB,
             SI(i).RO24NT,
             SI(i).RO20NB,
             SI(i).RO20NT,
             SI(i).RO19NB,
             SI(i).RO19NT,
             SI(i).RO9NB,
             SI(i).RO9NT,
             SI(i).RO5NB,
             SI(i).RO5NT,
             SI(i).ROSCN,
             SI(i).FO21B,
             SI(i).FO21T,
             SI(i).FO24B,
             SI(i).FO24T,
             SI(i).FO20B,
             SI(i).FO20T,
             SI(i).FO19BCB,
             SI(i).FO19BCT,
             SI(i).FO19BVB,
             SI(i).FO19BVT,
             SI(i).FO19BFB,
             SI(i).FO19BFT,
             SI(i).XX21TIB,
             SI(i).XX21TIT,
             SI(i).XX11TIB,
             SI(i).XX11TIT,
             SI(i).XX19TIB,
             SI(i).XX19TIT,
             SI(i).XX9TIB,
             SI(i).XX9TIT,
             SI(i).FO09BCB,
             SI(i).FO09BCT,
             SI(i).FO09BVB,
             SI(i).FO09BVT,
             SI(i).FO09BFB,
             SI(i).FO09BFT,
             SI(i).FOSCN,
             SI(i).CE24B,
             SI(i).CE24T,
             SI(i).CE20B,
             SI(i).CE20T,
             SI(i).CE5SB,
             SI(i).CE5ST,
             SI(i).CE9SB,
             SI(i).CE9ST,
             SI(i).CE21SB,
             SI(i).CE21ST,
             SI(i).CE24SB,
             SI(i).CE24ST,
             SI(i).CE20SB,
             SI(i).CE20ST,
             SI(i).CE19SB,
             SI(i).CE19ST,
             SI(i).CEBCB,
             SI(i).CEBCT,
             SI(i).CEBVB,
             SI(i).CEBVT,
             SI(i).CEBVSC,
             SI(i).CEBVN,
             SI(i).CEBFB,
             SI(i).CEBFT,
             SI(i).CEBFSC,
             SI(i).CEBFN,
             SI(i).CE21CTB,
             SI(i).CE21CTT,
             SI(i).CE11CTB,
             SI(i).CE11CTT,
             SI(i).CE19CTB,
             SI(i).CE19CTT,
             SI(i).CE9CTB,
             SI(i).CE9CTT,
             SI(i).CE5CTB,
             SI(i).CE5CTT,
             SI(i).CE21FTB,
             SI(i).CE21FTT,
             SI(i).CE11FTB,
             SI(i).CE11FTT,
             SI(i).CE19FTB,
             SI(i).CE19FTT,
             SI(i).CE9FTB,
             SI(i).CE9FTT,
             SI(i).CE5FTB,
             SI(i).CE5FTT,
             SI(i).TI21B,
             SI(i).TI21T,
             SI(i).TI11B,
             SI(i).TI11T,
             SI(i).TI24B,
             SI(i).TI24T,
             SI(i).TI20B,
             SI(i).TI20T,
             SI(i).TI19BCB,
             SI(i).TI19BCT,
             SI(i).TI19BVB,
             SI(i).TI19BVT,
             SI(i).TI19BFB,
             SI(i).TI19BFT,
             SI(i).TI09BVB,
             SI(i).TI09BVT,
             SI(i).TI09BFB,
             SI(i).TI09BFT,
             SI(i).ROTN,
             SI(i).ROTN50,
             SI(i).ROTN100,
             SI(i).RORTD21,
             SI(i).RORTD11,
             SI(i).RORTD24,
             SI(i).RORTD20,
             SI(i).RORTD19,
             SI(i).RORTD9,
             SI(i).RORTD5,
             SI(i).ID_SUCURSALA);
      END IF;

      -- BULK DELETE for rows where all values became 0 after UPDATE
      -- Only executed when tnScrie = nStergere
      IF SD.COUNT > 0 AND tnScrie = pack_contafin.nStergere THEN
        FORALL i IN 1 .. SD.COUNT
          DELETE FROM JC2007 J
           WHERE J.AN = SD(i).AN
             AND J.LUNA = SD(i).LUNA
             AND J.ID_FACT = SD(i).ID_FACT
             AND NVL(J.ID_SUCURSALA, -99) = NVL(SD(i).ID_SUCURSALA, -99)
             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.RO21B = 0
             AND J.RO21T = 0
             AND J.RO11B = 0
             AND J.RO11T = 0
             AND J.RO19BCB = 0
             AND J.RO19BCT = 0
             AND J.RO19BVB = 0
             AND J.RO19BVT = 0
             AND J.RO19BFB = 0
             AND J.RO19BFT = 0
             AND J.RO09BCB = 0
             AND J.RO09BCT = 0
             AND J.RO09BVB = 0
             AND J.RO09BVT = 0
             AND J.RO09BFB = 0
             AND J.RO09BFT = 0
             AND J.RO05B = 0
             AND J.RO05T = 0
             AND J.RO21NB = 0
             AND J.RO21NT = 0
             AND J.RO11NB = 0
             AND J.RO11NT = 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.ROSCN = 0
             AND J.FO21B = 0
             AND J.FO21T = 0
             AND J.FO24B = 0
             AND J.FO24T = 0
             AND J.FO20B = 0
             AND J.FO20T = 0
             AND J.FO19BCB = 0
             AND J.FO19BCT = 0
             AND J.FO19BVB = 0
             AND J.FO19BVT = 0
             AND J.FO19BFB = 0
             AND J.FO19BFT = 0
             AND J.XX21TIB = 0
             AND J.XX21TIT = 0
             AND J.XX11TIB = 0
             AND J.XX11TIT = 0
             AND J.XX19TIB = 0
             AND J.XX19TIT = 0
             AND J.XX9TIB = 0
             AND J.XX9TIT = 0
             AND J.FO09BCB = 0
             AND J.FO09BCT = 0
             AND J.FO09BVB = 0
             AND J.FO09BVT = 0
             AND J.FO09BFB = 0
             AND J.FO09BFT = 0
             AND J.FOSCN = 0
             AND J.CE24B = 0
             AND J.CE24T = 0
             AND J.CE20B = 0
             AND J.CE20T = 0
             AND J.CE5SB = 0
             AND J.CE5ST = 0
             AND J.CE9SB = 0
             AND J.CE9ST = 0
             AND J.CE21SB = 0
             AND J.CE21ST = 0
             AND J.CE24SB = 0
             AND J.CE24ST = 0
             AND J.CE20SB = 0
             AND J.CE20ST = 0
             AND J.CE19SB = 0
             AND J.CE19ST = 0
             AND J.CEBCB = 0
             AND J.CEBCT = 0
             AND J.CEBVB = 0
             AND J.CEBVT = 0
             AND J.CEBVSC = 0
             AND J.CEBVN = 0
             AND J.CEBFB = 0
             AND J.CEBFT = 0
             AND J.CEBFSC = 0
             AND J.CEBFN = 0
             AND J.CE21CTB = 0
             AND J.CE21CTT = 0
             AND J.CE11CTB = 0
             AND J.CE11CTT = 0
             AND J.CE19CTB = 0
             AND J.CE19CTT = 0
             AND J.CE9CTB = 0
             AND J.CE9CTT = 0
             AND J.CE5CTB = 0
             AND J.CE5CTT = 0
             AND J.CE21FTB = 0
             AND J.CE21FTT = 0
             AND J.CE11FTB = 0
             AND J.CE11FTT = 0
             AND J.CE19FTB = 0
             AND J.CE19FTT = 0
             AND J.CE9FTB = 0
             AND J.CE9FTT = 0
             AND J.CE5FTB = 0
             AND J.CE5FTT = 0
             AND J.TI21B = 0
             AND J.TI21T = 0
             AND J.TI11B = 0
             AND J.TI11T = 0
             AND J.TI24B = 0
             AND J.TI24T = 0
             AND J.TI20B = 0
             AND J.TI20T = 0
             AND J.TI19BCB = 0
             AND J.TI19BCT = 0
             AND J.TI19BVB = 0
             AND J.TI19BVT = 0
             AND J.TI19BFB = 0
             AND J.TI19BFT = 0
             AND J.TI09BVB = 0
             AND J.TI09BVT = 0
             AND J.TI09BFB = 0
             AND J.TI09BFT = 0
             AND J.ROTN = 0
             AND J.ROTN50 = 0
             AND J.ROTN100 = 0;
      END IF;
    END;

    <<sfarsit_procedura>>
    null;
  end SCRIE_JC_2007;

  -------------------------------------------------------------------
  -- COMPLETARE JV2007 CU ID_SET 10616 STORNARE PLATA/INCASARE
  -------------------------------------------------------------------
  Procedure COMPLETEAZA_JV_2007(tnAn            IN JV2007.AN%TYPE,
                                tnLuna          IN JV2007.LUNA%TYPE,
                                tcTabelSursa    IN VARCHAR2,
                                tcCondSucursala IN VARCHAR2) is
    lcMerge VARCHAR2(32000);
  begin
    lcMerge := 'MERGE INTO JV2007 A ' ||
               'USING (select :1 as an,
         :2 as luna,
         dataact,
         dataireg,
         nract,
         id_part,
         id_fdoc,
         totctva,
         cod,
         serie_act,
         id_sucursala,
         id_fact,
         totftvatax,
         tottvatax,
         totnetax
    from (select row_number() over(partition by a1.id_fact order by a1.an desc, a1.luna desc) as nrcrt,
                 a1.*
            from jv2007 a1
           where a1.id_Fact in (select distinct a2.id_factc
                                 from ' || tcTabelSursa ||
               ' a2 left join documente b2 on a2.id_factc = b2.id_doc
                                where a2.id_set = 10616
                                  and a2.an = :3
                                  and a2.luna = :4
                                  and a2.sters = 0
                                  and a2.id_factc <> 0
                                  and NVL(b2.tva_incasare,0) = 1 ' ||
               REPLACE(tcCondSucursala, 'ALIAS.', 'A2.') || ')
                                  ' ||
               REPLACE(tcCondSucursala, 'ALIAS.', 'A1.') ||
               ') where nrcrt = 1
     and an * 12 + luna <> :5 * 12 + :6) B ' ||
               'ON (A.AN = B.AN AND A.LUNA = B.LUNA AND A.ID_FACT = B.ID_FACT AND NVL(A.ID_SUCURSALA,-99)=NVL(B.ID_SUCURSALA,-99)) ' ||
               'WHEN NOT MATCHED THEN ' ||
               'insert (an,luna,dataact,dataireg,nract,id_part,id_fdoc,totctva,cod,serie_act,id_sucursala,id_fact,totftvatax,tottvatax,totnetax) ' ||
               'values (b.an,b.luna,b.dataact,b.dataireg,b.nract,b.id_part,b.id_fdoc,b.totctva,b.cod,b.serie_act,b.id_sucursala,b.id_fact,b.totftvatax,b.tottvatax,b.totnetax)';

    execute immediate lcMerge
      using tnAn, tnLuna, tnAn, tnLuna, tnAn, tnLuna;

  end COMPLETEAZA_JV_2007;

  -------------------------------------------------------------------
  -- COMPLETARE JC2007 CU ID_SET 10616 STORNARE PLATA/INCASARE
  -------------------------------------------------------------------
  Procedure COMPLETEAZA_JC_2007(tnAn            IN JC2007.AN%TYPE,
                                tnLuna          IN JC2007.LUNA%TYPE,
                                tcTabelSursa    IN VARCHAR2,
                                tcCondSucursala IN VARCHAR2) is
    lcMerge VARCHAR2(32000);
  begin
    lcMerge := 'MERGE INTO JC2007 A ' ||
               'USING (select :1 as an,
         :2 as luna,
         dataact,
         dataireg,
         nract,
         id_part,
         id_fdoc,
         totctva,
         cod,
         serie_act,
         id_sucursala,
         id_fact,
         totftvatax,
         tottvatax,
         totnetax
    from (select row_number() over(partition by a1.id_fact order by a1.an desc, a1.luna desc) as nrcrt,
                 a1.*
            from jc2007 a1
           where a1.id_Fact in (select distinct a2.id_factd
                                 from ' || tcTabelSursa ||
               ' a2 left join documente b2 on a2.id_factd = b2.id_doc
                                where a2.id_set = 10616
                                  and a2.an = :3
                                  and a2.luna = :4
                                  and a2.sters = 0
                                  and a2.id_factd <> 0
                                  and NVL(b2.tva_incasare,0) = 1 ' ||
               REPLACE(tcCondSucursala, 'ALIAS.', 'A2.') || ')
                                  ' ||
               REPLACE(tcCondSucursala, 'ALIAS.', 'A1.') ||
               ') where nrcrt = 1
     and an * 12 + luna <> :5 * 12 + :6) B ' ||
               'ON (A.AN = B.AN AND A.LUNA = B.LUNA AND A.ID_FACT = B.ID_FACT AND NVL(A.ID_SUCURSALA,-99)=NVL(B.ID_SUCURSALA,-99)) ' ||
               'WHEN NOT MATCHED THEN ' ||
               'insert (an,luna,dataact,dataireg,nract,id_part,id_fdoc,totctva,cod,serie_act,id_sucursala,id_fact,totftvatax,tottvatax,totnetax) ' ||
               'values (b.an,b.luna,b.dataact,b.dataireg,b.nract,b.id_part,b.id_fdoc,b.totctva,b.cod,b.serie_act,b.id_sucursala,b.id_fact,b.totftvatax,b.tottvatax,b.totnetax)';

    execute immediate lcMerge
      using tnAn, tnLuna, tnAn, tnLuna, tnAn, tnLuna;
  end COMPLETEAZA_JC_2007;
  -----------------------------------------------------------------------
  Procedure COMPLETEAZA_IREG_PARTENERI(tnAn            in IREG_PARTENERI.AN%TYPE,
                                       tnLuna          in IREG_PARTENERI.LUNA%TYPE,
                                       tcTabelSursa    IN VARCHAR2,
                                       tcCondSucursala IN VARCHAR2,
                                       tcCondInPlus    IN VARCHAR2,
                                       tcCont          IN VARCHAR2,
                                       tnIdPart        IN NUMBER) is
    ldDataNull DATE := TO_DATE('19700101', 'YYYYMMDD');
    lcMerge    VARCHAR2(32000);
  begin
    --  conditia de la ON e luata de la incasari
    --  inregistrarile vechi din IREG_PARTENERI le iau in functie de ID_FACTC/ID_FACTD pentru ca,
    -- atunci cand a fost ales documentul pereche pe nota initiala, ID_FACT-ul respectiv exista in IREG_PARTENERI, indiferent daca
    -- refacerea s-a facut dupa numar act sau id_fact
    lcMerge := 'MERGE INTO IREG_PARTENERI I ' || 'USING ' ||
               '(select id_sucursala,
       :1 as an,
       :2 as luna,
       cont,
       acont,
       id_part,
       id_valuta,
       id_fact,
       id_fdoc,
       id_lucrare,
       id_set,
       id_ctr,
       id_jtva_coloana,
       id_venchelt,
       proc_tva,
       curs,
       dataact,
       dataireg,
       datascad,
       nract,
       serie_act,
       id_responsabil,
       cod,
       explicatia,
       explicatia4,
       explicatia5,
       precdeb + debit as precdeb,
       preccred + credit as preccred,
       precvaldeb + valdebit as precvaldeb,
       precvalcred + valcredit as precvalcred
  from (select row_number() over(partition by a1.id_fact order by a1.an desc, a1.luna desc) as nrcrt,
               a1.*
          from ireg_parteneri a1
         where a1.id_Fact in
               (select distinct id_factc
                  from ' || tcTabelSursa || ' a2
                 WHERE A2.AN = :3
                   and A2.LUNA = :4
                   and A2.STERS = 0
                   and A2.ID_SET = 10616
                   and A2.SCC = :5
                   and NVL(A2.ID_FACTC,0) <> 0
                   and NVL2(:6,A2.ID_PARTC,-1) = NVL(:7,-1) ' ||
               REPLACE(tcCondSucursala, 'ALIAS.', 'A2.') || '
                 UNION ALL
                 select distinct id_factd
                  from ' || tcTabelSursa || ' a2
                 WHERE A2.AN = :8
                   and A2.LUNA = :9
                   and A2.STERS = 0
                   and A2.ID_SET = 10616
                   and A2.SCD = :10
                   and NVL(A2.ID_FACTD,0) <> 0
                   and NVL2(:11,A2.ID_PARTD,-1) = NVL(:12,-1) ' ||
               REPLACE(tcCondSucursala, 'ALIAS.', 'A2.') || ')
           and A1.cont = :13
           and A1.dataact is not null
           and A1.an * 12 + A1.luna < :14 * 12 + :15)
 where nrcrt = 1) S ' ||
               'ON (I.AN = S.AN AND I.LUNA = S.LUNA AND I.CONT = S.CONT AND NVL(I.ACONT, ''XXXX'') = NVL(S.ACONT, ''XXXX'') AND
NVL(I.NRACT,0) = NVL(S.NRACT,0) AND NVL(I.SERIE_ACT,''+_'')=NVL(S.SERIE_ACT,''+_'') AND NVL(I.DATAACT,:16) = NVL(S.DATAACT,:17) AND
NVL(I.DATAIREG,:18) = NVL(S.DATAIREG,:19) AND NVL(I.DATASCAD,:20) = NVL(S.DATASCAD,:21) AND
NVL(I.ID_PART,0) = NVL(S.ID_PART,0) AND NVL(I.ID_VALUTA,0) = NVL(S.ID_VALUTA,0) AND NVL(I.ID_SUCURSALA,0) = NVL(S.ID_SUCURSALA,0) ' ||
               tcCondInPlus || ')' || 'WHEN NOT MATCHED THEN ' ||
               'INSERT (id_sucursala, an, luna, cont, acont, id_part, id_valuta,' ||
               'id_fact, id_fdoc, id_lucrare, id_set, id_ctr, id_jtva_coloana,' ||
               'id_venchelt, proc_tva, curs, dataact, dataireg, datascad, nract,serie_act,' ||
               'id_responsabil, cod, explicatia, explicatia4, explicatia5,' ||
               'precdeb, preccred, precvaldeb, precvalcred)' ||
               'VALUES (S.id_sucursala, S.an, S.luna, S.cont, S.acont, S.id_part, S.id_valuta,' ||
               'S.id_fact, S.id_fdoc, S.id_lucrare, S.id_set, S.id_ctr, S.id_jtva_coloana,' ||
               'S.id_venchelt, S.proc_tva, S.curs, S.dataact, S.dataireg, S.datascad, S.nract, S.serie_act,' ||
               'S.id_responsabil, S.cod, S.explicatia, S.explicatia4, S.explicatia5,' ||
               'S.precdeb, S.preccred, S.precvaldeb, S.precvalcred)';

    --pinfo(lcMerge, 'CompleteazaIregPart');

    execute immediate lcMerge
      using tnAn, tnLuna, tnAn, tnLuna, tcCont, tnIdPart, tnIdPart, tnAn, tnLuna, tcCont, tnIdPart, tnIdPart, tcCont, tnAn, tnLuna, ldDataNull, ldDataNull, ldDataNull, ldDataNull, ldDataNull, ldDataNull;
  end COMPLETEAZA_IREG_PARTENERI;
  -----------------------------------------------------------------------
  FUNCTION VERIFICA_NOTE_CONTABILE(tlVerificAnalitic IN NUMBER) RETURN NUMBER IS
    V_TOTAL NUMBER(10);
  BEGIN
    V_TOTAL := 0;
    select sum(completare_partener +
               DECODE(tlVerificAnalitic, 1, completare_analitic, 0) +
               completare_pereche) as total
      INTO V_TOTAL
      from (select a.cont,
                   a.id_part,
                   a.analitic,
                   a.pereche,
                   a.cont_p,
                   a.cont_d,
                   b.cont as contpart,
                   b.fel_cont,
                   b.cu_inregistrari,
                   b.explicatie as explic_cont,
                   c.cont_c as exceptie,
                   c.debit as pozitie,
                   c.invers,
                   c1.lista,
                   NVL2(b.cont, decode(a.id_part, 0, 1, 0), 0) as completare_partener,
                   NVL2(d.cont, NVL2(a.analitic, 0, 1), 0) as completare_analitic,
                   NVL2(b.cont,
                        decode(a.cont_d,
                               1,
                               (case
                                 when a.pereche = 0 and b.cu_inregistrari = 1 and
                                      b.fel_cont = 1 then
                                  (case
                                    when instr(c1.lista, a.cont_p) > 0 and c.debit = 0 and
                                         c.invers = 1 then
                                     0
                                    else
                                     1
                                  end)
                                 else
                                  0
                               end),
                               0,
                               (case
                                 when a.pereche = 0 and b.cu_inregistrari = 1 and
                                      b.fel_cont = 0 then
                                  (case
                                    when instr(c1.lista, a.cont_p) > 0 and c.debit = 1 and
                                         c.invers = 1 then
                                     0
                                    else
                                     1
                                  end)
                                 else
                                  0
                               end)),
                        0) as completare_pereche
              from (select distinct scd      as cont,
                                    id_partd as id_part,
                                    ascd     as analitic,
                                    pereched as pereche,
                                    scc      as cont_p,
                                    1        as cont_d
                      from act_temp
                     where sters = 0
                       and scd is not null
                       and suma <> 0
                    union all
                    select distinct scc      as cont,
                                    id_partc as id_part,
                                    ascc     as analitic,
                                    perechec as pereche,
                                    scd      as cont_p,
                                    0        as cont_d
                      from act_temp
                     where sters = 0
                       and scc is not null
                       and suma <> 0) a
              left join vcoresp_tip_cont b
                on a.cont = b.cont
              left join exceptii_ireg c
                on b.cont = c.cont
               and c.invers = 1
              left join (select cont, stringagg(cont_c) as lista
                          from exceptii_ireg
                         where invers = 1
                         group by cont) c1
                on b.cont = c1.cont
              left join (select distinct cont
                          from vplcont_analitic
                         where inactiv = 0
                           and an = pack_sesiune.getan()) d
                on a.cont = d.cont);

    RETURN V_TOTAL;
  END VERIFICA_NOTE_CONTABILE;
  --------------------------------------------------------------------------
  ---------------------------------------------------------------------------------------
  PROCEDURE SCRIE_IN_BALANTA(tcS     in varchar2,
                             tnAn    in BAL.AN%TYPE,
                             tnLuna  in BAL.LUNA%TYPE,
                             tnScrie in number) IS
    lnScrie          NUMBER(1); -- 0/2 = SCRIERE/STERGERE; 1 = REFACERE
    lcAct            VARCHAR2(100);
    lcMerge          VARCHAR2(10000);
    lnIdSucursala    act.id_sucursala%type;
    lcWhereSucursale varchar2(1000);

  BEGIN

    lnIdSucursala := PACK_CONTAFIN.GET_ID_SUCURSALA();
    if lnIdSucursala is not null then
      lcWhereSucursale := ' and id_sucursala = ' || lnIdSucursala;
    end if;

    lnScrie := NVL(tnScrie, 0);
    IF lnScrie = pack_contafin.nRefacere then
      lcAct := 'ACT';
    ELSE
      lcAct := 'ACT_TEMP';
    END IF;

    -- BALANTA SINTETICA
    lcMerge := 'MERGE INTO BAL B
    USING (SELECT ID_SUCURSALA, CONT, SUM(DEBIT) AS DEBIT, SUM(CREDIT) AS CREDIT
             FROM (SELECT ID_SUCURSALA, scd as cont,
                          sum(suma) as DEBIT,
                          sum(0.0000) AS CREDIT
                     FROM ' || lcAct ||
               ' WHERE STERS = 0
                      AND AN = :1
                      AND LUNA = :2
                      AND SCD IS NOT NULL ' ||
               lcWhereSucursale || '
                  GROUP BY ID_SUCURSALA, SCD
                   UNION ALL
                   SELECT ID_SUCURSALA, scc as cont,
                          sum(0.0000) AS DEBIT,
                          sum(suma) as CREDIT
                     FROM ' || lcAct ||
               ' WHERE STERS = 0
                      AND AN = :3
                      AND LUNA = :4
                      AND SCC IS NOT NULL ' ||
               lcWhereSucursale || '
                 GROUP BY ID_SUCURSALA, SCC)
            GROUP BY ID_SUCURSALA, CONT) A
    ON (B.AN = :5 AND B.LUNA = :6 AND NVL(B.CONT, ''XXXX'') = NVL(A.CONT, ''XXXX'')
       AND NVL(B.id_sucursala,0) = NVL(A.id_sucursala,0))
    WHEN MATCHED THEN
      UPDATE
         SET B.RULDEB  = B.RULDEB + A.DEBIT,
             B.RULCRED = B.RULCRED + A.CREDIT
    WHEN NOT MATCHED THEN
      INSERT
        (ID_SUCURSALA, AN, LUNA, CONT, RULDEB, RULCRED)
      VALUES
        (A.ID_SUCURSALA, :7, :8, A.CONT, A.DEBIT, A.CREDIT)';

    EXECUTE IMMEDIATE lcMerge
      USING tnAn, tnLuna, tnAn, tnLuna, tnAn, tnLuna, tnAn, tnLuna;

    -- BALANTA ANALITICA
    lcMerge := 'MERGE INTO BALANA B
  USING
(SELECT ID_SUCURSALA, CONT, ACONT, SUM(DEBIT) AS DEBIT, SUM(CREDIT) AS CREDIT
   FROM (SELECT ID_SUCURSALA, scd as cont, ascd as acont, sum(suma) as DEBIT, sum(0.0000) AS CREDIT
            FROM ' || lcAct || ' WHERE STERS = 0 AND SCD IS NOT NULL AND ASCD IS NOT NULL AND AN = :1
            AND LUNA = :2 ' || lcWhereSucursale || '
         GROUP BY ID_SUCURSALA, SCD, ASCD
        UNION ALL
         SELECT ID_SUCURSALA, scc as cont, ascc as acont, sum(0.0000) AS DEBIT, sum(suma) as CREDIT
            FROM ' || lcAct || ' WHERE STERS = 0 AND SCC IS NOT NULL AND ASCC IS NOT NULL AND AN = :3
               AND LUNA = :4 ' || lcWhereSucursale || '
       GROUP BY ID_SUCURSALA, SCC, ASCC)
    GROUP BY ID_SUCURSALA, CONT, ACONT) A
  ON (B.AN = :5 AND B.LUNA = :6 AND NVL(B.CONT,''XXXX'') = NVL(A.CONT,''XXXX'') AND NVL(B.ACONT,''XXXX'') = NVL(A.ACONT,''XXXX'')
    AND NVL(B.id_sucursala,0) = NVL(A.id_sucursala,0))
  WHEN MATCHED THEN
  UPDATE  SET B.RULDEB = B.RULDEB + A.DEBIT, B.RULCRED = B.RULCRED + A.CREDIT
  WHEN NOT MATCHED THEN
  INSERT (ID_SUCURSALA, AN, LUNA, CONT, ACONT, RULDEB, RULCRED)
  VALUES (A.ID_SUCURSALA, :7, :8, A.CONT, A.ACONT, A.DEBIT, A.CREDIT)';

    EXECUTE IMMEDIATE lcMerge
      USING tnAn, tnLuna, tnAn, tnLuna, tnAn, tnLuna, tnAn, tnLuna;

  END SCRIE_IN_BALANTA;

  ---------------------------------------------------------------

  PROCEDURE SCRIE_IN_BALANTA_PARTENERI(tcs      in varchar2,
                                       tnAn     in BALANTA_PARTENERI.AN%TYPE,
                                       tnLuna   in BALANTA_PARTENERI.LUNA%TYPE,
                                       tcCont   in BALANTA_PARTENERI.CONT%TYPE,
                                       tnScrie  in number,
                                       tnIdPart in BALANTA_PARTENERI.ID_PART%TYPE DEFAULT NULL) IS
    lnScrie          NUMBER(1); -- 0/2 = SCRIERE/STERGERE; 1 = REFACERE
    lcAct            VARCHAR2(100);
    lcMerge          VARCHAR2(10000);
    lnIdSucursala    act.id_sucursala%type;
    lcWhereSucursale varchar2(1000);
    lcUpdWherePartD  varchar2(1000);
    lcUpdWherePartC  varchar2(1000);
  BEGIN

    lnIdSucursala := PACK_CONTAFIN.GET_ID_SUCURSALA();
    if lnIdSucursala is not null then
      lcWhereSucursale := ' and id_sucursala = ' || lnIdSucursala;
    end if;

    -- 30.06.2011
    if tnIdPart is not null then
      lcUpdWherePartD := ' and id_partd = ' || tnIdPart;
      lcUpdWherePartC := ' and id_partc = ' || tnIdPart;
    end if;
    -- 30.06.2011 ^

    lnScrie := NVL(tnScrie, 0);
    IF lnScrie = pack_contafin.nRefacere then
      lcAct := 'ACT';
    ELSE
      lcAct := 'ACT_TEMP';
    END IF;

    -- id_set 77772, 77773, 77774 - note reglare denominare 01-07-2005
    lcMerge := 'MERGE INTO BALANTA_PARTENERI B
    USING (SELECT J.ID_SUCURSALA,
                  J.CONT,
                  J.ACONT,
                  J.ID_PART,
                  J.ID_VALUTA,
                  SUM(J.DEBIT) AS DEBIT,
                  SUM(J.CREDIT) AS CREDIT,
                  SUM(J.VALDEBIT) AS VALDEBIT,
                  SUM(J.VALCREDIT) AS VALCREDIT
             FROM (SELECT ID_SUCURSALA,
                          scd as cont,
                          ascd as acont,
                          id_partd as id_part,
                          id_valuta,
                          sum(suma) as DEBIT,
                          0.0000 AS CREDIT,
                          sum(suma_val) as VALDEBIT,
                          0.0000 AS VALCREDIT
                     FROM ' || lcAct ||
               ' WHERE STERS = 0
                      and scd is not null
                      AND AN = :1
                      AND LUNA = :2 ' || lcUpdWherePartD ||
               lcWhereSucursale || case
                 when tcCont is not null then
                  'AND scd = :3'
                 else
                  ''
               end || ' AND ID_SET NOT IN (77772, 77773, 77774)
                   GROUP BY ID_SUCURSALA, SCD, ASCD, ID_PARTD, ID_VALUTA
                   UNION ALL
                   SELECT ID_SUCURSALA, scc as cont,
                          ascc as acont,
                          id_partc as id_part,
                          id_valuta,
                          0.0000 AS DEBIT,
                          sum(suma) as CREDIT,
                          0.0000 AS VALDEBIT,
                          sum(suma_val) as VALCREDIT
                     FROM ' || lcAct ||
               ' WHERE STERS = 0
                      and scc is not null
                      AND AN = :4
                      AND LUNA = :5 ' || lcUpdWherePartC ||
               lcWhereSucursale || case
                 when tcCont is not null then
                  'AND scc = :6'
                 else
                  ''
               end || ' AND ID_SET NOT IN (77772, 77773, 77774)
                      GROUP BY ID_SUCURSALA, SCC, ASCC, ID_PARTC, ID_VALUTA ) J join config_cont_ireg CI on J.cont=CI.cont
            GROUP BY J.ID_SUCURSALA, J.CONT, J.ACONT, J.ID_PART, J.ID_VALUTA) A
    ON (B.AN = :7 AND B.LUNA = :8 AND NVL(B.CONT, ''XXXX'') = NVL(A.CONT, ''XXXX'')
       AND NVL(B.ACONT, ''XXXX'') = NVL(A.ACONT, ''XXXX'') AND B.ID_PART = A.ID_PART
       AND B.ID_VALUTA = A.ID_VALUTA
       AND NVL(B.id_sucursala,0) = NVL(A.id_sucursala,0))
    WHEN MATCHED THEN
      UPDATE
         SET B.DEBIT     = B.DEBIT + A.DEBIT,
             B.CREDIT    = B.CREDIT + A.CREDIT,
             B.VALDEBIT  = B.VALDEBIT + A.VALDEBIT,
             B.VALCREDIT = B.VALCREDIT + A.VALCREDIT
    WHEN NOT MATCHED THEN
      INSERT
        (ID_SUCURSALA,
         AN,
         LUNA,
         CONT,
         ACONT,
         ID_PART,
         ID_VALUTA,
         DEBIT,
         CREDIT,
         VALDEBIT,
         VALCREDIT)
      VALUES
        (A.ID_SUCURSALA,
         :9,
         :10,
         A.CONT,
         A.ACONT,
         A.ID_PART,
         A.ID_VALUTA,
         A.DEBIT,
         A.CREDIT,
         A.VALDEBIT,
         A.VALCREDIT)';

    if tcCont is not null then
      EXECUTE IMMEDIATE lcMerge
        USING tnAn, tnLuna, tcCont, tnAn, tnLuna, tcCont, tnAn, tnLuna, tnAn, tnLuna;
    else
      EXECUTE IMMEDIATE lcMerge
        USING tnAn, tnLuna, tnAn, tnLuna, tnAn, tnLuna, tnAn, tnLuna;
    end if;

  END SCRIE_IN_BALANTA_PARTENERI;

  PROCEDURE SCRIE_IN_IREG_PARTENERI(tnAn     in IREG_PARTENERI.AN%TYPE,
                                    tnLuna   in IREG_PARTENERI.LUNA%TYPE,
                                    tcCont   in IREG_PARTENERI.CONT%TYPE,
                                    tnScrie  in number,
                                    tnIdPart in IREG_PARTENERI.ID_PART%TYPE DEFAULT NULL) IS
    lnScrie NUMBER(1); -- 0/2 = SCRIERE/STERGERE; 1 = REFACERE
    lcAct   VARCHAR2(100);
    lcMerge VARCHAR2(32000);

    lcRefacereNumarAct OPTIUNI.VARVALUE%Type;
    lnRefacereNumarAct number(10) := 0;
    lnFelCont          CONFIG_CONT_IREG.FEL_CONT%TYPE;
    lnCuInregistrari   CONFIG_CONT_IREG.CU_INREGISTRARI%TYPE;
    lnCuVenchelt       CONFIG_CONT_IREG.CU_VENCHELT%TYPE;
    lnCuProcTva        CONFIG_CONT_IREG.CU_PROC_TVA%TYPE;
    lcCondInPlus       VARCHAR2(1000);
    lcSelInPlus        VARCHAR2(1000);
    lcGrupInPlus       VARCHAR2(1000);
    lcInsertColInPlus  VARCHAR2(1000);
    lcInsertValInPlus  VARCHAR2(1000);
    tcAcontNull        VARCHAR2(4) := 'XXXX';
    ldDataNull         DATE := TO_DATE('19700101', 'YYYYMMDD');
    lnIdSucursala      act.id_sucursala%type;
    lcWhereSucursale   varchar2(1000);
    lcUpdWherePartAct  varchar2(1000);
    lcUpdWherePart     varchar2(1000);
  BEGIN

    lnIdSucursala := PACK_CONTAFIN.GET_ID_SUCURSALA();
    if lnIdSucursala is not null then
      lcWhereSucursale := ' and ALIAS.id_sucursala = ' || lnIdSucursala;
    end if;

    -- 30.06.2011
    if tnIdPart is not null then
      lcUpdWherePartAct := ' and (ALIAS.id_partd = ' || tnIdPart ||
                           ' OR ALIAS.id_partc = ' || tnIdPart || ')';
      lcUpdWherePart    := ' and ALIAS.id_part = ' || tnIdPart;
    end if;
    -- 30.06.2011 ^

    lnScrie := NVL(tnScrie, 0);
    IF lnScrie = 1 then
      lcAct := 'ACT';
    ELSE
      lcAct := 'ACT_TEMP';
    END IF;

    -- folosesc imperecherea de inregistrari dupa numar act sau id_fact
    lcRefacereNumarAct := PACK_SESIUNE.getOptiuneFirma('REFACERENUMARACT');
    IF lcRefacereNumarAct IS NULL THEN
      lnRefacereNumarAct := 0;
    ELSE
      lnRefacereNumarAct := to_number(lcRefacereNumarAct);
    END IF;

    -- felul contului;
    begin
      SELECT C.FEL_CONT, C.CU_INREGISTRARI, C.CU_VENCHELT, C.CU_PROC_TVA
        INTO lnFelCont, lnCuInregistrari, lnCuVenchelt, lnCuProcTva
        FROM VCONFIG_CONT_IREG C
       WHERE CONT = tcCont
         AND CU_INREGISTRARI = 1;
    exception
      when no_data_found then
        lnCuInregistrari := 0;
    end;

    IF lnCuInregistrari = 0 THEN
      GOTO EndProc;
    END IF;
    -- IMPERECHERE DUPA ID_FACT
    IF lnRefacereNumarAct = 0 THEN
      lcCondInPlus      := lcCondInPlus || ' AND I.ID_FACT = S.ID_FACT ';
      lcSelInPlus       := lcSelInPlus || ', ID_FACT ';
      lcGrupInPlus      := lcGrupInPlus || ', ID_FACT ';
      lcInsertColInPlus := lcInsertColInPlus || ', ID_FACT ';
      lcInsertValInPlus := lcInsertValInPlus || ', S.ID_FACT ';
    ELSE
      lcSelInPlus       := lcSelInPlus || ', MAX(ID_FACT) AS ID_FACT ';
      lcInsertColInPlus := lcInsertColInPlus || ', ID_FACT ';
      lcInsertValInPlus := lcInsertValInPlus || ', S.ID_FACT ';
    END IF;
    -- GRUPARE DUPA TIP VENIT/CHELTUIALA
    IF lnCuVenchelt = 1 THEN
      lcCondInPlus      := lcCondInPlus ||
                           ' AND I.ID_VENCHELT = S.ID_VENCHELT ';
      lcSelInPlus       := lcSelInPlus || ', ID_VENCHELT ';
      lcGrupInPlus      := lcGrupInPlus || ',ID_VENCHELT ';
      lcInsertColInPlus := lcInsertColInPlus || ', ID_VENCHELT ';
      lcInsertValInPlus := lcInsertValInPlus || ', S.ID_VENCHELT ';
    ELSE
      lcSelInPlus       := lcSelInPlus ||
                           ', MAX(ID_VENCHELT) AS ID_VENCHELT ';
      lcInsertColInPlus := lcInsertColInPlus || ', ID_VENCHELT ';
      lcInsertValInPlus := lcInsertValInPlus || ', S.ID_VENCHELT ';
    END IF;
    -- GRUPARE DUPA PROCENT TVA/ID_JTVA
    IF lnCuProcTva = 1 THEN
      -- LA INREGISTRAREA 408 AM ID_JTVA_COLOANA RO09BFB, RO09BVB...., IAR LA REGULARIZARE 408 POT SA AM RO09NB (TVA INCASARE NEEXIGIBIL)
      -- SI NU SE MAI PUPA INREGISTRARILE SI DUPA ID_JTVA_COLOANA
      -- INCERC SA LE PUP DOAR DUPA PROC_TVA (STIU CA POATE SA SARA IN AER) :)
      --      lcCondInPlus      := lcCondInPlus || ' AND NVL(I.PROC_TVA,0) = NVL(S.PROC_TVA,0) AND NVL(I.ID_JTVA_COLOANA,0) = NVL(S.ID_JTVA_COLOANA,0) ';
      lcCondInPlus      := lcCondInPlus ||
                           ' AND NVL(I.PROC_TVA,0) = NVL(S.PROC_TVA,0) ';
      lcSelInPlus       := lcSelInPlus ||
                           ', PROC_TVA, MAX(ID_JTVA_COLOANA) AS ID_JTVA_COLOANA';
      lcGrupInPlus      := lcGrupInPlus || ',  PROC_TVA ';
      lcInsertColInPlus := lcInsertColInPlus ||
                           ', PROC_TVA, ID_JTVA_COLOANA ';
      lcInsertValInPlus := lcInsertValInPlus ||
                           ', S.PROC_TVA, S.ID_JTVA_COLOANA ';
    END IF;

    pack_contafin.COMPLETEAZA_IREG_PARTENERI(tnAn,
                                             tnLuna,
                                             lcAct,
                                             lcWhereSucursale,
                                             lcCondInPlus,
                                             tcCont,
                                             tnIdPart);
    -- SELECTEZ TOATE FACTURILE DIN REGISTRUL JURNAL
    -- (CONTUL ESTE PE DEBIT SI ESTE ACTIV SAU
    -- CONTUL ESTE PE CREDIT SI ESTE ACTIV SI ESTE EXCEPTIE SAU REEVALUARE IN VALUTA ETC.
    -- NU ESTE 4111 = 4427 CU PERECHED SAU ID_FACTD COMPLETAT (REGULARIZARE AVANS) 419 = 4111; 4111 = 4427)

    -- INVERSEZ SUME, CONTURI, PARTENERI PENTRU EXCEPTII SI REEVALUARE IN VALUTA
    -- GRUPEZ DUPA NRACT, DATACT ETC.
    -- MERGE IN IREG_PARTENERI
    -- WHEN MATCHED : ACTUALIZEZ SUMELE SI CURSUL VALUTAR PENTRU REEVALUARE SOLDURI IN VALUTA
    --              : STERG LINIA DACA OPERATIA ESTE STERGERE SI AM NUMAI ZERO-URI
    -- WHEN NOT MATCHED : ADAUG LINIE IN IREG_PARTENERI

    -- ID_SET = 90501 : REEVALUARE SOLDURI IN VALUTA LA 31.12.XXXX
    -- A.ID_SET NOT IN (77771,77772,77773,77774) : NOTE REGULARIZARE DENOMINARE - AFECTEAZA DOAR BALANTA DE VERIFICARE

    lcMerge := 'MERGE INTO IREG_PARTENERI I  USING (SELECT an,
       luna,
       cont,
       acont,
       nract,
       serie_act,
       dataact,
       dataireg,
       MAX(datascad) AS DATASCAD,
       id_part,
       id_valuta,
       id_sucursala,
       SUM(debit) AS DEBIT,
       SUM(credit) AS CREDIT,
       SUM(valdebit) AS VALDEBIT,
       SUM(valcredit) AS VALCREDIT,
       max(curs) as curs,
       max(id_fdoc) as id_fdoc,
       max(id_lucrare) as id_lucrare,
       max(id_set) as id_set,
       max(id_ctr) as id_ctr,
       max(id_responsabil) as id_responsabil,
       max(cod) as cod,
       max(explicatia) as explicatia,
       max(explicatia4) as explicatia4,
       max(explicatia5) as explicatia5 ' || lcSelInPlus ||
               ' FROM (SELECT A.AN,
               A.LUNA,
               A.COD,
               A.NRACT,
               A.SERIE_ACT,
               TRUNC(A.DATAACT) AS DATAACT,
               TRUNC(A.DATASCAD) AS DATASCAD,
               TRUNC(A.DATAIREG) AS DATAIREG,
               :1 AS CONT,
               (CASE
                 WHEN ((A.SCD =  :2 AND CD.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 0) OR
                      (A.SCC =  :3 AND CC.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 1))
                 THEN CASE WHEN NVL(E.EXCEPTIE,0) = 1 THEN A.ASCC ELSE A.ASCD END
                 ELSE CASE WHEN NVL(E.EXCEPTIE,0) = 1 THEN A.ASCD ELSE A.ASCC END
               END) AS ACONT,
               (CASE
                 WHEN ((A.SCD = :4 AND CD.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 0) OR
                      (A.SCC = :5 AND CC.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 1))
                 THEN   CASE WHEN NVL(E.EXCEPTIE,0) = 1 THEN -SUMA ELSE SUMA END
                 ELSE   0.00
               END) AS DEBIT,
               (CASE
                 WHEN ((A.SCC = :6 AND CC.FEL_CONT IN (1,2) AND NVL(E.EXCEPTIE, 0) = 0) OR
                      (A.SCD = :7 AND CD.FEL_CONT IN (1,2) AND NVL(E.EXCEPTIE, 0) = 1))
                 THEN CASE WHEN NVL(E.EXCEPTIE,0) = 1 THEN -SUMA ELSE SUMA END
                 ELSE 0.00
               END) AS CREDIT,
               (CASE
                 WHEN ((A.SCD = :8 AND CD.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 0) OR
                      (A.SCC = :9 AND CC.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 1 ))
                 THEN CASE WHEN NVL(E.EXCEPTIE,0) = 1 THEN -SUMA_VAL ELSE SUMA_VAL END
                 ELSE 0.00
               END) AS VALDEBIT,
               (CASE
                 WHEN ((A.SCC = :10 AND CC.FEL_CONT IN (1,2) AND NVL(E.EXCEPTIE, 0) = 0) OR
                      (A.SCD = :11 AND CD.FEL_CONT IN (1,2) AND NVL(E.EXCEPTIE, 0) = 1))
                 THEN CASE WHEN NVL(E.EXCEPTIE,0) = 1 THEN -SUMA_VAL ELSE SUMA_VAL END
                 ELSE 0.00
               END) AS VALCREDIT,
               (CASE
                 WHEN (A.SCD = :12 AND CD.FEL_CONT IN (0,2)) OR
                      (A.SCD = :13 AND CD.FEL_CONT IN (1,2) AND NVL(E.EXCEPTIE, 0) = 1)
                      THEN ID_PARTD
                 WHEN (A.SCC = :14 AND CC.FEL_CONT IN (1,2)) OR
                      (A.SCC = :15 AND CC.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 1)
                      THEN ID_PARTC
                 ELSE 0
               END) ID_PART,
               A.ID_FACT,
               A.ID_FDOC,
               A.ID_VALUTA,
               A.ID_SUCURSALA,
               A.ID_CTR,
               A.ID_LUCRARE,
               A.ID_RESPONSABIL,
               A.ID_SET,
               A.ID_VENCHELT,
               NVL(T.ID_BAZA, A.ID_JTVA_COLOANA) AS ID_JTVA_COLOANA,
               A.PROC_TVA,
               A.CURS,
               A.explicatia,
               A.explicatia4,
               A.explicatia5
          FROM ' || lcAct || ' 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 CONT = ''' || tcCont ||
               ''') E ON A.SCD = E.SCD
                                         AND A.SCC = E.SCC
          LEFT JOIN CONFIG_CONT_IREG CD ON A.SCD = CD.CONT
          LEFT JOIN CONFIG_CONT_IREG CC ON A.SCC = CC.CONT
          LEFT JOIN (select id_jtva_coloana,
                           connect_by_root id_jtva_coloana as id_baza
                      from jtva_coloane
                     where level > 1
                     start with id_tva is not null
                    connect by prior id_tva = id_jtva_coloana) T ON A.ID_JTVA_COLOANA = T.ID_JTVA_COLOANA
           WHERE A.AN = :16
           and A.LUNA = :17
           and A.STERS = 0 ' ||
               REPLACE(lcWhereSucursale, 'ALIAS.', 'A.') ||
               REPLACE(lcUpdWherePartAct, 'ALIAS.', 'A.') || '
           AND A.ID_SET NOT IN (77771,77772,77773,77774, 90501) ' || CASE
                 WHEN tcCont = '4111' THEN
                  ' AND NOT (A.SCD = ''4111'' AND A.SCC = ''4427'' AND (NVL(PERECHED,0) <> 0 OR NVL(ID_FACTD,0)<>0))
                 AND NOT (A.SCD = ''419'' AND A.SCC = ''4111'' AND (NVL(ID_FACTD,0) <> 0 AND NVL(ID_FACTC,0)<>0))'
                 ELSE
                  ''
               END || ' AND ((A.SCD = :18 AND CD.FEL_CONT IN (0,2) AND NVL(A.ID_FACTD,0) = 0) OR
               (A.SCC = :19 AND CC.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 1) OR
               (A.SCC = :20 AND CC.FEL_CONT IN (1,2) AND NVL(A.ID_FACTC,0) = 0) OR
               (A.SCD = :21 AND CD.FEL_CONT IN (1,2) AND NVL(E.EXCEPTIE, 0) = 1)))
 group by an,
          luna,
          cont,
          acont,
          nract,
          serie_act,
          dataact,
          dataireg,
          id_part,
          id_valuta, id_sucursala ' || lcGrupInPlus || ') S
ON (I.AN = :22 AND I.LUNA = :23 AND I.CONT = :24 AND NVL(I.ACONT, :25) = NVL(S.ACONT, :26) AND
NVL(I.NRACT,0) = NVL(S.NRACT,0) AND NVL(I.SERIE_ACT,''+_'')=NVL(S.SERIE_ACT,''+_'') AND NVL(I.DATAACT,:27) = NVL(S.DATAACT,:28) AND
NVL(I.DATAIREG,:29) = NVL(S.DATAIREG,:30) AND NVL(I.DATASCAD,:31) = NVL(S.DATASCAD,:32) AND
NVL(I.ID_PART,0) = NVL(S.ID_PART,0) AND NVL(I.ID_VALUTA,0) = NVL(S.ID_VALUTA,0) AND NVL(I.ID_SUCURSALA,0) = NVL(S.ID_SUCURSALA,0) ' ||
               lcCondInPlus || ') WHEN MATCHED THEN
UPDATE
SET I.DEBIT = I.DEBIT + S.DEBIT,
    I.CREDIT = I.CREDIT + S.CREDIT,
    I.VALDEBIT = I.VALDEBIT + S.VALDEBIT,
    I.VALCREDIT = I.VALCREDIT + S.VALCREDIT

DELETE WHERE ' || lnScrie || ' = ' || pack_contafin.nStergere ||
               ' and I.precdeb = 0
         and I.preccred = 0
         and I.precvaldeb = 0
         and I.precvalcred = 0
         and I.debit = 0
         and I.credit = 0
         and I.valdebit = 0
         and I.valcredit = 0
   WHEN NOT MATCHED THEN
   INSERT
    (AN, LUNA, CONT, ACONT, ID_PART, ID_VALUTA, ID_SUCURSALA,
        id_fdoc, id_lucrare, id_set, id_ctr,
        curs, dataact, dataireg, datascad, nract, serie_act,
        id_responsabil, cod, explicatia, explicatia4, explicatia5,
        DEBIT, CREDIT, VALDEBIT, VALCREDIT ' ||
               lcInsertColInPlus || ')
  VALUES
    (:33, :34, S.CONT, S.ACONT, S.ID_PART, S.id_valuta, S.id_sucursala,
        S.id_fdoc, S.id_lucrare, S.id_set, S.id_ctr,
        S.curs, S.dataact, S.dataireg, S.datascad, S.nract, S.serie_act,
        S.id_responsabil, S.cod, S.explicatia, S.explicatia4, S.explicatia5,
        S.DEBIT, S.CREDIT, S.VALDEBIT, S.VALCREDIT ' ||
               lcInsertValInPlus || ')';

    -- pInfo(lcMerge, 'SCRIE_IREG INCASARI 1');

    EXECUTE IMMEDIATE lcMerge
      USING tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tnAn, tnLuna, tcCont, tcCont, tcCont, tcCont, tnAn, tnLuna, tcCont, tcAcontNull, tcAcontNull, ldDataNull, ldDataNull, ldDataNull, ldDataNull, ldDataNull, ldDataNull, tnAn, tnLuna;

    -- IMPERECHERE INCASARI/PLATI (CONT ACTIV PE CREDIT SAU PASIV PE DEBIT SI NU SUNT EXCEPTII)
    -- GRUPEZ DUPA NRACT, MIN(IREG_PARTENERI.DATACT) ETC.
    -- MERGE IN IREG_PARTENERI
    -- WHEN MATCHED : ACTUALIZEZ SUMELE
    --              : STERG LINIA DACA OPERATIA ESTE STERGERE SI AM NUMAI ZERO-URI
    -- WHEN NOT MATCHED : ADAUG LINIE IN IREG_PARTENERI

    -- ID_SET = 90501 : REEVALUARE SOLDURI IN VALUTA LA 31.12.XXXX
    -- A.ID_SET NOT IN (77771,77772,77773,77774) : NOTE REGULARIZARE DENOMINARE - AFECTEAZA DOAR BALANTA DE VERIFICARE

    lcMerge := 'MERGE INTO IREG_PARTENERI I  USING (' || CASE lnRefacereNumarAct
                 WHEN 1 THEN
                  'SELECT S1.an,
             S1.luna,
             S1.cont,
             S1.acont,
             S1.nract,
             S1.id_part,
             S1.id_valuta,
             S1.id_sucursala,
             S1.curs,' || CASE
                    WHEN lnCuProcTva = 1 THEN
                     'S1.PROC_TVA, S1.ID_JTVA_COLOANA,'
                    ELSE
                     ''
                  END || CASE
                    WHEN lnCuVenchelt = 1 THEN
                     'S1.ID_VENCHELT,'
                    ELSE
                     ''
                  END || 'S1.DEBIT,
             S1.CREDIT,
             S1.VALDEBIT,
             S1.VALCREDIT,
             S1.ID_FACT,
             MIN(I1.DATAACT) AS DATAACT,
             MAX(S1.ID_VENCHELT) AS ID_VENCHELT
        FROM ('
                 ELSE
                  ''
               END || 'SELECT an,
       luna,
       cont,
       acont,
       nract,
       id_part,
       id_valuta,
       id_sucursala,
       MAX(curs) AS CURS,
       SUM(debit) AS DEBIT,
       SUM(credit) AS CREDIT,
       SUM(valdebit) AS VALDEBIT,
       SUM(valcredit) AS VALCREDIT ' || lcSelInPlus ||
               '  FROM (SELECT A.AN,
               A.LUNA,
               A.COD,
       CASE
         WHEN (A.SCC = :1 AND NVL(CC.FEL_CONT, 0) = 0) OR
              (A.SCC = :2 AND NVL(CC.FEL_CONT, 0) IN (1,2) AND NVL(A.PERECHEC, 0) <> 0) THEN
          A.PERECHEC
         WHEN (A.SCD = :3 AND NVL(CD.FEL_CONT, 0) = 1) OR
              (A.SCD = :4 AND NVL(CD.FEL_CONT, 0) IN (0,2) AND NVL(A.PERECHED, 0) <> 0) THEN
          A.PERECHED
         ELSE
          0
       END NRACT,
       CASE
         WHEN (A.SCC = :5 AND NVL(CC.FEL_CONT, 0) = 0) OR
              (A.SCC = :6 AND NVL(CC.FEL_CONT, 0) IN (1,2) AND NVL(A.ID_FACTC, 0) <> 0) THEN
          A.ID_FACTC
         WHEN (A.SCD = :7 AND NVL(CD.FEL_CONT, 0) = 1) OR
              (A.SCD = :8 AND NVL(CD.FEL_CONT, 0) IN (0,2) AND NVL(A.ID_FACTD, 0) <> 0) THEN
          A.ID_FACTD
         ELSE
          0
       END ID_FACT,
               :9 AS CONT,
               DECODE(A.SCD, :10, A.ASCD, A.ASCC) AS ACONT,
       (CASE
         WHEN (A.SCD = :11 AND ((CD.FEL_CONT IN(1,2) AND A.ID_SET <> 90501) OR
              (CD.FEL_CONT = 0 AND A.ID_SET = 90501))) OR
              (A.SCC = :12 AND ((CC.FEL_CONT = 0 AND A.ID_SET = 90501) OR
              (CC.FEL_CONT = 1 AND A.ID_SET <> 90501 AND NVL(A.ID_FACTC, 0) <> 0))) THEN
          (CASE
         WHEN A.SCC = :13 AND ((CC.FEL_CONT = 0 AND A.ID_SET = 90501) OR
              (CC.FEL_CONT = 1 AND A.ID_SET <> 90501 AND NVL(A.ID_FACTC, 0) <> 0)) THEN
          -A.SUMA
         ELSE
          A.SUMA
       END) ELSE 0.00 END) AS DEBIT,
       (CASE
         WHEN (A.SCC = :14 AND ((CC.FEL_CONT IN(0,2) AND A.ID_SET <> 90501) OR
              (CC.FEL_CONT = 1 AND A.ID_SET = 90501))) OR
              (A.SCD = :15 AND ((CD.FEL_CONT = 1 AND A.ID_SET = 90501) OR
              (CD.FEL_CONT = 0 AND A.ID_SET <> 90501 AND NVL(A.ID_FACTD, 0) <> 0))) THEN
          (CASE
         WHEN A.SCD = :16 AND ((CD.FEL_CONT = 1 AND A.ID_SET = 90501) OR
              (CD.FEL_CONT = 0 AND A.ID_SET <> 90501 AND NVL(A.ID_FACTD, 0) <> 0)) THEN
          -A.SUMA
         ELSE
          A.SUMA
       END) ELSE 0.00 END) AS CREDIT,
               (CASE
                 WHEN (A.SCD = :17 AND ((NVL(CD.FEL_CONT,0) IN(1,2) AND A.ID_SET <> 90501) OR
                           (NVL(CD.FEL_CONT,0) = 0 AND A.ID_SET = 90501))) OR
                      (A.SCC = :18 AND ((NVL(CC.FEL_CONT,0) = 0 AND A.ID_SET = 90501) OR
                           (NVL(CC.FEL_CONT,0) = 1 AND A.ID_SET <> 90501 AND NVL(A.ID_FACTC, 0) <> 0)))
                    THEN
                   (CASE WHEN A.SCC = :19 AND ((NVL(CC.FEL_CONT,0) = 0 AND A.ID_SET = 90501) OR
                              (NVL(CC.FEL_CONT,0) = 1 AND A.ID_SET <> 90501 AND NVL(A.ID_FACTC, 0) <> 0))
                         THEN
                           -A.SUMA_VAL
                         ELSE
                           A.SUMA_VAL
                    END)
                 ELSE
                  0.00
               END) AS VALDEBIT,
               (CASE
                 WHEN (A.SCC =  :20 AND ((NVL(CC.FEL_CONT,0) IN(0,2) AND A.ID_SET <> 90501) OR
                         (NVL(CC.FEL_CONT,0) = 1 AND A.ID_SET = 90501))) OR
                      (A.SCD =  :21 AND ((NVL(CD.FEL_CONT,0) = 1 AND A.ID_SET = 90501) OR
                         (NVL(CD.FEL_CONT,0) = 0 AND A.ID_SET <> 90501 AND NVL(A.ID_FACTD, 0) <> 0)))
                 THEN
                   (CASE WHEN A.SCD =  :22 AND ((NVL(CD.FEL_CONT,0) = 1 AND A.ID_SET = 90501) OR
                             (NVL(CD.FEL_CONT,0) = 0 AND A.ID_SET <> 90501 AND NVL(A.ID_FACTD, 0) <> 0))
                             THEN
                               -A.SUMA_VAL
                             ELSE
                               A.SUMA_VAL
                    END)
                 ELSE
                  0.00
               END) AS VALCREDIT,
               CASE
                 WHEN (A.SCC = :23 AND (CC.FEL_CONT IN(0,2) OR (CC.FEL_CONT = 1 AND A.ID_SET = 90501)))
                 THEN A.ID_PARTC
                 WHEN (A.SCD = :24 AND (CD.FEL_CONT IN(1,2) OR (CD.FEL_CONT = 0 AND (A.ID_SET = 90501 OR NVL(PERECHED,0)<>0 OR NVL(ID_FACTD,0)<>0))))
                 THEN A.ID_PARTD
                 ELSE 0
               END ID_PART,
               A.ID_VALUTA,
               A.ID_SUCURSALA,
               CASE WHEN A.ID_SET = 90501 THEN A.CURS ELSE 0 END AS CURS,
               A.ID_VENCHELT,
               A.ID_JTVA_COLOANA,
               A.PROC_TVA
          FROM ' || lcAct || ' 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 CONT = ''' || tcCont ||
               ''') E ON A.SCD = E.SCD
                                         AND A.SCC = E.SCC
          LEFT JOIN CONFIG_CONT_IREG CD ON A.SCD = CD.CONT
          LEFT JOIN CONFIG_CONT_IREG CC ON A.SCC = CC.CONT
         WHERE A.AN = :25
           and A.LUNA = :26
           and A.STERS = 0 ' ||
               REPLACE(lcWhereSucursale, 'ALIAS.', 'A.') ||
               REPLACE(lcUpdWherePartAct, 'ALIAS.', 'A.') || '
           AND A.ID_SET NOT IN (77771,77772,77773,77774)
           AND ((A.SCC = :27 AND CC.FEL_CONT = 0 AND NVL(E.EXCEPTIE, 0) = 0) OR
                (A.SCD = :28 AND CD.FEL_CONT = 1 AND NVL(E.EXCEPTIE, 0) = 0) OR
                (A.SCC = :29 AND CC.FEL_CONT IN (1,2) AND NVL(E.EXCEPTIE, 0) = 0 AND NVL(A.ID_FACTC, 0) <> 0) OR
                (A.SCD = :30 AND CD.FEL_CONT IN (0,2) AND NVL(E.EXCEPTIE, 0) = 0 AND NVL(A.ID_FACTD, 0) <> 0) OR
                ((A.SCD = :31 OR A.SCC = :32) AND A.ID_SET = 90501) ' ||
              -- 4111 = 4427 (AVANS 419)
               CASE
                 WHEN tcCont = '4111' THEN
                  ' OR (A.SCD = ''4111'' AND A.SCC = ''4427'' AND (NVL(A.PERECHED,0) <> 0 OR NVL(A.ID_FACTD,0)<>0))
       OR (A.SCD = ''419'' AND A.SCC = ''4111'' AND (NVL(A.ID_FACTD,0) <> 0 AND NVL(A.ID_FACTC,0)<>0))'
                 ELSE
                  ''
               END || '))
 group by an,
          luna,
          cont,
          acont,
          nract,
          id_part,
          id_valuta,id_sucursala' || lcGrupInPlus || CASE lnRefacereNumarAct
                 WHEN 1 THEN
                  ') S1
  LEFT JOIN IREG_PARTENERI I1 ON I1.AN = ' || tnAn ||
                  ' AND I1.LUNA = ' || tnLuna ||
                 -- AM FOST NEVOIT - SA NU STRIC CELELALTE VARIABILE LEGATE :(
                  ' AND I1.CONT = ' || tcCont ||
                  ' AND NVL(I1.ACONT, ''XXXX'') =
                                 NVL(S1.ACONT, ''XXXX'')
                             AND I1.NRACT = S1.NRACT
                             AND I1.ID_PART = S1.ID_PART
                             AND I1.ID_VALUTA = S1.ID_VALUTA
                             AND NVL(I1.ID_SUCURSALA,0) = NVL(S1.ID_SUCURSALA,0) ' ||
                  REPLACE(lcUpdWherePart, 'ALIAS.', 'I1.') ||
                  ' GROUP BY S1.an,
          S1.luna,
          S1.cont,
          S1.acont,
          S1.nract,
          S1.id_part,
          S1.id_valuta,
          S1.id_sucursala,
          S1.curs,' || CASE
                    WHEN lnCuProcTva = 1 THEN
                     'S1.PROC_TVA, S1.ID_JTVA_COLOANA,'
                    ELSE
                     ''
                  END || CASE
                    WHEN lnCuVenchelt = 1 THEN
                     'S1.ID_VENCHELT,'
                    ELSE
                     ''
                  END || 'S1.DEBIT,
          S1.CREDIT,
          S1.VALDEBIT,
          S1.VALCREDIT,
          S1.ID_FACT'
                 ELSE
                  ''
               END || ') S
ON (I.AN = :33 AND I.LUNA = :34 AND I.CONT = :35 AND NVL(I.ACONT, :36) = NVL(S.ACONT, :37) AND NVL(I.NRACT,0) = NVL(S.NRACT,0) AND ' ||
               CASE lnRefacereNumarAct
                 WHEN 1 THEN
                  ' NVL(I.DATAACT,TO_DATE(''19700101'', ''YYYYMMDD'')) = NVL(S.DATAACT,TO_DATE(''19700101'', ''YYYYMMDD'')) AND '
                 ELSE
                  ''
               END ||
               ' NVL(I.ID_PART,0) = NVL(S.ID_PART,0) AND NVL(I.ID_VALUTA,0) = NVL(S.ID_VALUTA,0) AND NVL(I.ID_SUCURSALA,0) = NVL(S.ID_SUCURSALA,0) ' ||
               lcCondInPlus || ') WHEN MATCHED THEN
UPDATE
SET I.DEBIT = I.DEBIT + S.DEBIT,
    I.CREDIT = I.CREDIT + S.CREDIT,
    I.VALDEBIT = I.VALDEBIT + S.VALDEBIT,
    I.VALCREDIT = I.VALCREDIT + S.VALCREDIT,
    I.CURS = (CASE NVL(S.CURS,0) WHEN 0 THEN I.CURS ELSE S.CURS END)

DELETE WHERE ' || lnScrie || ' = ' || pack_contafin.nStergere ||
               ' and I.precdeb = 0
         and I.preccred = 0
         and I.precvaldeb = 0
         and I.precvalcred = 0
         and I.debit = 0
         and I.credit = 0
         and I.valdebit = 0
         and I.valcredit = 0
 WHEN NOT MATCHED THEN
   INSERT
    (AN, LUNA, CONT, ACONT, ID_PART, ID_VALUTA, ID_SUCURSALA,
        nract, DEBIT, CREDIT, VALDEBIT, VALCREDIT ' ||
               lcInsertColInPlus || ')
  VALUES
    (:38, :39, S.CONT, S.ACONT, S.ID_PART, S.ID_VALUTA, S.ID_SUCURSALA,
        S.nract, S.DEBIT, S.CREDIT, S.VALDEBIT, S.VALCREDIT' ||
               lcInsertValInPlus || ')';

    -- pInfo(lcMerge, 'SCRIE_IREG INCASARI 2');

    EXECUTE IMMEDIATE lcMerge
      USING tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tnAn, tnLuna, tcCont, tcCont, tcCont, tcCont, tcCont, tcCont, tnAn, tnLuna, tcCont, tcAcontNull, tcAcontNull, tnAn, tnLuna;

    <<EndProc>>
    NULL;

  END SCRIE_IN_IREG_PARTENERI;

  ---------------------------------------------------------------
  PROCEDURE EXECUTA_SCRIE_IN_IREG(tcS        VARCHAR2,
                                  tnAn       in number,
                                  tnLuna     in number,
                                  tcCont     in varchar2,
                                  tnScrie    number,
                                  tnVenChelt number) IS
  BEGIN
    -- TNSCRIE: 0 = SCRIERE; 2 = STERGERE
    -- SE APELEAZA DOAR PENTRU ACT_TEMP, FARA CONT PRECIZAT
    FOR ITEM IN (SELECT SCD AS CONT
                   FROM ACT_TEMP
                 UNION
                 SELECT SCC AS CONT
                   FROM ACT_TEMP) LOOP
      SCRIE_IN_IREG_PARTENERI(tnAn, tnLuna, ITEM.CONT, tnScrie, null);
    END LOOP;

  END EXECUTA_SCRIE_IN_IREG;

  -------------------------------------------------------------------
  PROCEDURE STERGE_TEMP_ACTRUL IS
  BEGIN
    DELETE FROM ACT_TEMP;
    DELETE FROM RUL_TEMP;
    DELETE FROM RUL_TEMP_OBINV;
  END STERGE_TEMP_ACTRUL;
  -------------------------------------------------------------------
  Procedure INIT_SCRIERE_ACT_RUL_LOCAL(tnIdUtil          SYN_UTILIZATORI.ID_UTIL%TYPE,
                                       tnAn              ACT.AN%TYPE,
                                       tnLuna            ACT.LUNA%TYPE,
                                       tnSuprascriereCod NUMBER,
                                       tnScrieSterge     NUMBER,
                                       tnIdSucursala     ACT.ID_SUCURSALA%TYPE) is
    -- se apeleaza inainte de completarea act_temp din Fox
    -- tnScrie_Sterge: 0 = scriere, 1 = refacere, 2 = stergere
  begin
    pack_contafin.STERGE_TEMP_ACTRUL();
    pack_contafin.INITIALIZEAZA_SCRIERE_ACT_RUL(tnIdUtil,
                                                NULL,
                                                tnAn,
                                                tnLuna,
                                                tnSuprascriereCod,
                                                1,
                                                tnScrieSterge,
                                                tnIdSucursala);
  end;
  -------------------------------------------------------------------
  PROCEDURE INITIALIZEAZA_SCRIERE_ACT_RUL(tnIdUtil             ACT.ID_UTIL%TYPE,
                                          tdDataOra            ACT.DATAORA%TYPE,
                                          tnAn                 ACT.AN%TYPE,
                                          tnLuna               ACT.LUNA%TYPE,
                                          tnSuprascriereCod    NUMBER,
                                          tnSuprascriereAnLuna NUMBER,
                                          tnScrieSterge        NUMBER,
                                          tnIdSucursala        ACT.ID_SUCURSALA%TYPE) IS
    -- tnScrie_Sterge: 0 = scriere, 1 = refacere, 2 = stergere
  BEGIN
    pack_contafin.reset_variables();
    if tnSuprascriereCod = 0 then
      pack_contafin.set_suprascriereCod(tnSuprascriereCod);
    Else
      if tnScrieSterge <> 2 then
        pack_contafin.SET_COD(user);
      end if;
    end if;
    pack_contafin.set_suprascriereAnLuna(tnSuprascriereAnLuna);
    pack_contafin.set_idutil(tnIdUtil);
    pack_contafin.set_dataora(NVL(tdDataOra, SYSDATE));
    pack_sesiune.setan(tnAn);
    pack_sesiune.setluna(tnLuna);
    pack_contafin.set_id_sucursala(tnIdSucursala);
  END INITIALIZEAZA_SCRIERE_ACT_RUL;
  -------------------------------------------------------------------

  -------------------------------------------------------------------
  -- sterge un document direct din baza de date, complet
  -------------------------------------------------------------------
  procedure sterge_document(tnCod    in number,
                            tnAn     in number,
                            tnLuna   in number,
                            tnIdUtil in number) is
  begin
    pack_sesiune.setan(tnAn);
    pack_sesiune.setluna(tnLuna);
    pack_sesiune.set_id_util(tnIdUtil);
    pack_contafin.sterge_document(tnCod);
  end sterge_document;

  -------------------------------------------------------------------
  -- sterge un document direct din baza de date, complet
  -- trebuie configurat an, luna, id_util
  -------------------------------------------------------------------
  procedure sterge_document(V_cod in number) is
    lnAn            number(4) := 0;
    lnLuna          number(2) := 0;
    lnAreReferinte  number(1) := 0;
    lnIdVanzare     vanzari.id_vanzare%type;
    lcMesajRefacere varchar2(3000);
    lnIdUtil        syn_utilizatori.id_util%type;
    lnIdSet         act.id_set%type;
    lnIdFact        act.id_fact%type;
    lnIdFactD       act.id_factd%type;
  begin

    lnAn           := pack_sesiune.getan();
    lnLuna         := pack_sesiune.getluna();
    lnIdUtil       := pack_sesiune.get_id_util();
    lnAreReferinte := pack_documente.ReferinteDocumenteNota(lnAn,
                                                            lnLuna,
                                                            V_cod);

    if lnAreReferinte = 1 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Documentul are referinte (incasari/plati). Nu poate fi sters.');
      goto sfarsit_procedura;
    end if;

    insert into act_temp
      (luna,
       an,
       cod,
       dataireg,
       nract,
       dataact,
       explicatia,
       scd,
       ascd,
       scc,
       ascc,
       suma,
       pereched,
       perechec,
       suma_val,
       curs,
       datascad,
       neimpozab,
       nnir,
       id_util,
       dataora,
       id_utils,
       dataoras,
       id_responsabil,
       id_venchelt,
       id_sectie,
       id_set,
       id_fact,
       id_partd,
       id_partc,
       id_sucursala,
       id_fdoc,
       explicatia4,
       explicatia5,
       id_lucrare,
       id_gestin,
       id_gestout,
       id_valuta,
       proc_tva,
       id_act,
       sters,
       id_factd,
       id_factc,
       id_ctr,
       id_jtva_coloana,
       serie_act,
       validat,
       id_utilv,
       dataorav)
      select luna,
             an,
             cod,
             dataireg,
             nract,
             dataact,
             explicatia,
             scd,
             ascd,
             scc,
             ascc,
             suma,
             pereched,
             perechec,
             suma_val,
             curs,
             datascad,
             neimpozab,
             nnir,
             id_util,
             dataora,
             id_utils,
             dataoras,
             id_responsabil,
             id_venchelt,
             id_sectie,
             id_set,
             id_fact,
             id_partd,
             id_partc,
             id_sucursala,
             id_fdoc,
             explicatia4,
             explicatia5,
             id_lucrare,
             id_gestin,
             id_gestout,
             id_valuta,
             proc_tva,
             id_act,
             sters,
             id_factd,
             id_factc,
             id_ctr,
             id_jtva_coloana,
             serie_act,
             validat,
             id_utilv,
             dataorav
        from act
       where an = lnAn
         and luna = lnLuna
         and cod = V_cod
       order by id_act;

    insert into rul_temp
      (id_rul,
       an,
       luna,
       cod,
       nnir,
       id_articol,
       id_gestiune,
       pret,
       pretv,
       tva,
       tvav,
       cant,
       cante,
       cont,
       adaos,
       id_lucrare,
       id_responsabil,
       dataact,
       procent,
       curs,
       pretd,
       id_util,
       dataora,
       id_sectie,
       alcooltutun,
       pretvtva,
       acont,
       datain,
       dataout,
       proc_disc,
       discunitar,
       proc_tva,
       id_fact,
       sters,
       discount_tva,
       adaos_tva,
       proc_tvav,
       valoare,
       valoarev,
       valtva,
       valtvav,
       valoare_adaos,
       valtva_adaos,
       valoare_discount,
       valtva_discount,
       id_gestiunec,
       id_responsabilc,
       id_tip_rulaj,
       id_reteta,
       id_set,
       id_sucursala,
       id_obiect_depozit,
       id_rul_aux,
       nract,
       id_utils,
       dataoras,
       serie,
       id_valuta,
       serie_act,
       lot,
       adata_expirare,
       id_part_rez,
       id_lucrare_rez)
      select id_rul,
             an,
             luna,
             cod,
             nnir,
             id_articol,
             id_gestiune,
             pret,
             pretv,
             tva,
             tvav,
             cant,
             cante,
             cont,
             adaos,
             id_lucrare,
             id_responsabil,
             dataact,
             procent,
             curs,
             pretd,
             id_util,
             dataora,
             id_sectie,
             alcooltutun,
             pretvtva,
             acont,
             datain,
             dataout,
             proc_disc,
             discunitar,
             proc_tva,
             id_fact,
             sters,
             discount_tva,
             adaos_tva,
             proc_tvav,
             valoare,
             valoarev,
             valtva,
             valtvav,
             valoare_adaos,
             valtva_adaos,
             valoare_discount,
             valtva_discount,
             id_gestiunec,
             id_responsabilc,
             id_tip_rulaj,
             id_reteta,
             id_set,
             id_sucursala,
             id_obiect_depozit,
             id_rul_aux,
             nract,
             id_utils,
             dataoras,
             serie,
             id_valuta,
             serie_act,
             lot,
             adata_expirare,
             id_part_rez,
             id_lucrare_rez
        from rul
       where an = lnAn
         and luna = lnLuna
         and cod = V_cod
       order by id_rul;

    insert into rul_temp_obinv
      (id_rul_obinv,
       an,
       luna,
       cod,
       nnir,
       id_articol,
       id_gestiune,
       pret,
       pretv,
       tva,
       tvav,
       cant,
       cante,
       cont,
       adaos,
       id_lucrare,
       id_responsabil,
       dataact,
       procent,
       curs,
       pretd,
       id_util,
       dataora,
       id_sectie,
       alcooltutun,
       pretvtva,
       acont,
       datain,
       dataout,
       proc_disc,
       discunitar,
       proc_tva,
       id_fact,
       sters,
       discount_tva,
       adaos_tva,
       proc_tvav,
       datapif,
       dns_luni,
       dns_ramas,
       valoare,
       valoarev,
       valtva,
       valtvav,
       valoare_adaos,
       valtva_adaos,
       valoare_discount,
       valtva_discount,
       id_gestiunec,
       id_responsabilc,
       id_tip_rulaj,
       id_set,
       pret_imputare,
       nract,
       id_utils,
       dataoras,
       id_rul_aux,
       serie,
       id_valuta,
       serie_act,
       id_sucursala)
      select id_rul_obinv,
             an,
             luna,
             cod,
             nnir,
             id_articol,
             id_gestiune,
             pret,
             pretv,
             tva,
             tvav,
             cant,
             cante,
             cont,
             adaos,
             id_lucrare,
             id_responsabil,
             dataact,
             procent,
             curs,
             pretd,
             id_util,
             dataora,
             id_sectie,
             alcooltutun,
             pretvtva,
             acont,
             datain,
             dataout,
             proc_disc,
             discunitar,
             proc_tva,
             id_fact,
             sters,
             discount_tva,
             adaos_tva,
             proc_tvav,
             datapif,
             dns_luni,
             dns_ramas,
             valoare,
             valoarev,
             valtva,
             valtvav,
             valoare_adaos,
             valtva_adaos,
             valoare_discount,
             valtva_discount,
             id_gestiunec,
             id_responsabilc,
             id_tip_rulaj,
             id_set,
             pret_imputare,
             nract,
             id_utils,
             dataoras,
             id_rul_aux,
             serie,
             id_valuta,
             serie_act,
             id_sucursala
        from rul_obinv
       where an = lnAn
         and luna = lnLuna
         and cod = v_cod
       order by id_rul_obinv;

    pack_contafin.finalizeaza_scriere_act_rul(lnIdUtil,
                                              v_cod,
                                              2,
                                              0,
                                              0,
                                              lcMesajRefacere);

    begin
      select max(id_set), max(id_fact), max(id_factd)
        into lnIdSet, lnIdFact, lnIdFactD
        from act_temp;

      pack_contafin.finalizeaza_stergere_nota(lnLuna,
                                              lnAn,
                                              sysdate,
                                              lnIdSet,
                                              v_cod,
                                              lnIdFact,
                                              lnIdFactD,
                                              lnIdUtil);
    exception
      WHEN NO_DATA_FOUND THEN
        NULL;
    END;

    <<sfarsit_procedura>>
    null;
  end sterge_document;

  --------------------------------------------------------------------------
  -- initializeaza_act_rul + finalizeaza_document + cursor_verificare)
  --------------------------------------------------------------------------
  procedure finalizeaza_document_verif(tnAn                 IN NUMBER,
                                       tnLuna               IN NUMBER,
                                       tnScrieSterge        IN number,
                                       tnDateCompletate     IN number,
                                       tnScadereStoc        IN number,
                                       tnCod                IN ACT.COD%type,
                                       tnIdUtil             IN ACT.ID_UTIL%TYPE,
                                       tdDataOra            IN ACT.DATAORA%TYPE,
                                       tnIdSucursala        IN ACT.ID_SUCURSALA%TYPE,
                                       tcSirModificariAcont IN VARCHAR2,
                                       tcSirModificariPart  IN VARCHAR2,
                                       CURSOR_VERIFICARE    OUT PACK_TYPES.tip_cursor) is
    lcMesajRefacere VARCHAR2(3000);
  begin
    -- tnDateCompletate = 0 / 1 - cod / 2 - cod + an + luna
    -- tnScrieSterge = 0 scriere/ 1- refacere / 2-stergere

    -- completare analitice si parteneri din configurari
    pack_contafin.completeaza_analitice;
    pack_contafin.completeaza_analitice_rul;
    pack_contafin.completeaza_parteneri_gestiuni;
    pack_contafin.sterge_analitice_gresite;
    pack_contafin.sterge_analitice_gresite_rul;

    -- completare analitice si parteneri din verificare note contabile
    if tcSirModificariAcont is not null or tcSirModificariPart is not null then
      pack_contafin.finalizeaza_document_compl(tcSirModificariAcont,
                                               tcSirModificariPart);
    end if;

    -- verificare note contabile la scriere
    if tnScrieSterge = 0 and pack_contafin.verifica_note_contabile(1) > 0 then
      pack_contafin.cursor_verificare(CURSOR_VERIFICARE);
    else
      pack_contafin.initializeaza_scriere_act_rul(tnIdUtil,
                                                  tdDataOra,
                                                  tnAn,
                                                  tnLuna,
                                                  1,
                                                  1,
                                                  tnScrieSterge,
                                                  tnIdSucursala);

      pack_contafin.finalizeaza_scriere_act_rul(tnIdUtil,
                                                tnCod,
                                                tnScrieSterge,
                                                0,
                                                0,
                                                lcMesajRefacere);

      OPEN CURSOR_VERIFICARE FOR
        SELECT * FROM ACT_TEMP WHERE 1 = 2;
    end if;

  end finalizeaza_document_verif;

  -----------------------------------------------------------------------
  -- completeaza act_temp cu analitice, parteneri din formularul de verificare
  -- se apeleaza din finalizeaza_document_verif
  -----------------------------------------------------------------------
  procedure finalizeaza_document_compl(V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                       V_SIR_MODIFICARI_PART  IN VARCHAR2) is
    V_SEPARATOR_LINIE VARCHAR2(5) := ';';
    V_SEPARATOR       VARCHAR2(5) := '|';
  BEGIN
    -- de mutat in pack_contafin
    IF V_SIR_MODIFICARI_ACONT IS NOT NULL THEN
      MERGE INTO ACT_TEMP A
      USING (select to_number(substr(lista || V_SEPARATOR,
                                     1,
                                     instr(lista, V_SEPARATOR) - 1)) as id_act,
                    substr(lista || V_SEPARATOR || V_SEPARATOR,
                           instr(lista || V_SEPARATOR || V_SEPARATOR,
                                 V_SEPARATOR) + 1,
                           instr(lista || V_SEPARATOR || V_SEPARATOR,
                                 V_SEPARATOR,
                                 1,
                                 2) -
                           instr(lista || V_SEPARATOR || V_SEPARATOR,
                                 V_SEPARATOR) - 1) as ascd,
                    rtrim(substr(lista || V_SEPARATOR || V_SEPARATOR,
                                 instr(lista || V_SEPARATOR || V_SEPARATOR,
                                       V_SEPARATOR,
                                       1,
                                       2) + 1),
                          V_SEPARATOR) as ascc
               from (SELECT X as LISTA
                       FROM table(charc2collection(V_SIR_MODIFICARI_ACONT,
                                                   V_SEPARATOR_LINIE))

                     )) B
      ON (A.ID_ACT = B.ID_ACT)
      WHEN MATCHED THEN
        UPDATE SET ASCD = B.ASCD, ASCC = B.ASCC;

    END IF;

    IF V_SIR_MODIFICARI_PART IS NOT NULL THEN
      MERGE INTO ACT_TEMP A
      USING (select to_number(substr(lista || V_SEPARATOR,
                                     1,
                                     instr(lista, V_SEPARATOR) - 1)) as id_act,
                    nvl(to_number(substr(lista || V_SEPARATOR || V_SEPARATOR,
                                         instr(lista || V_SEPARATOR ||
                                               V_SEPARATOR,
                                               V_SEPARATOR) + 1,
                                         instr(lista || V_SEPARATOR ||
                                               V_SEPARATOR,
                                               V_SEPARATOR,
                                               1,
                                               2) -
                                         instr(lista || V_SEPARATOR ||
                                               V_SEPARATOR,
                                               V_SEPARATOR) - 1)),
                        0) as id_partd,
                    nvl(to_number(rtrim(substr(lista || V_SEPARATOR ||
                                               V_SEPARATOR,
                                               instr(lista || V_SEPARATOR ||
                                                     V_SEPARATOR,
                                                     V_SEPARATOR,
                                                     1,
                                                     2) + 1),
                                        V_SEPARATOR)),
                        0) as id_partc
               from (SELECT X as LISTA
                       FROM table(charc2collection(V_SIR_MODIFICARI_PART,
                                                   V_SEPARATOR_LINIE)))) B
      ON (A.ID_ACT = B.ID_ACT)
      WHEN MATCHED THEN
        UPDATE SET ID_PARTD = B.ID_PARTD, ID_PARTC = B.ID_PARTC;

    END IF;

  end finalizeaza_document_compl;

  PROCEDURE cursor_verificare(V_CURSOR_VERIFICARE OUT PACK_TYPES.tip_cursor) IS
  BEGIN
    OPEN V_CURSOR_VERIFICARE FOR
      SELECT A.ID_ACT,
             A.LUNA,
             A.AN,
             A.DATAIREG as datairegt,
             A.NRACT,
             A.SERIE_ACT,
             A.DATAACT as dataactt,
             A.suma,
             A.EXPLICATIA,
             A.SCD,
             A.ASCD,
             A.ASCD as ascd1,
             A.SCC,
             A.ASCC,
             A.ASCC as ascc1,
             A.PERECHED,
             A.PERECHEC,
             A.SUMA_VAL,
             A.EXPLICATIA4,
             A.EXPLICATIA5,
             A.CURS,
             A.DATASCAD as datascadt,
             A.NEIMPOZAB,
             A.NNIR,
             A.ID_UTIL,
             UTIL2.UTILIZATOR AS UTIL,
             A.DATAORA,
             A.ID_UTILS,
             UTIL1.UTILIZATOR AS UTILS,
             A.DATAORAS,
             A.ID_RESPONSABIL,
             RESP.DENUMIRE AS NRESP,
             A.ID_VENCHELT,
             VENCHELT.EXPLICATIE AS DST_CHLT,
             A.ID_LUCRARE,
             LUCR.NRORD,
             A.ID_CTR,
             CTR.CONTRACT,
             A.ID_SECTIE,
             SECTII.SECTIE,
             A.PROC_TVA,
             A.ID_SET,
             A.ID_FACT,
             A.ID_FACTD,
             A.ID_FACTC,
             A.ID_PARTD,
             A.ID_PARTD AS ID_PARTD1,
             PART1.NUME AS PARTD,
             A.ID_PARTC,
             A.ID_PARTC AS ID_PARTC1,
             PART.NUME AS PARTC,
             A.ID_SUCURSALA,
             S.FIRMA AS SUCURSALA,
             A.ID_FDOC,
             FDOC.FEL_DOCUMENT AS FDOC,
             A.ID_GESTIN,
             A.ID_JTVA_COLOANA,
             G1.NUME_GESTIUNE AS GESTIN,
             A.ID_GESTOUT,
             G2.NUME_GESTIUNE AS GESTOUT,
             A.ID_VALUTA,
             V.NUME_VAL,
             0000000000 as id_pol,
             lpad(' ', 30, ' ') as nume_lista_preturi,
             A.TVA_INCASARE
        FROM ACT_TEMP A
        LEFT JOIN NOM_FDOC FDOC
          ON A.ID_FDOC = FDOC.ID_FDOC
        LEFT JOIN SYN_NOM_FIRME S
          ON A.ID_SUCURSALA = S.ID_FIRMA
        LEFT JOIN NOM_PARTENERI PART
          ON A.ID_PARTC = PART.ID_PART
        LEFT JOIN NOM_PARTENERI PART1
          ON A.ID_PARTD = PART1.ID_PART
        LEFT JOIN VNOM_VENCHEL VENCHELT
          ON A.ID_VENCHELT = VENCHELT.ID_VENCHELT
        LEFT JOIN NOM_PARTENERI RESP
          ON A.ID_RESPONSABIL = RESP.ID_PART
        LEFT JOIN SYN_UTILIZATORI UTIL1
          ON A.ID_UTILS = UTIL1.ID_UTIL
        LEFT JOIN SYN_UTILIZATORI UTIL2
          ON A.ID_UTIL = UTIL2.ID_UTIL
        LEFT JOIN VNOM_LUCRARI LUCR
          ON A.ID_LUCRARE = LUCR.ID_LUCRARE
        LEFT JOIN VCONTRACTE CTR
          ON A.ID_CTR = CTR.ID_CTR
        LEFT JOIN NOM_GESTIUNI G1
          ON A.ID_GESTIN = G1.ID_GESTIUNE
        LEFT JOIN NOM_GESTIUNI G2
          ON A.ID_GESTOUT = G2.ID_GESTIUNE
        LEFT JOIN NOM_VALUTE V
          ON A.ID_VALUTA = V.ID_VALUTA
        LEFT JOIN NOM_SECTII SECTII
          ON A.ID_SECTIE = SECTII.ID_SECTIE
       WHERE A.STERS = 0;
  END cursor_verificare;
  ------------------------------------------------------------
  Procedure final_scriere_act_rul_local(tnIdUtil            SYN_UTILIZATORI.ID_UTIL%TYPE,
                                        tnAn                ACT.AN%TYPE,
                                        tnLuna              ACT.LUNA%TYPE,
                                        tnCod               ACT.COD%TYPE,
                                        tnScrieSterge       NUMBER,
                                        tnModificareNota    NUMBER,
                                        tcMesajRefacereNota OUT VARCHAR2) is
    -- se apeleaza dupa pack_contafin.init_scriere_act_rul_local si dupa completarea act_temp din Fox
  begin
    pack_contafin.finalizeaza_scriere_act_rul(tnIdUtil,
                                              tnCod,
                                              tnScrieSterge,
                                              tnModificareNota,
                                              0, -- tnScrieCumpVanz
                                              tcMesajRefacereNota);
  end final_scriere_act_rul_local;
  ------------------------------------------------------------
  procedure finalizeaza_scriere_act_rul is
    lcMesajRefacereNota VARCHAR2(3000);
  begin
    pack_contafin.finalizeaza_scriere_act_rul(pack_contafin.GET_IDUTIL(),
                                              null,
                                              0,
                                              0,
                                              2,
                                              lcMesajRefacereNota);
  end finalizeaza_scriere_act_rul;
  ------------------------------------------------------------
  procedure finalizeaza_scriere_act_rul(tnIdUtil            SYN_UTILIZATORI.ID_UTIL%TYPE,
                                        tnCod               ACT.COD%TYPE,
                                        tnScrieSterge       NUMBER, -- 0 = scriere, 1 = refacere, 2 = stergere
                                        tnModificareNota    NUMBER, -- 0 = modificare, 1 = stergere
                                        tnScrieCumpVanz     NUMBER, -- 0 = cump + vanz, 1 = cumparari, 2 = vanzari
                                        tcMesajRefacereNota OUT VARCHAR2) is
    -- se apeleaza dupa pack_contafin.init_scriere_act_rul_local si dupa completarea act_temp din Fox
    lcCorespDTva      varchar2(100);
    lcCorespD         varchar2(100);
    lcCorespCTva      varchar2(100);
    lcCorespC         varchar2(100);
    lcExceptii        varchar2(2000);
    lnNrInregRul      number(10);
    lnNrInregRulObinv number(10);
    lnAn              ACT.AN%type;
    lnLuna            ACT.LUNA%type;

    lcSetNuScrie      optiuni.varvalue%type;
    lnIdSet           act.id_set%type;
    lcSituatiiNuScrie optiuni.varvalue%type;
    lnScrieBV         number(1) := 1;
    lnScrieBP         number(1) := 1;
    lnScrieIP         number(1) := 1;
    lnScrieTVA        number(1) := 1;
    lnScrieSTOC       number(1) := 1;
  begin
    lnAn   := pack_sesiune.getan();
    lnLuna := pack_sesiune.getluna();

    -- LISTA DE SETURI PENTRU CARE NU SE APELEAZA SCRIEREA balanta, balanta parteneri, inregistrari parteneri, tva, stoc
    -- Bonurile fiscale de la chioscuri
    -- Se refac situatiile doar prin refaceri/redeschidere de luna
    lcSetNuScrie := PACK_SESIUNE.getOptiuneFirma('SETURI_SITUATII_NU_SCRIE'); -- '25052|BV,BP,IP,TVA;'
    select max(id_set) into lnIdSet from act_temp;
    IF lcSetNuScrie IS NOT NULL and lnIdSet is not null THEN
      SELECT MAX(UPPER(SITUATII))
        into lcSituatiiNuScrie
        FROM (SELECT GETWORDNUM(SET_SITUATII, 1, '|') AS ID_SET,
                     GETWORDNUM(SET_SITUATII, 2, '|') AS SITUATII
                FROM (SELECT getwordnum(lcSetNuScrie, B.RAND, ';') AS SET_SITUATII
                        FROM DUAL A,
                             (SELECT ROWNUM AS RAND
                                FROM USER_OBJECTS
                               WHERE ROWNUM < 100) B)
               WHERE SET_SITUATII IS NOT NULL)
       WHERE ID_SET = to_char(lnIdSet);
      if lcSituatiiNuScrie LIKE '%BV%' then
        lnScrieBV := 0;
      end if;
      if lcSituatiiNuScrie LIKE '%BP%' then
        lnScrieBP := 0;
      end if;
      if lcSituatiiNuScrie LIKE '%IP%' then
        lnScrieIP := 0;
      end if;
      if lcSituatiiNuScrie LIKE '%TVA%' then
        lnScrieTVA := 0;
      end if;
      if lcSituatiiNuScrie LIKE '%STOC%' then
        lnScrieSTOC := 0;
      end if;
    END IF;

    if tnScrieSterge <> 2 then
      pack_contafin.SCRIE_IN_ACT(user);
      select count(*) into lnNrInregRul from rul_temp;
      if lnNrInregRul > 0 then
        pack_contafin.SCRIE_IN_RUL(user);
      end if;

      select count(*) into lnNrInregRulObinv from rul_temp_obinv;
      if lnNrInregRulObinv > 0 then
        pack_contafin.SCRIE_IN_RUL_OBINV(user);
      end if;
    else
      pack_contafin.STERGE_DIN_ACT(user,
                                   lnAn,
                                   lnLuna,
                                   tnCod,
                                   tnIdUtil,
                                   tnModificareNota);
      select count(*) into lnNrInregRul from rul where cod = tnCod;
      if lnNrInregRul > 0 then
        pack_contafin.STERGE_DIN_RUL(user, lnAn, lnLuna, tnCod, tnIdUtil);
      end if;

      select count(*)
        into lnNrInregRulObinv
        from rul_obinv
       where cod = tnCod;
      if lnNrInregRulObinv > 0 then
        pack_contafin.STERGE_DIN_RUL_OBINV(user,
                                           lnAn,
                                           lnLuna,
                                           tnCod,
                                           tnIdUtil);
      end if;
    end if;

    if lnScrieBV = 1 then
      pack_contafin.scrie_in_balanta(user, lnAn, lnLuna, tnScrieSterge);
    end if;
    if lnScrieBP = 1 then
      pack_contafin.scrie_in_balanta_parteneri(user,
                                               lnAn,
                                               lnLuna,
                                               NULL,
                                               tnScrieSterge);
    end if;
    if lnScrieIP = 1 then
      pack_contafin.executa_scrie_in_ireg(user,
                                          lnAn,
                                          lnLuna,
                                          Null,
                                          tnScrieSterge,
                                          0);
    end if;

    if tnScrieCumpVanz <> 2 and lnScrieTVA = 1 then
      If lnAn >= 2007 then
        pack_contafin.SCRIE_JC_2007(lnAn, lnLuna, tnScrieSterge);
        -- tnScrieSterge ar trebui sa aiba valoarea 0 sau 2
      Else
        lcCorespDTva := '401,404,5121,5124,542,4427,4428';
        lcCorespD    := '401,404,5121,5124,542,4427';
        lcCorespCTva := '0';
        lcCorespC    := '0';
        select stringagg(cont_c)
          INTO lcExceptii
          from (select distinct cont_c
                  from exceptii_ireg
                 where cont in
                       (SELECT X as CONT
                          FROM table(charc2collection(lcCorespD, ',')))
                   and invers = 1
                union all
                select '-1'
                  from dual);
        pack_contafin.EXECUTA_SCRIE_TVA(user,
                                        'cump',
                                        pack_contafin.getCotaTVAStandard(lnLuna,
                                                                         lnAn),
                                        CASE WHEN
                                        lnAn * 12 + lnLuna >= 2025 * 12 + 8 THEN 1.11 ELSE 1.09 END,
                                        lcCorespDtva,
                                        lcCorespD,
                                        lcCorespCtva,
                                        lcCorespC,
                                        '4426',
                                        lcExceptii,
                                        lnAn,
                                        lnLuna,
                                        tnScrieSterge);
      End if;
    end if;

    if tnScrieCumpVanz <> 1 and lnScrieTVA = 1 then
      If lnAn >= 2007 then
        pack_contafin.SCRIE_JV_2007(lnAn, lnLuna, tnScrieSterge);
        -- tnScrieSterge ar trebui sa aiba valoarea 0 sau 2
      Else
        lcCorespDTva := '411,4111,4112,4113,4114,4115,4116,4117,4428,4118';
        lcCorespD    := '411,4111,4112,4113,4114,4115,4116,4117,4428,4118';
        lcCorespCTva := '411,4111,4112,4113,4114,4115,4116,4117,461,428,4281,4282,4426,4428';
        lcCorespC    := '411,4111,4112,4113,4114,4115,4116,4117,461,428,4281,4282,4426,5121,5311';
        select stringagg(cont_c)
          INTO lcExceptii
          from (select distinct cont_c
                  from exceptii_ireg
                 where cont in
                       (SELECT X as CONT
                          FROM table(charc2collection(lcCorespC, ',')))
                   and invers = 1
                union all
                select '419' as cont_c
                  from dual);
        pack_contafin.EXECUTA_SCRIE_TVA(user,
                                        'vanz',
                                        pack_contafin.getCotaTVAStandard(lnLuna,
                                                                         lnAn),
                                        CASE WHEN
                                        lnAn * 12 + lnLuna >= 2025 * 12 + 8 THEN 1.11 ELSE 1.09 END,
                                        lcCorespDtva,
                                        lcCorespD,
                                        lcCorespCtva,
                                        lcCorespC,
                                        '4427',
                                        lcExceptii,
                                        lnAn,
                                        lnLuna,
                                        tnScrieSterge);
      End if;
    End if;

    if lnNrInregRul > 0 and lnScrieSTOC = 1 then
      pack_contafin.scrie_in_stoc(user,
                                  lnAn,
                                  lnLuna,
                                  0,
                                  'STOC',
                                  'ID_STOC',
                                  '2=2');
    end if;
    if lnNrInregRulObinv > 0 and lnScrieSTOC = 1 then
      pack_contafin.scrie_in_stoc_obinv(user,
                                        lnAn,
                                        lnLuna,
                                        0,
                                        'STOC_OBINV',
                                        'ID_STOC',
                                        '2=2');
    end if;
    pack_refacere_nota.refacere_nota(lnAn, lnLuna, tcMesajRefacereNota);
  end finalizeaza_scriere_act_rul;
  -----------------------------------------------
  PROCEDURE finalizeaza_modificare_nota(tnLuna    IN NUMBER,
                                        tnAn      IN NUMBER,
                                        tdDataOra IN DATE,
                                        tnIdSet   IN NUMBER,
                                        tnCod     IN NUMBER,
                                        tnIdFact  IN NUMBER,
                                        tnIdFactD IN NUMBER,
                                        tnIdUtil  IN NUMBER) IS
    lnEInVanzari NUMBER(10);
    lnCodNou     NUMBER(10);
  BEGIN
    lnCodNou := pack_contafin.get_cod();
    SELECT COUNT(*) INTO lnEInVanzari FROM vanzari WHERE cod = tnCod;

    IF lnEInVanzari > 0 THEN
      pack_facturare.actualizeaza_vanzari(tnCod, lnCodNou);
    END IF;

    UPDATE atasamente_vanzari SET cod = lnCodNou WHERE cod = tnCod;

    CASE
      WHEN tnIdSet = 90101 THEN
        UPDATE gest_inventar
           SET dataora_validat = pack_contafin.GET_DATAORA(),
               validat         = decode(validat, 0, 1, validat) -- pentru cazul in care se repuneau inregistrarile sterse
         WHERE dataora_validat = tdDataOra
           AND an = tnAn
           AND luna = tnLuna;
      WHEN tnIdSet IN (31003, 31004, 31005, 31011) THEN
        -- daca se modifica o inregistrare stearsa, atunci trebuie sa repun si id_fact in nom_lucrari
        UPDATE NOM_LUCRARI
           SET ID_FACT = tnIdFact
         WHERE ID_LUCRARE IN (SELECT DISTINCT ID_LUCRARE
                                FROM ACT
                               WHERE COD = lnCodNou
                                 AND AN = tnAn
                                 AND LUNA = tnLuna)
           AND ID_FACT IS NULL;
        /*      WHEN tnIdSet = 90011 THEN
          -- 426-5311 achitare drepturi neridicate lichidare
          UPDATE sal_stat
             SET achitlicnerid = 0
           WHERE id_fact_lic = tnIdFactD;
        WHEN tnIdSet = 90013 THEN
          -- 426-5311 achitare drepturi neridicate avans
          UPDATE sal_stat SET achitavnerid = 0 WHERE id_fact_av = tnIdFactD;*/
      ELSE
        lnEInVanzari := 0;
    END CASE;

  END finalizeaza_modificare_nota;
  -----------------------------------------------
  PROCEDURE finalizeaza_stergere_nota(tnLuna    IN NUMBER,
                                      tnAn      IN NUMBER,
                                      tdDataOra IN DATE,
                                      tnIdSet   IN NUMBER,
                                      tnCod     IN NUMBER,
                                      tnIdFact  IN NUMBER,
                                      tnIdFactD IN NUMBER,
                                      tnIdUtil  IN NUMBER) IS
    lnEInVanzari NUMBER(10);
  BEGIN
    SELECT COUNT(*) INTO lnEInVanzari FROM vanzari WHERE cod = tnCod;

    IF lnEInVanzari > 0 THEN
      pack_facturare.sterge_din_vanzari(tnCod, tnAn, tnLuna, tnIdUtil);
    END IF;

    UPDATE atasamente_vanzari
       SET sters = 1, id_utils = tnIdUtil, dataoras = sysdate
     WHERE cod = tnCod;

    CASE
      WHEN tnIdSet = 90101 THEN
        UPDATE gest_inventar
           SET validat = 0
         WHERE dataora_validat = tdDataOra
           AND an = tnAn
           AND luna = tnLuna;
      WHEN tnIdSet = 90010 THEN
        -- 421-426 drepturi neridicate lichidare
        UPDATE sal_stat
           SET bifalicnerid = 0, sumalicnerid = 0, id_fact_lic = 0
         WHERE id_fact_lic = tnIdFact;
      WHEN tnIdSet = 90011 THEN
        -- 426-5311 achitare drepturi neridicate lichidare
        UPDATE sal_stat
           SET achitlicnerid = 0
         WHERE id_fact_lic = tnIdFactD;
      WHEN tnIdSet = 90012 THEN
        -- 425-426 drepturi neridicate avans
        UPDATE sal_stat
           SET bifaavnerid = 0, sumaavnerid = 0, id_fact_av = 0
         WHERE id_fact_av = tnIdFact;
      WHEN tnIdSet = 90013 THEN
        -- 426-5311 achitare drepturi neridicate avans
        UPDATE sal_stat SET achitavnerid = 0 WHERE id_fact_av = tnIdFactD;
      WHEN tnIdSet IN (31003, 31004, 31005, 31011) THEN
        UPDATE nom_lucrari SET id_fact = Null WHERE id_fact = tnIdFact;
      WHEN tnIdSet IN (32000, 32003, 32011, 32013) THEN
        -- ROADEVIZE : raport de productie, inchidere comenzi regie/garantie, factura finala 1, factura finala 2
        UPDATE dev_oper
           SET sters = 1, id_utils = tnIdUtil, dataoras = sysdate
         WHERE id_fact = tnIdFact;
      ELSE
        lnEInVanzari := 0;
    END CASE;

  END finalizeaza_stergere_nota;
  -----------------------------------------------
  PROCEDURE completeaza_analitice IS
    cursor crs is
      select a.cont,
             a.tabel_prefix,
             a.col_prefix,
             a.tabel_sufix,
             a.col_sufix,
             a.camp_leg_prefix,
             a.camp_leg_sufix,
             a.poz_start_prefix,
             a.nr_return_prefix,
             a.poz_start_sufix,
             a.nr_return_sufix,
             b.tip
        from config_analitice a
        join (select distinct scd as cont, 1 as tip
                from act_temp
               where ascd is null
              union
              select distinct scc as cont, 2 as tip
                from act_temp
               where ascc is null) b
          on a.cont = b.cont;
    crs_linie      crs%rowtype;
    lcMerge        VARCHAR2(10000);
    lcAnalitic     VARCHAR2(1000);
    lcColoanaP     VARCHAR2(100);
    lcColoanaS     VARCHAR2(100);
    lcTipCont      VARCHAR(4);
    lcTipAcont     VARCHAR(4);
    lcNumeColoanaP VARCHAR2(1000);
    lcNumeColoanaS VARCHAR2(1000);
  BEGIN
    open crs;
    loop
      fetch crs
        into crs_linie;
      exit when crs%NOTFOUND;
      if crs_linie.tip = 1 then
        lcTipCont  := 'SCD';
        lcTipAcont := 'ASCD';
      else
        lcTipCont  := 'SCC';
        lcTipAcont := 'ASCC';
      end if;
      lcAnalitic := '';
      lcColoanaP := '';
      lcColoanaS := '';
      if crs_linie.tabel_prefix is not null then
        lcColoanaP     := 'A.' || crs_linie.camp_leg_prefix;
        lcNumeColoanaP := crs_linie.camp_leg_prefix;
        if UPPER(TRIM(crs_linie.camp_leg_prefix)) = 'ID_GESTIUNE' then
          if crs_linie.tip = 1 then
            lcNumeColoanaP := 'ID_GESTIN';
          else
            lcNumeColoanaP := 'ID_GESTOUT';
          end if;
          lcColoanaP := lcColoanaP || ' AS ' || lcNumeColoanaP;
        end if;
        lcAnalitic := 'substr(a.' || crs_linie.col_prefix || ',' ||
                      crs_linie.poz_start_prefix || ',' ||
                      crs_linie.nr_return_prefix || ')';
      end if;
      if crs_linie.tabel_sufix is not null then
        lcColoanaS     := 'B.' || crs_linie.camp_leg_sufix;
        lcNumeColoanaS := crs_linie.camp_leg_sufix;
        if UPPER(TRIM(crs_linie.camp_leg_sufix)) = 'ID_GESTIUNE' then
          if crs_linie.tip = 1 then
            lcNumeColoanaS := 'ID_GESTIN';
          else
            lcNumeColoanaS := 'ID_GESTOUT';
          end if;
          lcColoanaS := lcColoanaS || ' AS ' || lcNumeColoanaS;
        end if;
        lcAnalitic := lcAnalitic || '||substr(b.' || crs_linie.col_sufix || ',' ||
                      crs_linie.poz_start_sufix || ',' ||
                      crs_linie.nr_return_sufix || ')';
      end if;
      lcMerge := 'MERGE INTO ACT_TEMP A1 USING (SELECT ''' || crs_linie.cont ||
                 ''' AS ' || lcTipCont || ',' || lcAnalitic || ' AS ' ||
                 lcTipAcont || ',' || lcColoanaP || (CASE
                   WHEN lcColoanaS IS NULL THEN
                    ''
                   ELSE
                    ',' || lcColoanaS
                 END) || ' FROM ' || crs_linie.tabel_prefix || ' A ' || (CASE
                   WHEN lcColoanaS IS NULL THEN
                    ''
                   ELSE
                    'LEFT JOIN ' || crs_linie.tabel_sufix ||
                    ' B ON 1=1 AND A.STERS = 0 AND B.STERS = 0 '
                 END) || 'WHERE A.STERS = 0) A2 ' || 'ON (A1.' || lcTipCont ||
                 '=A2.' || lcTipCont || ' AND A1.' || (CASE
                   WHEN UPPER(lcNumeColoanaP) = 'CONT' THEN
                    lcTipCont
                   ELSE
                    lcNumeColoanaP
                 END) || '=A2.' || lcNumeColoanaP || ' ' || (CASE
                   WHEN lcColoanaS IS NULL THEN
                    ''
                   ELSE
                    'AND A1.' || (CASE
                      WHEN UPPER(lcNumeColoanaS) = 'CONT' THEN
                       lcTipCont
                      ELSE
                       lcNumeColoanaS
                    END) || '=A2.' || lcNumeColoanaS
                 END) || ') ' || 'WHEN MATCHED THEN UPDATE SET A1.' ||
                 lcTipAcont || '=NVL(A1.' || lcTipAcont || ',A2.' || lcTipAcont || ')';

      /* begin*/
      -- PINFO(lcMerge,'COMPLETEAZA_ANALITICE');
      EXECUTE IMMEDIATE lcMerge;
      /* exception
        when others then
          raise_application_error(-20000, lcMerge);
      end;*/
      -- La vanzarea de marfuri la pret de vanzare 4428 = 371 are completata ID_GESTOUT
      -- functioneaza pentru contul 371 pe credit, dar nu pentru contul 4428 pe debit
      -- mai repet o data completarea analiticelor si cu ID_GESTIN/ID_GESTOUT pentru analiticele ramase necompletate
      if UPPER(TRIM(crs_linie.camp_leg_prefix)) = 'ID_GESTIUNE' then
        if lcNumeColoanaP = 'ID_GESTIN' THEN
          lcMerge := replace(lcMerge, 'ID_GESTIN', 'ID_GESTOUT');
        else
          lcMerge := replace(lcMerge, 'ID_GESTOUT', 'ID_GESTIN');
        end if;

        EXECUTE IMMEDIATE lcMerge;
      end if;
    end loop;
    close crs;
    -- verificarea daca exista analitic in planul de conturi se face in sterge_analitice_gresite
  END completeaza_analitice;
  -----------------------------------------------------------------------
  PROCEDURE completeaza_analitice_rul IS
    cursor crs is
      select a.cont,
             a.tabel_prefix,
             a.col_prefix,
             a.tabel_sufix,
             a.col_sufix,
             a.camp_leg_prefix,
             a.camp_leg_sufix,
             a.poz_start_prefix,
             a.nr_return_prefix,
             a.poz_start_sufix,
             a.nr_return_sufix
        from config_analitice a
       where a.cont in
             (select distinct cont from rul_temp where acont is null);
    crs_linie      crs%rowtype;
    lcMerge        VARCHAR2(10000);
    lcAnalitic     VARCHAR2(1000);
    lcColoanaP     VARCHAR2(100);
    lcColoanaS     VARCHAR2(100);
    lcNumeColoanaP VARCHAR2(1000);
    lcNumeColoanaS VARCHAR2(1000);
  BEGIN
    open crs;
    loop
      fetch crs
        into crs_linie;
      exit when crs%NOTFOUND;
      lcAnalitic := '';
      lcColoanaP := '';
      lcColoanaS := '';
      if crs_linie.tabel_prefix is not null then
        lcColoanaP     := 'A.' || crs_linie.camp_leg_prefix;
        lcNumeColoanaP := crs_linie.camp_leg_prefix;
        lcAnalitic     := 'substr(a.' || crs_linie.col_prefix || ',' ||
                          crs_linie.poz_start_prefix || ',' ||
                          crs_linie.nr_return_prefix || ')';
      end if;
      if crs_linie.tabel_sufix is not null then
        lcColoanaS     := 'B.' || crs_linie.camp_leg_sufix;
        lcNumeColoanaS := crs_linie.camp_leg_prefix;
        lcAnalitic     := lcAnalitic || '||substr(b.' ||
                          crs_linie.col_sufix || ',' ||
                          crs_linie.poz_start_sufix || ',' ||
                          crs_linie.nr_return_sufix || ')';
      end if;

      lcMerge := 'MERGE INTO RUL_TEMP A1 USING (SELECT ''' || crs_linie.cont ||
                 ''' AS CONT,' || lcAnalitic || ' AS ACONT,' || lcColoanaP ||
                 (CASE
                   WHEN lcColoanaS IS NULL THEN
                    ''
                   ELSE
                    ',' || lcColoanaS
                 END) || ' FROM ' || crs_linie.tabel_prefix || ' A ' || (CASE
                   WHEN lcColoanaS IS NULL THEN
                    ''
                   ELSE
                    'LEFT JOIN ' || crs_linie.tabel_sufix ||
                    ' B ON 1=1 AND A.STERS = 0 AND B.STERS = 0 '
                 END) || 'WHERE A.STERS = 0) A2 ' ||
                 'ON (A1.CONT=A2.CONT AND A1.' || lcNumeColoanaP || '=A2.' ||
                 lcNumeColoanaP || ' ' || (CASE
                   WHEN lcColoanaS IS NULL THEN
                    ''
                   ELSE
                    'AND A1.' || lcNumeColoanaS || '=A2.' || lcNumeColoanaS
                 END) || ') ' ||
                 'WHEN MATCHED THEN UPDATE SET A1.ACONT=NVL(A1.ACONT,A2.ACONT)';

      /* begin*/
      EXECUTE IMMEDIATE lcMerge;
      /* exception
        when others then
          raise_application_error(-20000, lcMerge);
      end;*/

    end loop;
    close crs;
    -- verificarea daca exista analitic in planul de conturi se face in sterge_analitice_gresite_rul
  END completeaza_analitice_rul;
  -----------------------------------------------------------------------
  PROCEDURE completeaza_parteneri_gestiuni IS
  BEGIN
    MERGE INTO ACT_TEMP A
    USING (SELECT ID_PART, ID_GESTIUNE, CONT
             FROM ASOCIERE_PARTENERI_GESTIUNI
            WHERE ID_GESTIUNE IN (SELECT DISTINCT ID_GESTIN FROM ACT_TEMP)
              AND CONT IN (SELECT CONT FROM config_cont_ireg)) B
    ON (B.ID_GESTIUNE = A.ID_GESTIN AND B.CONT = A.SCD)
    WHEN MATCHED THEN
      UPDATE
         SET ID_PARTD = DECODE(NVL(A.ID_PARTD, 0), 0, B.ID_PART, A.ID_PARTD);

    MERGE INTO ACT_TEMP A
    USING (SELECT ID_PART, ID_GESTIUNE, CONT
             FROM ASOCIERE_PARTENERI_GESTIUNI
            WHERE ID_GESTIUNE IN (SELECT DISTINCT ID_GESTOUT FROM ACT_TEMP)
                 --          = pack_facturare.nid_gestiune_sursa
              AND CONT IN (SELECT CONT FROM config_cont_ireg)) B
    ON (B.ID_GESTIUNE = A.ID_GESTOUT AND B.CONT = A.SCC)
    WHEN MATCHED THEN
      UPDATE
         SET ID_PARTC = DECODE(NVL(A.ID_PARTC, 0), 0, B.ID_PART, A.ID_PARTC);
  END completeaza_parteneri_gestiuni;
  -----------------------------------------------------------------------
  PROCEDURE sterge_analitice_gresite IS
  BEGIN
    MERGE INTO ACT_TEMP A
    USING (SELECT ID_ACT, SUM(TIP) AS TIP
             FROM (SELECT ID_ACT, 1 AS TIP
                     FROM ACT_TEMP
                    WHERE ASCD IS NOT NULL
                      AND TRIM(SCD) || '.' || TRIM(ASCD) NOT IN
                          (SELECT TRIM(CONT) || '.' || TRIM(ACONT)
                             FROM VPLCONT_ANALITIC
                            WHERE AN = pack_sesiune.getan())
                   UNION ALL
                   SELECT ID_ACT, 2 AS TIP
                     FROM ACT_TEMP
                    WHERE ASCC IS NOT NULL
                      AND TRIM(SCC) || '.' || TRIM(ASCC) NOT IN
                          (SELECT TRIM(CONT) || '.' || TRIM(ACONT)
                             FROM VPLCONT_ANALITIC
                            WHERE AN = pack_sesiune.getan()))
            GROUP BY ID_ACT) B
    ON (A.ID_ACT = B.ID_ACT)
    WHEN MATCHED THEN
      UPDATE
         SET ASCD = DECODE(B.TIP, 1, NULL, 3, NULL, A.ASCD),
             ASCC = DECODE(B.TIP, 2, NULL, 3, NULL, A.ASCC);

  END sterge_analitice_gresite;
  -----------------------------------------------------------------------
  PROCEDURE sterge_analitice_gresite_rul IS
  BEGIN
    UPDATE RUL_TEMP
       SET ACONT = NULL
     WHERE ACONT IS NOT NULL
       AND TRIM(CONT) || '.' || TRIM(ACONT) NOT IN
           (SELECT TRIM(CONT) || '.' || TRIM(ACONT)
              FROM VPLCONT_ANALITIC
             WHERE AN = pack_sesiune.getan());

  END sterge_analitice_gresite_rul;
  -----------------------------------------------------------------------
  FUNCTION verifica_id_documente(lcSirIdFact IN VARCHAR2) RETURN VARCHAR2 IS
    lcSeparator VARCHAR2(1) := ',';
    lcMesaj     VARCHAR2(2000);
  BEGIN
    select stringagg(serie_act || ' ' || nract || '/' ||
                     to_char(dataact, 'DD.MM.YYYY')) as mesaj
      into lcMesaj
      from documente
     where sters = 1
       and id_doc in
           (SELECT X as id_doc
              FROM table(charn2collection(lcSirIdFact, lcSeparator)));
    Return lcMesaj;
  END;

  ---------------------------------------------------------------------
  -- caut o inregistrare cu id_furnizor in rul_auxiliar si o intorc - pentru reciclarea inregistrarilor din rul_auxiliar
  -- daca nu gasesc inregistrare, adaug una noua
  ---------------------------------------------------------------------
  function GetRulajAuxiliarFurnizor(tnIdFurnizor rul_auxiliar.id_furnizor%type)
    return rul_auxiliar.id_rul_aux%type is
    tnIdRulAux rul_auxiliar.id_rul_aux%type;
  begin
    select min(id_rul_aux)
      into tnIdRulAux
      from rul_auxiliar
     where id_furnizor = tnIdFurnizor;
    if tnIdRulAux is null then
      insert into rul_auxiliar
        (id_furnizor)
      values
        (tnIdFurnizor)
      returning id_rul_aux into tnIdRulAux;
    end if;
    return tnIdRulAux;

  end GetRulajAuxiliarFurnizor;

  procedure GetRulajAuxiliarFurnizor(tnIdFurnizor IN rul_auxiliar.id_furnizor%type,
                                     tnIdRulAux   OUT rul_auxiliar.id_rul_aux%type) is
  begin
    tnIdRulAux := GetRulajAuxiliarFurnizor(tnIdFurnizor);
  end;
BEGIN
  nCod     := 0;
  dDataOra := SYSDATE;
  nIdFact  := 0;
  nIdUtil  := 0;
end PACK_CONTAFIN;
/
