--
-- $Id$
--
-- Aanpassen organisatiestruktuur.

DEFINE thisfile = 'AAHB#53150.SQL'
DEFINE dbuser   = '^AAHB'

SET ECHO ON
SET DEFINE ON
COLUMN fcltlogfile NEW_VALUE fcltlogfile NOPRINT;
COLUMN fcltcusterr NEW_VALUE fcltcusterr NOPRINT;
WHENEVER SQLERROR CONTINUE;
SELECT adm.getscriptspoolfile('&thisfile') AS fcltlogfile FROM DUAL;
SPOOL &fcltlogfile
WHENEVER SQLERROR EXIT;
SELECT adm.checkscriptcust('&dbuser') AS fcltcusterr FROM DUAL;
WHENEVER SQLERROR CONTINUE;
SET DEFINE OFF

------ payload begin ------

DECLARE
    CURSOR c
    IS
          SELECT *
            FROM fac_imp_organisatie
        ORDER BY prs_afdeling_naam_parent NULLS FIRST;

    v_errormsg             VARCHAR2 (100);
    v_afdeling_key         NUMBER;
    v_bedrijf_key          NUMBER;
    v_afdeling_parentkey   NUMBER;
BEGIN
    FOR rec IN c
    LOOP
        BEGIN
            v_errormsg := 'Zoek afdeling:' || rec.prs_afdeling_naam;

            SELECT prs_afdeling_key
              INTO v_afdeling_key
              FROM prs_afdeling a
             WHERE     prs_afdeling_naam = rec.prs_afdeling_naam
                   AND prs_afdeling_verwijder IS NULL
                       AND NOT EXISTS (SELECT prs_afdeling_key
                                         FROM prs_afdeling a2
                                        WHERE a.prs_afdeling_parentkey = a2.prs_afdeling_key
                                          AND a2.prs_afdeling_verwijder IS NOT NULL);


            IF rec.prs_afdeling_naam_parent IS NOT NULL
            THEN
                v_errormsg := 'Zoek parent:' || rec.prs_afdeling_naam_parent;

                SELECT prs_afdeling_key
                  INTO v_afdeling_parentkey
                  FROM prs_afdeling a
                 WHERE     prs_afdeling_naam = rec.prs_afdeling_naam_parent
                       AND prs_afdeling_verwijder IS NULL
                       AND NOT EXISTS (SELECT prs_afdeling_key
                                         FROM prs_afdeling a2
                                        WHERE a.prs_afdeling_parentkey = a2.prs_afdeling_key
                                          AND a2.prs_afdeling_verwijder IS NOT NULL);

                v_bedrijf_key := NULL;
            ELSE
                v_afdeling_parentkey := NULL;
                v_bedrijf_key := 570;                                   -- HBC
            END IF;

--            DBMS_OUTPUT.put_line (
--                   'Update afdeling: '
--                || v_afdeling_key
--                || ' bedrijf: '
--                || v_bedrijf_key
--                || ' parent: '
--                || v_afdeling_parentkey);
        UPDATE prs_afdeling SET prs_afdeling_omschrijving = rec.prs_afdeling_omschrijving,
                                prs_afdeling_parentkey = v_afdeling_parentkey,
                                prs_bedrijf_key = v_bedrijf_key
                            WHERE prs_afdeling_key = v_afdeling_key;

        EXCEPTION
            WHEN OTHERS
            THEN
                DBMS_OUTPUT.put_line (v_errormsg);
        END;
    END LOOP;
END;
/

------ payload end   ------

SET DEFINE OFF
BEGIN adm.systrackscriptId ('$Id$', 1); END;
/

COMMIT;
SET ECHO OFF
SPOOL OFF
SET DEFINE ON
PROMPT Logfile of this upgrade is: &fcltlogfile
