-----------------------------------------------------------------
-- Package & package body PACK_DEVIZE pentru ROAAUTO v 1.4.151 --
-- Created by marius.atanasiu on 2/2/2006, 9:51:33 AM          --
-----------------------------------------------------------------

spool packages.log

prompt
prompt Creating package PACK_DEVIZE
prompt ============================
prompt
create or replace package pack_devize is

  -- Author  : LIANA.MACINIC
  -- Created : 1/7/2005 11:40:29 AM
  -- Purpose :
  procedure dev_sterge_comanda(tcSchema    in varchar2,
                               tnIdUtil    in number,
                               tnIdOrdl    in number,
                               tnIdLucrare in number);

  procedure dev_adauga_oper_fact(tcSchema    in varchar2,
                                 tnIdOrdl    in number,
                                 tnIdSectie  in number,
                                 tnIdNorme   in number,
                                 tnTotalFTva in number,
                                 tnTimpN     in number,
                                 tnOre       in number,
                                 tnIdUtil    in number,
                                 tnIdMecanic in number);

  procedure dev_adauga_lucrare(v_gcs    in varchar2,
                               tnan     in number,
                               tnluna   in number,
                               tnIdUtil in number,
                               pcNr     in varchar2,
                               pnIdInsp in number,
                               pnIdAsig in number,
                               pnIdMC   in number,
                               pnKmInt  in number,
                               pdTermen in date,
                               pnTipCom in number,
                               pnIdOrdl out number);

  procedure dev_valideaza_comanda(v_gcs     in varchar2,
                                  tnid_ordl in number,
                                  tnid_util in number);

  procedure dev_invalideaza_comanda(v_gcs      in varchar2,
                                    tnid_ordl  in number,
                                    tnid_utils in number);

  procedure dev_distribuie_timp_n(v_gcs in varchar2, v_filtru in varchar2);

  procedure dev_adauga_operatie(v_gcs           in varchar2,
                                tnid_sectie     in number,
                                tnid_ordl       in number,
                                tnpret          in number,
                                tnid_util       in number,
                                tntimpn         in number,
                                tnid_norme      in number,
                                tcsir_idmecanic in varchar2);

  procedure dev_actualizeaza_operatie(v_gcs          in varchar2,
                                      v_camp         in varchar2,
                                      v_valoare_noua in number,
                                      v_id_oper      in number,
                                      v_luna         in number,
                                      v_an           in number);

  procedure dev_valideaza_operatie(v_gcs     in varchar2,
                                   tnid_oper in number,
                                   tnid_util in number);

  procedure dev_valideaza_operatii(v_gcs     in varchar2,
                                   tnid_ordl in number,
                                   tnid_util in number);

  procedure dev_completeaza_rul(v_gcs in varchar2, tnid_lucrare in number);

  procedure valideaza_operatie(V_SCHEMA  IN VARCHAR2,
                               V_VALIDAT IN NUMBER,
                               V_ID_OPER IN NUMBER,
                               V_ID_UTIL IN NUMBER);

  procedure modifica_sectie(V_SCHEMA    IN VARCHAR2,
                            V_ID_SECTIE IN NUMBER,
                            V_ID_OPER   IN NUMBER,
                            V_ID_UTIL   IN NUMBER);

  procedure modifica_tip_comanda(V_SCHEMA  IN VARCHAR2,
                                 V_ID_TIP  IN NUMBER,
                                 V_ID_ORDL IN NUMBER,
                                 V_ID_UTIL IN NUMBER);

  procedure sterge_operatie(V_SCHEMA  IN VARCHAR2,
                            V_ID_OPER IN NUMBER,
                            V_ID_UTIL IN NUMBER);
end pack_devize;
/

prompt
prompt Creating package body PACK_DEVIZE
prompt =================================
prompt
CREATE OR REPLACE PACKAGE BODY pack_devize is
  procedure dev_sterge_comanda(tcSchema    in varchar2,
                               tnIdUtil    in number,
                               tnIdOrdl    in number,
                               tnIdLucrare in number) is
    lnRulaje   NUMBER(3) := 0;
    lnOperatii NUMBER(3) := 0;
  BEGIN
    EXECUTE IMMEDIATE 'SELECT COUNT(*) AS SUMA FROM ' || tcSchema ||
                      '.RUL WHERE ID_LUCRARE=:1 AND STERS=0'
      INTO lnRulaje
      USING tnIdLucrare;

    IF lnRulaje > 0 then
      raise_application_error(-20001,
                              'Aceasta comanda nu poate fi stearsa, deoarece are legaturi in rulaje!');
    ELSE
      EXECUTE IMMEDIATE 'SELECT COUNT(*) AS SUMA FROM ' || tcSchema ||
                        '.DEV_OPER WHERE ID_ORDL=:1 AND STERS=0'
        INTO lnOperatii
        USING tnIdOrdl;

      IF lnOperatii > 0 then
        raise_application_error(-20001,
                                'Aceasta comanda nu poate fi stearsa, deoarece are adaugate operatii!');
      ELSE
        EXECUTE IMMEDIATE 'UPDATE ' || tcSchema ||
                          '.DEV_ORDL SET STERS=1,ID_UTILS=:1,DATAORAS=SYSDATE WHERE ' ||
                          'ID_ORDL = :2'
          USING tnIdUtil, tnIdOrdl;
      END IF;
    END IF;
  END;
  ------------------------------------------------------------------
  procedure dev_adauga_oper_fact(tcSchema    in varchar2,
                                 tnIdOrdl    in number,
                                 tnIdSectie  in number,
                                 tnIdNorme   in number,
                                 tnTotalFTva in number,
                                 tnTimpN     in number,
                                 tnOre       in number,
                                 tnIdUtil    in number,
                                 tnIdMecanic in number) is
    V_ID_OPER number(10);
    V_PRET    number(17, 4);
    V_DATAORA date;
    V_VALIDAT number(1);
    v_nou     number(1);
  begin
    if pack_sesiune.getAn() * 12 + pack_sesiune.getLuna() >=
       pack_sesiune.GET_ANRON() * 12 + pack_sesiune.GET_LUNARON() then
      v_nou := 1;
    else
      v_nou := 0;
    end if;

    v_dataora := sysdate;
    v_validat := 1;
    execute immediate 'UPDATE ' || tcSchema || '.DEV_ORDL ' ||
                      'SET VALIDAT = :1,ID_UTIL_VALID = :2,DATAORAVALID = :3 WHERE ID_ORDL = :4 '
      using V_VALIDAT, tnIdUtil, V_DATAORA, tnIdOrdl;

    V_PRET := tnTotalFTva / tnTimpN;

    execute immediate 'INSERT INTO ' || tcSchema ||
                      '.DEV_OPER(ID_ORDL,ID_SECTIE,TIMPN,PRET,ID_NORME,ID_UTIL,DATAORA,VALIDAT,NOU) ' ||
                      'VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9) RETURNING ID_OPER INTO :10'
      using tnIdOrdl, tnIdSectie, tnTimpN, V_PRET, tnIdNorme, tnIdUtil, V_DATAORA, V_VALIDAT, V_NOU
      returning into V_ID_OPER;

    execute immediate 'INSERT INTO ' || tcSchema ||
                      '.DEV_OPER_MECANICI(ID_OPER,ID_MECANIC,ORE) ' ||
                      'VALUES(:1,:2,:3)'
      using V_ID_OPER, tnIdMecanic, tnOre;
  end;
  ------------------------------------------------------------------
  procedure dev_adauga_lucrare(v_gcs    in varchar2,
                               tnan     in number,
                               tnluna   in number,
                               tnIdUtil in number,
                               pcNr     in varchar2,
                               pnIdInsp in number,
                               pnIdAsig in number,
                               pnIdMC   in number,
                               pnKmInt  in number,
                               pdTermen in date,
                               pnTipCom in number,
                               pnIdOrdl out number) is
    V_DATAORA date := sysdate;

  begin
    execute immediate 'INSERT INTO ' || v_gcs || '.NOM_LUCRARI (NRORD,ID_MOD)
        VALUES( :1, 1200)'
      using pcNr;

    execute immediate 'insert into ' || v_gcs ||
                      '.dev_ordl
        (an, luna, id_inspector, id_lucrare, datai, id_util_ad, id_masiniclient,id_asigurator,id_tip,kmint,termen,dataoraad)
        values( :1 , :2 , :3 , :4 , :5 , :6 , :7 , :8, :9, :10, :11, :12) returning id_ordl into :13'
      using tnAn, tnLuna, pnIdInsp, pack_sesiune.dev_idLucrare, v_dataora, tnIdUtil, pnIdMC, pnIdAsig, pnTipCom, pnKmInt, pdTermen, V_DATAORA
      returning into pnidOrdl;

    execute immediate 'update ' || v_gcs ||
                      '.dev_masiniclienti set kmint=:1 ' ||
                      'where id_masiniclient=:2'
      using pnKmInt, pnIdMC;
  end;
  ------------------------------------------------------------------
  procedure dev_valideaza_comanda(v_gcs     in varchar2,
                                  tnid_ordl in number,
                                  tnid_util in number) as
    lnNevalidate NUMBER(3);
  begin
    execute immediate 'SELECT COUNT(*) FROM ' || v_gcs || '.DEV_OPER ' ||
                      'WHERE STERS=0 AND VALIDAT=0 AND ID_ORDL=:1'
      into lnNevalidate
      using tnid_ordl;

    if lnNevalidate > 0 then
      RAISE_APPLICATION_ERROR(-20000,
                              'Mai sunt operatii nevalidate pe aceasta comanda!' ||
                              CHR(13) || CHR(10) ||
                              LPAD(CHR(32), 12, CHR(32)) ||
                              'Comanda nu a fost validata!');
    end if;
    execute immediate 'UPDATE ' || v_gcs || '.DEV_ORDL SET VALIDAT=1,' ||
                      'ID_UTIL_VALID=:1,DATAORAVALID=SYSDATE ' ||
                      'WHERE ID_ORDL=:2'
      using tnid_util, tnid_ordl;
  end;
  ------------------------------------------------------------------
  procedure dev_invalideaza_comanda(v_gcs      in varchar2,
                                    tnid_ordl  in number,
                                    tnid_utils in number) AS
  begin
    execute immediate 'UPDATE ' || v_gcs || '.DEV_ORDL ' ||
                      'SET VALIDAT=0,ID_UTIL_VALID=0,DATAORAVALID=NULL,' ||
                      'ID_UTILS=:1,DATAORAS=SYSDATE ' || 'WHERE ID_ORDL=:2'
      using tnid_utils, tnid_ordl;
  end;
  ------------------------------------------------------------------
  procedure dev_distribuie_timp_n(v_gcs in varchar2, v_filtru in varchar2) AS
    lnManopera   number(10);
    lnSucces     number(10);
    lnTimpN      number(7, 3);
    lnId_oper    number(10);
    lnNrMecanici number(3);
    lnOre        number(7, 3);
    lcSql        varchar2(1000);
    lcFiltru     varchar2(2000);
    lnSumaOre    number(7, 3);
    lnRanduri    number(4);
    lnIdMecanic  number(1);
    lcDevOper    VARCHAR2(30);
  begin
    lcFiltru   := replace(v_filtru, CHR(63), CHR(39));
    lnManopera := dbms_sql.open_cursor;
    /*lcSql      := 'SELECT B.ID_OPER,B.TIMPN,ROUND(SUM(NVL(D.ORE,-1.000)),3)
    FROM ' || v_gcs || '.dev_vvalid_comenzi A
    JOIN ' || v_gcs ||
       '.DEV_VOPER B ON A.ID_ORDL=B.ID_ORDL
    LEFT JOIN ' || v_gcs ||
       '.DEV_VOPER_DETALII_M D ON B.ID_OPER=D.ID_OPER
    WHERE ' || lcFiltru || '
    GROUP BY B.ID_OPER,B.TIMPN
    HAVING B.TIMPN-ROUND(SUM(NVL(D.ORE,-1.000)),3)!=0';*/
    IF lcFiltru IS NULL THEN
      lcDevOper := 'DEV_VOPER';
    ELSE
      lcDevOper := 'DEV_VOPER_SIMPLU';
    END IF;

    lcSql := 'SELECT B.ID_OPER,B.TIMPN,ROUND(SUM(NVL(D.ORE,-1.000)),3)
               FROM ' || v_gcs || '.dev_normare_comenzi A
               JOIN ' || v_gcs || '.' || lcDevOper ||
             ' B ON A.ID_ORDL=B.ID_ORDL
               LEFT JOIN ' || v_gcs || '.DEV_VOPER_DETALII_M D ON B.ID_OPER=D.ID_OPER
               WHERE ' || lcFiltru || '
               GROUP BY B.ID_OPER,B.TIMPN
               HAVING B.TIMPN-ROUND(SUM(NVL(D.ORE,-1.000)),3)!=0';
    dbms_sql.parse(lnManopera, lcSql, dbms_sql.native);
    dbms_sql.define_column(lnManopera, 1, lnId_oper);
    dbms_sql.define_column(lnManopera, 2, lnTimpN);
    dbms_sql.define_column(lnManopera, 3, lnSumaOre);

    lnIdMecanic := 1;
    lnSucces    := dbms_sql.execute(lnManopera);
    loop
      if dbms_sql.fetch_rows(lnManopera) > 0 then
        dbms_sql.column_value(lnManopera, 1, lnId_oper);
        dbms_sql.column_value(lnManopera, 2, lnTimpN);
        dbms_sql.column_value(lnManopera, 3, lnSumaOre);

        execute immediate 'SELECT COUNT(ID_MECANIC) FROM ' || v_gcs ||
                          '.DEV_VOPER_DETALII_M WHERE ID_OPER=:1'
          into lnNrMecanici
          using lnId_oper;

        if lnNrMecanici = 0 then
          execute immediate 'INSERT INTO ' || v_gcs ||
                            '.DEV_OPER_MECANICI(ID_OPER,ID_MECANIC,ORE) ' ||
                            ' VALUES(:1,:2,:3)'
            using lnId_oper, lnIdMecanic, lnTimpN;
        else
          lnOre := lnTimpN / lnNrMecanici;
          execute immediate 'UPDATE ' || v_gcs ||
                            '.DEV_OPER_MECANICI SET ORE=:1 WHERE ID_OPER=:2 AND STERS=0'
            using lnOre, lnId_oper;
        end if;

      else
        exit;
      end if;
    end loop;

    dbms_sql.close_cursor(lnManopera);
  end;
  ------------------------------------------------------------------
  procedure dev_adauga_operatie(v_gcs           in varchar2,
                                tnid_sectie     in number,
                                tnid_ordl       in number,
                                tnpret          in number,
                                tnid_util       in number,
                                tntimpn         in number,
                                tnid_norme      in number,
                                tcsir_idmecanic in varchar2) AS
    v_id_oper     number(10);
    v_lungime_sir number(3);
    v_ore         number(7, 3);
    v_timp_ramas  number(7, 3);
    v_nou         number(1);
  begin

    if pack_sesiune.getAn() * 12 + pack_sesiune.getLuna() >=
       pack_sesiune.GET_ANRON() * 12 + pack_sesiune.GET_LUNARON() then
      v_nou := 1;
    else
      v_nou := 0;
    end if;

    /*    execute immediate 'INSERT INTO ' || v_gcs ||
                    '.DEV_OPER(ID_ORDL,ID_SECTIE,TIMPN,PRET,ID_NORME,ID_UTIL,NOU,DATAORA) ' ||
                    'VALUES (:1,:2,:3,:4,:5,:6,:7,SYSDATE) RETURNING ID_OPER INTO :8'
    using tnid_ordl, tnid_sectie, tntimpn, tnpret, tnid_norme, tnid_util, v_nou
    returning into v_id_oper;*/

    execute immediate 'INSERT INTO ' || v_gcs ||
                      '.DEV_OPER(ID_ORDL,ID_SECTIE,TIMPN,PRET,ID_NORME,ID_UTILI,NOU,DATAORA) ' ||
                      'VALUES (:1,:2,:3,:4,:5,:6,:7,:8) RETURNING ID_OPER INTO :9'
      using tnid_ordl, tnid_sectie, tntimpn, tnpret, tnid_norme, tnid_util, v_nou, SYSDATE
      returning into v_id_oper;

    v_lungime_sir := SIR_IN_SIR(tcsir_idmecanic, ';');
    v_ore         := round(tntimpn / v_lungime_sir, 3);

    if v_lungime_sir > 0 then
      for i in 1 .. v_lungime_sir loop
        execute immediate 'INSERT INTO ' || v_gcs ||
                          '.DEV_OPER_MECANICI(ID_OPER,ID_MECANIC,ORE) ' ||
                          'VALUES(:1,:2,:3)'
          using v_id_oper, ID_DIN_SIR(tcsir_idmecanic, ';', i), v_ore;
      end loop;
    end if;

  end;
  ------------------------------------------------------------------
  procedure dev_actualizeaza_operatie(v_gcs          in varchar2,
                                      v_camp         in varchar2,
                                      v_valoare_noua in number,
                                      v_id_oper      in number,
                                      v_luna         in number,
                                      v_an           in number) as
    v_datai     date;
    v_data_ron  date;
    v_data_util date;
    v_valoare   number;
  begin
    execute immediate 'SELECT DATAI FROM ' || v_gcs ||
                      '.DEV_OPER WHERE ID_OPER=:1'
      into v_datai
      using v_id_oper;

    v_data_ron  := to_date(pack_sesiune.get_lunaron || '/' ||
                           pack_sesiune.get_anron,
                           'MM/YYYY');
    v_data_util := to_date(v_luna || '/' || v_an, 'MM/YYYY');

    if UPPER(TRIM(v_camp)) = 'PRET' and v_datai < v_data_ron and
       v_data_util >= v_data_ron then
      v_valoare := v_valoare_noua * 10000;
    else
      v_valoare := v_valoare_noua;
    end if;

    execute immediate 'UPDATE ' || v_gcs || '.DEV_OPER SET ' || v_camp ||
                      '=:1 WHERE ID_OPER=:2'
      using v_valoare, v_id_oper;
  end;
  ------------------------------------------------------------------
  procedure dev_valideaza_operatie(v_gcs     in varchar2,
                                   tnid_oper in number,
                                   tnid_util in number) as
  begin
    execute immediate 'UPDATE ' || v_gcs || '.DEV_OPER SET VALIDAT=1,' ||
                      'ID_UTIL=:1,DATAORA=SYSDATE ' || 'WHERE ID_OPER=:2'
      using tnid_util, tnid_oper;
  end;
  ------------------------------------------------------------------
  procedure dev_valideaza_operatii(v_gcs     in varchar2,
                                   tnid_ordl in number,
                                   tnid_util in number) as
  begin
    execute immediate 'UPDATE ' || v_gcs || '.DEV_OPER SET VALIDAT=1,' ||
                      'ID_UTIL=:1,DATAORA=SYSDATE ' ||
                      'WHERE ID_ORDL=:2 AND STERS=0 AND VALIDAT=0'
      using tnid_util, tnid_ordl;
  end;
  ------------------------------------------------------------------
  procedure dev_completeaza_rul(v_gcs in varchar2, tnid_lucrare in number) AS
    V_ID_FACT number(20);
  begin
    V_ID_FACT := pack_contafin.get_idFact();
    execute immediate 'update ' || v_gcs ||
                      '.rul set id_fact=:1 where id_lucrare=:2'
      using V_ID_FACT, tnid_lucrare;
  end;
  ------------------------------------------------------------------
  procedure valideaza_operatie(V_SCHEMA  IN VARCHAR2,
                               V_VALIDAT IN NUMBER,
                               V_ID_OPER IN NUMBER,
                               V_ID_UTIL IN NUMBER) is
  begin
    EXECUTE IMMEDIATE 'UPDATE ' || V_SCHEMA || '.DEV_OPER SET VALIDAT=:1,' ||
                      'DATAORA=:2,ID_UTIL=:3 WHERE ID_OPER=:4'
      USING V_VALIDAT, SYSDATE, V_ID_UTIL, V_ID_OPER;
  end;
  ------------------------------------------------------------------
  procedure modifica_sectie(V_SCHEMA    IN VARCHAR2,
                            V_ID_SECTIE IN NUMBER,
                            V_ID_OPER   IN NUMBER,
                            V_ID_UTIL   IN NUMBER) is
  begin
    EXECUTE IMMEDIATE 'UPDATE ' || V_SCHEMA ||
                      '.DEV_OPER SET ID_SECTIE=:1 WHERE ID_OPER=:2'
      USING V_ID_SECTIE, V_ID_OPER;
  end;
  ------------------------------------------------------------------
  procedure modifica_tip_comanda(V_SCHEMA  IN VARCHAR2,
                                 V_ID_TIP  IN NUMBER,
                                 V_ID_ORDL IN NUMBER,
                                 V_ID_UTIL IN NUMBER) is
  begin
    EXECUTE IMMEDIATE 'UPDATE ' || V_SCHEMA || '.DEV_ORDL SET ID_TIP=:1 ' ||
                      'WHERE ID_ORDL=:2'
      USING V_ID_TIP, V_ID_ORDL;
  end;
  ------------------------------------------------------------------
  procedure sterge_operatie(V_SCHEMA  IN VARCHAR2,
                            V_ID_OPER IN NUMBER,
                            V_ID_UTIL IN NUMBER) is
  begin
    EXECUTE IMMEDIATE 'UPDATE ' || V_SCHEMA || '.DEV_OPER SET STERS=:1,' ||
                      'DATAORAS=:2,ID_UTILS=:3 WHERE ID_OPER=:4'
      USING 1, SYSDATE, V_ID_UTIL, V_ID_OPER;
  end;
  ------------------------------------------------------------------
  ------------------------------------------------------------------
end pack_devize;
/


spool off
