CREATE OR REPLACE FUNCTION FC_GET_WK_DT (   IN_SYS_ID   IN VARCHAR
                                        ,   IN_COMP_CD  IN VARCHAR
                                        ,   IN_WK_DAYS  IN NUMBER
                                        ,   IN_FR_DT    IN VARCHAR := TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')) RETURN VARCHAR
IS
    
    /*
     * 시작이 되는 날짜의 다음날 부터 최종 working date 찾기
     * IN_WK_DAYS : 찾고자하는 working 일수
     * IN_FR_DT : 시작이 되는 날짜
     */

    V_WK_DAYS   NUMBER      := 0;
    V_WK_DT     DATE        := TO_DATE(IN_FR_DT, 'YYYYMMDDHH24MISS')
    
BEGIN
    
    WHILE (IN_WK_DAYS != V_WK_DAYS)
    LOOP
        
        SELECT  V_WK_DAYS
        +       CASE WHEN TO_CHAR(D.WK_DT, 'D') IN ('1', '7') OR H.HOLY_YN = 'Y'
                    THEN 0
                    ELSE 1
                END
        ,       D.WK_DT
        
        INTO    V_WK_DAYS
        ,       V_WK_DT
        
        FROM   (SELECT  IN_SYS_ID                       AS SYS_ID
                ,       IN_COMP_CD                      AS COMP_CD
                ,       V_WK_DT + 1                     AS WK_DT
                FROM DUAL) D
        LEFT OUTER JOIN ESAHOLY H   ON  H.SYS_ID        = D.SYS_ID
                                    AND H.COMP_CD       = D.COMP_CD
                                    AND H.HOLY_YEAR     = TO_CHAR(D.WK_DT, 'YYYY')
                                    AND H.HOLY_MONTH    = TO_CHAR(D.WK_DT, 'MM')
                                    AND H.HOLY_DAY      = TO_CHAR(D.WK_DT, 'DD');
    
    END LOOP;

    RETURN TO_CHAR(V_WK_DT, 'YYYYMMDDHH24MISS');

    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE(SQLERRM);
            RETURN NULL;
END