  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 đơn 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 phườ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 đơn 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 đơn 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 cơ 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 cơ 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 cơ 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ất 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;

  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;
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 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
  BEGIN
    OPEN P_CUR FOR
      SELECT B.*,TO_CHAR(A.NGAY_SD_DAT,'DD/MM/YYYY') as NGAYSDDAT,B.TEN_DON_VI,B.MA_THUA,D.TEN_GIAO_DICH,
      A.MA_SU_DUNG_DAT,A.GHI_CHU_HSD,A.THANH_TIEN_SDD, A.DON_GIA
        FROM DC_HO_SO_DAT B,DC_SU_DUNG_DAT A,DC_GIAO_DICH D, DC_LUY_TIEN E
        WHERE A.MA_HS_DAT=B.MA_HS_DAT
        AND B.MA_THUA=E.MA_THUA
        AND A.MA_HS_DAT=B.MA_HS_DAT
        AND A.MA_GIAO_DICH=D.MA_GIAO_DICH
        AND E.SU_DUNG=0
        AND B.MA_HS_DAT LIKE P_MA_HS_DAT
       ORDER BY B.MA_THUA;
  END;
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
PROCEDURE SELECT_SU_DUNG_DAT_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_HS_DAT number,P_MA_SU_DUNG_DAT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.NGAY_SD_DAT,'DD/MM/YYYY') as NGAYSDDAT,B.TEN_DON_VI,C.SO_THUA,D.TEN_GIAO_DICH
        FROM DC_SU_DUNG_DAT A,DC_HO_SO_DAT B, DC_THUADAT C,DC_GIAO_DICH D
        WHERE A.MA_HS_DAT=B.MA_HS_DAT
        AND B.MA_THUA=C.MA_THUA
        AND A.MA_GIAO_DICH=D.MA_GIAO_DICH
        AND A.MA_HS_DAT LIKE P_MA_HS_DAT
        AND A.MA_SU_DUNG_DAT LIKE P_MA_SU_DUNG_DAT 
       ORDER BY A.MA_SU_DUNG_DAT;

  END;
------------------------------------------------------------------------------------------
 ------------------------------DC_THUADAT------------------------------------------
PROCEDURE SELECT_DC_THUADAT(P_CUR  IN OUT MYCUR, P_MA_PHUONG_XA NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_THUADAT A
        WHERE A.MA_PHUONG_XA LIKE P_MA_PHUONG_XA 
       ORDER BY A.MA_THUA;
  END;
-----------------------------------------DC_HO_SO_DAT----------------------------------------------       

PROCEDURE UPDATE_DC_HOSODAT(P_VAL            OUT NVARCHAR2,
                           P_MA_HS_DAT     NUMBER,
                           P_UPDATE_TYPE    NVARCHAR2,
                          -- P_MA_HUYEN    NVARCHAR2,
                           P_TEN_DON_VI    NVARCHAR2,
                           P_PHUONG_THUC NVARCHAR2,
                           P_MA_MUC_DICH NVARCHAR2,
                           P_THUOC_TO_CHUC int,
                           P_MA_QUOC_TICH NVARCHAR2,
                           P_SDT NVARCHAR2, 
                           P_GHI_CHU NVARCHAR2,
                           P_MA_HINH_THUC NVARCHAR2,
                           P_DAU_GIA INT,
                           P_TIEN_DAU_GIA FLOAT,
                           P_LY_DO NVARCHAR2,
                           P_TRANG_THAI NVARCHAR2,
                           P_MA_THUA NUMBER,
                           P_TIEN_THUE NVARCHAR2,
                           P_ID_DONG NUMBER,
                           P_THOI_HAN INT,
                           P_TG_BD NVARCHAR2,
                           P_TG_KT NVARCHAR2,
                           P_MA_DOI_TUONG INT,
                           P_DIEN_TICH_SD FLOAT,
                           P_DON_VI_TO_CHUC_DAU_GIA NVARCHAR2
                           ) AS
  BEGIN
      IF P_UPDATE_TYPE = 'THEM_MOI' THEN
          INSERT INTO DC_HO_SO_DAT
          (MA_HS_DAT,MA_QUAN_HUYEN,TEN_DON_VI,
          PHUONG_THUC_THANH_TOAN,MA_MUC_DICH_SU_DUNG_DAT, THUOC_TO_CHUC, MA_QUOC_TICH,
          SDT, GHI_CHU, MA_HINH_THUC,MA_THUA,DAU_GIA, TIEN_DAU_GIA, LY_DO,TRANG_THAI,TIEN_THUE,ID_DONG,MA_DOI_TUONG,DIEN_TICH_SD,DON_VI_TO_CHUC_DAU_GIA)
        VALUES
          (DC_HO_SO_DAT_SEQ.NEXTVAL,(SELECT MA_QUAN_HUYEN FROM DM_QUAN_HUYEN WHERE MA_QUAN_HUYEN IN(SELECT MA_QUAN_HUYEN FROM DM_PHUONG_XA WHERE MA_PHUONG_XA IN(SELECT MA_PHUONG_XA FROM DC_THUADAT WHERE MA_THUA=P_MA_THUA))),P_TEN_DON_VI,
           P_PHUONG_THUC,P_MA_MUC_DICH, P_THUOC_TO_CHUC, P_MA_QUOC_TICH,
            P_SDT, P_GHI_CHU, P_MA_HINH_THUC, P_MA_THUA, P_DAU_GIA, P_TIEN_DAU_GIA, P_LY_DO,P_TRANG_THAI,P_TIEN_THUE,P_ID_DONG,P_MA_DOI_TUONG,P_DIEN_TICH_SD,P_DON_VI_TO_CHUC_DAU_GIA ); 
        UPDATE DC_LUY_TIEN 
        SET NGUOI_DUNG=P_TEN_DON_VI,
        TG_BK=to_date(P_TG_BD,'DDMMYYYY'),
        TG_KT=to_date(P_TG_KT,'DDMMYYYY'),
        LA_THUOC_TO_CHUC=P_THUOC_TO_CHUC,
        THOI_HAN=P_THOI_HAN,
        MA_CAP_QUAN_LY =(SELECT MA_CAP_QUAN_LY FROM DC_CAP_QUAN_LY WHERE TRANG_THAI=-1)
        WHERE MA_THUA=P_MA_THUA AND SU_DUNG=0;
        P_VAL := 'IN_THANH_CONG';
      ELSE
        BEGIN
           ---cu---test thoi gian   
        UPDATE DC_LUY_TIEN 
        SET NGUOI_DUNG=null,
        TG_BK=NULL,--to_date(P_TG_BD,'DDMMYYYY'),
        TG_KT=NULL,--to_date(P_TG_KT,'DDMMYYYY'),
        LA_THUOC_TO_CHUC=0,
        THOI_HAN=NULL,--P_THOI_HAN,
        MA_CAP_QUAN_LY =(SELECT MA_CAP_QUAN_LY FROM DC_CAP_QUAN_LY WHERE TRANG_THAI=-1)
        WHERE ma_luy_tien in (SELECT b.ma_luy_tien FROM Dc_Ho_So_Dat A, DC_LUY_TIEN B 
        WHERE A.MA_THUA=B.MA_THUA AND A.MA_HS_DAT =P_MA_HS_DAT AND B.SU_DUNG=0)
        AND SU_DUNG=0;
        --SAU---UPDATE HS 
        UPDATE DC_THUADAT
        SET MA_MUC_DICH_SU_DUNG_DAT=(SELECT MA_MUC_DICH_SU_DUNG_DAT FROM DC_MUC_DICH_SU_DUNG_DAT  WHERE LOAI_MUC_DICH_MUC_DICH=-1),
        MA_HINH_THUC=(SELECT MA_HINH_THUC FROM DC_HINH_THUC WHERE TT_HINH_THUC=-1)
        WHERE MA_THUA IN (SELECT MA_THUA FROM DC_HO_SO_DAT WHERE MA_HS_DAT=P_MA_HS_DAT);
        -----
         UPDATE DC_HO_SO_DAT SET MA_QUAN_HUYEN=(SELECT MA_QUAN_HUYEN FROM DM_QUAN_HUYEN WHERE MA_QUAN_HUYEN IN(SELECT MA_QUAN_HUYEN FROM DM_PHUONG_XA WHERE MA_PHUONG_XA IN(SELECT MA_PHUONG_XA FROM DC_THUADAT WHERE MA_THUA=P_MA_THUA))),TEN_DON_VI=P_TEN_DON_VI,
       
            PHUONG_THUC_THANH_TOAN=P_PHUONG_THUC,
           MA_MUC_DICH_SU_DUNG_DAT=P_MA_MUC_DICH,THUOC_TO_CHUC=P_THUOC_TO_CHUC, MA_QUOC_TICH=P_MA_QUOC_TICH,
          SDT=P_SDT,GHI_CHU=P_GHI_CHU,MA_HINH_THUC=P_MA_HINH_THUC, MA_THUA=P_MA_THUA,
           DAU_GIA=P_DAU_GIA,TIEN_DAU_GIA=P_TIEN_DAU_GIA, LY_DO=P_LY_DO,TRANG_THAI=P_TRANG_THAI,
           TIEN_THUE=P_TIEN_THUE,ID_DONG=P_ID_DONG,MA_DOI_TUONG=P_MA_DOI_TUONG, DIEN_TICH_SD=P_DIEN_TICH_SD,
           DON_VI_TO_CHUC_DAU_GIA=P_DON_VI_TO_CHUC_DAU_GIA
           WHERE MA_HS_DAT LIKE P_MA_HS_DAT;
           
  
        --update moi
          UPDATE DC_THUADAT
        SET MA_MUC_DICH_SU_DUNG_DAT=P_MA_MUC_DICH,
        MA_HINH_THUC=P_MA_HINH_THUC
        WHERE MA_THUA =P_MA_THUA;
        UPDATE DC_LUY_TIEN 
        SET NGUOI_DUNG=P_TEN_DON_VI,
        TG_BK=to_date(P_TG_BD,'DDMMYYYY'),
        TG_KT=to_date(P_TG_KT,'DDMMYYYY'),
        LA_THUOC_TO_CHUC=P_THUOC_TO_CHUC,
        THOI_HAN=P_THOI_HAN,
        MA_CAP_QUAN_LY =(SELECT MA_CAP_QUAN_LY FROM DC_CAP_QUAN_LY WHERE TRANG_THAI=-1)
        WHERE MA_THUA=P_MA_THUA AND SU_DUNG=0;
        P_VAL := 'UP_THANH_CONG';
        END;
      END IF;
  END;
  ------------UPDATE HOSO DAT BIEN DONG
  
PROCEDURE UPDATE_DC_HOSODAT_BD(P_VAL            OUT NVARCHAR2,
                           P_MA_HS_DAT     NUMBER,
                          -- P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_HUYEN    NVARCHAR2,
                           P_TEN_DON_VI    NVARCHAR2,
                           P_PHUONG_THUC NVARCHAR2,
                           P_MA_MUC_DICH NVARCHAR2,
                           P_THUOC_TO_CHUC int,
                           P_MA_QUOC_TICH NVARCHAR2,
                           P_SDT NVARCHAR2, 
                           P_GHI_CHU NVARCHAR2,
                           P_MA_HINH_THUC NVARCHAR2,
                           P_DAU_GIA INT,
                           P_TIEN_DAU_GIA FLOAT,
                           P_LY_DO NVARCHAR2,
                           P_TRANG_THAI NVARCHAR2,
                           P_MA_THUA NUMBER,
                           P_TIEN_THUE NVARCHAR2,
                           P_ID_DONG NUMBER,
                           P_THOI_HAN INT,
                           P_TG_BD NVARCHAR2,
                           P_TG_KT NVARCHAR2,
                           P_MA_DOI_TUONG INT,
                           P_DIEN_TICH_SD FLOAT
                           ) AS
  BEGIN
		UPDATE DC_HO_SO_DAT
		SET TRANG_THAI='KSD'
		WHERE MA_HS_DAT=P_MA_HS_DAT;
          INSERT INTO DC_HO_SO_DAT
          (MA_HS_DAT,MA_QUAN_HUYEN,TEN_DON_VI,
          PHUONG_THUC_THANH_TOAN,MA_MUC_DICH_SU_DUNG_DAT, THUOC_TO_CHUC, MA_QUOC_TICH,
          SDT, GHI_CHU, MA_HINH_THUC,MA_THUA,DAU_GIA, TIEN_DAU_GIA, LY_DO,TRANG_THAI,TIEN_THUE,ID_DONG,MA_DOI_TUONG,DIEN_TICH_SD)
        VALUES
          (DC_HO_SO_DAT_SEQ.NEXTVAL,P_MA_HUYEN,P_TEN_DON_VI,
           P_PHUONG_THUC,P_MA_MUC_DICH, P_THUOC_TO_CHUC, P_MA_QUOC_TICH,
            P_SDT, P_GHI_CHU, P_MA_HINH_THUC, P_MA_THUA, P_DAU_GIA, P_TIEN_DAU_GIA, P_LY_DO,P_TRANG_THAI,P_TIEN_THUE,P_ID_DONG,P_MA_DOI_TUONG,P_DIEN_TICH_SD ); 
        UPDATE DC_LUY_TIEN 
        SET NGUOI_DUNG=P_TEN_DON_VI,
        TG_BK=to_date(P_TG_BD,'DDMMYYYY'),
        TG_KT=to_date(P_TG_KT,'DDMMYYYY'),
        LA_THUOC_TO_CHUC=P_THUOC_TO_CHUC,
        THOI_HAN=P_THOI_HAN,
        MA_CAP_QUAN_LY =(SELECT MA_CAP_QUAN_LY FROM DC_CAP_QUAN_LY WHERE TRANG_THAI=-1)
        WHERE MA_THUA=P_MA_THUA AND SU_DUNG=0;
        P_VAL := 'IN_THANH_CONG';
     
  END;
--------------------------------------------------------VAN_BAN_SDD-------------------------------------------------------
  PROCEDURE UPDATE_DC_VANBAN_SDD(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_HS     NUMBER,
                           P_MA_HS_DAT     NUMBER,
                           P_TIEU_DE_HS    NVARCHAR2,
                           P_TOM_TAT_HS    NVARCHAR2,
                           P_FILE_HS    NVARCHAR2,
                           P_NGAY_BAN_HANH     NVARCHAR2,
                           P_CO_QUAN_BAN_HANH     NVARCHAR2,
                           P_MA_LOAI_VAN_BAN     NVARCHAR2,
                           --P_MA_HS_DAT     NUMBER,
                           --P_NGAY_UP     NVARCHAR2,
                           P_NGUOI_UP    NVARCHAR2,
                           P_SO_KY_HIEU     NVARCHAR2,
                           P_SO_TRANG     INT,
                           P_TT_VAN_BAN     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_HS)
        INTO V_TON_TAI_MA
        FROM DC_VANBAN_SDD
       WHERE UPPER(MA_HS) LIKE UPPER(P_MA_HS);
      SELECT COUNT(MA_HS)
        INTO V_TON_TAI_TEN
        FROM Dc_Vanban_Sdd
       WHERE UPPER(TIEU_DE_HS) LIKE UPPER(P_TIEU_DE_HS);
      IF V_TON_TAI_MA > 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  DC_VANBAN_SDD
          (
           MA_HS,
           TIEU_DE_HS,
           TOM_TAT_HS,
           FILE_HS,
           NGAY_BAN_HANH,
           CO_QUAN_BAN_HANH,
           MA_LOAI_VAN_BAN,
           MA_HS_DAT,
          NGAY_UP,
           NGUOI_UP,
           SO_KY_HIEU,
           SO_TRANG,
           TT_VAN_BAN
          )
        VALUES
          (DC_VANBAN_SDD_SEQ.NEXTVAL,
         P_TIEU_DE_HS,
         P_TOM_TAT_HS,
         P_FILE_HS,
        to_date(P_NGAY_BAN_HANH,'DDMMYYYY') ,
         P_CO_QUAN_BAN_HANH,
         P_MA_LOAI_VAN_BAN,
         P_MA_HS_DAT,
         sysdate ,
         P_NGUOI_UP,
         P_SO_KY_HIEU,
         P_SO_TRANG,
         P_TT_VAN_BAN);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE DC_VANBAN_SDD
           SET TIEU_DE_HS=P_TIEU_DE_HS,
         TOM_TAT_HS=P_TOM_TAT_HS,
         FILE_HS=P_FILE_HS,
         NGAY_BAN_HANH=to_date(P_NGAY_BAN_HANH,'DDMMYYYY'),
         CO_QUAN_BAN_HANH=P_CO_QUAN_BAN_HANH,
         MA_LOAI_VAN_BAN=P_MA_LOAI_VAN_BAN,
         MA_HS_DAT=P_MA_HS_DAT,
         NGAY_UP=sysdate,
         NGUOI_UP=P_NGUOI_UP,
         SO_KY_HIEU=P_SO_KY_HIEU,
         SO_TRANG=P_SO_TRANG,
         TT_VAN_BAN=P_TT_VAN_BAN
         WHERE   MA_HS = P_MA_HS;
        P_VAL := 'THANH_CONG';
    END IF;
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_DC_VANBAN_SDD(P_VAL   OUT NVARCHAR2, P_MA_HS NUMBER) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_VANBAN_SDD WHERE MA_HS = P_MA_HS;
    P_VAL := 'THANH_CONG';
   END;
--------------------------------------------------------SU_DUNG_DAT-------------------------------------------------------
  PROCEDURE UPDATE_DC_SU_DUNG_DAT(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_SU_DUNG_DAT     number,
                           P_MA_HS_DAT number,
                           P_NGAY_SD_DAT    NVARCHAR2,
                           P_GHI_CHU_HSD    NVARCHAR2,
                           P_THANH_TIEN_SDD     FLOAT,
                           P_MA_GIAO_DICH     NVARCHAR2,
                           P_DON_GIA     INT
                           ) AS
    V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(MA_SU_DUNG_DAT)
        INTO V_TON_TAI_MA
        FROM DC_SU_DUNG_DAT
       WHERE UPPER(MA_SU_DUNG_DAT) LIKE UPPER(P_MA_SU_DUNG_DAT);
      IF V_TON_TAI_MA > 0  THEN
        -- kiem tra trung ma , khong cho them ma='0'
        P_VAL := 'TON_TAI_MA';
      ELSE
        INSERT INTO  DC_SU_DUNG_DAT
          (
           MA_SU_DUNG_DAT,
           NGAY_SD_DAT,
           THANH_TIEN_SDD,
           MA_HS_DAT,
           MA_GIAO_DICH,
           DON_GIA,
           GHI_CHU_HSD
          )
        VALUES
          (DC_GIAODICH_DAT_SEQ.NEXTVAL,
          to_date(P_NGAY_SD_DAT,'DDMMYYYY'),
           P_THANH_TIEN_SDD,
           P_MA_HS_DAT,
           P_MA_GIAO_DICH,
           P_DON_GIA,
           P_GHI_CHU_HSD);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
        UPDATE DC_SU_DUNG_DAT
           SET 
           NGAY_SD_DAT=to_date(P_NGAY_SD_DAT,'DDMMYYYY'),
           GHI_CHU_HSD=P_GHI_CHU_HSD,
           THANH_TIEN_SDD=P_THANH_TIEN_SDD,
           MA_HS_DAT=P_MA_HS_DAT,
           MA_GIAO_DICH=P_MA_GIAO_DICH,
           DON_GIA=P_DON_GIA
         WHERE   MA_SU_DUNG_DAT like P_MA_SU_DUNG_DAT;
        P_VAL := 'THANH_CONG';
    END IF;
  END;

   -------------------------------------------------------------------------------
  PROCEDURE DELETE_DC_SU_DUNG_DAT(P_VAL   OUT NVARCHAR2, P_MA_SU_DUNG_DAT NUMBER) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_SU_DUNG_DAT WHERE MA_SU_DUNG_DAT LIKE P_MA_SU_DUNG_DAT;
    P_VAL := 'THANH_CONG';
   END;

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

   --HINH THUC : THUE DAT, LOAI VAN BAN: QUYET DINH, GIAO DICH LAN 1, TRUYEN MA HUYEN QUA XA 
   PROCEDURE REPORT_TC_THUEDATHUYEN(P_CUR  IN OUT MYCUR, P_MA_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_HUYEN;
    IF V_KT=0 THEN
      V_MA_QUAN_HUYEN:='';
     ELSE
      V_MA_QUAN_HUYEN:=P_MA_HUYEN;
    END IF;
   IF V_MA_QUAN_HUYEN IS NOT NULL THEN
       OPEN P_CUR FOR
       SELECT A.MA_HS_DAT,A.TEN_DON_VI,i.vi_tri as DIA_CHI_DON_VI, f.SO_THUA,I.TO_BAN_DO,
      A.DIEN_TICH_SD, D.TEN_MUC_DICH_SU_DUNG_DAT, E.TIEU_DE_HS,TO_CHAR(I.TG_KT,'DD/MM/YYYY') as TG_KETTHUC ,

      CASE
               WHEN A.PHUONG_THUC_THANH_TOAN = 'ML' THEN '1 lần'
                 WHEN A.PHUONG_THUC_THANH_TOAN = 'TN' THEN 'Từng năm'
                  WHEN A.PHUONG_THUC_THANH_TOAN = 'NN' THEN '5 Năm'
             END AS PTTT, UPPER(H.TEN_QUAN_HUYEN) AS HUYEN
             
      ,C.Don_Gia, A.GHI_CHU
      FROM DC_HO_SO_DAT A, DC_HINH_THUC B, DC_SU_DUNG_DAT C,
      DC_MUC_DICH_SU_DUNG_DAT D , DC_VANBAN_SDD E,
      DC_THUADAT F, DM_PHUONG_XA G, DM_QUAN_HUYEN H, DC_LUY_TIEN I 
      WHERE A.MA_HINH_THUC=B.MA_HINH_THUC
      AND F.MA_THUA=I.MA_THUA AND SU_DUNG=0
      AND A.MA_HS_DAT=C.MA_HS_DAT
      AND D.MA_MUC_DICH_SU_DUNG_DAT=A.MA_MUC_DICH_SU_DUNG_DAT
      AND E.MA_HS_DAT=A.MA_HS_DAT
      AND F.MA_PHUONG_XA=G.MA_PHUONG_XA
      AND G.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
      AND E.MA_LOAI_VAN_BAN LIKE 'QĐ'
      AND B.MA_HINH_THUC LIKE 'SD_CT'
      AND C.MA_GIAO_DICH LIKE 'TGD_L1'
      AND G.MA_QUAN_HUYEN LIKE P_MA_HUYEN
      AND  TO_CHAR(I.TG_KT, 'YYYY') >=TO_CHAR(sysdate, 'YYYY') 
       ORDER BY A.MA_HS_DAT;
     ELSE
        OPEN P_CUR FOR
       SELECT A.MA_HS_DAT,A.TEN_DON_VI,I.VI_TRI AS DIA_CHI_DON_VI, F.SO_THUA,I.TO_BAN_DO,
      A.DIEN_TICH_SD, D.TEN_MUC_DICH_SU_DUNG_DAT, E.TIEU_DE_HS,TO_CHAR(I.TG_KT,'DD/MM/YYYY') as TG_KETTHUC ,

      CASE
               WHEN A.PHUONG_THUC_THANH_TOAN = 'ML' THEN '1 lần'
                 WHEN A.PHUONG_THUC_THANH_TOAN = 'TN' THEN 'Từng năm'
                  WHEN A.PHUONG_THUC_THANH_TOAN = 'NN' THEN '5 Năm'
             END AS PTTT, UPPER(H.TEN_QUAN_HUYEN) AS HUYEN
             
      ,C.Don_Gia, A.GHI_CHU
      FROM DC_HO_SO_DAT A, DC_HINH_THUC B, DC_SU_DUNG_DAT C,
      DC_MUC_DICH_SU_DUNG_DAT D , DC_VANBAN_SDD E,DC_LUY_TIEN I,
      DC_THUADAT F, DM_PHUONG_XA G, DM_QUAN_HUYEN H 
      WHERE A.MA_HINH_THUC=B.MA_HINH_THUC
      AND F.MA_THUA=I.MA_THUA AND SU_DUNG=0
      AND A.MA_HS_DAT=C.MA_HS_DAT
      AND D.MA_MUC_DICH_SU_DUNG_DAT=A.MA_MUC_DICH_SU_DUNG_DAT
      AND E.MA_HS_DAT=A.MA_HS_DAT

      AND F.MA_PHUONG_XA=G.MA_PHUONG_XA
      AND G.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
      AND E.MA_LOAI_VAN_BAN LIKE 'QĐ'
      AND B.MA_HINH_THUC LIKE 'SD_CT'
      AND C.MA_GIAO_DICH LIKE 'TGD_L1'
      AND  TO_CHAR(i.TG_KT, 'YYYY') >=TO_CHAR(sysdate, 'YYYY') 
       ORDER BY A.MA_HS_DAT;
      END IF;
  END;
   -------------------------------------------------------------------------------
   --QUOC TICH KHONG PHAI LA VN
    PROCEDURE REPORT_NUOC_NGOAI_THUE(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR

      SELECT DISTINCT A.MA_HS_DAT,A.TEN_DON_VI,K.TEN_QUOC_TICH, E.TEN_QUAN_HUYEN,F.DIEN_TICH_THUA,G.TEN_MUC_DICH_SU_DUNG_DAT,A.DIEN_TICH_SD,
        TO_CHAR(F.TG_KT, 'YYYY')-TO_CHAR(F.TG_BK, 'YYYY') AS "SO_NAM",
         CASE WHEN A.THUOC_TO_CHUC = 0 THEN 'Tổ chức'
              WHEN A.THUOC_TO_CHUC = 1 THEN 'Cá nhân, Hộ gia đình'
              END AS DT,
         TO_CHAR(F.TG_KT, 'YYYY') AS "NAM_KT",A.GHI_CHU
        FROM DC_HO_SO_DAT A, DC_THUADAT C, DM_PHUONG_XA D, DM_QUAN_HUYEN E,DC_LUY_TIEN F,
        DM_QUOC_TICH K,DC_MUC_DICH_SU_DUNG_DAT G
        WHERE A.MA_QUOC_TICH=K.MA_QUOC_TICH
        AND A.MA_THUA=C.MA_THUA
        AND F.MA_THUA=C.MA_THUA
        AND C.MA_PHUONG_XA=D.MA_PHUONG_XA
        AND D.MA_QUAN_HUYEN=E.MA_QUAN_HUYEN
        AND A.MA_MUC_DICH_SU_DUNG_DAT=G.MA_MUC_DICH_SU_DUNG_DAT
        AND A.MA_QUOC_TICH <> 'VN'
        AND F.SU_DUNG=0
        AND A.MA_HINH_THUC='SD_CT'
        AND TO_CHAR(F.TG_KT, 'YYYY')> P_NAM
        AND A.TRANG_THAI <>'KSD'
       ORDER BY A.MA_HS_DAT;
      
  END;
   -------------------------------------------------------------------------------

    PROCEDURE REPORT_XAY_DUNG_CSDL_DAT(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
    --MA_CSDL: HT= HOAN THANH; DTK=DANG TRIEN KHAI; DTH= DA TICH HOP;MA TINH=TIEN_GIANG; CTH= CHUA THUC HIEN
      SELECT DISTINCT A.MA_QUAN_HUYEN,TEN_QUAN_HUYEN,U.TONG,H.TONG_HT,N.TONG_TH,P.TONG_CTH,M.TONG_DTK,'' AS GHICHU
      FROM  DM_QUAN_HUYEN A 
      LEFT JOIN
      (
             SELECT QH.MA_QUAN_HUYEN,COUNT(QHC.MA_PHUONG_XA) AS "TONG_HT"
             FROM DC_QH_CSDL QHC,DM_PHUONG_XA PHX, DC_CSDL_DAT DLD, DM_QUAN_HUYEN QH 
             WHERE QHC.MA_CSDL_DAT LIKE 'HT'
             AND PHX.MA_PHUONG_XA=QHC.MA_PHUONG_XA
             AND QHC.MA_CSDL_DAT=DLD.MA_CSDL_DAT
             AND PHX.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
             GROUP BY QH.MA_QUAN_HUYEN 
      ) H ON  H.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
      LEFT JOIN
      (
             SELECT QH.MA_QUAN_HUYEN,COUNT(PHX.MA_PHUONG_XA) AS "TONG"
             FROM DM_PHUONG_XA PHX, DM_QUAN_HUYEN QH 
             WHERE PHX.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
             GROUP BY QH.MA_QUAN_HUYEN 
      ) U ON  U.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
      LEFT JOIN
      (
             SELECT QH.MA_QUAN_HUYEN,COUNT(QHC.MA_PHUONG_XA) AS "TONG_TH"
             FROM DC_QH_CSDL QHC,DM_PHUONG_XA PHX, DC_CSDL_DAT DLD, DM_QUAN_HUYEN QH 
             WHERE QHC.MA_CSDL_DAT LIKE 'DTH'
             AND PHX.MA_PHUONG_XA=QHC.MA_PHUONG_XA
             AND QHC.MA_CSDL_DAT=DLD.MA_CSDL_DAT
             AND PHX.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
             GROUP BY QH.MA_QUAN_HUYEN 
      ) N ON  N.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
      LEFT JOIN
      (
             SELECT QH.MA_QUAN_HUYEN,COUNT(QHC.MA_PHUONG_XA) AS "TONG_CTH"
             FROM DC_QH_CSDL QHC,DM_PHUONG_XA PHX, DC_CSDL_DAT DLD, DM_QUAN_HUYEN QH 
             WHERE QHC.MA_CSDL_DAT LIKE 'CTH'
             AND PHX.MA_PHUONG_XA=QHC.MA_PHUONG_XA
             AND QHC.MA_CSDL_DAT=DLD.MA_CSDL_DAT
             AND PHX.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
             GROUP BY QH.MA_QUAN_HUYEN 
      ) P ON  P.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
LEFT JOIN
      (
             SELECT QH.MA_QUAN_HUYEN,COUNT(QHC.MA_PHUONG_XA) AS "TONG_DTK"
             FROM DC_QH_CSDL QHC,DM_PHUONG_XA PHX, DC_CSDL_DAT DLD, DM_QUAN_HUYEN QH 
             WHERE QHC.MA_CSDL_DAT LIKE 'DTK'
             AND PHX.MA_PHUONG_XA=QHC.MA_PHUONG_XA
             AND QHC.MA_CSDL_DAT=DLD.MA_CSDL_DAT
             AND PHX.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
             GROUP BY QH.MA_QUAN_HUYEN 
      ) M ON  M.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
      WHERE  A.MA_TINH_THANH='TIEN_GIANG'
      ORDER BY A.MA_QUAN_HUYEN;
  END;
   -------------------------------------------------------------------------------
   -------------------------------------------------------------------------------

    PROCEDURE REPORT_GIAO_DAT(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
           SELECT HT.TEN_HINH_THUC, H.SL_TC_GD ,U.SL_CN_GD,P.DT_TC_GD,M.DT_CN_GD,HH.SL_TC_GD_DG,UU.SL_CN_GD_DG,PP.DT_TC_GD_DG,MM.DT_CN_GD_DG,
        PPP.TIEN_TC_GD_DG,MMM.TIEN_CN_GD_DG,KHH.SL_TC_GD_KDG,KUU.SL_CN_GD_KDG,PPPP.DT_TC_GD_KDG,MMMM.DT_CN_GD_KDG,'' GHI_CHU

        FROM  DC_HINH_THUC HT
        LEFT JOIN
              (--GIAO DAT=GD, TO_CHUC=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_TC_GD
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  H ON  H.MA_HINH_THUC=HT.MA_HINH_THUC
        LEFT JOIN
              (--GIAO DAT=GD, CA NHAN=1
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_CN_GD
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  U ON  U.MA_HINH_THUC=HT.MA_HINH_THUC 
        -- TINH DIEN TICH

        LEFT JOIN
              (--GIAO DAT=GD, TO CHUC=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_TC_GD
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_GD'
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  P ON  P.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--GIAO DAT=GD, CA NHAN=1
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_CN_GD
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  M ON  M.MA_HINH_THUC=HT.MA_HINH_THUC                               
        -- THONG QUA DAU GIA
        ------
        LEFT JOIN
              (--GIAO DAT=GD, TO_CHUC=0, DAU GIA=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_TC_GD_DG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  HH ON  HH.MA_HINH_THUC=HT.MA_HINH_THUC
        LEFT JOIN
              (--GIAO DAT=GD, CA NHAN=1 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_CN_GD_DG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  UU ON  UU.MA_HINH_THUC=HT.MA_HINH_THUC 
        --TINH DT DAU GIA

        LEFT JOIN
              (--GIAO DAT=GD, TO CHUC=0, DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_TC_GD_DG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  PP ON  PP.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--GIAO DAT=GD, CA NHAN=1 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_CN_GD_DG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_GD'
                 AND HS.DAU_GIA=0
                 AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                 AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  MM ON  MM.MA_HINH_THUC=HT.MA_HINH_THUC  
        
        LEFT JOIN
              (--GIAO DAT=GD, TO CHUC=0, DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.TIEN_DAU_GIA)/1000 TIEN_TC_GD_DG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  PPP ON  PPP.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--GIAO DAT=GD, CA NHAN=1 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.TIEN_DAU_GIA)/1000 TIEN_CN_GD_DG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                 AND HS.DAU_GIA=0
                 AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                 AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  MMM ON  MMM.MA_HINH_THUC=HT.MA_HINH_THUC  
        --------------------------------------------
        -----KHONG THONG QUA DAU GIA
        -- THONG QUA DAU GIA
        ------
        LEFT JOIN
              (--GIAO DAT=GD, TO_CHUC=0, DAU GIA=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_TC_GD_KDG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_GD'
                AND HS.DAU_GIA=1
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  KHH ON  HH.MA_HINH_THUC=HT.MA_HINH_THUC
        LEFT JOIN
              (--GIAO DAT=GD, CA NHAN=1 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_CN_GD_KDG
                FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.DAU_GIA=1
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  KUU ON  UU.MA_HINH_THUC=HT.MA_HINH_THUC 
    LEFT JOIN
              (--GIAO DAT=GD, TO CHUC=0, DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_TC_GD_KDG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.DAU_GIA=1
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  PPPP ON  PPPP.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--GIAO DAT=GD, CA NHAN=1 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_CN_GD_KDG
                FROM DC_HO_SO_DAT HS, DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND HS.MA_HINH_THUC ='SD_GD'
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                 AND HS.DAU_GIA=1
                 AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                 AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  MMMM ON  MMMM.MA_HINH_THUC=HT.MA_HINH_THUC  
       ------------------------------------------------      
        WHERE HT.MA_HINH_THUC='SD_GD'
        ORDER BY HT.MA_HINH_THUC;
  END;

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

   PROCEDURE REPORT_THUE_DAT(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT 
        HT.TEN_HINH_THUC, A.SL_TC_CT,N.SL_CN_CT, D.DT_TC_CT,O.DT_CN_CT,AA.SL_TC_CT_DG,NN.SL_CN_CT_DG,DD.DT_TC_CT_DG,OO.DT_CN_CT_DG,
        DDD.TIEN_TC_CT_DG,OOO.TIEN_CN_CT_DG,KAA.SL_TC_CT_KDG,KNN.SL_CN_CT_KDG,DDDD.DT_TC_CT_KDG,OOOO.DT_CN_CT_KDG,'' GHI_CHU

        FROM  DC_HINH_THUC HT
        
        LEFT JOIN
              (--CHO_THUE=CT, TO CHUC=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_TC_CT
                FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  A ON  A.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--CHO_THUE=CT, CA NHAN=1
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_CN_CT
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  N ON  N.MA_HINH_THUC=HT.MA_HINH_THUC  
        -- TINH DIEN TICH                              
        LEFT JOIN
              (--CHO_THUE_DAT=CT, TO_CHUC=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_TC_CT
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  D ON  D.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--CHO_THUE_DAT=CT, CA_NHAN=1
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_CN_CT
                FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  O ON  O.MA_HINH_THUC=HT.MA_HINH_THUC

        -- THONG QUA DAU GIA
        ------
 
        LEFT JOIN
              (--CHO_THUE=CT, TO CHUC=0 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_TC_CT_DG
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  AA ON  AA.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--CHO_THUE=CT, CA NHAN=1 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_CN_CT_DG
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  NN ON  NN.MA_HINH_THUC=HT.MA_HINH_THUC  
        --TINH DT DAU GIA

 
        LEFT JOIN
              (--CHO_THUE_DAT=CT, TO_CHUC=0, DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_TC_CT_DG
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  DD ON  DD.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--CHO_THUE_DAT=CT, CA_NHAN=1, DAU GIA
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_CN_CT_DG
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  OO ON  OO.MA_HINH_THUC=HT.MA_HINH_THUC
        --TINH TIEN DAU GIA
 
        LEFT JOIN
              (--CHO_THUE_DAT=CT, TO_CHUC=0, DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.TIEN_DAU_GIA)/1000 TIEN_TC_CT_DG
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  DDD ON  DDD.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--CHO_THUE_DAT=CT, CA_NHAN=1, DAU GIA
                SELECT  HS.MA_HINH_THUC, SUM(HS.TIEN_DAU_GIA)/1000 TIEN_CN_CT_DG
               FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=0
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  OOO ON  OOO.MA_HINH_THUC=HT.MA_HINH_THUC
        --------------------------------------------
        -----KHONG THONG QUA DAU GIA
        -- THONG QUA DAU GIA
        ------
        
        LEFT JOIN
              (--CHO_THUE=CT, TO CHUC=0 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_TC_CT_KDG
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=1
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  KAA ON  AA.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--CHO_THUE=CT, CA NHAN=1 DAU GIA=0
                SELECT  HS.MA_HINH_THUC, COUNT(*) SL_CN_CT_KDG
               FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=1
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  KNN ON  NN.MA_HINH_THUC=HT.MA_HINH_THUC  
        --TINH DT DAU GIA

 
        LEFT JOIN
              (--CHO_THUE_DAT=CT, TO_CHUC=0, DAU GIA=0
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_TC_CT_KDG
                 FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=0
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=1
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  DDDD ON  DDDD.MA_HINH_THUC=HT.MA_HINH_THUC 
        LEFT JOIN
              (--CHO_THUE_DAT=CT, CA_NHAN=1, DAU GIA
                SELECT  HS.MA_HINH_THUC, SUM(HS.DIEN_TICH_SD)/1000 DT_CN_CT_KDG
                FROM DC_HO_SO_DAT HS,DC_LUY_TIEN A
                WHERE HS.THUOC_TO_CHUC=1
                AND A.MA_THUA=HS.MA_THUA
                AND A.SU_DUNG=0
                AND HS.MA_HINH_THUC ='SD_CT'
                AND HS.DAU_GIA=1
                AND TO_CHAR(A.TG_KT, 'YYYY')>= P_NAM
                AND HS.MA_QUOC_TICH='VN'
                GROUP BY HS.MA_HINH_THUC
              )  OOOO ON  OOOO.MA_HINH_THUC=HT.MA_HINH_THUC
              ------------------------------------------------      
        WHERE HT.MA_HINH_THUC='SD_CT'
        ORDER BY HT.MA_HINH_THUC;
  END;
   -------------------------------------------------------------------------------

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

   PROCEDURE REPORT_VPDD(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
       SELECT HT.TEN_HINH_THUC,H.SOTH,U.DTTH/1000 AS DIENTICH, '' GHI_CHU FROM DC_HINH_THUC HT
        LEFT JOIN
        (
             SELECT HSD.MA_HINH_THUC,COUNT(*) SOTH 
             FROM DC_HO_SO_DAT HSD,DC_LUY_TIEN A 
             WHERE HSD.MA_HINH_THUC LIKE 'TH%'
             AND A.MA_THUA=HSD.MA_THUA
             AND A.SU_DUNG =0
             AND  TO_CHAR(A.TG_KT, 'YYYY')> P_NAM
             GROUP BY HSD.MA_HINH_THUC     
        ) H ON H.MA_HINH_THUC=HT.MA_HINH_THUC
        LEFT JOIN
        (
             SELECT HSD.MA_HINH_THUC,SUM(HSD.DIEN_TICH_SD) DTTH 
             FROM DC_HO_SO_DAT HSD, DC_LUY_TIEN A 
             WHERE HSD.MA_HINH_THUC LIKE 'TH%'
             AND A.MA_THUA=HSD.MA_THUA
             AND A.SU_DUNG =0
             AND  TO_CHAR(A.TG_KT, 'YYYY')> P_NAM
             GROUP BY HSD.MA_HINH_THUC     
        ) U ON U.MA_HINH_THUC=HT.MA_HINH_THUC
        WHERE HT.MA_HINH_THUC LIKE 'VP_TH%'
        ORDER BY HT.MA_HINH_THUC;
  END;
   -------------------------------------------------------------------------------
   PROCEDURE REPORT_THUHOIDAT(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
        SELECT LD.TEN_LOAI_DAT,U.DTTH/1000 AS DIENTICH, '' GHI_CHU FROM DC_LOAI_DAT LD
        right JOIN
        (
             SELECT TD.MA_LOAI_DAT,SUM(HS.DIEN_TICH_SD) DTTH 
             FROM  DC_THUADAT TD,DC_HO_SO_DAT HS, DC_LUY_TIEN C
             WHERE HS.MA_THUA=TD.MA_THUA
             AND TD.MA_THUA=C.MA_THUA
             AND C.SU_DUNG=0
             AND  TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
             AND ( TD.MA_HINH_THUC LIKE 'VPK_%' OR TD.MA_HINH_THUC LIKE 'VPT_%')
             GROUP BY TD.MA_LOAI_DAT
                  
        ) U ON U.MA_LOAI_DAT=LD.MA_LOAI_DAT
        ORDER BY LD.MA_LOAI_DAT;
  END;
   -------------------------------------------------------------------------------
   -------------------------------------------------------------------------------
   PROCEDURE REPORT_CHUYENDICH(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
 SELECT H.TEN_QUAN_HUYEN,U.TONG1, LN.LN,CLN.CLN,RPH.RPH,RDD.RDD,RSX.RSX,NTTS.NTTS,LM.LM,
 N.TONG2,NB1.NB1,NB2.NB2,NB3.NB3,NB4.NB4,NB5.NB5,NB6.NB6
 FROM DM_QUAN_HUYEN H
        LEFT JOIN
        (
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) TONG1 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=1
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) U ON U.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (--DAT TRONG LUA NUOC=LN
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) LN 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=1
             AND A.MA_CD_MUC_DICH ='LN'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) LN ON LN.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (--DAT TRONG CAY LAU NAM=CLN
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) CLN 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=1
             AND A.MA_CD_MUC_DICH ='CLN'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) CLN ON CLN.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (--Đất rừng phòng hộ=RPH
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) RPH 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=1
             AND A.MA_CD_MUC_DICH ='RPH'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) RPH ON RPH.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (--Đất rừng đặc dụng=RDD
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) RDD 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=1
             AND A.MA_CD_MUC_DICH ='RDD'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) RDD ON RDD.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (-- 	Đất rừng sản xuất=RSX
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) RSX 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=1
             AND A.MA_CD_MUC_DICH ='RSX'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) RSX ON RSX.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        LEFT JOIN
        (-- 	Đất nuôi trồng thuỷ sản=NTTS
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) NTTS 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=1
             AND A.MA_CD_MUC_DICH ='NTTS'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) NTTS ON NTTS.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
             LEFT JOIN
        (-- 	Đất LAM MUOI=LM
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) LM 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=1
             AND A.MA_CD_MUC_DICH ='LM'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) LM ON LM.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        LEFT JOIN
        (
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) TONG2 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=0
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) N ON N.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        
        --------------------------------
        LEFT JOIN
        (--Đất chuyên trồng lúa nước chuyển sang đất trồng cây lâu năm NB1
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) NB1 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=0
             AND A.MA_CD_MUC_DICH ='NB1'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) NB1 ON NB1.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (--Đất chuyên trồng lúa nước chuyên sang đất lâm nghiệp 	NB2
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) NB2 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=0
             AND A.MA_CD_MUC_DICH ='NB2'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) NB2 ON NB2.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (--Đất chuyên trồng lúa nước chuyển sang đất NTTS NB3
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) NB3 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=0
             AND A.MA_CD_MUC_DICH ='NB3'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) NB3 ON NB3.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (--Đất rừng chuyên sản xuất chuyển sang đất SXNN, đất NTTS đất làm muối và đất NN khác NB4
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) NB4 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=0
             AND A.MA_CD_MUC_DICH ='NB4'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) NB4 ON NB4.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
         LEFT JOIN
        (-- 	Đất rừng đặc dụng chuyển sang đất SXNN, đất NTTS, đất làm muối và đất NN khác NB5
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) NB5 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=0
             AND A.MA_CD_MUC_DICH ='NB5'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) NB5 ON NB5.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        LEFT JOIN
        (-- 	NB6
             SELECT A.MA_QUAN_HUYEN,SUM(A.DIEN_TICH) NB6 
             FROM DC_QH_MUC_DICH_SD A, DC_CHUYEN_DICH_MUC_DICH_SD_DAT B
             WHERE TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM AND A.MA_CD_MUC_DICH=B.MA_CD_MUC_DICH
             AND B.LA_NOI_BO=0
             AND A.MA_CD_MUC_DICH ='NB6'
             GROUP BY A.MA_QUAN_HUYEN
                  
        ) NB6 ON NB6.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        WHERE H.MA_TINH_THANH LIKE 'TIEN_GIANG'
        ORDER BY H.MA_QUAN_HUYEN;
  END;
   -------------------------------------------------------------------------------
      -------------------------------------------------------------------------------
   PROCEDURE REPORT_DODAC(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
       
        SELECT H.TEN_QUAN_HUYEN,DTTN.DTTN/1000 as DTTTN,DTDD.DTDD/1000 as DTDDD,(DTDD.DTDD/1000)/200 as TL200 
        ,(DTDD.DTDD/1000)/500 as TL500,(DTDD.DTDD/1000)/1000 as TL1000,(DTDD.DTDD/1000)/2000 as TL2000,
        (DTDD.DTDD/1000)/5000 as TL5000,(DTDD.DTDD/1000)/10000 as TL10000, ((DTTN.DTTN/1000)/(DTDD.DTDD/1000))*100 AS TYLE,
        M.DADK,N.CHDK, O.TONGSO
        FROM DM_QUAN_HUYEN H
        LEFT JOIN
        (
             SELECT DH.MA_QUAN_HUYEN,SUM(DH.DIEN_TICH_TU_NHIEN) DTTN FROM DC_DAT_HUYEN DH--, DM_QUAN_HUYEN QH
             WHERE --DH.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN AND 
             TO_CHAR(DH.NAM_CAP_NHAT, 'YYYY')>= P_NAM
             GROUP BY DH.MA_QUAN_HUYEN
                  
        ) DTTN ON DTTN.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        LEFT JOIN
        (
             SELECT DH.MA_QUAN_HUYEN,SUM(DH.DIEN_TICH_DO_DAC) DTDD FROM DC_DAT_HUYEN DH--, DM_QUAN_HUYEN QH
             WHERE --DH.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN AND 
             TO_CHAR(DH.NAM_CAP_NHAT, 'YYYY')>= P_NAM
             GROUP BY DH.MA_QUAN_HUYEN
                  
        ) DTDD ON DTDD.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        LEFT JOIN
        (--xa da trien khai
               SELECT QH.MA_QUAN_HUYEN,COUNT(QHC.MA_PHUONG_XA) AS "DADK"
               FROM DC_QH_CSDL QHC,DM_PHUONG_XA PHX, DC_CSDL_DAT DLD, DM_QUAN_HUYEN QH 
               WHERE QHC.MA_CSDL_DAT LIKE 'BDDC'
               AND PHX.MA_PHUONG_XA=QHC.MA_PHUONG_XA
               AND QHC.MA_CSDL_DAT=DLD.MA_CSDL_DAT
               AND PHX.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
               AND TO_CHAR(QHC.NAMCAPNHAT, 'YYYY')<=P_NAM
               GROUP BY QH.MA_QUAN_HUYEN 
        ) M ON  M.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        
        LEFT JOIN
        (--xa da trien khai
               SELECT QH.MA_QUAN_HUYEN,COUNT(QHC.MA_PHUONG_XA) AS "CHDK"
               FROM DC_QH_CSDL QHC,DM_PHUONG_XA PHX, DC_CSDL_DAT DLD, DM_QUAN_HUYEN QH 
               WHERE QHC.MA_CSDL_DAT LIKE 'CBDDC'
               AND PHX.MA_PHUONG_XA=QHC.MA_PHUONG_XA
               AND QHC.MA_CSDL_DAT=DLD.MA_CSDL_DAT
               AND PHX.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
               AND TO_CHAR(QHC.NAMCAPNHAT, 'YYYY')<=P_NAM
               GROUP BY QH.MA_QUAN_HUYEN 
        ) N ON  N.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
 LEFT JOIN
        (--xa da trien khai
               SELECT QH.MA_QUAN_HUYEN,COUNT(QHC.MA_PHUONG_XA) AS "TONGSO"
               FROM DC_QH_CSDL QHC,DM_PHUONG_XA PHX, DC_CSDL_DAT DLD, DM_QUAN_HUYEN QH 
               WHERE PHX.MA_PHUONG_XA=QHC.MA_PHUONG_XA
               AND (QHC.MA_CSDL_DAT LIKE 'CBDDC' OR QHC.MA_CSDL_DAT LIKE 'BDDC')
               AND QHC.MA_CSDL_DAT=DLD.MA_CSDL_DAT
               AND PHX.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
               AND TO_CHAR(QHC.NAMCAPNHAT, 'YYYY')<=P_NAM
               GROUP BY QH.MA_QUAN_HUYEN 
        ) O ON  O.MA_QUAN_HUYEN=H.MA_QUAN_HUYEN
        WHERE H.MA_TINH_THANH='TIEN_GIANG'
        ORDER BY H.MA_QUAN_HUYEN;

  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_HO_SO_DAT(P_VAL   OUT NVARCHAR2, P_MA_HS_DAT NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    UPDATE DC_LUY_TIEN 
        SET NGUOI_DUNG=null,
        TG_BK=NULL,--to_date(P_TG_BD,'DDMMYYYY'),
        TG_KT=NULL,--to_date(P_TG_KT,'DDMMYYYY'),
        LA_THUOC_TO_CHUC=0,
        THOI_HAN=NULL,--P_THOI_HAN,
        MA_CAP_QUAN_LY =(SELECT MA_CAP_QUAN_LY FROM DC_CAP_QUAN_LY WHERE TRANG_THAI=-1)
        WHERE ma_luy_tien in (SELECT b.ma_luy_tien FROM Dc_Ho_So_Dat A, DC_LUY_TIEN B 
        WHERE A.MA_THUA=B.MA_THUA AND A.MA_HS_DAT =P_MA_HS_DAT AND B.SU_DUNG=0)
        AND SU_DUNG=0
        AND MA_THUA IN (SELECT MA_THUA FROM DC_HO_SO_DAT WHERE MA_HS_DAT=P_MA_HS_DAT);
        --SAU---UPDATE HS 
        UPDATE DC_THUADAT
        SET MA_MUC_DICH_SU_DUNG_DAT=(SELECT MA_MUC_DICH_SU_DUNG_DAT FROM DC_MUC_DICH_SU_DUNG_DAT  WHERE LOAI_MUC_DICH_MUC_DICH=-1),
        MA_HINH_THUC=(SELECT MA_HINH_THUC FROM DC_HINH_THUC WHERE TT_HINH_THUC=-1)
        WHERE MA_THUA IN (SELECT MA_THUA FROM DC_HO_SO_DAT WHERE MA_HS_DAT=P_MA_HS_DAT);
    DELETE FROM DC_SU_DUNG_DAT WHERE MA_HS_DAT LIKE P_MA_HS_DAT;
    DELETE FROM DC_VANBAN_SDD WHERE MA_HS_DAT LIKE P_MA_HS_DAT;
    DELETE FROM DC_HO_SO_DAT WHERE MA_HS_DAT LIKE P_MA_HS_DAT;
     
    P_VAL := 'THANH_CONG';
   END;
-----------------------------------------------------------------------------------
    PROCEDURE REPORT_12(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
                 
          
SELECT QH.MA_QUAN_HUYEN,QH.TEN_QUAN_HUYEN, STC.STC,SDT.SDT,(TC_DTH.TC_DTH+TC_DLHS.TC_DLHS+TC_DXL.TC_DXL+TC_VPK.TC_VPK)as T_DD
,(DT_DTH.DT_DTH+DT_DLHS.DT_DLHS+DT_DXL.DT_DXL+DT_VPK.DT_VPK)as S_DD,TC_DTH.TC_DTH,DT_DTH.DT_DTH,TC_DLHS.TC_DLHS,
DT_DLHS.DT_DLHS,TC_DXL.TC_DXL,DT_DXL.DT_DXL,TC_VPK.TC_VPK,DT_VPK.DT_VPK,
(TC_TC_TG.TC_TC_TG+TC_TC_TT.TC_TC_TT+TC_TC_TXPHC.TC_TC_TXPHC+TC_TC_TXLK.TC_TC_TXLK)as T_TC,
(TG.TG+TT.TT+TXPHC.TXPHC+TXLK.TXLK)as TT_TC,TC_TC_TG,
TG.TG,TC_TC_TT,TT.TT,TC_TC_TXPHC.TC_TC_TXPHC,TXPHC.TXPHC,TC_TC_TXLK.TC_TC_TXLK,TXLK.TXLK
FROM DM_QUAN_HUYEN QH
LEFT JOIN(
          SELECT COALESCE(COUNT(MA_HS_DAT),0) AS STC, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VP%' AND A.THUOC_TO_CHUC=0 
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )STC ON STC.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
LEFT JOIN(
          SELECT COALESCE(SUM(DIEN_TICH_SD),0)/1000 AS SDT, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C 
          WHERE MA_HINH_THUC LIKE 'VP%'AND A.THUOC_TO_CHUC=0
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )SDT ON SDT.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN 
LEFT JOIN(
          ---DA THU HOI---
          SELECT COALESCE(COUNT(MA_HS_DAT),0) AS TC_DTH, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VP%' AND A.THUOC_TO_CHUC=0 AND TRANG_THAI LIKE 'HT'
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TC_DTH ON TC_DTH.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN 
LEFT JOIN(
          --DIEN TICH
          SELECT COALESCE(SUM(DIEN_TICH_SD),0)/1000 AS DT_DTH, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VP%' AND A.THUOC_TO_CHUC=0 AND TRANG_THAI LIKE 'HT'
          AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
           )DT_DTH ON DT_DTH.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
------------------------------------------------------------------------------------------------------            
LEFT JOIN(
          ---DA LAP HO SO---
          SELECT COALESCE(COUNT(MA_HS_DAT),0) AS TC_DLHS, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VP%' AND A.THUOC_TO_CHUC=0 AND TRANG_THAI LIKE 'DLHS'
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TC_DLHS ON TC_DLHS.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN 
LEFT JOIN(          
--DIEN TICH
          SELECT COALESCE(SUM(DIEN_TICH_SD),0)/1000 AS DT_DLHS, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VP%' AND A.THUOC_TO_CHUC=0 AND TRANG_THAI LIKE 'DLHS'
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
           )DT_DLHS ON DT_DLHS.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN 
 LEFT JOIN(          
          ---------------------------------
          ---DA XU LY--
          SELECT COALESCE(COUNT(MA_HS_DAT),0) AS TC_DXL, MA_QUAN_HUYEN
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VP%' 
          AND A.THUOC_TO_CHUC=0 AND TRANG_THAI LIKE 'DXL'
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TC_DXL ON TC_DXL.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN 
 LEFT JOIN(  
          --DIEN TICH
          SELECT COALESCE(SUM(DIEN_TICH_SD),0)/1000 AS DT_DXL, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VP%' AND A.THUOC_TO_CHUC=0 
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TRANG_THAI LIKE 'DXL'
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
           )DT_DXL ON DT_DXL.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
---------------------------------
 LEFT JOIN(  
          ---VP KHAC--
          SELECT COALESCE(COUNT(MA_HS_DAT),0) AS TC_VPK, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VPK%' AND A.THUOC_TO_CHUC=0
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0 
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TC_VPK ON TC_VPK.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
 LEFT JOIN(
          --DIEN TICH
          SELECT COALESCE(SUM(DIEN_TICH_SD),0)/1000 AS DT_VPK, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN C
          WHERE MA_HINH_THUC LIKE 'VPK%' AND A.THUOC_TO_CHUC=0 
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )DT_VPK ON DT_VPK.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
 LEFT JOIN(          
          ----SOTC-----------------------------NGHIA VU TAI CHINH
          --GIAO
          SELECT COALESCE(COUNT(A.MA_HS_DAT),0) AS TC_TC_TG, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_SU_DUNG_DAT B, DC_LUY_TIEN C
          WHERE A.MA_HS_DAT=B.MA_HS_DAT AND A.THUOC_TO_CHUC=0 
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND B.MA_GIAO_DICH LIKE 'TGD%'
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
         )TC_TC_TG ON TC_TC_TG.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN          
 LEFT JOIN(          
          --S-------------------------------NGHIA VU TAI CHINH
          --GIAO
          SELECT COALESCE(SUM(B.THANH_TIEN_SDD),0)/1000 AS TG, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_SU_DUNG_DAT B, DC_LUY_TIEN C
          WHERE A.MA_HS_DAT=B.MA_HS_DAT AND A.THUOC_TO_CHUC=0 
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND B.MA_GIAO_DICH LIKE 'TGD%'
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
         )TG ON TG.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
 LEFT JOIN(           
          --TC THUE
          SELECT COALESCE(COUNT(A.MA_HS_DAT),0) AS TC_TC_TT, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_SU_DUNG_DAT B, DC_LUY_TIEN C
          WHERE A.MA_HS_DAT=B.MA_HS_DAT AND A.THUOC_TO_CHUC=0 
          AND B.MA_GIAO_DICH LIKE 'TTD%'
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TC_TC_TT ON TC_TC_TT.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN         
 LEFT JOIN(           
          --THUE
          SELECT COALESCE(SUM(B.THANH_TIEN_SDD),0)/1000 AS TT, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_SU_DUNG_DAT B, DC_LUY_TIEN C
          WHERE A.MA_HS_DAT=B.MA_HS_DAT AND A.THUOC_TO_CHUC=0 
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND B.MA_GIAO_DICH LIKE 'TTD%'
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TT ON TT.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
 LEFT JOIN(--TC_PHAT HC
          SELECT COALESCE(COUNT(A.MA_HS_DAT),0) AS TC_TC_TXPHC, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_SU_DUNG_DAT B, DC_LUY_TIEN C
          WHERE A.MA_HS_DAT=B.MA_HS_DAT AND A.THUOC_TO_CHUC=0
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0 
          AND B.MA_GIAO_DICH LIKE 'XPHC%'
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TC_TC_TXPHC ON TC_TC_TXPHC.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN          
 LEFT JOIN(--S_PHAT HC
          SELECT COALESCE(SUM(B.THANH_TIEN_SDD),0)/1000 AS TXPHC, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_SU_DUNG_DAT B, DC_LUY_TIEN C
          WHERE A.MA_HS_DAT=B.MA_HS_DAT AND A.THUOC_TO_CHUC=0 
           AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND B.MA_GIAO_DICH LIKE 'XPHC%'
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TXPHC ON TXPHC.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
 LEFT JOIN(          
          --TC TIEN XL KHAC
          SELECT COALESCE(COUNT(A.MA_HS_DAT),0) AS TC_TC_TXLK, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_SU_DUNG_DAT B, DC_LUY_TIEN C
          WHERE A.MA_HS_DAT=B.MA_HS_DAT AND A.THUOC_TO_CHUC=0 
          AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND B.MA_GIAO_DICH LIKE 'TXLK%'
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TC_TC_TXLK ON TC_TC_TXLK.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
 LEFT JOIN(          
          --S TIEN XL KHAC
          SELECT COALESCE(SUM(B.THANH_TIEN_SDD),0)/1000 AS TXLK, MA_QUAN_HUYEN 
          FROM DC_HO_SO_DAT A, DC_SU_DUNG_DAT B, DC_LUY_TIEN C
          WHERE A.MA_HS_DAT=B.MA_HS_DAT 
          AND A.MA_THUA=C.MA_THUA AND C.SU_DUNG=0
          AND A.THUOC_TO_CHUC=0 
          AND B.MA_GIAO_DICH LIKE 'TXLK%'
          AND TO_CHAR(C.TG_KT, 'YYYY')>= P_NAM
          GROUP BY MA_QUAN_HUYEN
          )TXLK ON TXLK.MA_QUAN_HUYEN=QH.MA_QUAN_HUYEN
          WHERE QH.MA_TINH_THANH='TIEN_GIANG'  ;        

    END;
-------------------------------------------
    PROCEDURE REPORT_3(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
          SELECT MA_CD_DAT,A.MA_QUAN_HUYEN,A.MA_LOAI_DAT,A.NAM_CAP_NHAT,
          A.C_THUA_DAT_KK,A.C_THUA_DAT_KK_LT,A.S_THUA_DAT_KK,A.S_THUA_DAT_KK_LT,
          A.C_THUA_DAT_XD,A.C_THUA_DAT_XD_LT,A.S_THUA_DAT_XD,A.S_THUA_DAT_XD_LT,A.GHI_CHU_B3, B.TEN_LOAI_DAT 
          FROM DC_CHUYEN_DICH_LOAI_DAT A, DC_LOAI_DAT B
          WHERE A.MA_LOAI_DAT=B.MA_LOAI_DAT
          AND TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM;
    END;
-------------------------------------------
    PROCEDURE REPORT_2(P_CUR  IN OUT MYCUR, P_NAM NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
          SELECT A.*,B.TEN_LOAI_DAT FROM DC_CHUYEN_DICH_LOAI_DAT A, DC_LOAI_DAT B
          WHERE A.MA_LOAI_DAT=B.MA_LOAI_DAT
          AND TO_CHAR(A.NAM_CAP_NHAT, 'YYYY')= P_NAM;
    END;
------------------------------------------------------------------------------------------------
  PROCEDURE REPORT_DAU_GIA(P_CUR  IN OUT MYCUR
    --, P_NAM NVARCHAR2
    ) AS
  BEGIN
    OPEN P_CUR FOR
          SELECT TEN_QUAN_HUYEN, DON_VI_TO_CHUC_DAU_GIA,TEN_LOAI_DAT,CASE
               WHEN LA_DAT_NONG_NGHIEP = 0 THEN
                'X'
               ELSE
                ''
             END AS DAT_NONG_NGHIEP,
			CASE
               WHEN LA_DAT_NONG_NGHIEP = 1 THEN
                'X'
               ELSE
                ''
             END AS DAT_PHI_NONG_NGHIEP,
			 B.DIEN_TICH_THUA, TEN_HINH_THUC,
			 CASE
               WHEN A.PHUONG_THUC_THANH_TOAN = 'ML' THEN
                'X'
               ELSE
                ''
             END AS MOT_LAN,
			CASE
               WHEN A.PHUONG_THUC_THANH_TOAN = 'TN' THEN
                'X'
               ELSE
                ''
             END AS TUNG_NAM,
			 B.THOI_HAN,
			  CASE
               WHEN A.THUOC_TO_CHUC = 0 THEN
                'X'
               ELSE
                ''
             END AS TO_CHUC,
			 CASE
               WHEN A.THUOC_TO_CHUC = 1 THEN
                'X'
               ELSE
                ''
             END AS CA_NHAN,
			 TIEN_DAU_GIA
		  FROM DC_HO_SO_DAT A, DC_LUY_TIEN B, DM_QUAN_HUYEN C, DC_LOAI_DAT D, DC_THUADAT E,  DC_HINH_THUC F
          WHERE A.MA_THUA=B.MA_THUA
		  AND A.MA_QUAN_HUYEN=C.MA_QUAN_HUYEN
      AND A.MA_HINH_THUC=F.MA_HINH_THUC
		  AND B.MA_THUA=E.MA_THUA
		  AND A.MA_THUA=E.MA_THUA
		  AND B.SU_DUNG=0
      AND TT_LOAI_DAT=0
      AND DAU_GIA=0
      --AND D.MA_LOAI_DAT LIKE 'D_O_%'
		  AND D.MA_LOAI_DAT=E.MA_LOAI_DAT
        --  AND B.NAM_CAP_PHAT>to_date(concat('0101',P_NAM),'DDMMYYYY')
        --  AND B.NAM_CAP_PHAT<=to_date(concat('3006',P_NAM),'DDMMYYYY')
        ;
    END;
------------------------------------------------------------------------------------------------
PROCEDURE REPORT_GIAO_THUE_TC(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
       SELECT DISTINCT TEN_DON_VI,TEN_HINH_THUC, 
	   CASE
               WHEN A.PHUONG_THUC_THANH_TOAN = 'TN' THEN
                'Từng năm'
               WHEN A.PHUONG_THUC_THANH_TOAN = 'ML' THEN
				'Một lần'
			   ELSE
                '5 năm'
             END AS THANH_TOAN,
			 CONCAT( CONCAT(CONCAT(CONCAT(CONCAT('Số tờ: ',TO_BAN_DO),' Số thửa: '),SO_THUA),' Địa chỉ: '),VI_TRI) AS TONG_HOP,
			 DIEN_TICH_THUA,TEN_MUC_DICH_SU_DUNG_DAT, THOI_HAN,GHI_CHU
	   FROM DC_HO_SO_DAT A, DC_LUY_TIEN B, DC_HINH_THUC C, 
     DC_MUC_DICH_SU_DUNG_DAT D, dc_thuadat e 
	   WHERE A.MA_THUA=B.MA_THUA
     AND D.MA_MUC_DICH_SU_DUNG_DAT=A.MA_MUC_DICH_SU_DUNG_DAT
     AND A.MA_THUA=E.MA_THUA 
     AND B.MA_THUA=E.MA_THUA
	    AND SU_DUNG=0 AND A.TRANG_THAI NOT IN ('KSD')
     AND A.MA_HINH_THUC=C.MA_HINH_THUC
     AND A.MA_HINH_THUC IN('SD_GD','SD_CT')
	   AND THUOC_TO_CHUC=0
     
 
	   ;
    END;
------------------------------------------------------------------------------------------------
PROCEDURE REPORT_GIAO_THUE_CN(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
       SELECT DISTINCT TEN_DON_VI,TEN_HINH_THUC, 
	   CASE
               WHEN A.PHUONG_THUC_THANH_TOAN = 'TN' THEN
                'Từng năm'
               WHEN A.PHUONG_THUC_THANH_TOAN = 'ML' THEN
				'Một lần'
			   ELSE
                '5 năm'
             END AS THANH_TOAN,
			 CONCAT( CONCAT(CONCAT(CONCAT(CONCAT('Số tờ: ',TO_BAN_DO),' Số thửa: '),SO_THUA),' Địa chỉ: '),VI_TRI) AS TONG_HOP,
			 DIEN_TICH_THUA,TEN_MUC_DICH_SU_DUNG_DAT, THOI_HAN,GHI_CHU
	   FROM DC_HO_SO_DAT A, DC_LUY_TIEN B, DC_HINH_THUC C, DC_MUC_DICH_SU_DUNG_DAT D, dc_thuadat e 
	   WHERE A.MA_THUA=B.MA_THUA
     AND A.MA_THUA=E.MA_THUA AND B.MA_THUA=E.MA_THUA
     AND D.MA_MUC_DICH_SU_DUNG_DAT=A.MA_MUC_DICH_SU_DUNG_DAT
	   AND SU_DUNG=0 AND A.TRANG_THAI NOT IN ('KSD')
     AND A.MA_HINH_THUC=C.MA_HINH_THUC
     AND A.MA_HINH_THUC IN('SD_GD','SD_CT')
	   AND THUOC_TO_CHUC=1
	   ;
    END;
------------------------------------------------------------------------------------------------
PROCEDURE REPORT_5B(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
       SELECT SO_THUA,TEN_DON_VI,F.MA as MA_DT,VI_TRI,B.DIEN_TICH_THUA,G.MA as MA_LD,D.MA as MA_QD
	   FROM DC_HO_SO_DAT A, DC_LUY_TIEN B, DC_MUC_DICH_SU_DUNG_DAT D, DC_THUADAT E,DC_DOI_TUONG F, DC_LOAI_DAT G 
	   WHERE A.MA_THUA=B.MA_THUA
     AND A.MA_THUA=E.MA_THUA AND B.MA_THUA=E.MA_THUA
	   AND SU_DUNG=0 AND A.TRANG_THAI<>'KSD'
	   AND A.MA_DOI_TUONG=F.MA_DOI_TUONG
	   AND E.MA_LOAI_DAT=G.MA_LOAI_DAT
	   
	   AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT
	   AND (A.MA_HINH_THUC='SD_GD' OR  A.MA_HINH_THUC='SD_CT')
	   AND D.CHUYEN_MUC_DICH='CTH'
	   ;
    END;
------------------------------------------------------------------------------------------------
PROCEDURE REPORT_1(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
         SELECT LD.MA_LOAI_DAT,LD.TEN_LOAI_DAT,LD.MA,GDC.GDC,TKT.TKT,
            TCN.TCN,TSN.TSN,TKH.TKH,TVN.TVN,TNG.TNG,CNN.CNN,CDS.CDS,UBQ.UBQ,TPQ.TPQ,TKQ.TKQ
            FROM DC_LOAI_DAT LD
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS GDC FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG=11--GDC
            --AND  TO_CHAR(I.TG_KT, 'YYYY') >=TO_CHAR(sysdate, 'YYYY') 
            GROUP BY  MA_LOAI_DAT
            )GDC ON GDC.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKT FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG =5--TKT
            GROUP BY  MA_LOAI_DAT
            )TKT ON TKT.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TCN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG =6--TCN
            GROUP BY  MA_LOAI_DAT
            )TCN ON TCN.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TSN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG =7--TSN
            GROUP BY  MA_LOAI_DAT
            )TSN ON TSN.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKH FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG =8--TKH
            GROUP BY  MA_LOAI_DAT
            )TKH ON TKH.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TVN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG =9--TVN
            GROUP BY  MA_LOAI_DAT
            )TVN ON TVN.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TNG FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG =10--TNG
            GROUP BY  MA_LOAI_DAT
            )TNG ON TNG.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS CNN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG =11--CNN
            GROUP BY  MA_LOAI_DAT
            )CNN ON CNN.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS CDS FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
            WHERE A.MA_THUA=B.MA_THUA
            AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
            AND C.MA_DOI_TUONG =12--CDS
            GROUP BY  MA_LOAI_DAT
            )CDS ON CDS.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS UBQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
            WHERE  A.MA_THUA=D.MA_THUA
            AND B.MA_THUA=D.MA_THUA
            AND SU_DUNG=0
            AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
            AND C.MA_CAP_QUAN_LY=21
            GROUP BY  MA_LOAI_DAT
            )UBQ ON UBQ.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TPQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
            WHERE  A.MA_THUA=D.MA_THUA
            AND B.MA_THUA=D.MA_THUA
            AND SU_DUNG=0
            AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
            AND C.MA_CAP_QUAN_LY=22
            GROUP BY  MA_LOAI_DAT
            )TPQ ON TPQ.MA_LOAI_DAT=LD.MA_LOAI_DAT
            LEFT OUTER JOIN(
            SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
            WHERE  A.MA_THUA=D.MA_THUA
            AND B.MA_THUA=D.MA_THUA
            AND SU_DUNG=0
            AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
            AND C.MA_CAP_QUAN_LY=23
            GROUP BY  MA_LOAI_DAT
            )TKQ ON TKQ.MA_LOAI_DAT=LD.MA_LOAI_DAT ;
            --select * from dc_CAP_QUAN_LY;
    END;
------------------------------------------------------------------------------------------------
PROCEDURE REPORT_2N(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
         SELECT LD.MA_LOAI_DAT,LD.TEN_LOAI_DAT,LD.MA,GDC.GDC,TKT.TKT,
          TCN.TCN,TSN.TSN,TVN.TVN,TNG.TNG,CNN.CNN,CDS.CDS,UBQ.UBQ,TPQ.TPQ,TKQ.TKQ
          FROM DC_LOAI_DAT LD
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS GDC FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG=11--GDC
          --AND  TO_CHAR(I.TG_KT, 'YYYY') >=TO_CHAR(sysdate, 'YYYY') 
          GROUP BY  MA_LOAI_DAT
          )GDC ON GDC.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKT FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =5--TKT
          GROUP BY  MA_LOAI_DAT
          )TKT ON TKT.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TCN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =6--TCN
          GROUP BY  MA_LOAI_DAT
          )TCN ON TCN.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TSN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =7--TSN
          GROUP BY  MA_LOAI_DAT
          )TSN ON TSN.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKH FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =8--TKH
          GROUP BY  MA_LOAI_DAT
          )TKH ON TKH.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TVN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =9--TVN
          GROUP BY  MA_LOAI_DAT
          )TVN ON TVN.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TNG FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =10--TNG
          GROUP BY  MA_LOAI_DAT
          )TNG ON TNG.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS CNN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =11--CNN
          GROUP BY  MA_LOAI_DAT
          )CNN ON CNN.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS CDS FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =12--CDS
          GROUP BY  MA_LOAI_DAT
          )CDS ON CDS.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS UBQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
          WHERE  A.MA_THUA=D.MA_THUA
          AND B.MA_THUA=D.MA_THUA
          AND SU_DUNG=0
          AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
          AND C.MA_CAP_QUAN_LY=21
          GROUP BY  MA_LOAI_DAT
          )UBQ ON UBQ.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TPQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
          WHERE  A.MA_THUA=D.MA_THUA
          AND B.MA_THUA=D.MA_THUA
          AND SU_DUNG=0
          AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
          AND C.MA_CAP_QUAN_LY=22
          GROUP BY  MA_LOAI_DAT
          )TPQ ON TPQ.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
          WHERE  A.MA_THUA=D.MA_THUA
          AND B.MA_THUA=D.MA_THUA
          AND SU_DUNG=0
          AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
          AND C.MA_CAP_QUAN_LY=23
          GROUP BY  MA_LOAI_DAT
          )TKQ ON TKQ.MA_LOAI_DAT=LD.MA_LOAI_DAT 
          WHERE LD.LA_DAT_NONG_NGHIEP=0;
          --select * from dc_CAP_QUAN_LY;


END;
------------------------------------------------------------------------------------------------
PROCEDURE REPORT_3N(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
         SELECT LD.MA_LOAI_DAT,LD.TEN_LOAI_DAT,LD.MA,GDC.GDC,TKT.TKT,
          TCN.TCN,TSN.TSN,TVN.TVN,TNG.TNG,CNN.CNN,CDS.CDS,UBQ.UBQ,TPQ.TPQ,TKQ.TKQ
          FROM DC_LOAI_DAT LD
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS GDC FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG=11--GDC
          --AND  TO_CHAR(I.TG_KT, 'YYYY') >=TO_CHAR(sysdate, 'YYYY') 
          GROUP BY  MA_LOAI_DAT
          )GDC ON GDC.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKT FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =5--TKT
          GROUP BY  MA_LOAI_DAT
          )TKT ON TKT.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TCN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =6--TCN
          GROUP BY  MA_LOAI_DAT
          )TCN ON TCN.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TSN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =7--TSN
          GROUP BY  MA_LOAI_DAT
          )TSN ON TSN.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKH FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =8--TKH
          GROUP BY  MA_LOAI_DAT
          )TKH ON TKH.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TVN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =9--TVN
          GROUP BY  MA_LOAI_DAT
          )TVN ON TVN.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TNG FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =10--TNG
          GROUP BY  MA_LOAI_DAT
          )TNG ON TNG.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS CNN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =11--CNN
          GROUP BY  MA_LOAI_DAT
          )CNN ON CNN.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS CDS FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
          WHERE A.MA_THUA=B.MA_THUA
          AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
          AND C.MA_DOI_TUONG =12--CDS
          GROUP BY  MA_LOAI_DAT
          )CDS ON CDS.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS UBQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
          WHERE  A.MA_THUA=D.MA_THUA
          AND B.MA_THUA=D.MA_THUA
          AND SU_DUNG=0
          AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
          AND C.MA_CAP_QUAN_LY=21
          GROUP BY  MA_LOAI_DAT
          )UBQ ON UBQ.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TPQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
          WHERE  A.MA_THUA=D.MA_THUA
          AND B.MA_THUA=D.MA_THUA
          AND SU_DUNG=0
          AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
          AND C.MA_CAP_QUAN_LY=22
          GROUP BY  MA_LOAI_DAT
          )TPQ ON TPQ.MA_LOAI_DAT=LD.MA_LOAI_DAT
          LEFT OUTER JOIN(
          SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKQ FROM DC_HO_SO_DAT A,DC_LUY_TIEN B, DC_CAP_QUAN_LY  C, DC_THUADAT D
          WHERE  A.MA_THUA=D.MA_THUA
          AND B.MA_THUA=D.MA_THUA
          AND SU_DUNG=0
          AND B.MA_CAP_QUAN_LY=C.MA_CAP_QUAN_LY
          AND C.MA_CAP_QUAN_LY=23
          GROUP BY  MA_LOAI_DAT
          )TKQ ON TKQ.MA_LOAI_DAT=LD.MA_LOAI_DAT 
          WHERE LD.LA_DAT_NONG_NGHIEP=1;
          --select * from dc_CAP_QUAN_LY;


END;
------------------------------------------------------------------------------------------------
PROCEDURE REPORT_5A(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
         SELECT LD.MA_LOAI_DAT,LD.TEN_LOAI_DAT,LD.MA,GDC.GDC,TKT.TKT,
TCN.TCN,TSN.TSN,TKH.TKH,TVN.TVN,TNG.TNG,CNN.CNN,CDS.CDS
FROM DC_LOAI_DAT LD
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS GDC FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG=11--GDC
--AND  TO_CHAR(I.TG_KT, 'YYYY') >=TO_CHAR(sysdate, 'YYYY') 
GROUP BY  MA_LOAI_DAT
)GDC ON GDC.MA_LOAI_DAT=LD.MA_LOAI_DAT
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKT FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG =5--TKT
GROUP BY  MA_LOAI_DAT
)TKT ON TKT.MA_LOAI_DAT=LD.MA_LOAI_DAT
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TCN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG =6--TCN
GROUP BY  MA_LOAI_DAT
)TCN ON TCN.MA_LOAI_DAT=LD.MA_LOAI_DAT
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TSN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG =7--TSN
GROUP BY  MA_LOAI_DAT
)TSN ON TSN.MA_LOAI_DAT=LD.MA_LOAI_DAT
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TKH FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG =8--TKH
GROUP BY  MA_LOAI_DAT
)TKH ON TKH.MA_LOAI_DAT=LD.MA_LOAI_DAT
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TVN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG =9--TVN
GROUP BY  MA_LOAI_DAT
)TVN ON TVN.MA_LOAI_DAT=LD.MA_LOAI_DAT
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS TNG FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG =10--TNG
GROUP BY  MA_LOAI_DAT
)TNG ON TNG.MA_LOAI_DAT=LD.MA_LOAI_DAT
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS CNN FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG =11--CNN
GROUP BY  MA_LOAI_DAT
)CNN ON CNN.MA_LOAI_DAT=LD.MA_LOAI_DAT
LEFT OUTER JOIN(
SELECT MA_LOAI_DAT,SUM( A.DIEN_TICH_SD)AS CDS FROM DC_HO_SO_DAT A,DC_THUADAT B, DC_DOI_TUONG  C
, DC_MUC_DICH_SU_DUNG_DAT D
WHERE A.MA_THUA=B.MA_THUA AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT AND D.CHUYEN_MUC_DICH='CTH'
AND A.MA_DOI_TUONG=C.MA_DOI_TUONG
AND C.MA_DOI_TUONG =12--CDS
GROUP BY  MA_LOAI_DAT
)CDS ON CDS.MA_LOAI_DAT=LD.MA_LOAI_DAT;
--WHERE LD.LA_DAT_NONG_NGHIEP=0;
--select * from dc_CAP_QUAN_LY;

    END;
------------------------------------------------------------------------------------------------
end TNMT_DATCONG;
/

prompt
prompt Creating package body TNMT_DATCONG_DM
prompt =====================================
prompt
create or replace package body hai.TNMT_DATCONG_DM is
 PROCEDURE SELECT_HINH_THUC_DM(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_HINH_THUC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_HINH_THUC A
        WHERE TT_HINH_THUC<>-1
       ORDER BY A.STT_HINH_THUC , A.TEN_HINH_THUC;
  END;
------------------------------DC_HINH_THUC------------------------------------------
PROCEDURE SELECT_HINH_THUC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_HINH_THUC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_HINH_THUC A
       ORDER BY A.STT_HINH_THUC , A.TEN_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.TT_HINH_THUC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_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_TT_HINH_THUC     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 DC_HINH_THUC
       WHERE UPPER(MA_HINH_THUC) LIKE UPPER(P_MA_HINH_THUC);
      SELECT COUNT(MA_HINH_THUC)
        INTO V_TON_TAI_TEN
        FROM DC_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 DC_HINH_THUC
          (MA_HINH_THUC,
           TEN_HINH_THUC,
           STT_HINH_THUC,
           TT_HINH_THUC)
        VALUES
          (P_MA_HINH_THUC,
           P_TEN_HINH_THUC,
           P_STT_HINH_THUC,
           P_TT_HINH_THUC);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_HINH_THUC)
        INTO V_TON_TAI_TEN
        FROM DC_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 DC_HINH_THUC
           SET TEN_HINH_THUC    = P_TEN_HINH_THUC,
               STT_HINH_THUC    = P_STT_HINH_THUC,
               TT_HINH_THUC     = P_TT_HINH_THUC
         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 DC_HINH_THUC WHERE MA_HINH_THUC LIKE P_MA_HINH_THUC;
    P_VAL := 'THANH_CONG';
   END;  
 ------------------------------DC_LOAI_VAN_BAN------------------------------------------
PROCEDURE SELECT_LOAI_VAN_BAN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_VAN_BAN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_LOAI_VAN_BAN A
       ORDER BY A.STT_LOAI_VAN_BAN , A.TEN_LOAI_VAN_BAN;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_LOAI_VAN_BAN_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_LOAI_VAN_BAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_VAN_BAN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_LOAI_VAN_BAN A
       WHERE A.MA_LOAI_VAN_BAN LIKE P_MA_LOAI_VAN_BAN;
  END;
-------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_LOAI_VAN_BAN(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LOAI_VAN_BAN     NVARCHAR2,
                           P_TEN_LOAI_VAN_BAN    NVARCHAR2,
                           P_STT_LOAI_VAN_BAN    INT,
                           P_TT_LOAI_VAN_BAN     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_VAN_BAN)
        INTO V_TON_TAI_MA
        FROM DC_LOAI_VAN_BAN
       WHERE UPPER(MA_LOAI_VAN_BAN) LIKE UPPER(P_MA_LOAI_VAN_BAN);
      SELECT COUNT(MA_LOAI_VAN_BAN)
        INTO V_TON_TAI_TEN
        FROM DC_LOAI_VAN_BAN
       WHERE UPPER(TEN_LOAI_VAN_BAN) LIKE UPPER(P_TEN_LOAI_VAN_BAN);
      IF V_TON_TAI_MA > 0 OR P_MA_LOAI_VAN_BAN = '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 DC_LOAI_VAN_BAN
          (MA_LOAI_VAN_BAN,
           TEN_LOAI_VAN_BAN,
           STT_LOAI_VAN_BAN,
           TT_LOAI_VAN_BAN)
        VALUES
          (P_MA_LOAI_VAN_BAN,
           P_TEN_LOAI_VAN_BAN,
           P_STT_LOAI_VAN_BAN,
           P_TT_LOAI_VAN_BAN);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_LOAI_VAN_BAN)
        INTO V_TON_TAI_TEN
        FROM DC_LOAI_VAN_BAN
       WHERE UPPER(TEN_LOAI_VAN_BAN) LIKE UPPER(P_TEN_LOAI_VAN_BAN)
         AND MA_LOAI_VAN_BAN NOT LIKE P_MA_LOAI_VAN_BAN;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_LOAI_VAN_BAN
           SET TEN_LOAI_VAN_BAN    = P_TEN_LOAI_VAN_BAN,
               STT_LOAI_VAN_BAN    = P_STT_LOAI_VAN_BAN,
               TT_LOAI_VAN_BAN     = P_TT_LOAI_VAN_BAN
         WHERE MA_LOAI_VAN_BAN LIKE P_MA_LOAI_VAN_BAN;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_VAN_BAN(P_VAL   OUT NVARCHAR2, P_MA_LOAI_VAN_BAN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_LOAI_VAN_BAN WHERE MA_LOAI_VAN_BAN LIKE P_MA_LOAI_VAN_BAN;
    P_VAL := 'THANH_CONG';
   END;  
 ------------------------------DC_LOAI_DAT------------------------------------------
PROCEDURE SELECT_LOAI_DAT(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,CASE
               WHEN A.LA_DAT_NONG_NGHIEP = 0 THEN
                'Đất nông nghiệp'
               ELSE
                'Đất phi nông nghiệp'
             END AS DAT_NONG_NGHIEP,
             CASE
               WHEN A.TT_LOAI_DAT = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_LOAI_DAT A
       ORDER BY A.STT_LOAI_DAT , A.TEN_LOAI_DAT;
  END;
-------------------------------------------------------------------------------
     PROCEDURE SELECT_LOAI_DAT_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_LOAI_DAT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,CASE
               WHEN A.LA_DAT_NONG_NGHIEP = 0 THEN
                 'Đất nông nghiệp'
               ELSE
                'Đất phi nông nghiệp'
             END AS DAT_NONG_NGHIEP,
             CASE
               WHEN A.TT_LOAI_DAT = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_LOAI_DAT A
       WHERE A.MA_LOAI_DAT LIKE P_MA_LOAI_DAT;
  END;
    -------------------------------------------------------------------------------
 PROCEDURE UPDATE_LOAI_DAT(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LOAI_DAT     number,
                           P_TEN_LOAI_DAT    NVARCHAR2,
                           P_DAT_NONG_NGHIEP    INT,
                           P_STT_LOAI_DAT    INT,
                           P_TT_LOAI_DAT     INT,
                           P_MA    NVARCHAR2) AS

  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
    
        INSERT INTO DC_LOAI_DAT
          (MA_LOAI_DAT,
           TEN_LOAI_DAT,
           LA_DAT_NONG_NGHIEP,
           STT_LOAI_DAT,
           TT_LOAI_DAT,
           MA)
        VALUES
          (DC_LOAI_DAT_SEQ.NEXTVAL,
           P_TEN_LOAI_DAT,
           P_DAT_NONG_NGHIEP,
           P_STT_LOAI_DAT,
           P_TT_LOAI_DAT,
           P_MA);
        P_VAL := 'THANH_CONG';
     
    ELSE
        UPDATE DC_LOAI_DAT
           SET TEN_LOAI_DAT    = P_TEN_LOAI_DAT,
           LA_DAT_NONG_NGHIEP  = P_DAT_NONG_NGHIEP,
               STT_LOAI_DAT    = P_STT_LOAI_DAT,
               TT_LOAI_DAT     = P_TT_LOAI_DAT,
               MA=P_MA
         WHERE MA_LOAI_DAT LIKE P_MA_LOAI_DAT;
        P_VAL := 'THANH_CONG';
     
    END IF;
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_DAT(P_VAL   OUT NVARCHAR2, P_MA_LOAI_DAT NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_LOAI_DAT WHERE MA_LOAI_DAT LIKE P_MA_LOAI_DAT;
    P_VAL := 'THANH_CONG';
   END;
 
------------------------------DC_MUC_DICH_SU_DUNG_DAT------------------------------------------
PROCEDURE SELECT_MUC_DICH_SU_DUNG_DAT(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,CONCAT('(',CONCAT(CONCAT(CHUYEN_MUC_DICH ,')-'),ten_muc_dich_su_dung_dat)) as ten_muc_dich_su_dung_dat1,CASE
               WHEN A.LOAI_MUC_DICH_MUC_DICH = 0 THEN
                'Giao, thuê'
               ELSE
                'Bồi thường, nộp phạt, thu hồi'
             END AS LOAI,
             CASE
               WHEN A.TT_MUC_DICH_SU_DUNG_DAT = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_MUC_DICH_SU_DUNG_DAT A
       ORDER BY A.STT_MUC_DICH_SU_DUNG_DAT , A.TEN_MUC_DICH_SU_DUNG_DAT;
  END;
-------------------------------------------------------------------------------
     PROCEDURE SELECT_MUC_DICH_SD_DAT_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_MUC_DICH_SU_DUNG_DAT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,CASE
               WHEN A.LOAI_MUC_DICH_MUC_DICH = 0 THEN
                'Giao, thuê'
               ELSE
                'Bồi thường, nộp phạt, thu hồi'
             END AS LOAI,
             CASE
               WHEN A.TT_MUC_DICH_SU_DUNG_DAT = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_MUC_DICH_SU_DUNG_DAT A
       WHERE A.MA_MUC_DICH_SU_DUNG_DAT LIKE P_MA_MUC_DICH_SU_DUNG_DAT;
  END;
    -------------------------------------------------------------------------------
PROCEDURE UPDATE_MUC_DICH_SU_DUNG_DAT(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_MUC_DICH_SU_DUNG_DAT     NVARCHAR2,
                           P_TEN_MUC_DICH_SU_DUNG_DAT    NVARCHAR2,
                           P_LOAI_MUC_DICH_MUC_DICH    INT,
                           P_STT_MUC_DICH_SU_DUNG_DAT    INT,
                           P_TT_MUC_DICH_SU_DUNG_DAT     INT,
                           P_CHUYEN_MUC_DICH    NVARCHAR2,
                           P_MA    NVARCHAR2) 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_MUC_DICH_SU_DUNG_DAT)
        INTO V_TON_TAI_MA
        FROM DC_MUC_DICH_SU_DUNG_DAT
       WHERE UPPER(MA_MUC_DICH_SU_DUNG_DAT) LIKE UPPER(P_MA_MUC_DICH_SU_DUNG_DAT);
      SELECT COUNT(MA_MUC_DICH_SU_DUNG_DAT)
        INTO V_TON_TAI_TEN
        FROM DC_MUC_DICH_SU_DUNG_DAT
       WHERE UPPER(TEN_MUC_DICH_SU_DUNG_DAT) LIKE UPPER(P_TEN_MUC_DICH_SU_DUNG_DAT);
      IF V_TON_TAI_MA > 0 OR P_MA_MUC_DICH_SU_DUNG_DAT = '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 DC_MUC_DICH_SU_DUNG_DAT
          (MA_MUC_DICH_SU_DUNG_DAT,
           TEN_MUC_DICH_SU_DUNG_DAT,
           LOAI_MUC_DICH_MUC_DICH,
           STT_MUC_DICH_SU_DUNG_DAT,
           TT_MUC_DICH_SU_DUNG_DAT,
           CHUYEN_MUC_DICH,
           MA)
        VALUES
          (P_MA_MUC_DICH_SU_DUNG_DAT,
           P_TEN_MUC_DICH_SU_DUNG_DAT,
           P_LOAI_MUC_DICH_MUC_DICH,
           P_STT_MUC_DICH_SU_DUNG_DAT,
           P_TT_MUC_DICH_SU_DUNG_DAT,
           P_CHUYEN_MUC_DICH,
           P_MA);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_MUC_DICH_SU_DUNG_DAT)
        INTO V_TON_TAI_TEN
        FROM DC_MUC_DICH_SU_DUNG_DAT
       WHERE UPPER(TEN_MUC_DICH_SU_DUNG_DAT) LIKE UPPER(P_TEN_MUC_DICH_SU_DUNG_DAT)
         AND MA_MUC_DICH_SU_DUNG_DAT NOT LIKE P_MA_MUC_DICH_SU_DUNG_DAT;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_MUC_DICH_SU_DUNG_DAT
           SET TEN_MUC_DICH_SU_DUNG_DAT    = P_TEN_MUC_DICH_SU_DUNG_DAT,
           LOAI_MUC_DICH_MUC_DICH  = P_LOAI_MUC_DICH_MUC_DICH,
               STT_MUC_DICH_SU_DUNG_DAT    = P_STT_MUC_DICH_SU_DUNG_DAT,
               TT_MUC_DICH_SU_DUNG_DAT     = P_TT_MUC_DICH_SU_DUNG_DAT,
               CHUYEN_MUC_DICH=P_CHUYEN_MUC_DICH,
               MA=P_MA
         WHERE MA_MUC_DICH_SU_DUNG_DAT LIKE P_MA_MUC_DICH_SU_DUNG_DAT;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_MUC_DICH_SU_DUNG_DAT(P_VAL   OUT NVARCHAR2, P_MA_MUC_DICH_SU_DUNG_DAT NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_MUC_DICH_SU_DUNG_DAT WHERE MA_MUC_DICH_SU_DUNG_DAT LIKE P_MA_MUC_DICH_SU_DUNG_DAT;
    P_VAL := 'THANH_CONG';
   END;
------------------------------DC_GIAO_DICH------------------------------------------
PROCEDURE SELECT_GIAO_DICH(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_GIAO_DICH = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_GIAO_DICH A
       ORDER BY A.STT_GIAO_DICH , A.TEN_GIAO_DICH;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_GIAO_DICH_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_GIAO_DICH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_GIAO_DICH = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_GIAO_DICH A
       WHERE A.MA_GIAO_DICH LIKE P_MA_GIAO_DICH;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_GIAO_DICH(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_GIAO_DICH     NVARCHAR2,
                           P_TEN_GIAO_DICH    NVARCHAR2,
                           P_STT_GIAO_DICH    INT,
                           P_TT_GIAO_DICH     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_GIAO_DICH)
        INTO V_TON_TAI_MA
        FROM DC_GIAO_DICH
       WHERE UPPER(MA_GIAO_DICH) LIKE UPPER(P_MA_GIAO_DICH);
      SELECT COUNT(MA_GIAO_DICH)
        INTO V_TON_TAI_TEN
        FROM DC_GIAO_DICH
       WHERE UPPER(TEN_GIAO_DICH) LIKE UPPER(P_TEN_GIAO_DICH);
      IF V_TON_TAI_MA > 0 OR P_MA_GIAO_DICH = '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 DC_GIAO_DICH
          (MA_GIAO_DICH,
           TEN_GIAO_DICH,
           STT_GIAO_DICH,
           TT_GIAO_DICH)
        VALUES
          (P_MA_GIAO_DICH,
           P_TEN_GIAO_DICH,
           P_STT_GIAO_DICH,
           P_TT_GIAO_DICH);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_GIAO_DICH)
        INTO V_TON_TAI_TEN
        FROM DC_GIAO_DICH
       WHERE UPPER(TEN_GIAO_DICH) LIKE UPPER(P_TEN_GIAO_DICH)
         AND MA_GIAO_DICH NOT LIKE P_MA_GIAO_DICH;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_GIAO_DICH
           SET TEN_GIAO_DICH    = P_TEN_GIAO_DICH,
               STT_GIAO_DICH    = P_STT_GIAO_DICH,
               TT_GIAO_DICH     = P_TT_GIAO_DICH
         WHERE MA_GIAO_DICH LIKE P_MA_GIAO_DICH;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_GIAO_DICH(P_VAL   OUT NVARCHAR2, P_MA_GIAO_DICH NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_GIAO_DICH WHERE MA_GIAO_DICH LIKE P_MA_GIAO_DICH;
    P_VAL := 'THANH_CONG';
   END;
------------------------------DC_CSDL_DAT------------------------------------------
PROCEDURE SELECT_CSDL_DAT(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_CSDL_DAT = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_CSDL_DAT A
       ORDER BY A.STT_CSDL_DAT , A.TEN_CSDL_DAT;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_CSDL_DAT_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_CSDL_DAT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_CSDL_DAT = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_CSDL_DAT A
       WHERE A.MA_CSDL_DAT LIKE P_MA_CSDL_DAT;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_CSDL_DAT(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_CSDL_DAT     NVARCHAR2,
                           P_TEN_CSDL_DAT    NVARCHAR2,
                           P_STT_CSDL_DAT    INT,
                           P_TT_CSDL_DAT     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_CSDL_DAT)
        INTO V_TON_TAI_MA
        FROM DC_CSDL_DAT
       WHERE UPPER(MA_CSDL_DAT) LIKE UPPER(P_MA_CSDL_DAT);
      SELECT COUNT(MA_CSDL_DAT)
        INTO V_TON_TAI_TEN
        FROM DC_CSDL_DAT
       WHERE UPPER(TEN_CSDL_DAT) LIKE UPPER(P_TEN_CSDL_DAT);
      IF V_TON_TAI_MA > 0 OR P_MA_CSDL_DAT = '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 DC_CSDL_DAT
          (MA_CSDL_DAT,
           TEN_CSDL_DAT,
           STT_CSDL_DAT,
           TT_CSDL_DAT)
        VALUES
          (P_MA_CSDL_DAT,
           P_TEN_CSDL_DAT,
           P_STT_CSDL_DAT,
           P_TT_CSDL_DAT);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_CSDL_DAT)
        INTO V_TON_TAI_TEN
        FROM DC_CSDL_DAT
       WHERE UPPER(TEN_CSDL_DAT) LIKE UPPER(P_TEN_CSDL_DAT)
         AND MA_CSDL_DAT NOT LIKE P_MA_CSDL_DAT;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_CSDL_DAT
           SET TEN_CSDL_DAT    = P_TEN_CSDL_DAT,
               STT_CSDL_DAT    = P_STT_CSDL_DAT,
               TT_CSDL_DAT     = P_TT_CSDL_DAT
         WHERE MA_CSDL_DAT LIKE P_MA_CSDL_DAT;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_CSDL_DAT(P_VAL   OUT NVARCHAR2, P_MA_CSDL_DAT NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_CSDL_DAT WHERE MA_CSDL_DAT LIKE P_MA_CSDL_DAT;
    P_VAL := 'THANH_CONG';
   END;
------------------------------DC_LOAI_LUY_TIEN------------------------------------------
PROCEDURE SELECT_LOAI_LUY_TIEN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_LUY_TIEN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_LOAI_LUY_TIEN A
       ORDER BY A.STT_LOAI_LUY_TIEN , A.TEN_LOAI_LUY_TIEN;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_LOAI_LUY_TIEN_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_LOAI_LUY_TIEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_LOAI_LUY_TIEN = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_LOAI_LUY_TIEN A
       WHERE A.MA_LOAI_LUY_TIEN LIKE P_MA_LOAI_LUY_TIEN;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_LOAI_LUY_TIEN(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LOAI_LUY_TIEN     number,
                           P_TEN_LOAI_LUY_TIEN    NVARCHAR2,
                           P_STT_LOAI_LUY_TIEN    INT,
                           P_TT_LOAI_LUY_TIEN     INT) AS
  
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
     
        INSERT INTO DC_LOAI_LUY_TIEN
          (MA_LOAI_LUY_TIEN,
           TEN_LOAI_LUY_TIEN,
           STT_LOAI_LUY_TIEN,
           TT_LOAI_LUY_TIEN)
        VALUES
          (DC_LOAI_LUY_TIEN_SEQ.NEXTVAL,
           P_TEN_LOAI_LUY_TIEN,
           P_STT_LOAI_LUY_TIEN,
           P_TT_LOAI_LUY_TIEN);
        P_VAL := 'THANH_CONG';
     
    ELSE
      
        UPDATE DC_LOAI_LUY_TIEN
           SET TEN_LOAI_LUY_TIEN    = P_TEN_LOAI_LUY_TIEN,
               STT_LOAI_LUY_TIEN    = P_STT_LOAI_LUY_TIEN,
               TT_LOAI_LUY_TIEN     = P_TT_LOAI_LUY_TIEN
         WHERE MA_LOAI_LUY_TIEN LIKE P_MA_LOAI_LUY_TIEN;
        P_VAL := 'THANH_CONG';
    
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_LOAI_LUY_TIEN(P_VAL   OUT NVARCHAR2, P_MA_LOAI_LUY_TIEN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_LOAI_LUY_TIEN WHERE MA_LOAI_LUY_TIEN LIKE P_MA_LOAI_LUY_TIEN;
    P_VAL := 'THANH_CONG';
   END;
------------------------------DC_QH_CSDL------------------------------------------
PROCEDURE SELECT_QH_CSDL(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.MA_QH_CSDL,TO_CHAR(A.NAMCAPNHAT,'DD/MM/YYYY') as NAM,A.TRANG_THAI,B.TEN_PHUONG_XA,B.MA_PHUONG_XA,C.TEN_CSDL_DAT,C.MA_CSDL_DAT,
             CASE
               WHEN A.Trang_Thai = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI,SO_THUA
        FROM DC_QH_CSDL A, DM_PHUONG_XA B, DC_CSDL_DAT C, DM_QUAN_HUYEN D
        WHERE A.MA_CSDL_DAT=C.MA_CSDL_DAT
        AND A.MA_PHUONG_XA=B.MA_PHUONG_XA
        AND B.Ma_Quan_Huyen=d.Ma_Quan_Huyen
        AND B.MA_QUAN_HUYEN LIKE P_MA_QUAN_HUYEN
       ORDER BY A.MA_QH_CSDL;
    ELSE 
      OPEN P_CUR FOR SELECT  A.MA_QH_CSDL,TO_CHAR(A.NAMCAPNHAT,'DD/MM/YYYY') as NAM,A.TRANG_THAI,B.TEN_PHUONG_XA,B.MA_PHUONG_XA,C.TEN_CSDL_DAT,C.MA_CSDL_DAT,
             CASE
               WHEN A.Trang_Thai = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI,SO_THUA
                FROM DC_QH_CSDL A, DM_PHUONG_XA B, DC_CSDL_DAT C, DM_QUAN_HUYEN D
        WHERE   A.MA_CSDL_DAT=C.MA_CSDL_DAT
        AND A.MA_PHUONG_XA=B.MA_PHUONG_XA
        AND B.Ma_Quan_Huyen=d.Ma_Quan_Huyen
                AND B.MA_QUAN_HUYEN IN (SELECT E.MA_QUAN_HUYEN FROM DM_QUAN_HUYEN E WHERE E.MA_TINH_THANH=P_MA_TINH_THANH AND ROWNUM=1)
               ORDER BY B.MA_QUAN_HUYEN;
        END IF;
  END;
  -------------------------------------------------------------------------------
    PROCEDURE SELECT_QH_CSDL_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_QH_CSDL NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QH_CSDL,TO_CHAR(A.NAMCAPNHAT,'DD/MM/YYYY') as NAM,A.TRANG_THAI,B.TEN_PHUONG_XA,C.TEN_CSDL_DAT,B.MA_PHUONG_XA,C.MA_CSDL_DAT,
             CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI,SO_THUA
       FROM DC_QH_CSDL A, DM_PHUONG_XA B, DC_CSDL_DAT C
       WHERE A.MA_CSDL_DAT=C.MA_CSDL_DAT
       AND A.MA_PHUONG_XA=B.MA_PHUONG_XA
       AND A.MA_QH_CSDL LIKE P_MA_QH_CSDL;
  END;
  -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_QH_CSDL(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_QH_CSDL     NVARCHAR2,
                           P_NAMCAPNHAT    NVARCHAR2,
                           P_MA_PHUONG_XA    NVARCHAR2,
                           P_MA_CSDL_DAT    NVARCHAR2,
                            P_SO_LUONG     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_QH_CSDL)
        INTO V_TON_TAI_MA
        FROM DC_QH_CSDL
       WHERE UPPER(MA_QH_CSDL) LIKE UPPER(P_MA_QH_CSDL);
      SELECT COUNT(MA_QH_CSDL)
        INTO V_TON_TAI_TEN
        FROM DC_QH_CSDL
       WHERE UPPER(MA_PHUONG_XA) LIKE UPPER(P_MA_PHUONG_XA)
       AND UPPER(MA_CSDL_DAT) LIKE UPPER(P_MA_CSDL_DAT)
        AND UPPER(NAMCAPNHAT) LIKE UPPER(P_NAMCAPNHAT);
      IF V_TON_TAI_MA > 0 OR P_MA_QH_CSDL = '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 DC_QH_CSDL
          (MA_QH_CSDL,
           NAMCAPNHAT,
           MA_PHUONG_XA,
           MA_CSDL_DAT,
           SO_THUA,
           TRANG_THAI)
        VALUES
          (P_MA_QH_CSDL,
           to_date(P_NAMCAPNHAT,'DDMMYYYY'),
           P_MA_PHUONG_XA,
           P_MA_CSDL_DAT,
           P_SO_LUONG,
           P_TRANG_THAI);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      /*SELECT COUNT(MA_QH_CSDL)
        INTO V_TON_TAI_TEN
        FROM DC_QH_CSDL
       WHERE UPPER(MA_PHUONG_XA) LIKE UPPER(P_MA_PHUONG_XA)
       AND UPPER(MA_CSDL_DAT) LIKE UPPER(P_MA_CSDL_DAT)
       AND MA_QH_CSDL NOT LIKE P_MA_QH_CSDL;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE*/
        UPDATE DC_QH_CSDL
           SET NAMCAPNHAT    =  to_date(P_NAMCAPNHAT,'DDMMYYYY'),
               MA_PHUONG_XA    = P_MA_PHUONG_XA,
               MA_CSDL_DAT     = P_MA_CSDL_DAT,
               SO_THUA=P_SO_LUONG,
               TRANG_THAI      =P_TRANG_THAI
         WHERE MA_QH_CSDL LIKE P_MA_QH_CSDL;
        P_VAL := 'THANH_CONG';
     -- END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_QH_CSDL(P_VAL   OUT NVARCHAR2, P_MA_QH_CSDL NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_QH_CSDL WHERE MA_QH_CSDL LIKE P_MA_QH_CSDL;
    P_VAL := 'THANH_CONG';
   END;
   
       PROCEDURE DELETE_QH_MUC_DICH_SD(P_VAL   OUT NVARCHAR2, P_MA_QH_MUC_DICH_SD NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_QH_MUC_DICH_SD WHERE MA_QH_MUC_DICH_SD LIKE P_MA_QH_MUC_DICH_SD;
    P_VAL := 'THANH_CONG';
   END;
------------------------------DC_CD_MUC_DICH------------------------------------------
PROCEDURE SELECT_CD_MUC_DICH(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,CASE
               WHEN A.La_Noi_Bo = 0 THEN
                'Chuyển dịch nội bộ NN'
               ELSE
                'Chuyển dịch phi nội bộ NN'
             END AS DAT_NONG_NGHIEP,
             CASE
               WHEN A.TT_CD_MUC_DICH = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_CHUYEN_DICH_MUC_DICH_SD_DAT A
       ORDER BY A.STT_CD_MUC_DICH , A.TEN_CD_MUC_DICH;
  END;
-------------------------------------------------------------------------------
     PROCEDURE SELECT_CD_MUC_DICH_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_CD_MUC_DICH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,CASE
               WHEN A.La_Noi_Bo = 0 THEN
                 'Chuyển dịch nội bộ NN'
               ELSE
                'Chuyển dịch phi nội bộ NN'
             END AS DAT_NONG_NGHIEP,
             CASE
               WHEN A.TT_CD_MUC_DICH = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DC_CHUYEN_DICH_MUC_DICH_SD_DAT A
       WHERE A.MA_CD_MUC_DICH LIKE P_MA_CD_MUC_DICH;
  END;
    -------------------------------------------------------------------------------
 PROCEDURE UPDATE_CD_MUC_DICH(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_CD_MUC_DICH     NVARCHAR2,
                           P_TEN_CD_MUC_DICH    NVARCHAR2,
                           P_LA_NOI_BO    INT,
                           P_STT_CD_MUC_DICH    INT,
                           P_TT_CD_MUC_DICH     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_CD_MUC_DICH)
        INTO V_TON_TAI_MA
        FROM DC_CHUYEN_DICH_MUC_DICH_SD_DAT
       WHERE UPPER(MA_CD_MUC_DICH) LIKE UPPER(P_MA_CD_MUC_DICH);
      SELECT COUNT(MA_CD_MUC_DICH)
        INTO V_TON_TAI_TEN
        FROM DC_CHUYEN_DICH_MUC_DICH_SD_DAT
       WHERE UPPER(TEN_CD_MUC_DICH) LIKE UPPER(P_TEN_CD_MUC_DICH);
      IF V_TON_TAI_MA > 0 OR P_MA_CD_MUC_DICH = '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 DC_CHUYEN_DICH_MUC_DICH_SD_DAT
          (MA_CD_MUC_DICH,
           TEN_CD_MUC_DICH,
           LA_NOI_BO,
           STT_CD_MUC_DICH,
           TT_CD_MUC_DICH)
        VALUES
          (P_MA_CD_MUC_DICH,
           P_TEN_CD_MUC_DICH,
           P_LA_NOI_BO,
           P_STT_CD_MUC_DICH,
           P_TT_CD_MUC_DICH);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_CD_MUC_DICH)
        INTO V_TON_TAI_TEN
        FROM DC_CHUYEN_DICH_MUC_DICH_SD_DAT
       WHERE UPPER(TEN_CD_MUC_DICH) LIKE UPPER(P_TEN_CD_MUC_DICH)
         AND MA_CD_MUC_DICH NOT LIKE P_MA_CD_MUC_DICH;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_CHUYEN_DICH_MUC_DICH_SD_DAT
           SET TEN_CD_MUC_DICH    = P_TEN_CD_MUC_DICH,
           LA_NOI_BO  = P_LA_NOI_BO,
               STT_CD_MUC_DICH    = P_STT_CD_MUC_DICH,
               TT_CD_MUC_DICH     = P_TT_CD_MUC_DICH
         WHERE MA_CD_MUC_DICH LIKE P_MA_CD_MUC_DICH;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_CD_MUC_DICH(P_VAL   OUT NVARCHAR2, P_MA_CD_MUC_DICH NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_CHUYEN_DICH_MUC_DICH_SD_DAT WHERE MA_CD_MUC_DICH LIKE P_MA_CD_MUC_DICH;
    P_VAL := 'THANH_CONG';
   END;

------------------------------DC_QH_MUC_DICH_SD------------------------------------------
PROCEDURE SELECT_QH_MUC_DICH_SD(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.NAM_CAP_NHAT,'DD/MM/YYYY') as NAM,B.TEN_QUAN_HUYEN,C.TEN_CD_MUC_DICH
        FROM DC_QH_MUC_DICH_SD A,DM_QUAN_HUYEN B, DC_CHUYEN_DICH_MUC_DICH_SD_DAT C
        WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
        AND A.MA_CD_MUC_DICH=C.MA_CD_MUC_DICH
       ORDER BY A.MA_QH_MUC_DICH_SD;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_QH_MUC_DICH_SD_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_QH_MUC_DICH_SD NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.NAM_CAP_NHAT,'DD/MM/YYYY') as NAM,B.TEN_QUAN_HUYEN,C.TEN_CD_MUC_DICH
        FROM DC_QH_MUC_DICH_SD A,DM_QUAN_HUYEN B, DC_CHUYEN_DICH_MUC_DICH_SD_DAT C
        WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
        AND A.MA_CD_MUC_DICH=C.MA_CD_MUC_DICH
       AND A.MA_QH_MUC_DICH_SD LIKE P_MA_QH_MUC_DICH_SD;
  END;
    -------------------------------------------------------------------------------
PROCEDURE UPDATE_QH_MUC_DICH_SD(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_QH_MUC_DICH_SD     NVARCHAR2,
                           P_NAMCAPNHAT    NVARCHAR2,
                           P_MA_QUAN_HUYEN    NVARCHAR2,
                           P_MA_CD_MUC_DICH    NVARCHAR2,
                           P_DIEN_TICH     FLOAT) 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_QH_MUC_DICH_SD)
        INTO V_TON_TAI_MA
        FROM DC_QH_MUC_DICH_SD
       WHERE UPPER(MA_QH_MUC_DICH_SD) LIKE UPPER(P_MA_QH_MUC_DICH_SD);
      SELECT COUNT(MA_QH_MUC_DICH_SD)
        INTO V_TON_TAI_TEN
        FROM DC_QH_MUC_DICH_SD
       WHERE UPPER(MA_QUAN_HUYEN) LIKE UPPER(P_MA_QUAN_HUYEN)
       AND UPPER(MA_CD_MUC_DICH) LIKE UPPER(P_MA_CD_MUC_DICH);
      IF V_TON_TAI_MA > 0 OR P_MA_QH_MUC_DICH_SD = '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 DC_QH_MUC_DICH_SD
          (MA_QH_MUC_DICH_SD,
           NAM_CAP_NHAT,
           MA_QUAN_HUYEN,
           MA_CD_MUC_DICH,
           DIEN_TICH)
        VALUES
          (P_MA_QH_MUC_DICH_SD,
           to_date(P_NAMCAPNHAT,'DDMMYYYY'),
           P_MA_QUAN_HUYEN,
           P_MA_CD_MUC_DICH,
           P_DIEN_TICH);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_QH_MUC_DICH_SD)
        INTO V_TON_TAI_TEN
        FROM DC_QH_MUC_DICH_SD
       WHERE UPPER(MA_QUAN_HUYEN) LIKE UPPER(P_MA_QUAN_HUYEN)
       AND UPPER(MA_QH_MUC_DICH_SD) LIKE UPPER(P_MA_QH_MUC_DICH_SD)
       AND MA_QH_MUC_DICH_SD NOT LIKE P_MA_QH_MUC_DICH_SD;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_QH_MUC_DICH_SD
           SET NAM_CAP_NHAT    =  to_date(P_NAMCAPNHAT,'DDMMYYYY'),
               MA_QUAN_HUYEN    = P_MA_QUAN_HUYEN,
               MA_CD_MUC_DICH     = P_MA_CD_MUC_DICH,
               DIEN_TICH      =P_DIEN_TICH
         WHERE MA_QH_MUC_DICH_SD LIKE P_MA_QH_MUC_DICH_SD;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
PROCEDURE SELECT_DC_HO_SO_DAT(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.*,TO_CHAR(TG_BK,'DD/MM/YYYY')as TiG_BD,TO_CHAR(TG_KT,'DD/MM/YYYY')as TiG_KT,B.VI_TRI,B.TG_KT,C.SO_THUA,D.*,E.*,TO_BAN_DO,
        THOI_HAN,TEN_BIEN_DONG
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN B, DC_THUADAT C, DC_HINH_THUC D, DC_MUC_DICH_SU_DUNG_DAT E,DC_BIEN_DONG F
          WHERE A.MA_QUAN_HUYEN LIKE P_MA_QUAN_HUYEN
          AND A.MA_THUA=C.MA_THUA
          AND A.ID_DONG=F.ID_DONG
          AND A.MA_MUC_DICH_SU_DUNG_DAT=E.MA_MUC_DICH_SU_DUNG_DAT
          AND A.MA_HINH_THUC=D.MA_HINH_THUC
          AND A.MA_THUA=B.MA_THUA AND B.SU_DUNG=0
         ORDER BY B.TG_KT ASC;
    ELSE 
       OPEN P_CUR FOR
        SELECT A.*,TO_CHAR(TG_BK,'DD/MM/YYYY')as TiG_BD,TO_CHAR(TG_KT,'DD/MM/YYYY')as TiG_KT,B.VI_TRI,B.TG_KT,C.SO_THUA,D.*,E.*,TO_BAN_DO,
        THOI_HAN,TEN_BIEN_DONG
          FROM DC_HO_SO_DAT A, DC_LUY_TIEN B, DC_THUADAT C, DC_HINH_THUC D, DC_MUC_DICH_SU_DUNG_DAT E,DC_BIEN_DONG F
          WHERE A.MA_QUAN_HUYEN IN (SELECT E.MA_QUAN_HUYEN FROM DM_QUAN_HUYEN E WHERE ROWNUM=1 )
          AND A.MA_THUA=C.MA_THUA
          AND A.ID_DONG=F.ID_DONG 
         AND A.MA_MUC_DICH_SU_DUNG_DAT=E.MA_MUC_DICH_SU_DUNG_DAT
          AND A.MA_HINH_THUC=D.MA_HINH_THUC
         AND A.MA_THUA=B.MA_THUA AND B.SU_DUNG=0
         ORDER BY B.TG_KT ASC;
              END IF;
  END;
-------------------------------------------------------------------------------
     PROCEDURE SELECT_DC_HO_SO_DAT_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_HS_DAT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT MA_HS_DAT,TEN_DON_VI,VI_TRI,GHI_CHU,TO_CHAR(TG_BK,'DD/MM/YYYY') as TG_BATDAU,TO_CHAR(TG_KT,'DD/MM/YYYY')as TG_KET_THUC,
          C.THOI_HAN,PHUONG_THUC_THANH_TOAN,A.MA_MUC_DICH_SU_DUNG_DAT, THUOC_TO_CHUC, MA_QUOC_TICH,
          C.TO_BAN_DO,SDT, GHI_CHU,A.MA_HINH_THUC,SO_THUA,DAU_GIA,A.DIEN_TICH_SD, TIEN_DAU_GIA,TRANG_THAI,A.*
        FROM DC_HO_SO_DAT A, Dc_Thuadat B, DC_LUY_TIEN C
       WHERE A.MA_THUA=B.MA_THUA
       AND B.MA_THUA=C.MA_THUA
       AND C.SU_DUNG=0
       AND C.NAM_CAP_PHAT IN(SELECT MAX(NAM_CAP_PHAT) FROM DC_LUY_TIEN GROUP BY MA_THUA)
       AND A.Ma_Hs_Dat LIKE P_MA_HS_DAT;
  END;
-------------------------------------------------------------------------------
PROCEDURE SELECT_DC_THUA_DAT(P_CUR IN OUT MYCUR, P_MA_QUAN_HUYEN NVARCHAR2, P_MA_PHUONG_XA NVARCHAR2,P_DON_VI NVARCHAR2,P_MA_MUC_DICH NVARCHAR2,P_MA_HINH_THUC NVARCHAR2,P_NAM NVARCHAR2,P_TRANG_THAI NVARCHAR2) AS
    V_KT INT;
      V_NAM NVARCHAR2(100); 
 BEGIN
    SELECT COUNT(1) INTO V_KT FROM DC_DAT_HUYEN C 
    WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') =P_NAM;
    IF V_KT=0 THEN
      V_NAM:='';
     ELSE
      V_NAM:=P_NAM;
    END IF;
    IF V_NAM IS NOT NULL THEN
           OPEN P_CUR FOR
              SELECT DISTINCT B.ma_thua,TO_CHAR(TG_BK,'DD/MM/YYYY')as TiG_BD,TO_CHAR(TG_KT,'DD/MM/YYYY')as TiG_KT,B.MA_LUY_TIEN,A.SO_THUA,B.VI_TRI,B.TO_BAN_DO,B.DIEN_TICH_THUA,C.TEN_HINH_THUC,D.TEN_MUC_DICH_SU_DUNG_DAT,
              B.NGUOI_DUNG,B.THOI_HAN,B.TG_BK,B.TG_KT,A.SO_THUA_GOC,E.TEN_BIEN_DONG ,g.ten_phuong_xa,f.MA_QUAN_HUYEN,A.HINH_ANH,H.Ten_Cap_Quan_Ly,f.ten_quan_huyen,A.SO_THUA_GOC,A.TRANG_THAI_SD,a.so_thua_goc
              FROM DC_THUADAT A, DC_LUY_TIEN B, DC_HINH_THUC C, DC_MUC_DICH_SU_DUNG_DAT D, DC_BIEN_DONG E, DM_QUAN_HUYEN F, DM_PHUONG_XA G, dc_cap_quan_ly H
              WHERE A.MA_THUA=B.MA_THUA
              AND B.MA_CAP_QUAN_LY=H.MA_CAP_QUAN_LY
              AND A.MA_HINH_THUC=C.MA_HINH_THUC
              AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT
              AND A.ID_DONG=E.ID_DONG
              AND F.MA_QUAN_HUYEN=G.MA_QUAN_HUYEN
              AND G.MA_PHUONG_XA=A.MA_PHUONG_XA
              AND (F.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN OR P_MA_QUAN_HUYEN IS NULL )
              AND (G.MA_PHUONG_XA=P_MA_PHUONG_XA OR P_MA_PHUONG_XA IS NULL)
              AND (B.LA_DAT_CONG=P_DON_VI OR P_DON_VI IS NULL)
              AND (A.MA_MUC_DICH_SU_DUNG_DAT=P_MA_MUC_DICH OR P_MA_MUC_DICH IS NULL)
              AND (A.MA_HINH_THUC=P_MA_HINH_THUC OR P_MA_HINH_THUC IS NULL)
              AND (A.TRANG_THAI_SD=P_TRANG_THAI OR P_TRANG_THAI IS NULL) 
              AND TO_CHAR(B.NAM_CAP_PHAT, 'YYYY')= P_NAM
              ;
              ELSE
                OPEN P_CUR FOR
              SELECT DISTINCT B.ma_thua,TO_CHAR(TG_BK,'DD/MM/YYYY')as TiG_BD,TO_CHAR(TG_KT,'DD/MM/YYYY')as TiG_KT,B.MA_LUY_TIEN,A.SO_THUA,B.VI_TRI,B.TO_BAN_DO,B.DIEN_TICH_THUA,C.TEN_HINH_THUC,D.TEN_MUC_DICH_SU_DUNG_DAT,
              B.NGUOI_DUNG,A.Hinh_Anh,B.THOI_HAN,B.TG_BK,B.TG_KT,A.SO_THUA_GOC,E.TEN_BIEN_DONG ,g.ten_phuong_xa,f.MA_QUAN_HUYEN,H.Ten_Cap_Quan_Ly,f.ten_quan_huyen,A.SO_THUA_GOC,A.TRANG_THAI_SD,a.so_thua_goc
              FROM DC_THUADAT A, DC_LUY_TIEN B, DC_HINH_THUC C, DC_MUC_DICH_SU_DUNG_DAT D, DC_BIEN_DONG E, DM_QUAN_HUYEN F, DM_PHUONG_XA G, dc_cap_quan_ly H
              WHERE A.MA_THUA=B.MA_THUA
              AND B.MA_CAP_QUAN_LY=H.MA_CAP_QUAN_LY
              AND A.MA_HINH_THUC=C.MA_HINH_THUC
              AND A.MA_MUC_DICH_SU_DUNG_DAT=D.MA_MUC_DICH_SU_DUNG_DAT
              AND A.ID_DONG=E.ID_DONG
              AND F.MA_QUAN_HUYEN=G.MA_QUAN_HUYEN
              AND G.MA_PHUONG_XA=A.MA_PHUONG_XA
              AND (F.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN OR P_MA_QUAN_HUYEN IS NULL )
              AND (G.MA_PHUONG_XA=P_MA_PHUONG_XA OR P_MA_PHUONG_XA IS NULL)
              AND (B.LA_DAT_CONG=P_DON_VI OR P_DON_VI IS NULL)
              AND (A.MA_MUC_DICH_SU_DUNG_DAT=P_MA_MUC_DICH OR P_MA_MUC_DICH IS NULL)
              AND (A.MA_HINH_THUC=P_MA_HINH_THUC OR P_MA_HINH_THUC IS NULL)
              AND (A.TRANG_THAI_SD=P_TRANG_THAI OR P_TRANG_THAI IS NULL) 
              --------AND (TO_CHAR(B.NAM_CAP_PHAT, 'YYYY')= P_NAM OR P_NAM IS NULL)
              AND B.Su_Dung=0
              ;
           END IF;
  END;
-------------------------------------------------------------------------------
PROCEDURE SELECT_DC_THUA_DAT_DETAIL(P_CUR IN OUT MYCUR, P_MA_THUA NVARCHAR2) AS
  BEGIN
   
      OPEN P_CUR FOR
         SELECT A.*,H.TEN_QUAN_HUYEN,C.TEN_PHUONG_XA,MA_THUA,SO_THUA,SO_THUA_GOC,B.TEN_LOAI_DAT,C.TEN_PHUONG_XA,C.Ma_Quan_Huyen 
        FROM DC_THUADAT A, DC_LOAI_DAT B, DM_PHUONG_XA C, DC_BIEN_DONG D, Dc_Muc_Dich_Su_Dung_Dat E, DC_HINH_THUC F, DM_QUAN_HUYEN H
        WHERE A.MA_LOAI_DAT=B.MA_LOAI_DAT
        AND H.MA_QUAN_HUYEN=C.MA_QUAN_HUYEN
        AND A.MA_PHUONG_XA=C.MA_PHUONG_XA
        AND A.ID_DONG=D.ID_DONG
        AND A.MA_MUC_DICH_SU_DUNG_DAT=E.MA_MUC_DICH_SU_DUNG_DAT
        AND F.MA_HINH_THUC=A.MA_HINH_THUC
        AND A.MA_THUA=P_MA_THUA
         ORDER BY A.MA_THUA DESC;
  END;
-------------------------------------------------------------------------------   
PROCEDURE UPDATE_DC_THUADAT(P_VAL            OUT NVARCHAR2,
                       
                           P_MA_THUA     INT,
                           P_LOAI_DAT    NVARCHAR2,
                           P_SO_THUA    INT,
                           P_THUADATGOC    NVARCHAR2,
                           P_MA_PHUONG_XA    NVARCHAR2,
                           P_ID_BIEN_DONG   INT,
                           P_TRANG_THAI_SD   INT,
                           P_MA_MUC_DICH_SD    NVARCHAR2,
                           P_HINH_THUC_SD NVARCHAR2,
                            P_FILE_HS nvarchar2
                           ) AS

  BEGIN
    P_VAL := 'CO_LOI';
        UPDATE DC_THUADAT
           SET MA_LOAI_DAT    = P_LOAI_DAT,
           SO_THUA  = P_SO_THUA,
           SO_THUA_GOC    = P_THUADATGOC,
           MA_PHUONG_XA=P_MA_PHUONG_XA,
           ID_DONG=P_ID_BIEN_DONG,
           Trang_Thai_Sd=P_TRANG_THAI_SD,
           MA_MUC_DICH_SU_DUNG_DAT=P_MA_MUC_DICH_SD,
           MA_HINH_THUC=P_HINH_THUC_SD,
           HINH_ANH=P_FILE_HS
         WHERE MA_THUA LIKE P_MA_THUA;
        P_VAL := 'THANH_CONG';
  END;
  
------------------------------------------------------------------------------- 
PROCEDURE UPDATE_DC_THUADAT_THEM(P_VAL            OUT NVARCHAR2,
                       
                           P_LOAI_DAT    NVARCHAR2,
                           P_SO_THUA    INT,
                           P_THUADATGOC    NVARCHAR2,
                           P_MA_PHUONG_XA NVARCHAR2,
                           --
                           P_ID_DONG INT,
                           P_TRANG_THAI_SD INT,
                           P_MUC_DICH_SD NVARCHAR2,
                           P_MA_HINH_THUC NVARCHAR2,
                           --
                           P_NAM_CAP_NHAT NVARCHAR2,
                           P_DIEN_TICH FLOAT,
                           P_TOA_DO NVARCHAR2,
                           P_LA_DAT_CONG INT,
                           P_TO_BAN_DO NVARCHAR2,
                           P_LA_THUOC_TO_CHUC NVARCHAR2,
                           P_SO_GCN_DAT NVARCHAR2,
                           P_KET_QUA_XD NVARCHAR2,
                           P_MA_LOAI_LUY_TIEN NVARCHAR2,
                           P_DK_KE_KHAI INT,
                           ------
                           P_VI_TRI NVARCHAR2,
                           P_NGUOI_SU_DUNG NVARCHAR2,
                           P_THOI_HAN INT,
                           P_TG_BD NVARCHAR2,
                           P_TG_KT NVARCHAR2,
                           P_SU_DUNG NVARCHAR2, 
                           P_MA_CAP_QUAN_LY   INT,
                           P_FILE_HS nvarchar2
                           ) AS

  BEGIN
    P_VAL := 'CO_LOI';
    INSERT INTO DC_THUADAT
    (
           MA_THUA,
           MA_LOAI_DAT,
           SO_THUA,
           SO_THUA_GOC,
           MA_PHUONG_XA,
           ID_DONG,
           TRANG_THAI_SD,
           MA_MUC_DICH_SU_DUNG_DAT,
           MA_HINH_THUC,
           HINH_ANH
         
     )VALUES
     (
           DC_THUA_DAT_SEQ.NEXTVAL,
           P_LOAI_DAT,
           P_SO_THUA,
           P_THUADATGOC,
           P_MA_PHUONG_XA,
           P_ID_DONG,
           P_TRANG_THAI_SD,
           P_MUC_DICH_SD,
           P_MA_HINH_THUC,
           P_FILE_HS
     );
    INSERT INTO DC_LUY_TIEN
    (
           MA_LUY_TIEN,NAM_CAP_PHAT,DIEN_TICH_THUA,TOA_DO,LA_DAT_CONG,TO_BAN_DO,
           LA_THUOC_TO_CHUC,SO_GCN_DAT,KET_QUA_XD,MA_THUA,MA_LOAI_LUY_TIEN,DK_KE_KHAI,
           VI_TRI,NGUOI_DUNG,THOI_HAN,TG_BK,TG_KT,SU_DUNG, MA_CAP_QUAN_LY
     )VALUES
     (
           DC_LUY_TIEN_SEQ.NEXTVAL,to_date(P_NAM_CAP_NHAT,'DDMMYYYY'),
           P_DIEN_TICH,P_TOA_DO,P_LA_DAT_CONG,P_TO_BAN_DO,
           P_LA_THUOC_TO_CHUC,P_SO_GCN_DAT,P_KET_QUA_XD,
           DC_THUA_DAT_SEQ.CURRVAL,P_MA_LOAI_LUY_TIEN, P_DK_KE_KHAI,
           P_VI_TRI,P_NGUOI_SU_DUNG,P_THOI_HAN,to_date(P_TG_BD,'DDMMYYYY'),to_date(P_TG_KT,'DDMMYYYY'),P_SU_DUNG,
           P_MA_CAP_QUAN_LY
           
     );  
        P_VAL := 'THANH_CONG';
  END;
    -------------------------------------------------------------------------------
  PROCEDURE DELETE_DC_THUADAT_LUYTIEN(P_VAL   OUT NVARCHAR2, P_MA_THUADAT NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_LUY_TIEN WHERE MA_THUA LIKE P_MA_THUADAT;
    DELETE FROM DC_THUADAT WHERE MA_THUA LIKE P_MA_THUADAT;
    DELETE FROM DC_HO_SO_DAT WHERE MA_THUA LIKE P_MA_THUADAT;
    P_VAL := 'THANH_CONG';
   END; 
------------------------------------------------------------------------------- 
PROCEDURE SELECT_LUY_TIEN(P_CUR IN OUT MYCUR,P_MA_THUADAT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
     
SELECT A.*, CASE
               WHEN A.LA_DAT_CONG = 0 THEN
                'Có'
               ELSE
                'Không' END AS DAT_CONG 
                ,CASE
               WHEN A.LA_THUOC_TO_CHUC = 0 THEN
                'Có'
               ELSE
                'Không'  END AS TO_CHUC 
                ,CASE
               WHEN A.KET_QUA_XD = 'DTD' THEN
                'Đang thẩm định'
                WHEN A.KET_QUA_XD = 'DD' THEN 
                  'Đã duyệt'
               ELSE
                'Chưa duyệt'  END AS KET_QUA
                ,CASE
               WHEN A.DK_KE_KHAI = 0 THEN
                'Đã'
               ELSE
                'Chưa'  END AS KE_KHAI,               
 B.TEN_LOAI_LUY_TIEN ,TO_CHAR(A.NAM_CAP_PHAT,'DD/MM/YYYY') as NAM,
 TO_CHAR(A.Tg_Bk,'DD/MM/YYYY') as TG_BD,TO_CHAR(A.TG_KT,'DD/MM/YYYY') as TG_KTT                               
 FROM DC_LUY_TIEN A, DC_LOAI_LUY_TIEN B
 WHERE A.MA_LOAI_LUY_TIEN=B.MA_LOAI_LUY_TIEN
 AND A.MA_THUA =P_MA_THUADAT
       ORDER BY A.MA_LUY_TIEN ;
  END;
-------------------------------------------------------------------------------
PROCEDURE SELECT_LUY_TIEN_DETAIL(P_CUR IN OUT MYCUR,P_MA_LUY_TIEN number, P_MA_THUA INT) AS
  V_KT INT;
      V_NAM NVARCHAR2(100); 
 BEGIN
    SELECT COUNT(1) INTO V_KT FROM DC_LUY_TIEN C 
    WHERE MA_LUY_TIEN =P_MA_LUY_TIEN;
    IF V_KT=0 THEN
      V_NAM:='';
     ELSE
      V_NAM:=P_MA_LUY_TIEN;
    END IF;
    IF V_NAM IS NOT NULL THEN
    OPEN P_CUR FOR
    
    SELECT A.*,'CN'as P_VAL, CASE
               WHEN A.LA_DAT_CONG = 0 THEN
                'Có'
               ELSE
                'Không' END AS DAT_CONG 
                ,CASE
               WHEN A.LA_THUOC_TO_CHUC = 0 THEN
                'Có'
               ELSE
                'Không'  END AS TO_CHUC 
                ,CASE
               WHEN A.KET_QUA_XD = 'DTD' THEN
                'Đang thẩm định'
                WHEN A.KET_QUA_XD = 'DD' THEN 
                  'Đã duyệt'
               ELSE
                'Chưa duyệt'  END AS KET_QUA
                ,CASE
               WHEN A.DK_KE_KHAI = 0 THEN
                'Đã'
               ELSE
                'Chưa'  END AS KE_KHAI, 
                 CASE
               WHEN A.Su_Dung = 0 THEN
                'Có'
               ELSE
                'Không' END AS TEN_SU_DUNG 
                ,              
 B.TEN_LOAI_LUY_TIEN,TO_CHAR(A.NAM_CAP_PHAT,'DD/MM/YYYY') as NAM,TO_CHAR(A.Tg_Bk,'DD/MM/YYYY') as TG_BD ,TO_CHAR(A.TG_KT,'DD/MM/YYYY') as TG_KT1                              
 FROM DC_LUY_TIEN A, DC_LOAI_LUY_TIEN B
 WHERE A.MA_LOAI_LUY_TIEN=B.MA_LOAI_LUY_TIEN
 AND A.MA_LUY_TIEN =P_MA_LUY_TIEN
       ORDER BY A.MA_LUY_TIEN ;
       ELSE
          OPEN P_CUR FOR
          
         SELECT A.*,'TM'as P_VAL, CASE
               WHEN A.LA_DAT_CONG = 0 THEN
                'Có'
               ELSE
                'Không' END AS DAT_CONG 
                ,CASE
               WHEN A.LA_THUOC_TO_CHUC = 0 THEN
                'Có'
               ELSE
                'Không'  END AS TO_CHUC 
                ,CASE
               WHEN A.KET_QUA_XD = 'DTD' THEN
                'Đang thẩm định'
                WHEN A.KET_QUA_XD = 'DD' THEN 
                  'Đã duyệt'
               ELSE
                'Chưa duyệt'  END AS KET_QUA
                ,CASE
               WHEN A.DK_KE_KHAI = 0 THEN
                'Đã'
               ELSE
                'Chưa'  END AS KE_KHAI, 
                 CASE
               WHEN A.Su_Dung = 0 THEN
                'Có'
               ELSE
                'Không' END AS TEN_SU_DUNG 
                ,              
 B.TEN_LOAI_LUY_TIEN,TO_CHAR(A.NAM_CAP_PHAT,'DD/MM/YYYY') as NAM,TO_CHAR(A.Tg_Bk,'DD/MM/YYYY') as TG_BD ,TO_CHAR(A.TG_KT,'DD/MM/YYYY') as TG_KT1                              
 FROM DC_LUY_TIEN A, DC_LOAI_LUY_TIEN B
 WHERE A.MA_LOAI_LUY_TIEN=B.MA_LOAI_LUY_TIEN
 AND A.MA_THUA=P_MA_THUA
 AND SU_DUNG=0
       ORDER BY A.MA_LUY_TIEN ;
         END IF;
  END;
-------------------------------------------------------------------------------
PROCEDURE UPDATE_LUY_TIEN_DAT(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_LUY_TIEN NUMBER,
                           P_NAM_CAP_NHAT NVARCHAR2,
                           P_DIEN_TICH FLOAT,
                           P_TOA_DO NVARCHAR2,
                           P_LA_DAT_CONG INT,
                           P_TOA_DO_BAN_DO NVARCHAR2,
                           P_LA_THUOC_TO_CHUC NVARCHAR2,
                           P_SO_GCN_DAT NVARCHAR2,
                           P_KET_QUA_XD NVARCHAR2,
                           P_MA_LOAI_LUY_TIEN NVARCHAR2,
                           P_DK_KE_KHAI INT,
                           P_MA_THUA NUMBER,
                           
                           P_VI_TRI NVARCHAR2,
                           P_NGUOI_SU_DUNG NVARCHAR2,
                           P_THOI_HAN INT,
                           P_TG_BD NVARCHAR2,
                           P_TG_KT NVARCHAR2,
                           P_SU_DUNG NVARCHAR2,
                           P_MA_CAP_QUAN_LY   INT
                           ) AS

  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
          INSERT INTO DC_LUY_TIEN
    (
           MA_LUY_TIEN,NAM_CAP_PHAT,DIEN_TICH_THUA,TOA_DO,LA_DAT_CONG,TO_BAN_DO,
           LA_THUOC_TO_CHUC,SO_GCN_DAT,KET_QUA_XD,MA_THUA,MA_LOAI_LUY_TIEN,DK_KE_KHAI,VI_TRI,
           NGUOI_DUNG,THOI_HAN,TG_BK,TG_KT,SU_DUNG,MA_CAP_QUAN_LY
     )VALUES
     (
           DC_LUY_TIEN_SEQ.NEXTVAL,to_date(P_NAM_CAP_NHAT,'DDMMYYYY'),
           P_DIEN_TICH,P_TOA_DO,P_LA_DAT_CONG,P_TOA_DO_BAN_DO,
           P_LA_THUOC_TO_CHUC,P_SO_GCN_DAT,P_KET_QUA_XD,
          P_MA_THUA,P_MA_LOAI_LUY_TIEN, P_DK_KE_KHAI,P_VI_TRI,
          P_NGUOI_SU_DUNG,P_THOI_HAN,to_date(P_TG_BD,'DDMMYYYY'),to_date(P_TG_KT,'DDMMYYYY'),P_SU_DUNG,
          P_MA_CAP_QUAN_LY
           
     );
        P_VAL := 'THANH_CONG';
     
    ELSE
    
        UPDATE DC_LUY_TIEN
           SET NAM_CAP_PHAT=to_date(P_NAM_CAP_NHAT,'DDMMYYYY'),
           DIEN_TICH_THUA=P_DIEN_TICH,
           TOA_DO=P_TOA_DO,
           LA_DAT_CONG=P_LA_DAT_CONG,
           TO_BAN_DO=P_TOA_DO_BAN_DO,
           LA_THUOC_TO_CHUC=P_LA_THUOC_TO_CHUC,
           SO_GCN_DAT=P_SO_GCN_DAT,
           KET_QUA_XD=P_KET_QUA_XD,
           MA_THUA=P_MA_THUA,
           MA_LOAI_LUY_TIEN=P_MA_LOAI_LUY_TIEN,
           DK_KE_KHAI=P_DK_KE_KHAI,
           VI_TRI=P_VI_TRI,
           NGUOI_DUNG=P_NGUOI_SU_DUNG,
           THOI_HAN=P_THOI_HAN,
           TG_BK=to_date(P_TG_BD,'DDMMYYYY'),
           TG_KT=to_date(P_TG_KT,'DDMMYYYY'),
           SU_DUNG=P_SU_DUNG,
           MA_CAP_QUAN_LY=P_MA_CAP_QUAN_LY
         WHERE MA_LUY_TIEN= P_MA_LUY_TIEN ;
        P_VAL := 'THANH_CONG';
 
    END IF;
  
  END;
  -------------------------------------------------------------------------------
    PROCEDURE DELETE_DC_LUYTIEN(P_VAL   OUT NVARCHAR2, P_MA_LUY_TIEN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_LUY_TIEN WHERE MA_LUY_TIEN LIKE P_MA_LUY_TIEN;
    P_VAL := 'THANH_CONG';
   END; 
------------------------------------------------------------------------------- 
------------------------------DC_DAT_HUYEN------------------------------------------
PROCEDURE SELECT_DAT_HUYEN(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.*,B.TEN_QUAN_HUYEN,TO_CHAR(A.Nam_Cap_Nhat,'DD/MM/YYYY') as NAM   
        FROM DC_DAT_HUYEN A, DM_QUAN_HUYEN B
        WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
        AND A.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN
       ORDER BY  A.MA_DAT_HUYEN DESC;
    ELSE 
       OPEN P_CUR FOR
      
         
          SELECT A.*,B.TEN_QUAN_HUYEN,TO_CHAR(A.Nam_Cap_Nhat,'DD/MM/YYYY') as NAM   
        FROM DC_DAT_HUYEN A, DM_QUAN_HUYEN B
        WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
        AND A.MA_QUAN_HUYEN IN (SELECT E.MA_QUAN_HUYEN FROM DM_QUAN_HUYEN E WHERE ROWNUM=1 )
       ORDER BY  A.MA_DAT_HUYEN DESC;
       
              END IF;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_DAT_HUYEN_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_DAT_HUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,B.TEN_QUAN_HUYEN,TO_CHAR(A.Nam_Cap_Nhat,'DD/MM/YYYY') as NAM   
        FROM DC_DAT_HUYEN A, DM_QUAN_HUYEN B
        WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
        AND A.MA_DAT_HUYEN=P_MA_DAT_HUYEN
       ORDER BY  A.MA_DAT_HUYEN;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_DAT_HUYEN(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_DAT_HUYEN     NUMBER,
                           P_NAM_CAP_NHAT    NVARCHAR2,
                           P_DIEN_TICH_TU_NHIEN    FLOAT,
                           P_DIEN_TICH_DO_DAC     FLOAT,
                           P_MA_QUAN_HUYEN     NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      
        INSERT INTO DC_DAT_HUYEN
          (MA_DAT_HUYEN,
           NAM_CAP_NHAT,
           DIEN_TICH_TU_NHIEN,
           DIEN_TICH_DO_DAC,
           MA_QUAN_HUYEN)
        VALUES
          (DC_DAT_HUYEN_SEQ.NEXTVAL,
           to_date(P_NAM_CAP_NHAT,'DDMMYYYY'),
           P_DIEN_TICH_TU_NHIEN,
           P_DIEN_TICH_DO_DAC,
           P_MA_QUAN_HUYEN);
        P_VAL := 'THANH_CONG';
     
    ELSE
        UPDATE DC_DAT_HUYEN
           SET NAM_CAP_NHAT= to_date(P_NAM_CAP_NHAT,'DDMMYYYY'),
           DIEN_TICH_TU_NHIEN=P_DIEN_TICH_TU_NHIEN,
           DIEN_TICH_DO_DAC=P_DIEN_TICH_DO_DAC,
           MA_QUAN_HUYEN=P_MA_QUAN_HUYEN
         WHERE MA_DAT_HUYEN LIKE P_MA_DAT_HUYEN;
        P_VAL := 'THANH_CONG';
     
    END IF;
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_DAT_HUYEN(P_VAL   OUT NVARCHAR2, P_MA_DAT_HUYEN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_DAT_HUYEN WHERE MA_DAT_HUYEN LIKE P_MA_DAT_HUYEN;
    P_VAL := 'THANH_CONG';
   END;  
    -------------------------------------------------------------------------------
    -------------------------------------------------------------------------------
    PROCEDURE SELECT_BIEU_DO_DAT_CONG(P_CUR  IN OUT MYCUR) AS

  BEGIN
    OPEN P_CUR FOR
        SELECT TONG,TONGSD_HA,TONGSD_GD,TONGSD_TD,(TONGSD_HA/TONG)*100 AS F_SD,
 (TONGSD_GD/TONG)*100 AS F_GD, (TONGSD_TD/TONG)*100 AS F_TD,100-(((TONGSD_GD/TONG)*100 )+( (TONGSD_TD/TONG)*100)) AS F_CL
 FROM
 (
          SELECT COALESCE( SUM(S.DIEN_TICH_THUA),0)/1000 TONG
          FROM DC_LUY_TIEN S         
          WHERE SU_DUNG=0 
) 
,
(
          SELECT COALESCE( SUM(A.DIEN_TICH_THUA),0)/1000 AS TONGSD_HA 
          FROM DC_LUY_TIEN A, DC_HO_SO_DAT B
          WHERE  A.MA_THUA=B.MA_THUA AND
           A.Su_Dung=0
          
)
,
(
          SELECT COALESCE( SUM(A.DIEN_TICH_THUA),0)/1000 AS TONGSD_GD 
          FROM DC_LUY_TIEN A, DC_HO_SO_DAT C
          WHERE  A.MA_THUA=C.MA_THUA 
          AND a.su_dung=0
          AND C.MA_MUC_DICH_SU_DUNG_DAT LIKE 'GD'
          
)
,
(
          SELECT COALESCE( SUM(A.DIEN_TICH_THUA),0)/1000 AS TONGSD_TD 
          FROM DC_LUY_TIEN A, DC_HO_SO_DAT B, DC_THUADAT C
          WHERE  A.MA_THUA=B.MA_THUA AND
         B.MA_THUA=C.MA_THUA
          AND  a.su_dung=0
          AND B.MA_MUC_DICH_SU_DUNG_DAT LIKE 'CT'
          
)
;
  END;

-------------------------------------------------------------------------------
        PROCEDURE SELECT_NAM_TINH(P_CUR  IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT   DISTINCT TO_CHAR(NAM_CAP_NHAT, 'YYYY') NAM  FROM DC_DAT_HUYEN ORDER BY TO_CHAR(NAM_CAP_NHAT, 'YYYY') DESC;
  END;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
PROCEDURE SELECT_DAT_HUYEN_TINH(P_CUR  IN OUT MYCUR,P_NAM NVARCHAR2) AS
        V_KT INT;
      V_NAM NVARCHAR2(100); 
 BEGIN
    SELECT COUNT(1) INTO V_KT FROM DC_DAT_HUYEN C 
    WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') =P_NAM;
    IF V_KT=0 THEN
      V_NAM:='';
     ELSE
      V_NAM:=P_NAM;
    END IF;
    IF V_NAM IS NOT NULL THEN
    OPEN P_CUR FOR
          SELECT TEN_QUAN_HUYEN,TU_NHIEN/1000 as TU_NHIEN,DO_DAC/1000 as DO_DAC FROM DM_QUAN_HUYEN A
          LEFT JOIN 
          (
               SELECT MA_QUAN_HUYEN, COALESCE(SUM(DIEN_TICH_TU_NHIEN),0) TU_NHIEN FROM DC_DAT_HUYEN DH
               WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') = V_NAM
               GROUP BY MA_QUAN_HUYEN
          )TU_NHIEN ON TU_NHIEN.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
          LEFT JOIN 
          (
               SELECT MA_QUAN_HUYEN, COALESCE(SUM(DIEN_TICH_DO_DAC),0) DO_DAC FROM DC_DAT_HUYEN
               WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') = V_NAM
                GROUP BY MA_QUAN_HUYEN
              )DO_DAC ON DO_DAC.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
              WHERE A.MA_TINH_THANH='TIEN_GIANG';
    ELSE
                OPEN P_CUR FOR
                 SELECT TEN_QUAN_HUYEN,TU_NHIEN/1000 as TU_NHIEN,DO_DAC/1000 as DO_DAC FROM DM_QUAN_HUYEN A
          LEFT JOIN 
          (
               SELECT MA_QUAN_HUYEN, COALESCE(SUM(DIEN_TICH_TU_NHIEN),0) TU_NHIEN FROM DC_DAT_HUYEN DH
               WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') IN( SELECT TO_CHAR(NAM_CAP_NHAT, 'YYYY')   FROM DC_DAT_HUYEN WHERE ROWNUM=1)
               GROUP BY MA_QUAN_HUYEN
          )TU_NHIEN ON TU_NHIEN.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
          LEFT JOIN 
          (
               SELECT MA_QUAN_HUYEN, COALESCE(SUM(DIEN_TICH_DO_DAC),0) DO_DAC FROM DC_DAT_HUYEN
               WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') IN( SELECT TO_CHAR(NAM_CAP_NHAT, 'YYYY')   FROM DC_DAT_HUYEN WHERE ROWNUM=1)
                GROUP BY MA_QUAN_HUYEN
              )DO_DAC ON DO_DAC.MA_QUAN_HUYEN=A.MA_QUAN_HUYEN
              WHERE A.MA_TINH_THANH='TIEN_GIANG';
      END IF;
  END;
-------------------------------------------------------------------------------
PROCEDURE SELECT_DAT_TONG_TINH(P_CUR  IN OUT MYCUR,P_NAM NVARCHAR2) AS
        V_KT INT;
      V_NAM NVARCHAR2(100); 
 BEGIN
    SELECT COUNT(1) INTO V_KT FROM DC_DAT_HUYEN C 
    WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') =P_NAM;
    IF V_KT=0 THEN
      V_NAM:='';
     ELSE
      V_NAM:=P_NAM;
    END IF;
    IF V_NAM IS NOT NULL THEN
    OPEN P_CUR FOR
          SELECT TU_NHIEN/1000 as TU_NHIEN_HA,DO_DAC/1000 as DO_DAC_HA FROM
       
          (
               SELECT   COALESCE(SUM(DIEN_TICH_TU_NHIEN),0) TU_NHIEN FROM DC_DAT_HUYEN DH
               WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') = V_NAM
          ),
          (
               SELECT  COALESCE(SUM(DIEN_TICH_DO_DAC),0) DO_DAC FROM DC_DAT_HUYEN
               WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') = V_NAM
              );
    ELSE
                OPEN P_CUR FOR
                 SELECT TU_NHIEN/1000 as TU_NHIEN_HA,DO_DAC/1000 as DO_DAC_HA  FROM
          (
               SELECT  COALESCE(SUM(DIEN_TICH_TU_NHIEN),0) TU_NHIEN FROM DC_DAT_HUYEN DH
               WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') IN( SELECT TO_CHAR(NAM_CAP_NHAT, 'YYYY')   FROM DC_DAT_HUYEN WHERE ROWNUM=1)
          )
          ,
          (
               SELECT  COALESCE(SUM(DIEN_TICH_DO_DAC),0) DO_DAC FROM DC_DAT_HUYEN
               WHERE  TO_CHAR(NAM_CAP_NHAT, 'YYYY') IN( SELECT TO_CHAR(NAM_CAP_NHAT, 'YYYY')   FROM DC_DAT_HUYEN WHERE ROWNUM=1)
              );
      END IF;
  END;
------------------------------DC_CHUYEN_DICH_LOAI_DAT------------------------------------------
PROCEDURE SELECT_DC_CHUYEN_DICH_LOAI_DAT(P_CUR IN OUT MYCUR,P_QUAN_HUYEN NVARCHAR2,P_NAM NVARCHAR2) AS
    V_KT INT;
      V_NAM NVARCHAR2(100); 
 BEGIN
    SELECT COUNT(1) INTO V_KT FROM DM_QUAN_HUYEN C 
    WHERE  MA_QUAN_HUYEN =P_QUAN_HUYEN;
    IF V_KT=0 THEN
      V_NAM:='';
     ELSE
      V_NAM:=P_QUAN_HUYEN;
    END IF;
    IF V_NAM IS NOT NULL THEN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.Nam_Cap_Nhat,'YYYY') as NAM,TEN_QUAN_HUYEN,TEN_LOAI_DAT
        FROM DC_CHUYEN_DICH_LOAI_DAT A, DM_QUAN_HUYEN B,DC_LOAI_DAT D
        WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
        AND A.MA_LOAI_DAT=D.MA_LOAI_DAT
        AND B.MA_QUAN_HUYEN=P_QUAN_HUYEN
        AND TO_CHAR(A.NAM_CAP_NHAT, 'YYYY') =P_NAM
       ORDER BY A.MA_CD_DAT ;
        ELSE
           OPEN P_CUR FOR
           SELECT A.*,TO_CHAR(A.Nam_Cap_Nhat,'YYYY') as NAM,TEN_QUAN_HUYEN,TEN_LOAI_DAT
        FROM DC_CHUYEN_DICH_LOAI_DAT A, DM_QUAN_HUYEN B,DC_LOAI_DAT D
        WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
        AND A.MA_LOAI_DAT=D.MA_LOAI_DAT
        AND B.MA_QUAN_HUYEN IN (SELECT MIN(MA_QUAN_HUYEN)  FROM DM_QUAN_HUYEN )
        AND TO_CHAR(A.NAM_CAP_NHAT, 'YYYY') =to_char( ADD_MONTHS(SYSDATE, -60), 'YYYY') 
       ORDER BY A.MA_CD_DAT ;
          END IF;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_DC_CD_LOAI_DAT_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_CD_DAT INT) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,TO_CHAR(A.Nam_Cap_Nhat,'DD/MM/YYYY') as NAM,TEN_QUAN_HUYEN,TEN_LOAI_DAT
        FROM DC_CHUYEN_DICH_LOAI_DAT A, DM_QUAN_HUYEN B,DC_LOAI_DAT D
        WHERE A.MA_QUAN_HUYEN=B.MA_QUAN_HUYEN
        AND A.MA_LOAI_DAT=D.MA_LOAI_DAT
       AND A.MA_CD_DAT LIKE P_MA_CD_DAT;
  END;
 -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_DC_CD_LOAI_DAT(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_CD_DAT INT ,
       P_MA_QUAN_HUYEN NVARCHAR2,
       P_MA_LOAI_DAT NVARCHAR2,
       P_NAM_CAP_NHAT NVARCHAR2,--BIEU 3
       P_C_THUA_DAT_KK INT  ,
       P_C_THUA_DAT_KK_LT INT ,--CÓ LUỸ TIẾN
       P_S_THUA_DAT_KK FLOAT ,
       P_S_THUA_DAT_KK_LT FLOAT ,--CÓ LUỸ TIẾN
       P_C_THUA_DAT_XD INT ,
       P_C_THUA_DAT_XD_LT INT  ,--CÓ LUỸ TIẾN
       P_S_THUA_DAT_XD FLOAT   ,
       P_S_THUA_DAT_XD_LT FLOAT  ,--CÓ LUỸ TIẾN
       P_GHI_CHU_B3 NVARCHAR2,
       P_S_CAN_CAP FLOAT , --BIEU_02
       P_S_CAN_CAP_TC FLOAT , 
       P_S_CAN_CAP_CN FLOAT , 
       P_C_GCN INT   , 
       P_C_GCN_LT INT   ,
       P_C_GCN_TC INT   , 
       P_C_GCN_TC_LT INT ,   
       P_C_GCN_CN INT , 
       P_C_GCN_CN_LT INT ,
       P_S_GCN INT  , 
       P_S_GCN_LT INT  ,
       P_S_GCN_TC INT  , 
       P_S_GCN_TC_LT INT ,   
       P_S_GCN_CN INT , 
       P_S_GCN_CN_LT INT ,
       P_C_GCN_NGUOI INT ,
       P_GHI_CHU_B2 NVARCHAR2 ) AS
