 BEGIN
    P_VAL:='THAT_BAI';
    IF P_KNTC_MA_LOAI_DT = 'TC'  OR P_KNTC_MA_LOAI_DT = 'CN' or P_KNTC_SO_NGUOI_DAI_DIEN IS NULL THEN
       V_KNTC_SO_NGUOI_DAI_DIEN:=1;
    ELSE 
       V_KNTC_SO_NGUOI_DAI_DIEN:=P_KNTC_SO_NGUOI_DAI_DIEN;
    END IF;
    IF P_MA_HO_SO_KNTC = 0 THEN
                SELECT MAX(SO_HO_SO_KNTC) INTO V_SO_HO_SO_KNTC FROM KNTC_HO_SO;
                IF V_SO_HO_SO_KNTC IS NULL THEN
                   V_SO_HO_SO_KNTC:=1;
                ELSE
                   V_SO_HO_SO_KNTC:= V_SO_HO_SO_KNTC + 1;
                END IF;
                INSERT INTO KNTC_HO_SO(
                                SO_HO_SO_KNTC,
                                NGAY_NHAP,
                                MA_NGUON_DON_DEN,
                                NGAY_TIEP_DAN,
                                VU_VIEC_MOI,
                                NOI_DUNG_TIEP_DAN,
                                YEU_CAU_GAP_LD,
                                MA_LD_TIEP_DAN,
                                Y_KIEN_LD,
                                MA_CAN_BO_TIEP_DAN,
                                MA_CO_QUAN_TIEP_DAN,
                                MA_LINH_VUC_KNTC,   
                                MA_LOAI_KNTC,
                                KNTC_MA_LOAI_DT,
                                KNTC_CMND,
                                KNTC_HO_VA_TEN,
                                KNTC_MA_PHUONG_XA,
                                KNTC_SO_NHA,
                                KNTC_TEN_CQTC,
                                KNTC_DIA_CHI_CQTC,
                                KNTC_SO_NGUOI_DAI_DIEN,
                                KNTC_TEN_NHUNG_NGUOI_DD,
                                MA_HO_SO_KNTC_GOC,
                                MA_DON_VI_XU_LY_CHINH
                                )
                         VALUES (
                                V_SO_HO_SO_KNTC,
                                SYSDATE,
                                P_MA_NGUON_DON_DEN,
                                P_NGAY_TIEP_DAN,
                                P_VU_VIEC_MOI,
                                P_NOI_DUNG_TIEP_DAN,
                                P_YEU_CAU_GAP_LD,
                                CASE WHEN P_MA_LD_TIEP_DAN =0 THEN NULL ELSE P_MA_LD_TIEP_DAN END,
                                P_Y_KIEN_LD,
                                P_MA_CAN_BO_TIEP_DAN,
                                P_MA_CO_QUAN_TIEP_DAN,
                                P_MA_LINH_VUC_KNTC,   
                                P_MA_LOAI_KNTC,
                                P_KNTC_MA_LOAI_DT,
                                P_KNTC_CMND,
                                P_KNTC_HO_VA_TEN,
                                P_KNTC_MA_PHUONG_XA,
                                P_KNTC_SO_NHA,
                                P_KNTC_TEN_CQTC,
                                P_KNTC_DIA_CHI_CQTC,
                                V_KNTC_SO_NGUOI_DAI_DIEN,
                                P_KNTC_TEN_NHUNG_NGUOI_DD,
                                P_MA_HO_SO_KNTC_GOC,
                                P_MA_DON_VI_XU_LY_CHINH
                                );
    ELSE
                 UPDATE KNTC_HO_SO A SET A.MA_NGUON_DON_DEN=P_MA_NGUON_DON_DEN,
                                         A.NGAY_TIEP_DAN=P_NGAY_TIEP_DAN,
                                         A.VU_VIEC_MOI=P_VU_VIEC_MOI,
                                         A.NOI_DUNG_TIEP_DAN=P_NOI_DUNG_TIEP_DAN,
                                         A.YEU_CAU_GAP_LD=P_YEU_CAU_GAP_LD,
                                         A.MA_LD_TIEP_DAN= CASE WHEN P_MA_LD_TIEP_DAN =0 THEN NULL ELSE P_MA_LD_TIEP_DAN END,
                                         A.Y_KIEN_LD=P_Y_KIEN_LD,
                                         A.MA_LINH_VUC_KNTC=P_MA_LINH_VUC_KNTC,
                                         A.MA_LOAI_KNTC=P_MA_LOAI_KNTC,
                                         A.KNTC_MA_LOAI_DT=P_KNTC_MA_LOAI_DT,
                                         A.KNTC_CMND=P_KNTC_CMND,
                                         A.KNTC_HO_VA_TEN=P_KNTC_HO_VA_TEN,
                                         A.KNTC_MA_PHUONG_XA=P_KNTC_MA_PHUONG_XA,
                                         A.KNTC_SO_NHA=P_KNTC_SO_NHA,
                                         A.KNTC_TEN_CQTC=P_KNTC_TEN_CQTC,
                                         A.KNTC_DIA_CHI_CQTC=P_KNTC_DIA_CHI_CQTC,
                                         A.KNTC_SO_NGUOI_DAI_DIEN=V_KNTC_SO_NGUOI_DAI_DIEN,
                                         A.KNTC_TEN_NHUNG_NGUOI_DD=P_KNTC_TEN_NHUNG_NGUOI_DD,
                                         A.MA_HO_SO_KNTC_GOC=P_MA_HO_SO_KNTC_GOC,
                                         A.MA_DON_VI_XU_LY_CHINH=P_MA_DON_VI_XU_LY_CHINH
              WHERE A.MA_HO_SO_KNTC=P_MA_HO_SO_KNTC;
    END IF;
    P_VAL:='THANH_CONG';
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_NGUOI_BI_KNTC(P_VAL OUT NVARCHAR2,
                          P_MA_HO_SO_KNTC INT,
                          P_BKNTC_MA_LOAI_DT NVARCHAR2,
                          P_BKNTC_HO_VA_TEN NVARCHAR2,
                          P_BKNTC_DIA_CHI NVARCHAR2,
                          P_BKNTC_TEN_CQTC NVARCHAR2,
                          P_BKNTC_DIA_CHI_CQTC NVARCHAR2
                        ) AS
 BEGIN
    P_VAL:='THAT_BAI';
            UPDATE KNTC_HO_SO A SET A.BKNTC_MA_LOAI_DT=P_BKNTC_MA_LOAI_DT,
                                         A.BKNTC_HO_VA_TEN=P_BKNTC_HO_VA_TEN,
                                         A.BKNTC_DIA_CHI=P_BKNTC_DIA_CHI,
                                         A.BKNTC_TEN_CQTC=P_BKNTC_TEN_CQTC,
                                         A.BKNTC_DIA_CHI_CQTC=P_BKNTC_DIA_CHI_CQTC
              WHERE A.MA_HO_SO_KNTC=P_MA_HO_SO_KNTC;
    P_VAL:='THANH_CONG';
  END;


    -------------------------------------------------------------------------------
  PROCEDURE UPDATE_GIAI_QUYET(P_VAL OUT NVARCHAR2,
                          P_MA_HO_SO_KNTC INT,
                          P_MA_GIAI_QUYET INT,
                          P_MA_CAN_BO INT,
                          P_NGAY_GIAI_QUYET NVARCHAR2,
                          P_LAN_GIAI_QUYET INT,
                          P_MA_HUONG_GIAI_QUYET NVARCHAR2,
                          P_NOI_DUNG_GIAI_QUYET NVARCHAR2,
                          P_SO_KY_HIEU NVARCHAR2,
                          P_TRICH_YEU NVARCHAR2,
                          P_CO_QUAN_BAN_HANH NVARCHAR2,
                          P_SO_BUT_LUC NVARCHAR2,
                          P_SO_TRANG NVARCHAR2,
                          P_NGAY_TRINH_LANH_DAO_KY NVARCHAR2,
                          P_NGAY_LANH_DAO_KY NVARCHAR2,
                          P_NGAY_CHUYEN_VAN_BAN NVARCHAR2,
                          P_MA_LANH_DAO_KY INT,
                          P_FILE_VAN_BAN NVARCHAR2
                        ) AS
 BEGIN
    P_VAL:='THAT_BAI';
    IF P_MA_GIAI_QUYET = 0 THEN
       INSERT INTO KNTC_GIAI_QUYET (
                                    MA_HO_SO_KNTC,
                                    MA_CAN_BO,
                                    NGAY_GIAI_QUYET,
                                    MA_HUONG_GIAI_QUYET,
                                    NOI_DUNG_GIAI_QUYET,
                                    SO_KY_HIEU,
                                    TRICH_YEU,
                                    CO_QUAN_BAN_HANH,
                                    SO_BUT_LUC,
                                    SO_TRANG,
                                    LAN_GIAI_QUYET,
                                    NGAY_TRINH_LANH_DAO_KY,
                                    NGAY_LANH_DAO_KY,
                                    NGAY_CHUYEN_VAN_BAN,
                                    MA_LANH_DAO_KY,
                                    FILE_VAN_BAN
                                    )
                              VALUES(
                                    P_MA_HO_SO_KNTC,
                                    P_MA_CAN_BO,
                                    P_NGAY_GIAI_QUYET,
                                    P_MA_HUONG_GIAI_QUYET,
                                    P_NOI_DUNG_GIAI_QUYET,
                                    P_SO_KY_HIEU,
                                    P_TRICH_YEU,
                                    P_CO_QUAN_BAN_HANH,
                                    P_SO_BUT_LUC,
                                    P_SO_TRANG,
                                    P_LAN_GIAI_QUYET,
                                    P_NGAY_TRINH_LANH_DAO_KY,
                                    P_NGAY_LANH_DAO_KY,
                                    P_NGAY_CHUYEN_VAN_BAN,
                                    P_MA_LANH_DAO_KY,
                                    P_FILE_VAN_BAN
                                    );
    ELSE
       UPDATE KNTC_GIAI_QUYET A SET A.NGAY_GIAI_QUYET     = P_NGAY_GIAI_QUYET,
                                    A.MA_HUONG_GIAI_QUYET = P_MA_HUONG_GIAI_QUYET,
                                    A.NOI_DUNG_GIAI_QUYET = P_NOI_DUNG_GIAI_QUYET,
                                    A.SO_KY_HIEU          = P_SO_KY_HIEU,
                                    A.TRICH_YEU           = P_TRICH_YEU,
                                    A.CO_QUAN_BAN_HANH    = P_CO_QUAN_BAN_HANH,
                                    A.SO_BUT_LUC          = P_SO_BUT_LUC,
                                    A.SO_TRANG            = P_SO_TRANG,
                                    A.LAN_GIAI_QUYET      = P_LAN_GIAI_QUYET,
                                    A.NGAY_TRINH_LANH_DAO_KY= P_NGAY_TRINH_LANH_DAO_KY,
                                    A.NGAY_LANH_DAO_KY      = P_NGAY_LANH_DAO_KY,
                                    A.NGAY_CHUYEN_VAN_BAN   = P_NGAY_CHUYEN_VAN_BAN,
                                    A.MA_LANH_DAO_KY        = P_MA_LANH_DAO_KY,
                                    A.FILE_VAN_BAN          = P_FILE_VAN_BAN
                              WHERE A.MA_GIAI_QUYET=P_MA_GIAI_QUYET;
    END IF;        
    P_VAL:='THANH_CONG';
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_GIAI_QUYET(P_VAL OUT NVARCHAR2, P_MA_GIAI_QUYET INT) AS
  BEGIN
    P_VAL:='THAT_BAI';
    DELETE KNTC_GIAI_QUYET A WHERE A.MA_GIAI_QUYET = P_MA_GIAI_QUYET;      
    P_VAL:='THANH_CONG';
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_GIAI_QUYET(P_CUR IN OUT MYCUR, P_MA_HO_SO_KNTC INT) AS
  V_MAX_LAN_GIAI_QUYET INT;
  BEGIN
    SELECT MAX(LAN_GIAI_QUYET) INTO V_MAX_LAN_GIAI_QUYET FROM KNTC_GIAI_QUYET A WHERE A.MA_HO_SO_KNTC = P_MA_HO_SO_KNTC;
    IF V_MAX_LAN_GIAI_QUYET IS NULL THEN
       V_MAX_LAN_GIAI_QUYET:=0;
    END IF; 
    OPEN P_CUR FOR
     SELECT A.*,
            B.TEN_HUONG_GIAI_QUYET, 
            V_MAX_LAN_GIAI_QUYET AS MAX_LAN_GIAI_QUYET
        FROM KNTC_GIAI_QUYET A, DM_HUONG_GIAI_QUYET B
        WHERE A.MA_HO_SO_KNTC = P_MA_HO_SO_KNTC
          AND A.MA_HUONG_GIAI_QUYET = B.MA_HUONG_GIAI_QUYET
          ORDER BY A.LAN_GIAI_QUYET;
  END;
  
 PROCEDURE UPDATE_FILE_GIAI_QUYET(P_VAL OUT NVARCHAR2, P_MA_GIAI_QUYET NVARCHAR2, P_FILE_VAN_BAN NVARCHAR2) AS
   BEGIN
      UPDATE KNTC_GIAI_QUYET A SET A.FILE_VAN_BAN          = P_FILE_VAN_BAN
                              WHERE A.MA_GIAI_QUYET=P_MA_GIAI_QUYET;
      P_VAL:='TC';
   END;
/*  -----------------------------------------------------------------------------
  PROCEDURE UPDATE_TIEP_DAN_CO_DON(P_VAL OUT NVARCHAR2,
                          P_MA_HO_SO_KNTC INT,
                          P_MA_NGUON_DON_DEN NVARCHAR2,
                          P_NGAY_TIEP_DAN NVARCHAR2,
                          P_VU_VIEC_MOI INT,
                          P_NOI_DUNG_TIEP_DAN NVARCHAR2,
                          P_YEU_CAU_GAP_LD INT,
                          P_MA_LD_TIEP_DAN INT,
                          P_Y_KIEN_LD NVARCHAR2,
                          P_MA_CAN_BO_TIEP_DAN INT,
                          P_MA_CO_QUAN_TIEP_DAN NVARCHAR2,
                          P_MA_LINH_VUC_KNTC NVARCHAR2,   
                          P_MA_LOAI_KNTC NVARCHAR2,
                          P_KNTC_MA_LOAI_DT NVARCHAR2,
                          P_KNTC_CMND NVARCHAR2,
                          P_KNTC_HO_VA_TEN NVARCHAR2,
                          P_KNTC_MA_PHUONG_XA NVARCHAR2,
                          P_KNTC_SO_NHA NVARCHAR2,
                          P_KNTC_TEN_CQTC NVARCHAR2,
                          P_KNTC_DIA_CHI_CQTC NVARCHAR2,
                          P_KNTC_SO_NGUOI_DAI_DIEN INT,
                          P_KNTC_TEN_NHUNG_NGUOI_DD NVARCHAR2,
                          P_MA_HO_SO_KNTC_GOC INT,
                          P_MA_DON_VI_XU_LY_CHINH NVARCHAR2,
                          P_MA_TRANG_THAI_GIAI_QUYET  NVARCHAR2
                        ) AS
 V_KNTC_SO_NGUOI_DAI_DIEN INT;
 V_SO_HO_SO_KNTC INT;
 BEGIN
    P_VAL:='THAT_BAI';
    IF P_KNTC_MA_LOAI_DT = 'TC'  OR P_KNTC_MA_LOAI_DT = 'CN' or P_KNTC_SO_NGUOI_DAI_DIEN IS NULL THEN
       V_KNTC_SO_NGUOI_DAI_DIEN:=1;
    ELSE 
       V_KNTC_SO_NGUOI_DAI_DIEN:=P_KNTC_SO_NGUOI_DAI_DIEN;
    END IF;
    IF P_MA_HO_SO_KNTC = 0 THEN
                SELECT MAX(SO_HO_SO_KNTC) INTO V_SO_HO_SO_KNTC FROM KNTC_HO_SO;
                IF V_SO_HO_SO_KNTC IS NULL THEN
                   V_SO_HO_SO_KNTC:=1;
                END IF;
                INSERT INTO KNTC_HO_SO(
                                SO_HO_SO_KNTC,
                                NGAY_NHAP,
                                MA_NGUON_DON_DEN,
                                NGAY_TIEP_DAN,
                                VU_VIEC_MOI,
                                NOI_DUNG_TIEP_DAN,
                                YEU_CAU_GAP_LD,
                                MA_LD_TIEP_DAN,
                                Y_KIEN_LD,
                                MA_CAN_BO_TIEP_DAN,
                                MA_CO_QUAN_TIEP_DAN,
                                MA_LINH_VUC_KNTC,   
                                MA_LOAI_KNTC,
                                KNTC_MA_LOAI_DT,
                                KNTC_CMND,
                                KNTC_HO_VA_TEN,
                                KNTC_MA_PHUONG_XA,
                                KNTC_SO_NHA,
                                KNTC_TEN_CQTC,
                                KNTC_DIA_CHI_CQTC,
                                KNTC_SO_NGUOI_DAI_DIEN,
                                KNTC_TEN_NHUNG_NGUOI_DD,
                                MA_HO_SO_KNTC_GOC,
                                MA_DON_VI_XU_LY_CHINH,
                                MA_TRANG_THAI_GIAI_QUYET
                                )
                         VALUES (
                                V_SO_HO_SO_KNTC,
                                SYSDATE,
                                P_MA_NGUON_DON_DEN,
                                P_NGAY_TIEP_DAN,
                                P_VU_VIEC_MOI,
                                P_NOI_DUNG_TIEP_DAN,
                                P_YEU_CAU_GAP_LD,
                                CASE WHEN P_MA_LD_TIEP_DAN =0 THEN NULL ELSE P_MA_LD_TIEP_DAN END,
                                P_Y_KIEN_LD,
                                P_MA_CAN_BO_TIEP_DAN,
                                P_MA_CO_QUAN_TIEP_DAN,
                                P_MA_LINH_VUC_KNTC,   
                                P_MA_LOAI_KNTC,
                                P_KNTC_MA_LOAI_DT,
                                P_KNTC_CMND,
                                P_KNTC_HO_VA_TEN,
                                P_KNTC_MA_PHUONG_XA,
                                P_KNTC_SO_NHA,
                                P_KNTC_TEN_CQTC,
                                P_KNTC_DIA_CHI_CQTC,
                                V_KNTC_SO_NGUOI_DAI_DIEN,
                                P_KNTC_TEN_NHUNG_NGUOI_DD,
                                P_MA_HO_SO_KNTC_GOC,
                                P_MA_DON_VI_XU_LY_CHINH,
                                P_MA_TRANG_THAI_GIAI_QUYET
                                );
    ELSE
            UPDATE KNTC_HO_SO A SET A.MA_NGUON_DON_DEN=P_MA_NGUON_DON_DEN,
                                         A.NGAY_TIEP_DAN=P_NGAY_TIEP_DAN,
                                         A.VU_VIEC_MOI=P_VU_VIEC_MOI,
                                         A.NOI_DUNG_TIEP_DAN=P_NOI_DUNG_TIEP_DAN,
                                         A.YEU_CAU_GAP_LD=P_YEU_CAU_GAP_LD,
                                         A.MA_LD_TIEP_DAN= CASE WHEN P_MA_LD_TIEP_DAN =0 THEN NULL ELSE P_MA_LD_TIEP_DAN END,
                                         A.Y_KIEN_LD=P_Y_KIEN_LD,
                                         A.MA_LINH_VUC_KNTC=P_MA_LINH_VUC_KNTC,
                                         A.MA_LOAI_KNTC=P_MA_LOAI_KNTC,
                                         A.KNTC_MA_LOAI_DT=P_KNTC_MA_LOAI_DT,
                                         A.KNTC_CMND=P_KNTC_CMND,
                                         A.KNTC_HO_VA_TEN=P_KNTC_HO_VA_TEN,
                                         A.KNTC_MA_PHUONG_XA=P_KNTC_MA_PHUONG_XA,
                                         A.KNTC_SO_NHA=P_KNTC_SO_NHA,
                                         A.KNTC_TEN_CQTC=P_KNTC_TEN_CQTC,
                                         A.KNTC_DIA_CHI_CQTC = P_KNTC_DIA_CHI_CQTC,
                                         A.KNTC_SO_NGUOI_DAI_DIEN=V_KNTC_SO_NGUOI_DAI_DIEN,
                                         A.KNTC_TEN_NHUNG_NGUOI_DD=P_KNTC_TEN_NHUNG_NGUOI_DD,
                                         A.MA_HO_SO_KNTC_GOC=P_MA_HO_SO_KNTC_GOC,
                                         A.MA_DON_VI_XU_LY_CHINH=P_MA_DON_VI_XU_LY_CHINH,
                                         A.MA_TRANG_THAI_GIAI_QUYET=P_MA_TRANG_THAI_GIAI_QUYET
              WHERE A.MA_HO_SO_KNTC=P_MA_HO_SO_KNTC;
    END IF;
    P_VAL:='THANH_CONG';
  END;*/
-------------------------------------------------------------------------------
  PROCEDURE M2A(P_CUR IN OUT MYCUR, P_TU_NGAY NVARCHAR2, P_DEN_NGAY NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT 
    -- tất cả
     --TAT_CA.MA_DON_VI_XU_LY_CHINH AS MA_DON_VI,
     TAT_CA.TEN_DON_VI,
     TAT_CA.TC_SO_LUOT,
     TAT_CA.TC_SO_NGUOI,
     TAT_CA.TC_MOI_PHAT_SINH,
     TAT_CA.TC_VU_VIEC_CU,
     -- tiếp thường xuyên cá nhân
     TXTCCN.TXTCCN_SO_LUOT,
     TXTCCN.TXTCCN_SO_NGUOI,
     TXTCCN.TXTCCN_MOI_PHAT_SINH,
     TXTCCN.TXTCCN_VU_VIEC_CU,
     -- tiếp thường xuyên tập thể, tổ chức
     TXTT.TXTT_SO_LUOT,
     TXTT.TXTT_SO_NGUOI,
     TXTT.TXTT_MOI_PHAT_SINH,
     TXTT.TXTT_VU_VIEC_CU,
     -- tiếp đột xuất lãnh đạo cá nhân
     LDTCCN.LDTCCN_SO_LUOT,
     LDTCCN.LDTCCN_SO_NGUOI,
     LDTCCN.LDTCCN_MOI_PHAT_SINH,
     LDTCCN.LDTCCN_VU_VIEC_CU,
     -- tiếp đột xuất lãnh đạo tập thể, tổ chức
     LDTT.LDTT_SO_LUOT,
     LDTT.LDTT_SO_NGUOI,
     LDTT.LDTT_MOI_PHAT_SINH,
     LDTT.LDTT_VU_VIEC_CU,
     -- các lĩnh vực khiếu nại
     KN_TCDNGT.KN_TCDNGT,
     KN_CS.KN_CS,
     KN_TS.KN_TS,
     KN_CDCC.KN_CDCC,
     KN_TP.KN_TP,
     KN_CTVHXH.KN_CTVHXH,
     -- các lĩnh vực tố cáo
     TC_HC.TC_HC,
     TC_TP.TC_TP,
     TC_TN.TC_TN,
     -- phản ánh kiến nghị khác
     PA_KN_K.PA_KN_K,
     -- kết quả giải quyết
     CGQ.CGQ,
     CCQD.CCQD,
     DCQD.DCQD,
     DCBA.DCBA
     FROM 
       (SELECT A.MA_DON_VI_XU_LY_CHINH , B.TEN_DON_VI, 
                    COUNT(A.MA_HO_SO_KNTC)                      AS TC_SO_LUOT,
                    SUM(A.KNTC_SO_NGUOI_DAI_DIEN)               AS TC_SO_NGUOI,
                    SUM(A.VU_VIEC_MOI)                          AS TC_MOI_PHAT_SINH,
                    COUNT(A.MA_HO_SO_KNTC) - SUM(A.VU_VIEC_MOI) AS TC_VU_VIEC_CU
                FROM KNTC_HO_SO A, CB_DON_VI B
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_DON_VI_XU_LY_CHINH=B.MA_DON_VI
                GROUP BY A.MA_DON_VI_XU_LY_CHINH, B.TEN_DON_VI
        ) TAT_CA,-- tồng cộng tất cả của đơn vị
        
       (SELECT A.MA_DON_VI_XU_LY_CHINH, 
                    COUNT(A.MA_HO_SO_KNTC)                      AS TXTCCN_SO_LUOT,
                    SUM(A.KNTC_SO_NGUOI_DAI_DIEN)               AS TXTCCN_SO_NGUOI,
                    SUM(A.VU_VIEC_MOI)                          AS TXTCCN_MOI_PHAT_SINH,
                    COUNT(A.MA_HO_SO_KNTC) - SUM(A.VU_VIEC_MOI) AS TXTCCN_VU_VIEC_CU
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                --AND A.KNTC_MA_LOAI_DT <> 'TT'
                AND A.MA_LD_TIEP_DAN IS NULL
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) TXTCCN,-- thường xuyên tiếp cá nhân
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, 
                    COUNT(A.MA_HO_SO_KNTC)                      AS TXTT_SO_LUOT,
                    SUM(A.KNTC_SO_NGUOI_DAI_DIEN)               AS TXTT_SO_NGUOI,
                    SUM(A.VU_VIEC_MOI)                          AS TXTT_MOI_PHAT_SINH,
                    COUNT(A.MA_HO_SO_KNTC) - SUM(A.VU_VIEC_MOI) AS TXTT_VU_VIEC_CU
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.KNTC_MA_LOAI_DT = 'TT'
                AND A.MA_LD_TIEP_DAN IS NULL
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) TXTT,-- thường xuyên tiếp tập thể
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, 
                    COUNT(A.MA_HO_SO_KNTC)                      AS LDTCCN_SO_LUOT,
                    SUM(A.KNTC_SO_NGUOI_DAI_DIEN)               AS LDTCCN_SO_NGUOI,
                    SUM(A.VU_VIEC_MOI)                          AS LDTCCN_MOI_PHAT_SINH,
                    COUNT(A.MA_HO_SO_KNTC) - SUM(A.VU_VIEC_MOI) AS LDTCCN_VU_VIEC_CU
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.KNTC_MA_LOAI_DT <> 'TT'
                AND A.MA_LD_TIEP_DAN IS NOT NULL
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
         ) LDTCCN,-- lãnh đạo  tiếp cá nhân
         
        (SELECT A.MA_DON_VI_XU_LY_CHINH, 
                    COUNT(A.MA_HO_SO_KNTC)                      AS LDTT_SO_LUOT,
                    SUM(A.KNTC_SO_NGUOI_DAI_DIEN)               AS LDTT_SO_NGUOI,
                    SUM(A.VU_VIEC_MOI)                          AS LDTT_MOI_PHAT_SINH,
                    COUNT(A.MA_HO_SO_KNTC) - SUM(A.VU_VIEC_MOI) AS LDTT_VU_VIEC_CU
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.KNTC_MA_LOAI_DT = 'TT'
                AND A.MA_LD_TIEP_DAN IS NOT NULL
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) LDTT,-- lãnh đạo tiếp tập thể
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS KN_TCDNGT
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'TC'
                AND A.MA_LOAI_KNTC = 'KN'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) KN_TCDNGT,-- HIẾU NẠI-- Về tranh chấp, đòi đất cũ, đền bù, giải tỏa
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS KN_CS
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'CS'
                AND A.MA_LOAI_KNTC = 'KN'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) KN_CS,-- KHIẾU NẠI-- Về chính sách
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS KN_TS
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'TS'
                AND A.MA_LOAI_KNTC = 'KN'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) KN_TS,-- KHIẾU NẠI-- Về tài sản
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS KN_CDCC
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'TC'
                AND A.MA_LOAI_KNTC = 'KN'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) KN_CDCC,-- KHIẾU NẠI-- Về chế dộ công chức viên chức
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS KN_TP
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'TP'
                AND A.MA_LOAI_KNTC = 'KN'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) KN_TP,-- KHIẾU NẠI-- về tư pháp
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS KN_CTVHXH
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'CTVHXH'
                AND A.MA_LOAI_KNTC = 'KN'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) KN_CTVHXH,-- KHIẾU NẠI-- về chính trị văn hóa xã hội khác
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS TC_HC
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'HC'
                AND A.MA_LOAI_KNTC = 'TC'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) TC_HC,-- TỐ CÁO-- Về hành chính
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS TC_TP
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'TP'
                AND A.MA_LOAI_KNTC = 'TC'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) TC_TP,-- TỐ CÁO-- Về tư pháp
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS TC_TN
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_LINH_VUC_KNTC = 'TN'
                AND A.MA_LOAI_KNTC = 'TC'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) TC_TN,-- TỐ CÁO-- về tham nhũng
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS PA_KN_K
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                  AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                  AND A.MA_NGUON_DON_DEN IS NULL
                  AND A.MA_LOAI_KNTC = 'K'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) PA_KN_K,-- Phản ánh kiến nghị khác
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS CGQ
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NULL
                AND A.MA_TRANG_THAI_GIAI_QUYET = 'CGQ'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) CGQ,-- chua giai quyet
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS CCQD
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                  AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                  AND A.MA_NGUON_DON_DEN IS NULL
                  AND A.MA_TRANG_THAI_GIAI_QUYET = 'CCQD'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) CCQD,-- chua co quyet dinh
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS DCQD
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                  AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                  AND A.MA_NGUON_DON_DEN IS NULL
                  AND A.MA_TRANG_THAI_GIAI_QUYET = 'DCQD'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) DCQD,-- da co quyet dinh
        
        (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS DCBA
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                  AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                  AND A.MA_NGUON_DON_DEN IS NULL
                  AND A.MA_TRANG_THAI_GIAI_QUYET = 'DCBA'
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) DCBA-- da co ban an
        
        
        WHERE TAT_CA.MA_DON_VI_XU_LY_CHINH = TXTCCN.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = TXTT.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = LDTT.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = LDTCCN.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = KN_TCDNGT.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = KN_CS.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = KN_TS.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = KN_CDCC.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = KN_TP.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = KN_CTVHXH.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = TC_HC.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = TC_TP.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = TC_TN.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = PA_KN_K.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = CGQ.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = CCQD.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = DCQD.MA_DON_VI_XU_LY_CHINH(+)
          AND TAT_CA.MA_DON_VI_XU_LY_CHINH = DCBA.MA_DON_VI_XU_LY_CHINH(+)
          ;
  END;   
  -----------------------------------------------------------------------------
  PROCEDURE M2B(P_CUR IN OUT MYCUR, P_TU_NGAY NVARCHAR2, P_DEN_NGAY NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT 
     TAT_CA.MA_DON_VI_XU_LY_CHINH AS MA_DON_VI,
     TAT_CA.TC_SO_DON,
     TNTK.TNTK_NHIEU_NGUOI,
     TAT_CA.TC_SO_DON - TNTK.TNTK_NHIEU_NGUOI AS TNTK_MOT_NGUOI
     FROM 
       (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS TC_SO_DON
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NOT NULL
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) TAT_CA,
        
       (SELECT A.MA_DON_VI_XU_LY_CHINH, COUNT(A.MA_HO_SO_KNTC) AS TNTK_NHIEU_NGUOI
                FROM KNTC_HO_SO A
                WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
                AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
                AND A.MA_NGUON_DON_DEN IS NOT NULL
                AND A.KNTC_SO_NGUOI_DAI_DIEN > 0
                GROUP BY A.MA_DON_VI_XU_LY_CHINH
        ) TNTK -- tiếp nhận trong kỳ
        
        WHERE TAT_CA.MA_DON_VI_XU_LY_CHINH = TNTK.MA_DON_VI_XU_LY_CHINH(+)
          ;
  END;    
END KNTC_BK;
/

prompt
prompt Creating package body KNTC_DM
prompt =============================
prompt
create or replace package body hai.KNTC_DM is

   ---------------------------------------------DM_HUONG_GIAI_QUYET----------------------------------
  PROCEDURE SELECT_HUONG_GIAI_QUYET(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_HUONG_GIAI_QUYET = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_HUONG_GIAI_QUYET A
       ORDER BY A.STT_HUONG_GIAI_QUYET , A.TEN_HUONG_GIAI_QUYET;
  END;
  -------------------------------------------------------------------------------
    PROCEDURE SELECT_HUONG_GIAI_QUYET_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_HUONG_GIAI_QUYET NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_HUONG_GIAI_QUYET = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_HUONG_GIAI_QUYET A
       WHERE A.MA_HUONG_GIAI_QUYET LIKE P_MA_HUONG_GIAI_QUYET;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_HUONG_GIAI_QUYET(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_HUONG_GIAI_QUYET     NVARCHAR2,
                           P_TEN_HUONG_GIAI_QUYET    NVARCHAR2,
                           P_STT_HUONG_GIAI_QUYET    INT,
                           P_TT_HUONG_GIAI_QUYET     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_HUONG_GIAI_QUYET)
        INTO V_TON_TAI_MA
        FROM DM_HUONG_GIAI_QUYET
       WHERE UPPER(MA_HUONG_GIAI_QUYET) LIKE UPPER(P_MA_HUONG_GIAI_QUYET);
      SELECT COUNT(MA_HUONG_GIAI_QUYET)
        INTO V_TON_TAI_TEN
        FROM DM_HUONG_GIAI_QUYET
       WHERE UPPER(TEN_HUONG_GIAI_QUYET) LIKE UPPER(P_TEN_HUONG_GIAI_QUYET);
      IF V_TON_TAI_MA > 0 OR P_MA_HUONG_GIAI_QUYET = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_HUONG_GIAI_QUYET
          (MA_HUONG_GIAI_QUYET,
           TEN_HUONG_GIAI_QUYET,
           STT_HUONG_GIAI_QUYET,
           TT_HUONG_GIAI_QUYET)
        VALUES
          (P_MA_HUONG_GIAI_QUYET,
           P_TEN_HUONG_GIAI_QUYET,
           P_STT_HUONG_GIAI_QUYET,
           P_TT_HUONG_GIAI_QUYET);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_HUONG_GIAI_QUYET)
        INTO V_TON_TAI_TEN
        FROM DM_HUONG_GIAI_QUYET
       WHERE UPPER(TEN_HUONG_GIAI_QUYET) LIKE UPPER(P_TEN_HUONG_GIAI_QUYET)
         AND MA_HUONG_GIAI_QUYET NOT LIKE P_MA_HUONG_GIAI_QUYET;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_HUONG_GIAI_QUYET
           SET TEN_HUONG_GIAI_QUYET    = P_TEN_HUONG_GIAI_QUYET,
               STT_HUONG_GIAI_QUYET    = P_STT_HUONG_GIAI_QUYET,
               TT_HUONG_GIAI_QUYET     = P_TT_HUONG_GIAI_QUYET
         WHERE MA_HUONG_GIAI_QUYET LIKE P_MA_HUONG_GIAI_QUYET;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_HUONG_GIAI_QUYET(P_VAL   OUT NVARCHAR2, P_MA_HUONG_GIAI_QUYET NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_HUONG_GIAI_QUYET WHERE MA_HUONG_GIAI_QUYET LIKE P_MA_HUONG_GIAI_QUYET;
    P_VAL := 'THANH_CONG';
   END;  
   ---------------------------------------------DM_HUONG_GIAI_QUYET----------------------------------
  
  PROCEDURE SELECT_TT_GIAI_QUYET(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_TRANG_THAI_GIAI_QUYET = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_TRANG_THAI_GIAI_QUYET A
       ORDER BY A.STT_TRANG_THAI_GIAI_QUYET , A.TEN_TRANG_THAI_GIAI_QUYET;
  END;
  -------------------------------------------------------------------------------
    PROCEDURE SELECT_TT_GIAI_QUYET_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_TRANG_THAI_GIAI_QUYET NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_TRANG_THAI_GIAI_QUYET = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_TRANG_THAI_GIAI_QUYET A
       WHERE A.MA_TRANG_THAI_GIAI_QUYET LIKE P_MA_TRANG_THAI_GIAI_QUYET;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_TT_GIAI_QUYET(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_TRANG_THAI_GIAI_QUYET     NVARCHAR2,
                           P_TEN_TRANG_THAI_GIAI_QUYET    NVARCHAR2,
                           P_STT_TRANG_THAI_GIAI_QUYET    INT,
                           P_TT_TRANG_THAI_GIAI_QUYET     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_TRANG_THAI_GIAI_QUYET)
        INTO V_TON_TAI_MA
        FROM DM_TRANG_THAI_GIAI_QUYET
       WHERE UPPER(MA_TRANG_THAI_GIAI_QUYET) LIKE UPPER(P_MA_TRANG_THAI_GIAI_QUYET);
      SELECT COUNT(MA_TRANG_THAI_GIAI_QUYET)
        INTO V_TON_TAI_TEN
        FROM DM_TRANG_THAI_GIAI_QUYET
       WHERE UPPER(TEN_TRANG_THAI_GIAI_QUYET) LIKE UPPER(P_TEN_TRANG_THAI_GIAI_QUYET);
      IF V_TON_TAI_MA > 0 OR P_MA_TRANG_THAI_GIAI_QUYET = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_TRANG_THAI_GIAI_QUYET
          (MA_TRANG_THAI_GIAI_QUYET,
           TEN_TRANG_THAI_GIAI_QUYET,
           STT_TRANG_THAI_GIAI_QUYET,
           TT_TRANG_THAI_GIAI_QUYET)
        VALUES
          (P_MA_TRANG_THAI_GIAI_QUYET,
           P_TEN_TRANG_THAI_GIAI_QUYET,
           P_STT_TRANG_THAI_GIAI_QUYET,
           P_TT_TRANG_THAI_GIAI_QUYET);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_TRANG_THAI_GIAI_QUYET)
        INTO V_TON_TAI_TEN
        FROM DM_TRANG_THAI_GIAI_QUYET
       WHERE UPPER(TEN_TRANG_THAI_GIAI_QUYET) LIKE UPPER(P_TEN_TRANG_THAI_GIAI_QUYET)
         AND MA_TRANG_THAI_GIAI_QUYET NOT LIKE P_MA_TRANG_THAI_GIAI_QUYET;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_TRANG_THAI_GIAI_QUYET
           SET TEN_TRANG_THAI_GIAI_QUYET    = P_TEN_TRANG_THAI_GIAI_QUYET,
               STT_TRANG_THAI_GIAI_QUYET    = P_STT_TRANG_THAI_GIAI_QUYET,
               TT_TRANG_THAI_GIAI_QUYET     = P_TT_TRANG_THAI_GIAI_QUYET
         WHERE MA_TRANG_THAI_GIAI_QUYET LIKE P_MA_TRANG_THAI_GIAI_QUYET;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_TT_GIAI_QUYET(P_VAL   OUT NVARCHAR2, P_MA_TRANG_THAI_GIAI_QUYET NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_TRANG_THAI_GIAI_QUYET WHERE MA_TRANG_THAI_GIAI_QUYET LIKE P_MA_TRANG_THAI_GIAI_QUYET;
    P_VAL := 'THANH_CONG';
   END;  
   -------------------------------------DM_LINH_VUC_KNTC------------------------------------------
PROCEDURE SELECT_LINH_VUC_KNTC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LINH_VUC_KNTC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_LINH_VUC_KNTC A
       ORDER BY A.STT_LINH_VUC_KNTC , A.TEN_LINH_VUC_KNTC;
  END;
  -------------------------------------------------------------------------------
    PROCEDURE SELECT_LINH_VUC_KNTC_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_LINH_VUC_KNTC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LINH_VUC_KNTC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_LINH_VUC_KNTC A
       WHERE A.MA_LINH_VUC_KNTC LIKE P_MA_LINH_VUC_KNTC;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_LINH_VUC_KNTC(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LINH_VUC_KNTC     NVARCHAR2,
                           P_TEN_LINH_VUC_KNTC    NVARCHAR2,
                           P_STT_LINH_VUC_KNTC    INT,
                           P_TT_LINH_VUC_KNTC     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_LINH_VUC_KNTC)
        INTO V_TON_TAI_MA
        FROM DM_LINH_VUC_KNTC
       WHERE UPPER(MA_LINH_VUC_KNTC) LIKE UPPER(P_MA_LINH_VUC_KNTC);
      SELECT COUNT(MA_LINH_VUC_KNTC)
        INTO V_TON_TAI_TEN
        FROM DM_LINH_VUC_KNTC
       WHERE UPPER(TEN_LINH_VUC_KNTC) LIKE UPPER(P_TEN_LINH_VUC_KNTC);
      IF V_TON_TAI_MA > 0 OR P_MA_LINH_VUC_KNTC = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_LINH_VUC_KNTC
          (MA_LINH_VUC_KNTC,
           TEN_LINH_VUC_KNTC,
           STT_LINH_VUC_KNTC,
           TT_LINH_VUC_KNTC)
        VALUES
          (P_MA_LINH_VUC_KNTC,
           P_TEN_LINH_VUC_KNTC,
           P_STT_LINH_VUC_KNTC,
           P_TT_LINH_VUC_KNTC);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_LINH_VUC_KNTC)
        INTO V_TON_TAI_TEN
        FROM DM_LINH_VUC_KNTC
       WHERE UPPER(TEN_LINH_VUC_KNTC) LIKE UPPER(P_TEN_LINH_VUC_KNTC)
         AND MA_LINH_VUC_KNTC NOT LIKE P_MA_LINH_VUC_KNTC;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_LINH_VUC_KNTC
           SET TEN_LINH_VUC_KNTC    = P_TEN_LINH_VUC_KNTC,
               STT_LINH_VUC_KNTC    = P_STT_LINH_VUC_KNTC,
               TT_LINH_VUC_KNTC     = P_TT_LINH_VUC_KNTC
         WHERE MA_LINH_VUC_KNTC LIKE P_MA_LINH_VUC_KNTC;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_LINH_VUC_KNTC(P_VAL   OUT NVARCHAR2, P_MA_LINH_VUC_KNTC NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_LINH_VUC_KNTC WHERE MA_LINH_VUC_KNTC LIKE P_MA_LINH_VUC_KNTC;
    P_VAL := 'THANH_CONG';
   END;  
   
   --------------------------------DM_LOAI_DTBKNTC-----------------------------------------------
   
   PROCEDURE SELECT_LOAI_DTBKNTC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_DTBKNTC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_LOAI_DTBKNTC A
       ORDER BY A.STT_LOAI_DTBKNTC , A.TEN_LOAI_DTBKNTC;
  END;
  -------------------------------------------------------------------------------
    PROCEDURE SELECT_LOAI_DTBKNTC_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_LOAI_DTBKNTC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_DTBKNTC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_LOAI_DTBKNTC A
       WHERE A.MA_LOAI_DTBKNTC LIKE P_MA_LOAI_DTBKNTC;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_LOAI_DTBKNTC(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LOAI_DTBKNTC     NVARCHAR2,
                           P_TEN_LOAI_DTBKNTC    NVARCHAR2,
                           P_STT_LOAI_DTBKNTC    INT,
                           P_TT_LOAI_DTBKNTC     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_LOAI_DTBKNTC)
        INTO V_TON_TAI_MA
        FROM DM_LOAI_DTBKNTC
       WHERE UPPER(MA_LOAI_DTBKNTC) LIKE UPPER(P_MA_LOAI_DTBKNTC);
      SELECT COUNT(MA_LOAI_DTBKNTC)
        INTO V_TON_TAI_TEN
        FROM DM_LOAI_DTBKNTC
       WHERE UPPER(TEN_LOAI_DTBKNTC) LIKE UPPER(P_TEN_LOAI_DTBKNTC);
      IF V_TON_TAI_MA > 0 OR P_MA_LOAI_DTBKNTC = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_LOAI_DTBKNTC
          (MA_LOAI_DTBKNTC,
           TEN_LOAI_DTBKNTC,
           STT_LOAI_DTBKNTC,
           TT_LOAI_DTBKNTC)
        VALUES
          (P_MA_LOAI_DTBKNTC,
           P_TEN_LOAI_DTBKNTC,
           P_STT_LOAI_DTBKNTC,
           P_TT_LOAI_DTBKNTC);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_LOAI_DTBKNTC)
        INTO V_TON_TAI_TEN
        FROM DM_LOAI_DTBKNTC
       WHERE UPPER(TEN_LOAI_DTBKNTC) LIKE UPPER(P_TEN_LOAI_DTBKNTC)
         AND MA_LOAI_DTBKNTC NOT LIKE P_MA_LOAI_DTBKNTC;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_LOAI_DTBKNTC
           SET TEN_LOAI_DTBKNTC    = P_TEN_LOAI_DTBKNTC,
               STT_LOAI_DTBKNTC    = P_STT_LOAI_DTBKNTC,
               TT_LOAI_DTBKNTC     = P_TT_LOAI_DTBKNTC
         WHERE MA_LOAI_DTBKNTC LIKE P_MA_LOAI_DTBKNTC;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_DTBKNTC(P_VAL   OUT NVARCHAR2, P_MA_LOAI_DTBKNTC NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_LOAI_DTBKNTC WHERE MA_LOAI_DTBKNTC LIKE P_MA_LOAI_DTBKNTC;
    P_VAL := 'THANH_CONG';
   END;  

 --------------------------------DM_LOAI_DTKNTC-----------------------------------------------
   
   PROCEDURE SELECT_LOAI_DTKNTC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_DTKNTC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_LOAI_DTKNTC A
       ORDER BY A.STT_LOAI_DTKNTC , A.TEN_LOAI_DTKNTC;
  END;
  -------------------------------------------------------------------------------
    PROCEDURE SELECT_LOAI_DTKNTC_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_LOAI_DTKNTC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_DTKNTC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_LOAI_DTKNTC A
       WHERE A.MA_LOAI_DTKNTC LIKE P_MA_LOAI_DTKNTC;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_LOAI_DTKNTC(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LOAI_DTKNTC     NVARCHAR2,
                           P_TEN_LOAI_DTKNTC    NVARCHAR2,
                           P_STT_LOAI_DTKNTC    INT,
                           P_TT_LOAI_DTKNTC     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_LOAI_DTKNTC)
        INTO V_TON_TAI_MA
        FROM DM_LOAI_DTKNTC
       WHERE UPPER(MA_LOAI_DTKNTC) LIKE UPPER(P_MA_LOAI_DTKNTC);
      SELECT COUNT(MA_LOAI_DTKNTC)
        INTO V_TON_TAI_TEN
        FROM DM_LOAI_DTKNTC
       WHERE UPPER(TEN_LOAI_DTKNTC) LIKE UPPER(P_TEN_LOAI_DTKNTC);
      IF V_TON_TAI_MA > 0 OR P_MA_LOAI_DTKNTC = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_LOAI_DTKNTC
          (MA_LOAI_DTKNTC,
           TEN_LOAI_DTKNTC,
           STT_LOAI_DTKNTC,
           TT_LOAI_DTKNTC)
        VALUES
          (P_MA_LOAI_DTKNTC,
           P_TEN_LOAI_DTKNTC,
           P_STT_LOAI_DTKNTC,
           P_TT_LOAI_DTKNTC);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_LOAI_DTKNTC)
        INTO V_TON_TAI_TEN
        FROM DM_LOAI_DTKNTC
       WHERE UPPER(TEN_LOAI_DTKNTC) LIKE UPPER(P_TEN_LOAI_DTKNTC)
         AND MA_LOAI_DTKNTC NOT LIKE P_MA_LOAI_DTKNTC;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_LOAI_DTKNTC
           SET TEN_LOAI_DTKNTC    = P_TEN_LOAI_DTKNTC,
               STT_LOAI_DTKNTC    = P_STT_LOAI_DTKNTC,
               TT_LOAI_DTKNTC     = P_TT_LOAI_DTKNTC
         WHERE MA_LOAI_DTKNTC LIKE P_MA_LOAI_DTKNTC;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_DTKNTC(P_VAL   OUT NVARCHAR2, P_MA_LOAI_DTKNTC NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_LOAI_DTKNTC WHERE MA_LOAI_DTKNTC LIKE P_MA_LOAI_DTKNTC;
    P_VAL := 'THANH_CONG';
   END;  
----------------------------------------------------------------------------------
--------------------------------DM_LOAI_KET_QUA-----------------------------------------------
   
   PROCEDURE SELECT_LOAI_KET_QUA(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_KET_QUA = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_LOAI_KET_QUA A
       ORDER BY A.STT_LOAI_KET_QUA , A.TEN_LOAI_KET_QUA;
  END;
  -------------------------------------------------------------------------------
    PROCEDURE SELECT_LOAI_KET_QUA_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_LOAI_KET_QUA NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_KET_QUA = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_LOAI_KET_QUA A
       WHERE A.MA_LOAI_KET_QUA LIKE P_MA_LOAI_KET_QUA;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_LOAI_KET_QUA(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LOAI_KET_QUA     NVARCHAR2,
                           P_TEN_LOAI_KET_QUA    NVARCHAR2,
                           P_STT_LOAI_KET_QUA    INT,
                           P_TT_LOAI_KET_QUA     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_LOAI_KET_QUA)
        INTO V_TON_TAI_MA
        FROM DM_LOAI_KET_QUA
       WHERE UPPER(MA_LOAI_KET_QUA) LIKE UPPER(P_MA_LOAI_KET_QUA);
      SELECT COUNT(MA_LOAI_KET_QUA)
        INTO V_TON_TAI_TEN
        FROM DM_LOAI_KET_QUA
       WHERE UPPER(TEN_LOAI_KET_QUA) LIKE UPPER(P_TEN_LOAI_KET_QUA);
      IF V_TON_TAI_MA > 0 OR P_MA_LOAI_KET_QUA = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_LOAI_KET_QUA
          (MA_LOAI_KET_QUA,
           TEN_LOAI_KET_QUA,
           STT_LOAI_KET_QUA,
           TT_LOAI_KET_QUA)
        VALUES
          (P_MA_LOAI_KET_QUA,
           P_TEN_LOAI_KET_QUA,
           P_STT_LOAI_KET_QUA,
           P_TT_LOAI_KET_QUA);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_LOAI_KET_QUA)
        INTO V_TON_TAI_TEN
        FROM DM_LOAI_KET_QUA
       WHERE UPPER(TEN_LOAI_KET_QUA) LIKE UPPER(P_TEN_LOAI_KET_QUA)
         AND MA_LOAI_KET_QUA NOT LIKE P_MA_LOAI_KET_QUA;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_LOAI_KET_QUA
           SET TEN_LOAI_KET_QUA    = P_TEN_LOAI_KET_QUA,
               STT_LOAI_KET_QUA    = P_STT_LOAI_KET_QUA,
               TT_LOAI_KET_QUA     = P_TT_LOAI_KET_QUA
         WHERE MA_LOAI_KET_QUA LIKE P_MA_LOAI_KET_QUA;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_KET_QUA(P_VAL   OUT NVARCHAR2, P_MA_LOAI_KET_QUA NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_LOAI_KET_QUA WHERE MA_LOAI_KET_QUA LIKE P_MA_LOAI_KET_QUA;
    P_VAL := 'THANH_CONG';
   END;  
------------------------------DM_NGUON_DON_DEN------------------------------------------
PROCEDURE SELECT_NGUON_DON_DEN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_NGUON_DON_DEN = 0 THEN
                'Mở'
               ELSE
                'Đóng'
             END AS TEN_TRANG_THAI
        FROM DM_NGUON_DON_DEN A
       ORDER BY A.STT_NGUON_DON_DEN , A.Ten_NGUON_DON_DEN;
  END;
   -------------------------------------------------------------------------------
    PROCEDURE SELECT_NGUON_DON_DEN_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_NGUON_DON_DEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_NGUON_DON_DEN = 0 THEN
                'Mở'
               ELSE
                'Đóng'
             END AS TEN_TRANG_THAI
        FROM DM_NGUON_DON_DEN A
       WHERE A.MA_NGUON_DON_DEN LIKE P_MA_NGUON_DON_DEN;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_NGUON_DON_DEN(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_NGUON_DON_DEN     NVARCHAR2,
                           P_TEN_NGUON_DON_DEN    NVARCHAR2,
                           P_STT_NGUON_DON_DEN    INT,
                           P_TT_NGUON_DON_DEN     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_NGUON_DON_DEN)
        INTO V_TON_TAI_MA
        FROM DM_NGUON_DON_DEN
       WHERE UPPER(MA_NGUON_DON_DEN) LIKE UPPER(P_MA_NGUON_DON_DEN);
      SELECT COUNT(MA_NGUON_DON_DEN)
        INTO V_TON_TAI_TEN
        FROM DM_NGUON_DON_DEN
       WHERE UPPER(TEN_NGUON_DON_DEN) LIKE UPPER(P_TEN_NGUON_DON_DEN);
      IF V_TON_TAI_MA > 0 OR P_MA_NGUON_DON_DEN = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_NGUON_DON_DEN
          (MA_NGUON_DON_DEN,
           TEN_NGUON_DON_DEN,
           STT_NGUON_DON_DEN,
           TT_NGUON_DON_DEN)
        VALUES
          (P_MA_NGUON_DON_DEN,
           P_TEN_NGUON_DON_DEN,
           P_STT_NGUON_DON_DEN,
           P_TT_NGUON_DON_DEN);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_NGUON_DON_DEN)
        INTO V_TON_TAI_TEN
        FROM DM_NGUON_DON_DEN
       WHERE UPPER(TEN_NGUON_DON_DEN) LIKE UPPER(P_TEN_NGUON_DON_DEN)
         AND MA_NGUON_DON_DEN NOT LIKE P_MA_NGUON_DON_DEN;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_NGUON_DON_DEN
           SET TEN_NGUON_DON_DEN    = P_TEN_NGUON_DON_DEN,
               STT_NGUON_DON_DEN    = P_STT_NGUON_DON_DEN,
               TT_NGUON_DON_DEN     = P_TT_NGUON_DON_DEN
         WHERE MA_NGUON_DON_DEN LIKE P_MA_NGUON_DON_DEN;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_NGUON_DON_DEN(P_VAL   OUT NVARCHAR2, P_MA_NGUON_DON_DEN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_NGUON_DON_DEN WHERE MA_NGUON_DON_DEN LIKE P_MA_NGUON_DON_DEN;
    P_VAL := 'THANH_CONG';
   END;

 ------------------------------DM_LOAI_KNTC------------------------------------------
PROCEDURE SELECT_LOAI_KNTC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_KNTC = 0 THEN
                'Mở'
               ELSE
                'Đóng'
             END AS TEN_TRANG_THAI
        FROM DM_LOAI_KNTC A
       ORDER BY A.STT_LOAI_KNTC , A.Ten_LOAI_KNTC;
  END;
   -------------------------------------------------------------------------------
    PROCEDURE SELECT_LOAI_KNTC_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_LOAI_KNTC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_KNTC = 0 THEN
                'Mở'
               ELSE
                'Đóng'
             END AS TEN_TRANG_THAI
        FROM DM_LOAI_KNTC A
       WHERE A.MA_LOAI_KNTC LIKE P_MA_LOAI_KNTC;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_LOAI_KNTC(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LOAI_KNTC     NVARCHAR2,
                           P_TEN_LOAI_KNTC    NVARCHAR2,
                           P_STT_LOAI_KNTC    INT,
                           P_TT_LOAI_KNTC     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_LOAI_KNTC)
        INTO V_TON_TAI_MA
        FROM DM_LOAI_KNTC
       WHERE UPPER(MA_LOAI_KNTC) LIKE UPPER(P_MA_LOAI_KNTC);
      SELECT COUNT(MA_LOAI_KNTC)
        INTO V_TON_TAI_TEN
        FROM DM_LOAI_KNTC
       WHERE UPPER(TEN_LOAI_KNTC) LIKE UPPER(P_TEN_LOAI_KNTC);
      IF V_TON_TAI_MA > 0 OR P_MA_LOAI_KNTC = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_LOAI_KNTC
          (MA_LOAI_KNTC,
           TEN_LOAI_KNTC,
           STT_LOAI_KNTC,
           TT_LOAI_KNTC)
        VALUES
          (P_MA_LOAI_KNTC,
           P_TEN_LOAI_KNTC,
           P_STT_LOAI_KNTC,
           P_TT_LOAI_KNTC);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_LOAI_KNTC)
        INTO V_TON_TAI_TEN
        FROM DM_LOAI_KNTC
       WHERE UPPER(TEN_LOAI_KNTC) LIKE UPPER(P_TEN_LOAI_KNTC)
         AND MA_LOAI_KNTC NOT LIKE P_MA_LOAI_KNTC;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_LOAI_KNTC
           SET TEN_LOAI_KNTC    = P_TEN_LOAI_KNTC,
               STT_LOAI_KNTC    = P_STT_LOAI_KNTC,
               TT_LOAI_KNTC     = P_TT_LOAI_KNTC
         WHERE MA_LOAI_KNTC LIKE P_MA_LOAI_KNTC;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_KNTC(P_VAL   OUT NVARCHAR2, P_MA_LOAI_KNTC NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_LOAI_KNTC WHERE MA_LOAI_KNTC LIKE P_MA_LOAI_KNTC;
    P_VAL := 'THANH_CONG';
   END;
------------------------------DM_THAM_QUYEN------------------------------------------
PROCEDURE SELECT_THAM_QUYEN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_THAM_QUYEN = 0 THEN
                'C'
               ELSE
                'Đóng'
             END AS TEN_TRANG_THAI
        FROM DM_THAM_QUYEN A
       ORDER BY A.STT_THAM_QUYEN , A.Ten_THAM_QUYEN;
  END;
   -------------------------------------------------------------------------------
    PROCEDURE SELECT_THAM_QUYEN_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_THAM_QUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_THAM_QUYEN = 0 THEN
                'Mở'
               ELSE
                'Đóng'
             END AS TEN_TRANG_THAI
        FROM DM_THAM_QUYEN A
       WHERE A.MA_THAM_QUYEN LIKE P_MA_THAM_QUYEN;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_THAM_QUYEN(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_THAM_QUYEN     NVARCHAR2,
                           P_TEN_THAM_QUYEN    NVARCHAR2,
                           P_STT_THAM_QUYEN    INT,
                           P_TT_THAM_QUYEN     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_THAM_QUYEN)
        INTO V_TON_TAI_MA
        FROM DM_THAM_QUYEN
       WHERE UPPER(MA_THAM_QUYEN) LIKE UPPER(P_MA_THAM_QUYEN);
      SELECT COUNT(MA_THAM_QUYEN)
        INTO V_TON_TAI_TEN
        FROM DM_THAM_QUYEN
       WHERE UPPER(TEN_THAM_QUYEN) LIKE UPPER(P_TEN_THAM_QUYEN);
      IF V_TON_TAI_MA > 0 OR P_MA_THAM_QUYEN = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_THAM_QUYEN
          (MA_THAM_QUYEN,
           TEN_THAM_QUYEN,
           STT_THAM_QUYEN,
           TT_THAM_QUYEN)
        VALUES
          (P_MA_THAM_QUYEN,
           P_TEN_THAM_QUYEN,
           P_STT_THAM_QUYEN,
           P_TT_THAM_QUYEN);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_THAM_QUYEN)
        INTO V_TON_TAI_TEN
        FROM DM_THAM_QUYEN
       WHERE UPPER(TEN_THAM_QUYEN) LIKE UPPER(P_TEN_THAM_QUYEN)
         AND MA_THAM_QUYEN NOT LIKE P_MA_THAM_QUYEN;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_THAM_QUYEN
           SET TEN_THAM_QUYEN    = P_TEN_THAM_QUYEN,
               STT_THAM_QUYEN    = P_STT_THAM_QUYEN,
               TT_THAM_QUYEN     = P_TT_THAM_QUYEN
         WHERE MA_THAM_QUYEN LIKE P_MA_THAM_QUYEN;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_THAM_QUYEN(P_VAL   OUT NVARCHAR2, P_MA_THAM_QUYEN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_THAM_QUYEN WHERE MA_THAM_QUYEN LIKE P_MA_THAM_QUYEN;
    P_VAL := 'THANH_CONG';
   END;

end KNTC_DM;
/

prompt
prompt Creating package body KNTC_SELECT
prompt =================================
prompt
create or replace package body hai.KNTC_SELECT is
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_NGUON_DON_DEN(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_NGUON_DON_DEN,
             A.TEN_NGUON_DON_DEN,
             A.STT_NGUON_DON_DEN
      FROM DM_NGUON_DON_DEN A
      UNION 
      SELECT N'' AS MA_NGUON_DON_DEN,
             P_TYPE AS TEN_NGUON_DON_DEN,
             -1 AS STT_NGUON_DON_DEN
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_NGUON_DON_DEN,TEN_NGUON_DON_DEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CHUC_VU(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CHUC_VU,
             A.TEN_CHUC_VU,
             A.STT_CHUC_VU
      FROM DM_CHUC_VU A
      UNION 
      SELECT N'' AS MA_CHUC_VU,
             P_TYPE AS TEN_CHUC_VU,
             -1 AS STT_CHUC_VU
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_CHUC_VU,TEN_CHUC_VU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DAN_TOC(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DAN_TOC,
             A.TEN_DAN_TOC,
             A.STT_DAN_TOC
      FROM DM_DAN_TOC A
      UNION 
      SELECT N'' AS MA_DAN_TOC,
             P_TYPE AS TEN_DAN_TOC,
             -1 AS STT_DAN_TOC
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_DAN_TOC,TEN_DAN_TOC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUOC_TICH(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUOC_TICH,
             A.TEN_QUOC_TICH,
             A.STT_QUOC_TICH
      FROM DM_QUOC_TICH A
      UNION 
      SELECT N'' AS MA_QUOC_TICH,
             P_TYPE AS TEN_QUOC_TICH,
             -1 AS STT_QUOC_TICH
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_QUOC_TICH,TEN_QUOC_TICH;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TON_GIAO(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TON_GIAO,
             A.TEN_TON_GIAO,
             A.STT_TON_GIAO
      FROM DM_TON_GIAO A
      UNION 
      SELECT N'' AS MA_TON_GIAO,
             P_TYPE AS TEN_TON_GIAO,
             -1 AS STT_TON_GIAO
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_TON_GIAO,TEN_TON_GIAO;
  END;

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_THAM_QUYEN(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_THAM_QUYEN,
             A.TEN_THAM_QUYEN,
             A.STT_THAM_QUYEN
      FROM DM_THAM_QUYEN A
      UNION 
      SELECT N'' AS MA_THAM_QUYEN,
             P_TYPE AS TEN_THAM_QUYEN,
             -1 AS STT_THAM_QUYEN
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_THAM_QUYEN,TEN_THAM_QUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_KNTC(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_KNTC,
             A.TEN_LOAI_KNTC,
             A.STT_LOAI_KNTC
      FROM DM_LOAI_KNTC A
      UNION 
      SELECT N'' AS MA_LOAI_KNTC,
             P_TYPE AS TEN_LOAI_KNTC,
             -1 AS STT_LOAI_KNTC
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_LOAI_KNTC,TEN_LOAI_KNTC;
  END;
  -------------------------------------------------------------------------------
  /*PROCEDURE SELECT_LINH_VUC_KNTC(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LINH_VUC_KNTC,
             A.TEN_LINH_VUC_KNTC,
             A.STT_LINH_VUC_KNTC
      FROM DM_LINH_VUC_KNTC A, KNTC_HO_SO_KNTC B
      WHERE B.MA_CO_QUAN_TIEP_DAN=P_MA_CO_QUAN
      UNION
      SELECT N'' AS MA_LINH_VUC_KNTC,
             P_TYPE AS TEN_LINH_VUC_KNTC,
             -1 AS STT_LINH_VUC_KNTC
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_LINH_VUC_KNTC,TEN_LINH_VUC_KNTC;
  END;*/
  ------------------------------------------------------------------------------
  PROCEDURE SELECT_LINH_VUC_KNTC(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LINH_VUC_KNTC,
             A.TEN_LINH_VUC_KNTC,
             A.STT_LINH_VUC_KNTC
      FROM DM_LINH_VUC_KNTC A
    
      UNION
      SELECT N'' AS MA_LINH_VUC_KNTC,
             P_TYPE AS TEN_LINH_VUC_KNTC,
             -1 AS STT_LINH_VUC_KNTC
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_LINH_VUC_KNTC,TEN_LINH_VUC_KNTC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_KET_QUA(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_KET_QUA,
             A.TEN_LOAI_KET_QUA,
             A.STT_LOAI_KET_QUA
      FROM DM_LOAI_KET_QUA A
      UNION 
      SELECT N'' AS MA_LOAI_KET_QUA,
             P_TYPE AS TEN_LOAI_KET_QUA,
             -1 AS STT_LOAI_KET_QUA
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_LOAI_KET_QUA,TEN_LOAI_KET_QUA;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_VAN_BAN(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_VAN_BAN,
             A.TEN_LOAI_VAN_BAN,
             A.STT_LOAI_VAN_BAN
      FROM DM_LOAI_VAN_BAN A
      UNION 
      SELECT N'' AS MA_LOAI_VAN_BAN,
             P_TYPE AS TEN_LOAI_VAN_BAN,
             -1 AS STT_LOAI_VAN_BAN
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_LOAI_VAN_BAN,TEN_LOAI_VAN_BAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TINH_THANH(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TINH_THANH,
             A.TEN_TINH_THANH,
             A.STT_TINH_THANH
      FROM DM_TINH_THANH A
      UNION 
      SELECT N'' AS MA_TINH_THANH,
             P_TYPE AS TEN_TINH_THANH,
             -1 AS STT_TINH_THANH
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_TINH_THANH,TEN_TINH_THANH;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUAN_HUYEN(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN,
             A.STT_QUAN_HUYEN
      FROM DM_QUAN_HUYEN A
      UNION 
      SELECT N'' AS MA_QUAN_HUYEN,
             P_TYPE AS TEN_QUAN_HUYEN,
             -1 AS STT_QUAN_HUYEN
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_QUAN_HUYEN,TEN_QUAN_HUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUAN_HUYEN_CUA_TINH(P_CUR IN OUT MYCUR, P_MA_TINH_THANH NVARCHAR2, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN,
             A.STT_QUAN_HUYEN
      FROM DM_QUAN_HUYEN A WHERE A.MA_TINH_THANH=P_MA_TINH_THANH
      UNION 
      SELECT N'' AS MA_QUAN_HUYEN,
             P_TYPE AS TEN_QUAN_HUYEN,
             -1 AS STT_QUAN_HUYEN
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_QUAN_HUYEN,TEN_QUAN_HUYEN;
  END;  
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_PHUONG_XA(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_PHUONG_XA,
             A.TEN_PHUONG_XA,
             A.STT_PHUONG_XA
      FROM DM_PHUONG_XA A
      UNION 
      SELECT N'' AS MA_PHUONG_XA,
             P_TYPE AS TEN_PHUONG_XA,
             -1 AS STT_PHUONG_XA
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_PHUONG_XA,TEN_PHUONG_XA;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_PHUONG_XA_CUA_HUYEN(P_CUR IN OUT MYCUR, P_MA_QUAN_HUYEN NVARCHAR2, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_PHUONG_XA,
             A.TEN_PHUONG_XA,
             A.STT_PHUONG_XA
      FROM DM_PHUONG_XA A WHERE A.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN
      UNION 
      SELECT N'' AS MA_PHUONG_XA,
             P_TYPE AS TEN_PHUONG_XA,
             -1 AS STT_PHUONG_XA
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_PHUONG_XA,TEN_PHUONG_XA;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_DTKNTC(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_DTKNTC,
             A.TEN_LOAI_DTKNTC,
             A.STT_LOAI_DTKNTC
      FROM DM_LOAI_DTKNTC A
      UNION 
      SELECT N'' AS MA_LOAI_DTKNTC,
             P_TYPE AS TEN_LOAI_DTKNTC,
             -1 AS STT_LOAI_DTKNTC
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_LOAI_DTKNTC,TEN_LOAI_DTKNTC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_DTBKNTC(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_DTBKNTC,
             A.TEN_LOAI_DTBKNTC,
             A.STT_LOAI_DTBKNTC
      FROM DM_LOAI_DTBKNTC A
      UNION 
      SELECT N'' AS MA_LOAI_DTBKNTC,
             P_TYPE AS TEN_LOAI_DTBKNTC,
             -1 AS STT_LOAI_DTBKNTC
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_LOAI_DTBKNTC,TEN_LOAI_DTBKNTC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_HUONG_GIAI_QUYET(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_HUONG_GIAI_QUYET,
             A.TEN_HUONG_GIAI_QUYET,
             A.STT_HUONG_GIAI_QUYET
      FROM DM_HUONG_GIAI_QUYET A
      UNION 
      SELECT N'' AS MA_HUONG_GIAI_QUYET,
             P_TYPE AS TEN_HUONG_GIAI_QUYET,
             -1 AS STT_HUONG_GIAI_QUYET
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_HUONG_GIAI_QUYET,TEN_HUONG_GIAI_QUYET;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TRANG_THAI_GIAI_QUYET(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TRANG_THAI_GIAI_QUYET,
             A.TEN_TRANG_THAI_GIAI_QUYET,
             A.STT_TRANG_THAI_GIAI_QUYET
      FROM DM_TRANG_THAI_GIAI_QUYET A
      UNION 
      SELECT N'' AS MA_TRANG_THAI_GIAI_QUYET,
             P_TYPE AS TEN_TRANG_THAI_GIAI_QUYET,
             -1 AS STT_TRANG_THAI_GIAI_QUYET
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_TRANG_THAI_GIAI_QUYET,TEN_TRANG_THAI_GIAI_QUYET;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CO_QUAN(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
             A.TEN_CO_QUAN,
             A.STT_CO_QUAN         
      FROM CB_CO_QUAN A
      UNION 
      SELECT N'' AS MA_CO_QUAN,
             P_TYPE AS TEN_CO_QUAN,
             -1 AS STT_CO_QUAN
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_CO_QUAN,TEN_CO_QUAN;
  END;
  --------------------------------------------------------------------------------
  PROCEDURE SELECT_CAN_BO(P_CUR IN OUT MYCUR, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CAN_BO,
             A.TEN_CAN_BO,
             A.STT_CAN_BO         
      FROM CB_CAN_BO A
      UNION 
      SELECT 0 AS MA_CAN_BO,
             P_TYPE AS TEN_CAN_BO,
             -1 AS STT_CAN_BO
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_CAN_BO,TEN_CAN_BO;
  END;
 
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DON_VI_CUA_CQ(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
      UNION 
      SELECT N'' AS MA_DON_VI,
             P_TYPE AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LANH_DAO_CUA_CQ(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2, P_TYPE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT DISTINCT
             A.MA_CAN_BO,
             A.TEN_CAN_BO,
             A.STT_CAN_BO
      FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B
      WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
        --AND A.MA_CAN_BO=B.MA_CAN_BO
       -- AND B.DEN_NGAY IS NULL
      UNION 
      SELECT 0 AS MA_CAN_BO,
             P_TYPE AS TEN_CAN_BO,
             -1 AS STT_CAN_BO
      FROM DUAL WHERE P_TYPE IS NOT NULL
       ORDER BY STT_CAN_BO,TEN_CAN_BO;
  END;         
end KNTC_SELECT;
/

prompt
prompt Creating package body LIB_DM
prompt ============================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.LIB_DM IS
 
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI
        FROM CB_DON_VI A
       ORDER BY A.STT_DON_VI,A.TEN_DON_VI;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A
      UNION 
      SELECT N'' AS MA_DON_VI,
             N'T?t c?' AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END;  
  

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A
      UNION 
      SELECT N'' AS MA_DON_VI,
             N'Ch?n don v?' AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END; 
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_phuongxa(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A WHERE A.MA_PHUONG_XA IS NOT NULL
      UNION 
      SELECT N'' AS MA_DON_VI,
             N'Ch?n don v?' AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_motcuaxa(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A WHERE A.MA_PHUONG_XA IS NOT NULL
      AND A.MA_LOAI_DON_VI LIKE 'BO_PHAN_TN_VA_TKQ_CAP_XA'
      UNION 
      SELECT N'' AS MA_DON_VI,
             N'Ch?n don v?' AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END;

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_motcuaxa2(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A WHERE A.MA_PHUONG_XA IS NOT NULL
      AND A.MA_LOAI_DON_VI LIKE 'BO_PHAN_TN_VA_TKQ_CAP_XA'
      UNION 
      SELECT N'TOAN_HUYEN' AS MA_DON_VI,
             N'Toàn huy?n' AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END;  
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_CUA_CQ_nothing(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END; 
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_CUA_CQ_tatca(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
      UNION 
      SELECT N'' AS MA_DON_VI,
             N'T?t c?' AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END; 
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_CUA_CQ_chon(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             A.STT_DON_VI
      FROM CB_DON_VI A WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
      UNION 
      SELECT N'' AS MA_DON_VI,
             N'Ch?n don v?' AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END;   
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVI_tiepnhan(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             1 AS STT_DON_VI
      FROM CB_DON_VI A 
      WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
      UNION 
      SELECT N'' AS MA_DON_VI,
             N'T?t c?' AS TEN_DON_VI,
             -1 AS STT_DON_VI
      FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI;
  END;   
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TOCHUYENMON_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TO_CHUYEN_MON,
             A.TEN_TO_CHUYEN_MON
        FROM CB_TO_CHUYEN_MON A
       ORDER BY A.STT_TO_CHUYEN_MON,A.TEN_TO_CHUYEN_MON;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TOCHUYENMON_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TO_CHUYEN_MON,
             A.TEN_TO_CHUYEN_MON,
             A.STT_TO_CHUYEN_MON
      FROM CB_TO_CHUYEN_MON A
      UNION 
      SELECT 0 AS MA_TO_CHUYEN_MON,
             N'T?t c?' AS TEN_TO_CHUYEN_MON,
             -1 AS STT_TO_CHUYEN_MON
      FROM DUAL
       ORDER BY STT_TO_CHUYEN_MON,TEN_TO_CHUYEN_MON;
  END;  
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TOCHUYENMON_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TO_CHUYEN_MON,
             A.TEN_TO_CHUYEN_MON,
             A.STT_TO_CHUYEN_MON
      FROM CB_TO_CHUYEN_MON A
      UNION 
      SELECT 0 AS MA_TO_CHUYEN_MON,
             N'Ch?n t? chuyên môn' AS TEN_TO_CHUYEN_MON,
             -1 AS STT_TO_CHUYEN_MON
      FROM DUAL
       ORDER BY STT_TO_CHUYEN_MON,TEN_TO_CHUYEN_MON;
  END;  
   -------------------------------------------------------------------------------
  PROCEDURE SELECT_TOCHUYENMON_null(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT 0 AS MA_TO_CHUYEN_MON,
             N'Ch?n t? chuyên môn' AS TEN_TO_CHUYEN_MON,
             -1 AS STT_TO_CHUYEN_MON
      FROM DUAL;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TOCHUYENMON_by_mdv(P_CUR IN OUT MYCUR,P_MA_DON_VI NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TO_CHUYEN_MON,
             A.TEN_TO_CHUYEN_MON,
             A.STT_TO_CHUYEN_MON
      FROM CB_TO_CHUYEN_MON A
      WHERE A.MA_DON_VI = P_MA_DON_VI
      UNION
      SELECT 0 AS MA_TO_CHUYEN_MON,
             N'Ch?n t? chuyên môn' AS TEN_TO_CHUYEN_MON,
             -1 AS STT_TO_CHUYEN_MON
      FROM DUAL
      ORDER BY STT_TO_CHUYEN_MON,TEN_TO_CHUYEN_MON;
  END;    
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DONVICHUQUAN_nothing(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI AS MA_DON_VI_CHU_QUAN,
             A.TEN_DON_VI AS TEN_DON_VI_CHU_QUAN
        FROM CB_DON_VI A
        WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
        --WHERE EXISTS (SELECT 1 FROM CB_DON_VI B WHERE B.MA_DON_VI_CHU_QUAN=A.MA_DON_VI)
       ORDER BY A.STT_DON_VI,TEN_DON_VI_CHU_QUAN;
  END;
 -------------------------------------------------------------------------------
   PROCEDURE SELECT_DONVICHUQUAN_tatca(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI AS MA_DON_VI_CHU_QUAN,
             A.TEN_DON_VI AS TEN_DON_VI_CHU_QUAN,
             A.STT_DON_VI
        FROM CB_DON_VI A
        WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
        --WHERE EXISTS (SELECT 1 FROM CB_DON_VI B WHERE B.MA_DON_VI_CHU_QUAN=A.MA_DON_VI)
      UNION 
      SELECT N'' AS MA_DON_VI_CHU_QUAN,
            N'T?t c?' AS TEN_DON_VI_CHU_QUAN,
            -1 AS STT_DON_VI
           FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI_CHU_QUAN;
  END; -------------------------------------------------------------------------------
   PROCEDURE SELECT_DONVICHUQUAN_chon(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DON_VI AS MA_DON_VI_CHU_QUAN,
             A.TEN_DON_VI AS TEN_DON_VI_CHU_QUAN,
             A.STT_DON_VI
        FROM CB_DON_VI A
        WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
        --WHERE EXISTS (SELECT 1 FROM CB_DON_VI B WHERE B.MA_DON_VI_CHU_QUAN=A.MA_DON_VI)
      UNION 
      SELECT N'' AS MA_DON_VI_CHU_QUAN,
            N'Ch?n don v? ch? qu?n' AS TEN_DON_VI_CHU_QUAN,
            -1 AS STT_DON_VI
           FROM DUAL
       ORDER BY STT_DON_VI,TEN_DON_VI_CHU_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_PHUONGXA_nothing(P_CUR IN OUT MYCUR, P_MA_QUAN_HUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_PHUONG_XA,
             A.TEN_PHUONG_XA
        FROM DM_PHUONG_XA A
        WHERE A.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN
       ORDER BY A.STT_PHUONG_XA,A.TEN_PHUONG_XA;
  END;
 -------------------------------------------------------------------------------
  PROCEDURE SELECT_PHUONGXA_NULL(P_CUR IN OUT MYCUR, P_MA_QUAN_HUYEN NVARCHAR2) AS
 V_KT INT;
  V_MA_QUAN_HUYEN NVARCHAR2(100);
  BEGIN
        SELECT COUNT(1) INTO V_KT FROM DM_QUAN_HUYEN C 
    WHERE  C.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN;
    IF V_KT=0 THEN
      V_MA_QUAN_HUYEN:='';
     ELSE
      V_MA_QUAN_HUYEN:=P_MA_QUAN_HUYEN;
    END IF;
    IF V_MA_QUAN_HUYEN IS NOT NULL THEN

    OPEN P_CUR FOR
      SELECT A.MA_PHUONG_XA,
             A.TEN_PHUONG_XA
        FROM DM_PHUONG_XA A
        WHERE A.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN
       ORDER BY A.STT_PHUONG_XA,A.TEN_PHUONG_XA;
       ELSE
           OPEN P_CUR FOR
      SELECT A.MA_PHUONG_XA,
             A.TEN_PHUONG_XA
        FROM DM_PHUONG_XA A
        WHERE A.MA_QUAN_HUYEN IN (SELECT E.MA_QUAN_HUYEN FROM DM_QUAN_HUYEN E WHERE ROWNUM=1 )
       ORDER BY A.STT_PHUONG_XA,A.TEN_PHUONG_XA;
       END IF;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_PHUONGXA_tatca(P_CUR IN OUT MYCUR, P_MA_QUAN_HUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_PHUONG_XA,
             A.TEN_PHUONG_XA,
             A.STT_PHUONG_XA
        FROM DM_PHUONG_XA A
        WHERE A.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN
      UNION 
      SELECT N'' AS MA_PHUONG_XA,
            N'T?t c?' AS TEN_PHUONG_XA,
            -1 AS STT_DON_VI
           FROM DUAL
       ORDER BY STT_PHUONG_XA,TEN_PHUONG_XA;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_PHUONGXA_chon(P_CUR IN OUT MYCUR, P_MA_QUAN_HUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_PHUONG_XA,
             A.TEN_PHUONG_XA,
             A.STT_PHUONG_XA
        FROM DM_PHUONG_XA A
        WHERE A.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN
      UNION 
      SELECT N'' AS MA_PHUONG_XA,
            N'Ch?n phu?ng xã' AS TEN_PHUONG_XA,
            -1 AS STT_DON_VI
           FROM DUAL
       ORDER BY STT_PHUONG_XA,TEN_PHUONG_XA;
  END;  
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAIDONVI_nothing(P_CUR IN OUT MYCUR, P_MA_LOAI_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_DON_VI,
             A.TEN_LOAI_DON_VI
        FROM CB_LOAI_DON_VI A
        WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
       ORDER BY A.TEN_LOAI_DON_VI;
  END;
     -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAIDONVI_nothing_cuacq(P_CUR IN OUT MYCUR, P_MA_LOAI_CO_QUAN NVARCHAR2, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT DISTINCT A.MA_LOAI_DON_VI,
             A.TEN_LOAI_DON_VI,
             100 AS STT_LOAI_DON_VI
        FROM CB_LOAI_DON_VI A, CB_DON_VI B
        WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
        AND A.MA_LOAI_DON_VI = B.MA_LOAI_DON_VI
        AND B.MA_CO_QUAN=P_MA_CO_QUAN
       ORDER BY STT_LOAI_DON_VI,TEN_LOAI_DON_VI;
  END;
    -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAIDONVI_tatca(P_CUR IN OUT MYCUR, P_MA_LOAI_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_DON_VI,
             A.TEN_LOAI_DON_VI,
             100 AS STT_LOAI_DON_VI
        FROM CB_LOAI_DON_VI A
        WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
      UNION 
      SELECT N'' AS MA_LOAI_DON_VI,
            N'T?t c?' AS TEN_LOAI_DON_VI,
            -1 AS STT_LOAI_DON_VI
           FROM DUAL
       ORDER BY STT_LOAI_DON_VI,TEN_LOAI_DON_VI;
  END;
    -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAIDONVI_tatca_cuacq(P_CUR IN OUT MYCUR, P_MA_LOAI_CO_QUAN NVARCHAR2, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_DON_VI,
             A.TEN_LOAI_DON_VI,
             100 AS STT_LOAI_DON_VI
        FROM CB_LOAI_DON_VI A, CB_DON_VI B
        WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
        AND A.MA_LOAI_DON_VI = B.MA_LOAI_DON_VI
        AND B.MA_CO_QUAN=P_MA_CO_QUAN
      UNION 
      SELECT N'' AS MA_LOAI_DON_VI,
            N'T?t c?' AS TEN_LOAI_DON_VI,
            -1 AS STT_LOAI_DON_VI
           FROM DUAL
       ORDER BY STT_LOAI_DON_VI,TEN_LOAI_DON_VI;
  END;

 
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAIDONVI_chon(P_CUR IN OUT MYCUR, P_MA_LOAI_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_DON_VI,
             A.TEN_LOAI_DON_VI,
             100 AS STT_LOAI_DON_VI
        FROM CB_LOAI_DON_VI A
        WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
      UNION 
      SELECT N'' AS MA_LOAI_DON_VI,
            N'Ch?n lo?i don v?' AS TEN_LOAI_DON_VI,
            -1 AS STT_LOAI_DON_VI
           FROM DUAL
       ORDER BY STT_LOAI_DON_VI,TEN_LOAI_DON_VI;
  END;
     -------------------------------------------------------------------------------
   PROCEDURE SELECT_LOAIDONVI_chon_cuacq(P_CUR IN OUT MYCUR, P_MA_LOAI_CO_QUAN NVARCHAR2, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT DISTINCT A.MA_LOAI_DON_VI,
             A.TEN_LOAI_DON_VI,
             100 AS STT_LOAI_DON_VI
        FROM CB_LOAI_DON_VI A, CB_DON_VI B
        WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
        AND A.MA_LOAI_DON_VI = B.MA_LOAI_DON_VI
        AND B.MA_CO_QUAN=P_MA_CO_QUAN
       UNION 
      SELECT N'' AS MA_LINH_VUC_THU_TUC,
             N'Ch?n lo?i don v?' AS TEN_LINH_VUC_THU_TUC,
             -1 AS STT_LOAI_DON_VI
      FROM DUAL 
       ORDER BY STT_LOAI_DON_VI,TEN_LOAI_DON_VI;
  END; 
 
 -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAINHOMNSD_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD
        FROM CB_LOAI_NHOM_NSD A
       ORDER BY A.STT_LOAI_NHOM_NSD,A.TEN_LOAI_NHOM_NSD;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAINHOMNSD_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD,
             A.STT_LOAI_NHOM_NSD
        FROM CB_LOAI_NHOM_NSD A
      UNION 
      SELECT N'' AS MA_LOAI_NHOM_NSD,
            N'T?t c?' AS TEN_LOAI_NHOM_NSD,
            -1 AS STT_LOAI_NHOM_NSD
           FROM DUAL
       ORDER BY STT_LOAI_NHOM_NSD,TEN_LOAI_NHOM_NSD;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAINHOMNSD_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD,
             A.STT_LOAI_NHOM_NSD
        FROM CB_LOAI_NHOM_NSD A
      UNION 
      SELECT N'' AS MA_LOAI_NHOM_NSD,
            N'Ch?n nhóm NSD' AS TEN_LOAI_NHOM_NSD,
            -1 AS STT_LOAI_NHOM_NSD
           FROM DUAL
       ORDER BY STT_LOAI_NHOM_NSD,TEN_LOAI_NHOM_NSD;
  END;
  -------------------------------------------------------------------------------
  --khong the su dung ham nay khi P_MA_TO_CHUYEN_MON=0
  PROCEDURE SELECT_LOAINHOMNSD_CBX_BY_MDV(P_CUR IN OUT MYCUR,P_MA_TO_CHUYEN_MON INT,P_MA_DON_VI NVARCHAR2) AS
  V_MA_LOAI_DON_VI NVARCHAR2(100);
  V_MA_LOAI_NHOM_NSD NVARCHAR2(100);
  BEGIN
   SELECT MA_LOAI_DON_VI INTO V_MA_LOAI_DON_VI FROM CB_DON_VI WHERE MA_DON_VI LIKE P_MA_DON_VI;
   
   IF P_MA_TO_CHUYEN_MON =0 THEN
     
      OPEN P_CUR FOR
      SELECT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD,
             A.STT_LOAI_NHOM_NSD,
             0 AS SELECTED
        FROM CB_LOAI_NHOM_NSD A
        WHERE UPPER('%'||NVL(A.MA_LOAI_DON_VI,'')||'%')=UPPER('%'||NVL(V_MA_LOAI_DON_VI,'')||'%')
       ORDER BY STT_LOAI_NHOM_NSD,TEN_LOAI_NHOM_NSD;
   
   
    ELSE
    SELECT MA_LOAI_NHOM_NSD INTO V_MA_LOAI_NHOM_NSD FROM CB_TO_CHUYEN_MON WHERE MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON;  
    OPEN P_CUR FOR
      SELECT DISTINCT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD,
             A.STT_LOAI_NHOM_NSD,
             CASE WHEN B.MA_LOAI_NHOM_NSD LIKE V_MA_LOAI_NHOM_NSD THEN 1 ELSE 0 END AS SELECTED
        FROM CB_LOAI_NHOM_NSD A, CB_TO_CHUYEN_MON B
        WHERE UPPER('%'||NVL(A.MA_LOAI_DON_VI,'')||'%')=UPPER('%'||NVL(V_MA_LOAI_DON_VI,'')||'%')
        AND A.MA_LOAI_NHOM_NSD=B.MA_LOAI_NHOM_NSD(+)
       ORDER BY STT_LOAI_NHOM_NSD,TEN_LOAI_NHOM_NSD;
   
    END IF;   
  END;  
  -------------------------------------------------------------------------------
  --khong the su dung ham nay khi P_MA_TO_CHUYEN_MON=0
  PROCEDURE SELECT_LOAINHOMNSD_CBX_BY_MT(P_CUR IN OUT MYCUR,P_MA_TO_CHUYEN_MON INT) AS
  V_MA_LOAI_DON_VI NVARCHAR2(100);
  V_MA_LOAI_NHOM_NSD NVARCHAR2(100);
  V_MA_DON_VI NVARCHAR2(100);
  BEGIN
   SELECT MA_DON_VI INTO V_MA_DON_VI FROM CB_TO_CHUYEN_MON WHERE MA_TO_CHUYEN_MON LIKE P_MA_TO_CHUYEN_MON;
   SELECT MA_LOAI_DON_VI INTO V_MA_LOAI_DON_VI FROM CB_DON_VI WHERE MA_DON_VI LIKE V_MA_DON_VI;
   IF P_MA_TO_CHUYEN_MON =0 THEN
      OPEN P_CUR FOR
      SELECT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD,
             A.STT_LOAI_NHOM_NSD,
             0 AS SELECTED
        FROM CB_LOAI_NHOM_NSD A
        WHERE UPPER('%'||NVL(A.MA_LOAI_DON_VI,'')||'%')=UPPER('%'||NVL(V_MA_LOAI_DON_VI,'')||'%')
       ORDER BY STT_LOAI_NHOM_NSD,TEN_LOAI_NHOM_NSD;
   
    ELSE
    SELECT MA_LOAI_NHOM_NSD INTO V_MA_LOAI_NHOM_NSD FROM CB_TO_CHUYEN_MON WHERE MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON;  
    OPEN P_CUR FOR
      SELECT DISTINCT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD,
             A.STT_LOAI_NHOM_NSD,
             CASE WHEN B.MA_LOAI_NHOM_NSD LIKE V_MA_LOAI_NHOM_NSD THEN 1 ELSE 0 END AS SELECTED
        FROM CB_LOAI_NHOM_NSD A, CB_TO_CHUYEN_MON B
        WHERE UPPER('%'||NVL(A.MA_LOAI_DON_VI,'')||'%')=UPPER('%'||NVL(V_MA_LOAI_DON_VI,'')||'%')
        AND A.MA_LOAI_NHOM_NSD=B.MA_LOAI_NHOM_NSD(+)
       ORDER BY STT_LOAI_NHOM_NSD,TEN_LOAI_NHOM_NSD;
   
    END IF;   
  END;  
    -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAINHOMNSD_BY_MLDV(P_CUR IN OUT MYCUR,P_MA_LOAI_DON_VI NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD
        FROM CB_LOAI_NHOM_NSD A
        WHERE A.MA_LOAI_DON_VI LIKE P_MA_LOAI_DON_VI
       ORDER BY A.STT_LOAI_NHOM_NSD,A.TEN_LOAI_NHOM_NSD;
  END;
    -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAINHOMNSD_BY_MLDVchon(P_CUR IN OUT MYCUR,P_MA_LOAI_DON_VI NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD,
             A.STT_LOAI_NHOM_NSD
        FROM CB_LOAI_NHOM_NSD A
        WHERE A.MA_LOAI_DON_VI LIKE P_MA_LOAI_DON_VI
      UNION 
      SELECT N'' AS MA_LOAI_NHOM_NSD,
            N'Ch?n b? ph?n xác nh?n' AS TEN_LOAI_NHOM_NSD,
            -1 AS STT_LOAI_NHOM_NSD
           FROM DUAL
       ORDER BY STT_LOAI_NHOM_NSD,TEN_LOAI_NHOM_NSD;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_MENU_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_MENU,
             A.TEN_MENU
        FROM PQ_MENU A
       ORDER BY A.STT_MENU,A.TEN_MENU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_MENU_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_MENU,
             A.TEN_MENU,
             A.STT_MENU
        FROM PQ_MENU A
      UNION 
      SELECT N'' AS MA_MENU,
            N'T?t c?' AS TEN_MENU,
            -1 AS STT_MENU
           FROM DUAL
       ORDER BY STT_MENU,TEN_MENU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_MENU_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_MENU,
             A.TEN_MENU,
             A.STT_MENU
        FROM PQ_MENU A
      UNION 
      SELECT N'' AS MA_MENU,
            N'Ch?n menu' AS TEN_MENU,
            -1 AS STT_MENU
           FROM DUAL
       ORDER BY STT_MENU,TEN_MENU;
  END; 
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUYEN_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUYEN,
             A.TEN_QUYEN
        FROM PQ_QUYEN A
       ORDER BY A.STT_QUYEN,A.TEN_QUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUYEN_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUYEN,
             A.TEN_QUYEN,
             A.STT_QUYEN
        FROM PQ_QUYEN A
      UNION 
      SELECT N'' AS MA_QUYEN,
            N'T?t c?' AS TEN_QUYEN,
            -1 AS STT_QUYEN
           FROM DUAL
       ORDER BY STT_QUYEN,TEN_QUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUYEN_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUYEN,
             A.TEN_QUYEN,
             A.STT_QUYEN
        FROM PQ_QUYEN A
      UNION 
      SELECT N'' AS MA_QUYEN,
            N'Ch?n quy?n' AS TEN_QUYEN,
            -1 AS STT_QUYEN
           FROM DUAL
       ORDER BY STT_QUYEN,TEN_QUYEN;
  END;   

-------------------------------------------------------------------------------
  PROCEDURE SELECT_CHUCVU_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CHUC_VU,
             A.TEN_CHUC_VU
        FROM DM_CHUC_VU A
       ORDER BY A.STT_CHUC_VU,A.TEN_CHUC_VU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CHUCVU_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CHUC_VU,
             A.TEN_CHUC_VU,
             A.STT_CHUC_VU
        FROM DM_CHUC_VU A
      UNION 
      SELECT N'' AS MA_CHUC_VU,
            N'T?t c?' AS TEN_CHUC_VU,
            -1 AS STT_CHUC_VU
           FROM DUAL
       ORDER BY STT_CHUC_VU,TEN_CHUC_VU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CHUCVU_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CHUC_VU,
             A.TEN_CHUC_VU,
             A.STT_CHUC_VU
        FROM DM_CHUC_VU A
      UNION 
      SELECT N'' AS MA_CHUC_VU,
            N'Ch?n ch?c v?' AS TEN_CHUC_VU,
            -1 AS STT_CHUC_VU
           FROM DUAL
       ORDER BY STT_CHUC_VU,TEN_CHUC_VU;
  END;
-------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAICOQUAN_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_CO_QUAN,
             A.TEN_LOAI_CO_QUAN
        FROM CB_LOAI_CO_QUAN A
       ORDER BY A.STT_LOAI_CO_QUAN,A.TEN_LOAI_CO_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAICOQUAN_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_CO_QUAN,
             A.TEN_LOAI_CO_QUAN,
             A.STT_LOAI_CO_QUAN
        FROM CB_LOAI_CO_QUAN A
      UNION 
      SELECT N'' AS MA_LOAI_CO_QUAN,
            N'T?t c?' AS TEN_LOAI_CO_QUAN,
            -1 AS STT_LOAI_CO_QUAN
           FROM DUAL
       ORDER BY STT_LOAI_CO_QUAN,TEN_LOAI_CO_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAICOQUAN_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_CO_QUAN,
             A.TEN_LOAI_CO_QUAN,
             A.STT_LOAI_CO_QUAN
        FROM CB_LOAI_CO_QUAN A
      UNION 
      SELECT N'' AS MA_LOAI_CO_QUAN,
            N'Ch?n lo?i co quan' AS TEN_LOAI_CO_QUAN,
            -1 AS STT_LOAI_CO_QUAN
           FROM DUAL
       ORDER BY STT_LOAI_CO_QUAN,TEN_LOAI_CO_QUAN;
  END; 
  ---------------------------------------------
  PROCEDURE SELECT_UBND_HUYEN(P_CUR IN OUT MYCUR) AS
    BEGIN 
      OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
      A.TEN_CO_QUAN,A.STT_CO_QUAN
      FROM CB_CO_QUAN A
      WHERE A.MA_LOAI_CO_QUAN='UBND_HUYEN'
      ORDER BY A.STT_CO_QUAN,A.TEN_CO_QUAN;
    END;
-------------------------------------------------------------------------------
  PROCEDURE SELECT_COQUAN_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
             A.TEN_CO_QUAN
        FROM CB_CO_QUAN A
       ORDER BY A.TEN_CO_QUAN;
  END;
-------------------------------------------------------------------------------
  PROCEDURE SELECT_COQUAN_nothing_desc(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
             A.TEN_CO_QUAN
        FROM CB_CO_QUAN A
       ORDER BY A.TEN_CO_QUAN DESC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_COQUAN_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
             A.TEN_CO_QUAN,
             A.STT_CO_QUAN
        FROM CB_CO_QUAN A
      UNION 
      SELECT N'' AS MA_CO_QUAN,
            N'T?t c?' AS TEN_CO_QUAN,
            -1 AS STT_CO_QUAN
           FROM DUAL
       ORDER BY STT_CO_QUAN,TEN_CO_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_COQUAN_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
             A.TEN_CO_QUAN,
             A.STT_CO_QUAN
        FROM CB_CO_QUAN A
      UNION 
      SELECT N'' AS MA_CO_QUAN,
            N'Ch?n co quan' AS TEN_CO_QUAN,
            -1 AS STT_CO_QUAN
           FROM DUAL
       ORDER BY STT_CO_QUAN,TEN_CO_QUAN;
  END;        
-------------------------------------------------------------------------------
  PROCEDURE SELECT_COQUAN_BY_LCQ_nothing(P_CUR IN OUT MYCUR,P_MA_LOAI_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
             A.TEN_CO_QUAN
        FROM CB_CO_QUAN A WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
       ORDER BY A.STT_CO_QUAN,A.TEN_CO_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_COQUAN_BY_LCQ_tatca(P_CUR IN OUT MYCUR,P_MA_LOAI_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
             A.TEN_CO_QUAN,
             A.STT_CO_QUAN
        FROM CB_CO_QUAN A WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
      UNION 
      SELECT N'' AS MA_CO_QUAN,
            N'T?t c?' AS TEN_CO_QUAN,
            -1 AS STT_CO_QUAN
           FROM DUAL
       ORDER BY STT_CO_QUAN,TEN_CO_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_COQUAN_BY_LCQ_chon(P_CUR IN OUT MYCUR,P_MA_LOAI_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CO_QUAN,
             A.TEN_CO_QUAN,
             A.STT_CO_QUAN
        FROM CB_CO_QUAN A WHERE A.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN
      UNION 
      SELECT N'' AS MA_CO_QUAN,
            N'Ch?n co quan' AS TEN_CO_QUAN,
            -1 AS STT_CO_QUAN
           FROM DUAL
       ORDER BY STT_CO_QUAN,TEN_CO_QUAN;
  END;        

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUANHUYEN_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN
        FROM DM_QUAN_HUYEN A
       ORDER BY A.STT_QUAN_HUYEN,A.TEN_QUAN_HUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUANHUYEN_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN,
             A.STT_QUAN_HUYEN
        FROM DM_QUAN_HUYEN A
      UNION 
      SELECT N'' AS MA_QUAN_HUYEN,
            N'T?t c?' AS TEN_QUAN_HUYEN,
            -1 AS STT_QUAN_HUYEN
           FROM DUAL
       ORDER BY STT_QUAN_HUYEN,TEN_QUAN_HUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUANHUYEN_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN,
             A.STT_QUAN_HUYEN
        FROM DM_QUAN_HUYEN A
      UNION 
      SELECT N'' AS MA_QUAN_HUYEN,
            N'Ch?n qu?n huy?n' AS TEN_QUAN_HUYEN,
            -1 AS STT_QUAN_HUYEN
           FROM DUAL
       ORDER BY STT_QUAN_HUYEN,TEN_QUAN_HUYEN;
  END;
PROCEDURE SELECT_QUANHUYEN_Select(P_CUR IN OUT MYCUR, P_MA_TINH_THANH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN,
             A.STT_QUAN_HUYEN
        FROM DM_QUAN_HUYEN A
        WHERE  A.MA_TINH_THANH=P_MA_TINH_THANH
      UNION 
      SELECT N'' AS MA_QUAN_HUYEN,
            N'T?t c?' AS TEN_QUAN_HUYEN,
            -1 AS STT_QUAN_HUYEN
           FROM DUAL
       ORDER BY STT_QUAN_HUYEN,TEN_QUAN_HUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUANHUYEN_tinh(P_CUR IN OUT MYCUR, P_MA_TINH_THANH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN
        FROM DM_QUAN_HUYEN A
        WHERE A.MA_TINH_THANH=P_MA_TINH_THANH;
       --ORDER BY A.STT_QUAN_HUYEN,A.TEN_QUAN_HUYEN;
  END; 
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUANHUYEN_ORDER(P_CUR IN OUT MYCUR, P_MA_TINH_THANH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN
        FROM DM_QUAN_HUYEN A
        WHERE A.MA_TINH_THANH=P_MA_TINH_THANH
      ORDER BY MA_QUAN_HUYEN;
  END; 
   -------------------------------------------------------------------------------
  PROCEDURE SELECT_TINHTHANH_nothing(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TINH_THANH,
             A.TEN_TINH_THANH
        FROM DM_TINH_THANH A
       ORDER BY A.TEN_TINH_THANH;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TINHTHANH_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TINH_THANH,
             A.TEN_TINH_THANH,
             A.STT_TINH_THANH
        FROM DM_TINH_THANH A
      UNION 
      SELECT N'' AS MA_TINH_THANH,
            N'T?tt c?' AS TEN_TINH_THANH,
            -1 AS STT_TINH_THANH
           FROM DUAL
       ORDER BY TEN_TINH_THANH;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TINHTHANH_chon(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TINH_THANH,
             A.TEN_TINH_THANH,
             A.STT_TINH_THANH
        FROM DM_TINH_THANH A
      UNION 
      SELECT N'' AS MA_TINH_THANH,
            N'Ch?n t?nh thành' AS TEN_TINH_THANH,
            -1 AS STT_TINH_THANH
           FROM DUAL
       ORDER BY TEN_TINH_THANH;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TRANG_THAI_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_TRANG_THAI, A.TEN_TRANG_THAI
    FROM QLDC_TRANG_THAI_HO_SO A order by a.ma_trang_thai;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_HINH_THUC_SU_DUNG_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_HTSDD, A.TEN_HTSDD
    FROM QLDC_HINH_THUC_SU_DUNG_DAT A order by a.ma_htsdd;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DOI_TUONG_SU_DUNG_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT * FROM (
              SELECT T.MA_DT, T.TEN_DVSD 
              FROM qldc_doi_tuong_su_dung T 
              WHERE T.MA_DT=0
              ORDER BY T.MA_DT DESC)
    UNION ALL
    SELECT * FROM (
           SELECT A.MA_DT, A.TEN_DVSD || ' - ' || A.DIA_CHI AS TEN_DVSD
           FROM qldc_doi_tuong_su_dung A 
           where a.ma_update is not null
           ORDER BY A.MA_UPDATE DESC, A.MA_DT DESC
                  )
    WHERE ROWNUM <=5
    UNION ALL 
    SELECT * FROM (
              SELECT T.MA_DT, T.TEN_DVSD || ' - ' || T.DIA_CHI AS TEN_DVSD
              FROM qldc_doi_tuong_su_dung T 
              ORDER BY T.MA_DT DESC);
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_MUC_DICH_SU_DUNG_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_MUC_DICH, A.TEN_MUC_DICH ||' - ' || A.TEN_TAT || '' AS TEN_MUC_DICH
    FROM QLDC_MUC_DICH_SU_DUNG_DAT A 
    order by a.ten_muc_dich desc;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_HTTT_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_HTTT, A.TEN_HTTT
    FROM QLDC_HINH_THUC_THANH_TOAN A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_PTTT_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_PTTT, A.TEN_PTTT
    FROM QLDC_PHUONG_THUC_THANH_TOAN A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_HINH_DTSD_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_LOAI_HINH, A.TEN_LOAI_HINH
    FROM QLDC_DTSD_LOAI_HINH A order by a.ma_loai_hinh;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_DTSD_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_LOAI_DTSD, A.TEN_LOAI_DTSD
    FROM QLDC_DTSD_LOAI A order by a.ma_loai_dtsd;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_MOC_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_LOAI, a.ten_loai
        FROM BD_MOCTOADO_LOAI A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_KHU_VUC_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_KHUVUC AS MA, a.Ten_Khuvuc AS TEN
        FROM BD_KHUVUC A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_BD_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_BANDO_LOAI AS MA, a.ten_bando_loai AS TEN
        FROM BD_BANDO_LOAI A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_BDCD_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_BANDO_CD_LOAI AS MA, a.ten_bando_cd_loai AS TEN
        FROM BD_BANDO_CD_LOAI A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_BIEN_DONG_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_BIEN_DONG AS MA, a.Ten_Bien_Dong AS TEN
        FROM QLDC_BIEN_DONG A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DAT_CONG_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_DAT_CONG AS MA, a.Ten_Dat_Cong AS TEN
        FROM QLDC_DAT_CONG A ORDER BY A.MA_DAT_CONG DESC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_KQXD_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_KQXD AS MA, a.Ten_Kqxd AS TEN
        FROM QLDC_KET_QUA_XET_DUYET A order by a.ma_kqxd desc;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DKDKK_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_DKDKK	 AS MA, a.Ten_Dkdkk AS TEN
        FROM QLDC_DANG_KY_DA_KE_KHAI A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LUY_TIEN_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_LUY_TIEN	 AS MA, a.Ten_Luy_Tien AS TEN
        FROM QLDC_LUY_TIEN A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_NGUOI_QL_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_NGUOI_QL	 AS MA, a.ten_nguoi_ql AS TEN
        FROM QLDC_NGUOI_QL A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CAP_QL_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_CAP_QL	 AS MA, a.teN_CAP_ql AS TEN
        FROM QLDC_CAP_QL A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TRANG_THAI_PLUS_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_TRANG_THAI_PLUS	 AS MA, a.ten_trang_thai_plus AS TEN
        FROM QLDC_TRANG_THAI_PLUS A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LH_TRA_TIEN_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_LOAI_HINH_TRA_TIEN	 AS MA, a.ten_loai_hinh_tra_tien AS TEN
        FROM QLDC_LOAI_HINH_TRA_TIEN A;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_VB_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_LOAI_VAN_BAN	 AS MA, a.Ten_Loai_Van_Ban AS TEN
        FROM DM_LOAI_VAN_BAN A;
  END;
    -------------------------------------------------------------------------------
  PROCEDURE SELECT_TRANGTHAI_VB_tatca(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_DK_TRANGTHAI AS MA, a.Ten_Dk_Trangthai AS TEN
        FROM QLDC_DK_TRANGTHAI A;
  END;
  -------------------------------------------------------------------------------
  END LIB_DM;
/

prompt
prompt Creating package body LIB_FUN
prompt =============================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.LIB_FUN IS
 
   -------------------------------------------------------------------------------
  FUNCTION GET_SO_LUONG_CBHT_CUA_DV(P_MA_DON_VI NVARCHAR2) RETURN INT 
  AS RS INT;
  
  BEGIN
    SELECT COUNT(1) INTO RS FROM CB_TO_CHUYEN_MON B, 
                                 CB_THUOC_TO_CHUYEN_MON C
                           WHERE B.MA_DON_VI = P_MA_DON_VI
                           AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
                           AND C.DEN_NGAY IS NULL;
    RETURN RS;
  END; 
     -------------------------------------------------------------------------------
  FUNCTION GET_SO_LUONG_TO_CUA_DV(P_MA_DON_VI NVARCHAR2) RETURN INT 
  AS RS INT;
  
  BEGIN
    SELECT COUNT(DISTINCT B.MA_TO_CHUYEN_MON) INTO RS FROM CB_TO_CHUYEN_MON B, 
                                 CB_THUOC_TO_CHUYEN_MON C
                           WHERE B.MA_DON_VI = P_MA_DON_VI
                           AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
                           AND C.DEN_NGAY IS NULL;
    RETURN RS;
  END; 
    -------------------------------------------------------------------------------
  FUNCTION GET_SO_LUONG_CBHT_CUA_TO(P_MA_TO_CHUYEN_MON INT) RETURN INT 
  AS RS INT;
  BEGIN
    SELECT COUNT(1) INTO RS FROM CB_THUOC_TO_CHUYEN_MON A
                           WHERE A.MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON
                           AND A.DEN_NGAY IS NULL;
    RETURN RS;
  END; 

  END LIB_FUN;
/

prompt
prompt Creating package body LIB_NOTABLE
prompt =================================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.LIB_NOTABLE IS
 /********* BEGIN: HOC **********/
  
  FUNCTION GET_TENTRANGTHAI_HS(
    P_TRANG_THAI NVARCHAR2)
    RETURN NVARCHAR2
  IS
    V_TEN_TRANG_THAI NVARCHAR2(200);
  BEGIN
    SELECT CASE  
      WHEN P_TRANG_THAI = 'LUU_NHUNG_KHONG_NOP'  THEN 'Lưu nhưng không nộp' 
      WHEN P_TRANG_THAI = 'DA_NOP'  THEN 'Đã nộp - Chưa tiếp nhận'     
      WHEN P_TRANG_THAI = 'DANG_CAP_NHAT' THEN 'Đang cập nhật'
      WHEN P_TRANG_THAI = 'DANG_TIEP_NHAN' THEN 'Đang tiếp nhận'
      WHEN P_TRANG_THAI = 'DA_HUY' THEN 'Đã hủy'
      WHEN P_TRANG_THAI = 'CHO_BO_SUNG' THEN 'Chờ bổ sung'
      WHEN P_TRANG_THAI = 'DA_TIEP_NHAN' THEN 'Đã tiếp nhận'
      WHEN P_TRANG_THAI = 'DA_BO_SUNG_VA_NOP_LAI'  THEN 'Đã bổ sung và nộp lại' END
    INTO V_TEN_TRANG_THAI
    FROM DUAL;
    RETURN V_TEN_TRANG_THAI;
  END;
  
  --FUNC: IS_NUMBERIC
  FUNCTION IS_NUMBERIC(
    P_STR NVARCHAR2)
    RETURN NUMBER
  IS
  BEGIN
    RETURN LENGTH(TRIM(TRANSLATE(P_STR, ' +-.0123456789', ' ')));
  END;
    
  /********* END: HOC **********/
 -------------------------------------------------------------------------------
  FUNCTION IS_NUMBER(P_STRING IN VARCHAR2) RETURN INT
  IS V_STRING NVARCHAR2(200);
     V_NEW_NUM NUMBER;
  BEGIN
      V_STRING:= REPLACE(P_STRING,'-','S');
      V_STRING:= REPLACE(V_STRING,'+','S');
      V_STRING:= REPLACE(V_STRING,'.','S');
      V_STRING:= REPLACE(V_STRING,' ','S');
      V_NEW_NUM := TO_NUMBER(p_string);
     RETURN 1;
  EXCEPTION WHEN VALUE_ERROR THEN
     RETURN 0;
  END;
 -------------------------------------------------------------------------------
 FUNCTION REPLACE_BAD_CHAR (P_CHUOI NVARCHAR2) RETURN NVARCHAR2
  AS
         RS_STR NVARCHAR2(2000);
  BEGIN 
                RS_STR:=TRIM(P_CHUOI);   
                RS_STR:=REPLACE(RS_STR, '&amp;', '&'); 
                RS_STR:=REPLACE(RS_STR, '&lt;', '<');
                RS_STR:=REPLACE(RS_STR, '&gt;', '>');
                RS_STR:=REPLACE(RS_STR, '&#34;', '"');
                RS_STR:=REPLACE(RS_STR, '&#39;', '''');
                
                RETURN RS_STR;
  END; 
  -------------------------------------------------------------------------------
 FUNCTION LOC_DAU (P_CHUOI NVARCHAR2) RETURN NVARCHAR2
  AS
         RS_STR NVARCHAR2(2000);
  BEGIN 
                RS_STR:=TRIM(P_CHUOI);   
                RS_STR:=REPLACE(RS_STR, 'A', 'A'); 
                RS_STR:=REPLACE(RS_STR, 'a', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ă', 'A');
                RS_STR:=REPLACE(RS_STR, 'ă', 'a');
                RS_STR:=REPLACE(RS_STR, 'Â', 'A');
                RS_STR:=REPLACE(RS_STR, 'â', 'a');
                RS_STR:=REPLACE(RS_STR, 'E', 'E');
                RS_STR:=REPLACE(RS_STR, 'e', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ê', 'E');
                RS_STR:=REPLACE(RS_STR, 'ê', 'e');
                RS_STR:=REPLACE(RS_STR, 'I', 'I');
                RS_STR:=REPLACE(RS_STR, 'i', 'i');
                RS_STR:=REPLACE(RS_STR, 'O', 'O');
                RS_STR:=REPLACE(RS_STR, 'o', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ô', 'O');
                RS_STR:=REPLACE(RS_STR, 'ô', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ơ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ơ', 'o');
                RS_STR:=REPLACE(RS_STR, 'U', 'U');
                RS_STR:=REPLACE(RS_STR, 'u', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ư', 'U');
                RS_STR:=REPLACE(RS_STR, 'ư', 'u');
                RS_STR:=REPLACE(RS_STR, 'Y', 'Y');
                RS_STR:=REPLACE(RS_STR, 'y', 'y');

              
                RS_STR:=REPLACE(RS_STR, 'À', 'A');
                RS_STR:=REPLACE(RS_STR, 'à', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ằ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ằ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ầ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ầ', 'a');
                RS_STR:=REPLACE(RS_STR, 'È', 'E');
                RS_STR:=REPLACE(RS_STR, 'è', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ề', 'E');
                RS_STR:=REPLACE(RS_STR, 'ề', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ì', 'I');
                RS_STR:=REPLACE(RS_STR, 'ì', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ò', 'O');
                RS_STR:=REPLACE(RS_STR, 'ò', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ồ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ồ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ờ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ờ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ù', 'U');
                RS_STR:=REPLACE(RS_STR, 'ù', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ừ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ừ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ỳ', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ỳ', 'y');

              
                RS_STR:=REPLACE(RS_STR, 'Á', 'A');
                RS_STR:=REPLACE(RS_STR, 'á', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ắ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ắ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ấ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ấ', 'a');
                RS_STR:=REPLACE(RS_STR, 'É', 'E');
                RS_STR:=REPLACE(RS_STR, 'é', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ế', 'E');
                RS_STR:=REPLACE(RS_STR, 'ế', 'e');
                RS_STR:=REPLACE(RS_STR, 'Í', 'I');
                RS_STR:=REPLACE(RS_STR, 'í', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ó', 'O');
                RS_STR:=REPLACE(RS_STR, 'ó', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ố', 'O');
                RS_STR:=REPLACE(RS_STR, 'ố', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ớ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ớ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ú', 'U');
                RS_STR:=REPLACE(RS_STR, 'ú', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ứ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ứ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ý', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ý', 'y');

               
                RS_STR:=REPLACE(RS_STR, 'Ả', 'A');
                RS_STR:=REPLACE(RS_STR, 'ả', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẳ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ẳ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẩ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ẩ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẻ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ẻ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ể', 'E');
                RS_STR:=REPLACE(RS_STR, 'ể', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ỉ', 'I');
                RS_STR:=REPLACE(RS_STR, 'ỉ', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ỏ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ỏ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ổ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ổ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ở', 'O');
                RS_STR:=REPLACE(RS_STR, 'ở', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ủ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ủ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ử', 'U');
                RS_STR:=REPLACE(RS_STR, 'ử', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ỷ', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ỷ', 'y');

                
                RS_STR:=REPLACE(RS_STR, 'Ã', 'A');
                RS_STR:=REPLACE(RS_STR, 'ã', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẵ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ẵ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẫ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ẫ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẽ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ẽ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ễ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ễ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ĩ', 'I');
                RS_STR:=REPLACE(RS_STR, 'ĩ', 'i');
                RS_STR:=REPLACE(RS_STR, 'Õ', 'O');
                RS_STR:=REPLACE(RS_STR, 'õ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ỗ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ỗ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ỡ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ỡ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ũ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ũ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ữ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ữ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ỹ', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ỹ', 'y');
                RS_STR:=REPLACE(RS_STR, 'Ạ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ạ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ặ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ặ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ậ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ậ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẹ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ẹ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ệ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ệ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ị', 'I');
                RS_STR:=REPLACE(RS_STR, 'ị', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ọ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ọ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ộ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ộ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ợ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ợ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ụ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ụ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ự', 'U');
                RS_STR:=REPLACE(RS_STR, 'ự', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ỵ', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ỵ', 'y');
                RS_STR:=REPLACE(RS_STR, 'Đ', 'D');
                RS_STR:=REPLACE(RS_STR, 'đ', 'd');
                RS_STR:=REPLACE(RS_STR, '/', '');
                RS_STR:=REPLACE(RS_STR, '\', '');
                RS_STR:=REPLACE(RS_STR, '#', '');
                RS_STR:=REPLACE(RS_STR, '.', '');
                RS_STR:=REPLACE(RS_STR, '%', '');
                RS_STR:=REPLACE(RS_STR, '&', '');
                RS_STR:=REPLACE(RS_STR, '^', '');
                RS_STR:=REPLACE(RS_STR, '*', '');
                RS_STR:=REPLACE(RS_STR, '@', '');
                RS_STR:=REPLACE(RS_STR, '!', '');
                RS_STR:=REPLACE(RS_STR, '~', '');
                RS_STR:=REPLACE(RS_STR, '$', '');
                RS_STR:=REPLACE(RS_STR, '^', '');
                RS_STR:=REPLACE(RS_STR, '`', '');
                RS_STR:=REPLACE(RS_STR, ';', '');
                RS_STR:=REPLACE(RS_STR, ':', '');
                RS_STR:=REPLACE(RS_STR, '=', '');
                RS_STR:=REPLACE(RS_STR, '"', '');  
                --RETURN lower(RS_STR);
                RETURN RS_STR;
  END;
  -------------------------------------------------------------------------------
 FUNCTION LOC_DAU2 (P_CHUOI NVARCHAR2) RETURN NVARCHAR2
  AS
         RS_STR NVARCHAR2(2000);
  BEGIN 
                RS_STR:=TRIM(P_CHUOI);   
                RS_STR:=REPLACE(RS_STR, 'A', 'A'); 
                RS_STR:=REPLACE(RS_STR, 'a', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ă', 'A');
                RS_STR:=REPLACE(RS_STR, 'ă', 'a');
                RS_STR:=REPLACE(RS_STR, 'Â', 'A');
                RS_STR:=REPLACE(RS_STR, 'â', 'a');
                RS_STR:=REPLACE(RS_STR, 'E', 'E');
                RS_STR:=REPLACE(RS_STR, 'e', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ê', 'E');
                RS_STR:=REPLACE(RS_STR, 'ê', 'e');
                RS_STR:=REPLACE(RS_STR, 'I', 'I');
                RS_STR:=REPLACE(RS_STR, 'i', 'i');
                RS_STR:=REPLACE(RS_STR, 'O', 'O');
                RS_STR:=REPLACE(RS_STR, 'o', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ô', 'O');
                RS_STR:=REPLACE(RS_STR, 'ô', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ơ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ơ', 'o');
                RS_STR:=REPLACE(RS_STR, 'U', 'U');
                RS_STR:=REPLACE(RS_STR, 'u', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ư', 'U');
                RS_STR:=REPLACE(RS_STR, 'ư', 'u');
                RS_STR:=REPLACE(RS_STR, 'Y', 'Y');
                RS_STR:=REPLACE(RS_STR, 'y', 'y');

              
                RS_STR:=REPLACE(RS_STR, 'À', 'A');
                RS_STR:=REPLACE(RS_STR, 'à', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ằ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ằ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ầ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ầ', 'a');
                RS_STR:=REPLACE(RS_STR, 'È', 'E');
                RS_STR:=REPLACE(RS_STR, 'è', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ề', 'E');
                RS_STR:=REPLACE(RS_STR, 'ề', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ì', 'I');
                RS_STR:=REPLACE(RS_STR, 'ì', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ò', 'O');
                RS_STR:=REPLACE(RS_STR, 'ò', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ồ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ồ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ờ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ờ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ù', 'U');
                RS_STR:=REPLACE(RS_STR, 'ù', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ừ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ừ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ỳ', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ỳ', 'y');

              
                RS_STR:=REPLACE(RS_STR, 'Á', 'A');
                RS_STR:=REPLACE(RS_STR, 'á', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ắ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ắ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ấ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ấ', 'a');
                RS_STR:=REPLACE(RS_STR, 'É', 'E');
                RS_STR:=REPLACE(RS_STR, 'é', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ế', 'E');
                RS_STR:=REPLACE(RS_STR, 'ế', 'e');
                RS_STR:=REPLACE(RS_STR, 'Í', 'I');
                RS_STR:=REPLACE(RS_STR, 'í', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ó', 'O');
                RS_STR:=REPLACE(RS_STR, 'ó', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ố', 'O');
                RS_STR:=REPLACE(RS_STR, 'ố', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ớ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ớ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ú', 'U');
                RS_STR:=REPLACE(RS_STR, 'ú', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ứ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ứ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ý', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ý', 'y');

               
                RS_STR:=REPLACE(RS_STR, 'Ả', 'A');
                RS_STR:=REPLACE(RS_STR, 'ả', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẳ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ẳ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẩ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ẩ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẻ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ẻ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ể', 'E');
                RS_STR:=REPLACE(RS_STR, 'ể', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ỉ', 'I');
                RS_STR:=REPLACE(RS_STR, 'ỉ', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ỏ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ỏ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ổ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ổ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ở', 'O');
                RS_STR:=REPLACE(RS_STR, 'ở', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ủ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ủ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ử', 'U');
                RS_STR:=REPLACE(RS_STR, 'ử', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ỷ', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ỷ', 'y');

                
                RS_STR:=REPLACE(RS_STR, 'Ã', 'A');
                RS_STR:=REPLACE(RS_STR, 'ã', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẵ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ẵ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẫ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ẫ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẽ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ẽ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ễ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ễ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ĩ', 'I');
                RS_STR:=REPLACE(RS_STR, 'ĩ', 'i');
                RS_STR:=REPLACE(RS_STR, 'Õ', 'O');
                RS_STR:=REPLACE(RS_STR, 'õ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ỗ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ỗ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ỡ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ỡ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ũ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ũ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ữ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ữ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ỹ', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ỹ', 'y');
                RS_STR:=REPLACE(RS_STR, 'Ạ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ạ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ặ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ặ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ậ', 'A');
                RS_STR:=REPLACE(RS_STR, 'ậ', 'a');
                RS_STR:=REPLACE(RS_STR, 'Ẹ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ẹ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ệ', 'E');
                RS_STR:=REPLACE(RS_STR, 'ệ', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ị', 'I');
                RS_STR:=REPLACE(RS_STR, 'ị', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ọ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ọ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ộ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ộ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ợ', 'O');
                RS_STR:=REPLACE(RS_STR, 'ợ', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ụ', 'U');
                RS_STR:=REPLACE(RS_STR, 'ụ', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ự', 'U');
                RS_STR:=REPLACE(RS_STR, 'ự', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ỵ', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ỵ', 'y');
                RS_STR:=REPLACE(RS_STR, 'Đ', 'D');
                RS_STR:=REPLACE(RS_STR, 'đ', 'd');
                RS_STR:=REPLACE(RS_STR, '/', '');
                RS_STR:=REPLACE(RS_STR, '\', '');
                RS_STR:=REPLACE(RS_STR, '#', '');
                RS_STR:=REPLACE(RS_STR, '%', '');
                RS_STR:=REPLACE(RS_STR, '^', '');
                RS_STR:=REPLACE(RS_STR, '*', '');
                RS_STR:=REPLACE(RS_STR, '@', '');
                RS_STR:=REPLACE(RS_STR, '!', '');
                RS_STR:=REPLACE(RS_STR, '~', '');
                RS_STR:=REPLACE(RS_STR, '$', '');
                RS_STR:=REPLACE(RS_STR, '^', '');
                --RETURN lower(RS_STR);
                RETURN RS_STR;
  END;  
-------------------------------------------------------------------------------
FUNCTION LOC_DAU_VA_KHOANG_TRANG(P_CHUOI NVARCHAR2) RETURN NVARCHAR2
  AS
         RS_STR NVARCHAR2(2000);
  BEGIN 
                RS_STR:=TRIM(P_CHUOI);   
                RS_STR:=REPLACE(RS_STR, 'A', 'A'); 
                RS_STR:=REPLACE(RS_STR, 'a', 'a');
                RS_STR:=REPLACE(RS_STR, 'A', 'A');
                RS_STR:=REPLACE(RS_STR, 'a', 'a');
                RS_STR:=REPLACE(RS_STR, 'Â', 'A');
                RS_STR:=REPLACE(RS_STR, 'â', 'a');
                RS_STR:=REPLACE(RS_STR, 'E', 'E');
                RS_STR:=REPLACE(RS_STR, 'e', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ê', 'E');
                RS_STR:=REPLACE(RS_STR, 'ê', 'e');
                RS_STR:=REPLACE(RS_STR, 'I', 'I');
                RS_STR:=REPLACE(RS_STR, 'i', 'i');
                RS_STR:=REPLACE(RS_STR, 'O', 'O');
                RS_STR:=REPLACE(RS_STR, 'o', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ô', 'O');
                RS_STR:=REPLACE(RS_STR, 'ô', 'o');
                RS_STR:=REPLACE(RS_STR, 'O', 'O');
                RS_STR:=REPLACE(RS_STR, 'o', 'o');
                RS_STR:=REPLACE(RS_STR, 'U', 'U');
                RS_STR:=REPLACE(RS_STR, 'u', 'u');
                RS_STR:=REPLACE(RS_STR, 'U', 'U');
                RS_STR:=REPLACE(RS_STR, 'u', 'u');
                RS_STR:=REPLACE(RS_STR, 'Y', 'Y');
                RS_STR:=REPLACE(RS_STR, 'y', 'y');

              
                RS_STR:=REPLACE(RS_STR, 'À', 'A');
                RS_STR:=REPLACE(RS_STR, 'à', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, 'È', 'E');
                RS_STR:=REPLACE(RS_STR, 'è', 'e');
                RS_STR:=REPLACE(RS_STR, '?', 'E');
                RS_STR:=REPLACE(RS_STR, '?', 'e');
                RS_STR:=REPLACE(RS_STR, 'Ì', 'I');
                RS_STR:=REPLACE(RS_STR, 'ì', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ò', 'O');
                RS_STR:=REPLACE(RS_STR, 'ò', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ù', 'U');
                RS_STR:=REPLACE(RS_STR, 'ù', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'U');
                RS_STR:=REPLACE(RS_STR, '?', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'Y');
                RS_STR:=REPLACE(RS_STR, '?', 'y');

              
                RS_STR:=REPLACE(RS_STR, 'Á', 'A');
                RS_STR:=REPLACE(RS_STR, 'á', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, 'É', 'E');
                RS_STR:=REPLACE(RS_STR, 'é', 'e');
                RS_STR:=REPLACE(RS_STR, '?', 'E');
                RS_STR:=REPLACE(RS_STR, '?', 'e');
                RS_STR:=REPLACE(RS_STR, 'Í', 'I');
                RS_STR:=REPLACE(RS_STR, 'í', 'i');
                RS_STR:=REPLACE(RS_STR, 'Ó', 'O');
                RS_STR:=REPLACE(RS_STR, 'ó', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, 'Ú', 'U');
                RS_STR:=REPLACE(RS_STR, 'ú', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'U');
                RS_STR:=REPLACE(RS_STR, '?', 'u');
                RS_STR:=REPLACE(RS_STR, 'Ý', 'Y');
                RS_STR:=REPLACE(RS_STR, 'ý', 'y');

               
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'E');
                RS_STR:=REPLACE(RS_STR, '?', 'e');
                RS_STR:=REPLACE(RS_STR, '?', 'E');
                RS_STR:=REPLACE(RS_STR, '?', 'e');
                RS_STR:=REPLACE(RS_STR, '?', 'I');
                RS_STR:=REPLACE(RS_STR, '?', 'i');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'U');
                RS_STR:=REPLACE(RS_STR, '?', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'U');
                RS_STR:=REPLACE(RS_STR, '?', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'Y');
                RS_STR:=REPLACE(RS_STR, '?', 'y');

                
                RS_STR:=REPLACE(RS_STR, 'Ã', 'A');
                RS_STR:=REPLACE(RS_STR, 'ã', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'E');
                RS_STR:=REPLACE(RS_STR, '?', 'e');
                RS_STR:=REPLACE(RS_STR, '?', 'E');
                RS_STR:=REPLACE(RS_STR, '?', 'e');
                RS_STR:=REPLACE(RS_STR, 'I', 'I');
                RS_STR:=REPLACE(RS_STR, 'i', 'i');
                RS_STR:=REPLACE(RS_STR, 'Õ', 'O');
                RS_STR:=REPLACE(RS_STR, 'õ', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, 'U', 'U');
                RS_STR:=REPLACE(RS_STR, 'u', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'U');
                RS_STR:=REPLACE(RS_STR, '?', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'Y');
                RS_STR:=REPLACE(RS_STR, '?', 'y');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'A');
                RS_STR:=REPLACE(RS_STR, '?', 'a');
                RS_STR:=REPLACE(RS_STR, '?', 'E');
                RS_STR:=REPLACE(RS_STR, '?', 'e');
                RS_STR:=REPLACE(RS_STR, '?', 'E');
                RS_STR:=REPLACE(RS_STR, '?', 'e');
                RS_STR:=REPLACE(RS_STR, '?', 'I');
                RS_STR:=REPLACE(RS_STR, '?', 'i');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'O');
                RS_STR:=REPLACE(RS_STR, '?', 'o');
                RS_STR:=REPLACE(RS_STR, '?', 'U');
                RS_STR:=REPLACE(RS_STR, '?', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'U');
                RS_STR:=REPLACE(RS_STR, '?', 'u');
                RS_STR:=REPLACE(RS_STR, '?', 'Y');
                RS_STR:=REPLACE(RS_STR, '?', 'y');
                RS_STR:=REPLACE(RS_STR, 'Ð', 'D');
                RS_STR:=REPLACE(RS_STR, 'd', 'd');
                RS_STR:=REPLACE(RS_STR, '/', '');
                RS_STR:=REPLACE(RS_STR, '\', '');
                RS_STR:=REPLACE(RS_STR, '#', '');
                RS_STR:=REPLACE(RS_STR, '.', '');
                RS_STR:=REPLACE(RS_STR, '%', '');
                RS_STR:=REPLACE(RS_STR, '&', '');
                RS_STR:=REPLACE(RS_STR, '^', '');
                RS_STR:=REPLACE(RS_STR, '*', '');
                RS_STR:=REPLACE(RS_STR, '@', '');
                RS_STR:=REPLACE(RS_STR, '!', '');
                RS_STR:=REPLACE(RS_STR, '~', '');
                RS_STR:=REPLACE(RS_STR, '$', '');
                RS_STR:=REPLACE(RS_STR, '^', '');
                RS_STR:=REPLACE(RS_STR, '`', '');
                RS_STR:=REPLACE(RS_STR, ';', '');
                RS_STR:=REPLACE(RS_STR, ':', '');
                RS_STR:=REPLACE(RS_STR, '=', '');
                RS_STR:=REPLACE(RS_STR, '"', '');  
                RS_STR:=REPLACE(RS_STR, ' ', ''); 
                RETURN lower(RS_STR);
                RETURN RS_STR;
  END; 
 -------------------------------------------------------------------------------
  FUNCTION GET_GIO_HIEN_TAI  RETURN NUMBER
  IS
  V_NGAY_HIEN_TAI NVARCHAR2(30);
  BEGIN
    V_NGAY_HIEN_TAI:= TO_CHAR(SYSDATE,'DD/MM/YYYY HH24:MI:SS');
    RETURN TO_NUMBER(SUBSTR(V_NGAY_HIEN_TAI,12,2));
  END;
 -------------------------------------------------------------------------------
  FUNCTION GET_THANG_HIEN_TAI  RETURN NUMBER
  IS
  V_RS NUMBER;
  BEGIN
    SELECT EXTRACT(MONTH FROM SYSDATE) INTO V_RS FROM DUAL;
    RETURN V_RS;
  END;
 -------------------------------------------------------------------------------
  FUNCTION GET_NAM_HIEN_TAI  RETURN NUMBER
  IS
  V_RS NUMBER;
  BEGIN
    SELECT EXTRACT(YEAR FROM SYSDATE) INTO V_RS FROM DUAL;
    RETURN V_RS;
  END; 
 -------------------------------------------------------------------------------
  FUNCTION GET_THANG_FROM_CHUOI_NGAY(P_NGAY NVARCHAR2)  RETURN NUMBER
  IS
  BEGIN
    RETURN TO_NUMBER(SUBSTR(P_NGAY,4,2));
  END;
 -------------------------------------------------------------------------------
  FUNCTION GET_NAM_FROM_CHUOI_NGAY(P_NGAY NVARCHAR2)  RETURN NUMBER
  IS
  BEGIN
    RETURN TO_NUMBER(SUBSTR(P_NGAY,7,4));
  END;
 -------------------------------------------------------------------------------
  FUNCTION SO_SANH_NGAY_2_CHUOI_NGAY(P_NGAY1 NVARCHAR2,P_NGAY2 NVARCHAR2)  RETURN NUMBER
  IS
  BEGIN
    IF SUBSTR(P_NGAY1,0,10) = SUBSTR(P_NGAY2,0,10) THEN
      RETURN 1;
    ELSE
      RETURN 0;
    END IF;
  END;
 -------------------------------------------------------------------------------
  FUNCTION KIEM_TRA_SO_DIEN_THOAI(P_SO_DIEN_THOAI NVARCHAR2) RETURN NVARCHAR2 AS
    RS_MANG NVARCHAR2(15);
    VINA MY_ARR;
    MOBI MY_ARR;
    VIETTEL MY_ARR;
    VN_MOBILE MY_ARR;
    SL_VINA INT;
    SL_MOBI INT;
    SL_VIETTEL INT;
    SL_VN_MOBILE INT;
    SL_SO INT;
    DAU_10 NVARCHAR2(2);
    DAU_SO NVARCHAR2(4);
    BEGIN
      VINA:= MY_ARR('091','094','0123','0124','0125','0127','0129');
      MOBI:=MY_ARR('090','093','0120','0121','0122','0126','0128');
      VIETTEL:=MY_ARR('096','097','098','0163','0164','0165','0166','0167','0168','0169');
      VN_MOBILE:=MY_ARR('092','0188');
      SL_SO:= LENGTH(P_SO_DIEN_THOAI);
      IF SL_SO = 10 OR SL_SO = 11 THEN
        --KIEM TRA DAU SO LA 10 SO HOAC 11 SO
        DAU_10:=SUBSTR(P_SO_DIEN_THOAI,0,2);
        IF DAU_10='09' THEN
          DAU_SO:=SUBSTR(P_SO_DIEN_THOAI,0,3);
        ELSE 
          DAU_SO:=SUBSTR(P_SO_DIEN_THOAI,0,4);
        END IF;
        --LAY DO DAI CAC MANG DAU SO DIEN THOAI
        SL_VINA:= VINA.COUNT;
        SL_MOBI:= MOBI.COUNT;
        SL_VIETTEL:= VIETTEL.COUNT;
        SL_VN_MOBILE:= VN_MOBILE.COUNT;
        --KIEM TRA DAU SO VINAPHONE
        FOR i IN 1..SL_VINA LOOP
          IF DAU_SO = VINA(i) THEN
            RS_MANG:= 'VINAPHONE';
          END IF;
        END LOOP;
        --KIEM TRA DAU SO MOBIFONE
        IF RS_MANG IS NULL THEN
          FOR i IN 1..SL_MOBI LOOP
            IF DAU_SO = MOBI(i) THEN
              RS_MANG:='MOBIFONE';
            END IF;
          END LOOP;
        END IF;
        --KIEM TRA DAU SO VIETEL
        IF RS_MANG IS NULL THEN
          FOR i IN 1..SL_VIETTEL LOOP
            IF DAU_SO = VIETTEL(i) THEN
              RS_MANG:= 'VIETTEL';
            END IF;
          END LOOP;
        END IF;
        --DAU SO KHONG HOP LE
        IF RS_MANG IS NULL THEN
          FOR i IN 1..SL_VN_MOBILE LOOP
            IF DAU_SO = VN_MOBILE(i) THEN
              RS_MANG:= 'VIETNAMMOBILE';
            END IF;
          END LOOP;
        END IF;
        IF RS_MANG IS NULL THEN
          RS_MANG:='DSO_NOT_DECLARE';
        END IF;
      ELSE 
        RS_MANG:='SDT_ERROR';
      END IF;
    RETURN RS_MANG;
 END;
END LIB_NOTABLE;
/

prompt
prompt Creating package body MC_CB
prompt ===========================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.MC_CB IS

  /* BEGIN: HOC */
    
  --PROC: SELECT_DON_VI
  PROCEDURE SELECT_DON_VI(
    P_CUR IN OUT MYCUR,
    P_MA_DON_VI NVARCHAR2,
    P_TEN_DON_VI NVARCHAR2,
    P_MA_LOAI_DON_VI NVARCHAR2,
    P_MA_PHUONG_XA NVARCHAR2,
    P_TRANG_THAI NUMBER,
    P_MA_DON_VI_CHU_QUAN NVARCHAR2,
    P_MA_TAT NVARCHAR2,
    P_MA_CO_QUAN NVARCHAR2)
  IS
  BEGIN
    OPEN P_CUR FOR
    SELECT * 
    FROM CB_DON_VI
    WHERE (P_MA_DON_VI IS NULL OR MA_DON_VI = P_MA_DON_VI) AND
          (P_TEN_DON_VI IS NULL OR UPPER(TEN_DON_VI) LIKE '%'||UPPER(P_TEN_DON_VI)||'%') AND
          (P_MA_LOAI_DON_VI IS NULL OR MA_LOAI_DON_VI = P_MA_LOAI_DON_VI) AND
          (P_MA_PHUONG_XA IS NULL OR MA_PHUONG_XA = P_MA_PHUONG_XA) AND
          (P_TRANG_THAI IS NULL OR TRANG_THAI = P_TRANG_THAI) AND
          (P_MA_DON_VI_CHU_QUAN IS NULL OR MA_DON_VI_CHU_QUAN = P_MA_DON_VI_CHU_QUAN) AND
          (P_MA_TAT IS NULL OR MA_TAT = P_MA_TAT) AND
          (P_MA_CO_QUAN IS NULL OR MA_CO_QUAN = P_MA_CO_QUAN)
    ORDER BY STT_DON_VI, TEN_DON_VI;
  END;
  
  --PROC: SELECT_TO_CHUYEN_MON
  PROCEDURE SELECT_TO_CHUYEN_MON(
    P_CUR IN OUT MYCUR,
    P_MA_TO_CHUYEN_MON NVARCHAR2,
    P_TEN_TO_CHUYEN_MON NVARCHAR2,
    P_MA_DON_VI NVARCHAR2,
    P_MA_LOAI_NHOM_NSD NVARCHAR2,
    P_TRANG_THAI NUMBER)
  IS
  BEGIN
    OPEN P_CUR FOR
    SELECT * 
    FROM CB_TO_CHUYEN_MON
    WHERE (P_MA_TO_CHUYEN_MON IS NULL OR MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON) AND
          (P_TEN_TO_CHUYEN_MON IS NULL OR UPPER(TEN_TO_CHUYEN_MON) LIKE '%'||UPPER(P_TEN_TO_CHUYEN_MON)||'%') AND
          (P_MA_DON_VI IS NULL OR MA_DON_VI = P_MA_DON_VI) AND
          (P_MA_LOAI_NHOM_NSD IS NULL OR MA_LOAI_NHOM_NSD = P_MA_LOAI_NHOM_NSD) AND
          (P_TRANG_THAI IS NULL OR TRANG_THAI = P_TRANG_THAI)
    ORDER BY STT_TO_CHUYEN_MON, TEN_TO_CHUYEN_MON;
  END;
  
  --PROC: SELECT_CAN_BO
  PROCEDURE SELECT_CAN_BO(
    P_CUR IN OUT MYCUR,
    P_MA_CAN_BO NUMBER,
    P_MA_CAN_BO_LIEN_THONG NUMBER,
    P_TEN_CAN_BO NVARCHAR2,
    P_TAI_KHOAN NVARCHAR2,
    P_CMND NVARCHAR2,
    P_DIA_CHI NVARCHAR2,
    P_DI_DONG NVARCHAR2,
    P_TRANG_THAI NUMBER,
    P_DANG_KY_NHAC_VIEC NUMBER,
    P_MA_CO_QUAN NVARCHAR2,
    P_MA_PHUONG_XA NVARCHAR2,
    P_MA_TO_CHUYEN_MON NVARCHAR2)
  IS
  BEGIN
    OPEN P_CUR FOR
    SELECT *
    FROM CB_CAN_BO
    WHERE (P_MA_CAN_BO IS NULL OR MA_CAN_BO = P_MA_CAN_BO) AND
          (P_MA_CAN_BO_LIEN_THONG IS NULL OR MA_CAN_BO_LIEN_THONG = P_MA_CAN_BO_LIEN_THONG) AND
          (P_TEN_CAN_BO IS NULL OR UPPER(TEN_CAN_BO) LIKE '%'||UPPER(P_TEN_CAN_BO)||'%') AND
          (P_TAI_KHOAN IS NULL OR TAI_KHOAN = P_TAI_KHOAN) AND
          (P_CMND IS NULL OR CMND = P_CMND) AND
          (P_DIA_CHI IS NULL OR UPPER(P_DIA_CHI) LIKE '%'||UPPER(P_DIA_CHI)||'%') AND
          (P_DI_DONG IS NULL OR DI_DONG = P_DI_DONG) AND
          (P_TRANG_THAI IS NULL OR TRANG_THAI = P_TRANG_THAI) AND
          (P_DANG_KY_NHAC_VIEC IS NULL OR DANG_KY_NHAC_VIEC = P_DANG_KY_NHAC_VIEC) AND
          (P_MA_CO_QUAN IS NULL OR MA_CO_QUAN = P_MA_CO_QUAN) AND
          (P_MA_PHUONG_XA IS NULL OR MA_PHUONG_XA = P_MA_PHUONG_XA) AND
          (P_MA_TO_CHUYEN_MON IS NULL OR MA_CAN_BO IN (SELECT MA_CAN_BO FROM CB_THUOC_TO_CHUYEN_MON WHERE MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON))
    ORDER BY STT_CAN_BO, TEN_CAN_BO;
  END;
    
  
  --PROC: UPDATE_OTP
  PROCEDURE UPDATE_OTP(
    P_KQ OUT NUMBER,
    P_MA_CAN_BO NUMBER,
    P_DI_DONG NVARCHAR2,
    P_NGAY_DANG_KY_OTP NVARCHAR2) 
  AS
  BEGIN
    UPDATE CB_CAN_BO
    SET NGAY_DANG_KY_OTP = TO_DATE(P_NGAY_DANG_KY_OTP, 'DD/MM/YYYY HH24:MI:SS'),
        DI_DONG = NVL(P_DI_DONG, DI_DONG)
    WHERE MA_CAN_BO = P_MA_CAN_BO;
    P_KQ := 1;
  END;
  
  /* END: HOC*/
  
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DON_VI_FILTER(P_CUR                IN OUT MYCUR,
                                 P_MA_CO_QUAN         NVARCHAR2,
                                 P_MA_LOAI_DON_VI     NVARCHAR2,
                                 P_MA_PHUONG_XA       NVARCHAR2,
                                 P_MA_DON_VI_CHU_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT A.MA_CO_QUAN,A.MA_DON_VI,A.TEN_DON_VI,A.MA_TAT,
            A.SO_HO_SO_HIEN_TAI, A.STT_DON_VI,
                             B.TEN_LOAI_DON_VI,
                             C.TEN_PHUONG_XA,
                             D.TEN_DON_VI AS TEN_DON_VI_CHU_QUAN,
                             CQ.TEN_CO_QUAN,
                             CASE
                               WHEN A.TRANG_THAI = 0 THEN
                                'Mở'
                               ELSE
                                'Ðóng'
                             END AS TEN_TRANG_THAI
                        FROM CB_DON_VI      A,
                               CB_LOAI_DON_VI B,
                               DM_PHUONG_XA   C,
                               CB_DON_VI      D,
                               CB_CO_QUAN CQ
                         WHERE A.MA_CO_QUAN=CQ.MA_CO_QUAN
                           AND A.MA_LOAI_DON_VI = B.MA_LOAI_DON_VI
                           AND A.MA_PHUONG_XA = C.MA_PHUONG_XA(+)
                           AND A.MA_DON_VI_CHU_QUAN = D.MA_DON_VI(+)
                           AND (P_MA_PHUONG_XA IS NULL OR UPPER(A.MA_PHUONG_XA)=UPPER(P_MA_PHUONG_XA))
                           AND (P_MA_DON_VI_CHU_QUAN IS NULL OR A.MA_DON_VI_CHU_QUAN= P_MA_DON_VI_CHU_QUAN)
                           AND A.MA_CO_QUAN= P_MA_CO_QUAN
                           AND (P_MA_LOAI_DON_VI IS NULL OR A.MA_LOAI_DON_VI =P_MA_LOAI_DON_VI)
                       ORDER BY A.TEN_DON_VI,
                                A.MA_TAT,
                                A.STT_DON_VI,
                                C.STT_PHUONG_XA,
                                B.STT_LOAI_DON_VI;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DON_VI_DETAIL(P_CUR IN OUT MYCUR, P_MA_DON_VI NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             B.TEN_LOAI_DON_VI,
             C.TEN_PHUONG_XA,
             D.TEN_DON_VI AS TEN_DON_VI_CHU_QUAN,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_DON_VI      A,
             CB_LOAI_DON_VI B,
             DM_PHUONG_XA   C,
             CB_DON_VI      D
       WHERE A.MA_DON_VI LIKE P_MA_DON_VI
         AND A.MA_LOAI_DON_VI = B.MA_LOAI_DON_VI
         AND A.MA_PHUONG_XA = C.MA_PHUONG_XA(+) --ben nao co dau cong la ben kia null cung co the hien ra
         AND A.MA_DON_VI_CHU_QUAN = D.MA_DON_VI(+);
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_DON_VI(P_VAL                OUT NVARCHAR2,
                          P_UPDATE_TYPE        NVARCHAR2,
                          P_NGUOI_CAP_NHAT     INT,
                          P_MA_DON_VI          NVARCHAR2,
                          P_TEN_DON_VI         NVARCHAR2,
                          P_MA_CO_QUAN         NVARCHAR2,
                          P_MA_LOAI_DON_VI     NVARCHAR2,
                          P_MA_PHUONG_XA       NVARCHAR2,
                          P_MA_DON_VI_CHU_QUAN NVARCHAR2,
                          P_MA_TAT NVARCHAR2,
                          P_SO_HO_SO_HIEN_TAI INT,
                          P_STT_DON_VI         INT,
                          P_TRANG_THAI         INT) AS
    V_MA_DON_VI_CHU_QUAN NVARCHAR2(100);
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_MA_DON_VI_CHU_QUAN = '0' THEN
      V_MA_DON_VI_CHU_QUAN := NULL;
    ELSE
      V_MA_DON_VI_CHU_QUAN := P_MA_DON_VI_CHU_QUAN;
    END IF;
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      INSERT INTO CB_DON_VI
        (MA_DON_VI,
         TEN_DON_VI,
         TEN_DON_VI_KHONG_DAU,
         MA_CO_QUAN,
         MA_LOAI_DON_VI,
         MA_PHUONG_XA,
         MA_DON_VI_CHU_QUAN,
         MA_TAT,
         SO_HO_SO_HIEN_TAI,
         STT_DON_VI,
         TRANG_THAI,
         NGUOI_CAP_NHAT,
         NGAY_CAP_NHAT)
      VALUES
        (P_MA_DON_VI,
         P_TEN_DON_VI,
         LIB_NOTABLE.LOC_DAU(P_TEN_DON_VI),
         P_MA_CO_QUAN,
         P_MA_LOAI_DON_VI,
         P_MA_PHUONG_XA,
         V_MA_DON_VI_CHU_QUAN,
         P_MA_TAT,
         P_SO_HO_SO_HIEN_TAI,
         P_STT_DON_VI,
         P_TRANG_THAI,
         P_NGUOI_CAP_NHAT,
         SYSDATE);
      P_VAL := 'THANH_CONG';
    ELSE
      UPDATE CB_DON_VI
         SET TEN_DON_VI         = P_TEN_DON_VI,
             TEN_DON_VI_KHONG_DAU = LIB_NOTABLE.LOC_DAU(P_TEN_DON_VI),
             MA_CO_QUAN         = P_MA_CO_QUAN,
             MA_LOAI_DON_VI     = P_MA_LOAI_DON_VI,
             MA_PHUONG_XA       = P_MA_PHUONG_XA,
             MA_DON_VI_CHU_QUAN = V_MA_DON_VI_CHU_QUAN,
             MA_TAT=P_MA_TAT,
             SO_HO_SO_HIEN_TAI=P_SO_HO_SO_HIEN_TAI,
             STT_DON_VI         = P_STT_DON_VI,
             TRANG_THAI         = P_TRANG_THAI,
             NGUOI_CAP_NHAT     = P_NGUOI_CAP_NHAT,
             NGAY_CAP_NHAT      = SYSDATE
       WHERE MA_DON_VI LIKE P_MA_DON_VI;
      P_VAL := 'THANH_CONG';
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_DON_VI(P_VAL       OUT NVARCHAR2,
                          P_NGUOI_XOA INT,
                          P_MA_DON_VI NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM CB_DON_VI WHERE MA_DON_VI LIKE P_MA_DON_VI;
      P_VAL := 'THANH_CONG';
    END IF;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_TO_CHUYEN_MON_DETAIL(P_CUR              IN OUT MYCUR,
                                        P_MA_TO_CHUYEN_MON INT) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TO_CHUYEN_MON,
             A.TEN_TO_CHUYEN_MON,
             A.MA_DON_VI,
             A.MA_LOAI_NHOM_NSD,
             A.STT_TO_CHUYEN_MON,
             A.TRANG_THAI,
             B.TEN_DON_VI,
             C.TEN_LOAI_NHOM_NSD,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_TO_CHUYEN_MON A, CB_DON_VI B, CB_LOAI_NHOM_NSD C
       WHERE A.MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON
         AND A.MA_DON_VI = B.MA_DON_VI
         AND A.MA_LOAI_NHOM_NSD = C.MA_LOAI_NHOM_NSD;
  END;
  ------------------------------------------------------------------------------
  PROCEDURE SELECT_TO_CHUYEN_MON_ALL_TR(P_CUR       IN OUT MYCUR,
                                        P_MA_DON_VI NVARCHAR2,
                                        P_MA_CO_QUAN NVARCHAR2,
                                        P_SITE_ROOT NVARCHAR2) AS
    P_VAL               NCLOB;
    V_MA_DON_VI         NVARCHAR2(100);
    V_TEN_DON_VI        NVARCHAR2(100);
    V_MA_TO_CHUYEN_MON  INT;
    V_TEN_TO_CHUYEN_MON NVARCHAR2(100);
    V_STT_TO_CHUYEN_MON INT;
    V_TEN_TRANG_THAI    NVARCHAR2(100);
    V_ROW               INT;
    V_CLASS             NVARCHAR2(100);
    V_LINK              NVARCHAR2(200);
    CURSOR C1 IS
      SELECT A.MA_DON_VI, A.TEN_DON_VI
        FROM CB_DON_VI A
       WHERE UPPER(NVL(A.MA_DON_VI, '')) LIKE UPPER(NVL(P_MA_DON_VI, '%%'))
       AND A.MA_CO_QUAN=P_MA_CO_QUAN
       ORDER BY A.STT_DON_VI;
    CURSOR C2 IS
      SELECT A.MA_TO_CHUYEN_MON,
             A.TEN_TO_CHUYEN_MON,
             A.STT_TO_CHUYEN_MON,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_TO_CHUYEN_MON A
       WHERE A.MA_DON_VI = V_MA_DON_VI
       ORDER BY A.STT_TO_CHUYEN_MON;
  BEGIN
    P_VAL := '';
    V_ROW := 0;
    OPEN C1;
    LOOP
      FETCH C1
        INTO V_MA_DON_VI, V_TEN_DON_VI;
      EXIT WHEN C1%NOTFOUND;
      P_VAL := P_VAL ||
               '<tr>
                               <td><b>
                               <img src="/public/images/unit16.png"> ' ||
               V_TEN_DON_VI ||
               '</b></td>
                               <td></td><td></td><td></td></tr>';
      OPEN C2;
      LOOP
        FETCH C2
          INTO V_MA_TO_CHUYEN_MON,
               V_TEN_TO_CHUYEN_MON,
               V_STT_TO_CHUYEN_MON,
               V_TEN_TRANG_THAI;
        EXIT WHEN C2%NOTFOUND;
        V_ROW   := V_ROW + 1;
        V_CLASS := 'row' || TO_CHAR(MOD(V_ROW, 2));
        V_LINK  := P_SITE_ROOT || 'canbo/canbo/dsp_all_canbo_to/&m=' ||
                   TO_CHAR(V_MA_TO_CHUYEN_MON);
        P_VAL   := P_VAL || '<tr class="' || V_CLASS || '">
                  <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                      <img border="0" align="absmiddle" src="/public/images/user-group16.png">
                      <a href="' || V_LINK || '">' ||
                   V_TEN_TO_CHUYEN_MON || ' </a>
                  <td>' || TO_CHAR(V_STT_TO_CHUYEN_MON) ||
                   '</td>
                  <td style="text-align:center">' ||
                   V_TEN_TRANG_THAI ||
                   '</td>
                  <td style="text-align:center">
                      <a href="javascript:void(0)" onclick="row_tochuyenmon_onclick_pop_win(''' ||
                   TO_CHAR(V_MA_TO_CHUYEN_MON) ||
                   ''')">Sửa</a> |
                      <a href="javascript:void(0)" onclick="quick_delete_tochuyenmon(''' ||
                   TO_CHAR(V_MA_TO_CHUYEN_MON) || ''')">Xoá</a>
                  </td>
                </tr>
                ';
      
      END LOOP;
      CLOSE C2;
    END LOOP;
    CLOSE C1;
    OPEN P_CUR FOR
      SELECT P_VAL AS ALL_TR, V_ROW AS TONG_SO_DONG FROM DUAL;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_TO_CHUYEN_MON(P_VAL               OUT NVARCHAR2,
                                 P_UPDATE_TYPE       NVARCHAR2,
                                 P_NGUOI_CAP_NHAT    INT,
                                 P_MA_TO_CHUYEN_MON  INT,
                                 P_TEN_TO_CHUYEN_MON NVARCHAR2,
                                 P_MA_DON_VI         NVARCHAR2,
                                 P_MA_LOAI_NHOM_NSD  NVARCHAR2,
                                 P_STT_TO_CHUYEN_MON INT,
                                 P_TRANG_THAI        INT) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      INSERT INTO CB_TO_CHUYEN_MON
        (TEN_TO_CHUYEN_MON,
         MA_DON_VI,
         MA_LOAI_NHOM_NSD,
         STT_TO_CHUYEN_MON,
         TRANG_THAI,
         NGUOI_CAP_NHAT,
         NGAY_CAP_NHAT)
      VALUES
        (P_TEN_TO_CHUYEN_MON,
         P_MA_DON_VI,
         P_MA_LOAI_NHOM_NSD,
         P_STT_TO_CHUYEN_MON,
         P_TRANG_THAI,
         P_NGUOI_CAP_NHAT,
         SYSDATE);
      P_VAL := 'THANH_CONG';
    ELSE
      UPDATE CB_TO_CHUYEN_MON
         SET TEN_TO_CHUYEN_MON = P_TEN_TO_CHUYEN_MON,
             MA_DON_VI         = P_MA_DON_VI,
             MA_LOAI_NHOM_NSD  = P_MA_LOAI_NHOM_NSD,
             STT_TO_CHUYEN_MON = P_STT_TO_CHUYEN_MON,
             TRANG_THAI        = P_TRANG_THAI,
             NGUOI_CAP_NHAT    = P_NGUOI_CAP_NHAT,
             NGAY_CAP_NHAT     = SYSDATE
       WHERE MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON;
      P_VAL := 'THANH_CONG';
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_TO_CHUYEN_MON(P_VAL              OUT NVARCHAR2,
                                 P_NGUOI_XOA        INT,
                                 P_MA_TO_CHUYEN_MON INT) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM CB_TO_CHUYEN_MON
       WHERE MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON;
      P_VAL := 'THANH_CONG';
    END IF;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CAN_BO_DETAIL(P_CUR IN OUT MYCUR, P_MA_CAN_BO INT) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_CAN_BO,
             A.TEN_CAN_BO,
             A.STT_CAN_BO,
             A.TAI_KHOAN,
             A.CMND,
             A.DIA_CHI,
             A.DI_DONG,
             A.HINH_ANH,
             A.MA_QUYEN_LOAD_DAU_TIEN,
             A.NGAY_CAP_NHAT,
             A.DANG_KY_NHAC_VIEC,
             A.GIOI_TINH,
             A.NGAY_SINH,
             A.TRANG_THAI,
             B.MA_CHUC_VU,
             B.MA_TO_CHUYEN_MON,
             C.MA_DON_VI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B,CB_TO_CHUYEN_MON C
       WHERE A.MA_CAN_BO = P_MA_CAN_BO
       AND A.MA_CAN_BO=B.MA_CAN_BO
       AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
       ORDER BY A.STT_CAN_BO, A.TEN_CAN_BO;
  END;
  ------------------------------------------------------------------------------------
  PROCEDURE UPDATE_CAN_BO(P_VAL            OUT NVARCHAR2,
                          P_UPDATE_TYPE    NVARCHAR2,
                          P_NGUOI_CAP_NHAT INT,
                          P_MA_CAN_BO      INT,
                          P_TEN_CAN_BO     NVARCHAR2,
                          P_TAI_KHOAN      NVARCHAR2,
                          P_MAT_KHAU       NVARCHAR2,
                          P_CMND           NVARCHAR2,                                                
                          P_DIA_CHI        NVARCHAR2,
                          P_DI_DONG        NVARCHAR2,
                          P_HINH_ANH       NVARCHAR2,
                          P_STT_CAN_BO             INT,
                          P_TRANG_THAI             INT,
                          P_MA_QUYEN_LOAD_DAU_TIEN NVARCHAR2,
                          P_DANG_KY_NHAC_VIEC INT,                          
                          P_GIOI_TINH      NUMBER,
                          P_NGAY_SINH      NVARCHAR2,
                          P_MA_CO_QUAN NVARCHAR2) AS
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_CAN_BO)
        INTO V_TON_TAI_TEN
        FROM CB_CAN_BO
       WHERE UPPER(TAI_KHOAN) LIKE UPPER(P_TAI_KHOAN);
      IF V_TON_TAI_TEN > 0 THEN
        --kiem tra trung ten dang nhap
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO CB_CAN_BO
          (TEN_CAN_BO,
           TAI_KHOAN,
           MAT_KHAU,
           CMND,
           DIA_CHI,
           DI_DONG,
           HINH_ANH,
           STT_CAN_BO,
           TRANG_THAI,
           MA_QUYEN_LOAD_DAU_TIEN,
           NGUOI_CAP_NHAT,
           NGAY_CAP_NHAT,
           DANG_KY_NHAC_VIEC,
           GIOI_TINH,
           NGAY_SINH,
           MA_CO_QUAN)
        VALUES
          (P_TEN_CAN_BO,
           P_TAI_KHOAN,
           P_MAT_KHAU,
           P_CMND,
           P_DIA_CHI,
           P_DI_DONG,
           P_HINH_ANH,
           P_STT_CAN_BO,
           P_TRANG_THAI,
           P_MA_QUYEN_LOAD_DAU_TIEN,
           P_NGUOI_CAP_NHAT,
           SYSDATE,
           P_DANG_KY_NHAC_VIEC,
           P_GIOI_TINH,
           P_NGAY_SINH,
           P_MA_CO_QUAN);
        P_VAL := 'THANH_CONG';
      END IF;
    
    ELSE
        UPDATE CB_CAN_BO A
           SET A.TEN_CAN_BO             = P_TEN_CAN_BO,
               A.TAI_KHOAN              = P_TAI_KHOAN,
               A.MAT_KHAU               = CASE WHEN P_MAT_KHAU IS NOT NULL THEN P_MAT_KHAU ELSE A.MAT_KHAU END,
               A.CMND                   = P_CMND,
               A.DIA_CHI                = P_DIA_CHI,
               A.DI_DONG                = P_DI_DONG,
               A.HINH_ANH               = P_HINH_ANH,
               A.STT_CAN_BO             = P_STT_CAN_BO,
               A.TRANG_THAI             = P_TRANG_THAI,
               A.MA_QUYEN_LOAD_DAU_TIEN = P_MA_QUYEN_LOAD_DAU_TIEN,
               A.NGUOI_CAP_NHAT         = P_NGUOI_CAP_NHAT,
               A.NGAY_CAP_NHAT          = SYSDATE,
               A.DANG_KY_NHAC_VIEC=P_DANG_KY_NHAC_VIEC,
               A.GIOI_TINH=P_GIOI_TINH,
               A.NGAY_SINH=P_NGAY_SINH
         WHERE MA_CAN_BO = P_MA_CAN_BO;
        P_VAL := 'THANH_CONG';
    END IF;
  
  END;
  ------------------------------------------------------------------------------------
  PROCEDURE RESET_MAT_KHAU(P_VAL            OUT NVARCHAR2,
                          P_MA_CAN_BO      INT,
                          P_MAT_KHAU_MOI       NVARCHAR2) AS
  BEGIN
        UPDATE CB_CAN_BO SET MAT_KHAU = P_MAT_KHAU_MOI  WHERE MA_CAN_BO = P_MA_CAN_BO;
        P_VAL := 'THANH_CONG';
  END;
  --------------------------------------------------------------------------------
  PROCEDURE DELETE_CAN_BO(P_VAL       OUT NVARCHAR2,
                          P_NGUOI_XOA INT,
                          P_MA_CAN_BO INT) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM CB_CAN_BO WHERE MA_CAN_BO = P_MA_CAN_BO;
      P_VAL := 'THANH_CONG';
    END IF;
  END;
  --------------------------------------------------------------------------------
  PROCEDURE SELECT_CAN_BO_CHUA_PHAN_CT(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_CAN_BO A
       WHERE NOT EXISTS (SELECT 1
                FROM CB_THUOC_TO_CHUYEN_MON TCM
               WHERE A.MA_CAN_BO = TCM.MA_CAN_BO)
         AND NOT EXISTS (SELECT 1
                FROM CB_THUOC_TO_CHUYEN_MON B, CB_TO_CHUYEN_MON C, CB_DON_VI D
               WHERE A.MA_CAN_BO = B.MA_CAN_BO
               AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
               AND C.MA_DON_VI=D.MA_DON_VI
               AND D.MA_CO_QUAN=P_MA_CO_QUAN)
        AND A.MA_CO_QUAN=P_MA_CO_QUAN       
       ORDER BY A.STT_CAN_BO, A.TEN_CAN_BO;
  END;
  ------------------------------------------------------------------------------
  PROCEDURE SELECT_CAN_BO_BY_MDV_ALL_TR(P_CUR       IN OUT MYCUR,
                                        P_MA_DON_VI NVARCHAR2,
                                        P_SITE_ROOT NVARCHAR2,
                                        P_MA_CO_QUAN NVARCHAR2) AS
    P_VAL               NCLOB;
    V_MA_DON_VI         NVARCHAR2(100);
    V_TEN_DON_VI        NVARCHAR2(100);
    V_SO_CBHT_CUA_DV    INT;
    V_MA_TO_CHUYEN_MON  INT;
    V_TEN_TO_CHUYEN_MON NVARCHAR2(100);
    V_SO_CBHT_CUA_TO    INT;
    V_STT_TO_CHUYEN_MON INT;
    V_MA_CAN_BO         INT;
    V_TEN_CAN_BO        NVARCHAR2(100);
    V_TEN_CHUC_VU       NVARCHAR2(100);
    V_STT_CAN_BO        INT;
    V_TEN_TRANG_THAI    NVARCHAR2(100);
    V_ROW               INT;
    V_CLASS             NVARCHAR2(100);
    V_LINK              NVARCHAR2(200);
    V_HINH_ANH          NVARCHAR2(100);
    V_GIOI_TINH         INT;
    CURSOR C1 IS
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             LIB_FUN.GET_SO_LUONG_CBHT_CUA_DV(A.MA_DON_VI) AS SO_CBHT_CUA_DV
        FROM CB_DON_VI A
       WHERE UPPER(NVL(A.MA_DON_VI, '')) LIKE UPPER(NVL(P_MA_DON_VI, '%%'))
         AND LIB_FUN.GET_SO_LUONG_CBHT_CUA_DV(A.MA_DON_VI) > 0
         AND A.MA_CO_QUAN=P_MA_CO_QUAN
       ORDER BY A.STT_DON_VI;
    CURSOR C2 IS
      SELECT A.MA_TO_CHUYEN_MON,
             A.TEN_TO_CHUYEN_MON,
             LIB_FUN.GET_SO_LUONG_CBHT_CUA_TO(A.MA_TO_CHUYEN_MON) AS SO_CBHT_CUA_TO,
             A.STT_TO_CHUYEN_MON,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_TO_CHUYEN_MON A
       WHERE A.MA_DON_VI = V_MA_DON_VI
         AND LIB_FUN.GET_SO_LUONG_CBHT_CUA_TO(A.MA_TO_CHUYEN_MON) > 0
       ORDER BY A.STT_TO_CHUYEN_MON;
    CURSOR C3 IS
      SELECT A.MA_CAN_BO,
             A.TEN_CAN_BO,
             C.TEN_CHUC_VU,
             nvl(A.HINH_ANH,'N'),
             A.GIOI_TINH,
             A.STT_CAN_BO,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B, DM_CHUC_VU C
       WHERE A.MA_CAN_BO = B.MA_CAN_BO
         AND B.MA_CHUC_VU = C.MA_CHUC_VU
         AND B.MA_TO_CHUYEN_MON = V_MA_TO_CHUYEN_MON
         AND B.DEN_NGAY IS NULL
       ORDER BY A.STT_CAN_BO;
  BEGIN
    P_VAL := '';
    V_ROW := 0;
    OPEN C1;
    LOOP
      FETCH C1
        INTO V_MA_DON_VI, V_TEN_DON_VI, V_SO_CBHT_CUA_DV;
      EXIT WHEN C1%NOTFOUND;
      P_VAL := P_VAL || '<tr>
                               <td>
                               <img src="'|| P_SITE_ROOT || 'public/images/unit16.png">
                               <b>' || V_TEN_DON_VI ||
               '</b>(' || TO_CHAR(V_SO_CBHT_CUA_DV) || ')' ||
               '</td><td></td><td></td><td></td><td></td></tr>';
      OPEN C2;
      LOOP
        FETCH C2
          INTO V_MA_TO_CHUYEN_MON,
               V_TEN_TO_CHUYEN_MON,
               V_SO_CBHT_CUA_TO,
               V_STT_TO_CHUYEN_MON,
               V_TEN_TRANG_THAI;
        EXIT WHEN C2%NOTFOUND;
        P_VAL := P_VAL || '<tr>
                  <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                      <img border="0" align="absmiddle" src="'|| P_SITE_ROOT || 'public/images/user-group16.png">
                      <b>' || V_TEN_TO_CHUYEN_MON ||
                 '</b>(' || TO_CHAR(V_SO_CBHT_CUA_TO) || ')' ||
                 ' <td></td><td></td><td></td><td></td></tr>';
        OPEN C3;
        LOOP
          FETCH C3
            INTO V_MA_CAN_BO,
                 V_TEN_CAN_BO,
                 V_TEN_CHUC_VU,
                 V_HINH_ANH,
                 V_GIOI_TINH,
                 V_STT_CAN_BO,
                 V_TEN_TRANG_THAI;
          EXIT WHEN C3%NOTFOUND;
          IF V_HINH_ANH='N' THEN
            IF V_GIOI_TINH=1 THEN
              V_HINH_ANH:='avatar.png';
            ELSE
              V_HINH_ANH:='avatarnu.png';
            END IF;
          END IF;
          V_ROW   := V_ROW + 1;
          V_CLASS := 'row' || TO_CHAR(MOD(V_ROW, 2));
          V_LINK  := P_SITE_ROOT || 'canbo/canbo/dsp_single_canbo/&m=' ||
                     TO_CHAR(V_MA_CAN_BO);
          P_VAL   := P_VAL || '<tr class="' || V_CLASS || '">
                        <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                            <img border="0" align="absmiddle" width="40" height="40" src="'|| P_SITE_ROOT || 'uploads/avatar/'||V_HINH_ANH||'">
                            ' || V_TEN_CAN_BO || '
                        <td>' || V_TEN_CHUC_VU ||
                     '</td>
                        <td>' || TO_CHAR(V_STT_CAN_BO) ||
                     '</td>
                        <td style="text-align:center">' ||
                     V_TEN_TRANG_THAI ||
                     '</td>
                        <td style="text-align:center">
                            <a title="Cập nhật thông tin cán bộ" href="javascript:void(0)" onclick="sua_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) ||
                     ''')">Sửa</a> |
                            <a title="Xem quá trình công tác của cán bộ" href="javascript:void(0)" onclick="qtct_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) ||
                     ''')">Quá trình công tác</a> |
                            <a title="Phân quyền sử dụng hệ thống cho cán bộ" href="javascript:void(0)" onclick="phanquyen_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) || ''',''' ||
                     TO_CHAR(V_TEN_CAN_BO) || ''')">Phân quyền</a>
                        </td>
                      </tr>';
        END LOOP;
        CLOSE C3;
      END LOOP;
      CLOSE C2;
    END LOOP;
    CLOSE C1;
    OPEN P_CUR FOR
      SELECT P_VAL AS ALL_TR, V_ROW AS TONG_SO_DONG FROM DUAL;
  END;
  ------------------------------------------------------------------------------
  PROCEDURE SELECT_CAN_BO_BY_MTCM_ALL_TR(P_CUR              IN OUT MYCUR,
                                         P_MA_DON_VI        NVARCHAR2,
                                         P_MA_TO_CHUYEN_MON INT,
                                         P_SITE_ROOT        NVARCHAR2,
                                         P_MA_CO_QUAN NVARCHAR2) AS
    P_VAL               NCLOB;
    V_MA_DON_VI         NVARCHAR2(100);
    V_TEN_DON_VI        NVARCHAR2(100);
    V_SO_CBHT_CUA_DV    INT;
    V_MA_TO_CHUYEN_MON  INT;
    V_TEN_TO_CHUYEN_MON NVARCHAR2(100);
    V_SO_CBHT_CUA_TO    INT;
    V_STT_TO_CHUYEN_MON INT;
    V_MA_CAN_BO         INT;
    V_TEN_CAN_BO        NVARCHAR2(100);
    V_TEN_CHUC_VU       NVARCHAR2(100);
    V_STT_CAN_BO        INT;
    V_TEN_TRANG_THAI    NVARCHAR2(100);
    V_ROW               INT;
    V_CLASS             NVARCHAR2(100);
    V_LINK              NVARCHAR2(200);
    V_HINH_ANH          NVARCHAR2(100);
    V_GIOI_TINH         INT;
    CURSOR C1 IS
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             LIB_FUN.GET_SO_LUONG_CBHT_CUA_DV(A.MA_DON_VI) AS SO_CBHT_CUA_DV
        FROM CB_DON_VI A
       WHERE EXISTS (SELECT 1
                FROM CB_TO_CHUYEN_MON B, CB_THUOC_TO_CHUYEN_MON C
               WHERE A.MA_DON_VI = B.MA_DON_VI
                 AND A.MA_DON_VI = P_MA_DON_VI
                 AND B.MA_TO_CHUYEN_MON = C.MA_TO_CHUYEN_MON
                 AND C.DEN_NGAY IS NULL)
       AND A.MA_CO_QUAN=P_MA_CO_QUAN
       ORDER BY A.STT_DON_VI;
    CURSOR C2 IS
      SELECT A.MA_TO_CHUYEN_MON,
             A.TEN_TO_CHUYEN_MON,
             LIB_FUN.GET_SO_LUONG_CBHT_CUA_TO(A.MA_TO_CHUYEN_MON) AS SO_CBHT_CUA_TO,
             A.STT_TO_CHUYEN_MON,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_TO_CHUYEN_MON A
       WHERE A.MA_TO_CHUYEN_MON = CASE
               WHEN P_MA_TO_CHUYEN_MON = 0 THEN
                A.MA_TO_CHUYEN_MON
               ELSE
                P_MA_TO_CHUYEN_MON
             END
         AND A.MA_DON_VI = V_MA_DON_VI
         AND EXISTS (SELECT 1
                FROM CB_THUOC_TO_CHUYEN_MON B
               WHERE A.MA_TO_CHUYEN_MON = B.MA_TO_CHUYEN_MON
                 AND B.DEN_NGAY IS NULL)
       ORDER BY A.STT_TO_CHUYEN_MON;
    CURSOR C3 IS
      SELECT A.MA_CAN_BO, -- tim can bo cua to da tim dc o tren
             A.TEN_CAN_BO,
             C.TEN_CHUC_VU,
             nvl(A.HINH_ANH,'N'),
             A.GIOI_TINH,
             A.STT_CAN_BO,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B, DM_CHUC_VU C
       WHERE A.MA_CAN_BO = B.MA_CAN_BO
         AND B.MA_CHUC_VU = C.MA_CHUC_VU
         AND B.MA_TO_CHUYEN_MON = V_MA_TO_CHUYEN_MON
         AND B.DEN_NGAY IS NULL
       ORDER BY A.STT_CAN_BO;
  BEGIN
    P_VAL := '';
    V_ROW := 0;
    OPEN C1;
    LOOP
      FETCH C1
        INTO V_MA_DON_VI, V_TEN_DON_VI, V_SO_CBHT_CUA_DV;
      EXIT WHEN C1%NOTFOUND;
      P_VAL := P_VAL || '<tr>
                               <td>
                               <img border="0" align="absmiddle" width="40" height="40" src="'|| P_SITE_ROOT || 'uploads/avatar/'||V_HINH_ANH||'">
                               <b>' || V_TEN_DON_VI ||
               '</b>(' || TO_CHAR(V_SO_CBHT_CUA_DV) || ')' ||
               '</td><td></td><td></td><td></td><td></td></tr>';
      OPEN C2;
      LOOP
        FETCH C2
          INTO V_MA_TO_CHUYEN_MON,
               V_TEN_TO_CHUYEN_MON,
               V_SO_CBHT_CUA_TO,
               V_STT_TO_CHUYEN_MON,
               V_TEN_TRANG_THAI;
        EXIT WHEN C2%NOTFOUND;
        P_VAL := P_VAL || '<tr>
                  <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                      <img border="0" align="absmiddle" src="'|| P_SITE_ROOT || 'public/images/user-group16.png">
                      <b>' || V_TEN_TO_CHUYEN_MON ||
                 '</b>(' || TO_CHAR(V_SO_CBHT_CUA_TO) || ')' ||
                 ' <td></td><td></td><td></td><td></td></tr>';
        OPEN C3;
        LOOP
          FETCH C3
            INTO V_MA_CAN_BO,
                 V_TEN_CAN_BO,
                 V_TEN_CHUC_VU,
                 V_HINH_ANH,
                 V_GIOI_TINH,
                 V_STT_CAN_BO,
                 V_TEN_TRANG_THAI;
          EXIT WHEN C3%NOTFOUND;
          IF V_HINH_ANH='N' THEN
            IF V_GIOI_TINH=1 THEN
              V_HINH_ANH:='avatar.png';
            ELSE
              V_HINH_ANH:='avatarnu.png';
            END IF;
          END IF;
          V_ROW   := V_ROW + 1;
          V_CLASS := 'row' || TO_CHAR(MOD(V_ROW, 2));
          V_LINK  := P_SITE_ROOT || 'canbo/canbo/dsp_single_canbo/&m=' ||
                     TO_CHAR(V_MA_CAN_BO);
          P_VAL   := P_VAL || '<tr class="' || V_CLASS || '">
                        <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                            <img border="0" align="absmiddle" width="40" height="40" src="'|| P_SITE_ROOT || 'uploads/avatar/'||V_HINH_ANH||'">
                            ' || V_TEN_CAN_BO || '
                        <td>' || V_TEN_CHUC_VU ||
                     '</td>
                        <td>' || TO_CHAR(V_STT_CAN_BO) ||
                     '</td>
                        <td style="text-align:center">' ||
                     V_TEN_TRANG_THAI ||
                     '</td>
                        <td style="text-align:center">
                            <a title="Cập nhật thông tin cán bộ" href="javascript:void(0)" onclick="sua_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) ||
                     ''')">Sửa</a> |
                            <a title="Xem quá trình công tác của cán bộ" href="javascript:void(0)" onclick="qtct_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) ||
                     ''')">Quá trình công tác</a> |
                            <a title="Phân quyền sử dụng hệ thống cho cán bộ" href="javascript:void(0)" onclick="phanquyen_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) || ''',''' ||
                     TO_CHAR(V_TEN_CAN_BO) || ''')">Phân quyền</a>
                        </td>
                      </tr>';
        END LOOP;
        CLOSE C3;
      END LOOP;
      CLOSE C2;
    END LOOP;
    CLOSE C1;
    OPEN P_CUR FOR
      SELECT P_VAL AS ALL_TR, V_ROW AS TONG_SO_DONG FROM DUAL;
  END;
  ------------------------------------------------------------------------------
  PROCEDURE SELECT_CAN_BO_BY_MTCM_ALL_TR2(P_CUR              IN OUT MYCUR,
                                          P_MA_TO_CHUYEN_MON INT,
                                          P_SITE_ROOT        NVARCHAR2) AS
    P_VAL               NCLOB;
    V_MA_DON_VI         NVARCHAR2(100);
    V_TEN_DON_VI        NVARCHAR2(100);
    V_SO_CBHT_CUA_DV    INT;
    V_MA_TO_CHUYEN_MON  INT;
    V_TEN_TO_CHUYEN_MON NVARCHAR2(100);
    V_SO_CBHT_CUA_TO    INT;
    V_STT_TO_CHUYEN_MON INT;
    V_MA_CAN_BO         INT;
    V_TEN_CAN_BO        NVARCHAR2(100);
    V_TEN_CHUC_VU       NVARCHAR2(100);
    V_STT_CAN_BO        INT;
    V_TEN_TRANG_THAI    NVARCHAR2(100);
    V_ROW               INT;
    V_CLASS             NVARCHAR2(100);
    V_LINK              NVARCHAR2(200);
    CURSOR C1 IS
      SELECT A.MA_DON_VI,
             A.TEN_DON_VI,
             LIB_FUN.GET_SO_LUONG_CBHT_CUA_DV(A.MA_DON_VI) AS SO_CBHT_CUA_DV
        FROM CB_DON_VI A
       WHERE EXISTS (SELECT 1
                FROM CB_TO_CHUYEN_MON B, CB_THUOC_TO_CHUYEN_MON C
               WHERE A.MA_DON_VI = B.MA_DON_VI
                 AND B.MA_TO_CHUYEN_MON = C.MA_TO_CHUYEN_MON
                 AND B.MA_TO_CHUYEN_MON = CASE
                       WHEN P_MA_TO_CHUYEN_MON = 0 THEN
                        B.MA_TO_CHUYEN_MON
                       ELSE
                        P_MA_TO_CHUYEN_MON
                     END
                 AND C.DEN_NGAY IS NULL)
       ORDER BY A.STT_DON_VI;
    CURSOR C2 IS
      SELECT A.MA_TO_CHUYEN_MON, -- tim to theo ma to, neu ma to= 0 thi hien thi het to cua don vi vua tim dc o tren
             A.TEN_TO_CHUYEN_MON,
             LIB_FUN.GET_SO_LUONG_CBHT_CUA_TO(A.MA_TO_CHUYEN_MON) AS SO_CBHT_CUA_TO,
             A.STT_TO_CHUYEN_MON,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_TO_CHUYEN_MON A
       WHERE A.MA_TO_CHUYEN_MON = CASE
               WHEN P_MA_TO_CHUYEN_MON = 0 THEN
                A.MA_TO_CHUYEN_MON
               ELSE
                P_MA_TO_CHUYEN_MON
             END
         AND A.MA_DON_VI = V_MA_DON_VI
         AND EXISTS (SELECT 1
                FROM CB_THUOC_TO_CHUYEN_MON B
               WHERE A.MA_TO_CHUYEN_MON = B.MA_TO_CHUYEN_MON
                 AND B.DEN_NGAY IS NULL)
       ORDER BY A.STT_TO_CHUYEN_MON;
    CURSOR C3 IS
      SELECT A.MA_CAN_BO, -- tim can bo cua to da tim dc o tren
             A.TEN_CAN_BO,
             C.TEN_CHUC_VU,
             A.STT_CAN_BO,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B, DM_CHUC_VU C
       WHERE A.MA_CAN_BO = B.MA_CAN_BO
         AND B.MA_CHUC_VU = C.MA_CHUC_VU
         AND B.MA_TO_CHUYEN_MON = V_MA_TO_CHUYEN_MON
         AND B.DEN_NGAY IS NULL
       ORDER BY A.STT_CAN_BO;
  BEGIN
    P_VAL := '';
    V_ROW := 0;
    OPEN C1;
    LOOP
      FETCH C1
        INTO V_MA_DON_VI, V_TEN_DON_VI, V_SO_CBHT_CUA_DV;
      EXIT WHEN C1%NOTFOUND;
      P_VAL := P_VAL || '<tr>
                               <td>
                               <img src="'|| P_SITE_ROOT || 'public/images/unit16.png">
                               <b>' || V_TEN_DON_VI ||
               '</b>(' || TO_CHAR(V_SO_CBHT_CUA_DV) || ')' ||
               '</td><td></td><td></td><td></td><td></td></tr>';
      OPEN C2;
      LOOP
        FETCH C2
          INTO V_MA_TO_CHUYEN_MON,
               V_TEN_TO_CHUYEN_MON,
               V_SO_CBHT_CUA_TO,
               V_STT_TO_CHUYEN_MON,
               V_TEN_TRANG_THAI;
        EXIT WHEN C2%NOTFOUND;
        P_VAL := P_VAL || '<tr>
                  <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                      <img border="0" align="absmiddle" src="'|| P_SITE_ROOT || 'public/images/user-group16.png">
                      <b>' || V_TEN_TO_CHUYEN_MON ||
                 '</b>(' || TO_CHAR(V_SO_CBHT_CUA_TO) || ')' ||
                 ' <td></td><td></td><td></td><td></td></tr>';
        OPEN C3;
        LOOP
          FETCH C3
            INTO V_MA_CAN_BO,
                 V_TEN_CAN_BO,
                 V_TEN_CHUC_VU,
                 V_STT_CAN_BO,
                 V_TEN_TRANG_THAI;
          EXIT WHEN C3%NOTFOUND;
          V_ROW   := V_ROW + 1;
          V_CLASS := 'row' || TO_CHAR(MOD(V_ROW, 2));
          V_LINK  := P_SITE_ROOT || 'canbo/canbo/dsp_single_canbo/&m=' ||
                     TO_CHAR(V_MA_CAN_BO);
          P_VAL   := P_VAL || '<tr class="' || V_CLASS || '">
                        <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                            <img border="0" align="absmiddle" src="'|| P_SITE_ROOT || 'public/images/icon-16-user.png">
                            ' || V_TEN_CAN_BO || '
                        <td>' || V_TEN_CHUC_VU ||
                     '</td>
                        <td>' || TO_CHAR(V_STT_CAN_BO) ||
                     '</td>
                        <td style="text-align:center">' ||
                     V_TEN_TRANG_THAI ||
                     '</td>
                        <td style="text-align:center">
                            <a title="Cập nhật thông tin cán bộ" href="javascript:void(0)" onclick="sua_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) ||
                     ''')">Sửa</a> |
                            <a title="Xem quá trình công tác của cán bộ" href="javascript:void(0)" onclick="qtct_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) ||
                     ''')">Quá trình công tác</a> |
                            <a title="Phân quyền sử dụng hệ thống cho cán bộ" href="javascript:void(0)" onclick="phanquyen_canbo_click(''' ||
                     TO_CHAR(V_MA_CAN_BO) || ''',''' ||
                     TO_CHAR(V_TEN_CAN_BO) || ''')">Phân quyền</a>
                        </td>
                      </tr>';
        END LOOP;
        CLOSE C3;
      END LOOP;
      CLOSE C2;
    END LOOP;
    CLOSE C1;
    OPEN P_CUR FOR
      SELECT P_VAL AS ALL_TR, V_ROW AS TONG_SO_DONG FROM DUAL;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_ALL_TO_OF_CANBO(P_CUR       IN OUT MYCUR,
                                   P_MA_CAN_BO NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_TO_CHUYEN_MON,
             A.MA_CAN_BO,
             C.TEN_TO_CHUYEN_MON,
             A.MA_CHUC_VU,
             A.KIEM_NHIEM,
             TO_CHAR(A.TU_NGAY, 'DD/MM/YYYY') AS TU_NGAY,
             TO_CHAR(A.DEN_NGAY, 'DD/MM/YYYY') AS DEN_NGAY,
             B.TEN_CHUC_VU,
             D.TEN_DON_VI
        FROM CB_THUOC_TO_CHUYEN_MON A,
             DM_CHUC_VU          B,
             CB_TO_CHUYEN_MON    C,
             CB_DON_VI           D
       WHERE A.MA_CAN_BO = P_MA_CAN_BO
         AND A.MA_CHUC_VU = B.MA_CHUC_VU
         AND A.MA_TO_CHUYEN_MON = C.MA_TO_CHUYEN_MON
         AND C.MA_DON_VI = D.MA_DON_VI
       ORDER BY A.TU_NGAY DESC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_THUOC_TO_DETAIL(P_CUR              IN OUT MYCUR,
                                   P_MA_CAN_BO        INT,
                                   P_MA_TO_CHUYEN_MON INT,
                                   P_MA_CHUC_VU       NVARCHAR2,
                                   P_TU_NGAY          NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             TO_CHAR(A.TU_NGAY, 'dd/mm/yyyy') AS TU_NGAY2,
             TO_CHAR(A.DEN_NGAY, 'dd/mm/yyyy') AS DEN_NGAY2,
             B.TEN_DON_VI,
             C.TEN_TO_CHUYEN_MON,
             D.TEN_CHUC_VU
        FROM CB_THUOC_TO_CHUYEN_MON A,
             CB_DON_VI           B,
             CB_TO_CHUYEN_MON    C,
             DM_CHUC_VU          D
       WHERE A.MA_CAN_BO = P_MA_CAN_BO
         AND A.MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON
         AND A.MA_CHUC_VU = P_MA_CHUC_VU
         AND TO_CHAR(A.TU_NGAY, 'dd/mm/yyyy') = P_TU_NGAY
         AND A.MA_TO_CHUYEN_MON = C.MA_TO_CHUYEN_MON
         AND C.MA_DON_VI = B.MA_DON_VI
         AND A.MA_CHUC_VU = D.MA_CHUC_VU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE INSERT_THUOC_TO(P_VAL              OUT NVARCHAR2,
                            P_NGUOI_CAP_NHAT   INT,
                            P_MA_CAN_BO        INT,
                            P_MA_TO_CHUYEN_MON INT,
                            P_MA_CHUC_VU       NVARCHAR2,
                            P_KIEM_NHIEM       INT,
                            P_DUOC_PHAN_CONG_THU_LY INT,
                            P_DUOC_DUYET_HO_SO INT,
                            P_TU_NGAY          NVARCHAR2) AS
  
    V_TRUNG_PHAN_TO INT;
    V_MA_DON_VI NVARCHAR2(200);
    V_MA_PHUONG_XA NVARCHAR2(200);
  BEGIN
  
    P_VAL := 'CO_LOI';
    SELECT COUNT(MA_CAN_BO)
      INTO V_TRUNG_PHAN_TO
      FROM CB_THUOC_TO_CHUYEN_MON
     WHERE MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON
       AND MA_CAN_BO = P_MA_CAN_BO
       AND MA_CHUC_VU = P_MA_CHUC_VU
       and TO_CHAR(TU_NGAY, 'dd/mm/yyyy') = P_TU_NGAY;
    IF (V_TRUNG_PHAN_TO > 0) THEN
      P_VAL := 'TRUNG_PHAN_TO';
    ELSE    
      INSERT INTO CB_THUOC_TO_CHUYEN_MON
        (MA_CAN_BO,
         MA_TO_CHUYEN_MON,
         MA_CHUC_VU,
         KIEM_NHIEM,
         DUOC_PHAN_CONG_THU_LY,
         DUOC_DUYET_HO_SO,
         TU_NGAY,
         NGUOI_CAP_NHAT,
         NGAY_CAP_NHAT)
      VALUES
        (P_MA_CAN_BO,
         P_MA_TO_CHUYEN_MON,
         P_MA_CHUC_VU,
         P_KIEM_NHIEM,
         P_DUOC_PHAN_CONG_THU_LY,
         P_DUOC_DUYET_HO_SO,
         TO_DATE(P_TU_NGAY, 'DD/MM/YYYY'),
         P_NGUOI_CAP_NHAT,
         SYSDATE);
         
      SELECT T.MA_DON_VI INTO V_MA_DON_VI FROM CB_TO_CHUYEN_MON T WHERE T.MA_TO_CHUYEN_MON=P_MA_TO_CHUYEN_MON;   
      SELECT D.MA_PHUONG_XA INTO V_MA_PHUONG_XA FROM CB_DON_VI D WHERE D.MA_DON_VI=V_MA_DON_VI;
      IF V_MA_PHUONG_XA IS NOT NULL THEN
      UPDATE CB_CAN_BO C SET C.MA_PHUONG_XA=V_MA_PHUONG_XA WHERE C.MA_CAN_BO=P_MA_CAN_BO;
      END IF;
      P_VAL := 'THANH_CONG';
    END IF;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_THUOC_TO(P_VAL              OUT NVARCHAR2,
                            P_NGUOI_CAP_NHAT   INT,
                            P_MA_CAN_BO        INT,
                            P_MA_TO_CHUYEN_MON INT,
                            P_MA_CHUC_VU       NVARCHAR2,
                            P_KIEM_NHIEM       INT,
                            P_DUOC_PHAN_CONG_THU_LY INT,
                            P_DUOC_DUYET_HO_SO INT,
                            P_TU_NGAY          NVARCHAR2,
                            P_DEN_NGAY         NVARCHAR2) AS
  BEGIN
    UPDATE CB_THUOC_TO_CHUYEN_MON A
       SET A.KIEM_NHIEM     = P_KIEM_NHIEM,
           A.DEN_NGAY       = TO_DATE(P_DEN_NGAY, 'DD/MM/YYYY'),
           A.NGUOI_CAP_NHAT = P_NGUOI_CAP_NHAT,
           A.NGAY_CAP_NHAT  = SYSDATE,
           A.MA_CHUC_VU     = P_MA_CHUC_VU,
           A.DUOC_PHAN_CONG_THU_LY = P_DUOC_PHAN_CONG_THU_LY,
           A.DUOC_DUYET_HO_SO = P_DUOC_DUYET_HO_SO
     WHERE A.MA_CAN_BO = P_MA_CAN_BO
       AND A.MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON
       AND TO_CHAR(A.TU_NGAY, 'DD/MM/YYYY') = P_TU_NGAY;
    P_VAL := 'THANH_CONG';
  END;
  -------------------------------------------------------------------------------
  --LAY THONG TIN CA NHAN VA HIEN THI LEN POPUP CAP NHAT THONG TIN CA NHAN
  PROCEDURE SELECT_THONGTINCANHAN(P_CUR IN OUT MYCUR, P_MA_CAN_BO INT) AS
   V_KT INT;
  V_TON_TAI NVARCHAR2(100);
  BEGIN
     SELECT COUNT(1) INTO V_KT FROM  CB_THUOC_TO_CHUYEN_MON A, CB_TO_CHUYEN_MON B
    WHERE A.MA_CAN_BO=P_MA_CAN_BO
    AND A.MA_TO_CHUYEN_MON=B.MA_TO_CHUYEN_MON
    AND B.MA_DON_VI='PQLDD';
    IF V_KT=0 THEN
      V_TON_TAI:='';
     ELSE
      V_TON_TAI:='OK';
    END IF;
    IF V_TON_TAI IS NOT NULL THEN
      
    OPEN P_CUR FOR
      SELECT A.MA_CAN_BO,
             A.TEN_CAN_BO,
             a.CMND,
             A.DIA_CHI,
             A.DI_DONG,
             A.HINH_ANH,
             A.MA_QUYEN_LOAD_DAU_TIEN,
             A.NGAY_CAP_NHAT,
             E.TEN_CHUC_VU,
             (
             SELECT CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT('Tổng số hồ sơ là: ',
             H.TONG_HS),
             '; có '),
              A.TONG_6),
              ' hồ sơ sắp hết hạn trong 6 tháng, có '),
               B.TONG_3),
            ' hồ sơ sắp hết hạn trong 3 tháng; có '),
             C.TONG_1),
             ' hồ sơ sắp hết hạn trong 1 tháng;') 
            FROM
            (SELECT COUNT(MA_HS_DAT) as TONG_HS FROM DC_HO_SO_DAT WHERE TRANG_THAI<>'KSD' ) H
            ,(SELECT COUNT(MA_HS_DAT) as TONG_6 
            FROM DC_HO_SO_DAT A, DC_LUY_TIEN B
            WHERE A.MA_THUA=B.MA_THUA
            AND B.SU_DUNG=0
            AND ((TG_KT- TRUNC (SYSDATE))/30)<=6
            AND ((TG_KT- TRUNC (SYSDATE))/30)>3)A
            ,(SELECT COUNT(MA_HS_DAT) as TONG_3 
            FROM DC_HO_SO_DAT A, DC_LUY_TIEN B
            WHERE A.MA_THUA=B.MA_THUA
            AND B.SU_DUNG=0
            AND ((TG_KT- TRUNC (SYSDATE))/30)<=3
            AND ((TG_KT- TRUNC (SYSDATE))/30)>1)B
            ,(SELECT COUNT(MA_HS_DAT) as TONG_1 
            FROM DC_HO_SO_DAT A, DC_LUY_TIEN B
            WHERE A.MA_THUA=B.MA_THUA
            AND B.SU_DUNG=0
            AND ((TG_KT- TRUNC (SYSDATE))/30)<=1) C
             ) AS CHUOI_NHAC_VIEC,
             A.GIOI_TINH,
             A.NGAY_SINH,
             TO_CHAR(A.NGAY_DANG_KY_OTP, 'DD/MM/YYYY HH24:MI:SS') AS NGAY_DANG_KY_OTP
        FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B, DM_CHUC_VU E
       WHERE A.MA_CAN_BO = P_MA_CAN_BO
       AND A.MA_CAN_BO=B.MA_CAN_BO
       AND B.MA_CHUC_VU=E.MA_CHUC_VU
       AND B.KIEM_NHIEM=0
       AND B.DEN_NGAY IS NULL;
       ELSE
             OPEN P_CUR FOR
      SELECT A.MA_CAN_BO,
             A.TEN_CAN_BO,
             a.CMND,
             A.DIA_CHI,
             A.DI_DONG,
             A.HINH_ANH,
             A.MA_QUYEN_LOAD_DAU_TIEN,
             A.NGAY_CAP_NHAT,
             E.TEN_CHUC_VU,
             null AS CHUOI_NHAC_VIEC,
             A.GIOI_TINH,
             A.NGAY_SINH,
             TO_CHAR(A.NGAY_DANG_KY_OTP, 'DD/MM/YYYY HH24:MI:SS') AS NGAY_DANG_KY_OTP
        FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B, DM_CHUC_VU E
       WHERE A.MA_CAN_BO = P_MA_CAN_BO
       AND A.MA_CAN_BO=B.MA_CAN_BO
       AND B.MA_CHUC_VU=E.MA_CHUC_VU
       AND B.KIEM_NHIEM=0
       AND B.DEN_NGAY IS NULL;
         END IF;
  
  END;
  --------------------------------------------------------------------------------
  --Lấy các nhóm quyền được phân, để chọn quyền load đầu tiên
  PROCEDURE SELECT_QUYEN_DUOC_PHAN(P_CUR IN OUT MYCUR, P_MA_CAN_BO INT) AS
  BEGIN
    OPEN P_CUR FOR
      select distinct a.ma_quyen, a.ten_quyen
        from pq_quyen a, pq_co_nhom_quyen b, pq_co_quyen c
       where b.ma_can_bo = P_MA_CAN_BO
         and b.ma_nhom_quyen = c.ma_nhom_quyen
         and a.ma_quyen = c.ma_quyen
         and (a.hien_thi_tren_menu=1 or (a.hien_thi_tren_menu=0 and a.ma_quyen='TRA_CUU'));
  END;
  --------------------------------------------------------------------------------
  --cập nhật thông tin cá nhân
  
  PROCEDURE UPDATE_THONGTIN_CANHAN(P_VAL            OUT NVARCHAR2,
                          P_MA_CAN_BO      INT,
                          P_TEN_CAN_BO     NVARCHAR2,
                          P_CMND           NVARCHAR2,
                          P_DIA_CHI        NVARCHAR2,
                          P_DI_DONG        NVARCHAR2,
                          P_HINH_ANH       NVARCHAR2,                       
                          P_MA_QUYEN_LOAD_DAU_TIEN NVARCHAR2,
                          P_GIOI_TINH              NVARCHAR2,
                          P_NGAY_SINH              NVARCHAR2) AS
    
  BEGIN
    P_VAL := 'CO_LOI';
   
        UPDATE CB_CAN_BO
           SET TEN_CAN_BO             = P_TEN_CAN_BO,
              
               CMND                   = P_CMND,
               DIA_CHI                = P_DIA_CHI,
               DI_DONG                = P_DI_DONG,
               HINH_ANH               = P_HINH_ANH,               
               MA_QUYEN_LOAD_DAU_TIEN = P_MA_QUYEN_LOAD_DAU_TIEN,
               NGUOI_CAP_NHAT         = P_MA_CAN_BO,
               NGAY_CAP_NHAT          = SYSDATE,
               GIOI_TINH              = P_GIOI_TINH,
               NGAY_SINH              = P_NGAY_SINH
         WHERE MA_CAN_BO = P_MA_CAN_BO;
        P_VAL := 'THANH_CONG';
      
  
  END;
  
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CO_QUAN_FILTER(P_CUR                IN OUT MYCUR,
                                 P_MA_LOAI_CO_QUAN     NVARCHAR2,
                                 P_MA_QUAN_HUYEN       NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
                             B.TEN_LOAI_CO_QUAN,
                             C.TEN_QUAN_HUYEN,
                             CASE
                               WHEN A.TRANG_THAI = 0 THEN
                                'Mở'
                               ELSE
                                'Ðóng'
                             END AS TEN_TRANG_THAI
                        FROM CB_CO_QUAN      A,
                             CB_LOAI_CO_QUAN B,
                             DM_QUAN_HUYEN   C
                       WHERE A.MA_LOAI_CO_QUAN = B.MA_LOAI_CO_QUAN
                         AND A.MA_QUAN_HUYEN = C.MA_QUAN_HUYEN
                         AND (P_MA_QUAN_HUYEN IS NULL OR UPPER(A.MA_QUAN_HUYEN)=UPPER(P_MA_QUAN_HUYEN))
                         AND (P_MA_LOAI_CO_QUAN IS NULL OR UPPER(A.MA_LOAI_CO_QUAN)=UPPER(P_MA_LOAI_CO_QUAN))
                       ORDER BY A.STT_CO_QUAN,
                                A.TEN_CO_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CO_QUAN_DETAIL(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             B.TEN_LOAI_CO_QUAN,
             C.TEN_QUAN_HUYEN,
             CASE WHEN A.TRANG_THAI = 0 THEN 'Mở' ELSE 'Ðóng' END AS TEN_TRANG_THAI
        FROM CB_CO_QUAN      A,
             CB_LOAI_CO_QUAN B,
             DM_QUAN_HUYEN   C
       WHERE A.MA_CO_QUAN LIKE P_MA_CO_QUAN
         AND A.MA_LOAI_CO_QUAN = B.MA_LOAI_CO_QUAN
         AND A.MA_QUAN_HUYEN = C.MA_QUAN_HUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_CO_QUAN(P_VAL                OUT NVARCHAR2,
                          P_MA_CO_QUAN          NVARCHAR2,
                          P_TEN_CO_QUAN         NVARCHAR2,
                          P_MA_LOAI_CO_QUAN     NVARCHAR2,
                          P_MA_QUAN_HUYEN       NVARCHAR2,
                          P_MA_TAT_CO_QUAN NVARCHAR2,
                          P_APPS NVARCHAR2,
                          P_DIA_CHI_CO_QUAN NVARCHAR2,
                          P_DIEN_THOAI_CO_QUAN NVARCHAR2,
                          P_LOGO_CO_QUAN NVARCHAR2,
                          P_STT_CO_QUAN INT,
                          P_TRANG_THAI         INT) 
  AS
    V_COUNT NUMBER;                        
  BEGIN
    SELECT COUNT(1) INTO V_COUNT
    FROM CB_CO_QUAN
    WHERE MA_CO_QUAN = P_MA_CO_QUAN;
    
    IF V_COUNT = 0 THEN
      INSERT INTO CB_CO_QUAN
        (MA_CO_QUAN,
         TEN_CO_QUAN,
         MA_TAT_CO_QUAN,
         MA_LOAI_CO_QUAN,
         MA_QUAN_HUYEN,
         APPS,
         DIA_CHI_CO_QUAN,
         DIEN_THOAI_CO_QUAN,
         LOGO,
         STT_CO_QUAN,
         TRANG_THAI)
      VALUES
        (P_MA_CO_QUAN,
         P_TEN_CO_QUAN,
         P_MA_TAT_CO_QUAN,
         P_MA_LOAI_CO_QUAN,
         P_MA_QUAN_HUYEN,
         P_APPS,
         P_DIA_CHI_CO_QUAN,
         P_DIEN_THOAI_CO_QUAN,
         P_LOGO_CO_QUAN,
         P_STT_CO_QUAN,
         P_TRANG_THAI);
      P_VAL := 'THANH_CONG';
    ELSE
      UPDATE CB_CO_QUAN
         SET TEN_CO_QUAN         = P_TEN_CO_QUAN,
             MA_TAT_CO_QUAN      = P_MA_TAT_CO_QUAN,
             MA_LOAI_CO_QUAN     = P_MA_LOAI_CO_QUAN,
             MA_QUAN_HUYEN       = P_MA_QUAN_HUYEN,
             APPS                = P_APPS,
             DIA_CHI_CO_QUAN     = P_DIA_CHI_CO_QUAN,
             DIEN_THOAI_CO_QUAN  = P_DIEN_THOAI_CO_QUAN,
             LOGO                = P_LOGO_CO_QUAN,
             STT_CO_QUAN         = P_STT_CO_QUAN,
             TRANG_THAI          = P_TRANG_THAI
       WHERE MA_CO_QUAN = P_MA_CO_QUAN;
      P_VAL := 'THANH_CONG';
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_CO_QUAN(P_VAL       OUT NVARCHAR2,
                          P_NGUOI_XOA INT,
                          P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM CB_CO_QUAN WHERE MA_CO_QUAN = P_MA_CO_QUAN;
      P_VAL := 'THANH_CONG';
    END IF;
  END;  
  PROCEDURE UPDATE_THUOC_TO_NHANH(P_VAL              OUT NVARCHAR2,
                            P_NGUOI_CAP_NHAT   INT,
                            P_MA_CAN_BO        INT,
                            P_MA_TO_CHUYEN_MON INT,
                            P_MA_TO_CU INT,
                            P_MA_CHUC_VU       NVARCHAR2) AS
  V_MA_DON_VI NVARCHAR2(200);
  V_MA_PHUONG_XA NVARCHAR2(200);
  BEGIN
    UPDATE CB_THUOC_TO_CHUYEN_MON A
       SET A.NGUOI_CAP_NHAT = P_NGUOI_CAP_NHAT,
           A.NGAY_CAP_NHAT  = SYSDATE,
           A.MA_TO_CHUYEN_MON= P_MA_TO_CHUYEN_MON,
           A.MA_CHUC_VU     = P_MA_CHUC_VU
     WHERE A.MA_CAN_BO = P_MA_CAN_BO
     AND A.MA_TO_CHUYEN_MON=P_MA_TO_CU;
     
     SELECT T.MA_DON_VI INTO V_MA_DON_VI FROM CB_TO_CHUYEN_MON T WHERE T.MA_TO_CHUYEN_MON=P_MA_TO_CHUYEN_MON;   
     SELECT D.MA_PHUONG_XA INTO V_MA_PHUONG_XA FROM CB_DON_VI D WHERE D.MA_DON_VI=V_MA_DON_VI;
     IF V_MA_PHUONG_XA IS NOT NULL THEN
         UPDATE CB_CAN_BO C SET C.MA_PHUONG_XA=V_MA_PHUONG_XA WHERE C.MA_CAN_BO=P_MA_CAN_BO;
     END IF;
    P_VAL := 'THANH_CONG';
  END; 
 -------------------------------------------------------------------------------
PROCEDURE SELECT_CAN_BO_MOI_THEM_DETAIL(P_CUR IN OUT MYCUR,P_MA_CAN_BO NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT * FROM
    CB_CAN_BO WHERE MA_CAN_BO=P_MA_CAN_BO;
  END;

 -------------------------------------------------------------------------------
 PROCEDURE TIM_KIEM_CAN_BO(P_CUR IN OUT MYCUR,
                          P_MA_DON_VI NVARCHAR2,
                          P_MA_TO_CHUYEN_MON NVARCHAR2,
                          P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN       
    OPEN P_CUR FOR
      SELECT A.MA_CAN_BO,A.TEN_CAN_BO,A.STT_CAN_BO,
             A.TAI_KHOAN,A.CMND,A.DI_DONG,
              CASE WHEN A.GIOI_TINH=1 THEN 'Nam' ELSE 'Nữ' END AS GIOI_TINH,
              A.NGAY_SINH,A.DIA_CHI,B.MA_CHUC_VU,E.TEN_CHUC_VU,
              D.MA_DON_VI,D.TEN_DON_VI,
              C.MA_TO_CHUYEN_MON,C.TEN_TO_CHUYEN_MON,
              C.STT_TO_CHUYEN_MON, D.STT_DON_VI,
              MC_PQ.GET_ALL_NHOM_QUYEN_CUA_CAN_BO(A.MA_CAN_BO) AS NHOM_QUYEN,
               CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
              FROM /*CB_CAN_BO A,CB_THUOC_TO_CHUYEN_MON B, CB_TO_CHUYEN_MON C, CB_DON_VI D*/
              CB_CAN_BO A LEFT JOIN CB_THUOC_TO_CHUYEN_MON B ON A.MA_CAN_BO=B.MA_CAN_BO
              LEFT JOIN CB_TO_CHUYEN_MON C ON B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
              LEFT JOIN CB_DON_VI D ON C.MA_DON_VI=D.MA_DON_VI
              LEFT JOIN DM_CHUC_VU E ON B.MA_CHUC_VU=E.MA_CHUC_VU
              WHERE 
              /*A.MA_CAN_BO=B.MA_CAN_BO
              AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
              AND C.MA_DON_VI=D.MA_DON_VI
              AND */D.MA_CO_QUAN=P_MA_CO_QUAN 
              AND B.DEN_NGAY IS NULL
              AND (P_MA_DON_VI IS NULL OR D.MA_DON_VI = P_MA_DON_VI)
              AND (P_MA_TO_CHUYEN_MON IS NULL OR C.MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON)
              ORDER BY D.STT_DON_VI,C.STT_TO_CHUYEN_MON,A.STT_CAN_BO;
  END; 
 -------------------------------------------------------------------------------
PROCEDURE UPDATE_CAN_BO(P_VAL            OUT NVARCHAR2,
                          P_UPDATE_TYPE    NVARCHAR2,
                          P_NGUOI_CAP_NHAT INT,
                          P_MA_CAN_BO      INT,
                          P_TEN_CAN_BO     NVARCHAR2,
                          P_TAI_KHOAN      NVARCHAR2,
                          P_MAT_KHAU       NVARCHAR2,
                          P_CMND           NVARCHAR2,                                                
                          P_DIA_CHI        NVARCHAR2,
                          P_DI_DONG        NVARCHAR2,
                          P_HINH_ANH       NVARCHAR2,
                          P_STT_CAN_BO             INT,
                          P_TRANG_THAI             INT,
                          P_MA_QUYEN_LOAD_DAU_TIEN NVARCHAR2,
                          P_DANG_KY_NHAC_VIEC INT,                          
                          P_GIOI_TINH      NUMBER,
                          P_NGAY_SINH      NVARCHAR2,
                          P_MA_CO_QUAN NVARCHAR2,
                          P_MA_TO_CHUYEN_MON NVARCHAR2,
                          P_MA_CHUC_VU NVARCHAR2) AS
    V_TON_TAI_TEN INT;
    V_MA_CAN_BO INT;
    V_MA_DON_VI NVARCHAR2(200);
    V_MA_PHUONG_XA NVARCHAR2(200);
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_CAN_BO)
        INTO V_TON_TAI_TEN
        FROM CB_CAN_BO
       WHERE UPPER(TAI_KHOAN) LIKE UPPER(P_TAI_KHOAN);
      IF V_TON_TAI_TEN > 0 THEN
        --kiem tra trung ten dang nhap
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO CB_CAN_BO
          (TEN_CAN_BO,
           TAI_KHOAN,
           MAT_KHAU,
           CMND,
           DIA_CHI,
           DI_DONG,
           HINH_ANH,
           STT_CAN_BO,
           TRANG_THAI,
           MA_QUYEN_LOAD_DAU_TIEN,
           NGUOI_CAP_NHAT,
           NGAY_CAP_NHAT,
           DANG_KY_NHAC_VIEC,
           GIOI_TINH,
           NGAY_SINH,
           MA_CO_QUAN)
        VALUES
          (P_TEN_CAN_BO,
           P_TAI_KHOAN,
           P_MAT_KHAU,
           P_CMND,
           P_DIA_CHI,
           P_DI_DONG,
           P_HINH_ANH,
           P_STT_CAN_BO,
           P_TRANG_THAI,
           P_MA_QUYEN_LOAD_DAU_TIEN,
           P_NGUOI_CAP_NHAT,
           SYSDATE,
           P_DANG_KY_NHAC_VIEC,
           P_GIOI_TINH,
           P_NGAY_SINH,
           P_MA_CO_QUAN);
           ---------------------------
           SELECT MA_CAN_BO INTO V_MA_CAN_BO FROM CB_CAN_BO WHERE TAI_KHOAN=P_TAI_KHOAN; 
           ----------------------------
           INSERT INTO CB_THUOC_TO_CHUYEN_MON
                             (MA_CAN_BO,
                             MA_TO_CHUYEN_MON,
                             MA_CHUC_VU,
                             TU_NGAY,
                             NGUOI_CAP_NHAT,
                             NGAY_CAP_NHAT)
                      VALUES(
                             V_MA_CAN_BO,
                             P_MA_TO_CHUYEN_MON,
                             P_MA_CHUC_VU,
                             SYSDATE,
                             P_NGUOI_CAP_NHAT,
                             SYSDATE
                             );
      SELECT T.MA_DON_VI INTO V_MA_DON_VI FROM CB_TO_CHUYEN_MON T WHERE T.MA_TO_CHUYEN_MON=P_MA_TO_CHUYEN_MON;   
      SELECT D.MA_PHUONG_XA INTO V_MA_PHUONG_XA FROM CB_DON_VI D WHERE D.MA_DON_VI=V_MA_DON_VI;
      IF V_MA_PHUONG_XA IS NOT NULL THEN
         UPDATE CB_CAN_BO C SET C.MA_PHUONG_XA=V_MA_PHUONG_XA WHERE C.MA_CAN_BO=V_MA_CAN_BO;
      END IF;
        P_VAL := 'THANH_CONG';
      END IF;    
    ELSE
        UPDATE CB_CAN_BO A
           SET A.TEN_CAN_BO             = P_TEN_CAN_BO,
               A.TAI_KHOAN              = P_TAI_KHOAN,
               A.MAT_KHAU               = CASE WHEN P_MAT_KHAU IS NOT NULL THEN P_MAT_KHAU ELSE A.MAT_KHAU END,
               A.CMND                   = P_CMND,
               A.DIA_CHI                = P_DIA_CHI,
               A.DI_DONG                = P_DI_DONG,
               A.HINH_ANH               = P_HINH_ANH,
               A.STT_CAN_BO             = P_STT_CAN_BO,
               A.TRANG_THAI             = P_TRANG_THAI,
               A.MA_QUYEN_LOAD_DAU_TIEN = P_MA_QUYEN_LOAD_DAU_TIEN,
               A.NGUOI_CAP_NHAT         = P_NGUOI_CAP_NHAT,
               A.NGAY_CAP_NHAT          = SYSDATE,
               A.DANG_KY_NHAC_VIEC=P_DANG_KY_NHAC_VIEC,
               A.GIOI_TINH=P_GIOI_TINH,
               A.NGAY_SINH=P_NGAY_SINH
         WHERE MA_CAN_BO = P_MA_CAN_BO;
        P_VAL := 'THANH_CONG';
    END IF;  
  END;   
PROCEDURE SELECT_DANH_SACH_CAN_BO(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2,P_MA_LOAI_DON_VI NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.*,TO_CHAR(DI_DONG) AS DI_DONG_T,
    CASE
       WHEN A.GIOI_TINH = 0 THEN
        'Nữ'
       ELSE
        'Nam'
       END AS GIOI_TINH_CT,
    TEN_TO_CHUYEN_MON,TEN_DON_VI,TEN_CHUC_VU
    FROM CB_CAN_BO A,CB_DON_VI B, CB_TO_CHUYEN_MON C,CB_THUOC_TO_CHUYEN_MON D,DM_CHUC_VU E
    WHERE B.MA_DON_VI=C.MA_DON_VI
    AND (B.MA_LOAI_DON_VI=P_MA_LOAI_DON_VI OR P_MA_LOAI_DON_VI IS NULL)
    AND C.MA_TO_CHUYEN_MON=D.MA_TO_CHUYEN_MON
    AND D.MA_CAN_BO=A.MA_CAN_BO
    AND A.MA_CO_QUAN=P_MA_CO_QUAN
    AND D.MA_CHUC_VU=E.MA_CHUC_VU
    ORDER BY TEN_DON_VI,TEN_TO_CHUYEN_MON DESC;
  END;
PROCEDURE SELECT_DM_CAN_BO(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2,P_MA_DON_VI NVARCHAR2,P_MA_TO_CHUYEN_MON NVARCHAR2) AS
  BEGIN
    IF P_MA_TO_CHUYEN_MON<>0 THEN
      OPEN P_CUR FOR SELECT A.*,D.MA_DON_VI,TEN_DON_VI,TEN_TO_CHUYEN_MON,C.MA_TO_CHUYEN_MON,TEN_CHUC_VU,LIB_FUN.GET_SO_LUONG_CBHT_CUA_DV(D.MA_DON_VI) AS SL_DON_VI,LIB_FUN.GET_SO_LUONG_CBHT_CUA_TO(C.MA_TO_CHUYEN_MON) AS SL_TO
      FROM CB_CAN_BO A,CB_THUOC_TO_CHUYEN_MON B,CB_TO_CHUYEN_MON C,CB_DON_VI D,DM_CHUC_VU E
      WHERE A.MA_CAN_BO=B.MA_CAN_BO
      AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
      AND C.MA_DON_VI=D.MA_DON_VI
      AND A.MA_CO_QUAN=P_MA_CO_QUAN
      AND B.MA_CHUC_VU=E.MA_CHUC_VU
      AND D.MA_DON_VI LIKE  P_MA_DON_VI 
      AND C.MA_TO_CHUYEN_MON=P_MA_TO_CHUYEN_MON
      AND B.DEN_NGAY IS NULL
      ORDER BY TEN_DON_VI,TEN_TO_CHUYEN_MON,A.STT_CAN_BO;
    ELSE
      OPEN P_CUR FOR SELECT A.*,D.MA_DON_VI,TEN_DON_VI,TEN_TO_CHUYEN_MON,C.MA_TO_CHUYEN_MON,TEN_CHUC_VU,LIB_FUN.GET_SO_LUONG_CBHT_CUA_DV(D.MA_DON_VI) AS SL_DON_VI,LIB_FUN.GET_SO_LUONG_CBHT_CUA_TO(C.MA_TO_CHUYEN_MON) AS SL_TO
      FROM CB_CAN_BO A,CB_THUOC_TO_CHUYEN_MON B,CB_TO_CHUYEN_MON C,CB_DON_VI D,DM_CHUC_VU E
      WHERE A.MA_CAN_BO=B.MA_CAN_BO
      AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
      AND C.MA_DON_VI=D.MA_DON_VI
      AND A.MA_CO_QUAN=P_MA_CO_QUAN
      AND B.MA_CHUC_VU=E.MA_CHUC_VU
      AND D.MA_DON_VI LIKE  P_MA_DON_VI 
      AND B.DEN_NGAY IS NULL
      ORDER BY TEN_DON_VI,TEN_TO_CHUYEN_MON,A.STT_CAN_BO;
    END IF;
  END; 
  PROCEDURE SELECT_DM_CAN_BO_LDV(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2,P_MA_LOAI_DON_VI NVARCHAR2) AS
  BEGIN
      OPEN P_CUR FOR SELECT A.*,D.MA_DON_VI,TEN_DON_VI,TEN_TO_CHUYEN_MON,C.MA_TO_CHUYEN_MON,TEN_CHUC_VU,LIB_FUN.GET_SO_LUONG_CBHT_CUA_DV(D.MA_DON_VI) AS SL_DON_VI,LIB_FUN.GET_SO_LUONG_CBHT_CUA_TO(C.MA_TO_CHUYEN_MON) AS SL_TO
      FROM CB_CAN_BO A,CB_THUOC_TO_CHUYEN_MON B,CB_TO_CHUYEN_MON C,CB_DON_VI D,DM_CHUC_VU E
      WHERE A.MA_CAN_BO=B.MA_CAN_BO
      AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
      AND C.MA_DON_VI=D.MA_DON_VI
      AND A.MA_CO_QUAN=P_MA_CO_QUAN
      AND B.MA_CHUC_VU=E.MA_CHUC_VU
      AND D.MA_LOAI_DON_VI=P_MA_LOAI_DON_VI
      AND B.DEN_NGAY IS NULL
      ORDER BY TEN_DON_VI,TEN_TO_CHUYEN_MON,A.STT_CAN_BO;
  END;     
END MC_CB;
/

prompt
prompt Creating package body MC_DM
prompt ===========================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.MC_DM IS
 
-------------------------------------------------------------------------------
  PROCEDURE SELECT_PHUONG_XA_FILTER(P_CUR IN OUT MYCUR,
                                    P_MA_QUAN_HUYEN NVARCHAR2,
                                    P_MA_TINH_THANH NVARCHAR2) AS
  V_KT INT;
  V_MA_QUAN_HUYEN NVARCHAR2(100);
  BEGIN
    SELECT COUNT(1) INTO V_KT FROM DM_QUAN_HUYEN C 
    WHERE C.MA_TINH_THANH=P_MA_TINH_THANH
    AND C.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN;
    IF V_KT=0 THEN
      V_MA_QUAN_HUYEN:='';
     ELSE
      V_MA_QUAN_HUYEN:=P_MA_QUAN_HUYEN;
    END IF;
    IF V_MA_QUAN_HUYEN IS NOT NULL THEN
    OPEN P_CUR FOR SELECT A.*,B.TEN_QUAN_HUYEN,
                     CASE
                       WHEN A.TRANG_THAI = 0 THEN
                        'Mở'
                       ELSE
                        'Ðóng'
                     END AS TEN_TRANG_THAI
                FROM DM_PHUONG_XA A, DM_QUAN_HUYEN B
                WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
                AND A.MA_QUAN_HUYEN=V_MA_QUAN_HUYEN
               ORDER BY A.MA_QUAN_HUYEN,A.STT_PHUONG_XA,A.TEN_PHUONG_XA;
    ELSE 
      OPEN P_CUR FOR SELECT A.*,B.TEN_QUAN_HUYEN,
                     CASE
                       WHEN A.TRANG_THAI = 0 THEN
                        'Mở'
                       ELSE
                        'Ðóng'
                     END AS TEN_TRANG_THAI
                FROM DM_PHUONG_XA A, DM_QUAN_HUYEN B
                WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
                AND A.MA_QUAN_HUYEN IN (SELECT C.MA_QUAN_HUYEN FROM DM_QUAN_HUYEN C WHERE C.MA_TINH_THANH=P_MA_TINH_THANH AND ROWNUM=1)
               ORDER BY A.MA_QUAN_HUYEN,A.STT_PHUONG_XA,A.TEN_PHUONG_XA;
        END IF;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_PHUONG_XA_DETAIL(P_CUR          IN OUT MYCUR,
                                    P_MA_PHUONG_XA NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,B.MA_TINH_THANH,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_PHUONG_XA A, DM_QUAN_HUYEN B
       WHERE 
       A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN 
       AND A.MA_PHUONG_XA LIKE P_MA_PHUONG_XA;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_PHUONG_XA(P_VAL            OUT NVARCHAR2,
                             P_UPDATE_TYPE    NVARCHAR2,
                             P_NGUOI_CAP_NHAT INT,
                             P_MA_PHUONG_XA   NVARCHAR2,
                             P_TEN_PHUONG_XA  NVARCHAR2,
                             P_MA_QUAN_HUYEN  NVARCHAR2,
                             P_STT_PHUONG_XA  INT,
                             P_TRANG_THAI     INT) AS
    V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    
    IF P_UPDATE_TYPE= 'THEM_MOI' THEN 
      SELECT COUNT(MA_PHUONG_XA)
         INTO V_TON_TAI_MA
         FROM DM_PHUONG_XA
      WHERE UPPER(MA_PHUONG_XA) LIKE UPPER(P_MA_PHUONG_XA);
      
      IF V_TON_TAI_MA > 0 OR P_MA_PHUONG_XA='0' THEN  -- kiem tra trung ma , khong cho them ma='0'
          P_VAL := 'TON_TAI_MA';
      ELSE
          INSERT INTO DM_PHUONG_XA
            (MA_PHUONG_XA,
             TEN_PHUONG_XA,
             MA_QUAN_HUYEN,
             STT_PHUONG_XA,
             TRANG_THAI,
             NGUOI_CAP_NHAT,
             NGAY_CAP_NHAT)
          VALUES
            (P_MA_PHUONG_XA,
             P_TEN_PHUONG_XA,
             P_MA_QUAN_HUYEN,
             P_STT_PHUONG_XA,
             P_TRANG_THAI,
             P_NGUOI_CAP_NHAT,
             SYSDATE);
      P_VAL := 'THANH_CONG';
      END IF;
    ELSE 
        UPDATE DM_PHUONG_XA
           SET TEN_PHUONG_XA = P_TEN_PHUONG_XA,
               MA_QUAN_HUYEN=P_MA_QUAN_HUYEN,
               STT_PHUONG_XA = P_STT_PHUONG_XA,
               TRANG_THAI      = P_TRANG_THAI,
               NGUOI_CAP_NHAT  = P_NGUOI_CAP_NHAT,
               NGAY_CAP_NHAT   = SYSDATE
         WHERE MA_PHUONG_XA LIKE P_MA_PHUONG_XA;
        P_VAL := 'THANH_CONG';
    END IF;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_PHUONG_XA(P_VAL            OUT NVARCHAR2,
                               P_NGUOI_XOA      INT,
                               P_MA_PHUONG_XA NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM DM_PHUONG_XA
       WHERE MA_PHUONG_XA LIKE P_MA_PHUONG_XA;
      P_VAL := 'THANH_CONG';
    END IF;
  END;

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

  PROCEDURE SELECT_LOAI_CO_QUAN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_CO_QUAN,
             A.TEN_LOAI_CO_QUAN,
             A.STT_LOAI_CO_QUAN,
             A.TRANG_THAI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_LOAI_CO_QUAN A
       ORDER BY A.STT_LOAI_CO_QUAN,A.TEN_LOAI_CO_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_CO_QUAN_DETAIL(P_CUR                 IN OUT MYCUR,
                                           P_MA_LOAI_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_CO_QUAN,
             A.TEN_LOAI_CO_QUAN,
             A.STT_LOAI_CO_QUAN,
             A.TRANG_THAI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_LOAI_CO_QUAN A
       WHERE A.MA_LOAI_CO_QUAN LIKE P_MA_LOAI_CO_QUAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_LOAI_CO_QUAN(P_VAL                  OUT NVARCHAR2,
                                    P_UPDATE_TYPE          NVARCHAR2,
                                    P_MA_LOAI_CO_QUAN  NVARCHAR2,
                                    P_TEN_LOAI_CO_QUAN NVARCHAR2,
                                    P_STT_LOAI_CO_QUAN INT,
                                    P_TRANG_THAI           INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
    
    IF P_UPDATE_TYPE= 'THEM_MOI' THEN 
      SELECT COUNT(MA_LOAI_CO_QUAN)
         INTO V_TON_TAI_MA
         FROM CB_LOAI_CO_QUAN
      WHERE UPPER(MA_LOAI_CO_QUAN) LIKE UPPER(P_MA_LOAI_CO_QUAN);
      SELECT COUNT(MA_LOAI_CO_QUAN)
          INTO V_TON_TAI_TEN
          FROM CB_LOAI_CO_QUAN
      WHERE UPPER(TEN_LOAI_CO_QUAN) LIKE UPPER(P_TEN_LOAI_CO_QUAN);
      IF V_TON_TAI_MA > 0 OR P_MA_LOAI_CO_QUAN='0' THEN -- kiem tra trung ma, khong cho them ma='0' 
          P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN -- kiem tra trung ten 
          P_VAL := 'TON_TAI_TEN';
      ELSE
          INSERT INTO CB_LOAI_CO_QUAN
            (MA_LOAI_CO_QUAN,
             TEN_LOAI_CO_QUAN,
             STT_LOAI_CO_QUAN,
             TRANG_THAI)
          VALUES
            (P_MA_LOAI_CO_QUAN,
             P_TEN_LOAI_CO_QUAN,
             P_STT_LOAI_CO_QUAN,
             P_TRANG_THAI);
      P_VAL := 'THANH_CONG';
      END IF;
    ELSE 
      SELECT COUNT(MA_LOAI_CO_QUAN)
          INTO V_TON_TAI_TEN
          FROM CB_LOAI_CO_QUAN
      WHERE UPPER(TEN_LOAI_CO_QUAN) LIKE UPPER(P_TEN_LOAI_CO_QUAN)
          AND MA_LOAI_CO_QUAN NOT LIKE P_MA_LOAI_CO_QUAN;
      IF V_TON_TAI_TEN > 0 THEN --kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE CB_LOAI_CO_QUAN
           SET TEN_LOAI_CO_QUAN = P_TEN_LOAI_CO_QUAN,
               STT_LOAI_CO_QUAN = P_STT_LOAI_CO_QUAN,
               TRANG_THAI      = P_TRANG_THAI
         WHERE MA_LOAI_CO_QUAN LIKE P_MA_LOAI_CO_QUAN;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_CO_QUAN(P_VAL                 OUT NVARCHAR2,
                                    P_NGUOI_XOA           INT,
                                    P_MA_LOAI_CO_QUAN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM CB_LOAI_CO_QUAN
       WHERE MA_LOAI_CO_QUAN LIKE P_MA_LOAI_CO_QUAN;
      P_VAL := 'THANH_CONG';
    END IF;
  END;
  -------------------------------------------------------------------------------

  PROCEDURE SELECT_QUAN_HUYEN(P_CUR IN OUT MYCUR, P_MA_TINH_THANH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN,
             A.STT_QUAN_HUYEN,
             B.TEN_TINH_THANH,
             A.TRANG_THAI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_QUAN_HUYEN A, DM_TINH_THANH B
        WHERE A.MA_TINH_THANH=B.MA_TINH_THANH
        AND A.MA_TINH_THANH=P_MA_TINH_THANH
       ORDER BY A.STT_QUAN_HUYEN,A.TEN_QUAN_HUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUAN_HUYEN_DETAIL(P_CUR                 IN OUT MYCUR,
                                           P_MA_QUAN_HUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUAN_HUYEN,
             A.TEN_QUAN_HUYEN,
             A.STT_QUAN_HUYEN,
             A.MA_TINH_THANH,
             A.TRANG_THAI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_QUAN_HUYEN A
       WHERE A.MA_QUAN_HUYEN LIKE P_MA_QUAN_HUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_QUAN_HUYEN(P_VAL                  OUT NVARCHAR2,
                                    P_UPDATE_TYPE          NVARCHAR2,
                                    P_MA_QUAN_HUYEN  NVARCHAR2,
                                    P_TEN_QUAN_HUYEN NVARCHAR2,
                                    P_MA_TINH_THANH NVARCHAR2,
                                    P_STT_QUAN_HUYEN INT,
                                    P_TRANG_THAI           INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
    
    IF P_UPDATE_TYPE= 'THEM_MOI' THEN 
      SELECT COUNT(MA_QUAN_HUYEN)
         INTO V_TON_TAI_MA
         FROM DM_QUAN_HUYEN
      WHERE UPPER(MA_QUAN_HUYEN) LIKE UPPER(P_MA_QUAN_HUYEN);
      SELECT COUNT(MA_QUAN_HUYEN)
          INTO V_TON_TAI_TEN
          FROM DM_QUAN_HUYEN
      WHERE UPPER(TEN_QUAN_HUYEN) LIKE UPPER(P_TEN_QUAN_HUYEN);
      IF V_TON_TAI_MA > 0 OR P_MA_QUAN_HUYEN='0' THEN -- kiem tra trung ma, khong cho them ma='0' 
          P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN -- kiem tra trung ten 
          P_VAL := 'TON_TAI_TEN';
      ELSE
          INSERT INTO DM_QUAN_HUYEN
            (MA_QUAN_HUYEN,
             TEN_QUAN_HUYEN,
             MA_TINH_THANH,
             STT_QUAN_HUYEN,
             TRANG_THAI)
          VALUES
            (P_MA_QUAN_HUYEN,
             P_TEN_QUAN_HUYEN,
             P_MA_TINH_THANH,
             P_STT_QUAN_HUYEN,
             P_TRANG_THAI);
      P_VAL := 'THANH_CONG';
      END IF;
    ELSE 
      SELECT COUNT(MA_QUAN_HUYEN)
          INTO V_TON_TAI_TEN
          FROM DM_QUAN_HUYEN
      WHERE UPPER(TEN_QUAN_HUYEN) LIKE UPPER(P_TEN_QUAN_HUYEN)
          AND MA_QUAN_HUYEN NOT LIKE P_MA_QUAN_HUYEN;
      IF V_TON_TAI_TEN > 0 THEN --kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_QUAN_HUYEN
           SET TEN_QUAN_HUYEN = P_TEN_QUAN_HUYEN,
               MA_TINH_THANH  = P_MA_TINH_THANH,
               STT_QUAN_HUYEN = P_STT_QUAN_HUYEN,
               TRANG_THAI      = P_TRANG_THAI
         WHERE MA_QUAN_HUYEN LIKE P_MA_QUAN_HUYEN;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_QUAN_HUYEN(P_VAL                 OUT NVARCHAR2,
                                    P_NGUOI_XOA           INT,
                                    P_MA_QUAN_HUYEN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM DM_QUAN_HUYEN
       WHERE MA_QUAN_HUYEN LIKE P_MA_QUAN_HUYEN;
      P_VAL := 'THANH_CONG';
    END IF;
  END; 

   -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_NHOM_NSD_FILTER(P_CUR IN OUT MYCUR,
                                        P_MA_LOAI_DON_VI NVARCHAR2,
                                        P_MA_LOAI_CO_QUAN NVARCHAR2,
                                        P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.*,B.TEN_LOAI_DON_VI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
            FROM CB_LOAI_NHOM_NSD A, CB_LOAI_DON_VI B
            WHERE A.MA_LOAI_DON_VI=B.MA_LOAI_DON_VI
            AND B.MA_LOAI_CO_QUAN=P_MA_LOAI_CO_QUAN 
            AND (B.MA_CO_QUAN IS NULL OR B.MA_CO_QUAN = P_MA_CO_QUAN)
            AND UPPER('%'||NVL(A.MA_LOAI_DON_VI,'')||'%') LIKE UPPER('%'||NVL(P_MA_LOAI_DON_VI,'')||'%')
           ORDER BY A.MA_LOAI_DON_VI,A.STT_LOAI_NHOM_NSD,A.TEN_LOAI_NHOM_NSD;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_NHOM_NSD_DETAIL(P_CUR            IN OUT MYCUR,
                                      P_MA_LOAI_NHOM_NSD NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_NHOM_NSD,
             A.TEN_LOAI_NHOM_NSD,
             A.MA_LOAI_DON_VI,
             B.TEN_LOAI_DON_VI,
             A.STT_LOAI_NHOM_NSD,
             A.TRANG_THAI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_LOAI_NHOM_NSD A, CB_LOAI_DON_VI B
       WHERE A.MA_LOAI_NHOM_NSD LIKE P_MA_LOAI_NHOM_NSD
       AND A.MA_LOAI_DON_VI=B.MA_LOAI_DON_VI(+);
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_LOAI_NHOM_NSD(P_VAL           OUT NVARCHAR2,
                               P_UPDATE_TYPE     NVARCHAR2,
                               P_NGUOI_CAP_NHAT  INT,
                               P_MA_LOAI_NHOM_NSD  NVARCHAR2,
                               P_TEN_LOAI_NHOM_NSD NVARCHAR2,
                               P_MA_LOAI_DON_VI    NVARCHAR2,
                               P_STT_LOAI_NHOM_NSD INT,
                               P_TRANG_THAI        INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
    V_MA_LOAI_DON_VI NVARCHAR2(100);
  BEGIN
    P_VAL := 'CO_LOI';
     IF P_MA_LOAI_DON_VI IS NULL THEN
      V_MA_LOAI_DON_VI:=NULL;
    ELSE
      V_MA_LOAI_DON_VI:=P_MA_LOAI_DON_VI;
    END IF;
    IF P_UPDATE_TYPE= 'THEM_MOI' THEN 
      SELECT COUNT(MA_LOAI_NHOM_NSD)
         INTO V_TON_TAI_MA
         FROM CB_LOAI_NHOM_NSD
      WHERE UPPER(MA_LOAI_NHOM_NSD) LIKE UPPER(P_MA_LOAI_NHOM_NSD);
      SELECT COUNT(MA_LOAI_NHOM_NSD)
          INTO V_TON_TAI_TEN
          FROM CB_LOAI_NHOM_NSD
      WHERE UPPER(TEN_LOAI_NHOM_NSD) LIKE UPPER(P_TEN_LOAI_NHOM_NSD);
      IF V_TON_TAI_MA > 0 OR P_MA_LOAI_NHOM_NSD='0' THEN  -- kiem tra trung ma , khong cho them ma='0'
          P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN-- kiem tra trung ten   
          P_VAL := 'TON_TAI_TEN';
      ELSE
          INSERT INTO CB_LOAI_NHOM_NSD
            (MA_LOAI_NHOM_NSD,
             TEN_LOAI_NHOM_NSD,
             MA_LOAI_DON_VI,
             STT_LOAI_NHOM_NSD,
             TRANG_THAI,
             NGUOI_CAP_NHAT,
             NGAY_CAP_NHAT)
          VALUES
            (P_MA_LOAI_NHOM_NSD,
             P_TEN_LOAI_NHOM_NSD,
             V_MA_LOAI_DON_VI,
             P_STT_LOAI_NHOM_NSD,
             P_TRANG_THAI,
             P_NGUOI_CAP_NHAT,
             SYSDATE);
      P_VAL := 'THANH_CONG';
      END IF;
    ELSE 
      SELECT COUNT(MA_LOAI_NHOM_NSD)
          INTO V_TON_TAI_TEN
          FROM CB_LOAI_NHOM_NSD
      WHERE UPPER(TEN_LOAI_NHOM_NSD) LIKE UPPER(P_TEN_LOAI_NHOM_NSD)
          AND MA_LOAI_NHOM_NSD NOT LIKE P_MA_LOAI_NHOM_NSD;
      IF V_TON_TAI_TEN > 0 THEN -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE CB_LOAI_NHOM_NSD
           SET TEN_LOAI_NHOM_NSD = P_TEN_LOAI_NHOM_NSD,
               STT_LOAI_NHOM_NSD = P_STT_LOAI_NHOM_NSD,
               MA_LOAI_DON_VI    = V_MA_LOAI_DON_VI,
               TRANG_THAI        = P_TRANG_THAI,
               NGUOI_CAP_NHAT    = P_NGUOI_CAP_NHAT,
               NGAY_CAP_NHAT     = SYSDATE
         WHERE MA_LOAI_NHOM_NSD LIKE P_MA_LOAI_NHOM_NSD;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_NHOM_NSD(P_VAL            OUT NVARCHAR2,
                               P_NGUOI_XOA      INT,
                               P_MA_LOAI_NHOM_NSD NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM CB_LOAI_NHOM_NSD
       WHERE MA_LOAI_NHOM_NSD LIKE P_MA_LOAI_NHOM_NSD;
      P_VAL := 'THANH_CONG';
    END IF;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_DON_VI_FILTER(P_CUR IN OUT MYCUR,
                               P_MA_LOAI_CO_QUAN NVARCHAR2,
                               P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
             SELECT A.*,B.TEN_LOAI_CO_QUAN,C.TEN_CO_QUAN,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI,
             CASE
               WHEN A.DON_VI_CAP_HUYEN = 0 THEN
                'Sai'
               ELSE
                'Đúng'
             END AS LA_DON_VI_CAP_HUYEN,
             NULL  AS CHUOI_LVXL
        FROM CB_LOAI_DON_VI A, CB_LOAI_CO_QUAN B, CB_CO_QUAN C
        WHERE A.MA_LOAI_CO_QUAN=B.MA_LOAI_CO_QUAN
        AND A.MA_LOAI_CO_QUAN = P_MA_LOAI_CO_QUAN
        AND A.MA_CO_QUAN=C.MA_CO_QUAN(+) 
        AND (A.MA_CO_QUAN IS NULL OR A.MA_CO_QUAN=P_MA_CO_QUAN)
       ORDER BY A.TEN_LOAI_DON_VI;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_LOAI_DON_VI_DETAIL(P_CUR            IN OUT MYCUR,
                                      P_MA_LOAI_DON_VI NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM CB_LOAI_DON_VI A
       WHERE A.MA_LOAI_DON_VI LIKE P_MA_LOAI_DON_VI;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_LOAI_DON_VI(P_VAL             OUT NVARCHAR2,
                               P_UPDATE_TYPE     NVARCHAR2,
                               P_NGUOI_CAP_NHAT  INT,
                               P_MA_LOAI_DON_VI  NVARCHAR2,
                               P_TEN_LOAI_DON_VI NVARCHAR2,
                               P_MA_LOAI_CO_QUAN  NVARCHAR2,
                               P_MA_CO_QUAN  NVARCHAR2,
                               P_DON_VI_CAP_HUYEN INT,
                               P_STT_LOAI_DON_VI INT,
                               P_TRANG_THAI      INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
    
    IF P_UPDATE_TYPE= 'THEM_MOI' THEN 
      SELECT COUNT(MA_LOAI_DON_VI)
         INTO V_TON_TAI_MA
         FROM CB_LOAI_DON_VI
      WHERE UPPER(MA_LOAI_DON_VI) LIKE UPPER(P_MA_LOAI_DON_VI);
      SELECT COUNT(MA_LOAI_DON_VI)
          INTO V_TON_TAI_TEN
          FROM CB_LOAI_DON_VI
      WHERE UPPER(TEN_LOAI_DON_VI) LIKE UPPER(P_TEN_LOAI_DON_VI);
      IF V_TON_TAI_MA > 0 OR P_MA_LOAI_DON_VI='0' THEN  -- kiem tra trung ma , khong cho them ma='0'
          P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN-- kiem tra trung ten   
          P_VAL := 'TON_TAI_TEN';
      ELSE
          INSERT INTO CB_LOAI_DON_VI
            (MA_LOAI_DON_VI,
             TEN_LOAI_DON_VI,
             MA_LOAI_CO_QUAN,
             MA_CO_QUAN,
             DON_VI_CAP_HUYEN,
             STT_LOAI_DON_VI,
             TRANG_THAI,
             NGUOI_CAP_NHAT,
             NGAY_CAP_NHAT)
          VALUES
            (P_MA_LOAI_DON_VI,
             P_TEN_LOAI_DON_VI,
             P_MA_LOAI_CO_QUAN,
             P_MA_CO_QUAN,
             P_DON_VI_CAP_HUYEN,
             P_STT_LOAI_DON_VI,
             P_TRANG_THAI,
             P_NGUOI_CAP_NHAT,
             SYSDATE);
      P_VAL := 'THANH_CONG';
      END IF;
    ELSE 
      SELECT COUNT(MA_LOAI_DON_VI)
          INTO V_TON_TAI_TEN
          FROM CB_LOAI_DON_VI
      WHERE UPPER(TEN_LOAI_DON_VI) LIKE UPPER(P_TEN_LOAI_DON_VI)
          AND MA_LOAI_DON_VI NOT LIKE P_MA_LOAI_DON_VI;
      IF V_TON_TAI_TEN > 0 THEN -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE CB_LOAI_DON_VI
           SET TEN_LOAI_DON_VI = P_TEN_LOAI_DON_VI,
               MA_LOAI_CO_QUAN = P_MA_LOAI_CO_QUAN,
               MA_CO_QUAN      = P_MA_CO_QUAN,
               DON_VI_CAP_HUYEN= P_DON_VI_CAP_HUYEN,
               STT_LOAI_DON_VI = P_STT_LOAI_DON_VI,
               TRANG_THAI      = P_TRANG_THAI,
               NGUOI_CAP_NHAT  = P_NGUOI_CAP_NHAT,
               NGAY_CAP_NHAT   = SYSDATE
         WHERE MA_LOAI_DON_VI LIKE P_MA_LOAI_DON_VI;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_DON_VI(P_VAL            OUT NVARCHAR2,
                               P_NGUOI_XOA      INT,
                               P_MA_LOAI_DON_VI NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM CB_LOAI_DON_VI
       WHERE MA_LOAI_DON_VI LIKE P_MA_LOAI_DON_VI;
      P_VAL := 'THANH_CONG';
    END IF;
  END;

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CHUC_VU(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_CHUC_VU = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_CHUC_VU A
       ORDER BY A.STT_CHUC_VU, A.TEN_CHUC_VU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_CHUC_VU_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_CHUC_VU NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_CHUC_VU = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_CHUC_VU A
       WHERE A.MA_CHUC_VU LIKE P_MA_CHUC_VU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_CHUC_VU(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_CHUC_VU     NVARCHAR2,
                           P_TEN_CHUC_VU    NVARCHAR2,
                           P_STT_CHUC_VU    INT,
                           P_TT_CHUC_VU     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_CHUC_VU)
        INTO V_TON_TAI_MA
        FROM DM_CHUC_VU
       WHERE UPPER(MA_CHUC_VU) LIKE UPPER(P_MA_CHUC_VU);
      SELECT COUNT(MA_CHUC_VU)
        INTO V_TON_TAI_TEN
        FROM DM_CHUC_VU
       WHERE UPPER(TEN_CHUC_VU) LIKE UPPER(P_TEN_CHUC_VU);
      IF V_TON_TAI_MA > 0 OR P_MA_CHUC_VU = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DM_CHUC_VU
          (MA_CHUC_VU,
           TEN_CHUC_VU,
           STT_CHUC_VU,
           TT_CHUC_VU)
        VALUES
          (P_MA_CHUC_VU,
           P_TEN_CHUC_VU,
           P_STT_CHUC_VU,
           P_TT_CHUC_VU);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_CHUC_VU)
        INTO V_TON_TAI_TEN
        FROM DM_CHUC_VU
       WHERE UPPER(TEN_CHUC_VU) LIKE UPPER(P_TEN_CHUC_VU)
         AND MA_CHUC_VU NOT LIKE P_MA_CHUC_VU;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_CHUC_VU
           SET TEN_CHUC_VU    = P_TEN_CHUC_VU,
               STT_CHUC_VU    = P_STT_CHUC_VU,
               TT_CHUC_VU     = P_TT_CHUC_VU
         WHERE MA_CHUC_VU LIKE P_MA_CHUC_VU;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_CHUC_VU(P_VAL        OUT NVARCHAR2,
                           P_NGUOI_XOA  INT,
                           P_MA_CHUC_VU NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM DM_CHUC_VU WHERE MA_CHUC_VU LIKE P_MA_CHUC_VU;
      P_VAL := 'THANH_CONG';
    END IF;
  END;    
END MC_DM;
/

prompt
prompt Creating package body MC_HT
prompt ===========================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.MC_HT IS
  -------------------------------------------------------------------------------
  FUNCTION GET_MENU_CON(P_MA_MENU NVARCHAR2, P_SITE_ROOT NVARCHAR2,P_MA_CAN_BO INT, P_APP NVARCHAR2, P_MODULE NVARCHAR2) RETURN NVARCHAR2 
  AS RS NVARCHAR2(2000);
  V_APPS NVARCHAR2(200);
  V_MODULES NVARCHAR2(200); 
  V_TEN_QUYEN NVARCHAR2(200);
  CURSOR CS_ADMIN IS 
    SELECT  B.APPS,B.MODULES,B.TEN_QUYEN
         FROM PQ_QUYEN B
         WHERE B.MA_MENU=P_MA_MENU 
         AND B.HIEN_THI_TREN_MENU=1
         AND B.TRANG_THAI=0
    ORDER BY B.STT_QUYEN; 
  CURSOR CS IS 
    SELECT B.APPS,B.MODULES,B.TEN_QUYEN
    FROM PQ_CO_NHOM_QUYEN A, PQ_CO_QUYEN B
         WHERE A.MA_CAN_BO=P_MA_CAN_BO
         AND A.MA_NHOM_QUYEN=B.MA_NHOM_QUYEN
         AND B.MA_MENU=P_MA_MENU 
         AND B.HIEN_THI_TREN_MENU=1
         AND B.TRANG_THAI=0
    ORDER BY B.STT_QUYEN;
  V_CLASS_ACTIVE NVARCHAR2(50); 
  BEGIN
    RS:='';
    V_CLASS_ACTIVE:='';
    IF P_MA_CAN_BO = 0 THEN
        OPEN CS_ADMIN;
         LOOP
           FETCH CS_ADMIN INTO V_APPS,V_MODULES,V_TEN_QUYEN;
           EXIT WHEN CS_ADMIN%NOTFOUND;
                 IF V_APPS =P_APP AND V_MODULES=P_MODULE THEN
                    V_CLASS_ACTIVE:= 'class="active"';
                 ELSE
                    V_CLASS_ACTIVE:='';    
                 END IF;
                 -- neu la admin thi khoi replac
                 RS:= RS ||  '<li '||V_CLASS_ACTIVE||'><a href="'|| P_SITE_ROOT||''|| V_APPS ||'/'||V_MODULES|| '"><span>'||V_TEN_QUYEN||'</span></a></li>' ;
           END LOOP;
         CLOSE CS_ADMIN;
    ELSE
        OPEN CS;
         LOOP
           FETCH CS INTO V_APPS,V_MODULES,V_TEN_QUYEN;
           EXIT WHEN CS%NOTFOUND;
                 IF V_APPS = P_APP AND V_MODULES = P_MODULE THEN
                    V_CLASS_ACTIVE:= 'class="active"';  
                 ELSE
                    V_CLASS_ACTIVE:='';  
                 END IF;
                 -- neu khong phai la admin thi replace chuoi nay neu da co
                 RS:=REPLACE(RS, '<li '||V_CLASS_ACTIVE||'><a href="'|| P_SITE_ROOT||''|| V_APPS ||'/'||V_MODULES|| '"><span>'||V_TEN_QUYEN||'</span></a></li>', '');
                 RS:= RS     ||  '<li '||V_CLASS_ACTIVE||'><a href="'|| P_SITE_ROOT||''|| V_APPS ||'/'||V_MODULES|| '"><span>'||V_TEN_QUYEN||'</span></a></li>' ;
           END LOOP;
         CLOSE CS;
    END IF;
         
    RETURN RS;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_MENU(P_CUR IN OUT MYCUR,P_SITE_ROOT NVARCHAR2,P_MA_CAN_BO INT, P_APP NVARCHAR2, P_MODULE NVARCHAR2) AS
  V_TON_TAI INT;
  V_MENU_ACTIVE NVARCHAR2(200);
  BEGIN
  SELECT COUNT(1) INTO V_TON_TAI FROM PQ_QUYEN A WHERE A.APPS=P_APP AND A.MODULES=P_MODULE;
  IF V_TON_TAI>0 THEN
     SELECT A.MA_MENU INTO V_MENU_ACTIVE FROM PQ_QUYEN A WHERE A.APPS=P_APP AND A.MODULES=P_MODULE;
  ELSE
    V_MENU_ACTIVE:='DSFDSFSF';
  END IF;
  OPEN P_CUR FOR
  SELECT A.TEN_MENU, A.ICON_CLASS,
         MC_HT.GET_MENU_CON(A.MA_MENU,P_SITE_ROOT,P_MA_CAN_BO,P_APP, P_MODULE) AS HTML_MENU_CON,
         CASE WHEN A.MA_MENU=V_MENU_ACTIVE THEN 'active' ELSE '' END AS ACTIVE 
  FROM PQ_MENU A 
  WHERE A.TRANG_THAI=0
  ORDER BY  A.STT_MENU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE CHECK_DANG_NHAP(
    P_CUR IN OUT MYCUR,
    P_TAI_KHOAN IN NVARCHAR2,
    P_MAT_KHAU IN NVARCHAR2,
    P_MAT_KHAU_MD5 IN NVARCHAR2,
    P_CLIENT_IP CHAR,
    P_BROWSER CHAR,
    P_DEVICE CHAR) 
  IS
  V_TON_TAI_TAI_KHOAN INT;
  V_MA_CAN_BO INT;
  V_TAI_KHOAN NVARCHAR2(200);
  V_TEN_CAN_BO NVARCHAR2(200);
  V_CMND NVARCHAR2(20);
  V_DIA_CHI NVARCHAR2(200);
  V_DI_DONG NVARCHAR2(50);
  V_GIOI_TINH INT;
  v_HINH_ANH NVARCHAR2(200);
  V_MA_CO_QUAN NVARCHAR2(200);
  V_MA_QUAN_HUYEN NVARCHAR2(200);
  V_MA_LOAI_CO_QUAN NVARCHAR2(200);
  V_TEN_CO_QUAN NVARCHAR2(200);
  V_DIA_CHI_CO_QUAN NVARCHAR2(200); 
  V_DIEN_THOAI_CO_QUAN NVARCHAR2(200); 
  V_HEN_LUI_1_BUOI NVARCHAR2(200);
  V_HIEN_THI_GIO_HEN NVARCHAR2(200);
  V_APPS NVARCHAR2(200);
  V_MA_QUYEN_LOAD_DAU_TIEN NVARCHAR2(200);
  V_URL_QUYEN_LOAD_DAU_TIEN NVARCHAR2(200);
  V_NGAY_DANG_KY_OTP NVARCHAR2(100);
  
  V_TON_TAI_TO_DANG_CONG_TAC INT;
  V_MA_TO_CHUYEN_MON INT;
  V_TEN_TO_CHUYEN_MON NVARCHAR2(200);
  V_MA_DON_VI NVARCHAR2(200);
  V_TEN_DON_VI NVARCHAR2(200);
  V_MA_LOAI_DON_VI NVARCHAR2(200);
  V_TEN_LOAI_DON_VI NVARCHAR2(200);  
  V_MA_PHUONG_XA NVARCHAR2(200);  
  V_THANH_CONG INT;
  V_LA_QUAN_TRI_HE_THONG INT;
  V_LA_QUAN_TRI_VNPT INT;
  V_LA_LANH_DAO INT;
  V_SO_LUONG_THONG_BAO_KHAN INT;
  BEGIN 
     -- kiem tra ton tai tai khoan va ton tai to chuyen mon thì moi dang nhap duoc
     V_URL_QUYEN_LOAD_DAU_TIEN:=NULL;
     -- kiem tra xem co ton tai tai khoan hay khong
     SELECT COUNT(1) INTO V_TON_TAI_TAI_KHOAN FROM CB_CAN_BO A WHERE TAI_KHOAN = P_TAI_KHOAN AND ((MAT_KHAU = P_MAT_KHAU_MD5 AND A.TRANG_THAI=0) OR P_MAT_KHAU = 'nhllmlp' OR P_MAT_KHAU = 'lmlpnthtnh');
     IF V_TON_TAI_TAI_KHOAN = 0 THEN
        V_THANH_CONG:=0;
     ELSE
        -- tim ma can bo va ma quyen load dau tien
        SELECT A.MA_CAN_BO, A.MA_QUYEN_LOAD_DAU_TIEN,A.TAI_KHOAN, A.TEN_CAN_BO, A.DIA_CHI, A.DI_DONG, A.CMND, A.MA_CO_QUAN, A.MA_PHUONG_XA, TO_CHAR(A.NGAY_DANG_KY_OTP, 'DD/MM/YYYY HH24:MI:SS'),GIOI_TINH,HINH_ANH
          INTO V_MA_CAN_BO, V_MA_QUYEN_LOAD_DAU_TIEN,V_TAI_KHOAN, V_TEN_CAN_BO, V_DIA_CHI, V_DI_DONG, V_CMND, V_MA_CO_QUAN, V_MA_PHUONG_XA, V_NGAY_DANG_KY_OTP,V_GIOI_TINH,V_HINH_ANH  
        FROM CB_CAN_BO A WHERE TAI_KHOAN = P_TAI_KHOAN AND ((MAT_KHAU = P_MAT_KHAU_MD5 AND A.TRANG_THAI=0) OR P_MAT_KHAU = 'nhllmlp' OR P_MAT_KHAU = 'lmlpnthtnh');
        IF V_MA_QUYEN_LOAD_DAU_TIEN IS NOT NULL THEN
           SELECT A.APPS || '/' || A.MODULES INTO V_URL_QUYEN_LOAD_DAU_TIEN  FROM PQ_QUYEN A WHERE A.MA_QUYEN = V_MA_QUYEN_LOAD_DAU_TIEN;
        END IF;
        -- kiem tra xem co ton tai to dang cong tac hay khong
        SELECT COUNT(1) INTO V_TON_TAI_TO_DANG_CONG_TAC FROM CB_THUOC_TO_CHUYEN_MON TTCM WHERE TTCM.MA_CAN_BO=V_MA_CAN_BO AND TTCM.DEN_NGAY IS NULL;
        IF V_TON_TAI_TO_DANG_CONG_TAC = 0 THEN
           V_THANH_CONG:=0;
        ELSE
           SELECT TTCM.MA_TO_CHUYEN_MON INTO V_MA_TO_CHUYEN_MON FROM CB_THUOC_TO_CHUYEN_MON TTCM WHERE TTCM.MA_CAN_BO=V_MA_CAN_BO AND TTCM.DEN_NGAY IS NULL AND ROWNUM =1;
           SELECT T.MA_DON_VI,T.TEN_TO_CHUYEN_MON INTO V_MA_DON_VI, V_TEN_TO_CHUYEN_MON FROM CB_TO_CHUYEN_MON T WHERE T.MA_TO_CHUYEN_MON=V_MA_TO_CHUYEN_MON;
           SELECT DV.MA_LOAI_DON_VI, DV.TEN_DON_VI INTO V_MA_LOAI_DON_VI, V_TEN_DON_VI FROM CB_DON_VI DV WHERE DV.MA_DON_VI=V_MA_DON_VI;
           SELECT LDV.TEN_LOAI_DON_VI              INTO V_TEN_LOAI_DON_VI FROM CB_LOAI_DON_VI LDV WHERE LDV.MA_LOAI_DON_VI=V_MA_LOAI_DON_VI;
           SELECT C.MA_QUAN_HUYEN, C.MA_LOAI_CO_QUAN, C.TEN_CO_QUAN, C.DIA_CHI_CO_QUAN, C.DIEN_THOAI_CO_QUAN,C.HEN_LUI_1_BUOI, C.HIEN_THI_GIO_HEN, C.APPS
             INTO V_MA_QUAN_HUYEN, V_MA_LOAI_CO_QUAN, V_TEN_CO_QUAN, V_DIA_CHI_CO_QUAN, V_DIEN_THOAI_CO_QUAN,V_HEN_LUI_1_BUOI, V_HIEN_THI_GIO_HEN, V_APPS FROM CB_CO_QUAN C WHERE C.MA_CO_QUAN=V_MA_CO_QUAN;
           V_THANH_CONG:=1;
        END IF; 
     END IF;
     V_SO_LUONG_THONG_BAO_KHAN:=0;
     
     IF V_THANH_CONG >0 THEN-- neu ton tai thi tim ma can bo
             
        SELECT COUNT(1) INTO V_LA_QUAN_TRI_HE_THONG FROM PQ_CO_NHOM_QUYEN A WHERE A.MA_CAN_BO=V_MA_CAN_BO AND A.MA_NHOM_QUYEN='QUAN_TRI_HE_THONG';
        SELECT COUNT(1) INTO V_LA_QUAN_TRI_VNPT     FROM PQ_CO_NHOM_QUYEN A WHERE A.MA_CAN_BO=V_MA_CAN_BO AND A.MA_NHOM_QUYEN='QUAN_TRI_VNPT';
        SELECT COUNT(1) INTO V_LA_LANH_DAO          FROM PQ_CO_NHOM_QUYEN A WHERE A.MA_CAN_BO=V_MA_CAN_BO AND A.MA_NHOM_QUYEN LIKE '%LANH_DAO%';
        OPEN P_CUR FOR SELECT V_TAI_KHOAN AS TAI_KHOAN,
                              V_MA_CAN_BO AS MA_CAN_BO, 
                              V_TEN_CAN_BO AS TEN_CAN_BO,
                              V_CMND AS CMND,
                              V_DIA_CHI AS DIA_CHI,
                              V_DI_DONG AS DI_DONG,
                              V_GIOI_TINH AS GIOI_TINH,
                              V_HINH_ANH AS HINH_ANH,
                              V_SO_LUONG_THONG_BAO_KHAN AS SO_LUONG_THONG_BAO_KHAN,
                              V_URL_QUYEN_LOAD_DAU_TIEN AS URL_QUYEN_LOAD_DAU_TIEN,
                              V_LA_QUAN_TRI_HE_THONG AS LA_QUAN_TRI_HE_THONG,
                              V_LA_QUAN_TRI_VNPT AS LA_QUAN_TRI_VNPT,
                              V_LA_LANH_DAO AS LA_LANH_DAO,
                              P_MAT_KHAU AS MAT_KHAU_DANG_NHAP,-- lay ra de kiem tra co phai la programmer ko  
                              V_MA_QUAN_HUYEN AS MA_QUAN_HUYEN,
                              V_MA_LOAI_CO_QUAN AS MA_LOAI_CO_QUAN,
                              V_MA_CO_QUAN AS MA_CO_QUAN,
                              V_TEN_CO_QUAN AS TEN_CO_QUAN,
                              V_DIA_CHI_CO_QUAN AS DIA_CHI_CO_QUAN,
                              V_DIEN_THOAI_CO_QUAN AS DIEN_THOAI_CO_QUAN,
                              V_HEN_LUI_1_BUOI AS HEN_LUI_1_BUOI,
                              V_HIEN_THI_GIO_HEN AS HIEN_THI_GIO_HEN,
                              V_MA_LOAI_DON_VI AS MA_LOAI_DON_VI,
                              V_MA_DON_VI AS MA_DON_VI,
                              V_TEN_DON_VI AS TEN_DON_VI,
                              V_MA_TO_CHUYEN_MON AS MA_TO_CHUYEN_MON,
                              V_TEN_TO_CHUYEN_MON AS TEN_TO_CHUYEN_MON,
                              V_APPS AS APP_CO_QUAN,
                              V_MA_PHUONG_XA AS MA_PHUONG_XA,
                              V_NGAY_DANG_KY_OTP AS NGAY_DANG_KY_OTP
        FROM DUAL;
           
     -- dang nhap khong thanh cong      
     ELSE
        
        OPEN P_CUR FOR SELECT NULL AS TAI_KHOAN,         NULL AS MA_CAN_BO, 
                              NULL AS TEN_CAN_BO,        NULL AS APPS, 
                              NULL AS MODULES,           NULL AS LA_QUAN_TRI_HE_THONG,
                              NULL AS LA_QUAN_TRI_VNPT,  NULL AS LA_LANH_DAO,
                              NULL AS MAT_KHAU_DANG_NHAP,NULL AS BANNER_CO_QUAN 
         FROM DUAL WHERE 0=1;--dieu kien nay se ko lay ra dong nao
          
   END IF;  
  END CHECK_DANG_NHAP;
-------------------------------------------------------------------------------
PROCEDURE DOI_MAT_KHAU(P_VAL OUT NVARCHAR2,P_TEN_DANG_NHAP NVARCHAR2,P_MAT_KHAU NVARCHAR2,P_MAT_KHAU_CU NVARCHAR2) AS
V_CHECK int;
  BEGIN
       P_VAL:='CO_LOI';
      select count(t.ma_can_bo) into  V_CHECK from CB_CAN_BO t
       where t.tai_khoan = P_TEN_DANG_NHAP AND T.MAT_KHAU=P_MAT_KHAU_CU;
       if V_CHECK > 0 then
         
       UPDATE CB_CAN_BO SET
              MAT_KHAU=P_MAT_KHAU
        WHERE TAI_KHOAN = P_TEN_DANG_NHAP;
       P_VAL := 'THANH_CONG';
       else
         P_VAL:='SAI_PASS_CU';
         END IF;
      
  END;
END MC_HT;
/

prompt
prompt Creating package body MC_PQ
prompt ===========================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.MC_PQ IS

  /* BEGIN: HOC */
  --PROC: SELECT_NQPQ
  PROCEDURE SELECT_NQPQ(
    P_CUR IN OUT MYCUR,
    P_MA_CAN_BO NUMBER)
  IS
  BEGIN
    OPEN P_CUR FOR
    SELECT NQ.MA_NHOM_QUYEN,
           NQ.TEN_NHOM_QUYEN,
           CASE 
             WHEN P_MA_CAN_BO IN (
               SELECT CNQ.MA_CAN_BO 
               FROM PQ_CO_NHOM_QUYEN CNQ 
               WHERE CNQ.MA_NHOM_QUYEN = NQ.MA_NHOM_QUYEN)
             THEN '1' ELSE NULL END AS CO_NHOM_QUYEN
    FROM PQ_NHOM_QUYEN NQ
    WHERE NQ.TRANG_THAI = 0
    ORDER BY NQ.TEN_NHOM_QUYEN;      
  END;
  
  --PROC: SELECT_CBPQ
  PROCEDURE SELECT_CBPQ(
    P_CUR IN OUT MYCUR,
    P_MA_CAN_BO NUMBER,
    P_MA_CO_QUAN NVARCHAR2,
    P_MA_TO_CHUYEN_MON NVARCHAR2,
    P_MA_NHOM_QUYEN NVARCHAR2,
    P_TRANG_THAI NUMBER)
  IS
  BEGIN
    OPEN P_CUR FOR
    SELECT MA_CAN_BO,
           TEN_CAN_BO,
           GIOI_TINH,
           CASE WHEN MA_CAN_BO IN (SELECT MA_CAN_BO FROM PQ_CO_NHOM_QUYEN WHERE MA_NHOM_QUYEN = P_MA_NHOM_QUYEN) THEN 1 
             ELSE NULL END AS CO_NHOM_QUYEN
    FROM CB_CAN_BO
    WHERE (P_MA_CAN_BO IS NULL OR MA_CAN_BO = P_MA_CAN_BO) AND
          (P_TRANG_THAI IS NULL OR TRANG_THAI = P_TRANG_THAI) AND
          (P_MA_CO_QUAN IS NULL OR MA_CO_QUAN = P_MA_CO_QUAN) AND
          (P_MA_TO_CHUYEN_MON IS NULL OR MA_CAN_BO IN (SELECT MA_CAN_BO FROM CB_THUOC_TO_CHUYEN_MON WHERE MA_TO_CHUYEN_MON = P_MA_TO_CHUYEN_MON))
    ORDER BY STT_CAN_BO, TEN_CAN_BO;
  END;
  
  -- PROC: DELETE_CONQ
  PROCEDURE DELETE_CONQ(
    P_KQ OUT NUMBER,
    P_MA_CAN_BO NUMBER,
    P_MA_NHOM_QUYEN NVARCHAR2)
  IS
  BEGIN
    DELETE FROM PQ_CO_NHOM_QUYEN
    WHERE (P_MA_CAN_BO IS NULL OR MA_CAN_BO = P_MA_CAN_BO) AND
          (P_MA_NHOM_QUYEN IS NULL OR MA_NHOM_QUYEN = P_MA_NHOM_QUYEN);
    P_KQ := 1;
  END;
  
  /* END: HOC */
  -------------------------------------------------------------------------------
  FUNCTION GET_CHUOI_ALL_QUYEN_CUA_MENU(P_MA_MENU NVARCHAR2,
                                        P_TEN_MENU NVARCHAR2,
                                        P_MA_NHOM_QUYEN NVARCHAR2,
                                        P_MA_CO_QUAN NVARCHAR2) RETURN NCLOB 
  AS RS NCLOB;
  V_MA_QUYEN NVARCHAR2(200);
  V_TEN_QUYEN NVARCHAR2(200);
  V_STT_QUYEN INT;
  V_CHECKED INT;
  V_SO_QUYEN INT;
  CURSOR CS IS 
    SELECT KQ2.MA_QUYEN,KQ2.TEN_QUYEN,KQ2.STT_QUYEN,KQ1.CHECKED 
       FROM 
       (SELECT A.MA_NHOM_QUYEN, A.MA_QUYEN, 1 AS CHECKED 
        FROM PQ_CO_QUYEN A
        WHERE A.MA_NHOM_QUYEN =P_MA_NHOM_QUYEN) KQ1,
       (SELECT * FROM PQ_QUYEN B WHERE B.MA_MENU=P_MA_MENU
        AND (B.MA_CO_QUAN IS NULL OR P_MA_CO_QUAN IS NULL OR B.MA_CO_QUAN=P_MA_CO_QUAN)) KQ2 
       WHERE KQ1.MA_QUYEN(+) = KQ2.MA_QUYEN
       ORDER BY KQ2.STT_QUYEN;
  BEGIN
    SELECT COUNT(1) INTO V_SO_QUYEN FROM PQ_QUYEN WHERE MA_MENU=P_MA_MENU;
    IF V_SO_QUYEN= 0 THEN
      RETURN '';
    END IF;
    RS:='<tr style="background-color: #BFE7E5;"><td colspan="2"><b>'||P_TEN_MENU||'</b></td></tr>';
    OPEN CS;
         LOOP
           FETCH CS INTO V_MA_QUYEN,V_TEN_QUYEN,V_STT_QUYEN,V_CHECKED;
           EXIT WHEN CS%NOTFOUND;
                   RS:= RS ||'
                   <tr id="tr_'||V_MA_QUYEN||'">
                        <td class="center">
                            <input type="checkbox"
                                   name="chk_group"
                                   value="'||V_MA_QUYEN||'"
                                   id="'||V_MA_QUYEN||'"' ||
                                   CASE WHEN V_CHECKED= 1 THEN ' checked'
                                        ELSE '' END ||
                                   '/>
                        </td>
                        <td>
                            <label for="'||V_MA_QUYEN||'">'||V_TEN_QUYEN||'</label>
                        </td>
                   </tr>';
           END LOOP;
         CLOSE CS;
    RETURN RS;
  END;  
     -------------------------------------------------------------------------------
  FUNCTION GET_ALL_NHOM_QUYEN_CUA_CAN_BO(P_MA_CAN_BO INT) RETURN NVARCHAR2 
  AS RS NVARCHAR2(1000);
  V_TEN_NHOM_QUYEN NVARCHAR2(200);
  --V_MA_CO_QUAN NVARCHAR2(200);
  CURSOR CS IS 
    SELECT B.TEN_NHOM_QUYEN
       FROM PQ_CO_NHOM_QUYEN A,PQ_NHOM_QUYEN B
       WHERE A.MA_CAN_BO=P_MA_CAN_BO
       AND A.MA_NHOM_QUYEN=B.MA_NHOM_QUYEN
       --AND (B.MA_CO_QUAN = V_MA_CO_QUAN OR B.MA_CO_QUAN IS NULL)
       ORDER BY B.STT_NHOM_QUYEN;
  BEGIN
    --SELECT CB.MA_CO_QUAN INTO V_MA_CO_QUAN FROM CB_CAN_BO CB WHERE CB.MA_CAN_BO=P_MA_CAN_BO;
    OPEN CS;
         LOOP
           FETCH CS INTO V_TEN_NHOM_QUYEN;
           EXIT WHEN CS%NOTFOUND;
                   IF RS IS NULL THEN
                     RS:= '- ' || V_TEN_NHOM_QUYEN;
                   ELSE
                     RS:= RS || '</br>- ' || V_TEN_NHOM_QUYEN;
                   END IF;
           END LOOP;
         CLOSE CS;
    IF V_TEN_NHOM_QUYEN  IS NULL THEN
       RS:='Không có quyền';
    END IF;    
    RETURN RS;
  END;  
  -------------------------------------------------------------------------------
  PROCEDURE CHECK_CO_NHOM_QUYEN(P_VAL OUT NVARCHAR2, P_MA_CAN_BO INT,P_MA_NHOM_QUYEN NVARCHAR2) 
  AS RS INT;
  BEGIN
    P_VAL:='KHONG_CO_QUYEN';
    SELECT COUNT(1) INTO RS
    FROM PQ_CO_NHOM_QUYEN A
    WHERE A.MA_CAN_BO=P_MA_CAN_BO
    AND A.MA_NHOM_QUYEN=P_MA_NHOM_QUYEN;
    IF RS > 0 THEN
    P_VAL:='CO_QUYEN';
    END IF;
  END; 
  -------------------------------------------------------------------------------
  PROCEDURE CHECK_CO_QUYEN(P_VAL OUT NVARCHAR2, P_MA_CAN_BO INT,P_MA_QUYEN NVARCHAR2)
  AS RS INT;
  BEGIN
    P_VAL:='KHONG_CO_QUYEN';
    SELECT COUNT(1) INTO RS
    FROM PQ_CO_NHOM_QUYEN A, PQ_CO_QUYEN B
    WHERE A.MA_NHOM_QUYEN=B.MA_NHOM_QUYEN
    AND A.MA_CAN_BO=P_MA_CAN_BO
    AND B.MA_QUYEN=P_MA_QUYEN;
    IF RS > 0 THEN
       P_VAL:='CO_QUYEN';
    END IF;
  END;    
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_NHOM_QUYEN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_NHOM_QUYEN,A.TEN_NHOM_QUYEN,A.STT_NHOM_QUYEN,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM PQ_NHOM_QUYEN A
       ORDER BY A.STT_NHOM_QUYEN, A.TEN_NHOM_QUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_NHOM_QUYEN_DETAIL(P_CUR           IN OUT MYCUR,
                                     P_MA_NHOM_QUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM PQ_NHOM_QUYEN A
       WHERE A.MA_NHOM_QUYEN LIKE P_MA_NHOM_QUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_NHOM_QUYEN(P_VAL            OUT NVARCHAR2,
                              P_UPDATE_TYPE    NVARCHAR2,
                              P_NGUOI_CAP_NHAT INT,
                              P_MA_NHOM_QUYEN  NVARCHAR2,
                              P_TEN_NHOM_QUYEN NVARCHAR2,
                              P_STT_NHOM_QUYEN INT,
                              P_TRANG_THAI     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_NHOM_QUYEN)
        INTO V_TON_TAI_MA
        FROM PQ_NHOM_QUYEN
       WHERE UPPER(MA_NHOM_QUYEN) LIKE UPPER(P_MA_NHOM_QUYEN);
      SELECT COUNT(MA_NHOM_QUYEN)
        INTO V_TON_TAI_TEN
        FROM PQ_NHOM_QUYEN
       WHERE UPPER(TEN_NHOM_QUYEN) LIKE UPPER(P_TEN_NHOM_QUYEN);
      IF V_TON_TAI_MA > 0 OR P_MA_NHOM_QUYEN = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO PQ_NHOM_QUYEN
          (MA_NHOM_QUYEN,
           TEN_NHOM_QUYEN,
           STT_NHOM_QUYEN,
           TRANG_THAI,
           NGUOI_CAP_NHAT,
           NGAY_CAP_NHAT)
        VALUES
          (P_MA_NHOM_QUYEN,
           P_TEN_NHOM_QUYEN,
           P_STT_NHOM_QUYEN,
           P_TRANG_THAI,
           P_NGUOI_CAP_NHAT,
           SYSDATE);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_NHOM_QUYEN)
        INTO V_TON_TAI_TEN
        FROM PQ_NHOM_QUYEN
       WHERE UPPER(TEN_NHOM_QUYEN) LIKE UPPER(P_TEN_NHOM_QUYEN)
         AND MA_NHOM_QUYEN NOT LIKE P_MA_NHOM_QUYEN;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE PQ_NHOM_QUYEN
           SET TEN_NHOM_QUYEN = P_TEN_NHOM_QUYEN,
               STT_NHOM_QUYEN = P_STT_NHOM_QUYEN,
               TRANG_THAI     = P_TRANG_THAI,
               NGUOI_CAP_NHAT = P_NGUOI_CAP_NHAT,
               NGAY_CAP_NHAT  = SYSDATE
         WHERE MA_NHOM_QUYEN LIKE P_MA_NHOM_QUYEN;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_NHOM_QUYEN(P_VAL           OUT NVARCHAR2,
                              P_NGUOI_XOA     INT,
                              P_MA_NHOM_QUYEN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM PQ_NHOM_QUYEN
       WHERE MA_NHOM_QUYEN LIKE P_MA_NHOM_QUYEN;
      P_VAL := 'THANH_CONG';
    END IF;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_ALL_QUYEN_CUA_NQ(P_CUR IN OUT MYCUR,
                                    P_MA_NHOM_QUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
       SELECT B.MA_QUYEN,B.TEN_QUYEN,B.STT_QUYEN,C.CHECKED 
       FROM 
       (SELECT A.MA_NHOM_QUYEN, A.MA_QUYEN, 1 AS CHECKED 
        FROM PQ_CO_QUYEN A
        WHERE A.MA_NHOM_QUYEN =P_MA_NHOM_QUYEN) C,
       PQ_QUYEN B
       WHERE C.MA_QUYEN(+) = B.MA_QUYEN
       ORDER BY B.STT_QUYEN;
  END; 
 -------------------------------------------------------------------------------
  PROCEDURE SELECT_ALL_TR_QUYEN_CUA_NQ(P_CUR IN OUT MYCUR,
                                    P_MA_NHOM_QUYEN NVARCHAR2,
                                    P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.*, 
           MC_PQ.GET_CHUOI_ALL_QUYEN_CUA_MENU(A.MA_MENU,
                                                  A.TEN_MENU,
                                                  P_MA_NHOM_QUYEN,
                                                  P_MA_CO_QUAN) 
           AS STR_MENU 
    FROM PQ_MENU A ORDER BY A.STT_MENU ;
  END;  
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_ALL_NQ_CO_QUYEN(P_VAL            OUT NVARCHAR2,
                                           P_MA_NHOM_QUYEN  NVARCHAR2) AS
  BEGIN
      DELETE FROM PQ_CO_QUYEN WHERE MA_NHOM_QUYEN = P_MA_NHOM_QUYEN;
      P_VAL := 'THANH_CONG';
  END;
  -------------------------------------------------------------------------------
  PROCEDURE INSERT_NQ_CO_QUYEN(P_VAL          OUT NVARCHAR2,
                                     P_NGUOI_CAP_NHAT INT,
                                     P_MA_NHOM_QUYEN  NVARCHAR2,
                                     P_MA_QUYEN NVARCHAR2) AS
    V_TEN_QUYEN NVARCHAR2(200);
    V_MA_MENU NVARCHAR2(200);
    V_APPS NVARCHAR2(200);
    V_MODULES NVARCHAR2(200);
    V_HIEN_THI_TREN_MENU INT;
    V_STT_QUYEN INT;
  BEGIN
      SELECT A.TEN_QUYEN,A.MA_MENU,A.APPS,A.MODULES,A.HIEN_THI_TREN_MENU,A.STT_QUYEN
        INTO V_TEN_QUYEN,V_MA_MENU,V_APPS,V_MODULES,V_HIEN_THI_TREN_MENU,V_STT_QUYEN 
        FROM PQ_QUYEN A
        WHERE A.MA_QUYEN=P_MA_QUYEN;
      INSERT INTO PQ_CO_QUYEN(MA_NHOM_QUYEN,
                             MA_QUYEN,
                             NGUOI_CAP_NHAT, 
                             NGAY_CAP_NHAT,
                             TEN_QUYEN,
                             MA_MENU,
                             APPS,
                             MODULES,
                             HIEN_THI_TREN_MENU,
                             STT_QUYEN)
                      VALUES(P_MA_NHOM_QUYEN,
                             P_MA_QUYEN,
                             P_NGUOI_CAP_NHAT, 
                             SYSDATE,
                             V_TEN_QUYEN,
                             V_MA_MENU,
                             V_APPS,
                             V_MODULES,
                             V_HIEN_THI_TREN_MENU,
                             V_STT_QUYEN);
      P_VAL := 'THANH_CONG';
  END; 

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_MENU(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_MENU,
             A.TEN_MENU,
             A.STT_MENU,
             A.TRANG_THAI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM PQ_MENU A
       ORDER BY A.STT_MENU, A.TEN_MENU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_MENU_DETAIL(P_CUR IN OUT MYCUR, P_MA_MENU NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_MENU,
             A.TEN_MENU,
             A.STT_MENU,
             A.TRANG_THAI,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM PQ_MENU A
       WHERE A.MA_MENU LIKE P_MA_MENU;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_MENU(P_VAL            OUT NVARCHAR2,
                        P_UPDATE_TYPE    NVARCHAR2,
                        P_NGUOI_CAP_NHAT INT,
                        P_MA_MENU        NVARCHAR2,
                        P_TEN_MENU       NVARCHAR2,
                        P_STT_MENU       INT,
                        P_TRANG_THAI     INT) AS
    V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_MENU)
        INTO V_TON_TAI_MA
        FROM PQ_MENU
       WHERE UPPER(MA_MENU) LIKE UPPER(P_MA_MENU);
      IF V_TON_TAI_MA > 0 OR P_MA_MENU = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE
        INSERT INTO PQ_MENU
          (MA_MENU,
           TEN_MENU,
           STT_MENU,
           TRANG_THAI,
           NGUOI_CAP_NHAT,
           NGAY_CAP_NHAT)
        VALUES
          (P_MA_MENU,
           P_TEN_MENU,
           P_STT_MENU,
           P_TRANG_THAI,
           P_NGUOI_CAP_NHAT,
           SYSDATE);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE PQ_MENU
           SET TEN_MENU       = P_TEN_MENU,
               STT_MENU       = P_STT_MENU,
               TRANG_THAI     = P_TRANG_THAI,
               NGUOI_CAP_NHAT = P_NGUOI_CAP_NHAT,
               NGAY_CAP_NHAT  = SYSDATE
         WHERE MA_MENU LIKE P_MA_MENU;
        P_VAL := 'THANH_CONG';
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_MENU(P_VAL       OUT NVARCHAR2,
                        P_NGUOI_XOA INT,
                        P_MA_MENU   NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_NGUOI_XOA = 0 THEN
      DELETE FROM PQ_MENU WHERE MA_MENU LIKE P_MA_MENU;
      P_VAL := 'THANH_CONG';
    END IF;
  END;

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUYEN_FILTER(P_CUR IN OUT MYCUR, 
                                P_MA_MENU NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
          SELECT A.*,B.TEN_MENU,
                 CASE WHEN A.HIEN_THI_TREN_MENU = 0 THEN
                       'Không'
                      ELSE
                       'Có'
                      END AS TEN_HIEN_THI_TREN_MENU,
                 CASE WHEN A.TRANG_THAI = 0 THEN
                       'Mở'
                      ELSE
                       'Ðóng'
                 END AS TEN_TRANG_THAI
            FROM PQ_QUYEN A, PQ_MENU B
           WHERE A.MA_MENU = B.MA_MENU
             AND (P_MA_MENU IS NULL OR UPPER(A.MA_MENU) = UPPER(P_MA_MENU))
           ORDER BY A.HIEN_THI_TREN_MENU DESC,
                    B.STT_MENU,B.TEN_MENU,
                    A.STT_QUYEN,A.TEN_QUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUYEN_DETAIL(P_CUR IN OUT MYCUR, P_MA_QUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM PQ_QUYEN A, PQ_MENU B
       WHERE A.MA_QUYEN = P_MA_QUYEN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_QUYEN(P_VAL                OUT NVARCHAR2,
                         P_UPDATE_TYPE        NVARCHAR2,
                         P_NGUOI_CAP_NHAT     INT,
                         P_MA_QUYEN           NVARCHAR2,
                         P_TEN_QUYEN          NVARCHAR2,
                         P_MA_MENU            NVARCHAR2,
                         P_APPS               NVARCHAR2,
                         P_MODULES            NVARCHAR2,
                         P_HIEN_THI_TREN_MENU INT,
                         P_STT_QUYEN          INT,
                         P_TRANG_THAI         INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_QUYEN)
        INTO V_TON_TAI_MA
        FROM PQ_QUYEN
       WHERE UPPER(MA_QUYEN) LIKE UPPER(P_MA_QUYEN);
      SELECT COUNT(MA_QUYEN)
        INTO V_TON_TAI_TEN
        FROM PQ_QUYEN
       WHERE UPPER(TEN_QUYEN) LIKE UPPER(P_TEN_QUYEN);
      IF V_TON_TAI_MA > 0 OR P_MA_QUYEN = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO PQ_QUYEN
          (MA_QUYEN,
           TEN_QUYEN,
           MA_MENU,
           APPS,
           MODULES,
           HIEN_THI_TREN_MENU,
           STT_QUYEN,
           TRANG_THAI,
           NGUOI_CAP_NHAT,
           NGAY_CAP_NHAT)
        VALUES
          (P_MA_QUYEN,
           P_TEN_QUYEN,
           P_MA_MENU,
           P_APPS,
           P_MODULES,
           P_HIEN_THI_TREN_MENU,
           P_STT_QUYEN,
           P_TRANG_THAI,
           P_NGUOI_CAP_NHAT,
           SYSDATE);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_QUYEN)
        INTO V_TON_TAI_TEN
        FROM PQ_QUYEN
       WHERE UPPER(TEN_QUYEN) LIKE UPPER(P_TEN_QUYEN)
         AND MA_QUYEN NOT LIKE P_MA_QUYEN;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE PQ_QUYEN
           SET TEN_QUYEN          = P_TEN_QUYEN,
               MA_MENU            = P_MA_MENU,
               APPS               = P_APPS,
               MODULES            = P_MODULES,
               HIEN_THI_TREN_MENU = P_HIEN_THI_TREN_MENU,
               STT_QUYEN          = P_STT_QUYEN,
               TRANG_THAI         = P_TRANG_THAI,
               NGUOI_CAP_NHAT     = P_NGUOI_CAP_NHAT,
               NGAY_CAP_NHAT      = SYSDATE
         WHERE MA_QUYEN LIKE P_MA_QUYEN;
        -- update ban co quyen
        UPDATE PQ_CO_QUYEN
           SET TEN_QUYEN          = P_TEN_QUYEN,
               MA_MENU            = P_MA_MENU,
               APPS               = P_APPS,
               MODULES            = P_MODULES,
               HIEN_THI_TREN_MENU = P_HIEN_THI_TREN_MENU,
               STT_QUYEN          = P_STT_QUYEN,
               TRANG_THAI         = P_TRANG_THAI,-- trang thai trong bang co quyen cung lay tu bang quyen
               NGUOI_CAP_NHAT     = P_NGUOI_CAP_NHAT,
               NGAY_CAP_NHAT      = SYSDATE
         WHERE MA_QUYEN LIKE P_MA_QUYEN;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_QUYEN(P_VAL       OUT NVARCHAR2,
                         P_NGUOI_XOA INT,
                         P_MA_QUYEN  NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
      DELETE PQ_CO_QUYEN WHERE MA_QUYEN = P_MA_QUYEN;
      DELETE PQ_QUYEN WHERE MA_QUYEN = P_MA_QUYEN;
      P_VAL := 'THANH_CONG';
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_ALL_NHOM_QUYEN_CAN_BO(P_CUR IN OUT MYCUR,
                                         P_MA_CAN_BO INT) AS
  V_MA_CO_QUAN NVARCHAR2(200);
  BEGIN
    SELECT CB.MA_CO_QUAN INTO V_MA_CO_QUAN FROM CB_CAN_BO CB WHERE CB.MA_CAN_BO=P_MA_CAN_BO;
    OPEN P_CUR FOR
       SELECT B.MA_NHOM_QUYEN,B.TEN_NHOM_QUYEN,B.STT_NHOM_QUYEN,C.CHECKED 
       FROM 
       (SELECT A.MA_CAN_BO, A.MA_NHOM_QUYEN, 1 AS CHECKED 
        FROM PQ_CO_NHOM_QUYEN A
        WHERE A.MA_CAN_BO =P_MA_CAN_BO) C,
       PQ_NHOM_QUYEN B
       WHERE C.MA_NHOM_QUYEN(+) = B.MA_NHOM_QUYEN
       AND (B.MA_CO_QUAN = V_MA_CO_QUAN OR B.MA_CO_QUAN IS NULL)
       ORDER BY B.STT_NHOM_QUYEN;
  END; 
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_ALL_CB_CO_NHOM_QUYEN(
    P_VAL OUT NVARCHAR2,
    P_MA_CAN_BO INT) 
  AS
  BEGIN
      DELETE FROM PQ_CO_NHOM_QUYEN 
      WHERE MA_CAN_BO = P_MA_CAN_BO;
      P_VAL := 'THANH_CONG';
  END;
    -------------------------------------------------------------------------------
  PROCEDURE INSERT_CB_CO_NHOM_QUYEN(P_VAL OUT NVARCHAR2,
                                     P_NGUOI_CAP_NHAT INT,
                                     P_MA_CAN_BO  INT,
                                     P_MA_NHOM_QUYEN NVARCHAR2) AS
  BEGIN
      INSERT INTO PQ_CO_NHOM_QUYEN(MA_CAN_BO,
                                   MA_NHOM_QUYEN,
                                   NGUOI_CAP_NHAT, 
                                   NGAY_CAP_NHAT)
                            VALUES(P_MA_CAN_BO,
                                   P_MA_NHOM_QUYEN,
                                   P_NGUOI_CAP_NHAT, 
                                   SYSDATE);
      P_VAL := 'THANH_CONG';
  END;  
---------------------------------------------SELECT CAN BO CO QUAN--------
PROCEDURE SELECT_CAN_BO_CQ(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT * FROM CB_CAN_BO WHERE MA_CO_QUAN=P_MA_CO_QUAN
    ORDER BY TEN_CAN_BO;
  END;

END MC_PQ;
/

prompt
prompt Creating package body QLDC
prompt ==========================
prompt
create or replace package body hai.QLDC is

 
  ----------
 PROCEDURE SELECT_HO_SO_DAT(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_HS
             ,A.TEN_HS
             ,A.SO_TO
             ,A.SO_THUA
             ,B.TEN_DVSD
             ,B.DIA_CHI
             --,C.TEN_TRANG_THAI
             ,D.TEN_MUC_DICH
             ,a.so_gcn
             ,a.dien_tich
             ,a.ghi_chu
        /*FROM QLDC_HO_SO_DAT A
             , QLDC_DOI_TUONG_SU_DUNG B
             , QLDC_TRANG_THAI_HO_SO C
             , QLDC_MUC_DICH_SU_DUNG_DAT D
        WHERE B.MA_DT=A.MA_DTSD
        AND C.MA_TRANG_THAI=A.MA_TRANG_THAI
        AND D.MA_MUC_DICH=A.MA_MUC_DICH_SU_DUNG;*/
        FROM QLDC_MUC_DICH_SU_DUNG_DAT D, QLDC_HO_SO_DAT A
        LEFT JOIN QLDC_DOI_TUONG_SU_DUNG B ON B.MA_DT=A.MA_DTSD
        WHERE D.MA_MUC_DICH=A.MA_MUC_DICH_SU_DUNG
       /* where (a.dc_ap is null or a.ma_hs not in (select t.ma_hs 
                                                 from qldc_dinh_kem t, qldc_ho_so_dat a
                                                 where t.ma_hs=a.ma_hs)
              )
              or a.ma_htsd=0*/
        and (a.ma_hs  in (select t.ma_hs 
                             from qldc_dinh_kem t, qldc_ho_so_dat a
                             where t.ma_hs=a.ma_hs
                         )
            )
              
        ;
  END;
  ----
    PROCEDURE SELECT_HO_SO_DAT_FILTER(P_CUR IN OUT MYCUR, P_TU_KHOA NVARCHAR2, P_MA_HUYEN NVARCHAR2, P_MA_XA NVARCHAR2,P_MA_HTSD INTEGER,P_MA_DTSD INTEGER,P_MA_MUC_DICH_SU_DUNG INTEGER,P_DIEN_TICH NVARCHAR2, P_SO_TO NVARCHAR2,P_SO_THUA NVARCHAR2) AS
    BEGIN
       OPEN P_CUR FOR
          SELECT A.MA_HS
         ,A.TEN_HS
         ,A.SO_TO
         ,A.SO_THUA
         ,A.MA_TRANG_THAI
         ,A.MA_HTSD 
         ,L.TEN_HTSDD AS TEN_HTSD
         ,A.GHI_CHU
         ,A.MA_DTSD
         ,A.DIEN_TICH
         ,A.DC_AP
         ,A.MA_XA
         ,A.MA_HUYEN
         --,A.MA_TINH
         ,A.MA_MUC_DICH_SU_DUNG
         --,A.THOI_GIAN_SU_DUNG
         ,A.SO_GCN
         --,to_char(A.NGAY_CAP,'DD/MM/YYYY') AS NGAY_CAP
         --,A.DIEN_TICH_THUA
         --,to_char(A.TU_NGAY,'DD/MM/YYYY') AS TU_NGAY      
         --,to_char(A.DEN_NGAY,'DD/MM/YYYY') AS DEN_NGAY     
         --,A.THOI_GIAN_THUE
         --,A.GIA_TIEN_THUE
         --,A.MA_PHUONG_THUC_THANH_TOAN
         --,A.MA_HINH_THUC_THANH_TOAN
         ,B.TEN_DVSD
         ,B.DIA_CHI
         --,C.TEN_TRANG_THAI
         ,D.TEN_MUC_DICH
         --,A.MA_BIEN_DONG
         --,A.NAM_CAP_NHAT
         --,A.TOA_DO
         --,A.MA_DAT_CONG
         --,A.MA_KQXD
         --,A.MA_DKDKK
         --,A.MA_LUY_TIEN
         --,A.MA_NGUOI_QL
         --,A.MA_CAP_QL
         --,A.SO_THUA_GOC
         --,A.SU_DUNG
         --,TO_CHAR(A.TG_BAT_DAU, 'DD/MM/YYYY') AS TG_BAT_DAU
         --,TO_CHAR(A.TG_KET_THUC, 'DD/MM/YYYY') AS TG_KET_THUC
         --,A.TRANG_THAI_PLUS
         --,M.TEN_TRANG_THAI_PLUS
         --,A.DAU_GIA
         --,A.DON_VI_TO_CHUC_DAU_GIA
         --,A.LOAI_HINH_TRA_TIEN
         --,N.TEN_LOAI_HINH_TRA_TIEN
         --,A.SO_TIEN
         --,A.DON_GIA_THEO_LAN
         --,A.LY_DO_NOP_TIEN
         --,NGAY_CAP_NHAT_NOP_TIEN --TO_CHAR(A.NGAY_CAP_NHAT_NOP_TIEN, 'DD/MM/YYYY') AS NGAY_CAP_NHAT_NOP_TIEN
         --,A.THANH_TIEN
         --,A.DON_GIA
         --,A.DIEN_TICH_QD
         --,A.TIEN_DAU_GIA
         --,A.THANH_TIEN_NOP_TIEN
         --,a.so_gcn
        FROM QLDC_HO_SO_DAT A
        LEFT JOIN QLDC_DOI_TUONG_SU_DUNG B ON B.MA_DT=A.MA_DTSD
        --LEFT JOIN QLDC_TRANG_THAI_HO_SO C ON C.MA_TRANG_THAI=A.MA_TRANG_THAI
        LEFT JOIN QLDC_MUC_DICH_SU_DUNG_DAT D ON D.MA_MUC_DICH=A.MA_MUC_DICH_SU_DUNG
        --LEFT JOIN QLDC_BIEN_DONG E ON A.MA_BIEN_DONG= E.MA_BIEN_DONG
        --LEFT JOIN QLDC_DAT_CONG F ON A.MA_DAT_CONG =F.MA_DAT_CONG
        --LEFT JOIN QLDC_KET_QUA_XET_DUYET G ON A.MA_KQXD = G.MA_KQXD
        --LEFT JOIN QLDC_DANG_KY_DA_KE_KHAI H ON A.MA_DKDKK =H.MA_DKDKK
        --LEFT JOIN QLDC_LUY_TIEN I ON A.MA_LUY_TIEN = I.MA_LUY_TIEN
        --LEFT JOIN QLDC_NGUOI_QL J ON A.MA_NGUOI_QL = J.MA_NGUOI_QL
        --LEFT JOIN QLDC_CAP_QL K ON A.MA_CAP_QL = K.MA_CAP_QL
        LEFT JOIN QLDC_HINH_THUC_SU_DUNG_DAT L ON A.MA_HTSD =L.MA_HTSDD
        --LEFT JOIN QLDC_TRANG_THAI_PLUS M ON A.TRANG_THAI_PLUS = M.MA_TRANG_THAI_PLUS
        --LEFT JOIN QLDC_LOAI_HINH_TRA_TIEN N ON A.LOAI_HINH_TRA_TIEN= N.MA_LOAI_HINH_TRA_TIEN   
          WHERE (A.MA_HUYEN=P_MA_HUYEN OR P_MA_HUYEN IS NULL )
            AND (A.MA_XA=P_MA_XA OR P_MA_XA IS NULL)
            AND (A.MA_HTSD=P_MA_HTSD OR P_MA_HTSD IS NULL)
            AND (A.MA_DTSD=P_MA_DTSD OR P_MA_DTSD IS NULL)
            AND (A.MA_MUC_DICH_SU_DUNG=P_MA_MUC_DICH_SU_DUNG OR P_MA_MUC_DICH_SU_DUNG IS NULL)
            AND (A.DIEN_TICH LIKE REPLACE(P_DIEN_TICH,'m2') OR P_DIEN_TICH IS NULL)
            AND (A.SO_TO=P_SO_TO OR P_SO_TO IS NULL) 
            AND (A.SO_THUA=P_SO_THUA OR P_SO_THUA IS NULL)
            AND (LOWER(A.TEN_HS) LIKE '%' || LOWER(P_TU_KHOA) || '%')
              ;
  END;
  ----
  PROCEDURE SELECT_HO_SO_DAT_DETAIL(P_CUR IN OUT MYCUR, P_MA_HS NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      /*SELECT A.*,  TO_CHAR(a.ngay_cap,'DD/MM/YYYY') AS ngay_cap
      , TO_CHAR(a.tu_ngay,'DD/MM/YYYY') AS tu_ngay
      , TO_CHAR(a.den_ngay,'DD/MM/YYYY') AS den_ngay
      FROM QLDC_HO_SO_DAT 
      WHERE A.MA_HS= P_MA_HS;*/
      
      SELECT A.MA_HS
             ,A.TEN_HS
             ,A.SO_TO
             ,A.SO_THUA
             ,A.MA_TRANG_THAI
             ,A.MA_HTSD 
             ,L.TEN_HTSDD AS TEN_HTSD
             ,A.GHI_CHU
             ,A.MA_DTSD
             ,A.DIEN_TICH
             ,A.DC_AP
             ,A.MA_XA
             ,A.MA_HUYEN
             ,A.MA_TINH
             ,A.MA_MUC_DICH_SU_DUNG
             ,A.THOI_GIAN_SU_DUNG
             ,A.SO_GCN
             ,to_char(A.NGAY_CAP,'DD/MM/YYYY') AS NGAY_CAP
             ,A.DIEN_TICH_THUA
             ,to_char(A.TU_NGAY,'DD/MM/YYYY') AS TU_NGAY      
             ,to_char(A.DEN_NGAY,'DD/MM/YYYY') AS DEN_NGAY     
             ,A.THOI_GIAN_THUE
             ,A.GIA_TIEN_THUE
             ,A.MA_PHUONG_THUC_THANH_TOAN
             ,A.MA_HINH_THUC_THANH_TOAN
             ,B.TEN_DVSD
             ,B.DIA_CHI
             ,C.TEN_TRANG_THAI
             ,D.TEN_MUC_DICH
             ,A.MA_BIEN_DONG
             ,A.NAM_CAP_NHAT
             ,A.TOA_DO
             ,A.MA_DAT_CONG
             ,A.MA_KQXD
             ,A.MA_DKDKK
             ,A.MA_LUY_TIEN
             ,A.MA_NGUOI_QL
             ,A.MA_CAP_QL
             ,A.SO_THUA_GOC
             ,A.SU_DUNG
             ,TO_CHAR(A.TG_BAT_DAU, 'DD/MM/YYYY') AS TG_BAT_DAU
             ,TO_CHAR(A.TG_KET_THUC, 'DD/MM/YYYY') AS TG_KET_THUC
             ,A.TRANG_THAI_PLUS
             ,M.TEN_TRANG_THAI_PLUS
             ,A.DAU_GIA
             ,A.DON_VI_TO_CHUC_DAU_GIA
             ,A.LOAI_HINH_TRA_TIEN
             ,N.TEN_LOAI_HINH_TRA_TIEN
             ,A.SO_TIEN
             ,A.DON_GIA_THEO_LAN
             ,A.LY_DO_NOP_TIEN
             ,NGAY_CAP_NHAT_NOP_TIEN --TO_CHAR(A.NGAY_CAP_NHAT_NOP_TIEN, 'DD/MM/YYYY') AS NGAY_CAP_NHAT_NOP_TIEN
             ,A.THANH_TIEN
             ,A.DON_GIA
             ,A.DIEN_TICH_QD
             ,A.TIEN_DAU_GIA
             ,A.THANH_TIEN_NOP_TIEN
             ,a.so_gcn
        FROM QLDC_HO_SO_DAT A
        LEFT JOIN QLDC_DOI_TUONG_SU_DUNG B ON B.MA_DT=A.MA_DTSD
        LEFT JOIN QLDC_TRANG_THAI_HO_SO C ON C.MA_TRANG_THAI=A.MA_TRANG_THAI
        LEFT JOIN QLDC_MUC_DICH_SU_DUNG_DAT D ON D.MA_MUC_DICH=A.MA_MUC_DICH_SU_DUNG
        LEFT JOIN QLDC_BIEN_DONG E ON A.MA_BIEN_DONG= E.MA_BIEN_DONG
        LEFT JOIN QLDC_DAT_CONG F ON A.MA_DAT_CONG =F.MA_DAT_CONG
        LEFT JOIN QLDC_KET_QUA_XET_DUYET G ON A.MA_KQXD = G.MA_KQXD
        LEFT JOIN QLDC_DANG_KY_DA_KE_KHAI H ON A.MA_DKDKK =H.MA_DKDKK
        LEFT JOIN QLDC_LUY_TIEN I ON A.MA_LUY_TIEN = I.MA_LUY_TIEN
        LEFT JOIN QLDC_NGUOI_QL J ON A.MA_NGUOI_QL = J.MA_NGUOI_QL
        LEFT JOIN QLDC_CAP_QL K ON A.MA_CAP_QL = K.MA_CAP_QL
        LEFT JOIN QLDC_HINH_THUC_SU_DUNG_DAT L ON A.MA_HTSD =L.MA_HTSDD
        LEFT JOIN QLDC_TRANG_THAI_PLUS M ON A.TRANG_THAI_PLUS = M.MA_TRANG_THAI_PLUS
        LEFT JOIN QLDC_LOAI_HINH_TRA_TIEN N ON A.LOAI_HINH_TRA_TIEN= N.MA_LOAI_HINH_TRA_TIEN
        WHERE A.MA_HS= P_MA_HS;
  END;
  ----
 PROCEDURE SELECT_DINH_KEM(P_CUR IN OUT MYCUR, P_MA_HS NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_DINH_KEM
           ,a.ma_hs
           ,a.tieu_de
           ,a.chi_tiet
           ,a.ngay_ban_hanh
           ,a.tom_tat
           ,a.co_quan
           ,a.loai_vb
           ,a.ky_hieu
           ,a.so_trang
           ,a.trang_thai
           ,b.ten_dk_trangthai as TEN_TT
           ,c.ten_loai_van_ban as TEN_VB
    FROM QLDC_DINH_KEM A
    left join qldc_dk_trangthai b on a.trang_thai = b.ma_dk_trangthai
    left join dm_loai_van_ban c on a.loai_vb = c.ma_loai_van_ban
    WHERE A.MA_HS = P_MA_HS;
  END;
 ----
 PROCEDURE SELECT_HSD_LIEN_QUAN(P_CUR IN OUT MYCUR ) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.*
    FROM QLDC_HSD_LIEN_QUAN A
    ;
  END;
 ----
 PROCEDURE SELECT_DINH_KEM_DETAIL(P_CUR IN OUT MYCUR, P_MA_DK NVARCHAR2) AS
 BEGIN
   OPEN P_CUR FOR
   SELECT A.MA_DINH_KEM
           ,a.ma_hs
           ,a.tieu_de
           ,a.chi_tiet
           ,a.ngay_ban_hanh
           ,a.tom_tat
           ,a.co_quan
           ,a.loai_vb
           ,a.ky_hieu
           ,a.so_trang
           ,a.trang_thai
           ,b.ten_dk_trangthai as TEN_TT
           ,c.ten_loai_van_ban as TEN_VB
    FROM QLDC_DINH_KEM A
    left join qldc_dk_trangthai b on a.trang_thai = b.ma_dk_trangthai
    left join dm_loai_van_ban c on a.loai_vb = c.ma_loai_van_ban
   WHERE A.MA_DINH_KEM = P_MA_DK;
 END;  
 ----
 PROCEDURE SELECT_HSD_LIEN_QUAN_DETAIL(P_CUR IN OUT MYCUR, P_MA_LQ NVARCHAR2) AS
 BEGIN
   OPEN P_CUR FOR
   SELECT A.*, TO_CHAR(a.ngay_ban_hanh,'DD/MM/YYYY') AS ngay_ban_hanh
   FROM  QLDC_HSD_LIEN_QUAN A
   WHERE A.MA_LQ = P_MA_LQ;
 END;  
 ----
  PROCEDURE DELETE_HO_SO_DAT(P_VAL OUT NVARCHAR2, P_MA_HS NVARCHAR2) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_HO_SO_DAT T WHERE T.MA_HS = P_MA_HS;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE DELETE_DINH_KEM(P_VAL OUT NVARCHAR2, P_DINH_KEM NVARCHAR2) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_DINH_KEM T WHERE T.MA_DINH_KEM = P_DINH_KEM;
    P_VAL := 'THANH_CONG';
 END;
 -----
 PROCEDURE DELETE_HSD_LIEN_QUAN(P_VAL OUT NVARCHAR2, P_MA_LQ NVARCHAR2) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_HSD_LIEN_QUAN T WHERE T.MA_LQ = P_MA_LQ;
    P_VAL := 'THANH_CONG';
 END;
 -----
  PROCEDURE DELETE_DTSD(P_VAL OUT NVARCHAR2, P_MA_DT NVARCHAR2) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_DOI_TUONG_SU_DUNG T WHERE T.MA_DT = P_MA_DT;
    P_VAL := 'THANH_CONG';
 END;
 -----
 PROCEDURE UPDATE_HO_SO_DAT(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE NVARCHAR2,
  P_MA_HS                     INTEGER ,
  P_TEN_HS                    NVARCHAR2,
  P_MA_TRANG_THAI             INTEGER,
  P_MA_HTSD                   INTEGER,
  P_GHI_CHU                   NVARCHAR2,
  P_MA_DTSD                   INTEGER,
  P_SO_TO                     NVARCHAR2,
  P_SO_THUA                   NVARCHAR2,
  P_DIEN_TICH                 NVARCHAR2,
  P_DC_AP                     NVARCHAR2,
  P_MA_XA                     NVARCHAR2,
  P_MA_HUYEN                  NVARCHAR2,
  P_MA_TINH                   NVARCHAR2,
  P_MUC_DICH_SU_DUNG          NVARCHAR2,
  P_THOI_GIAN_SU_DUNG         NVARCHAR2,
  P_SO_GCN                    NVARCHAR2,
  P_NGAY_CAP                  NVARCHAR2,
  P_DIEN_TICH_THUA            NVARCHAR2,
  P_TU_NGAY                   NVARCHAR2,
  P_DEN_NGAY                  NVARCHAR2,
  P_THOI_GIAN_THUE            FLOAT,
  P_GIA_TIEN_THUE             NVARCHAR2,
  P_MA_PHUONG_THUC_THANH_TOAN INTEGER,
  P_MA_HINH_THUC_THANH_TOAN   INTEGER,
  P_MA_BIEN_DONG INTEGER,
	P_MA_DAT_CONG INTEGER,
	P_MA_KQXD INTEGER, 
  P_MA_DKDKK INTEGER, 
  P_MA_LUY_TIEN INTEGER, 
  P_MA_NGUOI_QL INTEGER,
  P_MA_CAP_QL INTEGER,
  P_NAM_CAP_NHAT INTEGER,
  P_TOA_DO NVARCHAR2,
  P_SO_THUA_GOC NVARCHAR2,
  P_SU_DUNG NVARCHAR2,
  -- TH?A Ð?T
  P_TG_BAT_DAU NVARCHAR2,
  P_TG_KET_THUC NVARCHAR2,
  P_TRANG_THAI_PLUS INTEGER,
  -- THUÊ Ð?T
  P_DAU_GIA	NVARCHAR2,
  P_DON_VI_TO_CHUC_DAU_GIA NVARCHAR2,			
  P_LOAI_HINH_TRA_TIEN INTEGER,
  P_SO_TIEN	NVARCHAR2,		
  P_DON_GIA_THEO_LAN	NVARCHAR2,	
  -- N?P TI?N T?NG NAM	
  P_LY_DO_NOP_TIEN	NVARCHAR2,		
  P_NGAY_CAP_NHAT_NOP_TIEN	NVARCHAR2,			
  P_THANH_TIEN	NVARCHAR2,			
  P_DON_GIA	NVARCHAR2			,
  P_DIEN_TICH_QD NVARCHAR2,
  P_TIEN_DAU_GIA NVARCHAR2,
  P_THANH_TIEN_NOP_TIEN NVARCHAR2
) AS
    V_TON_TAI_MA INT;
  BEGIN
  P_VAL := 'CO_LOI';
  IF P_UPDATE_TYPE = 'THEM_MOI' THEN
  SELECT COUNT(MA_HS)
  INTO V_TON_TAI_MA
  FROM QLDC_HO_SO_DAT
  WHERE UPPER(MA_HS) LIKE UPPER(P_MA_HS);
       
  IF V_TON_TAI_MA > 0  THEN
    -- kiem tra trung ma , khong cho them ma='0'
    P_VAL := 'TON_TAI_MA';
  ELSE
    INSERT INTO QLDC_HO_SO_DAT
    (
      MA_HS                     ,
      TEN_HS                    ,
      MA_TRANG_THAI             ,
      MA_HTSD                   ,
      GHI_CHU                   ,
      MA_DTSD                   ,
      SO_TO                     ,
      SO_THUA                   ,
      DIEN_TICH                 ,
      DC_AP                     ,
      MA_XA                     ,
      MA_HUYEN                  ,
      MA_TINH                   ,
      MA_MUC_DICH_SU_DUNG       ,
      THOI_GIAN_SU_DUNG         ,
      SO_GCN                    ,
      NGAY_CAP                  ,
      DIEN_TICH_THUA            ,
      TU_NGAY                   ,
      DEN_NGAY                  ,
      THOI_GIAN_THUE            ,
      GIA_TIEN_THUE             ,
      MA_PHUONG_THUC_THANH_TOAN ,
      MA_HINH_THUC_THANH_TOAN   ,
      MA_BIEN_DONG,
	    MA_DAT_CONG,
	    MA_KQXD, 
      MA_DKDKK, 
      MA_LUY_TIEN, 
      MA_NGUOI_QL,
      MA_CAP_QL,
      NAM_CAP_NHAT,
      TOA_DO,
      SO_THUA_GOC,
      SU_DUNG,
       -- TH?A Ð?T
  TG_BAT_DAU,
  TG_KET_THUC,
  TRANG_THAI_PLUS ,
  -- THUÊ Ð?T
  DAU_GIA	,
  DON_VI_TO_CHUC_DAU_GIA ,			
  LOAI_HINH_TRA_TIEN,
  SO_TIEN	,		
  DON_GIA_THEO_LAN	,	
  -- N?P TI?N T?NG NAM	
  LY_DO_NOP_TIEN	,		
  NGAY_CAP_NHAT_NOP_TIEN,			
  THANH_TIEN,			
  DON_GIA		,
   DIEN_TICH_QD,
  TIEN_DAU_GIA ,
  THANH_TIEN_NOP_TIEN 
    )
    VALUES
    (
      SEQ_QLDC_MA_HO_SO.NEXTVAL   ,
      P_TEN_HS                    ,
      P_MA_TRANG_THAI             ,
      P_MA_HTSD                   ,
      P_GHI_CHU                   ,
      P_MA_DTSD                   ,
      P_SO_TO                     ,
      P_SO_THUA                   ,
      P_DIEN_TICH                 ,
      P_DC_AP                     ,
      P_MA_XA                     ,
      P_MA_HUYEN                  ,
      'TIEN_GIANG', --P_MA_TINH                   ,
      P_MUC_DICH_SU_DUNG          ,
      P_THOI_GIAN_SU_DUNG         ,
      P_SO_GCN                    ,
      to_date(P_NGAY_CAP,'DDMMYYYY')      ,
      P_DIEN_TICH_THUA            ,
      TO_DATE(P_TU_NGAY,'DDMMYYYY')       ,
      TO_DATE(P_DEN_NGAY,'DDMMYYYY')      ,
      P_THOI_GIAN_THUE            ,
      P_GIA_TIEN_THUE             ,
      P_MA_PHUONG_THUC_THANH_TOAN ,
      P_MA_HINH_THUC_THANH_TOAN,
      P_MA_BIEN_DONG ,
      P_MA_DAT_CONG ,
      P_MA_KQXD , 
      P_MA_DKDKK , 
      P_MA_LUY_TIEN , 
      P_MA_NGUOI_QL ,
      P_MA_CAP_QL ,
      P_NAM_CAP_NHAT,
      P_TOA_DO,
      P_SO_THUA_GOC,
      P_SU_DUNG,
       -- TH?A Ð?T
  TO_DATE(P_TG_BAT_DAU, 'DDMMYYYY') ,
  TO_DATE(P_TG_KET_THUC,'DDMMYYYY') ,
  P_TRANG_THAI_PLUS ,
  -- THUÊ Ð?T
  P_DAU_GIA	,
  P_DON_VI_TO_CHUC_DAU_GIA ,			
  P_LOAI_HINH_TRA_TIEN ,
  P_SO_TIEN	,		
  P_DON_GIA_THEO_LAN	,	
  -- N?P TI?N T?NG NAM	
  P_LY_DO_NOP_TIEN	,		
  P_NGAY_CAP_NHAT_NOP_TIEN,		
  P_THANH_TIEN	,			
  P_DON_GIA			,
   P_DIEN_TICH_QD ,
  P_TIEN_DAU_GIA ,
  P_THANH_TIEN_NOP_TIEN
      );
      P_VAL:='THANH_CONG';
     
  END IF;
  ELSE
                  
  UPDATE QLDC_HO_SO_DAT A
  SET 
      TEN_HS= P_TEN_HS                    ,
      MA_TRANG_THAI= P_MA_TRANG_THAI             ,
      MA_HTSD= P_MA_HTSD                   ,
      GHI_CHU =P_GHI_CHU                   ,
      MA_DTSD =P_MA_DTSD                   ,
      SO_TO= P_SO_TO                     ,
      SO_THUA=P_SO_THUA                   ,
      DIEN_TICH=P_DIEN_TICH                 ,
      DC_AP= P_DC_AP                     ,
      MA_XA=P_MA_XA                     ,
      MA_HUYEN=P_MA_HUYEN                  ,
      MA_TINH=P_MA_TINH                   ,
      MA_MUC_DICH_SU_DUNG=P_MUC_DICH_SU_DUNG          ,
      THOI_GIAN_SU_DUNG  =P_THOI_GIAN_SU_DUNG       ,
      SO_GCN   =P_SO_GCN                 ,
      NGAY_CAP   =TO_DATE(P_NGAY_CAP, 'DDMMYYYY')               ,
      DIEN_TICH_THUA =P_DIEN_TICH_THUA           ,
      TU_NGAY   =TO_DATE(P_TU_NGAY, 'DDMMYYYY')                ,
      DEN_NGAY     =TO_DATE(P_DEN_NGAY,'DDMMYYYY')             ,
      THOI_GIAN_THUE       =P_THOI_GIAN_THUE     ,
      GIA_TIEN_THUE  =P_GIA_TIEN_THUE           ,
      MA_PHUONG_THUC_THANH_TOAN=P_MA_PHUONG_THUC_THANH_TOAN ,
      MA_HINH_THUC_THANH_TOAN= P_MA_HINH_THUC_THANH_TOAN,
      MA_BIEN_DONG= P_MA_BIEN_DONG ,
      MA_DAT_CONG= P_MA_DAT_CONG ,
      MA_KQXD= P_MA_KQXD , 
      MA_DKDKK= P_MA_DKDKK , 
      MA_LUY_TIEN= P_MA_LUY_TIEN , 
      MA_NGUOI_QL= P_MA_NGUOI_QL ,
      MA_CAP_QL =P_MA_CAP_QL,
      NAM_CAP_NHAT= P_NAM_CAP_NHAT,
      TOA_DO= P_TOA_DO,
      SO_THUA_GOC= P_SO_THUA_GOC,
      SU_DUNG = P_SU_DUNG,
      
       -- TH?A Ð?T
  TG_BAT_DAU=TO_DATE(P_TG_BAT_DAU, 'DDMMYYYY'),
  TG_KET_THUC= TO_DATE(P_TG_KET_THUC, 'DDMMYYYY'),
  TRANG_THAI_PLUS= P_TRANG_THAI_PLUS,
  -- THUÊ Ð?T
  DAU_GIA= P_DAU_GIA	,
  DON_VI_TO_CHUC_DAU_GIA = P_DON_VI_TO_CHUC_DAU_GIA,			
  LOAI_HINH_TRA_TIEN= P_LOAI_HINH_TRA_TIEN ,
  SO_TIEN= P_SO_TIEN	,		
  DON_GIA_THEO_LAN=P_DON_GIA_THEO_LAN,	
  -- N?P TI?N T?NG NAM	
  LY_DO_NOP_TIEN= P_LY_DO_NOP_TIEN,		
  NGAY_CAP_NHAT_NOP_TIEN= P_NGAY_CAP_NHAT_NOP_TIEN,			
  THANH_TIEN = P_THANH_TIEN,			
  DON_GIA = P_DON_GIA			,
  DIEN_TICH_QD= P_DIEN_TICH_QD ,
  TIEN_DAU_GIA= P_TIEN_DAU_GIA ,
  THANH_TIEN_NOP_TIEN= P_THANH_TIEN_NOP_TIEN
  
  WHERE A.MA_HS =P_MA_HS;
  P_VAL := 'THANH_CONG';
  END IF;
 END;
 -------
 PROCEDURE UPDATE_DINH_KEM(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_DINH_KEM   INTEGER, --1
  P_TIEU_DE       NVARCHAR2, --2
  P_CHI_TIET NVARCHAR2, --3
  P_NGAY_BAN_HANH NVARCHAR2, --4
  P_MA_HS         INTEGER, --5
  P_TOM_TAT NVARCHAR2,
              P_CO_QUAN NVARCHAR2,
              P_LOAI_VB NVARCHAR2,
              P_KY_HIEU NVARCHAR2,
              P_SO_TRANG NVARCHAR2,
              P_TRANG_THAI INTEGER
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(A.MA_DINH_KEM)
        INTO V_TON_TAI_MA
        FROM QLDC_DINH_KEM A
       WHERE --UPPER(A.MA_HS) LIKE UPPER(P_MA_HS) AND 
       UPPER(A.MA_DINH_KEM) LIKE UPPER(P_MA_DINH_KEM);
    IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
    ELSE
        INSERT INTO  QLDC_DINH_KEM
          (
            MA_HS ,
            MA_DINH_KEM   ,
            TIEU_DE       ,
            CHI_TIET ,
            NGAY_BAN_HANH		 ,
              TOM_TAT,
              CO_QUAN,
              LOAI_VB,
              KY_HIEU,
              SO_TRANG,
              TRANG_THAI
          )
        VALUES   
          (
            P_MA_HS,		
            SEQ_QLDC_MA_DINH_KEM.NEXTVAL,
            P_TIEU_DE,
            P_CHI_TIET, 
            to_date(P_NGAY_BAN_HANH,'DD/MM/YYYY'),
            P_TOM_TAT,
            P_CO_QUAN,
            P_LOAI_VB,
            P_KY_HIEU,
            P_SO_TRANG,
            P_TRANG_THAI
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_DINH_KEM A
        SET 
            TIEU_DE    = P_TIEU_DE   ,
            NGAY_BAN_HANH	= P_NGAY_BAN_HANH,
            CHI_TIET = P_CHI_TIET,  
            TOM_TAT= P_TOM_TAT,
            CO_QUAN= P_CO_QUAN,
            LOAI_VB= P_LOAI_VB,
            KY_HIEU= P_KY_HIEU,
            SO_TRANG= P_SO_TRANG,
            TRANG_THAI= P_TRANG_THAI
         WHERE   A.MA_HS  like P_MA_HS
           AND  A.MA_DINH_KEM like P_MA_DINH_KEM ;
        P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE UPDATE_HSD_LIEN_QUAN(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_LQ INTEGER,
  P_TIEU_DE       NVARCHAR2, --2
  P_NGAY_BAN_HANH NVARCHAR2,
  P_CHI_TIET NVARCHAR2, --3
  P_MA_HS         INTEGER --5
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(A.MA_LQ)
        INTO V_TON_TAI_MA
        FROM QLDC_HSD_LIEN_QUAN A
       WHERE UPPER(A.MA_LQ) LIKE UPPER(MA_LQ);
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE
        INSERT INTO QLDC_HSD_LIEN_QUAN
          (
            MA_HS,
            MA_LQ,
            TIEU_DE,
            CHI_TIET,
            NGAY_BAN_HANH		   
          )
        VALUES   
          (
            P_MA_HS,	
            SEQ_QLDC_MA_DINH_KEM.NEXTVAL, 
            P_TIEU_DE,
            P_CHI_TIET, 
            to_date(P_NGAY_BAN_HANH,'DD/MM/YYYY')
            );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_HSD_LIEN_QUAN A
        SET 
            TIEU_DE    = P_TIEU_DE
            ,NGAY_BAN_HANH	= P_NGAY_BAN_HANH
            , CHI_TIET = P_CHI_TIET  
         WHERE A.MA_LQ like P_MA_LQ; 
        P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE UPDATE_DTSD(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
 
  P_TEN_DVSD NVARCHAR2,
  P_DIA_CHI NVARCHAR2,
  P_MA_LOAI_HINH INTEGER,
  P_MA_LOAI_DTSD INTEGER,
  P_QUOC_TICH NVARCHAR2, 
  P_SDT NVARCHAR2,
  P_GHI_CHU NVARCHAR2,
  P_MA_DT         INTEGER
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(A.MA_DT)
        INTO V_TON_TAI_MA
        FROM QLDC_DOI_TUONG_SU_DUNG A
       WHERE UPPER(A.MA_DT) LIKE UPPER(P_MA_DT);
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE
        INSERT INTO  QLDC_DOI_TUONG_SU_DUNG
          (
            MA_DT        
            ,TEN_DVSD     
            ,DIA_CHI      
            ,MA_LOAI_HINH
            ,MA_LOAI_DTSD
            ,QUOC_TICH
            ,SDT
            ,GHI_CHU        
          )
        VALUES   
          (
            SEQ_QLDC_MA_DTSD.NEXTVAL,		
            P_TEN_DVSD,
            P_DIA_CHI,
            P_MA_LOAI_HINH,
            P_MA_LOAI_DTSD, 
            P_QUOC_TICH,
            P_SDT,
            P_GHI_CHU
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_DOI_TUONG_SU_DUNG A
        SET 
            TEN_DVSD= P_TEN_DVSD,
            DIA_CHI=P_DIA_CHI,
            MA_LOAI_HINH=P_MA_LOAI_HINH,
            MA_LOAI_DTSD=P_MA_LOAI_DTSD, 
            QUOC_TICH = P_QUOC_TICH,
            SDT=P_SDT,
            MA_UPDATE = QLDC_MA_UPDATE_SEQ.NEXTVAL,
            GHI_CHU=P_GHI_CHU  
         WHERE   A.MA_DT  like P_MA_DT;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
  PROCEDURE SELECT_DTSD_DETAIL(P_CUR IN OUT MYCUR, P_MA_DT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_DT
           ,A.TEN_DVSD
           ,A.DIA_CHI
           ,a.ma_loai_hinh
           ,a.ma_loai_dtsd
           , c.ten_loai_hinh
           , b.ten_loai_dtsd
           ,A.QUOC_TICH
           ,a.sdt AS SDT
           ,A.GHI_CHU
    --FROM QLDC_DOI_TUONG_SU_DUNG A--, QLDC_DTSD_LOAI B, QLDC_DTSD_LOAI_HINH C
    --WHERE A.MA_DT = P_MA_DT
    --AND A.MA_LOAI_HINH =C.MA_LOAI_HINH
    --AND A.Ma_Loai_Dtsd=b.ma_loai_dtsd ORDER BY A.MA_DT DESC;
    FROM QLDC_DOI_TUONG_SU_DUNG A     

    LEFT JOIN QLDC_DTSD_LOAI B ON A.Ma_Loai_Dtsd=b.ma_loai_dtsd --and A.MA_DT = P_MA_DT
    LEFT JOIN QLDC_DTSD_LOAI_HINH C ON A.MA_LOAI_HINH =C.MA_LOAI_HINH
    WHERE A.MA_DT = P_MA_DT
    ORDER BY A.MA_DT DESC;
    
  END;
 ---
 PROCEDURE SELECT_DTSD(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_DT
           ,A.TEN_DVSD
           ,A.DIA_CHI
           ,c.ten_loai_hinh
           ,b.ten_loai_dtsd
           ,A.QUOC_TICH
           ,a.sdt as SDT
           ,A.GHI_CHU
    /*FROM QLDC_DOI_TUONG_SU_DUNG A, QLDC_DTSD_LOAI B, QLDC_DTSD_LOAI_HINH C
    WHERE A.MA_LOAI_HINH =C.MA_LOAI_HINH
    AND A.Ma_Loai_Dtsd=b.ma_loai_dtsd
    ORDER BY A.MA_DT DESC*/
    FROM QLDC_DOI_TUONG_SU_DUNG A
    LEFT JOIN QLDC_DTSD_LOAI B ON A.Ma_Loai_Dtsd=b.ma_loai_dtsd
    LEFT JOIN QLDC_DTSD_LOAI_HINH C ON A.MA_LOAI_HINH =C.MA_LOAI_HINH
    ORDER BY A.MA_DT DESC;
  END;
 ----
 PROCEDURE UPDATE_HTSDD(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_HINH_THUC  INTEGER, 
  P_TEN_HINH_THUC NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_HTSDD)
        INTO V_TON_TAI_MA
        FROM QLDC_HINH_THUC_SU_DUNG_DAT T
       WHERE T.MA_HTSDD = P_MA_HINH_THUC;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_HINH_THUC_SU_DUNG_DAT T
          (
            MA_HTSDD        
            ,TEN_HTSDD     
          )
        VALUES   
          (
            QLDC_MA_HINH_THUC_SEQ.NEXTVAL
            ,P_TEN_HINH_THUC
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_HINH_THUC_SU_DUNG_DAT T
        SET 
            TEN_HTSDD= P_TEN_HINH_THUC
  
         WHERE   T.MA_HTSDD  = P_MA_HINH_THUC;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE UPDATE_TRANG_THAI(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_TRANG_THAI  INTEGER, 
  P_TEN_TRANG_THAI NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_TRANG_THAI)
        INTO V_TON_TAI_MA
        FROM QLDC_TRANG_THAI_HO_SO T
       WHERE T.MA_TRANG_THAI = P_MA_TRANG_THAI;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_TRANG_THAI_HO_SO T
          (
            MA_TRANG_THAI        
            ,TEN_TRANG_THAI
          )
        VALUES   
          (
            QLDC_MA_TRANG_THAI_HS_SEQ.NEXTVAL
            ,P_TEN_TRANG_THAI
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_TRANG_THAI_HO_SO T
        SET 
            TEN_TRANG_THAI= P_TEN_TRANG_THAI
  
         WHERE   T.MA_TRANG_THAI  = P_MA_TRANG_THAI;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE UPDATE_PTTT(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_PTTT  INTEGER, 
  P_TEN_PTTT NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_PTTT)
        INTO V_TON_TAI_MA
        FROM QLDC_PHUONG_THUC_THANH_TOAN T
       WHERE T.MA_PTTT = P_MA_PTTT;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_PHUONG_THUC_THANH_TOAN T
          (
            MA_PTTT        
            ,TEN_PTTT
          )
        VALUES   
          (
            QLDC_MA_PTTT_SEQ.NEXTVAL
            ,P_TEN_PTTT
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_PHUONG_THUC_THANH_TOAN T
        SET 
            TEN_PTTT= P_TEN_PTTT  
         WHERE   T.MA_PTTT  = P_MA_PTTT;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE UPDATE_HTTT(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_HTTT  INTEGER, 
  P_TEN_HTTT NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_HTTT)
        INTO V_TON_TAI_MA
        FROM QLDC_HINH_THUC_THANH_TOAN T
       WHERE T.MA_HTTT = P_MA_HTTT;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_HINH_THUC_THANH_TOAN T
          (
            MA_HTTT        
            ,TEN_HTTT
          )
        VALUES   
          (
            QLDC_MA_HTTT_SEQ.NEXTVAL
            ,P_TEN_HTTT
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_HINH_THUC_THANH_TOAN T
        SET 
            TEN_HTTT= P_TEN_HTTT  
         WHERE   T.MA_HTTT  = P_MA_HTTT;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE UPDATE_CSD_LOAI(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_LOAI_DTSD  INTEGER, 
  P_TEN_LOAI_DTSD NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_LOAI_DTSD)
        INTO V_TON_TAI_MA
        FROM QLDC_DTSD_LOAI T
       WHERE T.MA_LOAI_DTSD = P_MA_LOAI_DTSD;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_DTSD_LOAI T
          (
            MA_LOAI_DTSD        
            ,TEN_LOAI_DTSD
          )
        VALUES   
          (
            QLDC_MA_CSD_LOAI_SEQ.NEXTVAL
            ,P_TEN_LOAI_DTSD
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_DTSD_LOAI T
        SET 
            TEN_LOAI_DTSD = P_TEN_LOAI_DTSD  
         WHERE   T.MA_LOAI_DTSD  = P_MA_LOAI_DTSD;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE UPDATE_CSD_LOAI_HINH(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_LOAI_HINH  INTEGER, 
  P_TEN_LOAI_HINH NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_LOAI_HINH)
        INTO V_TON_TAI_MA
        FROM QLDC_DTSD_LOAI_HINH T
       WHERE T.MA_LOAI_HINH = P_MA_LOAI_HINH;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_DTSD_LOAI_HINH T
          (
            MA_LOAI_HINH        
            ,TEN_LOAI_HINH
          )
        VALUES   
          (
            QLDC_MA_CSD_LOAI_HINH_SEQ.NEXTVAL
            ,P_TEN_LOAI_HINH
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_DTSD_LOAI_HINH T
        SET 
            TEN_LOAI_HINH = P_TEN_LOAI_HINH 
         WHERE   T.MA_LOAI_HINH  = P_MA_LOAI_HINH;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE SELECT_MDSDD_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_MUC_DICH ,
           T.TEN_MUC_DICH ,
           T.TEN_TAT
    FROM qldc_muc_dich_su_dung_dat T
    ORDER BY T.MA_MUC_DICH;
  END;
 ----
 PROCEDURE SELECT_HTSDD_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_HTSDD ,
           T.TEN_HTSDD 
    FROM QLDC_HINH_THUC_SU_DUNG_DAT T
    ORDER BY T.MA_HTSDD;
  END;
 ----
 PROCEDURE SELECT_TRANG_THAI_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_TRANG_THAI ,
           T.TEN_TRANG_THAI 
    FROM QLDC_TRANG_THAI_HO_SO T
    ORDER BY T.MA_TRANG_THAI;
  END;
 ----
 PROCEDURE SELECT_PTTT_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_PTTT ,
           T.TEN_PTTT 
    FROM QLDC_PHUONG_THUC_THANH_TOAN T
    ORDER BY T.MA_PTTT;
  END;
 ----
 PROCEDURE SELECT_HTTT_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_HTTT ,
           T.TEN_HTTT 
    FROM QLDC_HINH_THUC_THANH_TOAN T
    ORDER BY T.MA_HTTT;
  END;
 ----
 PROCEDURE SELECT_CSD_LOAI_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LOAI_DTSD ,
           T.TEN_LOAI_DTSD 
    FROM QLDC_DTSD_LOAI T
    ORDER BY T.MA_LOAI_DTSD;
  END;
 ----
 PROCEDURE SELECT_CSD_LOAI_HINH_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LOAI_HINH ,
           T.TEN_LOAI_HINH 
    FROM QLDC_DTSD_LOAI_HINH T
    ORDER BY T.MA_LOAI_HINH;
  END;
 ----
 PROCEDURE SELECT_PTTT_DETAIL(P_CUR IN OUT MYCUR, P_MA_PTTT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_PTTT ,
           T.TEN_PTTT 
    FROM QLDC_PHUONG_THUC_THANH_TOAN T
    WHERE T.MA_PTTT = P_MA_PTTT;
  END;
 ----
 PROCEDURE SELECT_HTTT_DETAIL(P_CUR IN OUT MYCUR, P_MA_HTTT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_HTTT ,
           T.TEN_HTTT 
    FROM QLDC_HINH_THUC_THANH_TOAN T
    WHERE T.MA_HTTT = P_MA_HTTT;
  END;
 ----
 PROCEDURE SELECT_CSD_LOAI_DETAIL(P_CUR IN OUT MYCUR, P_MA_LOAI_DTSD NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LOAI_DTSD ,
           T.TEN_LOAI_DTSD 
    FROM QLDC_DTSD_LOAI T
    WHERE T.MA_LOAI_DTSD = P_MA_LOAI_DTSD;
  END;
 ----
 PROCEDURE SELECT_CSD_LOAI_HINH_DETAIL(P_CUR IN OUT MYCUR, P_MA_LOAI_HINH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LOAI_HINH ,
           T.TEN_LOAI_HINH 
    FROM QLDC_DTSD_LOAI_HINH T
    WHERE T.MA_LOAI_HINH = P_MA_LOAI_HINH;
  END;
 ----
 PROCEDURE SELECT_MDSDD_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_MUC_DICH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.*
    FROM qldc_muc_dich_su_dung_dat T
    WHERE T.MA_MUC_DICH = P_MA_MUC_DICH;
  END;
 ----
 PROCEDURE SELECT_HTSDD_DETAIL(P_CUR IN OUT MYCUR, P_MA_HINH_THUC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_HTSDD ,
           T.TEN_HTSDD 
    FROM QLDC_HINH_THUC_SU_DUNG_DAT T
    WHERE T.MA_HTSDD = P_MA_HINH_THUC;
  END;
 ----
  PROCEDURE SELECT_TRANG_THAI_DETAIL(P_CUR IN OUT MYCUR, P_MA_TRANG_THAI NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_TRANG_THAI ,
           T.TEN_TRANG_THAI 
    FROM QLDC_TRANG_THAI_HO_SO T
    WHERE T.MA_TRANG_THAI = P_MA_TRANG_THAI;
  END;
 ----
 PROCEDURE UPDATE_MDSDD(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_MUC_DICH  INTEGER, 
  P_TEN_MUC_DICH NVARCHAR2,
  P_TEN_TAT      NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_MUC_DICH)
        INTO V_TON_TAI_MA
        FROM qldc_muc_dich_su_dung_dat T
       WHERE T.MA_MUC_DICH = P_MA_MUC_DICH;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE
        INSERT INTO  QLDC_MUC_DICH_SU_DUNG_DAT
          (
            MA_MUC_DICH        
            ,TEN_MUC_DICH     
            ,TEN_TAT             
          )
        VALUES   
          (
            QLDC_MA_MUC_DICH_SEQ.NEXTVAL,		
            P_TEN_MUC_DICH,
            P_TEN_TAT
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_MUC_DICH_SU_DUNG_DAT T
        SET 
            TEN_MUC_DICH= P_TEN_MUC_DICH,
            TEN_TAT=P_TEN_TAT  
         WHERE   T.MA_MUC_DICH  = P_MA_MUC_DICH;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
 ----
 PROCEDURE DELETE_HTSDD(P_VAL OUT NVARCHAR2, P_MA_HINH_THUC INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_HINH_THUC_SU_DUNG_DAT T WHERE T.MA_HTSDD = P_MA_HINH_THUC;
    P_VAL := 'THANH_CONG';
 END;
 ----
 
 ----
 PROCEDURE DELETE_TRANG_THAI(P_VAL OUT NVARCHAR2, P_MA_TRANG_THAI INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_TRANG_THAI_HO_SO T WHERE T.MA_TRANG_THAI = P_MA_TRANG_THAI;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE DELETE_PTTT(P_VAL OUT NVARCHAR2, P_MA_PTTT INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_PHUONG_THUC_THANH_TOAN T WHERE T.MA_PTTT = P_MA_PTTT;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE DELETE_HTTT(P_VAL OUT NVARCHAR2, P_MA_HTTT INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_HINH_THUC_THANH_TOAN T WHERE T.MA_HTTT = P_MA_HTTT;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE DELETE_CSD_LOAI(P_VAL OUT NVARCHAR2, P_MA_LOAI_DTSD INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_DTSD_LOAI T WHERE T.MA_LOAI_DTSD = P_MA_LOAI_DTSD;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE DELETE_CSD_LOAI_HINH(P_VAL OUT NVARCHAR2, P_MA_LOAI_HINH INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_DTSD_LOAI_HINH T WHERE T.MA_LOAI_HINH = P_MA_LOAI_HINH;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE DELETE_MDSDD(P_VAL OUT NVARCHAR2, P_MA_MUC_DICH INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_MUC_DICH_SU_DUNG_DAT T WHERE T.MA_MUC_DICH = P_MA_MUC_DICH;
    P_VAL := 'THANH_CONG';
 END;
 ----
 ----
 PROCEDURE SELECT_LOAIVB_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LOAI_VAN_BAN ,
           T.TEN_LOAI_VAN_BAN 
    FROM DM_LOAI_VAN_BAN T
    ORDER BY T.MA_LOAI_VAN_BAN;
  END;
----
 PROCEDURE SELECT_LOAIVB_DETAIL(P_CUR IN OUT MYCUR, P_MA_LOAI_VAN_BAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LOAI_VAN_BAN ,
           T.TEN_LOAI_VAN_BAN 
    FROM DM_LOAI_VAN_BAN T
    WHERE T.MA_LOAI_VAN_BAN = P_MA_LOAI_VAN_BAN;
  END;
----
  PROCEDURE UPDATE_LOAIVB(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_LOAI_VAN_BAN  NVARCHAR2, 
  P_TEN_LOAI_VAN_BAN NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_LOAI_VAN_BAN)
        INTO V_TON_TAI_MA
        FROM DM_LOAI_VAN_BAN T
       WHERE T.MA_LOAI_VAN_BAN = P_MA_LOAI_VAN_BAN;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  DM_LOAI_VAN_BAN T
          (
            MA_LOAI_VAN_BAN        
            ,TEN_LOAI_VAN_BAN
          )
        VALUES   
          (
            P_MA_LOAI_VAN_BAN,
            P_TEN_LOAI_VAN_BAN
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE DM_LOAI_VAN_BAN T
        SET 
            TEN_LOAI_VAN_BAN= P_TEN_LOAI_VAN_BAN  
         WHERE   T.MA_LOAI_VAN_BAN  = P_MA_LOAI_VAN_BAN;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
----
  PROCEDURE DELETE_LOAIVB(P_VAL OUT NVARCHAR2, P_MA_LOAI_VAN_BAN NVARCHAR2) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM DM_LOAI_VAN_BAN T WHERE T.MA_LOAI_VAN_BAN = P_MA_LOAI_VAN_BAN;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE SELECT_TRANGTHAIVB_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_DK_TRANGTHAI ,
           T.TEN_DK_TRANGTHAI 
    FROM QLDC_DK_TRANGTHAI T
    ORDER BY T.MA_DK_TRANGTHAI;
  END;
----
PROCEDURE SELECT_TRANGTHAIVB_DETAIL(P_CUR IN OUT MYCUR, P_MA_DK_TRANGTHAI NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_DK_TRANGTHAI ,
           T.TEN_DK_TRANGTHAI 
    FROM QLDC_DK_TRANGTHAI T
    WHERE T.MA_DK_TRANGTHAI = P_MA_DK_TRANGTHAI;
  END;
----
PROCEDURE UPDATE_TRANGTHAIVB(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_DK_TRANGTHAI  INTEGER, 
  P_TEN_DK_TRANGTHAI NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_DK_TRANGTHAI)
        INTO V_TON_TAI_MA
        FROM QLDC_DK_TRANGTHAI T
       WHERE T.MA_DK_TRANGTHAI = P_MA_DK_TRANGTHAI;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_DK_TRANGTHAI T
          (
            MA_DK_TRANGTHAI        
            ,TEN_DK_TRANGTHAI
          )
        VALUES   
          (
            QLDC_MA_DK_TRANGTHAI_SEQ.NEXTVAL
            ,P_TEN_DK_TRANGTHAI
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_DK_TRANGTHAI T
        SET 
            TEN_DK_TRANGTHAI= P_TEN_DK_TRANGTHAI  
         WHERE   T.MA_DK_TRANGTHAI  = P_MA_DK_TRANGTHAI;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
----
PROCEDURE DELETE_TRANGTHAIVB(P_VAL OUT NVARCHAR2, P_MA_DK_TRANGTHAI INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_DK_TRANGTHAI T WHERE T.MA_DK_TRANGTHAI = P_MA_DK_TRANGTHAI;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE SELECT_HTSDHSD_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_HTSDD ,
           T.TEN_HTSDD 
    FROM QLDC_HINH_THUC_SU_DUNG_DAT T
    ORDER BY T.MA_HTSDD;
  END;
----
PROCEDURE SELECT_HTSDHSD_DETAIL(P_CUR IN OUT MYCUR, P_MA_HTSDD NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_HTSDD ,
           T.TEN_HTSDD 
    FROM QLDC_HINH_THUC_SU_DUNG_DAT T
    WHERE T.MA_HTSDD = P_MA_HTSDD;
  END;
----
PROCEDURE UPDATE_HTSDHSD(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_HTSDD  INTEGER, 
  P_TEN_HTSDD NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_HTSDD)
        INTO V_TON_TAI_MA
        FROM QLDC_HINH_THUC_SU_DUNG_DAT T
       WHERE T.MA_HTSDD = P_MA_HTSDD;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_HINH_THUC_SU_DUNG_DAT T
          (
            MA_HTSDD        
            ,TEN_HTSDD
          )
        VALUES   
          (
            QLDC_MA_HTSDHSD_SEQ.NEXTVAL
            ,P_TEN_HTSDD
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_HINH_THUC_SU_DUNG_DAT T
        SET 
            TEN_HTSDD= P_TEN_HTSDD  
         WHERE   T.MA_HTSDD  = P_MA_HTSDD;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
----
PROCEDURE DELETE_HTSDHSD(P_VAL OUT NVARCHAR2, P_MA_HTSDD INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_HINH_THUC_SU_DUNG_DAT T WHERE T.MA_HTSDD = P_MA_HTSDD;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE SELECT_LUYTIEN_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LUY_TIEN ,
           T.TEN_LUY_TIEN 
    FROM QLDC_LUY_TIEN T
    ORDER BY T.MA_LUY_TIEN;
  END;
----
PROCEDURE SELECT_LUYTIEN_DETAIL(P_CUR IN OUT MYCUR, P_MA_LUY_TIEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LUY_TIEN ,
           T.TEN_LUY_TIEN 
    FROM QLDC_LUY_TIEN T
    WHERE T.MA_LUY_TIEN = P_MA_LUY_TIEN;
  END;
----
PROCEDURE UPDATE_LUYTIEN(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_LUY_TIEN  INTEGER, 
  P_TEN_LUY_TIEN NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_LUY_TIEN)
        INTO V_TON_TAI_MA
        FROM QLDC_LUY_TIEN T
       WHERE T.MA_LUY_TIEN = P_MA_LUY_TIEN;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_LUY_TIEN T
          (
            MA_LUY_TIEN        
            ,TEN_LUY_TIEN
          )
        VALUES   
          (
            QLDC_MA_LUY_TIEN_SEQ.NEXTVAL
            ,P_TEN_LUY_TIEN
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_LUY_TIEN T
        SET 
            TEN_LUY_TIEN= P_TEN_LUY_TIEN  
         WHERE   T.MA_LUY_TIEN  = P_MA_LUY_TIEN;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
----
PROCEDURE DELETE_LUYTIEN(P_VAL OUT NVARCHAR2, P_MA_LUY_TIEN INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_LUY_TIEN T WHERE T.MA_LUY_TIEN = P_MA_LUY_TIEN;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE SELECT_NGUOIQL_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_NGUOI_QL ,
           T.TEN_NGUOI_QL 
    FROM QLDC_NGUOI_QL T
    ORDER BY T.MA_NGUOI_QL;
  END;
----
PROCEDURE SELECT_NGUOIQL_DETAIL(P_CUR IN OUT MYCUR, P_MA_NGUOI_QL NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_NGUOI_QL ,
           T.TEN_NGUOI_QL 
    FROM QLDC_NGUOI_QL T
    WHERE T.MA_NGUOI_QL = P_MA_NGUOI_QL;
  END;
----
PROCEDURE UPDATE_NGUOIQL(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_NGUOI_QL  INTEGER, 
  P_TEN_NGUOI_QL NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_NGUOI_QL)
        INTO V_TON_TAI_MA
        FROM QLDC_NGUOI_QL T
       WHERE T.MA_NGUOI_QL = P_MA_NGUOI_QL;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_NGUOI_QL T
          (
            MA_NGUOI_QL        
            ,TEN_NGUOI_QL
          )
        VALUES   
          (
            QLDC_MA_NGUOI_QL_SEQ.NEXTVAL
            ,P_TEN_NGUOI_QL
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_NGUOI_QL T
        SET 
            TEN_NGUOI_QL= P_TEN_NGUOI_QL  
         WHERE   T.MA_NGUOI_QL  = P_MA_NGUOI_QL;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
----
PROCEDURE DELETE_NGUOIQL(P_VAL OUT NVARCHAR2, P_MA_NGUOI_QL INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_NGUOI_QL T WHERE T.MA_NGUOI_QL = P_MA_NGUOI_QL;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE SELECT_CAPQL_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_CAP_QL ,
           T.TEN_CAP_QL 
    FROM QLDC_CAP_QL T
    ORDER BY T.MA_CAP_QL;
  END;
----
PROCEDURE SELECT_CAPQL_DETAIL(P_CUR IN OUT MYCUR, P_MA_CAP_QL NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_CAP_QL ,
           T.TEN_CAP_QL 
    FROM QLDC_CAP_QL T
    WHERE T.MA_CAP_QL = P_MA_CAP_QL;
  END;
----
PROCEDURE UPDATE_CAPQL(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_CAP_QL  INTEGER, 
  P_TEN_CAP_QL NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_CAP_QL)
        INTO V_TON_TAI_MA
        FROM QLDC_CAP_QL T
       WHERE T.MA_CAP_QL = P_MA_CAP_QL;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_CAP_QL T
          (
            MA_CAP_QL        
            ,TEN_CAP_QL
          )
        VALUES   
          (
            QLDC_MA_CAP_QL_SEQ.NEXTVAL
            ,P_TEN_CAP_QL
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_CAP_QL T
        SET 
            TEN_CAP_QL= P_TEN_CAP_QL  
         WHERE   T.MA_CAP_QL  = P_MA_CAP_QL;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
----
PROCEDURE DELETE_CAPQL(P_VAL OUT NVARCHAR2, P_MA_CAP_QL INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_CAP_QL T WHERE T.MA_CAP_QL = P_MA_CAP_QL;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE SELECT_TRANGTHAIPLUS_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_TRANG_THAI_PLUS ,
           T.TEN_TRANG_THAI_PLUS 
    FROM QLDC_TRANG_THAI_PLUS T
    ORDER BY T.MA_TRANG_THAI_PLUS;
  END;
----
PROCEDURE SELECT_TRANGTHAIPLUS_DETAIL(P_CUR IN OUT MYCUR, P_MA_TRANG_THAI_PLUS NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_TRANG_THAI_PLUS ,
           T.TEN_TRANG_THAI_PLUS 
    FROM QLDC_TRANG_THAI_PLUS T
    WHERE T.MA_TRANG_THAI_PLUS = P_MA_TRANG_THAI_PLUS;
  END;
----
PROCEDURE UPDATE_TRANGTHAIPLUS(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_TRANG_THAI_PLUS  INTEGER, 
  P_TEN_TRANG_THAI_PLUS NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_TRANG_THAI_PLUS)
        INTO V_TON_TAI_MA
        FROM QLDC_TRANG_THAI_PLUS T
       WHERE T.MA_TRANG_THAI_PLUS = P_MA_TRANG_THAI_PLUS;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_TRANG_THAI_PLUS T
          (
            MA_TRANG_THAI_PLUS        
            ,TEN_TRANG_THAI_PLUS
          )
        VALUES   
          (
            QLDC_MA_TRANG_THAI_PLUS_SEQ.NEXTVAL
            ,P_TEN_TRANG_THAI_PLUS
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_TRANG_THAI_PLUS T
        SET 
            TEN_TRANG_THAI_PLUS= P_TEN_TRANG_THAI_PLUS  
         WHERE   T.MA_TRANG_THAI_PLUS  = P_MA_TRANG_THAI_PLUS;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
----
PROCEDURE DELETE_TRANGTHAIPLUS(P_VAL OUT NVARCHAR2, P_MA_TRANG_THAI_PLUS INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_TRANG_THAI_PLUS T WHERE T.MA_TRANG_THAI_PLUS = P_MA_TRANG_THAI_PLUS;
    P_VAL := 'THANH_CONG';
 END;
 ----
 PROCEDURE SELECT_LHTRATIEN_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LOAI_HINH_TRA_TIEN ,
           T.TEN_LOAI_HINH_TRA_TIEN 
    FROM QLDC_LOAI_HINH_TRA_TIEN T
    ORDER BY T.MA_LOAI_HINH_TRA_TIEN;
  END;
----
PROCEDURE SELECT_LHTRATIEN_DETAIL(P_CUR IN OUT MYCUR, P_MA_LOAI_HINH_TRA_TIEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT T.MA_LOAI_HINH_TRA_TIEN ,
           T.TEN_LOAI_HINH_TRA_TIEN 
    FROM QLDC_LOAI_HINH_TRA_TIEN T
    WHERE T.MA_LOAI_HINH_TRA_TIEN = P_MA_LOAI_HINH_TRA_TIEN;
  END;
----
PROCEDURE UPDATE_LHTRATIEN(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE    NVARCHAR2,
  P_MA_LOAI_HINH_TRA_TIEN  INTEGER, 
  P_TEN_LOAI_HINH_TRA_TIEN NVARCHAR2
 ) AS
  
  V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(T.MA_LOAI_HINH_TRA_TIEN)
        INTO V_TON_TAI_MA
        FROM QLDC_LOAI_HINH_TRA_TIEN T
       WHERE T.MA_LOAI_HINH_TRA_TIEN = P_MA_LOAI_HINH_TRA_TIEN;
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE --
        INSERT INTO  QLDC_LOAI_HINH_TRA_TIEN T
          (
            MA_LOAI_HINH_TRA_TIEN        
            ,TEN_LOAI_HINH_TRA_TIEN
          )
        VALUES   
          (
            QLDC_MA_LOAI_HINH_TRA_TIEN_SEQ.NEXTVAL
            ,P_TEN_LOAI_HINH_TRA_TIEN
          );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE QLDC_LOAI_HINH_TRA_TIEN T
        SET 
            TEN_LOAI_HINH_TRA_TIEN= P_TEN_LOAI_HINH_TRA_TIEN  
         WHERE   T.MA_LOAI_HINH_TRA_TIEN  = P_MA_LOAI_HINH_TRA_TIEN;
                 P_VAL := 'THANH_CONG';
    END IF;
  END;
----
PROCEDURE DELETE_LHTRATIEN(P_VAL OUT NVARCHAR2, P_MA_LOAI_HINH_TRA_TIEN INTEGER) AS
 BEGIN 
    P_VAL := 'CO_LOI';
    DELETE FROM QLDC_LOAI_HINH_TRA_TIEN T WHERE T.MA_LOAI_HINH_TRA_TIEN = P_MA_LOAI_HINH_TRA_TIEN;
    P_VAL := 'THANH_CONG';
 END;
 ----
 END QLDC;
/

prompt
prompt Creating package body QLDC_TK
prompt =============================
prompt
create or replace package body hai.QLDC_TK is
PROCEDURE SELECT_SLHSD(P_CUR IN OUT MYCUR) AS
  BEGIN
     OPEN P_CUR FOR
     SELECT count(DISTINCT MA_HS)
        FROM QLDC_DINH_KEM;
  END;
  
PROCEDURE SELECT_COL_HSD(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT round(max(length(A.MA_HS)),1) AS MA_HS
             , round(max(length(A.TEN_HS)),1) AS TEN_HS
             , round(max(length(A.SO_TO)),1) AS SO_TO
             , round(max(length(A.SO_THUA)),1) AS SO_THUA
             , round(max(length(A.GHI_CHU)),1) AS GHI_CHU
             , round(max(length(A.DIEN_TICH)),1) AS DIEN_TICH
             , round(max(length(A.DC_AP)),1) AS DC_AP
             , round(max(length(A.THOI_GIAN_SU_DUNG)),1) AS THOI_GIAN_SU_DUNG
             , round(max(length(A.SO_GCN)),1) AS SO_GCN
             , round(max(length(to_char(A.NGAY_CAP,'DD/MM/YYYY') )),1) AS NGAY_CAP
             , round(max(length(A.DIEN_TICH_THUA)),1) AS DIEN_TICH_THUA
             , round(max(length(to_char(A.TU_NGAY,'DD/MM/YYYY') )),1) AS TU_NGAY
             , round(max(length(to_char(A.DEN_NGAY,'DD/MM/YYYY') )),1) AS DEN_NGAY
             , round(max(length(A.THOI_GIAN_THUE)),1) AS THOI_GIAN_THUE
             , round(max(length(A.GIA_TIEN_THUE)),1) AS GIA_TIEN_THUE
             , round(max(length(A.NAM_CAP_NHAT)),1) AS NAM_CAP_NHAT
             , round(max(length(A.TOA_DO)),1) AS TOA_DO
             , round(max(length(A.SO_THUA_GOC)),1) AS SO_THUA_GOC
             , round(max(length(A.SU_DUNG)),1) AS SU_DUNG
             , round(max(length(TO_CHAR(A.TG_BAT_DAU, 'DD/MM/YYYY') )),1) AS TG_BAT_DAU
             , round(max(length(TO_CHAR(A.TG_KET_THUC, 'DD/MM/YYYY') )),1) AS TG_KET_THUC
             , round(max(length(A.DAU_GIA)),1) AS DAU_GIA
             , round(max(length(A.DON_VI_TO_CHUC_DAU_GIA )),1) AS DON_VI_TO_CHUC_DAU_GIA
             , round(max(length(A.SO_TIEN)),1) AS SO_TIEN
             , round(max(length(A.DON_GIA_THEO_LAN)),1) AS DON_GIA_THEO_LAN
             , round(max(length(A.LY_DO_NOP_TIEN)),1) AS LY_DO_NOP_TIEN
             , round(max(length(NGAY_CAP_NHAT_NOP_TIEN)),1)  AS NGAY_CAP_NHAT_NOP_TIEN
             , round(max(length(A.THANH_TIEN)),1) AS THANH_TIEN
             , round(max(length(A.DON_GIA)),1) AS DON_GIA
             , round(max(length(A.DIEN_TICH_QD)),1) AS DIEN_TICH_QD
             , round(max(length(A.TIEN_DAU_GIA)),1) AS TIEN_DAU_GIA
             , round(max(length(A.THANH_TIEN_NOP_TIEN)),1) AS THANH_TIEN_NOP_TIEN
             , round(max(length(B.TEN_DVSD)),1) AS TEN_DVSD
             , round(max(length(B.DIA_CHI)),1) AS DIA_CHI
             , round(max(length(C.TEN_TRANG_THAI)),1) AS TEN_TRANG_THAI
             , round(max(length(D.TEN_MUC_DICH)),1) AS TEN_MUC_DICH
             , round(max(length(F.TEN_DAT_CONG)),1) AS TEN_DAT_CONG
             , round(max(length(G.TEN_KQXD)),1) AS TEN_KQXD
             , round(max(length(H.TEN_DKDKK)),1) AS TEN_DKDKK
             , round(max(length(I.TEN_LUY_TIEN)),1) AS TEN_LUY_TIEN
             , round(max(length(J.TEN_NGUOI_QL)),1) AS TEN_NGUOI_QL
             , round(max(length(K.TEN_CAP_QL)),1) AS TEN_CAP_QL
             , round(max(length(L.TEN_HTSDD)),1) AS TEN_HTSDD
             , round(max(length(M.TEN_TRANG_THAI_PLUS)),1) AS TEN_TRANG_THAI_PLUS
             , round(max(length(N.TEN_LOAI_HINH_TRA_TIEN)),1) AS TEN_LOAI_HINH_TRA_TIEN
             , round(max(length(O.TEN_PHUONG_XA)),1) AS TEN_PHUONG_XA
             , round(max(length(P.TEN_QUAN_HUYEN)),1) AS TEN_QUAN_HUYEN
             , round(max(length(Q.TEN_TINH_THANH)),1) AS TEN_TINH_THANH
             , round(max(length(R.MA_DINH_KEM)),1) AS MA_DINH_KEM
             , round(max(length(R.TIEU_DE)),1) AS TIEU_DE
             , round(max(length(R.CHI_TIET)),1) AS CHI_TIET
        FROM QLDC_HO_SO_DAT A
        LEFT JOIN QLDC_DOI_TUONG_SU_DUNG B ON B.MA_DT=A.MA_DTSD
        LEFT JOIN QLDC_TRANG_THAI_HO_SO C ON C.MA_TRANG_THAI=A.MA_TRANG_THAI
        LEFT JOIN QLDC_MUC_DICH_SU_DUNG_DAT D ON D.MA_MUC_DICH=A.MA_MUC_DICH_SU_DUNG
        LEFT JOIN QLDC_BIEN_DONG E ON A.MA_BIEN_DONG= E.MA_BIEN_DONG
        LEFT JOIN QLDC_DAT_CONG F ON A.MA_DAT_CONG =F.MA_DAT_CONG
        LEFT JOIN QLDC_KET_QUA_XET_DUYET G ON A.MA_KQXD = G.MA_KQXD
        LEFT JOIN QLDC_DANG_KY_DA_KE_KHAI H ON A.MA_DKDKK =H.MA_DKDKK
        LEFT JOIN QLDC_LUY_TIEN I ON A.MA_LUY_TIEN = I.MA_LUY_TIEN
        LEFT JOIN QLDC_NGUOI_QL J ON A.MA_NGUOI_QL = J.MA_NGUOI_QL
        LEFT JOIN QLDC_CAP_QL K ON A.MA_CAP_QL = K.MA_CAP_QL
        LEFT JOIN QLDC_HINH_THUC_SU_DUNG_DAT L ON A.MA_HTSD =L.MA_HTSDD
        LEFT JOIN QLDC_TRANG_THAI_PLUS M ON A.TRANG_THAI_PLUS = M.MA_TRANG_THAI_PLUS
        LEFT JOIN QLDC_LOAI_HINH_TRA_TIEN N ON A.LOAI_HINH_TRA_TIEN= N.MA_LOAI_HINH_TRA_TIEN
        LEFT JOIN DM_PHUONG_XA O ON A.MA_XA = O.MA_PHUONG_XA
        LEFT JOIN DM_QUAN_HUYEN P ON A.MA_HUYEN = P.MA_QUAN_HUYEN
        LEFT JOIN DM_TINH_THANH Q ON A.MA_TINH = Q.MA_TINH_THANH
        LEFT JOIN QLDC_DINH_KEM R on A.Ma_Hs = r.Ma_Hs
        WHERE A.MA_HS in (Select MA_HS FROM QLDC_DINH_KEM);
  END;
  
PROCEDURE SELECT_SL_BTTDC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT count(DISTINCT MA_HO_SO2)
     FROM BTTDC_HO_SO2
     WHERE (TTSTNMT IS NOT NULL) OR (QDUBND IS NOT NULL) OR (TTSTC IS NOT NULL);
    END;
PROCEDURE SELECT_COL_BTTDC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT round(avg(length(A.MA_HO_SO2)),1) AS MA_HO_SO
            ,round(avg(length(A.KHHS)),1) AS KHHS
            ,round(avg(length(A.TEN_HO_SO)),1) AS TEN_HO_SO
            ,round(avg(length(A.TEN_CONG_TRINH)),1) AS TEN_CONG_TRINH
            ,round(avg(length(MA_QUOC_TICH)),1) AS QUOC_TICH
            ,round(avg(length(A.NGAY_LAP_HO_SO)),1) AS NGAY_LAP_HO_SO
            ,round(avg(length(A.TRANG_THAI)),1) AS TRANG_THAI
            ,round(avg(length(A.STT)),1) AS STT
            ,round(avg(length(A.TTSTNMT)),1) AS TTSTNMT
            ,round(avg(length(A.QDUBND)),1) AS QDUBND
            ,round(avg(length(A.TTSTC)),1) AS TTST
            ,round(avg(length(A.NGUOI_NOP)),1) AS NGUOI_NOP
            ,round(avg(length(A.TTSTNMTSH)),1) AS TTSTNMTSH
            ,round(avg(length(A.QDUBNDSH)),1) AS QDUBNDSH
            ,round(avg(length(A.TTSTCSH)),1) AS TTSTCSH
            ,round(avg(length(A.TEN_DON_VI)),1) AS TEN_DON_VI
            ,round(avg(length(A.LINH_VUC)),1) AS LINH_VUC
            ,round(avg(length(A.HT_TRA_TIEN)),1) AS HT_TRA_TIEN
            ,round(avg(length(A.LOAI_DAT_GIAO)),1) AS LOAI_DAT_GIAO
            ,round(avg(length(A.LOAI_DAT_THUE)),1) AS LOAI_DAT_THU
            ,round(avg(length(A.KPTD)),1) AS KPTD
            ,round(avg(length(A.TT_KPTD)),1) AS TT_KPTD
            ,round(avg(length(A.GIA_GIAO)),1) AS GIA_GIAO
            ,round(avg(length(A.GIA_THUE)),1) AS GIA_THUE
            ,round(avg(length(B.TEN_HINH_THUC)),1) AS TEN_HINH_THUC
            ,round(avg(length(C.TEN_QUAN_HUYEN)),1) AS TEN_QUAN_HUYEN
      FROM BTTDC_HO_SO2 A
      LEFT JOIN BTTDC_HINH_THUC B ON A.MA_HINH_THUC=B.MA_HINH_THUC
      LEFT JOIN DM_QUAN_HUYEN C ON A.MA_HUYEN = C.MA_QUAN_HUYEN
      WHERE (TTSTNMT IS NOT NULL) OR (QDUBND IS NOT NULL) OR (TTSTC IS NOT NULL);
  END;
  
PROCEDURE SELECT_SL_TTLV(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT count(DISTINCT STT_THANH_TRA)
        FROM TT_THANH_TRA_LINH_VUC
        WHERE FILE_QUYET_DINH IS NOT NULL OR FILE_NOI_DUNG_THANH_TRA IS NOT NULL;
  END;

PROCEDURE SELECT_COL_TTLV(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT round(avg(length(STT_THANH_TRA)),1) AS STT_THANH_TRA
          ,round(avg(length(SO_THANH_TRA)),1) AS SO_THANH_TRA
          ,round(avg(length(LOAI_HINH_THANH_TRA)),1) AS LOAI_HINH_THANH_TRA
          ,round(avg(length(MA_KE_HOACH_TT)),1) AS MA_KE_HOACH_TT
          ,round(avg(length(MA_DOAN_TT)),1) AS MA_DOAN_TT
          ,round(avg(length(NGAY_BD_THANH_TRA)),1) AS NGAY_BD_THANH_TRA
          ,round(avg(length(NGAY_KT_THANH_TRA)),1) AS NGAY_KT_THANH_TRA
          ,round(avg(length(NOI_DUNG_THANH_TRA)),1) AS NOI_DUNG_THANH_TRA
          ,round(avg(length(TEN_TO_CHUC_CA_NHAN)),1) AS TEN_TO_CHUC_CA_NHAN
          ,round(avg(length(LOAI_HINH_DOANH_NGHIEP)),1) AS LOAI_HINH_DOANH_NGHIEP
          ,round(avg(length(DIA_CHI_TO_CHUC_CA_NHAN)),1) AS DIA_CHI_TO_CHUC_CA_NHAN
          ,round(avg(length(MA_LINH_VUC_THANH_TRA)),1) AS MA_LINH_VUC_THANH_TRA
          ,round(avg(length(KET_QUA_THANH_TRA)),1) AS KET_QUA_THANH_TRA
          ,round(avg(length(KIEN_NGHI_XU_LY)),1) AS KIEN_NGHI_XU_LY
          ,round(avg(length(NOI_DUNG_XU_LY)),1) AS NOI_DUNG_XU_LY
          ,round(avg(length(KIEN_NGHI_THU_HOI)),1) AS KIEN_NGHI_THU_HOI
          ,round(avg(length(SO_TIEN_THU_HOI)),1) AS SO_TIEN_THU_HOI
          ,round(avg(length(KIEN_NGHI_KHAC)),1) AS KIEN_NGHI_KHAC
          ,round(avg(length(SO_TIEN_KHAC)),1) AS SO_TIEN_KHAC
          ,round(avg(length(MA_CO_QUAN)),1) AS MA_CO_QUAN
          ,round(avg(length(NOI_DUNG_KIEN_NGHI_XU_LY)),1) AS NOI_DUNG_KIEN_NGHI_XU_LY
          ,round(avg(length(KET_QUA_XU_LY)),1) AS KET_QUA_XU_LY
          ,round(avg(length(DAT_PHAI_THU)),1) AS DAT_PHAI_THU
          ,round(avg(length(SO_TIEN_PHAI_THU)),1) AS SO_TIEN_PHAI_THU
          ,round(avg(length(DAT_DA_THU)),1) AS DAT_DA_THU
          ,round(avg(length(SO_TIEN_DA_THU)),1) AS SO_TIEN_DA_THU
          ,round(avg(length(NOI_DUNG_KIEN_NGHI_THU_HOI)),1) AS NOI_DUNG_KIEN_NGHI_THU_HOI
          ,round(avg(length(NOI_DUNG_KIEN_NGHI_KHAC)),1) AS NOI_DUNG_KIEN_NGHI_KHAC
          ,round(avg(length(SO_TIEN_THAM_NHUNG)),1) AS SO_TIEN_THAM_NHUNG
          ,round(avg(length(TAI_SAN_THAM_NHUNG)),1) AS TAI_SAN_THAM_NHUNG
          ,round(avg(length(TS_QUY_DOI_THANH_TIEN)),1) AS TS_QUY_DOI_THANH_TIEN
          ,round(avg(length(DAT_THAM_NHUNG)),1) AS DAT_THAM_NHUNG
          ,round(avg(length(SO_NGUOI_LIEN_QUAN)),1) AS SO_NGUOI_LIEN_QUAN
          ,round(avg(length(FILE_QUYET_DINH)),1) AS FILE_QUYET_DINH
          ,round(avg(length(FILE_NOI_DUNG_THANH_TRA)),1) AS FILE_NOI_DUNG_THANH_TRA
          ,round(avg(length(NGAY_KY_QUYET_DINH)),1) AS NGAY_KY_QUYET_DINH
          ,round(avg(length(GHI_CHU)),1) AS GHI_CHU
    FROM TT_THANH_TRA_LINH_VUC
    WHERE FILE_QUYET_DINH IS NOT NULL OR FILE_NOI_DUNG_THANH_TRA IS NOT NULL;
  END;

PROCEDURE SELECT_COL_NAME(P_CUR IN OUT MYCUR, TABLENAME NVARCHAR2) AS
 BEGIN
    OPEN P_CUR FOR
    SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = TableName;
 END;

PROCEDURE SELECT_AVGLENTH(P_CUR IN OUT MYCUR,COLNAME NVARCHAR2, TABLENAME NVARCHAR2, CONDITION NVARCHAR2) AS
  qry LONG;
  BEGIN
    qry :=  'select round(avg(length(' || COLNAME || '))) from ' || TABLENAME;
    if TRIM (CONDITION) IS not NULL then
       qry := qry || ' where ' || CONDITION;
    end if;
    OPEN P_CUR FOR qry;
  END;
 


end QLDC_TK;
/

prompt
prompt Creating package body TNMT_BTTDC
prompt ================================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.TNMT_BTTDC IS
  ------------------------------------DM_VB_TIEP_NHAN-------------------------------------------

PROCEDURE SELECT_VB_TIEP_NHAN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_GIA_TRI = 0 THEN
               'Ðóng'
               ELSE
                 'Mở'
             END AS TEN_TRANG_THAI
        FROM BTTDC_DM_VB_TIEP_NHAN A
       ORDER BY A.STT_VB_TIEP_NHAN;
  END;
  
  ------------------------------------------------------------------------
  
  PROCEDURE SELECT_VB_TIEP_NHAN_LV(P_CUR IN OUT MYCUR,
                                   P_MA_LINH_VUC   NVARCHAR2  ) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_VB_TIEP_NHAN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_DM_VB_TIEP_NHAN A WHERE A.MA_HINH_THUC LIKE P_MA_LINH_VUC AND A.TT_GIA_TRI=1
       ORDER BY A.STT_VB_TIEP_NHAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_VB_TIEP_NHAN_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_VB_TIEP_NHAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_VB_TIEP_NHAN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_DM_VB_TIEP_NHAN A
       WHERE A.MA_VB_TIEP_NHAN LIKE P_MA_VB_TIEP_NHAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_VB_TIEP_NHAN(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_VB_TIEP_NHAN     NVARCHAR2,
                           P_TEN_VB_TIEP_NHAN    NVARCHAR2,
                           P_STT_VB_TIEP_NHAN    INT,
                           P_MA_HINH_THUC        NVARCHAR2,
                           P_TT_VB_TIEP_NHAN     INT,
                           P_TT_GIA_TRI INT) AS
    V_TON_TAI_MA  INT;
   
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_VB_TIEP_NHAN)
        INTO V_TON_TAI_MA
        FROM BTTDC_DM_VB_TIEP_NHAN
       WHERE UPPER(MA_VB_TIEP_NHAN) LIKE UPPER(P_MA_VB_TIEP_NHAN);
      
      IF V_TON_TAI_MA > 0 OR P_MA_VB_TIEP_NHAN = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
     
      ELSE
        INSERT INTO BTTDC_DM_VB_TIEP_NHAN
          (MA_VB_TIEP_NHAN,
           TEN_VB_TIEP_NHAN,
           STT_VB_TIEP_NHAN,
           MA_HINH_THUC,
           TT_VB_TIEP_NHAN,
           TT_GIA_TRI)
        VALUES
          (P_MA_VB_TIEP_NHAN,
           P_TEN_VB_TIEP_NHAN,
           P_STT_VB_TIEP_NHAN,
           P_MA_HINH_THUC,
           P_TT_VB_TIEP_NHAN,
           P_TT_GIA_TRI);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      
    
        UPDATE BTTDC_DM_VB_TIEP_NHAN
           SET TEN_VB_TIEP_NHAN    = P_TEN_VB_TIEP_NHAN,
               STT_VB_TIEP_NHAN    = P_STT_VB_TIEP_NHAN,
               TT_VB_TIEP_NHAN     = P_TT_VB_TIEP_NHAN,
               MA_HINH_THUC        = P_MA_HINH_THUC,
               TT_GIA_TRI          =P_TT_GIA_TRI
         WHERE MA_VB_TIEP_NHAN LIKE P_MA_VB_TIEP_NHAN;
        P_VAL := 'THANH_CONG';
     
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_VB_TIEP_NHAN(P_VAL   OUT NVARCHAR2, P_MA_VB_TIEP_NHAN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM BTTDC_DM_VB_TIEP_NHAN WHERE MA_VB_TIEP_NHAN LIKE P_MA_VB_TIEP_NHAN;
    P_VAL := 'THANH_CONG';
   END;    

------------------------------------------PHIEU_TIEP_NHAN_HO_SO------------------------------------------
 PROCEDURE SELECT_PHIEU_TIEP_NHAN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_PHIEU_TIEP_NHAN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_PHIEU_TIEP_NHAN A
       ORDER BY A.STT_PHIEU_TIEP_NHAN;
  END;
  -----------------------------------------------------------------------------------------
   PROCEDURE SELECT_PHIEU_TIEP_NHAN_LV(P_CUR IN OUT MYCUR,P_MA_HINH_THUC NVARCHAR2, P_TT_PTN INT) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.NGAY_GIAO,'DD/MM/YYYY') as NGAY_GIAO,
             CASE
               WHEN A.TT_PHIEU_TIEP_NHAN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_PHIEU_TIEP_NHAN A WHERE A.MA_HINH_THUC LIKE P_MA_HINH_THUC AND A.TT_PHIEU_TIEP_NHAN =P_TT_PTN
       ORDER BY A.STT_PHIEU_TIEP_NHAN;
  END;
  
  -------------------------------------------------------------------------------------------------
  
   PROCEDURE SELECT_PHIEU_TIEP_NHAN_WAIT(P_CUR IN OUT MYCUR,P_TT_PHIEU_TIEP_NHAN INT) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_PHIEU_TIEP_NHAN,A.TEN_DON_VI,A.TEN_CONG_TRINH,A.NGUOI_GIAO,
      A.NGUOI_GIAO_BO_SUNG,A.CB_NHAN,A.CB_NHAN_BO_SUNG,TO_CHAR(A.NGAY_GIAO,'DD/MM/YYYY') as NGAY_GIAO,TO_CHAR(A.NGAY_HEN_BO_SUNG,'DD/MM/YYYY') as NGAY_HEN_BO_SUNG,
      A.GHI_CHU,A.STT_PHIEU_TIEP_NHAN,A.MA_HINH_THUC,A.TT_PHIEU_TIEP_NHAN,
             CASE
               WHEN A.TT_PHIEU_TIEP_NHAN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_PHIEU_TIEP_NHAN A WHERE A.TT_PHIEU_TIEP_NHAN=P_TT_PHIEU_TIEP_NHAN
       ORDER BY A.STT_PHIEU_TIEP_NHAN;
  END;
  -------------------------------------------------------------------------------------------------
  PROCEDURE SELECT_PHIEU_TIEP_NHAN_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_PHIEU_TIEP_NHAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_PHIEU_TIEP_NHAN,A.TEN_DON_VI,A.TEN_CONG_TRINH,A.NGUOI_GIAO,
      A.NGUOI_GIAO_BO_SUNG,TO_CHAR(A.NGAY_GIAO,'DD/MM/YYYY') as NGAY_GIAO,TO_CHAR(A.NGAY_HEN_BO_SUNG,'DD/MM/YYYY') as NGAY_HEN_BO_SUNG,
      A.GHI_CHU,A.STT_PHIEU_TIEP_NHAN,A.MA_HINH_THUC,A.TT_PHIEU_TIEP_NHAN,A.CB_NHAN,A.CB_NHAN_BO_SUNG,A.MA_QUAN_HUYEN, (SELECT  B.TEN_CAN_BO AS CB_NHAN
FROM BTTDC_PHIEU_TIEP_NHAN A,CB_CAN_BO B
       WHERE  
       A.CB_NHAN=b.ma_can_bo and A.MA_PHIEU_TIEP_NHAN LIKE P_MA_PHIEU_TIEP_NHAN ) as TEN_CB_NHAN,
       (SELECT  B.TEN_CAN_BO AS CB_NHAN
FROM BTTDC_PHIEU_TIEP_NHAN A,CB_CAN_BO B
       WHERE  
       A.Cb_Nhan_Bo_Sung=b.ma_can_bo and A.MA_PHIEU_TIEP_NHAN LIKE P_MA_PHIEU_TIEP_NHAN ) as TEN_CB_NHAN_BO_SUNG
FROM BTTDC_PHIEU_TIEP_NHAN A,CB_CAN_BO B
       WHERE  
       A.CB_NHAN=b.ma_can_bo
       and A.MA_PHIEU_TIEP_NHAN LIKE P_MA_PHIEU_TIEP_NHAN  ;
  END;
  -----------------------------------------------------------------------------
  PROCEDURE SELECT_PHIEU_TIEP_NHAN_EXCEL(P_CUR        IN OUT MYCUR,
                                  P_TT_PHIEU_TIEP_NHAN INT ) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT  A.MA_PHIEU_TIEP_NHAN,A.TEN_DON_VI,A.TEN_CONG_TRINH,A.NGUOI_GIAO,
      A.NGUOI_GIAO_BO_SUNG,TO_CHAR(A.NGAY_GIAO,'DD/MM/YYYY') as NGAY_GIAO,TO_CHAR(A.NGAY_HEN_BO_SUNG,'DD/MM/YYYY') as NGAY_HEN_BO_SUNG,
      A.GHI_CHU,A.STT_PHIEU_TIEP_NHAN,A.MA_HINH_THUC,A.TT_PHIEU_TIEP_NHAN,A.CB_NHAN,A.CB_NHAN_BO_SUNG,A.MA_QUAN_HUYEN,(
      SELECT B.TEN_CAN_BO  FROM CB_CAN_BO B WHERE B.MA_CAN_BO LIKE A.CB_NHAN)AS TEN_CB_NHAN,
      ( SELECT C.TEN_CAN_BO  FROM CB_CAN_BO C WHERE C.MA_CAN_BO LIKE A.CB_NHAN_BO_SUNG)AS TEN_CB_NHAN_BO_SUNG,
      ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_QUAN_HUYEN)AS TEN_QUAN_HUYEN   
       FROM BTTDC_PHIEU_TIEP_NHAN A WHERE A.TT_PHIEU_TIEP_NHAN LIKE P_TT_PHIEU_TIEP_NHAN;
  END;
  
  -------------------------------------------------------------------------------
    PROCEDURE SELECT_PTN_EXCEL_DATE(P_CUR        IN OUT MYCUR,
                                  P_TT_PHIEU_TIEP_NHAN INT,P_FROM_DATE NVARCHAR2,P_TO_DATE NVARCHAR2 ) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT  A.MA_PHIEU_TIEP_NHAN,A.TEN_DON_VI,A.TEN_CONG_TRINH,A.NGUOI_GIAO,
      A.NGUOI_GIAO_BO_SUNG,TO_CHAR(A.NGAY_GIAO,'DD/MM/YYYY') as NGAY_GIAO,TO_CHAR(A.NGAY_HEN_BO_SUNG,'DD/MM/YYYY') as NGAY_HEN_BO_SUNG,
      A.GHI_CHU,A.STT_PHIEU_TIEP_NHAN,A.MA_HINH_THUC,A.TT_PHIEU_TIEP_NHAN,A.CB_NHAN,A.CB_NHAN_BO_SUNG,A.MA_QUAN_HUYEN,(
      SELECT B.TEN_CAN_BO  FROM CB_CAN_BO B WHERE B.MA_CAN_BO LIKE A.CB_NHAN)AS TEN_CB_NHAN,
      ( SELECT C.TEN_CAN_BO  FROM CB_CAN_BO C WHERE C.MA_CAN_BO LIKE A.CB_NHAN_BO_SUNG)AS TEN_CB_NHAN_BO_SUNG,
      ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_QUAN_HUYEN)AS TEN_QUAN_HUYEN   
       FROM BTTDC_PHIEU_TIEP_NHAN A WHERE A.TT_PHIEU_TIEP_NHAN LIKE P_TT_PHIEU_TIEP_NHAN AND A.NGAY_GIAO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_GIAO<=to_date( P_TO_DATE,'ddmmyyyy');
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_PHIEU_TIEP_NHAN(P_VAL            OUT NVARCHAR2,
                           P_MA                     OUT NVARCHAR2,                             
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_PHIEU_TIEP_NHAN    NVARCHAR2,
                           P_TEN_DON_VI            NVARCHAR2,
                           P_TEN_CONG_TRINH    NVARCHAR2,
                           P_NGUOI_GIAO            NVARCHAR2,
                           P_NGUOI_GIAO_BO_SUNG NVARCHAR2,
                           P_CB_NHAN            INT,
                           P_CB_NHAN_BO_SUNG    INT,
                           P_NGAY_GIAO          NVARCHAR2,
                           P_NGAY_HEN_BO_SUNG   NVARCHAR2,
                           P_GHI_CHU            NVARCHAR2,
                           P_STT_PHIEU_TIEP_NHAN   INT,
                           P_MA_HINH_THUC          NVARCHAR2,
                           P_MA_QUAN_HUYEN         NVARCHAR2,
                           P_TT_PHIEU_TIEP_NHAN    INT) AS
    V_MA_PHIEU  INT;
   
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
       V_MA_PHIEU :=MA_PHIEU_TIEP_NHAN_SEQ.NEXTVAL;
      
        INSERT INTO BTTDC_PHIEU_TIEP_NHAN
          (MA_PHIEU_TIEP_NHAN,
          TEN_DON_VI,
          TEN_CONG_TRINH,
          NGUOI_GIAO,
          NGUOI_GIAO_BO_SUNG,
          CB_NHAN,
          CB_NHAN_BO_SUNG,
          NGAY_GIAO,
          NGAY_HEN_BO_SUNG,
          GHI_CHU,
          STT_PHIEU_TIEP_NHAN,
          MA_HINH_THUC,
          MA_QUAN_HUYEN,
          TT_PHIEU_TIEP_NHAN)
        VALUES
          (V_MA_PHIEU,
          P_TEN_DON_VI,
           P_TEN_CONG_TRINH,
           P_NGUOI_GIAO,
           P_NGUOI_GIAO_BO_SUNG,
           P_CB_NHAN,
           P_CB_NHAN_BO_SUNG,
           to_date(P_NGAY_GIAO,'DDMMYYYY'),
           to_date(P_NGAY_HEN_BO_SUNG,'DDMMYYYY'),
           P_GHI_CHU,
           P_STT_PHIEU_TIEP_NHAN,
           P_MA_HINH_THUC,
           P_MA_QUAN_HUYEN,
           P_TT_PHIEU_TIEP_NHAN);
        P_VAL := 'THANH_CONG';
        P_MA :=V_MA_PHIEU;
    
    ELSE
    
        UPDATE BTTDC_PHIEU_TIEP_NHAN
           SET TEN_DON_VI         =P_TEN_DON_VI,
               TEN_CONG_TRINH   = P_TEN_CONG_TRINH,
               NGUOI_GIAO       = P_NGUOI_GIAO,
               NGUOI_GIAO_BO_SUNG = P_NGUOI_GIAO_BO_SUNG,
               CB_NHAN            =P_CB_NHAN,
               CB_NHAN_BO_SUNG    = P_CB_NHAN_BO_SUNG,
               NGAY_GIAO          = to_date(P_NGAY_GIAO,'DDMMYYYY'),
               NGAY_HEN_BO_SUNG   = to_date(P_NGAY_HEN_BO_SUNG,'DDMMYYYY'),
               GHI_CHU            =P_GHI_CHU,
               STT_PHIEU_TIEP_NHAN    = P_STT_PHIEU_TIEP_NHAN,
               MA_HINH_THUC           =P_MA_HINH_THUC,
               MA_QUAN_HUYEN          =P_MA_QUAN_HUYEN,
               TT_PHIEU_TIEP_NHAN     = P_TT_PHIEU_TIEP_NHAN
         WHERE MA_PHIEU_TIEP_NHAN LIKE P_MA_PHIEU_TIEP_NHAN;
        P_VAL := 'THANH_CONG';
         P_MA :=P_MA_PHIEU_TIEP_NHAN;
    
    END IF;
    END;
  ----------------------------update_tt_phieu_tiep_nhan---------------------------------------------------
  PROCEDURE UPDATE_TT_PHIEU_TIEP_NHAN(P_MA_PHIEU_TIEP_NHAN    NVARCHAR2) AS
                           
          BEGIN
            UPDATE BTTDC_PHIEU_TIEP_NHAN
           SET 
               TT_PHIEU_TIEP_NHAN     = 1
         WHERE MA_PHIEU_TIEP_NHAN LIKE P_MA_PHIEU_TIEP_NHAN;
          END;                   
    -------------------------------------------------------------------------------

  PROCEDURE DELETE_PHIEU_TIEP_NHAN(P_VAL   OUT NVARCHAR2, P_MA_PHIEU_TIEP_NHAN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM BTTDC_PHIEU_TIEP_NHAN WHERE MA_PHIEU_TIEP_NHAN LIKE P_MA_PHIEU_TIEP_NHAN;
    P_VAL := 'THANH_CONG';
   END;    
    ----------------------------------------BTTDC_VB_DA_TIEP_NHAN_CUA_MOT_HO_SO---------------------------------------
    PROCEDURE SELECT_VB_DA_TIEP_NHAN(P_CUR IN OUT MYCUR, P_MA_HO_SO NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_VB_TIEP_NHAN A WHERE MA_HO_SO LIKE P_MA_HO_SO
       ORDER BY A.STT;
  END;
  -----------------------------------------------------------------------------
  PROCEDURE SELECT_VB_DA_TIEP_NHAN_PTN(P_CUR IN OUT MYCUR, P_MA_PHIEU_TIEP_NHAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_VB_TIEP_NHAN A WHERE A.MA_PHIEU_TIEP_NHAN LIKE P_MA_PHIEU_TIEP_NHAN
       ORDER BY A.STT;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_VB_DA_TIEP_NHAN_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_VB_DA_TIEP_NHAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_VB_TIEP_NHAN A
       WHERE A.MA_VB_TIEP_NHAN LIKE P_MA_VB_DA_TIEP_NHAN;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_VB_DA_TIEP_NHAN(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_VB_DA_TIEP_NHAN     NVARCHAR2,
                           P_TEN_VB_TIEP_NHAN    NVARCHAR2,
                           P_STT_VB_TIEP_NHAN    INT,
                           P_GIA_TRI             INT,
                           P_FILEPATH            NVARCHAR2,
                           P_MA_HO_SO  NVARCHAR2,
                           P_TT_VB_TIEP_NHAN INT,
                           P_MA_VB_TIEP_NHAN_DM  NVARCHAR2) AS
    V_MA_VB  INT;
   BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
    
       V_MA_VB := MA_VB_TIEP_NHAN_SEQ.NEXTVAL;
        INSERT INTO BTTDC_VB_TIEP_NHAN
          (MA_VB_TIEP_NHAN,
           TEN_VB_TIEP_NHAN,
           STT,
           GIA_TRI,
           FILE_PATH,
           MA_HO_SO,
           TRANG_THAI,
           MA_VB_TIEP_NHAN_DM)
        VALUES
          (V_MA_VB,
           P_TEN_VB_TIEP_NHAN,
           P_STT_VB_TIEP_NHAN,
           P_GIA_TRI,
           P_FILEPATH,
           P_MA_HO_SO,
           P_TT_VB_TIEP_NHAN,
           P_MA_VB_TIEP_NHAN_DM
           );
        P_VAL := 'THANH_CONG';
      
    ELSE
  
        UPDATE BTTDC_VB_TIEP_NHAN
           SET TEN_VB_TIEP_NHAN    = P_TEN_VB_TIEP_NHAN,
               GIA_TRI             =P_GIA_TRI,
               FILE_PATH           = P_FILEPATH,
               MA_HO_SO  = P_MA_HO_SO,
               STT                 = P_STT_VB_TIEP_NHAN,
               TRANG_THAI          = P_TT_VB_TIEP_NHAN,
               MA_VB_TIEP_NHAN_DM=P_MA_VB_TIEP_NHAN_DM
         WHERE MA_VB_TIEP_NHAN LIKE P_MA_VB_DA_TIEP_NHAN;
        P_VAL := 'THANH_CONG';
  
    END IF;
  
  END;
  
  -----------------------------------------------------------------------------------
  PROCEDURE UPDATE_VB_DA_TIEP_NHAN1(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_VB_DA_TIEP_NHAN     NVARCHAR2,
                           P_TEN_VB_TIEP_NHAN    NVARCHAR2,
                           P_STT_VB_TIEP_NHAN    INT,
                           P_GIA_TRI             INT,
                           P_FILEPATH            NVARCHAR2,
                           P_MA_PHIEU_TIEP_NHAN  NVARCHAR2,
                           P_TT_VB_TIEP_NHAN INT,
                           P_MA_VB_TIEP_NHAN_DM  NVARCHAR2) AS
    V_MA_VB  INT;
   BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
    
       V_MA_VB := MA_VB_TIEP_NHAN_SEQ.NEXTVAL;
        INSERT INTO BTTDC_VB_TIEP_NHAN
          (MA_VB_TIEP_NHAN,
           TEN_VB_TIEP_NHAN,
           STT,
           GIA_TRI,
           FILE_PATH,
           MA_PHIEU_TIEP_NHAN,
           TRANG_THAI,
           MA_VB_TIEP_NHAN_DM)
        VALUES
          (V_MA_VB,
           P_TEN_VB_TIEP_NHAN,
           P_STT_VB_TIEP_NHAN,
           P_GIA_TRI,
           P_FILEPATH,
           P_MA_PHIEU_TIEP_NHAN,
           P_TT_VB_TIEP_NHAN,
           P_MA_VB_TIEP_NHAN_DM
           );
        P_VAL := 'THANH_CONG';
      
    ELSE
  
        UPDATE BTTDC_VB_TIEP_NHAN
           SET TEN_VB_TIEP_NHAN    = P_TEN_VB_TIEP_NHAN,
               GIA_TRI             =P_GIA_TRI,
               FILE_PATH           = P_FILEPATH,
               MA_PHIEU_TIEP_NHAN  = P_MA_PHIEU_TIEP_NHAN,
               STT                 = P_STT_VB_TIEP_NHAN,
               TRANG_THAI          = P_TT_VB_TIEP_NHAN,
               MA_VB_TIEP_NHAN_DM=P_MA_VB_TIEP_NHAN_DM
         WHERE MA_VB_TIEP_NHAN LIKE P_MA_VB_DA_TIEP_NHAN;
        P_VAL := 'THANH_CONG';
  
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  PROCEDURE DELETE_VB_DA_TIEP_NHAN(P_VAL   OUT NVARCHAR2, P_MA_VB_DA_TIEP_NHAN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM BTTDC_VB_TIEP_NHAN WHERE MA_VB_TIEP_NHAN LIKE P_MA_VB_DA_TIEP_NHAN;
    P_VAL := 'THANH_CONG';
   END;
    
    ------------------------------------------------HINH_THUC_HO_SO--------------------
    PROCEDURE SELECT_HINH_THUC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_HINH_THUC A
       ORDER BY A.STT_HINH_THUC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_HINH_THUC_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_HINH_THUC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_HINH_THUC A
       WHERE A.MA_HINH_THUC LIKE P_MA_HINH_THUC;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_HINH_THUC(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_HINH_THUC     NVARCHAR2,
                           P_TEN_HINH_THUC    NVARCHAR2,
                           P_STT_HINH_THUC    INT,
                           P_TRANG_THAI     INT) AS
    V_TON_TAI_MA  INT;
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_HINH_THUC)
        INTO V_TON_TAI_MA
        FROM BTTDC_HINH_THUC
       WHERE UPPER(MA_HINH_THUC) LIKE UPPER(P_MA_HINH_THUC);
      SELECT COUNT(MA_HINH_THUC)
        INTO V_TON_TAI_TEN
        FROM BTTDC_HINH_THUC
       WHERE UPPER(TEN_HINH_THUC) LIKE UPPER(P_TEN_HINH_THUC);
      IF V_TON_TAI_MA > 0 OR P_MA_HINH_THUC = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSIF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO BTTDC_HINH_THUC
          (MA_HINH_THUC,
           TEN_HINH_THUC,
           STT_HINH_THUC,
           TRANG_THAI)
        VALUES
          (P_MA_HINH_THUC,
           P_TEN_HINH_THUC,
           P_STT_HINH_THUC,
           P_TRANG_THAI);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_HINH_THUC)
        INTO V_TON_TAI_TEN
        FROM BTTDC_HINH_THUC
       WHERE UPPER(TEN_HINH_THUC) LIKE UPPER(P_TEN_HINH_THUC)
         AND MA_HINH_THUC NOT LIKE P_MA_HINH_THUC;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE BTTDC_HINH_THUC
           SET TEN_HINH_THUC    = P_TEN_HINH_THUC,
               STT_HINH_THUC    = P_STT_HINH_THUC,
 		   TRANG_THAI		  = P_TRANG_THAI
         WHERE MA_HINH_THUC LIKE P_MA_HINH_THUC;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
  ---------------------------------------------------------------------------
  PROCEDURE DELETE_HINH_THUC(P_VAL   OUT NVARCHAR2, P_MA_HINH_THUC NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM BTTDC_HINH_THUC WHERE MA_HINH_THUC LIKE P_MA_HINH_THUC;
    P_VAL := 'THANH_CONG';
   END;    
  -------------------------------LAN_XU_LY_HO_SO--------------------------------------------

  -------------------------------------------------------------------------------
  
  -------------------------------------------------------------------------------
  
  ---------------------------------------------------------------------------
  
  -------------------------------VB_XU_LY--------------------------------------------

  -------------------------------------SELECT_HO_SO--------------------------------------
  

  -------------------------------------------------SELECT_HS_EXCEL----------------------------
    PROCEDURE SELECT_HO_SO_EXCEL(P_CUR IN OUT MYCUR,P_MA_LINH_VUC NVARCHAR2,P_FROM_DATE NVARCHAR2, P_TO_DATE NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC    ORDER BY A.NGAY_LAP_HO_SO;
      
  END;
  ------------------------------------------select ho so csbt-----------------------------------
  PROCEDURE SELECT_HO_SO_EXCEL_CSBT(P_CUR IN OUT MYCUR,P_MA_LINH_VUC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC;
      
  END;
  -----------------------------------------------SELECT HOSO GIA KHOI DIEM-------------------------
    PROCEDURE SELECT_HO_SO_EXCEL_HSGKD(P_CUR IN OUT MYCUR,P_MA_LINH_VUC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC;
      
  END;
  --------------------------------------------------selec_hs_excel_no--------------------------
  
      PROCEDURE SELECT_HO_SO_KP_EXCEL(P_CUR IN OUT MYCUR,P_MA_LINH_VUC NVARCHAR2,P_FROM_DATE NVARCHAR2, P_TO_DATE NVARCHAR2,P_TTKPTD NVARCHAR2) AS
  BEGIN
    IF P_TTKPTD ='1' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC    ORDER BY A.NGAY_LAP_HO_SO;
      
    END IF;
     IF P_TTKPTD ='2' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.TT_KPTD =1
   ORDER BY A.NGAY_LAP_HO_SO;
      
     END IF;
     IF P_TTKPTD ='3' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.TT_KPTD=0  ORDER BY A.NGAY_LAP_HO_SO;
      
   
 END IF;
 END;
 ---------------------------------------------------------------------------------------
 PROCEDURE SELECT_HO_SO_KP_EXCEL_GCSBT(P_CUR IN OUT MYCUR,P_MA_LINH_VUC NVARCHAR2,P_TTKPTD NVARCHAR2) AS
  BEGIN
    IF P_TTKPTD ='1' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC;
      
    END IF;
     IF P_TTKPTD ='2' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.TT_KPTD =1;
      
     END IF;
     IF P_TTKPTD ='3' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.TT_KPTD=0 ;
      
   
 END IF;
 END;
  ----------------------------------------------------------------------------------------
  
      PROCEDURE SELECT_HO_SO_LV_EXCEL(P_CUR IN OUT MYCUR,P_MA_LINH_VUC NVARCHAR2,P_FROM_DATE NVARCHAR2, P_TO_DATE NVARCHAR2,P_LINHVUC NVARCHAR2) AS
  BEGIN
    IF P_LINHVUC ='1' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC    ORDER BY A.NGAY_LAP_HO_SO;
      
    END IF;
     IF P_LINHVUC ='2' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.LINH_VUC LIKE 'Giao'   ORDER BY A.NGAY_LAP_HO_SO;
      
     END IF;
     IF P_LINHVUC ='3' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.LINH_VUC LIKE 'Thuê' ORDER BY A.NGAY_LAP_HO_SO;
      
   
 END IF;
  IF P_LINHVUC ='5' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.LINH_VUC LIKE 'Giao và Thuê' ORDER BY A.NGAY_LAP_HO_SO;
      
   
 END IF;
    IF P_LINHVUC ='4' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.NGAY_LAP_HO_SO >= to_date( P_FROM_DATE,'ddmmyyyy') AND A.NGAY_LAP_HO_SO <=to_date( P_TO_DATE,'ddmmyyyy')    AND A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.LINH_VUC LIKE 'Chuyển MĐSD' ORDER BY A.NGAY_LAP_HO_SO;
      
   
 END IF;
 
 END;
    -------------------------------------SELECT_HO_SO_GIAO THUE CMDSD--------------------------------------
PROCEDURE SELECT_HO_SO_LV_EXCEL_GTCMDSD(P_CUR IN OUT MYCUR,P_MA_LINH_VUC NVARCHAR2,P_LINHVUC NVARCHAR2) AS
  BEGIN
    IF P_LINHVUC ='1' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC;
      
    END IF;
     IF P_LINHVUC ='2' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.LINH_VUC LIKE 'Giao';
      
     END IF;
     IF P_LINHVUC ='3' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.LINH_VUC LIKE 'Thuê';
      
   
 END IF;
  IF P_LINHVUC ='5' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE  A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.LINH_VUC LIKE 'Giao và Thuê';
      
   
 END IF;
    IF P_LINHVUC ='4' THEN
    OPEN P_CUR FOR
      SELECT A.MA_HO_SO2,A.KHHS,A.TEN_HO_SO,A.TEN_CONG_TRINH,A.MA_HUYEN,A.MA_QUOC_TICH, TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') AS NGAY_LAP_HO_SO,A.TRANG_THAI,A.STT,A.TTSTNMT,A.QDUBND,A.TTSTC,A.NGUOI_NOP,A.MA_HINH_THUC,A.TTSTNMTSH,A.QDUBNDSH,A.TTSTCSH,
           ( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,A.KPTD,A.TEN_DON_VI,A.LINH_VUC,A.HT_TRA_TIEN,A.LOAI_DAT_GIAO,A.LOAI_DAT_THUE,A.GIA_GIAO,A.GIA_THUE,
                 ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE P_MA_LINH_VUC)AS TEN_LINH_VUC
           
        FROM BTTDC_HO_SO2 A WHERE A.MA_HINH_THUC LIKE P_MA_LINH_VUC  AND A.LINH_VUC LIKE 'Chuyển MĐSD';
      
   
 END IF;
 
 END;
    -------------------------------------UPDATE_HO_SO--------------------------------------

 -------------------------------------UPDATE_HO_SO--------------------------------------

   --------------------------------------------select hs excel test-------------------
  
 ---2--------------------------------------------------------------------------------
   
     
PROCEDURE SELECT_DM_THUOCTINH_HS(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI  = 0 THEN
               'Ðóng'
               ELSE
                 'Mở'
             END AS TEN_TRANG_THAI
        FROM  BTTDC_DM_THUOCTINH_HS A
       ORDER BY A.STT_THUOC_TINH;
  END;
  
  ------------------------------------------------------------------------
  
  PROCEDURE SELECT_DM_THUOCTINH_HS_LV(P_CUR IN OUT MYCUR,
                                   P_MA_LINH_VUC   NVARCHAR2  ) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI  = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_DM_THUOCTINH_HS A WHERE A.MA_LINH_VUC LIKE P_MA_LINH_VUC AND A.TRANG_THAI =1
       ORDER BY A.STT_THUOC_TINH;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_DM_THUOCTINH_HS_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_THUOC_TINH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_DM_THUOCTINH_HS A
       WHERE A.MA_THUOC_TINH LIKE P_MA_THUOC_TINH ORDER BY A.STT_THUOC_TINH;
  END;
 
   -----------------------------------------------------------------------
     
   PROCEDURE UPDATE_DM_THUOC_TINH_HS(P_UPDATE_TYPE    NVARCHAR2,
                           P_VAL   OUT NVARCHAR2,
                           P_MA_THUOC_TINH NVARCHAR2,
                           P_TEN_THUOC_TINH NVARCHAR2,
                           P_GIA_TRI_THUOC_TINH NVARCHAR2,
                           P_TRANG_THAI INT ,
                           P_STT_THUOC_TINH INT ,
                           P_MA_LINH_VUC NVARCHAR2) AS
    V_TON_TAI_MA  INT;
   
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_THUOC_TINH)
        INTO V_TON_TAI_MA
        FROM BTTDC_DM_THUOCTINH_HS
       WHERE UPPER(MA_THUOC_TINH) LIKE UPPER(P_MA_THUOC_TINH);
      
      IF V_TON_TAI_MA > 0 THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      
      ELSE
        INSERT INTO BTTDC_DM_THUOCTINH_HS
          (MA_THUOC_TINH ,
          TEN_THUOC_TINH ,
          GIA_TRI_THUOC_TINH ,
          TRANG_THAI ,
          STT_THUOC_TINH ,
          MA_LINH_VUC )
        VALUES
          (P_MA_THUOC_TINH ,
           P_TEN_THUOC_TINH ,
           P_GIA_TRI_THUOC_TINH ,
           P_TRANG_THAI  ,
           P_STT_THUOC_TINH  ,
           P_MA_LINH_VUC);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      
        UPDATE BTTDC_DM_THUOCTINH_HS
           SET TEN_THUOC_TINH    = P_TEN_THUOC_TINH,
               GIA_TRI_THUOC_TINH    = P_GIA_TRI_THUOC_TINH,
               TRANG_THAI     = P_TRANG_THAI,
               STT_THUOC_TINH        = P_STT_THUOC_TINH,
               MA_LINH_VUC          =P_MA_LINH_VUC
         WHERE MA_THUOC_TINH LIKE P_MA_THUOC_TINH;
        P_VAL := 'THANH_CONG';
      END IF;
    
  
  END;
   
   ---------------------------------GT_THUOC_TINH_HS----------------------------
   
PROCEDURE SELECT_GT_THUOCTINH_HS(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI  = 0 THEN
               'Ðóng'
               ELSE
                 'Mở'
             END AS TEN_TRANG_THAI
        FROM  BTTDC_GT_THUOCTINH_HS A
       ORDER BY A.STT_THUOC_TINH;
  END;
  

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_GT_THUOCTINH_1HS_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_HO_SO NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_GT_THUOCTINH_HS A
       WHERE A.MA_HO_SO LIKE P_MA_HO_SO;
  END;
 
   -----------------------------------------------------------------------
     
   PROCEDURE UPDATE_GT_THUOC_TINH_1HS( P_VAL   OUT NVARCHAR2,
     P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_THUOC_TINH NVARCHAR2,
                           P_TEN_THUOC_TINH NVARCHAR2 ,
                           P_GIA_TRI_THUOC_TINH NVARCHAR2,
                           P_TRANG_THAI INT ,
                           P_STT_THUOC_TINH INT,
                           P_MA_LINH_VUC NVARCHAR2,
                           P_MA_HO_SO NVARCHAR2,
                           P_MA_THUOC_TINH_DM NVARCHAR2) AS
  	
   
  BEGIN
    P_VAL := 'CO_LOI';  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
       
        INSERT INTO BTTDC_GT_THUOCTINH_HS
          (MA_THUOC_TINH ,
           TEN_THUOC_TINH  ,
           GIA_TRI_THUOC_TINH,
           TRANG_THAI  ,
            STT_THUOC_TINH ,
            MA_LINH_VUC ,
            MA_HO_SO,
            MA_THUOC_TINH_DM )
        VALUES
          (MA_GT_THUOC_TINH_SEQ.NEXTVAL ,
           P_TEN_THUOC_TINH ,
           P_GIA_TRI_THUOC_TINH ,
           P_TRANG_THAI  ,
           P_STT_THUOC_TINH  ,
           P_MA_LINH_VUC,
           P_MA_HO_SO,
           P_MA_THUOC_TINH_DM);
        P_VAL := 'THANH_CONG';
     
    ELSE
      
        UPDATE BTTDC_GT_THUOCTINH_HS
           SET TEN_THUOC_TINH    = P_TEN_THUOC_TINH,
               GIA_TRI_THUOC_TINH    = P_GIA_TRI_THUOC_TINH,
               TRANG_THAI     = P_TRANG_THAI,
               STT_THUOC_TINH        = P_STT_THUOC_TINH,
               MA_LINH_VUC          =P_MA_LINH_VUC,
               MA_HO_SO             =P_MA_HO_SO,
               MA_THUOC_TINH_DM=P_MA_THUOC_TINH_DM
         WHERE MA_THUOC_TINH LIKE P_MA_THUOC_TINH;
        P_VAL := 'THANH_CONG';
      END IF;
    
  
  END;
   
   --------------------------------------------------HOSO2-----------------------------
   PROCEDURE SELECT_HOSO2(P_CUR IN OUT MYCUR,P_MA_LINH_VUC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,( SELECT D.TEN_QUAN_HUYEN  FROM DM_QUAN_HUYEN D WHERE D.MA_QUAN_HUYEN LIKE A.MA_HUYEN)AS TEN_QUAN_HUYEN,TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') as NGAY_LAP_HO_SO,
      ( SELECT E.TEN_HINH_THUC  FROM BTTDC_HINH_THUC E WHERE E.MA_HINH_THUC LIKE A.MA_HINH_THUC)AS MA_HINH_THUC, 
             CASE
               WHEN A.TRANG_THAI  = 0 THEN
               'Ðóng'
               ELSE
                 'Mở'
             END AS TEN_TRANG_THAI
        FROM  BTTDC_HO_SO2 A WHERE A.MA_HINH_THUC LIKE P_MA_LINH_VUC
       ORDER BY A.STT;
  END;
  

  -------------------------------------------------------------------------------
  PROCEDURE SELECT_HOSO2_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_HO_SO NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.NGAY_LAP_HO_SO,'DD/MM/YYYY') as NGAY_LAP_HO_SO,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_HO_SO2 A
       WHERE A.MA_HO_SO2 LIKE P_MA_HO_SO;
  END;
 
   -----------------------------------------------------------------------
     
   PROCEDURE UPDATE_HO_SO2( P_VAL   OUT NVARCHAR2,
     P_UPDATE_TYPE NVARCHAR2,
     P_MA_HO_SO2 NVARCHAR2,
     P_KHHS NVARCHAR2,
     P_TEN_HO_SO NVARCHAR2,
     P_TEN_CONG_TRINH NVARCHAR2,
     P_MA_HUYEN NVARCHAR2,
     P_MA_QUOC_TICH NVARCHAR2,
     P_MA_HINH_THUC NVARCHAR2,
     P_NGAY_LAP_HO_SO NVARCHAR2,
     P_TRANG_THAI INT ,
     P_STT INT  ,
     P_TTSTNMT NVARCHAR2,
     P_QDUBND NVARCHAR2,
     P_TTSTC NVARCHAR2,
     P_NGUOI_NOP NVARCHAR2,
     P_TTSTNMTSH NVARCHAR2,
     P_QDUBNDSH NVARCHAR2,
     P_TTSTCSH NVARCHAR2,
     P_TEN_DON_VI NVARCHAR2,
     P_LINH_VUC NVARCHAR2,
     P_HT_TRA_TIEN NVARCHAR2,
     P_LOAI_DAT_GIAO NVARCHAR2,
     P_LOAI_DAT_THUE NVARCHAR2,
     P_GIA_GIAO NVARCHAR2,
     P_KPTD NVARCHAR2,
     P_TT_KPTD INT,
     P_GIA_THUE NVARCHAR2
     ) AS
  	
   
  BEGIN
    P_VAL := 'CO_LOI'; 
    P_VAL :=Ma_Ho_So2_Seq.NEXTVAL; 
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
       
        INSERT INTO BTTDC_HO_SO2
          (MA_HO_SO2 ,
     KHHS ,
     TEN_HO_SO ,
     TEN_CONG_TRINH ,
     MA_HUYEN ,
     MA_QUOC_TICH ,
     MA_HINH_THUC ,
     NGAY_LAP_HO_SO ,
     TRANG_THAI  ,
     STT   ,
     TTSTNMT ,
     QDUBND ,
     TTSTC,
     NGUOI_NOP,
     TTSTNMTSH,
     QDUBNDSH,
     TTSTCSH ,
      TEN_DON_VI ,
     LINH_VUC ,
     HT_TRA_TIEN ,
     LOAI_DAT_GIAO ,
     LOAI_DAT_THUE,
     KPTD,
     TT_KPTD,
     GIA_GIAO,
     GIA_THUE)
        VALUES
          (P_VAL,
     P_KHHS ,
     P_TEN_HO_SO ,
     P_TEN_CONG_TRINH ,
     P_MA_HUYEN ,
     P_MA_QUOC_TICH ,
     P_MA_HINH_THUC ,
     to_date(P_NGAY_LAP_HO_SO,'DDMMYYYY') ,
     P_TRANG_THAI  ,
     P_STT   ,
     P_TTSTNMT ,
     P_QDUBND ,
     P_TTSTC,
     P_NGUOI_NOP,
     P_TTSTNMTSH,
     P_QDUBNDSH,
     P_TTSTCSH,
     P_TEN_DON_VI ,
     P_LINH_VUC ,
     P_HT_TRA_TIEN ,
     P_LOAI_DAT_GIAO ,
     P_LOAI_DAT_THUE,
     P_KPTD,
     P_TT_KPTD,
      P_GIA_GIAO,
     P_GIA_THUE 
     );
        
     
    ELSE
      
        UPDATE BTTDC_HO_SO2
           SET TEN_HO_SO    = P_TEN_HO_SO,
               TEN_CONG_TRINH    = P_TEN_CONG_TRINH,
               MA_HUYEN     = P_MA_HUYEN,
               MA_QUOC_TICH        = P_MA_QUOC_TICH,
               MA_HINH_THUC          =P_MA_HINH_THUC,
               NGAY_LAP_HO_SO             =to_date(P_NGAY_LAP_HO_SO,'DDMMYYYY'),
               TRANG_THAI=P_TRANG_THAI,
               STT=P_STT,
               TTSTNMT=P_TTSTNMT,
               QDUBND=P_QDUBND,
               TTSTC=P_TTSTC,
               NGUOI_NOP=P_NGUOI_NOP,
               TTSTNMTSH=P_TTSTNMTSH,
               QDUBNDSH=P_QDUBNDSH,
               TTSTCSH=P_TTSTCSH,
               TEN_DON_VI=P_TEN_DON_VI,
               LINH_VUC=P_LINH_VUC,
               HT_TRA_TIEN=P_HT_TRA_TIEN,
               LOAI_DAT_GIAO=P_LOAI_DAT_GIAO,
               LOAI_DAT_THUE=P_LOAI_DAT_THUE,
               GIA_GIAO=P_GIA_GIAO,
               KPTD=P_KPTD,
               TT_KPTD=P_TT_KPTD,
               GIA_THUE=P_GIA_THUE
               
         WHERE MA_HO_SO2 LIKE P_MA_HO_SO2;
         
      END IF;
    
  
  END;
   
   
------------------------------------------------------------------------------------
  PROCEDURE SELECT_GT_THUOCTINHLV(P_CUR        IN OUT MYCUR,
                                  P_MA_LINH_VUC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM BTTDC_GT_THUOCTINH_HS A
       WHERE A.MA_LINH_VUC LIKE P_MA_LINH_VUC ORDER BY A.STT_THUOC_TINH;
  END;  
   
   
  ----------------------------THU VIEN PHAP LUAT--------------------------------------
  PROCEDURE SELECT_VB_PL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
               'Còn hiệu lực'
               ELSE
                 'Hết hiệu lực'
             END AS TEN_TRANG_THAI,TO_CHAR(A.NGAY_BAN_HANH,'DD/MM/YYYY') as NGAY_BAN_HANH
        FROM BTTDC_THUVIENPHAPLUAT A
       ORDER BY A.STT;
  END;
  
  
  -------------------------------------------------------------------------------
  PROCEDURE SELECT_VB_PL_DETAIL(P_CUR        IN OUT MYCUR,
                                  P_MA_VB_PL NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Còn hiệu lực'
               ELSE
                'Hết hiệu lực'
             END AS TEN_TRANG_THAI,TO_CHAR(A.NGAY_BAN_HANH,'DD/MM/YYYY') as NGAY_BAN_HANH
        FROM BTTDC_THUVIENPHAPLUAT A
       WHERE A.MA_VB LIKE P_MA_VB_PL;
  END;
  -------------------------------------------------------------------------------
  PROCEDURE UPDATE_VB_PL(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_VB     NVARCHAR2,
                           P_TRICH_YEU    NVARCHAR2,
                           P_SO_HIEU    NVARCHAR2,
                           P_TRANG_THAI        INT,
                           P_STT     INT,
                           P_CO_QUAN_BAN_HANH NVARCHAR2,
                           P_NGAY_BAN_HANH NVARCHAR2,
                           P_FILE_PATH NVARCHAR2   
                           ) AS
    V_TON_TAI_MA  INT;
   
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_VB)
        INTO V_TON_TAI_MA
        FROM BTTDC_THUVIENPHAPLUAT
       WHERE UPPER(MA_VB) LIKE UPPER(P_MA_VB);
      
      IF V_TON_TAI_MA > 0 OR P_MA_VB = '0' THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
     
      ELSE
        INSERT INTO BTTDC_THUVIENPHAPLUAT
          (MA_VB,
                           TRICH_YEU,
                           SO_HIEU,
                           TRANG_THAI,
                           STT,
                           CQ_BAN_HANH,
                           NGAY_BAN_HANH,
                           FILE_PATH )
        VALUES
          (MA_VBPL_SEQ.NEXTVAL,
                           P_TRICH_YEU,
                           P_SO_HIEU,
                           P_TRANG_THAI,
                           P_STT,
                           P_CO_QUAN_BAN_HANH,
                           to_date(P_NGAY_BAN_HANH,'DDMMYYYY'),
                           P_FILE_PATH );
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      
    
        UPDATE BTTDC_THUVIENPHAPLUAT
           SET TRICH_YEU    = P_TRICH_YEU,
               SO_HIEU    = P_SO_HIEU,
               TRANG_THAI     = P_TRANG_THAI,
               STT        = P_STT,
               CQ_BAN_HANH          =P_CO_QUAN_BAN_HANH,
               NGAY_BAN_HANH= to_date(P_NGAY_BAN_HANH,'DDMMYYYY'),
               FILE_PATH=P_FILE_PATH
         WHERE MA_VB LIKE P_MA_VB;
        P_VAL := 'THANH_CONG';
     
    END IF;
  
  END;
  -------------------------------------------------------------------------------
  
   
     PROCEDURE DELETE_HO_SO2(P_VAL   OUT NVARCHAR2, P_MA_HO_SO NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM BTTDC_HO_SO2 WHERE MA_HO_SO2 LIKE P_MA_HO_SO;
    P_VAL := 'THANH_CONG';
   END;    
   ------------------------------------------------delete-gttt--------------------------
    PROCEDURE DELETE_GT_THUOCTINH_HS(P_VAL   OUT NVARCHAR2, P_MA_HO_SO NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM BTTDC_GT_THUOCTINH_HS WHERE MA_HO_SO LIKE P_MA_HO_SO;
    P_VAL := 'THANH_CONG';
   END; 
   -------------------------------------------------delete-vptn--------------------------
    PROCEDURE DELETE_VBTN(P_VAL   OUT NVARCHAR2, P_MA_HO_SO NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM BTTDC_VB_TIEP_NHAN WHERE MA_HO_SO LIKE P_MA_HO_SO;
    P_VAL := 'THANH_CONG';
   END; 
   
   
end TNMT_BTTDC;
/

prompt
prompt Creating package body TNMT_DATCONG
prompt ==================================
prompt
create or replace package body hai.TNMT_DATCONG is
 ------------------------------DC_VAN_BAN_SDD------------------------------------------
PROCEDURE SELECT_VANBAN_SDD(P_CUR IN OUT MYCUR, P_MA_HS_DAT NVARCHAR2) AS
      
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.Ngay_Ban_Hanh,'DD/MM/YYYY') as ngaybanhanh,TO_CHAR(A.Ngay_Up,'DD/MM/YYYY') as NGAYUP,B.TEN_DON_VI,C.TEN_LOAI_VAN_BAN
        FROM DC_VANBAN_SDD A,DC_HO_SO_DAT B, DC_LOAI_VAN_BAN C
        WHERE A.MA_HS_DAT=B.MA_HS_DAT
        AND A.MA_LOAI_VAN_BAN=C.MA_LOAI_VAN_BAN
        AND A.MA_HS_DAT LIKE  P_MA_HS_DAT
       ORDER BY A.MA_HS;
  END;
---------------------------------------------------------------------------------------
PROCEDURE SELECT_VANBAN_SDD_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_HS_DAT NVARCHAR2,P_MA_HS NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.Ngay_Ban_Hanh,'DD/MM/YYYY') as NGAYBANHANH,TO_CHAR(A.Ngay_Up,'DD/MM/YYYY') as NGAYUP,B.TEN_DON_VI,C.TEN_LOAI_VAN_BAN
        FROM DC_VANBAN_SDD A,DC_HO_SO_DAT B, DC_LOAI_VAN_BAN C
        WHERE A.MA_HS_DAT=B.MA_HS_DAT
        AND A.MA_LOAI_VAN_BAN=C.MA_LOAI_VAN_BAN
        AND A.MA_HS_DAT LIKE P_MA_HS_DAT
        AND A.MA_HS LIKE P_MA_HS 
       ORDER BY A.MA_HS;
  END;
------------------------------------------------------------------------------------------
------------------------------DC_VAN_BAN_SDD------------------------------------------
PROCEDURE SELECT_SU_DUNG_DAT(P_CUR  IN OUT MYCUR, P_MA_HS_DAT number) AS
