CREATE OR REPLACE PACKAGE "PACK_FACTURARE" is

  -- Author  : MARIUS.ATANASIU
  -- Created : 05/09/2005 09:25:00 AM
  -- Purpose :

  -- 30.10.2009
  -- marius.mutu
  -- nTipVanzareRetail in contabilizeaza_articol, scrie_tva
  -- nTipIncasare: scrie_incsare2
  -- descarca_gestiune - tva adaos

  cnume_program VARCHAR(30) := 'ROAFACTURARE';

  TYPE cursor_facturare IS REF CURSOR;
  TYPE tip_date_gestiune IS RECORD(
    id_gestiune NOM_GESTIUNI.ID_GESTIUNE%TYPE,
    id_tip_gest TIPURI_GESTIUNI.ID_TIPGEST%TYPE,
    cont        NOM_GESTIUNI.CONT%TYPE,
    acont       NOM_GESTIUNI.ACONT%TYPE);

  TYPE linie_stoc IS RECORD(
    TIP            NUMBER(1),
    pret           RUL.PRET%TYPE,
    pretv          RUL.PRETV%TYPE,
    acont          RUL.ACONT%TYPE,
    cants          STOC.CANTS%TYPE,
    cant           RUL.CANT%TYPE,
    cante          RUL.CANTE%TYPE,
    tvav           RUL.TVAV%TYPE,
    proc_tvav      RUL.PROC_TVAV%TYPE,
    tva            RUL.TVA%TYPE,
    datain         RUL.DATAIN%TYPE,
    id_rul_aux     RUL.ID_RUL_AUX%TYPE,
    pretd          RUL.PRETD%TYPE,
    id_valuta      RUL.ID_VALUTA%TYPE,
    LOT            RUL.LOT%TYPE,
    ADATA_EXPIRARE RUL.ADATA_EXPIRARE%TYPE,
    serie          RUL.SERIE%TYPE,
    ID_LUCRARE_REZ RUL.ID_LUCRARE_REZ%TYPE,
    ID_PART_REZ    RUL.ID_PART_REZ%TYPE,
    PRETACHCTVA    NOM_ARTICOLE.PRETACHCTVA%TYPE);
  TYPE tab_stoc_type IS TABLE OF linie_stoc;

  TYPE articol_aviz IS RECORD(
    ID_VANZARE     VANZARI_DETALII.ID_VANZARE%TYPE,
    ID_VANZARE_DET VANZARI_DETALII.ID_VANZARE_DET%TYPE,
    CANTITATE      VANZARI_DETALII.CANTITATE%TYPE,
    CUSTODIE       VANZARI_DETALII.CUSTODIE%TYPE,
    COD            ACT.COD%TYPE,
    ID_FACT        ACT.ID_FACT%TYPE,
    NRACT          ACT.NRACT%TYPE,
    SERIE_ACT      ACT.SERIE_ACT%TYPE,
    DATAACT        ACT.DATAACT%TYPE,
    ASCD           ACT.ASCD%TYPE,
    ID_SECTIE      ACT.ID_SECTIE%TYPE);
  TYPE tab_articole_aviz_type IS TABLE OF articol_aviz;

  nid_tipnir     SERII_TIPDOC.ID_TIPDOC%TYPE := 1;
  nid_tipbon     SERII_TIPDOC.ID_TIPDOC%TYPE := 2;
  nid_tipfactura SERII_TIPDOC.ID_TIPDOC%TYPE := 5;
  nid_tipaviz    SERII_TIPDOC.ID_TIPDOC%TYPE := 6;

  -- 30.10.2009
  nTipVanzareRetail VANZARI.TIP%TYPE := 43;
  -- 30.10.2009 ^
  -- 26.08.2009
  nTipFacturaHotel      VANZARI.TIP%TYPE := 44;
  nTipFacturaRestaurant VANZARI.TIP%TYPE := 45;
  -- 26.08.2009 ^
  nTipNotaPlata VANZARI.TIP%TYPE := 46;

  nTipFacturaACN VANZARI.TIP%TYPE := 51;

  /***  Valori pentru (lnIdSet,tnTip)
  **  Facturare
  **    (25000,1) - pe baza de lista de preturi ( 25010 - daca e cu scadere din gestiune )
  **    (25001,2) - pe baza de contracte    ( 25011 - daca e cu scadere din gestiune )
  **    (25002,3) - pe baza de comenzi      ( 25012 - daca e cu scadere din gestiune )
  **    (25003,4) - pe baza de avize      ( 25013 - daca e cu scadere din gestiune )
  **    (25004,5) - pe baza de lista de preturi in valuta ( 25014 - daca e cu scadere din gestiune )
  **    (25005,6) - pe baza de contract in valuta     ( 25015 - daca e cu scadere din gestiune )
  **    (25006,7) - credit note
  **    (25007,8) - retur factura in lei    ( 25017 )
  **    (25008,9) - retur factura in valuta   ( 25018 )
  **    (25009,10)  - factura fiscala in valuta ( 25019 )
  **    (25042,43)  - bon fiscal ROARETAIL ( 25052 - daca e cu scadere din gestiune )
  **    (25043,44)  - factura hotel
  **    (25044,45)  - factura restaurant      ( 25054 )
  **    (25045,46)  - nota de plata restaurant  ( 25055 )
  **    (25047,48)  - factura marfa custodie cu desc. k ( 25057 )
  **    (25048,49)  - factura marfa custodie fara desc. k ( 25058 )
  **		(25051,52)	-	pe baza de contract factura fiscala in valuta			( 25061 - daca e cu scadere din gestiune )

  **  Avize de expeditie
  **    (25020,21)  - catre clienti din comanda         ( 25030 - daca e cu scadere din gestiune )
  **    (25021,22)  - catre clienti din lista           ( 25031 - daca e cu scadere din gestiune )
  **    (25022,23)  - transfer catre subunitati din lista     ( 25032 - daca e cu scadere din gestiune )
  **    (25023,24)  - aviz de retur               ( 25033 - daca e cu scadere din gestiune )
  **    (25024,25)  - transfer catre subunitati din comanda   ( 25034 - daca e cu scadere din gestiune )
  **    (25025,26)  - catre clienti din contract          ( 25035 - daca e cu scadere din gestiune )
  **    (25026,27)  - transfer catre subunitati pe baza de lucrare( 25036 - daca e cu scadere din gestiune )
  **    (25027,28)  - catre clienti debitori din comanda      ( 25037 - daca e cu scadere din gestiune )
  **    (25028,29)  - catre clienti debitori din lista      ( 25038 - daca e cu scadere din gestiune )
  **    (25029,30)  - transfer catre subunitati pe baza de NIR  ( 25039 - daca e cu scadere din gestiune )
  **    (25040,41)  - retur transfer catre subunitati lista pret  ( 25050 - daca e cu scadere din gestiune )
  **    (25041,42)  - catre clienti custodie pe baza de comanda ( 25051 - daca e cu scadere din gestiune )
  **    (25046,47)  - catre clienti custodie pe baza de comanda ptr. descarcare cu K  ( 25056 - daca e cu scadere din gestiune )
  **    (25049,50)  - in lucru  retur de la clienti custodie        ( 25059 - daca e cu scadere din gestiune )

  **    ROAGEST
  **    (236/240/241/242,-6)  - retur de la gestiune valorica
  **
  **    ROARETAIL
  **    (25042,43)  - bonuri fiscale colectate de la magazine   ( 25052 - daca e cu scadere din gestiune )

  **    ROAACNPRO
  **    (50100,51)  - factura ROAACNPRO   ( 25060 - daca e cu scadere din gestiune )
  */

  --------------------------------------
  nTipIncasareBonFiscal  NUMBER := 2;
  nTipIncasareCardBancar NUMBER := 3;
  nTipIncasareTichete    NUMBER := 5;
  nTipIncasareChitanta   NUMBER := 11;
  ------------------------------------
  nSaftPaymentCodeNumerar varchar2(9) := '10';
  nSaftPaymentCodeCard    varchar2(9) := '48';
  ------------------------------------
  nid_act              ACT.ID_ACT%TYPE;
  nid_serie            SERII.ID_SERIE%TYPE;
  cserie_act           SERII.SERIE%TYPE;
  v_date_gestiune      tip_date_gestiune;
  ddata_curs           DATE;
  ddata_ireg           ACT.DATAIREG%TYPE;
  nid_fdoc             ACT.ID_FDOC%TYPE;
  ddata_act            ACT.DATAACT%TYPE;
  ddata_scadenta       ACT.DATASCAD%TYPE;
  nnumar_act           ACT.NRACT%TYPE;
  nid_part             ACT.ID_PARTD%TYPE;
  nid_part_rez         ACT.ID_PARTD%TYPE;
  nid_lucrare          NOM_LUCRARI.ID_LUCRARE%TYPE;
  nid_sectie_stoc      NOM_SECTII.ID_SECTIE%TYPE; --  id_sectie pentru politica de preturi cu facturare din stoc
  nid_gestiune_sursa   NOM_GESTIUNI.ID_GESTIUNE%TYPE;
  nid_responsabil      ACT.ID_RESPONSABIL%TYPE;
  cexplicatia4         ACT.EXPLICATIA4%TYPE;
  nid_ordl             DEV_ORDL.ID_ORDL%TYPE;
  nid_set              ACT.ID_SET%TYPE;
  nid_util             SYN_UTILIZATORI.ID_UTIL%TYPE;
  nproc_tva_max        ACT.PROC_TVA%TYPE;
  cdescriere           ACT.EXPLICATIA%TYPE;
  ntip                 NUMBER(2);
  nid_moneda_nationala NOM_VALUTE.ID_VALUTA%TYPE;
  nid_fact             ACT.ID_FACT%TYPE;
  nid_factc            ACT.ID_FACTC%TYPE;
  nperechec            ACT.PERECHEC%TYPE;
  nid_partc            ACT.ID_PARTC%TYPE;
  nid_jtva_coloana     ACT.ID_JTVA_COLOANA%TYPE;
  nTaxCode             ACT.TAXCODE%TYPE;
  ntva_incasare        NUMBER(1);
  nid_comanda          COMENZI.ID_COMANDA%TYPE;
  nin_valuta           VANZARI.IN_VALUTA%TYPE;
  nid_valuta           NOM_VALUTE.ID_VALUTA%TYPE;
  nid_politica_stoc    CRM_POLITICI_PRETURI.ID_POL%TYPE;
  nid_sucursala        SYN_NOM_FIRME.ID_FIRMA%TYPE;
  nid_venchelt         NOM_VENIT_CHELTUIELI.ID_VENCHELT%TYPE;
  ntotftva             ACT.SUMA%TYPE;
  ntottva              ACT.SUMA%TYPE;
  ndifftva             VANZARI.DIFTOTFTVA%TYPE;
  ndiftva              VANZARI.DIFTOTTVA%TYPE;
  nfactavizcust        NUMBER(1);
  nafisare_scadenta    VANZARI.AFISARE_SCADENTA%TYPE;
  ncoeficient_k        VANZARI.COEFICIENT_K%TYPE;
  cserie_act_incasare  SERII.SERIE%TYPE;
  nnumar_act_incasare  ACT.NRACT%TYPE;
  ntip_doc_incasare    NUMBER(2); -- 2 (BON FISCAL) sau 11 (CHITANTA) pentru SCRIE_IN_VANZARI
  nsuma_incasare       ACT.SUMA%TYPE; -- SUMA pentru SCRIE_IN_VANZARI
  --         in rate sau articole
  clistaid       VARCHAR2(3000);
  clistaid_avize VARCHAR2(3000); -- folosit pentru facturare din avize
  nid_vanzare    VANZARI.ID_VANZARE%TYPE;
  ccod_retur     VARCHAR2(3000);

  clista_cursuri VARCHAR2(1000);

  nluna                ACT.LUNA%TYPE;
  nan                  ACT.AN%TYPE;
  nzecimale_procent    NUMBER(2);
  nscadere_stoc        NUMBER(1) := 0;
  ndiscount_evidentiat NUMBER(1) := 0;
  cascc                ACT.ASCC%TYPE;
  cascd                ACT.ASCD%TYPE;

  nnumar_bon     ACT.NNIR%TYPE := 0;
  nanalitice_378 NUMBER(1) := 0;

  ntip_factura   VANZARI.TIP_FACTURA%TYPE;
  nid_beneficiar VANZARI.ID_BENEFICIAR%TYPE;
  ntip_saft      VANZARI.TIP_SAFT%TYPE; -- EFACTURA 380/381/751

  PROCEDURE verificare_setari_program(V_ID_UTIL IN NUMBER,
                                      V_MESAJ   OUT VARCHAR2);

  PROCEDURE initializeaza_facturare(V_ID_UTIL IN NUMBER);

  PROCEDURE initializeaza_seturi_temp;

  PROCEDURE initializeaza_date_factura(V_DATA_IREG      IN DATE,
                                       V_ID_FDOC        IN NUMBER,
                                       V_DATA_ACT       IN DATE,
                                       V_DATA_SCADENTA  IN DATE,
                                       V_SERIE_FACTURA  IN VARCHAR2,
                                       V_NUMAR_ACT      IN NUMBER,
                                       V_ID_CLIENT      IN NUMBER,
                                       V_ID_LUCRARE     IN NUMBER,
                                       V_ID_SECTIE      IN NUMBER,
                                       V_ID_VENCHELT    IN NUMBER,
                                       V_ID_RESPONSABIL IN NUMBER,
                                       V_LISTAID        IN VARCHAR2,
                                       V_DESCRIERE      IN VARCHAR2,
                                       V_TIP            IN NUMBER,
                                       V_ID_SET         IN NUMBER,
                                       V_DATA_CURS      IN DATE,
                                       V_ID_VALUTA      IN NUMBER,
                                       V_IN_VALUTA      IN NUMBER,
                                       V_TVA_INCASARE   IN NUMBER,
                                       V_ID_SUCURSALA   IN NUMBER,
                                       V_ID_UTIL        IN NUMBER);

  PROCEDURE initializeaza_date_factura(V_DATA_IREG      IN DATE,
                                       V_ID_FDOC        IN NUMBER,
                                       V_DATA_ACT       IN DATE,
                                       V_DATA_SCADENTA  IN DATE,
                                       V_SERIE_FACTURA  IN VARCHAR2,
                                       V_NUMAR_ACT      IN NUMBER,
                                       V_ID_CLIENT      IN NUMBER,
                                       V_ID_LUCRARE     IN NUMBER,
                                       V_ID_SECTIE      IN NUMBER,
                                       V_ID_VENCHELT    IN NUMBER,
                                       V_ID_RESPONSABIL IN NUMBER,
                                       V_EXPLICATIA4    IN VARCHAR2,
                                       V_LISTAID        IN VARCHAR2,
                                       V_DESCRIERE      IN VARCHAR2,
                                       V_TIP            IN NUMBER,
                                       V_ID_SET         IN NUMBER,
                                       V_DATA_CURS      IN DATE,
                                       V_ID_VALUTA      IN NUMBER,
                                       V_IN_VALUTA      IN NUMBER,
                                       V_TVA_INCASARE   IN NUMBER,
                                       V_ID_SUCURSALA   IN NUMBER,
                                       V_ID_UTIL        IN NUMBER);

  PROCEDURE initializeaza_date_factura(V_DATA_IREG      IN DATE,
                                       V_ID_FDOC        IN NUMBER,
                                       V_DATA_ACT       IN DATE,
                                       V_DATA_SCADENTA  IN DATE,
                                       V_SERIE_FACTURA  IN VARCHAR2,
                                       V_NUMAR_ACT      IN NUMBER,
                                       V_ID_CLIENT      IN NUMBER,
                                       V_ID_LUCRARE     IN NUMBER,
                                       V_ID_SECTIE      IN NUMBER,
                                       V_ID_VENCHELT    IN NUMBER,
                                       V_ID_RESPONSABIL IN NUMBER,
                                       V_EXPLICATIA4    IN VARCHAR2,
                                       V_ID_ORDL        IN NUMBER,
                                       V_LISTAID        IN VARCHAR2,
                                       V_DESCRIERE      IN VARCHAR2,
                                       V_TIP            IN NUMBER,
                                       V_ID_SET         IN NUMBER,
                                       V_DATA_CURS      IN DATE,
                                       V_ID_VALUTA      IN NUMBER,
                                       V_IN_VALUTA      IN NUMBER,
                                       V_TVA_INCASARE   IN NUMBER,
                                       V_ID_SUCURSALA   IN NUMBER,
                                       V_ID_UTIL        IN NUMBER);

  PROCEDURE initializeaza_date_factura(V_DATA_IREG      IN DATE,
                                       V_ID_FDOC        IN NUMBER,
                                       V_DATA_ACT       IN DATE,
                                       V_DATA_SCADENTA  IN DATE,
                                       V_SERIE_FACTURA  IN VARCHAR2,
                                       V_NUMAR_ACT      IN NUMBER,
                                       V_ID_CLIENT      IN NUMBER,
                                       V_ID_LUCRARE     IN NUMBER,
                                       V_ID_SECTIE      IN NUMBER,
                                       V_ID_VENCHELT    IN NUMBER,
                                       V_ID_RESPONSABIL IN NUMBER,
                                       V_EXPLICATIA4    IN VARCHAR2,
                                       V_ID_ORDL        IN NUMBER,
                                       V_LISTAID        IN VARCHAR2,
                                       V_DESCRIERE      IN VARCHAR2,
                                       V_TIP            IN NUMBER,
                                       V_ID_SET         IN NUMBER,
                                       V_DATA_CURS      IN DATE,
                                       V_ID_VALUTA      IN NUMBER,
                                       V_IN_VALUTA      IN NUMBER,
                                       V_TVA_INCASARE   IN NUMBER,
                                       V_ID_SUCURSALA   IN NUMBER,
                                       V_ID_UTIL        IN NUMBER,
                                       V_TIP_FACTURA    IN NUMBER,
                                       V_ID_BENEFICIAR  IN NUMBER,
                                       V_TIP_SAFT       IN NUMBER DEFAULT 380);

  PROCEDURE initializeaza_setari_facturare;

  PROCEDURE initializeaza_date_gestiune(V_ID_GESTIUNE IN NUMBER,
                                        V_ID_TIPGEST  IN NUMBER,
                                        V_CONT        IN VARCHAR2,
                                        V_ACONT       IN VARCHAR2);

  PROCEDURE citeste_setari_document(V_TIP     IN NUMBER,
                                    V_ID_FDOC OUT NUMBER,
                                    V_FDOC    OUT VARCHAR2);

  PROCEDURE citeste_setari_partener(V_TIP      IN NUMBER,
                                    V_ID_PART  OUT NUMBER,
                                    V_DENUMIRE OUT VARCHAR2);

  PROCEDURE citeste_setari_pol_pret(V_TIP      IN NUMBER,
                                    V_ID_UTIL  IN NUMBER,
                                    V_ID_POL   OUT NUMBER,
                                    V_DENUMIRE OUT VARCHAR2);

  PROCEDURE genereaza_numar_proforma(V_NR_PROFORMA OUT NUMBER);

  PROCEDURE completare_politica_stoc;

  PROCEDURE modificare_politica_stoc(V_ID_POL IN NUMBER);

  PROCEDURE cursor_preturi(V_DATA_CURS        IN DATE,
                           V_TIP              IN NUMBER,
                           V_ID_VALUTA        IN NUMBER,
                           V_ID_GESTIUNE_INIT IN NUMBER,
                           V_LUNA             IN NUMBER,
                           V_AN               IN NUMBER,
                           V_ID_UTIL          IN NUMBER,
                           V_ID_SUCURSALA     IN NUMBER,
                           V_CURSOR           OUT cursor_facturare);

  PROCEDURE cursor_contract(V_DATA_CURS        IN DATE,
                            V_TIP              IN NUMBER,
                            V_ID_VALUTA        IN NUMBER,
                            V_LISTAID          IN VARCHAR2,
                            V_ID_GESTIUNE_INIT IN NUMBER,
                            V_LUNA             IN NUMBER,
                            V_AN               IN NUMBER,
                            V_ID_UTIL          IN NUMBER,
                            V_ID_SUCURSALA     IN NUMBER,
                            V_ID_AGENT         OUT NUMBER,
                            V_NUME_AGENT       OUT VARCHAR2,
                            V_CURSOR           OUT cursor_facturare,
                            V_CURSOR2          OUT cursor_facturare);

  PROCEDURE cursor_comanda(V_DATA_CURS IN DATE,
                           V_TIP       IN NUMBER,
                           V_LISTAID   IN VARCHAR2,
                           V_ID_UTIL   IN NUMBER,
                           V_CURSOR    OUT cursor_facturare);

  PROCEDURE cursor_lucrare(V_DATA_CURS    IN DATE,
                           V_ID_GESTIUNE  IN NUMBER,
                           V_ID_LUCRARE   IN NUMBER,
                           V_LUNA         IN NUMBER,
                           V_AN           IN NUMBER,
                           V_ID_UTIL      IN NUMBER,
                           V_ID_SUCURSALA IN NUMBER,
                           V_CURSOR       OUT cursor_facturare);

  PROCEDURE cursor_articole_k(V_DATA_CURS    IN DATE,
                              V_LUNA         IN NUMBER,
                              V_AN           IN NUMBER,
                              V_ID_UTIL      IN NUMBER,
                              V_ID_SUCURSALA IN NUMBER,
                              V_CURSOR       OUT cursor_facturare);

  PROCEDURE cursor_avize(V_LISTAID  IN VARCHAR2,
                         V_ID_UTIL  IN NUMBER,
                         V_DISCOUNT OUT NUMBER,
                         V_CURSOR   OUT cursor_facturare);

  PROCEDURE cursor_aviz_nir(V_ID_GESTIUNE_DEST IN NUMBER,
                            V_ID_FURNIZOR      IN NUMBER,
                            V_NR_ACT           IN NUMBER,
                            V_ID_SUCURSALA     IN NUMBER,
                            V_CURSOR           OUT cursor_facturare);

  PROCEDURE cursor_retur(V_IN_VALUTA IN NUMBER,
                         V_LISTAID   IN VARCHAR2,
                         V_ID_UTIL   IN NUMBER,
                         V_CURSOR    OUT cursor_facturare);

  PROCEDURE cursor_retur_document(V_IN_VALUTA IN NUMBER,
                                  V_LISTAID   IN VARCHAR2,
                                  V_COPIERE   IN NUMBER,
                                  V_PROFORMA  IN NUMBER,
                                  V_ID_UTIL   IN NUMBER,
                                  V_CURSOR    OUT cursor_facturare);

  PROCEDURE cursor_copiere(V_IN_VALUTA IN NUMBER,
                           V_LISTAID   IN VARCHAR2,
                           V_ID_UTIL   IN NUMBER,
                           V_CURSOR    OUT cursor_facturare);

  PROCEDURE cursor_gestiune(V_DATA_CURS    IN DATE,
                            V_ID_POL       IN NUMBER,
                            V_ID_GESTIUNE  IN NUMBER,
                            V_LUNA         IN NUMBER,
                            V_AN           IN NUMBER,
                            V_ID_UTIL      IN NUMBER,
                            V_ID_SUCURSALA IN NUMBER,
                            V_CURSOR       OUT cursor_facturare);

  PROCEDURE cursor_gestiuni_articol(V_ID_ARTICOL   IN NUMBER,
                                    V_TIP          IN NUMBER,
                                    V_ID_GESTIUNE  IN NUMBER,
                                    V_LUNA         IN NUMBER,
                                    V_AN           IN NUMBER,
                                    V_ID_UTIL      IN NUMBER,
                                    V_ID_SUCURSALA IN NUMBER,
                                    V_CURSOR       OUT cursor_facturare);

  PROCEDURE cursor_gestiuni_articol_stoc0(V_ID_ARTICOL   IN NUMBER,
                                          V_TIP          IN NUMBER,
                                          V_ID_GESTIUNE  IN NUMBER,
                                          V_LUNA         IN NUMBER,
                                          V_AN           IN NUMBER,
                                          V_ID_UTIL      IN NUMBER,
                                          V_ID_SUCURSALA IN NUMBER,
                                          V_CURSOR       OUT cursor_facturare);

  PROCEDURE cursor_gestiuni_articol_retur(V_ID_ARTICOL   IN NUMBER,
                                          V_TIP          IN NUMBER,
                                          V_ID_GESTIUNE  IN NUMBER,
                                          V_PRETV        IN NUMBER,
                                          V_DISCOUNT     IN NUMBER,
                                          V_LUNA         IN NUMBER,
                                          V_AN           IN NUMBER,
                                          V_ID_UTIL      IN NUMBER,
                                          V_LISTAID      IN VARCHAR2,
                                          V_ID_SUCURSALA IN NUMBER,
                                          V_CURSOR       OUT cursor_facturare);

  PROCEDURE adauga_articol_factura_deviz(V_ID_ARTICOL         IN NUMBER,
                                         V_EXPLICATIE         IN VARCHAR2,
                                         V_SERIE              IN VARCHAR2,
                                         V_PRET_ACHIZITIE     IN NUMBER,
                                         V_PRETD              IN NUMBER,
                                         V_ID_VALUTAD         IN NUMBER,
                                         V_PRET               IN NUMBER,
                                         V_ID_VALUTA          IN NUMBER,
                                         V_CURS               IN NUMBER,
                                         V_MULTIPLICATOR      IN NUMBER,
                                         V_PROC_TVAV          IN NUMBER,
                                         V_ID_JTVA_COLOANA    IN NUMBER,
                                         V_CANTITATE          IN NUMBER,
                                         V_DISCOUNT_UNITAR    IN NUMBER,
                                         V_ID_GESTIUNE        IN NUMBER,
                                         V_CONT               IN NUMBER,
                                         V_PRET_CU_TVA        IN NUMBER,
                                         V_ID_JTVA_COLOANA_EX IN NUMBER DEFAULT NULL,
                                         V_ID_CTR             IN NUMBER DEFAULT NULL,
                                         V_TAXCODE            IN NUMBER DEFAULT NULL,
                                         V_LOT                IN VARCHAR2 DEFAULT NULL);

  PROCEDURE adauga_articol_factura_stoc(V_ID_ARTICOL      IN NUMBER,
                                        V_SERIE           IN VARCHAR2,
                                        V_PRET_ACHIZITIE  IN NUMBER,
                                        V_PRETD           IN NUMBER,
                                        V_ID_VALUTAD      IN NUMBER,
                                        V_PRET            IN NUMBER,
                                        V_ID_VALUTA       IN NUMBER,
                                        V_CURS            IN NUMBER,
                                        V_MULTIPLICATOR   IN NUMBER,
                                        V_PROC_TVAV       IN NUMBER,
                                        V_ID_JTVA_COLOANA IN NUMBER,
                                        V_CANTITATE       IN NUMBER,
                                        V_DISCOUNT_UNITAR IN NUMBER,
                                        V_ID_GESTIUNE     IN NUMBER,
                                        V_CONT            IN NUMBER,
                                        V_PRET_CU_TVA     IN NUMBER,
                                        V_EXPLICATIE      IN VARCHAR2,
                                        V_ID_RUL_AUX      IN NUMBER,
                                        V_TAXCODE         IN NUMBER DEFAULT NULL,
                                        V_LOT             IN VARCHAR2 DEFAULT NULL);

  PROCEDURE adauga_articol_factura_stoc(V_ID_ARTICOL      IN NUMBER,
                                        V_SERIE           IN VARCHAR2,
                                        V_PRET_ACHIZITIE  IN NUMBER,
                                        V_PRETD           IN NUMBER,
                                        V_ID_VALUTAD      IN NUMBER,
                                        V_PRET            IN NUMBER,
                                        V_ID_VALUTA       IN NUMBER,
                                        V_CURS            IN NUMBER,
                                        V_MULTIPLICATOR   IN NUMBER,
                                        V_PROC_TVAV       IN NUMBER,
                                        V_ID_JTVA_COLOANA IN NUMBER,
                                        V_CANTITATE       IN NUMBER,
                                        V_DISCOUNT_UNITAR IN NUMBER,
                                        V_ID_GESTIUNE     IN NUMBER,
                                        V_CONT            IN NUMBER,
                                        V_PRET_CU_TVA     IN NUMBER);

  PROCEDURE adauga_articol_lucrare_pret(V_ID_COMANDA       IN NUMBER,
                                        V_ID_ARTICOL       IN NUMBER,
                                        V_ID_POL           IN NUMBER,
                                        V_CANTITATE        IN NUMBER,
                                        V_CONT             IN VARCHAR2,
                                        V_CURS             IN NUMBER,
                                        V_MULTIPLICATOR    IN NUMBER,
                                        V_ID_JTVA_COLOANA  IN NUMBER,
                                        V_ID_GESTIUNE_DEST IN NUMBER,
                                        V_ID_UTIL          IN NUMBER,
                                        V_PRETIN           in number);

  PROCEDURE adauga_articol_set(V_ID_VANZARE_SET  IN NUMBER,
                               V_DENUMIRE        IN VARCHAR2,
                               V_EXPLICATIE      IN VARCHAR2,
                               V_UM              IN VARCHAR2,
                               V_SERIE           IN VARCHAR2,
                               V_CANTITATE       IN NUMBER,
                               V_PRET            IN NUMBER,
                               V_DISCOUNT_UNITAR IN NUMBER);

  PROCEDURE adauga_articol_factura(V_ID_TEMP             IN NUMBER,
                                   V_ID_ARTICOL          IN NUMBER,
                                   V_SERIE               IN VARCHAR2,
                                   V_EXPLICATIE          IN VARCHAR2,
                                   V_ID_POL              IN NUMBER,
                                   V_ID_GESTIUNE         IN NUMBER,
                                   V_PRET_ACHIZITIE_TEMP IN NUMBER,
                                   V_PRETD               IN NUMBER,
                                   V_ID_VALUTAD          IN NUMBER,
                                   V_PRET_TEMP           IN NUMBER,
                                   V_ID_VALUTA_TEMP      IN NUMBER,
                                   V_PRETURI_CU_TVA_TEMP IN NUMBER,
                                   V_IN_STOC_TEMP        IN NUMBER,
                                   V_CANTITATE           IN NUMBER,
                                   V_DISCOUNT_UNITAR     IN NUMBER,
                                   V_CONT                IN VARCHAR2,
                                   V_CURS                IN NUMBER,
                                   V_MULTIPLICATOR       IN NUMBER,
                                   V_ID_JTVA_COLOANA     IN NUMBER,
                                   V_ID_PART_REZ         IN NUMBER,
                                   V_ID_LUCRARE_REZ      IN NUMBER,
                                   V_PRETV_ORIG          IN NUMBER,
                                   V_ID_VANZARE_SET      IN NUMBER,
                                   V_ID_CTR              IN NUMBER,
                                   V_ID_UTIL             IN NUMBER,
                                   V_TAXCODE             IN NUMBER DEFAULT NULL,
                                   V_LOT                 IN VARCHAR2 DEFAULT NULL);

  PROCEDURE sterge_articol_factura(V_ID_TEMP IN NUMBER,
                                   V_ID_UTIL IN NUMBER);

  PROCEDURE adauga_diferente_pret(V_SIR_ID_TEMP   IN VARCHAR2,
                                  V_SIR_DIFERENTA IN VARCHAR,
                                  V_ID_UTIL       IN NUMBER);

  PROCEDURE adauga_rata_factura(V_ID_RATA         IN NUMBER,
                                V_PRET            IN NUMBER,
                                V_CURS            IN NUMBER,
                                V_MULTIPLICATOR   IN NUMBER,
                                V_ID_JTVA_COLOANA IN NUMBER,
                                V_EXPLICATIE      IN VARCHAR2,
                                V_ID_UTIL         IN NUMBER,
                                V_TAXCODE         IN NUMBER DEFAULT NULL);

  PROCEDURE sterge_rata_factura(V_ID_RATA IN NUMBER, V_ID_UTIL IN NUMBER);

  PROCEDURE sterge_proforma_old(V_ID_PROFORMA IN NUMBER,
                                V_ID_UTIL     IN NUMBER);

  PROCEDURE sterge_factura(V_ID_VANZARE IN NUMBER,
                           V_LUNA       IN NUMBER,
                           V_AN         IN NUMBER,
                           V_ID_UTIL    IN NUMBER);

  PROCEDURE sterge_proforma(V_ID_VANZARE IN NUMBER, V_ID_UTIL IN NUMBER);

  PROCEDURE scrie_proforma_old(V_DISCOUNT_PROFORMA   IN NUMBER,
                               V_TEXT_ADITIONAL      IN VARCHAR2,
                               V_DISCOUNT_EVIDENTIAT IN NUMBER,
                               V_ID_PROFORMA         OUT NUMBER,
                               V_NUMAR_ACT           OUT NUMBER);

  PROCEDURE scrie_proforma(V_TOTFTVA             IN NUMBER,
                           V_TOTTVA              IN NUMBER,
                           V_DISCOUNT_FACTURA    IN NUMBER,
                           V_SERIE_ACT_INCASARE  IN VARCHAR2,
                           V_NUMAR_ACT_INCASARE  IN NUMBER,
                           V_LISTA_INCASARE      IN VARCHAR2,
                           V_ID_DELEGAT          IN NUMBER,
                           V_ID_MASINA           IN NUMBER,
                           V_ID_FACTURARE        IN NUMBER,
                           V_LISTARE_DETALIATA   IN NUMBER,
                           V_DATAORA_EXP         IN DATE,
                           V_ID_AGENT            IN NUMBER,
                           V_TEXT_ADITIONAL      IN VARCHAR2,
                           V_DISCOUNT_EVIDENTIAT IN NUMBER,
                           V_PARAMETRU_ADITIONAL IN NUMBER,
                           V_ID_VANZARE          OUT NUMBER);

  PROCEDURE inchide_comanda;

  PROCEDURE scrie_avize_lucrare(V_DISCOUNT_FACTURA    IN NUMBER,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_ID_SERIE_ACT        IN NUMBER,
                                V_ID_VANZARE          OUT NUMBER,
                                V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare);

  /*  PROCEDURE scrie_factura2(V_TOTFTVA             IN NUMBER,
  V_TOTTVA              IN NUMBER,
  V_DISCOUNT_FACTURA    IN NUMBER,
  V_SERIE_ACT_INCASARE  IN VARCHAR2,
  V_NUMAR_ACT_INCASARE  IN NUMBER,
  V_LISTA_INCASARE      IN VARCHAR2,
  V_ID_DELEGAT          IN NUMBER,
  V_ID_MASINA           IN NUMBER,
  V_DATAORA_EXP         IN DATE,
  V_ID_AGENT            IN NUMBER,
  V_TEXT_ADITIONAL      IN VARCHAR2,
  V_DISCOUNT_EVIDENTIAT IN NUMBER,
  V_PARAMETRU_ADITIONAL IN NUMBER,
  V_ID_VANZARE          OUT NUMBER,
  V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare);*/

  PROCEDURE scrie_factura2(V_TOTFTVA             IN NUMBER,
                           V_TOTTVA              IN NUMBER,
                           V_DISCOUNT_FACTURA    IN NUMBER,
                           V_SERIE_ACT_INCASARE  IN VARCHAR2,
                           V_NUMAR_ACT_INCASARE  IN NUMBER,
                           V_LISTA_INCASARE      IN VARCHAR2,
                           V_ID_DELEGAT          IN NUMBER,
                           V_ID_MASINA           IN NUMBER,
                           V_ID_FACTURARE        IN NUMBER,
                           V_LISTARE_DETALIATA   IN NUMBER,
                           V_DATAORA_EXP         IN DATE,
                           V_ID_AGENT            IN NUMBER,
                           V_TEXT_ADITIONAL      IN VARCHAR2,
                           V_DISCOUNT_EVIDENTIAT IN NUMBER,
                           V_PARAMETRU_ADITIONAL IN NUMBER,
                           V_ID_VANZARE          OUT NUMBER,
                           V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare);

  /*  PROCEDURE scrie_factura_avize_retur(V_DISCOUNT_FACTURA     IN NUMBER,
  V_ID_DELEGAT           IN NUMBER,
  V_ID_MASINA            IN NUMBER,
  V_DATAORA_EXP          IN DATE,
  V_ID_AGENT             IN NUMBER,
  V_DISCOUNT_EVIDENTIAT  IN NUMBER,
  V_TEXT_ADITIONAL       IN VARCHAR2,
  V_TEXT_ADITIONAL_RETUR IN VARCHAR2,
  V_SIR_MODIFICARI_ACONT IN VARCHAR2,
  V_SIR_MODIFICARI_PART  IN VARCHAR2,
  V_PARAMETRU_ADITIONAL  IN NUMBER,
  V_SERIE_ACT            IN VARCHAR2,
  V_NUMAR_ACT            IN NUMBER,
  V_ID_VANZARE           OUT NUMBER,
  V_ID_VANZARE_RETUR     OUT NUMBER,
  V_CURSOR_RETUR         OUT cursor_facturare);*/

  PROCEDURE scrie_factura_avize_retur(V_DISCOUNT_FACTURA     IN NUMBER,
                                      V_ID_DELEGAT           IN NUMBER,
                                      V_ID_MASINA            IN NUMBER,
                                      V_ID_FACTURARE         IN NUMBER,
                                      V_LISTARE_DETALIATA    IN NUMBER,
                                      V_DATAORA_EXP          IN DATE,
                                      V_ID_AGENT             IN NUMBER,
                                      V_DISCOUNT_EVIDENTIAT  IN NUMBER,
                                      V_TEXT_ADITIONAL       IN VARCHAR2,
                                      V_TEXT_ADITIONAL_RETUR IN VARCHAR2,
                                      V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                      V_SIR_MODIFICARI_PART  IN VARCHAR2,
                                      V_PARAMETRU_ADITIONAL  IN NUMBER,
                                      V_SERIE_ACT            IN VARCHAR2,
                                      V_NUMAR_ACT            IN NUMBER,
                                      V_ID_VANZARE           OUT NUMBER,
                                      V_ID_VANZARE_RETUR     OUT NUMBER,
                                      V_CURSOR_RETUR         OUT cursor_facturare);

  /*  PROCEDURE scrie_factura_avize(V_DISCOUNT_FACTURA    IN NUMBER,
  V_SERIE_ACT_INCASARE  IN VARCHAR2,
  V_NUMAR_ACT_INCASARE  IN NUMBER,
  V_LISTA_INCASARE      IN VARCHAR2,
  V_ID_DELEGAT          IN NUMBER,
  V_ID_MASINA           IN NUMBER,
  V_DATAORA_EXP         IN DATE,
  V_ID_AGENT            IN NUMBER,
  V_TEXT_ADITIONAL      IN VARCHAR2,
  V_DISCOUNT_EVIDENTIAT IN NUMBER,
  V_VERIFICARE_FACTURAT IN NUMBER,
  V_ID_VANZARE          OUT NUMBER,
  V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare);*/

  PROCEDURE scrie_factura_avize(V_DISCOUNT_FACTURA    IN NUMBER,
                                V_SERIE_ACT_INCASARE  IN VARCHAR2,
                                V_NUMAR_ACT_INCASARE  IN NUMBER,
                                V_LISTA_INCASARE      IN VARCHAR2,
                                V_ID_DELEGAT          IN NUMBER,
                                V_ID_MASINA           IN NUMBER,
                                V_ID_FACTURARE        IN NUMBER,
                                V_LISTARE_DETALIATA   IN NUMBER,
                                V_DATAORA_EXP         IN DATE,
                                V_ID_AGENT            IN NUMBER,
                                V_TEXT_ADITIONAL      IN VARCHAR2,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_VERIFICARE_FACTURAT IN NUMBER,
                                V_ID_VANZARE          OUT NUMBER,
                                V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare);

  /*  PROCEDURE scrie_aviz_retur(V_ID_DELEGAT          IN NUMBER,
  V_ID_MASINA           IN NUMBER,
  V_DATAORA_EXP         IN DATE,
  V_ID_AGENT            IN NUMBER,
  V_TEXT_ADITIONAL      IN VARCHAR2,
  V_DISCOUNT_EVIDENTIAT IN NUMBER,
  V_SCRIS               OUT NUMBER,
  V_ID_VANZARE          OUT NUMBER,
  V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare);*/

  PROCEDURE scrie_aviz_retur(V_ID_DELEGAT          IN NUMBER,
                             V_ID_MASINA           IN NUMBER,
                             V_ID_FACTURARE        IN NUMBER,
                             V_LISTARE_DETALIATA   IN NUMBER,
                             V_DATAORA_EXP         IN DATE,
                             V_ID_AGENT            IN NUMBER,
                             V_TEXT_ADITIONAL      IN VARCHAR2,
                             V_DISCOUNT_EVIDENTIAT IN NUMBER,
                             V_SCRIS               OUT NUMBER,
                             V_ID_VANZARE          OUT NUMBER,
                             V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare);

  FUNCTION contabilizeaza_articol(detalii_articol VANZARI_DETALII_TEMP%ROWTYPE)
    RETURN NUMBER;

  FUNCTION contabilizeaza_rata(detalii_rata VANZARI_DETALII_TEMP%ROWTYPE)
    RETURN NUMBER;

  PROCEDURE descarca_gestiune(V_ID_ARTICOL     IN NUMBER,
                              V_SERIE          IN VARCHAR2,
                              V_PRET_ACHIZITIE IN NUMBER,
                              V_PRETD          IN NUMBER,
                              V_ID_VALUTAD     IN NUMBER,
                              V_PRETV_ALES     IN NUMBER,
                              V_PRET_UNITAR    IN NUMBER,
                              V_AJUSTARE       IN NUMBER,
                              V_PROC_TVAV      IN NUMBER,
                              V_PRET_ARE_TVA   IN NUMBER,
                              V_CANTE          IN NUMBER,
                              V_DISCOUNT       IN NUMBER,
                              V_ID_GESTIUNE    IN NUMBER,
                              V_ID_SECTIE      IN NUMBER,
                              V_CONT           IN VARCHAR2,
                              V_ID_VENCHELT    IN NUMBER,
                              V_ID_VALUTA      IN NUMBER,
                              V_CURS           IN NUMBER,
                              V_ID_PART_REZ    IN NUMBER,
                              V_ID_LUCRARE_REZ IN NUMBER);

  PROCEDURE descarca_gestiune(V_ID_ARTICOL      IN NUMBER,
                              V_SERIE           IN VARCHAR2,
                              V_PRET_ACHIZITIE  IN NUMBER,
                              V_PRETD           IN NUMBER,
                              V_ID_VALUTAD      IN NUMBER,
                              V_PRETV_ALES      IN NUMBER,
                              V_PRET_UNITAR     IN NUMBER,
                              V_AJUSTARE        IN NUMBER,
                              V_PROC_TVAV       IN NUMBER,
                              V_PRET_ARE_TVA    IN NUMBER,
                              V_CANTE           IN NUMBER,
                              V_DISCOUNT        IN NUMBER,
                              V_ID_GESTIUNE     IN NUMBER,
                              V_ID_SECTIE       IN NUMBER,
                              V_CONT            IN VARCHAR2,
                              V_ID_VENCHELT     IN NUMBER,
                              V_ID_VALUTA       IN NUMBER,
                              V_CURS            IN NUMBER,
                              V_ID_PART_REZ     IN NUMBER,
                              V_ID_LUCRARE_REZ  IN NUMBER,
                              V_ID_JTVA_COLOANA IN NUMBER,
                              V_TAXCODE         IN NUMBER);

  PROCEDURE scrie_fact_aviz_custodie(V_COD             IN NUMBER,
                                     V_ID_SECTIE       IN NUMBER,
                                     V_ID_ARTICOL      IN NUMBER,
                                     V_ID_GESTIUNE     IN NUMBER,
                                     V_CONT            IN VARCHAR2,
                                     V_CANTITATE       IN NUMBER,
                                     V_PRET_ARE_TVA    IN NUMBER,
                                     V_PRET_ACHIZITIE  IN NUMBER,
                                     V_PRETD           IN NUMBER,
                                     V_ID_VALUTAD      IN NUMBER,
                                     V_PRETV           IN NUMBER,
                                     V_DISCOUNT_UNITAR IN NUMBER,
                                     V_ID_VALUTA       IN NUMBER,
                                     V_CURS            IN NUMBER,
                                     V_PROC_TVAV       IN NUMBER);

  PROCEDURE transfera_articol(detalii_articol VANZARI_DETALII_TEMP%ROWTYPE);

  FUNCTION scrie_nota(V_CANTITATE       IN NUMBER,
                      V_PRET            IN NUMBER,
                      V_DISCOUNT_UNITAR IN NUMBER,
                      V_PRET_ARE_TVA    IN NUMBER, -- 0  DACA V_PRET NU CONTINE SI TVA-UL ( PRETUL FOLOSIT CA REFERINTA )
                      V_ID_VALUTA       IN NUMBER,
                      V_IN_VALUTA       IN NUMBER,
                      V_CURS            IN NUMBER,
                      V_ID_VENCHELT     IN NUMBER,
                      V_ID_SECTIE       IN NUMBER,
                      V_ID_CTR          IN NUMBER,
                      V_EXPLICATIE      IN VARCHAR2,
                      V_SCD             IN VARCHAR2,
                      V_ASCD            IN VARCHAR2,
                      V_SCC             IN VARCHAR2,
                      V_ASCC            IN VARCHAR2,
                      V_ID_GESTIN       IN NUMBER,
                      V_ID_GESTOUT      IN NUMBER,
                      V_ID_SET          IN NUMBER,
                      V_CU_TVA          IN NUMBER, --      0  DACA SE SCRIE SI NOTA DE TVA
                      V_PTVA            IN NUMBER,
                      V_ID_JTVA_COLOANA IN NUMBER,
                      V_TAXCODE         IN NUMBER DEFAULT NULL,
                      V_PAYMENTCODE     IN NUMBER DEFAULT NULL) RETURN NUMBER;

  PROCEDURE scrie_tva(V_SUMA            IN NUMBER,
                      V_SUMA_VAL        IN NUMBER,
                      V_ID_VALUTA       IN NUMBER,
                      V_IN_VALUTA       IN NUMBER,
                      V_CONT            IN VARCHAR2,
                      V_ACONT           IN VARCHAR2,
                      V_CURS            IN NUMBER,
                      V_ID_VENCHELT     IN NUMBER,
                      V_ID_SECTIE       IN NUMBER,
                      V_ID_CTR          IN NUMBER,
                      V_EXPLICATIE      IN VARCHAR2,
                      V_PTVA            IN NUMBER,
                      V_ID_FACT         IN NUMBER,
                      V_ID_JTVA_COLOANA IN NUMBER);

  FUNCTION scrie_discount(V_CANTITATE       IN NUMBER,
                          V_DISCOUNT_UNITAR IN NUMBER,
                          V_ID_VALUTA       IN NUMBER,
                          V_IN_VALUTA       IN NUMBER,
                          V_ASCC            IN VARCHAR2,
                          V_ID_SECTIE       IN NUMBER,
                          V_ID_CTR          IN NUMBER,
                          V_CURS            IN NUMBER,
                          V_PROC_TVAV       IN NUMBER,
                          V_PRET_CU_TVA     IN NUMBER,
                          V_CU_TVA          IN NUMBER,
                          V_ID_JTVA_COLOANA IN NUMBER,
                          V_TAXCODE         IN NUMBER DEFAULT NULL)
    RETURN NUMBER;

  PROCEDURE scrie_descarcare_k(V_COEFICIENT_K IN NUMBER);

  PROCEDURE scrie_avans(V_SUMA IN NUMBER);

  Procedure scrie_incasari(V_SERIE_ACT_INCASARE IN VARCHAR2,
                           V_NUMAR_ACT_INCASARE IN NUMBER,
                           V_LISTA_INCASARE     IN VARCHAR2,
                           V_ID_CTR             IN NUMBER);

  PROCEDURE scrie_incasare2(V_TIP                IN NUMBER,
                            V_SERIE_ACT_INCASARE IN VARCHAR2,
                            V_NUMAR_ACT_INCASARE IN NUMBER,
                            V_ID_CTR             IN NUMBER,
                            V_ID_BANCASA         IN NUMBER,
                            V_SUMA               IN NUMBER,
                            V_ASCC               IN ACT.ASCC%TYPE DEFAULT NULL,
                            V_PSCD               IN ACT.SCD%TYPE DEFAULT NULL,
                            V_ASCD               IN ACT.ASCD%TYPE DEFAULT NULL);

  /*  PROCEDURE scrie_incasare(V_NUMAR_ACT_INCASARE IN NUMBER,
  V_ID_CTR             IN NUMBER,
  V_ID_CASA            IN NUMBER,
  V_SUMA               IN NUMBER);*/

  PROCEDURE scrie_note_banca_compensari;

  /*  PROCEDURE scrie_in_vanzari(V_DISCOUNT_FACTURA IN NUMBER,
  V_ID_DELEGAT       IN NUMBER,
  V_ID_MASINA        IN NUMBER,
  V_DATAORA_EXP      IN DATE,
  V_ID_AGENT         IN NUMBER,
  V_TEXT_ADITIONAL   IN VARCHAR2,
  V_ID_VANZARE       OUT NUMBER);*/

  PROCEDURE scrie_in_vanzari(V_DISCOUNT_FACTURA  IN NUMBER,
                             V_ID_DELEGAT        IN NUMBER,
                             V_ID_MASINA         IN NUMBER,
                             V_ID_FACTURARE      IN NUMBER,
                             V_LISTARE_DETALIATA IN NUMBER,
                             V_DATAORA_EXP       IN DATE,
                             V_ID_AGENT          IN NUMBER,
                             V_TEXT_ADITIONAL    IN VARCHAR2,
                             V_ID_VANZARE        OUT NUMBER);

  PROCEDURE scrie_atasamente_factura(V_ID_VANZARE IN NUMBER,
                                     V_NUME_FRX   IN NUMBER,
                                     V_WDOCUMENT  IN BLOB,
                                     V_ID_UTIL    IN NUMBER);

  PROCEDURE scrie_seturi;

  PROCEDURE scrie_seturi_proforma;

  PROCEDURE initializeaza_part_rez(V_ID_PART IN NUMBER);

  PROCEDURE cumuleaza_note_act;

  PROCEDURE cumuleaza_note_act_temp;

  PROCEDURE cursor_verificare(V_CURSOR_VERIFICARE OUT pack_facturare.cursor_facturare);

  PROCEDURE modifica_date_factura(V_ID_VANZARE        IN NUMBER,
                                  V_ID_RUTA           IN NUMBER,
                                  V_ID_DELEGAT        IN NUMBER,
                                  V_ID_AGENT          IN NUMBER,
                                  V_ID_MASINA         IN NUMBER,
                                  V_DATAORA_EXP       IN DATE,
                                  V_ID_FACTURARE      IN VANZARI.ID_FACTURARE%TYPE,
                                  V_LISTARE_DETALIATA IN VANZARI.LISTARE_DETALIATA%TYPE,
                                  V_TEXT_ADITIONAL    IN VANZARI.TEXT_ADITIONAL%TYPE,
                                  V_TIP_SAFT          IN VANZARI.TIP_SAFT%TYPE DEFAULT NULL,
                                  V_EFACTURA          IN VANZARI.EFACTURA%TYPE DEFAULT NULL,
                                  V_DATA_ACT          IN VANZARI.DATA_ACT%TYPE DEFAULT NULL,
                                  V_DATA_SCAD         IN VANZARI.DATA_SCAD%TYPE DEFAULT NULL,
                                  V_NUMAR_ACT         IN VANZARI.NUMAR_ACT%TYPE DEFAULT NULL,
                                  V_SERIE_ACT         IN VANZARI.SERIE_ACT%TYPE DEFAULT NULL);

  PROCEDURE modifica_explicatie_articol(V_ID_VANZARE_DET IN NUMBER,
                                        V_EXPLICATIE     IN VARCHAR2,
                                        V_ID_UTIL        IN NUMBER,
                                        V_TAXCODE        IN NUMBER DEFAULT NULL);

  PROCEDURE initializeaza_moneda_nationala;

  PROCEDURE scrie_cursuri_proforma(V_ID_PROFORMA IN NUMBER);

  PROCEDURE scrie_cursuri(V_ID_VANZARE IN NUMBER);

  PROCEDURE scrie_rate_factura(V_DATAORA IN DATE);

  FUNCTION calculeaza_k RETURN NUMBER;

  PROCEDURE calculeaza_pret(V_PRET_UNITAR   IN NUMBER,
                            V_CURS          IN NUMBER,
                            V_ID_VALUTA     IN NUMBER,
                            V_PROC_TVA      IN NUMBER,
                            V_PRET_ARE_TVA  IN NUMBER,
                            V_TIP           IN NUMBER,
                            V_PRET_FARA_TVA OUT NUMBER,
                            V_PRET_TVA      OUT NUMBER,
                            V_PRET_CU_TVA   OUT NUMBER);

  /*  function calculeaza_pret_fara_tva(V_PRET_UNITAR  IN NUMBER,
                                    V_CURS         IN NUMBER,
                                    V_ID_VALUTA    IN NUMBER,
                                    V_PROC_TVA     IN NUMBER,
                                    V_PRET_ARE_TVA IN NUMBER) return number;

  function calculeaza_pret_TVA(V_PRET_UNITAR  IN NUMBER,
                               V_CURS         IN NUMBER,
                               V_ID_VALUTA    IN NUMBER,
                               V_PROC_TVA     IN NUMBER,
                               V_PRET_ARE_TVA IN NUMBER) return number;

  function calculeaza_pret_cu_tva(V_PRET_UNITAR  IN NUMBER,
                                  V_CURS         IN NUMBER,
                                  V_ID_VALUTA    IN NUMBER,
                                  V_PROC_TVA     IN NUMBER,
                                  V_PRET_ARE_TVA IN NUMBER) return number;*/

  PROCEDURE calculeaza_sume(V_PRET_UNITAR     IN NUMBER,
                            V_DISCOUNT_UNITAR IN NUMBER,
                            V_ZECIMALE_PRET   IN NUMBER,
                            V_ZECIMALE_SUMA   IN NUMBER,
                            V_CANTITATE       IN NUMBER,
                            V_CURS            IN NUMBER,
                            V_ID_VALUTA       IN NUMBER,
                            V_PROC_TVA        IN NUMBER,
                            V_PRET_ARE_TVA    IN NUMBER,
                            V_SUMA_FARA_TVA   OUT NUMBER,
                            V_SUMA_TVA        OUT NUMBER,
                            V_SUMA_CU_TVA     OUT NUMBER);

  /*  PROCEDURE finalizeaza_factura(V_DISCOUNT_FACTURA    IN NUMBER,
  V_ID_DELEGAT          IN NUMBER,
  V_ID_MASINA           IN NUMBER,
  V_DATAORA_EXP         IN DATE,
  V_ID_AGENT            IN NUMBER,
  V_TEXT_ADITIONAL      IN VARCHAR2,
  V_PARAMETRU_ADITIONAL IN NUMBER);*/

  PROCEDURE finalizeaza_factura(V_DISCOUNT_FACTURA    IN NUMBER,
                                V_ID_DELEGAT          IN NUMBER,
                                V_ID_MASINA           IN NUMBER,
                                V_ID_FACTURARE        IN NUMBER,
                                V_LISTARE_DETALIATA   IN NUMBER,
                                V_DATAORA_EXP         IN DATE,
                                V_ID_AGENT            IN NUMBER,
                                V_TEXT_ADITIONAL      IN VARCHAR2,
                                V_PARAMETRU_ADITIONAL IN NUMBER);

  PROCEDURE finalizeaza_avize_lucrare(V_DISCOUNT_FACTURA IN NUMBER);

  /* PROCEDURE finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA     IN NUMBER,
  V_ID_DELEGAT           IN NUMBER,
  V_ID_MASINA            IN NUMBER,
  V_DATAORA_EXP          IN DATE,
  V_ID_AGENT             IN NUMBER,
  V_TEXT_ADITIONAL       IN VARCHAR2,
  V_SIR_MODIFICARI_ACONT IN VARCHAR2,
  V_SIR_MODIFICARI_PART  IN VARCHAR,
  V_PARAMETRU_ADITIONAL  IN NUMBER,
  V_ID_VANZARE           OUT NUMBER);*/

  PROCEDURE finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA     IN NUMBER,
                                           V_ID_DELEGAT           IN NUMBER,
                                           V_ID_MASINA            IN NUMBER,
                                           V_DATAORA_EXP          IN DATE,
                                           V_ID_AGENT             IN NUMBER,
                                           V_TEXT_ADITIONAL       IN VARCHAR2,
                                           V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                           V_SIR_MODIFICARI_PART  IN VARCHAR,
                                           V_PARAMETRU_ADITIONAL  IN NUMBER,
                                           V_ID_VANZARE           OUT NUMBER);

  PROCEDURE finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA     IN NUMBER,
                                           V_ID_DELEGAT           IN NUMBER,
                                           V_ID_MASINA            IN NUMBER,
                                           V_ID_FACTURARE         IN NUMBER,
                                           V_LISTARE_DETALIATA    IN NUMBER,
                                           V_DATAORA_EXP          IN DATE,
                                           V_ID_AGENT             IN NUMBER,
                                           V_TEXT_ADITIONAL       IN VARCHAR2,
                                           V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                           V_SIR_MODIFICARI_PART  IN VARCHAR,
                                           V_PARAMETRU_ADITIONAL  IN NUMBER,
                                           V_ID_VANZARE           OUT NUMBER);

  Procedure initializeaza_scriere_actrul(tdDataOra         IN DATE,
                                         tnSuprascriereCod IN NUMBER DEFAULT 1);

  PROCEDURE finalizeaza_scriere_actrul;

  PROCEDURE marcheaza_facturat(V_VERIFICARE IN NUMBER);

  PROCEDURE scrie_cantitati_vanzari_avize;

  PROCEDURE scrie_corespondente_vanzari(V_TIP IN NUMBER);

  PROCEDURE cauta_date_ultima_factura(V_ID_PART      IN NUMBER,
                                      V_ID_SUCURSALA IN NUMBER,
                                      V_ID_DELEGAT   OUT NUMBER,
                                      V_NUME_DELEGAT OUT VARCHAR2,
                                      V_SERIE_BI     OUT VARCHAR2,
                                      V_CNP          OUT VARCHAR2,
                                      V_ID_MASINA    OUT NUMBER,
                                      V_NRINMAT      OUT VARCHAR2);

  PROCEDURE cauta_date_ultima_factura_tip(V_TIP          IN NUMBER,
                                          V_ID_SUCURSALA IN NUMBER,
                                          V_ID_DELEGAT   OUT NUMBER,
                                          V_NUME_DELEGAT OUT VARCHAR2,
                                          V_SERIE_BI     OUT VARCHAR2,
                                          V_CNP          OUT VARCHAR2,
                                          V_ID_MASINA    OUT NUMBER,
                                          V_NRINMAT      OUT VARCHAR2);

  PROCEDURE cauta_date_comanda(V_ID_CLIENT    IN NUMBER,
                               V_ID_SUCURSALA IN NUMBER,
                               V_DATA_FACTURA IN DATE,
                               V_LISTAID      OUT VARCHAR2,
                               V_DESCRIERE    OUT VARCHAR2);

  PROCEDURE cauta_date_comanda_gest(V_ID_GESTIUNE  IN NUMBER,
                                    V_ID_SUCURSALA IN NUMBER,
                                    V_DATA_FACTURA IN DATE,
                                    V_LISTAID      OUT VARCHAR2,
                                    V_DESCRIERE    OUT VARCHAR2);

  FUNCTION calculeaza_total_cu_tva(V_PRET                IN NUMBER,
                                   V_AJUSTARE            IN NUMBER,
                                   V_CURS                IN NUMBER,
                                   V_DISCOUNT_UNITAR     IN NUMBER,
                                   V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                   V_CANTITATE           IN NUMBER,
                                   V_PRET_CU_TVA         IN NUMBER,
                                   V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER;

  FUNCTION calculeaza_total_cu_tva(V_PRET                IN NUMBER,
                                   V_AJUSTARE            IN NUMBER,
                                   V_CURS                IN NUMBER,
                                   V_DISCOUNT_UNITAR     IN NUMBER,
                                   V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                   V_CANTITATE           IN NUMBER,
                                   V_PRET_CU_TVA         IN NUMBER,
                                   V_PROC_TVAV           IN NUMBER,
                                   V_ZECIMALE_PRET       IN NUMBER,
                                   V_ZECIMALE_SUMA       IN NUMBER)
    RETURN NUMBER;

  FUNCTION calculeaza_total_cu_tva_fact(V_PRET                IN NUMBER,
                                        V_DIFERENTA           IN NUMBER,
                                        V_CURS                IN NUMBER,
                                        V_DISCOUNT_UNITAR     IN NUMBER,
                                        V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                        V_CANTITATE           IN NUMBER,
                                        V_PRET_CU_TVA         IN NUMBER,
                                        V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER;

  FUNCTION calculeaza_total_fara_tva(V_PRET                IN NUMBER,
                                     V_AJUSTARE            IN NUMBER,
                                     V_CURS                IN NUMBER,
                                     V_DISCOUNT_UNITAR     IN NUMBER,
                                     V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                     V_CANTITATE           IN NUMBER,
                                     V_PRET_CU_TVA         IN NUMBER,
                                     V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER;

  FUNCTION calculeaza_total_fara_tva(V_PRET                IN NUMBER,
                                     V_AJUSTARE            IN NUMBER,
                                     V_CURS                IN NUMBER,
                                     V_DISCOUNT_UNITAR     IN NUMBER,
                                     V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                     V_CANTITATE           IN NUMBER,
                                     V_PRET_CU_TVA         IN NUMBER,
                                     V_PROC_TVAV           IN NUMBER,
                                     V_ZECIMALE_PRET       IN NUMBER,
                                     V_ZECIMALE_SUMA       IN NUMBER)
    RETURN NUMBER;

  FUNCTION calculeaza_total_tva(V_PRET                IN NUMBER,
                                V_AJUSTARE            IN NUMBER,
                                V_CURS                IN NUMBER,
                                V_DISCOUNT_UNITAR     IN NUMBER,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_CANTITATE           IN NUMBER,
                                V_PRET_CU_TVA         IN NUMBER,
                                V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER;

  FUNCTION calculeaza_total_tva(V_PRET                IN NUMBER,
                                V_AJUSTARE            IN NUMBER,
                                V_CURS                IN NUMBER,
                                V_DISCOUNT_UNITAR     IN NUMBER,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_CANTITATE           IN NUMBER,
                                V_PRET_CU_TVA         IN NUMBER,
                                V_PROC_TVAV           IN NUMBER,
                                V_ZECIMALE_PRET       IN NUMBER,
                                V_ZECIMALE_SUMA       IN NUMBER)
    RETURN NUMBER;

  FUNCTION calculeaza_total_fara_tva_fact(V_PRET                IN NUMBER,
                                          V_DIFERENTA           IN NUMBER,
                                          V_CURS                IN NUMBER,
                                          V_DISCOUNT_UNITAR     IN NUMBER,
                                          V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                          V_CANTITATE           IN NUMBER,
                                          V_PRET_CU_TVA         IN NUMBER,
                                          V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER;

  FUNCTION calculeaza_total_tva_fact(V_PRET                IN NUMBER,
                                     V_DIFERENTA           IN NUMBER,
                                     V_CURS                IN NUMBER,
                                     V_DISCOUNT_UNITAR     IN NUMBER,
                                     V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                     V_CANTITATE           IN NUMBER,
                                     V_PRET_CU_TVA         IN NUMBER,
                                     V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER;

  PROCEDURE actualizeaza_vanzari(V_COD_VECHI IN NUMBER,
                                 V_COD_NOU   IN NUMBER);

  PROCEDURE sterge_din_vanzari(V_COD     IN NUMBER,
                               V_AN      IN NUMBER,
                               V_LUNA    IN NUMBER,
                               V_ID_UTIL IN NUMBER);

  PROCEDURE verifica_cursuri_valute(V_DATA_CURS IN DATE,
                                    V_ID_UTIL   IN NUMBER);

  PROCEDURE verifica_total_document;

  PROCEDURE verifica_analitice378;

  function GetAnaliticByGrupUtilizatori(tnIdUtilizator ACT.ID_UTIL%TYPE,
                                        tcCont         ACT.SCD%TYPE)
    return ACT.ASCD%TYPE;

  FUNCTION GetAnaliticByArticol(tcCont       RUL.CONT%TYPE,
                                tnIdGestiune RUL.ID_GESTIUNE%TYPE DEFAULT NULL,
                                tnIdArticol  RUL.ID_ARTICOL%TYPE DEFAULT NULL)
    return RUL.ACONT%TYPE;

  Procedure citeste_vanzari_seturi(V_COD    IN NUMBER,
                                   V_CURSOR OUT cursor_facturare);

  Procedure citeste_proforme_seturi(V_ID_PROFORMA IN NUMBER,
                                    V_CURSOR      OUT cursor_facturare);

  procedure adauga_note_tva_incasare;

  function citeste_lungcampexplart return NUMBER;

  function getAnaliticK(tnProcTvaV IN NUMBER) return VARCHAR2;

  procedure getSoldClient(tnAn     IN NUMBER,
                         tnLuna   IN NUMBER,
                         tnIdPart IN NUMBER,
                         tnIdFact IN NUMBER default 0,
                         V_CURSOR           OUT cursor_facturare);
end PACK_FACTURARE;
/
CREATE OR REPLACE PACKAGE BODY "PACK_FACTURARE" is
  -- ultima eroare atribuita : FACT-024

  -- 09.02.2010
  -- marius.mutu
  -- la vanzarea din stoc din ROARETAIL - aflu preturile de achizitie/vanzare, seria, gestiunea si trebuie sa intru pe ramura default
  -- DESCARCAREA DE GESTIUNE OFFLINE TREBUIE RESCRISA SA FOLOSEASCA PROCEDURA PACK_RETAIL_STOC.SALVEAZABON

  -- 29.03.2010
  -- marius.mutu
  -- adaugare GetAnaliticByArticol pentru obtinerea analiticului 607,608, 378,388
  -- dupa configurare_analitice pe gestiuni/articole in procedura descarcare_gestiune

  -- 08.07.2010
  -- marius.mutu
  -- descarca_gestiune - In mesaj Eroare FACT-007 se afiseaza Id gestiune,  Id articol

  -- 11.08.2010
  -- marius.mutu
  -- scrie_incasari, transmit parametri V_SCD, V_ASCD la scrie_incasare2 + optiune pentru CONT DEBIT V_SCD

  -- 08.11.2010
  -- transfera_articol
  -- (25040,41) - retur transfer catre subunitati lista pret  ( 25050 - daca e cu scadere din gestiune )
  -- la retur transfer, trebuie schimbat semnul diferentei de pret

  -- 05.07.2011
  -- marius.mutu
  -- contabilizeaza_articol - tratare exceptie NO_DATA_FOUND FACT-024

  -- 13.01.2014
  -- marius.mutu
  -- Am completat ID_RESPONSABIL la instructiunile INSERT INTO ACT_TEMP

  -- 03.02.2015
  -- marius.mutu
  -- citeste_setari_document: adaugat PROFORMA, BON FISCAL

  -- 16.02.2015
  -- marius.mutu
  -- CURSOR_PRETURI
  -- FACTURARE LISTA PRETURI: se selecteaza toate articolele din lista de preturi, indiferent daca au stoc > 0
  -- optiunea RF_FACTURARE_FARA_STOC = 1
  -- descarca_gestiune: se pot vinde pe factura si articole retur (cante < 0) si articole care nu sunt in stoc (RF_FACTURARE_FARA_STOC)

  -- 06.03.2015
  -- marius.mutu
  -- descarca_gestiune: la factura de vanzare lista de preturi se descarca mai intai
  -- articolele retur din facturi anterioare, apoi
  -- articolele din stoc (indiferent daca cantitate este pozitiva sau negativa), apoi
  -- articolele fara stoc (RF_FACTURARE_FARA_STOC)
  -- asa se pot factura articole din stoc, cu cantitate < 0 (retur, fara factura anterioara)

  -- 30.04.2015
  -- marius.mutu
  -- descarca gestiune: la factura de vanzare lista de preturi s-au pus 2 ramuri
  -- tip = 1 daca exista cantitate retur < 0 si lista articole retur din rulaj + tip = 2,3
  -- tip = 2,3 daca nu exista cantitate retur < 0 + lista articole retur din rulaj
  -- pentru ca la conpress group statea sa parcurga tot rulajul de la tip = 1, chiar daca v_cante nu era < 0

  -- 16.06.2015
  -- marius.mutu
  -- modifica_date_factura
  -- + id_facturare,listare_detaliata

  -- 01.02.2016
  -- marius.mutu
  -- cumuleaza_note_act
  -- sterg inregistrarile cu suma 0 doar daca am inregistrari cu suma <> 0
  -- altfel nu se inregistreaza nimic in Reg. Jurnal ACT, doar in RUL si VANZARI/VANZARI_DETALII

  -- getAnaliticK: tratare caz cota TVA 1.24, 1.20, 1.19

  -- 18.03.2016
  -- marius.mutu
  -- descarca_gestiune: PROC_TVAV_ORIG se ia din STOC daca este gestiune mf la pret de vz sau stoc.proc_tvav <> 0
  -- in loc sa se calculeze pack_sesiune.tva2proctva

  -- 14.07.2016
  -- marius.mutu
  -- modifica_date_factura - adaugare parametru V_TEXT_ADITIONAL

  -- 08.12.2016
  -- marius.mutu
  -- cursor_avize - factura din aviz. se iau si cantitatile negative, nu numai cele pozitive (avize de retur)

  -- 12.12.2016
  -- marius.mutu
  -- scrie_factura_avize - daca articolele aveau discount unitar si nu era evidentiat distinct contabil, valoarea 4111 = 418 era (pret diminuat cu discount) - discount
  -- am facut discountul = 0 pe ramura cu discount neevidentiat in notele contabile, dupa ce l-am extras din pret.

  -- 03.02.2017
  -- marius.mutu
  -- cursor_preturi - la factura in valuta nu aducea articole discount 667. nu stiu de ce. la facturile in lei nu era restrictia.

  -- 03.02.2017
  -- marius.mutu
  -- cursor_preturi - aviz de expeditie - se afiseaza si articole negestionabile

  -- 20.03.2017
  -- marius.mutu
  -- denormalizare vanzari - completare date redundante pentru a nu se mai selecta de fiecare data pentru fact_vfacturi
  -- scrie_incasari - se completeaza pack_Facturi.ntip_doc_incasare CHITANTA/BON FISCAL
  -- scrie_in_vanzari - completare vanzari cu sume calculate din vanzari_detalii si act pentru a nu se mai calcula la fiecare selectie din fact_vfacturi
  -- scrie_corespondente_vanzari - completare vanzari.avize cu seria/nr avize facturate pentru a nu se mai selecta din fact_vdetalii_avize

  -- 11.10.2017
  -- marius.mutu
  -- pack_facturare.getAnaliticK - se folosesc optiuni noi pentru fiecare cota TVA

  -- 27.10.2017
  -- marius.mutu
  -- pack_facturare.descarca_gestiune - vanzare de articole fara stoc - analiticul se completeaza conform configurarilor

  -- 16.03.2018
  -- marius.mutu
  -- scrie_factura2 - tratare aviz retur tip = 24

  -- 05.02.2019
  -- marius.mutu
  -- scrie_in_vanzari, finalizeaza_avize_lucrare: corectate vanzari.total_fara_tva si total_cu_tva pe ramura cu seturi

  -- 21.03.2019
  -- marius.mutu
  -- citeste_setari_document: adaugat FACTURA ROAACNPRO 51

  -- 10.04.2019
  -- marius.mutu
  -- adauga_articol_factura_stoc, adauga_articol_factura_deviz - completare VANZARI_DETALII_TEMP.ID_TEMP CU 0 (NOT NULL)
  -- scrie_in_vanzari tip = 51 factura ACN

  -- 09.08.2019
  -- marius.mutu
  -- adauga_articol_factura_deviz : V_ID_CTR
  -- scrie_in_vanzari: VANZARI_DETALII_TEMP.ID_JTVA_COLOANA_EX

  -- 28.08.2019
  -- marius.mutu
  -- finalizeaza_factura - completez id_fact
  -- scrie_in_vanzari - completeaza data_scad, id_ctr
  -- scrie_in_vanzari - completeaza id_valuta, curs, multiplicator. nu stiu de ce nu se completau

  -- 02.09.2019
  -- marius.mutu
  -- scrie_in_vanzari - completeaza vanzari.valval, tvaval, totval

  -- 29.10.2019
  -- marius.mutu
  -- sterge_factura pack_acn.sterge_vanzare pentru tip = 51 (ROAACNPRO)
  -- initializeaza_date_factura, scrie_in_vanzari - completare vanzari.tip_factura, id_beneficiar

  -- 06.11.2019
  -- marius.mutu
  -- scrie_nota, scrie_tva, verifica_totaluri - tratare cont 709 (rabat), la fel ca si 667 (discount)

  -- 03.07.2020
  -- marius.mutu
  -- initializeaza_date_factura se initializeaza cu NULL   ntip_doc_incasare, nsuma_incasare,  clistaid_avize
  -- ramaneau completate pe facturile urmatoare de la o factura anterioara

  -- 28.01.2021
  -- marius.mutu
  -- MODIFICARE PACK_FACTURARE.SCRIE_PROFORMA, STERGE_PROFORMA sa scrie in VANZARI
  -- + PACK_FACTURARE.cursor_retur_document = PACK_FACTURARE.cursor_retur + parametrul eproforma

  -- 26.03.2021
  -- marius.mutu
  -- cursor_retur_document + parametru v_copiere, pentru atributul gestionabil

  -- 05.07.2021
  -- marius.mutu
  -- scrie_in_vanzari - text aditional repl chr(170) cu chr(13) + chr(10) - bug da eroare la CHR(13) + chr(10)

  -- 27.07.2021
  -- marius.mutu
  -- cursor_retur_document - completat cu coloane din vanzari_detalii

  -- 22.11.2021
  -- marius.mutu
  -- scrie_incasare2 - INCASARE CARD 5125 IN LOC DE 5312
  -- citeste_setari_document

  -- 26.11.2021
  -- marius.mutu
  -- scrie_incasari - initializare ntip_doc_incasare cu ultimul tip de incasare folosit

  -- 04.03.2022
  -- scrie_tva - explicatia in registrul jurnal fara 'TVA '

  -- 22.03.2022
  -- ADAUGA_ARTICOL_FACTURA, ADAUGA_ARTICOL_FACTURA_DEVIZ, ADAUGA_ARTICOL_FACTURA_STOC, adauga_rata_factura, cumuleaza_note_act + V_TAXCODE (COD TAXA SAFT)
  -- incasari - SAFT ACT.paymentcode

  -- 27.03.2022
  -- ADAUGA_ARTICOL_FACTURA, ADAUGA_ARTICOL_FACTURA_DEVIZ, ADAUGA_ARTICOL_FACTURA_STOC, adauga_rata_factura, TAXCODE NULL

  -- 09.06.2022
  -- SCRIE_TVA: COMPLETEAZA TAXCODE PE LINIA DE TVA

  -- 24.06.2022
  -- modifica_date_factura - COMPLETARE VANZARI.TIP_SAFT

  -- 27.01.2023
  -- scrie_in_vanzari corectare calcul valori in lei, daca factura este in valuta, total_ron = sum(pret_ron * cantitatea), in loc de total_valuta * curs

  -- 08.02.2023
  -- modifica_date_factura + efactura

  -- 22.02.2023
  -- scrie_in_vanzari corectare calcul valori in lei, daca factura este in valuta sau invoice, total_ron = sum(pret_ron * cantitatea), in loc de total_valuta * curs

  -- 08.05.2023
  -- verifica_total_document - completare TAXCODE SAFT
  -- scrie_nota - tratare 512x - la incasarea cu cardul 5125 se aduna si suma de plata la total, apoi se scadea la verifica_total_document

  -- 22.05.2023
  -- modifica_explicatie_articol - completare TAXCODE SAFT

  -- 13.06.2023
  -- scriere ID_JTVA_COLOANA, SAFT.TAXCODE in RUL
  -- descarca_gestiune + ID_JTVA_COLOANA, TAXCODE
  -- facturare_articole2, contabilizeaza_articol > descarca_gestiune

  -- finalizeaza_factura - corectie vanzari.id_Fact daca factura este incasata

  -- 09.10.2023
  -- + getSoldClient

  -- 09.11.2023
  -- verifica_total_document - se exclud notele 512x = 4111 din valoarea facturii

  -- 17.11.2023
  -- adaugat tipul 52 factura fiscala in valuta pe contract
  -- scrie_factura2

  -- 22.11.2023
  -- getSoldClient - nu mai scad soldul 419. Presupun ca pentru avansuri 419 se emit facturi de avans 4111 si se storneaza avansul 419 pe factura finala

  -- 10.12.2023
  -- descarca_gestiune - 345: pun 711 = 345 valoare de achizitie, in loc de valoarea de vanzare
  -- descarca_gestiune - 345: am pus 348 = 345, in loc de 348 = 711
  -- descarca_gestiune - 345: Se inregistreaza 345 = 348, 348 = 345 doar daca lnDescarcare345FaraAdaos = 0

  -- 21.12.2023 scrie_nota - scrie_discount se completeaza taxcode pe linia 667 = 4111

  -- 15.01.2024
  -- scrie_discount - corectie note 667 = 4111 pentru bonuri fiscale magazine, tip 43, scria 667 = 418

  -- 23.04.2024
  -- GetAnaliticByGrupUtilizatori: se tine cont de inregistrarile sterse

  -- 04.06.2024
  -- cursor_gestiuni_articol, adauga_articol_factura, adauga_articol_factura_stoc, adauga_articol_factura_deviz, scrie_in_vanzari + LOT

  -- 18.10.2024
  -- initializeaza_date_factura.tip_saft

  -- 23.06.2025
  -- pack_facturare.modifica_date_factura - se pot modifica data_act, data_scad, numar_Act, serie_act

  -- 17.09.2025
  -- contabilizeaza_articol - limitare explicatie la 100 caractere
  -- scrie_corespondente_vanzari - VANZARI.AVIZE maxim 1000 caractere

  PROCEDURE verificare_setari_program(V_ID_UTIL IN NUMBER,
                                      V_MESAJ   OUT VARCHAR2) IS
    V_NR_INREGISTRARI NUMBER(5);
    V_ID_TIPENTITATE  SERII_TIPENTITATI.ID_TIPENTITATE%TYPE;
    V_LUNA            DATE;
  BEGIN
    V_MESAJ := NULL;
    V_LUNA  := to_date(pack_sesiune.getan() || pack_sesiune.getluna(),
                       'YYYYMM');

    -- verificare drepturi politici de preturi
    SELECT COUNT(ID_POLITICA)
      INTO V_NR_INREGISTRARI
      FROM UTILIZATORI_ROL_INTERN A
      LEFT JOIN POLITICI_GRUPURI B
        ON A.ID_GRUP = B.ID_GRUP
     WHERE A.STERS = 0
       AND B.STERS = 0
       AND A.ID_UTIL = V_ID_UTIL;

    IF V_NR_INREGISTRARI = 0 THEN
      V_MESAJ := 'Nu aveti drepturi pe politicile de preturi! (ROAPRETURI)';
    END IF;
    --
    -- verificare setari bonuri
    SELECT ID_TIPENTITATE
      INTO V_ID_TIPENTITATE
      FROM SERII_TIPDOC
     WHERE ID_TIPDOC = pack_facturare.nid_tipbon;

    CASE
      WHEN V_ID_TIPENTITATE = 1 THEN
        -- pe gestiuni
        SELECT COUNT(B.ID_GESTIUNE)
          INTO V_NR_INREGISTRARI
          FROM GEST_CORESP_UTIL_GRUPE A
          LEFT JOIN GEST_CORESP_GRUPE_GESTIUNI B
            ON A.ID_GRUPE = B.ID_GRUPE
         WHERE A.STERS = 0
           AND B.STERS = 0
           AND A.ID_UTIL = V_ID_UTIL
           AND B.ID_GESTIUNE IN
               (SELECT ID_ENTITATE
                  FROM PLAJE_NUMERE
                 WHERE STERS = 0
                   AND INACTIV = 0
                   AND ID_TIPDOC = pack_facturare.nid_tipbon
                   AND ID_TIPENTITATE = V_ID_TIPENTITATE
                   AND V_LUNA BETWEEN DATAI AND DATAS);

        IF V_NR_INREGISTRARI = 0 THEN
          V_MESAJ := V_MESAJ || CHR(13) || CHR(10) ||
                     'Nu aveti drepturi pe gestiunile pentru care sunt configurate seriile de bonuri! (ROAGEST)';
        END IF;

      WHEN V_ID_TIPENTITATE = 2 THEN
        -- o singura serie
        V_MESAJ := V_MESAJ;

      WHEN V_ID_TIPENTITATE = 3 THEN
        -- pe gestionar
        SELECT COUNT(ID_PLAJA)
          INTO V_NR_INREGISTRARI
          FROM PLAJE_NUMERE
         WHERE STERS = 0
           AND INACTIV = 0
           AND ID_TIPDOC = pack_facturare.nid_tipbon
           AND ID_TIPENTITATE = V_ID_TIPENTITATE
           AND V_LUNA BETWEEN DATAI AND DATAS;

        IF V_NR_INREGISTRARI = 0 THEN
          V_MESAJ := V_MESAJ || CHR(13) || CHR(10) ||
                     'Nu aveti setata plaja de numere pentru bonuri! (ROAGEST)';
        END IF;

      ELSE
        V_MESAJ := V_MESAJ || CHR(13) || CHR(10) ||
                   'Setare incorecta pentru seriile de bonuri! (ROAGEST)';
    END CASE;

    -- verificare drepturi gestiuni
    SELECT COUNT(ID_GESTIUNE)
      INTO V_NR_INREGISTRARI
      FROM GEST_CORESP_UTIL_GRUPE A
      LEFT JOIN GEST_CORESP_GRUPE_GESTIUNI B
        ON A.ID_GRUPE = B.ID_GRUPE
     WHERE A.STERS = 0
       AND B.STERS = 0
       AND A.ID_UTIL = V_ID_UTIL;

    IF V_NR_INREGISTRARI = 0 THEN
      V_MESAJ := V_MESAJ || CHR(13) || CHR(10) ||
                 'Nu aveti drepturi pe gestiuni! (ROAGEST)';
    END IF;

    -- verificare curs valutar
    SELECT COUNT(ID_CURS)
      INTO V_NR_INREGISTRARI
      FROM CURS A
     WHERE A.STERS = 0
       AND A.DATA <= TRUNC(SYSDATE)
       AND A.DATA2 >= TRUNC(SYSDATE);

    IF V_NR_INREGISTRARI = 0 THEN
      V_MESAJ := V_MESAJ || CHR(13) || CHR(10) ||
                 'Nu este setat cursul valutar pentru ziua de azi!';
    END IF;

    IF V_MESAJ IS NOT NULL THEN
      V_MESAJ := V_MESAJ || CHR(13) || CHR(10) || CHR(13) || CHR(10) ||
                 'Faceti setarile necesare! ';
    END IF;

  END verificare_setari_program;
  -------------------------------------------------------------------
  PROCEDURE initializeaza_facturare(V_ID_UTIL IN NUMBER) IS
    V_DENUMIRE_POL CRM_POLITICI_PRETURI.NUME_LISTA_PRETURI%TYPE;
  BEGIN
    pack_facturare.initializeaza_moneda_nationala();
    /*    pack_facturare.nzecimale_pretval := PACK_SESIUNE.getoptiunefirma(USER,
                                                                     'PVAL');
    pack_facturare.nzecimale_sumaval := pack_facturare.nzecimale_pretval;
    pack_facturare.nzecimale_preta   := PACK_SESIUNE.getoptiunefirma(USER,
                                                                     'PPRET');
    pack_facturare.nzecimale_sumaa   := PACK_SESIUNE.getoptiunefirma(USER,
                                                                     'PC');
    pack_facturare.nzecimale_pretv   := PACK_SESIUNE.getoptiunefirma(USER,
                                                                     'PPRETV');
    pack_facturare.nzecimale_sumav   := pack_facturare.nzecimale_sumaa;*/
    pack_facturare.nzecimale_procent := 3;
    pack_facturare.citeste_setari_pol_pret(1,
                                           V_ID_UTIL,
                                           pack_facturare.nid_politica_stoc,
                                           V_DENUMIRE_POL);
  END initializeaza_facturare;
  -------------------------------------------------------------------
  PROCEDURE initializeaza_seturi_temp IS
  begin
    DELETE FROM VANZARI_SETURI_TEMP;
  end initializeaza_seturi_temp;
  -------------------------------------------------------------------
  PROCEDURE initializeaza_date_factura(V_DATA_IREG      IN DATE,
                                       V_ID_FDOC        IN NUMBER,
                                       V_DATA_ACT       IN DATE,
                                       V_DATA_SCADENTA  IN DATE,
                                       V_SERIE_FACTURA  IN VARCHAR2,
                                       V_NUMAR_ACT      IN NUMBER,
                                       V_ID_CLIENT      IN NUMBER,
                                       V_ID_LUCRARE     IN NUMBER,
                                       V_ID_SECTIE      IN NUMBER,
                                       V_ID_VENCHELT    IN NUMBER,
                                       V_ID_RESPONSABIL IN NUMBER,
                                       V_LISTAID        IN VARCHAR2,
                                       V_DESCRIERE      IN VARCHAR2,
                                       V_TIP            IN NUMBER,
                                       V_ID_SET         IN NUMBER,
                                       V_DATA_CURS      IN DATE,
                                       V_ID_VALUTA      IN NUMBER,
                                       V_IN_VALUTA      IN NUMBER,
                                       V_TVA_INCASARE   IN NUMBER,
                                       V_ID_SUCURSALA   IN NUMBER,
                                       V_ID_UTIL        IN NUMBER) IS
  begin
    -- pentru compatibilitate cu restul aplicatiilor care apeleaza fara v_explicatia4, v_id_ordl
    pack_facturare.initializeaza_date_factura(V_DATA_IREG,
                                              V_ID_FDOC,
                                              V_DATA_ACT,
                                              V_DATA_SCADENTA,
                                              V_SERIE_FACTURA,
                                              V_NUMAR_ACT,
                                              V_ID_CLIENT,
                                              V_ID_LUCRARE,
                                              V_ID_SECTIE,
                                              V_ID_VENCHELT,
                                              V_ID_RESPONSABIL,
                                              NULL,
                                              NULL,
                                              V_LISTAID,
                                              V_DESCRIERE,
                                              V_TIP,
                                              V_ID_SET,
                                              V_DATA_CURS,
                                              V_ID_VALUTA,
                                              V_IN_VALUTA,
                                              V_TVA_INCASARE,
                                              V_ID_SUCURSALA,
                                              V_ID_UTIL,
                                              NULL,
                                              NULL,
                                              NULL);
  end initializeaza_date_factura;
  -------------------------------------------------------------------
  PROCEDURE initializeaza_date_factura(V_DATA_IREG      IN DATE,
                                       V_ID_FDOC        IN NUMBER,
                                       V_DATA_ACT       IN DATE,
                                       V_DATA_SCADENTA  IN DATE,
                                       V_SERIE_FACTURA  IN VARCHAR2,
                                       V_NUMAR_ACT      IN NUMBER,
                                       V_ID_CLIENT      IN NUMBER,
                                       V_ID_LUCRARE     IN NUMBER,
                                       V_ID_SECTIE      IN NUMBER,
                                       V_ID_VENCHELT    IN NUMBER,
                                       V_ID_RESPONSABIL IN NUMBER,
                                       V_EXPLICATIA4    IN VARCHAR2,
                                       V_LISTAID        IN VARCHAR2,
                                       V_DESCRIERE      IN VARCHAR2,
                                       V_TIP            IN NUMBER,
                                       V_ID_SET         IN NUMBER,
                                       V_DATA_CURS      IN DATE,
                                       V_ID_VALUTA      IN NUMBER,
                                       V_IN_VALUTA      IN NUMBER,
                                       V_TVA_INCASARE   IN NUMBER,
                                       V_ID_SUCURSALA   IN NUMBER,
                                       V_ID_UTIL        IN NUMBER) IS
  BEGIN
    -- pentru compatibilitate cu restul aplicatiilor care apeleaza fara v_id_ordl
    pack_facturare.initializeaza_date_factura(V_DATA_IREG,
                                              V_ID_FDOC,
                                              V_DATA_ACT,
                                              V_DATA_SCADENTA,
                                              V_SERIE_FACTURA,
                                              V_NUMAR_ACT,
                                              V_ID_CLIENT,
                                              V_ID_LUCRARE,
                                              V_ID_SECTIE,
                                              V_ID_VENCHELT,
                                              V_ID_RESPONSABIL,
                                              V_EXPLICATIA4,
                                              NULL,
                                              V_LISTAID,
                                              V_DESCRIERE,
                                              V_TIP,
                                              V_ID_SET,
                                              V_DATA_CURS,
                                              V_ID_VALUTA,
                                              V_IN_VALUTA,
                                              V_TVA_INCASARE,
                                              V_ID_SUCURSALA,
                                              V_ID_UTIL,
                                              NULL,
                                              NULL,
                                              NULL);
  END initializeaza_date_factura;

  -------------------------------------------------------------------
  PROCEDURE initializeaza_date_factura(V_DATA_IREG      IN DATE,
                                       V_ID_FDOC        IN NUMBER,
                                       V_DATA_ACT       IN DATE,
                                       V_DATA_SCADENTA  IN DATE,
                                       V_SERIE_FACTURA  IN VARCHAR2,
                                       V_NUMAR_ACT      IN NUMBER,
                                       V_ID_CLIENT      IN NUMBER,
                                       V_ID_LUCRARE     IN NUMBER,
                                       V_ID_SECTIE      IN NUMBER,
                                       V_ID_VENCHELT    IN NUMBER,
                                       V_ID_RESPONSABIL IN NUMBER,
                                       V_EXPLICATIA4    IN VARCHAR2,
                                       V_ID_ORDL        IN NUMBER,
                                       V_LISTAID        IN VARCHAR2,
                                       V_DESCRIERE      IN VARCHAR2,
                                       V_TIP            IN NUMBER,
                                       V_ID_SET         IN NUMBER,
                                       V_DATA_CURS      IN DATE,
                                       V_ID_VALUTA      IN NUMBER,
                                       V_IN_VALUTA      IN NUMBER,
                                       V_TVA_INCASARE   IN NUMBER,
                                       V_ID_SUCURSALA   IN NUMBER,
                                       V_ID_UTIL        IN NUMBER) IS
  BEGIN
    -- pentru compatibilitate cu restul aplicatiilor care apeleaza fara v_tip_factura, v_id_beneficiar
    pack_facturare.initializeaza_date_factura(V_DATA_IREG,
                                              V_ID_FDOC,
                                              V_DATA_ACT,
                                              V_DATA_SCADENTA,
                                              V_SERIE_FACTURA,
                                              V_NUMAR_ACT,
                                              V_ID_CLIENT,
                                              V_ID_LUCRARE,
                                              V_ID_SECTIE,
                                              V_ID_VENCHELT,
                                              V_ID_RESPONSABIL,
                                              V_EXPLICATIA4,
                                              V_ID_ORDL,
                                              V_LISTAID,
                                              V_DESCRIERE,
                                              V_TIP,
                                              V_ID_SET,
                                              V_DATA_CURS,
                                              V_ID_VALUTA,
                                              V_IN_VALUTA,
                                              V_TVA_INCASARE,
                                              V_ID_SUCURSALA,
                                              V_ID_UTIL,
                                              NULL,
                                              NULL,
                                              NULL);
  END initializeaza_date_factura;

  -------------------------------------------------------------------
  PROCEDURE initializeaza_date_factura(V_DATA_IREG      IN DATE,
                                       V_ID_FDOC        IN NUMBER,
                                       V_DATA_ACT       IN DATE,
                                       V_DATA_SCADENTA  IN DATE,
                                       V_SERIE_FACTURA  IN VARCHAR2,
                                       V_NUMAR_ACT      IN NUMBER,
                                       V_ID_CLIENT      IN NUMBER,
                                       V_ID_LUCRARE     IN NUMBER,
                                       V_ID_SECTIE      IN NUMBER,
                                       V_ID_VENCHELT    IN NUMBER,
                                       V_ID_RESPONSABIL IN NUMBER,
                                       V_EXPLICATIA4    IN VARCHAR2,
                                       V_ID_ORDL        IN NUMBER,
                                       V_LISTAID        IN VARCHAR2,
                                       V_DESCRIERE      IN VARCHAR2,
                                       V_TIP            IN NUMBER,
                                       V_ID_SET         IN NUMBER,
                                       V_DATA_CURS      IN DATE,
                                       V_ID_VALUTA      IN NUMBER,
                                       V_IN_VALUTA      IN NUMBER,
                                       V_TVA_INCASARE   IN NUMBER,
                                       V_ID_SUCURSALA   IN NUMBER,
                                       V_ID_UTIL        IN NUMBER,
                                       V_TIP_FACTURA    IN NUMBER,
                                       V_ID_BENEFICIAR  IN NUMBER,
                                       V_TIP_SAFT       IN NUMBER DEFAULT 380) IS
  BEGIN
    DELETE FROM VANZARI_DETALII_TEMP;
    pack_facturare.nid_act  := 0;
    pack_facturare.ntotftva := 0;
    pack_facturare.ntottva  := 0;
    pack_facturare.ndifftva := 0;
    pack_facturare.ndiftva  := 0;
    pack_facturare.nid_fact := -1;

    pack_facturare.nfactavizcust := 0;
    pack_facturare.nid_util      := V_ID_UTIL;
    pack_facturare.nid_sucursala := V_ID_SUCURSALA;

    pack_facturare.initializeaza_facturare(pack_facturare.nid_util);
    pack_facturare.initializeaza_seturi_temp();

    v_date_gestiune.id_gestiune := NULL;
    v_date_gestiune.id_tip_gest := NULL;
    v_date_gestiune.cont        := NULL;
    v_date_gestiune.acont       := NULL;

    pack_facturare.ndiscount_evidentiat := PACK_SESIUNE.getoptiunefirma(USER,
                                                                        'DISCOUNTEVIDENTIAT');
    pack_facturare.cascd                := NULL;
    pack_facturare.cascc                := NULL;
    pack_facturare.nid_jtva_coloana     := NULL;
    pack_facturare.nTaxCode             := NULL;
    pack_facturare.ddata_curs           := V_DATA_CURS;
    pack_facturare.ddata_ireg           := V_DATA_IREG;
    pack_facturare.nid_fdoc             := V_ID_FDOC;
    pack_facturare.ddata_act            := V_DATA_ACT;
    pack_facturare.ddata_scadenta       := V_DATA_SCADENTA;
    pack_facturare.cserie_act           := TRIM(V_SERIE_FACTURA);
    pack_facturare.nnumar_act           := V_NUMAR_ACT;
    IF V_TIP IN (23, 25, 27, 30, 41, -6) THEN
      pack_facturare.nid_gestiune_sursa := V_ID_CLIENT;
      pack_facturare.nid_part           := 0;
    ELSE
      pack_facturare.nid_gestiune_sursa := 0;
      pack_facturare.nid_part           := V_ID_CLIENT;
    END IF;
    pack_facturare.nid_lucrare         := nvl(V_ID_LUCRARE, 0);
    pack_facturare.nid_venchelt        := V_ID_VENCHELT;
    pack_facturare.nid_sectie_stoc     := V_ID_SECTIE;
    pack_facturare.nid_responsabil     := V_ID_RESPONSABIL;
    pack_facturare.nid_ordl            := V_ID_ORDL;
    pack_facturare.cexplicatia4        := V_EXPLICATIA4;
    pack_facturare.nid_set             := V_ID_SET;
    pack_facturare.ntip                := V_TIP;
    pack_facturare.clistaid            := V_LISTAID;
    pack_facturare.cdescriere          := V_DESCRIERE;
    pack_facturare.nproc_tva_max       := -1;
    pack_facturare.clista_cursuri      := NULL;
    pack_facturare.nperechec           := 0;
    pack_facturare.nid_factc           := 0;
    pack_facturare.nid_partc           := 0;
    pack_facturare.nan                 := EXTRACT(YEAR FROM V_DATA_IREG);
    pack_facturare.nluna               := EXTRACT(MONTH FROM V_DATA_IREG);
    pack_facturare.ntva_incasare       := V_TVA_INCASARE;
    pack_facturare.cserie_act_incasare := NULL;
    pack_facturare.nnumar_act_incasare := NULL;

    pack_facturare.ntip_doc_incasare := NULL;
    pack_facturare.nsuma_incasare    := NULL;
    pack_facturare.clistaid_avize    := NULL;

    pack_facturare.verifica_analitice378();

    pack_facturare.nin_valuta := V_IN_VALUTA;
    pack_facturare.nid_valuta := V_ID_VALUTA;

    IF pack_facturare.ntip IN (3, 21, 28, 42, 47) THEN
      -- facturare de pe comanda
      -- aviz de pe comanda
      pack_facturare.nid_comanda := to_number(pack_facturare.clistaid);
    ELSE
      pack_facturare.nid_comanda := NULL;
    END IF;

    pack_facturare.ntip_factura   := V_TIP_FACTURA;
    pack_facturare.nid_beneficiar := V_ID_BENEFICIAR;
    pack_facturare.ntip_saft      := V_TIP_SAFT;

  END initializeaza_date_factura;
  -------------------------------------------------------------------
  PROCEDURE initializeaza_setari_facturare IS
  BEGIN
    BEGIN
      SELECT TO_NUMBER(NVL(VARVALUE, '0'))
        INTO pack_facturare.nscadere_stoc
        FROM OPTIUNI
       WHERE PROGRAM = pack_facturare.cnume_program
         AND VARNAME = 'SCADERESTOC';

      IF pack_facturare.nscadere_stoc = 1 AND pack_facturare.ntip <> 4 THEN
        SELECT MAX(NVL(IN_STOC, 0))
          INTO pack_facturare.nscadere_stoc
          FROM VANZARI_DETALII_TEMP
         WHERE ID_RATA IS NULL;

        IF pack_facturare.nscadere_stoc = 1 THEN
          pack_serii_numere.aloca_numar(pack_facturare.nid_tipbon,
                                        NULL,
                                        pack_facturare.nid_gestiune_sursa,
                                        pack_facturare.nid_util,
                                        pack_facturare.nid_sucursala,
                                        pack_facturare.nnumar_bon);
        END IF;
      END IF;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        pack_facturare.nscadere_stoc := 0;
    END;
  END initializeaza_setari_facturare;
  -------------------------------------------------------------------
  PROCEDURE initializeaza_date_gestiune(V_ID_GESTIUNE IN NUMBER,
                                        V_ID_TIPGEST  IN NUMBER,
                                        V_CONT        IN VARCHAR2,
                                        V_ACONT       IN VARCHAR2) IS
  BEGIN
    v_date_gestiune.id_gestiune := V_ID_GESTIUNE;
    v_date_gestiune.id_tip_gest := V_ID_TIPGEST;
    v_date_gestiune.cont        := V_CONT;
    v_date_gestiune.acont       := V_ACONT;
  END initializeaza_date_gestiune;
  -------------------------------------------------------------------
  PROCEDURE citeste_setari_document(V_TIP     IN NUMBER,
                                    V_ID_FDOC OUT NUMBER,
                                    V_FDOC    OUT VARCHAR2) IS
    V_VARNAME OPTIUNI.VARNAME%TYPE;
  BEGIN
    CASE
      WHEN V_TIP BETWEEN 1 AND 20 OR V_TIP BETWEEN - 4 AND - 1 OR
           V_TIP IN (-8, -11, -12, 45, 48, 49, 50, 51, 52) THEN
        -- id_fdoc_factura
        V_VARNAME := 'ID_FDOC_FACT';
      WHEN V_TIP IN (24, 41, -6, -7, -13) THEN
        -- id_fdoc_aviz_retur
        V_VARNAME := 'ID_FDOC_AVIZ_RETUR';
      WHEN V_TIP BETWEEN 21 AND 40 OR V_TIP in (47, 42, -9, -10) THEN
        -- id_fdoc_aviz
        V_VARNAME := 'ID_FDOC_AVIZ';
      WHEN V_TIP = 0 THEN
        -- id_fdoc_compensare
        V_VARNAME := 'ID_FDOC_COMPENSARE';
      WHEN V_TIP = -100 THEN
        -- id_fdoc_chitanta
        V_VARNAME := 'ID_FDOC_CHITANTA';
      WHEN V_TIP = -101 THEN
        -- id_fdoc_bonfiscal
        V_VARNAME := 'ID_FDOC_BONFISCAL';
      WHEN V_TIP = -102 THEN
        -- id_fdoc_proforma
        V_VARNAME := 'ID_FDOC_PROFORMA';
      WHEN V_TIP = -103 THEN
        -- id_fdoc_card
        V_VARNAME := 'ID_FDOC_CARD';
      ELSE
        -- id_fdoc_factura
        V_VARNAME := 'ID_FDOC_FACT';
    END CASE;

    BEGIN
      SELECT TO_NUMBER(A.VARVALUE), B.FEL_DOCUMENT
        INTO V_ID_FDOC, V_FDOC
        FROM OPTIUNI A
        LEFT JOIN NOM_FDOC B
          ON TO_NUMBER(A.VARVALUE) = B.ID_FDOC
       WHERE A.VARNAME = V_VARNAME
         AND (A.PROGRAM = pack_facturare.cnume_program OR
             A.PROGRAME LIKE '%' || pack_facturare.cnume_program || '%')
         AND B.STERS = 0;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_ID_FDOC := NULL;
        V_FDOC    := '';
    END;
  END citeste_setari_document;
  -------------------------------------------------------------------
  PROCEDURE citeste_setari_partener(V_TIP      IN NUMBER,
                                    V_ID_PART  OUT NUMBER,
                                    V_DENUMIRE OUT VARCHAR2) IS
    V_VARNAME OPTIUNI.VARNAME%TYPE;
  BEGIN
    CASE
      WHEN V_TIP = 0 THEN
        -- id_part_banca_comp
        V_VARNAME := 'ID_PART_BANCA_COMP';
      WHEN V_TIP = 1 THEN
        V_VARNAME := 'ID_PART_CASA';
    END CASE;

    BEGIN
      SELECT TO_NUMBER(A.VARVALUE), B.DENUMIRE
        INTO V_ID_PART, V_DENUMIRE
        FROM OPTIUNI A
        LEFT JOIN NOM_PARTENERI B
          ON TO_NUMBER(A.VARVALUE) = B.ID_PART
       WHERE A.PROGRAM = pack_facturare.cnume_program
         AND A.VARNAME = V_VARNAME
         AND B.STERS = 0;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_ID_PART  := NULL;
        V_DENUMIRE := '';
    END;
  END citeste_setari_partener;
  -------------------------------------------------------------------
  PROCEDURE citeste_setari_pol_pret(V_TIP      IN NUMBER,
                                    V_ID_UTIL  IN NUMBER,
                                    V_ID_POL   OUT NUMBER,
                                    V_DENUMIRE OUT VARCHAR2) IS
  BEGIN
    BEGIN
      CASE
        WHEN V_TIP IN (23, 30, 41) THEN
          SELECT TO_NUMBER(A.VARVALUE), B.NUME
            INTO V_ID_POL, V_DENUMIRE
            FROM OPTIUNI A
            LEFT JOIN CRM_VPOLPRETCURUTIL B
              ON TO_NUMBER(A.VARVALUE) = B.ID_POL
           WHERE A.PROGRAM = pack_facturare.cnume_program
             AND A.VARNAME = 'ID_POL_PRET_TR'
             AND B.ID_UTIL = V_ID_UTIL;
        WHEN V_TIP = 1 THEN
          SELECT TO_NUMBER(A.VARVALUE), B.NUME
            INTO V_ID_POL, V_DENUMIRE
            FROM OPTIUNI A
            LEFT JOIN CRM_VPOLPRETCURUTIL B
              ON TO_NUMBER(A.VARVALUE) = B.ID_POL
           WHERE A.PROGRAM = pack_facturare.cnume_program
             AND A.VARNAME = 'ID_POL_PRET_STOC'
             AND B.ID_UTIL = V_ID_UTIL;
        WHEN V_TIP IN (48, 49) THEN
          SELECT TO_NUMBER(A.VARVALUE), B.NUME
            INTO V_ID_POL, V_DENUMIRE
            FROM OPTIUNI A
            LEFT JOIN CRM_VPOLPRETCURUTIL B
              ON TO_NUMBER(A.VARVALUE) = B.ID_POL
           WHERE A.PROGRAM = pack_facturare.cnume_program
             AND A.VARNAME = 'IDPOLPRETFACTK'
             AND B.ID_UTIL = V_ID_UTIL;
      END CASE;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_ID_POL   := NULL;
        V_DENUMIRE := '';
    END;
  END citeste_setari_pol_pret;
  -------------------------------------------------------------------
  PROCEDURE genereaza_numar_proforma(V_NR_PROFORMA OUT NUMBER) IS
  BEGIN
    pack_serii_numere.dezaloca_numar(pack_facturare.nid_tipfactura);

    SELECT SEQ_NR_PROFORME.NEXTVAL INTO V_NR_PROFORMA FROM DUAL;

    pack_facturare.nnumar_act := V_NR_PROFORMA;
  END genereaza_numar_proforma;
  -------------------------------------------------------------------
  PROCEDURE completare_politica_stoc IS
  BEGIN
    IF pack_facturare.nid_politica_stoc IS NOT NULL THEN
      MERGE INTO CRM_POLITICI_PRET_ART A
      USING (SELECT ID_ARTICOL
               FROM NOM_ARTICOLE
              WHERE STERS = 0
                AND INACTIV = 0
                AND IN_STOC = 1) B
      ON (A.ID_POL = pack_facturare.nid_politica_stoc AND A.ID_ARTICOL = B.ID_ARTICOL)
      WHEN NOT MATCHED THEN
        INSERT
          (ID_POL, ID_ARTICOL, ID_VALUTA)
        VALUES
          (pack_facturare.nid_politica_stoc,
           B.ID_ARTICOL,
           pack_facturare.nid_moneda_nationala);

      UPDATE NOM_ARTICOLE
         SET IN_CRM = 1
       WHERE ID_ARTICOL IN
             (SELECT ID_ARTICOL
                FROM CRM_POLITICI_PRET_ART
               WHERE STERS = 0
                 AND ID_POL = pack_facturare.nid_politica_stoc)
         AND IN_CRM = 0;
    END IF;
  END completare_politica_stoc;
  -------------------------------------------------------------------
  PROCEDURE modificare_politica_stoc(V_ID_POL IN NUMBER) is
  BEGIN
    UPDATE CRM_POLITICI_PRETURI
       SET ID_VALUTA = pack_def.GetIdMonedaNationala()
     WHERE ID_POL = V_ID_POL;

    UPDATE CRM_POLITICI_PRET_ART
       SET PRET            = 0,
           PRETFTVA        = 0,
           PRETCTVA        = 0,
           PROC_TVAV       = NULL,
           DISCOUNT_UNITAR = NULL,
           ID_VALUTA       = pack_def.GetIdMonedaNationala()
     WHERE ID_POL = V_ID_POL;
  END modificare_politica_stoc;
  -------------------------------------------------------------------
  PROCEDURE cursor_preturi(V_DATA_CURS        IN DATE,
                           V_TIP              IN NUMBER,
                           V_ID_VALUTA        IN NUMBER,
                           V_ID_GESTIUNE_INIT IN NUMBER,
                           V_LUNA             IN NUMBER,
                           V_AN               IN NUMBER,
                           V_ID_UTIL          IN NUMBER,
                           V_ID_SUCURSALA     IN NUMBER,
                           V_CURSOR           OUT cursor_facturare) IS
    lnFacturareFaraStoc NUMBER(1) := 0;
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);

    pack_facturare.completare_politica_stoc();

    pack_facturare.verifica_cursuri_valute(V_DATA_CURS, V_ID_UTIL);

    -- FACTURARE LISTA PRETURI: se selecteaza toate articolele din lista de preturi, indiferent daca au stoc > 0
    lnFacturareFaraStoc := TO_NUMBER(NVL(pack_sesiune.getoptiunefirma('RF_FACTURARE_FARA_STOC'),
                                         '0'));

    CASE
      WHEN V_TIP = 45 THEN
        -- factura restaurant
        OPEN V_CURSOR FOR
          SELECT rownum as id_c,
                 B.ID_ARTICOL,
                 NULL AS LOT,
                 NULL as SERIE,
                 A.ID_POL,
                 B.ID_VALUTA,
                 A.NUME_LISTA_PRETURI,
                 (CASE
                   WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(F.CURS, 0) *
                          ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                                pack_sesiune.nzecimale_pretvval) /
                          NVL(F.MULTIPLICATOR, 1),
                          pack_sesiune.nzecimale_pretv)
                   ELSE
                    ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                          pack_sesiune.nzecimale_pretv)
                 END) AS DISCOUNT_UNITAR,
                 (CASE
                   WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                          pack_sesiune.nzecimale_pretvval)
                   ELSE
                    0
                 END) AS DISCOUNT_UNITAR_VAL,
                 C.CODMAT,
                 C.CODBARE,
                 C.DENUMIRE,
                 NVL(C.UM, '') AS UM,
                 C.IN_STOC AS GESTIONABIL,
                 1 AS CANTITATE,
                 B.PROC_TVAV,
                 A.PRETURI_CU_TVA,
                 F.CURS,
                 F.MULTIPLICATOR,
                 (CASE
                   WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(F.CURS, 0) *
                          ROUND(NVL(B.PRET, 0),
                                pack_sesiune.nzecimale_pretvval) /
                          NVL(F.MULTIPLICATOR, 1),
                          pack_sesiune.nzecimale_pretv)
                   ELSE
                    ROUND(NVL(B.PRET, 0), pack_sesiune.nzecimale_pretv)
                 END) AS PRET,
                 (CASE
                   WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(B.PRET, 0), pack_sesiune.nzecimale_pretvval)
                   ELSE
                    0
                 END) AS PRET_VAL,
                 1 - NVL(G.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
                 G.NUME_VAL,
                 DECODE(A.ID_POL,
                        NVL(pack_facturare.nid_politica_stoc, A.ID_POL + 1),
                        1,
                        0) AS MODIFICABIL,
                 A.ID_GESTIUNE,
                 '371' AS CONT
            FROM (select a1.id_util,
                         a3.id_pol,
                         a3.preturi_cu_tva,
                         a3.nume_lista_preturi,
                         a3.id_nota,
                         a6.id_gestiune
                    from utilizatori_rol_intern a1
                    left join politici_grupuri a2
                      on a1.id_grup = a2.id_grup
                    left join crm_politici_preturi a3
                      on a2.id_politica = a3.id_pol
                    left join crm_note_vanzari a4
                      on a3.id_nota = a4.id_nota
                    left join note_contabile a5
                      on a4.id_set = a5.id_set
                    left join rest_coresp_pol_gest a6
                      on a3.id_pol = a6.id_pol
                   where a1.sters = 0
                     and a2.sters = 0
                     and a3.sters = 0
                     and a1.id_util = V_ID_UTIL
                     and NVL(a5.in_valuta, 0) = 0
                     and least(NVL(last_day(to_date(V_AN || V_LUNA, 'YYYYMM')),
                                   sysdate),
                               sysdate) between a3.datai and
                         NVL(a3.datas, to_date('300012', 'YYYYMM'))
                     and NVL(a1.ID_SUCURSALA, -99) =
                         NVL(V_ID_SUCURSALA, -99)
                     and nvl(a6.id_rest_locatie, 0) =
                         NVL(V_ID_GESTIUNE_INIT, -1)) A
            LEFT JOIN CRM_POLITICI_PRET_ART B
              ON A.ID_POL = B.ID_POL
            LEFT JOIN NOM_ARTICOLE C
              ON B.ID_ARTICOL = C.ID_ARTICOL
            LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                         FROM CURS
                        WHERE DATA <= V_DATA_CURS
                          AND DATA2 >= V_DATA_CURS
                          AND STERS = 0) F
              ON B.ID_VALUTA = F.ID_VALUTA
            LEFT JOIN NOM_VALUTE G
              ON B.ID_VALUTA = G.ID_VALUTA
           ORDER BY C.DENUMIRE;
      WHEN V_TIP IN (1, 2) THEN
        -- factura in lei
        OPEN V_CURSOR FOR
          SELECT rownum as id_c,
                 B.ID_ARTICOL,
                 NULL AS LOT,
                 NULL as SERIE,
                 A.ID_POL,
                 B.ID_VALUTA,
                 A.NUME_LISTA_PRETURI,
                 (CASE
                   WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(F.CURS, 0) *
                          ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                                pack_sesiune.nzecimale_pretvval) /
                          NVL(F.MULTIPLICATOR, 1),
                          pack_sesiune.nzecimale_pretv)
                   ELSE
                    ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                          pack_sesiune.nzecimale_pretv)
                 END) AS DISCOUNT_UNITAR,
                 (CASE
                   WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                          pack_sesiune.nzecimale_pretvval)
                   ELSE
                    0
                 END) AS DISCOUNT_UNITAR_VAL,
                 C.CODMAT,
                 C.CODBARE,
                 C.DENUMIRE,
                 NVL(C.UM, '') AS UM,
                 C.IN_STOC AS GESTIONABIL,
                 (CASE
                   WHEN lnFacturareFaraStoc = 1 AND E.CANTITATE < 0 THEN
                    0
                   ELSE
                    NVL(E.CANTITATE, 0)
                 END) AS CANTITATE,
                 B.PROC_TVAV,
                 A.PRETURI_CU_TVA,
                 F.CURS,
                 F.MULTIPLICATOR,
                 (CASE
                   WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(F.CURS, 0) *
                          ROUND(NVL(B.PRET, 0),
                                pack_sesiune.nzecimale_pretvval) /
                          NVL(F.MULTIPLICATOR, 1),
                          pack_sesiune.nzecimale_pretv)
                   ELSE
                    ROUND(NVL(B.PRET, 0), pack_sesiune.nzecimale_pretv)
                 END) AS PRET,
                 (CASE
                   WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(B.PRET, 0), pack_sesiune.nzecimale_pretvval)
                   ELSE
                    0
                 END) AS PRET_VAL,
                 1 - NVL(G.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
                 G.NUME_VAL,
                 DECODE(A.ID_POL,
                        NVL(pack_facturare.nid_politica_stoc, A.ID_POL + 1),
                        1,
                        0) AS MODIFICABIL
            FROM (select a1.id_util,
                         a3.id_pol,
                         a3.preturi_cu_tva,
                         a3.nume_lista_preturi,
                         a3.id_nota
                    from utilizatori_rol_intern a1
                    left join politici_grupuri a2
                      on a1.id_grup = a2.id_grup
                    left join crm_politici_preturi a3
                      on a2.id_politica = a3.id_pol
                    left join crm_note_vanzari a4
                      on a3.id_nota = a4.id_nota
                    left join note_contabile a5
                      on a4.id_set = a5.id_set
                   where a1.sters = 0
                     and a2.sters = 0
                     and a3.sters = 0
                     and a1.id_util = V_ID_UTIL
                     and NVL(a5.in_valuta, 0) = 0
                     and least(NVL(last_day(to_date(V_AN || V_LUNA, 'YYYYMM')),
                                   sysdate),
                               sysdate) between a3.datai and
                         NVL(a3.datas, to_date('300012', 'YYYYMM'))
                     and NVL(a1.ID_SUCURSALA, -99) =
                         NVL(V_ID_SUCURSALA, -99)) A
            LEFT JOIN CRM_POLITICI_PRET_ART B
              ON A.ID_POL = B.ID_POL
            LEFT JOIN NOM_ARTICOLE C
              ON B.ID_ARTICOL = C.ID_ARTICOL
            LEFT JOIN (SELECT ID_ARTICOL,
                              SUM(CANTS + CANT - CANTE) AS CANTITATE
                         FROM STOC
                        WHERE LUNA = V_LUNA
                          AND AN = V_AN
                          AND NVL(ID_SUCURSALA, -99) =
                              NVL(V_ID_SUCURSALA, -99)
                          AND ID_GESTIUNE IN
                              (SELECT DISTINCT D1.ID_GESTIUNE
                                 FROM GEST_CORESP_GRUPE_GESTIUNI D1
                                 LEFT JOIN GEST_CORESP_UTIL_GRUPE D2
                                   ON D1.ID_GRUPE = D2.ID_GRUPE
                                WHERE D1.STERS = 0
                                  AND D2.STERS = 0
                                  AND D2.ID_UTIL = V_ID_UTIL
                                  AND NVL2(V_ID_GESTIUNE_INIT,
                                           D1.ID_GESTIUNE,
                                           -1) = NVL(V_ID_GESTIUNE_INIT, -1))
                        GROUP BY ID_ARTICOL) E
              ON B.ID_ARTICOL = E.ID_ARTICOL
            LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                         FROM CURS
                        WHERE DATA <= V_DATA_CURS
                          AND DATA2 >= V_DATA_CURS
                          AND STERS = 0) F
              ON B.ID_VALUTA = F.ID_VALUTA
            LEFT JOIN NOM_VALUTE G
              ON B.ID_VALUTA = G.ID_VALUTA
           WHERE (lnFacturareFaraStoc = 1 and NVL(E.CANTITATE, 0) <= 0)
              OR DECODE(NVL(C.IN_STOC, 2), 1, NVL(E.CANTITATE, 0), 2, 0, 1) > 0
           ORDER BY C.DENUMIRE;

      WHEN V_TIP IN (5, 6, 10, 52) THEN
        -- factura in valuta
        OPEN V_CURSOR FOR
          SELECT rownum as id_c,
                 A.ID_ARTICOL,
                 NULL AS LOT,
                 NULL as SERIE,
                 A.ID_POL,
                 V_ID_VALUTA AS ID_VALUTA, -- A.ID_VALUTA
                 A.NUME_LISTA_PRETURI,
                 ROUND(NVL(D.CURS, 0) *
                       ROUND(A.DISCOUNT_UNITAR,
                             pack_sesiune.nzecimale_pretvval) /
                       NVL(D.MULTIPLICATOR, 1),
                       pack_sesiune.nzecimale_pretv) AS DISCOUNT_UNITAR,
                 ROUND(A.DISCOUNT_UNITAR, pack_sesiune.nzecimale_pretvval) AS DISCOUNT_UNITAR_VAL,
                 A.CODMAT,
                 A.CODBARE,
                 A.DENUMIRE,
                 NVL(A.UM, '') AS UM,
                 A.GESTIONABIL,
                 (CASE
                   WHEN lnFacturareFaraStoc = 1 AND C.CANTITATE < 0 THEN
                    0
                   ELSE
                    NVL(C.CANTITATE, 0)
                 END) AS CANTITATE,
                 A.PROC_TVAV,
                 A.PRETURI_CU_TVA,
                 D.CURS,
                 D.MULTIPLICATOR,
                 ROUND(NVL(D.CURS, 0) *
                       ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) /
                       NVL(D.MULTIPLICATOR, 1),
                       pack_sesiune.nzecimale_pretv) AS PRET,
                 ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) AS PRET_VAL,
                 1 AS TIP_VALUTA,
                 E.NUME_VAL
            FROM FACT_VPRETURI_UTILIZATOR A
            LEFT JOIN (SELECT ID_ARTICOL,
                              SUM(CANTS + CANT - CANTE) AS CANTITATE
                         FROM STOC
                        WHERE LUNA = V_LUNA
                          AND AN = V_AN
                          AND NVL(ID_SUCURSALA, -99) =
                              NVL(V_ID_SUCURSALA, -99)
                          AND ID_GESTIUNE IN
                              (SELECT DISTINCT A1.ID_GESTIUNE
                                 FROM GEST_CORESP_GRUPE_GESTIUNI A1
                                 LEFT JOIN GEST_CORESP_UTIL_GRUPE A2
                                   ON A1.ID_GRUPE = A2.ID_GRUPE
                                WHERE A1.STERS = 0
                                  AND A2.STERS = 0
                                  AND A2.ID_UTIL = V_ID_UTIL
                                  AND NVL2(V_ID_GESTIUNE_INIT,
                                           A1.ID_GESTIUNE,
                                           -1) = NVL(V_ID_GESTIUNE_INIT, -1))
                        GROUP BY ID_ARTICOL) C
              ON A.ID_ARTICOL = C.ID_ARTICOL
          /*                                  AND NVL(C.ID_RUL_AUX, 0) = 0*/
            LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                         FROM CURS
                        WHERE DATA <= V_DATA_CURS
                          AND DATA2 >= V_DATA_CURS
                          AND STERS = 0) D
              ON V_ID_VALUTA = D.ID_VALUTA
            LEFT JOIN NOM_VALUTE E
              ON V_ID_VALUTA = E.ID_VALUTA
           WHERE A.ID_UTIL = V_ID_UTIL
                -- AND A.ID_VALUTA = V_ID_VALUTA AND A.IN_VALUTA = 1
             AND ((A.ID_VALUTA = V_ID_VALUTA AND A.IN_VALUTA = 1) OR
                 A.ID_POL = pack_facturare.nid_politica_stoc)
             AND ((lnFacturareFaraStoc = 1 and NVL(C.CANTITATE, 0) <= 0) OR
                 DECODE(A.GESTIONABIL, 1, NVL(C.CANTITATE, 0), 1) > 0)
           ORDER BY A.DENUMIRE;

      WHEN V_TIP = 7 THEN
        -- credit note
        OPEN V_CURSOR FOR
          SELECT rownum as id_c,
                 A.ID_ARTICOL,
                 NULL AS LOT,
                 NULL as SERIE,
                 A.ID_POL,
                 V_ID_VALUTA AS ID_VALUTA, -- A.ID_VALUTA
                 A.NUME_LISTA_PRETURI,
                 ROUND(NVL(D.CURS, 0) *
                       ROUND(A.DISCOUNT_UNITAR,
                             pack_sesiune.nzecimale_pretvval) /
                       NVL(D.MULTIPLICATOR, 1),
                       pack_sesiune.nzecimale_pretv) AS DISCOUNT_UNITAR,
                 ROUND(A.DISCOUNT_UNITAR, pack_sesiune.nzecimale_pretvval) AS DISCOUNT_UNITAR_VAL,
                 A.CODMAT,
                 A.CODBARE,
                 A.DENUMIRE,
                 NVL(A.UM, '') AS UM,
                 A.GESTIONABIL,
                 (CASE
                   WHEN lnFacturareFaraStoc = 1 AND C.CANTITATE < 0 THEN
                    0
                   ELSE
                    NVL(C.CANTITATE, 0)
                 END) AS CANTITATE,
                 A.PROC_TVAV,
                 A.PRETURI_CU_TVA,
                 D.CURS,
                 D.MULTIPLICATOR,
                 ROUND(NVL(D.CURS, 0) *
                       ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) /
                       NVL(D.MULTIPLICATOR, 1),
                       pack_sesiune.nzecimale_pretv) AS PRET,
                 ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) AS PRET_VAL,
                 1 AS TIP_VALUTA,
                 E.NUME_VAL
            FROM FACT_VPRETURI_UTILIZATOR A
            LEFT JOIN (SELECT ID_ARTICOL,
                              SUM(CANTS + CANT - CANTE) AS CANTITATE
                         FROM STOC
                        WHERE LUNA = V_LUNA
                          AND AN = V_AN
                          AND NVL(ID_SUCURSALA, -99) =
                              NVL(V_ID_SUCURSALA, -99)
                          AND ID_GESTIUNE IN
                              (SELECT DISTINCT A1.ID_GESTIUNE
                                 FROM GEST_CORESP_GRUPE_GESTIUNI A1
                                 LEFT JOIN GEST_CORESP_UTIL_GRUPE A2
                                   ON A1.ID_GRUPE = A2.ID_GRUPE
                                WHERE A1.STERS = 0
                                  AND A2.STERS = 0
                                  AND A2.ID_UTIL = V_ID_UTIL
                                  AND NVL2(V_ID_GESTIUNE_INIT,
                                           A1.ID_GESTIUNE,
                                           -1) = NVL(V_ID_GESTIUNE_INIT, -1))
                        GROUP BY ID_ARTICOL) C
              ON A.ID_ARTICOL = C.ID_ARTICOL
            LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                         FROM CURS
                        WHERE DATA <= V_DATA_CURS
                          AND DATA2 >= V_DATA_CURS
                          AND ID_VALUTA = V_ID_VALUTA
                          AND STERS = 0) D
              ON V_ID_VALUTA = D.ID_VALUTA
            LEFT JOIN NOM_VALUTE E
              ON V_ID_VALUTA = E.ID_VALUTA
           WHERE A.ID_UTIL = V_ID_UTIL
             AND A.ID_VALUTA = V_ID_VALUTA
             AND A.IN_VALUTA = 1
             AND NVL(A.nota_discount, 0) = 1
             AND ((lnFacturareFaraStoc = 1 and NVL(C.CANTITATE, 0) <= 0) OR
                 DECODE(A.GESTIONABIL, 1, NVL(C.CANTITATE, 0), 1) > 0)
           ORDER BY A.DENUMIRE;
      ELSE
        -- aviz
        OPEN V_CURSOR FOR
          SELECT rownum as id_c,
                 A.ID_ARTICOL,
                 NULL AS LOT,
                 NULL as SERIE,
                 A.ID_POL,
                 A.ID_VALUTA,
                 A.NUME_LISTA_PRETURI,
                 (CASE
                   WHEN NVL(E.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(D.CURS, 0) *
                          ROUND(A.DISCOUNT_UNITAR,
                                pack_sesiune.nzecimale_pretvval) /
                          NVL(D.MULTIPLICATOR, 1),
                          pack_sesiune.nzecimale_pretv)
                   ELSE
                    ROUND(A.DISCOUNT_UNITAR, pack_sesiune.nzecimale_pretv)
                 END) AS DISCOUNT_UNITAR,
                 (CASE
                   WHEN NVL(E.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(A.DISCOUNT_UNITAR, pack_sesiune.nzecimale_pretvval)
                   ELSE
                    0
                 END) AS DISCOUNT_UNITAR_VAL,
                 A.CODMAT,
                 A.CODBARE,
                 A.DENUMIRE,
                 NVL(A.UM, '') AS UM,
                 A.GESTIONABIL,
                 (CASE
                   WHEN lnFacturareFaraStoc = 1 AND C.CANTITATE < 0 THEN
                    0
                   ELSE
                    NVL(C.CANTITATE, 0)
                 END) AS CANTITATE,
                 A.PROC_TVAV,
                 A.PRETURI_CU_TVA,
                 D.CURS,
                 D.MULTIPLICATOR,
                 (CASE
                   WHEN NVL(E.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(NVL(D.CURS, 0) *
                          ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) /
                          NVL(D.MULTIPLICATOR, 1),
                          pack_sesiune.nzecimale_pretv)
                   ELSE
                    ROUND(A.PRET, pack_sesiune.nzecimale_pretv)
                 END) AS PRET,
                 (CASE
                   WHEN NVL(E.MONEDA_NATIONALA, 1) <> 1 THEN
                    ROUND(A.PRET, pack_sesiune.nzecimale_pretvval)
                   ELSE
                    0
                 END) AS PRET_VAL,
                 1 - NVL(E.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
                 E.NUME_VAL,
                 DECODE(A.ID_POL,
                        NVL(pack_facturare.nid_politica_stoc, A.ID_POL + 1),
                        1,
                        0) AS MODIFICABIL
            FROM FACT_VPRETURI_UTILIZATOR A
            LEFT JOIN (SELECT ID_ARTICOL,
                              SUM(CANTS + CANT - CANTE) AS CANTITATE
                         FROM STOC
                        WHERE LUNA = V_LUNA
                          AND AN = V_AN
                          AND NVL(ID_SUCURSALA, -99) =
                              NVL(V_ID_SUCURSALA, -99)
                          AND ID_GESTIUNE IN
                              (SELECT DISTINCT A1.ID_GESTIUNE
                                 FROM GEST_CORESP_GRUPE_GESTIUNI A1
                                 LEFT JOIN GEST_CORESP_UTIL_GRUPE A2
                                   ON A1.ID_GRUPE = A2.ID_GRUPE
                                WHERE A1.STERS = 0
                                  AND A2.STERS = 0
                                  AND A2.ID_UTIL = V_ID_UTIL
                                  AND NVL2(V_ID_GESTIUNE_INIT,
                                           A1.ID_GESTIUNE,
                                           -1) = NVL(V_ID_GESTIUNE_INIT, -1))
                        GROUP BY ID_ARTICOL) C
              ON A.ID_ARTICOL = C.ID_ARTICOL
            LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                         FROM CURS
                        WHERE DATA <= V_DATA_CURS
                          AND DATA2 >= V_DATA_CURS
                          AND STERS = 0) D
              ON A.ID_VALUTA = D.ID_VALUTA
            LEFT JOIN NOM_VALUTE E
              ON A.ID_VALUTA = E.ID_VALUTA
           WHERE A.ID_UTIL = V_ID_UTIL
             AND ((lnFacturareFaraStoc = 1 and NVL(C.CANTITATE, 0) <= 0) OR
                 DECODE(NVL(A.GESTIONABIL, 2),
                         1,
                         NVL(C.CANTITATE, 0),
                         2,
                         0,
                         1) > 0)
           ORDER BY A.DENUMIRE;

    END CASE;
  END cursor_preturi;
  -------------------------------------------------------------------
  PROCEDURE cursor_contract(V_DATA_CURS        IN DATE,
                            V_TIP              IN NUMBER,
                            V_ID_VALUTA        IN NUMBER,
                            V_LISTAID          IN VARCHAR2,
                            V_ID_GESTIUNE_INIT IN NUMBER,
                            V_LUNA             IN NUMBER,
                            V_AN               IN NUMBER,
                            V_ID_UTIL          IN NUMBER,
                            V_ID_SUCURSALA     IN NUMBER,
                            V_ID_AGENT         OUT NUMBER,
                            V_NUME_AGENT       OUT VARCHAR2,
                            V_CURSOR           OUT cursor_facturare,
                            V_CURSOR2          OUT cursor_facturare) IS
    V_NR_INREGISTRARI     NUMBER(10);
    V_NR_INREGISTRARI_TOT NUMBER(10);
    V_SEPARATOR           VARCHAR2(10) := ',';
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);

    BEGIN
      SELECT DISTINCT ID_AGENT, NUME_AGENT
        INTO V_ID_AGENT, V_NUME_AGENT
        FROM VCONTRACTE
       WHERE ID_CTR IN
             (SELECT X as ID_CTR
                FROM table(charn2collection(V_LISTAID, V_SEPARATOR)));
    EXCEPTION
      WHEN TOO_MANY_ROWS THEN
        V_ID_AGENT   := NULL;
        V_NUME_AGENT := NULL;
    END;

    -- verificare ca exista cursul pentru valutele din CTR_SCADENTAR si CTR_ARTICOLE
    WITH CRS AS
     (SELECT X as ID_CTR
        FROM table(charn2collection(V_LISTAID, V_SEPARATOR)))
    SELECT SUM(CASE
                 WHEN B.ID_VALUTA <> pack_facturare.nid_moneda_nationala THEN
                  1
                 ELSE
                  0
               END),
           SUM(CASE
                 WHEN A.ID_VALUTA <> pack_facturare.nid_moneda_nationala THEN
                  1
                 ELSE
                  0
               END)
      INTO V_NR_INREGISTRARI, V_NR_INREGISTRARI_TOT
      FROM (SELECT DISTINCT NVL(C.ID_VALUTA, B.ID_VALUTA) AS ID_VALUTA
              FROM CONTRACTE A
              LEFT JOIN CTR_ARTICOLE B
                ON A.ID_CTR = B.ID_CTR
               AND A.OPT_FACTURARE = 3
              LEFT JOIN CTR_SCADENTAR C
                ON A.ID_CTR = B.ID_CTR
               AND A.OPT_FACTURARE IN (1, 2)
             WHERE A.ID_CTR IN (SELECT ID_CTR FROM CRS)
               AND A.STERS = 0
               AND A.INACTIV = 0) A
      LEFT JOIN CURS B
        ON A.ID_VALUTA = B.ID_VALUTA
       AND B.STERS = 0
       AND B.DATA <= V_DATA_CURS
       AND B.DATA2 >= V_DATA_CURS;

    IF V_NR_INREGISTRARI <> V_NR_INREGISTRARI_TOT THEN
      RAISE_APPLICATION_ERROR(-20005,
                              'Nu este setat cursul din data de ' ||
                              to_char(V_DATA_CURS, 'DD/MM/YYYY') || ' !');
    END IF;

    OPEN V_CURSOR2 FOR
      WITH CRS AS
       (SELECT X as ID_CTR
          FROM table(charn2collection(V_LISTAID, V_SEPARATOR)))
      SELECT rownum - 10000 as id_c,
             id_ctr,
             id_articol,
             id_rata,
             id_pol,
             id_valuta,
             numar || '/' || to_char(data, 'DD.MM.YYYY') as numar_contract,
             numar,
             data,
             discount_unitar,
             discount_unitar_val,
             codmat,
             codbare,
             denumire,
             preturi_cu_tva,
             um,
             gestionabil,
             cantitate,
             proc_tvav,
             curs,
             multiplicator,
             pret,
             pret_val,
             tip_valuta,
             nume_val,
             nr_rata,
             data_rata,
             data_scadenta,
             explicatie,
             opt_facturare
        FROM (SELECT A.ID_CTR,
                     C.ID_ARTICOL,
                     NULL as id_rata,
                     C.ID_POL,
                     B.ID_VALUTA,
                     A.NUMAR,
                     A.DATA,
                     (CASE
                       WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(F.CURS, 0) *
                              ROUND(ROUND(B.PRET_UNITAR,
                                          pack_sesiune.nzecimale_pretvval)

                                    * B.COEF_DISCOUNT / 100,
                                    pack_sesiune.nzecimale_pretvval) /
                              NVL(F.MULTIPLICATOR, 1),
                              pack_sesiune.nzecimale_pretv)
                       ELSE
                        ROUND(ROUND(B.PRET_UNITAR,
                                    pack_sesiune.nzecimale_pretv) *
                              B.COEF_DISCOUNT / 100,
                              pack_sesiune.nzecimale_pretv)
                     END) AS DISCOUNT_UNITAR,
                     (CASE
                       WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(ROUND(B.PRET_UNITAR,
                                    pack_sesiune.nzecimale_pretvval) *
                              B.COEF_DISCOUNT / 100,
                              pack_sesiune.nzecimale_pretvval)
                       ELSE
                        0
                     END) AS DISCOUNT_UNITAR_VAL,
                     E.CODMAT,
                     E.CODBARE,
                     E.DENUMIRE,
                     B.PRET_CU_TVA AS PRETURI_CU_TVA,
                     NVL(E.UM, '') AS UM,
                     E.IN_STOC AS GESTIONABIL,
                     B.CANT AS CANTITATE,
                     C.PROC_TVAV,
                     F.CURS,
                     F.MULTIPLICATOR,
                     (CASE
                       WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(F.CURS, 0) *
                              ROUND(B.PRET_UNITAR,
                                    pack_sesiune.nzecimale_pretvval) /
                              NVL(F.MULTIPLICATOR, 1),
                              pack_sesiune.nzecimale_pretv)
                       ELSE
                        ROUND(B.PRET_UNITAR, pack_sesiune.nzecimale_pretv)
                     END) AS PRET,
                     (CASE
                       WHEN NVL(G.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(B.PRET_UNITAR, pack_sesiune.nzecimale_pretvval)
                       ELSE
                        0
                     END) AS PRET_VAL,
                     1 - NVL(G.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
                     G.NUME_VAL,
                     NULL as NR_RATA,
                     null as data_rata,
                     null as data_scadenta,
                     '' as explicatie,
                     A.OPT_FACTURARE
                FROM (SELECT ID_CTR, NUMAR, DATA, OPT_FACTURARE
                        FROM CONTRACTE
                       WHERE ID_CTR in (SELECT ID_CTR FROM CRS)
                         AND OPT_FACTURARE = 3
                         AND STERS = 0) A
                LEFT JOIN CTR_ARTICOLE B
                  ON A.ID_CTR = B.ID_CTR
                LEFT JOIN CRM_POLITICI_PRET_ART C
                  ON B.ID_POL_ART = C.ID_POL_ART
                LEFT JOIN NOM_ARTICOLE E
                  ON C.ID_ARTICOL = E.ID_ARTICOL
                LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                            FROM CURS
                           WHERE DATA <= V_DATA_CURS
                             AND DATA2 >= V_DATA_CURS
                             AND STERS = 0) F
                  ON B.ID_VALUTA = F.ID_VALUTA
                LEFT JOIN NOM_VALUTE G
                  ON B.ID_VALUTA = G.ID_VALUTA
              UNION ALL
              SELECT A01.ID_CTR,
                     null as id_articol,
                     B01.ID_RATA,
                     NULL AS ID_POL,
                     B01.ID_VALUTA,
                     A01.NUMAR,
                     A01.DATA,
                     0 as discount_unitar,
                     0 as discount_unitar_val,
                     null as codmat,
                     null as codbare,
                     B01.DEN_RATA as denumire,
                     B01.PRET_CU_TVA AS PRETURI_CU_TVA,
                     NULL AS UM,
                     0 as gestionabil,
                     1 as cantitate,
                     (A01.PROC_TVA + 100) / 100 AS PROC_TVAV,
                     E01.CURS,
                     E01.MULTIPLICATOR,
                     (CASE
                       WHEN NVL(F01.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(E01.CURS, 0) *
                              (ROUND(B01.VALRATA,
                                     pack_sesiune.nzecimale_pretvval) -
                               DECODE(B01.PRET_CU_TVA,
                                      0,
                                      NVL(D01.VALVALUTA, 0),
                                      NVL(D01.VALVALUTACTVA, 0))) /
                              NVL(E01.MULTIPLICATOR, 1),
                              pack_sesiune.nzecimale_pretv)
                       ELSE
                        ROUND(ROUND(B01.VALRATA, pack_sesiune.nzecimale_pretv) -
                              DECODE(B01.PRET_CU_TVA,
                                     0,
                                     NVL(D01.VALVALUTA, 0),
                                     NVL(D01.VALVALUTACTVA, 0)),
                              pack_sesiune.nzecimale_pretv)
                     END) AS PRET,
                     (CASE
                       WHEN NVL(F01.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(ROUND(B01.VALRATA,
                                    pack_sesiune.nzecimale_pretvval) -
                              DECODE(B01.PRET_CU_TVA,
                                     0,
                                     NVL(D01.VALVALUTA, 0),
                                     NVL(D01.VALVALUTACTVA, 0)),
                              pack_sesiune.nzecimale_pretvval)
                       ELSE
                        0
                     END) AS PRET_VAL,
                     1 - NVL(F01.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
                     F01.NUME_VAL,
                     B01.NR_RATA,
                     B01.DATA_RATA,
                     B01.DATA_SCADENTA,
                     UPPER(NVL(C01.DENUMIRE, B01.DEN_RATA)) AS EXPLICATIE,
                     A01.OPT_FACTURARE
                FROM (SELECT ID_CTR, NUMAR, DATA, OPT_FACTURARE, PROC_TVA
                        FROM CONTRACTE
                       WHERE STERS = 0
                         AND ID_CTR in (SELECT ID_CTR FROM CRS)
                         AND OPT_FACTURARE IN (1, 2)) A01
                LEFT JOIN CTR_SCADENTAR B01
                  ON A01.ID_CTR = B01.ID_CTR
                 AND B01.STERS = 0
                LEFT JOIN (SELECT DENUMIRE, ID_CTR AS ID_CONTRACT
                            FROM (SELECT C1.ID_CTR,
                                         C3.DENUMIRE,
                                         ROW_NUMBER() OVER(PARTITION BY C1.ID_CTR ORDER BY C1.ID_CTR_ART) AS NRCRT
                                    FROM CTR_ARTICOLE C1
                                    LEFT JOIN CRM_POLITICI_PRET_ART C2
                                      ON C1.ID_POL_ART = C2.ID_POL_ART
                                    LEFT JOIN NOM_ARTICOLE C3
                                      ON C2.ID_ARTICOL = C3.ID_ARTICOL
                                   WHERE C1.id_ctr in
                                         (SELECT ID_CTR FROM CRS))
                           WHERE NRCRT = 1) C01
                  ON A01.ID_CTR = C01.ID_CONTRACT
                 AND A01.OPT_FACTURARE = 2
              -- 14.11.2012 : am scos round(...) de la valvaluta si valvalutactva - trebuie rotunjite in fct. de id_valuta
                LEFT JOIN (SELECT SUM(VALVALUTA) AS VALVALUTA,
                                 SUM(VALVALUTACTVA) AS VALVALUTACTVA,
                                 ID_RATA
                            FROM CTR_RATE_FACTURI
                           WHERE STERS = 0
                           GROUP BY ID_RATA) D01
                  ON B01.ID_RATA = D01.ID_RATA
                LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                            FROM CURS
                           WHERE DATA <= V_DATA_CURS
                             AND DATA2 >= V_DATA_CURS
                             AND STERS = 0) E01
                  ON B01.ID_VALUTA = E01.ID_VALUTA
                LEFT JOIN NOM_VALUTE F01
                  ON B01.ID_VALUTA = F01.ID_VALUTA
              -- 14.11.2012 : am scos round(...) de la valrata - trebuie rotunjita in fct. de id_valuta
               WHERE B01.VALRATA -
                     DECODE(B01.PRET_CU_TVA,
                            0,
                            NVL(D01.VALVALUTA, 0),
                            NVL(D01.VALVALUTACTVA, 0)) > 0)
       ORDER BY data, numar, data_rata, denumire;

    pack_facturare.cursor_preturi(V_DATA_CURS,
                                  V_TIP,
                                  V_ID_VALUTA,
                                  V_ID_GESTIUNE_INIT,
                                  V_LUNA,
                                  V_AN,
                                  V_ID_UTIL,
                                  V_ID_SUCURSALA,
                                  V_CURSOR);

  END cursor_contract;
  -------------------------------------------------------------------
  PROCEDURE cursor_comanda(V_DATA_CURS IN DATE,
                           V_TIP       IN NUMBER,
                           V_LISTAID   IN VARCHAR2,
                           V_ID_UTIL   IN NUMBER,
                           V_CURSOR    OUT cursor_facturare) IS
    V_ID_COMANDA          COMENZI.ID_COMANDA%TYPE;
    V_NR_INREGISTRARI     NUMBER(10);
    V_NR_INREGISTRARI_TOT NUMBER(10);
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);
    V_ID_COMANDA := TO_NUMBER(V_LISTAID);

    -- verificare ca exista cursul pentru valutele din COMENZI_ELEMENTE
    SELECT SUM(CASE
                 WHEN B.ID_VALUTA <> pack_facturare.nid_moneda_nationala THEN
                  1
                 ELSE
                  0
               END),
           SUM(CASE
                 WHEN A.ID_VALUTA <> pack_facturare.nid_moneda_nationala THEN
                  1
                 ELSE
                  0
               END)
      INTO V_NR_INREGISTRARI, V_NR_INREGISTRARI_TOT
      FROM (SELECT DISTINCT ID_VALUTA
              FROM COMENZI_ELEMENTE
             WHERE ID_COMANDA = V_ID_COMANDA) A
      LEFT JOIN CURS B
        ON A.ID_VALUTA = B.ID_VALUTA
       AND B.STERS = 0
       AND B.DATA <= V_DATA_CURS
       AND B.DATA2 >= V_DATA_CURS;

    IF V_NR_INREGISTRARI <> V_NR_INREGISTRARI_TOT THEN
      RAISE_APPLICATION_ERROR(-20005,
                              'Nu este setat cursul din data de ' ||
                              to_char(V_DATA_CURS, 'DD/MM/YYYY') || ' !');
    END IF;

    IF V_TIP <= 20 THEN
      -- factura
      OPEN V_CURSOR FOR
        SELECT ROWNUM as id_c,
               A.ID_ARTICOL,
               NULL AS LOT,
               NULL as SERIE,
               A.ID_POL,
               A.ID_VALUTA,
               G.NUME_LISTA_PRETURI,
               (CASE
                 WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(E.CURS, 0) *
                        ROUND(NVL(B.DISCOUNT_UNITAR, 0) +
                              NVL(A.DISCOUNT_UNITAR, 0),
                              pack_sesiune.nzecimale_pretvval) /
                        NVL(E.MULTIPLICATOR, 1),
                        pack_sesiune.nzecimale_pretv)
                 ELSE
                  ROUND(NVL(B.DISCOUNT_UNITAR, 0) +
                        NVL(A.DISCOUNT_UNITAR, 0),
                        pack_sesiune.nzecimale_pretv)
               END) AS DISCOUNT_UNITAR,
               (CASE
                 WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(B.DISCOUNT_UNITAR, 0) +
                        NVL(A.DISCOUNT_UNITAR, 0),
                        pack_sesiune.nzecimale_pretvval)
                 ELSE
                  0
               END) AS DISCOUNT_UNITAR_VAL,
               C.CODMAT,
               C.CODBARE,
               C.DENUMIRE,
               NVL(C.UM, '') AS UM,
               C.IN_STOC AS GESTIONABIL,
               A.CANTITATE - NVL(D.CANTITATE, 0) AS CANTITATE,
               B.PROC_TVAV,
               A.PRET_CU_TVA AS PRETURI_CU_TVA,
               E.CURS,
               E.MULTIPLICATOR,
               (CASE
                 WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(E.CURS, 0) *
                        ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) /
                        NVL(E.MULTIPLICATOR, 1),
                        pack_sesiune.nzecimale_pretv)
                 ELSE
                  ROUND(A.PRET, pack_sesiune.nzecimale_pretv)
               END) AS PRET,
               (CASE
                 WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(A.PRET, pack_sesiune.nzecimale_pretvval)
                 ELSE
                  0
               END) AS PRET_VAL,
               1 - NVL(F.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
               F.NUME_VAL
          FROM COMENZI_ELEMENTE A
          LEFT JOIN CRM_POLITICI_PRET_ART B
            ON A.ID_POL = B.ID_POL
           AND A.ID_ARTICOL = B.ID_ARTICOL
          LEFT JOIN CRM_POLITICI_PRETURI G
            ON B.ID_POL = G.ID_POL
          LEFT JOIN NOM_ARTICOLE C
            ON A.ID_ARTICOL = C.ID_ARTICOL
          LEFT JOIN (SELECT B1.ID_ARTICOL, SUM(B1.CANTITATE) AS CANTITATE
                       FROM VANZARI A1
                       LEFT JOIN VANZARI_DETALII B1
                         ON A1.ID_VANZARE = B1.ID_VANZARE
                        AND B1.STERS = 0
                      WHERE A1.STERS = 0
                        AND A1.ID_COMANDA = V_ID_COMANDA
                      GROUP BY B1.ID_ARTICOL) D
            ON A.ID_ARTICOL = D.ID_ARTICOL
          LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                       FROM CURS
                      WHERE DATA <= V_DATA_CURS
                        AND DATA2 >= V_DATA_CURS
                        AND STERS = 0) E
            ON A.ID_VALUTA = E.ID_VALUTA
          LEFT JOIN NOM_VALUTE F
            ON A.ID_VALUTA = F.ID_VALUTA
         WHERE A.STERS = 0
           AND A.ID_COMANDA = V_ID_COMANDA
           AND A.CANTITATE - NVL(D.CANTITATE, 0) > 0
         ORDER BY C.DENUMIRE;
    ELSE
      -- aviz
      OPEN V_CURSOR FOR
        SELECT ROWNUM as id_c,
               A.ID_ARTICOL,
               NULL AS LOT,
               NULL as SERIE,
               A.ID_POL,
               A.ID_VALUTA,
               G.NUME_LISTA_PRETURI,
               (CASE
                 WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(E.CURS, 0) *
                        ROUND(NVL(B.DISCOUNT_UNITAR, 0) +
                              NVL(A.DISCOUNT_UNITAR, 0),
                              pack_sesiune.nzecimale_pretvval) /
                        NVL(E.MULTIPLICATOR, 1),
                        pack_sesiune.nzecimale_pretv)
                 ELSE
                  ROUND(NVL(B.DISCOUNT_UNITAR, 0) +
                        NVL(A.DISCOUNT_UNITAR, 0),
                        pack_sesiune.nzecimale_pretv)
               END) AS DISCOUNT_UNITAR,
               (CASE
                 WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(B.DISCOUNT_UNITAR, 0) +
                        NVL(A.DISCOUNT_UNITAR, 0),
                        pack_sesiune.nzecimale_pretvval)
                 ELSE
                  0
               END) AS DISCOUNT_UNITAR_VAL,
               C.CODMAT,
               C.CODBARE,
               C.DENUMIRE,
               NVL(C.UM, '') AS UM,
               C.IN_STOC AS GESTIONABIL,
               A.CANTITATE - NVL(D.CANTITATE, 0) AS CANTITATE,
               B.PROC_TVAV,
               A.PRET_CU_TVA AS PRETURI_CU_TVA,
               E.CURS,
               E.MULTIPLICATOR,
               (CASE
                 WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(E.CURS, 0) *
                        ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) /
                        NVL(E.MULTIPLICATOR, 1),
                        pack_sesiune.nzecimale_pretv)
                 ELSE
                  ROUND(A.PRET, pack_sesiune.nzecimale_pretv)
               END) AS PRET,
               (CASE
                 WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(A.PRET, pack_sesiune.nzecimale_pretvval)
                 ELSE
                  0
               END) AS PRET_VAL,
               1 - NVL(F.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
               F.NUME_VAL
          FROM COMENZI_ELEMENTE A
          LEFT JOIN CRM_POLITICI_PRET_ART B
            ON A.ID_POL = B.ID_POL
           AND A.ID_ARTICOL = B.ID_ARTICOL
          LEFT JOIN CRM_POLITICI_PRETURI G
            ON B.ID_POL = G.ID_POL
          LEFT JOIN NOM_ARTICOLE C
            ON A.ID_ARTICOL = C.ID_ARTICOL
          LEFT JOIN (SELECT B1.ID_ARTICOL, SUM(B1.CANTITATE) AS CANTITATE
                       FROM VANZARI A1
                       LEFT JOIN VANZARI_DETALII B1
                         ON A1.ID_VANZARE = B1.ID_VANZARE
                        AND B1.STERS = 0
                      WHERE A1.STERS = 0
                        AND A1.ID_COMANDA = V_ID_COMANDA
                      GROUP BY B1.ID_ARTICOL) D
            ON A.ID_ARTICOL = D.ID_ARTICOL
          LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                       FROM CURS
                      WHERE DATA <= V_DATA_CURS
                        AND DATA2 >= V_DATA_CURS
                        AND STERS = 0) E
            ON A.ID_VALUTA = E.ID_VALUTA
          LEFT JOIN NOM_VALUTE F
            ON A.ID_VALUTA = F.ID_VALUTA
         WHERE A.STERS = 0
           AND A.ID_COMANDA = V_ID_COMANDA
           AND SIGN(A.CANTITATE) * (A.CANTITATE - NVL(D.CANTITATE, 0)) > 0
         ORDER BY C.DENUMIRE;
    END IF;
  END cursor_comanda;
  -------------------------------------------------------------------
  PROCEDURE cursor_lucrare(V_DATA_CURS    IN DATE,
                           V_ID_GESTIUNE  IN NUMBER,
                           V_ID_LUCRARE   IN NUMBER,
                           V_LUNA         IN NUMBER,
                           V_AN           IN NUMBER,
                           V_ID_UTIL      IN NUMBER,
                           V_ID_SUCURSALA IN NUMBER,
                           V_CURSOR       OUT cursor_facturare) IS
    V_NR_INREGISTRARI     NUMBER(10);
    V_NR_INREGISTRARI_TOT NUMBER(10);
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);

    -- verificare ca exista cursul pentru valutele din COMENZI_ELEMENTE
    SELECT SUM(CASE
                 WHEN B.ID_VALUTA <> pack_facturare.nid_moneda_nationala THEN
                  1
                 ELSE
                  0
               END),
           SUM(CASE
                 WHEN A.ID_VALUTA <> pack_facturare.nid_moneda_nationala THEN
                  1
                 ELSE
                  0
               END)
      INTO V_NR_INREGISTRARI, V_NR_INREGISTRARI_TOT
      FROM (SELECT DISTINCT ID_VALUTA
              FROM COMENZI_ELEMENTE
             WHERE ID_COMANDA IN
                   (SELECT ID_COMANDA
                      FROM COMENZI
                     WHERE ID_LUCRARE = V_ID_LUCRARE
                       AND STERS = 0
                       AND ID_SUCURSALA = V_ID_SUCURSALA)) A
      LEFT JOIN CURS B
        ON A.ID_VALUTA = B.ID_VALUTA
       AND B.STERS = 0
       AND B.DATA <= V_DATA_CURS
       AND B.DATA2 >= V_DATA_CURS;

    IF V_NR_INREGISTRARI <> V_NR_INREGISTRARI_TOT THEN
      RAISE_APPLICATION_ERROR(-20005,
                              'Nu este setat cursul din data de ' ||
                              to_char(V_DATA_CURS, 'DD/MM/YYYY') || ' !');
    END IF;

    IF TO_NUMBER(NVL(pack_sesiune.getoptiunefirma('FACTALGREPCOM'), '0')) = 1 THEN
      -- in gestiunea sursa exista un singur cont
      OPEN V_CURSOR FOR
        select A1.id_articol,
               A1.id_comanda,
               A1.id_comanda_element,
               A1.id_gestiune,
               A1.id_pol,
               A1.cont,
               A3.nume_gestiune,
               A2.codmat,
               A2.codbare,
               A2.denumire,
               A1.cant_com,
               A1.cant_ramasa,
               A1.cant_stoc,
               A1.cantitate,
               A1.cant_stoc -
               (SUM(a1.cantitate) over(partition by a1.id_articol)) as cant_disp,
               A1.ID_VALUTA,
               NVL(A4.PROC_TVAV, 1) AS PROC_TVAV,
               A1.PRET_CU_TVA AS PRETURI_CU_TVA,
               NVL(A5.CURS, 0) AS CURS,
               NVL(A5.MULTIPLICATOR, 1) AS MULTIPLICATOR,
               (CASE
                 WHEN NVL(A6.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(A5.CURS, 0) *
                        (ROUND(A1.PRET, pack_sesiune.nzecimale_pretvval) -
                         ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                               pack_sesiune.nzecimale_pretvval)) /
                        NVL(A5.MULTIPLICATOR, 1),
                        pack_sesiune.nzecimale_pretv)
                 ELSE
                  ROUND(A1.PRET, pack_sesiune.nzecimale_pretv) -
                  ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                        pack_sesiune.nzecimale_pretv)
               END) AS PRET,
               1 - NVL(A6.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
               A6.NUME_VAL,
               (CASE
                 WHEN NVL(A6.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(A5.CURS, 0) *
                        (ROUND(A1.PRET, pack_sesiune.nzecimale_pretvval) -
                         ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                               pack_sesiune.nzecimale_pretvval)) /
                        NVL(A5.MULTIPLICATOR, 1),
                        pack_sesiune.nzecimale_pretv)
                 ELSE
                  ROUND(A1.PRET, pack_sesiune.nzecimale_pretv) -
                  ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                        pack_sesiune.nzecimale_pretv)
               END) + ROUND((CASE
                              WHEN NVL(A6.MONEDA_NATIONALA, 1) <> 1 THEN
                               ROUND(NVL(A5.CURS, 0) *
                                     (ROUND(A1.PRET, pack_sesiune.nzecimale_pretvval) -
                                      ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                                            pack_sesiune.nzecimale_pretvval)) /
                                     NVL(A5.MULTIPLICATOR, 1),
                                     pack_sesiune.nzecimale_pretv)
                              ELSE
                               ROUND(A1.PRET, pack_sesiune.nzecimale_pretv) -
                               ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                                     pack_sesiune.nzecimale_pretv)
                            END) * (CASE
                              WHEN A1.PRET_CU_TVA = 0 THEN
                               A4.PROC_TVAV - 1
                              ELSE
                               0
                            END),
                            pack_sesiune.nzecimale_pretv) AS PRETCTVA
          from (SELECT CRS.ID_ARTICOL,
                       CRS.ID_VALUTA,
                       CRS.PRET_CU_TVA,
                       CRS.PRET,
                       CRS.ID_POL,
                       CRS.ID_COMANDA_ELEMENT,
                       CRS.ID_COMANDA,
                       CRS.ID_GESTIUNE,
                       CRS.CONT,
                       CRS.PROCENT,
                       CRS.CANT_COM,
                       CRS.CANT_RAMASA,
                       CRS.CANT_STOC,
                       CRS.CANT_REP,
                       CRS.INCOMPLET,
                       CRS.cant_rep + (case
                         when CRS.incomplet = 1 and CRS.cant_stoc > 0 and
                              CRS.cant_stoc - sum(CRS.cant_rep)
                          over(partition by CRS.id_articol) > 0 then
                          trunc((CRS.cant_stoc - sum(CRS.cant_rep)
                                 over(partition by CRS.id_articol)) /
                                sum(CRS.incomplet)
                                over(partition by CRS.id_articol)) +
                          (case
                             when mod((CRS.cant_stoc - sum(CRS.cant_rep)
                                       over(partition by CRS.id_articol)),
                                      sum(CRS.incomplet)
                                      over(partition by CRS.id_articol)) >=
                                  row_number()
                              over(partition by CRS.id_articol order by
                                       CRS.incomplet desc,
                                       CRS.procent desc) then
                              1
                             else
                              0
                           end)
                         else
                          0
                       end) as cantitate
                  FROM (SELECT A.ID_ARTICOL,
                               A.ID_VALUTA,
                               A.PRET_CU_TVA,
                               A.PRET - NVL(A.DISCOUNT_UNITAR, 0) AS PRET,
                               A.ID_POL,
                               A.ID_COMANDA_ELEMENT,
                               COM.ID_COMANDA,
                               COM.ID_GESTIUNE,
                               STOC.CONT,
                               FLOOR(RATIO_TO_REPORT(A.CANTITATE -
                                                     NVL(D.CANTITATE, 0))
                                     OVER(PARTITION BY A.ID_ARTICOL) * 100) / 100 as procent,
                               A.CANTITATE AS CANT_COM,
                               A.CANTITATE - NVL(D.CANTITATE, 0) AS CANT_RAMASA,
                               NVL(STOC.CANT_STOC, 0) AS CANT_STOC,
                               (case
                                 when NVL(STOC.CANT_STOC, 0) -
                                      (SUM(A.CANTITATE - NVL(D.CANTITATE, 0))
                                       OVER(PARTITION BY A.ID_ARTICOL)) > 0 and
                                      A.CANTITATE - NVL(D.CANTITATE, 0) > 0 and
                                      NVL(STOC.CANT_STOC, 0) > 0 then
                                  A.CANTITATE - NVL(D.CANTITATE, 0)
                                 when NVL(STOC.CANT_STOC, 0) > 0 and
                                      A.CANTITATE - NVL(D.CANTITATE, 0) > 0 then
                                  LEAST(A.CANTITATE - NVL(D.CANTITATE, 0),
                                        FLOOR(FLOOR(RATIO_TO_REPORT(A.CANTITATE -
                                                                    NVL(D.CANTITATE,
                                                                        0))
                                                    OVER(PARTITION BY
                                                         A.ID_ARTICOL) * 100) / 100 *
                                              NVL(STOC.CANT_STOC, 0)))
                                 else
                                  0
                               end) AS CANT_REP,
                               (case
                                 when NVL(STOC.CANT_STOC, 0) -
                                      (SUM(A.CANTITATE - NVL(D.CANTITATE, 0))
                                       OVER(PARTITION BY A.ID_ARTICOL)) > 0 and
                                      A.CANTITATE - NVL(D.CANTITATE, 0) > 0 and
                                      NVL(STOC.CANT_STOC, 0) > 0 then
                                  0
                                 when A.CANTITATE - NVL(D.CANTITATE, 0) -
                                      FLOOR(FLOOR(RATIO_TO_REPORT(A.CANTITATE -
                                                                  NVL(D.CANTITATE,
                                                                      0))
                                                  OVER(PARTITION BY
                                                       A.ID_ARTICOL) * 100) / 100 *
                                            NVL(STOC.CANT_STOC, 0)) > 0 AND
                                      NVL(STOC.CANT_STOC, 0) > 0 AND
                                      A.CANTITATE - NVL(D.CANTITATE, 0) > 0 then
                                  1
                                 else
                                  0
                               end) AS incomplet
                          FROM COMENZI COM
                          LEFT JOIN COMENZI_ELEMENTE A
                            ON COM.ID_COMANDA = A.ID_COMANDA
                          LEFT JOIN NOM_ARTICOLE C
                            ON A.ID_ARTICOL = C.ID_ARTICOL
                          LEFT JOIN (SELECT B1.ID_ARTICOL,
                                           A1.ID_COMANDA,
                                           SUM(B1.CANTITATE) AS CANTITATE
                                      FROM VANZARI A1
                                      LEFT JOIN VANZARI_DETALII B1
                                        ON A1.ID_VANZARE = B1.ID_VANZARE
                                       AND B1.STERS = 0
                                     WHERE A1.STERS = 0
                                     GROUP BY B1.ID_ARTICOL, A1.ID_COMANDA) D
                            ON A.ID_ARTICOL = D.ID_ARTICOL
                           AND A.ID_COMANDA = D.ID_COMANDA
                          LEFT JOIN (SELECT ID_ARTICOL,
                                           SUM(CANTS + CANT - CANTE) AS CANT_STOC,
                                           CONT
                                      FROM STOC
                                     WHERE AN = V_AN
                                       AND LUNA = V_LUNA
                                       AND ID_GESTIUNE = V_ID_GESTIUNE
                                       AND NVL2(V_ID_SUCURSALA,
                                                ID_SUCURSALA,
                                                -99) =
                                           NVL(V_ID_SUCURSALA, -99)
                                     GROUP BY ID_ARTICOL, CONT) STOC
                            ON A.ID_ARTICOL = STOC.ID_ARTICOL
                         WHERE A.STERS = 0
                           AND COM.ID_LUCRARE = V_ID_LUCRARE
                           AND COM.STERS = 0
                           AND COM.ID_SECTIE IN
                               (SELECT ID_SECTIE
                                  FROM NOM_SECTII
                                 WHERE STERS = 0
                                   AND NVL2(V_ID_SUCURSALA, ID_SUCURSALA, -99) =
                                       NVL(V_ID_SUCURSALA, -99))
                           AND C.IN_STOC = 1
                           AND A.CANTITATE - NVL(D.CANTITATE, 0) > 0
                           AND COM.ID_GESTIUNE IS NOT NULL
                         order by 1, 2 desc) CRS) A1
          LEFT JOIN NOM_ARTICOLE A2
            ON A1.ID_ARTICOL = A2.ID_ARTICOL
          LEFT JOIN NOM_GESTIUNI A3
            ON A1.ID_GESTIUNE = A3.ID_GESTIUNE
          LEFT JOIN CRM_POLITICI_PRET_ART A4
            ON A1.ID_POL = A4.ID_POL
           AND A1.ID_ARTICOL = A4.ID_ARTICOL
          LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                       FROM CURS
                      WHERE DATA <= V_DATA_CURS
                        AND DATA2 >= V_DATA_CURS
                        AND STERS = 0) A5
            ON A1.ID_VALUTA = A5.ID_VALUTA
          LEFT JOIN NOM_VALUTE A6
            ON A1.ID_VALUTA = A6.ID_VALUTA
         ORDER BY a2.denumire, a2.codmat, a3.nume_gestiune;

    ELSE

      OPEN V_CURSOR FOR
        select A1.id_articol,
               A1.id_comanda,
               A1.id_comanda_element,
               A1.id_gestiune,
               A1.id_pol,
               A1.cont,
               A3.nume_gestiune,
               A2.codmat,
               A2.codbare,
               A2.denumire,
               A1.cant_com,
               A1.cant_ramasa,
               A1.cant_stoc,
               A1.CANT_REP as cantitate,
               A1.cant_stoc -
               (SUM(a1.CANT_REP) over(partition by a1.id_articol)) as cant_disp,
               A1.ID_VALUTA,
               NVL(A4.PROC_TVAV, 1) AS PROC_TVAV,
               A1.PRET_CU_TVA AS PRETURI_CU_TVA,
               NVL(A5.CURS, 0) AS CURS,
               NVL(A5.MULTIPLICATOR, 1) AS MULTIPLICATOR,
               (CASE
                 WHEN NVL(A6.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(A5.CURS, 0) *
                        (ROUND(A1.PRET, pack_sesiune.nzecimale_pretvval) -
                         ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                               pack_sesiune.nzecimale_pretvval)) /
                        NVL(A5.MULTIPLICATOR, 1),
                        pack_sesiune.nzecimale_pretv)
                 ELSE
                  ROUND(A1.PRET, pack_sesiune.nzecimale_pretv) -
                  ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                        pack_sesiune.nzecimale_pretv)
               END) AS PRET,
               1 - NVL(A6.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
               A6.NUME_VAL,
               (CASE
                 WHEN NVL(A6.MONEDA_NATIONALA, 1) <> 1 THEN
                  ROUND(NVL(A5.CURS, 0) *
                        (ROUND(A1.PRET, pack_sesiune.nzecimale_pretvval) -
                         ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                               pack_sesiune.nzecimale_pretvval)) /
                        NVL(A5.MULTIPLICATOR, 1),
                        pack_sesiune.nzecimale_pretv)
                 ELSE
                  ROUND(A1.PRET, pack_sesiune.nzecimale_pretv) -
                  ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                        pack_sesiune.nzecimale_pretv)
               END) + ROUND((CASE
                              WHEN NVL(A6.MONEDA_NATIONALA, 1) <> 1 THEN
                               ROUND(NVL(A5.CURS, 0) *
                                     (ROUND(A1.PRET, pack_sesiune.nzecimale_pretvval) -
                                      ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                                            pack_sesiune.nzecimale_pretvval)) /
                                     NVL(A5.MULTIPLICATOR, 1),
                                     pack_sesiune.nzecimale_pretv)
                              ELSE
                               ROUND(A1.PRET, pack_sesiune.nzecimale_pretv) -
                               ROUND(NVL(A4.DISCOUNT_UNITAR, 0),
                                     pack_sesiune.nzecimale_pretv)
                            END) * (CASE
                              WHEN A1.PRET_CU_TVA = 0 THEN
                               A4.PROC_TVAV - 1
                              ELSE
                               0
                            END),
                            pack_sesiune.nzecimale_pretv) AS PRETCTVA
          from (SELECT A.ID_ARTICOL,
                       A.ID_VALUTA,
                       A.PRET_CU_TVA,
                       A.PRET - NVL(A.DISCOUNT_UNITAR, 0) AS PRET,
                       A.ID_POL,
                       A.ID_COMANDA_ELEMENT,
                       COM.ID_COMANDA,
                       COM.ID_GESTIUNE,
                       STOC.CONT,
                       A.CANTITATE AS CANT_COM,
                       A.CANTITATE - NVL(D.CANTITATE, 0) AS CANT_RAMASA,
                       NVL(STOC.CANT_STOC, 0) AS CANT_STOC,
                       (CASE
                         WHEN NVL(STOC.CANT_STOC, 0) > 0 THEN
                          A.CANTITATE - NVL(D.CANTITATE, 0)
                         ELSE
                          0
                       END) AS CANT_REP,
                       0 AS incomplet
                  FROM COMENZI COM
                  LEFT JOIN COMENZI_ELEMENTE A
                    ON COM.ID_COMANDA = A.ID_COMANDA
                  LEFT JOIN NOM_ARTICOLE C
                    ON A.ID_ARTICOL = C.ID_ARTICOL
                  LEFT JOIN (SELECT B1.ID_ARTICOL,
                                   A1.ID_COMANDA,
                                   SUM(B1.CANTITATE) AS CANTITATE
                              FROM VANZARI A1
                              LEFT JOIN VANZARI_DETALII B1
                                ON A1.ID_VANZARE = B1.ID_VANZARE
                               AND B1.STERS = 0
                             WHERE A1.STERS = 0
                             GROUP BY B1.ID_ARTICOL, A1.ID_COMANDA) D
                    ON A.ID_ARTICOL = D.ID_ARTICOL
                   AND A.ID_COMANDA = D.ID_COMANDA
                  LEFT JOIN (SELECT ID_ARTICOL,
                                   SUM(CANTS + CANT - CANTE) AS CANT_STOC,
                                   CONT
                              FROM STOC
                             WHERE AN = V_AN
                               AND LUNA = V_LUNA
                               AND ID_GESTIUNE = V_ID_GESTIUNE
                               AND NVL2(V_ID_SUCURSALA, ID_SUCURSALA, -99) =
                                   NVL(V_ID_SUCURSALA, -99)
                             GROUP BY ID_ARTICOL, CONT) STOC
                    ON A.ID_ARTICOL = STOC.ID_ARTICOL
                 WHERE A.STERS = 0
                   AND COM.ID_LUCRARE = V_ID_LUCRARE
                   AND COM.STERS = 0
                   AND COM.ID_SECTIE IN
                       (SELECT ID_SECTIE
                          FROM NOM_SECTII
                         WHERE STERS = 0
                           AND NVL2(V_ID_SUCURSALA, ID_SUCURSALA, -99) =
                               NVL(V_ID_SUCURSALA, -99))
                   AND C.IN_STOC = 1
                   AND A.CANTITATE - NVL(D.CANTITATE, 0) > 0
                   AND COM.ID_GESTIUNE IS NOT NULL
                 order by 1, 2 desc) A1
          LEFT JOIN NOM_ARTICOLE A2
            ON A1.ID_ARTICOL = A2.ID_ARTICOL
          LEFT JOIN NOM_GESTIUNI A3
            ON A1.ID_GESTIUNE = A3.ID_GESTIUNE
          LEFT JOIN CRM_POLITICI_PRET_ART A4
            ON A1.ID_POL = A4.ID_POL
           AND A1.ID_ARTICOL = A4.ID_ARTICOL
          LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                       FROM CURS
                      WHERE DATA <= V_DATA_CURS
                        AND DATA2 >= V_DATA_CURS
                        AND STERS = 0) A5
            ON A1.ID_VALUTA = A5.ID_VALUTA
          LEFT JOIN NOM_VALUTE A6
            ON A5.ID_VALUTA = A6.ID_VALUTA
         ORDER BY a2.denumire, a2.codmat, a3.nume_gestiune;
    END IF;

  END cursor_lucrare;
  -------------------------------------------------------------------
  PROCEDURE cursor_articole_k(V_DATA_CURS    IN DATE,
                              V_LUNA         IN NUMBER,
                              V_AN           IN NUMBER,
                              V_ID_UTIL      IN NUMBER,
                              V_ID_SUCURSALA IN NUMBER,
                              V_CURSOR       OUT cursor_facturare) IS
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);

    OPEN V_CURSOR FOR
      SELECT rownum as id_c,
             B.ID_ARTICOL,
             NULL AS LOT,
             NULL as SERIE,
             A.ID_POL,
             B.ID_VALUTA,
             A.NUME_LISTA_PRETURI,
             (CASE
               WHEN NVL(E.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(NVL(D.CURS, 0) *
                      ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                            pack_sesiune.nzecimale_pretvval) /
                      NVL(D.MULTIPLICATOR, 1),
                      pack_sesiune.nzecimale_pretv)
               ELSE
                ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                      pack_sesiune.nzecimale_pretv)
             END) AS DISCOUNT_UNITAR,
             (CASE
               WHEN NVL(E.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                      pack_sesiune.nzecimale_pretvval)
               ELSE
                0
             END) AS DISCOUNT_UNITAR_VAL,
             C.CODMAT,
             C.CODBARE,
             C.DENUMIRE,
             NVL(C.UM, '') AS UM,
             C.IN_STOC AS GESTIONABIL,
             0 AS CANTITATE,
             B.PROC_TVAV,
             A.PRETURI_CU_TVA,
             D.CURS,
             D.MULTIPLICATOR,
             (CASE
               WHEN NVL(E.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(NVL(D.CURS, 0) *
                      ROUND(NVL(B.PRET, 0), pack_sesiune.nzecimale_pretvval) /
                      NVL(D.MULTIPLICATOR, 1),
                      pack_sesiune.nzecimale_pretv)
               ELSE
                ROUND(NVL(B.PRET, 0), pack_sesiune.nzecimale_pretv)
             END) AS PRET,
             (CASE
               WHEN NVL(E.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(NVL(B.PRET, 0), pack_sesiune.nzecimale_pretvval)
               ELSE
                0
             END) AS PRET_VAL,
             1 - NVL(E.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
             E.NUME_VAL
        FROM (select a1.id_util,
                     a3.id_pol,
                     a3.preturi_cu_tva,
                     a3.nume_lista_preturi,
                     a3.id_nota
                from utilizatori_rol_intern a1
                left join politici_grupuri a2
                  on a1.id_grup = a2.id_grup
                left join crm_politici_preturi a3
                  on a2.id_politica = a3.id_pol
                left join crm_note_vanzari a4
                  on a3.id_nota = a4.id_nota
                left join note_contabile a5
                  on a4.id_set = a5.id_set
               where a1.sters = 0
                 and a2.sters = 0
                 and a3.sters = 0
                 and a1.id_util = V_ID_UTIL
                 and NVL(a5.in_valuta, 0) = 0
                 and least(NVL(last_day(to_date(V_AN || V_LUNA, 'YYYYMM')),
                               sysdate),
                           sysdate) between a3.datai and
                     NVL(a3.datas, to_date('300012', 'YYYYMM'))
                 and NVL(a1.ID_SUCURSALA, -99) = NVL(V_ID_SUCURSALA, -99)
                 and a3.id_pol =
                     to_number(pack_sesiune.getoptiunefirma('IDPOLPRETFACTK'))) A
        LEFT JOIN CRM_POLITICI_PRET_ART B
          ON A.ID_POL = B.ID_POL
        LEFT JOIN NOM_ARTICOLE C
          ON B.ID_ARTICOL = C.ID_ARTICOL
        LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                     FROM CURS
                    WHERE DATA <= V_DATA_CURS
                      AND DATA2 >= V_DATA_CURS
                      AND STERS = 0) D
          ON B.ID_VALUTA = D.ID_VALUTA
        LEFT JOIN NOM_VALUTE E
          ON B.ID_VALUTA = E.ID_VALUTA
       WHERE C.IN_STOC = 0
         AND C.IN_CRM = 1
         AND C.STERS = 0
         AND C.INACTIV = 0
       ORDER BY C.DENUMIRE;

  END cursor_articole_k;
  -------------------------------------------------------------------
  PROCEDURE cursor_avize(V_LISTAID  IN VARCHAR2,
                         V_ID_UTIL  IN NUMBER,
                         V_DISCOUNT OUT NUMBER,
                         V_CURSOR   OUT cursor_facturare) IS
    V_SEPARATOR VARCHAR2(10) := ',';
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);

    V_DISCOUNT := 0;

    OPEN V_CURSOR FOR
      WITH CRS AS
       (SELECT X as ID_VANZARE
          FROM table(charn2collection(V_LISTAID, V_SEPARATOR)))
      SELECT ROWNUM AS ID_C,
             A.ID_ARTICOL,
             A.LOT,
             A.SERIE,
             A.ID_POL,
             A.ID_VALUTA,
             (CASE
               WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.CURS *
                      ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                            pack_sesiune.nzecimale_pretvval) /
                      NVL(A.MULTIPLICATOR, 1),
                      pack_sesiune.nzecimale_pretv)
               ELSE
                ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                      pack_sesiune.nzecimale_pretv)
             END) AS DISCOUNT_UNITAR,
             (CASE
               WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                      pack_sesiune.nzecimale_pretvval)
               ELSE
                0
             END) AS DISCOUNT_UNITAR_VAL,
             C.CODMAT,
             C.CODBARE,
             C.DENUMIRE,
             NVL(C.UM, '') AS UM,
             0 AS GESTIONABIL,
             A.CANTITATE - NVL(B.CANTITATE, 0) AS CANTITATE,
             A.PROC_TVAV,
             A.ID_JTVA_COLOANA,
             A.ID_GESTIUNE,
             A.CONT,
             A.PRET_CU_TVA AS PRETURI_CU_TVA,
             A.CURS,
             A.MULTIPLICATOR,
             A.PRET_ACHIZITIE,
             A.PRETD,
             A.ID_VALUTAD AS ID_VALUTA_D,
             (CASE
               WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.CURS *
                      ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) /
                      NVL(A.MULTIPLICATOR, 1),
                      pack_sesiune.nzecimale_pretv)
               ELSE
                ROUND(A.PRET, pack_sesiune.nzecimale_pretv)
             END) AS PRET,
             (CASE
               WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.PRET, pack_sesiune.nzecimale_pretvval)
               ELSE
                0
             END) AS PRET_VAL,
             1 - NVL(D.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
             D.NUME_VAL
        FROM (SELECT A1.ID_ARTICOL,
                     A1.LOT,
                     A1.SERIE,
                     A1.ID_POL,
                     A1.DISCOUNT_UNITAR,
                     SUM(A1.CANTITATE) AS CANTITATE,
                     A1.PROC_TVAV,
                     A1.ID_JTVA_COLOANA,
                     NVL(A1.ID_GESTIUNE, -1000) AS ID_GESTIUNE,
                     NVL(A1.CONT, 'XXXX') AS CONT,
                     A1.PRET_CU_TVA,
                     A1.PRET_ACHIZITIE,
                     A1.PRETD,
                     A1.ID_VALUTAD,
                     A1.PRET,
                     A1.ID_VALUTA,
                     NVL(B1.CURS, 0) AS CURS,
                     NVL(B1.MULTIPLICATOR, 1) AS MULTIPLICATOR
                FROM VANZARI_DETALII A1
                LEFT JOIN VANZARI_CURSURI B1
                  ON A1.ID_VANZARE = B1.ID_VANZARE
                 AND A1.ID_VALUTA = B1.ID_VALUTA
               WHERE A1.STERS = 0
                 AND A1.ID_VANZARE IN (SELECT ID_VANZARE FROM CRS)
               GROUP BY A1.ID_ARTICOL,
                        A1.ID_POL,
                        A1.LOT,
                        A1.SERIE,
                        A1.DISCOUNT_UNITAR,
                        A1.PROC_TVAV,
                        A1.ID_JTVA_COLOANA,
                        NVL(A1.ID_GESTIUNE, -1000),
                        NVL(A1.CONT, 'XXXX'),
                        A1.PRET_CU_TVA,
                        A1.PRET_ACHIZITIE,
                        A1.PRETD,
                        A1.ID_VALUTAD,
                        A1.PRET,
                        A1.ID_VALUTA,
                        NVL(B1.CURS, 0),
                        NVL(B1.MULTIPLICATOR, 1)) A
        LEFT JOIN (SELECT A2.ID_ARTICOL,
                          A2.LOT,
                          A2.SERIE,
                          A2.ID_POL,
                          A2.DISCOUNT_UNITAR,
                          SUM(B2.CANTITATE) AS CANTITATE,
                          A2.PROC_TVAV,
                          A2.ID_JTVA_COLOANA,
                          NVL(A2.ID_GESTIUNE, -1000) AS ID_GESTIUNE,
                          NVL(A2.CONT, 'XXXX') AS CONT,
                          A2.PRET_CU_TVA,
                          A2.PRET_ACHIZITIE,
                          A2.PRETD,
                          A2.ID_VALUTAD,
                          A2.PRET,
                          A2.ID_VALUTA
                     FROM VANZARI_DETALII A2
                    RIGHT JOIN VANZARI_CANTITATI B2
                       ON A2.ID_VANZARE_DET = B2.ID_VANZARE_DET_AVIZ
                    WHERE A2.STERS = 0
                      AND A2.ID_VANZARE IN (SELECT ID_VANZARE FROM CRS)
                      AND B2.STERS = 0
                    GROUP BY A2.ID_ARTICOL,
                             A2.ID_POL,
                             A2.LOT,
                             A2.SERIE,
                             A2.DISCOUNT_UNITAR,
                             A2.PROC_TVAV,
                             A2.ID_JTVA_COLOANA,
                             NVL(A2.ID_GESTIUNE, -1000),
                             NVL(A2.CONT, 'XXXX'),
                             A2.PRET_CU_TVA,
                             A2.PRET_ACHIZITIE,
                             A2.PRETD,
                             A2.ID_VALUTAD,
                             A2.PRET,
                             A2.ID_VALUTA) B
          ON A.ID_ARTICOL = B.ID_ARTICOL
         AND A.ID_POL = B.ID_POL
         AND NVL(A.LOT, '+_') = NVL(B.LOT, '+_')
         AND NVL(A.SERIE, '+_') = NVL(B.SERIE, '+_')
         AND A.DISCOUNT_UNITAR = B.DISCOUNT_UNITAR
         AND A.PROC_TVAV = B.PROC_TVAV
         AND NVL(A.ID_JTVA_COLOANA, -9999) = NVL(B.ID_JTVA_COLOANA, -9999)
         AND NVL(A.ID_GESTIUNE, -9999) = NVL(B.ID_GESTIUNE, -9999)
         AND NVL(A.CONT, 'XXXX') = NVL(B.CONT, 'XXXX')
         AND A.PRET_CU_TVA = B.PRET_CU_TVA
         AND A.PRET = B.PRET
         AND A.ID_VALUTA = B.ID_VALUTA
         AND NVL(A.PRET_ACHIZITIE, 0) = NVL(B.PRET_ACHIZITIE, 0)
         AND NVL(A.PRETD, 0) = NVL(B.PRETD, 0)
         AND NVL(A.ID_VALUTAD, -99) = NVL(B.ID_VALUTAD, -99)
        LEFT JOIN NOM_ARTICOLE C
          ON A.ID_ARTICOL = C.ID_ARTICOL
        LEFT JOIN NOM_VALUTE D
          ON A.ID_VALUTA = D.ID_VALUTA
       WHERE A.CANTITATE <> NVL(B.CANTITATE, 0)
       ORDER BY C.DENUMIRE;

  END cursor_avize;
  -------------------------------------------------------------------
  PROCEDURE cursor_aviz_nir(V_ID_GESTIUNE_DEST IN NUMBER,
                            V_ID_FURNIZOR      IN NUMBER,
                            V_NR_ACT           IN NUMBER,
                            V_ID_SUCURSALA     IN NUMBER,
                            V_CURSOR           OUT cursor_facturare) IS

  BEGIN
    OPEN V_CURSOR FOR
      select rownum as id_c,
             rownum as id_temp,
             a.ID_ARTICOL,
             a.LOT,
             a.SERIE,
             a.ID_GESTIUNE,
             b.ID_VALUTA,
             0 as discount_unitar,
             c.CODMAT,
             c.DENUMIRE,
             Nvl(c.UM, Chr(32)) as um,
             1 as gestionabil,
             a.CANT as cantitate,
             a.PROC_TVAV,
             decode(d.nr_pag, 6, 1, 7, 1, 0) as preturi_cu_tva,
             0 as CURS,
             1 as multiplicator,
             a.PRET as preta,
             a.pretv + a.tvav as pretctva,
             a.pretv as pretftva,
             a.tvav,
             0 as pret_val,
             b.nume_val,
             0 as discount_unitar_val,
             0 as tip_valuta,
             a.cont,
             a.pretd,
             a.id_valuta as id_valuta_d
        from rul A
        left join (select id_valuta, nume_val
                     from vnom_valute
                    where moneda_nationala = 1
                      and rownum = 1) b
          on 1 = 1
        left join nom_articole c
          on a.id_articol = c.id_articol
        left join nom_gestiuni d
          on a.id_gestiune = d.id_gestiune
       where a.sters = 0
         and a.id_gestiune = V_ID_GESTIUNE_DEST
         and a.cod in (select max(a1.cod)
                         from act a1
                        where a1.sters = 0
                          and a1.id_partc = V_ID_FURNIZOR
                          and a1.scc = '401'
                          and a1.nract = V_NR_ACT
                          and NVL2(V_ID_SUCURSALA, a1.id_sucursala, -1) =
                              NVL(V_ID_SUCURSALA, -1));
  END cursor_aviz_nir;
  -------------------------------------------------------------------
  PROCEDURE cursor_retur(V_IN_VALUTA IN NUMBER,
                         V_LISTAID   IN VARCHAR2,
                         V_ID_UTIL   IN NUMBER,
                         V_CURSOR    OUT cursor_facturare) IS
    V_COPIERE  NUMBER := 0;
    V_PROFORMA NUMBER := 0;
  BEGIN
    pack_facturare.cursor_retur_document(V_IN_VALUTA,
                                         V_LISTAID,
                                         V_COPIERE,
                                         V_PROFORMA,
                                         V_ID_UTIL,
                                         V_CURSOR);
  END;

  PROCEDURE cursor_retur_document(V_IN_VALUTA IN NUMBER,
                                  V_LISTAID   IN VARCHAR2,
                                  V_COPIERE   IN NUMBER,
                                  V_PROFORMA  IN NUMBER,
                                  V_ID_UTIL   IN NUMBER,
                                  V_CURSOR    OUT cursor_facturare) IS
    V_SEPARATOR VARCHAR2(10) := ',';
    -- V_COPIERE: 1 (COPIERE) GESTIONABIL = NOM_ARTICOLE.IN_STOC, 0 (RETUR) GESTIONABIL = VANZARI_DETALII.ID_GEST <> 0
    -- V_PROFORMA: Daca este proforma (1), fac articolele negestionabile sa pot alege orice cantitate
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);

    OPEN V_CURSOR FOR
      WITH CRS AS
       (SELECT X as ID_VANZARE
          FROM table(charn2collection(V_LISTAID, V_SEPARATOR)))
      SELECT ROWNUM AS ID_C,
             A.ID_ARTICOL,
             A.LOT,
             A.SERIE,
             A.ID_POL,
             A.ID_VALUTA,
             (CASE
               WHEN NVL(C.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.CURS *
                      ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                            pack_sesiune.nzecimale_pretvval) /
                      NVL(A.MULTIPLICATOR, 1),
                      pack_sesiune.nzecimale_pretv)
               ELSE
                ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                      pack_sesiune.nzecimale_pretv)
             END) AS DISCOUNT_UNITAR,
             (CASE
               WHEN NVL(C.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                      pack_sesiune.nzecimale_pretvval)
               ELSE
                0
             END) AS DISCOUNT_UNITAR_VAL,
             B.CODMAT,
             B.CODBARE,
             B.DENUMIRE,
             NVL(B.UM, '') AS UM,
             (case
               when V_PROFORMA = 1 then
                0
               when V_COPIERE = 1 then
                B.IN_STOC
               else
                A.GESTIONABIL
             end) AS GESTIONABIL,
             A.CANTITATE,
             A.PROC_TVAV,
             A.ID_JTVA_COLOANA,
             A.PRET_CU_TVA AS PRETURI_CU_TVA,
             A.CURS,
             A.MULTIPLICATOR,
             (CASE
               WHEN NVL(C.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.CURS *
                      ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) /
                      NVL(A.MULTIPLICATOR, 1),
                      pack_sesiune.nzecimale_pretv)
               ELSE
                ROUND(A.PRET, pack_sesiune.nzecimale_pretv)
             END) + A.DIFERENTA AS PRET,
             (CASE
               WHEN NVL(C.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.PRET, pack_sesiune.nzecimale_pretvval)
               ELSE
                0
             END) AS PRET_VAL,
             1 - NVL(C.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
             C.NUME_VAL,
             A.EXPLICATIE,
             nvl(A.ID_GESTIUNE, 0) as ID_GESTIUNE,
             A.PRET_ACHIZITIE,
             A.PRETD,
             A.ID_JTVA_COLOANA_EX
        FROM (SELECT A1.ID_ARTICOL,
                     A1.LOT,
                     A1.SERIE,
                     A1.ID_POL,
                     A1.DISCOUNT_UNITAR,
                     A1.DIFERENTA,
                     --SUM(A1.CANTITATE) AS CANTITATE,
                     A1.CANTITATE,
                     A1.PROC_TVAV,
                     A1.ID_JTVA_COLOANA,
                     NVL2(A1.ID_GESTIUNE, 1, 0) AS GESTIONABIL,
                     A1.PRET_CU_TVA,
                     A1.PRET,
                     A1.ID_VALUTA,
                     NVL(A2.CURS, 0) AS CURS,
                     NVL(A2.MULTIPLICATOR, 1) AS MULTIPLICATOR,
                     A1.EXPLICATIE,
                     A1.ID_GESTIUNE,
                     A1.PRET_ACHIZITIE,
                     A1.PRETD,
                     A1.ID_JTVA_COLOANA_EX
                FROM VANZARI_DETALII A1
                LEFT JOIN VANZARI_CURSURI A2
                  ON A1.ID_VANZARE = A2.ID_VANZARE
                 AND A1.ID_VALUTA = A2.ID_VALUTA
               WHERE A1.STERS = 0
                 AND A1.ID_VANZARE IN (SELECT ID_VANZARE FROM CRS)) A
        LEFT JOIN NOM_ARTICOLE B
          ON A.ID_ARTICOL = B.ID_ARTICOL
        LEFT JOIN NOM_VALUTE C
          ON A.ID_VALUTA = C.ID_VALUTA
       ORDER BY B.DENUMIRE;

  END cursor_retur_document;
  -----------------------------------------------------------------
  PROCEDURE cursor_copiere(V_IN_VALUTA IN NUMBER,
                           V_LISTAID   IN VARCHAR2,
                           V_ID_UTIL   IN NUMBER,
                           V_CURSOR    OUT cursor_facturare) IS
    V_SEPARATOR VARCHAR2(10) := ',';
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);

    OPEN V_CURSOR FOR
      WITH CRS AS
       (SELECT X as ID_VANZARE
          FROM table(charn2collection(V_LISTAID, V_SEPARATOR)))
      SELECT ROWNUM AS ID_C,
             A.ID_ARTICOL,
             A.LOT,
             A.SERIE,
             A.ID_POL,
             A.ID_VALUTA,
             (CASE
               WHEN NVL(C.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.CURS *
                      ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                            pack_sesiune.nzecimale_pretvval) /
                      NVL(A.MULTIPLICATOR, 1),
                      pack_sesiune.nzecimale_pretv)
               ELSE
                ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                      pack_sesiune.nzecimale_pretv)
             END) AS DISCOUNT_UNITAR,
             (CASE
               WHEN NVL(C.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                      pack_sesiune.nzecimale_pretvval)
               ELSE
                0
             END) AS DISCOUNT_UNITAR_VAL,
             B.CODMAT,
             B.CODBARE,
             B.DENUMIRE,
             NVL(B.UM, '') AS UM,
             A.GESTIONABIL,
             A.CANTITATE,
             A.PROC_TVAV,
             A.ID_JTVA_COLOANA,
             A.PRET_CU_TVA AS PRETURI_CU_TVA,
             A.CURS,
             A.MULTIPLICATOR,
             (CASE
               WHEN NVL(C.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.CURS *
                      ROUND(A.PRET, pack_sesiune.nzecimale_pretvval) /
                      NVL(A.MULTIPLICATOR, 1),
                      pack_sesiune.nzecimale_pretv)
               ELSE
                ROUND(A.PRET, pack_sesiune.nzecimale_pretv)
             END) + A.DIFERENTA AS PRET,
             (CASE
               WHEN NVL(C.MONEDA_NATIONALA, 1) <> 1 THEN
                ROUND(A.PRET, pack_sesiune.nzecimale_pretvval)
               ELSE
                0
             END) AS PRET_VAL,
             1 - NVL(C.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
             C.NUME_VAL
        FROM (SELECT A1.ID_ARTICOL,
                     A1.LOT,
                     A1.SERIE,
                     A1.ID_POL,
                     A1.DISCOUNT_UNITAR,
                     A1.DIFERENTA,
                     --SUM(A1.CANTITATE) AS CANTITATE,
                     A1.CANTITATE,
                     A1.PROC_TVAV,
                     A1.ID_JTVA_COLOANA,
                     NVL2(A1.ID_GESTIUNE, 1, 0) AS GESTIONABIL,
                     A1.PRET_CU_TVA,
                     A1.PRET,
                     A1.ID_VALUTA,
                     NVL(A2.CURS, 0) AS CURS,
                     NVL(A2.MULTIPLICATOR, 1) AS MULTIPLICATOR
                FROM VANZARI_DETALII A1
                LEFT JOIN VANZARI_CURSURI A2
                  ON A1.ID_VANZARE = A2.ID_VANZARE
                 AND A1.ID_VALUTA = A2.ID_VALUTA
               WHERE A1.STERS = 0
                 AND A1.ID_VANZARE IN (SELECT ID_VANZARE FROM CRS)) A
        LEFT JOIN NOM_ARTICOLE B
          ON A.ID_ARTICOL = B.ID_ARTICOL
        LEFT JOIN NOM_VALUTE C
          ON A.ID_VALUTA = C.ID_VALUTA
       ORDER BY B.DENUMIRE;

  END cursor_copiere;
  -------------------------------------------------------------------
  PROCEDURE cursor_gestiune(V_DATA_CURS    IN DATE,
                            V_ID_POL       IN NUMBER,
                            V_ID_GESTIUNE  IN NUMBER,
                            V_LUNA         IN NUMBER,
                            V_AN           IN NUMBER,
                            V_ID_UTIL      IN NUMBER,
                            V_ID_SUCURSALA IN NUMBER,
                            V_CURSOR       OUT cursor_facturare) IS
    V_NR_INREGISTRARI     NUMBER(10);
    V_NR_INREGISTRARI_TOT NUMBER(10);
  BEGIN
    pack_facturare.initializeaza_facturare(V_ID_UTIL);

    -- verificare ca exista cursul pentru valutele din CRM_POLITICI_PRET_ART
    SELECT SUM(CASE
                 WHEN B.ID_VALUTA <> pack_facturare.nid_moneda_nationala THEN
                  1
                 ELSE
                  0
               END),
           SUM(CASE
                 WHEN A.ID_VALUTA <> pack_facturare.nid_moneda_nationala THEN
                  1
                 ELSE
                  0
               END)
      INTO V_NR_INREGISTRARI, V_NR_INREGISTRARI_TOT
      FROM (SELECT DISTINCT A2.ID_VALUTA
              FROM STOC A1
              LEFT JOIN CRM_POLITICI_PRET_ART A2
                ON A1.ID_ARTICOL = A2.ID_ARTICOL
             WHERE A1.ID_GESTIUNE = V_ID_GESTIUNE
               AND A2.ID_POL = V_ID_POL
               AND NVL(A1.ID_SUCURSALA, -99) = NVL(V_ID_SUCURSALA, -99)) A
      LEFT JOIN CURS B
        ON A.ID_VALUTA = B.ID_VALUTA
       AND B.STERS = 0
       AND B.DATA <= V_DATA_CURS
       AND B.DATA2 >= V_DATA_CURS;

    IF V_NR_INREGISTRARI <> V_NR_INREGISTRARI_TOT THEN
      RAISE_APPLICATION_ERROR(-20005,
                              'Nu este setat cursul din data de ' ||
                              to_char(V_DATA_CURS, 'DD/MM/YYYY') || ' !');
    END IF;

    -- nu se mai verifica dreptul pe gestiune
    -- v_id_gestiune este dintre gestiunile pe care are drepturi utilizatorul
    OPEN V_CURSOR FOR
      SELECT ROWNUM as id_c, A.*
        FROM (SELECT A.ID_ARTICOL,
                     NULL AS LOT,
                     NULL AS SERIE,
                     V_ID_GESTIUNE AS ID_GESTIUNE,
                     --A.CONT,
                     B.ID_POL,
                     B.ID_VALUTA,
                     D.NUME_LISTA_PRETURI,
                     (CASE
                       WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(E.CURS, 0) *
                              ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                                    pack_sesiune.nzecimale_pretvval) /
                              NVL(E.MULTIPLICATOR, 1),
                              pack_sesiune.nzecimale_pretv)
                       ELSE
                        ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                              pack_sesiune.nzecimale_pretv)
                     END) AS DISCOUNT_UNITAR,
                     (CASE
                       WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                              pack_sesiune.nzecimale_pretvval)
                       ELSE
                        0
                     END) AS DISCOUNT_UNITAR_VAL,
                     C.CODMAT,
                     C.CODBARE,
                     C.DENUMIRE,
                     NVL(C.UM, '') AS UM,
                     1 AS GESTIONABIL,
                     SUM(NVL(A.CANTS + A.CANT - A.CANTE, 0)) AS CANTITATE,
                     B.PROC_TVAV,
                     D.PRETURI_CU_TVA,
                     E.CURS,
                     E.MULTIPLICATOR,
                     (CASE
                       WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(E.CURS, 0) *
                              ROUND(B.PRET, pack_sesiune.nzecimale_pretvval) /
                              NVL(E.MULTIPLICATOR, 1),
                              pack_sesiune.nzecimale_pretv)
                       ELSE
                        ROUND(B.PRET, pack_sesiune.nzecimale_pretv)
                     END) AS PRET,
                     (CASE
                       WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(B.PRET, pack_sesiune.nzecimale_pretvval)
                       ELSE
                        0
                     END) AS PRET_VAL,
                     1 - NVL(F.MONEDA_NATIONALA, 1) AS TIP_VALUTA,
                     F.NUME_VAL,
                     DECODE(B.ID_POL,
                            NVL(pack_facturare.nid_politica_stoc,
                                B.ID_POL + 1),
                            1,
                            0) AS MODIFICABIL
                FROM (SELECT ID_ARTICOL, CANT, CANTS, CANTE
                        FROM STOC
                       WHERE AN = V_AN
                         AND LUNA = V_LUNA
                         AND ID_GESTIUNE = V_ID_GESTIUNE
                         AND NVL(ID_SUCURSALA, -99) =
                             NVL(V_ID_SUCURSALA, -99)) A
                JOIN (SELECT ID_ARTICOL,
                            ID_POL,
                            ID_VALUTA,
                            DISCOUNT_UNITAR,
                            PROC_TVAV,
                            PRET
                       FROM CRM_POLITICI_PRET_ART
                      WHERE ID_POL = V_ID_POL) B
                  ON A.ID_ARTICOL = B.ID_ARTICOL
                LEFT JOIN NOM_ARTICOLE C
                  ON A.ID_ARTICOL = C.ID_ARTICOL
                LEFT JOIN CRM_POLITICI_PRETURI D
                  ON B.ID_POL = D.ID_POL
                LEFT JOIN (SELECT ID_VALUTA, CURS, MULTIPLICATOR
                            FROM CURS
                           WHERE DATA <= V_DATA_CURS
                             AND DATA2 >= V_DATA_CURS
                             AND STERS = 0) E
                  ON B.ID_VALUTA = E.ID_VALUTA
                LEFT JOIN NOM_VALUTE F
                  ON B.ID_VALUTA = F.ID_VALUTA
               GROUP BY A.ID_ARTICOL,
                        -- A.CONT,
                        B.ID_POL,
                        B.ID_VALUTA,
                        D.NUME_LISTA_PRETURI,
                        (CASE
                          WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                           ROUND(NVL(E.CURS, 0) *
                                 ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                                       pack_sesiune.nzecimale_pretvval) /
                                 NVL(E.MULTIPLICATOR, 1),
                                 pack_sesiune.nzecimale_pretv)
                          ELSE
                           ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                                 pack_sesiune.nzecimale_pretv)
                        END),
                        (CASE
                          WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                           ROUND(NVL(B.DISCOUNT_UNITAR, 0),
                                 pack_sesiune.nzecimale_pretvval)
                          ELSE
                           0
                        END),
                        C.CODMAT,
                        C.CODBARE,
                        C.DENUMIRE,
                        NVL(C.UM, ''),
                        C.IN_STOC,
                        B.PROC_TVAV,
                        D.PRETURI_CU_TVA,
                        E.CURS,
                        E.MULTIPLICATOR,
                        (CASE
                          WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                           ROUND(NVL(E.CURS, 0) *
                                 ROUND(B.PRET,
                                       pack_sesiune.nzecimale_pretvval) /
                                 NVL(E.MULTIPLICATOR, 1),
                                 pack_sesiune.nzecimale_pretv)
                          ELSE
                           ROUND(B.PRET, pack_sesiune.nzecimale_pretv)
                        END),
                        (CASE
                          WHEN NVL(F.MONEDA_NATIONALA, 1) <> 1 THEN
                           ROUND(B.PRET, pack_sesiune.nzecimale_pretvval)
                          ELSE
                           0
                        END),
                        1 - NVL(F.MONEDA_NATIONALA, 1),
                        F.NUME_VAL
               ORDER BY C.DENUMIRE) A
       WHERE A.CANTITATE > 0;

  END cursor_gestiune;
  -------------------------------------------------------------------
  PROCEDURE cursor_gestiuni_articol(V_ID_ARTICOL   IN NUMBER,
                                    V_TIP          IN NUMBER,
                                    V_ID_GESTIUNE  IN NUMBER,
                                    V_LUNA         IN NUMBER,
                                    V_AN           IN NUMBER,
                                    V_ID_UTIL      IN NUMBER,
                                    V_ID_SUCURSALA IN NUMBER,
                                    V_CURSOR       OUT cursor_facturare) IS
  BEGIN
    OPEN V_CURSOR FOR
      SELECT 0                AS ALES,
             A.ID_GESTIUNE,
             A.CANTITATE,
             A.CONT,
             A.ACONT,
             A.LOT,
             A.SERIE,
             A.PRET,
             A.PRETD,
             A.ID_VALUTA,
             B.NUME_GESTIUNE,
             A.DATAIN,
             A.PRETV,
             A.PRETVCTVA,
             A.ID_PART_REZ,
             A.ID_LUCRARE_REZ,
             C.DENUMIRE       AS PART_REZ,
             D.NRORD          AS NRORD_REZ,
             E.NUME_VAL
        FROM (SELECT A1.ID_GESTIUNE,
                     A2.ID_ARTICOL,
                     A2.PRET,
                     A2.PRETD,
                     A2.ID_VALUTA,
                     A2.LOT,
                     A2.SERIE,
                     SUM(NVL(A2.CANTS + A2.CANT - A2.CANTE, 0)) AS CANTITATE,
                     A2.CONT,
                     A2.ACONT,
                     A2.DATAIN,
                     A2.PRETV,
                     A2.PRETV + A2.TVAV as PRETVCTVA,
                     A2.ID_PART_REZ,
                     A2.ID_LUCRARE_REZ
                FROM (SELECT DISTINCT A11.ID_GESTIUNE
                        FROM GEST_CORESP_GRUPE_GESTIUNI A11
                        LEFT JOIN GEST_CORESP_UTIL_GRUPE A12
                          ON A11.ID_GRUPE = A12.ID_GRUPE
                       WHERE A11.STERS = 0
                         AND A12.STERS = 0
                         AND A12.ID_UTIL = V_ID_UTIL
                         AND NVL2(V_ID_GESTIUNE, A11.ID_GESTIUNE, 2) =
                             NVL(V_ID_GESTIUNE, 2)) A1
                LEFT JOIN STOC A2
                  ON A1.ID_GESTIUNE = A2.ID_GESTIUNE
                 AND A2.LUNA = V_LUNA
                 AND A2.AN = V_AN
                 AND NVL2(V_ID_SUCURSALA, A2.ID_SUCURSALA, -99) =
                     NVL(V_ID_SUCURSALA, -99)
                 AND A2.ID_ARTICOL = V_ID_ARTICOL
               GROUP BY A1.ID_GESTIUNE,
                        A2.ID_ARTICOL,
                        A2.PRET,
                        A2.PRETD,
                        A2.ID_VALUTA,
                        A2.LOT,
                        A2.SERIE,
                        A2.CONT,
                        A2.ACONT,
                        A2.DATAIN,
                        A2.PRETV,
                        A2.TVAV,
                        A2.ID_PART_REZ,
                        A2.ID_LUCRARE_REZ) A
        LEFT JOIN NOM_GESTIUNI B
          ON A.ID_GESTIUNE = B.ID_GESTIUNE
        LEFT JOIN NOM_PARTENERI C
          ON A.ID_PART_REZ = C.ID_PART
        LEFT JOIN VNOM_LUCRARI D
          ON A.ID_LUCRARE_REZ = D.ID_LUCRARE
        LEFT JOIN NOM_VALUTE E
          ON A.ID_VALUTA = E.ID_VALUTA
       WHERE A.CANTITATE > 0
       ORDER BY A.DATAIN,
                B.NUME_GESTIUNE,
                A.LOT,
                A.SERIE,
                A.PRET,
                A.PRETV,
                A.CONT,
                A.ACONT,
                C.DENUMIRE,
                D.NRORD;

  END cursor_gestiuni_articol;

  -------------------------------------------------------------------
  -- adaugare articol gestionabil pe factura, daca nu exista in stoc
  -- pentru optiunea RF_FACTURARE_FARA_STOC = 1
  -- se selecteaza din stoc datele de pe ultima intrare, ultimele 12 luni
  -------------------------------------------------------------------
  PROCEDURE cursor_gestiuni_articol_stoc0(V_ID_ARTICOL   IN NUMBER,
                                          V_TIP          IN NUMBER,
                                          V_ID_GESTIUNE  IN NUMBER,
                                          V_LUNA         IN NUMBER,
                                          V_AN           IN NUMBER,
                                          V_ID_UTIL      IN NUMBER,
                                          V_ID_SUCURSALA IN NUMBER,
                                          V_CURSOR       OUT cursor_facturare) IS
  BEGIN
    OPEN V_CURSOR FOR
      SELECT 0 AS ALES,
             A.ID_GESTIUNE,
             A.CANTITATE,
             CAST(NVL2(B.CONT, B.CONT, NVL2(A.CONT, A.CONT, '371')) AS
                  VARCHAR2(4)) AS CONT,
             A.ACONT,
             A.LOT,
             A.SERIE,
             NVL(A.PRET, 0.00) AS PRET,
             NVL(A.PRETD, 0.00) AS PRETD,
             A.ID_VALUTA,
             B.NUME_GESTIUNE,
             A.DATAIN,
             NVL(A.PRETV, 0.00) AS PRETV,
             NVL(A.PRETVCTVA, 0.00) AS PRETVCTVA,
             A.ID_PART_REZ,
             A.ID_LUCRARE_REZ,
             C.DENUMIRE AS PART_REZ,
             D.NRORD AS NRORD_REZ,
             E.NUME_VAL
        FROM (SELECT A1.ID_GESTIUNE,
                     A2.ID_ARTICOL,
                     A2.PRET,
                     A2.PRETD,
                     A2.ID_VALUTA,
                     A2.LOT,
                     A2.SERIE,
                     1000 AS CANTITATE, -- cantitate suficienta 1000 pentru vanzarea articolelor gestionabile fara stoc
                     A2.CONT,
                     A2.ACONT,
                     A2.DATAIN,
                     A2.PRETV,
                     A2.PRETV + A2.TVAV as PRETVCTVA,
                     CAST(NULL AS Number(10, 0)) AS ID_PART_REZ,
                     CAST(NULL AS Number(10, 0)) AS ID_LUCRARE_REZ
                FROM (SELECT DISTINCT A11.ID_GESTIUNE
                        FROM GEST_CORESP_GRUPE_GESTIUNI A11
                        JOIN GEST_CORESP_UTIL_GRUPE A12
                          ON A11.ID_GRUPE = A12.ID_GRUPE
                        JOIN NOM_GESTIUNI A13
                          ON A11.ID_GESTIUNE = A13.ID_GESTIUNE
                       WHERE A11.STERS = 0
                         AND A12.STERS = 0
                         AND A12.ID_UTIL = V_ID_UTIL
                         AND A13.STERS = 0
                         AND A13.INACTIV = 0
                         AND A13.NR_PAG IN (2, 4, 5, 6, 7)
                         AND NVL2(V_ID_GESTIUNE, A11.ID_GESTIUNE, 2) =
                             NVL(V_ID_GESTIUNE, 2)) A1
                LEFT JOIN STOC A2
                  ON A1.ID_GESTIUNE = A2.ID_GESTIUNE
                 AND A2.AN >= V_AN - 1 -- anul precedent si anul curent
                 AND A2.PRET <> 0
                 AND NVL2(V_ID_SUCURSALA, A2.ID_SUCURSALA, -99) =
                     NVL(V_ID_SUCURSALA, -99)
                 AND A2.ID_ARTICOL = V_ID_ARTICOL
               GROUP BY A1.ID_GESTIUNE,
                        A2.ID_ARTICOL,
                        A2.PRET,
                        A2.PRETD,
                        A2.ID_VALUTA,
                        A2.LOT,
                        A2.SERIE,
                        A2.CONT,
                        A2.ACONT,
                        A2.DATAIN,
                        A2.PRETV,
                        A2.TVAV,
                        A2.ID_PART_REZ,
                        A2.ID_LUCRARE_REZ) A
        LEFT JOIN NOM_GESTIUNI B
          ON A.ID_GESTIUNE = B.ID_GESTIUNE
        LEFT JOIN NOM_PARTENERI C
          ON A.ID_PART_REZ = C.ID_PART
        LEFT JOIN VNOM_LUCRARI D
          ON A.ID_LUCRARE_REZ = D.ID_LUCRARE
        LEFT JOIN NOM_VALUTE E
          ON A.ID_VALUTA = E.ID_VALUTA
       ORDER BY NVL(A.DATAIN, TO_DATE('1900', 'YYYY')) DESC,
                B.NUME_GESTIUNE,
                A.LOT,
                A.SERIE,
                A.PRET,
                A.PRETV,
                A.CONT,
                A.ACONT,
                C.DENUMIRE,
                D.NRORD;

  END cursor_gestiuni_articol_stoc0;
  -------------------------------------------------------------------
  PROCEDURE cursor_gestiuni_articol_retur(V_ID_ARTICOL   IN NUMBER,
                                          V_TIP          IN NUMBER,
                                          V_ID_GESTIUNE  IN NUMBER,
                                          V_PRETV        IN NUMBER,
                                          V_DISCOUNT     IN NUMBER,
                                          V_LUNA         IN NUMBER,
                                          V_AN           IN NUMBER,
                                          V_ID_UTIL      IN NUMBER,
                                          V_LISTAID      IN VARCHAR2,
                                          V_ID_SUCURSALA IN NUMBER,
                                          V_CURSOR       OUT cursor_facturare) IS
    V_SEPARATOR VARCHAR2(10) := ',';
  BEGIN
    OPEN V_CURSOR FOR
      WITH CRS AS
       (

        SELECT X as ID_VANZARE
          FROM table(charn2collection(V_LISTAID, V_SEPARATOR))

        )
      SELECT 0                 AS ALES,
             A1.ID_GESTIUNE,
             A2.CANTITATE,
             A2.CONT,
             A2.ACONT,
             A2.LOT,
             A2.SERIE,
             A2.PRET,
             A2.PRETD,
             A2.ID_VALUTA,
             B.NUME_GESTIUNE,
             A2.DATAIN,
             A2.PRETV,
             A2.PRETVCTVA,
             A2.ID_PART_REZ,
             A2.ID_LUCRARE_REZ,
             C.DENUMIRE        AS PART_REZ,
             D.NRORD           AS NRORD_REZ,
             E.NUME_VAL
        FROM (SELECT DISTINCT A11.ID_GESTIUNE
                FROM GEST_CORESP_GRUPE_GESTIUNI A11
                LEFT JOIN GEST_CORESP_UTIL_GRUPE A12
                  ON A11.ID_GRUPE = A12.ID_GRUPE
               WHERE A11.STERS = 0
                 AND A12.STERS = 0
                 AND A12.ID_UTIL = V_ID_UTIL
                 AND NVL2(V_ID_GESTIUNE, A11.ID_GESTIUNE, 2) =
                     NVL(V_ID_GESTIUNE, 2)) A1
        LEFT JOIN (SELECT SUM(A22.CANTE) AS CANTITATE,
                          A22.ID_GESTIUNE,
                          A23.NR_PAG,
                          A22.ID_ARTICOL,
                          A22.PRET,
                          A22.PRETD,
                          A22.ID_VALUTA,
                          A22.LOT,
                          A22.SERIE,
                          A22.CONT,
                          A22.ACONT,
                          A22.DATAIN,
                          A22.PRETV,
                          A22.PRETV + A22.TVAV AS PRETVCTVA,
                          A22.ID_PART_REZ,
                          A22.ID_LUCRARE_REZ
                     FROM (SELECT COD
                             FROM VANZARI
                            WHERE ID_VANZARE IN (SELECT ID_VANZARE FROM CRS)) A21
                     LEFT JOIN RUL A22
                       ON A21.COD = A22.COD
                      AND A22.STERS = 0
                      AND A22.ID_TIP_RULAJ = 0
                      AND A22.ID_ARTICOL = V_ID_ARTICOL
                      AND NVL2(V_ID_SUCURSALA, A22.ID_SUCURSALA, -99) =
                          NVL(V_ID_SUCURSALA, -99)
                     LEFT JOIN NOM_GESTIUNI A23
                       ON A22.ID_GESTIUNE = A23.ID_GESTIUNE
                    GROUP BY A22.ID_GESTIUNE,
                             A23.NR_PAG,
                             A22.ID_ARTICOL,
                             A22.PRET,
                             A22.PRETD,
                             A22.ID_VALUTA,
                             A22.LOT,
                             A22.SERIE,
                             A22.CONT,
                             A22.ACONT,
                             A22.DATAIN,
                             A22.PRETV,
                             A22.TVAV,
                             A22.ID_PART_REZ,
                             A22.ID_LUCRARE_REZ) A2
          ON A1.ID_GESTIUNE = A2.ID_GESTIUNE
        LEFT JOIN NOM_GESTIUNI B
          ON A1.ID_GESTIUNE = B.ID_GESTIUNE
        LEFT JOIN NOM_PARTENERI C
          ON A2.ID_PART_REZ = C.ID_PART
        LEFT JOIN VNOM_LUCRARI D
          ON A2.ID_LUCRARE_REZ = D.ID_LUCRARE
        LEFT JOIN NOM_VALUTE E
          ON A2.ID_VALUTA = E.ID_VALUTA
       WHERE A2.CANTITATE > 0
       ORDER BY A2.DATAIN,
                B.NUME_GESTIUNE,
                A2.LOT,
                A2.SERIE,
                A2.PRET,
                A2.PRETV,
                A2.CONT,
                A2.ACONT,
                C.DENUMIRE,
                D.NRORD;

  END cursor_gestiuni_articol_retur;
  -------------------------------------------------------------------
  PROCEDURE adauga_articol_factura_deviz(V_ID_ARTICOL         IN NUMBER,
                                         V_EXPLICATIE         IN VARCHAR2,
                                         V_SERIE              IN VARCHAR2,
                                         V_PRET_ACHIZITIE     IN NUMBER,
                                         V_PRETD              IN NUMBER,
                                         V_ID_VALUTAD         IN NUMBER,
                                         V_PRET               IN NUMBER,
                                         V_ID_VALUTA          IN NUMBER,
                                         V_CURS               IN NUMBER,
                                         V_MULTIPLICATOR      IN NUMBER,
                                         V_PROC_TVAV          IN NUMBER,
                                         V_ID_JTVA_COLOANA    IN NUMBER,
                                         V_CANTITATE          IN NUMBER,
                                         V_DISCOUNT_UNITAR    IN NUMBER,
                                         V_ID_GESTIUNE        IN NUMBER,
                                         V_CONT               IN NUMBER,
                                         V_PRET_CU_TVA        IN NUMBER,
                                         V_ID_JTVA_COLOANA_EX IN NUMBER DEFAULT NULL,
                                         V_ID_CTR             IN NUMBER DEFAULT NULL,
                                         V_TAXCODE            IN NUMBER DEFAULT NULL,
                                         V_LOT                IN VARCHAR2 DEFAULT NULL) IS
  BEGIN
    INSERT INTO VANZARI_DETALII_TEMP
      (ID_ARTICOL,
       EXPLICATIA,
       SERIE,
       PRET_ACHIZITIE,
       PRETD,
       ID_VALUTAD,
       PRET,
       PROC_TVAV,
       ID_JTVA_COLOANA,
       ID_JTVA_COLOANA_EX,
       CANTITATE,
       DISCOUNT_UNITAR,
       ID_GESTIUNE,
       CONT,
       ID_VALUTA,
       CURS,
       MULTIPLICATOR,
       PRET_CU_TVA,
       IN_STOC,
       ID_TEMP,
       ID_CTR,
       TAXCODE,
       LOT)
    VALUES
      (V_ID_ARTICOL,
       V_EXPLICATIE,
       V_SERIE,
       V_PRET_ACHIZITIE,
       V_PRETD,
       V_ID_VALUTAD,
       V_PRET,
       V_PROC_TVAV,
       V_ID_JTVA_COLOANA,
       V_ID_JTVA_COLOANA_EX,
       V_CANTITATE,
       V_DISCOUNT_UNITAR,
       V_ID_GESTIUNE,
       V_CONT,
       NVL(V_ID_VALUTA, pack_facturare.nid_moneda_nationala),
       DECODE(V_CURS, 0, 1, V_CURS),
       V_MULTIPLICATOR,
       V_PRET_CU_TVA,
       1,
       0,
       V_ID_CTR,
       decode(V_TAXCODE, 0, null, V_TAXCODE),
       V_LOT);
  END adauga_articol_factura_deviz;
  -------------------------------------------------------------------
  PROCEDURE adauga_articol_factura_stoc(V_ID_ARTICOL      IN NUMBER,
                                        V_SERIE           IN VARCHAR2,
                                        V_PRET_ACHIZITIE  IN NUMBER,
                                        V_PRETD           IN NUMBER,
                                        V_ID_VALUTAD      IN NUMBER,
                                        V_PRET            IN NUMBER,
                                        V_ID_VALUTA       IN NUMBER,
                                        V_CURS            IN NUMBER,
                                        V_MULTIPLICATOR   IN NUMBER,
                                        V_PROC_TVAV       IN NUMBER,
                                        V_ID_JTVA_COLOANA IN NUMBER,
                                        V_CANTITATE       IN NUMBER,
                                        V_DISCOUNT_UNITAR IN NUMBER,
                                        V_ID_GESTIUNE     IN NUMBER,
                                        V_CONT            IN NUMBER,
                                        V_PRET_CU_TVA     IN NUMBER,
                                        V_EXPLICATIE      IN VARCHAR2,
                                        V_ID_RUL_AUX      IN NUMBER,
                                        V_TAXCODE         IN NUMBER DEFAULT NULL,
                                        V_LOT             IN VARCHAR2 DEFAULT NULL) IS
    lcExplicatia varchar2(240);
  BEGIN
    if pack_facturare.ntip = 50 and nvl(V_ID_RUL_AUX, 0) <> 0 then
      select to_char(V_CANTITATE / ROUND(LUNGIME * LATIME / 1000000, 2)) ||
             ' buc. - ' || to_char(LUNGIME) || 'x' || to_char(LATIME)
        into lcExplicatia
        from DEV_ESTIMARI_PRODUSE
       where id_dev_estimare_prod in
             (select id_dev_estimare_prod
                from rul_auxiliar
               where id_rul_aux = V_ID_RUL_AUX)
         and sters = 0;
    end if;
    INSERT INTO VANZARI_DETALII_TEMP
      (ID_ARTICOL,
       SERIE,
       PRET_ACHIZITIE,
       PRETD,
       ID_VALUTAD,
       PRET,
       PROC_TVAV,
       ID_JTVA_COLOANA,
       CANTITATE,
       DISCOUNT_UNITAR,
       ID_GESTIUNE,
       CONT,
       ID_VALUTA,
       CURS,
       MULTIPLICATOR,
       PRET_CU_TVA,
       IN_STOC,
       EXPLICATIA,
       ID_TEMP,
       TAXCODE,
       LOT)
    VALUES
      (V_ID_ARTICOL,
       V_SERIE,
       V_PRET_ACHIZITIE,
       V_PRETD,
       V_ID_VALUTAD,
       V_PRET,
       V_PROC_TVAV,
       V_ID_JTVA_COLOANA,
       V_CANTITATE,
       V_DISCOUNT_UNITAR,
       V_ID_GESTIUNE,
       V_CONT,
       NVL(V_ID_VALUTA, pack_facturare.nid_moneda_nationala),
       DECODE(V_CURS, 0, 1, V_CURS),
       V_MULTIPLICATOR,
       V_PRET_CU_TVA,
       1,
       lcExplicatia,
       0,
       decode(V_TAXCODE, 0, NULL, V_TAXCODE),
       V_LOT);
  END adauga_articol_factura_stoc;
  -------------------------------------------------------------------
  PROCEDURE adauga_articol_factura_stoc(V_ID_ARTICOL      IN NUMBER,
                                        V_SERIE           IN VARCHAR2,
                                        V_PRET_ACHIZITIE  IN NUMBER,
                                        V_PRETD           IN NUMBER,
                                        V_ID_VALUTAD      IN NUMBER,
                                        V_PRET            IN NUMBER,
                                        V_ID_VALUTA       IN NUMBER,
                                        V_CURS            IN NUMBER,
                                        V_MULTIPLICATOR   IN NUMBER,
                                        V_PROC_TVAV       IN NUMBER,
                                        V_ID_JTVA_COLOANA IN NUMBER,
                                        V_CANTITATE       IN NUMBER,
                                        V_DISCOUNT_UNITAR IN NUMBER,
                                        V_ID_GESTIUNE     IN NUMBER,
                                        V_CONT            IN NUMBER,
                                        V_PRET_CU_TVA     IN NUMBER) is
  begin
    adauga_articol_factura_stoc(V_ID_ARTICOL,
                                V_SERIE,
                                V_PRET_ACHIZITIE,
                                V_PRETD,
                                V_ID_VALUTAD,
                                V_PRET,
                                V_ID_VALUTA,
                                V_CURS,
                                V_MULTIPLICATOR,
                                V_PROC_TVAV,
                                V_ID_JTVA_COLOANA,
                                V_CANTITATE,
                                V_DISCOUNT_UNITAR,
                                V_ID_GESTIUNE,
                                V_CONT,
                                V_PRET_CU_TVA,
                                null,
                                null,
                                null,
                                null);
  end;
  -------------------------------------------------------------------

  PROCEDURE adauga_articol_lucrare_pret(V_ID_COMANDA       IN NUMBER,
                                        V_ID_ARTICOL       IN NUMBER,
                                        V_ID_POL           IN NUMBER,
                                        V_CANTITATE        IN NUMBER,
                                        V_CONT             IN VARCHAR2,
                                        V_CURS             IN NUMBER,
                                        V_MULTIPLICATOR    IN NUMBER,
                                        V_ID_JTVA_COLOANA  IN NUMBER,
                                        V_ID_GESTIUNE_DEST IN NUMBER,
                                        V_ID_UTIL          IN NUMBER,
                                        V_PRETIN           IN NUMBER) IS
    V_PRET            CRM_POLITICI_PRET_ART.PRET%TYPE;
    V_DISCOUNT_UNITAR CRM_POLITICI_PRET_ART.DISCOUNT_UNITAR%TYPE;
    V_PROC_TVAV       CRM_POLITICI_PRET_ART.PROC_TVAV%TYPE;
    V_ID_VALUTA       CRM_POLITICI_PRET_ART.ID_VALUTA%TYPE;
    V_PRETURI_CU_TVA  CRM_POLITICI_PRETURI.PRETURI_CU_TVA%TYPE;
    V_DENUMIRE        NOM_ARTICOLE.DENUMIRE%TYPE;
    V_CODMAT          NOM_ARTICOLE.CODMAT%TYPE;
    V_NR_COMANDA      COMENZI.NR_COMANDA%TYPE;
  BEGIN
    BEGIN
      SELECT NVL(V_PRETIN, A.PRET) as PRET,
             NVL(C.PROC_TVAV, (D.COTA_TVA + 100) / 100),
             C.ID_VALUTA,
             B.PRETURI_CU_TVA,
             NVL(C.DISCOUNT_UNITAR, 0) + NVL(A.DISCOUNT_UNITAR, 0)
        INTO V_PRET,
             V_PROC_TVAV,
             V_ID_VALUTA,
             V_PRETURI_CU_TVA,
             V_DISCOUNT_UNITAR
        FROM COMENZI_ELEMENTE A
        LEFT JOIN CRM_POLITICI_PRETURI B
          ON A.ID_POL = B.ID_POL
        LEFT JOIN CRM_POLITICI_PRET_ART C
          ON A.ID_POL = C.ID_POL
         AND A.ID_ARTICOL = C.ID_ARTICOL
        LEFT JOIN JTVA_COLOANE D
          ON D.ID_JTVA_COLOANA = V_ID_JTVA_COLOANA
       WHERE A.ID_COMANDA = V_ID_COMANDA
         AND A.ID_ARTICOL = V_ID_ARTICOL
         AND A.ID_POL = V_ID_POL
         AND A.STERS = 0;
    EXCEPTION
      WHEN TOO_MANY_ROWS THEN
        SELECT DENUMIRE, CODMAT
          INTO V_DENUMIRE, V_CODMAT
          FROM NOM_ARTICOLE
         WHERE ID_ARTICOL = V_ID_ARTICOL;

        SELECT NR_COMANDA
          INTO V_NR_COMANDA
          FROM COMENZI
         WHERE ID_COMANDA = V_ID_COMANDA;

        RAISE_APPLICATION_ERROR(-20000,
                                'Articolul ' || V_DENUMIRE || ' cu codul ' ||
                                V_CODMAT || ' este dublat pe comanda ' ||
                                V_NR_COMANDA || ' !');
    END;

    INSERT INTO VANZARI_DETALII_TEMP
      (ID_ARTICOL,
       ID_POL,
       PRET,
       PRET_CU_TVA,
       PROC_TVAV,
       CANTITATE,
       DISCOUNT_UNITAR,
       ID_GESTIUNE,
       CONT,
       ID_VALUTA,
       CURS,
       MULTIPLICATOR,
       ID_JTVA_COLOANA,
       IN_STOC,
       ID_GESTIUNE_DEST,
       ID_COMANDA,
       ID_UTIL,
       ID_TEMP)
      SELECT V_ID_ARTICOL,
             V_ID_POL,
             V_PRET,
             V_PRETURI_CU_TVA,
             V_PROC_TVAV,
             V_CANTITATE,
             V_DISCOUNT_UNITAR,
             pack_facturare.nid_gestiune_sursa,
             V_CONT,
             V_ID_VALUTA,
             DECODE(V_CURS, 0, 1, V_CURS),
             V_MULTIPLICATOR,
             V_ID_JTVA_COLOANA,
             1,
             V_ID_GESTIUNE_DEST,
             V_ID_COMANDA,
             V_ID_UTIL,
             COUNT(*) + 1
        FROM VANZARI_DETALII_TEMP;

  END adauga_articol_lucrare_pret;
  -------------------------------------------------------------------
  PROCEDURE adauga_articol_set(V_ID_VANZARE_SET  IN NUMBER,
                               V_DENUMIRE        IN VARCHAR2,
                               V_EXPLICATIE      IN VARCHAR2,
                               V_UM              IN VARCHAR2,
                               V_SERIE           IN VARCHAR2,
                               V_CANTITATE       IN NUMBER,
                               V_PRET            IN NUMBER,
                               V_DISCOUNT_UNITAR IN NUMBER) is
  begin
    INSERT INTO VANZARI_SETURI_TEMP
      (ID_VANZARE_SET,
       DENUMIRE,
       EXPLICATIE,
       CANTITATE,
       UM,
       SERIE,
       PRET,
       DISCOUNT_UNITAR)
    VALUES
      ((-1) * V_ID_VANZARE_SET,
       V_DENUMIRE,
       V_EXPLICATIE,
       V_CANTITATE,
       V_UM,
       V_SERIE,
       V_PRET,
       V_DISCOUNT_UNITAR);
  end adauga_articol_set;
  -------------------------------------------------------------------
  PROCEDURE adauga_articol_factura(V_ID_TEMP             IN NUMBER,
                                   V_ID_ARTICOL          IN NUMBER,
                                   V_SERIE               IN VARCHAR2,
                                   V_EXPLICATIE          IN VARCHAR2,
                                   V_ID_POL              IN NUMBER,
                                   V_ID_GESTIUNE         IN NUMBER,
                                   V_PRET_ACHIZITIE_TEMP IN NUMBER,
                                   V_PRETD               IN NUMBER,
                                   V_ID_VALUTAD          IN NUMBER,
                                   V_PRET_TEMP           IN NUMBER,
                                   V_ID_VALUTA_TEMP      IN NUMBER,
                                   V_PRETURI_CU_TVA_TEMP IN NUMBER,
                                   V_IN_STOC_TEMP        IN NUMBER,
                                   V_CANTITATE           IN NUMBER,
                                   V_DISCOUNT_UNITAR     IN NUMBER,
                                   V_CONT                IN VARCHAR2,
                                   V_CURS                IN NUMBER,
                                   V_MULTIPLICATOR       IN NUMBER,
                                   V_ID_JTVA_COLOANA     IN NUMBER,
                                   V_ID_PART_REZ         IN NUMBER,
                                   V_ID_LUCRARE_REZ      IN NUMBER,
                                   V_PRETV_ORIG          IN NUMBER,
                                   V_ID_VANZARE_SET      IN NUMBER,
                                   V_ID_CTR              IN NUMBER,
                                   V_ID_UTIL             IN NUMBER,
                                   V_TAXCODE             IN NUMBER DEFAULT NULL,
                                   V_LOT                 IN VARCHAR2 DEFAULT NULL) IS
    V_ID_GESTIUNE2 NOM_GESTIUNI.ID_GESTIUNE%TYPE;
    V_CONT2        VARCHAR2(4);
    V_ID_COMANDA   COMENZI.ID_COMANDA%TYPE;

    V_PRET_ACHIZITIE      VANZARI_DETALII.PRET_ACHIZITIE%TYPE;
    V_ID_POLITICA_PRETACH CRM_POLITICI_PRETURI.ID_POL%TYPE;
    V_PROCENT_ADAOS       NUMBER(10, 2);

    V_PRET           CRM_POLITICI_PRET_ART.PRET%TYPE;
    V_PROC_TVAV      CRM_POLITICI_PRET_ART.PROC_TVAV%TYPE;
    V_ID_VALUTA      CRM_POLITICI_PRET_ART.ID_VALUTA%TYPE;
    V_PRETURI_CU_TVA CRM_POLITICI_PRETURI.PRETURI_CU_TVA%TYPE;
    V_IN_STOC        NOM_ARTICOLE.IN_STOC%TYPE;
    V_OPT_FACTURARE  CONTRACTE.OPT_FACTURARE%TYPE;
  BEGIN
    V_PRET_ACHIZITIE := V_PRET_ACHIZITIE_TEMP;
    IF V_ID_GESTIUNE <> -1000 THEN
      V_ID_GESTIUNE2 := V_ID_GESTIUNE;
    END IF;
    IF V_CONT <> 'XXXX' THEN
      V_CONT2 := V_CONT;
    END IF;

    IF pack_facturare.ntip IN (2, 6, 26, 52) THEN
      -- factura / aviz pe baza de contract
      BEGIN
        SELECT NVL(OPT_FACTURARE, 3)
          INTO V_OPT_FACTURARE
          FROM CONTRACTE
         WHERE ID_CTR = V_ID_CTR;
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          V_OPT_FACTURARE := 4;
      END;
    END IF;

    CASE
      WHEN pack_facturare.ntip IN (3, 21, 28, 42, 47) THEN
        -- facturare/aviz din comenzi
        V_ID_COMANDA := to_number(pack_facturare.clistaid);

        SELECT A.PRET,
               C.PROC_TVAV,
               C.ID_VALUTA,
               B.PRETURI_CU_TVA,
               D.IN_STOC
          INTO V_PRET,
               V_PROC_TVAV,
               V_ID_VALUTA,
               V_PRETURI_CU_TVA,
               V_IN_STOC
          FROM COMENZI_ELEMENTE A
          LEFT JOIN CRM_POLITICI_PRETURI B
            ON A.ID_POL = B.ID_POL
          LEFT JOIN CRM_POLITICI_PRET_ART C
            ON A.ID_POL = C.ID_POL
           AND A.ID_ARTICOL = C.ID_ARTICOL
          LEFT JOIN NOM_ARTICOLE D
            ON A.ID_ARTICOL = D.ID_ARTICOL
         WHERE A.ID_COMANDA = V_ID_COMANDA
           AND A.ID_ARTICOL = V_ID_ARTICOL
           AND A.STERS = 0;

      WHEN pack_facturare.ntip = 4 THEN
        -- facturare din avize
        SELECT DISTINCT A.PRET,
                        A.PROC_TVAV,
                        A.ID_VALUTA,
                        A.PRET_CU_TVA,
                        B.IN_STOC
          INTO V_PRET,
               V_PROC_TVAV,
               V_ID_VALUTA,
               V_PRETURI_CU_TVA,
               V_IN_STOC
          FROM VANZARI_DETALII A
          LEFT JOIN NOM_ARTICOLE B
            ON A.ID_ARTICOL = B.ID_ARTICOL
         WHERE A.ID_ARTICOL = V_ID_ARTICOL
           AND A.ID_POL = V_ID_POL
           AND NVL(A.ID_GESTIUNE, -1000) = V_ID_GESTIUNE
           AND A.DISCOUNT_UNITAR = V_DISCOUNT_UNITAR
           AND NVL(A.CONT, 'XXXX') = V_CONT
           AND A.ID_VANZARE IN
               (SELECT X AS ID_VANZARE
                  FROM table(cast(CHARN2COLLECTION(pack_facturare.clistaid,
                                                   ',') AS num_tab)));
      WHEN pack_facturare.ntip = 45 THEN
        -- facturare restaurant
        BEGIN
          V_ID_POLITICA_PRETACH := to_number(pack_sesiune.getoptiunefirma('ID_POLPRODREST'));
          V_PROCENT_ADAOS       := to_number(pack_sesiune.getoptiunefirma('RESTPROCADAOS'));
          V_PRET                := V_PRET_TEMP;
          V_ID_VALUTA           := V_ID_VALUTA_TEMP;
          V_PRETURI_CU_TVA      := V_PRETURI_CU_TVA_TEMP;
          V_IN_STOC             := V_IN_STOC_TEMP;

          SELECT ROUND((A.COTA_TVA + 100) / 100, 2),
                 NVL2(B.PRETFTVA,
                      round(decode(B.ID_VALUTA,
                                   pack_def.GetIdMonedaNationala(),
                                   1,
                                   V_CURS) * NVL(B.PRETFTVA, 0),
                            pack_sesiune.nzecimale_preta),
                      NVL2(V_PROCENT_ADAOS,
                           round(pack_sesiune.calculeaza_pret_fara_tva(V_PRET,
                                                                       V_CURS,
                                                                       V_ID_VALUTA,
                                                                       (A.COTA_TVA + 100) / 100,
                                                                       V_PRETURI_CU_TVA,
                                                                       1) * 100 /
                                 (100 + V_PROCENT_ADAOS),
                                 pack_sesiune.nzecimale_preta),
                           0))
            INTO V_PROC_TVAV, V_PRET_ACHIZITIE
            FROM JTVA_COLOANE A
            LEFT JOIN (SELECT PRETFTVA, ID_VALUTA
                         FROM CRM_POLITICI_PRET_ART
                        WHERE NVL2(V_ID_POLITICA_PRETACH, ID_POL, 1) =
                              NVL(V_ID_POLITICA_PRETACH, 2)
                          AND ID_ARTICOL = V_ID_ARTICOL) B
              ON 1 = 1
           WHERE A.ID_JTVA_COLOANA = V_ID_JTVA_COLOANA;
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
            RAISE_APPLICATION_ERROR(-20000,
                                    'Nu a fost gasita cota de TVA! (FACT-018 : ' ||
                                    V_ID_ARTICOL || ')');
        END;
      WHEN V_OPT_FACTURARE = 3 THEN
        -- facturare/aviz de pe contract cu preturile de pe contract
        BEGIN
          SELECT DECODE(A.PRET_UNITAR, 0, V_PRET_TEMP, A.PRET_UNITAR),
                 B.PROC_TVAV,
                 B.ID_VALUTA,
                 A.PRET_CU_TVA,
                 C.IN_STOC
            INTO V_PRET,
                 V_PROC_TVAV,
                 V_ID_VALUTA,
                 V_PRETURI_CU_TVA,
                 V_IN_STOC
            FROM CTR_ARTICOLE A
            LEFT JOIN CRM_POLITICI_PRET_ART B
              ON A.ID_POL_ART = B.ID_POL_ART
            LEFT JOIN NOM_ARTICOLE C
              ON B.ID_ARTICOL = C.ID_ARTICOL
           WHERE B.ID_ARTICOL = V_ID_ARTICOL
             AND A.ID_CTR = V_ID_CTR
             AND B.ID_POL = V_ID_POL;
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
            BEGIN
              SELECT ROUND((COTA_TVA + 100) / 100, 2)
                INTO V_PROC_TVAV
                FROM JTVA_COLOANE
               WHERE ID_JTVA_COLOANA = V_ID_JTVA_COLOANA;
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
                RAISE_APPLICATION_ERROR(-20000,
                                        'Nu a fost gasita cota de TVA! (FACT-012 : ' ||
                                        V_ID_ARTICOL || ')');
            END;

            V_PRET           := V_PRET_TEMP;
            V_ID_VALUTA      := V_ID_VALUTA_TEMP;
            V_PRETURI_CU_TVA := V_PRETURI_CU_TVA_TEMP;
            V_IN_STOC        := V_IN_STOC_TEMP;
        END;

      ELSE
        BEGIN
          SELECT ROUND((COTA_TVA + 100) / 100, 2)
            INTO V_PROC_TVAV
            FROM JTVA_COLOANE
           WHERE ID_JTVA_COLOANA = V_ID_JTVA_COLOANA;
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
            RAISE_APPLICATION_ERROR(-20000,
                                    'Nu a fost gasita cota de TVA! (FACT-013 : ' ||
                                    V_ID_ARTICOL || ')');
        END;

        V_PRET           := V_PRET_TEMP;
        V_ID_VALUTA      := V_ID_VALUTA_TEMP;
        V_PRETURI_CU_TVA := V_PRETURI_CU_TVA_TEMP;
        V_IN_STOC        := V_IN_STOC_TEMP;
        /*        SELECT A.PRET,
              A.PROC_TVAV,
              A.ID_VALUTA,
              B.PRETURI_CU_TVA,
              C.IN_STOC
         INTO V_PRET,
              V_PROC_TVAV,
              V_ID_VALUTA,
              V_PRETURI_CU_TVA,
              V_IN_STOC
         FROM CRM_POLITICI_PRET_ART A
         LEFT JOIN CRM_POLITICI_PRETURI B ON A.ID_POL = B.ID_POL
         LEFT JOIN NOM_ARTICOLE C ON A.ID_ARTICOL = C.ID_ARTICOL
        WHERE A.ID_ARTICOL = V_ID_ARTICOL
          AND A.ID_POL = V_ID_POL;*/

    END CASE;

    INSERT INTO VANZARI_DETALII_TEMP
      (ID_TEMP,
       ID_ARTICOL,
       SERIE,
       LOT,
       EXPLICATIA,
       ID_POL,
       PRET_ACHIZITIE,
       PRETD,
       ID_VALUTAD,
       PRET,
       PRET_CU_TVA,
       PROC_TVAV,
       CANTITATE,
       DISCOUNT_UNITAR,
       ID_GESTIUNE,
       CONT,
       ID_VALUTA,
       CURS,
       MULTIPLICATOR,
       ID_JTVA_COLOANA,
       IN_STOC,
       ID_VANZARE_SET,
       ID_PART_REZ,
       ID_LUCRARE_REZ,
       PRETV_ORIG,
       CUSTODIE,
       ID_CTR,
       ID_UTIL,
       TAXCODE)
    VALUES
      (V_ID_TEMP,
       V_ID_ARTICOL,
       V_SERIE,
       V_LOT,
       V_EXPLICATIE,
       V_ID_POL,
       NVL(V_PRET_ACHIZITIE, 0),
       NVL(V_PRETD, 0),
       V_ID_VALUTAD,
       V_PRET,
       V_PRETURI_CU_TVA,
       V_PROC_TVAV,
       V_CANTITATE,
       V_DISCOUNT_UNITAR,
       V_ID_GESTIUNE2,
       V_CONT2,
       V_ID_VALUTA,
       DECODE(V_CURS, 0, 1, V_CURS),
       V_MULTIPLICATOR,
       V_ID_JTVA_COLOANA,
       V_IN_STOC,
       (-1) * V_ID_VANZARE_SET,
       V_ID_PART_REZ,
       V_ID_LUCRARE_REZ,
       V_PRETV_ORIG,
       (CASE WHEN pack_facturare.ntip IN (42, 47) OR
        pack_facturare.v_date_gestiune.cont = '357' THEN 1 ELSE 0 END),
       V_ID_CTR,
       V_ID_UTIL,
       decode(V_TAXCODE, 0, NULL, V_TAXCODE));

  END adauga_articol_factura;
  -------------------------------------------------------------------
  PROCEDURE sterge_articol_factura(V_ID_TEMP IN NUMBER,
                                   V_ID_UTIL IN NUMBER) IS
  BEGIN
    DELETE FROM VANZARI_DETALII_TEMP WHERE ID_TEMP = V_ID_TEMP;
  END sterge_articol_factura;
  -------------------------------------------------------------------
  PROCEDURE adauga_diferente_pret(V_SIR_ID_TEMP   IN VARCHAR2,
                                  V_SIR_DIFERENTA IN VARCHAR,
                                  V_ID_UTIL       IN NUMBER) IS
    V_SEPARATOR VARCHAR2(1) := '|';
  BEGIN
    MERGE INTO VANZARI_DETALII_TEMP A
    USING (SELECT ID_TEMP, SUM(DIFERENTA) AS DIFERENTA
             FROM (SELECT to_number(substr(V_SIR_ID_TEMP,
                                           decode(rownum,
                                                  1,
                                                  1,
                                                  instr(V_SIR_ID_TEMP,
                                                        V_SEPARATOR,
                                                        1,
                                                        rownum - 1) + 1),
                                           instr(V_SIR_ID_TEMP,
                                                 V_SEPARATOR,
                                                 1,
                                                 rownum) -
                                           decode(rownum,
                                                  1,
                                                  0,
                                                  instr(V_SIR_ID_TEMP,
                                                        V_SEPARATOR,
                                                        1,
                                                        rownum - 1)) - 1)) AS ID_TEMP,
                          to_number(substr(V_SIR_DIFERENTA,
                                           decode(rownum,
                                                  1,
                                                  1,
                                                  instr(V_SIR_DIFERENTA,
                                                        V_SEPARATOR,
                                                        1,
                                                        rownum - 1) + 1),
                                           instr(V_SIR_DIFERENTA,
                                                 V_SEPARATOR,
                                                 1,
                                                 rownum) -
                                           decode(rownum,
                                                  1,
                                                  0,
                                                  instr(V_SIR_DIFERENTA,
                                                        V_SEPARATOR,
                                                        1,
                                                        rownum - 1)) - 1)) AS DIFERENTA
                     FROM dual
                   CONNECT BY level <=
                              length(V_SIR_ID_TEMP) -
                              length(REPLACE(V_SIR_ID_TEMP, V_SEPARATOR)))
            GROUP BY ID_TEMP) B
    ON (A.ID_TEMP = B.ID_TEMP)
    WHEN MATCHED THEN
      UPDATE SET DIFERENTA = B.DIFERENTA;
  END adauga_diferente_pret;
  -------------------------------------------------------------------
  PROCEDURE adauga_rata_factura(V_ID_RATA         IN NUMBER,
                                V_PRET            IN NUMBER,
                                V_CURS            IN NUMBER,
                                V_MULTIPLICATOR   IN NUMBER,
                                V_ID_JTVA_COLOANA IN NUMBER,
                                V_EXPLICATIE      IN VARCHAR2,
                                V_ID_UTIL         IN NUMBER,
                                V_TAXCODE         IN NUMBER DEFAULT NULL) IS
    V_CANTITATE      VANZARI_DETALII.CANTITATE%TYPE := 1;
    V_PRETURI_CU_TVA VANZARI_DETALII.PRET_CU_TVA%TYPE;
    V_PROC_TVAV      VANZARI_DETALII.PROC_TVAV%TYPE;
    V_ID_CTR         CONTRACTE.ID_CTR%TYPE;

    V_ID_VALUTA NOM_VALUTE.ID_VALUTA%TYPE;
    lnIdTemp    NUMBER(10);
  BEGIN
    SELECT (B.PROC_TVA + 100) / 100 AS PROC_TVAV,
           A.ID_VALUTA,
           A.ID_CTR,
           A.PRET_CU_TVA
      INTO V_PROC_TVAV, V_ID_VALUTA, V_ID_CTR, V_PRETURI_CU_TVA
      FROM CTR_SCADENTAR A
      LEFT JOIN CONTRACTE B
        ON A.ID_CTR = B.ID_CTR
     WHERE A.ID_RATA = V_ID_RATA;

    SELECT COUNT(*) + 1 INTO lnIdTemp from vanzari_detalii_temp;

    INSERT INTO VANZARI_DETALII_TEMP
      (ID_RATA,
       PRET,
       PROC_TVAV,
       CANTITATE,
       ID_VALUTA,
       CURS,
       MULTIPLICATOR,
       EXPLICATIA,
       PRET_CU_TVA,
       ID_JTVA_COLOANA,
       ID_CTR,
       ID_UTIL,
       ID_TEMP,
       TAXCODE)
    VALUES
      (V_ID_RATA,
       V_PRET,
       V_PROC_TVAV,
       V_CANTITATE,
       V_ID_VALUTA,
       V_CURS,
       V_MULTIPLICATOR,
       V_EXPLICATIE,
       V_PRETURI_CU_TVA,
       V_ID_JTVA_COLOANA,
       V_ID_CTR,
       V_ID_UTIL,
       lnIdTemp,
       decode(V_TAXCODE, 0, NULL, V_TAXCODE));

  END adauga_rata_factura;
  -------------------------------------------------------------------
  PROCEDURE sterge_rata_factura(V_ID_RATA IN NUMBER, V_ID_UTIL IN NUMBER) IS
  BEGIN
    DELETE FROM VANZARI_DETALII_TEMP WHERE ID_RATA = V_ID_RATA;
  END sterge_rata_factura;
  -------------------------------------------------------------------
  PROCEDURE sterge_proforma_old(V_ID_PROFORMA IN NUMBER,
                                V_ID_UTIL     IN NUMBER) IS
    V_STERS   NUMBER(1) := 1;
    V_NESTERS NUMBER(1) := 0;
    V_DATAORA DATE := SYSDATE;

  BEGIN
    UPDATE PROFORME_DETALII
       SET STERS = V_STERS, ID_UTILS = V_ID_UTIL, DATAORAS = V_DATAORA
     WHERE ID_PROFORMA = V_ID_PROFORMA
       AND STERS = V_NESTERS;

    UPDATE PROFORME
       SET STERS = V_STERS, ID_UTILS = V_ID_UTIL, DATAORAS = V_DATAORA
     WHERE ID_PROFORMA = V_ID_PROFORMA;

  END sterge_proforma_old;

  -------------------------------------------------------------------
  PROCEDURE sterge_factura(V_ID_VANZARE IN NUMBER,
                           V_LUNA       IN NUMBER,
                           V_AN         IN NUMBER,
                           V_ID_UTIL    IN NUMBER) IS
    TYPE sir_id_rata IS TABLE OF VANZARI_DETALII.ID_RATA%TYPE;
    arr_id_rata sir_id_rata;

    V_COD     ACT.COD%TYPE;
    V_ID_SET  XSETS.ID_SET%TYPE := 0;
    V_STERS   NUMBER(1) := 1;
    V_NESTERS NUMBER(1) := 0;
    V_DATAORA DATE := SYSDATE;
    V_TIP     VANZARI.TIP%TYPE;

    V_NR_AVIZE_FACT NUMBER(10);
    V_NR_FACT_RETUR NUMBER(10);
    V_ID_COMANDA    COMENZI.ID_COMANDA%TYPE;
  BEGIN
    -- pentru o factura :
    --  verific daca exista facturi de retur
    SELECT COUNT(*)
      INTO V_NR_FACT_RETUR
      FROM VANZARI_CORESP
     WHERE STERS = 0
       AND ID_VANZARE_AVIZ = V_ID_VANZARE
       AND TIP = 3;

    IF V_NR_FACT_RETUR > 0 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Pentru acesta factura s-au emis facturi de retur. Trebuie sa stergeti mai intai factura de retur!');
    END IF;

    -- ptr. un aviz normal:
    --  verific daca exista facturi sau avize de retur pe avizul resp.
    SELECT COUNT(*)
      INTO V_NR_AVIZE_FACT
      FROM VANZARI_CORESP
     WHERE STERS = 0
       AND ID_VANZARE_AVIZ = V_ID_VANZARE
       AND TIP IN (1, 2);

    IF V_NR_AVIZE_FACT > 0 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Pe acest aviz s-au emis facturi / aviz de retur. Trebuie sa stergeti mai intai facturile / avizul de retur!');
    END IF;

    -- ptr. o factura din aviz:
    --  verific daca sunt avize de retur pe avizele pentru care s-a emis factura resp.
    SELECT COUNT(*)
      INTO V_NR_AVIZE_FACT
      FROM VANZARI_CORESP
     WHERE STERS = 0
       AND ID_VANZARE_AVIZ IN (SELECT ID_VANZARE_AVIZ
                                 FROM VANZARI_CORESP
                                WHERE ID_VANZARE_FACT = V_ID_VANZARE
                                  AND TIP = 1)
       AND TIP = 2;

    IF V_NR_AVIZE_FACT > 0 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Pe unul dintre avizele facturate s-a emis si aviz de retur! ' ||
                              'Trebuie sa stergeti avizul de retur mai intai!');
    END IF;

    UPDATE VANZARI
       SET STERS = V_STERS, ID_UTILS = V_ID_UTIL, DATAORAS = V_DATAORA
     WHERE ID_VANZARE = V_ID_VANZARE
    RETURNING COD, TIP, ID_COMANDA INTO V_COD, V_TIP, V_ID_COMANDA;

    CASE
      WHEN V_TIP = 24 THEN
        -- aviz de retur
        UPDATE VANZARI
           SET FACTURAT = 0, ID_UTILFACT = NULL, DATA_FACTURAT = NULL
         WHERE ID_VANZARE IN (SELECT ID_VANZARE_AVIZ
                                FROM VANZARI_CORESP
                               WHERE ID_VANZARE_FACT = V_ID_VANZARE
                                 AND STERS = 0)
           AND FACTURAT = 1;

      WHEN V_TIP > 20 and
           V_TIP not in (pack_facturare.nTipFacturaHotel,
                         pack_facturare.nTipFacturaRestaurant,
                         pack_facturare.nTipNotaPlata) THEN
        -- alte tipuri de avize
        UPDATE VANZARI_CANTITATI
           SET STERS = 1
         WHERE ID_VANZARE_DET_AVIZ IN
               (SELECT ID_VANZARE_DET AS ID_VANZARE_DET_AVIZ
                  FROM VANZARI_DETALII
                 WHERE ID_VANZARE = V_ID_VANZARE
                   AND STERS = 0);

      WHEN V_TIP = 4 THEN
        -- factura din aviz
        UPDATE VANZARI
           SET FACTURAT = 0, ID_UTILFACT = NULL, DATA_FACTURAT = NULL
         WHERE ID_VANZARE IN (SELECT ID_VANZARE_AVIZ
                                FROM VANZARI_CORESP
                               WHERE ID_VANZARE_FACT = V_ID_VANZARE
                                 AND STERS = 0)
           AND FACTURAT = 1;

        UPDATE VANZARI_CANTITATI
           SET STERS = 1
         WHERE ID_VANZARE_DET IN
               (SELECT ID_VANZARE_DET
                  FROM VANZARI_DETALII
                 WHERE ID_VANZARE = V_ID_VANZARE);

      WHEN V_TIP = pack_facturare.nTipFacturaRestaurant THEN
        SELECT DISTINCT ID_SET
          INTO V_ID_SET
          FROM ACT
         WHERE COD IN
               (SELECT COD FROM VANZARI WHERE ID_VANZARE = V_ID_VANZARE);

      ELSE
        V_STERS := 1;
    END CASE;

    IF V_ID_COMANDA IS NOT NULL THEN
      UPDATE COMENZI_ELEMENTE
         SET STERS = V_STERS, DATAORAS = V_DATAORA, ID_UTILS = V_ID_UTIL
       WHERE ID_COMANDA = V_ID_COMANDA
         AND CANTITATE < 0;
    END IF;

    UPDATE VANZARI_DETALII
       SET STERS = V_STERS, ID_UTILS = V_ID_UTIL, DATAORAS = V_DATAORA
     WHERE ID_VANZARE = V_ID_VANZARE
       AND STERS = V_NESTERS
    RETURNING ID_RATA BULK COLLECT INTO arr_id_rata;

    IF V_TIP IN (2, 6, 52) THEN
      FOR i IN arr_id_rata.FIRST .. arr_id_rata.LAST LOOP
        IF arr_id_rata(i) IS NOT NULL THEN
          UPDATE CTR_RATE_FACTURI
             SET STERS    = V_STERS,
                 ID_UTILS = V_ID_UTIL,
                 DATAORAS = V_DATAORA
           WHERE ID_FACT IN
                 (SELECT DISTINCT ID_FACT FROM ACT WHERE COD = V_COD)
             AND ID_RATA = arr_id_rata(i)
             AND STERS = V_NESTERS;
        END IF;
      END LOOP;

    END IF;

    UPDATE VANZARI_CORESP
       SET STERS = V_STERS
     WHERE ID_VANZARE_FACT = V_ID_VANZARE
        OR ID_VANZARE_AVIZ = V_ID_VANZARE;

    CASE
      WHEN V_TIP IN (pack_facturare.nTipFacturaRestaurant, nTipNotaPlata) AND
           V_ID_SET <>
           25000 + pack_facturare.nTipFacturaRestaurant - 1 + 10 then
        pack_restaurant.sterge_vanzare(V_ID_VANZARE, V_ID_UTIL);

      WHEN V_TIP = pack_facturare.nTipFacturaHotel then
        pack_hotel.sterge_vanzare(V_ID_VANZARE, V_ID_UTIL);

      WHEN V_TIP = pack_facturare.nTipFacturaACN then
        -- apelez cu execute immediate pentru ca nu exista pack_acn decat pe firma ACN
        if pack_migrare.ObjectExist('PACK_ACN') = 1 then
          execute immediate 'begin pack_acn.sterge_vanzare(:1, :2); end;'
            using V_ID_VANZARE, V_ID_UTIL;
        end if;

      ELSE
        NULL;
    END CASE;

  END sterge_factura;

  -------------------------------------------------------------------
  PROCEDURE sterge_proforma(V_ID_VANZARE IN NUMBER, V_ID_UTIL IN NUMBER) IS
    TYPE sir_id_rata IS TABLE OF VANZARI_DETALII.ID_RATA%TYPE;
    arr_id_rata sir_id_rata;

    V_COD     ACT.COD%TYPE;
    V_ID_SET  XSETS.ID_SET%TYPE := 0;
    V_STERS   NUMBER(1) := 1;
    V_NESTERS NUMBER(1) := 0;
    V_DATAORA DATE := SYSDATE;
    V_TIP     VANZARI.TIP%TYPE;

    V_NR_AVIZE_FACT NUMBER(10);
    V_NR_FACT_RETUR NUMBER(10);
    V_ID_COMANDA    COMENZI.ID_COMANDA%TYPE;
  BEGIN

    UPDATE VANZARI
       SET STERS = V_STERS, ID_UTILS = V_ID_UTIL, DATAORAS = V_DATAORA
     WHERE ID_VANZARE = V_ID_VANZARE;

    UPDATE VANZARI_DETALII
       SET STERS = V_STERS, ID_UTILS = V_ID_UTIL, DATAORAS = V_DATAORA
     WHERE ID_VANZARE = V_ID_VANZARE
       AND STERS = V_NESTERS;

  END sterge_proforma;

  PROCEDURE scrie_proforma(V_TOTFTVA             IN NUMBER,
                           V_TOTTVA              IN NUMBER,
                           V_DISCOUNT_FACTURA    IN NUMBER,
                           V_SERIE_ACT_INCASARE  IN VARCHAR2,
                           V_NUMAR_ACT_INCASARE  IN NUMBER,
                           V_LISTA_INCASARE      IN VARCHAR2,
                           V_ID_DELEGAT          IN NUMBER,
                           V_ID_MASINA           IN NUMBER,
                           V_ID_FACTURARE        IN NUMBER,
                           V_LISTARE_DETALIATA   IN NUMBER,
                           V_DATAORA_EXP         IN DATE,
                           V_ID_AGENT            IN NUMBER,
                           V_TEXT_ADITIONAL      IN VARCHAR2,
                           V_DISCOUNT_EVIDENTIAT IN NUMBER,
                           V_PARAMETRU_ADITIONAL IN NUMBER,
                           V_ID_VANZARE          OUT NUMBER) is
  begin
    pack_facturare.scrie_in_vanzari(V_DISCOUNT_FACTURA,
                                    V_ID_DELEGAT,
                                    V_ID_MASINA,
                                    V_ID_FACTURARE,
                                    V_LISTARE_DETALIATA,
                                    V_DATAORA_EXP,
                                    V_ID_AGENT,
                                    V_TEXT_ADITIONAL,
                                    pack_facturare.nid_vanzare);
    if pack_facturare.nid_vanzare is not null then
      V_ID_VANZARE := pack_facturare.nid_vanzare;

      -- completez vanzari.eproforma
      update vanzari
         set eproforma = 1
       where id_vanzare = pack_facturare.nid_vanzare;
    end if;
  end;

  -------------------------------------------------------------------
  PROCEDURE scrie_proforma_old(V_DISCOUNT_PROFORMA   IN NUMBER,
                               V_TEXT_ADITIONAL      IN VARCHAR2,
                               V_DISCOUNT_EVIDENTIAT IN NUMBER,
                               V_ID_PROFORMA         OUT NUMBER,
                               V_NUMAR_ACT           OUT NUMBER) IS

    V_DATAORA DATE := SYSDATE;

  BEGIN
    pack_serii_numere.dezaloca_numar(pack_facturare.nid_tipfactura);
    pack_facturare.ndiscount_evidentiat := V_DISCOUNT_EVIDENTIAT;
    pack_facturare.genereaza_numar_proforma(V_NUMAR_ACT);

    INSERT /*+ APPEND */
    INTO PROFORME
      (id_util,
       dataora,
       id_part,
       id_comanda,
       numar_act,
       tip,
       data_act,
       discount,
       discount_evidentiat,
       text_aditional,
       in_valuta,
       id_sucursala)
    VALUES
      (pack_facturare.nid_util,
       V_DATAORA,
       pack_facturare.nid_part,
       pack_facturare.nid_comanda,
       pack_facturare.nnumar_act,
       pack_facturare.ntip,
       pack_facturare.ddata_act,
       V_DISCOUNT_PROFORMA,
       pack_facturare.ndiscount_evidentiat,
       REPLACE(V_TEXT_ADITIONAL, CHR(13) || CHR(10), CHR(32)),
       pack_facturare.nin_valuta,
       pack_facturare.nid_sucursala)
    RETURNING ID_PROFORMA INTO V_ID_PROFORMA;

    pack_facturare.scrie_cursuri_proforma(V_ID_PROFORMA);

    pack_facturare.scrie_seturi_proforma;

    INSERT /*+ APPEND */
    INTO PROFORME_DETALII
      (ID_PROFORMA,
       ID_ARTICOL,
       ID_POL,
       CANTITATE,
       PRET,
       PROC_TVAV,
       ID_JTVA_COLOANA,
       DISCOUNT_UNITAR,
       ID_GESTIUNE,
       ID_VALUTA,
       EXPLICATIE,
       CONT,
       PRET_CU_TVA,
       SERIE,
       PRET_ACHIZITIE,
       PRETD,
       ID_VALUTAD,
       ID_CTR,
       ID_PROFORMA_SET)
      SELECT V_ID_PROFORMA AS ID_PROFORMA,
             ID_ARTICOL,
             ID_POL,
             CANTITATE,
             PRET,
             PROC_TVAV,
             ID_JTVA_COLOANA,
             DISCOUNT_UNITAR,
             ID_GESTIUNE,
             ID_VALUTA,
             EXPLICATIA AS EXPLICATIE,
             CONT,
             PRET_CU_TVA,
             SERIE,
             PRET_ACHIZITIE,
             PRETD,
             ID_VALUTAD,
             ID_CTR,
             DECODE(ID_VANZARE_SET, 0, NULL, ID_VANZARE_SET) AS ID_PROFORMA_SET
        FROM VANZARI_DETALII_TEMP;

    IF SQL%ROWCOUNT = 0 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu exista articole in lista de vanzare! (FACT-001)');
    END IF;

  END scrie_proforma_old;
  -------------------------------------------------------------------
  PROCEDURE inchide_comanda IS
  BEGIN
    INSERT INTO COMENZI_ELEMENTE
      (ID_COMANDA,
       ID_ARTICOL,
       ID_POL,
       PRET,
       DISCOUNT_UNITAR,
       CANTITATE,
       ID_VALUTA,
       PRET_CU_TVA)
      SELECT to_number(pack_facturare.clistaid) AS ID_COMANDA,
             A.ID_ARTICOL,
             A.ID_POL,
             A.PRET,
             A.DISCOUNT_UNITAR,
             NVL(C.CANTITATE, 0) + NVL(B.CANTITATE, 0) - A.CANTITATE AS CANTITATE,
             A.ID_VALUTA,
             A.PRET_CU_TVA
        FROM COMENZI_ELEMENTE A
        LEFT JOIN (SELECT ID_ARTICOL,
                          ID_POL,
                          ID_VALUTA,
                          SUM(CANTITATE) AS CANTITATE
                     FROM VANZARI_DETALII_TEMP
                    GROUP BY ID_ARTICOL, ID_POL, ID_VALUTA) B
          ON A.ID_ARTICOL = B.ID_ARTICOL
         AND A.ID_POL = B.ID_POL
         AND A.ID_VALUTA = B.ID_VALUTA
        LEFT JOIN (SELECT B.ID_ARTICOL,
                          B.ID_POL,
                          B.ID_VALUTA,
                          SUM(B.CANTITATE) AS CANTITATE
                     FROM VANZARI A
                     LEFT JOIN VANZARI_DETALII B
                       ON A.ID_VANZARE = B.ID_VANZARE
                      AND B.STERS = 0
                    WHERE A.ID_COMANDA = to_number(pack_facturare.clistaid)
                      AND A.STERS = 0
                    GROUP BY B.ID_ARTICOL, B.ID_POL, B.ID_VALUTA) C
          ON A.ID_ARTICOL = C.ID_ARTICOL
         AND A.ID_POL = C.ID_POL
         AND A.ID_VALUTA = C.ID_VALUTA
       WHERE A.STERS = 0
         AND A.ID_COMANDA = to_number(pack_facturare.clistaid)
         AND A.CANTITATE > NVL(C.CANTITATE, 0) + NVL(B.CANTITATE, 0);

  END inchide_comanda;
  -------------------------------------------------------------------
  PROCEDURE scrie_avize_lucrare(V_DISCOUNT_FACTURA    IN NUMBER,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_ID_SERIE_ACT        IN NUMBER,
                                V_ID_VANZARE          OUT NUMBER,
                                V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare) IS
    CURSOR CRS IS
      SELECT ROWNUM AS NRCRT, ID_COMANDA, ID_GESTIUNE
        FROM (SELECT ID_COMANDA, ID_GESTIUNE
                FROM COMENZI
               WHERE STERS = 0
                 AND ID_LUCRARE = pack_facturare.nid_lucrare
                    -- modificare 11.07
                 AND ID_COMANDA IN
                     (SELECT ID_COMANDA FROM VANZARI_DETALII_TEMP)
              --               AND ID_GESTIUNE IN
              --                     (SELECT DISTINCT ID_GESTIUNE_DEST FROM VANZARI_DETALII_TEMP)
              -- modificare 11.07    ^
               ORDER BY ID_COMANDA);
    CRS_LINIE CRS%ROWTYPE;

    TYPE tab_detalii_type IS TABLE OF vanzari_detalii_temp%ROWTYPE;
    tab_detalii tab_detalii_type;

    NR_AVIZ      NUMBER(5) := 0;
    NR_ELEMENT   NUMBER(5) := 0;
    ELEM_OPTIUNE NUMBER(5) := 0;
  BEGIN
    DELETE FROM VANZARI_TEMP;
    pack_contafin.sterge_temp_actrul();
    pack_facturare.initializeaza_setari_facturare();
    pack_facturare.ndiscount_evidentiat := V_DISCOUNT_EVIDENTIAT;
    pack_facturare.nid_serie            := V_ID_SERIE_ACT;
    ELEM_OPTIUNE                        := NVL(pack_sesiune.getoptiunefirma('NRMAXELEMPAG'),
                                               9999);

    OPEN CRS;
    LOOP
      FETCH CRS
        INTO CRS_LINIE;
      EXIT WHEN CRS%NOTFOUND;

      IF CRS_LINIE.NRCRT > 1 THEN
        IF pack_facturare.nid_serie IS NULL THEN
          pack_facturare.nnumar_act := pack_facturare.nnumar_act + 1;
        ELSE
          pack_serii_numere.aloca_numar(pack_facturare.nid_tipaviz,
                                        pack_facturare.nid_serie,
                                        NULL,
                                        pack_facturare.nid_util,
                                        pack_facturare.nid_sucursala,
                                        pack_facturare.nnumar_act);
        END IF;
      END IF;

      pack_facturare.nid_comanda                 := CRS_LINIE.ID_COMANDA;
      pack_facturare.v_date_gestiune.id_gestiune := CRS_LINIE.ID_GESTIUNE;
      pack_facturare.v_date_gestiune.acont       := NULL;

      SELECT NR_PAG, CONT, ACONT
        INTO pack_facturare.v_date_gestiune.id_tip_gest,
             pack_facturare.v_date_gestiune.cont,
             pack_facturare.v_date_gestiune.acont
        FROM NOM_GESTIUNI
       WHERE ID_GESTIUNE = pack_facturare.v_date_gestiune.id_gestiune;

      INSERT INTO VANZARI_TEMP
        (id_gestiune_dest, id_lucrare, numar_act, id_comanda, id_gestiune)
      values
        (pack_facturare.v_date_gestiune.id_gestiune,
         pack_facturare.nid_lucrare,
         pack_facturare.nnumar_act,
         pack_facturare.nid_comanda,
         pack_facturare.nid_gestiune_sursa);

      pack_serii_numere.aloca_numar(pack_facturare.nid_tipbon,
                                    NULL,
                                    pack_facturare.nid_gestiune_sursa,
                                    pack_facturare.nid_util,
                                    pack_facturare.nid_sucursala,
                                    pack_facturare.nnumar_bon);

      SELECT *
        BULK COLLECT
        INTO tab_detalii
        FROM VANZARI_DETALII_TEMP
       WHERE ID_COMANDA = CRS_LINIE.ID_COMANDA;
      -- modificare 11.07 : WHERE ID_GESTIUNE_DEST = pack_facturare.v_date_gestiune.id_gestiune;

      IF SQL%ROWCOUNT = 0 THEN
        RAISE_APPLICATION_ERROR(-20000,
                                'Nu exista articole in lista de vanzare! (FACT-002)');
      END IF;

      NR_AVIZ    := 1;
      NR_ELEMENT := 0;

      FOR i IN tab_detalii.first .. tab_detalii.last LOOP
        --  modificare 01.07.2008
        --  daca depasesc un anumit numar de elemente pe aviz, generez datele pentru urmatorul
        NR_ELEMENT := NR_ELEMENT + 1;
        IF NR_AVIZ < FLOOR((NR_ELEMENT - 1) / ELEM_OPTIUNE) + 1 THEN
          NR_AVIZ := NR_AVIZ + 1;
          IF pack_facturare.nid_serie IS NULL THEN
            pack_facturare.nnumar_act := pack_facturare.nnumar_act + 1;
          ELSE
            pack_serii_numere.aloca_numar(pack_facturare.nid_tipaviz,
                                          pack_facturare.nid_serie,
                                          NULL,
                                          pack_facturare.nid_util,
                                          pack_facturare.nid_sucursala,
                                          pack_facturare.nnumar_act);
          END IF;

          INSERT INTO VANZARI_TEMP
            (id_gestiune_dest,
             id_lucrare,
             numar_act,
             id_comanda,
             id_gestiune)
          values
            (pack_facturare.v_date_gestiune.id_gestiune,
             pack_facturare.nid_lucrare,
             pack_facturare.nnumar_act,
             pack_facturare.nid_comanda,
             pack_facturare.nid_gestiune_sursa);

          pack_serii_numere.aloca_numar(pack_facturare.nid_tipbon,
                                        NULL,
                                        pack_facturare.nid_gestiune_sursa,
                                        pack_facturare.nid_util,
                                        pack_facturare.nid_sucursala,
                                        pack_facturare.nnumar_bon);

        END IF;
        --  modificare 01.07.2008   ^

        pack_facturare.transfera_articol(tab_detalii(i));
      END LOOP;
    END LOOP;

    CLOSE CRS;

    -- cumulare note
    pack_facturare.cumuleaza_note_act();
    -- pack_facturare.completeaza_analitice();  am mutat-o in cumuleaza_note_act

    if pack_contafin.verifica_note_contabile(1) > 0 then
      pack_facturare.cursor_verificare(V_CURSOR_VERIFICARE);
    else
      pack_facturare.finalizeaza_avize_lucrare(V_DISCOUNT_FACTURA);

      OPEN V_CURSOR_VERIFICARE FOR
        SELECT * FROM ACT_TEMP WHERE 1 = 2;
    end if;
    V_ID_VANZARE := NULL;

  END scrie_avize_lucrare;
  -------------------------------------------------------------------
  /*  PROCEDURE scrie_factura2(V_TOTFTVA             IN NUMBER,
                           V_TOTTVA              IN NUMBER,
                           V_DISCOUNT_FACTURA    IN NUMBER,
                           V_SERIE_ACT_INCASARE  IN VARCHAR2,
                           V_NUMAR_ACT_INCASARE  IN NUMBER,
                           V_LISTA_INCASARE      IN VARCHAR2,
                           V_ID_DELEGAT          IN NUMBER,
                           V_ID_MASINA           IN NUMBER,
                           V_DATAORA_EXP         IN DATE,
                           V_ID_AGENT            IN NUMBER,
                           V_TEXT_ADITIONAL      IN VARCHAR2,
                           V_DISCOUNT_EVIDENTIAT IN NUMBER,
                           V_PARAMETRU_ADITIONAL IN NUMBER,
                           V_ID_VANZARE          OUT NUMBER,
                           V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare) IS

    lnIdFacturare      NUMBER(10) := null;
    lnListareDetaliata NUMBER(1) := 0;
  begin
    pack_facturare.scrie_factura2(V_TOTFTVA,
                                  V_TOTTVA,
                                  V_DISCOUNT_FACTURA,
                                  V_SERIE_ACT_INCASARE,
                                  V_NUMAR_ACT_INCASARE,
                                  V_LISTA_INCASARE,
                                  V_ID_DELEGAT,
                                  V_ID_MASINA,
                                  lnIdFacturare,
                                  lnListareDetaliata,
                                  V_DATAORA_EXP,
                                  V_ID_AGENT,
                                  V_TEXT_ADITIONAL,
                                  V_DISCOUNT_EVIDENTIAT,
                                  V_PARAMETRU_ADITIONAL,
                                  V_ID_VANZARE,
                                  V_CURSOR_VERIFICARE);

  end;*/
  -------------------------------------------------------------------

  PROCEDURE scrie_factura2(V_TOTFTVA             IN NUMBER,
                           V_TOTTVA              IN NUMBER,
                           V_DISCOUNT_FACTURA    IN NUMBER,
                           V_SERIE_ACT_INCASARE  IN VARCHAR2,
                           V_NUMAR_ACT_INCASARE  IN NUMBER,
                           V_LISTA_INCASARE      IN VARCHAR2,
                           V_ID_DELEGAT          IN NUMBER,
                           V_ID_MASINA           IN NUMBER,
                           V_ID_FACTURARE        IN NUMBER,
                           V_LISTARE_DETALIATA   IN NUMBER,
                           V_DATAORA_EXP         IN DATE,
                           V_ID_AGENT            IN NUMBER,
                           V_TEXT_ADITIONAL      IN VARCHAR2,
                           V_DISCOUNT_EVIDENTIAT IN NUMBER,
                           V_PARAMETRU_ADITIONAL IN NUMBER,
                           V_ID_VANZARE          OUT NUMBER,
                           V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare) IS
    -- V_LISTA_INCASARE : tip1|valoare1|id_bancasa1;tip2|valoare2|id_bancasa2 (pentru incasari cu mai multe tipuri ex: NUMERAR + CARD + TICHETE)

    TYPE tab_detalii_type IS TABLE OF vanzari_detalii_temp%ROWTYPE;
    tab_detalii tab_detalii_type;

    V_INCASAT_CALCUL        ACT.SUMA%TYPE := 0;
    V_DISCOUNT_TOTAL_CU_TVA ACT.SUMA%TYPE := 0;
    V_ASCD                  ACT.ASCD%TYPE;
    V_ID_SECTIE             ACT.ID_SECTIE%TYPE;
    V_ID_CTR                ACT.ID_CTR%TYPE;
    V_SEPARATOR             VARCHAR2(1) := ',';
  BEGIN
    pack_contafin.sterge_temp_actrul();
    pack_facturare.ntotftva := V_TOTFTVA;
    pack_facturare.ntottva  := V_TOTTVA;

    pack_facturare.initializeaza_setari_facturare();

    IF pack_facturare.nscadere_stoc = 0 AND
       pack_facturare.ntip IN (23, 25, 30, 41) THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu exista configurari pentru transfer intre subunitati fara scadere din stoc! (FACT-003)');
    END IF;

    pack_facturare.ndiscount_evidentiat := V_DISCOUNT_EVIDENTIAT;

    SELECT * BULK COLLECT INTO tab_detalii FROM VANZARI_DETALII_TEMP;

    IF SQL%ROWCOUNT = 0 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu exista articole in lista de vanzare! (FACT-004)');
    END IF;

    FOR i IN tab_detalii.first .. tab_detalii.last LOOP

      CASE
        WHEN pack_facturare.ntip IN (23, 25, 30, 41) THEN
          -- transfer intre subunitati
          V_INCASAT_CALCUL := 0;
          pack_facturare.transfera_articol(tab_detalii(i));
          /*       WHEN pack_facturare.ntip = pack_facturare.nTipNotaPlata THEN
          pack_facturare.descarca_gestiune(tab_detalii(i).id_articol,
                                           tab_detalii(i).serie,
                                           tab_detalii(i).pret_achizitie,
                                           tab_detalii(i).pretd,
                                           tab_detalii(i).id_valutad,
                                           tab_detalii(i).pretv_orig,
                                           tab_detalii(i).pret,
                                           tab_detalii(i).diferenta,
                                           tab_detalii(i).proc_tvav,
                                           tab_detalii(i).pret_cu_tva,
                                           tab_detalii(i).cantitate,
                                           tab_detalii(i).discount_unitar,
                                           tab_detalii(i).id_gestiune,
                                           pack_facturare.nid_sectie_stoc, -- V_ID_SECTIE
                                           tab_detalii(i).cont,
                                           pack_facturare.nid_venchelt, -- V_ID_VENCHELT
                                           tab_detalii(i).id_valuta,
                                           tab_detalii(i).curs,
                                           NULL, --      V_ID_PART_REZ
                                           NULL);*/
        WHEN pack_facturare.ntip IN (42, 47) THEN
          V_INCASAT_CALCUL             := 0;
          pack_facturare.nfactavizcust := 1;
          pack_facturare.descarca_gestiune(tab_detalii                   (i).id_articol,
                                           tab_detalii                   (i).serie,
                                           tab_detalii                   (i).pret_achizitie,
                                           tab_detalii                   (i).pretd,
                                           tab_detalii                   (i).id_valutad,
                                           tab_detalii                   (i).pretv_orig,
                                           tab_detalii                   (i).pret,
                                           tab_detalii                   (i).diferenta,
                                           tab_detalii                   (i).proc_tvav,
                                           tab_detalii                   (i).pret_cu_tva,
                                           tab_detalii                   (i).cantitate,
                                           tab_detalii                   (i).discount_unitar,
                                           tab_detalii                   (i).id_gestiune,
                                           pack_facturare.nid_sectie_stoc, -- V_ID_SECTIE
                                           tab_detalii                   (i).cont,
                                           pack_facturare.nid_venchelt, -- V_ID_VENCHELT
                                           tab_detalii                   (i).id_valuta,
                                           tab_detalii                   (i).curs / tab_detalii(i).multiplicator,
                                           NULL, --      V_ID_PART_REZ
                                           NULL,
                                           tab_detalii                   (i).id_jtva_coloana,
                                           tab_detalii                   (i).taxcode);
          pack_facturare.nfactavizcust := 0;
        WHEN pack_facturare.ntip IN (2, 6, 52) AND
             NVL(tab_detalii(i).id_rata, 0) <> 0 THEN
          V_INCASAT_CALCUL := V_INCASAT_CALCUL +
                              pack_facturare.contabilizeaza_rata(tab_detalii(i));
        ELSE
          -- 24 = AVIZ RETUR
          IF pack_facturare.ntip = 24 THEN
            SELECT STRINGAGG(COD)
              INTO pack_facturare.ccod_retur
              FROM VANZARI
             WHERE ID_VANZARE IN
                   (SELECT X AS ID_VANZARE
                      FROM table(charn2collection(pack_facturare.clistaid,
                                                  V_SEPARATOR)));
          END IF;

          V_INCASAT_CALCUL := V_INCASAT_CALCUL +
                              pack_facturare.contabilizeaza_articol(tab_detalii(i));
      END CASE;

    END LOOP;

    IF pack_facturare.ntip in (48, 49) THEN
      pack_facturare.scrie_descarcare_k(V_PARAMETRU_ADITIONAL);
    END IF;

    BEGIN
      SELECT ID_CTR
        INTO V_ID_CTR
        FROM (SELECT SUM(SUMA), ID_CTR
                FROM ACT_TEMP
               WHERE SCD IN ('4111', '418')
               GROUP BY ID_CTR
               ORDER BY 1)
       WHERE ROWNUM < 2;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_ID_CTR := NULL;
    END;

    IF V_DISCOUNT_FACTURA <> 0 THEN
      SELECT DISTINCT ASCD, ID_SECTIE
        INTO V_ASCD, V_ID_SECTIE
        FROM ACT_TEMP
       WHERE SCD IN ('4111', '418', '461')
         AND ROWNUM < 2;

      V_DISCOUNT_TOTAL_CU_TVA := pack_facturare.scrie_discount(1,
                                                               V_DISCOUNT_FACTURA,
                                                               pack_facturare.nid_valuta,
                                                               pack_facturare.nin_valuta,
                                                               V_ASCD,
                                                               V_ID_SECTIE,
                                                               V_ID_CTR,
                                                               1,
                                                               (100 +
                                                               pack_facturare.nproc_tva_max) / 100,
                                                               0,
                                                               1, -- V_CU_TVA
                                                               pack_facturare.nid_jtva_coloana,
                                                               pack_facturare.nTaxCode);
    END IF;

    V_INCASAT_CALCUL := V_INCASAT_CALCUL - V_DISCOUNT_TOTAL_CU_TVA;

    /*    IF V_AVANS <> 0 THEN
      V_INCASAT_CALCUL := V_INCASAT_CALCUL - ABS(v_avans);
      pack_facturare.scrie_avans(V_ID_CONTRACT, (-1) * ABS(v_avans));
    END IF;*/

    IF V_LISTA_INCASARE IS NOT NULL THEN
      pack_facturare.scrie_incasari(V_SERIE_ACT_INCASARE,
                                    V_NUMAR_ACT_INCASARE,
                                    V_LISTA_INCASARE,
                                    V_ID_CTR);
    END IF;

    -- cumulare note
    pack_facturare.cumuleaza_note_act();

    if pack_contafin.verifica_note_contabile(1) > 0 then
      pack_facturare.cursor_verificare(V_CURSOR_VERIFICARE);
      V_ID_VANZARE := NULL;
    else
      pack_facturare.finalizeaza_factura(V_DISCOUNT_FACTURA,
                                         V_ID_DELEGAT,
                                         V_ID_MASINA,
                                         V_ID_FACTURARE,
                                         V_LISTARE_DETALIATA,
                                         V_DATAORA_EXP,
                                         V_ID_AGENT,
                                         V_TEXT_ADITIONAL,
                                         V_PARAMETRU_ADITIONAL);

      OPEN V_CURSOR_VERIFICARE FOR
        SELECT * FROM VACT WHERE 1 = 2;
      V_ID_VANZARE := pack_facturare.nid_vanzare;
    end if;

  END scrie_factura2;
  -------------------------------------------------------------------
  /* PROCEDURE scrie_factura_avize_retur(V_DISCOUNT_FACTURA     IN NUMBER,
                                      V_ID_DELEGAT           IN NUMBER,
                                      V_ID_MASINA            IN NUMBER,
                                      V_DATAORA_EXP          IN DATE,
                                      V_ID_AGENT             IN NUMBER,
                                      V_DISCOUNT_EVIDENTIAT  IN NUMBER,
                                      V_TEXT_ADITIONAL       IN VARCHAR2,
                                      V_TEXT_ADITIONAL_RETUR IN VARCHAR2,
                                      V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                      V_SIR_MODIFICARI_PART  IN VARCHAR2,
                                      V_PARAMETRU_ADITIONAL  IN NUMBER,
                                      V_SERIE_ACT            IN VARCHAR2,
                                      V_NUMAR_ACT            IN NUMBER,
                                      V_ID_VANZARE           OUT NUMBER,
                                      V_ID_VANZARE_RETUR     OUT NUMBER,
                                      V_CURSOR_RETUR         OUT cursor_facturare) is
    lnIdFacturare      NUMBER(10) := null;
    lnListareDetaliata NUMBER(1) := 0;
  begin
    pack_facturare.scrie_factura_avize_retur(V_DISCOUNT_FACTURA,
                                             V_ID_DELEGAT,
                                             V_ID_MASINA,
                                             lnIdFacturare,
                                             lnListareDetaliata,
                                             V_DATAORA_EXP,
                                             V_ID_AGENT,
                                             V_DISCOUNT_EVIDENTIAT,
                                             V_TEXT_ADITIONAL,
                                             V_TEXT_ADITIONAL_RETUR,
                                             V_SIR_MODIFICARI_ACONT,
                                             V_SIR_MODIFICARI_PART,
                                             V_PARAMETRU_ADITIONAL,
                                             V_SERIE_ACT,
                                             V_NUMAR_ACT,
                                             V_ID_VANZARE,
                                             V_ID_VANZARE_RETUR,
                                             V_CURSOR_RETUR);
  end;*/
  -------------------------------------------------------------------
  PROCEDURE scrie_factura_avize_retur(V_DISCOUNT_FACTURA     IN NUMBER,
                                      V_ID_DELEGAT           IN NUMBER,
                                      V_ID_MASINA            IN NUMBER,
                                      V_ID_FACTURARE         IN NUMBER,
                                      V_LISTARE_DETALIATA    IN NUMBER,
                                      V_DATAORA_EXP          IN DATE,
                                      V_ID_AGENT             IN NUMBER,
                                      V_DISCOUNT_EVIDENTIAT  IN NUMBER,
                                      V_TEXT_ADITIONAL       IN VARCHAR2,
                                      V_TEXT_ADITIONAL_RETUR IN VARCHAR2,
                                      V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                      V_SIR_MODIFICARI_PART  IN VARCHAR2,
                                      V_PARAMETRU_ADITIONAL  IN NUMBER,
                                      V_SERIE_ACT            IN VARCHAR2,
                                      V_NUMAR_ACT            IN NUMBER,
                                      V_ID_VANZARE           OUT NUMBER,
                                      V_ID_VANZARE_RETUR     OUT NUMBER,
                                      V_CURSOR_RETUR         OUT cursor_facturare) IS

    lcSirIdFacturi      VARCHAR2(3000);
    V_NUME_DOC          NOM_FDOC.FEL_DOCUMENT%TYPE;
    V_CURSOR_VERIFICARE cursor_facturare;
    V_SCRIS             NUMBER(1);
    V_SEPARATOR         VARCHAR2(1) := ',';
  BEGIN
    IF V_SIR_MODIFICARI_ACONT IS NOT NULL OR
       V_SIR_MODIFICARI_PART IS NOT NULL THEN
      -- nu a fost scrisa factura la prima apelare
      pack_facturare.finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA,
                                                    V_ID_DELEGAT,
                                                    V_ID_MASINA,
                                                    V_ID_FACTURARE,
                                                    V_LISTARE_DETALIATA,
                                                    V_DATAORA_EXP,
                                                    V_ID_AGENT,
                                                    V_TEXT_ADITIONAL,
                                                    V_SIR_MODIFICARI_ACONT,
                                                    V_SIR_MODIFICARI_PART,
                                                    V_PARAMETRU_ADITIONAL,
                                                    V_ID_VANZARE);
    END IF;
    -- scrie avizul de retur
    DELETE FROM VANZARI_DETALII_TEMP;

    SELECT STRINGAGG(COD)
      INTO pack_facturare.ccod_retur
      FROM VANZARI
     WHERE ID_VANZARE IN
           (SELECT X AS ID_VANZARE
              FROM table(charn2collection(pack_facturare.clistaid,
                                          V_SEPARATOR)));

    SELECT STRINGAGG(ID_VANZARE_FACT)
      INTO lcSirIdFacturi
      FROM (SELECT DISTINCT ID_VANZARE_FACT
              FROM VANZARI_CORESP
             WHERE ID_VANZARE_AVIZ IN
                   (SELECT X AS ID_VANZARE_AVIZ
                      FROM table(charn2collection(pack_Facturare.clistaid,
                                                  V_SEPARATOR)))
               AND STERS = 0
               AND TIP = 1);

    INSERT INTO VANZARI_DETALII_TEMP
      (CUSTODIE,
       ID_ARTICOL,
       SERIE,
       ID_POL,
       DISCOUNT_UNITAR,
       PRET,
       CANTITATE,
       PROC_TVAV,
       ID_GESTIUNE,
       CONT,
       ID_UTIL,
       ID_VALUTA,
       PRET_CU_TVA,
       ID_JTVA_COLOANA,
       PRET_ACHIZITIE,
       PRETD,
       ID_VALUTAD,
       IN_STOC,
       CURS,
       MULTIPLICATOR)
      SELECT CUSTODIE,
             ID_ARTICOL,
             SERIE,
             ID_POL,
             DISCOUNT_UNITAR,
             PRET,
             CANTITATE,
             PROC_TVAV,
             ID_GESTIUNE,
             CONT,
             ID_UTIL,
             ID_VALUTA,
             PRET_CU_TVA,
             (CASE
               WHEN ID_JTVA_COLOANA IS NULL THEN
                (CASE PROC_TVAV
                  WHEN 1.24 THEN
                   15
                  WHEN 1.19 THEN
                   1
                  WHEN 1.09 THEN
                   3
                  ELSE
                   8
                END)
               ELSE
                ID_JTVA_COLOANA
             END) AS ID_JTVA_COLOANA,
             PRET_ACHIZITIE,
             PRETD,
             ID_VALUTAD,
             1 AS IN_STOC,
             CURS,
             MULTIPLICATOR
        FROM (SELECT A.CUSTODIE,
                     A.ID_ARTICOL,
                     A.SERIE,
                     A.ID_POL,
                     NVL(A.DISCOUNT_UNITAR, 0) AS DISCOUNT_UNITAR,
                     A.PRET,
                     SUM(CASE
                           WHEN A.ID_VANZARE IN
                                (SELECT X AS ID_VANZARE
                                   FROM table(charn2collection(lcSirIdFacturi,
                                                               V_SEPARATOR))) THEN
                            A.CANTITATE
                           ELSE
                            (-1) * A.CANTITATE
                         END) AS CANTITATE,
                     A.PROC_TVAV,
                     A.ID_GESTIUNE,
                     A.CONT,
                     pack_facturare.nid_util AS ID_UTIL,
                     A.ID_VALUTA,
                     A.PRET_CU_TVA,
                     A.PRET_ACHIZITIE,
                     A.PRETD,
                     A.ID_VALUTAD,
                     NVL(B.CURS, 1) AS CURS,
                     NVL(B.MULTIPLICATOR, 1) AS MULTIPLICATOR,
                     A.ID_JTVA_COLOANA
                FROM VANZARI_DETALII A
                LEFT JOIN VANZARI_CURSURI B
                  ON A.ID_VANZARE = B.ID_VANZARE
                 AND A.ID_VALUTA = B.ID_VALUTA
               WHERE A.STERS = 0
                 AND A.ID_VANZARE IN
                     (SELECT X AS ID_VANZARE
                        FROM table(charn2collection(pack_facturare.clistaid ||
                                                    V_SEPARATOR ||
                                                    lcSirIdFacturi,
                                                    V_SEPARATOR)))
               GROUP BY A.CUSTODIE,
                        A.ID_ARTICOL,
                        A.ID_POL,
                        NVL(A.DISCOUNT_UNITAR, 0),
                        A.SERIE,
                        A.PRET_ACHIZITIE,
                        A.PRETD,
                        A.ID_VALUTAD,
                        A.PRET,
                        A.PROC_TVAV,
                        A.ID_JTVA_COLOANA,
                        A.ID_GESTIUNE,
                        A.CONT,
                        A.ID_VALUTA,
                        A.PRET_CU_TVA,
                        NVL(B.CURS, 1),
                        NVL(B.MULTIPLICATOR, 1))
       WHERE CANTITATE < 0;

    /*    lcSelect_discount := 'SELECT (-1)*SUM(DISCOUNT) FROM VANZARI WHERE ID_VANZARE IN (' ||
                         pack_facturare.v_listaid || ')';

    EXECUTE IMMEDIATE lcSelect_discount
      INTO V_DISCOUNT_AVIZ;*/

    pack_facturare.nperechec := 0;
    pack_facturare.nid_factc := 0;

    pack_facturare.ntip := 24;
    -- numar aviz retur
    pack_facturare.cserie_act := V_SERIE_ACT;
    pack_facturare.nnumar_act := V_NUMAR_ACT;

    pack_facturare.nid_set := 25000 + pack_facturare.ntip - 1 +
                              pack_facturare.nscadere_stoc * 10;

    pack_facturare.citeste_setari_document(pack_facturare.ntip,
                                           pack_facturare.nid_fdoc,
                                           V_NUME_DOC);
    -- numar de bon pentru retur
    pack_serii_numere.aloca_numar(pack_facturare.nid_tipbon,
                                  NULL,
                                  pack_facturare.nid_gestiune_sursa,
                                  pack_facturare.nid_util,
                                  pack_facturare.nid_sucursala,
                                  pack_facturare.nnumar_bon);

    pack_facturare.scrie_aviz_retur(V_ID_DELEGAT,
                                    V_ID_MASINA,
                                    V_ID_FACTURARE,
                                    V_LISTARE_DETALIATA,
                                    V_DATAORA_EXP,
                                    V_ID_AGENT,
                                    V_TEXT_ADITIONAL_RETUR,
                                    V_DISCOUNT_EVIDENTIAT,
                                    V_SCRIS,
                                    V_ID_VANZARE_RETUR,
                                    V_CURSOR_VERIFICARE);

    CLOSE V_CURSOR_VERIFICARE;

    IF V_SCRIS = 0 THEN
      pack_facturare.finalizeaza_factura(0,
                                         V_ID_DELEGAT,
                                         V_ID_MASINA,
                                         V_ID_FACTURARE,
                                         V_LISTARE_DETALIATA,
                                         V_DATAORA_EXP,
                                         V_ID_AGENT,
                                         V_TEXT_ADITIONAL_RETUR,
                                         0);
      V_ID_VANZARE_RETUR := pack_facturare.nid_vanzare;
    END IF;

    OPEN V_CURSOR_RETUR FOR
      SELECT ID_VANZARE_DET,
             NULL                AS EXPLICATIE,
             DENUMIRE,
             SERIE,
             DISCOUNT_UNITAR,
             DISCOUNT_UNITAR_VAL,
             PRET,
             PRET_VAL,
             CURS,
             MULTIPLICATOR,
             CANTITATE,
             PROC_TVAV,
             UM,
             ID_VALUTA,
             PRET_CU_TVA,
             TIP_VALUTA,
             CODMAT,
             CODMATF,
             ID_JTVA_COLOANA,
             CODBARE,
             0                   AS PRET_ACHIZITIE,
             NULL                AS ID_VANZARE_SET,
             0                   AS ID_GESTIUNE,
             0                   AS ID_CTR,
             NULL                AS NUMAR_CONTRACT,
             NUME_VAL
        FROM (SELECT MIN(A.ID_VANZARE_DET) AS ID_VANZARE_DET,
                     B.DENUMIRE,
                     A.SERIE,
                     C.CURS,
                     C.MULTIPLICATOR,
                     (CASE
                       WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(C.CURS, 0) *
                              ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                                    pack_sesiune.nzecimale_pretvval) /
                              NVL(C.MULTIPLICATOR, 1),
                              pack_sesiune.nzecimale_pretv)
                       ELSE
                        ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                              pack_sesiune.nzecimale_pretv)
                     END) AS DISCOUNT_UNITAR,
                     (CASE
                       WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                              pack_sesiune.nzecimale_pretvval)
                       ELSE
                        0
                     END) AS DISCOUNT_UNITAR_VAL,
                     (CASE
                       WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(C.CURS, 0) *
                              ROUND(NVL(A.PRET, 0),
                                    pack_sesiune.nzecimale_pretvval) /
                              NVL(C.MULTIPLICATOR, 1),
                              pack_sesiune.nzecimale_pretv)
                       ELSE
                        ROUND(NVL(A.PRET, 0), pack_sesiune.nzecimale_pretv)
                     END) AS PRET,
                     (CASE
                       WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                        ROUND(NVL(A.PRET, 0), pack_sesiune.nzecimale_pretvval)
                       ELSE
                        0
                     END) AS PRET_VAL,
                     SUM(CASE
                           WHEN A.ID_VANZARE IN
                                (SELECT X AS ID_VANZARE
                                   FROM table(charn2collection(lcSirIdFacturi,
                                                               V_SEPARATOR))) THEN
                            A.CANTITATE
                           ELSE
                            (-1) * A.CANTITATE
                         END) AS CANTITATE,
                     A.PROC_TVAV,
                     B.UM,
                     A.ID_VALUTA,
                     A.PRET_CU_TVA,
                     (CASE
                       WHEN A.ID_VALUTA <>
                            pack_facturare.nid_moneda_nationala THEN
                        1
                       ELSE
                        0
                     END) AS TIP_VALUTA,
                     B.CODMAT,
                     B.CODMATF,
                     A.ID_JTVA_COLOANA,
                     B.CODBARE,
                     D.NUME_VAL
                FROM VANZARI_DETALII A
                LEFT JOIN NOM_ARTICOLE B
                  ON A.ID_ARTICOL = B.ID_ARTICOL
                LEFT JOIN VANZARI_CURSURI C
                  ON A.ID_VALUTA = C.ID_VALUTA
                 AND A.ID_VANZARE = C.ID_VANZARE
                LEFT JOIN NOM_VALUTE D
                  ON A.ID_VALUTA = D.ID_VALUTA
               WHERE A.STERS = 0
                 AND A.ID_VANZARE IN
                     (SELECT X AS ID_VANZARE
                        FROM table(charn2collection(pack_facturare.clistaid ||
                                                    V_SEPARATOR ||
                                                    lcSirIdFacturi,
                                                    V_SEPARATOR)))
               GROUP BY B.DENUMIRE,
                        A.SERIE,
                        (CASE
                          WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                           ROUND(NVL(C.CURS, 0) *
                                 ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                                       pack_sesiune.nzecimale_pretvval) /
                                 NVL(C.MULTIPLICATOR, 1),
                                 pack_sesiune.nzecimale_pretv)
                          ELSE
                           ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                                 pack_sesiune.nzecimale_pretv)
                        END),
                        (CASE
                          WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                           ROUND(NVL(A.DISCOUNT_UNITAR, 0),
                                 pack_sesiune.nzecimale_pretvval)
                          ELSE
                           0
                        END),
                        (CASE
                          WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                           ROUND(NVL(C.CURS, 0) *
                                 ROUND(NVL(A.PRET, 0),
                                       pack_sesiune.nzecimale_pretvval) /
                                 NVL(C.MULTIPLICATOR, 1),
                                 pack_sesiune.nzecimale_pretv)
                          ELSE
                           ROUND(NVL(A.PRET, 0), pack_sesiune.nzecimale_pretv)
                        END),
                        (CASE
                          WHEN NVL(D.MONEDA_NATIONALA, 1) <> 1 THEN
                           ROUND(NVL(A.PRET, 0),
                                 pack_sesiune.nzecimale_pretvval)
                          ELSE
                           0
                        END),
                        A.PROC_TVAV,
                        A.ID_JTVA_COLOANA,
                        B.UM,
                        B.CODMAT,
                        B.CODMATF,
                        B.CODBARE,
                        A.ID_VALUTA,
                        A.PRET_CU_TVA,
                        C.CURS,
                        C.MULTIPLICATOR,
                        (CASE
                          WHEN A.ID_VALUTA <>
                               pack_facturare.nid_moneda_nationala THEN
                           1
                          ELSE
                           0
                        END),
                        D.NUME_VAL)
       WHERE CANTITATE < 0;

  END scrie_factura_avize_retur;
  /* -------------------------------------------------------------------
  PROCEDURE scrie_factura_avize(V_DISCOUNT_FACTURA    IN NUMBER,
                                V_SERIE_ACT_INCASARE  IN VARCHAR2,
                                V_NUMAR_ACT_INCASARE  IN NUMBER,
                                V_LISTA_INCASARE      IN VARCHAR2,
                                V_ID_DELEGAT          IN NUMBER,
                                V_ID_MASINA           IN NUMBER,
                                V_DATAORA_EXP         IN DATE,
                                V_ID_AGENT            IN NUMBER,
                                V_TEXT_ADITIONAL      IN VARCHAR2,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_VERIFICARE_FACTURAT IN NUMBER,
                                V_ID_VANZARE          OUT NUMBER,
                                V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare) is
    lnIdFacturare      NUMBER(10) := null;
    lnListareDetaliata NUMBER(1) := 0;
  begin
    pack_facturare.scrie_factura_avize(V_DISCOUNT_FACTURA,
                                       V_SERIE_ACT_INCASARE,
                                       V_NUMAR_ACT_INCASARE,
                                       V_LISTA_INCASARE,
                                       V_ID_DELEGAT,
                                       V_ID_MASINA,
                                       lnIdFacturare,
                                       lnListareDetaliata,
                                       V_DATAORA_EXP,
                                       V_ID_AGENT,
                                       V_TEXT_ADITIONAL,
                                       V_DISCOUNT_EVIDENTIAT,
                                       V_VERIFICARE_FACTURAT,
                                       V_ID_VANZARE,
                                       V_CURSOR_VERIFICARE);
  end;*/
  -------------------------------------------------------------------
  PROCEDURE scrie_factura_avize(V_DISCOUNT_FACTURA    IN NUMBER,
                                V_SERIE_ACT_INCASARE  IN VARCHAR2,
                                V_NUMAR_ACT_INCASARE  IN NUMBER,
                                V_LISTA_INCASARE      IN VARCHAR2,
                                V_ID_DELEGAT          IN NUMBER,
                                V_ID_MASINA           IN NUMBER,
                                V_ID_FACTURARE        IN NUMBER,
                                V_LISTARE_DETALIATA   IN NUMBER,
                                V_DATAORA_EXP         IN DATE,
                                V_ID_AGENT            IN NUMBER,
                                V_TEXT_ADITIONAL      IN VARCHAR2,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_VERIFICARE_FACTURAT IN NUMBER,
                                V_ID_VANZARE          OUT NUMBER,
                                V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare) IS

    TYPE tab_detalii_type IS TABLE OF vanzari_detalii_temp%ROWTYPE;
    tab_detalii   tab_detalii_type;
    articole_aviz tab_articole_aviz_type;

    V_INCASAT_CALCUL        ACT.SUMA%TYPE := 0;
    V_DISCOUNT_TOTAL_CU_TVA ACT.SUMA%TYPE := 0;

    V_PRET             ACT.SUMA%TYPE;
    V_ID_VENCHELT      ACT.ID_VENCHELT%TYPE;
    V_ID_SECTIE        ACT.ID_SECTIE%TYPE;
    V_EXPLICATIE       ACT.EXPLICATIA%TYPE;
    V_ASCD             ACT.ASCD%TYPE;
    V_SCD              ACT.SCD%TYPE;
    V_SCC              ACT.SCC%TYPE;
    V_CU_TVA           NOTE_CONTABILE.CU_TVA%TYPE;
    V_IN_VALUTA        NOTE_CONTABILE.IN_VALUTA%TYPE;
    V_CANTITATE        VANZARI_DETALII.CANTITATE%TYPE;
    V_CANTITATE_RAMASA VANZARI_DETALII.CANTITATE%TYPE;
    V_PTVA             NUMBER(4);

    V_LISTAID VARCHAR2(32000) := '<>';
    V_ID_CTR  ACT.ID_CTR%TYPE;

    V_DISCOUNT_UNITAR VANZARI_DETALII.DISCOUNT_UNITAR%TYPE;
  BEGIN
    pack_contafin.sterge_temp_actrul();
    pack_facturare.ndiscount_evidentiat := V_DISCOUNT_EVIDENTIAT;
    pack_facturare.initializeaza_setari_facturare();

    V_SCD         := '4111';
    V_SCC         := '418';
    V_ID_VENCHELT := 0;
    V_CU_TVA      := 0;
    V_IN_VALUTA   := 0;

    V_EXPLICATIE := SUBSTR('FACTURARE AVIZE ' || pack_facturare.cdescriere,
                           1,
                           100);

    pack_facturare.nid_partc := pack_facturare.nid_part;

    SELECT * BULK COLLECT INTO tab_detalii FROM VANZARI_DETALII_TEMP;

    IF SQL%ROWCOUNT = 0 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu exista articole in lista de vanzare! (FACT-005)');
    END IF;

    FOR i IN tab_detalii.first .. tab_detalii.last LOOP

      IF tab_detalii(i).proc_tvav > 1 THEN
        V_CU_TVA := 1;
        V_PTVA   := (tab_detalii(i).proc_tvav - 1) * 100;
      ELSE
        V_CU_TVA := 0;
        V_PTVA   := 0;
      END IF;

      IF pack_facturare.nproc_tva_max < V_PTVA THEN
        pack_facturare.nproc_tva_max    := V_PTVA;
        pack_facturare.nid_jtva_coloana := tab_detalii(i).id_jtva_coloana;
        pack_facturare.nTaxCode         := tab_detalii(i).taxcode;
      END IF;

      SELECT A.ID_VANZARE,
             A.ID_VANZARE_DET,
             A.CANTITATE,
             A.CUSTODIE,
             C.COD,
             C.ID_FACT,
             C.NRACT,
             C.SERIE_ACT,
             C.DATAACT,
             MIN(C.ASCD) AS ASCD,
             NVL(B.ID_SECTIE, E.ID_SECTIE) AS ID_SECTIE
        BULK COLLECT
        INTO articole_aviz
        FROM VANZARI_DETALII A
        LEFT JOIN VANZARI B
          ON A.ID_VANZARE = B.ID_VANZARE
        LEFT JOIN ACT C
          ON B.COD = C.COD
         AND C.SCD = DECODE(B.TIP, 42, '357', '418')
         AND DECODE(B.TIP, 42, '4428', C.SCC) = '4428'
        LEFT JOIN CRM_POLITICI_PRETURI D
          ON A.ID_POL = D.ID_POL
        LEFT JOIN CRM_NOTE_VANZARI E
          ON D.ID_NOTA = E.ID_NOTA
       WHERE A.ID_VANZARE IN
             (SELECT X AS ID_VANZARE
                FROM table(cast(CHARN2COLLECTION(pack_facturare.clistaid,
                                                 ',') as num_tab)))
         AND A.ID_ARTICOL = tab_detalii(i).id_articol
         AND A.DISCOUNT_UNITAR = tab_detalii(i).discount_unitar
         AND A.ID_POL = tab_detalii(i).id_pol
         AND A.ID_VALUTA = tab_detalii(i).id_valuta
         AND A.PRET = tab_detalii(i).pret
         AND A.PRET_ACHIZITIE = tab_detalii(i).pret_achizitie
         AND NVL(A.PRETD, 0) = NVL(tab_detalii(i).pretd, NVL(A.PRETD, 0))
         AND NVL(A.ID_VALUTAD, -99) =
             NVL(tab_detalii(i).id_valutad, NVL(A.ID_VALUTAD, -99))
         AND NVL(A.ID_GESTIUNE, -9999) =
             NVL(tab_detalii(i).id_gestiune, -9999)
         AND NVL(A.CONT, 'XXXX') = NVL(tab_detalii(i).cont, 'XXXX')
         AND NVL(A.SERIE, '+_') = NVL(tab_detalii(i).serie, '+_')
       GROUP BY A.ID_VANZARE,
                A.ID_VANZARE_DET,
                A.CANTITATE,
                A.CUSTODIE,
                C.COD,
                C.ID_FACT,
                C.NRACT,
                C.SERIE_ACT,
                C.DATAACT,
                NVL(B.ID_SECTIE, E.ID_SECTIE)
       ORDER BY 6;

      V_CANTITATE_RAMASA := tab_detalii(i).cantitate;

      IF SQL%ROWCOUNT = 0 THEN
        RAISE_APPLICATION_ERROR(-20000,
                                'Nu au fost gasite articolele! (FACT-015)');
      END IF;

      FOR j IN articole_aviz.first .. articole_aviz.last LOOP
        EXIT WHEN V_CANTITATE_RAMASA = 0;

        pack_facturare.nid_factc := articole_aviz(j).id_fact;
        pack_facturare.nperechec := articole_aviz(j).nract;

        V_CANTITATE := articole_aviz(j).cantitate;

        IF V_CANTITATE_RAMASA >= V_CANTITATE THEN
          V_CANTITATE_RAMASA := V_CANTITATE_RAMASA - V_CANTITATE;

        ELSE
          V_CANTITATE        := V_CANTITATE_RAMASA;
          V_CANTITATE_RAMASA := 0;
        END IF;

        -- modificare 20.11
        IF instr(V_LISTAID, '<' || articole_aviz(j).id_vanzare || '>') = 0 THEN
          V_LISTAID := V_LISTAID || '<' || articole_aviz(j).id_vanzare || '>,';
        END IF;

        IF articole_aviz(j).custodie = 1 THEN
          pack_facturare.nfactavizcust := 1;
          tab_detalii(i).cantitate := V_CANTITATE;
          tab_detalii(i).id_rata := articole_aviz(j).cod;
          V_INCASAT_CALCUL := V_INCASAT_CALCUL +
                              pack_facturare.contabilizeaza_articol(tab_detalii(i));

          UPDATE VANZARI_DETALII_TEMP
             SET CANTITATE = CANTITATE - V_CANTITATE
           WHERE ID_TEMP = tab_detalii(i).id_temp;

          MERGE INTO VANZARI_DETALII_TEMP A
          USING DUAL B
          ON (A.ID_ARTICOL = tab_detalii(i).id_articol AND A.ID_POL = tab_detalii(i).id_pol AND A.PRET = tab_detalii(i).pret AND A.PRET_CU_TVA = tab_detalii(i).pret_cu_tva AND A.PROC_TVAV = tab_detalii(i).proc_tvav AND A.DISCOUNT_UNITAR = tab_detalii(i).discount_unitar AND A.ID_GESTIUNE = tab_detalii(i).id_gestiune AND NVL(A.CONT, 'XXXX') = NVL(tab_detalii(i).cont, 'XXX') AND NVL(A.ID_VALUTA, -1) = NVL(tab_detalii(i).id_valuta, -1) AND NVL(A.CURS, 1) = NVL(tab_detalii(i).curs, 1) AND A.ID_JTVA_COLOANA = tab_detalii(i).id_jtva_coloana AND A.IN_STOC = tab_detalii(i).in_stoc AND NVL(A.ID_GESTIUNE_DEST, -99) = NVL(tab_detalii(i).id_gestiune_dest, -99) AND NVL(A.ID_UTIL, 0) = NVL(tab_detalii(i).id_util, 0) AND A.PRET_ACHIZITIE = tab_detalii(i).pret_achizitie AND NVL(A.ID_VANZARE_SET, -99) = NVL(tab_detalii(i).id_vanzare_set, -99) AND A.CUSTODIE = articole_aviz(j).custodie)
          WHEN MATCHED THEN
            UPDATE SET CANTITATE = CANTITATE + V_CANTITATE
          WHEN NOT MATCHED THEN
            INSERT
              (ID_ARTICOL,
               ID_POL,
               PRET,
               PRET_CU_TVA,
               PROC_TVAV,
               CANTITATE,
               DISCOUNT_UNITAR,
               ID_GESTIUNE,
               CONT,
               ID_VALUTA,
               CURS,
               MULTIPLICATOR,
               ID_JTVA_COLOANA,
               IN_STOC,
               ID_GESTIUNE_DEST,
               ID_COMANDA,
               ID_UTIL,
               PRET_ACHIZITIE,
               PRETD,
               ID_VALUTAD,
               ID_VANZARE_SET,
               ID_LUCRARE_REZ,
               ID_PART_REZ,
               NUMAR_ACT,
               CUSTODIE,
               ID_TEMP)
            VALUES
              (tab_detalii(i).id_articol,
               tab_detalii(i).id_pol,
               tab_detalii(i).pret,
               tab_detalii(i).pret_cu_tva,
               tab_detalii(i).proc_tvav,
               V_CANTITATE,
               tab_detalii(i).discount_unitar,
               tab_detalii(i).id_gestiune,
               tab_detalii(i).cont,
               tab_detalii(i).id_valuta,
               tab_detalii(i).curs,
               tab_detalii(i).multiplicator,
               tab_detalii(i).id_jtva_coloana,
               tab_detalii(i).in_stoc,
               tab_detalii(i).id_gestiune_dest,
               tab_detalii(i).id_comanda,
               tab_detalii(i).id_util,
               tab_detalii(i).pret_achizitie,
               NVL(tab_detalii(i).pretd, 0),
               tab_detalii(i).id_valutad,
               tab_detalii(i).id_vanzare_set,
               tab_detalii(i).id_lucrare_rez,
               tab_detalii(i).id_part_rez,
               pack_facturare.nnumar_act,
               articole_aviz(j).custodie,
               -1000);

          pack_facturare.nfactavizcust := 0;
        ELSE
          IF pack_facturare.ndiscount_evidentiat = 1 AND tab_detalii(i).discount_unitar <> 0 THEN
            V_PRET            := tab_detalii(i).pret;
            V_DISCOUNT_UNITAR := tab_detalii(i).discount_unitar;

            V_DISCOUNT_TOTAL_CU_TVA := V_DISCOUNT_TOTAL_CU_TVA +
                                       pack_facturare.scrie_discount(V_CANTITATE,
                                                                     tab_detalii  (i).discount_unitar,
                                                                     tab_detalii  (i).id_valuta,
                                                                     V_IN_VALUTA,
                                                                     articole_aviz(j).ascd, -- ASCD
                                                                     articole_aviz(j).id_sectie,
                                                                     tab_detalii  (i).id_ctr,
                                                                     tab_detalii  (i).curs / tab_detalii(i).multiplicator,
                                                                     tab_detalii  (i).proc_tvav,
                                                                     tab_detalii  (i).pret_cu_tva,
                                                                     V_CU_TVA,
                                                                     tab_detalii  (i).id_jtva_coloana);
          ELSE
            V_PRET            := tab_detalii(i).pret - tab_detalii(i).discount_unitar;
            V_DISCOUNT_UNITAR := 0;
          END IF;

          -- scriere nota in act
          V_INCASAT_CALCUL := V_INCASAT_CALCUL +
                              pack_facturare.scrie_nota(V_CANTITATE,
                                                        V_PRET,
                                                        V_DISCOUNT_UNITAR,
                                                        tab_detalii           (i).pret_cu_tva,
                                                        tab_detalii           (i).id_valuta,
                                                        V_IN_VALUTA,
                                                        tab_detalii           (i).curs / tab_detalii(i).multiplicator,
                                                        V_ID_VENCHELT,
                                                        articole_aviz         (j).id_sectie,
                                                        tab_detalii           (i).id_ctr,
                                                        V_EXPLICATIE,
                                                        V_SCD,
                                                        articole_aviz         (j).ascd, -- ascd
                                                        V_SCC,
                                                        articole_aviz         (j).ascd, -- ascc
                                                        0, -- V_ID_GESTIN
                                                        0, -- V_ID_GESTOUT
                                                        pack_facturare.nid_set, -- V_ID_SET
                                                        V_CU_TVA,
                                                        tab_detalii           (i).proc_tvav * 100 - 100,
                                                        tab_detalii           (i).id_jtva_coloana,
                                                        tab_detalii           (i).taxcode,
                                                        null);
        END IF;
      END LOOP;

    END LOOP;

    BEGIN
      SELECT ID_CTR
        INTO V_ID_CTR
        FROM (SELECT SUM(SUMA), ID_CTR
                FROM ACT_TEMP
               WHERE SCD IN ('4111', '418')
               GROUP BY ID_CTR
               ORDER BY 1)
       WHERE ROWNUM < 2;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_ID_CTR := NULL;
    END;

    IF V_DISCOUNT_FACTURA <> 0 THEN
      SELECT DISTINCT ASCD, ID_SECTIE
        INTO V_ASCD, V_ID_SECTIE
        FROM ACT_TEMP
       WHERE SCD = '4111'
         AND ROWNUM < 2;

      V_DISCOUNT_TOTAL_CU_TVA := V_DISCOUNT_TOTAL_CU_TVA +
                                 pack_facturare.scrie_discount(1,
                                                               V_DISCOUNT_FACTURA,
                                                               pack_facturare.nid_valuta,
                                                               pack_facturare.nin_valuta,
                                                               V_ASCD,
                                                               V_ID_SECTIE,
                                                               V_ID_CTR,
                                                               1,
                                                               (100 +
                                                               pack_facturare.nproc_tva_max) / 100,
                                                               1,
                                                               1, -- V_CU_TVA
                                                               pack_facturare.nid_jtva_coloana,
                                                               pack_facturare.nTaxCode);
    END IF;

    V_INCASAT_CALCUL := V_INCASAT_CALCUL - V_DISCOUNT_TOTAL_CU_TVA;

    /*    IF V_AVANS <> 0 THEN
      V_INCASAT_CALCUL := V_INCASAT_CALCUL - ABS(v_avans);
      pack_facturare.scrie_avans((-1) * ABS(v_avans));
    END IF;*/

    IF V_LISTA_INCASARE IS NOT NULL THEN
      pack_facturare.scrie_incasari(V_SERIE_ACT_INCASARE,
                                    V_NUMAR_ACT_INCASARE,
                                    V_LISTA_INCASARE,
                                    V_ID_CTR);
    END IF;

    -- modificare 22.08.2008
    DELETE FROM VANZARI_DETALII_TEMP WHERE CANTITATE = 0;

    -- cumulare note
    pack_facturare.cumuleaza_note_act();

    pack_facturare.clistaid_avize := REPLACE(REPLACE(V_LISTAID, '<'), '>');

    if pack_contafin.verifica_note_contabile(1) > 0 then
      pack_facturare.cursor_verificare(V_CURSOR_VERIFICARE);
      V_ID_VANZARE := NULL;
    else
      pack_facturare.finalizeaza_factura(V_DISCOUNT_FACTURA,
                                         V_ID_DELEGAT,
                                         V_ID_MASINA,
                                         V_ID_FACTURARE,
                                         V_LISTARE_DETALIATA,
                                         V_DATAORA_EXP,
                                         V_ID_AGENT,
                                         V_TEXT_ADITIONAL,
                                         V_VERIFICARE_FACTURAT);

      OPEN V_CURSOR_VERIFICARE FOR
        SELECT * FROM ACT_TEMP WHERE 1 = 2;
      V_ID_VANZARE := pack_facturare.nid_vanzare;
    end if;

  END scrie_factura_avize;
  -------------------------------------------------------------------
  /*  PROCEDURE scrie_aviz_retur(V_ID_DELEGAT          IN NUMBER,
                             V_ID_MASINA           IN NUMBER,
                             V_DATAORA_EXP         IN DATE,
                             V_ID_AGENT            IN NUMBER,
                             V_TEXT_ADITIONAL      IN VARCHAR2,
                             V_DISCOUNT_EVIDENTIAT IN NUMBER,
                             V_SCRIS               OUT NUMBER,
                             V_ID_VANZARE          OUT NUMBER,
                             V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare) IS
    lnIdFacturare      NUMBER(10) := null;
    lnListareDetaliata NUMBER(1) := 0;
  begin
    pack_facturare.scrie_aviz_retur(V_ID_DELEGAT,
                                    V_ID_MASINA,
                                    lnIdFacturare,
                                    lnListareDetaliata,
                                    V_DATAORA_EXP,
                                    V_ID_AGENT,
                                    V_TEXT_ADITIONAL,
                                    V_DISCOUNT_EVIDENTIAT,
                                    V_SCRIS,
                                    V_ID_VANZARE,
                                    V_CURSOR_VERIFICARE);
  end;*/
  -------------------------------------------------------------------
  PROCEDURE scrie_aviz_retur(V_ID_DELEGAT          IN NUMBER,
                             V_ID_MASINA           IN NUMBER,
                             V_ID_FACTURARE        IN NUMBER,
                             V_LISTARE_DETALIATA   IN NUMBER,
                             V_DATAORA_EXP         IN DATE,
                             V_ID_AGENT            IN NUMBER,
                             V_TEXT_ADITIONAL      IN VARCHAR2,
                             V_DISCOUNT_EVIDENTIAT IN NUMBER,
                             V_SCRIS               OUT NUMBER,
                             V_ID_VANZARE          OUT NUMBER,
                             V_CURSOR_VERIFICARE   OUT pack_facturare.cursor_facturare) IS

    TYPE tab_detalii_type IS TABLE OF vanzari_detalii_temp%ROWTYPE;
    tab_detalii tab_detalii_type;

    V_INCASAT_CALCUL ACT.SUMA%TYPE := 0;
  BEGIN
    pack_contafin.sterge_temp_actrul();
    pack_facturare.ndiscount_evidentiat := V_DISCOUNT_EVIDENTIAT;
    pack_facturare.initializeaza_setari_facturare();

    SELECT * BULK COLLECT INTO tab_detalii FROM VANZARI_DETALII_TEMP;

    IF SQL%ROWCOUNT = 0 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu exista articole in lista de vanzare! (FACT-006)');
    END IF;

    FOR i IN tab_detalii.first .. tab_detalii.last LOOP
      pack_facturare.nfactavizcust := tab_detalii(i).custodie;
      IF pack_facturare.nfactavizcust = 1 THEN
        pack_facturare.descarca_gestiune(tab_detalii                   (i).id_articol,
                                         tab_detalii                   (i).serie,
                                         tab_detalii                   (i).pret_achizitie,
                                         tab_detalii                   (i).pretd,
                                         tab_detalii                   (i).id_valutad,
                                         tab_detalii                   (i).pretv_orig,
                                         tab_detalii                   (i).pret,
                                         tab_detalii                   (i).diferenta,
                                         tab_detalii                   (i).proc_tvav,
                                         tab_detalii                   (i).pret_cu_tva,
                                         tab_detalii                   (i).cantitate,
                                         tab_detalii                   (i).discount_unitar,
                                         tab_detalii                   (i).id_gestiune,
                                         pack_facturare.nid_sectie_stoc, -- V_ID_SECTIE
                                         tab_detalii                   (i).cont,
                                         pack_facturare.nid_venchelt, -- ID_VENCHELT,
                                         tab_detalii                   (i).id_valuta,
                                         tab_detalii                   (i).curs / tab_detalii(i).multiplicator,
                                         NULL, --      V_ID_PART_REZ
                                         NULL,
                                         tab_detalii                   (i).id_jtva_coloana,
                                         tab_detalii                   (i).taxcode);
      ELSE
        V_INCASAT_CALCUL := V_INCASAT_CALCUL +
                            pack_facturare.contabilizeaza_articol(tab_detalii(i));
      END IF;
      pack_facturare.nfactavizcust := 0;
    END LOOP;

    -- cumulare note
    pack_facturare.scrie_note_banca_compensari();
    pack_facturare.cumuleaza_note_act();

    if pack_contafin.verifica_note_contabile(1) > 0 then
      pack_facturare.cursor_verificare(V_CURSOR_VERIFICARE);
      V_SCRIS      := 0;
      V_ID_VANZARE := NULL;
    else
      pack_facturare.finalizeaza_factura(0, -- V_DISCOUNT_FACTURA
                                         V_ID_DELEGAT,
                                         V_ID_MASINA,
                                         V_ID_FACTURARE,
                                         V_LISTARE_DETALIATA,
                                         V_DATAORA_EXP,
                                         V_ID_AGENT,
                                         V_TEXT_ADITIONAL,
                                         0); -- V_VERIFICAT_FACTURAT

      OPEN V_CURSOR_VERIFICARE FOR
        SELECT * FROM ACT_TEMP WHERE 1 = 2;

      V_SCRIS      := 1;
      V_ID_VANZARE := pack_facturare.nid_vanzare;
    end if;

  END scrie_aviz_retur;
  -------------------------------------------------------------------
  FUNCTION contabilizeaza_articol(detalii_articol VANZARI_DETALII_TEMP%ROWTYPE)
    RETURN NUMBER IS
    V_COMPUS     NUMBER(1);
    V_ID_POL_ART CRM_POLITICI_PRET_ART.Id_Pol_Art%TYPE;

    V_INCASAT_CALCUL ACT.SUMA%TYPE := 0;

    V_SCD        ACT.SCD%TYPE;
    V_ASCD       ACT.ASCD%TYPE;
    V_SCC        ACT.SCC%TYPE;
    V_ASCC       ACT.ASCC%TYPE;
    V_EXPLICATIE ACT.EXPLICATIA%TYPE;
    V_IN_VALUTA  NUMBER(1) := 0;

    /*    CURSOR cursor_elemente_pachet IS
      SELECT A.ID_POL_ART,
             B.ID_POL,
             B.ID_ARTICOL,
             B.PRET,
             B.PROC_TVAV,
             B.ID_VALUTA,
             C.ID_NOTA,
             C.PRETURI_CU_TVA,
             NVL(B.ID_VENCHELT, D.ID_VENCHELT) AS ID_VENCHELT,
             DECODE(B.ID_POL,
                    NVL(pack_facturare.nid_politica_stoc, -99),
                    pack_facturare.nid_sectie_stoc,
                    D.ID_SECTIE) as ID_SECTIE,
             D.ID_SET,
             E.EXPLICATIE,
             E.SCD,
             E.ASCD,
             E.SCC,
             E.ASCC,
             E.CU_TVA,
             E.IN_VALUTA
        FROM CRM_PACHETE_ARTICOLE A
        LEFT JOIN CRM_POLITICI_PRET_ART B ON A.ID_POL_ART = B.ID_POL_ART
        LEFT JOIN CRM_POLITICI_PRETURI C ON B.ID_POL = C.ID_POL
        LEFT JOIN CRM_NOTE_VANZARI D ON C.ID_NOTA = D.ID_NOTA
        LEFT JOIN NOTE_CONTABILE E ON D.ID_SET = E.ID_SET
       WHERE A.STERS = 0
         AND A.ID_PACHET = V_ID_POL_ART;
    crs_rand_elemente_pachet cursor_elemente_pachet%rowtype;*/

    CURSOR cursor_articol IS
      SELECT A.ID_POL_ART,
             A.ID_POL,
             A.ID_ARTICOL,
             A.PRET,
             (CASE
               WHEN A.ID_POL = pack_facturare.nid_politica_stoc AND
                    pack_facturare.nin_valuta = 1 THEN
                pack_facturare.nid_valuta
               ELSE
                A.ID_VALUTA
             END) AS ID_VALUTA,
             B.ID_NOTA,
             B.PRETURI_CU_TVA,
             /*             (CASE
               WHEN (A.ID_POL = pack_facturare.nid_politica_stoc AND
                    pack_facturare.nid_venchelt IS NOT NULL) OR
                    (A.ID_VENCHELT IS NULL AND C.ID_VENCHELT IS NULL) THEN
                pack_facturare.nid_venchelt
               ELSE
                NVL(A.ID_VENCHELT, C.ID_VENCHELT)
             END) AS ID_VENCHELT,
             DECODE(A.ID_POL,
                    NVL(pack_facturare.nid_politica_stoc, -99),
                    pack_facturare.nid_sectie_stoc,
                    C.ID_SECTIE) as ID_SECTIE,*/
             NVL(pack_facturare.nid_venchelt,
                 NVL(A.ID_VENCHELT, C.ID_VENCHELT)) AS ID_VENCHELT,
             NVL(pack_facturare.nid_sectie_stoc, C.ID_SECTIE) as ID_SECTIE,
             C.ID_SET,
             D.EXPLICATIE,
             D.SCD,
             D.ASCD,
             D.SCC,
             D.ASCC,
             D.CU_TVA,
             (CASE
               WHEN A.ID_POL = pack_facturare.nid_politica_stoc AND
                    pack_facturare.nin_valuta = 1 THEN
                1
               ELSE
                D.IN_VALUTA
             END) AS IN_VALUTA
        FROM CRM_POLITICI_PRET_ART A
        LEFT JOIN CRM_POLITICI_PRETURI B
          ON A.ID_POL = B.ID_POL
        LEFT JOIN CRM_NOTE_VANZARI C
          ON B.ID_NOTA = C.ID_NOTA
        LEFT JOIN NOTE_CONTABILE D
          ON C.ID_SET = D.ID_SET
       WHERE
      -- A.STERS = 0 AND
       A.ID_POL = detalii_articol.id_pol
       AND A.ID_ARTICOL = detalii_articol.id_articol;
    crs_rand_articol cursor_articol%rowtype;
    lcArticol        nom_articole.denumire%type;
    lcPolitica       crm_politici_preturi.nume_lista_preturi%type;
  BEGIN

    -- 05.07.2011
    BEGIN
      SELECT COMPUS, ID_POL_ART
        INTO V_COMPUS, V_ID_POL_ART
        FROM VCRM_POLITICI_PRET_ART
       WHERE ID_ARTICOL = detalii_articol.id_articol
         AND ID_POL = detalii_articol.id_pol;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        SELECT DENUMIRE
          INTO lcArticol
          FROM NOM_ARTICOLE
         where id_articol = detalii_articol.id_articol;

        SELECT NUME_LISTA_PRETURI
          INTO lcPolitica
          FROM CRM_POLITICI_PRETURI
         where id_pol = detalii_articol.id_pol;

        RAISE_APPLICATION_ERROR(-20000,
                                'Articolul ' || detalii_articol.id_articol || '|' ||
                                lcArticol ||
                                ' nu este definit in politica de preturi ' ||
                                detalii_articol.id_pol || '|' || lcPolitica ||
                                '! (FACT-024)');
    END;
    -- 05.07.2011 ^

    IF V_COMPUS = 1 THEN
      -- articol compus
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu sunt facute configurarile pentru articolele compuse! (FACT-016 : ' ||
                              detalii_articol.id_articol || '.' ||
                              detalii_articol.id_pol || ' )');
      -- in_valuta = ?

      /*      OPEN cursor_elemente_pachet;
      FETCH cursor_elemente_pachet
        INTO crs_rand_elemente_pachet;
      WHILE cursor_elemente_pachet%FOUND LOOP

        CASE
          WHEN pack_facturare.v_tip <= 20 THEN
            -- factura
            V_SCD  := crs_rand_elemente_pachet.scd;
            V_ASCD := crs_rand_elemente_pachet.ascd;
            V_SCC  := crs_rand_elemente_pachet.scc;
            V_ASCC := crs_rand_elemente_pachet.ascc;
          ELSE
            -- aviz
            V_SCD  := '418';
            V_ASCD := crs_rand_elemente_pachet.ascd;
            V_SCC  := crs_rand_elemente_pachet.scc;
            V_ASCC := crs_rand_elemente_pachet.ascc;
        END CASE;

        IF pack_facturare.v_discount_evidentiat = 0 THEN
          V_PRET := crs_rand_elemente_pachet.pret -
                    detalii_articol.discount_unitar *
                    ROUND(crs_rand_elemente_pachet.pret /
                          detalii_articol.pret,
                          pack_facturare.v_zecimale_procent);
        ELSE
          V_PRET := crs_rand_elemente_pachet.pret;
        END IF;

        V_INCASAT_CALCUL := V_INCASAT_CALCUL +
                     pack_facturare.scrie_nota(detalii_articol.cantitate,
                                               V_PRET,
                                               detalii_articol.pret_cu_tva,
                                               detalii_articol.id_valuta,
                                               crs_rand_elemente_pachet.in_valuta,
                                               detalii_articol.curs,
                                               crs_rand_elemente_pachet.id_venchelt,
                                               crs_rand_elemente_pachet.id_sectie,
                                               crs_rand_elemente_pachet.explicatie,
                                               V_SCD,
                                               V_ASCD,
                                               V_SCC,
                                               V_ASCC,
                                               0,
                                               0,
                                               V_ID_CONTRACT,
                                               pack_facturare.v_id_set,
                                               crs_rand_elemente_pachet.cu_tva,
                                               detalii_articol.proc_tvav * 100 - 100,
                                               detalii_articol.id_jtva_coloana);

        IF pack_facturare.v_scadere_stoc = 1 AND
           detalii_articol.id_gestiune <> -1000 AND
           detalii_articol.in_stoc = 1 THEN
          pack_facturare.descarca_gestiune(crs_rand_elemente_pachet.id_articol,
                                           detalii_articol.serie,
                                           crs_rand_elemente_pachet.pret,
                                           crs_rand_elemente_pachet.proc_tvav,
                                           detalii_articol.pret_cu_tva,
                                           detalii_articol.cantitate,
                                           detalii_articol.discount_unitar *
                                           ROUND(crs_rand_elemente_pachet.pret /
                                                 detalii_articol.pret,
                                                 pack_facturare.v_zecimale_procent),
                                           detalii_articol.id_gestiune,
                                           detalii_articol.cont,
                                           detalii_articol.id_valuta,
                                           detalii_articol.curs);
        END IF;

        FETCH cursor_elemente_pachet
          INTO crs_rand_elemente_pachet;
      END LOOP;
      CLOSE cursor_elemente_pachet;

      -- discount ?      */

    ELSE
      -- articol simplu
      OPEN cursor_articol;
      FETCH cursor_articol
        INTO crs_rand_articol;
      WHILE cursor_articol%FOUND LOOP

        CASE
        -- 30.10.2009
          WHEN pack_facturare.ntip <= 20 or
               pack_facturare.ntip IN (pack_facturare.nTipFacturaHotel,
                                       pack_facturare.nTipFacturaRestaurant,
                                       pack_facturare.nTipNotaPlata,
                                       pack_facturare.nTipVanzareRetail,
                                       48,
                                       49,51,52) THEN
            -- 30.10.2009 ^
            -- factura,factura roahotel
            V_SCD  := crs_rand_articol.scd;
            V_ASCD := NVL(crs_rand_articol.ascd,
                          PACK_FACTURARE.GetAnaliticByGrupUtilizatori(pack_facturare.nid_util,
                                                                      V_SCD));
          WHEN pack_facturare.ntip in (28, 29) THEN
            -- aviz catre clienti debitori
            V_SCD  := '461';
            V_ASCD := PACK_FACTURARE.GetAnaliticByGrupUtilizatori(pack_facturare.nid_util,
                                                                  V_SCD);
          ELSE
            -- aviz
            V_SCD  := '418';
            V_ASCD := PACK_FACTURARE.GetAnaliticByGrupUtilizatori(pack_facturare.nid_util,
                                                                  V_SCD);
        END CASE;

        V_SCC  := crs_rand_articol.scc;
        V_ASCC := NVL(crs_rand_articol.ascc,
                      PACK_FACTURARE.GetAnaliticByGrupUtilizatori(pack_facturare.nid_util,
                                                                  V_SCC));

        CASE
          WHEN pack_facturare.ntip = 7 THEN
            V_EXPLICATIE := SUBSTR(TRIM(crs_rand_articol.explicatie) ||
                            ' INVOICE:' || pack_facturare.cdescriere, 1, 100);
          WHEN pack_facturare.ntip IN (8, 9) THEN
            V_EXPLICATIE := SUBSTR(TRIM(crs_rand_articol.explicatie) ||
                            ' RETUR FACTURA:' || pack_facturare.cdescriere, 1,100);
          ELSE
            V_EXPLICATIE := crs_rand_articol.explicatie;
        END CASE;

        IF pack_facturare.nTip <> pack_facturare.nTipNotaPlata THEN

          V_INCASAT_CALCUL := V_INCASAT_CALCUL +
                              pack_facturare.scrie_nota(detalii_articol.cantitate,
                                                        detalii_articol.pret,
                                                        detalii_articol.discount_unitar,
                                                        detalii_articol.pret_cu_tva,
                                                        detalii_articol.id_valuta,
                                                        crs_rand_articol.in_valuta,
                                                        detalii_articol.curs /
                                                        detalii_articol.multiplicator,
                                                        crs_rand_articol.id_venchelt,
                                                        crs_rand_articol.id_sectie,
                                                        detalii_articol.id_ctr,
                                                        V_EXPLICATIE,
                                                        V_SCD,
                                                        V_ASCD,
                                                        V_SCC,
                                                        V_ASCC,
                                                        0,
                                                        0,
                                                        pack_facturare.nid_set,
                                                        crs_rand_articol.cu_tva,
                                                        detalii_articol.proc_tvav * 100 - 100,
                                                        detalii_articol.id_jtva_coloana,
                                                        detalii_articol.taxcode,
                                                        null);
        END IF;

        V_IN_VALUTA := crs_rand_articol.in_valuta;

        IF pack_facturare.ntip <> 4 THEN
          IF pack_facturare.nscadere_stoc = 1 AND
             detalii_articol.id_gestiune <> -1000 AND
             detalii_articol.in_stoc = 1 THEN
            pack_facturare.descarca_gestiune(detalii_articol.id_articol,
                                             detalii_articol.serie,
                                             detalii_articol.pret_achizitie,
                                             detalii_articol.pretd,
                                             detalii_articol.id_valutad,
                                             detalii_articol.pretv_orig,
                                             detalii_articol.pret,
                                             detalii_articol.diferenta,
                                             detalii_articol.proc_tvav,
                                             detalii_articol.pret_cu_tva,
                                             detalii_articol.cantitate,
                                             detalii_articol.discount_unitar,
                                             detalii_articol.id_gestiune,
                                             crs_rand_articol.id_sectie,
                                             detalii_articol.cont,
                                             crs_rand_articol.id_venchelt,
                                             detalii_articol.id_valuta,
                                             detalii_articol.curs /
                                             detalii_articol.multiplicator,
                                             detalii_articol.id_part_rez,
                                             detalii_articol.id_lucrare_rez,
                                             detalii_articol.id_jtva_coloana,
                                             detalii_articol.taxcode);
          END IF;

          IF detalii_articol.discount_unitar <> 0 AND
             pack_facturare.ndiscount_evidentiat = 1 THEN
            V_INCASAT_CALCUL := V_INCASAT_CALCUL -
                                pack_facturare.scrie_discount(detalii_articol.cantitate,
                                                              detalii_articol.discount_unitar,
                                                              detalii_articol.id_valuta,
                                                              V_IN_VALUTA,
                                                              V_ASCD,
                                                              crs_rand_articol.id_sectie,
                                                              detalii_articol.id_ctr,
                                                              detalii_articol.curs /
                                                              detalii_articol.multiplicator,
                                                              detalii_articol.proc_tvav,
                                                              detalii_articol.pret_cu_tva,
                                                              crs_rand_articol.cu_tva,
                                                              detalii_articol.id_jtva_coloana,
                                                              detalii_articol.taxcode);
          END IF;

        ELSE
          pack_facturare.scrie_fact_aviz_custodie(detalii_articol.id_rata, -- V_COD
                                                  crs_rand_articol.id_sectie,
                                                  detalii_articol.id_articol,
                                                  detalii_articol.id_gestiune,
                                                  detalii_articol.cont,
                                                  detalii_articol.cantitate,
                                                  detalii_articol.pret_cu_tva,
                                                  detalii_articol.pret_achizitie,
                                                  detalii_articol.pretd,
                                                  detalii_articol.id_valutad,
                                                  detalii_articol.pret,
                                                  detalii_articol.discount_unitar,
                                                  detalii_articol.id_valuta,
                                                  detalii_articol.curs /
                                                  detalii_articol.multiplicator,
                                                  detalii_articol.proc_tvav);
        END IF;

        FETCH cursor_articol
          INTO crs_rand_articol;
      END LOOP;
      CLOSE cursor_articol;

    END IF;

    RETURN V_INCASAT_CALCUL;
  END contabilizeaza_articol;
  -------------------------------------------------------------------
  FUNCTION contabilizeaza_rata(detalii_rata VANZARI_DETALII_TEMP%ROWTYPE)
    RETURN NUMBER IS
    V_INCASAT_CALCUL ACT.SUMA%TYPE := 0;

    V_ID_VENCHELT ACT.ID_VENCHELT%TYPE;
    V_ID_SECTIE   ACT.ID_SECTIE%TYPE;
    V_ID_SET      ACT.ID_SET%TYPE;
    V_EXPLICATIE  ACT.EXPLICATIA%TYPE;
    V_SCD         ACT.SCD%TYPE;
    V_ASCD        ACT.ASCD%TYPE;
    V_SCC         ACT.SCC%TYPE;
    V_ASCC        ACT.ASCC%TYPE;
    V_CU_TVA      NOTE_CONTABILE.CU_TVA%TYPE;
    V_IN_VALUTA   NOTE_CONTABILE.IN_VALUTA%TYPE;
  BEGIN
    BEGIN
      SELECT NVL(B.ID_VENCHELT, pack_facturare.nid_venchelt),
             NVL(pack_facturare.nid_sectie_stoc, B.ID_SECTIE) AS ID_SECTIE,
             B.ID_SET,
             C.EXPLICATIE,
             C.SCD,
             C.ASCD,
             C.SCC,
             C.ASCC,
             C.CU_TVA,
             C.IN_VALUTA
        INTO V_ID_VENCHELT,
             V_ID_SECTIE,
             V_ID_SET,
             V_EXPLICATIE,
             V_SCD,
             V_ASCD,
             V_SCC,
             V_ASCC,
             V_CU_TVA,
             V_IN_VALUTA
        FROM CONTRACTE A
        LEFT JOIN CRM_NOTE_VANZARI B
          ON A.ID_NOTA = B.ID_NOTA
        LEFT JOIN NOTE_CONTABILE C
          ON B.ID_SET = C.ID_SET
       WHERE A.STERS = 0
         AND A.ID_CTR = detalii_rata.id_ctr
         AND B.STERS = 0;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        RAISE_APPLICATION_ERROR(-20000,
                                'Nu sunt configurate notele de vanzare pentru acest contract!');
    END;

    IF pack_facturare.ndiscount_evidentiat = 1 AND
       detalii_rata.discount_unitar > 0 THEN
      V_INCASAT_CALCUL := V_INCASAT_CALCUL -
                          pack_facturare.scrie_discount(detalii_rata.cantitate,
                                                        detalii_rata.discount_unitar,
                                                        detalii_rata.id_valuta,
                                                        V_IN_VALUTA,
                                                        V_ASCD,
                                                        V_ID_SECTIE,
                                                        detalii_rata.id_ctr,
                                                        detalii_rata.curs /
                                                        detalii_rata.multiplicator,
                                                        detalii_rata.proc_tvav,
                                                        detalii_rata.pret_cu_tva,
                                                        V_CU_TVA,
                                                        detalii_rata.id_jtva_coloana,
                                                        detalii_rata.taxcode);
    END IF;

    -- scriere nota in act
    V_INCASAT_CALCUL := V_INCASAT_CALCUL +
                        pack_facturare.scrie_nota(detalii_rata.cantitate,
                                                  detalii_rata.pret,
                                                  detalii_rata.discount_unitar,
                                                  detalii_rata.pret_cu_tva,
                                                  detalii_rata.id_valuta,
                                                  V_IN_VALUTA,
                                                  detalii_rata.curs /
                                                  detalii_rata.Multiplicator,
                                                  V_ID_VENCHELT,
                                                  V_ID_SECTIE,
                                                  detalii_rata.id_ctr,
                                                  detalii_rata.explicatia,
                                                  V_SCD,
                                                  V_ASCD,
                                                  V_SCC,
                                                  V_ASCC,
                                                  0, -- V_ID_GESTIN
                                                  0, -- V_ID_GESTOUT
                                                  pack_facturare.nid_set, -- V_ID_SET
                                                  V_CU_TVA,
                                                  detalii_rata.proc_tvav * 100 - 100,
                                                  detalii_rata.id_jtva_coloana,
                                                  detalii_Rata.Taxcode,
                                                  null); --(tab_detalii(i).proc_tvav - 1) * 100

    RETURN V_INCASAT_CALCUL;
  END;
  -------------------------------------------------------------------
  PROCEDURE descarca_gestiune(V_ID_ARTICOL     IN NUMBER,
                              V_SERIE          IN VARCHAR2,
                              V_PRET_ACHIZITIE IN NUMBER,
                              V_PRETD          IN NUMBER,
                              V_ID_VALUTAD     IN NUMBER,
                              V_PRETV_ALES     IN NUMBER,
                              V_PRET_UNITAR    IN NUMBER,
                              V_AJUSTARE       IN NUMBER,
                              V_PROC_TVAV      IN NUMBER,
                              V_PRET_ARE_TVA   IN NUMBER,
                              V_CANTE          IN NUMBER,
                              V_DISCOUNT       IN NUMBER,
                              V_ID_GESTIUNE    IN NUMBER,
                              V_ID_SECTIE      IN NUMBER,
                              V_CONT           IN VARCHAR2,
                              V_ID_VENCHELT    IN NUMBER,
                              V_ID_VALUTA      IN NUMBER,
                              V_CURS           IN NUMBER,
                              V_ID_PART_REZ    IN NUMBER,
                              V_ID_LUCRARE_REZ IN NUMBER) IS
  begin

    PACK_FACTURARE.descarca_gestiune(V_ID_ARTICOL      => V_ID_ARTICOL,
                                     V_SERIE           => V_SERIE,
                                     V_PRET_ACHIZITIE  => V_PRET_ACHIZITIE,
                                     V_PRETD           => V_PRETD,
                                     V_ID_VALUTAD      => V_ID_VALUTAD,
                                     V_PRETV_ALES      => V_PRETV_ALES,
                                     V_PRET_UNITAR     => V_PRET_UNITAR,
                                     V_AJUSTARE        => V_AJUSTARE,
                                     V_PROC_TVAV       => V_PROC_TVAV,
                                     V_PRET_ARE_TVA    => V_PRET_ARE_TVA,
                                     V_CANTE           => V_CANTE,
                                     V_DISCOUNT        => V_DISCOUNT,
                                     V_ID_GESTIUNE     => V_ID_GESTIUNE,
                                     V_ID_SECTIE       => V_ID_SECTIE,
                                     V_CONT            => V_CONT,
                                     V_ID_VENCHELT     => V_ID_VENCHELT,
                                     V_ID_VALUTA       => V_ID_VALUTA,
                                     V_CURS            => V_CURS,
                                     V_ID_PART_REZ     => V_ID_PART_REZ,
                                     V_ID_LUCRARE_REZ  => V_ID_LUCRARE_REZ,
                                     V_ID_JTVA_COLOANA => NULL,
                                     V_TAXCODE         => NULL);
  end;

  PROCEDURE descarca_gestiune(V_ID_ARTICOL      IN NUMBER,
                              V_SERIE           IN VARCHAR2,
                              V_PRET_ACHIZITIE  IN NUMBER,
                              V_PRETD           IN NUMBER,
                              V_ID_VALUTAD      IN NUMBER,
                              V_PRETV_ALES      IN NUMBER,
                              V_PRET_UNITAR     IN NUMBER,
                              V_AJUSTARE        IN NUMBER,
                              V_PROC_TVAV       IN NUMBER,
                              V_PRET_ARE_TVA    IN NUMBER,
                              V_CANTE           IN NUMBER,
                              V_DISCOUNT        IN NUMBER,
                              V_ID_GESTIUNE     IN NUMBER,
                              V_ID_SECTIE       IN NUMBER,
                              V_CONT            IN VARCHAR2,
                              V_ID_VENCHELT     IN NUMBER,
                              V_ID_VALUTA       IN NUMBER,
                              V_CURS            IN NUMBER,
                              V_ID_PART_REZ     IN NUMBER,
                              V_ID_LUCRARE_REZ  IN NUMBER,
                              V_ID_JTVA_COLOANA IN NUMBER,
                              V_TAXCODE         IN NUMBER) IS
    V_PRET       STOC.PRET%TYPE;
    V_TVA        STOC.TVA%TYPE;
    V_PRETV_ORIG STOC.PRETV%TYPE;
    V_ACONT      STOC.ACONT%TYPE;
    V_ACONT_REST STOC.ACONT%TYPE;

    V_ACONT_GEST NOM_GESTIUNI.ACONT%TYPE;

    V_ADAOS           RUL.ADAOS%TYPE;
    V_ADAOS_ORIG      RUL.ADAOS%TYPE;
    V_PRETV           RUL.PRETV%TYPE;
    V_TVAV            RUL.PRETV%TYPE;
    V_PRETVTVA        RUL.PRETVTVA%TYPE;
    V_TVAV_ORIG       RUL.PRETV%TYPE;
    V_PROC_TVAV_ORIG  RUL.PROC_TVAV%TYPE;
    V_DISCOUNT_UNITAR RUL.DISCUNITAR%TYPE;
    V_DISCOUNT_TVA    RUL.DISCOUNT_TVA%TYPE;
    V_DISCOUNT_CU_TVA RUL.DISCUNITAR%TYPE;
    V_PROC_DISCOUNT   RUL.PROC_DISC%TYPE;
    V_PROCENT         RUL.PROCENT%TYPE;
    V_PROCENT_ORIG    RUL.PROCENT%TYPE;

    V_EXPLICATIE     ACT.EXPLICATIA%TYPE;
    V_SUMA           ACT.SUMA%TYPE;
    V_INCASAT_CALCUL ACT.SUMA%TYPE;
    V_CONT2          ACT.SCD%TYPE;
    V_CONT3          ACT.SCD%TYPE;
    V_ACONT2         ACT.ASCD%TYPE;
    V_ANALITICCUST   ACT.ASCD%TYPE;

    V_TIP_GESTIUNE NOM_GESTIUNI.NR_PAG%TYPE;
    tab_stoc       tab_stoc_type;

    V_CANTITATE        STOC.CANTS%TYPE;
    V_CANTITATE_RAMASA STOC.CANTS%TYPE;
    V_DENUMIRE         NOM_ARTICOLE.DENUMIRE%TYPE;

    V_ID_TIP_RULAJ NOM_TIP_RULAJ.ID_TIP_RULAJ%TYPE := 3;
    --V_ZECIMALE_PRETV          NUMBER(1) := pack_sesiune.nzecimale_pretv;
    V_VALOARE_PRET            ACT.SUMA%TYPE;
    V_VALOARE_PRETV           ACT.SUMA%TYPE;
    V_VALOARE_PRETV_ORIG      ACT.SUMA%TYPE;
    V_VALOARE_TVA             ACT.SUMA%TYPE;
    V_VALOARE_TVAV            ACT.SUMA%TYPE;
    V_VALOARE_TVAV_ORIG       ACT.SUMA%TYPE;
    V_VALOARE_ADAOS           ACT.SUMA%TYPE;
    V_VALOARE_ADAOS_ORIG      ACT.SUMA%TYPE;
    V_VALOARE_DIFERENTA_PRETV ACT.SUMA%TYPE;
    V_VALOARE_DIFERENTA_TVAV  ACT.SUMA%TYPE;
    V_VALOARE_PRETVTVA        ACT.SUMA%TYPE;
    V_PROC_TVA                ACT.PROC_TVA%TYPE;
    V_TIP_INREG               RUL.AN%TYPE;
    V_DATAIN                  RUL.DATAIN%TYPE;
    -- diferenta dintre pretul de lista si pretul de vanzare din stoc

    V_DISCOUNT_EVIDENTIAT_AVIZ VANZARI.DISCOUNT_EVIDENTIAT%TYPE;

    lcAcont607          ACT.ASCD%TYPE;
    lcAcont608          ACT.ASCD%TYPE;
    lcAcont378          ACT.ASCD%TYPE;
    lcAcont388          ACT.ASCD%TYPE;
    V_ACONT3            ACT.ASCD%TYPE;
    lnFacturareFaraStoc NUMBER(1) := 0;
    lnDescarcare345FaraAdaos  NUMBER(1) := 0;
    lnInStoc number(10) := 1;

  BEGIN
    lnFacturareFaraStoc := TO_NUMBER(NVL(pack_sesiune.getoptiunefirma('RF_FACTURARE_FARA_STOC'),
                                         '0'));

    lnDescarcare345FaraAdaos := TO_NUMBER(NVL(pack_sesiune.getoptiunefirma('RF_DESCARCARE_345_FARA_ADAOS'),
                                         '0'));

    -- NU SE DESCARCA GESTIUNEA PENTRU ARTICOLELE NEGESTIONABILE
    -- ASTFEL INCAT SA SE POATA FACE OPERATII GEN AVIZ DIN CUSTODIE INCLUSIV CU ARTICOLE NEGESTIONABILE
    SELECT MAX(IN_STOC)
      INTO lnInStoc
      FROM NOM_ARTICOLE
     WHERE ID_ARTICOL = V_ID_ARTICOL;
    if lnInStoc = 0 then
      GOTO SFARSIT;
    end if;
    -- V_PRETV_ORIG si V_PRETV_ALES
    V_EXPLICATIE := NULL;

    pack_facturare.calculeaza_pret(V_PRET_UNITAR,
                                   V_CURS,
                                   V_ID_VALUTA,
                                   V_PROC_TVAV,
                                   V_PRET_ARE_TVA,
                                   2,
                                   V_PRETV,
                                   V_TVAV,
                                   V_PRETVTVA);

    pack_facturare.calculeaza_pret(V_DISCOUNT,
                                   V_CURS,
                                   V_ID_VALUTA,
                                   V_PROC_TVAV,
                                   V_PRET_ARE_TVA,
                                   2,
                                   V_DISCOUNT_UNITAR,
                                   V_DISCOUNT_TVA,
                                   V_DISCOUNT_CU_TVA);

    IF V_PRETVTVA <> 0 THEN
      V_PROC_DISCOUNT := ROUND(V_DISCOUNT_CU_TVA / V_PRETVTVA,
                               pack_facturare.nzecimale_procent) * 100;
    ELSE
      V_PROC_DISCOUNT := 0;
    END IF;

    -- modificare pentru discount evidentiat
    IF pack_facturare.ndiscount_evidentiat = 0 THEN
      V_PRETV           := V_PRETV - V_DISCOUNT_UNITAR;
      V_TVAV            := V_TVAV - V_DISCOUNT_TVA;
      V_PRETVTVA        := V_PRETVTVA - V_DISCOUNT_CU_TVA;
      V_DISCOUNT_UNITAR := 0;
      V_DISCOUNT_TVA    := 0;
      V_DISCOUNT_CU_TVA := 0;
    END IF;

    -- modificare pentru facturile in valuta
    IF V_AJUSTARE <> 0 THEN
      V_PRETV    := V_PRETV + V_AJUSTARE;
      V_TVAV     := V_TVAV + ROUND(V_AJUSTARE * V_PROC_TVAV,
                                   pack_sesiune.nzecimale_pretv);
      V_PRETVTVA := V_PRETV + V_TVAV;
    END IF;
    --

    BEGIN
      SELECT NR_PAG, NVL(ACONT, 'XXXX')
        INTO V_TIP_GESTIUNE, V_ACONT_GEST
        FROM NOM_GESTIUNI
       WHERE ID_GESTIUNE = V_ID_GESTIUNE
         AND STERS = 0;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        RAISE_APPLICATION_ERROR(-20000,
                                'Eroare la selectarea gestiunii! (FACT-007 : ' ||
                                V_ID_GESTIUNE || '.' || V_ID_ARTICOL || ')');
    END;

    CASE
      WHEN pack_facturare.ntip IN (8, 9) THEN
        -- factura de retur
        SELECT 1                as tip,
               a.pret,
               a.pretv,
               a.acont,
               0                as cants,
               a.cante          as cant,
               0                as cante,
               a.tvav,
               a.proc_tvav,
               a.tva,
               a.datain,
               a.id_rul_aux,
               a.pretd,
               a.id_valuta,
               a.lot,
               a.adata_expirare,
               a.serie,
               a.id_lucrare_rez,
               a.id_part_rez,
               b.PRETACHCTVA
          BULK COLLECT
          INTO tab_stoc
          FROM RUL A
          LEFT JOIN NOM_ARTICOLE B
            ON A.ID_ARTICOL = B.ID_ARTICOL
         WHERE A.STERS = 0
           AND NVL(A.ID_SUCURSALA, -99) =
               NVL(pack_facturare.nid_sucursala, -99)
           AND A.ID_ARTICOL = V_ID_ARTICOL
           AND A.ID_GESTIUNE = V_ID_GESTIUNE
           AND NVL(A.CONT, 'XXXX') = V_CONT
           AND NVL(A.SERIE, '+_') = NVL(V_SERIE, '+_')
           AND A.PRETV = V_PRETV_ALES
           AND A.PRET = NVL(V_PRET_ACHIZITIE, A.PRET)
           AND NVL(A.PRETD, 0) = NVL(V_PRETD, NVL(A.PRETD, 0))
           AND NVL(A.ID_VALUTA, 0) =
               NVL(DECODE(V_ID_VALUTAD, -99, 0, V_ID_VALUTAD),
                   NVL(A.ID_VALUTA, 0))
           AND A.CANTE <> 0
           AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(V_ID_LUCRARE_REZ, 0)
           AND NVL(A.ID_PART_REZ, 0) = NVL(V_ID_PART_REZ, 0)
           AND A.ID_TIP_RULAJ = 0
           AND A.COD IN
               (SELECT COD
                  FROM VANZARI
                 WHERE ID_VANZARE IN
                       (SELECT X AS ID_VANZARE
                          FROM table(cast(CHARN2COLLECTION(pack_facturare.clistaid,
                                                           ',') as num_tab))));
      WHEN pack_Facturare.ntip = 24 THEN
        BEGIN
          SELECT DISTINCT DISCOUNT_EVIDENTIAT
            INTO V_DISCOUNT_EVIDENTIAT_AVIZ
            FROM VANZARI
           WHERE COD IN
                 (SELECT X AS COD
                    FROM table(cast(CHARN2COLLECTION(pack_facturare.ccod_retur,
                                                     ',') as num_tab)));
        EXCEPTION
          WHEN TOO_MANY_ROWS THEN
            RAISE_APPLICATION_ERROR(-20000,
                                    'O parte dintre avize sunt cu discount evidentiat,iar altele sunt fara! (FACT-022)');
        END;
        -- aviz de retur
        SELECT 1                as tip,
               A.pret,
               A.pretv,
               A.acont,
               0                as cants,
               A.cante          as cant,
               0                as cante,
               A.tvav,
               A.proc_tvav,
               A.tva,
               A.datain,
               A.id_rul_aux,
               A.pretd,
               A.id_valuta,
               A.lot,
               A.adata_expirare,
               A.serie,
               A.id_lucrare_rez,
               A.id_part_rez,
               B.PRETACHCTVA
          BULK COLLECT
          INTO tab_stoc
          FROM RUL A
          LEFT JOIN NOM_ARTICOLE B
            ON A.ID_ARTICOL = B.ID_ARTICOL
         WHERE A.STERS = 0
           AND A.ID_TIP_RULAJ = 0
           AND NVL(A.ID_SUCURSALA, -99) =
               NVL(pack_facturare.nid_sucursala, -99)
           AND A.ID_ARTICOL = V_ID_ARTICOL
           AND A.ID_GESTIUNE = V_ID_GESTIUNE
           AND NVL(A.CONT, 'XXXX') = V_CONT
           AND NVL(A.SERIE, '+_') = NVL(V_SERIE, '+_')
           AND A.PRETV = DECODE(V_DISCOUNT_EVIDENTIAT_AVIZ,
                                1,
                                V_PRETV,
                                V_PRETV - V_DISCOUNT_UNITAR)
           AND A.PRET = NVL(V_PRET_ACHIZITIE, A.PRET)
           AND NVL(A.PRETD, 0) = NVL(V_PRETD, NVL(A.PRETD, 0))
           AND NVL(A.ID_VALUTA, 0) =
               NVL(DECODE(V_ID_VALUTAD, -99, 0, V_ID_VALUTAD),
                   NVL(A.ID_VALUTA, 0))
           AND A.CANTE <> 0
           AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(V_ID_LUCRARE_REZ, 0)
           AND NVL(A.ID_PART_REZ, 0) = NVL(V_ID_PART_REZ, 0)
           AND A.COD IN
               (SELECT X AS COD
                  FROM table(cast(CHARN2COLLECTION(pack_facturare.ccod_retur,
                                                   ',') as num_tab)));

      WHEN pack_Facturare.ntip = pack_facturare.nTipFacturaHotel THEN
        -- 09.02.2010
        -- la vanzarea din stoc din ROARETAIL - aflu preturile de achizitie/vanzare, seria, gestiunea si trebuie sa intru pe ramura default
        -- DESCARCAREA DE GESTIUNE OFFLINE TREBUIE RESCRISA SA FOLOSEASCA PROCEDURA PACK_RETAIL_STOC.SALVEAZABON
        -- pack_Facturare.ntip = pack_gestiuni_retail.nTipVanzareRetail
        -- BONURI FISCALE DIN ROARETAIL (NU AM PRETUL DE ACHIZITIE, CONTUL) PACK_GESTIUNI_RETAIL.DESCARCARE_RETAIL
        -- 09.02.2010 ^
        SELECT 1 as tip,
               a.pret,
               a.pretv,
               a.acont,
               a.cants,
               a.cant,
               a.cante,
               a.tvav,
               a.proc_tvav,
               a.tva,
               a.datain,
               a.id_rul_aux,
               a.pretd,
               a.id_valuta,
               a.lot,
               a.adata_expirare,
               a.serie,
               a.id_lucrare_rez,
               a.id_part_rez,
               b.PRETACHCTVA
          BULK COLLECT
          INTO tab_stoc
          FROM STOC A
          LEFT JOIN NOM_ARTICOLE B
            ON A.ID_ARTICOL = B.ID_ARTICOL
         WHERE NVL(A.ID_SUCURSALA, -99) =
               NVL(pack_facturare.nid_sucursala, -99)
           AND A.ID_ARTICOL = V_ID_ARTICOL
           AND A.ID_GESTIUNE = V_ID_GESTIUNE
           AND A.LUNA = pack_facturare.nluna
           AND A.AN = pack_facturare.nan
           AND A.CANTS + A.CANT > a.cante
         ORDER BY A.PRET;

      WHEN pack_Facturare.ntip in
           (pack_facturare.nTipFacturaRestaurant,
            pack_facturare.nTipNotaPlata) THEN
        V_ACONT_REST := NULL;

        SELECT *
          BULK COLLECT
          INTO tab_stoc
          FROM (SELECT 1 as tip,
                       a.pret,
                       a.pretv,
                       a.acont,
                       a.cants,
                       a.cant + nvl(b.cant, 0) as cant,
                       a.cante + nvl(b.cante, 0) as cante,
                       a.tvav,
                       a.proc_tvav,
                       a.tva,
                       a.datain,
                       a.id_rul_aux,
                       a.pretd,
                       a.id_valuta,
                       a.lot,
                       a.adata_expirare,
                       a.serie,
                       a.id_lucrare_rez,
                       a.id_part_rez,
                       c.pretachctva
                  FROM STOC A
                  LEFT JOIN (select sum(cant) as cant,
                                   sum(cante) as cante,
                                   id_articol,
                                   id_gestiune,
                                   cont,
                                   acont,
                                   serie,
                                   pret,
                                   pretv,
                                   id_lucrare_rez,
                                   id_part_rez,
                                   proc_tvav,
                                   id_rul_aux,
                                   pretd,
                                   id_valuta,
                                   lot,
                                   adata_expirare
                              from rul_temp
                             where an = 1
                             group by id_articol,
                                      id_gestiune,
                                      cont,
                                      acont,
                                      serie,
                                      pret,
                                      pretv,
                                      id_lucrare_rez,
                                      id_part_rez,
                                      proc_tvav,
                                      id_rul_aux,
                                      pretd,
                                      id_valuta,
                                      lot,
                                      adata_expirare) B
                    ON A.ID_ARTICOL = B.ID_ARTICOL
                   AND A.ID_GESTIUNE = B.ID_GESTIUNE
                   AND NVL(A.CONT, 'XXXX') = NVL(B.CONT, 'XXXX')
                   AND NVL(A.ACONT, 'XXXX') = NVL(B.ACONT, 'XXXX')
                   AND A.PRET = B.PRET
                   AND A.PRETV = B.PRETV
                   AND NVL(A.SERIE, '+_') = NVL(B.SERIE, '+_')
                   AND NVL(A.ID_PART_REZ, 0) = NVL(B.ID_PART_REZ, 0)
                   AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(B.ID_LUCRARE_REZ, 0)
                   AND A.PROC_TVAV = B.PROC_TVAV
                   AND NVL(A.ID_RUL_AUX, 0) = NVL(B.ID_RUL_AUX, 0)
                   AND A.PRETD = B.PRETD
                   AND NVL(A.ID_VALUTA, 0) = NVL(B.ID_VALUTA, 0)
                   AND NVL(A.LOT, '_') = NVL(B.LOT, '_')
                   AND NVL(A.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY')) =
                       NVL(B.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY'))
                  LEFT JOIN NOM_ARTICOLE C
                    ON A.ID_ARTICOL = C.ID_ARTICOL
                 WHERE NVL(A.ID_SUCURSALA, -99) =
                       NVL(pack_facturare.nid_sucursala, -99)
                   AND A.ID_ARTICOL = V_ID_ARTICOL
                   AND A.ID_GESTIUNE = V_ID_GESTIUNE
                   AND A.LUNA = pack_facturare.nluna
                   AND A.AN = pack_facturare.nan
                   AND A.CANTS + A.CANT + nvl(b.cant, 0) >
                       a.cante + nvl(b.cante, 0)
                UNION ALL
                SELECT 2 AS TIP,
                       NVL(V_PRET_ACHIZITIE, 0) as pret,
                       NVL(V_PRETV_ALES, 0) as pretv,
                       V_ACONT_REST as acont,
                       V_CANTE as cants,
                       0 as cant,
                       0 as cante,
                       pack_sesiune.calculeaza_pret_tva(V_PRETV_ALES,
                                                        1,
                                                        pack_Facturare.nid_moneda_nationala,
                                                        V_PROC_TVAV,
                                                        0,
                                                        2) as tvav,
                       V_PROC_TVAV as proc_tvav,
                       pack_sesiune.calculeaza_pret_tva(V_PRET_ACHIZITIE,
                                                        1,
                                                        pack_facturare.nid_moneda_nationala,
                                                        V_PROC_TVAV,
                                                        0,
                                                        1) as tva,
                       NULL as datain,
                       NULL as id_rul_aux,
                       V_PRETD as pretd,
                       V_ID_VALUTAD as id_valuta,
                       NULL as lot,
                       NULL as adata_expirare,
                       V_SERIE as serie,
                       V_ID_LUCRARE_REZ as id_lucrare_rez,
                       V_ID_PART_REZ as id_part_rez,
                       pretachctva
                  FROM NOM_ARTICOLE
                 WHERE ID_ARTICOL = V_ID_ARTICOL)
         ORDER BY TIP, PRET;

      WHEN V_CANTE < 0 and pack_facturare.clistaid is not null and
           instr(pack_facturare.clistaid, ':') > 0 THEN
        -- tip = 1 facturare cu articole retur din rulaj, nu din stoc
        -- am facut ramura separata pt ca la conpress group bon fiscal de la chiosc
        -- statea foarte mult sa parcurga rulajul,
        -- desi nu aveam nici V_CANTE < 0 nici pack_facturare.clista_id
        -- este la fel ca ramura else, dar cu tip = 1 in plus
        -- tip = 2 facturare articole din stoc (cantitate pozitiva sau negativa)
        -- tip = 3 facturare articole fara stoc, din nomenclatorul de articole, RF_FACTURARE_FARA_STOC = 1
        SELECT *
          BULK COLLECT
          INTO tab_stoc
          FROM (SELECT 1                as tip, -- ARTICOLE RETUR, DIN RUL
                       a.pret,
                       a.pretv,
                       a.acont,
                       0                as cants,
                       a.cante          as cant,
                       0                as cante,
                       a.tvav,
                       a.proc_tvav,
                       a.tva,
                       a.datain,
                       a.id_rul_aux,
                       a.pretd,
                       a.id_valuta,
                       a.lot,
                       a.adata_expirare,
                       a.serie,
                       a.id_lucrare_rez,
                       a.id_part_rez,
                       b.PRETACHCTVA
                  FROM RUL A
                  LEFT JOIN NOM_ARTICOLE B
                    ON A.ID_ARTICOL = B.ID_ARTICOL
                 WHERE V_CANTE < 0
                   AND A.STERS = 0
                   AND NVL(A.ID_SUCURSALA, -99) =
                       NVL(pack_facturare.nid_sucursala, -99)
                   AND A.ID_ARTICOL = V_ID_ARTICOL
                   AND A.ID_GESTIUNE = V_ID_GESTIUNE
                   AND NVL(A.CONT, 'XXXX') = V_CONT
                   AND NVL(A.SERIE, '+_') = NVL(V_SERIE, '+_')
                   AND A.PRETV = V_PRETV_ALES
                   AND A.PRET = NVL(V_PRET_ACHIZITIE, A.PRET)
                   AND NVL(A.PRETD, 0) = NVL(V_PRETD, NVL(A.PRETD, 0))
                   AND NVL(A.ID_VALUTA, 0) =
                       NVL(DECODE(V_ID_VALUTAD, -99, 0, V_ID_VALUTAD),
                           NVL(A.ID_VALUTA, 0))
                   AND A.CANTE <> 0
                   AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(V_ID_LUCRARE_REZ, 0)
                   AND NVL(A.ID_PART_REZ, 0) = NVL(V_ID_PART_REZ, 0)
                   AND A.ID_TIP_RULAJ = 0
                   AND A.COD IN
                       (SELECT COD
                          FROM VANZARI
                         WHERE ID_VANZARE IN
                               (select id_vanzare
                                  from (select cast(getwordnum(id_articol_id_vanzare,
                                                               1,
                                                               ':') as
                                                    number(20, 0)) as id_articol,
                                               cast(getwordnum(id_articol_id_vanzare,
                                                               2,
                                                               ':') as
                                                    number(20, 0)) as id_vanzare
                                          from (select x as id_articol_id_vanzare
                                                  FROM table(cast(CHARC2COLLECTION(pack_facturare.clistaid,
                                                                                   ',') as
                                                                  char_tab))))
                                 where id_articol = V_ID_ARTICOL)) -- ID_ARTICOL:ID_VANZARE,ID_ARTICOL:ID_VANZARE
                UNION ALL
                SELECT 2 as tip, -- FARA ARTICOLE RETUR, DIN STOC. ulterior am inclus si articolele vandute normal (nu din rulaj) dar cu cantitate negativa, care exista in stoc
                       a.pret,
                       a.pretv,
                       a.acont,
                       a.cants,
                       a.cant + nvl(b.cant, 0) as cant,
                       a.cante + nvl(b.cante, 0) as cante,
                       a.tvav,
                       a.proc_tvav,
                       a.tva,
                       a.datain,
                       a.id_rul_aux,
                       a.pretd,
                       a.id_valuta,
                       a.lot,
                       a.adata_expirare,
                       a.serie,
                       a.id_lucrare_rez,
                       a.id_part_rez,
                       c.pretachctva
                  FROM STOC A
                  LEFT JOIN (select sum(cant) as cant,
                                   sum(cante) as cante,
                                   id_articol,
                                   id_gestiune,
                                   cont,
                                   acont,
                                   serie,
                                   pret,
                                   pretv,
                                   id_lucrare_rez,
                                   id_part_rez,
                                   proc_tvav,
                                   id_rul_aux,
                                   pretd,
                                   id_valuta,
                                   lot,
                                   adata_expirare
                              from rul_temp
                             where an = 1
                             group by id_articol,
                                      id_gestiune,
                                      cont,
                                      acont,
                                      serie,
                                      pret,
                                      pretv,
                                      id_lucrare_rez,
                                      id_part_rez,
                                      proc_tvav,
                                      id_rul_aux,
                                      pretd,
                                      id_valuta,
                                      lot,
                                      adata_expirare) B
                    ON A.ID_ARTICOL = B.ID_ARTICOL
                   AND A.ID_GESTIUNE = B.ID_GESTIUNE
                   AND NVL(A.CONT, 'XXXX') = NVL(B.CONT, 'XXXX')
                   AND NVL(A.ACONT, 'XXXX') = NVL(B.ACONT, 'XXXX')
                   AND A.PRET = B.PRET
                   AND A.PRETV = B.PRETV
                   AND NVL(A.SERIE, '+_') = NVL(B.SERIE, '+_')
                   AND NVL(A.ID_PART_REZ, 0) = NVL(B.ID_PART_REZ, 0)
                   AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(B.ID_LUCRARE_REZ, 0)
                   AND A.PROC_TVAV = B.PROC_TVAV
                   AND NVL(A.ID_RUL_AUX, 0) = NVL(B.ID_RUL_AUX, 0)
                   AND A.PRETD = B.PRETD
                   AND NVL(A.ID_VALUTA, 0) = NVL(B.ID_VALUTA, 0)
                   AND NVL(A.LOT, '_') = NVL(B.LOT, '_')
                   AND NVL(A.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY')) =
                       NVL(B.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY'))
                  LEFT JOIN NOM_ARTICOLE C
                    ON A.ID_ARTICOL = C.ID_ARTICOL
                 WHERE V_CANTE <> 0
                   AND NVL(A.ID_SUCURSALA, -99) =
                       NVL(pack_facturare.nid_sucursala, -99)
                   AND A.ID_ARTICOL = V_ID_ARTICOL
                   AND A.ID_GESTIUNE = V_ID_GESTIUNE
                   AND NVL(A.CONT, 'XXXX') = V_CONT
                   AND A.PRET = V_PRET_ACHIZITIE
                   AND A.PRETD = V_PRETD
                   AND NVL(A.ID_VALUTA, 0) =
                       DECODE(V_ID_VALUTAD, -99, 0, NVL(V_ID_VALUTAD, 0))
                   AND A.PRETV = V_PRETV_ALES --           modificare v 2.0.19
                   AND NVL(A.SERIE, '+_') = NVL(V_SERIE, '+_')
                   AND A.LUNA = pack_facturare.nluna
                   AND A.AN = pack_facturare.nan
                   AND A.CANTS + A.CANT + nvl(b.cant, 0) >
                       a.cante + nvl(b.cante, 0)
                   AND NVL(A.ID_PART_REZ, 0) = NVL(V_ID_PART_REZ, 0)
                   AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(V_ID_LUCRARE_REZ, 0)
                UNION ALL
                SELECT 3 AS TIP, -- ARTICOLE FARA STOC, DIN NOMENCLATORUL DE ARTICOLE, RF_FACTURARE_FARA_STOC = 1
                       NVL(V_PRET_ACHIZITIE, 0) as pret,
                       NVL(V_PRETV_ALES, 0) as pretv,
                       pack_facturare.getanaliticbyarticol(V_CONT,
                                                           V_ID_GESTIUNE,
                                                           V_ID_ARTICOL) as acont,
                       V_CANTE as cants,
                       0 as cant,
                       0 as cante,
                       pack_sesiune.calculeaza_pret_tva(V_PRETV_ALES,
                                                        1,
                                                        pack_Facturare.nid_moneda_nationala,
                                                        V_PROC_TVAV,
                                                        0,
                                                        2) as tvav,
                       V_PROC_TVAV as proc_tvav,
                       pack_sesiune.calculeaza_pret_tva(V_PRET_ACHIZITIE,
                                                        1,
                                                        pack_facturare.nid_moneda_nationala,
                                                        V_PROC_TVAV,
                                                        0,
                                                        1) as tva,
                       NULL as datain,
                       NULL as id_rul_aux,
                       V_PRETD as pretd,
                       V_ID_VALUTAD as id_valuta,
                       NULL as lot,
                       NULL as adata_expirare,
                       V_SERIE as serie,
                       V_ID_LUCRARE_REZ as id_lucrare_rez,
                       V_ID_PART_REZ as id_part_rez,
                       pretachctva
                  FROM NOM_ARTICOLE
                 WHERE lnFacturareFaraStoc = 1
                   and ID_ARTICOL = V_ID_ARTICOL)
         ORDER BY TIP, PRET;

      ELSE
        SELECT *
          BULK COLLECT
          INTO tab_stoc
          FROM (SELECT 2 as tip, -- FARA ARTICOLE RETUR, DIN STOC. ulterior am inclus si articolele vandute normal (nu din rulaj) dar cu cantitate negativa, care exista in stoc
                       a.pret,
                       a.pretv,
                       a.acont,
                       a.cants,
                       a.cant + nvl(b.cant, 0) as cant,
                       a.cante + nvl(b.cante, 0) as cante,
                       a.tvav,
                       a.proc_tvav,
                       a.tva,
                       a.datain,
                       a.id_rul_aux,
                       a.pretd,
                       a.id_valuta,
                       a.lot,
                       a.adata_expirare,
                       a.serie,
                       a.id_lucrare_rez,
                       a.id_part_rez,
                       c.pretachctva
                  FROM STOC A
                  LEFT JOIN (select sum(cant) as cant,
                                   sum(cante) as cante,
                                   id_articol,
                                   id_gestiune,
                                   cont,
                                   acont,
                                   serie,
                                   pret,
                                   pretv,
                                   id_lucrare_rez,
                                   id_part_rez,
                                   proc_tvav,
                                   id_rul_aux,
                                   pretd,
                                   id_valuta,
                                   lot,
                                   adata_expirare
                              from rul_temp
                             where an = 1
                             group by id_articol,
                                      id_gestiune,
                                      cont,
                                      acont,
                                      serie,
                                      pret,
                                      pretv,
                                      id_lucrare_rez,
                                      id_part_rez,
                                      proc_tvav,
                                      id_rul_aux,
                                      pretd,
                                      id_valuta,
                                      lot,
                                      adata_expirare) B
                    ON A.ID_ARTICOL = B.ID_ARTICOL
                   AND A.ID_GESTIUNE = B.ID_GESTIUNE
                   AND NVL(A.CONT, 'XXXX') = NVL(B.CONT, 'XXXX')
                   AND NVL(A.ACONT, 'XXXX') = NVL(B.ACONT, 'XXXX')
                   AND A.PRET = B.PRET
                   AND A.PRETV = B.PRETV
                   AND NVL(A.SERIE, '+_') = NVL(B.SERIE, '+_')
                   AND NVL(A.ID_PART_REZ, 0) = NVL(B.ID_PART_REZ, 0)
                   AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(B.ID_LUCRARE_REZ, 0)
                   AND A.PROC_TVAV = B.PROC_TVAV
                   AND NVL(A.ID_RUL_AUX, 0) = NVL(B.ID_RUL_AUX, 0)
                   AND A.PRETD = B.PRETD
                   AND NVL(A.ID_VALUTA, 0) = NVL(B.ID_VALUTA, 0)
                   AND NVL(A.LOT, '_') = NVL(B.LOT, '_')
                   AND NVL(A.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY')) =
                       NVL(B.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY'))
                  LEFT JOIN NOM_ARTICOLE C
                    ON A.ID_ARTICOL = C.ID_ARTICOL
                 WHERE V_CANTE <> 0
                   AND NVL(A.ID_SUCURSALA, -99) =
                       NVL(pack_facturare.nid_sucursala, -99)
                   AND A.ID_ARTICOL = V_ID_ARTICOL
                   AND A.ID_GESTIUNE = V_ID_GESTIUNE
                   AND NVL(A.CONT, 'XXXX') = V_CONT
                   AND A.PRET = V_PRET_ACHIZITIE
                   AND A.PRETD = V_PRETD
                   AND NVL(A.ID_VALUTA, 0) =
                       DECODE(V_ID_VALUTAD, -99, 0, NVL(V_ID_VALUTAD, 0))
                   AND A.PRETV = V_PRETV_ALES --           modificare v 2.0.19
                   AND NVL(A.SERIE, '+_') = NVL(V_SERIE, '+_')
                   AND A.LUNA = pack_facturare.nluna
                   AND A.AN = pack_facturare.nan
                   AND A.CANTS + A.CANT + nvl(b.cant, 0) >
                       a.cante + nvl(b.cante, 0)
                   AND NVL(A.ID_PART_REZ, 0) = NVL(V_ID_PART_REZ, 0)
                   AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(V_ID_LUCRARE_REZ, 0)
                UNION ALL
                SELECT 3 AS TIP, -- ARTICOLE FARA STOC, DIN NOMENCLATORUL DE ARTICOLE, RF_FACTURARE_FARA_STOC = 1
                       NVL(V_PRET_ACHIZITIE, 0) as pret,
                       NVL(V_PRETV_ALES, 0) as pretv,
                       pack_facturare.getanaliticbyarticol(V_CONT,
                                                           V_ID_GESTIUNE,
                                                           V_ID_ARTICOL) as acont,
                       V_CANTE as cants,
                       0 as cant,
                       0 as cante,
                       pack_sesiune.calculeaza_pret_tva(V_PRETV_ALES,
                                                        1,
                                                        pack_Facturare.nid_moneda_nationala,
                                                        V_PROC_TVAV,
                                                        0,
                                                        2) as tvav,
                       V_PROC_TVAV as proc_tvav,
                       pack_sesiune.calculeaza_pret_tva(V_PRET_ACHIZITIE,
                                                        1,
                                                        pack_facturare.nid_moneda_nationala,
                                                        V_PROC_TVAV,
                                                        0,
                                                        1) as tva,
                       NULL as datain,
                       NULL as id_rul_aux,
                       V_PRETD as pretd,
                       V_ID_VALUTAD as id_valuta,
                       NULL as lot,
                       NULL as adata_expirare,
                       V_SERIE as serie,
                       V_ID_LUCRARE_REZ as id_lucrare_rez,
                       V_ID_PART_REZ as id_part_rez,
                       pretachctva
                  FROM NOM_ARTICOLE
                 WHERE lnFacturareFaraStoc = 1
                   and ID_ARTICOL = V_ID_ARTICOL)
         ORDER BY TIP, PRET;
        --         AND NVL(A.ID_RUL_AUX, 0) = 0
    END CASE;

    IF SQL%ROWCOUNT = 0 THEN
      BEGIN
        SELECT DENUMIRE
          INTO V_DENUMIRE
          FROM NOM_ARTICOLE
         WHERE ID_ARTICOL = V_ID_ARTICOL;

        RAISE_APPLICATION_ERROR(-20000,
                                'Articolul ' || V_DENUMIRE ||
                                ' nu mai e in stoc! (FACT-008)');
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          RAISE_APPLICATION_ERROR(-20000,
                                  'Unul dintre articole nu mai exista in stoc! (FACT-009)');
      END;
    END IF;

    V_CANTITATE_RAMASA := V_CANTE;

    FOR i IN tab_stoc.first .. tab_stoc.last LOOP

      EXIT WHEN V_CANTITATE_RAMASA = 0;

      V_TIP_INREG  := 1;
      V_PRET       := tab_stoc(i).PRET;
      V_TVA        := tab_stoc(i).TVA;
      V_PRETV_ORIG := tab_stoc(i).PRETV;
      V_ACONT      := tab_stoc(i).ACONT;
      V_DATAIN     := tab_stoc(i).DATAIN;

      -- 29.03.2010
      lcAcont607 := GetAnaliticByArticol('607', V_ID_GESTIUNE, V_ID_ARTICOL);
      lcAcont607 := CASE
                      WHEN lcAcont607 IS NULL THEN
                       REPLACE(V_ACONT, 'X')
                      ELSE
                       lcAcont607
                    END;

      lcAcont608 := GetAnaliticByArticol('608', V_ID_GESTIUNE, V_ID_ARTICOL);
      lcAcont608 := CASE
                      WHEN lcAcont608 IS NULL THEN
                       REPLACE(V_ACONT, 'X')
                      ELSE
                       lcAcont608
                    END;

      lcAcont378 := GetAnaliticByArticol('378', V_ID_GESTIUNE, V_ID_ARTICOL);
      lcAcont378 := CASE
                      WHEN lcAcont378 IS NULL THEN
                       REPLACE(V_ACONT, 'X')
                      ELSE
                       lcAcont378
                    END;

      lcAcont388 := GetAnaliticByArticol('388', V_ID_GESTIUNE, V_ID_ARTICOL);
      lcAcont388 := CASE
                      WHEN lcAcont388 IS NULL THEN
                       REPLACE(V_ACONT, 'X')
                      ELSE
                       lcAcont388
                    END;
      -- 29.03.2010

      V_CANTITATE := tab_stoc(i).CANTS + tab_stoc(i).CANT - tab_stoc(i).CANTE;

      IF V_CANTITATE_RAMASA >= V_CANTITATE THEN
        V_CANTITATE_RAMASA := V_CANTITATE_RAMASA - V_CANTITATE;
      ELSE
        V_CANTITATE        := V_CANTITATE_RAMASA;
        V_CANTITATE_RAMASA := 0;
      END IF;

      V_ADAOS      := V_PRETV - V_PRET;
      V_ADAOS_ORIG := V_PRETV_ORIG - V_PRET;

      IF V_PRET <> 0 THEN
        V_PROCENT      := ROUND(V_ADAOS * 100 / V_PRET,
                                pack_facturare.nzecimale_procent);
        V_PROCENT_ORIG := ROUND(V_ADAOS_ORIG * 100 / V_PRET,
                                pack_facturare.nzecimale_procent);
      ELSE
        V_PROCENT      := 0;
        V_PROCENT_ORIG := 0;
      END IF;

      V_TVAV_ORIG := tab_stoc(i).TVAV;

      -- gestiune marfa la pret de vanzare sau marfa pret de achizitie* (defalcata in stoc si dupa pretul de vanzare)
      IF V_TIP_GESTIUNE in (6, 7) OR tab_stoc(i).TVAV <> 0 THEN
        V_PROC_TVAV_ORIG := tab_stoc(i).PROC_TVAV;
      ELSE
        V_PROC_TVAV_ORIG := pack_sesiune.TVA2PROCTVA(V_TVAV_ORIG,
                                                     V_PRETV_ORIG,
                                                     pack_facturare.nan,
                                                     pack_facturare.nluna);
      END IF;

      V_SUMA           := 0;
      V_INCASAT_CALCUL := 0;

      /*      V_VALOARE_TVAV_ORIG      := pack_facturare.calculeaza_total_tva(V_PRET => CASE V_PRET_ARE_TVA WHEN 0 THEN V_PRETV_ORIG ELSE V_PRETV_ORIG + V_TVAV_ORIG END, V_AJUSTARE => 0, V_CURS => 1, V_DISCOUNT_UNITAR => 0, V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat, V_CANTITATE => V_CANTITATE, V_PRET_CU_TVA => V_PRET_ARE_TVA, V_PROC_TVAV => V_PROC_TVAV, V_ZECIMALE_PRET => V_ZECIMALE_PRET);*/
      V_VALOARE_TVAV_ORIG      := pack_facturare.calculeaza_total_tva(V_PRET                => CASE
                                                                                                V_PRET_ARE_TVA
                                                                                                 WHEN 0 THEN
                                                                                                  V_PRETV_ORIG
                                                                                                 ELSE
                                                                                                  V_PRETV_ORIG +
                                                                                                  V_TVAV_ORIG
                                                                                               END,
                                                                      V_AJUSTARE            => 0,
                                                                      V_CURS                => 1,
                                                                      V_DISCOUNT_UNITAR     => 0,
                                                                      V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                      V_CANTITATE           => V_CANTITATE,
                                                                      V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                      V_PROC_TVAV           => V_PROC_TVAV_ORIG,
                                                                      V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                      V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      V_VALOARE_TVAV           := pack_facturare.calculeaza_total_tva(V_PRET                => CASE
                                                                                                V_PRET_ARE_TVA
                                                                                                 WHEN 0 THEN
                                                                                                  V_PRETV
                                                                                                 ELSE
                                                                                                  V_PRETV +
                                                                                                  V_TVAV
                                                                                               END,
                                                                      V_AJUSTARE            => 0,
                                                                      V_CURS                => 1,
                                                                      V_DISCOUNT_UNITAR     => CASE
                                                                                                V_PRET_ARE_TVA
                                                                                                 WHEN 0 THEN
                                                                                                  V_DISCOUNT_UNITAR
                                                                                                 ELSE
                                                                                                  V_DISCOUNT_CU_TVA
                                                                                               END,
                                                                      V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                      V_CANTITATE           => V_CANTITATE,
                                                                      V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                      V_PROC_TVAV           => V_PROC_TVAV,
                                                                      V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                      V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      V_VALOARE_DIFERENTA_TVAV := V_VALOARE_TVAV - V_VALOARE_TVAV_ORIG;

      /*      V_VALOARE_PRETV_ORIG      := pack_facturare.calculeaza_total_fara_tva(V_PRET => CASE V_PRET_ARE_TVA WHEN 0 THEN V_PRETV_ORIG ELSE V_PRETV_ORIG + V_TVAV_ORIG END, V_AJUSTARE => 0, V_CURS => 1, V_DISCOUNT_UNITAR => 0, V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat, V_CANTITATE => V_CANTITATE, V_PRET_CU_TVA => V_PRET_ARE_TVA, V_PROC_TVAV => V_PROC_TVAV, V_ZECIMALE_PRET => V_ZECIMALE_PRET);*/
      V_VALOARE_PRETV_ORIG      := pack_facturare.calculeaza_total_fara_tva(V_PRET                => CASE
                                                                                                      V_PRET_ARE_TVA
                                                                                                       WHEN 0 THEN
                                                                                                        V_PRETV_ORIG
                                                                                                       ELSE
                                                                                                        V_PRETV_ORIG +
                                                                                                        V_TVAV_ORIG
                                                                                                     END,
                                                                            V_AJUSTARE            => 0,
                                                                            V_CURS                => 1,
                                                                            V_DISCOUNT_UNITAR     => 0,
                                                                            V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                            V_CANTITATE           => V_CANTITATE,
                                                                            V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                            V_PROC_TVAV           => V_PROC_TVAV_ORIG,
                                                                            V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                            V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      V_VALOARE_PRETV           := pack_facturare.calculeaza_total_fara_tva(V_PRET                => CASE
                                                                                                      V_PRET_ARE_TVA
                                                                                                       WHEN 0 THEN
                                                                                                        V_PRETV
                                                                                                       ELSE
                                                                                                        V_PRETV +
                                                                                                        V_TVAV
                                                                                                     END,
                                                                            V_AJUSTARE            => 0,
                                                                            V_CURS                => 1,
                                                                            V_DISCOUNT_UNITAR     => CASE
                                                                                                      V_PRET_ARE_TVA
                                                                                                       WHEN 0 THEN
                                                                                                        V_DISCOUNT_UNITAR
                                                                                                       ELSE
                                                                                                        V_DISCOUNT_CU_TVA
                                                                                                     END,
                                                                            V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                            V_CANTITATE           => V_CANTITATE,
                                                                            V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                            V_PROC_TVAV           => V_PROC_TVAV,
                                                                            V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                            V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      V_VALOARE_DIFERENTA_PRETV := V_VALOARE_PRETV - V_VALOARE_PRETV_ORIG;

      V_PROC_TVA := pack_sesiune.TVA2PROCTVA(V_TVA,
                                             V_PRET,
                                             pack_facturare.nan,
                                             pack_facturare.nluna);

      V_VALOARE_PRET := pack_facturare.calculeaza_total_fara_tva(V_PRET                => (CASE
                                                                                            WHEN tab_stoc(i).pretachctva = 1 THEN
                                                                                             V_PRET + V_TVA
                                                                                            ELSE
                                                                                             V_PRET
                                                                                          END),
                                                                 V_AJUSTARE            => 0,
                                                                 V_CURS                => 1,
                                                                 V_DISCOUNT_UNITAR     => 0,
                                                                 V_DISCOUNT_EVIDENTIAT => 0,
                                                                 V_CANTITATE           => V_CANTITATE,
                                                                 V_PRET_CU_TVA         => tab_stoc(i).pretachctva,
                                                                 V_PROC_TVAV           => V_PROC_TVA,
                                                                 V_ZECIMALE_PRET       => pack_sesiune.nzecimale_preta,
                                                                 V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumaa);
      V_VALOARE_TVA := pack_facturare.calculeaza_total_fara_tva(V_PRET                => (CASE
                                                                                           WHEN tab_stoc(i).pretachctva = 1 THEN
                                                                                            V_PRET + V_TVA
                                                                                           ELSE
                                                                                            V_TVA
                                                                                         END),
                                                                V_AJUSTARE            => 0,
                                                                V_CURS                => 1,
                                                                V_DISCOUNT_UNITAR     => 0,
                                                                V_DISCOUNT_EVIDENTIAT => 0,
                                                                V_CANTITATE           => V_CANTITATE,
                                                                V_PRET_CU_TVA         => tab_stoc(i).pretachctva,
                                                                V_PROC_TVAV           => V_PROC_TVA,
                                                                V_ZECIMALE_PRET       => pack_sesiune.nzecimale_preta,
                                                                V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumaa);
      V_VALOARE_ADAOS      := V_VALOARE_PRETV - V_VALOARE_PRET;
      V_VALOARE_ADAOS_ORIG := V_VALOARE_PRETV_ORIG - V_VALOARE_PRET;
      V_VALOARE_PRETVTVA   := pack_facturare.calculeaza_total_cu_tva(V_PRET                => CASE
                                                                                               V_PRET_ARE_TVA
                                                                                                WHEN 0 THEN
                                                                                                 V_PRETV
                                                                                                ELSE
                                                                                                 V_PRETV +
                                                                                                 V_TVAV
                                                                                              END,
                                                                     V_AJUSTARE            => 0,
                                                                     V_CURS                => 1,
                                                                     V_DISCOUNT_UNITAR     => 0,
                                                                     V_DISCOUNT_EVIDENTIAT => 0,
                                                                     V_CANTITATE           => V_CANTITATE,
                                                                     V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                     V_PROC_TVAV           => V_PROC_TVAV,
                                                                     V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                     V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      CASE
        WHEN V_CONT = '371' AND pack_facturare.ntip = 47 THEN
          V_ANALITICCUST := pack_facturare.getAnaliticK(V_PROC_TVAV);

          -- 357-371.C
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        (CASE
                                                          WHEN V_TIP_GESTIUNE = 6 THEN
                                                           V_VALOARE_PRETVTVA
                                                          ELSE
                                                           V_VALOARE_PRET
                                                        END),
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        NVL(V_ID_SECTIE, 0),
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '357',
                                                        NULL,
                                                        '371',
                                                        V_ANALITICCUST,
                                                        0,
                                                        0,
                                                        1,
                                                        0,
                                                        0,
                                                        NULL);

          -- 371.C-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        NVL(V_ID_SECTIE, 0),
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '371',
                                                        V_ANALITICCUST,
                                                        '371',
                                                        REPLACE(V_ACONT, 'X'),
                                                        0,
                                                        V_ID_GESTIUNE,
                                                        1,
                                                        0,
                                                        0,
                                                        NULL);

          IF V_TIP_GESTIUNE = 6 THEN
            --371.C-378.C
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_ADAOS,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          NVL(V_ID_SECTIE, 0),
                                                          NULL, -- V_ID_CTR,
                                                          V_EXPLICATIE,
                                                          '371',
                                                          V_ANALITICCUST,
                                                          '378',
                                                          V_ANALITICCUST,
                                                          0, -- id_gestin
                                                          0,
                                                          2,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

            --371.C-4428.C
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_TVAV,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          NVL(V_ID_SECTIE, 0),
                                                          NULL, -- V_ID_CTR,
                                                          V_EXPLICATIE,
                                                          '371',
                                                          V_ANALITICCUST,
                                                          '4428',
                                                          V_ANALITICCUST,
                                                          0, -- id_gestin
                                                          0,
                                                          2,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

            --378-371
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_ADAOS,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          NVL(V_ID_SECTIE, 0),
                                                          NULL, -- V_ID_CTR,
                                                          V_EXPLICATIE,
                                                          '378',
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          '371',
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          V_ID_GESTIUNE, -- id_gestin
                                                          V_ID_GESTIUNE,
                                                          2,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

            --4428-371
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_TVAV,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          NVL(V_ID_SECTIE, 0),
                                                          NULL, -- V_ID_CTR,
                                                          V_EXPLICATIE,
                                                          '4428',
                                                          NULL,
                                                          '371',
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          V_ID_GESTIUNE, -- id_gestin
                                                          V_ID_GESTIUNE,
                                                          2,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);
          END IF;

          IF V_TIP_GESTIUNE IN (6, 7) AND
             (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV OR
             V_DISCOUNT_UNITAR <> 0) THEN

            IF V_TIP_GESTIUNE = 6 THEN
              --371-378
              V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                            V_VALOARE_DIFERENTA_PRETV,
                                                            0, -- DISCOUNT_UNITAR
                                                            0, --   PRET CU TVA
                                                            pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                            0, --     V_IN_VALUTA
                                                            0, -- V_CURS,
                                                            0, -- ID_VENCHELT
                                                            NVL(V_ID_SECTIE,
                                                                0),
                                                            NULL, -- V_ID_CTR,
                                                            V_EXPLICATIE,
                                                            '371',
                                                            REPLACE(V_ACONT,
                                                                    'X'),
                                                            '378',
                                                            REPLACE(V_ACONT,
                                                                    'X'),
                                                            V_ID_GESTIUNE, -- id_gestin
                                                            V_ID_GESTIUNE,
                                                            2,
                                                            0,
                                                            (V_PROC_TVAV - 1) * 100,
                                                            NULL);

              --371-4428
              V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                            V_VALOARE_DIFERENTA_TVAV,
                                                            0, -- DISCOUNT_UNITAR
                                                            0, --   PRET CU TVA
                                                            pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                            0, --     V_IN_VALUTA
                                                            0, -- V_CURS,
                                                            0, -- ID_VENCHELT
                                                            NVL(V_ID_SECTIE,
                                                                0),
                                                            NULL, -- V_ID_CTR,
                                                            V_EXPLICATIE,
                                                            '371',
                                                            REPLACE(V_ACONT,
                                                                    'X'),
                                                            '4428',
                                                            NULL,
                                                            V_ID_GESTIUNE, -- id_gestin
                                                            V_ID_GESTIUNE,
                                                            2,
                                                            0,
                                                            (V_PROC_TVAV - 1) * 100,
                                                            NULL);
            END IF;

            IF (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV) THEN
              INSERT INTO rul_temp
                (AN,
                 ID_ARTICOL,
                 SERIE,
                 ID_GESTIUNE,
                 ID_LUCRARE,
                 ID_RESPONSABIL,
                 NRACT,
                 SERIE_ACT,
                 CANT,
                 CANTE,
                 CONT,
                 ACONT,
                 PRETV,
                 TVAV,
                 PRETVTVA,
                 PROC_TVAV,
                 PROC_TVA,
                 PRET,
                 TVA,
                 ADAOS,
                 PROCENT,
                 DATAACT,
                 NNIR,
                 DATAIN,
                 DATAOUT,
                 PRETD,
                 ID_VALUTA,
                 ID_SET,
                 ID_TIP_RULAJ,
                 LOT,
                 ADATA_EXPIRARE,
                 ID_RUL_AUX,
                 ID_LUCRARE_REZ,
                 ID_PART_REZ)
                SELECT decode(rownum, 1, V_TIP_INREG, 0),
                       V_ID_ARTICOL,
                       tab_stoc(i).serie,
                       V_ID_GESTIUNE,
                       NVL(pack_facturare.nid_lucrare, 0),
                       pack_facturare.nid_responsabil,
                       pack_facturare.nnumar_act,
                       pack_facturare.cserie_act,
                       decode(rownum, 2, V_CANTITATE, 0) as CANT,
                       decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                       V_CONT as CONT,
                       REPLACE(V_ACONT, 'X', '') as ACONT,
                       decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                       decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                       decode(rownum,
                              1,
                              tab_stoc(i).pretv + tab_stoc(i).tvav,
                              V_PRETVTVA) as PRETVTVA,
                       decode(rownum, 1, tab_stoc(i).proc_Tvav, V_PROC_TVAV) as PROC_TVAV,
                       V_PROC_TVA as PROC_TVA,
                       V_PRET as PRET,
                       V_TVA AS TVA,
                       decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                       decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                       pack_facturare.ddata_act,
                       pack_facturare.nnumar_bon,
                       decode(rownum,
                              1,
                              tab_stoc(i).datain,
                              pack_facturare.ddata_act) as datain,
                       decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                       tab_stoc(i).pretd,
                       tab_stoc(i).id_valuta,
                       pack_facturare.nid_set,
                       3 as ID_TIP_RULAJ,
                       tab_stoc(i).lot,
                       tab_stoc(i).adata_expirare,
                       tab_stoc(i).id_rul_aux,
                       tab_stoc(i).ID_LUCRARE_REZ,
                       tab_stoc(i).ID_PART_REZ
                  FROM dual
                CONNECT BY level <= 2;

              V_TIP_INREG := V_TIP_INREG - 1;
              V_DATAIN    := pack_facturare.ddata_act;
            END IF;
          END IF;

        WHEN V_CONT = '371' AND pack_facturare.nfactavizcust = 1
        /*             (pack_facturare.ntip = 42 or (pack_facturare.ntip = 24 and pack_facturare.nfactavizcust = 1))*/
         THEN
          -- 357-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        (CASE
                                                          WHEN V_TIP_GESTIUNE = 6 THEN
                                                           V_VALOARE_PRETVTVA
                                                          ELSE
                                                           V_VALOARE_PRET
                                                        END),
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        NVL(V_ID_SECTIE, 0),
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '357',
                                                        NULL,
                                                        '371',
                                                        REPLACE(V_ACONT, 'X', ''),
                                                        0,
                                                        V_ID_GESTIUNE,
                                                        1,
                                                        0,
                                                        0,
                                                        NULL);

          IF V_TIP_GESTIUNE IN (6, 7) AND
             (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV OR
             V_DISCOUNT_UNITAR <> 0) THEN

            IF V_TIP_GESTIUNE = 6 THEN
              --371-378
              V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                            V_VALOARE_DIFERENTA_PRETV,
                                                            0, -- DISCOUNT_UNITAR
                                                            0, --   PRET CU TVA
                                                            pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                            0, --     V_IN_VALUTA
                                                            0, -- V_CURS,
                                                            0, -- ID_VENCHELT
                                                            NVL(V_ID_SECTIE,
                                                                0),
                                                            NULL, -- V_ID_CTR,
                                                            V_EXPLICATIE,
                                                            '371',
                                                            REPLACE(V_ACONT,
                                                                    'X'),
                                                            '378',
                                                            REPLACE(V_ACONT,
                                                                    'X'),
                                                            V_ID_GESTIUNE, -- id_gestin
                                                            V_ID_GESTIUNE,
                                                            2,
                                                            0,
                                                            (V_PROC_TVAV - 1) * 100,
                                                            NULL);

              --371-4428
              V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                            V_VALOARE_DIFERENTA_TVAV,
                                                            0, -- DISCOUNT_UNITAR
                                                            0, --   PRET CU TVA
                                                            pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                            0, --     V_IN_VALUTA
                                                            0, -- V_CURS,
                                                            0, -- ID_VENCHELT
                                                            NVL(V_ID_SECTIE,
                                                                0),
                                                            NULL, -- V_ID_CTR,
                                                            V_EXPLICATIE,
                                                            '371',
                                                            REPLACE(V_ACONT,
                                                                    'X'),
                                                            '4428',
                                                            NULL,
                                                            V_ID_GESTIUNE, -- id_gestin
                                                            V_ID_GESTIUNE,
                                                            2,
                                                            0,
                                                            (V_PROC_TVAV - 1) * 100,
                                                            NULL);
            END IF;

            IF (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV) THEN
              INSERT INTO rul_temp
                (AN,
                 ID_ARTICOL,
                 SERIE,
                 ID_GESTIUNE,
                 ID_LUCRARE,
                 ID_RESPONSABIL,
                 NRACT,
                 SERIE_ACT,
                 CANT,
                 CANTE,
                 CONT,
                 ACONT,
                 PRETV,
                 TVAV,
                 PRETVTVA,
                 PROC_TVAV,
                 PROC_TVA,
                 PRET,
                 TVA,
                 ADAOS,
                 PROCENT,
                 DATAACT,
                 NNIR,
                 DATAIN,
                 DATAOUT,
                 PRETD,
                 ID_VALUTA,
                 ID_SET,
                 ID_TIP_RULAJ,
                 LOT,
                 ADATA_EXPIRARE,
                 ID_RUL_AUX,
                 ID_LUCRARE_REZ,
                 ID_PART_REZ)
                SELECT decode(rownum, 1, V_TIP_INREG, 0),
                       V_ID_ARTICOL,
                       tab_stoc(i).serie,
                       V_ID_GESTIUNE,
                       NVL(pack_facturare.nid_lucrare, 0),
                       pack_facturare.nid_responsabil,
                       pack_facturare.nnumar_act,
                       pack_facturare.cserie_act,
                       decode(rownum, 2, V_CANTITATE, 0) as CANT,
                       decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                       V_CONT as CONT,
                       REPLACE(V_ACONT, 'X', '') as ACONT,
                       decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                       decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                       decode(rownum,
                              1,
                              tab_stoc(i).pretv + tab_stoc(i).tvav,
                              V_PRETVTVA) as PRETVTVA,
                       decode(rownum, 1, tab_stoc(i).proc_Tvav, V_PROC_TVAV) as PROC_TVAV,
                       V_PROC_TVA as PROC_TVA,
                       V_PRET as PRET,
                       V_TVA AS TVA,
                       decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                       decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                       pack_facturare.ddata_act,
                       pack_facturare.nnumar_bon,
                       decode(rownum,
                              1,
                              tab_stoc(i).datain,
                              pack_facturare.ddata_act) as datain,
                       decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                       tab_stoc(i).pretd,
                       tab_stoc(i).id_valuta,
                       pack_facturare.nid_set,
                       3 as ID_TIP_RULAJ,
                       tab_stoc(i).lot,
                       tab_stoc(i).adata_expirare,
                       tab_stoc(i).id_rul_aux,
                       tab_stoc(i).ID_LUCRARE_REZ,
                       tab_stoc(i).ID_PART_REZ
                  FROM dual
                CONNECT BY level <= 2;

              V_TIP_INREG := V_TIP_INREG - 1;
              V_DATAIN    := pack_facturare.ddata_act;
            END IF;
          END IF;

        WHEN V_CONT IN ('371', '357') AND V_TIP_GESTIUNE <> 6 AND
             pack_facturare.nfactavizcust = 0 THEN
          --607-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, -- PRET ARE TVA
                                                        pack_facturare.nid_moneda_nationala, -- V_ID_VALUTA
                                                        0, -- V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        V_ID_VENCHELT,
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR,
                                                        V_EXPLICATIE,
                                                        '607',
                                                        lcAcont607, -- REPLACE(V_ACONT, 'X')
                                                        V_CONT,
                                                        REPLACE(V_ACONT, 'X'),
                                                        0, -- ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        3,
                                                        0,
                                                        0,
                                                        NULL);
          --371-378
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_ADAOS,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET ARE TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- V_ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, --  V_ID_CTR,
                                                        V_EXPLICATIE,
                                                        V_CONT,
                                                        REPLACE(V_ACONT, 'X'),
                                                        '378',
                                                        lcAcont378, -- REPLACE(V_ACONT, 'X')
                                                        V_ID_GESTIUNE, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        3,
                                                        0,
                                                        (V_PROC_TVAV - 1) * 100,
                                                        NULL);

          --378-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_ADAOS,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET ARE TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR,
                                                        V_EXPLICATIE,
                                                        '378',
                                                        lcAcont378, -- REPLACE(V_ACONT, 'X')
                                                        V_CONT,
                                                        REPLACE(V_ACONT, 'X'),
                                                        0, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        3,
                                                        0,
                                                        (V_PROC_TVAV - 1) * 100,
                                                        NULL);

          IF V_CONT = '357' THEN
            -- 371 - 357
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_PRET,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          '371',
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          V_CONT,
                                                          NULL,
                                                          0, -- V_ID_GESTIN
                                                          V_ID_GESTIUNE,
                                                          3,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

          END IF;

          IF V_TIP_GESTIUNE = 7 AND
             (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV) THEN
            INSERT INTO rul_temp
              (AN,
               ID_ARTICOL,
               SERIE,
               ID_GESTIUNE,
               ID_LUCRARE,
               ID_RESPONSABIL,
               NRACT,
               SERIE_ACT,
               CANT,
               CANTE,
               CONT,
               ACONT,
               PRETV,
               TVAV,
               PRETVTVA,
               PROC_TVAV,
               PROC_TVA,
               PRET,
               TVA,
               ADAOS,
               PROCENT,
               DATAACT,
               NNIR,
               DATAIN,
               DATAOUT,
               PRETD,
               ID_VALUTA,
               ID_SET,
               ID_TIP_RULAJ,
               LOT,
               ADATA_EXPIRARE,
               ID_RUL_AUX,
               ID_LUCRARE_REZ,
               ID_PART_REZ)
              SELECT decode(rownum, 1, V_TIP_INREG, 0),
                     V_ID_ARTICOL,
                     tab_stoc(i).serie,
                     V_ID_GESTIUNE,
                     NVL(pack_facturare.nid_lucrare, 0),
                     pack_facturare.nid_responsabil,
                     pack_facturare.nnumar_act,
                     pack_facturare.cserie_act,
                     decode(rownum, 2, V_CANTITATE, 0) as CANT,
                     decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                     V_CONT as CONT,
                     REPLACE(V_ACONT, 'X', '') as ACONT,
                     decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                     decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                     decode(rownum,
                            1,
                            tab_stoc(i).pretv + tab_stoc(i).tvav,
                            V_PRETVTVA) as PRETVTVA,
                     decode(rownum, 1, tab_stoc(i).proc_Tvav, V_PROC_TVAV) as PROC_TVAV,
                     V_PROC_TVA as PROC_TVA,
                     V_PRET as PRET,
                     V_TVA AS TVA,
                     decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                     decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                     pack_facturare.ddata_act,
                     pack_facturare.nnumar_bon,
                     decode(rownum,
                            1,
                            tab_stoc(i).datain,
                            pack_facturare.ddata_act) as datain,
                     decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                     tab_stoc(i).pretd,
                     tab_stoc(i).id_valuta,
                     pack_facturare.nid_set,
                     3 as ID_TIP_RULAJ,
                     tab_stoc(i).lot,
                     tab_stoc(i).adata_expirare,
                     tab_stoc(i).id_rul_aux,
                     tab_stoc(i).ID_LUCRARE_REZ,
                     tab_stoc(i).ID_PART_REZ
                FROM dual
              CONNECT BY level <= 2;

            V_TIP_INREG := V_TIP_INREG - 1;
            V_DATAIN    := pack_facturare.ddata_act;
          END IF;

        WHEN V_CONT IN ('371', '357') AND V_TIP_GESTIUNE = 6 THEN
          --607-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        V_ID_VENCHELT,
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '607',
                                                        lcAcont607, -- REPLACE(V_ACONT, 'X'),
                                                        '371',
                                                        REPLACE(V_ACONT, 'X'),
                                                        0,
                                                        V_ID_GESTIUNE,
                                                        4,
                                                        0,
                                                        0,
                                                        NULL);
          --378-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_ADAOS_ORIG,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '378',
                                                        lcAcont378, -- REPLACE(V_ACONT, 'X'),
                                                        '371',
                                                        REPLACE(V_ACONT, 'X'),
                                                        0, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        4,
                                                        0,
                                                        (V_PROC_TVAV - 1) * 100,
                                                        NULL);
          --4428-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_TVAV_ORIG,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '4428',
                                                        NULL,
                                                        '371',
                                                        REPLACE(V_ACONT, 'X'),
                                                        0, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        4,
                                                        0,
                                                        (V_PROC_TVAV - 1) * 100,
                                                        NULL);

          IF V_CONT = '357' THEN
            -- 371 - 357
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_PRET +
                                                          V_VALOARE_ADAOS_ORIG +
                                                          V_VALOARE_TVAV_ORIG,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          '371',
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          V_CONT,
                                                          NULL,
                                                          0, -- V_ID_GESTIN
                                                          V_ID_GESTIUNE,
                                                          4,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

          END IF;

          IF (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV) THEN

            INSERT INTO RUL_TEMP
              (AN,
               ID_ARTICOL,
               SERIE,
               ID_GESTIUNE,
               ID_LUCRARE,
               ID_RESPONSABIL,
               NRACT,
               SERIE_ACT,
               CANTE,
               CANT,
               CONT,
               ACONT,
               PRETV,
               TVAV,
               PRETVTVA,
               PROC_TVAV,
               PROC_TVA,
               PRET,
               TVA,
               ADAOS,
               PROCENT,
               DATAACT,
               NNIR,
               DATAIN,
               DATAOUT,
               ID_RUL_AUX,
               PRETD,
               ID_VALUTA,
               ID_SET,
               ID_TIP_RULAJ,
               LOT,
               ADATA_EXPIRARE,
               ID_LUCRARE_REZ,
               ID_PART_REZ)
              SELECT decode(rownum, 1, V_TIP_INREG, 0),
                     V_ID_ARTICOL,
                     tab_stoc(i).serie,
                     V_ID_GESTIUNE,
                     nvl(pack_facturare.nid_lucrare, 0),
                     pack_facturare.nid_responsabil,
                     pack_facturare.nnumar_act,
                     pack_facturare.cserie_act,
                     decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                     decode(rownum, 2, V_CANTITATE, 0) as CANT,
                     V_CONT,
                     V_ACONT,
                     decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                     decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                     decode(rownum,
                            1,
                            tab_stoc(i).pretv + tab_stoc(i).tvav,
                            V_PRETVTVA) as PRETVTVA,
                     decode(rownum, 1, tab_stoc(i).proc_Tvav, V_PROC_TVAV) as PROC_TVAV,
                     V_PROC_TVA as PROC_TVA,
                     V_PRET,
                     V_TVA,
                     decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                     decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                     pack_facturare.ddata_act,
                     pack_facturare.nnumar_bon,
                     decode(rownum,
                            1,
                            tab_stoc(i).datain,
                            pack_facturare.ddata_act) as datain,
                     decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                     tab_stoc(i).id_rul_aux,
                     tab_stoc(i).pretd,
                     tab_stoc(i).id_valuta,
                     pack_facturare.nid_set,
                     V_ID_TIP_RULAJ,
                     tab_stoc(i).LOT,
                     tab_stoc(i).ADATA_EXPIRARE,
                     tab_stoc(i).ID_LUCRARE_REZ,
                     tab_stoc(i).ID_PART_REZ
                FROM DUAL
              CONNECT BY level <= 2;
            V_DATAIN    := pack_facturare.ddata_act;
            V_TIP_INREG := V_TIP_INREG - 1;

            --378-371
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_DIFERENTA_PRETV,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          '378',
                                                          lcAcont378, --REPLACE(V_ACONT,'X'),
                                                          V_CONT,
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          V_ID_GESTIUNE, -- V_ID_GESTIN
                                                          V_ID_GESTIUNE,
                                                          5,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

            -- 30.10.2009
            --4428-371
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_DIFERENTA_TVAV,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          NVL(V_ID_SECTIE, 0),
                                                          NULL, -- V_ID_CTR,
                                                          V_EXPLICATIE,
                                                          '4428',
                                                          NULL,
                                                          V_CONT,
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          V_ID_GESTIUNE, -- id_gestin
                                                          V_ID_GESTIUNE,
                                                          5,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);
            -- 30.10.2009 ^

            --371-378
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_DIFERENTA_PRETV,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          V_CONT,
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          '378',
                                                          lcAcont378, --REPLACE(V_ACONT,'X'),
                                                          V_ID_GESTIUNE, -- V_ID_GESTIN
                                                          V_ID_GESTIUNE,
                                                          5,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

            -- 30.10.2009
            --371-4428
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_DIFERENTA_TVAV,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          V_CONT,
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          '4428',
                                                          NULL,
                                                          V_ID_GESTIUNE, -- V_ID_GESTIN
                                                          V_ID_GESTIUNE,
                                                          5,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);
            -- 30.10.2009 ^

          END IF;
        WHEN V_CONT IN ('341', '345', '346', '381') AND
             pack_facturare.nfactavizcust = 0 THEN
          -- 711 - 34X / 608 - 381
          IF V_CONT = '381' THEN
            V_CONT2  := '608';
            V_CONT3  := '388';
            V_SUMA   := V_VALOARE_PRET;
            V_ACONT2 := lcAcont608;
            V_ACONT3 := lcAcont388;
          ELSE
            V_CONT2  := '711';
            V_CONT3  := '348';
            V_SUMA   := V_VALOARE_PRET;
            V_ACONT2 := REPLACE(V_ACONT, 'X');
            V_ACONT3 := REPLACE(V_ACONT, 'X');
          END IF;

          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_SUMA,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        V_ID_VENCHELT,
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        V_CONT2,
                                                        V_ACONT2, -- REPLACE(V_ACONT, 'X'),
                                                        V_CONT,
                                                        REPLACE(V_ACONT, 'X'),
                                                        0, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        6,
                                                        0,
                                                        (V_PROC_TVAV - 1) * 100,
                                                        NULL);

          IF V_VALOARE_ADAOS <> 0 AND lnDescarcare345FaraAdaos = 0 THEN
            -- 34X -348 / 381 - 388
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_ADAOS,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          V_CONT,
                                                          REPLACE(V_ACONT,
                                                                  'X'),
                                                          V_CONT3,
                                                          V_ACONT3, -- REPLACE(V_ACONT,'X'),
                                                          0, -- V_ID_GESTIN
                                                          V_ID_GESTIUNE,
                                                          7,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

            -- 348 - 34x / 388 - 381
/*            IF V_CONT = '381' THEN
              V_CONT2  := V_CONT;
              V_ACONT2 := REPLACE(V_ACONT, 'X');
            ELSE
              V_ACONT2 := NULL;
            END IF;*/
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_ADAOS,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          V_ID_VENCHELT,
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          V_CONT3,
                                                          V_ACONT3, -- REPLACE(V_ACONT,'X'),
                                                          V_CONT,
                                                          REPLACE(V_ACONT, 'X'),
                                                          0, -- V_ID_GESTIN
                                                          V_ID_GESTIUNE,
                                                          7,
                                                          0,
                                                          (V_PROC_TVAV - 1) * 100,
                                                          NULL);

          END IF;

          IF (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV) AND
             V_TIP_GESTIUNE IN (6, 7) THEN
            INSERT INTO rul_temp
              (AN,
               ID_ARTICOL,
               SERIE,
               ID_GESTIUNE,
               ID_LUCRARE,
               ID_RESPONSABIL,
               NRACT,
               SERIE_ACT,
               CANT,
               CANTE,
               CONT,
               ACONT,
               PRETV,
               TVAV,
               PRETVTVA,
               PROC_TVAV,
               PROC_TVA,
               PRET,
               TVA,
               ADAOS,
               PROCENT,
               DATAACT,
               NNIR,
               DATAIN,
               DATAOUT,
               PRETD,
               ID_VALUTA,
               ID_SET,
               ID_TIP_RULAJ,
               LOT,
               ADATA_EXPIRARE,
               ID_RUL_AUX,
               ID_LUCRARE_REZ,
               ID_PART_REZ)
              SELECT decode(rownum, 1, V_TIP_INREG, 0),
                     V_ID_ARTICOL,
                     tab_stoc(i).serie,
                     V_ID_GESTIUNE,
                     NVL(pack_facturare.nid_lucrare, 0),
                     pack_facturare.nid_responsabil,
                     pack_facturare.nnumar_act,
                     pack_facturare.cserie_act,
                     decode(rownum, 2, V_CANTITATE, 0) as CANT,
                     decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                     V_CONT as CONT,
                     REPLACE(V_ACONT, 'X', '') as ACONT,
                     decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                     decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                     decode(rownum,
                            1,
                            tab_stoc(i).pretv + tab_stoc(i).tvav,
                            V_PRETVTVA) as PRETVTVA,
                     decode(rownum, 1, tab_stoc(i).proc_Tvav, V_PROC_TVAV) as PROC_TVAV,
                     V_PROC_TVA as PROC_TVA,
                     V_PRET as PRET,
                     V_TVA AS TVA,
                     decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                     decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                     pack_facturare.ddata_act,
                     pack_facturare.nnumar_bon,
                     decode(rownum,
                            1,
                            tab_stoc(i).datain,
                            pack_facturare.ddata_act) as datain,
                     decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                     tab_stoc(i).pretd,
                     tab_stoc(i).id_valuta,
                     pack_facturare.nid_set,
                     3 as ID_TIP_RULAJ,
                     tab_stoc(i).lot,
                     tab_stoc(i).adata_expirare,
                     tab_stoc(i).id_rul_aux,
                     tab_stoc(i).ID_LUCRARE_REZ,
                     tab_stoc(i).ID_PART_REZ
                FROM dual
              CONNECT BY level <= 2;

            V_TIP_INREG := V_TIP_INREG - 1;
            V_DATAIN    := pack_facturare.ddata_act;
          END IF;

        WHEN V_CONT <> '371' AND pack_facturare.nfactavizcust = 0 THEN
          --371-x
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        V_ID_VENCHELT,
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '371',
                                                        REPLACE(V_ACONT, 'X'),
                                                        V_CONT,
                                                        REPLACE(V_ACONT, 'X'),
                                                        0, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        8,
                                                        0,
                                                        (V_PROC_TVAV - 1) * 100,
                                                        NULL);
          --607-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        V_ID_VENCHELT,
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '607',
                                                        lcAcont607, -- REPLACE(V_ACONT, 'X'),
                                                        '371',
                                                        REPLACE(V_ACONT, 'X'),
                                                        0, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        8,
                                                        0,
                                                        0,
                                                        NULL);
          --371-378
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_ADAOS,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '371',
                                                        REPLACE(V_ACONT, 'X'),
                                                        '378',
                                                        lcAcont378, -- REPLACE(V_ACONT, 'X'),
                                                        0, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        8,
                                                        0,
                                                        (V_PROC_TVAV - 1) * 100,
                                                        NULL);

          --378-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_ADAOS,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '378',
                                                        lcAcont378, -- REPLACE(V_ACONT, 'X'),
                                                        '371',
                                                        REPLACE(V_ACONT, 'X'),
                                                        0, -- V_ID_GESTIN
                                                        V_ID_GESTIUNE,
                                                        8,
                                                        0,
                                                        (V_PROC_TVAV - 1) * 100,
                                                        NULL);

          IF (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV) AND
             V_TIP_GESTIUNE IN (6, 7) THEN
            INSERT INTO rul_temp
              (AN,
               ID_ARTICOL,
               SERIE,
               ID_GESTIUNE,
               ID_LUCRARE,
               ID_RESPONSABIL,
               NRACT,
               SERIE_ACT,
               CANT,
               CANTE,
               CONT,
               ACONT,
               PRETV,
               TVAV,
               PRETVTVA,
               PROC_TVAV,
               PROC_TVA,
               PRET,
               TVA,
               ADAOS,
               PROCENT,
               DATAACT,
               NNIR,
               DATAIN,
               DATAOUT,
               PRETD,
               ID_VALUTA,
               ID_SET,
               ID_TIP_RULAJ,
               LOT,
               ADATA_EXPIRARE,
               ID_RUL_AUX,
               ID_LUCRARE_REZ,
               ID_PART_REZ)
              SELECT decode(rownum, 1, V_TIP_INREG, 0),
                     V_ID_ARTICOL,
                     tab_stoc(i).serie,
                     V_ID_GESTIUNE,
                     NVL(pack_facturare.nid_lucrare, 0),
                     pack_facturare.nid_responsabil,
                     pack_facturare.nnumar_act,
                     pack_facturare.cserie_act,
                     decode(rownum, 2, V_CANTITATE, 0) as CANT,
                     decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                     V_CONT as CONT,
                     REPLACE(V_ACONT, 'X', '') as ACONT,
                     decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                     decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                     decode(rownum,
                            1,
                            tab_stoc(i).pretv + tab_stoc(i).tvav,
                            V_PRETVTVA) as PRETVTVA,
                     decode(rownum, 1, tab_stoc(i).proc_Tvav, V_PROC_TVAV) as PROC_TVAV,
                     V_PROC_TVA as PROC_TVA,
                     V_PRET as PRET,
                     V_TVA AS TVA,
                     decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                     decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                     pack_facturare.ddata_act,
                     pack_facturare.nnumar_bon,
                     decode(rownum,
                            1,
                            tab_stoc(i).datain,
                            pack_facturare.ddata_act) as datain,
                     decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                     tab_stoc(i).pretd,
                     tab_stoc(i).id_valuta,
                     pack_facturare.nid_set,
                     3 as ID_TIP_RULAJ,
                     tab_stoc(i).lot,
                     tab_stoc(i).adata_expirare,
                     tab_stoc(i).id_rul_aux,
                     tab_stoc(i).ID_LUCRARE_REZ,
                     tab_stoc(i).ID_PART_REZ
                FROM dual
              CONNECT BY level <= 2;

            V_TIP_INREG := V_TIP_INREG - 1;
            V_DATAIN    := pack_facturare.ddata_act;
          END IF;

        ELSE
          RAISE_APPLICATION_ERROR(-20000,
                                  'Combinatie invalida! (FACT-010 : ' ||
                                  V_ID_ARTICOL || '.' || V_ID_GESTIUNE || '.' ||
                                  V_CONT || ')');
      END CASE;

      INSERT INTO RUL_TEMP
        (AN,
         ID_ARTICOL,
         SERIE,
         ID_GESTIUNE,
         ID_LUCRARE,
         ID_RESPONSABIL,
         NRACT,
         SERIE_ACT,
         CANTE,
         CONT,
         ACONT,
         PRETV,
         TVAV,
         PRETVTVA,
         PROC_TVAV,
         PROC_TVA,
         PRET,
         TVA,
         DISCUNITAR,
         DISCOUNT_TVA,
         PROC_DISC,
         ADAOS,
         PROCENT,
         DATAACT,
         NNIR,
         DATAIN,
         DATAOUT,
         PRETD,
         ID_VALUTA,
         ID_RUL_AUX,
         ID_SET,
         LOT,
         ADATA_EXPIRARE,
         ID_LUCRARE_REZ,
         ID_PART_REZ,
         ID_JTVA_COLOANA,
         TAXCODE)
      VALUES
        (V_TIP_INREG,
         V_ID_ARTICOL,
         tab_stoc(i).serie,
         V_ID_GESTIUNE,
         nvl(pack_facturare.nid_lucrare, 0),
         pack_facturare.nid_responsabil,
         pack_facturare.nnumar_act,
         pack_facturare.cserie_act,
         V_CANTITATE,
         V_CONT,
         V_ACONT,
         V_PRETV,
         V_TVAV,
         V_PRETVTVA,
         V_PROC_TVAV,
         V_PROC_TVAV,
         V_PRET,
         V_TVA,
         V_DISCOUNT_UNITAR,
         V_DISCOUNT_TVA,
         V_PROC_DISCOUNT,
         V_ADAOS,
         V_PROCENT,
         pack_facturare.ddata_act,
         pack_facturare.nnumar_bon,
         V_DATAIN,
         pack_facturare.ddata_act,
         tab_stoc(i).pretd,
         tab_stoc(i).id_valuta,
         tab_stoc(i).id_rul_aux,
         pack_facturare.nid_set,
         tab_stoc(i).lot,
         tab_stoc(i).adata_expirare,
         tab_stoc(i).ID_LUCRARE_REZ,
         tab_stoc(i).ID_PART_REZ,
         V_ID_JTVA_COLOANA,
         V_TAXCODE);

    END LOOP;

    <<SFARSIT>>
    NULL;
  END descarca_gestiune;
  -------------------------------------------------------------------
  PROCEDURE scrie_fact_aviz_custodie(V_COD             IN NUMBER,
                                     V_ID_SECTIE       IN NUMBER,
                                     V_ID_ARTICOL      IN NUMBER,
                                     V_ID_GESTIUNE     IN NUMBER,
                                     V_CONT            IN VARCHAR2,
                                     V_CANTITATE       IN NUMBER,
                                     V_PRET_ARE_TVA    IN NUMBER,
                                     V_PRET_ACHIZITIE  IN NUMBER,
                                     V_PRETD           IN NUMBER,
                                     V_ID_VALUTAD      IN NUMBER,
                                     V_PRETV           IN NUMBER,
                                     V_DISCOUNT_UNITAR IN NUMBER,
                                     V_ID_VALUTA       IN NUMBER,
                                     V_CURS            IN NUMBER,
                                     V_PROC_TVAV       IN NUMBER) IS
    V_VALOARE_TVAV     ACT.SUMA%TYPE;
    V_VALOARE_PRETV    ACT.SUMA%TYPE;
    V_VALOARE_PRET     ACT.SUMA%TYPE;
    V_VALOARE_ADAOS    ACT.SUMA%TYPE;
    V_VALOARE_PRETVTVA ACT.SUMA%TYPE;
    V_PRETACHCTVA      NOM_ARTICOLE.PRETACHCTVA%TYPE;

    V_INCASAT_CALCUL ACT.SUMA%TYPE := 0;

    V_PROC_TVA  RUL.PROC_TVA%TYPE;
    V_ACONT     RUL.ACONT%TYPE;
    V_TVA       RUL.TVA%TYPE;
    V_PRETV_RON RUL.PRETV%TYPE;

    V_DISCOUNT_EVIDENTIAT_AVIZ NUMBER(1);
  BEGIN
    SELECT DISCOUNT_EVIDENTIAT
      INTO V_DISCOUNT_EVIDENTIAT_AVIZ
      FROM VANZARI
     WHERE COD = V_COD;

    IF V_PRET_ARE_TVA = 1 THEN
      IF V_DISCOUNT_EVIDENTIAT_AVIZ = 1 THEN
        V_PRETV_RON := pack_sesiune.calculeaza_pret_cu_tva(V_PRETV,
                                                           V_CURS,
                                                           V_ID_VALUTA,
                                                           V_PROC_TVA,
                                                           V_PRET_ARE_TVA,
                                                           2);
      ELSE
        V_PRETV_RON := pack_sesiune.calculeaza_pret_cu_tva(V_PRETV -
                                                           V_DISCOUNT_UNITAR,
                                                           V_CURS,
                                                           V_ID_VALUTA,
                                                           V_PROC_TVA,
                                                           V_PRET_ARE_TVA,
                                                           2);
      END IF;
    ELSE
      IF V_DISCOUNT_EVIDENTIAT_AVIZ = 1 THEN
        V_PRETV_RON := pack_sesiune.calculeaza_pret_fara_tva(V_PRETV,
                                                             V_CURS,
                                                             V_ID_VALUTA,
                                                             V_PROC_TVA,
                                                             V_PRET_ARE_TVA,
                                                             2);
      ELSE
        V_PRETV_RON := pack_sesiune.calculeaza_pret_fara_tva(V_PRETV -
                                                             V_DISCOUNT_UNITAR,
                                                             V_CURS,
                                                             V_ID_VALUTA,
                                                             V_PROC_TVA,
                                                             V_PRET_ARE_TVA,
                                                             2);
      END IF;
    END IF;

    SELECT A.PROC_TVA, A.TVA, A.ACONT, B.PRETACHCTVA
      INTO V_PROC_TVA, V_TVA, V_ACONT, V_PRETACHCTVA
      FROM RUL A
      LEFT JOIN NOM_ARTICOLE B
        ON A.ID_ARTICOL = B.ID_ARTICOL
     WHERE A.COD = V_COD
       AND A.STERS = 0
       AND A.ID_ARTICOL = V_ID_ARTICOL
       AND A.PRET = V_PRET_ACHIZITIE
       AND A.PRETD = V_PRETD
       AND NVL(A.ID_VALUTA, 0) = NVL(V_ID_VALUTAD, 0)
       AND DECODE(V_PRET_ARE_TVA, 1, A.PRETV + A.TVAV, A.PRETV) =
           V_PRETV_RON
       AND A.ID_GESTIUNE = V_ID_GESTIUNE
       AND A.ID_TIP_RULAJ = 0
       AND ROWNUM = 1;

    V_VALOARE_TVAV := pack_facturare.calculeaza_total_tva(V_PRET                => V_PRETV,
                                                          V_AJUSTARE            => 0,
                                                          V_CURS                => V_CURS,
                                                          V_DISCOUNT_UNITAR     => V_DISCOUNT_UNITAR,
                                                          V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                          V_CANTITATE           => V_CANTITATE,
                                                          V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                          V_PROC_TVAV           => V_PROC_TVAV,
                                                          V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                          V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);

    V_VALOARE_PRETV := pack_facturare.calculeaza_total_fara_tva(V_PRET                => V_PRETV,
                                                                V_AJUSTARE            => 0,
                                                                V_CURS                => V_CURS,
                                                                V_DISCOUNT_UNITAR     => V_DISCOUNT_UNITAR,
                                                                V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                V_CANTITATE           => V_CANTITATE,
                                                                V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                V_PROC_TVAV           => V_PROC_TVAV,
                                                                V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);

    V_VALOARE_PRET := pack_facturare.calculeaza_total_fara_tva(V_PRET                => V_PRET_ACHIZITIE +
                                                                                        (CASE
                                                                                          WHEN V_PRETACHCTVA = 1 THEN
                                                                                           V_TVA
                                                                                          ELSE
                                                                                           0
                                                                                        END),
                                                               V_AJUSTARE            => 0,
                                                               V_CURS                => 1,
                                                               V_DISCOUNT_UNITAR     => 0,
                                                               V_DISCOUNT_EVIDENTIAT => 0,
                                                               V_CANTITATE           => V_CANTITATE,
                                                               V_PRET_CU_TVA         => V_PRETACHCTVA,
                                                               V_PROC_TVAV           => V_PROC_TVA,
                                                               V_ZECIMALE_PRET       => pack_sesiune.nzecimale_preta,
                                                               V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumaa);
    V_VALOARE_ADAOS := V_VALOARE_PRETV - V_VALOARE_PRET;

    V_VALOARE_PRETVTVA := pack_facturare.calculeaza_total_cu_tva(V_PRET                => V_PRETV,
                                                                 V_AJUSTARE            => 0,
                                                                 V_CURS                => V_CURS,
                                                                 V_DISCOUNT_UNITAR     => V_DISCOUNT_UNITAR,
                                                                 V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                 V_CANTITATE           => V_CANTITATE,
                                                                 V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                 V_PROC_TVAV           => V_PROC_TVAV,
                                                                 V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                 V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);

    IF V_CONT = '371' THEN
      -- 371 - 357
      V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                    V_VALOARE_PRETVTVA,
                                                    0, -- DISCOUNT_UNITAR
                                                    0, --   PRET CU TVA
                                                    pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                    0, --     V_IN_VALUTA
                                                    0, -- V_CURS,
                                                    0, -- ID_VENCHELT
                                                    NVL(V_ID_SECTIE, 0),
                                                    0, -- V_ID_CTR
                                                    NULL,
                                                    '371',
                                                    REPLACE(V_ACONT, 'X'),
                                                    '357',
                                                    NULL,
                                                    0,
                                                    V_ID_GESTIUNE,
                                                    1,
                                                    0,
                                                    0,
                                                    NULL);

      --607-371
      V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                    V_VALOARE_PRET,
                                                    0, -- DISCOUNT_UNITAR
                                                    0, --   PRET CU TVA
                                                    pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                    0, --     V_IN_VALUTA
                                                    0, -- V_CURS,
                                                    0, -- ID_VENCHELT
                                                    NVL(V_ID_SECTIE, 0),
                                                    NULL, -- V_ID_CTR
                                                    NULL,
                                                    '607',
                                                    REPLACE(V_ACONT, 'X', ''),
                                                    '371',
                                                    REPLACE(V_ACONT, 'X', ''),
                                                    0,
                                                    V_ID_GESTIUNE,
                                                    1,
                                                    0,
                                                    0,
                                                    NULL);

      --378-371
      V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                    V_VALOARE_ADAOS,
                                                    0, -- DISCOUNT_UNITAR
                                                    0, --   PRET CU TVA
                                                    pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                    0, --     V_IN_VALUTA
                                                    0, -- V_CURS,
                                                    0, -- ID_VENCHELT
                                                    NVL(V_ID_SECTIE, 0),
                                                    NULL, -- V_ID_CTR
                                                    NULL,
                                                    '378',
                                                    REPLACE(V_ACONT, 'X'),
                                                    '371',
                                                    REPLACE(V_ACONT, 'X'),
                                                    V_ID_GESTIUNE,
                                                    V_ID_GESTIUNE,
                                                    1,
                                                    0,
                                                    0,
                                                    NULL);
      --4428-371
      V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                    V_VALOARE_TVAV,
                                                    0, -- DISCOUNT_UNITAR
                                                    0, --   PRET CU TVA
                                                    pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                    0, --     V_IN_VALUTA
                                                    0, -- V_CURS,
                                                    0, -- ID_VENCHELT
                                                    NVL(V_ID_SECTIE, 0),
                                                    NULL, -- V_ID_CTR
                                                    NULL,
                                                    '4428',
                                                    NULL,
                                                    '371',
                                                    REPLACE(V_ACONT, 'X'),
                                                    V_ID_GESTIUNE,
                                                    V_ID_GESTIUNE,
                                                    1,
                                                    0,
                                                    0,
                                                    NULL);
    ELSE
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu exista configurari pentru contul ' ||
                              V_CONT || ' ! (FACT-011)');
    END IF;
  END scrie_fact_aviz_custodie;
  -------------------------------------------------------------------
  PROCEDURE transfera_articol(detalii_articol VANZARI_DETALII_TEMP%ROWTYPE) IS
    V_PRET       STOC.PRET%TYPE;
    V_PRETV_ORIG STOC.PRETV%TYPE;
    V_ACONT      STOC.ACONT%TYPE;

    V_ACONT_GEST NOM_GESTIUNI.ACONT%TYPE;

    V_ID_SECTIE       RUL.ID_SECTIE%TYPE;
    V_ADAOS           RUL.ADAOS%TYPE;
    V_ADAOS_ORIG      RUL.ADAOS%TYPE;
    V_PRETV           RUL.PRETV%TYPE;
    V_TVAV            RUL.PRETV%TYPE;
    V_PRETVTVA        RUL.PRETVTVA%TYPE;
    V_TVAV_ORIG       RUL.PRETV%TYPE;
    V_DISCOUNT_CU_TVA RUL.DISCUNITAR%TYPE;
    V_DISCOUNT_UNITAR RUL.DISCUNITAR%TYPE;
    V_DISCOUNT_TVA    RUL.DISCOUNT_TVA%TYPE;
    --    V_PROC_DISCOUNT   RUL.PROC_DISC%TYPE;
    V_PROCENT      RUL.PROCENT%TYPE;
    V_PROCENT_ORIG RUL.PROCENT%TYPE;
    V_DATAIN       RUL.DATAIN%TYPE;

    V_CONT2  ACT.SCD%TYPE;
    V_CONT3  ACT.SCD%TYPE;
    V_ACONT2 ACT.ASCC%TYPE;

    V_EXPLICATIE     ACT.EXPLICATIA%TYPE;
    V_SUMA           ACT.SUMA%TYPE;
    V_INCASAT_CALCUL ACT.SUMA%TYPE;

    V_TIP_GESTIUNE NOM_GESTIUNI.NR_PAG%TYPE;
    tab_stoc       tab_stoc_type;

    V_CANTITATE        STOC.CANTS%TYPE;
    V_CANTITATE_RAMASA STOC.CANTS%TYPE;
    V_DENUMIRE         NOM_ARTICOLE.DENUMIRE%TYPE;

    V_VALOARE_PRET            ACT.SUMA%TYPE;
    V_VALOARE_PRETV           ACT.SUMA%TYPE;
    V_VALOARE_PRETV_ORIG      ACT.SUMA%TYPE;
    V_VALOARE_PRETVTVA        ACT.SUMA%TYPE;
    V_VALOARE_TVAV            ACT.SUMA%TYPE;
    V_VALOARE_TVAV_ORIG       ACT.SUMA%TYPE;
    V_VALOARE_ADAOS           ACT.SUMA%TYPE;
    V_VALOARE_ADAOS_ORIG      ACT.SUMA%TYPE;
    V_VALOARE_DIFERENTA_PRETV ACT.SUMA%TYPE;
    V_VALOARE_DIFERENTA_TVAV  ACT.SUMA%TYPE;

    V_PRET_ARE_TVA NUMBER(1) := 0;
    V_PROC_TVAV    RUL.PROC_TVAV%TYPE;
    V_PROC_TVA     ACT.PROC_TVA%TYPE;
    V_TVA          ACT.SUMA%TYPE;
    --   V_ZECIMALE_PRET NUMBER(1) := pack_facturare.nzecimale_calcul_ach;

    V_TIP_INREG RUL.AN%TYPE;
  BEGIN
    IF pack_facturare.nid_sectie_stoc IS NULL THEN
      BEGIN
        SELECT B.ID_SECTIE
          INTO V_ID_SECTIE
          FROM CRM_POLITICI_PRETURI A
          LEFT JOIN CRM_NOTE_VANZARI B
            ON A.ID_NOTA = B.ID_NOTA
         WHERE A.ID_POL = detalii_articol.id_pol;
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          V_ID_SECTIE := 0;
      END;
    ELSE
      V_ID_SECTIE := pack_facturare.nid_sectie_stoc;
    END IF;

    V_EXPLICATIE := NULL;

    pack_facturare.calculeaza_pret(detalii_articol.pret,
                                   detalii_articol.curs /
                                   detalii_articol.multiplicator,
                                   detalii_articol.id_valuta,
                                   detalii_articol.proc_tvav,
                                   detalii_articol.pret_cu_tva,
                                   2,
                                   V_PRETV,
                                   V_TVAV,
                                   V_PRETVTVA);

    pack_facturare.calculeaza_pret(detalii_articol.discount_unitar,
                                   detalii_articol.curs /
                                   detalii_articol.multiplicator,
                                   detalii_articol.id_valuta,
                                   detalii_articol.proc_tvav,
                                   detalii_articol.pret_cu_tva,
                                   2,
                                   V_DISCOUNT_UNITAR,
                                   V_DISCOUNT_TVA,
                                   V_DISCOUNT_CU_TVA);

    -- modificare pentru discount evidentiat
    IF pack_facturare.ndiscount_evidentiat = 0 THEN
      V_PRETV           := V_PRETV - V_DISCOUNT_UNITAR;
      V_TVAV            := V_TVAV - V_DISCOUNT_TVA;
      V_PRETVTVA        := V_PRETVTVA - V_DISCOUNT_CU_TVA;
      V_DISCOUNT_UNITAR := 0;
      V_DISCOUNT_TVA    := 0;
      V_DISCOUNT_CU_TVA := 0;
    END IF;

    /*    IF V_PRETVTVA <> 0 THEN
      V_PROC_DISCOUNT := ROUND(V_DISCOUNT_CU_TVA / V_PRETVTVA,
                               pack_facturare.nzecimale_procent) * 100;
    ELSE
      V_PROC_DISCOUNT := 0;
    END IF;*/

    BEGIN
      SELECT NR_PAG, NVL(ACONT, 'XXXX')
        INTO V_TIP_GESTIUNE, V_ACONT_GEST
        FROM NOM_GESTIUNI
       WHERE ID_GESTIUNE = detalii_articol.id_gestiune
         AND STERS = 0;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        RAISE_APPLICATION_ERROR(-20000,
                                'Eroare la selectarea gestiunii! (FACT-019 : ' ||
                                detalii_articol.id_gestiune || ')');
    END;

    CASE
      WHEN pack_facturare.ntip = 27 THEN
        -- (25026,27) - transfer catre subunitati pe baza de lucrare( 25036 - daca e cu scadere din gestiune )
        SELECT 1 as tip,
               a.pret,
               a.pretv,
               a.acont,
               a.cants,
               a.cant + nvl(b.cant, 0) as cant,
               a.cante + nvl(b.cante, 0) as cante,
               a.tvav,
               a.proc_tvav,
               a.tva,
               a.datain,
               a.id_rul_aux,
               a.pretd,
               a.id_valuta,
               a.lot,
               a.adata_expirare,
               a.serie,
               a.id_lucrare_rez,
               a.id_part_rez,
               c.pretachctva
          BULK COLLECT
          INTO tab_stoc
          FROM STOC a
          LEFT JOIN (select sum(cant) as cant,
                            sum(cante) as cante,
                            id_articol,
                            id_gestiune,
                            cont,
                            acont,
                            serie,
                            pret,
                            pretv,
                            id_lucrare_rez,
                            id_part_rez,
                            proc_tvav,
                            id_rul_aux,
                            pretd,
                            id_valuta,
                            lot,
                            adata_expirare
                       from rul_temp
                      where an = 1
                      group by id_articol,
                               id_gestiune,
                               cont,
                               acont,
                               serie,
                               pret,
                               pretv,
                               id_lucrare_rez,
                               id_part_rez,
                               proc_tvav,
                               id_rul_aux,
                               pretd,
                               id_valuta,
                               lot,
                               adata_expirare) B
            ON A.ID_ARTICOL = B.ID_ARTICOL
           AND A.ID_GESTIUNE = B.ID_GESTIUNE
           AND NVL(A.CONT, 'XXXX') = NVL(B.CONT, 'XXXX')
           AND NVL(A.ACONT, 'XXXX') = NVL(B.ACONT, 'XXXX')
           AND A.PRET = B.PRET
           AND A.PRETV = B.PRETV
           AND NVL(A.SERIE, '+_') = NVL(B.SERIE, '+_')
           AND NVL(A.ID_PART_REZ, 0) = NVL(B.ID_PART_REZ, 0)
           AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(B.ID_LUCRARE_REZ, 0)
           AND A.PROC_TVAV = B.PROC_TVAV
           AND NVL(A.ID_RUL_AUX, 0) = NVL(B.ID_RUL_AUX, 0)
           AND NVL(A.PRETD, 0) = NVL(B.PRETD, 0)
           AND NVL(A.ID_VALUTA, 0) = NVL(B.ID_VALUTA, 0)
           AND NVL(A.LOT, '_') = NVL(B.LOT, '_')
           AND NVL(A.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY')) =
               NVL(B.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY'))
          LEFT JOIN NOM_ARTICOLE C
            ON A.ID_ARTICOL = C.ID_ARTICOL
         WHERE NVL(A.ID_SUCURSALA, -99) =
               NVL(pack_facturare.nid_sucursala, -99)
           AND A.ID_ARTICOL = detalii_articol.id_articol
           AND A.ID_GESTIUNE = detalii_articol.id_gestiune
           AND NVL(A.CONT, 'XXXX') = detalii_articol.cont
           AND A.LUNA = pack_facturare.nluna
           AND A.AN = pack_facturare.nan
           AND A.CANTS + A.CANT + NVL(B.CANT, 0) >
               A.CANTE + NVL(B.CANTE, 0)
           AND NVL(A.ID_LUCRARE_REZ, 0) =
               NVL(detalii_articol.id_lucrare_rez, 0)
           AND NVL(A.ID_PART_REZ, 0) = NVL(detalii_articol.id_part_rez, 0)
         ORDER BY A.PRET;
      WHEN pack_facturare.ntip = 30 THEN
        -- (25029,30) - transfer catre subunitati pe baza de NIR  ( 25039 - daca e cu scadere din gestiune )
        SELECT 1 as tip,
               a.pret,
               a.pretv,
               a.acont,
               a.cants,
               a.cant + nvl(b.cant, 0) as cant,
               a.cante + nvl(b.cante, 0) as cante,
               a.tvav,
               a.proc_tvav,
               a.tva,
               a.datain,
               a.id_rul_aux,
               a.pretd,
               a.id_valuta,
               a.lot,
               a.adata_expirare,
               a.serie,
               a.id_lucrare_rez,
               a.id_part_rez,
               c.pretachctva
          BULK COLLECT
          INTO tab_stoc
          FROM STOC A
          LEFT JOIN (select sum(cant) as cant,
                            sum(cante) as cante,
                            id_articol,
                            id_gestiune,
                            cont,
                            acont,
                            serie,
                            pret,
                            pretv,
                            id_lucrare_rez,
                            id_part_rez,
                            proc_tvav,
                            id_rul_aux,
                            pretd,
                            id_valuta,
                            lot,
                            adata_expirare
                       from rul_temp
                      where an = 1
                      group by id_articol,
                               id_gestiune,
                               cont,
                               acont,
                               serie,
                               pret,
                               pretv,
                               id_lucrare_rez,
                               id_part_rez,
                               proc_tvav,
                               id_rul_aux,
                               pretd,
                               id_valuta,
                               lot,
                               adata_expirare) B
            ON A.ID_ARTICOL = B.ID_ARTICOL
           AND A.ID_GESTIUNE = B.ID_GESTIUNE
           AND NVL(A.CONT, 'XXXX') = NVL(B.CONT, 'XXXX')
           AND NVL(A.ACONT, 'XXXX') = NVL(B.ACONT, 'XXXX')
           AND A.PRET = B.PRET
           AND A.PRETV = B.PRETV
           AND NVL(A.SERIE, '+_') = NVL(B.SERIE, '+_')
           AND NVL(A.ID_PART_REZ, 0) = NVL(B.ID_PART_REZ, 0)
           AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(B.ID_LUCRARE_REZ, 0)
           AND A.PROC_TVAV = B.PROC_TVAV
           AND NVL(A.ID_RUL_AUX, 0) = NVL(B.ID_RUL_AUX, 0)
           AND A.PRETD = B.PRETD
           AND NVL(A.ID_VALUTA, 0) = NVL(B.ID_VALUTA, 0)
           AND NVL(A.LOT, '_') = NVL(B.LOT, '_')
           AND NVL(A.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY')) =
               NVL(B.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY'))
          LEFT JOIN NOM_ARTICOLE C
            ON A.ID_ARTICOL = C.ID_ARTICOL
         WHERE NVL(A.ID_SUCURSALA, -99) =
               NVL(pack_facturare.nid_sucursala, -99)
           AND A.ID_ARTICOL = detalii_articol.id_articol
           AND NVL(A.SERIE, '+_') = NVL(detalii_articol.serie, '+_')
           AND A.ID_GESTIUNE = detalii_articol.id_gestiune
           AND A.PRETV = V_PRETV
           AND A.PRETD = detalii_articol.pretd
           AND NVL(A.ID_VALUTA, 0) =
               decode(detalii_articol.id_valutad,
                      -99,
                      0,
                      NVL(detalii_articol.id_valutad, 0))
           AND A.PRET = detalii_articol.pret_achizitie
           AND NVL(A.CONT, 'XXXX') = detalii_articol.cont
           AND A.LUNA = pack_facturare.nluna
           AND A.AN = pack_facturare.nan
           AND A.CANTS + A.CANT + NVL(B.CANT, 0) >
               A.CANTE + NVL(B.CANTE, 0)
           AND NVL(A.ID_LUCRARE_REZ, 0) =
               NVL(detalii_articol.id_lucrare_rez, 0)
           AND NVL(A.ID_PART_REZ, 0) = NVL(detalii_articol.id_part_rez, 0)
        /*       AND NVL(ID_RUL_AUX, 0) = 0*/
         ORDER BY A.PRET;
      ELSE
        SELECT 1 as tip,
               a.pret,
               a.pretv,
               a.acont,
               a.cants,
               a.cant + nvl(b.cant, 0) as cant,
               a.cante + nvl(b.cante, 0) as cante,
               a.tvav,
               a.proc_tvav,
               a.tva,
               a.datain,
               a.id_rul_aux,
               a.pretd,
               a.id_valuta,
               a.lot,
               a.adata_expirare,
               a.serie,
               a.id_lucrare_rez,
               a.id_part_rez,
               c.pretachctva
          BULK COLLECT
          INTO tab_stoc
          FROM STOC A
          LEFT JOIN (select sum(cant) as cant,
                            sum(cante) as cante,
                            id_articol,
                            id_gestiune,
                            cont,
                            acont,
                            serie,
                            pret,
                            pretv,
                            id_lucrare_rez,
                            id_part_rez,
                            proc_tvav,
                            id_rul_aux,
                            pretd,
                            id_valuta,
                            lot,
                            adata_expirare
                       from rul_temp
                      where an = 1
                      group by id_articol,
                               id_gestiune,
                               cont,
                               acont,
                               serie,
                               pret,
                               pretv,
                               id_lucrare_rez,
                               id_part_rez,
                               proc_tvav,
                               id_rul_aux,
                               pretd,
                               id_valuta,
                               lot,
                               adata_expirare) B
            ON A.ID_ARTICOL = B.ID_ARTICOL
           AND A.ID_GESTIUNE = B.ID_GESTIUNE
           AND NVL(A.CONT, 'XXXX') = NVL(B.CONT, 'XXXX')
           AND NVL(A.ACONT, 'XXXX') = NVL(B.ACONT, 'XXXX')
           AND A.PRET = B.PRET
           AND A.PRETV = B.PRETV
           AND NVL(A.SERIE, '+_') = NVL(B.SERIE, '+_')
           AND NVL(A.ID_PART_REZ, 0) = NVL(B.ID_PART_REZ, 0)
           AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(B.ID_LUCRARE_REZ, 0)
           AND A.PROC_TVAV = B.PROC_TVAV
           AND NVL(A.ID_RUL_AUX, 0) = NVL(B.ID_RUL_AUX, 0)
           AND A.PRETD = B.PRETD
           AND NVL(A.ID_VALUTA, 0) = NVL(B.ID_VALUTA, 0)
           AND NVL(A.LOT, '_') = NVL(B.LOT, '_')
           AND NVL(A.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY')) =
               NVL(B.ADATA_EXPIRARE, to_date('01011500', 'DDMMYYYY'))
          LEFT JOIN NOM_ARTICOLE C
            ON A.ID_ARTICOL = C.ID_ARTICOL
         WHERE NVL(A.ID_SUCURSALA, -99) =
               NVL(pack_facturare.nid_sucursala, -99)
           AND A.ID_ARTICOL = detalii_articol.id_articol
           AND NVL(A.SERIE, '+_') = NVL(detalii_articol.serie, '+_')
           AND A.ID_GESTIUNE = detalii_articol.id_gestiune
           AND A.PRET = detalii_articol.pret_achizitie
           AND A.PRETV = detalii_articol.pretv_orig --           modificare v 2.0.19
           AND NVL(A.CONT, 'XXXX') = detalii_articol.cont
           AND A.LUNA = pack_facturare.nluna
           AND A.AN = pack_facturare.nan
           AND A.CANTS + A.CANT + NVL(B.CANT, 0) >
               A.CANTE + NVL(B.CANTE, 0)
           AND NVL(A.ID_LUCRARE_REZ, 0) =
               NVL(detalii_articol.id_lucrare_rez, 0)
           AND NVL(A.ID_PART_REZ, 0) = NVL(detalii_articol.id_part_rez, 0)
        /*       AND NVL(ID_RUL_AUX, 0) = 0*/
         ORDER BY PRET;
    END CASE;

    IF SQL%ROWCOUNT = 0 THEN
      BEGIN
        SELECT DENUMIRE
          INTO V_DENUMIRE
          FROM NOM_ARTICOLE
         WHERE ID_ARTICOL = detalii_articol.id_articol;

        RAISE_APPLICATION_ERROR(-20000,
                                'Articolul ' || V_DENUMIRE ||
                                ' nu mai e in stoc! (FACT-020)');
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          RAISE_APPLICATION_ERROR(-20000,
                                  'Unul dintre articole nu mai exista in stoc! (FACT-021 : ' ||
                                  detalii_articol.id_articol || ')');
      END;
    END IF;

    V_CANTITATE_RAMASA := detalii_articol.cantitate;

    IF pack_facturare.ntip = 27 THEN
      -- (25026,27) - transfer catre subunitati pe baza de lucrare( 25036 - daca e cu scadere din gestiune )
      DELETE FROM VANZARI_DETALII_TEMP
       WHERE ID_TEMP = detalii_articol.id_temp;
    END IF;

    FOR i IN tab_stoc.first .. tab_stoc.last LOOP

      EXIT WHEN V_CANTITATE_RAMASA = 0;

      V_TIP_INREG  := 1;
      V_TVA        := tab_stoc(i).TVA;
      V_PRET       := tab_stoc(i).PRET;
      V_PRETV_ORIG := tab_stoc(i).PRETV;
      V_ACONT      := tab_stoc(i).ACONT;
      V_DATAIN     := tab_stoc(i).DATAIN;

      V_CANTITATE := tab_stoc(i).CANTS + tab_stoc(i).CANT - tab_stoc(i).CANTE;

      IF V_CANTITATE_RAMASA >= V_CANTITATE THEN
        V_CANTITATE_RAMASA := V_CANTITATE_RAMASA - V_CANTITATE;
      ELSE
        V_CANTITATE        := V_CANTITATE_RAMASA;
        V_CANTITATE_RAMASA := 0;
      END IF;

      V_ADAOS      := V_PRETV - V_PRET;
      V_ADAOS_ORIG := V_PRETV_ORIG - V_PRET;
      IF V_PRET <> 0 THEN
        V_PROCENT      := ROUND(V_ADAOS * 100 / V_PRET,
                                pack_facturare.nzecimale_procent);
        V_PROCENT_ORIG := ROUND(V_ADAOS_ORIG * 100 / V_PRET,
                                pack_facturare.nzecimale_procent);
      ELSE
        V_PROCENT      := 0;
        V_PROCENT_ORIG := 0;
      END IF;

      V_TVAV_ORIG := tab_stoc(i).TVAV;

      V_SUMA           := 0;
      V_INCASAT_CALCUL := 0;

      V_PRET_ARE_TVA := detalii_articol.pret_cu_tva;
      V_PROC_TVAV    := detalii_articol.proc_tvav;
      V_VALOARE_TVAV := pack_facturare.calculeaza_total_tva(V_PRET                => CASE
                                                                                      V_PRET_ARE_TVA
                                                                                       WHEN 0 THEN
                                                                                        V_PRETV
                                                                                       ELSE
                                                                                        V_PRETV +
                                                                                        V_TVAV
                                                                                     END,
                                                            V_AJUSTARE            => 0,
                                                            V_CURS                => 1,
                                                            V_DISCOUNT_UNITAR     => CASE
                                                                                      V_PRET_ARE_TVA
                                                                                       WHEN 0 THEN
                                                                                        V_DISCOUNT_UNITAR
                                                                                       ELSE
                                                                                        V_DISCOUNT_CU_TVA
                                                                                     END,
                                                            V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                            V_CANTITATE           => V_CANTITATE,
                                                            V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                            V_PROC_TVAV           => V_PROC_TVAV,
                                                            V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                            V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      /*      V_VALOARE_TVAV_ORIG       := pack_facturare.calculeaza_total_tva(V_PRET => CASE V_PRET_ARE_TVA WHEN 0 THEN V_PRETV_ORIG ELSE V_PRETV_ORIG + V_TVAV_ORIG END, V_AJUSTARE => 0, V_CURS => 1, V_DISCOUNT_UNITAR => 0, V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat, V_CANTITATE => V_CANTITATE, V_PRET_CU_TVA => V_PRET_ARE_TVA, V_PROC_TVAV => V_PROC_TVAV, V_ZECIMALE_PRET => V_ZECIMALE_PRET);*/
      V_VALOARE_TVAV_ORIG      := pack_facturare.calculeaza_total_tva(V_PRET                => CASE
                                                                                                V_PRET_ARE_TVA
                                                                                                 WHEN 0 THEN
                                                                                                  V_PRETV_ORIG
                                                                                                 ELSE
                                                                                                  V_PRETV_ORIG +
                                                                                                  V_TVAV_ORIG
                                                                                               END,
                                                                      V_AJUSTARE            => 0,
                                                                      V_CURS                => 1,
                                                                      V_DISCOUNT_UNITAR     => 0,
                                                                      V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                      V_CANTITATE           => V_CANTITATE,
                                                                      V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                      V_PROC_TVAV           => pack_sesiune.TVA2PROCTVA(V_TVAV_ORIG,
                                                                                                                        V_PRETV_ORIG,
                                                                                                                        pack_facturare.nan,
                                                                                                                        pack_facturare.nluna),
                                                                      V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                      V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      V_VALOARE_DIFERENTA_TVAV := V_VALOARE_TVAV - V_VALOARE_TVAV_ORIG;
      V_VALOARE_PRETV          := pack_facturare.calculeaza_total_fara_tva(V_PRET                => CASE
                                                                                                     V_PRET_ARE_TVA
                                                                                                      WHEN 0 THEN
                                                                                                       V_PRETV
                                                                                                      ELSE
                                                                                                       V_PRETV +
                                                                                                       V_TVAV
                                                                                                    END,
                                                                           V_AJUSTARE            => 0,
                                                                           V_CURS                => 1,
                                                                           V_DISCOUNT_UNITAR     => CASE
                                                                                                     V_PRET_ARE_TVA
                                                                                                      WHEN 0 THEN
                                                                                                       V_DISCOUNT_UNITAR
                                                                                                      ELSE
                                                                                                       V_DISCOUNT_CU_TVA
                                                                                                    END,
                                                                           V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                           V_CANTITATE           => V_CANTITATE,
                                                                           V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                           V_PROC_TVAV           => V_PROC_TVAV,
                                                                           V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                           V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      /*      V_VALOARE_PRETV_ORIG      := pack_facturare.calculeaza_total_fara_tva(V_PRET => CASE V_PRET_ARE_TVA WHEN 0 THEN V_PRETV_ORIG ELSE V_PRETV_ORIG + V_TVAV_ORIG END, V_AJUSTARE => 0, V_CURS => 1, V_DISCOUNT_UNITAR => 0, V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat, V_CANTITATE => V_CANTITATE, V_PRET_CU_TVA => V_PRET_ARE_TVA, V_PROC_TVAV => V_PROC_TVAV, V_ZECIMALE_PRET => V_ZECIMALE_PRET);*/
      V_VALOARE_PRETV_ORIG      := pack_facturare.calculeaza_total_fara_tva(V_PRET                => CASE
                                                                                                      V_PRET_ARE_TVA
                                                                                                       WHEN 0 THEN
                                                                                                        V_PRETV_ORIG
                                                                                                       ELSE
                                                                                                        V_PRETV_ORIG +
                                                                                                        V_TVAV_ORIG
                                                                                                     END,
                                                                            V_AJUSTARE            => 0,
                                                                            V_CURS                => 1,
                                                                            V_DISCOUNT_UNITAR     => 0,
                                                                            V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                            V_CANTITATE           => V_CANTITATE,
                                                                            V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                            V_PROC_TVAV           => pack_sesiune.TVA2PROCTVA(V_TVAV_ORIG,
                                                                                                                              V_PRETV_ORIG,
                                                                                                                              pack_facturare.nan,
                                                                                                                              pack_facturare.nluna),
                                                                            V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                            V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);
      V_VALOARE_DIFERENTA_PRETV := V_VALOARE_PRETV - V_VALOARE_PRETV_ORIG;
      V_VALOARE_PRETVTVA        := pack_facturare.calculeaza_total_cu_tva(V_PRET                => CASE
                                                                                                    V_PRET_ARE_TVA
                                                                                                     WHEN 0 THEN
                                                                                                      V_PRETV
                                                                                                     ELSE
                                                                                                      V_PRETV +
                                                                                                      V_TVAV
                                                                                                   END,
                                                                          V_AJUSTARE            => 0,
                                                                          V_CURS                => 1,
                                                                          V_DISCOUNT_UNITAR     => CASE
                                                                                                    V_PRET_ARE_TVA
                                                                                                     WHEN 0 THEN
                                                                                                      V_DISCOUNT_UNITAR
                                                                                                     ELSE
                                                                                                      V_DISCOUNT_CU_TVA
                                                                                                   END,
                                                                          V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                          V_CANTITATE           => V_CANTITATE,
                                                                          V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                          V_PROC_TVAV           => V_PROC_TVAV,
                                                                          V_ZECIMALE_PRET       => pack_sesiune.nzecimale_pretv,
                                                                          V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumav);

      V_PROC_TVA     := pack_sesiune.TVA2PROCTVA(V_TVA,
                                                 V_PRET,
                                                 pack_facturare.nan,
                                                 pack_facturare.nluna);
      V_VALOARE_PRET := pack_facturare.calculeaza_total_fara_tva(V_PRET                => (CASE
                                                                                            WHEN tab_stoc(i).pretachctva = 1 THEN
                                                                                             V_PRET + V_TVA
                                                                                            ELSE
                                                                                             V_PRET
                                                                                          END),
                                                                 V_AJUSTARE            => 0,
                                                                 V_CURS                => 1,
                                                                 V_DISCOUNT_UNITAR     => 0,
                                                                 V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                 V_CANTITATE           => V_CANTITATE,
                                                                 V_PRET_CU_TVA         => tab_stoc(i).pretachctva,
                                                                 V_PROC_TVAV           => V_PROC_TVA,
                                                                 V_ZECIMALE_PRET       => pack_sesiune.nzecimale_preta,
                                                                 V_ZECIMALE_SUMA       => pack_sesiune.nzecimale_sumaa);

      V_VALOARE_ADAOS      := V_VALOARE_PRETV - V_VALOARE_PRET;
      V_VALOARE_ADAOS_ORIG := V_VALOARE_PRETV_ORIG - V_VALOARE_PRET;

      -- 08.11.2010
      -- (25040,41) - retur transfer catre subunitati lista pret  ( 25050 - daca e cu scadere din gestiune )
      -- la retur transfer, trebuie schimbat semnul diferentei de pret
      if pack_facturare.ntip = 41 then
        V_VALOARE_DIFERENTA_TVAV  := -V_VALOARE_DIFERENTA_TVAV;
        V_VALOARE_DIFERENTA_PRETV := -V_VALOARE_DIFERENTA_PRETV;
      end if;
      -- 08.11.2010 ^

      CASE
        WHEN detalii_articol.cont IN ('341', '345', '346', '381') THEN
          -- x - 345 / 341 / 346 / 381
          IF detalii_articol.cont = '381' THEN
            V_CONT2 := '388';
            V_CONT3 := detalii_articol.cont;
          ELSE
            V_CONT2 := '348';
            V_CONT3 := '711';
          END IF;

          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.cont
                                                          else
                                                           pack_facturare.v_date_gestiune.cont
                                                        end), -- scd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(V_ACONT, 'X', '')
                                                          else
                                                           REPLACE(pack_facturare.v_date_gestiune.acont,
                                                                   'X',
                                                                   '')
                                                        end), -- ascd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.cont
                                                          else
                                                           detalii_articol.cont
                                                        end), -- scc
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(pack_facturare.v_date_gestiune.acont,
                                                                   'X',
                                                                   '')
                                                          else
                                                           REPLACE(V_ACONT, 'X', '')
                                                        end), -- ascc
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.id_gestiune
                                                          else
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                        end), -- gestin
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- gestout
                                                        1,
                                                        0,
                                                        0,
                                                        NULL);

          IF V_VALOARE_ADAOS <> 0 THEN
            -- 341 / 345 / 346 - 348 / 381 - 388
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_ADAOS,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          detalii_articol.cont,
                                                          REPLACE(V_ACONT,
                                                                  'X',
                                                                  ''),
                                                          V_CONT2,
                                                          NULL,
                                                          pack_facturare.v_date_gestiune.id_gestiune, -- V_ID_GESTIN
                                                          detalii_articol.id_gestiune,
                                                          2,
                                                          0,
                                                          0,
                                                          NULL);

            -- 348 - 711 / 388 - 381
            IF V_CONT3 = '711' THEN
              V_ACONT2 := NULL;
            ELSE
              V_ACONT2 := REPLACE(V_ACONT, 'X');
            END IF;

            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_ADAOS,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          V_CONT2,
                                                          NULL,
                                                          V_CONT3,
                                                          V_ACONT2,
                                                          0, -- V_ID_GESTIN
                                                          detalii_articol.id_gestiune,
                                                          2,
                                                          0,
                                                          0,
                                                          NULL);

          END IF;

        WHEN V_TIP_GESTIUNE <> 6 AND
             pack_facturare.v_date_gestiune.id_tip_gest <> 6 THEN

          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.cont
                                                          else
                                                           pack_facturare.v_date_gestiune.cont
                                                        end), -- scd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(V_ACONT, 'X')
                                                          else
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                        end), -- ascd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.cont
                                                          else
                                                           detalii_articol.cont
                                                        end), -- scc
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                          else
                                                           REPLACE(V_ACONT, 'X')
                                                        end), -- ascc
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.id_gestiune
                                                          else
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                        end), -- id_gestin
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- id_gestout
                                                        3,
                                                        0,
                                                        0,
                                                        NULL);

          IF V_TIP_GESTIUNE = 7 AND
             (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV OR
             V_DISCOUNT_UNITAR <> 0) THEN

            INSERT INTO rul_temp
              (AN,
               ID_ARTICOL,
               SERIE,
               ID_GESTIUNE,
               ID_LUCRARE,
               ID_RESPONSABIL,
               NRACT,
               SERIE_ACT,
               CANT,
               CANTE,
               CONT,
               ACONT,
               PRETV,
               TVAV,
               PRETVTVA,
               PROC_TVAV,
               PROC_TVA,
               PRET,
               TVA,
               ADAOS,
               PROCENT,
               DATAACT,
               NNIR,
               DATAIN,
               DATAOUT,
               PRETD,
               ID_VALUTA,
               ID_SET,
               ID_TIP_RULAJ,
               LOT,
               ADATA_EXPIRARE,
               ID_RUL_AUX,
               ID_LUCRARE_REZ,
               ID_PART_REZ)
              SELECT decode(rownum, 1, V_TIP_INREG, 0),
                     detalii_articol.id_articol,
                     tab_stoc(i).serie,
                     detalii_articol.id_gestiune,
                     nvl(pack_facturare.nid_lucrare, 0),
                     pack_facturare.nid_responsabil,
                     pack_facturare.nnumar_act,
                     pack_facturare.cserie_act,
                     --                   decode(rownum, 2, V_CANTITATE, 0) as CANT,
                     (case
                       when (rownum = 1 and pack_facturare.ntip = 41) or
                            (rownum = 2 and pack_facturare.ntip <> 41) then
                        V_CANTITATE
                       else
                        0
                     end) as CANT,
                     --               decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                     (case
                       when (rownum = 2 and pack_facturare.ntip = 41) or
                            (rownum = 1 and pack_facturare.ntip <> 41) then
                        V_CANTITATE
                       else
                        0
                     end) as CANTE,
                     detalii_articol.cont as CONT,
                     REPLACE(V_ACONT, 'X', '') as ACONT,
                     decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                     decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                     decode(rownum,
                            1,
                            tab_stoc(i).pretv + tab_stoc(i).tvav,
                            V_PRETVTVA) as PRETVTVA,
                     decode(rownum,
                            1,
                            tab_stoc(i).proc_Tvav,
                            detalii_articol.proc_tvav) as PROC_TVAV,
                     V_PROC_TVA as PROC_TVA,
                     V_PRET as PRET,
                     V_TVA AS TVA,
                     decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                     decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                     pack_facturare.ddata_act,
                     pack_facturare.nnumar_bon,
                     decode(rownum,
                            1,
                            tab_stoc(i).datain,
                            pack_facturare.ddata_act) as datain,
                     decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                     tab_stoc(i).pretd,
                     tab_stoc(i).id_valuta,
                     pack_facturare.nid_set,
                     3 as ID_TIP_RULAJ,
                     tab_stoc(i).lot,
                     tab_stoc(i).adata_expirare,
                     tab_stoc(i).id_rul_aux,
                     tab_stoc(i).ID_LUCRARE_REZ,
                     tab_stoc(i).ID_PART_REZ
                FROM dual
              CONNECT BY level <= 2;

            V_TIP_INREG := V_TIP_INREG - 1;
            V_DATAIN    := pack_facturare.ddata_act;

          END IF;

        WHEN V_TIP_GESTIUNE = pack_facturare.v_date_gestiune.id_tip_gest AND
             pack_facturare.v_date_gestiune.id_tip_gest = 6 THEN
          -- SCHIMB PRETUL DE VANZARE IN GESTIUNEA SURSA SI APOI IL TRANSFER LA NOUL PRET DE VANZARE
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRETVTVA,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.cont
                                                          else
                                                           pack_facturare.v_date_gestiune.cont
                                                        end), -- scd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(V_ACONT, 'X')
                                                          else
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                        end), -- ascd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.cont
                                                          else
                                                           detalii_articol.cont
                                                        end), -- scc
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                          else
                                                           REPLACE(V_ACONT, 'X')
                                                        end), -- ascc
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.id_gestiune
                                                          else
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                        end), -- id_gestin
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- id_gestout
                                                        4,
                                                        0,
                                                        0,
                                                        NULL);

          IF V_TIP_GESTIUNE = 6 AND
             (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV OR
             V_DISCOUNT_UNITAR <> 0) THEN
            -- SCHIMB PRETUL DE VANZARE IN GESTIUNEA SURSA SI APOI IL TRANSFER LA NOUL PRET DE VANZARE
            --371-378
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_DIFERENTA_PRETV,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          '371',
                                                          REPLACE(V_ACONT, --     pack_facturare.v_date_gestiune.acont,
                                                                  'X'),
                                                          '378',
                                                          NULL, --     pack_facturare.v_date_gestiune.acont,
                                                          detalii_articol.id_gestiune,
                                                          detalii_articol.id_gestiune, --     pack_facturare.v_date_gestiune.id_gestiune,
                                                          5,
                                                          0,
                                                          (detalii_articol.proc_tvav - 1) * 100,
                                                          NULL);
            --371-4428
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_DIFERENTA_TVAV,
                                                          0, -- DISCOUNT_UNITAR
                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          '371',
                                                          REPLACE(V_ACONT, --     pack_facturare.v_date_gestiune.acont,
                                                                  'X'),
                                                          '4428',
                                                          NULL,
                                                          detalii_articol.id_gestiune,
                                                          detalii_articol.id_gestiune, --     pack_facturare.v_date_gestiune.id_gestiune,
                                                          5,
                                                          0,
                                                          (detalii_articol.proc_tvav - 1) * 100,
                                                          NULL);

            IF V_DISCOUNT_UNITAR = 0 THEN
              INSERT INTO rul_temp
                (AN,
                 ID_ARTICOL,
                 SERIE,
                 ID_GESTIUNE,
                 ID_GESTIUNEC,
                 ID_LUCRARE,
                 ID_RESPONSABIL,
                 NRACT,
                 SERIE_ACT,
                 CANT,
                 CANTE,
                 CONT,
                 ACONT,
                 PRETV,
                 TVAV,
                 PRETVTVA,
                 PROC_TVAV,
                 PROC_TVA,
                 PRET,
                 TVA,
                 ADAOS,
                 PROCENT,
                 DATAACT,
                 NNIR,
                 DATAIN,
                 DATAOUT,
                 PRETD,
                 ID_VALUTA,
                 ID_SET,
                 ID_TIP_RULAJ,
                 LOT,
                 ADATA_EXPIRARE,
                 ID_RUL_AUX,
                 ID_LUCRARE_REZ,
                 ID_PART_REZ)
                SELECT decode(rownum, 1, V_TIP_INREG, 0),
                       detalii_articol.id_articol,
                       tab_stoc(i).serie,
                       detalii_articol.id_gestiune,
                       pack_facturare.v_date_gestiune.id_gestiune,
                       nvl(pack_facturare.nid_lucrare, 0),
                       pack_facturare.nid_responsabil,
                       pack_facturare.nnumar_act,
                       pack_facturare.cserie_act,
                       --                   decode(rownum, 2, V_CANTITATE, 0) as CANT,
                       (case
                         when (rownum = 1 and pack_facturare.ntip = 41) or
                              (rownum = 2 and pack_facturare.ntip <> 41) then
                          V_CANTITATE
                         else
                          0
                       end) as CANT,
                       --               decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                       (case
                         when (rownum = 2 and pack_facturare.ntip = 41) or
                              (rownum = 1 and pack_facturare.ntip <> 41) then
                          V_CANTITATE
                         else
                          0
                       end) as CANTE,
                       detalii_articol.cont as CONT,
                       REPLACE(V_ACONT, 'X', '') as ACONT,
                       decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                       decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                       decode(rownum,
                              1,
                              tab_stoc(i).pretv + tab_stoc(i).tvav,
                              V_PRETVTVA) as PRETVTVA,
                       decode(rownum,
                              1,
                              tab_stoc(i).proc_Tvav,
                              detalii_articol.proc_tvav) as PROC_TVAV,
                       V_PROC_TVA as PROC_TVA,
                       V_PRET as PRET,
                       V_TVA AS TVA,
                       decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                       decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                       pack_facturare.ddata_act,
                       pack_facturare.nnumar_bon,
                       decode(rownum,
                              1,
                              tab_stoc(i).datain,
                              pack_facturare.ddata_act) as datain,
                       decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                       tab_stoc(i).pretd,
                       tab_stoc(i).id_valuta,
                       pack_facturare.nid_set,
                       3 as ID_TIP_RULAJ,
                       tab_stoc(i).lot,
                       tab_stoc(i).adata_expirare,
                       tab_stoc(i).id_rul_aux,
                       tab_stoc(i).ID_LUCRARE_REZ,
                       tab_stoc(i).ID_PART_REZ
                  FROM dual
                CONNECT BY level <= 2;

              V_TIP_INREG := V_TIP_INREG - 1;
              V_DATAIN    := pack_facturare.ddata_act;
            END IF;
          END IF;

          -- daca are analitice la adaos : trebuie sa transfer adaosul dintr-un analitic in altul
          IF pack_facturare.nanalitice_378 = 1 THEN
            -- 378 - 378
            V_INCASAT_CALCUL := scrie_nota(1,
                                           V_VALOARE_ADAOS,
                                           0, -- DISCOUNT_UNITAR
                                           0, --   PRET CU TVA
                                           pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                           0, --     V_IN_VALUTA
                                           0, -- V_CURS,
                                           0, -- ID_VENCHELT
                                           V_ID_SECTIE,
                                           NULL, -- V_ID_CTR
                                           V_EXPLICATIE,
                                           '378', -- scd
                                           Null, -- ascd
                                           '378', -- scc
                                           Null, -- ascc
                                           (case
                                             when pack_facturare.ntip = 41 then
                                              pack_facturare.v_date_gestiune.id_gestiune
                                             else
                                              detalii_articol.id_gestiune
                                           end), -- id_gestin
                                           (case
                                             when pack_facturare.ntip = 41 then
                                              detalii_articol.id_gestiune
                                             else
                                              pack_facturare.v_date_gestiune.id_gestiune
                                           end), -- id_gestout
                                           4,
                                           0,
                                           0,
                                           NULL);

            IF NVL(V_ACONT, '') <>
               NVL(pack_facturare.v_date_gestiune.acont, '') THEN
              -- 4428 - 4428
              V_INCASAT_CALCUL := scrie_nota(1,
                                             V_VALOARE_TVAV,
                                             0, -- DISCOUNT_UNITAR
                                             0, --   PRET CU TVA
                                             pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                             0, --     V_IN_VALUTA
                                             0, -- V_CURS,
                                             0, -- ID_VENCHELT
                                             V_ID_SECTIE,
                                             NULL, -- V_ID_CTR
                                             V_EXPLICATIE,
                                             '4428', -- scd
                                             Null, -- ascd
                                             '4428', -- scc
                                             Null, -- ascc
                                             (case
                                               when pack_facturare.ntip = 41 then
                                                pack_facturare.v_date_gestiune.id_gestiune
                                               else
                                                detalii_articol.id_gestiune
                                             end), -- id_gestin
                                             (case
                                               when pack_facturare.ntip = 41 then
                                                detalii_articol.id_gestiune
                                               else
                                                pack_facturare.v_date_gestiune.id_gestiune
                                             end), -- id_gestout
                                             4,
                                             0,
                                             0,
                                             NULL);
            END IF;

          END IF;

        WHEN (V_TIP_GESTIUNE IN (4, 5, 7) AND
             pack_facturare.v_date_gestiune.id_tip_gest = 6 and
             pack_facturare.ntip <> 41) OR
             (pack_facturare.ntip = 41 AND V_TIP_GESTIUNE = 6 AND
             pack_facturare.v_date_gestiune.id_tip_gest IN (4, 5, 7)) THEN
          -- achizitie > vanzare
          -- 371- x
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- V_ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.cont
                                                          else
                                                           '371'
                                                        end), -- scd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(V_ACONT, 'X')
                                                          else
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                        end), --ascd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           '371'
                                                          else
                                                           detalii_articol.cont
                                                        end), -- scc
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                          else
                                                           REPLACE(V_ACONT, 'X')
                                                        end), -- ascc
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.id_gestiune
                                                          else
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                        end), -- id_gestin
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- id_gestout
                                                        6,
                                                        0,
                                                        0,
                                                        NULL);
          --371-378
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_ADAOS,
                                                        0, -- DISCOUNT_UNITAR

                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- V_ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '371', -- scd
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(V_ACONT, 'X')
                                                          else
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                        end), -- ascd
                                                        '378',
                                                        NULL, -- ascc
                                                        (case
                                                          when pack_facturare.ntip <> 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- id_gestin
                                                        (case
                                                          when pack_facturare.ntip <> 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- id_gestout
                                                        6,
                                                        0,
                                                        0,
                                                        NULL);

          --371-4428
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_TVAV,
                                                        0, -- DISCOUNT_UNITAR

                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '371', -- scd
                                                        (case
                                                          when pack_facturare.ntip <> 41 then
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                          else
                                                           REPLACE(V_ACONT, 'X')
                                                        end), -- ascd
                                                        '4428',
                                                        NULL, -- ascc
                                                        (case
                                                          when pack_facturare.ntip <> 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- id_gestin
                                                        (case
                                                          when pack_facturare.ntip <> 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- id_gestout
                                                        6,
                                                        0,
                                                        (detalii_articol.proc_tvav - 1) * 100,
                                                        NULL);

          IF ((V_TIP_GESTIUNE = 7 AND pack_facturare.ntip <> 41) or
             (pack_facturare.v_date_gestiune.id_tip_gest = 7 AND
             pack_facturare.ntip = 41)) AND
             (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV OR
             V_DISCOUNT_UNITAR <> 0) THEN

            IF pack_facturare.v_date_gestiune.id_tip_gest = 7 and
               V_TIP_GESTIUNE = 6 and pack_facturare.ntip = 41 THEN
              -- 371-378
              V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                            V_VALOARE_DIFERENTA_PRETV,
                                                            0, -- DISCOUNT_UNITAR
                                                            0, --   PRET CU TVA
                                                            pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                            0, --     V_IN_VALUTA
                                                            0, -- V_CURS,
                                                            0, -- ID_VENCHELT
                                                            V_ID_SECTIE,
                                                            NULL, -- V_ID_CTR
                                                            V_EXPLICATIE,
                                                            '371',
                                                            REPLACE(V_ACONT,
                                                                    'X'),
                                                            '378',
                                                            NULL,
                                                            detalii_articol.id_gestiune,
                                                            detalii_articol.id_gestiune,
                                                            7,
                                                            0,
                                                            (detalii_articol.proc_tvav - 1) * 100,
                                                            NULL);
              --371-4428
              V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                            V_VALOARE_DIFERENTA_TVAV,
                                                            0, -- DISCOUNT_UNITAR
                                                            0, --   PRET CU TVA
                                                            pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                            0, --     V_IN_VALUTA
                                                            0, -- V_CURS,
                                                            0, -- ID_VENCHELT
                                                            V_ID_SECTIE,
                                                            NULL, -- V_ID_CTR
                                                            V_EXPLICATIE,
                                                            '371',
                                                            REPLACE(V_ACONT,
                                                                    'X'),
                                                            '4428',
                                                            NULL,
                                                            detalii_articol.id_gestiune,
                                                            detalii_articol.id_gestiune,
                                                            7,
                                                            0,
                                                            (detalii_articol.proc_tvav - 1) * 100,
                                                            NULL);

            END IF;

            IF V_DISCOUNT_UNITAR = 0 THEN
              INSERT INTO rul_temp
                (AN,
                 ID_ARTICOL,
                 SERIE,
                 ID_GESTIUNE,
                 ID_LUCRARE,
                 ID_RESPONSABIL,
                 NRACT,
                 SERIE_ACT,
                 CANT,
                 CANTE,
                 CONT,
                 ACONT,
                 PRETV,
                 TVAV,
                 PRETVTVA,
                 PROC_TVAV,
                 PROC_TVA,
                 PRET,
                 TVA,
                 ADAOS,
                 PROCENT,
                 DATAACT,
                 NNIR,
                 DATAIN,
                 DATAOUT,
                 PRETD,
                 ID_VALUTA,
                 ID_SET,
                 ID_TIP_RULAJ,
                 LOT,
                 ADATA_EXPIRARE,
                 ID_RUL_AUX,
                 ID_LUCRARE_REZ,
                 ID_PART_REZ)
                SELECT decode(rownum, 1, V_TIP_INREG, 0),
                       detalii_articol.id_articol,
                       tab_stoc(i).serie,
                       detalii_articol.id_gestiune,
                       nvl(pack_facturare.nid_lucrare, 0),
                       pack_facturare.nid_responsabil,
                       pack_facturare.nnumar_act,
                       pack_facturare.cserie_act,
                       (case
                         when (rownum = 1 and pack_facturare.ntip = 41) or
                              (rownum = 2 and pack_facturare.ntip <> 41) then
                          V_CANTITATE
                         else
                          0
                       end) as CANT,
                       (case
                         when (rownum = 2 and pack_facturare.ntip = 41) or
                              (rownum = 1 and pack_facturare.ntip <> 41) then
                          V_CANTITATE
                         else
                          0
                       end) as CANTE,
                       detalii_articol.cont as CONT,
                       REPLACE(V_ACONT, 'X', '') as ACONT,
                       decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                       decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                       decode(rownum,
                              1,
                              tab_stoc(i).pretv + tab_stoc(i).tvav,
                              V_PRETVTVA) as PRETVTVA,
                       decode(rownum,
                              1,
                              tab_stoc(i).proc_Tvav,
                              detalii_articol.proc_tvav) as PROC_TVAV,
                       V_PROC_TVA as PROC_TVA,
                       V_PRET as PRET,
                       V_TVA AS TVA,
                       decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                       decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                       pack_facturare.ddata_act,
                       pack_facturare.nnumar_bon,
                       decode(rownum,
                              1,
                              tab_stoc(i).datain,
                              pack_facturare.ddata_act) as datain,
                       decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                       tab_stoc(i).pretd,
                       tab_stoc(i).id_valuta,
                       pack_facturare.nid_set,
                       3 as ID_TIP_RULAJ,
                       tab_stoc(i).lot,
                       tab_stoc(i).adata_expirare,
                       tab_stoc(i).id_rul_aux,
                       tab_stoc(i).ID_LUCRARE_REZ,
                       tab_stoc(i).ID_PART_REZ
                  FROM dual
                CONNECT BY level <= 2;

              V_TIP_INREG := V_TIP_INREG - 1;
              V_DATAIN    := pack_facturare.ddata_act;
            END IF;

          END IF;

        WHEN (V_TIP_GESTIUNE = 6 AND
             pack_facturare.v_date_gestiune.id_tip_gest IN (4, 5, 7) and
             pack_facturare.ntip <> 41) OR
             (pack_facturare.ntip = 41 AND V_TIP_GESTIUNE IN (4, 5, 7) AND
             pack_facturare.v_date_gestiune.id_tip_gest = 6) THEN
          -- vanzare > achizitie
          -- retur achizitie > vanzare
          -- pack_facturare.ntip = 41 = retur transfer

          --x-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_PRET,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           '371'
                                                          else
                                                           pack_facturare.v_date_gestiune.cont
                                                        end),
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(V_ACONT, 'X')
                                                          else
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                        end),
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.cont
                                                          else
                                                           '371'
                                                        end),
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                          else
                                                           REPLACE(V_ACONT, 'X')
                                                        end),
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           detalii_articol.id_gestiune
                                                          else
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                        end), -- V_ID_GESTIN
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end),
                                                        8,
                                                        0,
                                                        0,
                                                        NULL);
          --378-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_ADAOS,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '378',
                                                        NULL,
                                                        '371',
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                          else
                                                           REPLACE(V_ACONT, 'X')
                                                        end),
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- V_ID_GESTIN
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end),
                                                        8,
                                                        0,
                                                        (detalii_articol.proc_tvav - 1) * 100,
                                                        NULL);

          --4428-371
          V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                        V_VALOARE_TVAV,
                                                        0, -- DISCOUNT_UNITAR
                                                        0, --   PRET CU TVA
                                                        pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                        0, --     V_IN_VALUTA
                                                        0, -- V_CURS,
                                                        0, -- ID_VENCHELT
                                                        V_ID_SECTIE,
                                                        NULL, -- V_ID_CTR
                                                        V_EXPLICATIE,
                                                        '4428',
                                                        NULL,
                                                        '371',
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           REPLACE(pack_facturare.v_date_gestiune.acont, 'X')
                                                          else
                                                           REPLACE(V_ACONT, 'X')
                                                        end),
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end), -- V_ID_GESTIN
                                                        (case
                                                          when pack_facturare.ntip = 41 then
                                                           pack_facturare.v_date_gestiune.id_gestiune
                                                          else
                                                           detalii_articol.id_gestiune
                                                        end),
                                                        8,
                                                        0,
                                                        (detalii_articol.proc_tvav - 1) * 100,
                                                        NULL);

          IF (V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV OR
             V_DISCOUNT_UNITAR <> 0) and pack_facturare.ntip <> 41 THEN
            -- SCHIMB PRETUL DE VANZARE IN GESTIUNEA SURSA SI APOI IL TRANSFER LA NOUL PRET DE VANZARE
            --371-378
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_DIFERENTA_PRETV,
                                                          0, -- DISCOUNT_UNITAR

                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          '371',
                                                          REPLACE(V_ACONT, 'X'),
                                                          '378',
                                                          NULL,
                                                          (case
                                                            when pack_facturare.ntip = 41 then
                                                             pack_facturare.v_date_gestiune.id_gestiune
                                                            else
                                                             detalii_articol.id_gestiune
                                                          end),
                                                          (case
                                                            when pack_facturare.ntip = 41 then
                                                             pack_facturare.v_date_gestiune.id_gestiune
                                                            else
                                                             detalii_articol.id_gestiune
                                                          end),
                                                          9,
                                                          0,
                                                          (detalii_articol.proc_tvav - 1) * 100,
                                                          NULL);

            --371-4428
            V_INCASAT_CALCUL := pack_facturare.scrie_nota(1,
                                                          V_VALOARE_DIFERENTA_TVAV,
                                                          0, -- DISCOUNT_UNITAR

                                                          0, --   PRET CU TVA
                                                          pack_facturare.nid_moneda_nationala, --     V_ID_VALUTA
                                                          0, --     V_IN_VALUTA
                                                          0, -- V_CURS,
                                                          0, -- ID_VENCHELT
                                                          V_ID_SECTIE,
                                                          NULL, -- V_ID_CTR
                                                          V_EXPLICATIE,
                                                          '371',
                                                          REPLACE(V_ACONT, --     pack_facturare.v_date_gestiune.acont,
                                                                  'X'),
                                                          '4428',
                                                          NULL,
                                                          (case
                                                            when pack_facturare.ntip = 41 then
                                                             pack_facturare.v_date_gestiune.id_gestiune
                                                            else
                                                             detalii_articol.id_gestiune
                                                          end),
                                                          (case
                                                            when pack_facturare.ntip = 41 then
                                                             pack_facturare.v_date_gestiune.id_gestiune
                                                            else
                                                             detalii_articol.id_gestiune
                                                          end),
                                                          9,
                                                          0,
                                                          (detalii_articol.proc_tvav - 1) * 100,
                                                          NULL);
          END IF;

          IF ((V_PRETV_ORIG <> V_PRETV OR V_TVAV_ORIG <> V_TVAV) AND
             V_DISCOUNT_UNITAR = 0 AND
             (pack_facturare.ntip <> 41 OR
             (pack_facturare.ntip = 41 AND V_TIP_GESTIUNE = 7))) THEN
            INSERT INTO rul_temp
              (AN,
               ID_ARTICOL,
               SERIE,
               ID_GESTIUNE,
               ID_LUCRARE,
               ID_RESPONSABIL,
               NRACT,
               SERIE_ACT,
               CANT,
               CANTE,
               CONT,
               ACONT,
               PRETV,
               TVAV,
               PRETVTVA,
               PROC_TVAV,
               PROC_TVA,
               PRET,
               TVA,
               ADAOS,
               PROCENT,
               DATAACT,
               NNIR,
               DATAIN,
               DATAOUT,
               PRETD,
               ID_VALUTA,
               ID_SET,
               ID_TIP_RULAJ,
               LOT,
               ADATA_EXPIRARE,
               ID_RUL_AUX,
               ID_LUCRARE_REZ,
               ID_PART_REZ)
              SELECT decode(rownum, 1, V_TIP_INREG, 0),
                     detalii_articol.id_articol,
                     tab_stoc(i).serie,
                     (case
                       when pack_facturare.ntip = 41 then
                        pack_facturare.v_date_gestiune.id_gestiune
                       else
                        detalii_articol.id_gestiune
                     end),
                     nvl(pack_facturare.nid_lucrare, 0),
                     pack_facturare.nid_responsabil,
                     pack_facturare.nnumar_act,
                     pack_facturare.cserie_act,
                     --                     decode(rownum, 2, V_CANTITATE, 0) as CANT,
                     (case
                       when (rownum = 1 and pack_facturare.ntip = 41) or
                            (rownum = 2 and pack_facturare.ntip <> 41) then
                        V_CANTITATE
                       else
                        0
                     end) as CANT,
                     --               decode(rownum, 1, V_CANTITATE, 0) as CANTE,
                     (case
                       when (rownum = 2 and pack_facturare.ntip = 41) or
                            (rownum = 1 and pack_facturare.ntip <> 41) then
                        V_CANTITATE
                       else
                        0
                     end) as CANTE,
                     detalii_articol.cont as CONT,
                     REPLACE(V_ACONT, 'X', '') as ACONT,
                     decode(rownum, 1, V_PRETV_ORIG, V_PRETV) as PRETV,
                     decode(rownum, 1, tab_stoc(i).tvav, V_TVAV) as TVAV,
                     decode(rownum,
                            1,
                            tab_stoc(i).pretv + tab_stoc(i).tvav,
                            V_PRETVTVA) as PRETVTVA,
                     decode(rownum,
                            1,
                            tab_stoc(i).proc_Tvav,
                            detalii_articol.proc_tvav) as PROC_TVAV,
                     V_PROC_TVA as PROC_TVA,
                     V_PRET as PRET,
                     V_TVA AS TVA,
                     decode(rownum, 1, V_ADAOS_ORIG, V_ADAOS) as ADAOS,
                     decode(rownum, 1, V_PROCENT_ORIG, V_PROCENT) as PROCENT,
                     pack_facturare.ddata_act,
                     pack_facturare.nnumar_bon,
                     decode(rownum,
                            1,
                            tab_stoc(i).datain,
                            pack_facturare.ddata_act) as datain,
                     decode(rownum, 1, pack_facturare.ddata_act, null) as dataout,
                     tab_stoc(i).pretd,
                     tab_stoc(i).id_valuta,
                     pack_facturare.nid_set,
                     3 as ID_TIP_RULAJ,
                     tab_stoc(i).lot,
                     tab_stoc(i).adata_expirare,
                     tab_stoc(i).id_rul_aux,
                     tab_stoc(i).ID_LUCRARE_REZ,
                     tab_stoc(i).ID_PART_REZ
                FROM dual
              CONNECT BY level <= 2;
            V_TIP_INREG := V_TIP_INREG - 1;
            V_DATAIN    := pack_facturare.ddata_act;
          END IF;

        ELSE
          RAISE_APPLICATION_ERROR(-20000,
                                  'Combinatie invalida de gestiuni! (FACT-014 : ' ||
                                  detalii_articol.cont || '.' ||
                                  detalii_articol.id_gestiune || '.' ||
                                  pack_facturare.v_date_gestiune.id_gestiune || ')');
      END CASE;

      -- pe randul 1 e iesirea
      -- pe randul 2 e intrarea
      INSERT INTO rul_temp
        (AN,
         ID_ARTICOL,
         SERIE,
         ID_GESTIUNE,
         ID_GESTIUNEC,
         ID_LUCRARE,
         ID_RESPONSABIL,
         NRACT,
         SERIE_ACT,
         CANT,
         CANTE,
         CONT,
         ACONT,
         PRETV,
         TVAV,
         PRETVTVA,
         PROC_TVAV,
         PROC_TVA,
         PRET,
         TVA,
         ADAOS,
         PROCENT,
         DATAACT,
         NNIR,
         DATAIN,
         DATAOUT,
         PRETD,
         ID_VALUTA,
         ID_SET,
         ID_TIP_RULAJ,
         LOT,
         ADATA_EXPIRARE,
         ID_RUL_AUX,
         ID_LUCRARE_REZ,
         ID_PART_REZ)
        SELECT decode(rownum, 1, V_TIP_INREG, 0),
               detalii_articol.id_articol as ID_ARTICOL,
               tab_stoc(i).serie as SERIE,
               decode(rownum,
                      1,
                      detalii_articol.id_gestiune,
                      pack_facturare.v_date_gestiune.id_gestiune) as ID_GESTIUNE,
               decode(rownum,
                      2,
                      detalii_articol.id_gestiune,
                      pack_facturare.v_date_gestiune.id_gestiune) as ID_GESTIUNEC,
               nvl(pack_facturare.nid_lucrare, 0) as ID_LUCRARE,
               pack_facturare.nid_responsabil as ID_RESPONSABIL,
               pack_facturare.nnumar_act,
               pack_facturare.cserie_act,
               --               decode(rownum, 2, V_CANTITATE, 0) as CANT,
               (case
                 when (rownum = 1 and pack_facturare.ntip = 41) or
                      (rownum = 2 and pack_facturare.ntip <> 41) then
                  V_CANTITATE
                 else
                  0
               end) as CANT,
               --               decode(rownum, 1, V_CANTITATE, 0) as CANTE,
               (case
                 when (rownum = 2 and pack_facturare.ntip = 41) or
                      (rownum = 1 and pack_facturare.ntip <> 41) then
                  V_CANTITATE
                 else
                  0
               end) as CANTE,
               decode(rownum,
                      1,
                      detalii_articol.cont,
                      pack_facturare.v_date_gestiune.cont) as CONT,
               decode(rownum,
                      1,
                      REPLACE(V_ACONT, 'X'),
                      REPLACE(pack_facturare.v_date_gestiune.acont, 'X')) as ACONT,
               (case
                 when rownum = 1 and V_TIP_GESTIUNE IN (6, 7) then
                  V_PRETV
                 when rownum = 2 and
                      pack_facturare.v_date_gestiune.id_tip_gest in (6, 7) then
                  V_PRETV - V_DISCOUNT_UNITAR
                 else
                  0
               end) as PRETV,
               (case
                 when rownum = 1 and V_TIP_GESTIUNE IN (6, 7) then
                  V_TVAV
                 when rownum = 2 and
                      pack_facturare.v_date_gestiune.id_tip_gest in (6, 7) then
                  V_TVAV - V_DISCOUNT_TVA
                 else
                  0
               end) as TVAV,
               (case
                 when rownum = 1 and V_TIP_GESTIUNE in (6, 7) then
                  V_PRETVTVA
                 when rownum = 2 and
                      pack_facturare.v_date_gestiune.id_tip_gest in (6, 7) then
                  V_PRETVTVA - V_DISCOUNT_CU_TVA
                 else
                  0
               end) as PRETVTVA,
               detalii_articol.proc_tvav as PROC_TVAV,
               V_PROC_TVA as PROC_TVA,
               V_PRET as PRET,
               V_TVA AS TVA,
               V_ADAOS as ADAOS,
               V_PROCENT as PROCENT,
               pack_facturare.ddata_act as DATAACT,
               pack_facturare.nnumar_bon as NNIR,
               decode(rownum, 1, V_DATAIN, pack_facturare.ddata_act) as datain,
               decode(rownum, 1, pack_facturare.ddata_act, NULL) as dataout,
               tab_stoc(i).pretd as PRETD,
               tab_stoc(i).id_valuta as ID_VALUTA,
               pack_facturare.nid_set as ID_SET,
               1 as ID_TIP_RULAJ,
               tab_stoc(i).lot,
               tab_stoc(i).adata_expirare,
               tab_stoc(i).id_rul_aux,
               tab_stoc(i).ID_LUCRARE_REZ,
               (case
                 when rownum = 2 and pack_facturare.ntip in (23, 25) and
                      pack_facturare.v_date_gestiune.cont = '357' then
                  pack_facturare.nid_part_rez
                 else
                  tab_stoc(i).ID_PART_REZ
               end)
          FROM dual
        CONNECT BY level <= 2;

      IF pack_facturare.ntip = 27 THEN
        MERGE INTO VANZARI_DETALII_TEMP A
        USING DUAL B
        ON (A.ID_ARTICOL = detalii_articol.id_articol AND A.ID_POL = detalii_articol.id_pol AND A.PRET = detalii_articol.pret AND A.PRET_CU_TVA = detalii_articol.pret_cu_tva AND A.PROC_TVAV = detalii_articol.proc_tvav AND A.DISCOUNT_UNITAR = detalii_articol.discount_unitar AND A.ID_GESTIUNE = detalii_articol.id_gestiune AND NVL(A.CONT, 'XXXX') = NVL(detalii_articol.cont, 'XXX') AND NVL(A.ID_VALUTA, -1) = NVL(detalii_articol.id_valuta, -1) AND NVL(A.MULTIPLICATOR, 1) = NVL(detalii_articol.multiplicator, 1) AND NVL(A.CURS, 1) = NVL(detalii_articol.curs, 1) AND A.ID_JTVA_COLOANA = detalii_articol.id_jtva_coloana AND A.IN_STOC = detalii_articol.in_stoc AND NVL(A.ID_GESTIUNE_DEST, -99) = NVL(detalii_articol.id_gestiune_dest, -99) AND NVL(A.ID_UTIL, 0) = NVL(detalii_articol.id_util, 0) AND A.PRET_ACHIZITIE = V_PRET AND NVL(A.ID_VANZARE_SET, -99) = NVL(detalii_articol.id_vanzare_set, -99) AND NVL(A.PRETD, 0) = NVL(tab_stoc(i).pretd, 0) AND NVL(A.ID_VALUTAD, -99) = NVL(tab_stoc(i).id_valuta, -99))
        WHEN MATCHED THEN
          UPDATE SET CANTITATE = CANTITATE + V_CANTITATE
        WHEN NOT MATCHED THEN
          INSERT
            (ID_ARTICOL,
             ID_POL,
             PRET,
             PRET_CU_TVA,
             PROC_TVAV,
             CANTITATE,
             DISCOUNT_UNITAR,
             ID_GESTIUNE,
             CONT,
             ID_VALUTA,
             CURS,
             MULTIPLICATOR,
             ID_JTVA_COLOANA,
             IN_STOC,
             ID_GESTIUNE_DEST,
             ID_COMANDA,
             ID_UTIL,
             PRET_ACHIZITIE,
             PRETD,
             ID_VALUTAD,
             ID_VANZARE_SET,
             ID_LUCRARE_REZ,
             ID_PART_REZ,
             NUMAR_ACT,
             ID_TEMP)
          VALUES
            (detalii_articol.id_articol,
             detalii_articol.id_pol,
             detalii_articol.pret,
             detalii_articol.pret_cu_tva,
             detalii_articol.proc_tvav,
             V_CANTITATE,
             detalii_articol.discount_unitar,
             detalii_articol.id_gestiune,
             detalii_articol.cont,
             detalii_articol.id_valuta,
             detalii_articol.curs,
             detalii_articol.multiplicator,
             detalii_articol.id_jtva_coloana,
             detalii_articol.in_stoc,
             detalii_articol.id_gestiune_dest,
             detalii_articol.id_comanda,
             detalii_articol.id_util,
             V_PRET,
             tab_stoc                        (i).pretd,
             tab_stoc                        (i).id_valuta,
             detalii_articol.id_vanzare_set,
             detalii_articol.id_lucrare_rez,
             detalii_articol.id_part_rez,
             pack_facturare.nnumar_act,
             detalii_articol.id_temp);

      END IF;
    END LOOP;
  END transfera_articol;
  -------------------------------------------------------------------
  FUNCTION scrie_nota(V_CANTITATE       IN NUMBER,
                      V_PRET            IN NUMBER,
                      V_DISCOUNT_UNITAR IN NUMBER,
                      V_PRET_ARE_TVA    IN NUMBER,
                      V_ID_VALUTA       IN NUMBER,
                      V_IN_VALUTA       IN NUMBER,
                      V_CURS            IN NUMBER,
                      V_ID_VENCHELT     IN NUMBER,
                      V_ID_SECTIE       IN NUMBER,
                      V_ID_CTR          IN NUMBER,
                      V_EXPLICATIE      IN VARCHAR2,
                      V_SCD             IN VARCHAR2,
                      V_ASCD            IN VARCHAR2,
                      V_SCC             IN VARCHAR2,
                      V_ASCC            IN VARCHAR2,
                      V_ID_GESTIN       IN NUMBER,
                      V_ID_GESTOUT      IN NUMBER,
                      V_ID_SET          IN NUMBER,
                      V_CU_TVA          IN NUMBER, --      0  DACA SE SCRIE SI NOTA DE TVA
                      V_PTVA            IN NUMBER,
                      V_ID_JTVA_COLOANA IN NUMBER,
                      V_TAXCODE         IN NUMBER DEFAULT NULL,
                      V_PAYMENTCODE     IN NUMBER DEFAULT NULL) RETURN NUMBER IS
    V_INCASAT_CALCUL    ACT_TEMP.SUMA%TYPE := 0;
    V_SUMA              ACT_TEMP.SUMA%TYPE;
    V_SUMA_FARA_TVA     ACT_TEMP.SUMA%TYPE;
    V_SUMA_TVA          ACT_TEMP.SUMA%TYPE;
    V_SUMA_CU_TVA       ACT_TEMP.SUMA%TYPE := 0;
    V_SUMA_VAL          ACT_TEMP.SUMA_VAL%TYPE := 0;
    V_SUMA_FARA_TVA_VAL ACT_TEMP.SUMA_VAL%TYPE := 0;
    V_SUMA_TVA_VAL      ACT_TEMP.SUMA_VAL%TYPE := 0;
    V_SUMA_CU_TVA_VAL   ACT_TEMP.SUMA_VAL%TYPE := 0;
    V_PROCENT_TVA       ACT_TEMP.PROC_TVA%TYPE;
    V_ID_FACT           ACT_TEMP.ID_FACT%TYPE := -1;
    V_ZECIMALE_PRET     NUMBER(2);
    V_ZECIMALE_SUMA     NUMBER(2);
    V_SEMN              NUMBER(1) := 1; -- V_SEMN = -1 DACA AM 667 = 4111 (INTRU CU SUMA NEGATIVA SI O PUN POZITIVA,  O LAS NEGATIVA LA SCRIE_TVA)

  BEGIN
    V_PROCENT_TVA := (V_PTVA + 100) / 100;

    IF NVL(V_ID_GESTIN, 0) <> 0 OR NVL(V_ID_GESTOUT, 0) <> 0 THEN
      V_ZECIMALE_PRET := pack_sesiune.nzecimale_preta;
      V_ZECIMALE_SUMA := pack_sesiune.nzecimale_sumaa;
    ELSE
      V_ZECIMALE_PRET := pack_sesiune.nzecimale_pretv;
      V_ZECIMALE_SUMA := pack_sesiune.nzecimale_sumav;
    END IF;

    pack_facturare.calculeaza_sume(V_PRET,
                                   V_DISCOUNT_UNITAR,
                                   V_ZECIMALE_PRET,
                                   V_ZECIMALE_SUMA,
                                   V_CANTITATE,
                                   V_CURS,
                                   V_ID_VALUTA,
                                   V_PROCENT_TVA,
                                   V_PRET_ARE_TVA,
                                   V_SUMA_FARA_TVA,
                                   V_SUMA_TVA,
                                   V_SUMA_CU_TVA);

    IF V_IN_VALUTA = 1 THEN
      pack_facturare.calculeaza_sume(V_PRET,
                                     V_DISCOUNT_UNITAR,
                                     pack_sesiune.nzecimale_pretvval,
                                     pack_sesiune.nzecimale_sumaval,
                                     V_CANTITATE,
                                     1,
                                     V_ID_VALUTA,
                                     V_PROCENT_TVA,
                                     V_PRET_ARE_TVA,
                                     V_SUMA_FARA_TVA_VAL,
                                     V_SUMA_TVA_VAL,
                                     V_SUMA_CU_TVA_VAL);
    END IF;

    IF V_SCD in ('667', '267', '2678', '709') AND
       ((pack_facturare.ntip <= 20 AND pack_facturare.ntip <> 7) or
       pack_facturare.ntip in
       (pack_facturare.nTipFacturaHotel,
         pack_facturare.nTipFacturaRestaurant,
         pack_facturare.nTipNotaPlata)) THEN
      -- FACTURA DISCOUNT ( in afara de credit note )
      V_SEMN := -1;
    END IF;

    IF V_SCD NOT IN ('5311', '5314', '5125', '5121', '5124', '5126') THEN
      IF V_CU_TVA = 0 THEN
        V_INCASAT_CALCUL := V_INCASAT_CALCUL + V_SUMA_FARA_TVA;
      ELSE
        V_INCASAT_CALCUL := V_INCASAT_CALCUL + V_SUMA_CU_TVA;
      END IF;
    END IF;

    IF pack_facturare.ntip = 4 AND pack_facturare.nfactavizcust = 0 THEN
      V_SUMA     := V_SUMA_CU_TVA;
      V_SUMA_VAL := V_SUMA_CU_TVA_VAL;
    ELSE
      V_SUMA     := V_SUMA_FARA_TVA;
      V_SUMA_VAL := V_SUMA_FARA_TVA_VAL;
    END IF;

    /*    IF V_SUMA IS NULL THEN
      RAISE_APPLICATION_ERROR(-20000,
                              V_CANTITATE || ' ; ' || V_PRET || ' ; ' ||
                              V_DISCOUNT_UNITAR || ' ; ' || V_PRET_ARE_TVA ||
                              ' ; ' || V_ID_VALUTA || ' ; ' || V_IN_VALUTA ||
                              ' ; ' || V_CURS || ' ; ' || V_ID_VENCHELT ||
                              ' ; ' || V_ID_SECTIE || ' ; ' || V_EXPLICATIE ||
                              ' ; ' || V_SCD || ' ; ' || V_ASCD || ' ; ' ||
                              V_SCC || ' ; ' || V_ASCC || ' ; ' ||
                              V_ID_GESTIN || ' ; ' || V_ID_GESTOUT || ' ; ' ||
                              V_ID_SET || ' ; ' || V_CU_TVA || ' ; ' ||
                              V_PTVA || ' ; ' || V_ID_JTVA_COLOANA);
    END IF;*/

    -- cursul il completez in cumuleaza_note_act
    pack_facturare.nid_act := pack_facturare.nid_act + 1;

    INSERT INTO ACT_TEMP
      (ID_ACT,
       NRACT,
       SERIE_ACT,
       DATAIREG,
       DATAACT,
       DATASCAD,
       ID_VALUTA,
       ID_VENCHELT,
       ID_LUCRARE,
       ID_SECTIE,
       ID_SET,
       EXPLICATIA,
       SCD,
       ASCD,
       SCC,
       ASCC,
       SUMA,
       SUMA_VAL,
       CURS,
       PROC_TVA,
       ID_FDOC,
       ID_PARTD,
       ID_GESTIN,
       ID_GESTOUT,
       NNIR,
       ID_CTR,
       ID_FACT,
       PERECHEC,
       ID_FACTC,
       ID_PARTC,
       ID_JTVA_COLOANA,
       EXPLICATIA4,
       ID_RESPONSABIL,
       TAXCODE,
       PAYMENTCODE)
    VALUES
      (pack_facturare.nid_act,
       pack_facturare.nnumar_act,
       pack_facturare.cserie_act,
       pack_facturare.ddata_ireg,
       pack_facturare.ddata_act,
       pack_facturare.ddata_scadenta,
       DECODE(V_IN_VALUTA,
              1,
              V_ID_VALUTA,
              pack_facturare.nid_moneda_nationala),
       V_ID_VENCHELT,
       NVL(pack_Facturare.nid_lucrare, 0),
       NVL(V_ID_SECTIE, 0),
       V_ID_SET,
       V_EXPLICATIE,
       V_SCD,
       V_ASCD,
       V_SCC,
       V_ASCC,
       V_SEMN * V_SUMA,
       DECODE(V_IN_VALUTA, 1, V_SEMN * V_SUMA_VAL, 0),
       DECODE(V_IN_VALUTA, 1, V_CURS, 0),
       V_PROCENT_TVA,
       pack_facturare.nid_fdoc,
       (CASE WHEN
        V_SCD LIKE '41%' OR V_SCD LIKE '46%' OR V_SCD LIKE '45%' OR
        (V_SCD = '357' AND pack_facturare.ntip in (42, 47)) OR
        (pack_facturare.ntip = 24 and pack_facturare.nfactavizcust = 1) THEN
        pack_facturare.nid_part WHEN V_SCD = '357' THEN
        pack_facturare.nid_part_rez ELSE 0 END),
       V_ID_GESTIN,
       V_ID_GESTOUT,
       pack_facturare.nnumar_bon,
       V_ID_CTR,
       V_ID_FACT,
       pack_facturare.nperechec,
       pack_facturare.nid_factc,
       DECODE(V_SCC,
              '419',
              pack_facturare.nid_part,
              '4111',
              pack_facturare.nid_part,
              '357',
              pack_facturare.nid_part,
              pack_facturare.nid_partc),
       V_ID_JTVA_COLOANA,
       pack_facturare.cexplicatia4,
       NVL(pack_facturare.nid_responsabil, 0),
       V_TAXCODE,
       V_PAYMENTCODE);

    IF V_CU_TVA = 1 THEN
      IF pack_facturare.nproc_tva_max < V_PTVA THEN
        pack_facturare.nproc_tva_max    := V_PTVA;
        pack_facturare.nid_jtva_coloana := V_ID_JTVA_COLOANA;
        pack_facturare.nTaxCode         := V_TAXCODE;
      END IF;

      pack_facturare.scrie_tva(V_SUMA_TVA,
                               V_SUMA_TVA_VAL,
                               V_ID_VALUTA,
                               V_IN_VALUTA,
                               V_SCD,
                               V_ASCD,
                               V_CURS,
                               V_ID_VENCHELT,
                               V_ID_SECTIE,
                               V_ID_CTR,
                               V_EXPLICATIE,
                               V_PROCENT_TVA,
                               V_ID_FACT,
                               V_ID_JTVA_COLOANA);
    END IF;

    RETURN V_INCASAT_CALCUL;
  END scrie_nota;
  -------------------------------------------------------------------
  PROCEDURE scrie_tva(V_SUMA            IN NUMBER,
                      V_SUMA_VAL        IN NUMBER,
                      V_ID_VALUTA       IN NUMBER,
                      V_IN_VALUTA       IN NUMBER,
                      V_CONT            IN VARCHAR2,
                      V_ACONT           IN VARCHAR2,
                      V_CURS            IN NUMBER,
                      V_ID_VENCHELT     IN NUMBER,
                      V_ID_SECTIE       IN NUMBER,
                      V_ID_CTR          IN NUMBER,
                      V_EXPLICATIE      IN VARCHAR2,
                      V_PTVA            IN NUMBER,
                      V_ID_FACT         IN NUMBER,
                      V_ID_JTVA_COLOANA IN NUMBER) IS
    V_EXPLICATIA ACT_TEMP.EXPLICATIA%TYPE;
    V_SCD        ACT_TEMP.SCD%TYPE;
    V_ASCD       ACT_TEMP.ASCD%TYPE;
    V_SCC        ACT_TEMP.SCC%TYPE;
    V_ID_PARTD   ACT_TEMP.ID_PARTD%TYPE;
    V_ID_TVA     ACT_TEMP.ID_JTVA_COLOANA%TYPE;
    lnAn         ACT_TEMP.AN%TYPE;
    lnLuna       ACT_TEMP.LUNA%TYPE;
    lnTaxCode    ACT_TEMP.TAXCODE%TYPE;
  BEGIN
    CASE
    -- 30.10.2009
    -- 26.08.2009
    --      WHEN V_CONT = '667' AND pack_facturare.ntip <= 20 THEN
      WHEN V_CONT in ('667', '267', '2678', '709') AND
           (pack_facturare.ntip <= 20 or
           pack_facturare.ntip in (pack_facturare.nTipFacturaHotel,
                                    pack_facturare.nTipFacturaRestaurant,
                                    pack_facturare.nTipNotaPlata,
                                    pack_facturare.nTipVanzareRetail,
                                    48,
                                    49,51,52)) THEN
        -- 26.08.2009 ^
        -- 30.10.2009 ^
        -- FACTURA DISCOUNT
        V_SCD      := '4111';
        V_ASCD     := NULL;
        V_SCC      := '4427';
        V_ID_PARTD := pack_facturare.nid_part;
      WHEN pack_facturare.ntip = 4 AND pack_facturare.nfactavizcust = 0 THEN
        -- factura din aviz
        V_SCD      := '4428';
        V_ASCD     := NULL;
        V_SCC      := '4427';
        V_ID_PARTD := 0;

    -- 30.10.2009
    -- 26.08.2009
    --      WHEN pack_facturare.ntip <= 20 OR pack_facturare.ntip in (28, 29) THEN
      WHEN pack_facturare.ntip <= 20 OR
           pack_facturare.ntip in (28,
                                   29,
                                   pack_facturare.nTipFacturaHotel,
                                   pack_facturare.nTipFacturaRestaurant,
                                   pack_facturare.nTipNotaPlata,
                                   pack_facturare.nTipVanzareRetail,
                                   48,
                                   49,51,52) THEN
        -- 26.08.2009 ^
        -- 30.10.2009 ^
        -- factura
        V_SCD      := V_CONT;
        V_ASCD     := V_ACONT;
        V_SCC      := '4427';
        V_ID_PARTD := pack_facturare.nid_part;
      ELSE
        -- aviz
        V_SCD      := '418';
        V_ASCD     := V_ACONT;
        V_SCC      := '4428';
        V_ID_PARTD := pack_facturare.nid_part;
    END CASE;

    BEGIN
      SELECT ID_TVA
        INTO V_ID_TVA
        FROM JTVA_COLOANE
       WHERE ID_JTVA_COLOANA = V_ID_JTVA_COLOANA;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_ID_TVA := NULL;
    END;
    lnAn      := pack_sesiune.getan();
    lnLuna    := pack_sesiune.getluna();
    lnTaxCode := PACK_SAFT.GetTaxCode(lnAn,
                                      lnLuna,
                                      pack_facturare.ddata_act,
                                      V_ID_TVA);

    -- V_EXPLICATIA           := 'TVA ' || V_EXPLICATIE;
    pack_facturare.nid_act := pack_facturare.nid_act + 1;
    INSERT INTO ACT_TEMP
      (ID_ACT,
       NRACT,
       SERIE_ACT,
       DATAIREG,
       DATAACT,
       DATASCAD,
       ID_VALUTA,
       ID_VENCHELT,
       ID_LUCRARE,
       ID_SECTIE,
       ID_SET,
       EXPLICATIA,
       SCD,
       ASCD,
       SCC,
       SUMA,
       SUMA_VAL,
       CURS,
       PROC_TVA,
       ID_CTR,
       ID_FDOC,
       ID_PARTD,
       ID_FACT,
       ID_JTVA_COLOANA,
       EXPLICATIA4,
       ID_RESPONSABIL,
       TAXCODE)
    VALUES
      (pack_facturare.nid_act,
       pack_facturare.nnumar_act,
       pack_facturare.cserie_act,
       pack_facturare.ddata_ireg,
       pack_facturare.ddata_act,
       pack_facturare.ddata_scadenta,
       DECODE(V_IN_VALUTA,
              1,
              V_ID_VALUTA,
              pack_facturare.nid_moneda_nationala),
       V_ID_VENCHELT,
       nvl(pack_facturare.nid_lucrare, 0),
       V_ID_SECTIE,
       pack_facturare.nid_set,
       V_EXPLICATIA,
       V_SCD,
       V_ASCD,
       V_SCC,
       V_SUMA,
       DECODE(V_IN_VALUTA, 1, V_SUMA_VAL, 0),
       DECODE(V_IN_VALUTA, 1, V_CURS, 0),
       V_PTVA,
       V_ID_CTR,
       pack_facturare.nid_fdoc,
       V_ID_PARTD,
       V_ID_FACT,
       V_ID_TVA,
       pack_facturare.cexplicatia4,
       NVL(pack_facturare.nid_responsabil, 0),
       lnTaxCode);
  END;
  -------------------------------------------------------------------
  PROCEDURE scrie_descarcare_k(V_COEFICIENT_K IN NUMBER) IS
    V_ANALITICCUST ACT.ASCC%TYPE;
    V_INCASAT      ACT.SUMA%TYPE;
  BEGIN
    for grup in (SELECT SUM(pack_facturare.calculeaza_total_fara_tva(pret,
                                                                     diferenta,
                                                                     curs,
                                                                     0,
                                                                     0,
                                                                     cantitate,
                                                                     pret_cu_tva,
                                                                     proc_tvav)) as totalfaratvafaradisc,
                        SUM(pack_facturare.calculeaza_total_fara_tva(pret,
                                                                     diferenta,
                                                                     curs,
                                                                     discount_unitar,
                                                                     pack_facturare.ndiscount_evidentiat,
                                                                     cantitate,
                                                                     pret_cu_tva,
                                                                     proc_tvav)) as totalfaratva,
                        SUM(pack_facturare.calculeaza_total_tva(pret,
                                                                diferenta,
                                                                curs,
                                                                discount_unitar,
                                                                pack_facturare.ndiscount_evidentiat,
                                                                cantitate,
                                                                pret_cu_tva,
                                                                proc_tvav)) as totaltva,
                        proc_tvav
                   FROM VANZARI_DETALII_TEMP
                  GROUP BY PROC_TVAV) loop

      V_ANALITICCUST := pack_facturare.getAnaliticK(grup.proc_tvav);

      -- 371-357
      V_INCASAT := pack_facturare.scrie_nota(1,
                                             grup.totalfaratva +
                                             grup.totaltva,
                                             0,
                                             0,
                                             pack_def.getidmonedanationala(),
                                             0,
                                             1,
                                             0,
                                             0,
                                             NULL,
                                             'DESCARCARE CU K',
                                             '371',
                                             V_ANALITICCUST,
                                             '357',
                                             NULL,
                                             0,
                                             0,
                                             pack_facturare.nid_set,
                                             0,
                                             0,
                                             NULL);

      IF pack_facturare.ntip = 48 THEN
        pack_facturare.ncoeficient_k := V_COEFICIENT_K;

        -- 607-371
        V_INCASAT := pack_facturare.scrie_nota(1,
                                               ROUND(grup.totalfaratvafaradisc *
                                                     pack_facturare.ncoeficient_k / 100,
                                                     pack_sesiune.nzecimale_sumav),
                                               0,
                                               0,
                                               pack_def.getidmonedanationala(),
                                               0,
                                               1,
                                               pack_facturare.nid_venchelt,
                                               0,
                                               NULL,
                                               'DESCARCARE CU K',
                                               '607',
                                               NULL,
                                               '371',
                                               V_ANALITICCUST,
                                               0,
                                               0,
                                               pack_facturare.nid_set,
                                               0,
                                               0,
                                               NULL);
        -- 378-371
        V_INCASAT := pack_facturare.scrie_nota(1,
                                               grup.totalfaratva -
                                               ROUND(grup.totalfaratvafaradisc *
                                                     pack_facturare.ncoeficient_k / 100,
                                                     pack_sesiune.nzecimale_sumav),
                                               0,
                                               0,
                                               pack_def.getidmonedanationala(),
                                               0,
                                               1,
                                               0,
                                               0,
                                               NULL,
                                               'DESCARCARE CU K',
                                               '378',
                                               V_ANALITICCUST,
                                               '371',
                                               V_ANALITICCUST,
                                               0,
                                               0,
                                               pack_facturare.nid_set,
                                               0,
                                               0,
                                               NULL);

        -- 4428-371
        V_INCASAT := pack_facturare.scrie_nota(1,
                                               grup.totaltva,
                                               0,
                                               0,
                                               pack_def.getidmonedanationala(),
                                               0,
                                               1,
                                               0,
                                               0,
                                               NULL,
                                               'DESCARCARE CU K',
                                               '4428',
                                               V_ANALITICCUST,
                                               '371',
                                               V_ANALITICCUST,
                                               0,
                                               0,
                                               pack_facturare.nid_set,
                                               0,
                                               0,
                                               NULL);
      END IF;
    end loop;
  END scrie_descarcare_k;
  -------------------------------------------------------------------
  FUNCTION scrie_discount(V_CANTITATE       IN NUMBER,
                          V_DISCOUNT_UNITAR IN NUMBER,
                          V_ID_VALUTA       IN NUMBER,
                          V_IN_VALUTA       IN NUMBER,
                          V_ASCC            IN VARCHAR2,
                          V_ID_SECTIE       IN NUMBER,
                          V_ID_CTR          IN NUMBER,
                          V_CURS            IN NUMBER,
                          V_PROC_TVAV       IN NUMBER,
                          V_PRET_CU_TVA     IN NUMBER,
                          V_CU_TVA          IN NUMBER, -- daca se scrie si nota pentru TVA
                          V_ID_JTVA_COLOANA IN NUMBER,
                          V_TAXCODE         IN NUMBER DEFAULT NULL)
    RETURN NUMBER IS
    V_EXPLICATIA        ACT.EXPLICATIA%TYPE;
    V_SCD               ACT.SCD%TYPE;
    V_ASCD              ACT.ASCD%TYPE;
    V_SCC               ACT.SCC%TYPE;
    V_ID_PARTD          ACT.ID_PARTD%TYPE := 0;
    V_ID_FACT           ACT.ID_FACT%TYPE := -1;
    V_DATA_SCADENTA     ACT.DATASCAD%TYPE;
    V_ID_SET            ACT.ID_SET%TYPE;
    V_SUMA_DISCOUNT     ACT.SUMA%TYPE;
    V_SUMA_TVA_DISCOUNT ACT.SUMA%TYPE;

    V_DISCOUNT_TOTAL_FARA_TVA ACT.SUMA%TYPE := 0;
    V_DISCOUNT_TOTAL_TVA      ACT.SUMA%TYPE := 0;
    V_DISCOUNT_TOTAL_CU_TVA   ACT.SUMA%TYPE := 0;

    V_SUMA_DISCOUNT_VAL           ACT.SUMA_VAL%TYPE;
    V_SUMA_TVA_DISCOUNT_VAL       ACT.SUMA_VAL%TYPE;
    V_DISCOUNT_TOTAL_FARA_TVA_VAL ACT.SUMA_VAL%TYPE := 0;
    V_DISCOUNT_TOTAL_TVA_VAL      ACT.SUMA_VAL%TYPE := 0;
    V_DISCOUNT_TOTAL_CU_TVA_VAL   ACT.SUMA_VAL%TYPE := 0;

  BEGIN
    V_EXPLICATIA := 'DISCOUNT';

    V_ID_SET        := pack_facturare.nid_set;
    V_DATA_SCADENTA := pack_facturare.ddata_scadenta;

    pack_facturare.ddata_scadenta := NULL;
    pack_facturare.nid_set        := pack_facturare.nid_set + 5;

    pack_facturare.calculeaza_sume(V_DISCOUNT_UNITAR,
                                   0,
                                   pack_sesiune.nzecimale_pretv,
                                   pack_sesiune.nzecimale_sumav,
                                   V_CANTITATE,
                                   V_CURS,
                                   V_ID_VALUTA,
                                   V_PROC_TVAV,
                                   V_PRET_CU_TVA,
                                   V_DISCOUNT_TOTAL_FARA_TVA,
                                   V_DISCOUNT_TOTAL_TVA,
                                   V_DISCOUNT_TOTAL_CU_TVA);

    IF V_IN_VALUTA = 1 THEN
      pack_facturare.calculeaza_sume(V_DISCOUNT_UNITAR,
                                     0,
                                     pack_sesiune.nzecimale_pretvval,
                                     pack_sesiune.nzecimale_sumaval,
                                     V_CANTITATE,
                                     1,
                                     V_ID_VALUTA,
                                     V_PROC_TVAV,
                                     V_PRET_CU_TVA,
                                     V_DISCOUNT_TOTAL_FARA_TVA_VAL,
                                     V_DISCOUNT_TOTAL_TVA_VAL,
                                     V_DISCOUNT_TOTAL_CU_TVA_VAL);
    END IF;

    CASE
      WHEN pack_facturare.ntip = 4 THEN
        -- facturare din aviz
        V_SCD               := '4111';
        V_ASCD              := V_ASCC;
        V_SCC               := '418';
        V_SUMA_DISCOUNT     := (-1) * V_DISCOUNT_TOTAL_CU_TVA;
        V_SUMA_TVA_DISCOUNT := (-1) * V_DISCOUNT_TOTAL_TVA;

        V_SUMA_DISCOUNT_VAL     := (-1) * V_DISCOUNT_TOTAL_CU_TVA_VAL;
        V_SUMA_TVA_DISCOUNT_VAL := (-1) * V_DISCOUNT_TOTAL_TVA_VAL;
        V_ID_PARTD              := pack_facturare.nid_part;
      WHEN pack_facturare.ntip <= 20 OR
           pack_facturare.nTip in
           (pack_facturare.nTipFacturaHotel,
            pack_facturare.nTipFacturaRestaurant,
            pack_facturare.nTipNotaPlata,
            pack_facturare.nTipVanzareRetail,
            48,49,51,52) THEN
        -- factura
        V_SCD               := '667';
        V_ASCD              := NULL;
        V_SCC               := '4111';
        V_SUMA_DISCOUNT     := V_DISCOUNT_TOTAL_FARA_TVA;
        V_SUMA_TVA_DISCOUNT := (-1) * V_DISCOUNT_TOTAL_TVA;

        V_SUMA_DISCOUNT_VAL     := V_DISCOUNT_TOTAL_FARA_TVA_VAL;
        V_SUMA_TVA_DISCOUNT_VAL := (-1) * V_DISCOUNT_TOTAL_TVA_VAL;
      ELSE
        -- aviz
        V_SCD               := '667';
        V_ASCD              := NULL;
        V_SCC               := '418';
        V_SUMA_DISCOUNT     := V_DISCOUNT_TOTAL_FARA_TVA;
        V_SUMA_TVA_DISCOUNT := (-1) * V_DISCOUNT_TOTAL_TVA;

        V_SUMA_DISCOUNT_VAL     := V_DISCOUNT_TOTAL_FARA_TVA_VAL;
        V_SUMA_TVA_DISCOUNT_VAL := (-1) * V_DISCOUNT_TOTAL_TVA_VAL;
    END CASE;

    pack_facturare.nid_act := pack_facturare.nid_act + 1;
    INSERT INTO ACT_TEMP
      (ID_ACT,
       NRACT,
       SERIE_ACT,
       DATAIREG,
       DATAACT,
       DATASCAD,
       EXPLICATIA,
       SCD,
       ASCD,
       SCC,
       ASCC,
       SUMA,
       SUMA_VAL,
       CURS,
       ID_SECTIE,
       ID_LUCRARE,
       ID_CTR,
       ID_SET,
       ID_FDOC,
       ID_PARTD,
       ID_PARTC,
       ID_FACT,
       ID_VALUTA,
       PROC_TVA,
       PERECHEC,
       ID_FACTC,
       ID_JTVA_COLOANA,
       EXPLICATIA4,
       ID_RESPONSABIL,
       TAXCODE)
    VALUES
      (pack_facturare.nid_act,
       pack_facturare.nnumar_act,
       pack_facturare.cserie_act,
       pack_facturare.ddata_ireg,
       pack_facturare.ddata_act,
       pack_facturare.ddata_scadenta,
       V_EXPLICATIA,
       V_SCD,
       V_ASCD,
       V_SCC,
       V_ASCC,
       V_SUMA_DISCOUNT,
       DECODE(V_IN_VALUTA, 1, V_SUMA_DISCOUNT_VAL, 0),
       DECODE(V_IN_VALUTA, 1, V_CURS, 0),
       NVL(V_ID_SECTIE, 0),
       NVL(pack_facturare.nid_lucrare, 0),
       V_ID_CTR,
       pack_facturare.nid_set,
       pack_facturare.nid_fdoc,
       V_ID_PARTD,
       pack_facturare.nid_part,
       V_ID_FACT,
       DECODE(V_IN_VALUTA,
              1,
              V_ID_VALUTA,
              pack_facturare.nid_moneda_nationala),
       V_PROC_TVAV,
       pack_facturare.nperechec,
       pack_facturare.nid_factc,
       V_ID_JTVA_COLOANA,
       pack_facturare.cexplicatia4,
       NVL(pack_facturare.nid_responsabil, 0),
       V_TAXCODE);

    IF V_CU_TVA = 1 THEN
      pack_facturare.scrie_tva(V_SUMA_TVA_DISCOUNT,
                               V_SUMA_TVA_DISCOUNT_VAL,
                               V_ID_VALUTA,
                               V_IN_VALUTA,
                               V_SCC,
                               V_ASCC,
                               V_CURS, -- V_CURS,
                               0, -- V_ID_VENCHELT,
                               V_ID_SECTIE,
                               V_ID_CTR,
                               V_EXPLICATIA,
                               V_PROC_TVAV,
                               V_ID_FACT,
                               V_ID_JTVA_COLOANA);
    END IF;
    pack_facturare.nid_set        := V_ID_SET;
    pack_facturare.ddata_scadenta := V_DATA_SCADENTA;
    RETURN V_DISCOUNT_TOTAL_CU_TVA;
  END;
  -------------------------------------------------------------------
  PROCEDURE scrie_avans(V_SUMA IN NUMBER) IS
    /*    V_EXPLICATIA ACT.EXPLICATIA%TYPE;
    V_SCD        ACT.SCD%TYPE := '4111';
    V_SCC        ACT.SCC%TYPE := '419';
    V_SUMA_AVANS ACT.SUMA%TYPE := 0;*/
  BEGIN
    DBMS_OUTPUT.PUT_LINE('Explicatie TVA pentru stornare avans!');
    /*    V_EXPLICATIA := 'STORNARE AVANS';
      IF V_SUMA <> 0 THEN
        V_SUMA_AVANS := pack_facturare.scrie_nota(1,
                                                  V_SUMA,
                                                  1, --   PRET CU TVA
                                                  pack_facturare.v_id_moneda_nationala, -- ID_VALUTA,
                                                  0, -- CURS,
                                                  0, -- ID_VENCHELT
                                                  NULL, -- ID_SECTIE
                                                  V_EXPLICATIA,
                                                  V_SCD,
                                                  '',
                                                  V_SCC,
                                                  '',
                                                  0,
                                                  0,
                                                  V_ID_CONTRACT,
                                                  pack_facturare.v_id_set,
                                                  1,
                                                  pack_facturare.v_proc_tva_max,
                                                  V_ID_JTVA_COLOANA);
    END IF;*/
  END;
  -------------------------------------------------------------------
  Procedure scrie_incasari(V_SERIE_ACT_INCASARE IN VARCHAR2,
                           V_NUMAR_ACT_INCASARE IN NUMBER,
                           V_LISTA_INCASARE     IN VARCHAR2,
                           V_ID_CTR             IN NUMBER) is
    V_INCASAT    ACT.SUMA%TYPE := 0;
    V_TIP        NUMBER(10);
    V_ID_BANCASA ACT.ID_PARTD%TYPE;
    V_ASCC       ACT.ASCC%TYPE;
    V_SCD        ACT.SCD%TYPE;
    V_ASCD       ACT.ASCD%TYPE;
  begin
    pack_facturare.cserie_act_incasare := V_SERIE_ACT_INCASARE;
    pack_facturare.nnumar_act_incasare := V_NUMAR_ACT_INCASARE;
    pack_facturare.ntip_doc_incasare   := pack_facturare.nTipIncasareChitanta;
    pack_facturare.nsuma_incasare      := 0;
    -- daca se incaseaza factura in valuta : trebuie sa trimit si cursul, suma in valuta, id_valuta
    -- V_LISTA_INCASARE : tip|valoare|id_bancasa|ascc|scd|ascd
    -- daca ascc, scd, ascd sunt nule primesc 'xxxx' pentru ca getwordnume nu considera cuvant valoarea NULL si sare peste ea, decaland restul valorilor

    FOR R_INCASARE IN (SELECT X as LINIE_INCASARE
                         FROM table(charc2collection(V_LISTA_INCASARE, ';'))) LOOP
      V_TIP        := TO_NUMBER(getwordnum(R_INCASARE.LINIE_INCASARE,
                                           1,
                                           '|'));
      V_INCASAT    := TO_NUMBER(getwordnum(R_INCASARE.LINIE_INCASARE,
                                           2,
                                           '|'));
      V_ID_BANCASA := TO_NUMBER(getwordnum(R_INCASARE.LINIE_INCASARE,
                                           3,
                                           '|'));

      V_ASCC := getwordnum(R_INCASARE.LINIE_INCASARE, 4, '|');
      -- 11.08.2010
      V_SCD  := getwordnum(R_INCASARE.LINIE_INCASARE, 5, '|');
      V_ASCD := getwordnum(R_INCASARE.LINIE_INCASARE, 6, '|');
      IF lower(V_ASCC) = 'xxxx' THEN
        V_ASCC := NULL;
      END IF;
      IF lower(V_SCD) = 'xxxx' THEN
        V_SCD := NULL;
      END IF;
      IF lower(V_ASCD) = 'xxxx' THEN
        V_ASCD := NULL;
      END IF;

      pack_facturare.nsuma_incasare := pack_facturare.nsuma_incasare +
                                       V_INCASAT;

      IF V_INCASAT <> 0 THEN
        pack_facturare.ntip_doc_incasare := V_TIP;
        if V_TIP not in (pack_facturare.nTipIncasareChitanta,
                         pack_facturare.nTipIncasareCardBancar,
                         pack_facturare.nTipIncasareBonFiscal) then
          pack_facturare.ntip_doc_incasare := pack_facturare.nTipIncasareBonFiscal;
        end if;

        pack_facturare.scrie_incasare2(V_TIP,
                                       V_SERIE_ACT_INCASARE,
                                       V_NUMAR_ACT_INCASARE,
                                       V_ID_CTR,
                                       V_ID_BANCASA,
                                       V_INCASAT,
                                       V_ASCC,
                                       V_SCD,
                                       V_ASCD);
      END IF;
      -- 11.08.2010 ^
    END LOOP;

  end scrie_incasari;
  -------------------------------------------------------------------
  PROCEDURE scrie_incasare2(V_TIP                IN NUMBER,
                            V_SERIE_ACT_INCASARE IN VARCHAR2,
                            V_NUMAR_ACT_INCASARE IN NUMBER,
                            V_ID_CTR             IN NUMBER,
                            V_ID_BANCASA         IN NUMBER,
                            V_SUMA               IN NUMBER,
                            V_ASCC               IN ACT.ASCC%TYPE DEFAULT NULL,
                            V_PSCD               IN ACT.SCD%TYPE DEFAULT NULL,
                            V_ASCD               IN ACT.ASCD%TYPE DEFAULT NULL) IS
    V_EXPLICATIA  VARCHAR2(100);
    V_SCD         VARCHAR2(4);
    V_FDOC        VARCHAR2(20);
    V_PAYMENTCODE ACT_TEMP.PAYMENTCODE%TYPE;
    lcMesaj       varchar2(1000);
    lnD406        number;
  BEGIN
    lnD406 := TO_NUMBER(NVL(pack_sesiune.getoptiunefirma('D406'), '0'));
    -- 11.08.2010
    V_PAYMENTCODE := case lnD406
                       when 1 then
                        nSaftPaymentCodeNumerar
                       else
                        NULL
                     end;
    V_SCD         := V_PSCD;
    CASE
      WHEN V_TIP = nTipIncasareBonFiscal THEN
        V_EXPLICATIA := 'INCASARE NUMERAR';
        V_FDOC       := 'ID_FDOC_BONFISCAL';

        IF V_SCD IS NULL THEN
          V_SCD := PACK_SESIUNE.getoptiunefirma('RF_CONT_INCASARE_BONFISCAL');
        END IF;
        IF V_SCD IS NULL THEN
          V_SCD := '5311';
        END IF;

      WHEN V_TIP = nTipIncasareCardBancar THEN
        V_EXPLICATIA := 'INCASARE CARD BANCAR';
        V_FDOC       := 'ID_FDOC_CARD';

        IF V_SCD IS NULL THEN
          V_SCD := PACK_SESIUNE.getoptiunefirma('RF_CONT_INCASARE_CARDBANCAR');
        END IF;
        IF V_SCD IS NULL THEN
          V_SCD := '5125';
        END IF;
        V_PAYMENTCODE := case lnD406
                           when 1 then
                            nSaftPaymentCodeCard
                           else
                            NULL
                         end;

      WHEN V_TIP = nTipIncasareTichete THEN
        V_EXPLICATIA := 'INCASARE TICHETE';
        V_FDOC       := 'ID_FDOC_BONFISCAL';
        IF V_SCD IS NULL THEN
          V_SCD := PACK_SESIUNE.getoptiunefirma('RF_CONT_INCASARE_TICHETE');
        END IF;
        IF V_SCD IS NULL THEN
          V_SCD := '5323';
        END IF;

      WHEN V_TIP = nTipIncasareChitanta THEN
        V_EXPLICATIA := 'INCASARE NUMERAR';
        V_FDOC       := 'ID_FDOC_CHITANTA';
        IF V_SCD IS NULL THEN
          V_SCD := PACK_SESIUNE.getoptiunefirma('RF_CONT_INCASARE_CHITANTA');
        END IF;
        IF V_SCD IS NULL THEN
          V_SCD := '5311';
        END IF;
    END CASE;
    -- 11.08.2010 ^

    -- select pack_sesiune.getoptiunefirma(user, 'ID_FDOC_BONFISCAL') into lcMesaj from dual;
    /*     pinfo('V_FDOC: ' || V_FDOC || ' VALOARE: ' || V_SUMA,'SCRIE_INCASARE2');*/

    pack_facturare.nid_act := pack_facturare.nid_act + 1;
    INSERT INTO ACT_TEMP
      (ID_ACT,
       SERIE_ACT,
       NRACT,
       DATAIREG,
       DATAACT,
       EXPLICATIA,
       PERECHEC,
       SCD,
       ASCD,
       SCC,
       ASCC,
       SUMA,
       ID_CTR,
       ID_LUCRARE,
       ID_FDOC,
       ID_PARTC,
       ID_PARTD,
       ID_SECTIE,
       ID_VENCHELT,
       ID_RESPONSABIL,
       PAYMENTCODE)
      SELECT pack_facturare.nid_act as ID_ACT,
             V_SERIE_ACT_INCASARE as serie_act,
             V_NUMAR_ACT_INCASARE as nract,
             pack_facturare.ddata_ireg as dataireg,
             pack_facturare.ddata_act as dataact,
             V_EXPLICATIA as explicatia,
             pack_facturare.nnumar_act as perechec,
             V_SCD as scd,
             V_ASCD as ascd, -- 11.08.2010
             SCD as scc,
             CASE
               WHEN V_ASCC IS NULL THEN
                max(ASCD)
               ELSE
                V_ASCC
             END as ascc, -- 03.09.2009 -- 18.05.2010
             V_SUMA as suma,
             V_ID_CTR,
             NVL(pack_facturare.nid_lucrare, 0) as id_lucrare,
             TO_NUMBER(pack_sesiune.getoptiunefirma(user, V_FDOC)) as id_fdoc,
             pack_facturare.nid_part as id_partc,
             V_ID_BANCASA as id_partd,
             MAX(ID_SECTIE) as id_sectie,
             MAX(ID_VENCHELT) as id_venchelt,
             NVL(pack_facturare.nid_responsabil, 0),
             V_PAYMENTCODE
        FROM ACT_TEMP
       WHERE SCD = '4111'
       GROUP BY SCD;

    /*    IF pack_facturare.ntva_incasare = 1 THEN
         INSERT INTO ACT_TEMP(ID_ACT,
           NRACT,
           SERIE_ACT,
           DATAIREG,
           DATAACT,
           ID_VALUTA,
           ID_VENCHELT,
           ID_LUCRARE,
           ID_SECTIE,
           ID_SET,
           EXPLICATIA,
           SCD,
           ASCD,
           SCC,
           ASCC,
           SUMA,
           SUMA_VAL,
           CURS,
           PROC_TVA,
           ID_FDOC,
           NNIR,
           ID_CTR,
           PERECHEC,
           ID_FACT,
           ID_JTVA_COLOANA,
           EXPLICATIA4)
           SELECT pack_facturare.nid_act as ID_ACT,
                 V_NUMAR_ACT_INCASARE as nract,
    V_SERIE_ACT_INCASARE as serie_act,
                 pack_facturare.ddata_ireg as dataireg,
                 pack_facturare.ddata_act as dataact,
           ID_VALUTA,                     -- ??
           ID_VENCHELT,                   -- ??
           NVL(pack_facturare.nid_lucrare, 0) as ID_LUCRARE,
           ID_SECTIE,                      -- ??
           ID_SET,                         -- ??
           V_EXPLICATIA as explicatia,
           SCD,                            -- ??
           ASCD,                           -- ??
           SCC,                            -- ??
           ASCC,                           -- ??
           SUMA,                           -- ??
           SUMA_VAL,                       -- ??
           CURS,                           -- ??
           PROC_TVA,                       -- ??
                        TO_NUMBER(pack_sesiune.getoptiunefirma(user, V_FDOC)) as ID_FDOC,
           NNIR,                           -- ??
           V_ID_CTR as ID_CTR,
    pack_facturare.nnumar_act as perechec,
           -5 AS ID_FACT,
           ID_JTVA_COLOANA,
           EXPLICATIA4
        END IF;*/
  END scrie_incasare2;
  -------------------------------------------------------------------
  PROCEDURE scrie_note_banca_compensari IS
    V_ID_BANCA     NOM_PARTENERI.ID_PART%TYPE := 0;
    V_NUME_BANCA   NOM_PARTENERI.DENUMIRE%TYPE := NULL;
    V_ID_FDOC_COMP NOM_FDOC.ID_FDOC%TYPE := 0;
    V_NUME_FDOC    NOM_FDOC.FEL_DOCUMENT%TYPE := NULL;
  BEGIN
    pack_facturare.citeste_setari_document(0, V_ID_FDOC_COMP, V_NUME_FDOC);
    pack_facturare.citeste_setari_partener(0, V_ID_BANCA, V_NUME_BANCA);

    -- compensare aviz retur
    INSERT INTO ACT_TEMP
      (ID_ACT,
       NRACT,
       SERIE_ACT,
       DATAIREG,
       DATAACT,
       ID_VALUTA,
       ID_SET,
       EXPLICATIA,
       SCD,
       SCC,
       SUMA,
       PROC_TVA,
       ID_JTVA_COLOANA,
       ID_FDOC,
       ID_PARTD,
       PERECHEC,
       ID_PARTC)
      SELECT 9998 as ID_ACT,
             pack_facturare.nnumar_act AS NRACT,
             pack_facturare.cserie_act,
             DATAIREG,
             DATAACT,
             ID_VALUTA,
             MIN(ID_SET) AS ID_SET,
             'COMPENSARE AVIZ ' || pack_facturare.nnumar_act AS EXPLICATIA,
             '5121' AS SCD,
             '418' AS SCC,
             SUM(CASE
                   WHEN SCD in ('667', '267', '2678', '709') THEN
                    (-1) * SUMA
                   ELSE
                    SUMA
                 END) AS SUMA,
             PROC_TVA,
             MAX(CASE
                   WHEN SCC = '4428' THEN
                    0
                   ELSE
                    ID_JTVA_COLOANA
                 END) AS ID_JTVA_COLOANA,
             V_ID_FDOC_COMP AS ID_FDOC,
             V_ID_BANCA AS ID_PARTD,
             pack_facturare.nnumar_act AS PERECHEC,
             pack_facturare.nid_part ID_PARTC
        FROM ACT_TEMP
       WHERE SCD = '418'
          OR (SCD in ('667', '267', '2678', '709') AND SCC = '418')
       GROUP BY DATAIREG, DATAACT, ID_VALUTA, PROC_TVA;

    -- compensari avize - aici
    INSERT INTO ACT_TEMP
      (ID_ACT,
       NRACT,
       SERIE_ACT,
       DATAIREG,
       DATAACT,
       ID_VALUTA,
       ID_VENCHELT,
       ID_SET,
       EXPLICATIA,
       SCD,
       SCC,
       SUMA,
       PROC_TVA,
       ID_JTVA_COLOANA,
       ID_FDOC,
       ID_PARTD,
       PERECHEC,
       ID_FACTC,
       ID_PARTC)
      SELECT 9999 as ID_ACT,
             pack_facturare.nnumar_act AS NRACT,
             pack_facturare.cserie_act,
             to_date(to_char(pack_facturare.ddata_ireg, 'YYYYMMDD'),
                     'YYYYMMDD') AS DATAIREG,
             to_date(to_char(pack_facturare.ddata_act, 'YYYYMMDD'),
                     'YYYYMMDD') AS DATAACT,
             ID_VALUTA,
             ID_VENCHELT,
             pack_Facturare.nid_set AS ID_SET,
             'COMPENSARE AVIZ ' || NRACT AS EXPLICATIA,
             '5121' AS SCD,
             '418' AS SCC,
             debit + precdeb - credit - preccred AS SUMA,
             PROC_TVA,
             ID_JTVA_COLOANA,
             V_ID_FDOC_COMP AS ID_FDOC,
             V_ID_BANCA AS ID_PARTD,
             NRACT AS PERECHEC,
             ID_FACT AS ID_FACTC,
             ID_PART AS ID_PARTC
        FROM IREG_PARTENERI
       WHERE CONT = '418'
         AND COD IN
             (SELECT COD
                FROM VANZARI
               WHERE ID_VANZARE IN
                     (SELECT X AS ID_VANZARE
                        FROM table(cast(CHARN2COLLECTION(pack_facturare.clistaid,
                                                         ',') AS num_tab))))
         AND AN = pack_facturare.nan
         AND LUNA = pack_facturare.nluna
         AND debit + precdeb - credit - preccred > 0;

  END scrie_note_banca_compensari;

  -------------------------------------------------------------------
  /*  PROCEDURE scrie_in_vanzari(V_DISCOUNT_FACTURA IN NUMBER,
                             V_ID_DELEGAT       IN NUMBER,
                             V_ID_MASINA        IN NUMBER,
                             V_DATAORA_EXP      IN DATE,
                             V_ID_AGENT         IN NUMBER,
                             V_TEXT_ADITIONAL   IN VARCHAR2,
                             V_ID_VANZARE       OUT NUMBER) is
    lnIdFacturare      NUMBER(10) := null;
    lnListareDetaliata NUMBER(1) := 0;
  begin
    pack_facturare.scrie_in_vanzari(V_DISCOUNT_FACTURA,
                                    V_ID_DELEGAT,
                                    V_ID_MASINA,
                                    lnIdFacturare,
                                    lnListareDetaliata,
                                    V_DATAORA_EXP,
                                    V_ID_AGENT,
                                    V_TEXT_ADITIONAL,
                                    V_ID_VANZARE);
  end;*/
  -------------------------------------------------------------------
  PROCEDURE scrie_in_vanzari(V_DISCOUNT_FACTURA  IN NUMBER,
                             V_ID_DELEGAT        IN NUMBER,
                             V_ID_MASINA         IN NUMBER,
                             V_ID_FACTURARE      IN NUMBER,
                             V_LISTARE_DETALIATA IN NUMBER,
                             V_DATAORA_EXP       IN DATE,
                             V_ID_AGENT          IN NUMBER,
                             V_TEXT_ADITIONAL    IN VARCHAR2,
                             V_ID_VANZARE        OUT NUMBER) IS

    V_DATAORA          DATE := SYSDATE;
    V_FACTURAT         VANZARI.FACTURAT%TYPE := 0;
    lnDiscountTVA      VANZARI.DISCOUNT_TVA%TYPE;
    lnValoareAchizitie VANZARI.VALOARE_ACHIZITIE%TYPE;
    lnSerieIncasat     ACT.SERIE_ACT%TYPE;
    lnNrIncasat        ACT.NRACT%TYPE;
    lnSumaIncasat      ACT.SUMA%TYPE;
    lnTipIncasat       NUMBER(2);
    lnTotalFaraTVA     VANZARI.TOTAL_FARA_TVA%TYPE;
    lnTotalTVA         VANZARI.TOTAL_TVA%TYPE;
    lnTotalCuTVA       VANZARI.TOTAL_CU_TVA%TYPE;
    lnIdGestiune       VANZARI.ID_GESTIUNE%TYPE;
    lnIdValuta         VANZARI.Id_Valuta%TYPE;
    lnCurs             VANZARI.CURS%TYPE;
    lnMultiplicator    VANZARI.MULTIPLICATOR%TYPE;
    lnIdCtr            VANZARI.ID_CTR%TYPE;
    lnValVal           VANZARI.VALVAL%TYPE;
    lnTVAVal           VANZARI.TVAVAL%TYPE;
    lnTotVal           VANZARI.TOTVAL%TYPE;
    lnPrecizieCalcul   NUMBER(2) := pack_sesiune.getOptiuneFirma('PC');
    lnPreciziePretV    NUMBER(2) := pack_sesiune.getOptiuneFirma('PPRETV');

  BEGIN
    -- 1 - 20 facturi, 51 factura ACN
    -- 21 - 42, 47 avize
    -- -6 retur transfer din gestiune valorica (ROAGEST)
    -- -7 retur aviz ( valoric ) ( ROAGEST )
    -- -8 retur factura ( valorica ) ( ROAGEST )
    -- -13 retur marfa din custodie ( valoric ) ( ROAFACTURARE )
    IF pack_facturare.ntip between 1 and 20 OR
       pack_facturare.ntip in (-8,
                               pack_facturare.nTipFacturaHotel,
                               pack_facturare.nTipFacturaRestaurant,
                               -12,
                               48,
                               49,
                               51,
                               52) THEN
      V_FACTURAT := 1;
    ELSE
      V_FACTURAT := 0;
    END IF;

    IF pack_facturare.ntip in (2,6,52) THEN
      SELECT NVL(MAX(AFISARE_SCADENTA), 1)
        INTO pack_facturare.nafisare_scadenta
        FROM VCONTRACTE
       WHERE ID_CTR IN (SELECT DISTINCT ID_CTR FROM VANZARI_DETALII_TEMP);
    ELSE
      pack_facturare.nafisare_scadenta := 1;
    END IF;

    -- 23,25,27,30 ROAFACTURARE aviz transfer catre subunitati
    -- 41 = ROAFACTURARE retur transfer catre subunitati;
    -- 6 = ROAGEST retur de la gestiune valorica
    if pack_facturare.ntip in (23, 25, 27, 30, 41) then
      lnIdGestiune := pack_facturare.v_date_gestiune.id_gestiune;
    elsif pack_facturare.ntip = -6 then
      lnIdGestiune := pack_facturare.nid_gestiune_sursa;
    end if;

    if (lnIdGestiune is null and pack_facturare.ntip in (41, -6)) then
      select min(id_gestiune) into lnIdGestiune from vanzari_detalii_temp;
    end if;

    -- completez vanzari.id_ctr, ca sa nu mai caut in vanzari_detalii
    SELECT MAX(ID_CTR) INTO lnIdCtr FROM VANZARI_DETALII_TEMP;

    -- nu stiu de ce nu se completa id_valuta, curs, multiplicator mai jos
    -- il selectez direct din vanzari_detalii_temp, in loc de vanzari_cursuri
    SELECT max(ID_VALUTA), max(CURS), max(MULTIPLICATOR)
      INTO lnIdValuta, lnCurs, lnMultiplicator
      FROM (SELECT DISTINCT ID_VALUTA, CURS, MULTIPLICATOR
              FROM VANZARI_DETALII_TEMP
             WHERE ID_VALUTA <> pack_facturare.nid_moneda_nationala)
     WHERE ROWNUM = 1;

    INSERT INTO VANZARI
      (id_util,
       dataora,
       id_part,
       numar_act,
       serie_act,
       facturat,
       data_facturat,
       id_utilfact,
       id_lucrare,
       id_comanda,
       tip,
       id_gestiune,
       cod,
       id_delegat,
       id_masina,
       id_facturare,
       listare_detaliata,
       dataora_exp,
       data_act,
       discount,
       discount_evidentiat,
       id_agent,
       text_aditional,
       in_valuta,
       id_sucursala,
       id_sectie,
       diftotftva,
       diftottva,
       afisare_scadenta,
       coeficient_k,
       id_ordl,
       tva_incasare,
       data_scad,
       id_ctr,
       tip_factura,
       id_beneficiar,
       tip_saft)
    VALUES
      (pack_facturare.nid_util,
       V_DATAORA,
       DECODE(pack_facturare.ntip,
              23,
              NULL,
              25,
              NULL,
              41,
              NULL,
              -6,
              NULL,
              pack_facturare.nid_part),
       pack_facturare.nnumar_act,
       pack_facturare.cserie_act,
       V_FACTURAT,
       (CASE WHEN
        pack_facturare.ntip < 20 OR
        pack_facturare.ntip in (pack_facturare.nTipFacturaHotel,
                                pack_facturare.nTipFacturaRestaurant,
                                -12,
                                48,
                                49,51,52) THEN V_DATAORA ELSE NULL END),
       (CASE WHEN
        pack_facturare.ntip < 20 OR
        pack_facturare.ntip in (pack_facturare.nTipFacturaHotel,
                                pack_facturare.nTipFacturaRestaurant,
                                -12,
                                48,
                                49,51,52) THEN pack_facturare.nid_util ELSE NULL END),
       pack_facturare.nid_lucrare,
       pack_facturare.nid_comanda,
       pack_facturare.ntip,
       lnIdGestiune,
       pack_contafin.get_cod(),
       V_ID_DELEGAT,
       V_ID_MASINA,
       V_ID_FACTURARE,
       V_LISTARE_DETALIATA,
       V_DATAORA_EXP,
       pack_facturare.ddata_act,
       V_DISCOUNT_FACTURA,
       pack_facturare.ndiscount_evidentiat,
       V_ID_AGENT,
       REPLACE(V_TEXT_ADITIONAL, CHR(170), CHR(13) || CHR(10)),
       pack_facturare.nin_valuta,
       pack_facturare.nid_sucursala,
       pack_facturare.nid_sectie_stoc,
       pack_facturare.ndifftva,
       pack_facturare.ndiftva,
       pack_facturare.nafisare_scadenta,
       pack_facturare.ncoeficient_k,
       pack_facturare.nid_ordl,
       pack_facturare.ntva_incasare,
       pack_facturare.ddata_scadenta,
       lnIdCtr,
       pack_facturare.ntip_factura,
       pack_facturare.nid_beneficiar,
       pack_facturare.ntip_saft)
    RETURNING ID_VANZARE INTO V_ID_VANZARE;

    pack_facturare.scrie_cursuri(V_ID_VANZARE);

    pack_facturare.scrie_seturi;

    INSERT /*+ APPEND */
    INTO VANZARI_DETALII
      (ID_VANZARE,
       ID_ARTICOL,
       LOT,
       SERIE,
       ID_RATA,
       ID_POL,
       CANTITATE,
       PRET_ACHIZITIE,
       PRETD,
       ID_VALUTAD,
       PRET,
       PROC_TVAV,
       ID_JTVA_COLOANA,
       ID_JTVA_COLOANA_EX,
       DISCOUNT_UNITAR,
       ID_GESTIUNE,
       ID_VALUTA,
       CONT,
       EXPLICATIE,
       PRET_CU_TVA,
       DIFERENTA,
       CUSTODIE,
       ID_VANZARE_SET,
       ID_CTR,
       TAXCODE)
      SELECT V_ID_VANZARE as ID_VANZARE,
             ID_ARTICOL,
             LOT,
             SERIE,
             ID_RATA,
             ID_POL,
             CANTITATE,
             PRET_ACHIZITIE,
             PRETD,
             ID_VALUTAD,
             PRET,
             PROC_TVAV,
             ID_JTVA_COLOANA,
             ID_JTVA_COLOANA_EX,
             DISCOUNT_UNITAR,
             ID_GESTIUNE,
             ID_VALUTA,
             CONT,
             EXPLICATIA,
             PRET_CU_TVA,
             DIFERENTA,
             CUSTODIE,
             DECODE(ID_VANZARE_SET, 0, NULL, ID_VANZARE_SET),
             ID_CTR,
             TAXCODE
        FROM VANZARI_DETALII_TEMP;

    -- Completare totaluri in vanzari din vanzari_detalii pentru a usura selectiile din fact_vfacturi
    begin
      select DISC_TVA_VAL AS DISCOUNT_TVA,
             VALOARE_ACHIZITIE,
             a.suma_fara_tva_ron - a.disc_fara_tva_ron as TOTAL_FARA_TVA,
             a.suma_tva_ron - a.disc_tva_ron as TOTAL_TVA,
             a.suma_fara_tva_ron - a.disc_fara_tva_ron + a.suma_tva_ron -
             a.disc_tva_ron as TOTAL_CU_TVA,
             a.suma_fara_tva_val - a.disc_fara_tva_val as VALVAL,
             a.suma_tva_val - a.disc_tva_val as TVAVAL,
             a.suma_fara_tva_val - a.disc_fara_tva_val + a.suma_tva_val -
             a.disc_tva_val as TOTVAL,
             id_valuta,
             curs,
             multiplicator,
             pack_facturare.cserie_act_incasare as SERIE_INCASAT,
             pack_facturare.nnumar_act_incasare as NR_INCASAT,
             pack_facturare.nsuma_incasare AS SUMA_INCASAT,
             pack_facturare.ntip_doc_incasare as TIP_INCASAT
        INTO lnDiscountTVA,
             lnValoareAchizitie,
             lnTotalFaraTVA,
             lnTotalTVA,
             lnTotalCuTVA,
             lnValVal,
             lnTVAVal,
             lnTotVal,
             lnIdValuta,
             lnCurs,
             lnMultiplicator,
             lnSerieIncasat,
             lnNrIncasat,
             lnSumaIncasat,
             lnTipIncasat
        FROM (select MAX(decode(pack_facturare.nin_valuta,
                                1,
                                ROUND(a1.curs * NVL(V_DISCOUNT_FACTURA, 0) /
                                      a1.multiplicator,
                                      lnPreciziePretV),
                                NVL(V_DISCOUNT_FACTURA, 0))) as DISC_FARA_TVA_RON,
                     NVL(V_DISCOUNT_FACTURA, 0) as DISC_FARA_TVA_VAL,
                     pack_facturare.nin_valuta AS IN_VALUTA,
                     MAX(ROUND(decode(pack_facturare.nin_valuta,
                                      1,
                                      ROUND(a1.curs *
                                            NVL(V_DISCOUNT_FACTURA, 0) /
                                            a1.multiplicator,
                                            lnPreciziePretV),
                                      NVL(V_DISCOUNT_FACTURA, 0)) *
                               (a1.proc_tvav - 1),
                               lnPreciziePretV)) as DISC_TVA_RON,
                     MAX(ROUND(NVL(V_DISCOUNT_FACTURA, 0) *
                               (a1.proc_tvav - 1),
                               lnPreciziePretV)) as DISC_TVA_VAL,
                     sum(pack_facturare.calculeaza_total_fara_tva_fact(a1.pret_ron,
                                                                       0,
                                                                       1,
                                                                       NVL(a1.discount_unitar_ron,
                                                                           0),
                                                                       pack_facturare.ndiscount_evidentiat,
                                                                       a1.cantitate,
                                                                       a1.pret_cu_tva,
                                                                       a1.proc_tvav)) AS SUMA_FARA_TVA_RON,
                     sum(pack_facturare.calculeaza_total_tva_fact(a1.pret_ron,
                                                                  0,
                                                                  1,
                                                                  NVL(a1.discount_unitar_ron,
                                                                      0),
                                                                  pack_facturare.ndiscount_evidentiat,
                                                                  a1.cantitate,
                                                                  a1.pret_cu_tva,
                                                                  a1.proc_tvav)) as SUMA_TVA_RON,
                     sum(pack_facturare.calculeaza_total_fara_tva_fact(a1.pret_val,
                                                                       0,
                                                                       1,
                                                                       NVL(a1.discount_unitar_val,
                                                                           0),
                                                                       pack_facturare.ndiscount_evidentiat,
                                                                       a1.cantitate,
                                                                       a1.pret_cu_tva,
                                                                       a1.proc_tvav)) AS SUMA_FARA_TVA_VAL,
                     sum(pack_facturare.calculeaza_total_tva_fact(a1.pret_val,
                                                                  0,
                                                                  1,
                                                                  NVL(a1.discount_unitar_val,
                                                                      0),
                                                                  pack_facturare.ndiscount_evidentiat,
                                                                  a1.cantitate,
                                                                  a1.pret_cu_tva,
                                                                  a1.proc_tvav)) as SUMA_TVA_VAL,
                     sum(round(a1.cantitate * a1.pret_achizitie,
                               lnPrecizieCalcul)) AS VALOARE_ACHIZITIE,
                     max(a1.id_valuta) as id_valuta,
                     max(a1.curs) as curs,
                     max(a1.multiplicator) as multiplicator
                from (select vd.id_vanzare_set,
                             (case
                               when (pack_facturare.nin_valuta = 1 or
                                    vd.id_valuta <>
                                    pack_def.GetIdMonedaNationala()) then
                                ROUND(vc.curs * vd.pret / vc.multiplicator,
                                      lnPreciziePretV)
                               else
                                vd.pret
                             end) as pret_ron,
                             vd.pret as pret_val,
                             vd.proc_tvav,
                             vd.cantitate,
                             vd.diferenta,
                             (case
                               when (pack_facturare.nin_valuta = 1 or
                                    vd.id_valuta <>
                                    pack_def.GetIdMonedaNationala()) then
                                ROUND(vc.curs * vd.discount_unitar /
                                      vc.multiplicator,
                                      lnPreciziePretV)
                               else
                                vd.discount_unitar
                             end) as discount_unitar_ron,
                             vd.discount_unitar as discount_unitar_val,
                             vd.id_valuta,
                             vd.pret_cu_tva,
                             vd.pret_achizitie,
                             vc.curs,
                             vc.multiplicator
                        from (select a.id_vanzare_set,
                                     a.pret,
                                     a.proc_tvav,
                                     a.cantitate,
                                     a.diferenta,
                                     a.discount_unitar,
                                     a.id_valuta,
                                     a.pret_cu_tva,
                                     a.pret_achizitie
                                from VANZARI_DETALII_TEMP a
                               where nvl(a.id_vanzare_set, 0) = 0
                              union all
                              select b.id_vanzare_set,
                                     b.pret,
                                     max(c.proc_tvav) as proc_tvav,
                                     b.cantitate,
                                     0 as diferenta,
                                     b.discount_unitar,
                                     decode(pack_facturare.nin_valuta,
                                            0,
                                            pack_def.GetIdMonedaNationala(),
                                            c.id_valuta) as id_valuta,
                                     b.pret_cu_tva,
                                     sum(decode(b.cantitate,
                                                0,
                                                0,
                                                c.pret_achizitie * c.cantitate /
                                                b.cantitate)) as pret_achizitie
                                from vanzari_detalii_temp c
                                left join vanzari_seturi b
                                  on b.id_vanzare_set = c.id_vanzare_set
                               where nvl(c.id_vanzare_set, 0) <> 0
                                 and nvl(pack_facturare.nin_valuta, -1) > -1
                               group by b.id_vanzare_set,
                                        b.pret,
                                        b.cantitate,
                                        b.discount_unitar,
                                        b.pret_cu_tva,
                                        decode(pack_facturare.nin_valuta,
                                               0,
                                               pack_def.GetIdMonedaNationala(),
                                               c.id_valuta)) vd
                        left join vanzari_cursuri vc
                          on vc.id_vanzare = V_ID_VANZARE
                         and vd.id_valuta = vc.id_valuta) a1) a;

      update vanzari
         set discount_tva      = lnDiscountTVA,
             valoare_achizitie = lnValoareAchizitie,
             total_fara_tva    = lnTotalFaraTVA,
             total_tva         = lnTotalTVA,
             total_cu_tva      = lnTotalCuTVA,
             valval            = lnValVal,
             tvaval            = lnTVAVal,
             totval            = lnTotVal,
             id_valuta         = lnIdValuta,
             curs              = lnCurs,
             multiplicator     = lnMultiplicator,
             serie_incasat     = lnSerieIncasat,
             nr_incasat        = lnNrIncasat,
             suma_incasat      = lnSumaIncasat,
             tip_incasat       = lnTipIncasat
       where id_vanzare = V_ID_VANZARE;

    exception
      when NO_DATA_FOUND then
        null;
    end;

  END scrie_in_vanzari;
  -------------------------------------------------------------------
  PROCEDURE scrie_atasamente_factura(V_ID_VANZARE IN NUMBER,
                                     V_NUME_FRX   IN NUMBER,
                                     V_WDOCUMENT  IN BLOB,
                                     V_ID_UTIL    IN NUMBER) IS
    V_TIP    NUMBER(2) := 0;
    V_FORMAT ATASAMENTE_VANZARI.FORMAT%TYPE;
  begin
    CASE
      WHEN UPPER(TRIM(V_NUME_FRX)) LIKE 'FACTURA_VAL' THEN
        V_TIP := 2;
      WHEN UPPER(TRIM(V_NUME_FRX)) LIKE 'FACTURA%' THEN
        V_TIP := 1;
      WHEN UPPER(TRIM(V_NUME_FRX)) LIKE 'INVOICE%' THEN
        V_TIP := 3;
      WHEN UPPER(TRIM(V_NUME_FRX)) = 'RECAPITULATIE' THEN
        V_TIP := 4;
      WHEN UPPER(TRIM(V_NUME_FRX)) LIKE 'AVIZ%' THEN
        V_TIP := 5;
    END CASE;

    IF UPPER(TRIM(V_NUME_FRX)) LIKE '%_A5' THEN
      V_FORMAT := 'A5';
    ELSE
      V_FORMAT := 'A4';
    END IF;

    IF V_WDOCUMENT IS NOT NULL THEN
      INSERT INTO ATASAMENTE_VANZARI
        (ID_VANZARE, TIP, FORMAT, DOCUMENT)
      VALUES
        (V_ID_VANZARE, V_TIP, V_FORMAT, V_WDOCUMENT);
    END IF;

  end scrie_atasamente_factura;
  -------------------------------------------------------------------
  PROCEDURE scrie_seturi is
    cursor crs is
      SELECT * FROM VANZARI_SETURI_TEMP;
    linie_crs crs%ROWTYPE;
    V_ID      VANZARI_SETURI.ID_VANZARE_SET%TYPE;
  begin
    open crs;
    loop
      fetch crs
        into linie_crs;
      exit when crs%NOTFOUND;
      INSERT INTO VANZARI_SETURI
        (DENUMIRE,
         EXPLICATIE,
         CANTITATE,
         UM,
         SERIE,
         PRET,
         DISCOUNT_UNITAR,
         PRET_CU_TVA)
      VALUES
        (linie_crs.denumire,
         linie_crs.explicatie,
         linie_crs.cantitate,
         linie_crs.um,
         linie_crs.serie,
         linie_crs.pret,
         linie_crs.discount_unitar,
         linie_crs.pret_cu_tva)
      RETURNING ID_VANZARE_SET INTO V_ID;
      UPDATE VANZARI_DETALII_TEMP
         SET ID_VANZARE_SET = V_ID
       WHERE ID_VANZARE_SET = linie_crs.id_vanzare_set;
    end loop;
    close crs;
  end scrie_seturi;
  -------------------------------------------------------------------
  PROCEDURE scrie_seturi_proforma is
    cursor crs is
      SELECT * FROM VANZARI_SETURI_TEMP;
    linie_crs crs%ROWTYPE;
    V_ID      NUMBER(10);
  begin
    open crs;
    loop
      fetch crs
        into linie_crs;
      exit when crs%NOTFOUND;
      INSERT INTO PROFORME_SETURI
        (DENUMIRE,
         EXPLICATIE,
         CANTITATE,
         UM,
         SERIE,
         PRET,
         DISCOUNT_UNITAR,
         PRET_CU_TVA)
      VALUES
        (linie_crs.denumire,
         linie_crs.explicatie,
         linie_crs.cantitate,
         linie_crs.um,
         linie_crs.serie,
         linie_crs.pret,
         linie_crs.discount_unitar,
         linie_crs.pret_cu_tva)
      RETURNING ID_PROFORMA_SET INTO V_ID;
      UPDATE VANZARI_DETALII_TEMP
         SET ID_VANZARE_SET = V_ID
       WHERE ID_VANZARE_SET = linie_crs.id_vanzare_set;
    end loop;
    close crs;

  end scrie_seturi_proforma;
  -------------------------------------------------------------------
  PROCEDURE initializeaza_part_rez(V_ID_PART IN NUMBER) IS
  BEGIN
    pack_facturare.nid_part_rez := V_ID_PART;
  END initializeaza_part_rez;
  -------------------------------------------------------------------
  PROCEDURE cumuleaza_note_act IS
    lnInregSumaNot0 NUMBER(10) := 0;
  BEGIN
    -- era apelata la sfarsitul procedurii
    pack_contafin.completeaza_parteneri_gestiuni();
    --IF pack_facturare.ntip = 27 THEN
    pack_contafin.completeaza_analitice();
    --END IF;
    UPDATE ACT_TEMP
       SET STERS = 1, TVA_INCASARE = pack_facturare.ntva_incasare;

    -- verific daca am vreo inregistrare cu suma <> 0
    -- sterg inregistrarile cu suma 0 doar daca am inregistrari cu suma <> 0
    -- altfel nu se inregistreaza nimic in Reg. Jurnal ACT, doar in RUL si VANZARI/VANZARI_DETALII
    SELECT COUNT(*) INTO lnInregSumaNot0 from ACT_TEMP where SUMA <> 0;
    if lnInregSumaNot0 > 0 then
      DELETE FROM ACT_TEMP
       WHERE SUMA = 0
         AND NOT ((SCD LIKE '4%' OR SCC LIKE '4%') AND SCC NOT LIKE '4427')
         AND ID_SET <> 32003;
    end if;

    pack_facturare.cumuleaza_note_act_temp();

    if pack_facturare.ntip <> pack_facturare.nTipNotaPlata then
      pack_facturare.verifica_total_document();
    end if;
    pack_contafin.sterge_analitice_gresite();

    if pack_facturare.ntip in
       (pack_facturare.nTipFacturaRestaurant, pack_facturare.nTipNotaPlata) then
      pack_contafin.completeaza_analitice_rul();
      pack_contafin.sterge_analitice_gresite_rul();
    end if;

    pack_facturare.adauga_note_tva_incasare();

  END cumuleaza_note_act;
  -----------------------------------------------------------
  PROCEDURE cumuleaza_note_act_temp IS
    lcMesaj xmltype;
  BEGIN

    /*    SELECT DBMS_XMLGEN.GETXMLTYPE('SELECT * FROM ACT_TEMP') INTO lcMesaj FROM DUAL;

    PINFO(lcMesaj.getClobVal(), 'cumuleaza_note_act_temp');*/

    UPDATE ACT_TEMP SET STERS = 1, ID_ACT = ROWNUM;

    INSERT INTO ACT_TEMP
      (ID_ACT,
       LUNA,
       AN,
       COD,
       DATAIREG,
       NRACT,
       SERIE_ACT,
       DATAACT,
       EXPLICATIA,
       SCD,
       ASCD,
       SCC,
       ASCC,
       SUMA,
       SUMA_VAL,
       CURS,
       PERECHED,
       PERECHEC,
       DATASCAD,
       NEIMPOZAB,
       NNIR,
       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_FACTD,
       ID_FACTC,
       ID_CTR,
       ID_JTVA_COLOANA,
       TVA_INCASARE,
       TAXCODE,
       PAYMENTCODE)
      SELECT ROW_NUMBER() OVER(ORDER BY A.ID_SET, A.ID_ACT) AS ID_ACT,
             A.LUNA,
             A.AN,
             A.COD,
             A.DATAIREG,
             A.NRACT,
             A.SERIE_ACT,
             A.DATAACT,
             A.EXPLICATIA,
             A.SCD,
             A.ASCD,
             A.SCC,
             A.ASCC,
             (CASE
               WHEN pack_facturare.nin_valuta = 1 AND
                    A.ID_VALUTA NOT IN
                    (0, pack_facturare.nid_moneda_nationala) THEN
                ROUND(A.SUMA_VAL * A.CURS, pack_sesiune.nzecimale_sumav)
               ELSE
                A.SUMA
             END) AS SUMA,
             A.SUMA_VAL,
             A.CURS,
             A.PERECHED,
             A.PERECHEC,
             A.DATASCAD,
             A.NEIMPOZAB,
             A.NNIR,
             A.ID_RESPONSABIL,
             A.ID_VENCHELT,
             A.ID_SECTIE,
             pack_facturare.nid_set AS ID_SET,
             A.ID_FACT,
             A.ID_PARTD,
             A.ID_PARTC,
             A.ID_SUCURSALA,
             A.ID_FDOC,
             A.EXPLICATIA4,
             A.EXPLICATIA5,
             A.ID_LUCRARE,
             A.ID_GESTIN,
             A.ID_GESTOUT,
             A.ID_VALUTA,
             A.PROC_TVA,
             A.ID_FACTD,
             A.ID_FACTC,
             A.ID_CTR,
             A.ID_JTVA_COLOANA,
             pack_facturare.ntva_incasare as TVA_INCASARE,
             A.TAXCODE,
             A.PAYMENTCODE
        FROM (SELECT MIN(A11.ID_ACT) AS ID_ACT,
                     A11.ID_SET,
                     A11.LUNA,
                     A11.AN,
                     A11.COD,
                     A11.DATAIREG,
                     A11.NRACT,
                     A11.SERIE_ACT,
                     A11.DATAACT,
                     A11.EXPLICATIA,
                     A11.SCD,
                     A11.ASCD,
                     A11.SCC,
                     A11.ASCC,
                     SUM(A11.SUMA) AS SUMA,
                     SUM(A11.SUMA_VAL) AS SUMA_VAL,
                     A11.CURS,
                     A11.PERECHED,
                     A11.PERECHEC,
                     A11.DATASCAD,
                     A11.NEIMPOZAB,
                     A11.NNIR,
                     DECODE(pack_facturare.ntip,
                            2,
                            NVL(A12.ID_RESPONSABIL, A11.ID_RESPONSABIL),
                            A11.ID_RESPONSABIL) AS ID_RESPONSABIL,
                     A11.ID_VENCHELT,
                     A11.ID_SECTIE,
                     A11.ID_FACT,
                     A11.ID_PARTD,
                     A11.ID_PARTC,
                     A11.ID_SUCURSALA,
                     A11.ID_FDOC,
                     A11.EXPLICATIA4,
                     A11.EXPLICATIA5,
                     A11.ID_LUCRARE,
                     A11.ID_GESTIN,
                     A11.ID_GESTOUT,
                     A11.ID_VALUTA,
                     (CASE
                       WHEN A11.ID_JTVA_COLOANA IS NOT NULL THEN
                        A11.PROC_TVA
                       ELSE
                        0
                     END) AS PROC_TVA,
                     A11.ID_FACTD,
                     A11.ID_FACTC,
                     A11.ID_CTR,
                     A11.ID_JTVA_COLOANA,
                     A11.TAXCODE,
                     A11.PAYMENTCODE
                FROM ACT_TEMP A11
                LEFT JOIN CONTRACTE A12
                  ON A11.ID_CTR = A12.ID_CTR
               GROUP BY A11.LUNA,
                        A11.AN,
                        A11.COD,
                        A11.DATAIREG,
                        A11.SERIE_ACT,
                        A11.NRACT,
                        A11.DATAACT,
                        A11.EXPLICATIA,
                        A11.SCD,
                        A11.ASCD,
                        A11.SCC,
                        A11.ASCC,
                        A11.PERECHED,
                        A11.PERECHEC,
                        A11.DATASCAD,
                        A11.NEIMPOZAB,
                        A11.NNIR,
                        A11.ID_SET,
                        DECODE(pack_facturare.ntip,
                               2,
                               NVL(A12.ID_RESPONSABIL, A11.ID_RESPONSABIL),
                               A11.ID_RESPONSABIL),
                        A11.ID_VENCHELT,
                        A11.ID_SECTIE,
                        A11.ID_SET,
                        A11.ID_FACT,
                        A11.ID_PARTD,
                        A11.ID_PARTC,
                        A11.ID_SUCURSALA,
                        A11.ID_FDOC,
                        A11.EXPLICATIA4,
                        A11.EXPLICATIA5,
                        A11.ID_LUCRARE,
                        A11.ID_GESTIN,
                        A11.ID_GESTOUT,
                        A11.ID_VALUTA,
                        A11.CURS,
                        A11.PROC_TVA,
                        A11.ID_FACTD,
                        A11.ID_FACTC,
                        A11.ID_CTR,
                        A11.ID_JTVA_COLOANA,
                        A11.TAXCODE,
                        A11.PAYMENTCODE) A;

    /*    SELECT DBMS_XMLGEN.GETXMLTYPE('SELECT * FROM ACT_TEMP') INTO lcMesaj FROM DUAL;
        PINFO(lcMesaj.getClobVal(), 'cumuleaza_note_act_temp');
    */

    DELETE FROM ACT_TEMP WHERE STERS = 1;
  END cumuleaza_note_act_temp;
  -------------------------------------------------------------------
  PROCEDURE cursor_verificare(V_CURSOR_VERIFICARE OUT pack_facturare.cursor_facturare) 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,
             jtva.denumire as explicatie_tva,
             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,
             a.taxcode,
             st.descriere as saft_descriere,
             st.procent_taxa as saft_procent,
             st.taxname,
             a.paymentcode,
             sp.methodcode,
             sp.name as paymentmechanism,
             sp.methodname as paymentmethod,
             sp.paymentname
        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
        LEFT JOIN jtva_coloane jtva
          ON a.id_jtva_coloana = jtva.id_jtva_coloana
        left join vsaft_taxtable st
          on a.taxcode = st.taxcode
        left join vsaft_mecanisme_plati sp
          on a.paymentcode = sp.mechanismcode
       WHERE A.STERS = 0;
  END cursor_verificare;
  -------------------------------------------------------------------
  -- MODIFICA NUMAR, DATA, DATA_SCAD si in restul tabelelor
  -------------------------------------------------------------------
  PROCEDURE modifica_date_factura(V_ID_VANZARE        IN NUMBER,
                                  V_ID_RUTA           IN NUMBER,
                                  V_ID_DELEGAT        IN NUMBER,
                                  V_ID_AGENT          IN NUMBER,
                                  V_ID_MASINA         IN NUMBER,
                                  V_DATAORA_EXP       IN DATE,
                                  V_ID_FACTURARE      IN VANZARI.ID_FACTURARE%TYPE,
                                  V_LISTARE_DETALIATA IN VANZARI.LISTARE_DETALIATA%TYPE,
                                  V_TEXT_ADITIONAL    IN VANZARI.TEXT_ADITIONAL%TYPE,
                                  V_TIP_SAFT          IN VANZARI.TIP_SAFT%TYPE DEFAULT NULL,
                                  V_EFACTURA          IN VANZARI.EFACTURA%TYPE DEFAULT NULL,
                                  V_DATA_ACT          IN VANZARI.DATA_ACT%TYPE DEFAULT NULL,
                                  V_DATA_SCAD         IN VANZARI.DATA_SCAD%TYPE DEFAULT NULL,
                                  V_NUMAR_ACT         IN VANZARI.NUMAR_ACT%TYPE DEFAULT NULL,
                                  V_SERIE_ACT         IN VANZARI.SERIE_ACT%TYPE DEFAULT NULL) IS
    lnIdFact VANZARI.ID_FACT%TYPE;
    lnNrAct VANZARI.NUMAR_ACT%TYPE;
    ldDataAct VANZARI.DATA_ACT%TYPE;
    ldDataScad VANZARI.DATA_SCAD%TYPE;
    lcSerieAct VANZARI.SERIE_ACT%TYPE;
  BEGIN
    UPDATE VANZARI
       SET ID_RUTA           = V_ID_RUTA,
           ID_DELEGAT        = V_ID_DELEGAT,
           ID_AGENT          = V_ID_AGENT,
           ID_MASINA         = V_ID_MASINA,
           DATAORA_EXP       = V_DATAORA_EXP,
           ID_FACTURARE      = V_ID_FACTURARE,
           LISTARE_DETALIATA = NVL(V_LISTARE_DETALIATA, 0),
           TEXT_ADITIONAL    = V_TEXT_ADITIONAL,
           TIP_SAFT          = V_TIP_SAFT,
           EFACTURA          = V_EFACTURA
     WHERE ID_VANZARE = V_ID_VANZARE;

     select id_fact, numar_act, data_act, data_scad, serie_act into lnIdFact, lnNrAct, ldDataAct, ldDataScad, lcSerieAct from vanzari where ID_VANZARE = V_ID_VANZARE;

     IF V_SERIE_ACT IS NOT NULL AND V_SERIE_ACT <> NVL(lcSerieAct, '') THEN
        UPDATE VANZARI SET SERIE_ACT = V_SERIE_ACT WHERE ID_VANZARE = V_ID_VANZARE;
        UPDATE DOCUMENTE SET SERIE_ACT = V_SERIE_ACT WHERE ID_DOC = lnIdFact;
        UPDATE ACT SET SERIE_ACT = V_SERIE_ACT WHERE ID_FACT = lnIdFact;
        UPDATE IREG_PARTENERI SET SERIE_ACT = V_SERIE_ACT WHERE ID_FACT = lnIdFact;
        UPDATE JV2007 SET SERIE_ACT = V_SERIE_ACT WHERE ID_FACT = lnIdFact;
        UPDATE RUL SET SERIE_ACT = V_SERIE_ACT WHERE ID_FACT = lnIdFact;
     END IF;


     IF V_NUMAR_ACT IS NOT NULL AND V_NUMAR_ACT <> NVL(lnNrAct, 0) THEN
        UPDATE VANZARI SET NUMAR_ACT = V_NUMAR_ACT WHERE ID_VANZARE = V_ID_VANZARE;
        UPDATE DOCUMENTE SET NRACT = V_NUMAR_ACT WHERE ID_DOC = lnIdFact;
        UPDATE ACT SET NRACT = V_NUMAR_ACT WHERE ID_FACT = lnIdFact;
        UPDATE IREG_PARTENERI SET NRACT = V_NUMAR_ACT WHERE ID_FACT = lnIdFact;
        UPDATE JV2007 SET NRACT = V_NUMAR_ACT WHERE ID_FACT = lnIdFact;
        UPDATE RUL SET NRACT = V_NUMAR_ACT WHERE ID_FACT = lnIdFact;
     END IF;

     IF V_DATA_ACT IS NOT NULL and V_DATA_ACT <> NVL(ldDataAct, SYSDATE) THEN
        UPDATE VANZARI SET DATA_ACT = V_DATA_ACT WHERE ID_VANZARE = V_ID_VANZARE;
        UPDATE DOCUMENTE SET DATAACT = V_DATA_ACT WHERE ID_DOC = lnIdFact;
        UPDATE ACT SET DATAACT = V_DATA_ACT WHERE ID_FACT = lnIdFact;
        UPDATE IREG_PARTENERI SET DATAACT = V_DATA_ACT WHERE ID_FACT = lnIdFact;
        UPDATE JV2007 SET DATAACT = V_DATA_ACT WHERE ID_FACT = lnIdFact;
        UPDATE RUL SET DATAACT = V_DATA_ACT, DATAOUT = V_DATA_ACT WHERE ID_FACT = lnIdFact;
     END IF;

     IF V_DATA_SCAD IS NOT NULL and V_DATA_SCAD <> NVL(ldDataScad, sysdate) THEN
        UPDATE VANZARI SET DATA_SCAD = V_DATA_SCAD WHERE ID_VANZARE = V_ID_VANZARE;
        UPDATE ACT SET DATASCAD = V_DATA_SCAD WHERE ID_FACT = lnIdFact;
        UPDATE IREG_PARTENERI SET DATASCAD = V_DATA_SCAD WHERE ID_FACT = lnIdFact;
     END IF;

  END modifica_date_factura;
  -------------------------------------------------------------------
  PROCEDURE modifica_explicatie_articol(V_ID_VANZARE_DET IN NUMBER,
                                        V_EXPLICATIE     IN VARCHAR2,
                                        V_ID_UTIL        IN NUMBER,
                                        V_TAXCODE        IN NUMBER DEFAULT NULL) is
  BEGIN
    UPDATE VANZARI_DETALII
       SET EXPLICATIE = V_EXPLICATIE, TAXCODE = V_TAXCODE
     WHERE ID_VANZARE_DET = V_ID_VANZARE_DET;
  END modifica_explicatie_articol;
  -------------------------------------------------------------------
  PROCEDURE initializeaza_moneda_nationala IS
  BEGIN
    pack_facturare.nid_moneda_nationala := pack_def.GetIdMonedaNationala();
  END initializeaza_moneda_nationala;
  -------------------------------------------------------------------
  PROCEDURE scrie_cursuri_proforma(V_ID_PROFORMA IN NUMBER) IS
  BEGIN
    INSERT INTO PROFORME_CURSURI
      (ID_PROFORMA, ID_VALUTA, CURS, MULTIPLICATOR)
      SELECT DISTINCT V_ID_PROFORMA AS ID_PROFORMA,
                      ID_VALUTA,
                      CURS,
                      MULTIPLICATOR
        FROM VANZARI_DETALII_TEMP
       WHERE ID_VALUTA <> pack_facturare.nid_moneda_nationala;
  END scrie_cursuri_proforma;
  -------------------------------------------------------------------
  PROCEDURE scrie_cursuri(V_ID_VANZARE IN NUMBER) IS
  BEGIN
    INSERT INTO VANZARI_CURSURI
      (ID_VANZARE, ID_VALUTA, CURS, MULTIPLICATOR)
      SELECT DISTINCT V_ID_VANZARE AS ID_VANZARE,
                      ID_VALUTA,
                      CURS,
                      MULTIPLICATOR
        FROM VANZARI_DETALII_TEMP
       WHERE ID_VALUTA <> pack_facturare.nid_moneda_nationala;
  END scrie_cursuri;
  -------------------------------------------------------------------
  PROCEDURE scrie_rate_factura(V_DATAORA IN DATE) IS
  BEGIN
    INSERT INTO CTR_RATE_FACTURI
      (ID_RATA,
       ID_FACT,
       VALVALUTA,
       VALVALUTACTVA,
       PROCTVA,
       ID_VALUTA,
       CURS,
       ID_UTIL,
       DATAORA)
      SELECT A.ID_RATA,
             B.ID_FACT,
             pack_facturare.calculeaza_total_fara_tva(A.PRET,
                                                      A.DIFERENTA,
                                                      1,
                                                      A.DISCOUNT_UNITAR,
                                                      pack_facturare.ndiscount_evidentiat,
                                                      A.CANTITATE,
                                                      A.PRET_CU_TVA,
                                                      A.PROC_TVAV,
                                                      pack_sesiune.nzecimale_pretvval,
                                                      pack_sesiune.nzecimale_sumaval) as VALVALUTA,
             pack_facturare.calculeaza_total_cu_tva(A.PRET,
                                                    A.DIFERENTA,
                                                    1,
                                                    A.DISCOUNT_UNITAR,
                                                    pack_facturare.ndiscount_evidentiat,
                                                    A.CANTITATE,
                                                    A.PRET_CU_TVA,
                                                    A.PROC_TVAV,
                                                    pack_sesiune.nzecimale_pretvval,
                                                    pack_sesiune.nzecimale_sumaval) as VALVALUTACTVA,
             (A.PROC_TVAV - 1) * 100 AS PROC_TVAV,
             A.ID_VALUTA,
             A.CURS / NVL(A.MULTIPLICATOR, 1) AS CURS,
             pack_facturare.nid_util as ID_UTIL,
             V_DATAORA as DATAORA
        FROM VANZARI_DETALII_TEMP A
        LEFT JOIN (SELECT DISTINCT ID_FACT, ID_CTR
                     FROM ACT_TEMP
                    WHERE STERS = 0
                      AND SCD LIKE '4%') B
          ON A.ID_CTR = B.ID_CTR
       WHERE A.ID_RATA IS NOT NULL;

  END scrie_rate_factura;
  -------------------------------------------------------------------
  FUNCTION calculeaza_k RETURN NUMBER IS
  BEGIN
    RETURN 0;
  END calculeaza_k;
  -------------------------------------------------------------------
  PROCEDURE calculeaza_pret(V_PRET_UNITAR   IN NUMBER,
                            V_CURS          IN NUMBER,
                            V_ID_VALUTA     IN NUMBER,
                            V_PROC_TVA      IN NUMBER,
                            V_PRET_ARE_TVA  IN NUMBER,
                            V_TIP           IN NUMBER,
                            V_PRET_FARA_TVA OUT NUMBER,
                            V_PRET_TVA      OUT NUMBER,
                            V_PRET_CU_TVA   OUT NUMBER) IS
  BEGIN
    V_PRET_FARA_TVA := pack_sesiune.calculeaza_pret_fara_tva(v_pret_unitar,
                                                             v_curs,
                                                             v_id_valuta,
                                                             v_proc_tva,
                                                             v_pret_are_tva,
                                                             v_tip);
    V_PRET_TVA      := pack_sesiune.calculeaza_pret_tva(v_pret_unitar,
                                                        v_curs,
                                                        v_id_valuta,
                                                        v_proc_tva,
                                                        v_pret_are_tva,
                                                        v_tip);
    V_PRET_CU_TVA   := pack_sesiune.calculeaza_pret_cu_tva(v_pret_unitar,
                                                           v_curs,
                                                           v_id_valuta,
                                                           v_proc_tva,
                                                           v_pret_are_tva,
                                                           v_tip);
  END calculeaza_pret;
  -------------------------------------------------------------------
  /*  function calculeaza_pret_fara_tva(V_PRET_UNITAR  IN NUMBER,
                                    V_CURS         IN NUMBER,
                                    V_ID_VALUTA    IN NUMBER,
                                    V_PROC_TVA     IN NUMBER,
                                    V_PRET_ARE_TVA IN NUMBER,
                                                                V_TIP           IN NUMBER) return number IS
  BEGIN

    return pack_sesiune.calculeaza_pret_fara_tva(V_PRET_UNITAR,
                                                 V_CURS,
                                                 V_ID_VALUTA,
                                                 V_PROC_TVA,
                                                 V_PRET_ARE_TVA, V_TIP);
  END calculeaza_pret_fara_tva;*/

  -------------------------------------------------------------------
  /* function calculeaza_pret_TVA(V_PRET_UNITAR  IN NUMBER,
                               V_CURS         IN NUMBER,
                               V_ID_VALUTA    IN NUMBER,
                               V_PROC_TVA     IN NUMBER,
                               V_PRET_ARE_TVA IN NUMBER,
                               V_TIP           IN NUMBER) return NUMBER IS
    V_PRET_TVA number := 0;
  BEGIN
    return pack_sesiune.calculeaza_pret_TVA(V_PRET_UNITAR,
                                            V_CURS,
                                            V_ID_VALUTA,
                                            V_PROC_TVA,
                                            V_PRET_ARE_TVA);
  END calculeaza_pret_TVA;*/
  -------------------------------------------------------------------
  /*  function calculeaza_pret_cu_tva(V_PRET_UNITAR  IN NUMBER,
                                  V_CURS         IN NUMBER,
                                  V_ID_VALUTA    IN NUMBER,
                                  V_PROC_TVA     IN NUMBER,
                                  V_PRET_ARE_TVA IN NUMBER) RETURN NUMBER IS
  BEGIN
    return pack_sesiune.calculeaza_pret_cu_tva(V_PRET_UNITAR,
                                               V_CURS,
                                               V_ID_VALUTA,
                                               V_PROC_TVA,
                                               V_PRET_ARE_TVA);
  END calculeaza_pret_cu_tva;*/
  -------------------------------------------------------------------
  PROCEDURE calculeaza_sume(V_PRET_UNITAR     IN NUMBER,
                            V_DISCOUNT_UNITAR IN NUMBER,
                            V_ZECIMALE_PRET   IN NUMBER,
                            V_ZECIMALE_SUMA   IN NUMBER,
                            V_CANTITATE       IN NUMBER,
                            V_CURS            IN NUMBER,
                            V_ID_VALUTA       IN NUMBER,
                            V_PROC_TVA        IN NUMBER,
                            V_PRET_ARE_TVA    IN NUMBER,
                            V_SUMA_FARA_TVA   OUT NUMBER,
                            V_SUMA_TVA        OUT NUMBER,
                            V_SUMA_CU_TVA     OUT NUMBER) IS
    V_CURS_NOU NUMBER;
  BEGIN
    IF NVL(V_CURS, 0) = 0 THEN
      V_CURS_NOU := 1;
    ELSE
      V_CURS_NOU := V_CURS;
    END IF;
    V_SUMA_FARA_TVA := pack_facturare.calculeaza_total_fara_tva(V_PRET                => V_PRET_UNITAR,
                                                                V_AJUSTARE            => 0,
                                                                V_CURS                => V_CURS_NOU,
                                                                V_DISCOUNT_UNITAR     => (CASE
                                                                                          pack_facturare.ndiscount_evidentiat
                                                                                           WHEN 0 THEN
                                                                                            V_DISCOUNT_UNITAR
                                                                                           ELSE
                                                                                            0
                                                                                         END),
                                                                V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                                V_CANTITATE           => V_CANTITATE,
                                                                V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                                V_PROC_TVAV           => V_PROC_TVA,
                                                                V_ZECIMALE_PRET       => V_ZECIMALE_PRET,
                                                                V_ZECIMALE_SUMA       => V_ZECIMALE_SUMA);
    V_SUMA_TVA      := pack_facturare.calculeaza_total_tva(V_PRET                => V_PRET_UNITAR,
                                                           V_AJUSTARE            => 0,
                                                           V_CURS                => V_CURS_NOU,
                                                           V_DISCOUNT_UNITAR     => (CASE
                                                                                     pack_facturare.ndiscount_evidentiat
                                                                                      WHEN 0 THEN
                                                                                       V_DISCOUNT_UNITAR
                                                                                      ELSE
                                                                                       0
                                                                                    END),
                                                           V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                           V_CANTITATE           => V_CANTITATE,
                                                           V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                           V_PROC_TVAV           => V_PROC_TVA,
                                                           V_ZECIMALE_PRET       => V_ZECIMALE_PRET,
                                                           V_ZECIMALE_SUMA       => V_ZECIMALE_SUMA);
    V_SUMA_CU_TVA   := pack_facturare.calculeaza_total_cu_tva(V_PRET                => V_PRET_UNITAR,
                                                              V_AJUSTARE            => 0,
                                                              V_CURS                => V_CURS_NOU,
                                                              V_DISCOUNT_UNITAR     => (CASE
                                                                                        pack_facturare.ndiscount_evidentiat
                                                                                         WHEN 0 THEN
                                                                                          V_DISCOUNT_UNITAR
                                                                                         ELSE
                                                                                          0
                                                                                       END),
                                                              V_DISCOUNT_EVIDENTIAT => pack_facturare.ndiscount_evidentiat,
                                                              V_CANTITATE           => V_CANTITATE,
                                                              V_PRET_CU_TVA         => V_PRET_ARE_TVA,
                                                              V_PROC_TVAV           => V_PROC_TVA,
                                                              V_ZECIMALE_PRET       => V_ZECIMALE_PRET,
                                                              V_ZECIMALE_SUMA       => V_ZECIMALE_SUMA);

  END calculeaza_sume;
  -------------------------------------------------------------------
  /*  PROCEDURE finalizeaza_factura(V_DISCOUNT_FACTURA    IN NUMBER,
                                  V_ID_DELEGAT          IN NUMBER,
                                  V_ID_MASINA           IN NUMBER,
                                  V_DATAORA_EXP         IN DATE,
                                  V_ID_AGENT            IN NUMBER,
                                  V_TEXT_ADITIONAL      IN VARCHAR2,
                                  V_PARAMETRU_ADITIONAL IN NUMBER) IS
      lnIdFacturare      NUMBER(10) := null;
      lnListareDetaliata NUMBER(1) := 0;
    begin
      pack_facturare.finalizeaza_factura(V_DISCOUNT_FACTURA,
                                         V_ID_DELEGAT,
                                         V_ID_MASINA,
                                         lnIdFacturare,
                                         lnListareDetaliata,
                                         V_DATAORA_EXP,
                                         V_ID_AGENT,
                                         V_TEXT_ADITIONAL,
                                         V_PARAMETRU_ADITIONAL);
    end;
  */
  -------------------------------------------------------------------
  PROCEDURE finalizeaza_factura(V_DISCOUNT_FACTURA    IN NUMBER,
                                V_ID_DELEGAT          IN NUMBER,
                                V_ID_MASINA           IN NUMBER,
                                V_ID_FACTURARE        IN NUMBER,
                                V_LISTARE_DETALIATA   IN NUMBER,
                                V_DATAORA_EXP         IN DATE,
                                V_ID_AGENT            IN NUMBER,
                                V_TEXT_ADITIONAL      IN VARCHAR2,
                                V_PARAMETRU_ADITIONAL IN NUMBER) IS
    V_DATAORA DATE := SYSDATE;
    lnIdFact  vanzari.id_fact%type;
    lnIdFact2 vanzari.id_fact%type;
  BEGIN
    pack_facturare.nid_vanzare := NULL;

    pack_facturare.initializeaza_scriere_actrul(V_DATAORA);

    pack_facturare.scrie_in_vanzari(V_DISCOUNT_FACTURA,
                                    V_ID_DELEGAT,
                                    V_ID_MASINA,
                                    V_ID_FACTURARE,
                                    V_LISTARE_DETALIATA,
                                    V_DATAORA_EXP,
                                    V_ID_AGENT,
                                    V_TEXT_ADITIONAL,
                                    pack_facturare.nid_vanzare);

    pack_facturare.finalizeaza_scriere_actrul();

    -- completez id_fact dupa generarea id_fact la scrierea in contabilitate
    -- daca este factura cu incasare, pack_contafin.id_fact este id_fact-ul documentului de incasare, nu id_fact-ul facturii
    lnIdFact := pack_contafin.get_idFact();
    select min(id_fact)
      into lnIdFact2
      from act
     where cod in (select cod
                     from act
                    where id_fact = lnIdFact
                      and sters = 0)
       and scd not like '5%';
    if lnIdFact <> lnIdFact2 then
      lnIdFact := lnIdFact2;
    end if;

    update vanzari
       set id_fact = lnIdFact
     where id_vanzare = pack_facturare.nid_vanzare;

    CASE
      WHEN V_PARAMETRU_ADITIONAL = 1 AND
           pack_facturare.ntip IN (3, 21, 25, 28, 42, 47) THEN
        -- daca e comanda, atunci V_PARAMETRU_ADITIONAL este V_INCHIDERE_COMANDA
        pack_facturare.inchide_comanda();
      WHEN pack_facturare.ntip = 4 THEN
        -- daca e facturare din aviz, atunci V_PARAMETRU_ADITIONAL este V_VERIFICARE_FACTURAT
        pack_facturare.scrie_cantitati_vanzari_avize;
        pack_facturare.scrie_corespondente_vanzari(1);
        pack_facturare.marcheaza_facturat(V_PARAMETRU_ADITIONAL);
      WHEN pack_facturare.ntip = 24 THEN
        -- daca e aviz de retur, atunci V_PARAMETRU_ADITIONAL este V_VERIFICARE_FACTURAT ( = 0 )
        pack_facturare.scrie_corespondente_vanzari(2);
        pack_facturare.marcheaza_facturat(V_PARAMETRU_ADITIONAL);
      WHEN pack_facturare.ntip in ( 2, 6, 52) THEN
        pack_facturare.scrie_rate_factura(V_DATAORA);
      WHEN pack_facturare.ntip in (8, 9) THEN
        -- daca e factura de retur
        pack_facturare.scrie_corespondente_vanzari(3);
      ELSE
        dbms_output.put_line('---');
    END CASE;

    IF pack_facturare.nid_set =
       25000 + pack_facturare.nTipFacturaRestaurant - 1 +
       pack_facturare.nscadere_stoc * 10 AND
       to_number(pack_sesiune.getoptiunefirma('DESCARCAREGESTZ')) = 1 THEN
      pack_grestaurant.descarcare_automata_zi(pack_facturare.ddata_act,
                                              pack_facturare.nid_vanzare,
                                              NULL,
                                              NULL,
                                              pack_facturare.nid_sucursala,
                                              pack_facturare.nid_util);
    END IF;
  END finalizeaza_factura;
  -------------------------------------------------------------------
  PROCEDURE finalizeaza_avize_lucrare(V_DISCOUNT_FACTURA IN NUMBER) IS
    V_DATAORA       DATE := SYSDATE;
    V_ID_DELEGAT    VANZARI.ID_DELEGAT%TYPE;
    V_ID_MASINA     VANZARI.ID_MASINA%TYPE;
    V_ID_AGENT      VANZARI.ID_AGENT%TYPE;
    V_ID_RUTA       VANZARI.ID_RUTA%TYPE;
    V_COD           VANZARI.COD%TYPE;
    i               NUMBER(10);
    V_NUME_GESTIUNE NOM_GESTIUNI.NUME_GESTIUNE%TYPE;

    TYPE tab_vanzari_type IS TABLE OF vanzari_temp%ROWTYPE;
    tab_vanz tab_vanzari_type;

    lnDiscountTVA      VANZARI.DISCOUNT_TVA%TYPE;
    lnValoareAchizitie VANZARI.VALOARE_ACHIZITIE%TYPE;
    lnSerieIncasat     ACT.SERIE_ACT%TYPE;
    lnNrIncasat        ACT.NRACT%TYPE;
    lnSumaIncasat      ACT.SUMA%TYPE;
    lnTipIncasat       NUMBER(2);
    lnTotalFaraTVA     VANZARI.TOTAL_FARA_TVA%TYPE;
    lnTotalTVA         VANZARI.TOTAL_TVA%TYPE;
    lnTotalCuTVA       VANZARI.TOTAL_CU_TVA%TYPE;
    lnIdGestiune       VANZARI.ID_GESTIUNE%TYPE;
    lnIdValuta         VANZARI.Id_Valuta%TYPE;
    lnCurs             VANZARI.CURS%TYPE;
    lnMultiplicator    VANZARI.MULTIPLICATOR%TYPE;
  BEGIN
    pack_facturare.nid_vanzare := NULL;
    -- nu se mai genereaza codul la initializare, trebuie sa-l generez la fiecare document
    -- pana acum aveam conditia sa-l genereze doar de la i > 1
    pack_facturare.initializeaza_scriere_actrul(V_DATAORA, 0);

    SELECT * BULK COLLECT INTO tab_vanz FROM VANZARI_TEMP;

    FOR I IN tab_vanz.first .. tab_vanz.last LOOP

      V_COD := pack_contafin.GENEREAZA_COD(user);

      BEGIN
        SELECT ID_DELEGAT, ID_MASINA, ID_AGENT, ID_RUTA
          INTO V_ID_DELEGAT, V_ID_MASINA, V_ID_AGENT, V_ID_RUTA
          FROM FACT_NOM_RUTE
         WHERE ID_RUTA IN (SELECT ID_RUTA
                             FROM FACT_NOM_RUTEGEST
                            WHERE ID_GESTIUNE = tab_vanz(i).id_gestiune_dest
                              AND STERS = 0)
           AND STERS = 0;
      EXCEPTION
        WHEN TOO_MANY_ROWS THEN
          SELECT NUME_GESTIUNE
            INTO V_NUME_GESTIUNE
            FROM NOM_GESTIUNI
           WHERE ID_GESTIUNE = tab_vanz(i).id_gestiune_dest;

          RAISE_APPLICATION_ERROR(-20000,
                                  'Pentru gestiunea ' || V_NUME_GESTIUNE ||
                                  ' aveti configurate mai multe rute! (FACT-023)');

        WHEN NO_DATA_FOUND THEN
          V_ID_DELEGAT := NULL;
          V_ID_MASINA  := NULL;
          V_ID_AGENT   := NULL;
          V_ID_RUTA    := NULL;
      END;

      INSERT INTO VANZARI
        (id_util,
         dataora,
         numar_act,
         serie_act,
         id_lucrare,
         id_comanda,
         tip,
         id_gestiune,
         cod,
         data_act,
         discount,
         discount_evidentiat,
         id_delegat,
         id_masina,
         id_agent,
         id_ruta,
         id_sucursala,
         id_sectie)
      VALUES
        (pack_facturare.nid_util,
         V_DATAORA,
         tab_vanz                           (i).numar_act,
         pack_facturare.cserie_act,
         tab_vanz                           (i).id_lucrare,
         tab_vanz                           (i).id_comanda,
         pack_facturare.ntip,
         tab_vanz                           (i).id_gestiune_dest,
         V_COD,
         pack_facturare.ddata_act,
         V_DISCOUNT_FACTURA,
         pack_facturare.ndiscount_evidentiat,
         V_ID_DELEGAT,
         V_ID_MASINA,
         V_ID_AGENT,
         V_ID_RUTA,
         pack_facturare.nid_sucursala,
         pack_facturare.nid_sectie_stoc)
      RETURNING ID_VANZARE INTO pack_facturare.nid_vanzare;

      pack_facturare.scrie_cursuri(pack_facturare.nid_vanzare);

      INSERT /*+ APPEND */
      INTO VANZARI_DETALII
        (ID_VANZARE,
         ID_ARTICOL,
         SERIE,
         ID_POL,
         CANTITATE,
         PRET_ACHIZITIE,
         PRETD,
         ID_VALUTAD,
         PRET,
         PROC_TVAV,
         DISCOUNT_UNITAR,
         ID_GESTIUNE,
         ID_VALUTA,
         CONT,
         PRET_CU_TVA,
         ID_JTVA_COLOANA)
        SELECT pack_facturare.nid_vanzare as ID_VANZARE,
               ID_ARTICOL,
               SERIE,
               ID_POL,
               CANTITATE,
               PRET_ACHIZITIE,
               PRETD,
               ID_VALUTAD,
               PRET,
               PROC_TVAV,
               DISCOUNT_UNITAR,
               ID_GESTIUNE,
               ID_VALUTA,
               CONT,
               PRET_CU_TVA,
               ID_JTVA_COLOANA
          FROM VANZARI_DETALII_TEMP
         WHERE ID_COMANDA = tab_vanz(i).id_comanda
           AND NUMAR_ACT = tab_vanz(i).numar_act
         ORDER BY ID_TEMP;

      UPDATE RUL_TEMP SET COD = V_COD WHERE nract = tab_vanz(i).numar_act;
      UPDATE ACT_TEMP SET COD = V_COD WHERE nract = tab_vanz(i).numar_act;

      -- Completare totaluri in vanzari din vanzari_detalii pentru a usura selectiile din fact_vfacturi
      begin
        select DISC_TVA AS DISCOUNT_TVA,
               VALOARE_ACHIZITIE,
               decode(a.in_valuta,
                      1,
                      ROUND(a.curs * (a.suma_fara_tva - a.disc_fara_tva) /
                            a.multiplicator,
                            pack_sesiune.getOptiuneFirma('PC')),
                      a.suma_fara_tva - a.disc_fara_tva) as TOTAL_FARA_TVA,
               decode(a.in_valuta,
                      1,
                      ROUND(a.curs * (a.suma_tva - a.disc_tva) /
                            a.multiplicator,
                            pack_sesiune.getOptiuneFirma('PC')),
                      a.suma_tva - a.disc_tva) as TOTAL_TVA,
               decode(a.in_valuta,
                      1,
                      ROUND(a.curs * (a.suma_fara_tva - a.disc_fara_tva) /
                            a.multiplicator,
                            pack_sesiune.getOptiuneFirma('PC')),
                      a.suma_fara_tva - a.disc_fara_tva) +
               decode(a.in_valuta,
                      1,
                      ROUND(a.curs * (a.suma_tva - a.disc_tva) /
                            a.multiplicator,
                            pack_sesiune.getOptiuneFirma('PC')),
                      a.suma_tva - a.disc_tva) as TOTAL_CU_TVA,
               pack_facturare.cserie_act_incasare as SERIE_INCASAT,
               pack_facturare.nnumar_act_incasare as NR_INCASAT,
               pack_facturare.nsuma_incasare AS SUMA_INCASAT,
               pack_facturare.ntip_doc_incasare as TIP_INCASAT,
               a.id_valuta,
               a.curs,
               a.multiplicator
          INTO lnDiscountTVA,
               lnValoareAchizitie,
               lnTotalFaraTVA,
               lnTotalTVA,
               lnTotalCuTVA,
               lnSerieIncasat,
               lnNrIncasat,
               lnSumaIncasat,
               lnTipIncasat,
               lnIdValuta,
               lnCurs,
               lnMultiplicator
          FROM (select NVL(V_DISCOUNT_FACTURA, 0) as DISC_FARA_TVA,
                       pack_facturare.nin_valuta AS IN_VALUTA,
                       ROUND(NVL(V_DISCOUNT_FACTURA, 0) *
                             max(vd.proc_tvav - 1),
                             pack_sesiune.getOptiuneFirma('PPRETV')) as DISC_TVA,
                       sum(pack_facturare.calculeaza_total_fara_tva_fact(vd.pret /
                                                                         decode(pack_facturare.nin_valuta,
                                                                                1,
                                                                                1,
                                                                                NVL(vc.multiplicator,
                                                                                    1)),
                                                                         decode(pack_facturare.nin_valuta,
                                                                                1,
                                                                                0,
                                                                                vd.diferenta),
                                                                         decode(pack_facturare.nin_valuta,
                                                                                1,
                                                                                1,
                                                                                NVL(vc.curs,
                                                                                    1)),
                                                                         NVL(vd.discount_unitar,
                                                                             0) /
                                                                         decode(pack_facturare.nin_valuta,
                                                                                1,
                                                                                1,
                                                                                NVL(vc.multiplicator,
                                                                                    1)),
                                                                         pack_facturare.ndiscount_evidentiat,
                                                                         vd.cantitate,
                                                                         vd.pret_cu_tva,
                                                                         vd.proc_tvav)) AS SUMA_FARA_TVA,
                       sum(pack_facturare.calculeaza_total_tva_fact(vd.pret /
                                                                    decode(pack_facturare.nin_valuta,
                                                                           1,
                                                                           1,
                                                                           NVL(vc.multiplicator,
                                                                               1)),
                                                                    decode(pack_facturare.nin_valuta,
                                                                           1,
                                                                           0,
                                                                           vd.diferenta),
                                                                    decode(pack_facturare.nin_valuta,
                                                                           1,
                                                                           1,
                                                                           NVL(vc.curs,
                                                                               1)),
                                                                    NVL(vd.discount_unitar,
                                                                        0) /
                                                                    decode(pack_facturare.nin_valuta,
                                                                           1,
                                                                           1,
                                                                           NVL(vc.multiplicator,
                                                                               1)),
                                                                    pack_facturare.ndiscount_evidentiat,
                                                                    vd.cantitate,
                                                                    vd.pret_cu_tva,
                                                                    vd.proc_tvav)) as SUMA_TVA,
                       sum(round(vd.cantitate * vd.pret_achizitie,
                                 pack_sesiune.getOptiuneFirma('PC'))) AS VALOARE_ACHIZITIE,
                       max(decode(pack_facturare.nin_valuta,
                                  1,
                                  vc.id_valuta,
                                  0)) as id_valuta,
                       max(decode(pack_facturare.nin_valuta, 1, vc.curs, 1)) as curs,
                       max(decode(pack_facturare.nin_valuta,
                                  1,
                                  vc.multiplicator,
                                  1)) as multiplicator
                  from (select a.id_vanzare_set,
                               a.pret,
                               a.proc_tvav,
                               a.cantitate,
                               a.diferenta,
                               a.discount_unitar,
                               a.id_valuta,
                               a.pret_cu_tva,
                               a.pret_achizitie
                          FROM VANZARI_DETALII_TEMP a
                         WHERE a.ID_COMANDA = tab_vanz(i).id_comanda
                           AND a.NUMAR_ACT = tab_vanz(i).numar_act
                           and nvl(a.id_vanzare_set, 0) = 0
                        union all
                        select b.id_vanzare_set,
                               b.pret,
                               max(c.proc_tvav) as proc_tvav,
                               b.cantitate,
                               0 as diferenta,
                               b.discount_unitar,
                               decode(pack_facturare.nin_valuta,
                                      0,
                                      pack_def.GetIdMonedaNationala(),
                                      c.id_valuta) as id_valuta,
                               b.pret_cu_tva,
                               sum(decode(b.cantitate,
                                          0,
                                          0,
                                          c.pret_achizitie * c.cantitate /
                                          b.cantitate)) as pret_achizitie
                          from vanzari_detalii_temp c
                          left join vanzari_seturi b
                            on b.id_vanzare_set = c.id_vanzare_set
                         WHERE nvl(c.id_vanzare_set, 0) <> 0
                           and c.ID_COMANDA = tab_vanz(i).id_comanda
                           AND c.NUMAR_ACT = tab_vanz(i).numar_act
                           and nvl(pack_facturare.nin_valuta, -1) > -1
                         group by b.id_vanzare_set,
                                  b.pret,
                                  b.cantitate,
                                  b.discount_unitar,
                                  b.pret_cu_tva,
                                  decode(pack_facturare.nin_valuta,
                                         0,
                                         pack_def.GetIdMonedaNationala(),
                                         c.id_valuta)) vd
                  left join vanzari_cursuri vc
                    on vc.id_vanzare = pack_facturare.nid_vanzare
                   and vd.id_valuta = vc.id_valuta) a;

        update vanzari
           set discount_tva      = lnDiscountTVA,
               valoare_achizitie = lnValoareAchizitie,
               total_fara_tva    = lnTotalFaraTVA,
               total_tva         = lnTotalTVA,
               total_cu_tva      = lnTotalCuTVA,
               serie_incasat     = lnSerieIncasat,
               nr_incasat        = lnNrIncasat,
               suma_incasat      = lnSumaIncasat,
               tip_incasat       = lnTipIncasat,
               id_valuta         = lnIdValuta,
               curs              = lnCurs,
               multiplicator     = lnMultiplicator
         where id_vanzare = pack_facturare.nid_vanzare;

      exception
        when NO_DATA_FOUND then
          null;
      end;
      ---------------------------------------------
    END LOOP;

    pack_facturare.finalizeaza_scriere_actrul();

  END finalizeaza_avize_lucrare;

  -------------------------------------------------------------------
  /*  PROCEDURE finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA     IN NUMBER,
                                           V_ID_DELEGAT           IN NUMBER,
                                           V_ID_MASINA            IN NUMBER,
                                           V_DATAORA_EXP          IN DATE,
                                           V_ID_AGENT             IN NUMBER,
                                           V_TEXT_ADITIONAL       IN VARCHAR2,
                                           V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                           V_SIR_MODIFICARI_PART  IN VARCHAR,
                                           V_PARAMETRU_ADITIONAL  IN NUMBER,
                                           V_ID_VANZARE           OUT NUMBER) is
    lnIdFacturare      NUMBER(10) := null;
    lnListareDetaliata NUMBER(1) := 0;
  begin
    pack_facturare.finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA,
                                                  V_ID_DELEGAT,
                                                  V_ID_MASINA,
                                                  lnIdFacturare,
                                                  lnListareDetaliata,
                                                  V_DATAORA_EXP,
                                                  V_ID_AGENT,
                                                  V_TEXT_ADITIONAL,
                                                  V_SIR_MODIFICARI_ACONT,
                                                  V_SIR_MODIFICARI_PART,
                                                  V_PARAMETRU_ADITIONAL,
                                                  V_ID_VANZARE);
  end;*/

  -------------------------------------------------------------------
  PROCEDURE finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA     IN NUMBER,
                                           V_ID_DELEGAT           IN NUMBER,
                                           V_ID_MASINA            IN NUMBER,
                                           V_DATAORA_EXP          IN DATE,
                                           V_ID_AGENT             IN NUMBER,
                                           V_TEXT_ADITIONAL       IN VARCHAR2,
                                           V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                           V_SIR_MODIFICARI_PART  IN VARCHAR,
                                           V_PARAMETRU_ADITIONAL  IN NUMBER,
                                           V_ID_VANZARE           OUT NUMBER) IS
    V_ID_FACTURARE      NUMBER(20) := NULL;
    V_LISTARE_DETALIATA NUMBER(20) := NULL;
  BEGIN
    finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA,
                                   V_ID_DELEGAT,
                                   V_ID_MASINA,
                                   V_ID_FACTURARE,
                                   V_LISTARE_DETALIATA,
                                   V_DATAORA_EXP,
                                   V_ID_AGENT,
                                   V_TEXT_ADITIONAL,
                                   V_SIR_MODIFICARI_ACONT,
                                   V_SIR_MODIFICARI_PART,
                                   V_PARAMETRU_ADITIONAL,
                                   V_ID_VANZARE);
  END;

  PROCEDURE finalizeaza_scriere_verificare(V_DISCOUNT_FACTURA     IN NUMBER,
                                           V_ID_DELEGAT           IN NUMBER,
                                           V_ID_MASINA            IN NUMBER,
                                           V_ID_FACTURARE         IN NUMBER,
                                           V_LISTARE_DETALIATA    IN NUMBER,
                                           V_DATAORA_EXP          IN DATE,
                                           V_ID_AGENT             IN NUMBER,
                                           V_TEXT_ADITIONAL       IN VARCHAR2,
                                           V_SIR_MODIFICARI_ACONT IN VARCHAR2,
                                           V_SIR_MODIFICARI_PART  IN VARCHAR,
                                           V_PARAMETRU_ADITIONAL  IN NUMBER,
                                           V_ID_VANZARE           OUT NUMBER) IS
    V_SEPARATOR_LINIE VARCHAR2(5) := ';';
    V_SEPARATOR       VARCHAR2(5) := '|';
  BEGIN
    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;

    IF pack_facturare.ntip = 27 THEN
      pack_facturare.finalizeaza_avize_lucrare(V_DISCOUNT_FACTURA);
      V_ID_VANZARE := NULL;
    ELSE
      pack_facturare.finalizeaza_factura(V_DISCOUNT_FACTURA,
                                         V_ID_DELEGAT,
                                         V_ID_MASINA,
                                         V_ID_FACTURARE,
                                         V_LISTARE_DETALIATA,
                                         V_DATAORA_EXP,
                                         V_ID_AGENT,
                                         V_TEXT_ADITIONAL,
                                         V_PARAMETRU_ADITIONAL);
      V_ID_VANZARE := pack_facturare.nid_vanzare;
    END IF;

  END finalizeaza_scriere_verificare;
  -------------------------------------------------------------------
  Procedure initializeaza_scriere_actrul(tdDataOra         IN DATE,
                                         tnSuprascriereCod IN NUMBER DEFAULT 1) IS
  BEGIN
    pack_contafin.initializeaza_scriere_act_rul(pack_facturare.nid_util,
                                                tdDataOra,
                                                pack_facturare.nan,
                                                pack_facturare.nluna,
                                                tnSuprascriereCod,
                                                1, -- tnSuprascriereAnLuna
                                                0, -- tnScrie_Sterge: 0 = scriere, 1 = refacere, 2 = stergere
                                                pack_facturare.nid_sucursala);
  END initializeaza_scriere_actrul;
  -------------------------------------------------------------------
  PROCEDURE finalizeaza_scriere_actrul IS
  BEGIN
    pack_contafin.finalizeaza_scriere_act_rul();
  END finalizeaza_scriere_actrul;
  -------------------------------------------------------------------
  PROCEDURE marcheaza_facturat(V_VERIFICARE IN NUMBER) IS
  BEGIN
    IF V_VERIFICARE = 0 THEN
      UPDATE VANZARI
         SET FACTURAT = 1, ID_UTILFACT = pack_facturare.nid_util
       WHERE ID_VANZARE IN
             (SELECT id_vanzare_aviz as id_vanzare
                FROM vanzari_coresp
               WHERE id_vanzare_Fact = pack_facturare.nid_vanzare
                 AND sters = 0
                 AND tip <> 3) -- modificare v 2.0.56 : tip <> 3
         AND FACTURAT = 0;
    ELSE
      UPDATE VANZARI
         SET FACTURAT = 1, ID_UTILFACT = pack_facturare.nid_util
       WHERE ID_VANZARE IN
             (select id_vanzare
                from (select a.id_vanzare,
                             sum(a.cantitate - NVL(b.cantitate, 0)) as ramas
                        from vanzari_detalii a
                        left join (select sum(cantitate) as cantitate,
                                         id_vanzare_det_aviz as id_vanzare_det
                                    from vanzari_cantitati
                                   where sters = 0
                                   group by id_vanzare_det_aviz) b
                          on a.id_vanzare_det = b.id_vanzare_det
                       where a.id_vanzare in
                             (select id_vanzare_aviz as id_vanzare
                                from vanzari_coresp
                               where id_vanzare_Fact =
                                     pack_facturare.nid_vanzare
                                 and sters = 0
                                 and tip <> 3) -- modificare v 2.0.56 : sters = 0 and tip <> 3
                       group by a.id_vanzare)
               where ramas = 0);

    END IF;
  END marcheaza_facturat;
  -------------------------------------------------------------------
  PROCEDURE scrie_cantitati_vanzari_avize IS
    V_SEPARATOR VARCHAR2(10) := ',';
    V_LISTA     VARCHAR2(3000) := '';
  BEGIN
    V_LISTA := pack_facturare.clistaid_avize || V_SEPARATOR ||
               pack_facturare.nid_vanzare || V_SEPARATOR;

    INSERT INTO VANZARI_CANTITATI
      (ID_VANZARE_DET, ID_VANZARE_DET_AVIZ, CANTITATE)
      SELECT A.MAX_IDT AS ID_VANZARE_DET,
             A.ID_VANZARE_DET AS ID_VANZARE_DET_AVIZ,
             (CASE
               WHEN A.TOTAL >= 0 THEN
                A.CANTITATE
               ELSE
                A.CANTITATE + A.TOTAL
             END) AS CANTITATE
        FROM (SELECT MAX(A.ID_VANZARE) OVER(PARTITION BY A.ID_ARTICOL, A.SERIE, A.DISCOUNT_UNITAR, A.ID_POL, A.ID_VALUTA, A.PRET, A.ID_GESTIUNE, A.CONT) AS MAX_ID,
                     MAX(A.ID_VANZARE_DET) OVER(PARTITION BY A.ID_ARTICOL, A.SERIE, A.DISCOUNT_UNITAR, A.ID_POL, A.ID_VALUTA, A.PRET, A.ID_GESTIUNE, A.CONT) AS MAX_IDT,
                     SUM(DECODE(A.ID_VANZARE,
                                pack_facturare.nid_vanzare,
                                A.CANTITATE,
                                (-1) * A.CANTITATE)) OVER(PARTITION BY A.ID_ARTICOL, A.SERIE, A.DISCOUNT_UNITAR, A.ID_POL, A.ID_VALUTA, A.PRET, A.ID_GESTIUNE, A.CONT ORDER BY DECODE(A.ID_VANZARE, pack_facturare.nid_vanzare, 0, A.ID_VANZARE_DET)) AS TOTAL,
                     A.ID_VANZARE,
                     DECODE(A.ID_VANZARE,
                            pack_facturare.nid_vanzare,
                            0,
                            A.ID_VANZARE_DET) AS ID_VANZARE_DET,
                     DECODE(A.ID_VANZARE,
                            pack_facturare.nid_vanzare,
                            0,
                            A.CANTITATE) AS CANTITATE,
                     A.ID_ARTICOL,
                     A.SERIE,
                     A.DISCOUNT_UNITAR,
                     A.ID_POL,
                     A.ID_VALUTA,
                     A.PRET,
                     A.ID_GESTIUNE,
                     A.CONT
                FROM VANZARI_DETALII A
               WHERE A.ID_VANZARE IN
                     (SELECT X as ID_VANZARE
                        FROM table(charn2collection(V_LISTA, V_SEPARATOR)))
                 AND A.STERS = 0
               ORDER BY A.ID_ARTICOL,
                        A.SERIE,
                        A.DISCOUNT_UNITAR,
                        A.ID_POL,
                        A.ID_VALUTA,
                        A.PRET,
                        A.ID_GESTIUNE,
                        A.CONT,
                        A.ID_VANZARE_DET DESC) A
       WHERE A.MAX_ID = pack_facturare.nid_vanzare
         AND A.ID_VANZARE_DET <> 0
         AND A.CANTITATE + A.TOTAL > 0
       ORDER BY A.MAX_IDT, A.ID_VANZARE_DET;

  END scrie_cantitati_vanzari_avize;
  -------------------------------------------------------------------
  PROCEDURE scrie_corespondente_vanzari(V_TIP IN NUMBER) IS
    V_LISTAID   VARCHAR2(2000);
    V_SEPARATOR VARCHAR2(1) := ',';
  BEGIN
    IF V_TIP = 1 THEN
      -- facturare din aviz:
      V_LISTAID := SUBSTR(pack_facturare.clistaid_avize,
                          1,
                          LENGTH(pack_facturare.clistaid_avize) - 1);
    ELSE
      V_LISTAID := pack_facturare.clistaid;
    END IF;

    INSERT INTO VANZARI_CORESP
      (ID_VANZARE_FACT, ID_VANZARE_AVIZ, TIP)
      SELECT pack_facturare.nid_vanzare as ID_VANZARE_FACT,
             ID_VANZARE                 as ID_VANZARE_AVIZ,
             V_TIP                      as TIP
        FROM VANZARI
       WHERE ID_VANZARE IN
             (SELECT X as ID_VANZARE
                FROM table(charn2collection(V_LISTAID, V_SEPARATOR))

              );
    -- Completez VANZARI.AVIZE redundant, pentru a nu face mai rapid fact_vfacturi (selecta din fact_vdetalii_avize)
    UPDATE VANZARI
       SET AVIZE =
           SUBSTR((select stringagg(nvl(a2.serie_act, '') || CHR(32) ||
                             a2.numar_act) as nr_avize
              from vanzari_coresp a1
              join vanzari a2
                on a1.id_vanzare_aviz = a2.id_vanzare
             where a1.id_vanzare_fact = pack_facturare.nid_vanzare),1,1000);

  END scrie_corespondente_vanzari;
  -------------------------------------------------------------------
  PROCEDURE cauta_date_ultima_factura(V_ID_PART      IN NUMBER,
                                      V_ID_SUCURSALA IN NUMBER,
                                      V_ID_DELEGAT   OUT NUMBER,
                                      V_NUME_DELEGAT OUT VARCHAR2,
                                      V_SERIE_BI     OUT VARCHAR2,
                                      V_CNP          OUT VARCHAR2,
                                      V_ID_MASINA    OUT NUMBER,
                                      V_NRINMAT      OUT VARCHAR2) IS
  BEGIN
    BEGIN
      SELECT A.ID_DELEGAT,
             B.DENUMIRE,
             B.REG_COMERT,
             B.COD_FISCAL,
             A.ID_MASINA,
             C.NRINMAT
        INTO V_ID_DELEGAT,
             V_NUME_DELEGAT,
             V_SERIE_BI,
             V_CNP,
             V_ID_MASINA,
             V_NRINMAT
        FROM (SELECT ID_DELEGAT,
                     ID_MASINA,
                     ROW_NUMBER() OVER(ORDER BY DATA_ACT DESC) AS NRCRT
                FROM VANZARI
               WHERE STERS = 0
                 AND ID_PART = V_ID_PART
                 AND NVL(ID_SUCURSALA, -99) = NVL(V_ID_SUCURSALA, -99)) A
        LEFT JOIN NOM_PARTENERI B
          ON A.ID_DELEGAT = B.ID_PART
         AND B.STERS = 0
         AND B.INACTIV = 0
        LEFT JOIN NOM_MASINI C
          ON A.ID_MASINA = C.ID_MASINA
         AND C.STERS = 0
         AND C.INACTIV = 0
       WHERE A.NRCRT = 1;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        BEGIN
          -- daca nu am gasit datele de pe ultima factura, iar id_part corespunde unei persoane fizice,
          -- atunci iau datele acesteia
          SELECT ID_PART, DENUMIRE, REG_COMERT, COD_FISCAL
            INTO V_ID_DELEGAT, V_NUME_DELEGAT, V_SERIE_BI, V_CNP
            FROM NOM_PARTENERI
           WHERE ID_PART = V_ID_PART
             AND TIP_PERSOANA = 2;

          MERGE INTO CORESP_TIP_PART
          USING DUAL
          ON (ID_TIP_PART = pack_sesiune.getoptiunefirma(USER, 'IDTIPDELEGATI') AND ID_PART = V_ID_PART)
          WHEN NOT MATCHED THEN
            INSERT
              (ID_TIP_PART, ID_PART)
            VALUES
              (pack_sesiune.getoptiunefirma(USER, 'IDTIPDELEGATI'),
               V_ID_PART);

          MERGE INTO CORESP_DELEGATI_PART
          USING DUAL
          ON (ID_DELEGAT = V_ID_PART AND ID_PART = V_ID_PART)
          WHEN NOT MATCHED THEN
            INSERT (ID_DELEGAT, ID_PART) VALUES (V_ID_PART, V_ID_PART);

        EXCEPTION
          WHEN NO_DATA_FOUND THEN
            V_ID_DELEGAT   := NULL;
            V_NUME_DELEGAT := '';
            V_SERIE_BI     := '';
            V_CNP          := '';
        END;
        V_ID_MASINA := NULL;
        V_NRINMAT   := '';
    END;
  END cauta_date_ultima_factura;
  --------------------------------------------------------------------------------------------------------------
  -- caut datele ultimei facturi dupa tip (pentru avize de transfer intre subunitati, nu am client)
  PROCEDURE cauta_date_ultima_factura_tip(V_TIP          IN NUMBER,
                                          V_ID_SUCURSALA IN NUMBER,
                                          V_ID_DELEGAT   OUT NUMBER,
                                          V_NUME_DELEGAT OUT VARCHAR2,
                                          V_SERIE_BI     OUT VARCHAR2,
                                          V_CNP          OUT VARCHAR2,
                                          V_ID_MASINA    OUT NUMBER,
                                          V_NRINMAT      OUT VARCHAR2) IS
  BEGIN
    BEGIN
      SELECT A.ID_DELEGAT,
             B.DENUMIRE,
             B.REG_COMERT,
             B.COD_FISCAL,
             A.ID_MASINA,
             C.NRINMAT
        INTO V_ID_DELEGAT,
             V_NUME_DELEGAT,
             V_SERIE_BI,
             V_CNP,
             V_ID_MASINA,
             V_NRINMAT
        FROM (SELECT ID_DELEGAT,
                     ID_MASINA,
                     ROW_NUMBER() OVER(ORDER BY ID_VANZARE DESC) AS NRCRT
                FROM VANZARI
               WHERE STERS = 0
                 AND TIP = V_TIP
                 AND NVL(ID_SUCURSALA, -99) = NVL(V_ID_SUCURSALA, -99)) A
        LEFT JOIN NOM_PARTENERI B
          ON A.ID_DELEGAT = B.ID_PART
         AND B.STERS = 0
         AND B.INACTIV = 0
        LEFT JOIN NOM_MASINI C
          ON A.ID_MASINA = C.ID_MASINA
         AND C.STERS = 0
         AND C.INACTIV = 0
       WHERE A.NRCRT = 1;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_ID_DELEGAT   := NULL;
        V_NUME_DELEGAT := '';
        V_SERIE_BI     := '';
        V_CNP          := '';
        V_ID_MASINA    := NULL;
        V_NRINMAT      := '';
    END;

  END cauta_date_ultima_factura_tip;
  ----------------------------------------------------------------------------
  PROCEDURE cauta_date_comanda(V_ID_CLIENT    IN NUMBER,
                               V_ID_SUCURSALA IN NUMBER,
                               V_DATA_FACTURA IN DATE,
                               V_LISTAID      OUT VARCHAR2,
                               V_DESCRIERE    OUT VARCHAR2) is
  BEGIN
    BEGIN
      SELECT TO_CHAR(ID_COMANDA), NR_COMANDA
        INTO V_LISTAID, V_DESCRIERE
        FROM (SELECT ROW_NUMBER() OVER(ORDER BY DATA_LIVRARE) AS NR_CRT,
                     ID_COMANDA,
                     NR_COMANDA
                FROM VCOMENZI
               WHERE FACTURAT = 0
                 AND INTERNA = 2
                 AND ID_PART = V_ID_CLIENT
                 AND NVL(ID_SUCURSALA, -99) = NVL(V_ID_SUCURSALA, -99)
                 AND TRUNC(DATA_LIVRARE) = TRUNC(V_DATA_FACTURA))
       WHERE NR_CRT = 1;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_LISTAID   := NULL;
        V_DESCRIERE := NULL;
    END;
  END cauta_date_comanda;
  ----------------------------------------------------------------------------
  PROCEDURE cauta_date_comanda_gest(V_ID_GESTIUNE  IN NUMBER,
                                    V_ID_SUCURSALA IN NUMBER,
                                    V_DATA_FACTURA IN DATE,
                                    V_LISTAID      OUT VARCHAR2,
                                    V_DESCRIERE    OUT VARCHAR2) is
  BEGIN
    BEGIN
      SELECT TO_CHAR(ID_COMANDA), NR_COMANDA
        INTO V_LISTAID, V_DESCRIERE
        FROM (SELECT ROW_NUMBER() OVER(ORDER BY DATA_LIVRARE) AS NR_CRT,
                     ID_COMANDA,
                     NR_COMANDA
                FROM VCOMENZI
               WHERE FACTURAT = 0
                 AND NVL(ID_SUCURSALA, -99) = NVL(V_ID_SUCURSALA, -99)
                 AND INTERNA = 3
                 AND ID_GESTIUNE = V_ID_GESTIUNE
                 AND TRUNC(DATA_LIVRARE) = TRUNC(V_DATA_FACTURA))
       WHERE NR_CRT = 1;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        V_LISTAID   := NULL;
        V_DESCRIERE := NULL;
    END;
  END cauta_date_comanda_gest;
  ----------------------------------------------------------------------------
  -- e folosita in view-urile fact_vrap_centralizator_* ( in afara de _fact )
  FUNCTION calculeaza_total_cu_tva(V_PRET                IN NUMBER,
                                   V_AJUSTARE            IN NUMBER,
                                   V_CURS                IN NUMBER,
                                   V_DISCOUNT_UNITAR     IN NUMBER,
                                   V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                   V_CANTITATE           IN NUMBER,
                                   V_PRET_CU_TVA         IN NUMBER,
                                   V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER IS
  BEGIN
    -- APELEZ calculeaza_total_cu_tva cu V_ZECIMALE_PRET = 0
    RETURN pack_sesiune.calculeaza_total_cu_tva(V_PRET                => V_PRET,
                                                V_AJUSTARE            => V_AJUSTARE,
                                                V_CURS                => V_CURS,
                                                V_DISCOUNT_UNITAR     => V_DISCOUNT_UNITAR,
                                                V_DISCOUNT_EVIDENTIAT => V_DISCOUNT_EVIDENTIAT,
                                                V_CANTITATE           => V_CANTITATE,
                                                V_PRET_CU_TVA         => V_PRET_CU_TVA,
                                                V_PROC_TVAV           => V_PROC_TVAV,
                                                V_ZECIMALE_PRET       => NULL,
                                                V_ZECIMALE_SUMA       => NULL);

  END calculeaza_total_cu_tva;
  -------------------------------------------------------------------
  FUNCTION calculeaza_total_cu_tva(V_PRET                IN NUMBER,
                                   V_AJUSTARE            IN NUMBER,
                                   V_CURS                IN NUMBER,
                                   V_DISCOUNT_UNITAR     IN NUMBER,
                                   V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                   V_CANTITATE           IN NUMBER,
                                   V_PRET_CU_TVA         IN NUMBER,
                                   V_PROC_TVAV           IN NUMBER,
                                   V_ZECIMALE_PRET       IN NUMBER,
                                   V_ZECIMALE_SUMA       IN NUMBER)
    RETURN NUMBER IS
  BEGIN
    RETURN pack_sesiune.calculeaza_total_cu_tva(V_PRET,
                                                V_AJUSTARE,
                                                V_CURS,
                                                V_DISCOUNT_UNITAR,
                                                V_DISCOUNT_EVIDENTIAT,
                                                V_CANTITATE,
                                                V_PRET_CU_TVA,
                                                V_PROC_TVAV,
                                                V_ZECIMALE_PRET,
                                                V_ZECIMALE_SUMA);
  END calculeaza_total_cu_tva;
  -------------------------------------------------------------------
  -- e folosita in view-ul fact_vrap_centralizator_fact
  FUNCTION calculeaza_total_cu_tva_fact(V_PRET                IN NUMBER,
                                        V_DIFERENTA           IN NUMBER,
                                        V_CURS                IN NUMBER,
                                        V_DISCOUNT_UNITAR     IN NUMBER,
                                        V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                        V_CANTITATE           IN NUMBER,
                                        V_PRET_CU_TVA         IN NUMBER,
                                        V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER IS
    V_ZECIMALE_SUMA  NUMBER(1) := pack_sesiune.getOptiuneFirma('PC');
    V_ZECIMALE_PRETV NUMBER(1) := pack_sesiune.getOptiuneFirma('PPRETV');
    V_SUMA_CU_TVA    ACT.SUMA%TYPE;
  BEGIN
    IF V_PRET_CU_TVA = 1 THEN
      IF V_DISCOUNT_EVIDENTIAT = 1 THEN
        V_SUMA_CU_TVA := ROUND((ROUND(NVL(V_CURS, 1) * V_PRET,
                                      V_ZECIMALE_PRETV) -
                               ROUND(V_DIFERENTA * V_PROC_TVAV,
                                      V_ZECIMALE_PRETV)) * V_CANTITATE,
                               V_ZECIMALE_SUMA) -
                         ROUND(ROUND(NVL(V_CURS, 1) *
                                     NVL(V_DISCOUNT_UNITAR, 0),
                                     V_ZECIMALE_PRETV) * V_CANTITATE,
                               V_ZECIMALE_SUMA);
      ELSE
        V_SUMA_CU_TVA := ROUND((ROUND(NVL(V_CURS, 1) *
                                      ROUND(V_PRET, V_ZECIMALE_PRETV),
                                      V_ZECIMALE_PRETV) -
                               ROUND(NVL(V_CURS, 1) *
                                      ROUND(NVL(V_DISCOUNT_UNITAR, 0),
                                            V_ZECIMALE_PRETV) -
                                      ROUND(V_DIFERENTA * V_PROC_TVAV,
                                            V_ZECIMALE_PRETV),
                                      V_ZECIMALE_PRETV)) * V_CANTITATE,
                               V_ZECIMALE_SUMA);
      END IF;
    ELSE
      V_SUMA_CU_TVA := pack_facturare.calculeaza_total_fara_tva_fact(V_PRET,
                                                                     V_DIFERENTA,
                                                                     V_CURS,
                                                                     V_DISCOUNT_UNITAR,
                                                                     V_DISCOUNT_EVIDENTIAT,
                                                                     V_CANTITATE,
                                                                     V_PRET_CU_TVA,
                                                                     V_PROC_TVAV) +
                       pack_facturare.calculeaza_total_tva_fact(V_PRET,
                                                                V_DIFERENTA,
                                                                V_CURS,
                                                                V_DISCOUNT_UNITAR,
                                                                V_DISCOUNT_EVIDENTIAT,
                                                                V_CANTITATE,
                                                                V_PRET_CU_TVA,
                                                                V_PROC_TVAV);
    END IF;
    RETURN V_SUMA_CU_TVA;
  END calculeaza_total_cu_tva_fact;
  -------------------------------------------------------------------
  -- e folosita in view-urile fact_vrap_centralizator_* ( in afara de _fact )
  FUNCTION calculeaza_total_fara_tva(V_PRET                IN NUMBER,
                                     V_AJUSTARE            IN NUMBER,
                                     V_CURS                IN NUMBER,
                                     V_DISCOUNT_UNITAR     IN NUMBER,
                                     V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                     V_CANTITATE           IN NUMBER,
                                     V_PRET_CU_TVA         IN NUMBER,
                                     V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER IS
  BEGIN
    -- APELEZ calculeaza_total_cu_tva cu V_ZECIMALE_PRET = 0
    RETURN pack_sesiune.calculeaza_total_fara_tva(V_PRET                => V_PRET,
                                                  V_AJUSTARE            => V_AJUSTARE,
                                                  V_CURS                => V_CURS,
                                                  V_DISCOUNT_UNITAR     => V_DISCOUNT_UNITAR,
                                                  V_DISCOUNT_EVIDENTIAT => V_DISCOUNT_EVIDENTIAT,
                                                  V_CANTITATE           => V_CANTITATE,
                                                  V_PRET_CU_TVA         => V_PRET_CU_TVA,
                                                  V_PROC_TVAV           => V_PROC_TVAV,
                                                  V_ZECIMALE_PRET       => NULL,
                                                  V_ZECIMALE_SUMA       => NULL);

  END calculeaza_total_fara_tva;
  -------------------------------------------------------------------
  FUNCTION calculeaza_total_fara_tva(V_PRET                IN NUMBER,
                                     V_AJUSTARE            IN NUMBER,
                                     V_CURS                IN NUMBER,
                                     V_DISCOUNT_UNITAR     IN NUMBER,
                                     V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                     V_CANTITATE           IN NUMBER,
                                     V_PRET_CU_TVA         IN NUMBER,
                                     V_PROC_TVAV           IN NUMBER,
                                     V_ZECIMALE_PRET       IN NUMBER,
                                     V_ZECIMALE_SUMA       IN NUMBER)
    RETURN NUMBER IS
  BEGIN
    RETURN pack_sesiune.calculeaza_total_fara_tva(V_PRET,
                                                  V_AJUSTARE,
                                                  V_CURS,
                                                  V_DISCOUNT_UNITAR,
                                                  V_DISCOUNT_EVIDENTIAT,
                                                  V_CANTITATE,
                                                  V_PRET_CU_TVA,
                                                  V_PROC_TVAV,
                                                  V_ZECIMALE_PRET,
                                                  V_ZECIMALE_SUMA);
  END calculeaza_total_fara_tva;
  -------------------------------------------------------------------
  -- e folosita in view-ul fact_vrap_centralizator_fact
  FUNCTION calculeaza_total_fara_tva_fact(V_PRET                IN NUMBER,
                                          V_DIFERENTA           IN NUMBER,
                                          V_CURS                IN NUMBER,
                                          V_DISCOUNT_UNITAR     IN NUMBER,
                                          V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                          V_CANTITATE           IN NUMBER,
                                          V_PRET_CU_TVA         IN NUMBER,
                                          V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER IS
    V_ZECIMALE_SUMA  NUMBER(1) := pack_sesiune.getOptiuneFirma('PC');
    V_ZECIMALE_PRETV NUMBER(1) := pack_sesiune.getOptiuneFirma('PPRETV');
    V_SUMA_FARA_TVA  ACT.SUMA%TYPE;
  BEGIN
    IF V_PRET_CU_TVA = 1 THEN
      V_SUMA_FARA_TVA := pack_facturare.calculeaza_total_cu_tva_fact(V_PRET,
                                                                     V_DIFERENTA,
                                                                     V_CURS,
                                                                     V_DISCOUNT_UNITAR,
                                                                     V_DISCOUNT_EVIDENTIAT,
                                                                     V_CANTITATE,
                                                                     V_PRET_CU_TVA,
                                                                     V_PROC_TVAV) -
                         pack_facturare.calculeaza_total_tva_fact(V_PRET,
                                                                  V_DIFERENTA,
                                                                  V_CURS,
                                                                  V_DISCOUNT_UNITAR,
                                                                  V_DISCOUNT_EVIDENTIAT,
                                                                  V_CANTITATE,
                                                                  V_PRET_CU_TVA,
                                                                  V_PROC_TVAV);
    ELSE
      IF V_DISCOUNT_EVIDENTIAT = 1 THEN
        V_SUMA_FARA_TVA := ROUND((ROUND(NVL(V_CURS, 1) * V_PRET,
                                        V_ZECIMALE_PRETV) - V_DIFERENTA) *
                                 V_CANTITATE,
                                 V_ZECIMALE_SUMA) -
                           ROUND(ROUND(NVL(V_CURS, 1) *
                                       NVL(V_DISCOUNT_UNITAR, 0),
                                       V_ZECIMALE_PRETV) * V_CANTITATE,
                                 V_ZECIMALE_SUMA);
      ELSE
        V_SUMA_FARA_TVA := ROUND((ROUND(NVL(V_CURS, 1) *
                                        ROUND(V_PRET, V_ZECIMALE_PRETV),
                                        V_ZECIMALE_PRETV) -
                                 ROUND(NVL(V_CURS, 1) *
                                        ROUND(NVL(V_DISCOUNT_UNITAR, 0),
                                              V_ZECIMALE_PRETV),
                                        V_ZECIMALE_PRETV) - V_DIFERENTA) *
                                 V_CANTITATE,
                                 V_ZECIMALE_SUMA);
      END IF;
    END IF;
    RETURN V_SUMA_FARA_TVA;
  END calculeaza_total_fara_tva_fact;
  -------------------------------------------------------------------
  -- e folosita in view-urile fact_vrap_centralizator_* ( in afara de _fact )
  FUNCTION calculeaza_total_tva(V_PRET                IN NUMBER,
                                V_AJUSTARE            IN NUMBER,
                                V_CURS                IN NUMBER,
                                V_DISCOUNT_UNITAR     IN NUMBER,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_CANTITATE           IN NUMBER,
                                V_PRET_CU_TVA         IN NUMBER,
                                V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER IS
  BEGIN
    -- APELEZ calculeaza_total_cu_tva cu V_ZECIMALE_PRET = 0
    Return pack_sesiune.calculeaza_total_tva(V_PRET                => V_PRET,
                                             V_AJUSTARE            => V_AJUSTARE,
                                             V_CURS                => V_CURS,
                                             V_DISCOUNT_UNITAR     => V_DISCOUNT_UNITAR,
                                             V_DISCOUNT_EVIDENTIAT => V_DISCOUNT_EVIDENTIAT,
                                             V_CANTITATE           => V_CANTITATE,
                                             V_PRET_CU_TVA         => V_PRET_CU_TVA,
                                             V_PROC_TVAV           => V_PROC_TVAV,
                                             V_ZECIMALE_PRET       => NULL,
                                             V_ZECIMALE_SUMA       => NULL);

  END calculeaza_total_tva;
  -------------------------------------------------------------------
  FUNCTION calculeaza_total_tva(V_PRET                IN NUMBER,
                                V_AJUSTARE            IN NUMBER,
                                V_CURS                IN NUMBER,
                                V_DISCOUNT_UNITAR     IN NUMBER,
                                V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                V_CANTITATE           IN NUMBER,
                                V_PRET_CU_TVA         IN NUMBER,
                                V_PROC_TVAV           IN NUMBER,
                                V_ZECIMALE_PRET       IN NUMBER,
                                V_ZECIMALE_SUMA       IN NUMBER)
    RETURN NUMBER IS
  BEGIN
    RETURN pack_sesiune.calculeaza_total_tva(V_PRET,
                                             V_AJUSTARE,
                                             V_CURS,
                                             V_DISCOUNT_UNITAR,
                                             V_DISCOUNT_EVIDENTIAT,
                                             V_CANTITATE,
                                             V_PRET_CU_TVA,
                                             V_PROC_TVAV,
                                             V_ZECIMALE_PRET,
                                             V_ZECIMALE_SUMA);
  END calculeaza_total_tva;
  ----------------------------------------------------------------------------------------
  -- e folosita in view-ul fact_vrap_centralizator_fact
  FUNCTION calculeaza_total_tva_fact(V_PRET                IN NUMBER,
                                     V_DIFERENTA           IN NUMBER,
                                     V_CURS                IN NUMBER,
                                     V_DISCOUNT_UNITAR     IN NUMBER,
                                     V_DISCOUNT_EVIDENTIAT IN NUMBER,
                                     V_CANTITATE           IN NUMBER,
                                     V_PRET_CU_TVA         IN NUMBER,
                                     V_PROC_TVAV           IN NUMBER)
    RETURN NUMBER IS
    V_ZECIMALE_SUMA  NUMBER(1) := pack_sesiune.getOptiuneFirma('PC');
    V_ZECIMALE_PRETV NUMBER(1) := pack_sesiune.getOptiuneFirma('PPRETV');
    V_SUMA_TVA       ACT.SUMA%TYPE;
  BEGIN
    IF V_PRET_CU_TVA = 1 THEN
      V_SUMA_TVA := ROUND(pack_facturare.calculeaza_total_cu_tva_fact(V_PRET,
                                                                      V_DIFERENTA,
                                                                      V_CURS,
                                                                      V_DISCOUNT_UNITAR,
                                                                      V_DISCOUNT_EVIDENTIAT,
                                                                      V_CANTITATE,
                                                                      V_PRET_CU_TVA,
                                                                      V_PROC_TVAV) *
                          (V_PROC_TVAV - 1) / V_PROC_TVAV,
                          V_ZECIMALE_SUMA);
    ELSE
      IF V_DISCOUNT_EVIDENTIAT = 1 THEN
        V_SUMA_TVA := ROUND(ROUND((ROUND(NVL(V_CURS, 1) * V_PRET,
                                         V_ZECIMALE_PRETV) - V_DIFERENTA) *
                                  V_CANTITATE,
                                  V_ZECIMALE_SUMA) * (V_PROC_TVAV - 1),
                            V_ZECIMALE_SUMA) -
                      ROUND(ROUND(ROUND(NVL(V_CURS, 1) *
                                        NVL(V_DISCOUNT_UNITAR, 0),
                                        V_ZECIMALE_PRETV) * V_CANTITATE,
                                  V_ZECIMALE_SUMA) * (V_PROC_TVAV - 1),
                            V_ZECIMALE_SUMA);
      ELSE
        V_SUMA_TVA := ROUND(ROUND((ROUND(NVL(V_CURS, 1) *
                                         ROUND(V_PRET, V_ZECIMALE_PRETV),
                                         V_ZECIMALE_PRETV) -
                                  ROUND(NVL(V_CURS, 1) *
                                         ROUND(NVL(V_DISCOUNT_UNITAR, 0),
                                               V_ZECIMALE_PRETV),
                                         V_ZECIMALE_PRETV) - V_DIFERENTA) *
                                  V_CANTITATE,
                                  V_ZECIMALE_SUMA) * (V_PROC_TVAV - 1),
                            V_ZECIMALE_SUMA);
      END IF;
    END IF;
    RETURN V_SUMA_TVA;
  END calculeaza_total_tva_fact;
  -------------------------------------------------------------------
  PROCEDURE actualizeaza_vanzari(V_COD_VECHI IN NUMBER,
                                 V_COD_NOU   IN NUMBER) is
  BEGIN
    -- de modificat in caz ca il las sa stearga manual inregistrari din VANZARI_DETALII
    -- acum se marcheaza cu STERS = 1 doar cand se sterge toata factura
    UPDATE VANZARI_DETALII
       SET STERS = 0
     WHERE ID_VANZARE IN
           (SELECT ID_VANZARE FROM VANZARI WHERE COD = V_COD_VECHI);
    UPDATE VANZARI SET COD = V_COD_NOU, STERS = 0 WHERE COD = V_COD_VECHI;
  END actualizeaza_vanzari;
  -------------------------------------------------------------------
  PROCEDURE sterge_din_vanzari(V_COD     IN NUMBER,
                               V_AN      IN NUMBER,
                               V_LUNA    IN NUMBER,
                               V_ID_UTIL IN NUMBER) is
    V_ID_VANZARE VANZARI.ID_VANZARE%TYPE;
  BEGIN
    BEGIN
      SELECT ID_VANZARE INTO V_ID_VANZARE FROM VANZARI WHERE COD = V_COD;
      pack_facturare.sterge_factura(V_ID_VANZARE, V_LUNA, V_AN, V_ID_UTIL);
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        RAISE_APPLICATION_ERROR(-20000,
                                'Aceasta factura nu a fost inregistrata in ROAFACTURARE! (FACT-017 : ' ||
                                V_COD || ')');
    END;
  END sterge_din_vanzari;
  -------------------------------------------------------------------
  PROCEDURE verifica_cursuri_valute(V_DATA_CURS IN DATE,
                                    V_ID_UTIL   IN NUMBER) IS
    V_NUME_VALUTE VARCHAR2(100);
  BEGIN
    -- verificare ca exista cursul pentru valutele din FACT_VPRETURI_UTILIZATOR
    SELECT STRINGAGG(B.NUME_VAL)
      INTO V_NUME_VALUTE
      FROM (SELECT A.ID_VALUTA, B.CURS
              FROM (SELECT DISTINCT ID_VALUTA
                      FROM FACT_VPRETURI_UTILIZATOR
                     WHERE ID_UTIL = V_ID_UTIL) A
              LEFT JOIN CURS B
                ON A.ID_VALUTA = B.ID_VALUTA
               AND B.STERS = 0
               AND B.DATA <= V_DATA_CURS
               AND B.DATA2 >= V_DATA_CURS) A
      LEFT JOIN NOM_VALUTE B
        ON A.ID_VALUTA = B.ID_VALUTA
     WHERE A.CURS IS NULL
       AND A.ID_VALUTA <> pack_facturare.nid_moneda_nationala;

    IF V_NUME_VALUTE IS NOT NULL THEN
      RAISE_APPLICATION_ERROR(-20005,
                              'Nu este setat cursul din data de ' ||
                              to_char(V_DATA_CURS, 'DD/MM/YYYY') ||
                              ' pentru ' || V_NUME_VALUTE || '!');
    END IF;
  END verifica_cursuri_valute;
  -------------------------------------------------------------------
  PROCEDURE verifica_total_document IS
    V_TOTFTVA_VER ACT.SUMA%TYPE;
    V_ID_TOTFTVA  ACT.SUMA%TYPE;
    V_TOTTVA_VER  ACT.SUMA%TYPE;
    V_ID_TOTTVA   ACT.SUMA%TYPE;
  BEGIN
    IF pack_facturare.ntip < 20 or
       pack_facturare.ntip in (pack_facturare.nTipFacturaHotel,
                               pack_facturare.nTipFacturaRestaurant,
                               48,
                               49,51,52) THEN
      SELECT SUM(CASE
                   WHEN SCD in ('667', '267', '2678', '709') THEN
                    (-1) * SUMA
                   WHEN SCC <> '4427' THEN
                    SUMA
                   ELSE
                    0
                 END),
             SUM(CASE
                   WHEN SCC = '4427' THEN
                    SUMA
                   ELSE
                    0
                 END),
             MIN(CASE
                   WHEN SCC <> '4427' AND
                        SCD not in ('667', '267', '2678', '709') THEN
                    ID_ACT
                   ELSE
                    9999999
                 END),
             MIN(CASE
                   WHEN SCC = '4427' THEN
                    ID_ACT
                   ELSE
                    9999999
                 END)
        INTO V_TOTFTVA_VER, V_TOTTVA_VER, V_ID_TOTFTVA, V_ID_TOTTVA
        FROM ACT_TEMP
       WHERE (SCD = '4111' OR (SCC = '4111' AND SCD not in ('5311','5314','5121','5125','5126')));
    ELSE
      SELECT SUM(CASE
                   WHEN SCD in ('667', '267', '2678', '709') THEN
                    (-1) * SUMA
                   WHEN SCC <> '4428' THEN
                    SUMA
                   ELSE
                    0
                 END),
             SUM(CASE
                   WHEN SCC = '4428' THEN
                    SUMA
                   ELSE
                    0
                 END),
             MIN(CASE
                   WHEN SCC <> '4428' AND
                        SCD not in ('667', '267', '2678', '709') THEN
                    ID_ACT
                   ELSE
                    9999999
                 END),
             MIN(CASE
                   WHEN SCC = '4428' THEN
                    ID_ACT
                   ELSE
                    9999999
                 END)
        INTO V_TOTFTVA_VER, V_TOTTVA_VER, V_ID_TOTFTVA, V_ID_TOTTVA
        FROM ACT_TEMP
       WHERE (SCD = '418' OR SCC = '418');
    END IF;
    IF NVL(pack_facturare.ntotftva, 0) <> 0 and
       NVL(pack_facturare.ntotftva, 0) <> V_TOTFTVA_VER THEN

      pack_facturare.ndifftva := pack_facturare.ntotftva - V_TOTFTVA_VER;
      insert into act_temp
        (id_act,
         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,
         sters,
         id_factd,
         id_factc,
         id_ctr,
         id_jtva_coloana,
         serie_act,
         tva_incasare,
         validat,
         id_utilv,
         dataorav,
         taxcode)
        select b.id_act,
               a.luna,
               a.an,
               a.cod,
               a.dataireg,
               a.nract,
               a.dataact,
               a.explicatia,
               a.scd,
               a.ascd,
               a.scc,
               a.ascc,
               pack_facturare.ndifftva      as suma,
               a.pereched,
               a.perechec,
               0                            as suma_val,
               a.curs,
               a.datascad,
               a.neimpozab,
               a.nnir,
               a.id_util,
               a.dataora,
               a.id_utils,
               a.dataoras,
               a.id_responsabil,
               a.id_venchelt,
               a.id_sectie,
               a.id_set,
               a.id_fact,
               a.id_partd,
               a.id_partc,
               a.id_sucursala,
               a.id_fdoc,
               a.explicatia4,
               a.explicatia5,
               a.id_lucrare,
               a.id_gestin,
               a.id_gestout,
               a.id_valuta,
               a.proc_tva,
               a.sters,
               a.id_factd,
               a.id_factc,
               a.id_ctr,
               a.id_jtva_coloana,
               a.serie_act,
               pack_facturare.ntva_incasare,
               a.validat,
               a.id_utilv,
               a.dataorav,
               a.taxcode
          from act_temp a
          left join (select max(id_act) + 1 as id_act, 0 as sters
                       from act_temp) b
            on a.sters = b.sters
         where a.id_act = V_ID_TOTFTVA;
    END IF;
    IF NVL(pack_facturare.ntottva, 0) <> 0 and
       NVL(pack_facturare.ntottva, 0) <> V_TOTTVA_VER THEN

      pack_facturare.ndiftva := pack_facturare.ntottva - V_TOTTVA_VER;

      insert into act_temp
        (id_act,
         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,
         sters,
         id_factd,
         id_factc,
         id_ctr,
         id_jtva_coloana,
         serie_act,
         tva_incasare,
         validat,
         id_utilv,
         dataorav,
         taxcode)
        select b.id_act,
               a.luna,
               a.an,
               a.cod,
               a.dataireg,
               a.nract,
               a.dataact,
               a.explicatia,
               a.scd,
               a.ascd,
               a.scc,
               a.ascc,
               pack_facturare.ndiftva       as suma,
               a.pereched,
               a.perechec,
               0                            as suma_val,
               a.curs,
               a.datascad,
               a.neimpozab,
               a.nnir,
               a.id_util,
               a.dataora,
               a.id_utils,
               a.dataoras,
               a.id_responsabil,
               a.id_venchelt,
               a.id_sectie,
               a.id_set,
               a.id_fact,
               a.id_partd,
               a.id_partc,
               a.id_sucursala,
               a.id_fdoc,
               a.explicatia4,
               a.explicatia5,
               a.id_lucrare,
               a.id_gestin,
               a.id_gestout,
               a.id_valuta,
               a.proc_tva,
               a.sters,
               a.id_factd,
               a.id_factc,
               a.id_ctr,
               a.id_jtva_coloana,
               a.serie_act,
               pack_facturare.ntva_incasare,
               a.validat,
               a.id_utilv,
               a.dataorav,
               a.taxcode
          from act_temp a
          left join (select max(id_act) + 1 as id_act, 0 as sters
                       from act_temp) b
            on a.sters = b.sters
         where a.id_act = V_ID_TOTTVA;
    END IF;

    IF pack_facturare.ntip in (48, 49) AND
       ((NVL(pack_facturare.ntottva, 0) <> 0 and
       NVL(pack_facturare.ntottva, 0) <> V_TOTTVA_VER) OR
       (NVL(pack_facturare.ntotftva, 0) <> 0 and
       NVL(pack_facturare.ntotftva, 0) <> V_TOTFTVA_VER)) THEN
      insert into act_temp
        (id_act,
         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,
         sters,
         id_factd,
         id_factc,
         id_ctr,
         id_jtva_coloana,
         serie_act,
         tva_incasare,
         validat,
         id_utilv,
         dataorav)
        select b.id_act,
               a.luna,
               a.an,
               a.cod,
               a.dataireg,
               a.nract,
               a.dataact,
               a.explicatia,
               a.scd,
               a.ascd,
               a.scc,
               a.ascc,
               pack_facturare.ndifftva + pack_facturare.ndiftva as suma,
               a.pereched,
               a.perechec,
               0 as suma_val,
               a.curs,
               a.datascad,
               a.neimpozab,
               a.nnir,
               a.id_util,
               a.dataora,
               a.id_utils,
               a.dataoras,
               a.id_responsabil,
               a.id_venchelt,
               a.id_sectie,
               a.id_set,
               a.id_fact,
               a.id_partd,
               a.id_partc,
               a.id_sucursala,
               a.id_fdoc,
               a.explicatia4,
               a.explicatia5,
               a.id_lucrare,
               a.id_gestin,
               a.id_gestout,
               a.id_valuta,
               a.proc_tva,
               a.sters,
               a.id_factd,
               a.id_factc,
               a.id_ctr,
               a.id_jtva_coloana,
               a.serie_act,
               pack_facturare.ntva_incasare,
               a.validat,
               a.id_utilv,
               a.dataorav
          from act_temp a
          left join (select max(id_act) + 1 as id_act, 0 as sters
                       from act_temp) b
            on a.sters = b.sters
         where a.scc = '357';
    END IF;

  END verifica_total_document;
  -------------------------------------------------------------------
  PROCEDURE verifica_analitice378 is
  BEGIN
    SELECT DECODE(X, 0, 0, 1)
      INTO PACK_FACTURARE.NANALITICE_378
      FROM (SELECT COUNT(*) AS X
              FROM plcont
             WHERE CONT = '378'
               AND AN = PACK_FACTURARE.nan
               AND TIP_CONT = 1
               AND INACTIV = 0);
  END verifica_analitice378;
  -------------------------------------------------------------------
  function GetAnaliticByGrupUtilizatori(tnIdUtilizator ACT.ID_UTIL%TYPE,
                                        tcCont         ACT.SCD%TYPE)
    return ACT.ASCD%TYPE IS
    lcAcont ACT.ASCD%TYPE;
  BEGIN
    BEGIN
      select ga.acont
        into lcAcont
        from config_gruputil_analitice ga
        join utilizatori_rol_intern u
          on ga.id_grup = u.id_grup
       where u.id_util = tnIdUtilizator
         and ga.cont = tcCont
         and u.sters = 0;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        NULL;
    END;
    return lcAcont;
  end GetAnaliticByGrupUtilizatori;
  -------------------------------------------------------------------
  -- intoarce analiticul unui cont (371,607,378 etc.)
  -- pe baza configurarilor din config_analitice si a GESTIUNII/ARTICOLULUI
  FUNCTION GetAnaliticByArticol(tcCont       RUL.CONT%TYPE,
                                tnIdGestiune RUL.ID_GESTIUNE%TYPE DEFAULT NULL,
                                tnIdArticol  RUL.ID_ARTICOL%TYPE DEFAULT NULL)
    return RUL.ACONT%TYPE IS

    crs_linie      config_analitice%rowtype;
    lcSelect       VARCHAR2(10000);
    lcAnalitic     VARCHAR2(1000);
    lcColoanaP     VARCHAR2(100);
    lcColoanaS     VARCHAR2(100);
    lcNumeColoanaP VARCHAR2(1000);
    lcNumeColoanaS VARCHAR2(1000);
    lcAcont        RUL.ACONT%TYPE;
    lcParametruP   VARCHAR2(100);
    lcParametruS   VARCHAR2(100);
  BEGIN

    begin
      select *
        into crs_linie
        from config_analitice a
       where a.cont = tcCont;
    exception
      when NO_DATA_FOUND then
        return lcAcont;
    end;

    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;

    lcParametruP := CASE UPPER(lcNumeColoanaP)
                      WHEN 'ID_GESTIUNE' THEN
                       'tnIdGestiune'
                      WHEN 'ID_ARTICOL' THEN
                       'tnIdArticol'
                      ELSE
                       ''
                    END;
    lcParametruS := CASE UPPER(lcNumeColoanaS)
                      WHEN 'ID_GESTIUNE' THEN
                       'tnIdGestiune'
                      WHEN 'ID_ARTICOL' THEN
                       'tnIdArticol'
                      ELSE
                       ''
                    END;
    if lcParametruP is not null then
      lcSelect := 'SELECT ' || lcAnalitic || ' AS ACONT
                       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 ' || CASE UPPER(lcNumeColoanaP)
                    WHEN 'ID_GESTIUNE' THEN
                     tnIdGestiune
                    WHEN 'ID_ARTICOL' THEN
                     tnIdArticol
                  END || ' = ' || lcNumeColoanaP || ' ' || (CASE
                    WHEN lcColoanaS IS NULL or lcParametruS IS NULL THEN
                     ''
                    ELSE
                     'AND  ' || CASE UPPER(lcNumeColoanaS)
                       WHEN 'ID_GESTIUNE' THEN
                        'tnIdGestiune'
                       WHEN 'ID_ARTICOL' THEN
                        'tnIdArticol'
                     END || ' = ' || lcNumeColoanaS
                  END);

      EXECUTE IMMEDIATE lcSelect
        into lcAcont;
    END IF; -- lcParametruP

    return lcAcont;
  END GetAnaliticByArticol;
  -----------------------------------------------------------------------------------------------
  Procedure citeste_vanzari_seturi(V_COD    IN NUMBER,
                                   V_CURSOR OUT cursor_facturare) is
  begin
    OPEN V_CURSOR FOR
      select b2.id_vanzare_set,
             cast(a2.id_vanzare_det as number(10)) as id_vanzare_det,
             b2.denumire,
             b2.explicatie,
             b2.cantitate,
             b2.um,
             b2.serie,
             cast(decode(a2.in_valuta,
                         1,
                         round(b2.pret * a2.curs / a2.multiplicator,
                               pack_sesiune.nzecimale_pretv),
                         b2.pret) as number(14, 4)) as pret,
             cast(decode(a2.in_valuta, 1, b2.pret, 0) as number(14, 4)) as pret_val,
             a2.id_valuta,
             a2.in_valuta as tip_valuta,
             c2.nume_val,
             decode(a2.in_valuta,
                    1,
                    round(Nvl(b2.discount_unitar, 0) * a2.curs /
                          a2.multiplicator,
                          pack_sesiune.nzecimale_pretv),
                    Nvl(b2.discount_unitar, 0)) as discount_unitar,
             decode(a2.in_valuta, 1, Nvl(b2.discount_unitar, 0), 0) as discount_unitar_val,
             b2.pret_cu_tva as pret_cu_tva,
             a2.curs,
             a2.multiplicator,
             cast(0 as number(18)) as id_ctr, --       a2.id_ctr,
             cast(null as varchar2(100)) as numar_contract, --       c2.numar_contract,
             cast(a2.proc_tvav as number(8, 4)) as proc_Tvav,
             cast(a2.id_jtva_coloana as number(10)) as id_jtva_coloana,
             cast(a2.gestionabil as number(1)) as gestionabil,
             cast(null as varchar2(100)) as codmat,
             cast(null as varchar2(100)) as codmatf,
             cast(null as varchar2(100)) as codbare,
             cast(0 as number(14, 4)) as pret_achizitie,
             cast(null as number(10)) as id_gestiune
        from (select b.id_vanzare_set,
                     a.in_valuta,
                     --   b.id_ctr,
                     max(b.proc_tvav) as proc_tvav,
                     max(nvl2(b.id_gestiune, 1, 0)) as gestionabil,
                     min(b.id_jtva_coloana) as id_jtva_coloana,
                     min(b.id_vanzare_det) as id_vanzare_det,
                     c.curs,
                     c.multiplicator,
                     decode(a.in_valuta,
                            1,
                            c.id_valuta,
                            pack_def.getidmonedanationala()) as id_valuta
                from (select id_vanzare, in_valuta
                        from vanzari
                       where cod = V_COD
                         and sters = 0) a
                left join vanzari_detalii b
                  on a.id_vanzare = b.id_vanzare
                 and b.sters = 0
                left join vanzari_cursuri c
                  on decode(a.in_valuta, 1, b.id_valuta, -10000) =
                     decode(a.in_valuta, 1, c.id_valuta, -9999)
                 and a.id_vanzare = c.id_vanzare
               where b.id_vanzare_set is not null
               group by b.id_vanzare_set,
                        a.in_valuta,
                        b.id_ctr,
                        c.curs,
                        c.multiplicator,
                        decode(a.in_valuta,
                               1,
                               c.id_valuta,
                               pack_def.getidmonedanationala())) a2
        left join vanzari_seturi b2
          on a2.id_vanzare_set = b2.id_vanzare_set
        left join nom_valute c2
          on a2.id_valuta = c2.id_valuta;

  end citeste_vanzari_seturi;
  -----------------------------------------------------------------------------------------------
  Procedure citeste_proforme_seturi(V_ID_PROFORMA IN NUMBER,
                                    V_CURSOR      OUT cursor_facturare) is
  begin
    OPEN V_CURSOR FOR
      select b2.id_proforma_set as id_vanzare_set,
             cast(a2.id_proforma_det as number(10)) as id_vanzare_det,
             b2.denumire,
             b2.explicatie,
             b2.cantitate,
             b2.um,
             b2.serie,
             cast(round(decode(a2.in_valuta,
                               1,
                               round(b2.pret * a2.curs / a2.multiplicator,
                                     pack_sesiune.nzecimale_pretv),
                               b2.pret),
                        pack_sesiune.nzecimale_pretv) as number(14, 4)) as pret,
             cast(round(decode(a2.in_valuta, 1, b2.pret, 0),
                        pack_sesiune.nzecimale_pretvval) as number(14, 4)) as pret_val,
             a2.id_valuta,
             a2.in_valuta as tip_valuta,
             c2.nume_val,
             decode(a2.in_valuta,
                    1,
                    round(Nvl(b2.discount_unitar, 0) * a2.curs /
                          a2.multiplicator,
                          pack_sesiune.nzecimale_pretv),
                    Nvl(b2.discount_unitar, 0)) as discount_unitar,
             decode(a2.in_valuta, 1, Nvl(b2.discount_unitar, 0), 0) as discount_unitar_val,
             b2.pret_cu_tva as pret_cu_tva,
             a2.curs,
             a2.multiplicator,
             cast(0 as number(18)) as id_ctr, --       a2.id_ctr,
             cast(null as varchar2(100)) as numar_contract, --       c2.numar_contract,
             cast(a2.proc_tvav as number(8, 4)) as proc_Tvav,
             cast(a2.id_jtva_coloana as number(10)) as id_jtva_coloana,
             cast(a2.gestionabil as number(1)) as gestionabil,
             cast(null as varchar2(100)) as codmat,
             cast(null as varchar2(100)) as codmatf,
             cast(null as varchar2(100)) as codbare,
             cast(0 as number(14, 4)) as pret_achizitie,
             cast(null as number(10)) as id_gestiune
        from (select b.id_proforma_set,
                     a.in_valuta,
                     --   b.id_ctr,
                     max(b.proc_tvav) as proc_tvav,
                     max(nvl2(b.id_gestiune, 1, 0)) as gestionabil,
                     min(b.id_jtva_coloana) as id_jtva_coloana,
                     min(b.id_proforma_det) as id_proforma_det,
                     c.curs,
                     c.multiplicator,
                     decode(a.in_valuta,
                            1,
                            c.id_valuta,
                            pack_def.getidmonedanationala()) as id_valuta
                from (select id_proforma, in_valuta
                        from proforme
                       where id_proforma = V_ID_PROFORMA
                         and sters = 0) a
                left join proforme_detalii b
                  on a.id_proforma = b.id_proforma
                 and b.sters = 0
                left join proforme_cursuri c
                  on decode(a.in_valuta, 1, b.id_valuta, -10000) =
                     decode(a.in_valuta, 1, c.id_valuta, -9999)
                 and a.id_proforma = c.id_proforma
               where b.id_proforma_set is not null
               group by b.id_proforma_set,
                        a.in_valuta,
                        b.id_ctr,
                        c.curs,
                        c.multiplicator,
                        decode(a.in_valuta,
                               1,
                               c.id_valuta,
                               pack_def.getidmonedanationala())) a2
        left join proforme_seturi b2
          on a2.id_proforma_set = b2.id_proforma_set
        left join nom_valute c2
          on a2.id_valuta = c2.id_valuta;

  end citeste_proforme_seturi;
  -----------------------------------------------------------------------------------------------
  procedure adauga_note_tva_incasare is
  begin
    UPDATE ACT_TEMP
       SET SCC = '4428'
     WHERE SCC = '4427'
       AND TVA_INCASARE = 1
       AND ID_JTVA_COLOANA IN
           (SELECT ID_TVA
              FROM JTVA_COLOANE
             WHERE ID_JTVA_COLOANA IN (SELECT ID_JTVA_NEEX
                                         FROM JTVA_COLOANE
                                        WHERE STERS = 0
                                          AND JV = 1));

    INSERT INTO ACT_TEMP
      (ID_ACT,
       SERIE_ACT,
       NRACT,
       DATAIREG,
       DATAACT,
       PERECHEC,
       SCD,
       ASCD,
       SCC,
       ASCC,
       SUMA,
       ID_JTVA_COLOANA,
       PROC_TVA,
       ID_CTR,
       ID_LUCRARE,
       ID_FDOC,
       ID_SECTIE,
       ID_VENCHELT,
       ID_FACT,
       TVA_INCASARE,
       ID_SET)
      SELECT pack_facturare.nid_act + rownum as ID_ACT,
             pack_facturare.cserie_act_incasare as serie_act,
             pack_facturare.nnumar_act_incasare as nract,
             pack_facturare.ddata_ireg as dataireg,
             pack_facturare.ddata_act as dataact,
             pack_facturare.nnumar_act as perechec,
             '4428' as scd,
             A1.ACONT as ascd,
             '4427' as scc,
             '' as ascc,
             ROUND((CASE
                     WHEN A1.DIF <= 0 THEN
                      A1.totctva
                     ELSE
                      A1.totctva - A1.DIF
                   END) / A1.proc_tva * (A1.proc_tva - 1),
                   pack_sesiune.getoptiunefirma('PC')) as suma,
             A3.ID_TVA AS ID_JTVA_COLOANA,
             A1.PROC_TVA,
             Null as id_ctr,
             NVL(pack_facturare.nid_lucrare, 0) as id_lucrare,
             A1.ID_FDOC,
             A1.ID_SECTIE,
             A1.ID_VENCHELT,
             -5 AS id_fact,
             pack_facturare.ntva_incasare,
             pack_facturare.nid_set as id_set
        FROM (select MAX(ID_FDOC) OVER() as id_fdoc,
                     acont,
                     proc_tva,
                     id_jtva_coloana,
                     baza,
                     tva,
                     totctva,
                     id_sectie,
                     id_venchelt,
                     SUM(totctva) OVER(ORDER BY PROC_TVA DESC, ACONT) as DIF
                from (SELECT MAX(ID_FDOC) AS ID_FDOC,
                             NULL AS ACONT,
                             2 AS PROC_TVA,
                             0 as baza,
                             0 as tva,
                             (-1) * sum(suma) AS totctva,
                             NULL AS ID_JTVA_COLOANA,
                             NULL AS ID_SECTIE,
                             NULL AS ID_VENCHELT
                        FROM act_temp
                       WHERE scc = '4111'
                         and id_jtva_coloana is null
                      UNION ALL
                      select null as id_fdoc,
                             a.ascc as acont,
                             a.proc_tva,
                             c.baza,
                             a.tva,
                             c.baza + a.tva as totctva,
                             a.id_jtva_coloana,
                             a.id_sectie,
                             a.id_venchelt
                        from (select ascc,
                                     proc_tva,
                                     sum(suma) as tva,
                                     id_jtva_coloana,
                                     max(id_sectie) as id_sectie,
                                     max(id_venchelt) as id_venchelt
                                from act_temp
                               where id_jtva_coloana in
                                     (SELECT ID_TVA
                                        FROM VJTVA_COLOANE
                                       WHERE ID_JTVA_EX IS NOT NULL)
                               group by proc_tva, id_jtva_coloana, ascc) a
                        left join jtva_coloane b
                          on a.id_jtva_coloana = b.id_tva
                        left join (select sum(decode(scc, '4111', -1, 1) * suma) as baza,
                                         id_jtva_coloana
                                    from act_temp
                                   where id_jtva_coloana in
                                         (SELECT ID_JTVA_COLOANA
                                            FROM VJTVA_COLOANE
                                           WHERE ID_JTVA_EX IS NOT NULL)
                                   group by id_jtva_coloana) c
                          on b.id_jtva_coloana = c.id_jtva_coloana)) A1
        LEFT JOIN VJTVA_COLOANE A2
          ON A1.ID_JTVA_COLOANA = A2.ID_TVA
        LEFT JOIN VJTVA_COLOANE A3
          ON A2.ID_JTVA_EX = A3.ID_JTVA_COLOANA
       WHERE A1.ID_JTVA_COLOANA IS NOT NULL
         AND A1.totctva > A1.DIF;

  end adauga_note_tva_incasare;
  -----------------------------------------------------------------------------------------------
  function citeste_lungcampexplart return NUMBER is
    V_LUNGIME NUMBER(10);
  begin
    select data_length
      into V_LUNGIME
      from user_tab_columns
     where column_name = 'EXPLICATIE'
       and table_name = 'VANZARI_DETALII';
    return V_LUNGIME;
  end citeste_lungcampexplart;
  -----------------------------------------------------------------------------------------------
  function getAnaliticK(tnProcTvaV IN NUMBER) return VARCHAR2 is
    V_ANALITICCUST ACT.ASCC%TYPE;
    lnProcTVA      number(2);
  begin
    if tnProcTvaV between 1 and 2 then
      lnProcTVA := round((tnProcTvaV - 1) * 100, 0);
    else
      lnProcTVA := tnProcTvaV;
    end if;

    -- analiticele tip nou 371,378,4428 pentru descarcarea K
    -- tnProcTvaV = 5/9/19/20/24 sau 1.05/1.09/1.19/1.20/1.24
    -- optiuni de tipul FACTANALITICCUSTK_5/FACTANALITICCUSTK_9/FACTANALITICCUSTK_19...
    V_ANALITICCUST := pack_sesiune.getoptiunefirma('FACTANALITICCUSTK_' ||
                                                   lnProcTVA);
    if V_ANALITICCUST is null then
      -- daca nu gasesc analitice cu optiuni de tip nou, caut optiunile vechi pentru cota standard si cota redusa 5%
      if tnProcTvaV in (1.24, 1.20, 1.19) then
        V_ANALITICCUST := pack_sesiune.getoptiunefirma('FACTANALITICCUSTK2');
      else
        V_ANALITICCUST := pack_sesiune.getoptiunefirma('FACTANALITICCUSTK');
      end if;
    end if;
    return V_ANALITICCUST;
  end getAnaliticK;

  ----------------------------
  -- Obtine sold client 4111 + 461 -419 din balanta_parteneri sau din ireg_parteneri pana la numarul facturii
  -- Pentru afisarea in raportul factura
  procedure getSoldClient(tnAn     IN NUMBER,
                         tnLuna   IN NUMBER,
                         tnIdPart IN NUMBER,
                         tnIdFact IN NUMBER default 0,
                         V_CURSOR           OUT cursor_facturare) is
  begin
    IF nvl(tnIdFact, 0) = 0 then
      OPEN V_CURSOR FOR
        SELECT SUM(semn * (precdeb + debit - preccred - credit)) as sold_lei,
               SUM(semn * (precvaldeb + valdebit - precvalcred - valcredit)) as sold_valuta
          from (select 1 as semn,
                       precdeb,
                       debit,
                       preccred,
                       credit,
                       precvaldeb,
                       valdebit,
                       precvalcred,
                       valcredit
                  from balanta_parteneri
                 WHERE an = tnAn
                   AND luna = tnLuna
                   and id_part = tnIdPart
                   AND cont in ('4111', '461'));
    ELSE

      OPEN V_CURSOR FOR
        SELECT SUM(semn * (precdeb + debit - preccred - credit)) as sold_lei,
               SUM(semn * (precvaldeb + valdebit - precvalcred - valcredit)) as sold_valuta
          from (select 1 as semn,
                       precdeb,
                       debit,
                       preccred,
                       credit,
                       precvaldeb,
                       valdebit,
                       precvalcred,
                       valcredit
                  from ireg_parteneri
                 WHERE an = tnAn
                   AND luna = tnLuna
                   and id_part = tnIdPart
                   AND cont in ('4111', '461')
                   and (dataact < (select max(dataact)
                                     from ireg_parteneri
                                    where an = tnAn
                                      and luna = tnLuna
                                      and id_part = tnIdPart
                                      and id_fact = tnIdFact) or
                       nract <= (select max(nract)
                                    from ireg_parteneri
                                   where an = tnAn
                                     and luna = tnLuna
                                     and id_part = tnIdPart
                                     and id_fact = tnIdFact)));

    END IF;
  end getSoldClient;

end PACK_FACTURARE;
/
