CREATE OR REPLACE PACKAGE pack_imob IS
  imob_data_operatie date;

  nNrLuniCurent number; -- an*12+luna curenta
  nLunaCurenta  number;
  nAnCurent     number;
  dDataCurenta  date; -- 01/ll/aaaa

  type tip_valori_mf is record(
    data_test            date,
    id_mf                number,
    id_lista_mf          number,
    id_operatie_mf       number,
    id_operatie_prec     number,
    id_calcul_rate       number,
    denumire             varchar2(500),
    data_achizitie       date,
    data_pif             date,
    explicatia           varchar2(500),
    nract                number,
    nr_pif               number,
    procent              number,
    coeficient           number,
    id_tip_imobilizare   number,
    tip_imobilizare      varchar2(50),
    id_tip_amortizare    number,
    tip_amortizare       varchar2(50),
    dns_luni             number,
    cont                 varchar2(4),
    acont                varchar2(4),
    cod_mf               varchar2(20),
    id_gestiune          number,
    gestiune             varchar2(50),
    id_lucrare           number,
    nrord                varchar2(50),
    id_responsabil       number,
    responsabil          varchar2(50),
    id_sectie            number,
    sectie               varchar2(50),
    id_sucursala         number,
    sucursala            varchar2(100),
    nr_inventar          number,
    lper_in              varchar2(7),
    lper_fin             varchar2(7),
    amort_max_deductibil number,
    data_operatie        date,
    id_tip_operatie      number,
    ultima_operatie      varchar2(50),
    cauza                varchar2(50),
    uzura_prec           number,
    dns_ramas            number,
    iesit_din_gest       number,
    cota_an              number,
    cota_luna            number,
    data_expirare        date,
    ultima_rata          number,
    valoare              number,
    valoare_ramasa       number,
    valoare_reziduala    number,
    rata                 number,
    --valoare_ramasa_amort number,
    amort_prec number,
    --amort_prec_rata    number,
    uzura              number,
    uzura_rata         number,
    rper_in            varchar2(7),
    rper_fin           varchar2(7),
    valoare_rol        number,
    valoare_ramasa_rol number,
    rata_rol           number,
    ultima_rata_rol    number,
    valoare_achizitie  number,
    valoare_prec       number,
    id_tip_iesire      number,
    tip_iesire         varchar2(100),
    durata_ramasa      number);

  procedure setlunacurenta(v_data IN DATE);
  function getNrLuniCurent return number;
  function getDataCurenta return date;

  PROCEDURE adauga_mf(v_schema               IN VARCHAR2,
                      v_data                 IN DATE,
                      v_nract                IN NUMBER,
                      v_dataact              IN DATE,
                      v_denumire             IN VARCHAR2,
                      v_explicatia           IN VARCHAR2,
                      v_valoare              IN NUMBER,
                      v_data_pif             IN DATE,
                      v_nr_pif               IN NUMBER,
                      v_nr_inventar          IN NUMBER,
                      v_cod                  IN VARCHAR2,
                      v_dns                  IN NUMBER,
                      v_uzura                IN NUMBER,
                      v_amort_prec           IN NUMBER,
                      v_id_gestiune          IN NUMBER,
                      v_id_responsabil       IN NUMBER,
                      v_id_sectie            IN NUMBER,
                      v_id_lucrare           IN NUMBER,
                      v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                      v_id_tip_amortizare    IN NUMBER,
                      v_procent              IN NUMBER,
                      v_coeficient           IN NUMBER,
                      v_id_tip_imobilizare   IN NUMBER,
                      v_cont                 IN VARCHAR2,
                      v_acont                IN VARCHAR2,
                      v_id_util              IN NUMBER,
                      v_amort_max_deductibil IN NUMBER,
                      v_id_fact              IN NUMBER,
                      v_tip_operatie         IN NUMBER,
                      v_explicatia_operatie  in varchar2,
                      v_bunCapital           in number,
                      v_bazatva              in number,
                      v_tva                  in number,
                      v_id_cotatva           in number,
                      v_prorataTva           in number);

  PROCEDURE adauga_mf(v_id_mf                IN OUT NUMBER,
                      v_schema               IN VARCHAR2,
                      v_data                 IN DATE,
                      v_nract                IN NUMBER,
                      v_dataact              IN DATE,
                      v_denumire             IN VARCHAR2,
                      v_explicatia           IN VARCHAR2,
                      v_valoare              IN NUMBER,
                      v_data_pif             IN DATE,
                      v_nr_pif               IN NUMBER,
                      v_nr_inventar          IN NUMBER,
                      v_cod                  IN VARCHAR2,
                      v_dns                  IN NUMBER,
                      v_uzura                IN NUMBER,
                      v_amort_prec           IN NUMBER,
                      v_id_gestiune          IN NUMBER,
                      v_id_responsabil       IN NUMBER,
                      v_id_sectie            IN NUMBER,
                      v_id_lucrare           IN NUMBER,
                      v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                      v_id_tip_amortizare    IN NUMBER,
                      v_procent              IN NUMBER,
                      v_coeficient           IN NUMBER,
                      v_id_tip_imobilizare   IN NUMBER,
                      v_cont                 IN VARCHAR2,
                      v_acont                IN VARCHAR2,
                      v_id_util              IN NUMBER,
                      v_amort_max_deductibil IN NUMBER,
                      v_id_fact              IN NUMBER,
                      v_tip_operatie         IN NUMBER,
                      v_explicatia_operatie  in varchar2,
                      v_bunCapital           in number,
                      v_bazatva              in number,
                      v_tva                  in number,
                      v_id_cotatva           in number,
                      v_prorataTva           in number);

  PROCEDURE adauga_mf(v_id_mf                IN OUT NUMBER,
                      v_schema               IN VARCHAR2,
                      v_data                 IN DATE,
                      v_nract                IN NUMBER,
                      v_dataact              IN DATE,
                      v_denumire             IN VARCHAR2,
                      v_explicatia           IN VARCHAR2,
                      v_valoare              IN NUMBER,
                      v_valoare_reziduala    IN NUMBER,
                      v_data_pif             IN DATE,
                      v_nr_pif               IN NUMBER,
                      v_nr_inventar          IN NUMBER,
                      v_cod                  IN VARCHAR2,
                      v_dns                  IN NUMBER,
                      v_uzura                IN NUMBER,
                      v_amort_prec           IN NUMBER,
                      v_id_gestiune          IN NUMBER,
                      v_id_responsabil       IN NUMBER,
                      v_id_sectie            IN NUMBER,
                      v_id_lucrare           IN NUMBER,
                      v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                      v_id_tip_amortizare    IN NUMBER,
                      v_procent              IN NUMBER,
                      v_coeficient           IN NUMBER,
                      v_id_tip_imobilizare   IN NUMBER,
                      v_cont                 IN VARCHAR2,
                      v_acont                IN VARCHAR2,
                      v_id_util              IN NUMBER,
                      v_amort_max_deductibil IN NUMBER,
                      v_id_fact              IN NUMBER,
                      v_tip_operatie         IN NUMBER,
                      v_explicatia_operatie  in varchar2,
                      v_bunCapital           in number,
                      v_bazatva              in number,
                      v_tva                  in number,
                      v_id_cotatva           in number,
                      v_prorataTva           in number);

  PROCEDURE adauga_rata(v_schema          IN VARCHAR2,
                        v_id_operatie_mf  IN NUMBER,
                        v_id_tip_operatie IN NUMBER,
                        v_id_mf           IN NUMBER,
                        v_dns_ramas       IN NUMBER,
                        v_data_calcul     IN DATE,
                        v_valoare         IN NUMBER,
                        v_valoare_ramasa  IN NUMBER,
                        v_uzura_prec      in number,
                        v_fiscala         in number);

  PROCEDURE inreg_majorare(v_schema       IN VARCHAR2,
                           v_id_sucursala in imob_lista_mf.id_sucursala%type,
                           v_id_mf        IN NUMBER,
                           v_valoare      IN NUMBER,
                           v_datareeval   IN DATE,
                           v_explicatia   in varchar2,
                           v_id_util      IN NUMBER,
                           v_fiscala      in number,
                           v_datadoc      in date,
                           v_nrdoc        in number,
                           v_buncapital   in number,
                           v_bazaTva      in number,
                           v_idcotaTva    in number,
                           v_Tva          in number,
                           v_prorataTva   in number);

  PROCEDURE inreg_reevaluare(v_schema       IN VARCHAR2,
                             v_id_sucursala in imob_lista_mf.id_sucursala%type,
                             v_id_mf        IN NUMBER,
                             v_valoare      IN NUMBER,
                             v_amortizare   IN NUMBER,
                             v_datareeval   IN DATE,
                             v_explicatia   in varchar2,
                             v_id_util      IN NUMBER,
                             v_fiscala      in number,
                             v_datadoc      in date,
                             v_nrdoc        in number,
                             v_buncapital   in number,
                             v_valreev      in number);

  PROCEDURE inreg_schimbdns_reevaluare(v_schema            in varchar2,
                                       v_id_sucursala      in imob_lista_mf.id_sucursala%type,
                                       v_id_mf             in number,
                                       v_cod               IN VARCHAR2, -- codul nou
                                       v_dns               IN NUMBER, -- dns nou
                                       v_valoare           in number, -- valoarea noua de inventar
                                       v_amortizare        in number, -- valoarea noua total amortizata
                                       v_uzura             IN NUMBER, -- uzura noua totala
                                       v_valoare_reziduala IN NUMBER, -- valoarea noua reziduala
                                       v_datareeval        in date,
                                       v_explicatia        in varchar2,
                                       v_id_util           in number,
                                       v_fiscala           in number,
                                       v_datadoc           in date,
                                       v_nrdoc             in number,
                                       v_buncapital        in number,
                                       v_valreev           in number);

  PROCEDURE inreg_conservare(v_schema         IN VARCHAR2,
                             v_id_sucursala   in imob_lista_mf.id_sucursala%type,
                             v_id_mf          IN NUMBER,
                             v_tip            IN NUMBER,
                             v_cauza          IN VARCHAR2,
                             v_dataconservare IN DATE,
                             v_id_util        IN NUMBER,
                             v_fiscala        in number,
                             v_datadoc        in date,
                             v_nrdoc          in number,
                             v_buncapital     in number,
                             v_bazatva        in number,
                             v_idcotatva      in number,
                             v_tva            in number,
                             v_prorataTva     in number);

  PROCEDURE inreg_schimbdns(v_schema        IN VARCHAR2,
                            v_id_sucursala  in imob_lista_mf.id_sucursala%type,
                            v_id_mf         IN NUMBER,
                            v_codnou        IN VARCHAR2,
                            v_dnsnou        IN NUMBER,
                            v_dataschimbare IN DATE,
                            v_cauza         IN VARCHAR2,
                            v_id_util       IN NUMBER,
                            v_fiscala       in number,
                            v_datadoc       in date,
                            v_nrdoc         in number);

  PROCEDURE inreg_gest(v_schema        IN VARCHAR2,
                       v_id_sucursala  in imob_lista_mf.id_sucursala%type,
                       v_id_mf         IN NUMBER,
                       v_tip           IN NUMBER,
                       v_cauza         IN VARCHAR2,
                       v_dataschimbare IN DATE,
                       v_id_util       IN NUMBER,
                       v_fiscala       in number,
                       v_datadoc       in date,
                       v_nrdoc         in number,
                       v_buncapital    in number,
                       v_bazatva       in number,
                       v_idcotaTva     in number,
                       v_tva           in number,
                       v_prorataTva    in number,
                       v_id_tip_iesire in imob_tipuri_iesire.id%type default null);

  PROCEDURE inreg_transformare_in_mf(v_schema               IN VARCHAR2,
                                     v_id_mf                IN NUMBER,
                                     v_id_mf_nou            IN NUMBER,
                                     v_datamodificare       IN DATE,
                                     v_nract                IN NUMBER,
                                     v_dataact              IN DATE,
                                     v_denumire             IN VARCHAR2,
                                     v_explicatia           IN VARCHAR2,
                                     v_valoare              IN NUMBER,
                                     v_data_pif             IN DATE,
                                     v_nr_pif               IN NUMBER,
                                     v_nr_inventar          IN NUMBER,
                                     v_cod                  IN VARCHAR2,
                                     v_dns                  IN NUMBER,
                                     v_uzura                IN NUMBER,
                                     v_dns_ramas            IN NUMBER,
                                     v_valoare_ramasa       IN NUMBER,
                                     v_valoare_retinuta     IN NUMBER,
                                     v_id_gestiune          IN NUMBER,
                                     v_id_responsabil       IN NUMBER,
                                     v_id_sectie            IN NUMBER,
                                     v_id_lucrare           IN NUMBER,
                                     v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                                     v_id_tip_amortizare    IN NUMBER,
                                     v_procent              IN NUMBER,
                                     v_coeficient           IN NUMBER,
                                     v_id_tip_imobilizare   IN NUMBER,
                                     v_cont                 IN VARCHAR2,
                                     v_acont                IN VARCHAR2,
                                     v_id_util              IN NUMBER,
                                     v_amort_max_deductibil IN NUMBER,
                                     v_fiscala              in number,
                                     v_datadoc              in date,
                                     v_nrdoc                in number,
                                     v_id_tip_operatie      in number DEFAULT 3);

  FUNCTION getLuniOperatie RETURN NUMBER;


  procedure schimba_mf(v_id_mf                IN NUMBER,
                       v_nract                IN NUMBER,
                       v_data_achizitie       IN DATE,
                       v_denumire             IN VARCHAR2,
                       v_explicatie_n         IN VARCHAR2,
                       v_data_pif             IN DATE,
                       v_nr_pif               IN NUMBER,
                       v_procent              IN NUMBER,
                       v_coeficient           IN NUMBER,
                       v_id_tip_amortizare    IN NUMBER,
                       v_id_tip_imobilizare   IN NUMBER,
                       v_id_lista_mf          IN NUMBER,
                       v_dns_luni             IN NUMBER,
                       v_Cod_mf               IN VARCHAR2,
                       v_cont                 IN VARCHAR2,
                       v_acont                IN VARCHAR2,
                       v_nr_inventar          IN NUMBER,
                       v_id_gestiune          IN NUMBER,
                       v_id_responsabil       IN NUMBER,
                       v_id_sectie            IN NUMBER,
                       v_id_lucrare           IN NUMBER,
                       v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                       v_lper_in              in VARCHAR2,
                       v_lper_fin             in VARCHAR2,
                       v_id_operatie_mf       IN NUMBER,
                       v_id_tip_operatie      in NUMBER,
                       v_valoare              IN NUMBER,
                       v_valoare_ramasa       IN NUMBER,
                       v_dns_ramas            in NUMBER,
                       v_valoare_reziduala    IN NUMBER,
                       v_data_operatie        in DATE,
                       v_explicatie_o         in VARCHAR2,
                       v_uzura_prec           in NUMBER,
                       v_id_rata              in NUMBER,
                       v_rper_in              in VARCHAR2,
                       v_rper_fin             in VARCHAR2,
                       v_rata                 in NUMBER,
                       v_cota_an              in NUMBER,
                       v_cota_luna            in NUMBER,
                       v_ultima_rata          in NUMBER,
                       v_data_ultima_rata     in DATE,
                       v_fiscala              in NUMBER,
                       v_id_util              IN NUMBER,
                       v_amort_max_deductibil IN NUMBER,
                       v_nrdoc                IN IMOB_OPERATII_MF.NRDOC%TYPE DEFAULT NULL,
                       v_datadoc              in IMOB_OPERATII_MF.DATADOC%TYPE DEFAULT NULL);

  procedure sterge_mf(v_id_mf IN NUMBER, v_id_util IN number default null);

  /*  function add_operatie_mf(v_schema              IN VARCHAR2,
                         v_id_mf               IN NUMBER,
                         v_id_tip_operatie     IN NUMBER,
                         v_valoare             IN NUMBER,
                         v_valoare_ramasa      IN NUMBER,
                         v_dns_ramas           IN NUMBER,
                         v_data_operatie       IN DATE,
                         v_explicatia_operatie IN VARCHAR2,
                         v_id_util             IN NUMBER,
                         v_fiscala             in number,
                         v_datadoc             in date,
                         v_nrdoc               in number,
                         v_id_tip_iesire       in imob_tipuri_iesire.id%type default null)
  return number;*/

  function add_operatie_mf(v_id_mf               IN NUMBER,
                           v_id_tip_operatie     IN NUMBER,
                           v_valoare             IN NUMBER,
                           v_valoare_ramasa      IN NUMBER,
                           v_dns_ramas           IN NUMBER,
                           v_valoare_reziduala   IN NUMBER,
                           v_data_operatie       IN DATE,
                           v_explicatia_operatie IN VARCHAR2,
                           v_id_util             IN NUMBER,
                           v_fiscala             in number,
                           v_datadoc             in date,
                           v_nrdoc               in number,
                           v_id_tip_iesire       in imob_tipuri_iesire.id%type default null)
    return number;

  procedure add_in_lista_mf(v_schema               IN VARCHAR2,
                            v_id_mf                IN NUMBER,
                            v_dns                  IN NUMBER,
                            v_cod_mf               IN VARCHAR2,
                            v_cont                 IN VARCHAR2,
                            v_acont                IN VARCHAR2,
                            v_id_lucrare           IN NUMBER,
                            v_id_gestiune          IN NUMBER,
                            v_id_sectie            IN NUMBER,
                            v_id_responsabil       IN NUMBER,
                            v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                            v_nr_inventar          IN NUMBER,
                            v_id_util              IN NUMBER,
                            v_data_calcul          IN DATE,
                            v_durata               IN NUMBER,
                            v_amort_max_deductibil IN NUMBER);

  procedure adauga_la_transformare(v_id_mf_nou        IN NUMBER,
                                   v_id_operatie_nou  IN NUMBER,
                                   v_id_mf_curs       IN NUMBER,
                                   v_id_operatie_curs IN NUMBER,
                                   v_valoare          IN NUMBER);

  procedure undo_in_imobilizare_in_curs(v_id_tip_imobilizare IN NUMBER,
                                        v_id_operatie        IN NUMBER);

  procedure recalculat_expirarea(v_schema           IN VARCHAR2,
                                 v_id_operatie_prec IN NUMBER,
                                 v_fiscala          IN NUMBER);

  FUNCTION get_max_id(v_schema  IN VARCHAR2,
                      v_tabela  IN VARCHAR2,
                      v_nume_id IN VARCHAR2) RETURN NUMBER;

  procedure CALCUL_SITUATIE_LUNARA(v_datain       in date,
                                   v_datasf       in date,
                                   v_cond         in varchar2,
                                   v_fiscala      in number,
                                   v_id_sucursala in number,
                                   crsMF          out PACK_TYPES.tip_cursor);

  procedure CALCUL_SITUATIE_LUNARA2(v_datain       in date,
                                    v_datasf       in date,
                                    v_cond         in varchar2,
                                    v_fiscala      in number,
                                    v_id_sucursala in number,
                                    v_tip_selectie in number,
                                    crsMF          out PACK_TYPES.tip_cursor);

  function valori_calcul(v_schema       in varchar2,
                         v_id_sucursala in IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                         v_datain       in date,
                         v_datasf       in date,
                         v_cond         in varchar2,
                         v_fiscala      in number)
    return PACK_TYPES.tip_cursor;

  function valori_calcul_view(v_id_mf   in number,
                              v_data    in date,
                              v_fiscala in number)
    return PACK_TYPES.tip_cursor;

  procedure sterge_operatie_mf(v_id_operatie      IN NUMBER,
                               v_id_operatie_prec IN NUMBER,
                               v_fiscala          IN NUMBER);

  procedure modifica_lista_mf(v_id_mf                IN IMOB_LISTA_MF.ID_MF%TYPE,
                              v_id_lista_mf          IN IMOB_LISTA_MF.ID_LISTA_MF%TYPE,
                              v_dns_luni             IN IMOB_LISTA_MF.DNS_LUNI%TYPE,
                              v_Cod_mf               IN IMOB_LISTA_MF.COD_MF%TYPE,
                              v_cont                 IN IMOB_LISTA_MF.CONT%TYPE,
                              v_acont                IN IMOB_LISTA_MF.ACONT%TYPE,
                              v_nr_inventar          IN IMOB_LISTA_MF.NR_INVENTAR%TYPE,
                              v_id_gestiune          IN IMOB_LISTA_MF.ID_GESTIUNE%TYPE,
                              v_id_responsabil       IN IMOB_LISTA_MF.ID_RESPONSABIL%TYPE,
                              v_id_sectie            IN IMOB_LISTA_MF.ID_SECTIE%TYPE,
                              v_id_lucrare           IN IMOB_LISTA_MF.ID_LUCRARE%TYPE,
                              v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                              v_id_util              IN IMOB_LISTA_MF.ID_UTIL%TYPE,
                              v_an                   IN IMOB_LISTA_MF.AN%TYPE,
                              v_luna                 IN IMOB_LISTA_MF.LUNA%TYPE,
                              v_an_exp               IN IMOB_LISTA_MF.AN%TYPE,
                              v_luna_exp             IN IMOB_LISTA_MF.LUNA%TYPE,
                              v_amort_max_deductibil IN IMOB_LISTA_MF.AMORT_MAX_DEDUCTIBIL%TYPE);

  procedure adauga_buncapital(v_id_mf           in imob_bunuricapital.id_mf%type,
                              v_nrdoc           in imob_bunuricapital.nrdoc%type,
                              v_datadoc         in imob_bunuricapital.datadoc%type,
                              v_bazatva         in imob_bunuricapital.bazatva%type,
                              v_tva             in imob_bunuricapital.tva%type,
                              v_id_cota_tva     in imob_bunuricapital.id_cota_tva%type,
                              v_ajustare        in imob_bunuricapital.ajustare%type,
                              v_id_tip_operatie in imob_bunuricapital.id_tip_operatie%type,
                              v_tvaDatorat      in imob_bunuricapital.tva_datorat%type,
                              v_prorataTva      in imob_bunuricapital.proratatva%type);
  procedure modifica_buncapital(v_id_mf           in imob_bunuricapital.id_mf%type,
                                v_nrdoc           in imob_bunuricapital.nrdoc%type,
                                v_datadoc         in imob_bunuricapital.datadoc%type,
                                v_bazatva         in imob_bunuricapital.bazatva%type,
                                v_tva             in imob_bunuricapital.tva%type,
                                v_id_cota_tva     in imob_bunuricapital.id_cota_tva%type,
                                v_ajustare        in imob_bunuricapital.ajustare%type,
                                v_id_tip_operatie in imob_bunuricapital.id_tip_operatie%type,
                                v_tvaDatorat      in imob_bunuricapital.tva_datorat%type,
                                v_id_regbc        in imob_bunuricapital.id_regbc%type,
                                v_prorataTva      in imob_bunuricapital.proratatva%type);
  procedure sterge_buncapital(v_id_regbc in imob_bunuricapital.id_regbc%type);

  FUNCTION verifica_nr_inventar(v_nr_inventar        IN IMOB_LISTA_MF.NR_INVENTAR%TYPE,
                                v_data               IN DATE,
                                v_id_tip_imobilizare IN NUMBER DEFAULT 1)
    RETURN NUMBER;

END pack_imob;
/
CREATE OR REPLACE PACKAGE BODY pack_imob is
  /*TIP OPERATII
  1 INTRODUCERE
  2 PRELUARE
  3 TRANSFORMARE IN MF
  4 IESIRE DIN GESTIUNE
  5 INTRARE IN GESTIUNE
  6 MAJORARE VALOARE INVENTAR
  7 REEVALUARE VALOARE INVENTAR
  8 SCHIMBARE DNS
  9 RECALCULARE RATA
  10 CONSERVARE
  11 SCOATERE DIN CONSERVARE
  12 MODIFICARE CU ISTORIC
  13 INCHIDERE
  14 DENOMINARE
  15 SCHIMBARE DNS + REEVALUARE */

  -- 25.03.2013
  -- marius.mutu
  -- + v_amort_max_deductibil in add_in_lista_mf, adauga_transformare,

  -- 15.05.2014
  -- marius.mutu
  -- + tabel IMOB_TIPURI_IESIRE cu valori predefinite pentru tipul de iesire din gestiune: 1=VANZARE, 2=CASARE, 3=FURT, 4=ALTE
  -- foloseste la PACK_MG_EVOLUTIE pentru bilant F40 (active imobilizate) - reduceri amortizare provenite din CASARE
  -- CALCUL_SITUATIE_LUNARA, type tip_valori_mf: + coloane valoare_prec, id_tip_iesire, tip_iesire
  -- inreg_gest (iesire din gestiune), add_operatie_mf: + v_id_tip_iesire

  -- marius.atanasiu
  -- CALCUL_SITUATIE_LUNARA: + parametru v_tip_selectie: 0 (default), 1 = se apeleaza selectul din pack_mg_evolutie cu mai putine coloane

  -- 09.10.2014
  -- marius.mutu
  -- overloading-ul la IMOB_CALCUL_SITUATIE_LUNARA am redenumit in IMOB_CALCUL_SITUATIE_LUNARA2 pentru ca dadea eroare la ODBC instantclient 11.2
  -- nu stia probabil ce declaratie sa foloseasca (in ODBC 10x functiona)

  -- 12.11.2019
  -- marius.mutu
  -- inreg_majorare - calculez dns_ramas = dns_ramas_anterior - uzura de la ultima operatie, in loc de DNS - uzura totala
  -- UTMIDIA avea imobilizari la care marise DNS RAMAS, si nu mai corespundea cu DNS

  -- 16.04.2020
  -- marius.mutu
  -- adauga_mf + id_mf

  -- 21.05.2020
  -- marius.mutu
  -- schimba_mf - tratare transformare imobilizari in curs in mijloace fixe nu se transmite id_rata
  -- probabil ca dadea eroare la selectie din imob_calcul_rate si nu se inregistra in imob_operatii si imob_calculc_rate
  -- nu apare eroare din cauza unui bug in Oracle

  -- inreg_modificare este acum inreg_transformare_in_mf
  -- am eliminat adauga_transformare - apelez din inreg_transformare_in_mf

  -- 09.06.2020
  -- marius.mutu
  -- + setlunacurenta.
  -- se apeleaza inainte de imob_vsituatie_lunara, imobf_vsituatie_lunara

  -- 07.07.2021
  -- marius.mutu
  -- add_in_lista_mf trim(v_cod)

  -- 25.01.2021
  -- marius.mutu
  -- adaugare valoare_reziduala in imob_operatii_mf, imobf_operatii_mf si modificare proceduri

  -- 28.01.2021
  -- marius.mutu
  -- + PACK_IMOB.calcul_situatie_lunara2 + durata_ramasa

  -- 02.02.2021
  -- pack_imob.tip_valori_mf + durata_ramasa

  -- 02.04.2021
  -- marius.mutu
  -- inreg_gest, inreg_majorare, inreg_reevaluare, inreg_schimbdns, inreg_conservare
  -- s-a corectat selectia din view-ul contabil/fiscal
  -- se lua din cel fiscal, chiar si pentru calculele amortizare contabila.

  -- 02.06.2021
  -- marius.mutu
  -- adaugare inreg_schimbdns_reevaluare (schimbare DNS si reevaluare)
  -- corectare valoare ramasa la inreg_schimbdns (era scazuta valoarea reziduala)

  -- 06.12.2021
  -- marius.mutu
  -- schimba_mf - tratare id_tip_imobilizare = 3
  -- imobilizarile in curs nu au operatii si dadea eroare

  -- 16.02.2022
  -- marius.mutu
  -- sterge_mf - se sterge si din imob_nom_mf

  -- 23.03.2023
  -- + tip_valori_mf.id_operatie_prec pentru ca s-a modificat view-ul imob_vsituatie_lunara

  -- 28.08.2024
  -- schimba_mf - modificarea operatiilor/ratelor se executa si pentru id_tip_imob = 3 (imobilizari in curs)
  -- am adaugat informatiile lipsa in ROAIMOB > modificare operatie

  -- 19.02.2026
  -- + verifica_nr_inventar - verifica numar inventar duplicat in lista de imobilizari curenta
  -- schimba_mf + nrdoc, datadoc
  -- am sters definitia schimba_mf cu parametri vechi

  -- 19.02.2026
  -- schimba_mf + nrdoc, datadoc DEFAULT NULL, pentru ca este apelata din ROACONT - definire rate venituri/cheltuieli in avans

  -- 24.06.2026
  -- inreg_schimbdns - am calculat durata ramasa de calcul = dns nou - uzura totala
  
  procedure setlunacurenta(v_data IN DATE) is
  begin
    dDataCurenta  := v_data;
    nLunaCurenta  := extract(month from v_data);
    nAnCurent     := extract(year from v_data);
    nNrLuniCurent := nAnCurent * 12 + nLunaCurenta;
  end;

  function getNrLuniCurent return number is
  begin
    return nNrLuniCurent;
  end;

  function getDataCurenta return date is
  begin
    return dDataCurenta;
  end;
  ---------------------------------------------------------------------------------------------
  PROCEDURE adauga_mf(v_schema               IN VARCHAR2,
                      v_data                 IN DATE,
                      v_nract                IN NUMBER,
                      v_dataact              IN DATE,
                      v_denumire             IN VARCHAR2,
                      v_explicatia           IN VARCHAR2,
                      v_valoare              IN NUMBER,
                      v_data_pif             IN DATE,
                      v_nr_pif               IN NUMBER,
                      v_nr_inventar          IN NUMBER,
                      v_cod                  IN VARCHAR2,
                      v_dns                  IN NUMBER,
                      v_uzura                IN NUMBER,
                      v_amort_prec           IN NUMBER,
                      v_id_gestiune          IN NUMBER,
                      v_id_responsabil       IN NUMBER,
                      v_id_sectie            IN NUMBER,
                      v_id_lucrare           IN NUMBER,
                      v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                      v_id_tip_amortizare    IN NUMBER,
                      v_procent              IN NUMBER,
                      v_coeficient           IN NUMBER,
                      v_id_tip_imobilizare   IN NUMBER,
                      v_cont                 IN VARCHAR2,
                      v_acont                IN VARCHAR2,
                      v_id_util              IN NUMBER,
                      v_amort_max_deductibil IN NUMBER,
                      v_id_fact              IN NUMBER,
                      v_tip_operatie         IN NUMBER,
                      v_explicatia_operatie  in varchar2,
                      v_bunCapital           in number,
                      v_bazatva              in number,
                      v_tva                  in number,
                      v_id_cotatva           in number,
                      v_prorataTva           in number) is
    v_id_mf number := 0;
  begin
    adauga_mf(v_id_mf,
              v_schema,
              v_data,
              v_nract,
              v_dataact,
              v_denumire,
              v_explicatia,
              v_valoare,
              v_data_pif,
              v_nr_pif,
              v_nr_inventar,
              v_cod,
              v_dns,
              v_uzura,
              v_amort_prec,
              v_id_gestiune,
              v_id_responsabil,
              v_id_sectie,
              v_id_lucrare,
              v_id_sucursala,
              v_id_tip_amortizare,
              v_procent,
              v_coeficient,
              v_id_tip_imobilizare,
              v_cont,
              v_acont,
              v_id_util,
              v_amort_max_deductibil,
              v_id_fact,
              v_tip_operatie,
              v_explicatia_operatie,
              v_bunCapital,
              v_bazatva,
              v_tva,
              v_id_cotatva,
              v_prorataTva);
  end adauga_mf;

  ---------------------------------------------------------------------------------------------
  PROCEDURE adauga_mf(v_id_mf                IN OUT NUMBER,
                      v_schema               IN VARCHAR2,
                      v_data                 IN DATE,
                      v_nract                IN NUMBER,
                      v_dataact              IN DATE,
                      v_denumire             IN VARCHAR2,
                      v_explicatia           IN VARCHAR2,
                      v_valoare              IN NUMBER,
                      v_data_pif             IN DATE,
                      v_nr_pif               IN NUMBER,
                      v_nr_inventar          IN NUMBER,
                      v_cod                  IN VARCHAR2,
                      v_dns                  IN NUMBER,
                      v_uzura                IN NUMBER,
                      v_amort_prec           IN NUMBER,
                      v_id_gestiune          IN NUMBER,
                      v_id_responsabil       IN NUMBER,
                      v_id_sectie            IN NUMBER,
                      v_id_lucrare           IN NUMBER,
                      v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                      v_id_tip_amortizare    IN NUMBER,
                      v_procent              IN NUMBER,
                      v_coeficient           IN NUMBER,
                      v_id_tip_imobilizare   IN NUMBER,
                      v_cont                 IN VARCHAR2,
                      v_acont                IN VARCHAR2,
                      v_id_util              IN NUMBER,
                      v_amort_max_deductibil IN NUMBER,
                      v_id_fact              IN NUMBER,
                      v_tip_operatie         IN NUMBER,
                      v_explicatia_operatie  in varchar2,
                      v_bunCapital           in number,
                      v_bazatva              in number,
                      v_tva                  in number,
                      v_id_cotatva           in number,
                      v_prorataTva           in number) is
    v_valoare_reziduala number := 0;
  begin
    adauga_mf(v_id_mf,
              v_schema,
              v_data,
              v_nract,
              v_dataact,
              v_denumire,
              v_explicatia,
              v_valoare,
              v_valoare_reziduala,
              v_data_pif,
              v_nr_pif,
              v_nr_inventar,
              v_cod,
              v_dns,
              v_uzura,
              v_amort_prec,
              v_id_gestiune,
              v_id_responsabil,
              v_id_sectie,
              v_id_lucrare,
              v_id_sucursala,
              v_id_tip_amortizare,
              v_procent,
              v_coeficient,
              v_id_tip_imobilizare,
              v_cont,
              v_acont,
              v_id_util,
              v_amort_max_deductibil,
              v_id_fact,
              v_tip_operatie,
              v_explicatia_operatie,
              v_bunCapital,
              v_bazatva,
              v_tva,
              v_id_cotatva,
              v_prorataTva);
  end adauga_mf;

  PROCEDURE adauga_mf(v_id_mf                IN OUT NUMBER,
                      v_schema               IN VARCHAR2,
                      v_data                 IN DATE,
                      v_nract                IN NUMBER,
                      v_dataact              IN DATE,
                      v_denumire             IN VARCHAR2,
                      v_explicatia           IN VARCHAR2,
                      v_valoare              IN NUMBER,
                      v_valoare_reziduala    IN NUMBER,
                      v_data_pif             IN DATE,
                      v_nr_pif               IN NUMBER,
                      v_nr_inventar          IN NUMBER,
                      v_cod                  IN VARCHAR2,
                      v_dns                  IN NUMBER,
                      v_uzura                IN NUMBER,
                      v_amort_prec           IN NUMBER,
                      v_id_gestiune          IN NUMBER,
                      v_id_responsabil       IN NUMBER,
                      v_id_sectie            IN NUMBER,
                      v_id_lucrare           IN NUMBER,
                      v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                      v_id_tip_amortizare    IN NUMBER,
                      v_procent              IN NUMBER,
                      v_coeficient           IN NUMBER,
                      v_id_tip_imobilizare   IN NUMBER,
                      v_cont                 IN VARCHAR2,
                      v_acont                IN VARCHAR2,
                      v_id_util              IN NUMBER,
                      v_amort_max_deductibil IN NUMBER,
                      v_id_fact              IN NUMBER,
                      v_tip_operatie         IN NUMBER,
                      v_explicatia_operatie  in varchar2,
                      v_bunCapital           in number,
                      v_bazatva              in number,
                      v_tva                  in number,
                      v_id_cotatva           in number,
                      v_prorataTva           in number) IS

    v_valoare_ramasa  number := 0;
    v_data_operatie   date;
    v_dns_ramas       number := 0;
    v_id_tip_operatie number := 0;
    v_scd             varchar2(4) := '';
    v_id_operatie_mfc number := 0;
    v_id_operatie_mff number := 0;
    v_data_calcul     date;
    v_id_tip_iesire   imob_tipuri_iesire.id%type;
  BEGIN
    --SAVEPOINT A;
    INSERT /*+APPEND*/
    INTO IMOB_NOM_MF
      (DENUMIRE,
       EXPLICATIA,
       DATA_ACHIZITIE,
       DATA_PIF,
       ID_TIP_AMORTIZARE,
       ID_TIP_IMOBILIZARE,
       ID_UTIL,
       NRACT,
       NR_PIF,
       ID_FACT)
    VALUES
      (v_denumire,
       v_explicatia,
       v_dataact,
       v_data_pif,
       v_id_tip_amortizare,
       v_id_tip_imobilizare,
       v_id_util,
       v_nract,
       v_nr_pif,
       v_id_fact)
    RETURNING ID_MF INTO v_id_mf;

    -- IF v_id_mf > 0 THEN
    -- BEGIN
    v_scd := v_cont;

    v_data_operatie := v_data;
    case v_tip_operatie
      when 1 then
        -- Introducere
        v_id_tip_operatie := 1;
      when 2 then
        -- Preluare
        v_id_tip_operatie := 2;
      when 3 then
        -- Transformare imobilizare in curs in mijloc fix
        v_id_tip_operatie := 3;
    end case;

    v_valoare_ramasa := v_valoare - v_amort_prec;
    v_dns_ramas      := v_dns - v_uzura;

    pack_imob.add_in_lista_mf(v_schema,
                              v_id_mf,
                              v_dns,
                              v_cod,
                              v_scd,
                              v_acont,
                              v_id_lucrare,
                              v_id_gestiune,
                              v_id_sectie,
                              v_id_responsabil,
                              v_id_sucursala,
                              v_nr_inventar,
                              v_id_util,
                              v_data_operatie,
                              v_dns_ramas,
                              v_amort_max_deductibil);

    -- amortizare contabila
    v_id_operatie_mfc := pack_imob.add_operatie_mf(v_id_mf,
                                                   v_id_tip_operatie,
                                                   v_valoare,
                                                   v_valoare_ramasa,
                                                   v_dns_ramas,
                                                   v_valoare_reziduala,
                                                   v_data_operatie,
                                                   v_explicatia_operatie,
                                                   v_id_util,
                                                   0,
                                                   v_data,
                                                   v_nract,
                                                   v_id_tip_iesire);
    -- amortizare fiscala
    v_id_operatie_mff := pack_imob.add_operatie_mf(v_id_mf,
                                                   v_id_tip_operatie,
                                                   v_valoare,
                                                   v_valoare_ramasa,
                                                   v_dns_ramas,
                                                   v_valoare_reziduala,
                                                   v_data_operatie,
                                                   v_explicatia_operatie,
                                                   v_id_util,
                                                   1,
                                                   v_data,
                                                   v_nract,
                                                   v_id_tip_iesire);
    IF v_id_tip_imobilizare <> 3 THEN

      IF (extract(year from v_data_pif) * 12 +
         extract(month from v_data_pif) =
         extract(year from v_data_operatie) * 12 +
         extract(month from v_data_operatie)) AND v_uzura = 0 THEN
        v_data_calcul := add_months(v_data_operatie, 1);
      ELSE
        v_data_calcul := v_data_operatie;
      END IF;
      -- amortizare contabila
      pack_imob.adauga_rata(v_schema,
                            v_id_operatie_mfc,
                            v_id_tip_operatie,
                            v_id_mf,
                            v_dns_ramas,
                            v_data_calcul,
                            v_valoare,
                            v_valoare_ramasa,
                            v_uzura,
                            0);
      -- amortizare fiscala
      pack_imob.adauga_rata(v_schema,
                            v_id_operatie_mff,
                            v_id_tip_operatie,
                            v_id_mf,
                            v_dns_ramas,
                            v_data_calcul,
                            v_valoare,
                            v_valoare_ramasa,
                            v_uzura,
                            1);
    END IF;
    if v_bunCapital = 1 then
      pack_imob.adauga_buncapital(v_id_mf,
                                  v_nract,
                                  v_dataact,
                                  v_bazatva,
                                  v_tva,
                                  v_id_cotatva,
                                  0,
                                  v_tip_operatie,
                                  0,
                                  v_prorataTva);
    end if;
    -- EXCEPTION
    --    WHEN OTHERS THEN
    -- ROLLBACK TO SAVEPOINT A;
    --      RAISE;
    --RAISE_APPLICATION_ERROR(-20000, SQLERRM);
    --   END;
    --  ELSE
    --    ROLLBACK TO SAVEPOINT A;
    --  END IF;

  END adauga_mf;
  ---------------------------------------------------------------------------------------------
  PROCEDURE adauga_rata(v_schema          IN VARCHAR2,
                        v_id_operatie_mf  IN NUMBER,
                        v_id_tip_operatie IN NUMBER,
                        v_id_mf           IN NUMBER,
                        v_dns_ramas       IN NUMBER,
                        v_data_calcul     IN DATE,
                        v_valoare         IN NUMBER,
                        v_valoare_ramasa  IN NUMBER,
                        v_uzura_prec      in number,
                        v_fiscala         in number) IS
    v_an                   number;
    v_luna                 number;
    v_data_exp             date;
    v_an_exp               number;
    v_luna_exp             number;
    v_data_prec            date;
    v_an_prec              number;
    v_luna_prec            number;
    v_rata                 number;
    v_cota_luna            number;
    v_cota_an              number;
    v_rata_ultima          number;
    v_amort_prec           number;
    nPrecizie              number := 0;
    v_table                varchar2(30) := 'IMOB_CALCUL_RATE';
    v_valoare_reziduala    number;
    v_valoare_amortizabila number;

  BEGIN
    if v_dns_ramas <> 0 then
      v_data_exp := add_months(v_data_calcul, v_dns_ramas - 1);
    else
      v_data_exp := v_data_calcul;
    end if;
    v_an       := extract(year from v_data_calcul);
    v_luna     := extract(month from v_data_calcul);
    v_an_exp   := extract(year from v_data_exp);
    v_luna_exp := extract(month from v_data_exp);
    pack_contafin.SET_DATA_RON(v_schema);
    if v_luna + v_an * 12 >=
       pack_contafin.nAnRon * 12 + pack_contafin.nLunaRon then
      nPrecizie := 2;
    end if;

    IF v_dns_ramas = 0 OR v_id_tip_operatie = 4 OR v_id_tip_operatie = 10 THEN
      -- iesire din gestiune sau conservare
      v_cota_luna := 0;
      v_cota_an   := 0;
    ELSE
      v_cota_luna := ROUND(100 / v_dns_ramas, 2);
      v_cota_an   := ROUND((1 / v_dns_ramas) * 100 * 12, 2);
    END IF;
    IF V_FISCALA = 0 THEN
      select valoare_reziduala
        into v_valoare_reziduala
        from imob_operatii_mf
       where id_operatie_mf = v_id_operatie_mf;
    ELSE
      select valoare_reziduala
        into v_valoare_reziduala
        from imobf_operatii_mf
       where id_operatie_mf = v_id_operatie_mf;
    END IF;

    v_valoare_amortizabila := v_valoare_ramasa - v_valoare_reziduala;
    v_rata                 := ROUND(v_valoare_amortizabila * v_cota_an / 12 / 100,
                                    nPrecizie);
    v_data_prec            := add_months(v_data_calcul, -1);
    v_an_prec              := extract(year from v_data_prec);
    v_luna_prec            := extract(month from v_data_prec);

    IF v_dns_ramas = 0 OR v_id_tip_operatie = 4 OR v_id_tip_operatie = 10 THEN
      v_rata_ultima := 0;
    else
      v_rata_ultima := v_valoare_amortizabila -
                       v_rata *
                       (12 * v_an_exp + v_luna_exp - (12 * v_an + v_luna));
    END IF;

    IF V_FISCALA = 1 THEN
      v_table := 'IMOBF_CALCUL_RATE';
    end if;
    SAVEPOINT A;
    EXECUTE IMMEDIATE 'UPDATE ' || v_schema || '.' || v_table ||
                      ' SET AN_EXP=:1,LUNA_EXP=:2 WHERE ID_MF=:3 AND' ||
                      ' AN_EXP*12+LUNA_EXP>=:4 AND STERS=0'
      USING v_an_prec, v_luna_prec, v_id_mf, v_an * 12 + v_luna;

    IF SQL%ROWCOUNT > 1 THEN
      ROLLBACK;
      RAISE_APPLICATION_ERROR(-20000,
                              'Pe acest mijloc fix s-au facut mai multe operatii dupa data specificata!');
    END IF;
    v_amort_prec := v_valoare - v_valoare_ramasa;

    EXECUTE IMMEDIATE 'INSERT /*+APPEND*/ INTO ' || v_schema || '.' ||
                      v_table ||
                      ' (ID_OPERATIE_MF,ID_MF,AN,LUNA,AN_EXP,LUNA_EXP,RATA,COTA_LUNA,COTA_AN,ULTIMA_RATA,DATA_ULTIMA_RATA,UZURA_PREC,AMORTIZARE_PREC) ' ||
                      'VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13)'
      USING v_id_operatie_mf, v_id_mf, v_an, v_luna, v_an_exp, v_luna_exp, v_rata, v_cota_luna, v_cota_an, v_rata_ultima, v_data_exp, v_uzura_prec, v_amort_prec;
  END adauga_rata;
  ---------------------------------------------------------------------------------------------
  /*  FUNCTION calculeaza_rata_amort(v_amort_prec in number,
                                 v_an         in number,
                                 v_luna       in number,
                                 v_rata       in number) return number is
    v_luni_c  number;
    v_luni_dc number;
    v_suma    number;
  BEGIN
    v_luni_c  := pack_sesiune.getluna() + pack_sesiune.getan() * 12;
    v_luni_dc := v_luna + v_an * 12;
    v_suma    := v_rata * (v_luni_c - v_luni_dc);
    return(v_amort_prec + v_suma);
  END calculeaza_rata_amort;
  ---------------------------------------------------------------------------------------------
  FUNCTION calculeaza_uzura(v_dns_luni    in number,
                            v_dns_ramas   in number,
                            v_luna_calcul in number,
                            v_an_calcul   in number) return number is
    v_luni_c  number;
    v_luni_dc number;
    v_uzura   number;
  BEGIN
    v_luni_c  := pack_sesiune.getluna() + pack_sesiune.getan() * 12;
    v_luni_dc := v_luna_calcul + v_an_calcul * 12;
    v_uzura   := v_dns_luni - v_dns_ramas + (v_luni_c - v_luni_dc);
    --    raise_application_error(-20000,v_luna_calcul||' '||v_an_calcul);
    return(v_uzura);
  END calculeaza_uzura;
  ---------------------------------------------------------------------------------------------
  FUNCTION calculeaza_uzura_prec(v_dns_vechi          in number,
                                 v_dns_ramas          in number,
                                 v_luna_calcul        in number,
                                 v_an_calcul          in number,
                                 v_data_operatie_noua in date) return number is
    v_luni_c  number;
    v_luni_dc number;
    v_uzura   number;
  BEGIN
    v_luni_c  := extract(month from v_data_operatie_noua) +
                 extract(year from v_data_operatie_noua) * 12;
    v_luni_dc := v_luna_calcul + v_an_calcul * 12;
    v_uzura   := v_dns_vechi - v_dns_ramas + (v_luni_c - v_luni_dc);
    return(v_uzura);
  END calculeaza_uzura_prec;*/
  ---------------------------------------------------------------------------------------------
  FUNCTION getLuniOperatie return number is
  BEGIN
    return(extract(year from imob_data_operatie) * 12 +
           (extract(month from imob_data_operatie)));
  END getLuniOperatie;
  ---------------------------------------------------------------------------------------------
  PROCEDURE inreg_majorare(v_schema       in varchar2,
                           v_id_sucursala in imob_lista_mf.id_sucursala%type,
                           v_id_mf        in number,
                           v_valoare      in number,
                           v_datareeval   in date,
                           v_explicatia   in varchar2,
                           v_id_util      in number,
                           v_fiscala      in number,
                           v_datadoc      in date,
                           v_nrdoc        in number,
                           v_buncapital   in number,
                           v_bazaTva      in number,
                           v_idcotaTva    in number,
                           v_Tva          in number,
                           v_prorataTva   in number) is

    v_verificareluni number;
    v_valoare_prec   number;
    v_valoare_finala number;
    v_valoare_ramasa number;
    v_dns_ramas      number;
    v_dns            number;
    v_amort_prec     number;
    --    v_amort_prec_rata number;
    v_rata            number;
    v_id_tip_operatie number;
    --    v_data_operatie   date;
    v_data_calcul       date;
    v_id_operatie_mf    number;
    v_uzura             number;
    v_uzura_rata        number;
    v_data              date;
    v_cond              varchar2(250);
    i                   number(1);
    crsMf               pack_types.tip_cursor;
    item                tip_valori_mf;
    v_valoare_reziduala number;
    v_id_tip_iesire     imob_tipuri_iesire.id%type;

  BEGIN
    v_verificareluni := months_between(v_datareeval,
                                       pack_sesiune.getUltimaZi());

    IF v_verificareluni > -1 and v_verificareluni <= 0 THEN
      -- operatia trebuie sa fie din luna curenta

      v_data := to_date(extract(month from v_datareeval) || '/' ||
                        extract(year from v_datareeval),
                        'MM/YYYY');
      v_cond := 'n.id_mf = ' || to_char(v_id_mf);

      i := 0;
      for i in 0 .. v_fiscala loop
        --        pinfo(TO_CHAR(V_DATA) || ' ' || V_COND || ' ' || TO_CHAR(I));

        /*        crsMf := pack_imob.valori_calcul(v_schema,
        v_id_sucursala,
        v_data,
        v_data,
        v_cond,
        i);*/

        crsMF := pack_imob.valori_calcul_view(v_id_mf, v_data, i);

        fetch crsMf
          into item;

        v_valoare_prec      := item.valoare;
        v_dns_ramas         := item.dns_ramas; -- durata ramasa la operatia anterioara
        v_dns               := item.dns_luni;
        v_uzura_rata        := item.uzura_rata; -- uzura in luni de la operatia anterioara pana la luna curenta inclusiv
        v_uzura             := item.uzura;
        v_amort_prec        := item.amort_prec;
        v_rata              := item.rata;
        v_valoare_reziduala := item.valoare_reziduala;

        close crsMf;

        -- majorare
        v_id_tip_operatie := 6;
        v_valoare_finala  := v_valoare_prec + v_valoare;
        v_valoare_ramasa  := v_valoare_finala - v_amort_prec - v_rata;
        v_dns_ramas       := v_dns_ramas - v_uzura_rata; -- durata ramasa la operatia anterioara - uzura de la operatia anterioara 12.11.2019 (UTMIDIA)

        v_id_operatie_mf := pack_imob.add_operatie_mf(v_id_mf,
                                                      v_id_tip_operatie,
                                                      v_valoare_finala,
                                                      v_valoare_ramasa,
                                                      v_dns_ramas,
                                                      v_valoare_reziduala,
                                                      v_datareeval,
                                                      v_explicatia,
                                                      v_id_util,
                                                      i,
                                                      v_datadoc,
                                                      v_nrdoc,
                                                      v_id_tip_iesire);

        v_data_calcul := add_months(v_data, 1);
        pack_imob.adauga_rata(v_schema,
                              v_id_operatie_mf,
                              v_id_tip_operatie,
                              v_id_mf,
                              v_dns_ramas,
                              v_data_calcul,
                              v_valoare_finala,
                              v_valoare_ramasa,
                              v_uzura,
                              i);

      end loop;
      if v_buncapital = 1 then
        pack_imob.adauga_buncapital(v_id_mf,
                                    v_nrdoc,
                                    v_datadoc,
                                    v_bazaTva,
                                    v_tva,
                                    v_idcotaTva,
                                    0,
                                    v_id_tip_operatie,
                                    0,
                                    v_prorataTva);
      end if;
    ELSE
      RAISE_APPLICATION_ERROR(-20001,
                              'Data majorarii trebuie sa fie din luna curenta!');
    END IF;
  END inreg_majorare;
  ---------------------------------------------------------------------------------------------
  PROCEDURE inreg_reevaluare(v_schema       in varchar2,
                             v_id_sucursala in imob_lista_mf.id_sucursala%type,
                             v_id_mf        in number,
                             v_valoare      in number,
                             v_amortizare   in number,
                             v_datareeval   in date,
                             v_explicatia   in varchar2,
                             v_id_util      in number,
                             v_fiscala      in number,
                             v_datadoc      in date,
                             v_nrdoc        in number,
                             v_buncapital   in number,
                             v_valreev      in number) is
    v_verificareluni  number;
    v_valoare_finala  number;
    v_valoare_ramasa  number;
    v_dns_ramas       number;
    v_dns             number;
    v_id_tip_operatie number;
    v_data_calcul     date;
    v_id_operatie_mf  number;
    v_uzura           number;
    i                 number;
    --v_uzura_rata      number;
    v_data              date;
    v_cond              varchar2(250);
    crsMf               pack_types.tip_cursor;
    item                tip_valori_mf;
    v_valoare_reziduala number;
    v_id_tip_iesire     imob_tipuri_iesire.id%type;

  BEGIN
    v_verificareluni := months_between(v_datareeval,
                                       pack_sesiune.getUltimaZi());

    IF v_verificareluni > -1 and v_verificareluni <= 0 THEN
      -- operatia trebuie sa fie din luna curenta

      v_data := to_date(extract(month from v_datareeval) || '/' ||
                        extract(year from v_datareeval),
                        'MM/YYYY');
      v_cond := 'n.id_mf = ' || to_char(v_id_mf);
      i      := 0;
      for i in 0 .. v_fiscala loop
        /*        crsMf := pack_imob.valori_calcul(v_schema,
        v_id_sucursala,
        v_data,
        v_data,
        v_cond,
        i);*/
        crsMF := pack_imob.valori_calcul_view(v_id_mf, v_data, i);

        fetch crsMf
          into item;

        --v_dns_ramas  := item.dns_ramas;
        v_dns               := item.dns_luni;
        v_uzura             := item.uzura;
        v_valoare_reziduala := item.valoare_reziduala;
        --v_uzura_rata := item.uzura_rata;

        close crsMf;

        -- reevaluare
        v_id_tip_operatie := 7;
        v_valoare_finala  := v_valoare;
        v_valoare_ramasa  := v_valoare - v_amortizare;
        --v_dns_ramas       := v_dns_ramas - v_uzura_rata;
        v_dns_ramas := v_dns - v_uzura;

        v_id_operatie_mf := pack_imob.add_operatie_mf(v_id_mf,
                                                      v_id_tip_operatie,
                                                      v_valoare_finala,
                                                      v_valoare_ramasa,
                                                      v_dns_ramas,
                                                      v_valoare_reziduala,
                                                      v_datareeval,
                                                      v_explicatia,
                                                      v_id_util,
                                                      i,
                                                      v_datadoc,
                                                      v_nrdoc,
                                                      v_id_tip_iesire);

        v_data_calcul := add_months(v_data, 1);

        pack_imob.adauga_rata(v_schema,
                              v_id_operatie_mf,
                              v_id_tip_operatie,
                              v_id_mf,
                              v_dns_ramas,
                              v_data_calcul,
                              v_valoare_finala,
                              v_valoare_ramasa,
                              v_uzura,
                              i);
      end loop;
      if v_buncapital = 1 then
        update imob_bunuricapital
           set valreev = v_valreev
         where id_mf = v_id_mf
           and id_tip_operatie in (1, 2);
      end if;
    ELSE
      RAISE_APPLICATION_ERROR(-20001,
                              'Data reevaluarii trebuie sa fie din luna curenta!');
    END IF;
  END inreg_reevaluare;

  ---------------------------------------------------------------------
  -- schimbare DNS si reevaluarea valorii de inventar si amortizarii totale
  ---------------------------------------------------------------------
  PROCEDURE inreg_schimbdns_reevaluare(v_schema            in varchar2,
                                       v_id_sucursala      in imob_lista_mf.id_sucursala%type,
                                       v_id_mf             in number,
                                       v_cod               IN VARCHAR2, -- codul nou
                                       v_dns               IN NUMBER, -- dns nou
                                       v_valoare           in number, -- valoarea noua de inventar
                                       v_amortizare        in number, -- valoarea noua total amortizata
                                       v_uzura             IN NUMBER, -- uzura noua totala
                                       v_valoare_reziduala IN NUMBER, -- valoarea noua reziduala
                                       v_datareeval        in date,
                                       v_explicatia        in varchar2,
                                       v_id_util           in number,
                                       v_fiscala           in number,
                                       v_datadoc           in date,
                                       v_nrdoc             in number,
                                       v_buncapital        in number,
                                       v_valreev           in number) is
    v_verificareluni  number;
    v_codvechi        imob_lista_mf.cod_mf%type;
    v_dns_vechi       imob_lista_mf.dns_luni%type;
    v_valoare_ramasa  number;
    v_dns_ramas       number;
    v_id_tip_operatie number := 15;
    v_data_calcul     date;
    v_id_operatie_mf  number;
    i                 number;
    v_data            date;
    -- v_cond              varchar2(250);
    crsMf           pack_types.tip_cursor;
    item            tip_valori_mf;
    v_id_tip_iesire imob_tipuri_iesire.id%type;

  BEGIN
    v_verificareluni := months_between(v_datareeval,
                                       pack_sesiune.getUltimaZi());

    IF v_verificareluni = 0 THEN
      -- operatia trebuie sa fie din luna curenta

      v_data := to_date(extract(month from v_datareeval) || '/' ||
                        extract(year from v_datareeval),
                        'MM/YYYY');
      -- v_cond := 'n.id_mf = ' || to_char(v_id_mf);
      i := 0;
      for i in 0 .. v_fiscala loop
        /*        crsMf := pack_imob.valori_calcul(v_schema,
        v_id_sucursala,
        v_data,
        v_data,
        v_cond,
        i);*/
        crsMF := pack_imob.valori_calcul_view(v_id_mf, v_data, i);

        fetch crsMf
          into item;

        v_dns_vechi := item.dns_luni;
        v_codvechi  := item.cod_mf;

        close crsMf;

        -- valoarea ramasa = valoarea noua de inventar - amortizarea noua totala
        v_valoare_ramasa := v_valoare - v_amortizare;
        -- durata ramasa = DNS - uzura noua totala
        v_dns_ramas := v_dns - v_uzura;

        -- schimbare DNS
        IF (v_codvechi <> v_cod OR v_dns_vechi <> v_dns) and i = 0 THEN
          --sa intre doar o data
          pack_imob.add_in_lista_mf(v_schema,
                                    v_id_mf,
                                    v_dns,
                                    v_cod,
                                    item.cont,
                                    item.acont,
                                    item.id_lucrare,
                                    item.id_gestiune,
                                    item.id_sectie,
                                    item.id_responsabil,
                                    item.id_sucursala,
                                    item.nr_inventar,
                                    v_id_util,
                                    v_datareeval,
                                    v_dns_ramas,
                                    item.amort_max_deductibil);

        END IF;

        v_id_operatie_mf := pack_imob.add_operatie_mf(v_id_mf,
                                                      v_id_tip_operatie,
                                                      v_valoare,
                                                      v_valoare_ramasa,
                                                      v_dns_ramas,
                                                      v_valoare_reziduala,
                                                      v_datareeval,
                                                      v_explicatia,
                                                      v_id_util,
                                                      i,
                                                      v_datadoc,
                                                      v_nrdoc,
                                                      v_id_tip_iesire);

        v_data_calcul := add_months(v_data, 1);

        pack_imob.adauga_rata(v_schema,
                              v_id_operatie_mf,
                              v_id_tip_operatie,
                              v_id_mf,
                              v_dns_ramas,
                              v_data_calcul,
                              v_valoare,
                              v_valoare_ramasa,
                              v_uzura,
                              i);
      end loop;
      if v_buncapital = 1 then
        update imob_bunuricapital
           set valreev = v_valreev
         where id_mf = v_id_mf
           and id_tip_operatie in (1, 2);
      end if;
    ELSE
      RAISE_APPLICATION_ERROR(-20001,
                              'Data reevaluarii trebuie sa fie din luna curenta!');
    END IF;
  END inreg_schimbdns_reevaluare;

  ---------------------------------------------------------------------------------------------
  PROCEDURE inreg_conservare(v_schema         IN VARCHAR2,
                             v_id_sucursala   in imob_lista_mf.id_sucursala%type,
                             v_id_mf          IN NUMBER,
                             v_tip            IN NUMBER,
                             v_cauza          IN VARCHAR2,
                             v_dataconservare IN DATE,
                             v_id_util        IN NUMBER,
                             v_fiscala        in number,
                             v_datadoc        in date,
                             v_nrdoc          in number,
                             v_buncapital     in number,
                             v_bazatva        in number,
                             v_idcotaTva      in number,
                             v_tva            in number,
                             v_prorataTva     in number) IS
    v_verificareluni  number;
    v_valoare         number;
    v_valoare_ramasa  number;
    v_dns_ramas       number;
    v_dns             number;
    v_id_tip_operatie number;
    v_data_calcul     date;
    v_data            date;
    v_amort_prec      number;
    v_id_operatie_mf  number;
    v_uzura           number;
    --v_uzura_rata      number;
    v_uzura_prec        number;
    v_rata              number;
    v_mesaj             varchar2(50);
    v_cond              varchar2(250);
    i                   number;
    crsMf               pack_types.tip_cursor;
    item                tip_valori_mf;
    v_valoare_reziduala number;
    v_id_tip_iesire     imob_tipuri_iesire.id%type;

  BEGIN
    v_verificareluni := months_between(v_dataconservare,
                                       pack_sesiune.getUltimaZi());

    IF v_verificareluni > -1 and v_verificareluni <= 0 THEN
      -- operatia trebuie sa fie din luna curenta

      v_data := to_date(extract(month from v_dataconservare) || '/' ||
                        extract(year from v_dataconservare),
                        'MM/YYYY');
      v_cond := 'n.id_mf = ' || to_char(v_id_mf);
      i      := 0;
      for i in 0 .. v_fiscala loop
        /*        crsMf := pack_imob.valori_calcul(v_schema,
        v_id_sucursala,
        v_data,
        v_data,
        v_cond,
        i);*/
        crsMF := pack_imob.valori_calcul_view(v_id_mf, v_data, i);

        fetch crsMf
          into item;
        v_valoare := item.valoare;
        --v_dns_ramas      := item.dns_ramas;
        v_dns := item.dns_luni;
        --v_valoare_ramasa := item.valoare_ramasa_amort;
        v_amort_prec := item.amort_prec;
        v_rata       := item.rata;
        v_uzura      := item.uzura;
        --v_uzura_rata := item.uzura_rata;
        v_uzura_prec        := item.uzura_prec;
        v_valoare_reziduala := item.valoare_reziduala;

        close crsMf;

        IF v_tip = 1 THEN
          -- intrare in conservare
          v_id_tip_operatie := 10;
        ELSE
          -- iesire din conservare
          v_id_tip_operatie := 11;
          v_uzura           := v_uzura_prec; --(nu stiu dc numar si lunile de conservare)
        END IF;

        v_valoare_ramasa := v_valoare - v_amort_prec - v_rata;
        v_dns_ramas      := v_dns - v_uzura;

        v_id_operatie_mf := pack_imob.add_operatie_mf(v_id_mf,
                                                      v_id_tip_operatie,
                                                      v_valoare,
                                                      v_valoare_ramasa,
                                                      v_dns_ramas,
                                                      v_valoare_reziduala,
                                                      v_dataconservare,
                                                      v_cauza,
                                                      v_id_util,
                                                      i,
                                                      v_datadoc,
                                                      v_nrdoc,
                                                      v_id_tip_iesire);

        v_data_calcul := add_months(v_data, 1);

        pack_imob.adauga_rata(v_schema,
                              v_id_operatie_mf,
                              v_id_tip_operatie,
                              v_id_mf,
                              v_dns_ramas,
                              v_data_calcul,
                              v_valoare,
                              v_valoare_ramasa,
                              v_uzura,
                              i);
      end loop;
      if v_buncapital = 1 then
        if v_tip = 1 then
          pack_imob.adauga_buncapital(v_id_mf,
                                      v_nrdoc,
                                      v_datadoc,
                                      v_bazaTva,
                                      0,
                                      v_idcotaTva,
                                      0,
                                      v_id_tip_operatie,
                                      v_tva,
                                      v_prorataTva);
        else
          pack_imob.adauga_buncapital(v_id_mf,
                                      v_nrdoc,
                                      v_datadoc,
                                      v_bazaTva,
                                      0,
                                      v_idcotaTva,
                                      v_tva,
                                      v_id_tip_operatie,
                                      0,
                                      v_prorataTva);
        end if;
      end if;
    ELSE
      IF v_tip = 1 THEN
        v_mesaj := 'Data intrarii in conservare trebuie sa fie din luna curenta!';
      ELSE
        v_mesaj := 'Data iesirii din conservare trebuie sa fie din luna curenta!';
      END IF;
      RAISE_APPLICATION_ERROR(-20001, v_mesaj);
    END IF;
  END inreg_conservare;
  ---------------------------------------------------------------------------------------------
  PROCEDURE inreg_schimbdns(v_schema        IN VARCHAR2,
                            v_id_sucursala  in imob_lista_mf.id_sucursala%type,
                            v_id_mf         IN NUMBER,
                            v_codnou        IN VARCHAR2,
                            v_dnsnou        IN NUMBER,
                            v_dataschimbare IN DATE,
                            v_cauza         IN VARCHAR2,
                            v_id_util       IN NUMBER,
                            v_fiscala       in number,
                            v_datadoc       in date,
                            v_nrdoc         in number) IS

    v_verificareluni number;
    v_valoare        number;
    v_valoare_ramasa number;
    --v_rata            number;
    v_uzura_calc      number;
    v_codvechi        varchar2(30);
    v_dns_ramas_nou   number;
    v_dns_vechi       number;
    v_id_tip_operatie number := 8;
    v_uzura_prec      number;
    v_data_calcul     date;
    v_id_operatie_mf  number;
    i                 number;
    --v_cauza           varchar2(10);
    v_data              date;
    v_cond              varchar2(250);
    crsMf               pack_types.tip_cursor;
    item                tip_valori_mf;
    v_valoare_reziduala number;
    v_id_tip_iesire     imob_tipuri_iesire.id%type;

  BEGIN

    v_verificareluni := months_between(v_dataschimbare,
                                       pack_sesiune.getUltimaZi());

    IF v_verificareluni > -1 and v_verificareluni <= 0 THEN
      -- operatia trebuie sa fie din luna curenta

      v_data := to_date(extract(month from v_dataschimbare) || '/' ||
                        extract(year from v_dataschimbare),
                        'MM/YYYY');
      v_cond := 'n.id_mf = ' || to_char(v_id_mf);
      i      := 0;
      for i in 0 .. v_fiscala loop
        /*        crsMf := pack_imob.valori_calcul(v_schema,
        v_id_sucursala,
        v_data,
        v_data,
        v_cond,
        i);*/
        crsMF := pack_imob.valori_calcul_view(v_id_mf, v_data, i);

        fetch crsMf
          into item;
        v_valoare           := item.valoare;
        v_dns_vechi         := item.dns_luni;
        v_valoare_ramasa    := item.valoare - item.amort_prec;
        v_uzura_calc        := item.uzura - 1;
        v_codvechi          := item.cod_mf;
        v_valoare_reziduala := item.valoare_reziduala;

        close crsMf;

        -- 27.02.2013 : am pus case in loc de 1 - (v_uzura_calc / v_dns_vechi) pentru cazul
        --in care aveam v_dns_vechi = 0
        IF v_dns_vechi = 0 THEN
            v_dns_ramas_nou := v_dnsnou;
        ELSE
            v_dns_ramas_nou :=  (v_dnsnou-nvl(v_uzura_calc,0));
        END IF;

        IF (v_codvechi <> v_codnou OR v_dns_vechi <> v_dnsnou) and i = 0 THEN
          --sa intre doar o data
          pack_imob.add_in_lista_mf(v_schema,
                                    v_id_mf,
                                    v_dnsnou,
                                    v_codnou,
                                    item.cont,
                                    item.acont,
                                    item.id_lucrare,
                                    item.id_gestiune,
                                    item.id_sectie,
                                    item.id_responsabil,
                                    item.id_sucursala,
                                    item.nr_inventar,
                                    v_id_util,
                                    v_dataschimbare,
                                    v_dns_ramas_nou,
                                    item.amort_max_deductibil);

        END IF;

        v_id_operatie_mf := pack_imob.add_operatie_mf(v_id_mf,
                                                      v_id_tip_operatie,
                                                      v_valoare,
                                                      v_valoare_ramasa,
                                                      v_dns_ramas_nou,
                                                      v_valoare_reziduala,
                                                      v_dataschimbare,
                                                      v_cauza,
                                                      v_id_util,
                                                      i,
                                                      v_datadoc,
                                                      v_nrdoc,
                                                      v_id_tip_iesire);

        v_data_calcul := v_dataschimbare;
        v_uzura_prec  := v_dnsnou - v_dns_ramas_nou;

        pack_imob.adauga_rata(v_schema,
                              v_id_operatie_mf,
                              v_id_tip_operatie,
                              v_id_mf,
                              v_dns_ramas_nou,
                              v_data_calcul,
                              v_valoare,
                              v_valoare_ramasa,
                              v_uzura_prec,
                              i);
      end loop;
    ELSE
      RAISE_APPLICATION_ERROR(-20001,
                              'Data schimbarii DNS trebuie sa fie din luna curenta!');
    END IF;
  END inreg_schimbdns;
  ---------------------------------------------------------------------------------------------
  PROCEDURE inreg_gest(v_schema        IN VARCHAR2,
                       v_id_sucursala  in imob_lista_mf.id_sucursala%type,
                       v_id_mf         IN NUMBER,
                       v_tip           IN NUMBER,
                       v_cauza         IN VARCHAR2,
                       v_dataschimbare IN DATE,
                       v_id_util       IN NUMBER,
                       v_fiscala       in number,
                       v_datadoc       in date,
                       v_nrdoc         in number,
                       v_buncapital    in number,
                       v_bazatva       in number,
                       v_idcotaTva     in number,
                       v_tva           in number,
                       v_prorataTva    in number,
                       v_id_tip_iesire in imob_tipuri_iesire.id%type default null) IS

    v_verificareluni  number;
    v_valoare         number;
    v_valoare_ramasa  number;
    v_dns_ramas       number;
    v_dns             number;
    v_id_tip_operatie number;
    --v_amortizare      number;
    v_id_operatie_mf number;
    v_mesaj          varchar2(100);
    v_uzura_prec     number;
    --v_uzura_rata      number;
    v_uzura      number;
    v_amort_prec number;
    i            number;
    --v_rata            number;
    v_data              date;
    v_cond              varchar2(200);
    crsMF               PACK_TYPES.tip_cursor;
    item                tip_valori_mf;
    v_valoare_reziduala number;

  BEGIN
    v_verificareluni := months_between(v_dataschimbare,
                                       pack_sesiune.getUltimaZi());

    IF v_verificareluni > -1 and v_verificareluni <= 0 THEN
      -- operatia trebuie sa fie din luna curenta

      v_data := to_date(extract(month from v_dataschimbare) || '/' ||
                        extract(year from v_dataschimbare),
                        'MM/YYYY');
      v_cond := 'n.id_mf = ' || to_char(v_id_mf);
      i      := 0;
      for i in 0 .. v_fiscala loop
        /*        crsMf := pack_imob.valori_calcul(v_schema,
        v_id_sucursala,
        v_data,
        v_data,
        v_cond,
        i);*/
        crsMF := pack_imob.valori_calcul_view(v_id_mf, v_data, i);

        fetch crsMf
          into item;

        v_valoare    := item.valoare;
        v_dns        := item.dns_luni;
        v_amort_prec := item.amort_prec;
        --v_rata       := item.rata;
        v_uzura             := item.uzura - 1;
        v_uzura_prec        := item.uzura_prec;
        v_valoare_reziduala := item.valoare_reziduala;
        --dbms_output.put_line('valoare ' || v_valoare);
        close crsMf;

        IF v_tip = 1 THEN
          -- iesire din gestiune
          v_id_tip_operatie := 4;
        ELSE
          -- intrare in gestiune
          v_id_tip_operatie := 5;
          v_uzura           := v_uzura_prec; --(nu stiu dc numar si lunile de iesire)
        END IF;

        v_valoare_ramasa := v_valoare - v_amort_prec;
        v_dns_ramas      := v_dns - v_uzura;

        v_id_operatie_mf := pack_imob.add_operatie_mf(v_id_mf,
                                                      v_id_tip_operatie,
                                                      v_valoare,
                                                      v_valoare_ramasa,
                                                      v_dns_ramas,
                                                      v_valoare_reziduala,
                                                      v_dataschimbare,
                                                      v_cauza,
                                                      v_id_util,
                                                      i,
                                                      v_datadoc,
                                                      v_nrdoc,
                                                      v_id_tip_iesire);

        pack_imob.adauga_rata(v_schema,
                              v_id_operatie_mf,
                              v_id_tip_operatie,
                              v_id_mf,
                              v_dns_ramas,
                              v_data,
                              v_valoare,
                              v_valoare_ramasa,
                              v_uzura,
                              i);
      end loop;
      if v_buncapital = 1 then
        if v_tip = 1 then
          pack_imob.adauga_buncapital(v_id_mf,
                                      v_nrdoc,
                                      v_datadoc,
                                      v_bazaTva,
                                      0,
                                      v_idcotaTva,
                                      0,
                                      v_id_tip_operatie,
                                      v_tva,
                                      v_prorataTva);
        else
          pack_imob.adauga_buncapital(v_id_mf,
                                      v_nrdoc,
                                      v_datadoc,
                                      v_bazaTva,
                                      0,
                                      v_idcotaTva,
                                      v_tva,
                                      v_id_tip_operatie,
                                      0,
                                      v_prorataTva);
        end if;
      end if;
    ELSE
      IF v_tip = 1 THEN
        v_mesaj := 'Data iesirii din gestiune trebuie sa fie din luna curenta!';
      ELSE
        v_mesaj := 'Data intrarii in gestiune trebuie sa fie din luna curenta!';
      END IF;
      RAISE_APPLICATION_ERROR(-20001, v_mesaj);
    END IF;
  END inreg_gest;

  ---------------------------------------------------------------------------------------------
  -- SE ADAUGA OPERATIA TRANSFORMARE IN MIJLOC FIX PENTRU IMOBILIZARI IN CURS
  ---------------------------------------------------------------------------------------------
  PROCEDURE inreg_transformare_in_mf(v_schema               IN VARCHAR2,
                                     v_id_mf                IN NUMBER,
                                     v_id_mf_nou            IN NUMBER,
                                     v_datamodificare       IN DATE,
                                     v_nract                IN NUMBER,
                                     v_dataact              IN DATE,
                                     v_denumire             IN VARCHAR2,
                                     v_explicatia           IN VARCHAR2,
                                     v_valoare              IN NUMBER,
                                     v_data_pif             IN DATE,
                                     v_nr_pif               IN NUMBER,
                                     v_nr_inventar          IN NUMBER,
                                     v_cod                  IN VARCHAR2,
                                     v_dns                  IN NUMBER,
                                     v_uzura                IN NUMBER,
                                     v_dns_ramas            IN NUMBER,
                                     v_valoare_ramasa       IN NUMBER,
                                     v_valoare_retinuta     IN NUMBER,
                                     v_id_gestiune          IN NUMBER,
                                     v_id_responsabil       IN NUMBER,
                                     v_id_sectie            IN NUMBER,
                                     v_id_lucrare           IN NUMBER,
                                     v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                                     v_id_tip_amortizare    IN NUMBER,
                                     v_procent              IN NUMBER,
                                     v_coeficient           IN NUMBER,
                                     v_id_tip_imobilizare   IN NUMBER,
                                     v_cont                 IN VARCHAR2,
                                     v_acont                IN VARCHAR2,
                                     v_id_util              IN NUMBER,
                                     v_amort_max_deductibil IN NUMBER,
                                     v_fiscala              in number,
                                     v_datadoc              in date,
                                     v_nrdoc                in number,
                                     v_id_tip_operatie      in number DEFAULT 3) IS

    v_id_operatie_mf    number := 0;
    v_id_operatie_nou   number := 0;
    v_valoare_reziduala number := 0; -- valoare reziduala a unei imobilizari in curs = 0
    v_id_tip_iesire     imob_tipuri_iesire.id%type;

  BEGIN
    IF nvl(v_id_mf, 0) = 0 THEN
      RAISE_APPLICATION_ERROR(-20000,
                              'Nu este completat id-ul imobilizarii in curs!');
    END IF;

    UPDATE IMOB_NOM_MF
       SET DENUMIRE           = v_denumire,
           EXPLICATIA         = v_explicatia,
           DATA_ACHIZITIE     = v_dataact,
           DATA_PIF           = v_data_pif,
           ID_TIP_AMORTIZARE  = v_id_tip_amortizare,
           ID_TIP_IMOBILIZARE = v_id_tip_imobilizare,
           ID_UTIL            = v_id_util,
           NRACT              = v_nract,
           NR_PIF             = v_nr_pif
     WHERE ID_MF = v_id_mf;

    pack_imob.add_in_lista_mf(v_schema,
                              v_id_mf,
                              v_dns,
                              v_cod,
                              v_cont,
                              v_acont,
                              v_id_lucrare,
                              v_id_gestiune,
                              v_id_sectie,
                              v_id_responsabil,
                              v_id_sucursala,
                              v_nr_inventar,
                              v_id_util,
                              v_datamodificare,
                              v_dns_ramas,
                              v_amort_max_deductibil);

    v_id_operatie_mf := pack_imob.add_operatie_mf(v_id_mf,
                                                  v_id_tip_operatie,
                                                  v_valoare,
                                                  v_valoare_ramasa,
                                                  v_dns_ramas,
                                                  v_valoare_reziduala,
                                                  v_datamodificare,
                                                  v_explicatia,
                                                  v_id_util,
                                                  v_fiscala,
                                                  v_datadoc,
                                                  v_nrdoc,
                                                  v_id_tip_iesire);

    pack_imob.adauga_rata(v_schema,
                          v_id_operatie_mf,
                          v_id_tip_operatie,
                          v_id_mf,
                          v_dns_ramas,
                          v_datamodificare,
                          v_valoare,
                          v_valoare_ramasa,
                          v_uzura,
                          v_fiscala);

    -- inregistrez (id_mf_nou, id_operatie_nou, id_imobilizare_curs, id_operatie_curs, valoare)
    select min(id_operatie_mf)
      into v_id_operatie_nou
      from imob_operatii_mf
     where id_mf = v_id_mf_nou;

    pack_imob.adauga_la_transformare(v_id_mf_nou,
                                     v_id_operatie_nou,
                                     v_id_mf,
                                     v_id_operatie_mf,
                                     v_valoare_retinuta);

  END inreg_transformare_in_mf;

  ---------------------------------------------------------------------------------------------
  -- MODIFICARE CU ISTORIC
  -- schimba_mf: cand se modifica valori din nomenclator/lista/operatie/rata mobilizare (lista, operatie, rata)
  -- din butonul frm_lista.modificare()
  -- se sterg vechile operatii, rate si se adauga inregistrari noi
  ---------------------------------------------------------------------------------------------
  ---------------------------------------------------------------------------------------------


  procedure schimba_mf(v_id_mf                IN NUMBER,
                       v_nract                IN NUMBER,
                       v_data_achizitie       IN DATE,
                       v_denumire             IN VARCHAR2,
                       v_explicatie_n         IN VARCHAR2,
                       v_data_pif             IN DATE,
                       v_nr_pif               IN NUMBER,
                       v_procent              IN NUMBER,
                       v_coeficient           IN NUMBER,
                       v_id_tip_amortizare    IN NUMBER,
                       v_id_tip_imobilizare   IN NUMBER,
                       v_id_lista_mf          IN NUMBER,
                       v_dns_luni             IN NUMBER,
                       v_Cod_mf               IN VARCHAR2,
                       v_cont                 IN VARCHAR2,
                       v_acont                IN VARCHAR2,
                       v_nr_inventar          IN NUMBER,
                       v_id_gestiune          IN NUMBER,
                       v_id_responsabil       IN NUMBER,
                       v_id_sectie            IN NUMBER,
                       v_id_lucrare           IN NUMBER,
                       v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                       v_lper_in              in VARCHAR2,
                       v_lper_fin             in VARCHAR2,
                       v_id_operatie_mf       IN NUMBER,
                       v_id_tip_operatie      in NUMBER,
                       v_valoare              IN NUMBER,
                       v_valoare_ramasa       IN NUMBER,
                       v_dns_ramas            in NUMBER,
                       v_valoare_reziduala    IN NUMBER,
                       v_data_operatie        in DATE,
                       v_explicatie_o         in VARCHAR2,
                       v_uzura_prec           in NUMBER,
                       v_id_rata              in NUMBER,
                       v_rper_in              in VARCHAR2,
                       v_rper_fin             in VARCHAR2,
                       v_rata                 in NUMBER,
                       v_cota_an              in NUMBER,
                       v_cota_luna            in NUMBER,
                       v_ultima_rata          in NUMBER,
                       v_data_ultima_rata     in DATE,
                       v_fiscala              in NUMBER,
                       v_id_util              IN NUMBER,
                       v_amort_max_deductibil IN NUMBER,
                       v_nrdoc                IN IMOB_OPERATII_MF.NRDOC%TYPE DEFAULT NULL,
                       v_datadoc              in IMOB_OPERATII_MF.DATADOC%TYPE DEFAULT NULL) IS

    tnLuna           number := 0;
    tnLunaExp        number := 0;
    tnAn             number := 0;
    tnAnExp          number := 0;
    v_amort_prec     number := 0;
    v_table_operatii varchar2(30);
    v_table_rate     varchar2(30);

    v_row_op  imob_operatii_mf%ROWTYPE;
    v_row_opf imobf_operatii_mf%ROWTYPE;

    v_row_rate  imob_calcul_rate%ROWTYPE;
    v_row_ratef imobf_calcul_rate%ROWTYPE;
    ldDataOra   date := sysdate;
    lnIdRata    imob_calcul_rate.id_calcul_rate%type := v_id_rata;
  begin

    if v_fiscala = 0 then
      v_table_operatii := 'imob_operatii_mf';
      v_table_rate     := 'imob_calcul_rate';
    else
      v_table_operatii := 'imobf_operatii_mf';
      v_table_rate     := 'imobf_calcul_rate';
    end if;

    UPDATE IMOB_NOM_MF
       SET nract              = v_nract,
           data_achizitie     = v_data_achizitie,
           denumire           = v_denumire,
           explicatia         = v_explicatie_n,
           data_pif           = v_data_pif,
           nr_pif             = v_nr_pif,
           procent            = v_procent,
           coeficient         = v_coeficient,
           id_tip_amortizare  = v_id_tip_amortizare,
           id_tip_imobilizare = v_id_tip_imobilizare
     WHERE ID_MF = v_id_mf;

    tnLuna    := to_number(substr(v_lper_in, 1, 2));
    tnAn      := to_number(substr(v_lper_in, 3, 4));
    tnLunaExp := to_number(substr(v_lper_fin, 1, 2));
    tnAnExp   := to_number(substr(v_lper_fin, 3, 4));
    /*    EXECUTE IMMEDIATE 'UPDATE ' || v_schema || '.IMOB_LISTA_MF ' ||
                    'SET NR_INVENTAR=:1,id_lucrare=:2,id_gestiune=:3,' ||
                    'id_sectie=:4,id_responsabil=:5,dns_luni=:6,cod_mf=:7,cont=:8,acont=:9,an=:10,luna=:11,an_exp=:12,luna_exp=:13 WHERE ID_LISTA_MF =:14 '
    USING v_nr_inventar, v_id_lucrare, v_id_gestiune, v_id_sectie, v_id_responsabil, v_dns_luni, v_Cod_mf, v_cont, v_acont, tnAn, tnLuna, tnAnExp, tnLunaExp, v_id_lista_mf;*/

    pack_imob.modifica_lista_mf(v_id_mf,
                                v_id_lista_mf,
                                v_dns_luni,
                                v_Cod_mf,
                                v_cont,
                                v_acont,
                                v_nr_inventar,
                                v_id_gestiune,
                                v_id_responsabil,
                                v_id_sectie,
                                v_id_lucrare,
                                v_id_sucursala,
                                v_id_util,
                                tnAn,
                                tnLuna,
                                tnAnExp,
                                tnLunaExp,
                                v_amort_max_deductibil);

    --------------------------------------
    tnLuna       := to_number(substr(v_rper_in, 1, 2));
    tnAn         := to_number(substr(v_rper_in, 3, 4));
    tnLunaExp    := to_number(substr(v_rper_fin, 1, 2));
    tnAnExp      := to_number(substr(v_rper_fin, 3, 4));
    v_amort_prec := v_valoare - v_valoare_ramasa;

    -- if v_id_tip_imobilizare <> 3 then
    -- imobilizarile in curs nu au operatii si rate
    if v_fiscala = 0 then
      -- imobilizarile in curs nu transmit informatiile despre rate (nu au rate), dar sunt necesare
      -- determin id_rata in functie de id_operate
      if v_id_tip_imobilizare = 3 and nvl(lnIdRata, 0) = 0 then
        select id_calcul_rate
          into lnIdRata
          from imob_calcul_rate a
         where a.id_operatie_mf = v_id_operatie_mf;
      end if;

      if nvl(v_id_operatie_mf, 0) = 0 or nvl(lnIdRata, 0) = 0 then
        RAISE_APPLICATION_ERROR(-20000,
                                'Nu este completat Id operatie sau Id rata!');
      end if;

      select *
        into v_row_op
        from imob_operatii_mf
       where id_operatie_mf = v_id_operatie_mf;

      select *
        into v_row_rate
        from imob_calcul_rate
       where id_calcul_rate = lnIdRata;

      UPDATE imob_operatii_mf
         SET sters         = 1,
             id_util_sters = v_id_util,
             dataora_sters = ldDataOra
       WHERE id_operatie_mf = v_id_operatie_mf;

      v_row_op.valoare           := v_valoare;
      v_row_op.valoare_ramasa    := v_valoare_ramasa;
      v_row_op.dns_ramas         := v_dns_ramas;
      v_row_op.valoare_reziduala := v_valoare_reziduala;
      v_row_op.data_operatie     := v_data_operatie;
      v_row_op.explicatia        := v_explicatie_o;
      v_row_op.id_tip_operatie   := v_id_tip_operatie;
      v_row_op.id_util           := v_id_util;
      v_row_op.dataora           := ldDataOra;
      v_row_op.nrdoc             := NVL(v_nrdoc, v_row_op.nrdoc);
      v_row_op.datadoc           := NVL(v_datadoc, v_row_op.datadoc);

      insert into imob_operatii_mf
      values v_row_op
      returning id_operatie_mf into v_row_op.id_operatie_mf;

      UPDATE imob_calcul_rate
         SET sters = 1
       WHERE id_calcul_rate = lnIdRata;

      v_row_rate.rata             := v_rata;
      v_row_rate.cota_An          := v_cota_an;
      v_row_rate.cota_luna        := v_cota_luna;
      v_row_rate.ultima_rata      := v_ultima_rata;
      v_row_rate.data_ultima_rata := v_data_ultima_rata;
      v_row_rate.an               := tnAn;
      v_row_rate.luna             := tnLuna;
      v_row_rate.an_exp           := tnAnExp;
      v_row_rate.luna_exp         := tnLunaExp;
      v_row_rate.uzura_prec       := v_uzura_prec;
      v_row_rate.amortizare_prec  := v_amort_prec;
      v_row_rate.id_operatie_mf   := v_row_op.id_operatie_mf;

      insert into imob_calcul_rate
      values v_row_rate
      returning id_calcul_rate into v_row_rate.id_calcul_rate;

    else
      -- imobilizarile in curs nu transmit informatiile despre rate (nu au rate), dar sunt necesare
      -- determin id_rata in functie de id_operate
      if v_id_tip_imobilizare = 3 and nvl(lnIdRata, 0) = 0 then
        begin
          select id_calcul_rate
            into lnIdRata
            from imobf_calcul_rate a
           where a.id_operatie_mf = v_id_operatie_mf;
        exception
          when NO_DATA_FOUND then
            RAISE_APPLICATION_ERROR(-20000,
                                    'Nu este completat Id operatie sau Id rata!');
        end;
      end if;

      if nvl(v_id_operatie_mf, 0) = 0 or nvl(lnIdRata, 0) = 0 then
        RAISE_APPLICATION_ERROR(-20000,
                                'Nu este completat Id operatie sau Id rata!');
      end if;

      select *
        into v_row_opf
        from imobf_operatii_mf
       where id_operatie_mf = v_id_operatie_mf;

      select *
        into v_row_ratef
        from imobf_calcul_rate
       where id_calcul_rate = lnIdRata;

      UPDATE imobf_operatii_mf
         SET sters         = 1,
             id_util_sters = v_id_util,
             dataora_sters = ldDataOra
       WHERE id_operatie_mf = v_id_operatie_mf;

      v_row_opf.valoare         := v_valoare;
      v_row_opf.valoare_ramasa  := v_valoare_ramasa;
      v_row_opf.dns_ramas       := v_dns_ramas;
      v_row_opf.data_operatie   := v_data_operatie;
      v_row_opf.explicatia      := v_explicatie_o;
      v_row_opf.id_tip_operatie := v_id_tip_operatie;

      insert into imobf_operatii_mf
      values v_row_opf
      returning id_operatie_mf into v_row_opf.id_operatie_mf;

      UPDATE imobf_calcul_rate
         SET sters = 1
       WHERE id_calcul_rate = lnIdRata;

      v_row_ratef.rata             := v_rata;
      v_row_ratef.cota_An          := v_cota_an;
      v_row_ratef.cota_luna        := v_cota_luna;
      v_row_ratef.ultima_rata      := v_ultima_rata;
      v_row_ratef.data_ultima_rata := v_data_ultima_rata;
      v_row_ratef.an               := tnAn;
      v_row_ratef.luna             := tnLuna;
      v_row_ratef.an_exp           := tnAnExp;
      v_row_ratef.luna_exp         := tnLunaExp;
      v_row_ratef.uzura_prec       := v_uzura_prec;
      v_row_ratef.amortizare_prec  := v_amort_prec;
      v_row_ratef.id_operatie_mf   := v_row_opf.id_operatie_mf;

      insert into imobf_calcul_rate
      values v_row_ratef
      returning id_calcul_rate into v_row_ratef.id_calcul_rate;

    end if;
    -- end if;

  end schimba_mf;

  ------------------------------------------------------------------------------------------------
  procedure sterge_mf(v_id_mf IN NUMBER, v_id_util IN number default null) IS
  begin
    for item in (select id_operatie_mf, id_operatie_prec, 0 as fiscala
                   from imob_operatii_mf
                  where id_mf = v_id_mf
                  order by id_operatie_mf desc) loop
      pack_imob.sterge_operatie_mf(item.id_operatie_mf,
                                   item.id_operatie_prec,
                                   item.fiscala);
    end loop;

    for item in (select id_operatie_mf, id_operatie_prec, 1 as fiscala
                   from imobf_operatii_mf
                  where id_mf = v_id_mf
                  order by id_operatie_mf desc) loop
      pack_imob.sterge_operatie_mf(item.id_operatie_mf,
                                   item.id_operatie_prec,
                                   item.fiscala);
    end loop;

    UPDATE imob_lista_mf
       SET sters = 1
     WHERE id_mf = v_id_mf
       and sters = 0;

    update imob_nom_mf
       set sters = 1, dataora_sters = sysdate, id_util_sters = v_id_util
     where id_mf = v_id_mf;

  end sterge_mf;
  ------------------------------------------------------------------------------------------------
  /*  function add_operatie_mf(v_schema              IN VARCHAR2,
                           v_id_mf               IN NUMBER,
                           v_id_tip_operatie     IN NUMBER,
                           v_valoare             IN NUMBER,
                           v_valoare_ramasa      IN NUMBER,
                           v_dns_ramas           IN NUMBER,
                           v_data_operatie       IN DATE,
                           v_explicatia_operatie IN VARCHAR2,
                           v_id_util             IN NUMBER,
                           v_fiscala             in number,
                           v_datadoc             in date,
                           v_nrdoc               in number,
                           v_id_tip_iesire       in imob_tipuri_iesire.id%type default null)
    return number as
    -- am adaugat v_valoare_reziduala pentru compatibilitate
    v_id_operatie_mf    number := 0;
    v_valoare_reziduala number := 0;
  begin
    v_id_operatie_mf := add_operatie_mf(v_id_mf,
                                        v_id_tip_operatie,
                                        v_valoare,
                                        v_valoare_ramasa,
                                        v_dns_ramas,
                                        v_valoare_reziduala,
                                        v_data_operatie,
                                        v_explicatia_operatie,
                                        v_id_util,
                                        v_fiscala,
                                        v_datadoc,
                                        v_nrdoc,
                                        v_id_tip_iesire);

  end add_operatie_mf;*/

  function add_operatie_mf(v_id_mf               IN NUMBER,
                           v_id_tip_operatie     IN NUMBER,
                           v_valoare             IN NUMBER,
                           v_valoare_ramasa      IN NUMBER,
                           v_dns_ramas           IN NUMBER,
                           v_valoare_reziduala   IN NUMBER,
                           v_data_operatie       IN DATE,
                           v_explicatia_operatie IN VARCHAR2,
                           v_id_util             IN NUMBER,
                           v_fiscala             in number,
                           v_datadoc             in date,
                           v_nrdoc               in number,
                           v_id_tip_iesire       in imob_tipuri_iesire.id%type default null)
    return number as

    v_id_operatie_prec number := 0;
    v_id_operatie_mf   number := 0;
    v_view             varchar2(30) := 'imob_vultima_operatie';
    v_table            varchar2(30) := 'IMOB_OPERATII_MF';

  begin
    if v_fiscala = 1 then
      v_view  := 'imobf_vultima_operatie_fiscala';
      v_table := 'IMOBF_OPERATII_MF';
    else
      v_view  := 'imob_vultima_operatie';
      v_table := 'IMOB_OPERATII_MF';
    end if;
    BEGIN
      Execute immediate 'SELECT id_operatie_mf FROM ' || v_view ||
                        ' where id_mf = :1'
        into v_id_operatie_prec
        using v_id_mf;
    EXCEPTION
      WHEN OTHERS THEN
        v_id_operatie_prec := 0;
    END;

    EXECUTE IMMEDIATE 'INSERT /*+APPEND*/ INTO ' || v_table ||
                      '(ID_MF,ID_TIP_OPERATIE,VALOARE,valoare_ramasa,DNS_RAMAS,VALOARE_REZIDUALA,DATA_OPERATIE,EXPLICATIA,ID_UTIL,id_operatie_prec,datadoc,nrdoc, id_tip_iesire) ' ||
                      'VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13) RETURNING ID_OPERATIE_MF INTO :14'
      USING v_id_mf, v_id_tip_operatie, v_valoare, v_valoare_ramasa, v_dns_ramas, v_valoare_reziduala, v_data_operatie, v_explicatia_operatie, v_id_util, v_id_operatie_prec, v_datadoc, v_nrdoc, v_id_tip_iesire
      RETURNING INTO v_id_operatie_mf;

    return v_id_operatie_mf;

  end add_operatie_mf;
  ---------------------------------------------------------------------------------------------
  procedure add_in_lista_mf(v_schema               IN VARCHAR2,
                            v_id_mf                IN NUMBER,
                            v_dns                  IN NUMBER,
                            v_cod_mf               IN VARCHAR2,
                            v_cont                 IN VARCHAR2,
                            v_acont                IN VARCHAR2,
                            v_id_lucrare           IN NUMBER,
                            v_id_gestiune          IN NUMBER,
                            v_id_sectie            IN NUMBER,
                            v_id_responsabil       IN NUMBER,
                            v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                            v_nr_inventar          IN NUMBER,
                            v_id_util              IN NUMBER,
                            v_data_calcul          IN DATE,
                            v_durata               IN NUMBER,
                            v_amort_max_deductibil IN NUMBER) IS
    v_data_exp  date;
    v_an        number;
    v_luna      number;
    v_an_exp    number;
    v_luna_exp  number;
    v_data_prec date;
    v_an_prec   number;
    v_luna_prec number;

  begin

    v_data_exp  := add_months(v_data_calcul, v_durata);
    v_an        := extract(year from v_data_calcul);
    v_luna      := extract(month from v_data_calcul);
    v_an_exp    := extract(year from v_data_exp);
    v_luna_exp  := extract(month from v_data_exp);
    v_data_prec := add_months(v_data_calcul, -1);
    v_an_prec   := extract(year from v_data_prec);
    v_luna_prec := extract(month from v_data_prec);

    -- inactivez inregistrarile precedente pt un MF dc sunt facute in aceeasi luna
    /*    EXECUTE IMMEDIATE 'UPDATE ' || v_schema || '.IMOB_LISTA_MF ' ||
                    'SET inactiv = 1 ' ||
                    'WHERE ID_MF=:1 AND AN*12+LUNA =:2 AND STERS=0'
    USING v_id_mf, v_an * 12 + v_luna;*/

    -- setez data expirare pt precedenta activa
    UPDATE IMOB_LISTA_MF
       SET an_exp = v_an_prec, luna_exp = v_luna_prec
     WHERE ID_MF = v_id_mf
       and AN_EXP * 12 + LUNA_EXP > v_an * 12 + v_luna
       AND STERS = 0
       AND INACTIV = 0;

    -- inserez linie noua
    INSERT /*+APPEND*/
    INTO IMOB_LISTA_MF
      (ID_MF,
       DNS_LUNI,
       NR_INVENTAR,
       COD_MF,
       CONT,
       ACONT,
       ID_LUCRARE,
       ID_GESTIUNE,
       ID_SECTIE,
       ID_RESPONSABIL,
       ID_SUCURSALA,
       ID_UTIL,
       AN,
       LUNA,
       AN_EXP,
       LUNA_EXP,
       AMORT_MAX_DEDUCTIBIL)
    VALUES
      (v_id_mf,
       v_dns,
       v_nr_inventar,
       trim(v_cod_mf),
       trim(v_cont),
       trim(v_acont),
       v_id_lucrare,
       v_id_gestiune,
       v_id_sectie,
       v_id_responsabil,
       v_id_sucursala,
       v_id_util,
       v_an,
       v_luna,
       v_an_exp,
       v_luna_exp,
       v_amort_max_deductibil);

  end add_in_lista_mf;
  ------------------------------------------------------------------------------------------
  procedure adauga_la_transformare(v_id_mf_nou        IN NUMBER,
                                   v_id_operatie_nou  IN NUMBER,
                                   v_id_mf_curs       IN NUMBER,
                                   v_id_operatie_curs IN NUMBER,
                                   v_valoare          IN NUMBER) IS
  begin

    INSERT INTO IMOB_TRANSFORMARE_MF
      (ID_MF_NOU, ID_OPERATIE_NOU, ID_MF_VECHI, ID_OPERATIE_CURS, VALOARE)
    VALUES
      (v_id_mf_nou,
       v_id_operatie_nou,
       v_id_mf_curs,
       v_id_operatie_curs,
       v_valoare);

  end adauga_la_transformare;

  -------------------------------------------------------------------------------------------------------
  -- se apeleaza din sterge_operatie_mf cand sterg operatia de transformare imobilizare in curs in mijlocul fix
  -------------------------------------------------------------------------------------------------------
  procedure undo_in_imobilizare_in_curs(v_id_tip_imobilizare IN NUMBER,
                                        v_id_operatie        IN NUMBER) IS
    lnIdUtil         imob_operatii_mf.id_util%type;
    lnIdOperatieCurs imob_operatii_mf.id_operatie_mf%type;
  begin
    lnIdUtil := pack_contafin.GET_IDUTIL();

    IF v_id_tip_imobilizare <> 3 THEN
      -- sterg operatia de transformare in mijloc fix
      -- doar daca operatia apartine unei imobilizari corporale/necorporale
      -- nu si pentru o imobilizare in curs
      UPDATE IMOB_OPERATII_MF
         SET STERS = 1, DATAORA_STERS = SYSDATE, ID_UTIL_STERS = lnIdUtil
       WHERE id_operatie_mf in
             (SELECT ID_OPERATIE_CURS
                FROM IMOB_TRANSFORMARE_MF
               WHERE ID_OPERATIE_NOU = v_id_operatie);
      DELETE FROM IMOB_TRANSFORMARE_MF
       WHERE ID_OPERATIE_NOU = v_id_operatie;
    else
      DELETE FROM IMOB_TRANSFORMARE_MF
       WHERE ID_OPERATIE_CURS = v_id_operatie;
    END IF;

  end undo_in_imobilizare_in_curs;
  -------------------------------------------------------------------------------------------------------
  procedure recalculat_expirarea(v_schema           IN VARCHAR2,
                                 v_id_operatie_prec IN NUMBER,
                                 v_fiscala          IN NUMBER) is

    v_an_exp         number;
    v_luna_exp       number;
    v_dns_ramas      number;
    v_data           date;
    v_table_operatii varchar2(30);
    v_table_rate     varchar2(30);

  begin

    if v_fiscala = 0 then
      v_table_operatii := 'imob_operatii_mf';
      v_table_rate     := 'imob_calcul_rate';
    else
      v_table_operatii := 'imobf_operatii_mf';
      v_table_rate     := 'imobf_calcul_rate';
    end if;

    EXECUTE IMMEDIATE 'SELECT dns_ramas FROM ' || v_schema || '.' ||
                      v_table_operatii || ' WHERE ID_operatie_mf=:1'
      INTO v_dns_ramas
      USING v_id_operatie_prec;

    EXECUTE IMMEDIATE 'SELECT an,luna  FROM ' || v_schema || '.' ||
                      v_table_rate || ' WHERE ID_operatie_mf=:1'
      INTO v_an_exp, v_luna_exp
      USING v_id_operatie_prec;

    v_data     := to_date(to_char(v_an_exp) ||
                          lpad(to_char(v_luna_exp), 2, 0) || '01',
                          'yyyymmdd');
    v_data     := add_months(v_data, v_dns_ramas - 1);
    v_an_exp   := extract(year from v_data);
    v_luna_exp := extract(month from v_data);

    EXECUTE IMMEDIATE 'UPDATE ' || v_schema || '.' || v_table_rate ||
                      'SET an_exp=:1,luna_exp=:2 WHERE ID_operatie_mf =:3'
      USING v_an_exp, v_luna_exp, v_id_operatie_prec;

  end recalculat_expirarea;
  ------------------------------------------------------------------------------------------------------
  FUNCTION get_max_id(v_schema  IN VARCHAR2,
                      v_tabela  IN VARCHAR2,
                      v_nume_id IN VARCHAR2) return number is
    v_id_max NUMBER := 0;

  begin

    EXECUTE IMMEDIATE 'SELECT MAX(' || V_NUME_ID || ') AS ID_MAX FROM  ' ||
                      V_SCHEMA || '.' || V_TABELA
      INTO V_ID_MAX;
    return v_id_max;
  end get_max_id;
  -------------------------------------------------------------------------------------------------------
  procedure CALCUL_SITUATIE_LUNARA(v_datain       in date,
                                   v_datasf       in date,
                                   v_cond         in varchar2,
                                   v_fiscala      in number,
                                   v_id_sucursala in number,
                                   crsMF          out PACK_TYPES.tip_cursor) is
  begin

    pack_imob.CALCUL_SITUATIE_LUNARA2(v_datain,
                                      v_datasf,
                                      v_cond,
                                      v_fiscala,
                                      v_id_sucursala,
                                      0,
                                      crsMF);
  end;
  ------------------------------------------------------------------------------------------------------
  /*  PROCEDURE VIZUALIZARE_LISTA(V_DATAIN       IN DATE,
                                    V_DATASF       IN DATE,
                                    V_COND         IN VARCHAR2,
                                    V_FISCALA      IN NUMBER,
                                    V_ID_SUCURSALA IN NUMBER,
                                    V_TIP_SELECTIE IN NUMBER DEFAULT 0,
                                    CRSMF          OUT PACK_TYPES.TIP_CURSOR) IS
  CRSMF PACK_TYPES.TIP_CURSOR;
  BEGIN
  PACK_IMOB.CALCUL_SITUATIE_LUNARA2(V_DATAIN => :V_DATAIN,
                                    V_DATASF => :V_DATASF,
                                    V_COND => :V_COND,
                                    V_FISCALA => :V_FISCALA,
                                    V_ID_SUCURSALA => :V_ID_SUCURSALA,
                                    V_TIP_SELECTIE => :V_TIP_SELECTIE,
                                    CRSMF => :CRSMF);

  END VIZUALIZARE_LISTA;*/

  procedure CALCUL_SITUATIE_LUNARA2(v_datain       in date,
                                    v_datasf       in date,
                                    v_cond         in varchar2,
                                    v_fiscala      in number,
                                    v_id_sucursala in number,
                                    v_tip_selectie in number,
                                    crsMF          out PACK_TYPES.tip_cursor) is

    -- v_tip_selectie: 0 (default), 1 = se apeleaza selectul din pack_mg_evolutie cu mai putine coloane

    lcSql varchar2(32000);
    /*    uw_selection          uw_sel_tab := uw_sel_tab();*/
    lnMf                  number(10) := 0;
    lnIdMf                number(10) := 0;
    lnIdLista             number(10) := 0;
    lnIdOperatie          number(10) := 0;
    lnIdOperatieAchizitie number(10) := 0;
    lnIdOperatiePrec      number(10) := 0;
    lnIdCalcul            number(10) := 0;
    lnIdListaRezerva      number(10) := 0;
    lnIdCalculRezerva     number(10) := 0;
    ignore                number(4);
    lnDataTest            number(10) := 0;
    llInserez             number(1) := 0;
    v_table_operatii      varchar2(40) := 'IMOB_OPERATII_MF';
    v_table_rata          varchar2(40) := 'IMOB_CALCUL_RATE';
    v_view_operatii       varchar2(40) := 'IMOB_VOPERATII_MF';
    v_view_rata           varchar2(40) := 'IMOB_VCALCUL_RATE';
    lcWhere               varchar2(1000);

    i number(20) := 0;
  begin
    DELETE FROM TUW_SEL_TAB;

    if v_id_sucursala is not null then
      lcWhere := case
                   when v_cond is not null then
                    v_cond || ' and l.id_sucursala = ' || v_id_sucursala
                   else
                    ' l.id_sucursala = ' || v_id_sucursala
                 end;
    else
      lcWhere := v_cond;
    end if;
    if lcWhere is not null then
      lcWhere := ' where ' || lcWhere;
    end if;

    if v_fiscala = 1 then
      v_table_operatii := 'IMOBF_OPERATII_MF';
      v_table_rata     := 'IMOBF_CALCUL_RATE';
      v_view_operatii  := 'IMOBF_VOPERATII_MF';
      v_view_rata      := 'IMOBF_VCALCUL_RATE';
    end if;
    lcSql := 'select nvl(n.id_mf, 0) as id_mf,
                     nvl(l.id_lista_mf, 0) as id_lista_mf,
                     nvl(o.id_operatie_mf, 0) as id_operatie_mf,
                     nvl(o.id_operatie_achizitie, 0) as id_operatie_achizitie,
                     nvl(o.id_operatie_prec, 0) as id_operatie_prec,
                     nvl(c.id_calcul_rate, 0) as id_calcul_rate,
                     nvl(s.id_lista_rezerva, 0) as id_lista_rezerva,
                     nvl(x.id_calcul_rate, 0) as id_calcul_rezerva
          from (select o1.id_operatie_mf, o1.id_mf, o1.id_operatie_prec, o2.id_operatie_achizitie from (select id_operatie_mf, id_mf, id_operatie_prec from ' ||
             v_table_operatii || ' where sters = 0) o1 join (select distinct first_value(id_operatie_mf) over(partition by id_mf order by data_operatie desc) as id_operatie_mf,
                      first_value(id_operatie_mf) over(partition by id_mf order by data_operatie asc) as id_operatie_achizitie
                 from ' || v_table_operatii || '
                where extract(month from data_operatie) +
                      extract(year from data_operatie) * 12 <= :1
                  and sters = 0
                  and id_operatie_mf in (select distinct id_operatie_mf
                                           from ' ||
             v_table_rata || '
                                          where sters = 0)) o2 on o1.id_operatie_mf =
                                                                  o2.id_operatie_mf) o
          join imob_nom_mf n on o.id_mf = n.id_mf and n.sters = 0
          left join (select distinct max(id_lista_mf) over(partition by id_mf) as id_lista_mf,id_mf
                                                                  from imob_lista_mf
                                                                 where :2 between
                                                                       (an * 12 + luna) and
                                                                       (an_exp * 12 +
                                                                       luna_exp)
                                                                   and sters = 0) l on o.id_mf = l.id_mf
          left join (select id_mf, id_lista_mf as id_lista_rezerva from imob_lista_mf where id_lista_mf in (select first_value(id_lista_mf) over (partition by id_mf order by (an*12 + luna) desc) as id_lista_rezerva from imob_lista_mf
                                                                 where an*12 + luna <= :3 and
                                                                 (an_exp*12 + luna_exp - an*12 -luna)>=0 and sters=0)) s on s.id_mf = o.id_mf
          left join (select distinct max(id_calcul_rate) over(partition by id_mf) as id_calcul_rate,id_mf
                                                                  from ' ||
             v_table_rata || '
                                                                 where :4 between
                                                                       (an * 12 + luna) and
                                                                       (an_exp * 12 +
                                                                       luna_exp)
                                                                   and sters = 0) c on o.id_mf = c.id_mf
          left join (select distinct min(id_calcul_rate) over(partition by id_operatie_mf) as id_calcul_rate, id_operatie_mf from ' ||
             v_table_rata ||
             ' where sters = 0) x on x.id_operatie_mf = o.id_operatie_mf';

    --pinfo(lcSql, 'IMOBILIZARI 1');

    FOR ITEM IN (SELECT data_test
                   FROM ani_luni
                  where data_test between v_datain and v_datasf
                  order by data_test) LOOP

      lnDataTest := extract(month from item.data_test) +
                    extract(year from item.data_test) * 12;

      /*       execute immediate 'SELECT uw_sel_row(data_test, id_mf, id_lista_mf, id_operatie_mf, id_calcul_rate)
            BULK COLLECT INTO uw_selection
            FROM (' || lcSql || ')'
      using item.data_test,lnDataTest, lnDataTest;*/

      lnMF := dbms_sql.open_cursor;
      DBMS_SQL.PARSE(lnMf, lcSql, DBMS_SQL.native);

      DBMS_SQL.DEFINE_COLUMN(lnMf, 1, lnIdMf);
      DBMS_SQL.DEFINE_COLUMN(lnMf, 2, lnIdLista);
      DBMS_SQL.DEFINE_COLUMN(lnMf, 3, lnIdOperatie);
      DBMS_SQL.DEFINE_COLUMN(lnMf, 4, lnIdOperatieAchizitie);
      DBMS_SQL.DEFINE_COLUMN(lnMf, 5, lnIdOperatiePrec);
      DBMS_SQL.DEFINE_COLUMN(lnMf, 6, lnIdCalcul);
      DBMS_SQL.DEFINE_COLUMN(lnMf, 7, lnIdListaRezerva);
      DBMS_SQL.DEFINE_COLUMN(lnMf, 8, lnIdCalculRezerva);

      dbms_sql.bind_variable(lnMf, ':1', lnDataTest);
      dbms_sql.bind_variable(lnMf, ':2', lnDataTest);
      dbms_sql.bind_variable(lnMf, ':3', lnDataTest);
      dbms_sql.bind_variable(lnMf, ':4', lnDataTest);

      ignore := DBMS_SQL.EXECUTE(lnMf);
      LOOP
        IF DBMS_SQL.FETCH_ROWS(lnMf) > 0 THEN
          -- get column values of the row
          DBMS_SQL.COLUMN_VALUE(lnMf, 1, lnIdMf);
          DBMS_SQL.COLUMN_VALUE(lnMf, 2, lnIdLista);
          DBMS_SQL.COLUMN_VALUE(lnMf, 3, lnIdOperatie);
          DBMS_SQL.COLUMN_VALUE(lnMf, 4, lnIdOperatieAchizitie);
          DBMS_SQL.COLUMN_VALUE(lnMf, 5, lnIdOperatiePrec);
          DBMS_SQL.COLUMN_VALUE(lnMf, 6, lnIdCalcul);
          DBMS_SQL.COLUMN_VALUE(lnMf, 7, lnIdListaRezerva);
          DBMS_SQL.COLUMN_VALUE(lnMf, 8, lnIdCalculRezerva);

          if lnIdLista = 0 then
            lnIdLista := lnIdListaRezerva;
          end if;

          if lnIdCalcul = 0 then
            --select nvl(id_calcul_rate,0) into lnIdCalcul from imob_calcul_rate where id_operatie_mf = lnIdOperatie ;
            lnIdCalcul := lnIdCalculRezerva;
          end if;

          if lnIdCalcul <> 0 then
            llInserez := 1;
          else
            llInserez := 0;
          end if;

          if llInserez = 1 then
            I := I + 1;
            /*            uw_selection.extend;
            uw_selection(i) := uw_sel_row(item.data_test,
                                          lnIdMf,
                                          lnIdLista,
                                          lnIdOperatie,
                                          lnIdOperatieAchizitie,
                                          lnIdCalcul);*/
            insert into tuw_sel_tab
              (data_test,
               id_mf,
               id_lista_mf,
               id_operatie_mf,
               id_operatie_achizitie,
               id_operatie_prec,
               id_calcul_rate)
            values
              (item.data_test,
               lnIdMf,
               lnIdLista,
               lnIdOperatie,
               lnIdOperatieAchizitie,
               lnIdOperatiePrec,
               lnIdCalcul);
          end if;
        else
          exit;
        end if; -- DBMS_SQL.FETCH_ROWS(lnMf) > 0
      end loop;

      dbms_sql.close_cursor(lnMf);
    end loop;
    if v_tip_selectie = 0 then
      -- 04.04.2007
      -- rata doar daca o.id_tip_operatie <> 4 (ultima operatie nu este iesire din gestiune)
      lcSql := 'select a.data_test,a.id_mf,a.id_lista_mf,a.id_operatie_mf,a.id_calcul_rate
    ,n.denumire, n.data_achizitie, n.data_pif,n.explicatia,n.nract,n.nr_pif,n.procent,n.coeficient,n.id_tip_imobilizare,n.tip_imobilizare,n.id_tip_amortizare,n.tip_amortizare
    ,l.dns_luni,l.cont,l.acont,l.cod_mf,l.id_gestiune,l.gestiune,l.id_lucrare,l.nrord,l.id_responsabil,l.responsabil,l.id_sectie,l.sectie,l.id_sucursala,l.sucursala,l.nr_inventar,lpad(l.luna,2,''0'')|| l.an as lper_in,lpad(l.luna_exp,2,''0'')|| l.an_exp as lper_fin, l.amort_max_deductibil
    ,o.data_operatie,o.id_tip_operatie,o.denumire as ultima_operatie,o.explicatia as cauza,r.uzura_prec,o.dns_ramas
    ,(case o.id_tip_operatie when 4 then 1 else 0 end) as iesit_din_gest
    ,nvl(r.cota_an,0) as cota_an,nvl(r.cota_luna,0) as cota_luna,r.data_ultima_rata as data_expirare,nvl(r.ultima_rata,0) as ultima_rata
    ,o.valoare, o.valoare_ramasa, o.valoare_reziduala
    ,(case when a.data_test>= r.data_ultima_rata then (case when a.data_test = r.data_ultima_rata and o.id_tip_operatie <> 4 then r.ultima_rata else 0 end) else (case when a.data_test>=to_date(r.luna || ''/'' || r.an,''MM/YYYY'') and o.id_tip_operatie <> 4 then r.rata else 0 end) end) as rata ' ||
              --',o.valoare_ramasa - (case when o.id_operatie_mf = r.id_operatie_mf then (r.rata*GREATEST(0,months_between(trunc(least(a.data_test,r.data_ultima_rata),''MM''),to_date(r.luna || ''/'' || r.an,''MM/YYYY''))) + (case when a.data_test >= r.data_ultima_rata then ultima_rata else (case when a.data_test<to_date(r.luna || ''/'' || r.an,''MM/YYYY'') THEN 0 ELSE rata END) end)) else 0 end) as valoare_ramasa_amort ' ||
              --',o.valoare-o.valoare_ramasa + (case when trunc(o.data_operatie,''MM'') <=  to_date(r.luna || ''/'' || r.an,''MM/YYYY'') then  r.rata*GREATEST(0,months_between(trunc(least(a.data_test,r.data_ultima_rata),''MM''),to_date(r.luna || ''/'' || r.an,''MM/YYYY''))) + (case when a.data_test > r.data_ultima_rata then ultima_rata else 0 end) else 0 end) as amort_prec ' ||
               ',r.amortizare_prec + r.rata*GREATEST(0,months_between(trunc(least(a.data_test,r.data_ultima_rata),''MM''),to_date(r.luna || ''/'' || r.an,''MM/YYYY''))) + (case when a.data_test > r.data_ultima_rata then r.ultima_rata else 0 end) as amort_prec ' ||
              --',r.rata*GREATEST(0,months_between(trunc(least(a.data_test,r.data_ultima_rata),''MM''),to_date(r.luna || ''/'' || r.an,''MM/YYYY''))) + (case when a.data_test > r.data_ultima_rata then ultima_rata else 0 end) as amort_prec_rata
               ',r.uzura_prec + greatest(0,months_between(trunc(least(a.data_test,r.data_ultima_rata),''MM''),to_date(r.luna || ''/'' || r.an,''MM/YYYY'')) + 1) as uzura
                                                    ,greatest(0,months_between(trunc(least(a.data_test,r.data_ultima_rata),''MM''),to_date(r.luna || ''/'' || r.an,''MM/YYYY'')) + 1) as uzura_rata
    ,lpad(r.luna,2,''0'')|| r.an as rper_in,lpad(r.luna_exp,2,''0'')|| r.an_exp as rper_fin
    ,o.valoare_rol,o.valoare_ramasa_rol,r.rata_rol,r.ultima_rata_rol, o2.valoare as valoare_achizitie, o3.valoare as valoare_prec, o.id_tip_iesire, i.tip as tip_iesire,
    greatest(0, months_between(r.data_ultima_rata,a.data_test)) as durata_ramasa ';
    else
      -- pentru apelul din pack_mg_evolutie
      lcSql := 'select l.cont, o.data_operatie, o.id_tip_iesire,o.valoare ';
    end if;

    --'from TABLE(cast(:1 as uw_sel_tab)) a ' ||
    lcSql := lcSql || 'from tuw_sel_tab a ' || 'left join imob_vnom_mf n on a.id_mf = n.id_mf
    left join imob_vlista_mf l on a.id_lista_mf = l.id_lista_mf
    left join ' || v_view_operatii || ' o on a.id_operatie_mf = o.id_operatie_mf
    left join ' || v_view_operatii || ' o2 on a.id_operatie_achizitie = o2.id_operatie_mf
    left join ' || v_view_operatii || ' o3 on a.id_operatie_prec = o3.id_operatie_mf
    left join ' || v_view_rata ||
             ' r on a.id_calcul_rate = r.id_calcul_rate
    left join imob_tipuri_iesire i on o.id_tip_iesire = i.id ' ||
             lcWhere;

    --pinfo(lcSql, 'IMOBILIZARI 2');

    open crsMF for lcSql;
  /*    open crsMF for lcSql
    using uw_selection;*/
    DELETE FROM TUW_SEL_TAB;

  end;

  -----------------------------------------------------------------------------------
  function valori_calcul(v_schema       in varchar2,
                         v_id_sucursala in IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                         v_datain       in date,
                         v_datasf       in date,
                         v_cond         in varchar2,
                         v_fiscala      in number)
    return PACK_TYPES.tip_cursor is
    crsMF PACK_TYPES.tip_cursor;
  begin

    pack_imob.CALCUL_SITUATIE_LUNARA2(v_datain,
                                      v_datasf,
                                      v_cond,
                                      v_fiscala,
                                      v_id_sucursala,
                                      0,
                                      crsMF);

    return crsMF;
  end valori_calcul;

  -----------------------------------------------------------------------------------
  -- folosesc imob_vsituatie_lunara in loc de procedura calcul_situatie_lunara2
  -----------------------------------------------------------------------------------
  function valori_calcul_view(v_id_mf   in number,
                              v_data    in date,
                              v_fiscala in number)
    return PACK_TYPES.tip_cursor is
    crsMF PACK_TYPES.tip_cursor;
  begin

    pack_imob.setlunacurenta(v_data);
    if v_fiscala = 0 then
      open crsMF for
        select * from imob_vsituatie_lunara where id_mf = v_id_mf;
    else
      open crsMF for
        select * from imobf_vsituatie_lunara where id_mf = v_id_mf;
    end if;

    return crsMF;
  end valori_calcul_view;

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

  procedure sterge_operatie_mf(v_id_operatie      IN NUMBER,
                               v_id_operatie_prec IN NUMBER,
                               v_fiscala          IN NUMBER) IS

    lnID_Util            act_temp.id_util%Type := 0;
    LD_DATAORA           DATE;
    v_ultima_operatie    number;
    v_dataop             date;
    v_table_operatii     varchar2(30);
    v_table_rate         varchar2(30);
    v_id_tip_operatie    imob_operatii_mf.id_tip_operatie%type;
    v_id_tip_imobilizare imob_nom_mf.id_tip_imobilizare%type;
  begin

    if v_fiscala = 0 then
      v_table_operatii := 'imob_operatii_mf';
      v_table_rate     := 'imob_calcul_rate';
    else
      v_table_operatii := 'imobf_operatii_mf';
      v_table_rate     := 'imobf_calcul_rate';
    end if;
    begin
      EXECUTE IMMEDIATE 'SELECT max(nvl(id_operatie_mf,0)), max(data_operatie) from ' ||
                        v_table_operatii ||
                        ' where id_operatie_prec = :1 and sters = 0 group by id_operatie_prec'
        into v_ultima_operatie, v_dataop
        using v_id_operatie;
    EXCEPTION
      WHEN OTHERS THEN
        v_ultima_operatie := 0;
    end;

    if v_ultima_operatie = 0 or v_ultima_operatie is null then
      EXECUTE IMMEDIATE 'SELECT max(id_tip_operatie), max(m.id_tip_imobilizare) from ' ||
                        v_table_operatii ||
                        ' o join imob_nom_mf m on o.id_mf = m.id_mf where id_operatie_mf = :1'
        into v_id_tip_operatie, v_id_tip_imobilizare
        using v_id_operatie;

      LNID_UTIL  := pack_contafin.GET_IDUTIL();
      LD_DATAORA := pack_contafin.GET_DATAORA();

      EXECUTE IMMEDIATE 'UPDATE ' || v_table_operatii ||
                        ' SET STERS=1, id_util_sters = :1, dataora_sters = :2 WHERE ID_operatie_mf =: 3'
        USING lnId_Util, ld_dataora, v_id_operatie;

      EXECUTE IMMEDIATE 'UPDATE ' || v_table_rate ||
                        ' SET STERS=1 WHERE ID_operatie_mf =: 1'
        USING v_id_operatie;

      if v_id_operatie_prec <> 0 then
        EXECUTE IMMEDIATE 'UPDATE ' || v_table_rate ||
                          ' SET an_exp = extract(year from data_ultima_rata), luna_exp = extract(month from data_ultima_rata) WHERE ID_operatie_mf =: 1'
          using v_id_operatie_prec;
      end if;

      -- anulare transformare imobilizare in curs in mijloc fix
      -- doar daca sterg operatia din imobilizari corporale/necorporale
      -- nu apelez daca sterg operatia din imobilizari in curs
      if v_id_tip_operatie = 3 then
        pack_imob.undo_in_imobilizare_in_curs(v_id_tip_imobilizare,
                                              v_id_operatie);
      end if;

    else
      RAISE_APPLICATION_ERROR(-20000,
                              '*Nu se poate sterge aceasta operatie. Exista o operatie pe acest MF in data de ' ||
                              v_dataop || '.*');
    END IF;

  end sterge_operatie_mf;
  -----------------------------------------------------------------------------------------------------------------------------
  procedure modifica_lista_mf(v_id_mf                IN IMOB_LISTA_MF.ID_MF%TYPE,
                              v_id_lista_mf          IN IMOB_LISTA_MF.ID_LISTA_MF%TYPE,
                              v_dns_luni             IN IMOB_LISTA_MF.DNS_LUNI%TYPE,
                              v_Cod_mf               IN IMOB_LISTA_MF.COD_MF%TYPE,
                              v_cont                 IN IMOB_LISTA_MF.CONT%TYPE,
                              v_acont                IN IMOB_LISTA_MF.ACONT%TYPE,
                              v_nr_inventar          IN IMOB_LISTA_MF.NR_INVENTAR%TYPE,
                              v_id_gestiune          IN IMOB_LISTA_MF.ID_GESTIUNE%TYPE,
                              v_id_responsabil       IN IMOB_LISTA_MF.ID_RESPONSABIL%TYPE,
                              v_id_sectie            IN IMOB_LISTA_MF.ID_SECTIE%TYPE,
                              v_id_lucrare           IN IMOB_LISTA_MF.ID_LUCRARE%TYPE,
                              v_id_sucursala         IN IMOB_LISTA_MF.ID_SUCURSALA%TYPE,
                              v_id_util              IN IMOB_LISTA_MF.ID_UTIL%TYPE,
                              v_an                   IN IMOB_LISTA_MF.AN%TYPE,
                              v_luna                 IN IMOB_LISTA_MF.LUNA%TYPE,
                              v_an_exp               IN IMOB_LISTA_MF.AN%TYPE,
                              v_luna_exp             IN IMOB_LISTA_MF.LUNA%TYPE,
                              v_amort_max_deductibil in IMOB_LISTA_MF.AMORT_MAX_DEDUCTIBIL%TYPE) is

    lnAnMin      IMOB_LISTA_MF.AN%TYPE;
    lnLunaMin    IMOB_LISTA_MF.LUNA%TYPE;
    lnIdListaMax IMOB_LISTA_MF.ID_LISTA_MF%TYPE;
    lnFoundMin   NUMBER(1) := 1;
    lnFoundMax   NUMBER(1) := 1;

    lnAn      IMOB_LISTA_MF.AN%TYPE := v_an;
    lnLuna    IMOB_LISTA_MF.LUNA%TYPE := v_luna;
    lnAnExp   IMOB_LISTA_MF.AN%TYPE := v_an_exp;
    lnLunaExp IMOB_LISTA_MF.LUNA%TYPE := v_luna_exp;

    ldDataInc       DATE;
    ldDataExp       DATE;
    ldDataExpPerAnt DATE;
    ldDataIncPerUrm DATE;
    v_rowcount      number;

  begin
    -- pinfo('id_mf ' || v_id_mf || ' an ' || v_an || ' luna ' || v_luna ||          ' id_lucrare' || v_id_lucrare,          'modifica_lista_mf');

    -- daca mai exista luna in imob_lista_mf, modific datele pentru luna operatiei
    MERGE INTO IMOB_LISTA_MF
    USING DUAL
    ON (AN = v_an and LUNA = v_luna and ID_MF = v_id_mf)
    WHEN MATCHED THEN
      UPDATE
         SET NR_INVENTAR          = v_nr_inventar,
             id_lucrare           = v_id_lucrare,
             id_gestiune          = v_id_gestiune,
             id_sectie            = v_id_sectie,
             id_responsabil       = v_id_responsabil,
             id_sucursala         = v_id_sucursala,
             dns_luni             = v_dns_luni,
             cod_mf               = v_Cod_mf,
             cont                 = v_cont,
             acont                = v_acont,
             id_util              = v_id_util,
             amort_max_deductibil = v_amort_max_deductibil;

    -- DACA EXISTA LUNA OPERATIEI TREBUIE DOAR SA MODIFIC DATELE
    -- pinfo('SQL%ROWCOUNT ' || SQL%ROWCOUNT, 'modifica_lista_mf');
    v_rowcount := SQL%ROWCOUNT;
    IF v_rowcount > 0 THEN
      GOTO END_PROC;
    END IF;

    -- ALTFEL, TREBUIE SA MODIFIC DATA DE EXPIRARE PENTRU OPERATIA PRECEDENTA SI, EVENTUAL, DATA DE EXPIRARARE A OPERATIEI

    -- OPERATIA PRECEDENTA
    -- selectez luna maxima mai mica decat luna operatiei
    BEGIN
      select ID_LISTA_MF
        INTO lnIdListaMax
        from (SELECT ID_LISTA_MF, AN, LUNA, MAXLUNA
                FROM (SELECT ID_LISTA_MF,
                             AN,
                             LUNA,
                             (SELECT MAX(AN * 12 + LUNA)
                                FROM IMOB_LISTA_MF
                               WHERE ID_MF = v_id_mf
                                 AND AN * 12 + LUNA < v_an * 12 + v_luna) AS MAXLUNA
                        FROM IMOB_LISTA_MF
                       WHERE ID_MF = v_id_mf)
               where an * 12 + luna = MAXLUNA);
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        lnFoundMax := 0;
    END;

    -- daca am gasit operatia precedenta
    -- modific data de expirare a operatiei precedente cu luna operatiei - 1
    IF lnFoundMax = 1 THEN
      ldDataInc       := TO_DATE(LPAD(TO_CHAR(v_an), 4, '0') ||
                                 LPAD(TO_CHAR(v_luna), 2, '0') || '01',
                                 'YYYYMMDD');
      ldDataExpPerAnt := ADD_MONTHS(ldDataInc, -1);
      UPDATE IMOB_LISTA_MF
         SET AN_EXP   = EXTRACT(YEAR FROM ldDataExpPerAnt),
             LUNA_EXP = EXTRACT(MONTH FROM ldDataExpPerAnt)
       WHERE ID_LISTA_MF = lnIdListaMax;
    END IF;

    -- OPERATIA URMATOARE
    -- selectez luna minima mai mare decat luna operatiei
    BEGIN
      select an, luna
        INTO lnAnMin, lnLunaMin
        from (SELECT ID_LISTA_MF, AN, LUNA, MINLUNA
                FROM (SELECT ID_LISTA_MF,
                             AN,
                             LUNA,
                             (SELECT MIN(AN * 12 + LUNA)
                                FROM IMOB_LISTA_MF
                               WHERE ID_MF = v_id_mf
                                 AND AN * 12 + LUNA > v_an * 12 + v_luna) AS MINLUNA
                        FROM IMOB_LISTA_MF
                       WHERE ID_MF = v_id_mf)
               WHERE an * 12 + luna = MINLUNA);
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        lnFoundMin := 0;
    END;

    -- daca am gasit operatia urmatoare, data de expirare a operatiei devine luna de inceput a operatie urmatoare - 1
    -- daca nu am gasit - luna expirarii = luna de inceput
    IF lnFoundMin = 1 THEN
      ldDataIncPerUrm := TO_DATE(LPAD(TO_CHAR(lnAnMin), 4, '0') ||
                                 LPAD(TO_CHAR(lnLunaMin), 2, '0') || '01',
                                 'YYYYMMDD');
      ldDataExp       := ADD_MONTHS(ldDataIncPerUrm, -1);
      lnAnExp         := EXTRACT(YEAR FROM ldDataExp);
      lnLunaExp       := EXTRACT(MONTH FROM ldDataExp);
    ELSE
      lnAnExp   := lnAn;
      lnLunaExp := lnLuna;
    END IF;

    INSERT INTO IMOB_LISTA_MF
      (ID_MF,
       DNS_LUNI,
       NR_INVENTAR,
       COD_MF,
       CONT,
       ACONT,
       ID_LUCRARE,
       ID_GESTIUNE,
       ID_SECTIE,
       ID_RESPONSABIL,
       ID_SUCURSALA,
       ID_UTIL,
       AN,
       LUNA,
       AN_EXP,
       LUNA_EXP,
       AMORT_MAX_DEDUCTIBIL)
    VALUES
      (v_id_mf,
       v_dns_luni,
       v_nr_inventar,
       v_cod_mf,
       v_cont,
       v_acont,
       v_id_lucrare,
       v_id_gestiune,
       v_id_sectie,
       v_id_responsabil,
       v_id_sucursala,
       v_id_util,
       lnAn,
       lnLuna,
       lnAnExp,
       lnLunaExp,
       v_amort_max_deductibil);

    <<END_PROC>>
    NULL;
  end modifica_lista_mf;
  -----------------------------------------------------------------------------------
  procedure adauga_buncapital(v_id_mf           in imob_bunuricapital.id_mf%type,
                              v_nrdoc           in imob_bunuricapital.nrdoc%type,
                              v_datadoc         in imob_bunuricapital.datadoc%type,
                              v_bazatva         in imob_bunuricapital.bazatva%type,
                              v_tva             in imob_bunuricapital.tva%type,
                              v_id_cota_tva     in imob_bunuricapital.id_cota_tva%type,
                              v_ajustare        in imob_bunuricapital.ajustare%type,
                              v_id_tip_operatie in imob_bunuricapital.id_tip_operatie%type,
                              v_tvaDatorat      in imob_bunuricapital.tva_datorat%type,
                              v_prorataTVA      in imob_bunuricapital.proratatva%type) is
  begin
    insert into imob_bunuricapital
      (id_mf,
       nrdoc,
       datadoc,
       bazatva,
       tva,
       ajustare,
       id_cota_tva,
       id_util,
       id_tip_operatie,
       tva_datorat,
       prorataTVA)
    values
      (v_id_mf,
       v_nrdoc,
       v_datadoc,
       v_bazatva,
       v_tva,
       v_ajustare,
       v_id_cota_tva,
       pack_sesiune.get_id_util(),
       v_id_tip_operatie,
       v_tvadatorat,
       v_prorataTVA);

  end adauga_buncapital;
  -----------------------------------------------------------------------------------
  procedure modifica_buncapital(v_id_mf           in imob_bunuricapital.id_mf%type,
                                v_nrdoc           in imob_bunuricapital.nrdoc%type,
                                v_datadoc         in imob_bunuricapital.datadoc%type,
                                v_bazatva         in imob_bunuricapital.bazatva%type,
                                v_tva             in imob_bunuricapital.tva%type,
                                v_id_cota_tva     in imob_bunuricapital.id_cota_tva%type,
                                v_ajustare        in imob_bunuricapital.ajustare%type,
                                v_id_tip_operatie in imob_bunuricapital.id_tip_operatie%type,
                                v_tvaDatorat      in imob_bunuricapital.tva_datorat%type,
                                v_id_regbc        in imob_bunuricapital.id_regbc%type,
                                v_prorataTva      in imob_bunuricapital.proratatva%type) is
  begin
    update imob_bunuricapital
       set id_mf           = v_id_mf,
           nrdoc           = v_nrdoc,
           datadoc         = v_datadoc,
           bazatva         = v_bazatva,
           tva             = v_tva,
           id_cota_tva     = v_id_cota_tva,
           ajustare        = v_ajustare,
           id_tip_operatie = v_id_tip_operatie,
           tva_Datorat     = v_tvaDatorat,
           proratatva      = v_prorataTva
     where id_regbc = v_id_regBc;

  end modifica_buncapital;
  -------------------------------------------------------------------------------------

  procedure sterge_buncapital(v_id_regbc in imob_bunuricapital.id_regbc%type) is
  begin
    update imob_bunuricapital set sters = 1 where id_regbc = v_id_regbc;
  end sterge_buncapital;

  -------------------------------------------------------------------------------------------
  /*
  Caut daca mai exista numarul de inventar
  */

  FUNCTION verifica_nr_inventar(v_nr_inventar        IN IMOB_LISTA_MF.NR_INVENTAR%TYPE,
                                v_data               IN DATE,
                                v_id_tip_imobilizare IN NUMBER DEFAULT 1)
    RETURN NUMBER IS
    lnNrDuplicate NUMBER;
  BEGIN
    pack_imob.setlunacurenta(v_data);

    SELECT COUNT(*)
      INTO lnNrDuplicate
      FROM IMOB_VSITUATIE_LUNARA t
     WHERE t.id_tip_imobilizare = v_id_tip_imobilizare
       AND t.nr_inventar = v_nr_inventar;

    RETURN lnNrDuplicate;
  END verifica_nr_inventar;
  -------------------------------------------------------------------------------------
/* begin
 setlunacurenta(to_date(pack_sesiune.getan() || '/' ||
                         pack_sesiune.getluna(),
                         'YYYY/MM'));*/

END pack_imob;
/
