  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
  BEGIN
    P_VAL := 'CO_LOI';  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
        INSERT INTO DC_CHUYEN_DICH_LOAI_DAT
          (MA_CD_DAT ,
       MA_QUAN_HUYEN ,
       MA_LOAI_DAT ,
       NAM_CAP_NHAT ,
       C_THUA_DAT_KK ,
       C_THUA_DAT_KK_LT,
       S_THUA_DAT_KK ,
       S_THUA_DAT_KK_LT ,
       C_THUA_DAT_XD ,
       C_THUA_DAT_XD_LT ,
       S_THUA_DAT_XD ,
       S_THUA_DAT_XD_LT ,
       GHI_CHU_B3 ,
       S_CAN_CAP ,
       S_CAN_CAP_TC , 
       S_CAN_CAP_CN , 
       C_GCN , 
       C_GCN_LT ,
       C_GCN_TC , 
       C_GCN_TC_LT ,   
       C_GCN_CN , 
       C_GCN_CN_LT ,
       S_GCN , 
       S_GCN_LT ,
       S_GCN_TC , 
       S_GCN_TC_LT ,   
       S_GCN_CN , 
       S_GCN_CN_LT ,
       C_GCN_NGUOI ,
       GHI_CHU_B2 )
        VALUES
          (DC_CHUYEN_DICH_LOAI_DAT_SEQ.NEXTVAL ,
       P_MA_QUAN_HUYEN ,
       P_MA_LOAI_DAT ,
       to_date(P_NAM_CAP_NHAT,'DDMMYYYY') ,
       P_C_THUA_DAT_KK ,
       P_C_THUA_DAT_KK_LT,
       P_S_THUA_DAT_KK ,
       P_S_THUA_DAT_KK_LT ,
       P_C_THUA_DAT_XD ,
       P_C_THUA_DAT_XD_LT ,
       P_S_THUA_DAT_XD ,
       P_S_THUA_DAT_XD_LT ,
       P_GHI_CHU_B3 ,
       P_S_CAN_CAP ,
       P_S_CAN_CAP_TC , 
       P_S_CAN_CAP_CN , 
       P_C_GCN , 
       P_C_GCN_LT ,
       P_C_GCN_TC , 
       P_C_GCN_TC_LT ,   
       P_C_GCN_CN , 
       P_C_GCN_CN_LT ,
       P_S_GCN , 
       P_S_GCN_LT ,
       P_S_GCN_TC , 
       P_S_GCN_TC_LT ,   
       P_S_GCN_CN , 
       P_S_GCN_CN_LT ,
       P_C_GCN_NGUOI ,
       P_GHI_CHU_B2);
        P_VAL := 'THANH_CONG';  
    ELSE
        UPDATE DC_CHUYEN_DICH_LOAI_DAT
           SET 
       MA_QUAN_HUYEN =P_MA_QUAN_HUYEN,
       MA_LOAI_DAT =P_MA_LOAI_DAT,
       NAM_CAP_NHAT= to_date(P_NAM_CAP_NHAT,'DDMMYYYY'),
       C_THUA_DAT_KK =P_C_THUA_DAT_KK,
       C_THUA_DAT_KK_LT=P_C_THUA_DAT_KK_LT,
       S_THUA_DAT_KK =P_S_THUA_DAT_KK,
       S_THUA_DAT_KK_LT =P_S_THUA_DAT_KK_LT,
       C_THUA_DAT_XD =P_C_THUA_DAT_XD,
       C_THUA_DAT_XD_LT =P_C_THUA_DAT_XD_LT,
       S_THUA_DAT_XD =P_S_THUA_DAT_XD,
       S_THUA_DAT_XD_LT =P_S_THUA_DAT_XD_LT,
       GHI_CHU_B3 =P_GHI_CHU_B3,
       S_CAN_CAP =P_S_CAN_CAP,
       S_CAN_CAP_TC=P_S_CAN_CAP_TC , 
       S_CAN_CAP_CN =P_S_CAN_CAP_CN, 
       C_GCN =P_C_GCN, 
       C_GCN_LT =P_C_GCN_LT,
       C_GCN_TC =P_C_GCN_TC, 
       C_GCN_TC_LT =P_C_GCN_TC_LT,   
       C_GCN_CN =P_C_GCN_CN, 
       C_GCN_CN_LT =P_C_GCN_CN_LT,
       S_GCN =P_S_GCN, 
       S_GCN_LT =P_S_GCN_LT,
       S_GCN_TC =P_S_GCN_TC, 
       S_GCN_TC_LT =P_S_GCN_TC_LT,   
       S_GCN_CN= P_S_GCN_CN, 
       S_GCN_CN_LT =P_S_GCN_CN_LT,
       C_GCN_NGUOI =P_C_GCN_NGUOI,
       GHI_CHU_B2=P_GHI_CHU_B2
         WHERE MA_CD_DAT LIKE P_MA_CD_DAT;
        P_VAL := 'THANH_CONG';
    END IF;
  END;
     -------------------------------------------------------------------------------
  PROCEDURE DELETE_DC_CD_LOAI_DAT(P_VAL   OUT NVARCHAR2, P_MA_CD_DAT NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_CHUYEN_DICH_LOAI_DAT WHERE MA_CD_DAT LIKE P_MA_CD_DAT;
    P_VAL := 'THANH_CONG';
   END;
------------------------------DELETE_MUC_DICH_SU_DUNG_DAT------------------------------------------
------------------------------DC_BIEN_DONG------------------------------------------
PROCEDURE SELECT_BIEN_DONG(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*, CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Hiển thị'
               WHEN A.TRANG_THAI = 1 THEN
                'Không' END AS TRANG_THAI_A 
        FROM DC_BIEN_DONG A
        WHERE ((A.TRANG_THAI=0)OR(A.TRANG_THAI =1))
       ORDER BY A.ID_DONG;
  END;

-------------------------------------------------------------------------------
    PROCEDURE SELECT_BIEN_DONG_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_ID_DONG NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_BIEN_DONG A
       WHERE A.ID_DONG LIKE P_ID_DONG;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_BIEN_DONG(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_ID_DONG     number,
                           P_TEN_BIEN_DONG    NVARCHAR2,
                           P_LOAI NVARCHAR2,
                           P_TRANG_THAI INT) AS
    V_TON_TAI_TEN INT;
  BEGIN
    P_VAL := 'CO_LOI';
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
      SELECT COUNT(ID_DONG)
        INTO V_TON_TAI_TEN
        FROM DC_BIEN_DONG
       WHERE UPPER(TEN_BIEN_DONG) LIKE UPPER(P_TEN_BIEN_DONG);
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten   
        P_VAL := 'TON_TAI_TEN';
      ELSE
        INSERT INTO DC_BIEN_DONG
          (ID_DONG,
           TEN_BIEN_DONG,
           LOAI,
           TRANG_THAI)
        VALUES
          (DC_BIEN_DONG_SEQ.NEXTVAL,
           P_TEN_BIEN_DONG,P_LOAI,P_TRANG_THAI);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(ID_DONG)
        INTO V_TON_TAI_TEN
        FROM DC_BIEN_DONG
       WHERE UPPER(TEN_BIEN_DONG) LIKE UPPER(P_TEN_BIEN_DONG)
         AND ID_DONG NOT LIKE P_ID_DONG;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_BIEN_DONG
           SET TEN_BIEN_DONG    = P_TEN_BIEN_DONG,
            LOAI    = P_LOAI,
            TRANG_THAI=P_TRANG_THAI
         WHERE ID_DONG LIKE P_ID_DONG;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
     -------------------------------------------------------------------------------
  PROCEDURE DELETE_BIEN_DONG(P_VAL   OUT NVARCHAR2, P_ID_DONG INT) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_BIEN_DONG WHERE ID_DONG LIKE P_ID_DONG;
    P_VAL := 'THANH_CONG';
   END;
   -------------------------------------------------------------------------------
   PROCEDURE SELECT_MUC_DICH_SD_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_MUC_DICH_SU_DUNG_DAT,A.TEN_MUC_DICH_SU_DUNG_DAT,A.STT_MUC_DICH_SU_DUNG_DAT,
      A.CHUYEN_MUC_DICH,A.MA
        FROM DC_MUC_DICH_SU_DUNG_DAT A
       
        UNION 
      SELECT N'' AS MA_MUC_DICH_SU_DUNG_DAT,
            N'Tất cả' AS TEN_MUC_DICH_SU_DUNG_DAT,
            -1 AS STT_MUC_DICH_SU_DUNG_DAT,
            N'' AS CHUYEN_MUC_DICH,
            N'' AS MA
           FROM DUAL
       ORDER BY STT_MUC_DICH_SU_DUNG_DAT , TEN_MUC_DICH_SU_DUNG_DAT;
  END;
-------------------------------------------------------------------------------
   PROCEDURE SELECT_HINH_THUC_SD_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_HINH_THUC,A.TEN_HINH_THUC,A.STT_HINH_THUC
        FROM DC_HINH_THUC A
       
        UNION 
      SELECT N'' AS MA_HINH_THUC,
            N'Tất cả' AS TEN_HINH_THUC,
            -1 AS STT_HINH_THUC
           FROM DUAL
       ORDER BY STT_HINH_THUC , TEN_HINH_THUC;
  END;
 --------
FUNCTION datediff (datepart IN VARCHAR2, date_from IN DATE, date_to IN DATE)
RETURN NUMBER
AS
  diff NUMBER;
BEGIN
  diff :=  CASE datepart
    WHEN 'day'   THEN TRUNC(date_to,'DD') - TRUNC(date_from, 'DD')
    WHEN 'week'  THEN (TRUNC(date_to,'DAY') - TRUNC(date_from, 'DAY')) / 7
    WHEN 'month' THEN MONTHS_BETWEEN(TRUNC(date_to, 'MONTH'), TRUNC(date_from, 'MONTH'))
    WHEN 'year'  THEN TRUNC(date_to, 'YEAR') - TRUNC(date_from, 'YEAR')
  END;
  RETURN diff;
END;
------
-------------------------------------------------------------------------------
   PROCEDURE SELECT_THUADAT_SD(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_THUA,A.SO_THUA
        FROM DC_THUADAT A,DC_LUY_TIEN C
        WHERE A.MA_THUA=C.MA_THUA
        AND C.NAM_CAP_PHAT IN(SELECT MAX(NAM_CAP_PHAT) FROM DC_LUY_TIEN GROUP BY MA_THUA) 
    
       ORDER BY MA_THUA;
  END;
 --------
 -------------------------------------------------------------------------------
   PROCEDURE SELECT_BIEN_DONG_HS(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT * FROM DC_BIEN_DONG WHERE LOAI LIKE 'HS'
       ORDER BY ID_DONG;
  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_MA_DOI_TUONG INT
                           ) 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)
        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_VAL := 'IN_THANH_CONG';
      ELSE
        BEGIN
          UPDATE DC_HO_SO_DAT SET MA_QUAN_HUYEN=P_MA_HUYEN,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
           WHERE MA_HS_DAT LIKE P_MA_HS_DAT;
        P_VAL := 'UP_THANH_CONG';
        END;
      END IF;
  END;
  -------------------------------------------------------------------------------
  ------------------------------DC_DOI_TUONG------------------------------------------
PROCEDURE SELECT_DOI_TUONG(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_DOI_TUONG A
       WHERE A.TRANG_THAI IS NULL
       ORDER BY A.MA_DOI_TUONG;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_DOI_TUONG_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_DOI_TUONG NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_DOI_TUONG A
       WHERE A.MA_DOI_TUONG LIKE P_MA_DOI_TUONG;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_DOI_TUONG(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_DOI_TUONG     INT,
                           P_TEN_DOI_TUONG    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_DOI_TUONG)
        INTO V_TON_TAI_MA
        FROM DC_DOI_TUONG
       WHERE UPPER(MA_DOI_TUONG) LIKE UPPER(P_MA_DOI_TUONG);
      SELECT COUNT(MA_DOI_TUONG)
        INTO V_TON_TAI_TEN
        FROM DC_DOI_TUONG
       WHERE UPPER(TEN_DOI_TUONG) LIKE UPPER(P_TEN_DOI_TUONG);
      IF V_TON_TAI_MA > 0 OR P_MA_DOI_TUONG = '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_DOI_TUONG
          (MA_DOI_TUONG,
           TEN_DOI_TUONG)
        VALUES
          (DC_DOI_TUONG_SEQ.NEXTVAL,
           P_TEN_DOI_TUONG);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_DOI_TUONG)
        INTO V_TON_TAI_TEN
        FROM DC_DOI_TUONG
       WHERE UPPER(TEN_DOI_TUONG) LIKE UPPER(P_TEN_DOI_TUONG)
         AND MA_DOI_TUONG NOT LIKE P_MA_DOI_TUONG;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_DOI_TUONG
           SET TEN_DOI_TUONG    = P_TEN_DOI_TUONG
         WHERE MA_DOI_TUONG LIKE P_MA_DOI_TUONG;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_DOI_TUONG(P_VAL   OUT NVARCHAR2, P_MA_DOI_TUONG NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_DOI_TUONG WHERE MA_DOI_TUONG LIKE P_MA_DOI_TUONG;
    P_VAL := 'THANH_CONG';
   END;  
   -------------------------------------------------------------------------------
   ------------------------------DC_CAP_QUAN_LY------------------------------------------
PROCEDURE SELECT_CAP_QUAN_LY(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_CAP_QUAN_LY A
        WHERE A.TRANG_THAI IS NULL
       ORDER BY A.MA_CAP_QUAN_LY;
  END;
     ------------------------------DC_CAP_QUAN_LY------------------------------------------
PROCEDURE SELECT_CAP_QUAN_LY_DM(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_CAP_QUAN_LY A
       ORDER BY A.MA_CAP_QUAN_LY;
  END;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
    PROCEDURE SELECT_CAP_QUAN_LY_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_CAP_QUAN_LY NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_CAP_QUAN_LY A
       WHERE A.MA_CAP_QUAN_LY LIKE P_MA_CAP_QUAN_LY;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_CAP_QUAN_LY(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_CAP_QUAN_LY     INT,
                           P_TEN_CAP_QUAN_LY    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_CAP_QUAN_LY)
        INTO V_TON_TAI_MA
        FROM DC_CAP_QUAN_LY
       WHERE UPPER(MA_CAP_QUAN_LY) LIKE UPPER(P_MA_CAP_QUAN_LY);
      SELECT COUNT(MA_CAP_QUAN_LY)
        INTO V_TON_TAI_TEN
        FROM DC_CAP_QUAN_LY
       WHERE UPPER(TEN_CAP_QUAN_LY) LIKE UPPER(P_TEN_CAP_QUAN_LY);
      IF V_TON_TAI_MA > 0 OR P_MA_CAP_QUAN_LY = '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_CAP_QUAN_LY
          (MA_CAP_QUAN_LY,
           TEN_CAP_QUAN_LY)
        VALUES
          (DC_CAP_QUAN_LY_SEQ.NEXTVAL,
           P_TEN_CAP_QUAN_LY);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_CAP_QUAN_LY)
        INTO V_TON_TAI_TEN
        FROM DC_CAP_QUAN_LY
       WHERE UPPER(TEN_CAP_QUAN_LY) LIKE UPPER(P_TEN_CAP_QUAN_LY)
         AND MA_CAP_QUAN_LY NOT LIKE P_MA_CAP_QUAN_LY;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DC_CAP_QUAN_LY
           SET TEN_CAP_QUAN_LY    = P_TEN_CAP_QUAN_LY
         WHERE MA_CAP_QUAN_LY LIKE P_MA_CAP_QUAN_LY;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_CAP_QUAN_LY(P_VAL   OUT NVARCHAR2, P_MA_CAP_QUAN_LY NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_CAP_QUAN_LY WHERE MA_CAP_QUAN_LY LIKE P_MA_CAP_QUAN_LY;
    P_VAL := 'THANH_CONG';
   END;  
   -------------------------------------------------------------------------------
PROCEDURE SELECT_DOI_TUONG_FULL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_DOI_TUONG A
       ORDER BY A.MA_DOI_TUONG;
  END;
  ----------------------------------------
  -------------------------------------------------------------------------------
   PROCEDURE SELECT_THUADAT_SD_HS(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_THUA,A.SO_THUA
        FROM DC_THUADAT A,DC_LUY_TIEN C
        WHERE A.MA_THUA=C.MA_THUA
        AND C.NAM_CAP_PHAT IN(SELECT MAX(NAM_CAP_PHAT) FROM DC_LUY_TIEN GROUP BY MA_THUA) 
        AND a.MA_THUA NOT IN (SELECT MA_THUA FROM DC_HO_SO_DAT)
       ORDER BY MA_THUA;
  END;
 --------
   -----------------------------------------------------------------
  PROCEDURE SELECT_BIEN_DONG_TD(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*, CASE
               WHEN A.TRANG_THAI = 0 THEN
                'Hiển thị'
               WHEN A.TRANG_THAI = 1 THEN
                'Không' END AS TRANG_THAI_A 
        FROM DC_BIEN_DONG A
        WHERE LOAI='TD'
       ORDER BY A.ID_DONG;
  END;
  -----------------------------------------------
   PROCEDURE SELECT_DOI_TUONG_ALL(P_CUR IN OUT MYCUR) AS

       BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_DOI_TUONG,A.TEN_DOI_TUONG,A.TRANG_THAI
        FROM DC_DOI_TUONG A
       
       
        UNION 
      SELECT NULL AS MA_DOI_TUONG,
            N'Tất cả' AS TEN_DOI_TUONG,
            -1 AS TRANG_THAI
           FROM DUAL
       ORDER BY MA_DOI_TUONG , TEN_DOI_TUONG;
  END;
  -----------------
   PROCEDURE SELECT_QUOC_TICH_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_QUOC_TICH,A.TEN_QUOC_TICH
        FROM DM_QUOC_TICH A
       
        UNION 
      SELECT N'' AS MA_QUOC_TICH,
            N'Tất cả' AS TEN_QUOC_TICH
           FROM DUAL
       ORDER BY MA_QUOC_TICH , TEN_QUOC_TICH;
  END;
  --------------------------------------------
  PROCEDURE SELECT_LOAI_DAT_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_LOAI_DAT,A.TEN_LOAI_DAT
        FROM DC_LOAI_DAT A
       
        UNION 
      SELECT N'' AS MA_LOAI_DAT,
            N'Tất cả' AS TEN_LOAI_DAT
           FROM DUAL
       ORDER BY MA_LOAI_DAT , TEN_LOAI_DAT;
  END;
  ----------------------------------------------
   PROCEDURE SELECT_BIEN_DONG_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.ID_DONG,A.TEN_BIEN_DONG
        FROM DC_BIEN_DONG A
       
        UNION 
      SELECT NULL AS ID_DONG,
            N'Tất cả' AS TEN_BIEN_DONG
           FROM DUAL
       ORDER BY ID_DONG , TEN_BIEN_DONG;
  END;
  --------------------------------------------------
  PROCEDURE SELECT_DC_HO_SO_DAT_1(P_CUR IN OUT MYCUR, P_MA_QUAN_HUYEN NVARCHAR2,P_MA_PHUONG_XA NVARCHAR2,P_MA_DOI_TUONG INT,P_MA_MUC_DICH NVARCHAR2,
P_MA_HINH_THUC NVARCHAR2,P_MA_QUOC_TICH NVARCHAR2,P_TRANG_THAI NVARCHAR2,P_PTTT NVARCHAR2,P_MA_LOAI_DAT NVARCHAR2,P_ID_DONG NUMBER) AS
  
  BEGIN
    
      OPEN P_CUR FOR
         SELECT DISTINCT A.MA_HS_DAT,TEN_PHUONG_XA,A.*,TEN_DON_VI,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, DM_QUAN_HUYEN G, DM_PHUONG_XA H
          WHERE A.MA_THUA=C.MA_THUA AND C.MA_THUA=B.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_QUAN_HUYEN=G.MA_QUAN_HUYEN
          AND C.MA_PHUONG_XA=H.MA_PHUONG_XA
          AND B.SU_DUNG=0
		 AND (A.MA_QUAN_HUYEN=P_MA_QUAN_HUYEN OR P_MA_QUAN_HUYEN IS NULL )
     AND (H.MA_PHUONG_XA=P_MA_PHUONG_XA OR P_MA_PHUONG_XA IS NULL)
		AND (A.MA_DOI_TUONG=P_MA_DOI_TUONG OR P_MA_DOI_TUONG 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=P_TRANG_THAI OR P_TRANG_THAI IS NULL)
		 AND (A.MA_QUOC_TICH=P_MA_QUOC_TICH OR P_MA_QUOC_TICH IS NULL)
		 AND (A.PHUONG_THUC_THANH_TOAN=P_PTTT OR P_PTTT IS NULL)
		 AND (C.MA_LOAI_DAT=P_MA_LOAI_DAT OR P_MA_LOAI_DAT IS NULL)
		AND (A.ID_DONG=P_ID_DONG OR P_ID_DONG IS NULL)		/**/  
     ORDER BY B.TG_KT ASC;
    
  END;
     -------------------------------------------------------------------------------
  PROCEDURE SELECT_QUYEN_DUNG(P_CUR  IN OUT MYCUR, P_MA_CAN_BO NUMBER, P_MA_MODULE NVARCHAR2) AS
  BEGIN
     OPEN P_CUR FOR
    SELECT * FROM PQ_CB_MENU WHERE MA_CAN_BO LIKE P_MA_CAN_BO
    AND MA_MENU LIKE P_MA_MODULE;

   END;  
   -------------------------------------------------------------------------------
     PROCEDURE SELECT_PHAN_QUYEN_CT(P_CUR  IN OUT MYCUR, P_MA_CAN_BO NUMBER) AS
  BEGIN
     OPEN P_CUR FOR
     SELECT A.MA_MENU as MODULES,A.MA_CAN_BO ,C.TEN_QUYEN, XEM,THEM,SUA,XOA
    FROM PQ_CB_MENU A,PQ_QUYEN C
    WHERE C.MODULES=A.MA_MENU
    AND A.MA_CAN_BO=P_MA_CAN_BO
  UNION
SELECT DISTINCT C.MODULES,A.MA_CAN_BO,C.TEN_QUYEN,0 AS XEM,0 AS THEM,0 AS SUA,0 AS XOA 
    FROM PQ_CO_NHOM_QUYEN A, PQ_QUYEN C, PQ_CO_QUYEN B
    WHERE A.MA_NHOM_QUYEN=B.MA_NHOM_QUYEN
    AND B.MA_QUYEN=C.MA_QUYEN
    AND C.Modules NOT IN(SELECT A.MA_MENU 
    FROM PQ_CB_MENU A,PQ_QUYEN C
    WHERE C.MODULES=A.MA_MENU
    AND A.MA_CAN_BO=P_MA_CAN_BO)
    AND A.MA_CAN_BO=P_MA_CAN_BO;

   END;  
   -------------------------------------------------------------------------------
        PROCEDURE SELECT_PHAN_QUYEN_CT_DETAIL(P_CUR  IN OUT MYCUR, P_MA_CAN_BO NUMBER) AS
  BEGIN
     OPEN P_CUR FOR
    SELECT A.MA_CB_MN,A.MA_CAN_BO ,A.MA_MENU as MA_MENU_1, XEM as XEM_1,
    THEM As THEM_1,SUA AS SUA_1,XOA AS XOA_1 
    FROM PQ_CB_MENU A
    WHERE A.MA_CAN_BO=P_MA_CAN_BO;

   END;  
   -------------------------------------------------------------------------------
       PROCEDURE SELECT_PHAN_QUYEN_CT_COUNT(P_CUR  IN OUT MYCUR, P_MA_CAN_BO NUMBER) AS
  BEGIN
     OPEN P_CUR FOR
    SELECT COUNT(*) as DEM
    FROM PQ_CB_MENU A
    WHERE A.MA_CAN_BO=P_MA_CAN_BO;

   END; 
   -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_PHAN_QUYEN_CB(P_VAL            OUT NVARCHAR2,
                           P_MA_CAN_BO    INT,
                           P_MA_MENU    NVARCHAR2,
                           P_LOAI    NVARCHAR2,
                           P_TRANG_THAI    INT) AS
    V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
   SELECT COUNT(MA_CB_MN)
        INTO V_TON_TAI_MA
        FROM PQ_CB_MENU
       WHERE MA_CAN_BO LIKE P_MA_CAN_BO
       AND MA_MENU LIKE P_MA_MENU;
       
    IF V_TON_TAI_MA >0 THEN
      --UPDATE;
      IF P_LOAI='T' THEN
      UPDATE PQ_CB_MENU
      SET THEM=P_TRANG_THAI
      WHERE MA_CAN_BO LIKE P_MA_CAN_BO
      AND MA_MENU=P_MA_MENU;
      ELSE IF P_LOAI='S' THEN
      UPDATE PQ_CB_MENU
      SET SUA=P_TRANG_THAI
      WHERE MA_CAN_BO LIKE P_MA_CAN_BO
      AND MA_MENU=P_MA_MENU;
      ELSE  IF P_LOAI='X' THEN
      UPDATE PQ_CB_MENU
      SET XOA=P_TRANG_THAI
      WHERE MA_CAN_BO LIKE P_MA_CAN_BO
      AND MA_MENU=P_MA_MENU;
      ELSE  IF P_LOAI='XE' THEN
      UPDATE PQ_CB_MENU
      SET XEM=P_TRANG_THAI
      WHERE MA_CAN_BO LIKE P_MA_CAN_BO
      AND MA_MENU=P_MA_MENU;
      END IF;
      END IF;
      END IF;
      END IF;
       P_VAL := 'THANH_CONG';
    ELSE
     --CAP_NHAT;
     IF P_LOAI='T' THEN
      INSERT INTO PQ_CB_MENU(MA_CB_MN,MA_CAN_BO,MA_MENU,THEM,SUA,XOA,XEM) VALUES
      (PQ_CB_MENU_SEQ.NEXTVAL,P_MA_CAN_BO,P_MA_MENU,P_TRANG_THAI,0,0,0);
      ELSE IF P_LOAI='S' THEN
      INSERT INTO PQ_CB_MENU(MA_CB_MN,MA_CAN_BO,MA_MENU,THEM,SUA,XOA,XEM) VALUES
      (PQ_CB_MENU_SEQ.NEXTVAL,P_MA_CAN_BO,P_MA_MENU,0,P_TRANG_THAI,0,0);
      ELSE  IF P_LOAI='X' THEN
     INSERT INTO PQ_CB_MENU(MA_CB_MN,MA_CAN_BO,MA_MENU,THEM,SUA,XOA,XEM) VALUES
      (PQ_CB_MENU_SEQ.NEXTVAL,P_MA_CAN_BO,P_MA_MENU,0,0,P_TRANG_THAI,0);
      ELSE  IF P_LOAI='XE' THEN
      INSERT INTO PQ_CB_MENU(MA_CB_MN,MA_CAN_BO,MA_MENU,THEM,SUA,XOA,XEM) VALUES
      (PQ_CB_MENU_SEQ.NEXTVAL,P_MA_CAN_BO,P_MA_MENU,0,0,0,P_TRANG_THAI);
      P_VAL := 'THANH_CONG';
      END IF;
    END IF;
   END IF;
   END IF;
  END IF;
  END;
  ------------------------------DC_PHAN_QUYEN------------------------------------------
PROCEDURE SELECT_DC_PHAN_QUYEN(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,B.TEN_MUC_DICH_SU_DUNG_DAT AS TEN_QUYEN, C.TEN_CAN_BO
      FROM DC_PHAN_QUYEN A, DC_MUC_DICH_SU_DUNG_DAT B, CB_CAN_BO C
      WHERE A.TEN_PHAN_QUYEN=B.MA_MUC_DICH_SU_DUNG_DAT
      AND A.MA_CAN_BO=C.MA_CAN_BO
      AND A.LOAI_PHAN_QUYEN LIKE 'MD'
      UNION
      SELECT A.*,B.TEN_HINH_THUC AS TEN_QUYEN , C.TEN_CAN_BO
      FROM DC_PHAN_QUYEN A, DC_HINH_THUC B, CB_CAN_BO C
      WHERE A.TEN_PHAN_QUYEN=B.MA_HINH_THUC
      AND A.MA_CAN_BO=C.MA_CAN_BO
      AND A.LOAI_PHAN_QUYEN LIKE 'HT'
       ;
  END;
------------------------------------------------------------------------------------
    PROCEDURE SELECT_DC_PHAN_QUYEN_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_PHAN_QUYEN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM DC_PHAN_QUYEN A
        WHERE A.MA_PHAN_QUYEN=P_MA_PHAN_QUYEN;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_DC_PHAN_QUYEN(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_PHAN_QUYEN    NVARCHAR2,
                           P_TEN_PHAN_QUYEN    NVARCHAR2,
                           P_MA_CAN_BO    INT,
                           P_LOAI     NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
  
    IF P_UPDATE_TYPE = 'THEM_MOI' THEN
        INSERT INTO DC_PHAN_QUYEN
          (MA_PHAN_QUYEN,
           TEN_PHAN_QUYEN,
           MA_CAN_BO,
           LOAI_PHAN_QUYEN)
        VALUES
          (DC_PHAN_QUYEN_SEQ.NEXTVAL,
           P_TEN_PHAN_QUYEN,
           P_MA_CAN_BO,
           P_LOAI);
        P_VAL := 'THANH_CONG';
    ELSE
      
        UPDATE DC_PHAN_QUYEN
           SET TEN_PHAN_QUYEN    = P_TEN_PHAN_QUYEN,
               MA_CAN_BO    = P_MA_CAN_BO,
               LOAI_PHAN_QUYEN     = P_LOAI
         WHERE MA_PHAN_QUYEN LIKE P_MA_PHAN_QUYEN;
        P_VAL := 'THANH_CONG';
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_DC_PHAN_QUYEN(P_VAL   OUT NVARCHAR2, P_MA_PHAN_QUYEN NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_PHAN_QUYEN WHERE MA_PHAN_QUYEN LIKE P_MA_PHAN_QUYEN;
    P_VAL := 'THANH_CONG';
   END;  
   -------------------------------------------------------------------------------

PROCEDURE SELECT_CB_CAN_BO(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
        FROM CB_CAN_BO A;
  END;
------------------------------------------------------------------------------------

PROCEDURE SELECT_DC_MUC_DICH_PHAN_QUYEN(P_CUR IN OUT MYCUR, P_MA_CAN_BO INT) AS
  BEGIN
    OPEN P_CUR FOR
     SELECT DISTINCT A.MA_MUC_DICH_SU_DUNG_DAT,CONCAT('(',CONCAT(CONCAT(CHUYEN_MUC_DICH ,')-'),ten_muc_dich_su_dung_dat)) as ten_muc_dich_su_dung_dat,A.STT_MUC_DICH_SU_DUNG_DAT FROM DC_MUC_DICH_SU_DUNG_DAT A, DC_PHAN_QUYEN B
      WHERE  A.MA_MUC_DICH_SU_DUNG_DAT=B.TEN_PHAN_QUYEN
      AND B.MA_CAN_BO=P_MA_CAN_BO
      AND LOAI_PHAN_QUYEN LIKE'MD'
         UNION 
      SELECT N'' AS MA_MUC_DICH_SU_DUNG_DAT,
            N'Tất cả' AS ten_muc_dich_su_dung_dat,
            -1 AS STT_MUC_DICH_SU_DUNG_DAT
           FROM DUAL
       ORDER BY STT_MUC_DICH_SU_DUNG_DAT , ten_muc_dich_su_dung_dat;
  END;
------------------------------------------------------------------------------------

PROCEDURE SELECT_DC_HINH_THUC_PHAN_QUYEN(P_CUR IN OUT MYCUR, P_MA_CAN_BO INT) AS
  BEGIN
    OPEN P_CUR FOR
    SELECT DISTINCT A.MA_HINH_THUC,A.TEN_HINH_THUC,A.STT_HINH_THUC FROM DC_HINH_THUC A, DC_PHAN_QUYEN B
    WHERE  A.MA_HINH_THUC=B.TEN_PHAN_QUYEN
    AND B.MA_CAN_BO=P_MA_CAN_BO
    AND LOAI_PHAN_QUYEN LIKE 'HT'
     UNION 
      SELECT N'' AS MA_HINH_THUC,
            N'Tất cả' AS TEN_HINH_THUC,
            -1 AS STT_HINH_THUC
           FROM DUAL
       ORDER BY STT_HINH_THUC , TEN_HINH_THUC;

  END;
------------------------------------------------------------------------------------
PROCEDURE UPDATE_DC_THUADAT_EXCEL(P_VAL            OUT NVARCHAR2,
                           P_LOAI_DAT    number,
                           P_SO_THUA    INT,
                           P_THUADATGOC    NVARCHAR2,
                           P_MA_PHUONG_XA NVARCHAR2,
                           --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_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 number,
                           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_MA_CAP_QUAN_LY   INT,
                           P_MA_HUYEN    NVARCHAR2,
                           P_PHUONG_THUC NVARCHAR2,
                           P_MA_QUOC_TICH NVARCHAR2,
                           P_SDT NVARCHAR2, 
                           P_GHI_CHU NVARCHAR2,
                           P_DAU_GIA INT,
                           P_TIEN_DAU_GIA FLOAT,
                           P_LY_DO NVARCHAR2,
                           P_TIEN_THUE NVARCHAR2,
                           P_MA_DOI_TUONG INT,
                           P_DIEN_TICH_SD FLOAT,
                           P_DON_VI_TO_CHUC_DAU_GIA NVARCHAR2
                           ) AS
 V_TON_TAI_MA  INT;
  BEGIN
    P_VAL := 'CO_LOI';
     SELECT COUNT(SO_THUA)
        INTO V_TON_TAI_MA
        FROM DC_THUADAT
       WHERE SO_THUA LIKE P_SO_THUA
       AND MA_MUC_DICH_SU_DUNG_DAT=P_MUC_DICH_SD
       AND MA_HINH_THUC=P_MA_HINH_THUC;
       
    IF V_TON_TAI_MA >0 THEN
      --UPDATE
       UPDATE DC_THUADAT SET
           MA_LOAI_DAT=P_LOAI_DAT,
           SO_THUA_GOC=P_THUADATGOC,
           MA_PHUONG_XA=P_MA_PHUONG_XA,
           ID_DONG=16,
           TRANG_THAI_SD=0,
           MA_MUC_DICH_SU_DUNG_DAT=P_MUC_DICH_SD,
           MA_HINH_THUC=P_MA_HINH_THUC
        WHERE SO_THUA=P_SO_THUA;
        
        UPDATE DC_LUY_TIEN SET
           NAM_CAP_PHAT=to_date(P_NAM_CAP_NHAT,'DDMMYYYY'),
           DIEN_TICH_THUA=P_DIEN_TICH,
           TOA_DO='',
           LA_DAT_CONG=0,
           TO_BAN_DO=P_TO_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_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'),
            MA_CAP_QUAN_LY=P_MA_CAP_QUAN_LY
           WHERE SU_DUNG=0 
           AND MA_THUA=(SELECT MAX(MA_THUA) AS MA_THUA FROM DC_THUADAT 
           WHERE SO_THUA=P_SO_THUA);
           
           IF(P_NGUOI_SU_DUNG IS NOT NULL) THEN
               UPDATE DC_HO_SO_DAT SET
               MA_QUAN_HUYEN=P_MA_HUYEN,
               TEN_DON_VI=P_NGUOI_SU_DUNG,
              PHUONG_THUC_THANH_TOAN=P_PHUONG_THUC,
              MA_QUOC_TICH=P_MA_QUOC_TICH,
              SDT=P_SDT,
               GHI_CHU=P_GHI_CHU,
               DAU_GIA=P_DAU_GIA,
               TIEN_DAU_GIA=P_TIEN_DAU_GIA,
               LY_DO=P_LY_DO,
               TRANG_THAI='HT',
               TIEN_THUE=P_TIEN_THUE,
               ID_DONG=22,
               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,
               THUOC_TO_CHUC=P_LA_THUOC_TO_CHUC,
               MA_MUC_DICH_SU_DUNG_DAT=P_MUC_DICH_SD,
               MA_HINH_THUC=P_MA_HINH_THUC
               WHERE MA_THUA=(SELECT MAX(MA_THUA) AS MA_THUA FROM DC_THUADAT WHERE SO_THUA=P_SO_THUA)
               ;
           END IF;
         
         P_VAL := 'THANH_CONG';
      ELSE
        
    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,
           16,
           0,
           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,'',0,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'),0,
           P_MA_CAP_QUAN_LY
           
     );  
      IF(P_NGUOI_SU_DUNG IS NOT NULL) THEN
               INSERT INTO DC_HO_SO_DAT
              (MA_HS_DAT,MA_QUAN_HUYEN,TEN_DON_VI,
              PHUONG_THUC_THANH_TOAN, MA_QUOC_TICH,SDT, GHI_CHU,
              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,THUOC_TO_CHUC,MA_MUC_DICH_SU_DUNG_DAT,MA_HINH_THUC)
            VALUES
              (DC_HO_SO_DAT_SEQ.NEXTVAL,P_MA_HUYEN, P_NGUOI_SU_DUNG,
              P_PHUONG_THUC, P_MA_QUOC_TICH,P_SDT, P_GHI_CHU, 
              DC_THUA_DAT_SEQ.CURRVAL, P_DAU_GIA, P_TIEN_DAU_GIA,
                 P_LY_DO,'HT',P_TIEN_THUE,22,P_MA_DOI_TUONG,
                 P_DIEN_TICH_SD,P_DON_VI_TO_CHUC_DAU_GIA,P_LA_THUOC_TO_CHUC,P_MUC_DICH_SD,P_MA_HINH_THUC );     
                         
           END IF;
        P_VAL := 'THANH_CONG';  
        END IF;
       
  END;
------------------------------------------------------------------------------------
------------------------------DC_LOG------------------------------------------
PROCEDURE SELECT_DC_LOG(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*, B.*
        FROM DC_LOG A, CB_CAN_BO B
        WHERE A.MA_CAN_BO=B.MA_CAN_BO
       ORDER BY A.MA_LOG;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_LOG(P_VAL            OUT NVARCHAR2,
                           P_MA_CAN_BO    NUMBER,
                           P_THAO_TAC    NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
        INSERT INTO DC_LOG
          (MA_LOG,
           MA_CAN_BO,
           THAO_TAC,
           THOI_GIAN)
        VALUES
          (DC_LOG_SEQ.NEXTVAL,
           P_MA_CAN_BO,
           P_THAO_TAC,
           SYSDATE);
        P_VAL := 'THANH_CONG';
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_DC_LOG(P_VAL   OUT NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_LOG ;
    P_VAL := 'THANH_CONG';
   END;  
 -----------------------
    -------------------------------------------------------------------------------
  PROCEDURE DELETE_DC_LOG_ROWS(P_VAL   OUT NVARCHAR2, P_MA_LOG NUMBER) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DC_LOG WHERE MA_LOG=P_MA_LOG;
    P_VAL := 'THANH_CONG';
   END;  
------------------------------------------------------------------------------------
PROCEDURE SELECT_PHX_ALL(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*
             
        FROM DM_PHUONG_XA A,  DM_QUAN_HUYEN BC
        WHERE A.MA_QUAN_HUYEN=BC.MA_QUAN_HUYEN AND BC.MA_TINH_THANH=N'TIEN_GIANG';
  END;
  ------------------------------DC_QUOC_TICH------------------------------------------
PROCEDURE SELECT_QUOC_TICH(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_QUOC_TICH = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_QUOC_TICH A
       ORDER BY A.STT_QUOC_TICH , A.TEN_QUOC_TICH;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_QUOC_TICH_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_QUOC_TICH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_QUOC_TICH = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_QUOC_TICH A
       WHERE A.MA_QUOC_TICH LIKE P_MA_QUOC_TICH;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_QUOC_TICH(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_QUOC_TICH     NVARCHAR2,
                           P_TEN_QUOC_TICH    NVARCHAR2,
                           P_STT_QUOC_TICH    INT,
                           P_TT_QUOC_TICH     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_QUOC_TICH)
        INTO V_TON_TAI_MA
        FROM DM_QUOC_TICH
       WHERE UPPER(MA_QUOC_TICH) LIKE UPPER(P_MA_QUOC_TICH);
      SELECT COUNT(MA_QUOC_TICH)
        INTO V_TON_TAI_TEN
        FROM DM_QUOC_TICH
       WHERE UPPER(TEN_QUOC_TICH) LIKE UPPER(P_TEN_QUOC_TICH);
      IF V_TON_TAI_MA > 0 OR P_MA_QUOC_TICH = '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_QUOC_TICH
          (MA_QUOC_TICH,
           TEN_QUOC_TICH,
           STT_QUOC_TICH,
           TT_QUOC_TICH)
        VALUES
          (P_MA_QUOC_TICH,
           P_TEN_QUOC_TICH,
           P_STT_QUOC_TICH,
           P_TT_QUOC_TICH);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_QUOC_TICH)
        INTO V_TON_TAI_TEN
        FROM DM_QUOC_TICH
       WHERE UPPER(TEN_QUOC_TICH) LIKE UPPER(P_TEN_QUOC_TICH)
         AND MA_QUOC_TICH NOT LIKE P_MA_QUOC_TICH;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_QUOC_TICH
           SET TEN_QUOC_TICH    = P_TEN_QUOC_TICH,
               STT_QUOC_TICH    = P_STT_QUOC_TICH,
               TT_QUOC_TICH     = P_TT_QUOC_TICH
         WHERE MA_QUOC_TICH LIKE P_MA_QUOC_TICH;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_QUOC_TICH(P_VAL   OUT NVARCHAR2, P_MA_QUOC_TICH NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_QUOC_TICH WHERE MA_QUOC_TICH LIKE P_MA_QUOC_TICH;
    P_VAL := 'THANH_CONG';
   END;

------------------------------------------------------------------------------------
PROCEDURE SELECT_THUADAT_TO(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.MA_THUA,CONCAT('Số thửa: ',CONCAT(CONCAT(A.SO_THUA ,' Số tờ: '), C.TO_BAN_DO)) as THANG_TO
        FROM DC_THUADAT A,DC_LUY_TIEN C
        WHERE A.MA_THUA=C.MA_THUA
        AND C.NAM_CAP_PHAT IN(SELECT MAX(NAM_CAP_PHAT) FROM DC_LUY_TIEN GROUP BY MA_THUA) 
       ORDER BY MA_THUA;
  END;
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------ 
end TNMT_DATCONG_DM;
/

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

end TNMT_DATCONG_TK;
/

prompt
prompt Creating package body TNMT_DUNGCHUNG
prompt ====================================
prompt
create or replace package body hai.TNMT_DUNGCHUNG is
  -------------------------------------------------------------------------------
  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_MA_CHUC_VU NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_CHUC_VU WHERE MA_CHUC_VU LIKE P_MA_CHUC_VU;
    P_VAL := 'THANH_CONG';
   END;    
------------------------------DM_DAN_TOC------------------------------------------
PROCEDURE SELECT_DAN_TOC(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_DAN_TOC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_DAN_TOC A
       ORDER BY A.STT_DAN_TOC , A.TEN_DAN_TOC;
  END;
-------------------------------------------------------------------------------
    PROCEDURE SELECT_DAN_TOC_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_DAN_TOC NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_DAN_TOC = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_DAN_TOC A
       WHERE A.MA_DAN_TOC LIKE P_MA_DAN_TOC;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_DAN_TOC(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_DAN_TOC     NVARCHAR2,
                           P_TEN_DAN_TOC    NVARCHAR2,
                           P_STT_DAN_TOC    INT,
                           P_TT_DAN_TOC     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_DAN_TOC)
        INTO V_TON_TAI_MA
        FROM DM_DAN_TOC
       WHERE UPPER(MA_DAN_TOC) LIKE UPPER(P_MA_DAN_TOC);
      SELECT COUNT(MA_DAN_TOC)
        INTO V_TON_TAI_TEN
        FROM DM_DAN_TOC
       WHERE UPPER(TEN_DAN_TOC) LIKE UPPER(P_TEN_DAN_TOC);
      IF V_TON_TAI_MA > 0 OR P_MA_DAN_TOC = '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_DAN_TOC
          (MA_DAN_TOC,
           TEN_DAN_TOC,
           STT_DAN_TOC,
           TT_DAN_TOC)
        VALUES
          (P_MA_DAN_TOC,
           P_TEN_DAN_TOC,
           P_STT_DAN_TOC,
           P_TT_DAN_TOC);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_DAN_TOC)
        INTO V_TON_TAI_TEN
        FROM DM_DAN_TOC
       WHERE UPPER(TEN_DAN_TOC) LIKE UPPER(P_TEN_DAN_TOC)
         AND MA_DAN_TOC NOT LIKE P_MA_DAN_TOC;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_DAN_TOC
           SET TEN_DAN_TOC    = P_TEN_DAN_TOC,
               STT_DAN_TOC    = P_STT_DAN_TOC,
               TT_DAN_TOC     = P_TT_DAN_TOC
         WHERE MA_DAN_TOC LIKE P_MA_DAN_TOC;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_DAN_TOC(P_VAL   OUT NVARCHAR2, P_MA_DAN_TOC NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_DAN_TOC WHERE MA_DAN_TOC LIKE P_MA_DAN_TOC;
    P_VAL := 'THANH_CONG';
   END;  

------------------------------DM_TON_GIAO------------------------------------------
PROCEDURE SELECT_TON_GIAO(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_TON_GIAO = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_TON_GIAO A
       ORDER BY A.STT_TON_GIAO , A.Ten_Ton_Giao;
  END;
   -------------------------------------------------------------------------------
    PROCEDURE SELECT_TON_GIAO_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_TON_GIAO NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_TON_GIAO = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_TON_GIAO A
       WHERE A.MA_TON_GIAO LIKE P_MA_TON_GIAO;
  END;
    -------------------------------------------------------------------------------
  PROCEDURE UPDATE_TON_GIAO(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_TON_GIAO     NVARCHAR2,
                           P_TEN_TON_GIAO    NVARCHAR2,
                           P_STT_TON_GIAO    INT,
                           P_TT_TON_GIAO     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_TON_GIAO)
        INTO V_TON_TAI_MA
        FROM DM_TON_GIAO
       WHERE UPPER(MA_TON_GIAO) LIKE UPPER(P_MA_TON_GIAO);
      SELECT COUNT(MA_TON_GIAO)
        INTO V_TON_TAI_TEN
        FROM DM_TON_GIAO
       WHERE UPPER(TEN_TON_GIAO) LIKE UPPER(P_TEN_TON_GIAO);
      IF V_TON_TAI_MA > 0 OR P_MA_TON_GIAO = '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_TON_GIAO
          (MA_TON_GIAO,
           TEN_TON_GIAO,
           STT_TON_GIAO,
           TT_TON_GIAO)
        VALUES
          (P_MA_TON_GIAO,
           P_TEN_TON_GIAO,
           P_STT_TON_GIAO,
           P_TT_TON_GIAO);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_TON_GIAO)
        INTO V_TON_TAI_TEN
        FROM DM_TON_GIAO
       WHERE UPPER(TEN_TON_GIAO) LIKE UPPER(P_TEN_TON_GIAO)
         AND MA_TON_GIAO NOT LIKE P_MA_TON_GIAO;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_TON_GIAO
           SET TEN_TON_GIAO    = P_TEN_TON_GIAO,
               STT_TON_GIAO    = P_STT_TON_GIAO,
               TT_TON_GIAO     = P_TT_TON_GIAO
         WHERE MA_TON_GIAO LIKE P_MA_TON_GIAO;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_TON_GIAO(P_VAL   OUT NVARCHAR2, P_MA_TON_GIAO NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_TON_GIAO WHERE MA_TON_GIAO LIKE P_MA_TON_GIAO;
    P_VAL := 'THANH_CONG';
   END;  

------------------------------DM_QUOC_TICH------------------------------------------
PROCEDURE SELECT_QUOC_TICH(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_QUOC_TICH = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_QUOC_TICH A
       ORDER BY A.STT_QUOC_TICH , A.Ten_QUOC_TICH;
  END;
   -------------------------------------------------------------------------------
    PROCEDURE SELECT_QUOC_TICH_DETAIL(P_CUR  IN OUT MYCUR,
                                  P_MA_QUOC_TICH NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR
      SELECT A.*,
             CASE
               WHEN A.TT_QUOC_TICH = 0 THEN
                'Mở'
               ELSE
                'Ðóng'
             END AS TEN_TRANG_THAI
        FROM DM_QUOC_TICH A
       WHERE A.MA_QUOC_TICH LIKE P_MA_QUOC_TICH;
  END;
    -------------------------------------------------------------------------------
 
  PROCEDURE UPDATE_QUOC_TICH(P_VAL            OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_QUOC_TICH     NVARCHAR2,
                           P_TEN_QUOC_TICH    NVARCHAR2,
                           P_STT_QUOC_TICH    INT,
                           P_TT_QUOC_TICH     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_QUOC_TICH)
        INTO V_TON_TAI_MA
        FROM DM_QUOC_TICH
       WHERE UPPER(MA_QUOC_TICH) LIKE UPPER(P_MA_QUOC_TICH);
      SELECT COUNT(MA_QUOC_TICH)
        INTO V_TON_TAI_TEN
        FROM DM_QUOC_TICH
       WHERE UPPER(TEN_QUOC_TICH) LIKE UPPER(P_TEN_QUOC_TICH);
      IF V_TON_TAI_MA > 0 OR P_MA_QUOC_TICH = '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_QUOC_TICH
          (MA_QUOC_TICH,
           TEN_QUOC_TICH,
           STT_QUOC_TICH,
           TT_QUOC_TICH)
        VALUES
          (P_MA_QUOC_TICH,
           P_TEN_QUOC_TICH,
           P_STT_QUOC_TICH,
           P_TT_QUOC_TICH);
        P_VAL := 'THANH_CONG';
      END IF;
    ELSE
      SELECT COUNT(MA_QUOC_TICH)
        INTO V_TON_TAI_TEN
        FROM DM_QUOC_TICH
       WHERE UPPER(TEN_QUOC_TICH) LIKE UPPER(P_TEN_QUOC_TICH)
         AND MA_QUOC_TICH NOT LIKE P_MA_QUOC_TICH;
      IF V_TON_TAI_TEN > 0 THEN
        -- kiem tra trung ten khac no
        P_VAL := 'TON_TAI_TEN';
      ELSE
        UPDATE DM_QUOC_TICH
           SET TEN_QUOC_TICH    = P_TEN_QUOC_TICH,
               STT_QUOC_TICH    = P_STT_QUOC_TICH,
               TT_QUOC_TICH     = P_TT_QUOC_TICH
         WHERE MA_QUOC_TICH LIKE P_MA_QUOC_TICH;
        P_VAL := 'THANH_CONG';
      END IF;
    END IF;
  
  END;
   -------------------------------------------------------------------------------
  PROCEDURE DELETE_QUOC_TICH(P_VAL   OUT NVARCHAR2, P_MA_QUOC_TICH NVARCHAR2) AS
  BEGIN
    P_VAL := 'CO_LOI';
    DELETE FROM DM_QUOC_TICH WHERE MA_QUOC_TICH LIKE P_MA_QUOC_TICH;
    P_VAL := 'THANH_CONG';
   END;

 ------------------------------DM_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 DM_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 DM_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 DM_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 DM_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 DM_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 DM_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 DM_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 DM_LOAI_VAN_BAN WHERE MA_LOAI_VAN_BAN LIKE P_MA_LOAI_VAN_BAN;
    P_VAL := 'THANH_CONG';
   END;  
end TNMT_DUNGCHUNG;
/

prompt
prompt Creating package body TNMT_LICH_CONG_TAC
prompt ========================================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.TNMT_LICH_CONG_TAC IS
--------------------------------
PROCEDURE UPDATE_LICH_CONG_TAC(P_VAL OUT NVARCHAR2,P_STT INT,P_MA_CAN_BO NVARCHAR2, P_NGAY_CONG_TAC NVARCHAR2,P_NGAY_KET_THUC NVARCHAR2,P_VAN_TAT NVARCHAR2, P_NOI_DUNG NVARCHAR2) AS
  BEGIN
    P_VAL:='CL';
    IF P_STT IS NULL THEN
      INSERT INTO LICH_CONG_TAC(MA_CAN_BO,NGAY_CONG_TAC,NGAY_KET_THUC,VAN_TAT,NOI_DUNG_CONG_VIEC)
                  VALUES(P_MA_CAN_BO,P_NGAY_CONG_TAC,P_NGAY_KET_THUC,P_VAN_TAT,P_NOI_DUNG);
      P_VAL:='TC';
    ELSE
      UPDATE LICH_CONG_TAC SET NGAY_CONG_TAC=P_NGAY_CONG_TAC,
                               NGAY_KET_THUC=P_NGAY_KET_THUC,
                               VAN_TAT      =P_VAN_TAT,
                               NOI_DUNG_CONG_VIEC=P_NOI_DUNG;
     P_VAL:='TC';
   END IF;
END;
END TNMT_LICH_CONG_TAC;
/

prompt
prompt Creating package body TNMT_QUANLYNHANUOC
prompt ========================================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.TNMT_QUANLYNHANUOC IS
--============== lop tap huan
PROCEDURE SELECT_LOP_TAP_HUANS(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.*,B.TEN_LOP_TAP_HUAN FROM TT_LOP_TAP_HUAN A, DM_LOP_TAP_HUAN B
    WHERE A.MA_LOP_TAP_HUAN=B.MA_LOP_TAP_HUAN
    AND (B.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL);
  END;
--============== llop tap huan
PROCEDURE SELECT_LOP_TAP_HUAN(P_CUR IN OUT MYCUR, P_MA_LOP_TAP_HUAN INT,P_NGAY_MO_LOP NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_LOP_TAP_HUAN A
    WHERE A.MA_LOP_TAP_HUAN=P_MA_LOP_TAP_HUAN
    AND A.NGAY_MO_LOP=P_NGAY_MO_LOP;
  END;
--============== select lop tap huan
PROCEDURE SELECT_DM_LOP_TAP_HUAN(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_LOP_TAP_HUAN, A.TEN_LOP_TAP_HUAN FROM DM_LOP_TAP_HUAN A
    WHERE (A.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL);
  END;
--============== update lop tap huan
PROCEDURE UPDATE_LOP_TAP_HUAN(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE NVARCHAR2,P_MA_LOP_TAP_HUAN NVARCHAR2,
                              P_NGAY_MO_LOP NVARCHAR2, P_SO_LUONG NVARCHAR2,P_SO_LUONG_DI_HOC NVARCHAR2,
                              P_GHI_CHU NVARCHAR2, P_TRANG_THAI NVARCHAR2) AS
  V_KT INT;
  BEGIN
    P_VAL:='CL';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(MA_LOP_TAP_HUAN) INTO V_KT FROM TT_LOP_TAP_HUAN
      WHERE MA_LOP_TAP_HUAN=P_MA_LOP_TAP_HUAN AND NGAY_MO_LOP=P_NGAY_MO_LOP;
      IF V_KT=0 THEN
        INSERT INTO TT_LOP_TAP_HUAN(MA_LOP_TAP_HUAN,NGAY_MO_LOP,SO_LUONG,SO_LUONG_DI_HOC,GHI_CHU,TRANG_THAI)
                         VALUES(P_MA_LOP_TAP_HUAN,P_NGAY_MO_LOP,P_SO_LUONG,P_SO_LUONG_DI_HOC,P_GHI_CHU,P_TRANG_THAI);
        P_VAL:='TC';
      ELSE
        P_VAL:='TON_TAI';
      END IF;
    ELSE
      UPDATE TT_LOP_TAP_HUAN SET SO_LUONG=P_SO_LUONG,
                              SO_LUONG_DI_HOC=P_SO_LUONG_DI_HOC,
                              GHI_CHU=P_GHI_CHU,
                              TRANG_THAI=P_TRANG_THAI
             WHERE MA_LOP_TAP_HUAN=P_MA_LOP_TAP_HUAN
             AND NGAY_MO_LOP=P_NGAY_MO_LOP;
      P_VAL:='TC';
    END IF;
  END;  
--============== delete lop tap huan
PROCEDURE DELETE_LOP_TAP_HUAN(P_VAL OUT NVARCHAR2, P_NGUOI_XOA INT,P_MA_LOP_TAP_HUAN INT, P_NGAY_MO_LOP NVARCHAR2) AS
  BEGIN
    IF P_NGUOI_XOA='0' THEN
      DELETE FROM TT_LOP_TAP_HUAN WHERE MA_LOP_TAP_HUAN=P_MA_LOP_TAP_HUAN AND NGAY_MO_LOP=P_NGAY_MO_LOP;
      P_VAL:='TC';
    ELSE 
      P_VAL:='CL';
    END IF;
  END;               
END TNMT_QUANLYNHANUOC;
/

prompt
prompt Creating package body TNMT_TEST
prompt ===============================
prompt
create or replace package body hai.TNMT_TEST wrapped
a000000
367
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
b
1905 566
Mb760zsUZPoMOigu6u2RAGxt0Dgwg82TDPHrfC8BzV7S/72N7FFU02VcGDtX4rTCLUNrwiXx
pBfuOXasbAzcFS7I5FweCxJAR2mOMIItGisK+O1IHj2JFEXIRhUt42Tdk/9EsCoHoMuswsVm
q3iUAuzJIQaskVjOUHE+H1KX/xMJoi5EPtZR7sgPQ9nijq3zzKjq8zmKN8OzWMMgxGf8po9a
t4n6nabpoYj1i7BRJwcOv+FduYHiLClP43eO0KbPetmkhaRk5ww2JLeOy6U5U3Yq6uZ1teT5
KiEH3skgnu7QliS8EPmRVuhWp/umsFlZ+dRssOWFN8dif2fhzXVlEF1Il5c1rrNL4k3go68I
gztun4NbO/7tHW17AaFgMmTReg6B6GLgLj8aPZ+I0POykZ1erZxC3e/YWuloS6JzImObwyfm
cREZ3BtdxWYJnp8bz7CEQHBhNYcoCO8vn+Z6KKXT1ATJefqvR/mmSTb7WanUZetxgMAdJaU3
kZItlBoLAM2KS6WGNVVWav/ZmB1G8dCT90D4OIa5WUpTHY5/M/FjH/yeiJjxvo1d456nRu/t
NUkwXtQ2wWqF1JJxiVGaXw8M5Tkro+5TkaEIRB1xtgqueI7cJEs8dD20I4KnISWaaRj2Uezy
rgDZOeKLJBctAkrxKKwfwjNa5dMm5IofMMVbBVjSkXBo01+LZpwK0oezxANNEQSayjDElw84
dq8M5Er+LpMlJ9Y1mvKaVVOhB81pNpugtNcQmlDU54R5n+Vgd/oJK+sYvwefp+gjXwOotNRI
8Ht2JwURreDwf2PEnxDU613Nlk9mr866iw5rDd8BY9PN3z3Qf4oo1FbhIZMP9ukUat22GB6C
4qyINU7ENPltt0FAnL0ZiOwi2gwSumbQQPia+BIP6sS97UByxRlrqT3rPWRPRypDcCG0HUdl
hLBCmDyhjm3+zQkkQoonfTMzgpfF096P4IjRWNF7eM24lGvZXXykBLHxlJzx6RX8z5A/EuLo
8htxfb7Ks5O4+Q8bDtVdAhgignlKQ/LKrre969zPk0lZMEd9NnmAgRBCGvJQ2FtYcHQNY8j6
CQTU6uc5pcU5XuzklJ4VRJ2AoX0RdlapM3xJG6CbmPB1fCYIkT44Sd3xjkVkoV4E9Q9vlGEa
Zy4pK/GsO7GOYNPqEZ5V4HMqmk3CnMwfzoJOrm2Zsg2TMu47RE75QN/bH1cQ/K0yQJ1IgO1r
vxIMGw5hnStCzl1ESOTEHxz9mORWiH8f62hiJTtl5PNzOERRTh5EJl+V4xk5Y6tSqrJIYmCL
iCTj007VAID9F3/Sq+BK3K62xR6Rxqx7MCJYZQDLr8K4/jpM+lEZxrUyitbqnaYk08Sm
/

prompt
prompt Creating package body TNMT_THANHTRABAOCAO
prompt =========================================
prompt
CREATE OR REPLACE PACKAGE BODY HAI.TNMT_THANHTRABAOCAO IS
--===========//// BAO CAO CHUYEN NGANH TNMT DANH SACH//=============--
PROCEDURE BAO_CAO_CHUYEN_NGANH_LIST(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2,P_DEN_NGAY NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT KQ.* FROM(SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT
    UNION SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA FROM TT_THANH_TRA A
    WHERE SO_THANH_TRA NOT IN(SELECT SO_THANH_TRA FROM TT_THANH_TRA A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT)) KQ
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (KQ.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL);
  END;
--===========//// BAO CAO LINH VUC DANH SACH//=============--
PROCEDURE BAO_CAO_LINH_VUC_LIST(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2,P_DEN_NGAY NVARCHAR2,P_MA_LINH_VUC NVARCHAR2,P_THAM_NHUNG INT, P_THANH_TRA_LAI INT, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    IF P_THAM_NHUNG=1 AND P_THANH_TRA_LAI!=1 THEN
      OPEN P_CUR FOR SELECT KQ.* FROM(SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
      WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT
      UNION SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A
      WHERE SO_THANH_TRA NOT IN(SELECT SO_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
      WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT)) KQ
      WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND (KQ.SO_TIEN_THAM_NHUNG IS NOT NULL OR KQ.TAI_SAN_THAM_NHUNG IS NOT NULL 
          OR KQ.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR KQ.DAT_THAM_NHUNG IS NOT NULL)
      AND (KQ.MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
      AND (KQ.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL);
   ELSIF P_THANH_TRA_LAI=1 AND P_THAM_NHUNG!=1 THEN
      OPEN P_CUR FOR SELECT DISTINCT KQ.SO_THANH_TRA,KQ.* FROM(SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
      WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT
      UNION SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A
      WHERE SO_THANH_TRA NOT IN(SELECT SO_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
      WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT)) KQ,TT_THANH_TRA_LAI TTL
      WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND (KQ.MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
      AND (KQ.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
      AND KQ.SO_THANH_TRA=TTL.SO_THANH_TRA;
   ELSIF P_THAM_NHUNG=1 AND P_THANH_TRA_LAI=1 THEN
      OPEN P_CUR FOR SELECT DISTINCT KQ.SO_THANH_TRA,KQ.* FROM(SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
      WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT
      UNION SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A
      WHERE SO_THANH_TRA NOT IN(SELECT SO_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
      WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT)) KQ,TT_THANH_TRA_LAI TTL
      WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND (KQ.MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
      AND (KQ.SO_TIEN_THAM_NHUNG IS NOT NULL OR KQ.TS_QUY_DOI_THANH_TIEN IS NOT NULL
          OR KQ.TAI_SAN_THAM_NHUNG IS NOT NULL OR KQ.DAT_THAM_NHUNG IS NOT NULL
          OR TTL.SO_TIEN_THAM_NHUNG IS NOT NULL OR TTL.TAI_SAN_THAM_NHUNG IS NOT NULL
          OR TTL.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR TTL.DAT_THAM_NHUNG IS NOT NULL)
      AND (KQ.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
      AND KQ.SO_THANH_TRA=TTL.SO_THANH_TRA; 
   ELSE
     OPEN P_CUR FOR SELECT KQ.* FROM(SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
      WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT
      UNION SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A
      WHERE SO_THANH_TRA NOT IN(SELECT SO_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
      WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT)) KQ
      WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND (KQ.MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
      AND (KQ.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL);
   END IF;
  END;
--==========// BAO CAO CHUYEN NGANH TNMT
PROCEDURE BAO_CAO_CHUYEN_NGANH(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2,P_DEN_NGAY NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT KQ.*,TEN_CO_QUAN FROM (SELECT TS_THANH_TRA,CO_DOAN_THANH_TRA,CO_KE_HOACH,TS_CA_NHAN,TT_CA_NHAN,KT_CA_NHAN,
    TS_TO_CHUC,TT_TO_CHUC,KT_TO_CHUC,LBB_CA_NHAN,QD_CA_NHAN,LBB_TO_CHUC, QD_TO_CHUC,SO_TIEN_TICH_THU,
    SO_TIEN_TIEU_HUY,SO_TIEN_XU_PHAT_CN,SO_TIEN_XU_PHAT_TC,SO_TIEN_TICH_THU_CN,
    SO_TIEN_TIEU_HUY_CN, SO_TIEN_TICH_THU_TC,SO_TIEN_TIEU_HUY_TC, A.MA_CO_QUAN FROM
    --tong so thanh tra
    (SELECT COUNT(SO_THANH_TRA) AS TS_THANH_TRA, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) A,
    --co doan thanh tra
    (SELECT COUNT(SO_THANH_TRA) AS CO_DOAN_THANH_TRA, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND MA_DOAN_TT IS NOT NULL
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) B,
    --co ke hoach
    (SELECT COUNT(SO_THANH_TRA) AS CO_KE_HOACH, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND MA_KE_HOACH_TT IS NOT NULL
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) C,
    --tong so ca nhan
    (SELECT COUNT(SO_THANH_TRA) AS TS_CA_NHAN, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) D,
    --thanh tra ca nhan 
    (SELECT COUNT(SO_THANH_TRA) AS TT_CA_NHAN, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1
    AND LOAI_HINH_THANH_TRA='TT'
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) E,
    --kiem tra ca nhan
    (SELECT COUNT(SO_THANH_TRA) AS KT_CA_NHAN, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1
    AND LOAI_HINH_THANH_TRA='KT'
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) F,
    --tong so to chuc
    --tong so ca nhan
    (SELECT COUNT(SO_THANH_TRA) AS TS_TO_CHUC, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) G,
    --thanh tra to chuc
    (SELECT COUNT(SO_THANH_TRA) AS TT_TO_CHUC, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0
    AND LOAI_HINH_THANH_TRA='TT'
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) H,
    --kiem tra to chuc
    (SELECT COUNT(SO_THANH_TRA) AS KT_TO_CHUC, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0
    AND LOAI_HINH_THANH_TRA='KT'
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) I,
    --tong so ca nhan vi pham
    (SELECT COUNT(SO_THANH_TRA) AS LBB_CA_NHAN, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1--0 to chuc, 1 ca nhan
    AND HINH_THUC_XU_PHAT=0 --0 lap bien ban 1 ra quyet dinh
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) J,
    --tong to chuc vi pham
    (SELECT COUNT(SO_THANH_TRA) AS LBB_TO_CHUC, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0--0 to chuc, 1 ca nhan
    AND HINH_THUC_XU_PHAT=0--0 lap bien ban 1 ra quyet dinh
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) K,
    --tong ca nhan ra quyet dinh phat
    (SELECT COUNT(SO_THANH_TRA) AS QD_CA_NHAN, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1--0 to chuc, 1 ca nhan
    AND HINH_THUC_XU_PHAT=1--0 lap bien ban 1 ra quyet dinh
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) L,
    --tong to chuc ra quyet dinh phat
    (SELECT COUNT(SO_THANH_TRA) AS QD_TO_CHUC, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0--0 to chuc, 1 ca nhan
    AND HINH_THUC_XU_PHAT=1--0 lap bien ban 1 ra quyet dinh
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) M,
    --so tien tich thu
    (SELECT SUM(SO_TIEN_TICH_THU) AS SO_TIEN_TICH_THU, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) N,
     --so tien tich thu ca nhan
    (SELECT SUM(SO_TIEN_TICH_THU) AS SO_TIEN_TICH_THU_CN, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) O,
    --so tien tich thu to chuc
    (SELECT SUM(SO_TIEN_TICH_THU) AS SO_TIEN_TICH_THU_TC, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) P,
    --so tien tieu huy
    (SELECT SUM(SO_TIEN_TIEU_HUY) AS SO_TIEN_TIEU_HUY, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) Q,
    --so tien tieu huy ca nhan
    (SELECT SUM(SO_TIEN_TIEU_HUY) AS SO_TIEN_TIEU_HUY_CN, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1--0 to chuc, 1 ca nhan
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) R,
    --so tien tieu huy to chuc
    (SELECT SUM(SO_TIEN_TIEU_HUY) AS SO_TIEN_TIEU_HUY_TC, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0--0 to chuc, 1 ca nhan
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) S,
    --tong so tien xu phat ca nhan
    (SELECT SUM(SO_TIEN_XU_PHAT) AS SO_TIEN_XU_PHAT_CN, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1--0 to chuc, 1 ca nhan
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) T,
    --tong so tien xu phat to chuc
    (SELECT SUM(SO_TIEN_XU_PHAT) AS SO_TIEN_XU_PHAT_TC, MA_CO_QUAN FROM TT_THANH_TRA 
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0--0 to chuc, 1 ca nhan
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    GROUP BY MA_CO_QUAN) U
    WHERE A.MA_CO_QUAN=B.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=C.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=D.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=E.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=F.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=G.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=H.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=I.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=J.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=K.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=L.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=M.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=N.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=O.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=P.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=Q.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=R.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=S.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=T.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=U.MA_CO_QUAN(+)) KQ, CB_CO_QUAN W
    WHERE KQ.MA_CO_QUAN=W.MA_CO_QUAN;
  END; 
PROCEDURE BAO_CAO_LINH_VUC(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2,P_DEN_NGAY NVARCHAR2,P_MA_LINH_VUC NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT KQ.*,TEN_CO_QUAN FROM (SELECT TRONG_KY_THANH_TRA,KY_TRUOC_CHUYEN_SANG,TT_THEO_KE_HOACH,TT_KO_THEO_KE_HOACH,
    KIEN_NGHI_THU_HOI, SO_TIEN_THU_HOI,KIEN_NGHI_KHAC, SO_TIEN_KHAC,XU_LY_HANH_CHINH_TO_CHUC,
    XU_LY_HANH_CHINH_CA_NHAN,CHUYEN_VU_VIEC_QUA_CQ,CHUYEN_DOI_TUONG_QUA_CQ,TS_VI_PHAM,
    DAT_PHAI_THU, SO_TIEN_PHAI_THU,DAT_DA_THU,SO_TIEN_DA_THU,DAT_LAN_CHIEM,
    CAP_SAI_DOI_TUONG,CAP_TRAI_THAM_QUYEN, CAP_GCN_SAI,SAI_QUY_DINH,SAI_MUC_DICH,
    BO_HOANG_HOA,VI_PHAM_KHAC, A.MA_CO_QUAN FROM
    --trong ky thanh tra
    (SELECT COUNT(SO_THANH_TRA) AS TRONG_KY_THANH_TRA,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) A,
    --ky truoc chuyen sang
    (SELECT COUNT(SO_THANH_TRA) AS KY_TRUOC_CHUYEN_SANG,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) B,
    --theo ke hoach
    (SELECT COUNT(SO_THANH_TRA) AS TT_THEO_KE_HOACH,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND MA_KE_HOACH_TT IS NOT NULL
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) C,
    --theo ke hoach
    (SELECT COUNT(SO_THANH_TRA) AS TT_KO_THEO_KE_HOACH,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND MA_KE_HOACH_TT IS NULL
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) D,
    --kien nghi thu hoi
    (SELECT SUM(KIEN_NGHI_THU_HOI) AS KIEN_NGHI_THU_HOI,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) E,
    --so tien thu hoi
    (SELECT SUM(SO_TIEN_THU_HOI) AS SO_TIEN_THU_HOI,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) F,
    --kien nghi khac
    (SELECT SUM(KIEN_NGHI_KHAC) AS KIEN_NGHI_KHAC,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) G,
    --so tien khac
    (SELECT SUM(SO_TIEN_KHAC) AS SO_TIEN_KHAC,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) H,
    --xu ly hanh chinh to chuc
    (SELECT COUNT(SO_THANH_TRA) AS XU_LY_HANH_CHINH_TO_CHUC,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0--0: TO CHUC, 1: CA NHAN
    AND KIEN_NGHI_XU_LY = 0 --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) I,
    --xu ly hanh chinh ca nhan
    (SELECT COUNT(SO_THANH_TRA) AS XU_LY_HANH_CHINH_CA_NHAN, MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1--0: TO CHUC, 1: CA NHAN
    AND KIEN_NGHI_XU_LY = 0 --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) J,
    --chuyen vu viec qua co quan khac
    (SELECT COUNT(SO_THANH_TRA) AS CHUYEN_VU_VIEC_QUA_CQ,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND KIEN_NGHI_XU_LY = 1 --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) K,
    --chuyen doi tuong qua co quan khac
    (SELECT COUNT(SO_THANH_TRA) AS CHUYEN_DOI_TUONG_QUA_CQ,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND KIEN_NGHI_XU_LY = 2 --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) L,
    --tong so vi pham
    (SELECT COUNT(SO_THANH_TRA) AS TS_VI_PHAM, MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND KIEN_NGHI_XU_LY IS NOT NULL --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) M,
    --dat phai thu
    (SELECT SUM(DAT_PHAI_THU) AS DAT_PHAI_THU, MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) N,
     --so tien phai thu
    (SELECT SUM(SO_TIEN_PHAI_THU) AS SO_TIEN_PHAI_THU, MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) O,
    --dat da thu
    (SELECT SUM(DAT_DA_THU) AS DAT_DA_THU, MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) P,
    --so tien da thu
    (SELECT SUM(SO_TIEN_DA_THU) AS SO_TIEN_DA_THU, MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) Q,
    --ket qua xu ly hanh chinh to chuc
    (SELECT COUNT(SO_THANH_TRA) AS KET_QUA_XU_LY_HANH_CHINH_TC,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=0--0: TO CHUC, 1: CA NHAN
    AND KET_QUA_XU_LY = 0 --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) R,
    --ket qua xu ly hanh chinh ca nhan
    (SELECT COUNT(SO_THANH_TRA) AS KET_QUA_XU_LY_HANH_CHINH_CN, MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND LOAI_HINH_DOANH_NGHIEP=1--0: TO CHUC, 1: CA NHAN
    AND KET_QUA_XU_LY = 0 --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) S,
    --khoi to vu viec qua co quan khac
    (SELECT COUNT(SO_THANH_TRA) AS KT_VU_VIEC,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND KET_QUA_XU_LY = 1 --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) T,
    --khoi to doi tuong qua co quan khac
    (SELECT COUNT(SO_THANH_TRA) AS KT_DOI_TUONG,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND KIEN_NGHI_XU_LY = 2 --0: hanh chinh, 1: chuyen vu, 2: chuyen doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) U,
    --dang vi pham dat lan chiem
    (SELECT SUM(VI_PHAM) AS DAT_LAN_CHIEM,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC A,TT_THANH_TRA_CO_DANG_VI_PHAM B
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND A.SO_THANH_TRA=B.SO_THANH_TRA
    AND B.MA_DANG_VI_PHAM=1 --dat lan chiem
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) V,
    --dang vi pham cap sai doi tuong
    (SELECT SUM(VI_PHAM) AS CAP_SAI_DOI_TUONG,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC A,TT_THANH_TRA_CO_DANG_VI_PHAM B
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND A.SO_THANH_TRA=B.SO_THANH_TRA
    AND B.MA_DANG_VI_PHAM=2 --dat cap sai doi tuong
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) W,
    --dang vi pham cap trai tham quyen
    (SELECT SUM(VI_PHAM) AS CAP_TRAI_THAM_QUYEN,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC A,TT_THANH_TRA_CO_DANG_VI_PHAM B
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND A.SO_THANH_TRA=B.SO_THANH_TRA
    AND B.MA_DANG_VI_PHAM=3 --dat cap trai tham quyen
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) X,
    --dang vi pham cap GCN sai
    (SELECT SUM(VI_PHAM) AS CAP_GCN_SAI,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC A,TT_THANH_TRA_CO_DANG_VI_PHAM B
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND A.SO_THANH_TRA=B.SO_THANH_TRA
    AND B.MA_DANG_VI_PHAM=4 --dat cap GCN sai
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) Y,
    --dang vi pham chuyen nhuong, cho thue khong dung quy dinh
    (SELECT SUM(VI_PHAM) AS SAI_QUY_DINH,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC A,TT_THANH_TRA_CO_DANG_VI_PHAM B
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND A.SO_THANH_TRA=B.SO_THANH_TRA
    AND B.MA_DANG_VI_PHAM=5 --chuyen nhuong, cho thue khong dung quy dinh
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) Z,
    --dang vi pham su dung dat khong dung muc dich
    (SELECT SUM(VI_PHAM) AS SAI_MUC_DICH,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC A,TT_THANH_TRA_CO_DANG_VI_PHAM B
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND A.SO_THANH_TRA=B.SO_THANH_TRA
    AND B.MA_DANG_VI_PHAM=6 --su dung khong dung muc dich
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) AA,
    --dang vi pham bo hoang hoa
    (SELECT SUM(VI_PHAM) AS BO_HOANG_HOA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC A,TT_THANH_TRA_CO_DANG_VI_PHAM B
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND A.SO_THANH_TRA=B.SO_THANH_TRA
    AND B.MA_DANG_VI_PHAM=7 --bo hoang hoa
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) AB,
    --dang vi pham khac
    (SELECT SUM(VI_PHAM) AS VI_PHAM_KHAC,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC A,TT_THANH_TRA_CO_DANG_VI_PHAM B
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND A.SO_THANH_TRA=B.SO_THANH_TRA
    AND B.MA_DANG_VI_PHAM=8 --vi pham khac
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) AC
    WHERE A.MA_CO_QUAN=B.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=C.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=D.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=E.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=F.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=G.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=H.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=I.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=J.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=K.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=L.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=M.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=N.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=O.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=P.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=Q.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=R.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=S.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=T.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=U.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=V.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=W.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=X.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=Y.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=Z.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=AA.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=AB.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=AC.MA_CO_QUAN(+)) KQ, CB_CO_QUAN W
    WHERE KQ.MA_CO_QUAN=W.MA_CO_QUAN;
  END;
--BAO CAO THANH TRA LAI
PROCEDURE BAO_CAO_THANH_TRA_LAI(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2,P_DEN_NGAY NVARCHAR2,P_MA_LINH_VUC NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT KQ.*,TEN_CO_QUAN FROM (SELECT TRONG_KY_THANH_TRA,KY_TRUOC_CHUYEN_SANG,
     VI_PHAM_TRINH_TU,NOI_DUNG_KL_KO_PHU_HOP,SAI_LAM_KHI_KL,CO_Y_KL_SAI,VI_PHAM_NGHIEM_TRONG,
     SO_DON_VI_THANH_TRA,TS_VI_PHAM,SO_TIEN_VI_PHAM,DAT_VI_PHAM,KIEN_NGHI_THU_HOI,SO_TIEN_THU_HOI,
     KIEN_NGHI_KHAC,SO_TIEN_KHAC,KIEN_NGHI_XU_LY_HC_TO_CHUC,KIEN_NGHI_XU_LY_HC_CA_NHAN,
     KIEN_NGHI_CHUYEN_VU_VIEC,KIEN_NGHI_CHUYEN_DOI_TUONG, KET_QUA_XU_LY_HC_TO_CHUC,KET_QUA_XU_LY_HC_CA_NHAN,
     DA_KHOI_TO_VU_VIEC, DA_KHOI_TO_DOI_TUONG, A.MA_CO_QUAN FROM
    --trong ky thanh tra
    (SELECT COUNT(A1.SO_THANH_TRA) AS TRONG_KY_THANH_TRA,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC A1, TT_THANH_TRA_LAI A2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    AND A1.SO_THANH_TRA=A2.SO_THANH_TRA
    GROUP BY MA_CO_QUAN) A,
    --ky truoc chuyen sang
    (SELECT COUNT(B1.SO_THANH_TRA) AS KY_TRUOC_CHUYEN_SANG,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC B1, TT_THANH_TRA_LAI B2
    WHERE TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    AND B1.SO_THANH_TRA=B1.SO_THANH_TRA
    GROUP BY MA_CO_QUAN) B,
    --vi pham trinh tu
    (SELECT COUNT(C1.SO_THANH_TRA) AS VI_PHAM_TRINH_TU,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC C1, TT_THANH_TRA_LAI C2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND C1.SO_THANH_TRA=C2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    AND C2.LY_DO_THANH_TRA_LAI LIKE '%-1-%'--vi pham trinh tu
    GROUP BY MA_CO_QUAN) C,
    --noi dung ket luan khong phu hop
    (SELECT COUNT(D1.SO_THANH_TRA) AS NOI_DUNG_KL_KO_PHU_HOP,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC D1, TT_THANH_TRA_LAI D2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND D1.SO_THANH_TRA=D2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    AND D2.LY_DO_THANH_TRA_LAI LIKE '%-2-%'--noi dung ket luan khong phu hop
    GROUP BY MA_CO_QUAN) D,
    --sai lam khi ket luan
    (SELECT COUNT(E1.SO_THANH_TRA) AS SAI_LAM_KHI_KL,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC E1, TT_THANH_TRA_LAI E2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND E1.SO_THANH_TRA=E2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    AND E2.LY_DO_THANH_TRA_LAI LIKE '%-3-%'--sai lam khi ket luan
    GROUP BY MA_CO_QUAN) E,
    --co y lam sai
    (SELECT COUNT(F1.SO_THANH_TRA) AS CO_Y_KL_SAI,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC F1, TT_THANH_TRA_LAI F2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND F1.SO_THANH_TRA=F2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    AND F2.LY_DO_THANH_TRA_LAI LIKE '%-4-%'--co y lam sai
    GROUP BY MA_CO_QUAN) F,
     --vi pham nghiem trong
    (SELECT COUNT(G1.SO_THANH_TRA) AS VI_PHAM_NGHIEM_TRONG,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC G1, TT_THANH_TRA_LAI G2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND G1.SO_THANH_TRA=G2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    AND G2.LY_DO_THANH_TRA_LAI LIKE '%-5-%'--vi pham nghiem trong
    GROUP BY MA_CO_QUAN) G,
    --so don vi thanh tra
    (SELECT COUNT(SO_THANH_TRA) AS SO_DON_VI_THANH_TRA,MA_CO_QUAN FROM (SELECT DISTINCT H1.SO_THANH_TRA,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC H1, TT_THANH_TRA_LAI H2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND H1.SO_THANH_TRA=H2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL))
    GROUP BY MA_CO_QUAN) H,
     --co vi pham qua thanh tra
    (SELECT COUNT(I1.SO_THANH_TRA) AS TS_VI_PHAM,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC I1, TT_THANH_TRA_LAI I2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND I1.SO_THANH_TRA=I2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    AND I2.KIEN_NGHI_XU_LY IS NOT NULL--co vi pham
    GROUP BY MA_CO_QUAN) I,
     --so tien vi pham ve kinh te
    (SELECT SUM(J2.SO_TIEN_VI_PHAM) AS SO_TIEN_VI_PHAM,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC J1, TT_THANH_TRA_LAI J2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND J1.SO_THANH_TRA=J2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) J,
     --dat vi pham ve kinh te
    (SELECT SUM(K2.DAT_VI_PHAM) AS DAT_VI_PHAM,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC K1, TT_THANH_TRA_LAI K2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND K1.SO_THANH_TRA=K2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) K,
    --kien nghi thu hoi
    (SELECT SUM(L2.KIEN_NGHI_THU_HOI) AS KIEN_NGHI_THU_HOI,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC L1, TT_THANH_TRA_LAI L2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND L1.SO_THANH_TRA=L2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) L,
    --so tien thu hoi
    (SELECT SUM(M2.SO_TIEN_THU_HOI) AS SO_TIEN_THU_HOI,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC M1, TT_THANH_TRA_LAI M2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND M1.SO_THANH_TRA=M2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) M,
    --so tien khac
    (SELECT SUM(N2.KIEN_NGHI_KHAC) AS KIEN_NGHI_KHAC,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC N1, TT_THANH_TRA_LAI N2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND N1.SO_THANH_TRA=N2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) N,
    --so tien khac
    (SELECT SUM(O2.SO_TIEN_KHAC) AS SO_TIEN_KHAC,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC O1, TT_THANH_TRA_LAI O2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND O1.SO_THANH_TRA=O2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) O,
    --kien nghi xu ly hanh chinh to chuc
    (SELECT COUNT(P2.SO_THANH_TRA) AS KIEN_NGHI_XU_LY_HC_TO_CHUC,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC P1, TT_THANH_TRA_LAI P2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND P1.SO_THANH_TRA=P2.SO_THANH_TRA
    AND P2.KIEN_NGHI_XU_LY=0--xu ly hanh chinh
    AND P1.LOAI_HINH_DOANH_NGHIEP=0--to chuc, 1 ca nhan
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) P,
    --kien nghi xu ly hanh chinh ca nhan
    (SELECT COUNT(Q2.SO_THANH_TRA) AS KIEN_NGHI_XU_LY_HC_CA_NHAN,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC Q1, TT_THANH_TRA_LAI Q2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND Q1.SO_THANH_TRA=Q2.SO_THANH_TRA
    AND Q2.KIEN_NGHI_XU_LY=0--xu ly hanh chinh
    AND Q1.LOAI_HINH_DOANH_NGHIEP=1--to chuc, 1 ca nhan
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) Q,
    --kien nghi chuyen vu viec
    (SELECT COUNT(R2.SO_THANH_TRA) AS KIEN_NGHI_CHUYEN_VU_VIEC,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC R1, TT_THANH_TRA_LAI R2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND R1.SO_THANH_TRA=R2.SO_THANH_TRA
    AND R2.KIEN_NGHI_XU_LY=1--xu ly hanh chinh
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) R,
    --kien nghi chuyen doi tuong
    (SELECT COUNT(S2.SO_THANH_TRA) AS KIEN_NGHI_CHUYEN_DOI_TUONG,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC S1, TT_THANH_TRA_LAI S2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND S1.SO_THANH_TRA=S2.SO_THANH_TRA
    AND S2.KIEN_NGHI_XU_LY=2--xu ly hanh chinh
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) S,
     --so tien da thu
    (SELECT SUM(T2.SO_TIEN_DA_THU) AS SO_TIEN_DA_THU,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC T1, TT_THANH_TRA_LAI T2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND T1.SO_THANH_TRA=T2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) T,
     --dat da thu
    (SELECT SUM(U2.DAT_DA_THU) AS DAT_DA_THU,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC U1, TT_THANH_TRA_LAI U2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND U1.SO_THANH_TRA=U2.SO_THANH_TRA
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) U,
     --ket qua xu ly hanh chinh to chuc
    (SELECT COUNT(V2.SO_THANH_TRA) AS KET_QUA_XU_LY_HC_TO_CHUC,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC V1, TT_THANH_TRA_LAI V2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND V1.SO_THANH_TRA=V2.SO_THANH_TRA
    AND V2.KET_QUA_XU_LY=0--xu ly hanh chinh
    AND V1.LOAI_HINH_DOANH_NGHIEP=0--to chuc, 1 ca nhan
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) V,
    --ket qua xu ly hanh chinh to chuc
    (SELECT COUNT(W2.SO_THANH_TRA) AS KET_QUA_XU_LY_HC_CA_NHAN,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC W1, TT_THANH_TRA_LAI W2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND W1.SO_THANH_TRA=W2.SO_THANH_TRA
    AND W2.KET_QUA_XU_LY=0--xu ly hanh chinh
    AND W1.LOAI_HINH_DOANH_NGHIEP=1--to chuc, 1 ca nhan
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) W,
     --da khoi to vu viec
    (SELECT COUNT(X2.SO_THANH_TRA) AS DA_KHOI_TO_VU_VIEC,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC X1, TT_THANH_TRA_LAI X2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
   AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND X1.SO_THANH_TRA=X2.SO_THANH_TRA
    AND X2.KET_QUA_XU_LY=1--xu ly hanh chinh
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) X,
    --da khoi to doi tuong
    (SELECT COUNT(Y2.SO_THANH_TRA) AS DA_KHOI_TO_DOI_TUONG,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC Y1, TT_THANH_TRA_LAI Y2
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND Y1.SO_THANH_TRA=Y2.SO_THANH_TRA
    AND Y2.KET_QUA_XU_LY=2--xu ly hanh chinh
    AND Y1.LOAI_HINH_DOANH_NGHIEP=0--to chuc, 1 ca nhan
    AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
    GROUP BY MA_CO_QUAN) Y
    WHERE A.MA_CO_QUAN=B.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=C.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=D.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=E.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=F.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=G.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=H.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=I.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=J.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=K.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=L.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=M.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=N.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=O.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=P.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=Q.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=R.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=S.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=T.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=U.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=V.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=W.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=X.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=Y.MA_CO_QUAN(+)
   /* AND A.MA_CO_QUAN=Z.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=AA.MA_CO_QUAN(+)*/) KQ, CB_CO_QUAN W
    WHERE KQ.MA_CO_QUAN=W.MA_CO_QUAN;
  END;
PROCEDURE BAO_CAO_THANH_TRA_LAI_LIST(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2,P_DEN_NGAY NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT KQ1.* FROM (SELECT KQ.* FROM(SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT
    UNION SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA FROM TT_THANH_TRA A
    WHERE SO_THANH_TRA NOT IN(SELECT SO_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT)) KQ
    WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    AND (KQ.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)) KQ1, TT_THANH_TRA_LAI D
    WHERE KQ1.SO_THANH_TRA=D.SO_THANH_TRA;
  END;
PROCEDURE BAO_CAO_THAM_NHUNG(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2,P_DEN_NGAY NVARCHAR2,P_MA_LINH_VUC NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
     OPEN P_CUR FOR SELECT KQ.*,TEN_CO_QUAN FROM (SELECT TS_VU_VIEC,TS_NGUOI_LIEN_QUAN,SO_TIEN_THAM_NHUNG, TS_QUY_DOI_THANH_TIEN,
                                                  DAT_THAM_NHUNG, KIEN_NGHI_THU_HOI, SO_TIEN_THU_HOI, KIEN_NGHI_KHAC, SO_TIEN_KHAC,
                                                  DAT_DA_THU, SO_TIEN_DA_THU,KIEN_NGHI_XU_LY_HC_TO_CHUC, KIEN_NGHI_XU_LY_HC_CA_NHAN,
                                                  KIEN_NGHI_CHUYEN_VU_VIEC, KIEN_NGHI_CHUYEN_DOI_TUONG, KET_QUA_XU_LY_HC_TO_CHUC,KET_QUA_XU_LY_HC_CA_NHAN,
                                                  KHOI_TO_VU_VIEC, KHOI_TO_DOI_TUONG, A.MA_CO_QUAN FROM
    --tong so vu viec
    (SELECT COUNT(SO_THANH_TRA) AS TS_VU_VIEC,MA_CO_QUAN FROM
      (SELECT SO_THANH_TRA,MA_CO_QUAN  FROM TT_THANH_TRA_LINH_VUC
      WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
      AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
      AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
      UNION 
      SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
      WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND A.SO_THANH_TRA=B.SO_THANH_TRA
      AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
      AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
      AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL 
           OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
     GROUP BY MA_CO_QUAN) A,
    --so nguoi lien quan
    (SELECT SUM(SO_NGUOI_LIEN_QUAN) AS TS_NGUOI_LIEN_QUAN,MA_CO_QUAN  FROM
      (SELECT SO_THANH_TRA,SO_NGUOI_LIEN_QUAN,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
      WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
      AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
      AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
      UNION
      SELECT A.SO_THANH_TRA,SO_NGUOI_LIEN_QUAN,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
      WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND A.SO_THANH_TRA=B.SO_THANH_TRA
      AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
      AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
      AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) B,
    --so tien tham nhung
    (SELECT SUM(SO_TIEN_THAM_NHUNG) AS SO_TIEN_THAM_NHUNG,MA_CO_QUAN  FROM 
       (SELECT SO_THANH_TRA,SO_TIEN_THAM_NHUNG,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,A.SO_TIEN_THAM_NHUNG,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) C,
    -- tai san quy doi thanh tien
    (SELECT SUM(TS_QUY_DOI_THANH_TIEN) AS TS_QUY_DOI_THANH_TIEN,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,TS_QUY_DOI_THANH_TIEN,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND (TS_QUY_DOI_THANH_TIEN IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,A.TS_QUY_DOI_THANH_TIEN,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND (A.TS_QUY_DOI_THANH_TIEN IS NOT NULL))
    GROUP BY MA_CO_QUAN) D,
    --dat tham nhung
    (SELECT SUM(DAT_THAM_NHUNG) AS DAT_THAM_NHUNG,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,DAT_THAM_NHUNG,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND (DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,A.DAT_THAM_NHUNG,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND (A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) E,
    --dat thu hoi, so tien thu hoi, kien nghi khac, ...
    (SELECT SUM(KIEN_NGHI_THU_HOI) AS KIEN_NGHI_THU_HOI,SUM(SO_TIEN_THU_HOI) AS SO_TIEN_THU_HOI,
            SUM(KIEN_NGHI_KHAC) AS KIEN_NGHI_KHAC, SUM(SO_TIEN_KHAC) AS SO_TIEN_KHAC,
            SUM(DAT_DA_THU) AS DAT_DA_THU, SUM(SO_TIEN_DA_THU) AS SO_TIEN_DA_THU,
            MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,KIEN_NGHI_THU_HOI,SO_TIEN_THU_HOI,KIEN_NGHI_KHAC,SO_TIEN_KHAC,
                DAT_DA_THU,SO_TIEN_DA_THU,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,A.KIEN_NGHI_THU_HOI,A.SO_TIEN_THU_HOI,A.KIEN_NGHI_KHAC,A.SO_TIEN_KHAC,
               A.DAT_DA_THU,A.SO_TIEN_DA_THU,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) F,
    --kien nghi xu ly hanh chinh to chuc
   (SELECT COUNT(SO_THANH_TRA) AS KIEN_NGHI_XU_LY_HC_TO_CHUC,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 0 -- to chuc, 1 ca nhan,
        AND KIEN_NGHI_XU_LY=0
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 0
        AND A.KIEN_NGHI_XU_LY = 0
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) G,
    --kien nghi xu ly hanh chinh ca nhan
   (SELECT COUNT(SO_THANH_TRA) AS KIEN_NGHI_XU_LY_HC_CA_NHAN,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 1 -- to chuc, 1 ca nhan,
        AND KIEN_NGHI_XU_LY=0
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 1
        AND A.KIEN_NGHI_XU_LY=0
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) H,
    --kien nghi chuyen vu viec
   (SELECT COUNT(SO_THANH_TRA) AS KIEN_NGHI_CHUYEN_VU_VIEC,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND KIEN_NGHI_XU_LY=1
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND A.KIEN_NGHI_XU_LY=1
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) I,
    --kien nghi chuyen doi tuong
   (SELECT COUNT(SO_THANH_TRA) AS KIEN_NGHI_CHUYEN_DOI_TUONG,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND KIEN_NGHI_XU_LY=2
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND A.KIEN_NGHI_XU_LY=2
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) J,
    --ket qua xu ly hanh chinh to chuc
   (SELECT COUNT(SO_THANH_TRA) AS KET_QUA_XU_LY_HC_TO_CHUC,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 0 -- to chuc, 1 ca nhan,
        AND KET_QUA_XU_LY=0
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 0
        AND A.KET_QUA_XU_LY=0
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) K,
    --ket qua xu ly hanh chinh ca nhan
   (SELECT COUNT(SO_THANH_TRA) AS KET_QUA_XU_LY_HC_CA_NHAN,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 0 -- to chuc, 1 ca nhan,
        AND KET_QUA_XU_LY=0
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 0
        AND A.KET_QUA_XU_LY=0
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) L,
   /* --ket qua xu ly hanh chinh ca nhan
   (SELECT COUNT(SO_THANH_TRA) AS KET_QUA_XU_LY_HC_CA_NHAN,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 1 -- to chuc, 1 ca nhan,
        AND KET_QUA_XU_LY=0
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP = 1
        AND A.KET_QUA_XU_LY=0
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) M,*/
    --khoi to vu viec
   (SELECT COUNT(SO_THANH_TRA) AS KHOI_TO_VU_VIEC,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND KET_QUA_XU_LY=1
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND A.KET_QUA_XU_LY=1
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) M,
    --khoi to doi tuong
   (SELECT COUNT(SO_THANH_TRA) AS KHOI_TO_DOI_TUONG,MA_CO_QUAN  FROM
        (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND KET_QUA_XU_LY=2
        AND (SO_TIEN_THAM_NHUNG IS NOT NULL OR TAI_SAN_THAM_NHUNG IS NOT NULL OR TS_QUY_DOI_THANH_TIEN IS NOT NULL OR DAT_THAM_NHUNG IS NOT NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND (MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC OR P_MA_LINH_VUC IS NULL)
        AND A.KET_QUA_XU_LY=2
        AND (A.SO_TIEN_THAM_NHUNG IS NOT NULL OR A.TAI_SAN_THAM_NHUNG IS NOT NULL OR A.TS_QUY_DOI_THANH_TIEN IS NOT NULL OR A.DAT_THAM_NHUNG IS NOT NULL))
    GROUP BY MA_CO_QUAN) N
    WHERE A.MA_CO_QUAN=B.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=C.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=D.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=E.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=F.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=G.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=H.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=I.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=J.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=K.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=L.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=M.MA_CO_QUAN(+)
    AND A.MA_CO_QUAN=N.MA_CO_QUAN(+)) KQ, CB_CO_QUAN W
    WHERE KQ.MA_CO_QUAN=W.MA_CO_QUAN;
 END;
PROCEDURE THONG_KE_XU_LY_DON(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2, P_DEN_NGAY NVARCHAR2, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR 
    /*SELECT DISTINCT(KQ.MA_HO_SO_KNTC),KQ.NGAY_TIEP_DAN, KQ.MA_NGUON_DON_DEN, KQ.KNTC_HO_VA_TEN,KQ.NOI_DUNG_TIEP_DAN,
          KQ.MA_LOAI_KNTC,KQ.KNTC_SO_NHA,KQ.TEN_LINH_VUC_KNTC,
          KQ.TEN_PHUONG_XA,KQ.TEN_QUAN_HUYEN,KQ.TEN_TINH_THANH,KQ.TEN_NGUON_DON_DEN,KQ.NGAY_GIAI_QUYET,KQ.NGAY_TRINH_LANH_DAO_KY,
          KQ.NGAY_LANH_DAO_KY,KQ.NGAY_CHUYEN_VAN_BAN,KQ.NOI_DUNG_GIAI_QUYET,KQ.FILE_VAN_BAN
    FROM
     (*/SELECT A.MA_HO_SO_KNTC,A.NGAY_TIEP_DAN, A.MA_NGUON_DON_DEN, A.KNTC_HO_VA_TEN,A.NOI_DUNG_TIEP_DAN,
             A.MA_LOAI_KNTC,A.KNTC_SO_NHA,D.TEN_LINH_VUC_KNTC,NULL AS MA_LOAI_NHOM_NSD,
             (SELECT A1.TEN_PHUONG_XA FROM DM_PHUONG_XA A1 
              WHERE A1.MA_PHUONG_XA=A.KNTC_MA_PHUONG_XA) AS TEN_PHUONG_XA,
             (SELECT B2.TEN_QUAN_HUYEN FROM DM_PHUONG_XA B1, DM_QUAN_HUYEN B2 
              WHERE B1.MA_PHUONG_XA=A.KNTC_MA_PHUONG_XA AND B1.MA_QUAN_HUYEN=B2.MA_QUAN_HUYEN) AS TEN_QUAN_HUYEN,
             (SELECT C3.TEN_TINH_THANH FROM DM_PHUONG_XA C1, DM_QUAN_HUYEN C2, DM_TINH_THANH C3 
              WHERE C1.MA_PHUONG_XA=A.KNTC_MA_PHUONG_XA AND C1.MA_QUAN_HUYEN=C2.MA_QUAN_HUYEN AND C2.MA_TINH_THANH=C3.MA_TINH_THANH) AS TEN_TINH_THANH,
              B.TEN_NGUON_DON_DEN,NULL AS NGAY_GIAI_QUYET,NULL AS NGAY_TRINH_LANH_DAO_KY,
      NULL AS NGAY_LANH_DAO_KY,NULL AS NGAY_CHUYEN_VAN_BAN,NULL AS NOI_DUNG_GIAI_QUYET,NULL AS FILE_VAN_BAN FROM KNTC_HO_SO A, DM_NGUON_DON_DEN B,DM_LINH_VUC_KNTC D
      WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND A.MA_CO_QUAN_TIEP_DAN=P_MA_CO_QUAN
      AND A.MA_NGUON_DON_DEN = B.MA_NGUON_DON_DEN
      AND A.MA_LINH_VUC_KNTC=D.MA_LINH_VUC_KNTC
      UNION
      SELECT A.MA_HO_SO_KNTC,A.NGAY_TIEP_DAN, A.MA_NGUON_DON_DEN, A.KNTC_HO_VA_TEN,A.NOI_DUNG_TIEP_DAN,
             A.MA_LOAI_KNTC,A.KNTC_SO_NHA,D.TEN_LINH_VUC_KNTC,
             (SELECT D1.MA_LOAI_NHOM_NSD FROM CB_TO_CHUYEN_MON D1, CB_THUOC_TO_CHUYEN_MON D2 WHERE D1.MA_TO_CHUYEN_MON=D2.MA_TO_CHUYEN_MON AND D2.MA_CAN_BO=C.MA_LANH_DAO_KY) AS MA_LOAI_NHOM_NSD,
             (SELECT A1.TEN_PHUONG_XA FROM DM_PHUONG_XA A1 
              WHERE A1.MA_PHUONG_XA=A.KNTC_MA_PHUONG_XA) AS TEN_PHUONG_XA,
             (SELECT B2.TEN_QUAN_HUYEN FROM DM_PHUONG_XA B1, DM_QUAN_HUYEN B2 
              WHERE B1.MA_PHUONG_XA=A.KNTC_MA_PHUONG_XA AND B1.MA_QUAN_HUYEN=B2.MA_QUAN_HUYEN) AS TEN_QUAN_HUYEN,
             (SELECT C3.TEN_TINH_THANH FROM DM_PHUONG_XA C1, DM_QUAN_HUYEN C2, DM_TINH_THANH C3 
              WHERE C1.MA_PHUONG_XA=A.KNTC_MA_PHUONG_XA AND C1.MA_QUAN_HUYEN=C2.MA_QUAN_HUYEN AND C2.MA_TINH_THANH=C3.MA_TINH_THANH) AS TEN_TINH_THANH,
              B.TEN_NGUON_DON_DEN,C.NGAY_GIAI_QUYET,C.NGAY_TRINH_LANH_DAO_KY,
      C.NGAY_LANH_DAO_KY,C.NGAY_CHUYEN_VAN_BAN,C.NOI_DUNG_GIAI_QUYET,C.FILE_VAN_BAN FROM KNTC_HO_SO A, DM_NGUON_DON_DEN B, KNTC_GIAI_QUYET C,DM_LINH_VUC_KNTC D
      WHERE TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') >= TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
      AND TO_DATE(A.NGAY_TIEP_DAN,'DD/MM/YYYY') <= TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
      AND A.MA_NGUON_DON_DEN = B.MA_NGUON_DON_DEN
      AND A.MA_HO_SO_KNTC = C.MA_HO_SO_KNTC
      AND A.MA_CO_QUAN_TIEP_DAN=P_MA_CO_QUAN
      AND A.MA_LINH_VUC_KNTC=D.MA_LINH_VUC_KNTC/*) KQ*/;
    
  END; 
PROCEDURE BAO_CAO_QLNN(P_CUR IN OUT MYCUR,P_TU_NGAY NVARCHAR2,P_DEN_NGAY NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT SO_CUOC_THANH_TRA,SO_DON_VI_THANH_TRA,SO_LUONG_LOP,SO_LUONG_DI_HOC FROM 
    (SELECT COUNT(SO_THANH_TRA) AS SO_CUOC_THANH_TRA,MA_CO_QUAN  FROM
    (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
       )
    GROUP BY MA_CO_QUAN) A,
    (SELECT COUNT(SO_THANH_TRA) AS SO_DON_VI_THANH_TRA,MA_CO_QUAN  FROM
    (SELECT SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LINH_VUC
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP=0
        UNION
        SELECT A.SO_THANH_TRA,MA_CO_QUAN FROM TT_THANH_TRA_LAI A, TT_THANH_TRA_LINH_VUC B
        WHERE TO_DATE(NGAY_BD_THANH_TRA,'DD/MM/YYYY')>=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
        AND TO_DATE(NGAY_KT_THANH_TRA,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
        AND A.SO_THANH_TRA=B.SO_THANH_TRA
        AND (MA_CO_QUAN = P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
        AND LOAI_HINH_DOANH_NGHIEP=0
       )
    GROUP BY MA_CO_QUAN) B,
    (SELECT COUNT(A2.MA_LOP_TAP_HUAN) AS SO_LUONG_LOP,SUM(A2.SO_LUONG_DI_HOC) AS SO_LUONG_DI_HOC,MA_CO_QUAN FROM DM_LOP_TAP_HUAN A1, TT_LOP_TAP_HUAN A2
    WHERE A1.MA_LOP_TAP_HUAN=A2.MA_LOP_TAP_HUAN
    AND (A1.MA_CO_QUAN=P_MA_CO_QUAN OR P_MA_CO_QUAN IS NULL)
    AND TO_DATE(A2.NGAY_MO_LOP,'DD/MM/YYYY')<=TO_DATE(P_TU_NGAY,'DD/MM/YYYY')
    AND TO_DATE(A2.NGAY_MO_LOP,'DD/MM/YYYY')<=TO_DATE(P_DEN_NGAY,'DD/MM/YYYY')
    ORDER BY A1.MA_CO_QUAN
    ) C 
    WHERE A.MA_CO_QUAN=B.MA_CO_QUAN
          AND A.MA_CO_QUAN=C.MA_CO_QUAN;
  END;
END TNMT_THANHTRABAOCAO;
/

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


end TNMT_THANHTRA_DT;
/

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

--===========//GET DS DOAN THANH TRA//=============--
PROCEDURE GET_DOAN_THANH_TRAS(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_DOAN_THANH_TRA A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;
--===========//GET DOAN THANH TRA//=============--
PROCEDURE GET_DOAN_THANH_TRA(P_CUR IN OUT MYCUR, P_MA_DOAN_TT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_DOAN_THANH_TRA A
    WHERE A.MA_DOAN_TT=P_MA_DOAN_TT;
  END;
--===========//UPDATE DOAN THANH TRA//=============--
PROCEDURE UPDATE_DOAN_THANH_TRA(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE NVARCHAR2, P_MA_DOAN_TT NVARCHAR2,P_TEN_DOAN_TT NVARCHAR2,
                                P_MUC_DICH NVARCHAR2, P_NGAY_THANH_LAP NVARCHAR2,P_NGAY_HET_HAN NVARCHAR2,P_NGAY_GIA_HAN NVARCHAR2,
                                P_MA_CO_QUAN NVARCHAR2,P_TRANG_THAI INT) AS
  V_KT INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(MA_DOAN_TT) INTO V_KT FROM TT_DOAN_THANH_TRA WHERE UPPER(LIB_NOTABLE.LOC_DAU(TEN_DOAN_TT))=UPPER(LIB_NOTABLE.LOC_DAU(P_TEN_DOAN_TT));
      IF V_KT!=0 THEN
        P_VAL:='TON_TAI_TEN';
      ELSE
        INSERT INTO TT_DOAN_THANH_TRA(TEN_DOAN_TT,MUC_DICH_THANH_LAP,NGAY_THANH_LAP,
                                      NGAY_HET_HAN,NGAY_GIA_HAN,MA_CO_QUAN,TRANG_THAI)
                               VALUES(P_TEN_DOAN_TT,P_MUC_DICH,P_NGAY_THANH_LAP,
                                      P_NGAY_HET_HAN,P_NGAY_GIA_HAN,P_MA_CO_QUAN,P_TRANG_THAI);
        P_VAL:='THANH_CONG';
       END IF;
    ELSE
       UPDATE TT_DOAN_THANH_TRA SET TEN_DOAN_TT=P_TEN_DOAN_TT,
                                    MUC_DICH_THANH_LAP=P_MUC_DICH,
                                    NGAY_THANH_LAP=P_NGAY_THANH_LAP,
                                    NGAY_HET_HAN=P_NGAY_HET_HAN,
                                    NGAY_GIA_HAN=P_NGAY_GIA_HAN,
                                    MA_CO_QUAN=P_MA_CO_QUAN,
                                    TRANG_THAI=P_TRANG_THAI
               WHERE MA_DOAN_TT=P_MA_DOAN_TT;
       P_VAL:='THANH_CONG';
    END IF;
END;
--===========//DELETE DOAN THANH TRA//=============--
PROCEDURE DELETE_DOAN_THANH_TRA(P_VAL OUT NVARCHAR2,P_NGUOI_XOA INT,P_MA_DOAN_TT NVARCHAR2) AS
  BEGIN
    P_VAL:='LOI';
    IF P_NGUOI_XOA=0 THEN
      DELETE TT_DOAN_THANH_TRA WHERE MA_DOAN_TT=P_MA_DOAN_TT;
      P_VAL:='THANH_CONG';
    END IF;
  END;
--===========//SELECT CAN BO THANH TRA//=============-- 
PROCEDURE GET_DS_CAN_BOS(P_CUR IN OUT MYCUR,P_MA_DOAN_TT NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_CAN_BO,TEN_CAN_BO,HINH_ANH,TEN_TO_CHUYEN_MON
                   FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B, CB_TO_CHUYEN_MON C
                   WHERE A.MA_CAN_BO=B.MA_CAN_BO
                   AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
                   AND A.MA_CO_QUAN=P_MA_CO_QUAN
                   AND A.MA_CAN_BO NOT IN(SELECT MA_CAN_BO FROM TT_DOAN_TT_CO_CAN_BO WHERE MA_DOAN_TT=P_MA_DOAN_TT);
  END;
--===========//SELECT CAN BO DOAN THANH TRA//=============-- 
PROCEDURE GET_CAN_BO_DOAN_TT(P_CUR IN OUT MYCUR,P_MA_DOAN_TT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_CAN_BO,TEN_CAN_BO,HINH_ANH,TEN_TO_CHUYEN_MON
    FROM CB_CAN_BO A, TT_DOAN_TT_CO_CAN_BO B,CB_THUOC_TO_CHUYEN_MON C, CB_TO_CHUYEN_MON D
    WHERE A.MA_CAN_BO=B.MA_CAN_BO
    AND A.MA_CAN_BO=C.MA_CAN_BO
    AND C.MA_TO_CHUYEN_MON=D.MA_TO_CHUYEN_MON
    AND B.MA_DOAN_TT=P_MA_DOAN_TT;
  END; 
--===========//DELETE CAN BO DOAN THANH TRA//=============--
PROCEDURE DELETE_CAN_BO_DOAN_TT(P_MA_DOAN_TT NVARCHAR2) AS
  BEGIN
    DELETE FROM TT_DOAN_TT_CO_CAN_BO WHERE MA_DOAN_TT=P_MA_DOAN_TT;
  END;
--===========//INSERT CAN BO DOAN THANH TRA//=============--
PROCEDURE INSERT_CAN_BO_DOAN_TT(P_MA_DOAN_TT NVARCHAR2,P_MA_CAN_BO NVARCHAR2,P_MA_CHUC_DANH NVARCHAR2) AS
  BEGIN
    INSERT INTO TT_DOAN_TT_CO_CAN_BO(MA_DOAN_TT,MA_CAN_BO,MA_CHUC_DANH,NGAY_CAP_NHAT)
                        VALUES(P_MA_DOAN_TT,P_MA_CAN_BO,P_MA_CHUC_DANH,SYSDATE);
  END;
--===========//SELECT KE HOACH THANH TRA//=============--
/*PROCEDURE SELECT_KE_HOACH_THANH_TRA(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_KE_HOACH_TT,A.TEN_KE_HOACH_TT FROM TT_KE_HOACH_THANH_TRA A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;*/
  PROCEDURE SELECT_KE_HOACH_THANH_TRA(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2, P_STT_THANH_TRA INTEGER ) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_KE_HOACH_TT,A.TEN_KE_HOACH_TT 
    FROM TT_KE_HOACH_THANH_TRA A, tt_thanh_tra_linh_vuc t
    WHERE A.Ma_Co_Quan=P_MA_CO_QUAN 
    and t.ma_ke_hoach_tt=a.ma_ke_hoach_tt
    and t.stt_thanh_tra = P_STT_THANH_TRA;
  END;
--===========//SELECT DOAN THANH TRA//=============--
/*PROCEDURE SELECT_DOAN_THANH_TRA(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_DOAN_TT,A.TEN_DOAN_TT FROM TT_DOAN_THANH_TRA A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;*/
  PROCEDURE SELECT_DOAN_THANH_TRA(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2, P_STT_THANH_TRA INTEGER) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.MA_DOAN_TT,A.TEN_DOAN_TT 
    FROM TT_DOAN_THANH_TRA A, TT_THANH_TRA_LINH_VUC T 
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
    AND T.STT_THANH_TRA= P_STT_THANH_TRA
    AND T.MA_DOAN_TT=A.MA_DOAN_TT;
  END;
--===========//SELECT LINH VUC THANH TRA//=============--
PROCEDURE SELECT_LINH_VUC_THANH_TRA(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_LINH_VUC_THANH_TRA,A.TEN_LINH_VUC_THANH_TRA FROM TT_LINH_VUC_THANH_TRA A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;
--===========//UPDATE BIEN BAN THANH TRA//=============--
PROCEDURE UPDATE_BIEN_BAN_THANH_TRA(P_VAL OUT NVARCHAR2,P_UPDATE_TYPE NVARCHAR2,P_SO_THANH_TRA NVARCHAR2,P_MA_KE_HOACH_TT INT, P_MA_DOAN_TT INT,P_LOAI_HINH_THANH_TRA NVARCHAR2,
                                    P_NGAY_BD_THANH_TRA NVARCHAR2,P_NGAY_KT_THANH_TRA NVARCHAR2,P_NOI_DUNG_THANH_TRA NVARCHAR2,P_TEN_TO_CHUC_CA_NHAN NVARCHAR2,P_LOAI_HINH_DN INT, P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2,
                                    P_KET_QUA_THANH_TRA NVARCHAR2,P_HINH_THUC_XU_PHAT INT, P_LY_DO_XU_PHAT NVARCHAR2, P_TAI_SAN_TICH_THU NVARCHAR2,P_SO_TIEN_TICH_THU NVARCHAR2,
                                    P_TAI_SAN_TIEU_HUY NVARCHAR2,P_SO_TIEN_TIEU_HUY NVARCHAR2,P_SO_TIEN_XU_PHAT NVARCHAR2,P_MA_CO_QUAN NVARCHAR2,
                                    P_FILE_QUYET_DINH NVARCHAR2, P_NGAY_KY_QUYET_DINH NVARCHAR2, P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2,P_SO_BIEN_BAN_VI_PHAM_HC NVARCHAR2,
                                    P_NGAY_LAP_BIEN_BAN NVARCHAR2, P_FILE_BIEN_BAN_VI_PHAM NVARCHAR2,P_SO_QUYET_DINH_XU_PHAT NVARCHAR2,P_TRANG_THAI_NOP_PHAT NVARCHAR2) AS
  TON_TAI INT;
  --V_MA_DOAN_TT INT;
  --V_MA_KE_HOACH_TT INT;
  --V_MA_QUYET_DINH INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(SO_THANH_TRA) INTO TON_TAI FROM TT_THANH_TRA WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      IF TON_TAI > 0 THEN
        P_VAL:='TON_TAI';
      ELSE
        /*--ISERT VAO DM QUYET DINH
        SELECT SEQ_TT_QUYET_DINH.NEXTVAL INTO V_MA_QUYET_DINH FROM DUAL;
        INSERT INTO TT_QUYET_DINH(TEN_QUYET_DINH,NGAY_CAP_NHAT,MA_CO_QUAN)
                     VALUES(P_SO_THANH_TRA,SYSDATE,P_MA_CO_QUAN);
        --ISERT VAO DM KE HOACH TT
        SELECT SEQ_TT_KE_HOACH_TT.NEXTVAL INTO V_MA_KE_HOACH_TT FROM DUAL;
        INSERT INTO TT_KE_HOACH_THANH_TRA(TEN_KE_HOACH_TT,NGAY_LAP_KE_HOACH,NGAY_CAP_NHAT,
                                          MA_QUYET_DINH,MA_CO_QUAN)
                    VALUES(P_MA_KE_HOACH_TT,SYSDATE,SYSDATE,'41',P_MA_CO_QUAN);
        ---INSERT VAO DM DOAN TT
        SELECT SEQ_TT_DOAN_THANH_TRA.NEXTVAL INTO V_MA_DOAN_TT FROM DUAL;
        INSERT INTO TT_DOAN_THANH_TRA(TEN_DOAN_TT,NGAY_THANH_LAP,MA_CO_QUAN,TRANG_THAI)
                    VALUES(P_MA_DOAN_TT,SYSDATE,P_MA_CO_QUAN,'0');*/
        INSERT INTO TT_THANH_TRA(SO_THANH_TRA,MA_KE_HOACH_TT,MA_DOAN_TT,LOAI_HINH_THANH_TRA,NGAY_BD_THANH_TRA,NGAY_KT_THANH_TRA,
                               NOI_DUNG_THANH_TRA,TEN_TO_CHUC_CA_NHAN,LOAI_HINH_DOANH_NGHIEP,DIA_CHI_TO_CHUC_CA_NHAN,KET_QUA_THANH_TRA,
                               HINH_THUC_XU_PHAT,LY_DO_XU_PHAT,TAI_SAN_TICH_THU,SO_TIEN_TICH_THU,TAI_SAN_TIEU_HUY, SO_TIEN_TIEU_HUY,SO_TIEN_XU_PHAT,MA_CO_QUAN,
                               FILE_QUYET_DINH,NGAY_KY_QUYET_DINH,FILE_NOI_DUNG_THANH_TRA,SO_BIEN_BAN_VI_PHAM_HC,NGAY_LAP_BIEN_BAN,FILE_BIEN_BAN_VI_PHAM,
                               SO_QUYET_DINH_XU_PHAT,TRANG_THAI_NOP_PHAT)
                        VALUES(P_SO_THANH_TRA,P_MA_KE_HOACH_TT,P_MA_DOAN_TT,P_LOAI_HINH_THANH_TRA,P_NGAY_BD_THANH_TRA,P_NGAY_KT_THANH_TRA,
                               P_NOI_DUNG_THANH_TRA,P_TEN_TO_CHUC_CA_NHAN,P_LOAI_HINH_DN,P_DIA_CHI_TO_CHUC_CA_NHAN,P_KET_QUA_THANH_TRA,
                               P_HINH_THUC_XU_PHAT,P_LY_DO_XU_PHAT,P_TAI_SAN_TICH_THU,P_SO_TIEN_TICH_THU,P_TAI_SAN_TIEU_HUY,P_SO_TIEN_TIEU_HUY,P_SO_TIEN_XU_PHAT,P_MA_CO_QUAN,
                               P_FILE_QUYET_DINH,P_NGAY_KY_QUYET_DINH,P_FILE_NOI_DUNG_THANH_TRA,P_SO_BIEN_BAN_VI_PHAM_HC,P_NGAY_LAP_BIEN_BAN,
                               P_FILE_BIEN_BAN_VI_PHAM,P_SO_QUYET_DINH_XU_PHAT,P_TRANG_THAI_NOP_PHAT);
      P_VAL:='THANH_CONG';
     END IF;
    ELSE
      UPDATE TT_THANH_TRA SET MA_KE_HOACH_TT=P_MA_KE_HOACH_TT,
                              MA_DOAN_TT=P_MA_DOAN_TT,
                              LOAI_HINH_THANH_TRA=P_LOAI_HINH_THANH_TRA,
                              NGAY_BD_THANH_TRA=P_NGAY_BD_THANH_TRA,
                              NGAY_KT_THANH_TRA=P_NGAY_KT_THANH_TRA,
                              NOI_DUNG_THANH_TRA=P_NOI_DUNG_THANH_TRA,
                              TEN_TO_CHUC_CA_NHAN=P_TEN_TO_CHUC_CA_NHAN,
                              LOAI_HINH_DOANH_NGHIEP=P_LOAI_HINH_DN,
                              DIA_CHI_TO_CHUC_CA_NHAN=P_DIA_CHI_TO_CHUC_CA_NHAN,
                              KET_QUA_THANH_TRA=P_KET_QUA_THANH_TRA,
                              HINH_THUC_XU_PHAT=P_HINH_THUC_XU_PHAT,
                              LY_DO_XU_PHAT=P_LY_DO_XU_PHAT,
                              TAI_SAN_TICH_THU=P_TAI_SAN_TICH_THU,
                              SO_TIEN_TICH_THU=P_SO_TIEN_TICH_THU,
                              TAI_SAN_TIEU_HUY=P_TAI_SAN_TIEU_HUY,
                              SO_TIEN_TIEU_HUY=P_SO_TIEN_TIEU_HUY,
                              SO_TIEN_XU_PHAT=P_SO_TIEN_XU_PHAT,
                              MA_CO_QUAN=P_MA_CO_QUAN,
                              FILE_QUYET_DINH=P_FILE_QUYET_DINH,
                              NGAY_KY_QUYET_DINH=P_NGAY_KY_QUYET_DINH,
                              FILE_NOI_DUNG_THANH_TRA=P_FILE_NOI_DUNG_THANH_TRA,
                              SO_BIEN_BAN_VI_PHAM_HC=P_SO_BIEN_BAN_VI_PHAM_HC,
                              NGAY_LAP_BIEN_BAN=P_NGAY_LAP_BIEN_BAN,
                              FILE_BIEN_BAN_VI_PHAM=P_FILE_BIEN_BAN_VI_PHAM,
                              SO_QUYET_DINH_XU_PHAT=P_SO_QUYET_DINH_XU_PHAT,
                              TRANG_THAI_NOP_PHAT=P_TRANG_THAI_NOP_PHAT
      WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      P_VAL:='UP_THANH_CONG';
    END IF;
 END;
 --===========//UPDATE BIEN BAN THANH TRA BANG STT//=============--
PROCEDURE UPDATE_BBTT_BY_STT(P_VAL OUT NVARCHAR2,P_UPDATE_TYPE NVARCHAR2,P_STT_THANH_TRA INTEGER,P_SO_THANH_TRA NVARCHAR2,P_MA_KE_HOACH_TT INT, P_MA_DOAN_TT INT,P_LOAI_HINH_THANH_TRA NVARCHAR2,
                                    P_NGAY_BD_THANH_TRA NVARCHAR2,P_NGAY_KT_THANH_TRA NVARCHAR2,P_NOI_DUNG_THANH_TRA NVARCHAR2,P_TEN_TO_CHUC_CA_NHAN NVARCHAR2,P_LOAI_HINH_DN INT, P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2,
                                    P_KET_QUA_THANH_TRA NVARCHAR2,P_HINH_THUC_XU_PHAT INT, P_LY_DO_XU_PHAT NVARCHAR2, P_TAI_SAN_TICH_THU NVARCHAR2,P_SO_TIEN_TICH_THU NVARCHAR2,
                                    P_TAI_SAN_TIEU_HUY NVARCHAR2,P_SO_TIEN_TIEU_HUY NVARCHAR2,P_SO_TIEN_XU_PHAT NVARCHAR2,P_MA_CO_QUAN NVARCHAR2,
                                    P_FILE_QUYET_DINH NVARCHAR2, P_NGAY_KY_QUYET_DINH NVARCHAR2, P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2,P_SO_BIEN_BAN_VI_PHAM_HC NVARCHAR2,
                                    P_NGAY_LAP_BIEN_BAN NVARCHAR2, P_FILE_BIEN_BAN_VI_PHAM NVARCHAR2,P_SO_QUYET_DINH_XU_PHAT NVARCHAR2,P_TRANG_THAI_NOP_PHAT NVARCHAR2) AS
  TON_TAI INT;
  --V_MA_DOAN_TT INT;
  --V_MA_KE_HOACH_TT INT;
  --V_MA_QUYET_DINH INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(STT_THANH_TRA) INTO TON_TAI FROM TT_THANH_TRA WHERE STT_THANH_TRA=P_STT_THANH_TRA;
      IF TON_TAI > 0 THEN
        P_VAL:='TON_TAI';
      ELSE
        /*--ISERT VAO DM QUYET DINH
        SELECT SEQ_TT_QUYET_DINH.NEXTVAL INTO V_MA_QUYET_DINH FROM DUAL;
        INSERT INTO TT_QUYET_DINH(TEN_QUYET_DINH,NGAY_CAP_NHAT,MA_CO_QUAN)
                     VALUES(P_SO_THANH_TRA,SYSDATE,P_MA_CO_QUAN);
        --ISERT VAO DM KE HOACH TT
        SELECT SEQ_TT_KE_HOACH_TT.NEXTVAL INTO V_MA_KE_HOACH_TT FROM DUAL;
        INSERT INTO TT_KE_HOACH_THANH_TRA(TEN_KE_HOACH_TT,NGAY_LAP_KE_HOACH,NGAY_CAP_NHAT,
                                          MA_QUYET_DINH,MA_CO_QUAN)
                    VALUES(P_MA_KE_HOACH_TT,SYSDATE,SYSDATE,'41',P_MA_CO_QUAN);
        ---INSERT VAO DM DOAN TT
        SELECT SEQ_TT_DOAN_THANH_TRA.NEXTVAL INTO V_MA_DOAN_TT FROM DUAL;
        INSERT INTO TT_DOAN_THANH_TRA(TEN_DOAN_TT,NGAY_THANH_LAP,MA_CO_QUAN,TRANG_THAI)
                    VALUES(P_MA_DOAN_TT,SYSDATE,P_MA_CO_QUAN,'0');*/
        INSERT INTO TT_THANH_TRA(SO_THANH_TRA,MA_KE_HOACH_TT,MA_DOAN_TT,LOAI_HINH_THANH_TRA,NGAY_BD_THANH_TRA,NGAY_KT_THANH_TRA,
                               NOI_DUNG_THANH_TRA,TEN_TO_CHUC_CA_NHAN,LOAI_HINH_DOANH_NGHIEP,DIA_CHI_TO_CHUC_CA_NHAN,KET_QUA_THANH_TRA,
                               HINH_THUC_XU_PHAT,LY_DO_XU_PHAT,TAI_SAN_TICH_THU,SO_TIEN_TICH_THU,TAI_SAN_TIEU_HUY, SO_TIEN_TIEU_HUY,SO_TIEN_XU_PHAT,MA_CO_QUAN,
                               FILE_QUYET_DINH,NGAY_KY_QUYET_DINH,FILE_NOI_DUNG_THANH_TRA,SO_BIEN_BAN_VI_PHAM_HC,NGAY_LAP_BIEN_BAN,FILE_BIEN_BAN_VI_PHAM,
                               SO_QUYET_DINH_XU_PHAT,TRANG_THAI_NOP_PHAT)
                        VALUES(P_SO_THANH_TRA,P_MA_KE_HOACH_TT,P_MA_DOAN_TT,P_LOAI_HINH_THANH_TRA,P_NGAY_BD_THANH_TRA,P_NGAY_KT_THANH_TRA,
                               P_NOI_DUNG_THANH_TRA,P_TEN_TO_CHUC_CA_NHAN,P_LOAI_HINH_DN,P_DIA_CHI_TO_CHUC_CA_NHAN,P_KET_QUA_THANH_TRA,
                               P_HINH_THUC_XU_PHAT,P_LY_DO_XU_PHAT,P_TAI_SAN_TICH_THU,P_SO_TIEN_TICH_THU,P_TAI_SAN_TIEU_HUY,P_SO_TIEN_TIEU_HUY,P_SO_TIEN_XU_PHAT,P_MA_CO_QUAN,
                               P_FILE_QUYET_DINH,P_NGAY_KY_QUYET_DINH,P_FILE_NOI_DUNG_THANH_TRA,P_SO_BIEN_BAN_VI_PHAM_HC,P_NGAY_LAP_BIEN_BAN,
                               P_FILE_BIEN_BAN_VI_PHAM,P_SO_QUYET_DINH_XU_PHAT,P_TRANG_THAI_NOP_PHAT);
      P_VAL:='THANH_CONG';
     END IF;
    ELSE
      UPDATE TT_THANH_TRA SET SO_THANH_TRA=P_SO_THANH_TRA,
                              MA_KE_HOACH_TT=P_MA_KE_HOACH_TT,
                              MA_DOAN_TT=P_MA_DOAN_TT,
                              LOAI_HINH_THANH_TRA=P_LOAI_HINH_THANH_TRA,
                              NGAY_BD_THANH_TRA=P_NGAY_BD_THANH_TRA,
                              NGAY_KT_THANH_TRA=P_NGAY_KT_THANH_TRA,
                              NOI_DUNG_THANH_TRA=P_NOI_DUNG_THANH_TRA,
                              TEN_TO_CHUC_CA_NHAN=P_TEN_TO_CHUC_CA_NHAN,
                              LOAI_HINH_DOANH_NGHIEP=P_LOAI_HINH_DN,
                              DIA_CHI_TO_CHUC_CA_NHAN=P_DIA_CHI_TO_CHUC_CA_NHAN,
                              KET_QUA_THANH_TRA=P_KET_QUA_THANH_TRA,
                              HINH_THUC_XU_PHAT=P_HINH_THUC_XU_PHAT,
                              LY_DO_XU_PHAT=P_LY_DO_XU_PHAT,
                              TAI_SAN_TICH_THU=P_TAI_SAN_TICH_THU,
                              SO_TIEN_TICH_THU=P_SO_TIEN_TICH_THU,
                              TAI_SAN_TIEU_HUY=P_TAI_SAN_TIEU_HUY,
                              SO_TIEN_TIEU_HUY=P_SO_TIEN_TIEU_HUY,
                              SO_TIEN_XU_PHAT=P_SO_TIEN_XU_PHAT,
                              MA_CO_QUAN=P_MA_CO_QUAN,
                              FILE_QUYET_DINH=P_FILE_QUYET_DINH,
                              NGAY_KY_QUYET_DINH=P_NGAY_KY_QUYET_DINH,
                              FILE_NOI_DUNG_THANH_TRA=P_FILE_NOI_DUNG_THANH_TRA,
                              SO_BIEN_BAN_VI_PHAM_HC=P_SO_BIEN_BAN_VI_PHAM_HC,
                              NGAY_LAP_BIEN_BAN=P_NGAY_LAP_BIEN_BAN,
                              FILE_BIEN_BAN_VI_PHAM=P_FILE_BIEN_BAN_VI_PHAM,
                              SO_QUYET_DINH_XU_PHAT=P_SO_QUYET_DINH_XU_PHAT,
                              TRANG_THAI_NOP_PHAT=P_TRANG_THAI_NOP_PHAT
      WHERE STT_THANH_TRA=P_STT_THANH_TRA;
      P_VAL:='UP_THANH_CONG';
    END IF;
 END;
--===========//SELECT BIEN BAN THANH TRA//=============--
PROCEDURE SELECT_BIEN_BAN_THANH_TRAS(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT
    UNION SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA FROM TT_THANH_TRA A
    WHERE SO_THANH_TRA NOT IN(SELECT A.SO_THANH_TRA FROM TT_THANH_TRA A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT);
  END;
--===========//SELECT 1 BIEN BAN THANH TRA//=============--
PROCEDURE SELECT_BIEN_BAN_THANH_TRA(P_CUR IN OUT MYCUR,P_SO_THANH_TRA NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_THANH_TRA A
    WHERE A.SO_THANH_TRA=P_SO_THANH_TRA;
  END;
  --===========//SELECT 1 BIEN BAN THANH TRA BANG STT//=============--
PROCEDURE SELECT_BBTT_BY_STT(P_CUR IN OUT MYCUR,P_STT_THANH_TRA INTEGER) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_THANH_TRA A
    WHERE A.STT_THANH_TRA=P_STT_THANH_TRA;
  END;
--===========//delete 1 BIEN BAN THANH TRA//=============--
PROCEDURE DELETE_BIEN_BAN_THANH_TRA(P_VAL OUT NVARCHAR2,P_STT_THANH_TRA integer,P_NGUOI_XOA INT) AS
  BEGIN
    P_VAL:='CL';
    IF P_NGUOI_XOA =0 THEN
      DELETE FROM TT_THANH_TRA WHERE STT_THANH_TRA=P_STT_THANH_TRA;
      P_VAL:='THANH_CONG';
    END IF;
  END;
--===========//UPDATE BIEN BAN THANH TRA//=============--
/*
PROCEDURE UPDATE_BIEN_BAN_TT_LINH_VUC(P_VAL OUT NVARCHAR2,
                                      P_UPDATE_TYPE NVARCHAR2,
                                      P_SO_THANH_TRA NVARCHAR2,
                                      P_MA_KE_HOACH_TT INT, 
                                      P_MA_DOAN_TT INT,
                                      P_LOAI_HINH_THANH_TRA NVARCHAR2,
                                      P_NGAY_BD_THANH_TRA NVARCHAR2,
                                      P_NGAY_KT_THANH_TRA NVARCHAR2,
                                      P_NOI_DUNG_THANH_TRA NVARCHAR2,
                                      P_LINH_VUC_THANH_TRA NVARCHAR2,
                                      P_TEN_TO_CHUC_CA_NHAN NVARCHAR2,
                                      P_LOAI_HINH_DN INT, 
                                      P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2,
                                      P_KET_QUA_THANH_TRA NVARCHAR2,
                                      P_KIEN_NGHI_XU_LY INT, 
                                      P_NOI_DUNG_KIEN_NGHI_XU_LY NVARCHAR2,
                                      P_NOI_DUNG_KIEN_NGHI_THU_HOI NVARCHAR2, 
                                      P_KIEN_NGHI_THU_HOI NVARCHAR2,
                                      P_SO_TIEN_THU_HOI FLOAT,
                                      P_NOI_DUNG_KIEN_NGHI_KHAC NVARCHAR2, 
                                      P_KIEN_NGHI_KHAC NVARCHAR2,
                                      P_SO_TIEN_KIEN_NGHI_KHAC NVARCHAR2,
                                      P_KET_QUA_XU_LY INT,
                                      P_NOI_DUNG_XU_LY NVARCHAR2,
                                      P_DAT_PHAI_THU FLOAT,
                                      P_SO_TIEN_PHAI_THU FLOAT, 
                                      P_DAT_DA_THU FLOAT,
                                      P_SO_TIEN_DA_THU FLOAT,
                                      P_SO_NGUOI_LIEN_QUAN INT,
                                      P_SO_TIEN_THAM_NHUNG FLOAT, 
                                      P_TAI_SAN_THAM_NHUNG NVARCHAR2,
                                      P_TS_QUY_DOI_THANH_TIEN FLOAT, 
                                      P_DAT_THAM_NHUNG FLOAT,
                                      P_FILE_QUYET_DINH NVARCHAR2, 
                                      P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2,
                                      P_NGAY_KY_QUYET_DINH NVARCHAR2, 
                                      P_MA_CO_QUAN NVARCHAR2) AS
  TON_TAI INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(SO_THANH_TRA) INTO TON_TAI FROM TT_THANH_TRA WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      IF TON_TAI > 0 THEN
        P_VAL:='TON_TAI';
      ELSE 
        INSERT INTO TT_THANH_TRA_LINH_VUC(SO_THANH_TRA,MA_KE_HOACH_TT,MA_DOAN_TT,LOAI_HINH_THANH_TRA,NGAY_BD_THANH_TRA,NGAY_KT_THANH_TRA,
                               NOI_DUNG_THANH_TRA,MA_LINH_VUC_THANH_TRA,TEN_TO_CHUC_CA_NHAN,LOAI_HINH_DOANH_NGHIEP,DIA_CHI_TO_CHUC_CA_NHAN,KET_QUA_THANH_TRA,
                               KIEN_NGHI_XU_LY,NOI_DUNG_KIEN_NGHI_XU_LY,NOI_DUNG_KIEN_NGHI_THU_HOI,KIEN_NGHI_THU_HOI,SO_TIEN_THU_HOI,NOI_DUNG_KIEN_NGHI_KHAC,KIEN_NGHI_KHAC,SO_TIEN_KHAC,
                               KET_QUA_XU_LY, NOI_DUNG_XU_LY, DAT_PHAI_THU, SO_TIEN_PHAI_THU, DAT_DA_THU, SO_TIEN_DA_THU
                               ,SO_NGUOI_LIEN_QUAN,SO_TIEN_THAM_NHUNG, TAI_SAN_THAM_NHUNG, TS_QUY_DOI_THANH_TIEN, DAT_THAM_NHUNG,FILE_QUYET_DINH,FILE_NOI_DUNG_THANH_TRA,NGAY_KY_QUYET_DINH,MA_CO_QUAN)
                        VALUES(P_SO_THANH_TRA,P_MA_KE_HOACH_TT,P_MA_DOAN_TT,P_LOAI_HINH_THANH_TRA,P_NGAY_BD_THANH_TRA,P_NGAY_KT_THANH_TRA,
                               P_NOI_DUNG_THANH_TRA,P_LINH_VUC_THANH_TRA,P_TEN_TO_CHUC_CA_NHAN,P_LOAI_HINH_DN,P_DIA_CHI_TO_CHUC_CA_NHAN,P_KET_QUA_THANH_TRA,
                               P_KIEN_NGHI_XU_LY,P_NOI_DUNG_KIEN_NGHI_XU_LY,P_NOI_DUNG_KIEN_NGHI_THU_HOI,P_KIEN_NGHI_THU_HOI,P_SO_TIEN_THU_HOI,P_NOI_DUNG_KIEN_NGHI_KHAC,P_KIEN_NGHI_KHAC,P_SO_TIEN_KIEN_NGHI_KHAC,
                               P_KET_QUA_XU_LY, P_NOI_DUNG_XU_LY, P_DAT_PHAI_THU, P_SO_TIEN_PHAI_THU,P_DAT_DA_THU,
                               P_SO_TIEN_DA_THU,P_SO_NGUOI_LIEN_QUAN,P_SO_TIEN_THAM_NHUNG,P_TAI_SAN_THAM_NHUNG,P_TS_QUY_DOI_THANH_TIEN,P_DAT_THAM_NHUNG,P_FILE_QUYET_DINH, P_FILE_NOI_DUNG_THANH_TRA, P_NGAY_KY_QUYET_DINH, P_MA_CO_QUAN);
      P_VAL:='THANH_CONG';
     END IF;
    ELSE
      UPDATE TT_THANH_TRA_LINH_VUC SET MA_KE_HOACH_TT=P_MA_KE_HOACH_TT,
                              MA_DOAN_TT=P_MA_DOAN_TT,
                              LOAI_HINH_THANH_TRA=P_LOAI_HINH_THANH_TRA,
                              NGAY_BD_THANH_TRA=P_NGAY_BD_THANH_TRA,
                              NGAY_KT_THANH_TRA=P_NGAY_KT_THANH_TRA,
                              NOI_DUNG_THANH_TRA=P_NOI_DUNG_THANH_TRA,
                              MA_LINH_VUC_THANH_TRA=P_LINH_VUC_THANH_TRA,
                              TEN_TO_CHUC_CA_NHAN=P_TEN_TO_CHUC_CA_NHAN,
                              LOAI_HINH_DOANH_NGHIEP=P_LOAI_HINH_DN,
                              DIA_CHI_TO_CHUC_CA_NHAN=P_DIA_CHI_TO_CHUC_CA_NHAN,
                              KET_QUA_THANH_TRA=P_KET_QUA_THANH_TRA,
                              KIEN_NGHI_XU_LY=P_KIEN_NGHI_XU_LY,
                              NOI_DUNG_KIEN_NGHI_XU_LY=P_NOI_DUNG_KIEN_NGHI_XU_LY,
                              NOI_DUNG_KIEN_NGHI_THU_HOI=P_NOI_DUNG_KIEN_NGHI_THU_HOI,
                              KIEN_NGHI_THU_HOI=P_KIEN_NGHI_THU_HOI,
                              SO_TIEN_THU_HOI=P_SO_TIEN_THU_HOI,
                              NOI_DUNG_KIEN_NGHI_KHAC=P_NOI_DUNG_KIEN_NGHI_KHAC,
                              KIEN_NGHI_KHAC=P_KIEN_NGHI_KHAC,
                              SO_TIEN_KHAC=P_SO_TIEN_KIEN_NGHI_KHAC,
                              KET_QUA_XU_LY=P_KET_QUA_XU_LY,
                              NOI_DUNG_XU_LY=P_NOI_DUNG_XU_LY,
                              DAT_PHAI_THU=P_DAT_PHAI_THU,
                              SO_TIEN_PHAI_THU=P_SO_TIEN_PHAI_THU,
                              DAT_DA_THU=P_DAT_DA_THU,
                              SO_TIEN_DA_THU=P_SO_TIEN_DA_THU,
                              SO_NGUOI_LIEN_QUAN=P_SO_NGUOI_LIEN_QUAN,
                              SO_TIEN_THAM_NHUNG=P_SO_TIEN_THAM_NHUNG,
                              TAI_SAN_THAM_NHUNG=P_TAI_SAN_THAM_NHUNG,
                              TS_QUY_DOI_THANH_TIEN=P_TS_QUY_DOI_THANH_TIEN,
                              DAT_THAM_NHUNG=P_DAT_THAM_NHUNG,
                              FILE_QUYET_DINH=P_FILE_QUYET_DINH,
                              FILE_NOI_DUNG_THANH_TRA=P_FILE_NOI_DUNG_THANH_TRA,
                              NGAY_KY_QUYET_DINH=P_NGAY_KY_QUYET_DINH,
                              MA_CO_QUAN=P_MA_CO_QUAN
      WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      P_VAL:='UP_THANH_CONG';
    END IF;
 END;
 */

PROCEDURE UPDATE_BIEN_BAN_TT_LINH_VUC(P_VAL OUT NVARCHAR2 --1
                                    , P_UPDATE_TYPE NVARCHAR2 --2
                                    , P_STT_THANH_TRA INTEGER
                                    , P_SO_THANH_TRA NVARCHAR2 --3
                                    , P_LOAI_HINH_THANH_TRA NVARCHAR2 --4
                                    , P_MA_KE_HOACH_TT INTEGER --5
                                    , P_MA_DOAN_TT INTEGER --6
                                    , P_NGAY_BD_THANH_TRA NVARCHAR2 --7
                                    , P_NGAY_KT_THANH_TRA NVARCHAR2 --8
                                    , P_NOI_DUNG_THANH_TRA NVARCHAR2 --9
                                    , P_TEN_TO_CHUC_CA_NHAN NVARCHAR2 --10
                                    , P_LOAI_HINH_DOANH_NGHIEP INTEGER --11
                                    , P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2 --12
                                    , P_MA_LINH_VUC_THANH_TRA NVARCHAR2 --13
                                    , P_KET_QUA_THANH_TRA NVARCHAR2 --14
                                    , P_KIEN_NGHI_XU_LY INTEGER --15
                                    , P_NOI_DUNG_XU_LY NVARCHAR2 --16
                                    , P_KIEN_NGHI_THU_HOI NVARCHAR2 --17
                                    , P_SO_TIEN_THU_HOI FLOAT --18
                                    , P_KIEN_NGHI_KHAC NVARCHAR2 --19
                                    , P_SO_TIEN_KHAC NVARCHAR2 --20
                                    , P_MA_CO_QUAN NVARCHAR2 --21
                                    , P_NOI_DUNG_KIEN_NGHI_XU_LY NVARCHAR2 --22
                                    , P_KET_QUA_XU_LY INTEGER --23
                                    , P_DAT_PHAI_THU FLOAT --24
                                    , P_SO_TIEN_PHAI_THU FLOAT --25
                                    , P_DAT_DA_THU FLOAT --26
                                    , P_SO_TIEN_DA_THU FLOAT --27
                                    , P_NOI_DUNG_KIEN_NGHI_THU_HOI NVARCHAR2 --28
                                    , P_NOI_DUNG_KIEN_NGHI_KHAC NVARCHAR2 --29
                                    , P_SO_TIEN_THAM_NHUNG FLOAT --30
                                    , P_TAI_SAN_THAM_NHUNG NVARCHAR2 --31
                                    , P_TS_QUY_DOI_THANH_TIEN FLOAT --32
                                    , P_DAT_THAM_NHUNG FLOAT --33
                                    , P_SO_NGUOI_LIEN_QUAN INTEGER --34
                                    , P_FILE_QUYET_DINH NVARCHAR2 --35
                                    , P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2 --36
                                    , P_NGAY_KY_QUYET_DINH NVARCHAR2 --37
                                    ) 
AS
TON_TAI INTEGER;
BEGIN
  P_VAL:='LOI';
  IF P_UPDATE_TYPE='THEM_MOI' THEN
    SELECT COUNT(SO_THANH_TRA)
    INTO TON_TAI
    FROM TT_THANH_TRA_LINH_VUC
    WHERE SO_THANH_TRA=P_SO_THANH_TRA;
    IF TON_TAI > 0 THEN
      P_VAL:='TON_TAI';
    ELSE
      INSERT INTO TT_THANH_TRA_LINH_VUC(STT_THANH_TRA
                                      , SO_THANH_TRA --1
                                      , LOAI_HINH_THANH_TRA --2
                                      , MA_KE_HOACH_TT --3
                                      , MA_DOAN_TT --4
                                      , NGAY_BD_THANH_TRA --5
                                      , NGAY_KT_THANH_TRA --6
                                      , NOI_DUNG_THANH_TRA --7
                                      , TEN_TO_CHUC_CA_NHAN --8
                                      , LOAI_HINH_DOANH_NGHIEP --9
                                      , DIA_CHI_TO_CHUC_CA_NHAN --10
                                      , MA_LINH_VUC_THANH_TRA --11
                                      , KET_QUA_THANH_TRA --12
                                      , KIEN_NGHI_XU_LY --13
                                      , NOI_DUNG_XU_LY --14
                                      , KIEN_NGHI_THU_HOI --15
                                      , SO_TIEN_THU_HOI --16
                                      , KIEN_NGHI_KHAC --17
                                      , SO_TIEN_KHAC --18
                                      , MA_CO_QUAN --19
                                      , NOI_DUNG_KIEN_NGHI_XU_LY --20
                                      , KET_QUA_XU_LY --21
                                      , DAT_PHAI_THU --22
                                      , SO_TIEN_PHAI_THU --23
                                      , DAT_DA_THU --24
                                      , SO_TIEN_DA_THU --25
                                      , NOI_DUNG_KIEN_NGHI_THU_HOI --26
                                      , NOI_DUNG_KIEN_NGHI_KHAC --27
                                      , SO_TIEN_THAM_NHUNG --28
                                      , TAI_SAN_THAM_NHUNG --29
                                      , TS_QUY_DOI_THANH_TIEN --30
                                      , DAT_THAM_NHUNG --31
                                      , SO_NGUOI_LIEN_QUAN --32
                                      , FILE_QUYET_DINH --33
                                      , FILE_NOI_DUNG_THANH_TRA --34
                                      , NGAY_KY_QUYET_DINH --35
                                      )
      VALUES(SEQ_TT_THANH_TRA_LINH_VUC.NEXTVAL
      , P_SO_THANH_TRA --1
      , P_LOAI_HINH_THANH_TRA --2
      , P_MA_KE_HOACH_TT --3
      , P_MA_DOAN_TT --4
      , P_NGAY_BD_THANH_TRA --5
      , P_NGAY_KT_THANH_TRA --6
      , P_NOI_DUNG_THANH_TRA --7
      , P_TEN_TO_CHUC_CA_NHAN --8
      , P_LOAI_HINH_DOANH_NGHIEP --9
      , P_DIA_CHI_TO_CHUC_CA_NHAN --10
      , P_MA_LINH_VUC_THANH_TRA --11
      , P_KET_QUA_THANH_TRA --12
      , P_KIEN_NGHI_XU_LY --13
      , P_NOI_DUNG_XU_LY --14
      , P_KIEN_NGHI_THU_HOI --15
      , P_SO_TIEN_THU_HOI --16
      , P_KIEN_NGHI_KHAC --17
      , P_SO_TIEN_KHAC --18
      , P_MA_CO_QUAN --19
      , P_NOI_DUNG_KIEN_NGHI_XU_LY --20
      , P_KET_QUA_XU_LY --21
      , P_DAT_PHAI_THU --22
      , P_SO_TIEN_PHAI_THU --23
      , P_DAT_DA_THU --24
      , P_SO_TIEN_DA_THU --25
      , P_NOI_DUNG_KIEN_NGHI_THU_HOI --26
      , P_NOI_DUNG_KIEN_NGHI_KHAC --27
      , P_SO_TIEN_THAM_NHUNG --28
      , P_TAI_SAN_THAM_NHUNG --29
      , P_TS_QUY_DOI_THANH_TIEN --30
      , P_DAT_THAM_NHUNG --31
      , P_SO_NGUOI_LIEN_QUAN --32
      , P_FILE_QUYET_DINH --33
      , P_FILE_NOI_DUNG_THANH_TRA --34
      , P_NGAY_KY_QUYET_DINH --35
      );
      P_VAL:='THANH_CONG';
    END IF;
  ELSE
    UPDATE TT_THANH_TRA_LINH_VUC
    SET 
        SO_THANH_TRA = P_SO_THANH_TRA
      , LOAI_HINH_THANH_TRA = P_LOAI_HINH_THANH_TRA
      , MA_KE_HOACH_TT = P_MA_KE_HOACH_TT
      , MA_DOAN_TT = P_MA_DOAN_TT
      , NGAY_BD_THANH_TRA = P_NGAY_BD_THANH_TRA
      , NGAY_KT_THANH_TRA = P_NGAY_KT_THANH_TRA
      , NOI_DUNG_THANH_TRA = P_NOI_DUNG_THANH_TRA
      , TEN_TO_CHUC_CA_NHAN = P_TEN_TO_CHUC_CA_NHAN
      , LOAI_HINH_DOANH_NGHIEP = P_LOAI_HINH_DOANH_NGHIEP
      , DIA_CHI_TO_CHUC_CA_NHAN = P_DIA_CHI_TO_CHUC_CA_NHAN
      , MA_LINH_VUC_THANH_TRA = P_MA_LINH_VUC_THANH_TRA
      , KET_QUA_THANH_TRA = P_KET_QUA_THANH_TRA
      , KIEN_NGHI_XU_LY = P_KIEN_NGHI_XU_LY
      , NOI_DUNG_XU_LY = P_NOI_DUNG_XU_LY
      , KIEN_NGHI_THU_HOI = P_KIEN_NGHI_THU_HOI
      , SO_TIEN_THU_HOI = P_SO_TIEN_THU_HOI
      , KIEN_NGHI_KHAC = P_KIEN_NGHI_KHAC
      , SO_TIEN_KHAC = P_SO_TIEN_KHAC
      , MA_CO_QUAN = P_MA_CO_QUAN
      , NOI_DUNG_KIEN_NGHI_XU_LY = P_NOI_DUNG_KIEN_NGHI_XU_LY
      , KET_QUA_XU_LY = P_KET_QUA_XU_LY
      , DAT_PHAI_THU = P_DAT_PHAI_THU
      , SO_TIEN_PHAI_THU = P_SO_TIEN_PHAI_THU
      , DAT_DA_THU = P_DAT_DA_THU
      , SO_TIEN_DA_THU = P_SO_TIEN_DA_THU
      , NOI_DUNG_KIEN_NGHI_THU_HOI = P_NOI_DUNG_KIEN_NGHI_THU_HOI
      , NOI_DUNG_KIEN_NGHI_KHAC = P_NOI_DUNG_KIEN_NGHI_KHAC
      , SO_TIEN_THAM_NHUNG = P_SO_TIEN_THAM_NHUNG
      , TAI_SAN_THAM_NHUNG = P_TAI_SAN_THAM_NHUNG
      , TS_QUY_DOI_THANH_TIEN = P_TS_QUY_DOI_THANH_TIEN
      , DAT_THAM_NHUNG = P_DAT_THAM_NHUNG
      , SO_NGUOI_LIEN_QUAN = P_SO_NGUOI_LIEN_QUAN
      , FILE_QUYET_DINH = P_FILE_QUYET_DINH
      , FILE_NOI_DUNG_THANH_TRA = P_FILE_NOI_DUNG_THANH_TRA
      , NGAY_KY_QUYET_DINH = P_NGAY_KY_QUYET_DINH
    WHERE STT_THANH_TRA = P_STT_THANH_TRA;
    P_VAL:='UP_THANH_CONG';
    END IF;
END;
 

/*PROCEDURE UPDATE_BIEN_BAN_TT_LINH_VUC(P_VAL OUT NVARCHAR2,
                                      P_UPDATE_TYPE NVARCHAR2,
                                      P_SO_THANH_TRA NVARCHAR2,--1
                                      P_LOAI_HINH_THANH_TRA NVARCHAR2,--2
                                      P_MA_KE_HOACH_TT INT, --3
                                      P_MA_DOAN_TT INT,--4                                      
                                      P_NGAY_BD_THANH_TRA NVARCHAR2,--5
                                      P_NGAY_KT_THANH_TRA NVARCHAR2,--6
                                      P_NOI_DUNG_THANH_TRA NVARCHAR2,--7                                      
                                      P_TEN_TO_CHUC_CA_NHAN NVARCHAR2,--8
                                      P_LOAI_HINH_DN INT, --9
                                      P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2,--10
                                      P_LINH_VUC_THANH_TRA NVARCHAR2,--11
                                      P_KET_QUA_THANH_TRA NVARCHAR2,--12
                                      P_KIEN_NGHI_XU_LY INT, --13
                                      P_NOI_DUNG_XU_LY NVARCHAR2,--14                                                                            
                                      P_KIEN_NGHI_THU_HOI NVARCHAR2,--15
                                      P_SO_TIEN_THU_HOI FLOAT,--16
                                      P_KIEN_NGHI_KHAC NVARCHAR2,--17
                                      P_SO_TIEN_KIEN_NGHI_KHAC NVARCHAR2,--18
                                      P_MA_CO_QUAN NVARCHAR2,--19
                                      P_NOI_DUNG_KIEN_NGHI_XU_LY NVARCHAR2,--20
                                      P_KET_QUA_XU_LY INT,--21
                                      P_DAT_PHAI_THU FLOAT,--22
                                      P_SO_TIEN_PHAI_THU FLOAT,--23
                                      P_DAT_DA_THU FLOAT,--24
                                      P_SO_TIEN_DA_THU FLOAT,--25
                                      P_NOI_DUNG_KIEN_NGHI_THU_HOI NVARCHAR2,--26                                                                            
                                      P_NOI_DUNG_KIEN_NGHI_KHAC NVARCHAR2,--27 
                                      P_SO_TIEN_THAM_NHUNG FLOAT,--28
                                      P_TAI_SAN_THAM_NHUNG NVARCHAR2,--29
                                      P_TS_QUY_DOI_THANH_TIEN FLOAT,--30
                                      P_DAT_THAM_NHUNG FLOAT,--31
                                      P_SO_NGUOI_LIEN_QUAN INT,--32
                                      P_FILE_QUYET_DINH NVARCHAR2,--33 
                                      P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2,--34
                                      P_NGAY_KY_QUYET_DINH NVARCHAR2--35                                                                                                                                                                                                                                                                                                                                                                                                
                                      ) AS
  TON_TAI INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(SO_THANH_TRA) INTO TON_TAI FROM TT_THANH_TRA_LINH_VUC WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      IF TON_TAI > 0 THEN
        P_VAL:='TON_TAI';
      ELSE 
        INSERT INTO TT_THANH_TRA_LINH_VUC(SO_THANH_TRA,--1
                                          LOAI_HINH_THANH_TRA,--2                                                                                    
                                          MA_KE_HOACH_TT,--3
                                          MA_DOAN_TT,--4                                          
                                          NGAY_BD_THANH_TRA,--5
                                          NGAY_KT_THANH_TRA,--6
                                          NOI_DUNG_THANH_TRA,--7
                                          TEN_TO_CHUC_CA_NHAN,--8
                                          LOAI_HINH_DOANH_NGHIEP,--9
                                          DIA_CHI_TO_CHUC_CA_NHAN,--10
                                          MA_LINH_VUC_THANH_TRA,--11                                          
                                          KET_QUA_THANH_TRA,--12
                                          KIEN_NGHI_XU_LY,--13
                                          NOI_DUNG_XU_LY,--14
                                          KIEN_NGHI_THU_HOI,--15
                                          SO_TIEN_THU_HOI,--16
                                          KIEN_NGHI_KHAC,--17
                                          SO_TIEN_KHAC,--18
                                          MA_CO_QUAN,--19
                                          NOI_DUNG_KIEN_NGHI_XU_LY,--20
                                          KET_QUA_XU_LY,--21
                                          DAT_PHAI_THU,--22 
                                          SO_TIEN_PHAI_THU,--23 
                                          DAT_DA_THU, --24
                                          SO_TIEN_DA_THU,--25
                                          NOI_DUNG_KIEN_NGHI_THU_HOI,--26                                          
                                          NOI_DUNG_KIEN_NGHI_KHAC,--27
                                          SO_TIEN_THAM_NHUNG,--28 
                                          TAI_SAN_THAM_NHUNG, --29
                                          TS_QUY_DOI_THANH_TIEN, --30
                                          DAT_THAM_NHUNG,--31                                                                                                                                                                         
                                          SO_NGUOI_LIEN_QUAN,--32                                          
                                          FILE_QUYET_DINH,--33
                                          FILE_NOI_DUNG_THANH_TRA,--34
                                          NGAY_KY_QUYET_DINH--35
                                          )
                        VALUES(P_SO_THANH_TRA,--1
                                      P_LOAI_HINH_THANH_TRA,--2
                                      P_MA_KE_HOACH_TT, --3
                                      P_MA_DOAN_TT,--4                                      
                                      P_NGAY_BD_THANH_TRA,--5
                                      P_NGAY_KT_THANH_TRA,--6
                                      P_NOI_DUNG_THANH_TRA,--7                                      
                                      P_TEN_TO_CHUC_CA_NHAN,--8
                                      P_LOAI_HINH_DN, --9
                                      P_DIA_CHI_TO_CHUC_CA_NHAN,--10
                                      P_LINH_VUC_THANH_TRA,--11
                                      P_KET_QUA_THANH_TRA,--12
                                      P_KIEN_NGHI_XU_LY, --13
                                      P_NOI_DUNG_XU_LY,--14                                                                            
                                      P_KIEN_NGHI_THU_HOI,--15
                                      P_SO_TIEN_THU_HOI,--16
                                      P_KIEN_NGHI_KHAC,--17
                                      P_SO_TIEN_KIEN_NGHI_KHAC,--18
                                      P_MA_CO_QUAN,--19
                                      P_NOI_DUNG_KIEN_NGHI_XU_LY,--20
                                      P_KET_QUA_XU_LY,--21
                                      P_DAT_PHAI_THU,--22
                                      P_SO_TIEN_PHAI_THU,--23
                                      P_DAT_DA_THU,--24
                                      P_SO_TIEN_DA_THU,--25
                                      P_NOI_DUNG_KIEN_NGHI_THU_HOI,--26                                                                            
                                      P_NOI_DUNG_KIEN_NGHI_KHAC,--27 
                                      P_SO_TIEN_THAM_NHUNG,--28
                                      P_TAI_SAN_THAM_NHUNG,--29
                                      P_TS_QUY_DOI_THANH_TIEN,--30
                                      P_DAT_THAM_NHUNG,--31
                                      P_SO_NGUOI_LIEN_QUAN,--32
                                      P_FILE_QUYET_DINH,--33 
                                      P_FILE_NOI_DUNG_THANH_TRA,--34
                                      P_NGAY_KY_QUYET_DINH--35
                                      );
      P_VAL:='THANH_CONG';
     END IF;
    ELSE
      UPDATE TT_THANH_TRA_LINH_VUC SET LOAI_HINH_THANH_TRA = P_LOAI_HINH_THANH_TRA,--2                                                                                  
                                       MA_KE_HOACH_TT = P_MA_KE_HOACH_TT, --3
                                       MA_DOAN_TT = P_MA_DOAN_TT,--4                                       
                                       NGAY_BD_THANH_TRA = P_NGAY_BD_THANH_TRA,--5
                                       NGAY_KT_THANH_TRA = P_NGAY_KT_THANH_TRA,--6
                                       NOI_DUNG_THANH_TRA = P_NOI_DUNG_THANH_TRA,--7
                                       TEN_TO_CHUC_CA_NHAN = P_TEN_TO_CHUC_CA_NHAN,--8
                                       LOAI_HINH_DOANH_NGHIEP = P_LOAI_HINH_DN, --9
                                       DIA_CHI_TO_CHUC_CA_NHAN = P_DIA_CHI_TO_CHUC_CA_NHAN,--10
                                       MA_LINH_VUC_THANH_TRA = P_LINH_VUC_THANH_TRA,--11                                         
                                       KET_QUA_THANH_TRA = P_KET_QUA_THANH_TRA,--12
                                       KIEN_NGHI_XU_LY = P_KIEN_NGHI_XU_LY, --13
                                       NOI_DUNG_XU_LY = P_NOI_DUNG_XU_LY,--14
                                       KIEN_NGHI_THU_HOI = P_KIEN_NGHI_THU_HOI,--15
                                       SO_TIEN_THU_HOI = P_SO_TIEN_THU_HOI,--16
                                       KIEN_NGHI_KHAC = P_KIEN_NGHI_KHAC,--17
                                       SO_TIEN_KHAC = P_SO_TIEN_KIEN_NGHI_KHAC,--18
                                       MA_CO_QUAN = P_MA_CO_QUAN,--19
                                       NOI_DUNG_KIEN_NGHI_XU_LY = P_NOI_DUNG_KIEN_NGHI_XU_LY,--20
                                       KET_QUA_XU_LY = P_KET_QUA_XU_LY,--21
                                       DAT_PHAI_THU = P_DAT_PHAI_THU,--22
                                       SO_TIEN_PHAI_THU = P_SO_TIEN_PHAI_THU,--23
                                       DAT_DA_THU = P_DAT_DA_THU,--24
                                       SO_TIEN_DA_THU = P_SO_TIEN_DA_THU,--25
                                       NOI_DUNG_KIEN_NGHI_THU_HOI = P_NOI_DUNG_KIEN_NGHI_THU_HOI,--26                                         
                                       NOI_DUNG_KIEN_NGHI_KHAC = P_NOI_DUNG_KIEN_NGHI_KHAC,--27
                                       SO_TIEN_THAM_NHUNG = P_SO_TIEN_THAM_NHUNG,--28
                                       TAI_SAN_THAM_NHUNG = P_TAI_SAN_THAM_NHUNG,--29
                                       TS_QUY_DOI_THANH_TIEN = P_TS_QUY_DOI_THANH_TIEN,--30
                                       DAT_THAM_NHUNG = P_DAT_THAM_NHUNG,--31                                                                                                                                                                        
                                       SO_NGUOI_LIEN_QUAN = P_SO_NGUOI_LIEN_QUAN,--32                                         
                                       FILE_QUYET_DINH = P_FILE_QUYET_DINH,--33
                                       FILE_NOI_DUNG_THANH_TRA = P_FILE_NOI_DUNG_THANH_TRA,--34
                                       NGAY_KY_QUYET_DINH = P_NGAY_KY_QUYET_DINH--35
      WHERE SO_THANH_TRA = P_SO_THANH_TRA;
      P_VAL:='UP_THANH_CONG';
    END IF;
 END;*/
 --===========//SELECT 1 BIEN BAN THANH TRA LINH VUC//=============--
PROCEDURE SELECT_BIEN_BAN_TT_LINH_VUC(P_CUR IN OUT MYCUR,P_STT_THANH_TRA INTEGER) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_THANH_TRA_LINH_VUC A
    WHERE A.STT_THANH_TRA=P_STT_THANH_TRA;
  END;
--===========//SELECT BIEN BAN THANH TRA LINH VUC//=============--
PROCEDURE SELECT_BIEN_BAN_TT_LINH_VUCS(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    /*SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT
    UNION 
    SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A
    WHERE SO_THANH_TRA NOT IN(SELECT A.SO_THANH_TRA FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT);*/
    SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT FROM TT_THANH_TRA_LINH_VUC A
    left join TT_KE_HOACH_THANH_TRA B on  A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    left join TT_DOAN_THANH_TRA C on A.MA_DOAN_TT=C.MA_DOAN_TT
    UNION 
    SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA 
    FROM TT_THANH_TRA_LINH_VUC A
    WHERE SO_THANH_TRA NOT IN(SELECT A.SO_THANH_TRA 
                          FROM TT_THANH_TRA_LINH_VUC A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
                          WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
                          AND A.MA_DOAN_TT=C.MA_DOAN_TT);
  END;
--===========//delete 1 BIEN BAN THANH TRA LINH VUC//=============--
PROCEDURE DELETE_BIEN_BAN_TT_LINH_VUC(P_VAL OUT NVARCHAR2,P_STT_THANH_TRA NVARCHAR2,P_NGUOI_XOA INT) AS
  BEGIN
    P_VAL:='CL';
    IF P_NGUOI_XOA =0 THEN
      DELETE FROM TT_THANH_TRA_LINH_VUC WHERE STT_THANH_TRA=P_STT_THANH_TRA;
      P_VAL:='THANH_CONG';
    END IF;
  END;
--===========// select dang thanh tra 
PROCEDURE SELECT_DANG_VI_PHAM(P_CUR IN OUT MYCUR, P_MA_LINH_VUC NVARCHAR2,P_SO_THANH_TRA NVARCHAR2, P_MA_CO_QUAN NVARCHAR2) AS
 V_TS INT;
  BEGIN
    BEGIN
    SELECT COUNT(MA_DANG_VI_PHAM) INTO V_TS FROM TT_DANG_VI_PHAM A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
    AND A.MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC
    GROUP BY MA_CO_QUAN;
    EXCEPTION 
      WHEN NO_DATA_FOUND THEN
      V_TS:=0;
    END;
    OPEN P_CUR FOR SELECT MA_DANG_VI_PHAM,TEN_DANG_VI_PHAM,SUM(VI_PHAM) AS VI_PHAM,STT_HIEN_THI,TS FROM (SELECT A.MA_DANG_VI_PHAM,A.TEN_DANG_VI_PHAM,NULL AS VI_PHAM,A.STT_HIEN_THI,V_TS AS TS FROM TT_DANG_VI_PHAM A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
    AND A.MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC
    UNION
    SELECT B.MA_DANG_VI_PHAM,C.TEN_DANG_VI_PHAM, B.VI_PHAM,C.STT_HIEN_THI, V_TS AS TS FROM TT_THANH_TRA_CO_DANG_VI_PHAM B,TT_DANG_VI_PHAM C
    WHERE B.MA_DANG_VI_PHAM=C.MA_DANG_VI_PHAM
    AND B.SO_THANH_TRA=P_SO_THANH_TRA) KQ
    GROUP BY MA_DANG_VI_PHAM,TEN_DANG_VI_PHAM,STT_HIEN_THI,TS
    ORDER BY KQ.STT_HIEN_THI;
 END;
PROCEDURE UPDATE_TT_DANG_VI_PHAM(P_MA_DANG_VI_PHAM INT,P_SO_THANH_TRA NVARCHAR2,P_VI_PHAM NVARCHAR2) AS
  BEGIN
    INSERT INTO TT_THANH_TRA_CO_DANG_VI_PHAM(MA_DANG_VI_PHAM,SO_THANH_TRA,VI_PHAM)
                                      VALUES(P_MA_DANG_VI_PHAM,P_SO_THANH_TRA,P_VI_PHAM);
  END;
PROCEDURE DELETE_TT_DANG_VI_PHAM(P_SO_THANH_TRA NVARCHAR2) AS
  BEGIN
    DELETE FROM TT_THANH_TRA_CO_DANG_VI_PHAM
    WHERE SO_THANH_TRA=P_SO_THANH_TRA;
  END;
-----------------------------------------------
PROCEDURE SELECT_LY_DO_TTL(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_LY_DO_TTL,A.LY_DO_TTL FROM TT_LY_DO_THANH_TRA_LAI A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;
------------------------------------------------
PROCEDURE UPDATE_THANH_TRA_LAI(P_VAL OUT NVARCHAR2,P_UPDATE_TYPE NVARCHAR2, P_SO_THANH_TRA INT, P_NGAY_TTL NVARCHAR2,P_LAN_TTL NVARCHAR2,
                               P_LY_DO_TTL NVARCHAR2,P_KET_QUA_TTL NVARCHAR2,P_KIEN_NGHI_XU_LY NVARCHAR2, P_NOI_DUNG_KIEN_NGHI_XU_LY NVARCHAR2,
                               P_NOI_DUNG_KIEN_NGHI_THU_HOI NVARCHAR2, P_KIEN_NGHI_THU_HOI NVARCHAR2, P_SO_TIEN_THU_HOI NVARCHAR2, P_NOI_DUNG_KIEN_NGHI_KHAC NVARCHAR2,
                               P_KIEN_NGHI_KHAC NVARCHAR2, P_SO_TIEN_KHAC NVARCHAR2, P_KET_QUA_XU_LY NVARCHAR2, P_NOI_DUNG_XU_LY NVARCHAR2,
                               P_NOI_DUNG_VI_PHAM_KINH_TE NVARCHAR2,P_DAT_VI_PHAM NVARCHAR2,P_SO_TIEN_VI_PHAM NVARCHAR2,P_DAT_DA_THU NVARCHAR2,P_SO_TIEN_DA_THU NVARCHAR2
                               ,P_SO_TIEN_THAM_NHUNG FLOAT, P_TAI_SAN_THAM_NHUNG NVARCHAR2, P_TS_QUY_DOI_THANH_TIEN FLOAT,P_DAT_THAM_NHUNG FLOAT) AS
  V_KT INT;
  BEGIN
    P_VAL:='CL';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(SO_THANH_TRA) INTO V_KT FROM TT_THANH_TRA_LAI WHERE SO_THANH_TRA=P_SO_THANH_TRA AND LAN_THANH_TRA_LAI=P_LAN_TTL;
      IF V_KT=0 THEN
         INSERT INTO TT_THANH_TRA_LAI(SO_THANH_TRA,NGAY_THANH_TRA_LAI,LAN_THANH_TRA_LAI,LY_DO_THANH_TRA_LAI,KET_QUA_THANH_TRA_LAI,KIEN_NGHI_XU_LY,
                                   NOI_DUNG_KIEN_NGHI_XU_LY,NOI_DUNG_KIEN_NGHI_THU_HOI,KIEN_NGHI_THU_HOI,SO_TIEN_THU_HOI,NOI_DUNG_KIEN_NGHI_KHAC,
                                   KIEN_NGHI_KHAC,SO_TIEN_KHAC,NOI_DUNG_VI_PHAM_KINH_TE,DAT_VI_PHAM,SO_TIEN_VI_PHAM,KET_QUA_XU_LY,NOI_DUNG_XU_LY,DAT_DA_THU,SO_TIEN_DA_THU
                                   ,SO_TIEN_THAM_NHUNG,TAI_SAN_THAM_NHUNG,TS_QUY_DOI_THANH_TIEN,DAT_THAM_NHUNG)
                             VALUES(P_SO_THANH_TRA,P_NGAY_TTL,P_LAN_TTL,P_LY_DO_TTL,P_KET_QUA_TTL,P_KIEN_NGHI_XU_LY,P_NOI_DUNG_KIEN_NGHI_XU_LY,
                                   P_NOI_DUNG_KIEN_NGHI_THU_HOI,P_KIEN_NGHI_THU_HOI,P_SO_TIEN_THU_HOI,P_NOI_DUNG_KIEN_NGHI_KHAC,P_KIEN_NGHI_KHAC,
                                   P_SO_TIEN_KHAC,P_NOI_DUNG_VI_PHAM_KINH_TE,P_DAT_VI_PHAM,P_SO_TIEN_VI_PHAM,P_KET_QUA_XU_LY,P_NOI_DUNG_XU_LY,P_DAT_DA_THU,P_SO_TIEN_DA_THU
                                   ,P_SO_TIEN_THAM_NHUNG, P_TAI_SAN_THAM_NHUNG, P_TS_QUY_DOI_THANH_TIEN, P_DAT_THAM_NHUNG);
         P_VAL:='THANH_CONG';
      ELSE
         P_VAL:='TON_TAI';
      END IF;
    ELSE
      UPDATE TT_THANH_TRA_LAI SET NGAY_THANH_TRA_LAI=P_NGAY_TTL,
                              LY_DO_THANH_TRA_LAI=P_LY_DO_TTL,
                              KET_QUA_THANH_TRA_LAI=P_KET_QUA_TTL,
                              KIEN_NGHI_XU_LY=P_KIEN_NGHI_XU_LY,
                              NOI_DUNG_KIEN_NGHI_XU_LY=P_NOI_DUNG_KIEN_NGHI_XU_LY,
                              NOI_DUNG_KIEN_NGHI_THU_HOI=P_NOI_DUNG_KIEN_NGHI_THU_HOI,
                              KIEN_NGHI_THU_HOI=P_KIEN_NGHI_THU_HOI,
                              SO_TIEN_THU_HOI=P_SO_TIEN_THU_HOI,
                              NOI_DUNG_KIEN_NGHI_KHAC=P_NOI_DUNG_KIEN_NGHI_KHAC,
                              KIEN_NGHI_KHAC=P_KIEN_NGHI_KHAC,
                              SO_TIEN_KHAC=P_SO_TIEN_KHAC,
                              NOI_DUNG_VI_PHAM_KINH_TE=P_NOI_DUNG_VI_PHAM_KINH_TE,
                              DAT_VI_PHAM=P_DAT_VI_PHAM,
                              SO_TIEN_VI_PHAM=P_SO_TIEN_VI_PHAM,
                              KET_QUA_XU_LY=P_KET_QUA_XU_LY,
                              NOI_DUNG_XU_LY=P_NOI_DUNG_XU_LY,
                              DAT_DA_THU=P_DAT_DA_THU,
                              SO_TIEN_DA_THU=P_SO_TIEN_DA_THU
                              ,SO_TIEN_THAM_NHUNG=P_SO_TIEN_THAM_NHUNG,
                              TAI_SAN_THAM_NHUNG=P_TAI_SAN_THAM_NHUNG,
                              TS_QUY_DOI_THANH_TIEN=P_TS_QUY_DOI_THANH_TIEN,
                              DAT_THAM_NHUNG=P_DAT_THAM_NHUNG
        WHERE SO_THANH_TRA=P_SO_THANH_TRA AND LAN_THANH_TRA_LAI=P_LAN_TTL;
       P_VAL:='THANH_CONG';
    END IF;
  END;
PROCEDURE SELECT_THANH_TRA_LAI(P_CUR OUT MYCUR,P_SO_THANH_TRA INT,P_LAN_TTL INT) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_THANH_TRA_LAI A
    WHERE A.SO_THANH_TRA=P_SO_THANH_TRA
    AND (A.LAN_THANH_TRA_LAI=P_LAN_TTL OR P_LAN_TTL IS NULL)
    AND LAN_THANH_TRA_LAI =( SELECT MAX(LAN_THANH_TRA_LAI) FROM TT_THANH_TRA_LAI A
                           WHERE A.SO_THANH_TRA=P_SO_THANH_TRA
                           AND (A.LAN_THANH_TRA_LAI=P_LAN_TTL OR P_LAN_TTL IS NULL));
  END;
END TNMT_THANHTRA_H;
/

prompt
prompt Creating package body TNMT_THANHTRA_NAT
prompt =======================================
prompt
create or replace package body hai.TNMT_THANHTRA_NAT is
------------------SELECT_ALL_QUYET_DINH-----------------------
PROCEDURE SELECT_ALL_QUYET_DINH(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 TT_QUYET_DINH A
        WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
       ORDER BY A.MA_QUYET_DINH;
  END;
--------------------SELECT_SINGLE_QUYET_DINH-----------------
PROCEDURE SELECT_SINGLE_QUYET_DINH (P_CUR IN OUT MYCUR, P_MA_QUYET_DINH NUMBER)AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_QUYET_DINH, A.TEN_QUYET_DINH, A.NOI_DUNG, A.FILE_DINH_KEM, A.NGAY_CAP_NHAT,A.TRANG_THAI,
           CASE
             WHEN A.TRANG_THAI = 0 THEN
               'Mở'
               ELSE
                 'Ðóng'
           END AS TEN_TRANG_THAI
           FROM TT_QUYET_DINH A
           WHERE A.MA_QUYET_DINH = P_MA_QUYET_DINH;
  END; 
------------------DELETE_QUYET_DINH---------------------
 PROCEDURE DELETE_QUYET_DINH(  P_VAL            OUT NVARCHAR2,
                               P_MA_QUYET_DINH NUMBER) AS
  BEGIN
    P_VAL := 'CO_LOI';
       DELETE FROM TT_QUYET_DINH
       WHERE MA_QUYET_DINH LIKE P_MA_QUYET_DINH;
      P_VAL := 'THANH_CONG';
  END;
 
-------------------------UPDATE_QUYET_DINH-------------------
PROCEDURE UPDATE_QUYET_DINH(P_VAL          OUT NVARCHAR2,
                           P_UPDATE_TYPE    VARCHAR2,
                           P_MA_QUYET_DINH  INT,
                           P_TEN_QUYET_DINH NVARCHAR2,
                           P_NOI_DUNG       NVARCHAR2,
                           P_FILE_DINH_KEM  NVARCHAR2,
                           P_NGUOI_CAP_NHAT INT,
                           P_TRANG_THAI     INT,
                           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_QUYET_DINH) INTO V_TON_TAI_TEN FROM TT_QUYET_DINH
      WHERE UPPER(TEN_QUYET_DINH) = UPPER(P_TEN_QUYET_DINH);
      IF V_TON_TAI_TEN > 0 THEN P_VAL :='TON_TAI_TEN';
       ELSE
        INSERT INTO TT_QUYET_DINH
           (TEN_QUYET_DINH,
           NOI_DUNG,
           FILE_DINH_KEM,
           NGUOI_CAP_NHAT,
           NGAY_CAP_NHAT,
           TRANG_THAI,
           MA_CO_QUAN
          )
        VALUES
          (P_TEN_QUYET_DINH,
           P_NOI_DUNG,
           P_FILE_DINH_KEM,
           P_NGUOI_CAP_NHAT,
           SYSDATE,
           P_TRANG_THAI,
           P_MA_CO_QUAN
          );
        P_VAL := 'THANH_CONG';
     END IF;
     ELSE
      SELECT COUNT(MA_QUYET_DINH)INTO V_TON_TAI_TEN FROM TT_QUYET_DINH
      WHERE UPPER(TEN_QUYET_DINH) = UPPER(P_TEN_QUYET_DINH)
      AND MA_QUYET_DINH <> P_MA_QUYET_DINH;
      IF V_TON_TAI_TEN >0 THEN P_VAL :='TON_TAI_TEN';
      ELSE   
        UPDATE TT_QUYET_DINH
           SET TEN_QUYET_DINH = P_TEN_QUYET_DINH,
               NOI_DUNG = P_NOI_DUNG,
               FILE_DINH_KEM = P_FILE_DINH_KEM,
               NGUOI_CAP_NHAT = P_NGUOI_CAP_NHAT,
               NGAY_CAP_NHAT = SYSDATE,
               TRANG_THAI = P_TRANG_THAI,
               MA_CO_QUAN=P_MA_CO_QUAN        
         WHERE MA_QUYET_DINH = P_MA_QUYET_DINH;
        P_VAL := 'THANH_CONG';
    END IF;
    END IF;
  END;
--------------------SELECT_ALL_KEHOACH_TT--------------------
PROCEDURE SELECT_ALL_KEHOACH_TT(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 TT_KE_HOACH_THANH_TRA A WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
       ORDER BY A.MA_KE_HOACH_TT;
  END;
--------------------SELECT_SINGLE_KEHOACH_TT------------------
PROCEDURE SELECT_SINGLE_KEHOACH_TT (P_CUR IN OUT MYCUR, P_MA_KE_HOACH_TT INT)AS
  BEGIN
    OPEN P_CUR FOR
    SELECT A.MA_KE_HOACH_TT, A.TEN_KE_HOACH_TT, A.NGAY_LAP_KE_HOACH, A.FILE_DINH_KEM, A.NGAY_CAP_NHAT, A.TRANG_THAI,
           a.ma_quyet_dinh,CASE
             WHEN A.TRANG_THAI = 0 THEN
               'Mở'
               ELSE
                 'Ðóng'
           END AS TEN_TRANG_THAI
           FROM TT_KE_HOACH_THANH_TRA A
           WHERE A.MA_KE_HOACH_TT = P_MA_KE_HOACH_TT; 
  END; 
--------------------DELETE_KEHOACH_TT--------------------
 PROCEDURE DELETE_KEHOACH_TT(  P_VAL            OUT NVARCHAR2,
                               P_MA_KE_HOACH_TT INT) AS
  BEGIN
    P_VAL := 'CO_LOI';
       DELETE FROM TT_KE_HOACH_THANH_TRA
       WHERE MA_KE_HOACH_TT LIKE P_MA_KE_HOACH_TT;
      P_VAL := 'THANH_CONG';
  END;
--------------------UPDATE_KEHOACH_TT--------------------
PROCEDURE UPDATE_KEHOACH_TT(P_VAL          OUT NVARCHAR2,
                           P_UPDATE_TYPE    NVARCHAR2,
                           P_MA_KE_HOACH_TT INT,
                           P_TEN_KE_HOACH_TT NVARCHAR2,
                           P_NGAY_LAP_KE_HOACH       NVARCHAR2,
                           P_FILE_DINH_KEM  NVARCHAR2,
                           P_MA_QUYET_DINH INT,
                           P_TRANG_THAI     INT,
                           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_KE_HOACH_TT) INTO V_TON_TAI_TEN FROM TT_KE_HOACH_THANH_TRA
      WHERE UPPER(TEN_KE_HOACH_TT) = UPPER(P_TEN_KE_HOACH_TT);
      IF V_TON_TAI_TEN > 0 THEN P_VAL :='TON_TAI_TEN';
       ELSE
        INSERT INTO TT_KE_HOACH_THANH_TRA
           (TEN_KE_HOACH_TT,
           NGAY_LAP_KE_HOACH,
           FILE_DINH_KEM,
           NGAY_CAP_NHAT,
           MA_QUYET_DINH,
           TRANG_THAI,
           MA_CO_QUAN
          )
        VALUES
         ( P_TEN_KE_HOACH_TT,
           P_NGAY_LAP_KE_HOACH,
           P_FILE_DINH_KEM,
           SYSDATE,
           P_MA_QUYET_DINH,
           P_TRANG_THAI,
           P_MA_CO_QUAN
          );
        P_VAL := 'THANH_CONG';
     END IF;
     ELSE
      SELECT COUNT(MA_KE_HOACH_TT)INTO V_TON_TAI_TEN FROM TT_KE_HOACH_THANH_TRA
      WHERE UPPER(TEN_KE_HOACH_TT) = UPPER(P_TEN_KE_HOACH_TT)
      AND MA_KE_HOACH_TT <> MA_KE_HOACH_TT;
      IF V_TON_TAI_TEN >0 THEN P_VAL :='TON_TAI_TEN';
      ELSE   
        UPDATE TT_KE_HOACH_THANH_TRA
           SET TEN_KE_HOACH_TT = P_TEN_KE_HOACH_TT,
               NGAY_LAP_KE_HOACH = P_NGAY_LAP_KE_HOACH,
               FILE_DINH_KEM = P_FILE_DINH_KEM,
               NGAY_CAP_NHAT = SYSDATE,
               MA_QUYET_DINH = P_MA_QUYET_DINH,
               TRANG_THAI = P_TRANG_THAI,
               MA_CO_QUAN=P_MA_CO_QUAN                  
         WHERE MA_KE_HOACH_TT = P_MA_KE_HOACH_TT;
        P_VAL := 'THANH_CONG';
    END IF;
    END IF;
  END;
-------------------------------------------------------------  
END TNMT_THANHTRA_NAT;
/

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


--===========//GET DS DOAN THANH TRA//=============--
PROCEDURE GET_DOAN_THANH_TRAS(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_DOAN_THANH_TRA A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;
--===========//GET DOAN THANH TRA//=============--
PROCEDURE GET_DOAN_THANH_TRA(P_CUR IN OUT MYCUR, P_MA_DOAN_TT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_DOAN_THANH_TRA A
    WHERE A.MA_DOAN_TT=P_MA_DOAN_TT;
  END;
--===========//UPDATE DOAN THANH TRA//=============--
PROCEDURE UPDATE_DOAN_THANH_TRA(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE NVARCHAR2, P_MA_DOAN_TT NVARCHAR2,P_TEN_DOAN_TT NVARCHAR2,
                                P_MUC_DICH NVARCHAR2, P_NGAY_THANH_LAP NVARCHAR2,P_NGAY_HET_HAN NVARCHAR2,P_NGAY_GIA_HAN NVARCHAR2,
                                P_MA_CO_QUAN NVARCHAR2,P_TRANG_THAI INT) AS
  V_KT INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(MA_DOAN_TT) INTO V_KT FROM TT_DOAN_THANH_TRA WHERE UPPER(LIB_NOTABLE.LOC_DAU(TEN_DOAN_TT))=UPPER(LIB_NOTABLE.LOC_DAU(P_TEN_DOAN_TT));
      IF V_KT!=0 THEN
        P_VAL:='TON_TAI_TEN';
      ELSE
        INSERT INTO TT_DOAN_THANH_TRA(TEN_DOAN_TT,MUC_DICH_THANH_LAP,NGAY_THANH_LAP,
                                      NGAY_HET_HAN,NGAY_GIA_HAN,MA_CO_QUAN,TRANG_THAI)
                               VALUES(P_TEN_DOAN_TT,P_MUC_DICH,P_NGAY_THANH_LAP,
                                      P_NGAY_HET_HAN,P_NGAY_GIA_HAN,P_MA_CO_QUAN,P_TRANG_THAI);
        P_VAL:='THANH_CONG';
       END IF;
    ELSE
       UPDATE TT_DOAN_THANH_TRA SET TEN_DOAN_TT=P_TEN_DOAN_TT,
                                    MUC_DICH_THANH_LAP=P_MUC_DICH,
                                    NGAY_THANH_LAP=P_NGAY_THANH_LAP,
                                    NGAY_HET_HAN=P_NGAY_HET_HAN,
                                    NGAY_GIA_HAN=P_NGAY_GIA_HAN,
                                    MA_CO_QUAN=P_MA_CO_QUAN,
                                    TRANG_THAI=P_TRANG_THAI
               WHERE MA_DOAN_TT=P_MA_DOAN_TT;
       P_VAL:='THANH_CONG';
    END IF;
END;
--===========//DELETE DOAN THANH TRA//=============--
PROCEDURE DELETE_DOAN_THANH_TRA(P_VAL OUT NVARCHAR2,P_NGUOI_XOA INT,P_MA_DOAN_TT NVARCHAR2) AS
  BEGIN
    P_VAL:='LOI';
    IF P_NGUOI_XOA=0 THEN
      DELETE TT_DOAN_THANH_TRA WHERE MA_DOAN_TT=P_MA_DOAN_TT;
      P_VAL:='THANH_CONG';
    END IF;
  END;
--===========//SELECT CAN BO THANH TRA//=============-- 
PROCEDURE GET_DS_CAN_BOS(P_CUR IN OUT MYCUR,P_MA_DOAN_TT NVARCHAR2,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_CAN_BO,TEN_CAN_BO,HINH_ANH,TEN_TO_CHUYEN_MON
                   FROM CB_CAN_BO A, CB_THUOC_TO_CHUYEN_MON B, CB_TO_CHUYEN_MON C
                   WHERE A.MA_CAN_BO=B.MA_CAN_BO
                   AND B.MA_TO_CHUYEN_MON=C.MA_TO_CHUYEN_MON
                   AND A.MA_CO_QUAN=P_MA_CO_QUAN
                   AND A.MA_CAN_BO NOT IN(SELECT MA_CAN_BO FROM TT_DOAN_TT_CO_CAN_BO WHERE MA_DOAN_TT=P_MA_DOAN_TT);
  END;
--===========//SELECT CAN BO DOAN THANH TRA//=============-- 
PROCEDURE GET_CAN_BO_DOAN_TT(P_CUR IN OUT MYCUR,P_MA_DOAN_TT NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_CAN_BO,TEN_CAN_BO,HINH_ANH,TEN_TO_CHUYEN_MON
    FROM CB_CAN_BO A, TT_DOAN_TT_CO_CAN_BO B,CB_THUOC_TO_CHUYEN_MON C, CB_TO_CHUYEN_MON D
    WHERE A.MA_CAN_BO=B.MA_CAN_BO
    AND A.MA_CAN_BO=C.MA_CAN_BO
    AND C.MA_TO_CHUYEN_MON=D.MA_TO_CHUYEN_MON
    AND B.MA_DOAN_TT=P_MA_DOAN_TT;
  END; 
--===========//DELETE CAN BO DOAN THANH TRA//=============--
PROCEDURE DELETE_CAN_BO_DOAN_TT(P_MA_DOAN_TT NVARCHAR2) AS
  BEGIN
    DELETE FROM TT_DOAN_TT_CO_CAN_BO WHERE MA_DOAN_TT=P_MA_DOAN_TT;
  END;
--===========//INSERT CAN BO DOAN THANH TRA//=============--
PROCEDURE INSERT_CAN_BO_DOAN_TT(P_MA_DOAN_TT NVARCHAR2,P_MA_CAN_BO NVARCHAR2,P_MA_CHUC_DANH NVARCHAR2) AS
  BEGIN
    INSERT INTO TT_DOAN_TT_CO_CAN_BO(MA_DOAN_TT,MA_CAN_BO,MA_CHUC_DANH,NGAY_CAP_NHAT)
                        VALUES(P_MA_DOAN_TT,P_MA_CAN_BO,P_MA_CHUC_DANH,SYSDATE);
  END;
--===========//SELECT KE HOACH THANH TRA//=============--
PROCEDURE SELECT_KE_HOACH_THANH_TRA(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_KE_HOACH_TT,A.TEN_KE_HOACH_TT FROM TT_KE_HOACH_THANH_TRA A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;
--===========//SELECT DOAN THANH TRA//=============--
PROCEDURE SELECT_DOAN_THANH_TRA(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_DOAN_TT,A.TEN_DOAN_TT FROM TT_DOAN_THANH_TRA A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;
--===========//SELECT LINH VUC THANH TRA//=============--
PROCEDURE SELECT_LINH_VUC_THANH_TRA(P_CUR IN OUT MYCUR,P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_LINH_VUC_THANH_TRA,A.TEN_LINH_VUC_THANH_TRA FROM TT_LINH_VUC_THANH_TRA A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;
--===========//UPDATE BIEN BAN THANH TRA//=============--
PROCEDURE UPDATE_BIEN_BAN_THANH_TRA(P_VAL OUT NVARCHAR2,P_UPDATE_TYPE NVARCHAR2,P_SO_THANH_TRA NVARCHAR2,P_MA_KE_HOACH_TT INT, P_MA_DOAN_TT INT,P_LOAI_HINH_THANH_TRA NVARCHAR2,
                                    P_NGAY_BD_THANH_TRA NVARCHAR2,P_NGAY_KT_THANH_TRA NVARCHAR2,P_NOI_DUNG_THANH_TRA NVARCHAR2,P_TEN_TO_CHUC_CA_NHAN NVARCHAR2,P_LOAI_HINH_DN INT, P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2,
                                    P_KET_QUA_THANH_TRA NVARCHAR2,P_HINH_THUC_XU_PHAT INT, P_LY_DO_XU_PHAT NVARCHAR2, P_TAI_SAN_TICH_THU NVARCHAR2,P_SO_TIEN_TICH_THU NVARCHAR2,
                                    P_TAI_SAN_TIEU_HUY NVARCHAR2,P_SO_TIEN_TIEU_HUY NVARCHAR2,P_SO_TIEN_XU_PHAT NVARCHAR2,P_MA_CO_QUAN NVARCHAR2,
                                    P_FILE_QUYET_DINH NVARCHAR2, P_NGAY_KY_QUYET_DINH NVARCHAR2, P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2,P_SO_BIEN_BAN_VI_PHAM_HC NVARCHAR2,
                                    P_NGAY_LAP_BIEN_BAN NVARCHAR2, P_FILE_BIEN_BAN_VI_PHAM NVARCHAR2,P_SO_QUYET_DINH_XU_PHAT NVARCHAR2,P_TRANG_THAI_NOP_PHAT NVARCHAR2) AS
  TON_TAI INT;
  --V_MA_DOAN_TT INT;
  --V_MA_KE_HOACH_TT INT;
  --V_MA_QUYET_DINH INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(SO_THANH_TRA) INTO TON_TAI FROM TT_THANH_TRA WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      IF TON_TAI > 0 THEN
        P_VAL:='TON_TAI';
      ELSE
        /*--ISERT VAO DM QUYET DINH
        SELECT SEQ_TT_QUYET_DINH.NEXTVAL INTO V_MA_QUYET_DINH FROM DUAL;
        INSERT INTO TT_QUYET_DINH(TEN_QUYET_DINH,NGAY_CAP_NHAT,MA_CO_QUAN)
                     VALUES(P_SO_THANH_TRA,SYSDATE,P_MA_CO_QUAN);
        --ISERT VAO DM KE HOACH TT
        SELECT SEQ_TT_KE_HOACH_TT.NEXTVAL INTO V_MA_KE_HOACH_TT FROM DUAL;
        INSERT INTO TT_KE_HOACH_THANH_TRA(TEN_KE_HOACH_TT,NGAY_LAP_KE_HOACH,NGAY_CAP_NHAT,
                                          MA_QUYET_DINH,MA_CO_QUAN)
                    VALUES(P_MA_KE_HOACH_TT,SYSDATE,SYSDATE,'41',P_MA_CO_QUAN);
        ---INSERT VAO DM DOAN TT
        SELECT SEQ_TT_DOAN_THANH_TRA.NEXTVAL INTO V_MA_DOAN_TT FROM DUAL;
        INSERT INTO TT_DOAN_THANH_TRA(TEN_DOAN_TT,NGAY_THANH_LAP,MA_CO_QUAN,TRANG_THAI)
                    VALUES(P_MA_DOAN_TT,SYSDATE,P_MA_CO_QUAN,'0');*/
        INSERT INTO TT_THANH_TRA_S(SO_THANH_TRA,MA_KE_HOACH_TT,MA_DOAN_TT,LOAI_HINH_THANH_TRA,NGAY_BD_THANH_TRA,NGAY_KT_THANH_TRA,
                               NOI_DUNG_THANH_TRA,TEN_TO_CHUC_CA_NHAN,LOAI_HINH_DOANH_NGHIEP,DIA_CHI_TO_CHUC_CA_NHAN,KET_QUA_THANH_TRA,
                               HINH_THUC_XU_PHAT,LY_DO_XU_PHAT,TAI_SAN_TICH_THU,SO_TIEN_TICH_THU,TAI_SAN_TIEU_HUY, SO_TIEN_TIEU_HUY,SO_TIEN_XU_PHAT,MA_CO_QUAN,
                               FILE_QUYET_DINH,NGAY_KY_QUYET_DINH,FILE_NOI_DUNG_THANH_TRA,SO_BIEN_BAN_VI_PHAM_HC,NGAY_LAP_BIEN_BAN,FILE_BIEN_BAN_VI_PHAM,
                               SO_QUYET_DINH_XU_PHAT,TRANG_THAI_NOP_PHAT)
                        VALUES(P_SO_THANH_TRA,P_MA_KE_HOACH_TT,P_MA_DOAN_TT,P_LOAI_HINH_THANH_TRA,P_NGAY_BD_THANH_TRA,P_NGAY_KT_THANH_TRA,
                               P_NOI_DUNG_THANH_TRA,P_TEN_TO_CHUC_CA_NHAN,P_LOAI_HINH_DN,P_DIA_CHI_TO_CHUC_CA_NHAN,P_KET_QUA_THANH_TRA,
                               P_HINH_THUC_XU_PHAT,P_LY_DO_XU_PHAT,P_TAI_SAN_TICH_THU,P_SO_TIEN_TICH_THU,P_TAI_SAN_TIEU_HUY,P_SO_TIEN_TIEU_HUY,P_SO_TIEN_XU_PHAT,P_MA_CO_QUAN,
                               P_FILE_QUYET_DINH,P_NGAY_KY_QUYET_DINH,P_FILE_NOI_DUNG_THANH_TRA,P_SO_BIEN_BAN_VI_PHAM_HC,P_NGAY_LAP_BIEN_BAN,
                               P_FILE_BIEN_BAN_VI_PHAM,P_SO_QUYET_DINH_XU_PHAT,P_TRANG_THAI_NOP_PHAT);
      P_VAL:='THANH_CONG';
     END IF;
    ELSE
      UPDATE TT_THANH_TRA_S SET MA_KE_HOACH_TT=P_MA_KE_HOACH_TT,
                              MA_DOAN_TT=P_MA_DOAN_TT,
                              LOAI_HINH_THANH_TRA=P_LOAI_HINH_THANH_TRA,
                              NGAY_BD_THANH_TRA=P_NGAY_BD_THANH_TRA,
                              NGAY_KT_THANH_TRA=P_NGAY_KT_THANH_TRA,
                              NOI_DUNG_THANH_TRA=P_NOI_DUNG_THANH_TRA,
                              TEN_TO_CHUC_CA_NHAN=P_TEN_TO_CHUC_CA_NHAN,
                              LOAI_HINH_DOANH_NGHIEP=P_LOAI_HINH_DN,
                              DIA_CHI_TO_CHUC_CA_NHAN=P_DIA_CHI_TO_CHUC_CA_NHAN,
                              KET_QUA_THANH_TRA=P_KET_QUA_THANH_TRA,
                              HINH_THUC_XU_PHAT=P_HINH_THUC_XU_PHAT,
                              LY_DO_XU_PHAT=P_LY_DO_XU_PHAT,
                              TAI_SAN_TICH_THU=P_TAI_SAN_TICH_THU,
                              SO_TIEN_TICH_THU=P_SO_TIEN_TICH_THU,
                              TAI_SAN_TIEU_HUY=P_TAI_SAN_TIEU_HUY,
                              SO_TIEN_TIEU_HUY=P_SO_TIEN_TIEU_HUY,
                              SO_TIEN_XU_PHAT=P_SO_TIEN_XU_PHAT,
                              MA_CO_QUAN=P_MA_CO_QUAN,
                              FILE_QUYET_DINH=P_FILE_QUYET_DINH,
                              NGAY_KY_QUYET_DINH=P_NGAY_KY_QUYET_DINH,
                              FILE_NOI_DUNG_THANH_TRA=P_FILE_NOI_DUNG_THANH_TRA,
                              SO_BIEN_BAN_VI_PHAM_HC=P_SO_BIEN_BAN_VI_PHAM_HC,
                              NGAY_LAP_BIEN_BAN=P_NGAY_LAP_BIEN_BAN,
                              FILE_BIEN_BAN_VI_PHAM=P_FILE_BIEN_BAN_VI_PHAM,
                              SO_QUYET_DINH_XU_PHAT=P_SO_QUYET_DINH_XU_PHAT,
                              TRANG_THAI_NOP_PHAT=P_TRANG_THAI_NOP_PHAT
      WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      P_VAL:='UP_THANH_CONG';
    END IF;
 END;
--===========//SELECT BIEN BAN THANH TRA//=============--
PROCEDURE SELECT_BIEN_BAN_THANH_TRAS(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT 
    FROM TT_THANH_TRA_S A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
      AND A.MA_DOAN_TT=C.MA_DOAN_TT
    UNION 
    SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA 
    FROM TT_THANH_TRA_S A
    WHERE SO_THANH_TRA NOT IN(SELECT A.SO_THANH_TRA 
                              FROM TT_THANH_TRA A, TT_KE_HOACH_THANH_TRA B,TT_DOAN_THANH_TRA C
                              WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
                                  AND A.MA_DOAN_TT=C.MA_DOAN_TT);
  END;
--===========//SELECT 1 BIEN BAN THANH TRA//=============--
PROCEDURE SELECT_BIEN_BAN_THANH_TRA(P_CUR IN OUT MYCUR,P_SO_THANH_TRA NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.* FROM TT_THANH_TRA_S A
    WHERE A.SO_THANH_TRA=P_SO_THANH_TRA;
  END;
--===========//delete 1 BIEN BAN THANH TRA//=============--
PROCEDURE DELETE_BIEN_BAN_THANH_TRA(P_VAL OUT NVARCHAR2,P_SO_THANH_TRA NVARCHAR2,P_NGUOI_XOA INT) AS
  BEGIN
    P_VAL:='CL';
    IF P_NGUOI_XOA =0 THEN
      DELETE FROM TT_THANH_TRA_S WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      P_VAL:='THANH_CONG';
    END IF;
  END;
--===========//UPDATE BIEN BAN THANH TRA//=============--
/*
PROCEDURE UPDATE_BIEN_BAN_TT_LINH_VUC(P_VAL OUT NVARCHAR2,
                                      P_UPDATE_TYPE NVARCHAR2,
                                      P_SO_THANH_TRA NVARCHAR2,
                                      P_MA_KE_HOACH_TT INT, 
                                      P_MA_DOAN_TT INT,
                                      P_LOAI_HINH_THANH_TRA NVARCHAR2,
                                      P_NGAY_BD_THANH_TRA NVARCHAR2,
                                      P_NGAY_KT_THANH_TRA NVARCHAR2,
                                      P_NOI_DUNG_THANH_TRA NVARCHAR2,
                                      P_LINH_VUC_THANH_TRA NVARCHAR2,
                                      P_TEN_TO_CHUC_CA_NHAN NVARCHAR2,
                                      P_LOAI_HINH_DN INT, 
                                      P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2,
                                      P_KET_QUA_THANH_TRA NVARCHAR2,
                                      P_KIEN_NGHI_XU_LY INT, 
                                      P_NOI_DUNG_KIEN_NGHI_XU_LY NVARCHAR2,
                                      P_NOI_DUNG_KIEN_NGHI_THU_HOI NVARCHAR2, 
                                      P_KIEN_NGHI_THU_HOI NVARCHAR2,
                                      P_SO_TIEN_THU_HOI FLOAT,
                                      P_NOI_DUNG_KIEN_NGHI_KHAC NVARCHAR2, 
                                      P_KIEN_NGHI_KHAC NVARCHAR2,
                                      P_SO_TIEN_KIEN_NGHI_KHAC NVARCHAR2,
                                      P_KET_QUA_XU_LY INT,
                                      P_NOI_DUNG_XU_LY NVARCHAR2,
                                      P_DAT_PHAI_THU FLOAT,
                                      P_SO_TIEN_PHAI_THU FLOAT, 
                                      P_DAT_DA_THU FLOAT,
                                      P_SO_TIEN_DA_THU FLOAT,
                                      P_SO_NGUOI_LIEN_QUAN INT,
                                      P_SO_TIEN_THAM_NHUNG FLOAT, 
                                      P_TAI_SAN_THAM_NHUNG NVARCHAR2,
                                      P_TS_QUY_DOI_THANH_TIEN FLOAT, 
                                      P_DAT_THAM_NHUNG FLOAT,
                                      P_FILE_QUYET_DINH NVARCHAR2, 
                                      P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2,
                                      P_NGAY_KY_QUYET_DINH NVARCHAR2, 
                                      P_MA_CO_QUAN NVARCHAR2) AS
  TON_TAI INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(SO_THANH_TRA) INTO TON_TAI FROM TT_THANH_TRA WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      IF TON_TAI > 0 THEN
        P_VAL:='TON_TAI';
      ELSE 
        INSERT INTO TT_THANH_TRA_LINH_VUC(SO_THANH_TRA,MA_KE_HOACH_TT,MA_DOAN_TT,LOAI_HINH_THANH_TRA,NGAY_BD_THANH_TRA,NGAY_KT_THANH_TRA,
                               NOI_DUNG_THANH_TRA,MA_LINH_VUC_THANH_TRA,TEN_TO_CHUC_CA_NHAN,LOAI_HINH_DOANH_NGHIEP,DIA_CHI_TO_CHUC_CA_NHAN,KET_QUA_THANH_TRA,
                               KIEN_NGHI_XU_LY,NOI_DUNG_KIEN_NGHI_XU_LY,NOI_DUNG_KIEN_NGHI_THU_HOI,KIEN_NGHI_THU_HOI,SO_TIEN_THU_HOI,NOI_DUNG_KIEN_NGHI_KHAC,KIEN_NGHI_KHAC,SO_TIEN_KHAC,
                               KET_QUA_XU_LY, NOI_DUNG_XU_LY, DAT_PHAI_THU, SO_TIEN_PHAI_THU, DAT_DA_THU, SO_TIEN_DA_THU
                               ,SO_NGUOI_LIEN_QUAN,SO_TIEN_THAM_NHUNG, TAI_SAN_THAM_NHUNG, TS_QUY_DOI_THANH_TIEN, DAT_THAM_NHUNG,FILE_QUYET_DINH,FILE_NOI_DUNG_THANH_TRA,NGAY_KY_QUYET_DINH,MA_CO_QUAN)
                        VALUES(P_SO_THANH_TRA,P_MA_KE_HOACH_TT,P_MA_DOAN_TT,P_LOAI_HINH_THANH_TRA,P_NGAY_BD_THANH_TRA,P_NGAY_KT_THANH_TRA,
                               P_NOI_DUNG_THANH_TRA,P_LINH_VUC_THANH_TRA,P_TEN_TO_CHUC_CA_NHAN,P_LOAI_HINH_DN,P_DIA_CHI_TO_CHUC_CA_NHAN,P_KET_QUA_THANH_TRA,
                               P_KIEN_NGHI_XU_LY,P_NOI_DUNG_KIEN_NGHI_XU_LY,P_NOI_DUNG_KIEN_NGHI_THU_HOI,P_KIEN_NGHI_THU_HOI,P_SO_TIEN_THU_HOI,P_NOI_DUNG_KIEN_NGHI_KHAC,P_KIEN_NGHI_KHAC,P_SO_TIEN_KIEN_NGHI_KHAC,
                               P_KET_QUA_XU_LY, P_NOI_DUNG_XU_LY, P_DAT_PHAI_THU, P_SO_TIEN_PHAI_THU,P_DAT_DA_THU,
                               P_SO_TIEN_DA_THU,P_SO_NGUOI_LIEN_QUAN,P_SO_TIEN_THAM_NHUNG,P_TAI_SAN_THAM_NHUNG,P_TS_QUY_DOI_THANH_TIEN,P_DAT_THAM_NHUNG,P_FILE_QUYET_DINH, P_FILE_NOI_DUNG_THANH_TRA, P_NGAY_KY_QUYET_DINH, P_MA_CO_QUAN);
      P_VAL:='THANH_CONG';
     END IF;
    ELSE
      UPDATE TT_THANH_TRA_LINH_VUC SET MA_KE_HOACH_TT=P_MA_KE_HOACH_TT,
                              MA_DOAN_TT=P_MA_DOAN_TT,
                              LOAI_HINH_THANH_TRA=P_LOAI_HINH_THANH_TRA,
                              NGAY_BD_THANH_TRA=P_NGAY_BD_THANH_TRA,
                              NGAY_KT_THANH_TRA=P_NGAY_KT_THANH_TRA,
                              NOI_DUNG_THANH_TRA=P_NOI_DUNG_THANH_TRA,
                              MA_LINH_VUC_THANH_TRA=P_LINH_VUC_THANH_TRA,
                              TEN_TO_CHUC_CA_NHAN=P_TEN_TO_CHUC_CA_NHAN,
                              LOAI_HINH_DOANH_NGHIEP=P_LOAI_HINH_DN,
                              DIA_CHI_TO_CHUC_CA_NHAN=P_DIA_CHI_TO_CHUC_CA_NHAN,
                              KET_QUA_THANH_TRA=P_KET_QUA_THANH_TRA,
                              KIEN_NGHI_XU_LY=P_KIEN_NGHI_XU_LY,
                              NOI_DUNG_KIEN_NGHI_XU_LY=P_NOI_DUNG_KIEN_NGHI_XU_LY,
                              NOI_DUNG_KIEN_NGHI_THU_HOI=P_NOI_DUNG_KIEN_NGHI_THU_HOI,
                              KIEN_NGHI_THU_HOI=P_KIEN_NGHI_THU_HOI,
                              SO_TIEN_THU_HOI=P_SO_TIEN_THU_HOI,
                              NOI_DUNG_KIEN_NGHI_KHAC=P_NOI_DUNG_KIEN_NGHI_KHAC,
                              KIEN_NGHI_KHAC=P_KIEN_NGHI_KHAC,
                              SO_TIEN_KHAC=P_SO_TIEN_KIEN_NGHI_KHAC,
                              KET_QUA_XU_LY=P_KET_QUA_XU_LY,
                              NOI_DUNG_XU_LY=P_NOI_DUNG_XU_LY,
                              DAT_PHAI_THU=P_DAT_PHAI_THU,
                              SO_TIEN_PHAI_THU=P_SO_TIEN_PHAI_THU,
                              DAT_DA_THU=P_DAT_DA_THU,
                              SO_TIEN_DA_THU=P_SO_TIEN_DA_THU,
                              SO_NGUOI_LIEN_QUAN=P_SO_NGUOI_LIEN_QUAN,
                              SO_TIEN_THAM_NHUNG=P_SO_TIEN_THAM_NHUNG,
                              TAI_SAN_THAM_NHUNG=P_TAI_SAN_THAM_NHUNG,
                              TS_QUY_DOI_THANH_TIEN=P_TS_QUY_DOI_THANH_TIEN,
                              DAT_THAM_NHUNG=P_DAT_THAM_NHUNG,
                              FILE_QUYET_DINH=P_FILE_QUYET_DINH,
                              FILE_NOI_DUNG_THANH_TRA=P_FILE_NOI_DUNG_THANH_TRA,
                              NGAY_KY_QUYET_DINH=P_NGAY_KY_QUYET_DINH,
                              MA_CO_QUAN=P_MA_CO_QUAN
      WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      P_VAL:='UP_THANH_CONG';
    END IF;
 END;
 */

PROCEDURE UPDATE_BIEN_BAN_TT_LINH_VUC(P_VAL OUT NVARCHAR2, P_UPDATE_TYPE NVARCHAR2, P_SO_THANH_TRA NVARCHAR2, P_LOAI_HINH_THANH_TRA NVARCHAR2, P_MA_KE_HOACH_TT INTEGER, P_MA_DOAN_TT INTEGER, P_NGAY_BD_THANH_TRA NVARCHAR2, P_NGAY_KT_THANH_TRA NVARCHAR2, P_NOI_DUNG_THANH_TRA NVARCHAR2, P_TEN_TO_CHUC_CA_NHAN NVARCHAR2, P_LOAI_HINH_DOANH_NGHIEP INTEGER, P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2, P_MA_LINH_VUC_THANH_TRA NVARCHAR2, P_KET_QUA_THANH_TRA NVARCHAR2, P_KIEN_NGHI_XU_LY INTEGER, P_NOI_DUNG_XU_LY NVARCHAR2, P_KIEN_NGHI_THU_HOI NVARCHAR2, P_SO_TIEN_THU_HOI FLOAT, P_KIEN_NGHI_KHAC NVARCHAR2, P_SO_TIEN_KHAC NVARCHAR2, P_MA_CO_QUAN NVARCHAR2, P_NOI_DUNG_KIEN_NGHI_XU_LY NVARCHAR2, P_KET_QUA_XU_LY INTEGER, P_DAT_PHAI_THU FLOAT, P_SO_TIEN_PHAI_THU FLOAT, P_DAT_DA_THU FLOAT, P_SO_TIEN_DA_THU FLOAT, P_NOI_DUNG_KIEN_NGHI_THU_HOI NVARCHAR2, P_NOI_DUNG_KIEN_NGHI_KHAC NVARCHAR2, P_SO_TIEN_THAM_NHUNG FLOAT, P_TAI_SAN_THAM_NHUNG NVARCHAR2, P_TS_QUY_DOI_THANH_TIEN FLOAT, P_DAT_THAM_NHUNG FLOAT, P_SO_NGUOI_LIEN_QUAN INTEGER, P_FILE_QUYET_DINH NVARCHAR2, P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2, P_NGAY_KY_QUYET_DINH NVARCHAR2) AS
TON_TAI INTEGER;
BEGIN
  P_VAL:='LOI';
  IF P_UPDATE_TYPE='THEM_MOI' THEN
    SELECT COUNT(SO_THANH_TRA)
    INTO TON_TAI
    FROM TT_THANH_TRA_LINH_VUC_S
    WHERE SO_THANH_TRA=P_SO_THANH_TRA;
    IF TON_TAI > 0 THEN
      P_VAL:='TON_TAI';
    ELSE
      INSERT INTO TT_THANH_TRA_LINH_VUC_S(SO_THANH_TRA, LOAI_HINH_THANH_TRA, MA_KE_HOACH_TT, MA_DOAN_TT, NGAY_BD_THANH_TRA, NGAY_KT_THANH_TRA, NOI_DUNG_THANH_TRA, TEN_TO_CHUC_CA_NHAN, LOAI_HINH_DOANH_NGHIEP, DIA_CHI_TO_CHUC_CA_NHAN, MA_LINH_VUC_THANH_TRA, KET_QUA_THANH_TRA, KIEN_NGHI_XU_LY, NOI_DUNG_XU_LY, KIEN_NGHI_THU_HOI, SO_TIEN_THU_HOI, KIEN_NGHI_KHAC, SO_TIEN_KHAC, MA_CO_QUAN, NOI_DUNG_KIEN_NGHI_XU_LY, KET_QUA_XU_LY, DAT_PHAI_THU, SO_TIEN_PHAI_THU, DAT_DA_THU, SO_TIEN_DA_THU, NOI_DUNG_KIEN_NGHI_THU_HOI, NOI_DUNG_KIEN_NGHI_KHAC, SO_TIEN_THAM_NHUNG, TAI_SAN_THAM_NHUNG, TS_QUY_DOI_THANH_TIEN, DAT_THAM_NHUNG, SO_NGUOI_LIEN_QUAN, FILE_QUYET_DINH, FILE_NOI_DUNG_THANH_TRA, NGAY_KY_QUYET_DINH)
      VALUES(P_SO_THANH_TRA, P_LOAI_HINH_THANH_TRA, P_MA_KE_HOACH_TT, P_MA_DOAN_TT, P_NGAY_BD_THANH_TRA, P_NGAY_KT_THANH_TRA, P_NOI_DUNG_THANH_TRA, P_TEN_TO_CHUC_CA_NHAN, P_LOAI_HINH_DOANH_NGHIEP, P_DIA_CHI_TO_CHUC_CA_NHAN, P_MA_LINH_VUC_THANH_TRA, P_KET_QUA_THANH_TRA, P_KIEN_NGHI_XU_LY, P_NOI_DUNG_XU_LY, P_KIEN_NGHI_THU_HOI, P_SO_TIEN_THU_HOI, P_KIEN_NGHI_KHAC, P_SO_TIEN_KHAC, P_MA_CO_QUAN, P_NOI_DUNG_KIEN_NGHI_XU_LY, P_KET_QUA_XU_LY, P_DAT_PHAI_THU, P_SO_TIEN_PHAI_THU, P_DAT_DA_THU, P_SO_TIEN_DA_THU, P_NOI_DUNG_KIEN_NGHI_THU_HOI, P_NOI_DUNG_KIEN_NGHI_KHAC, P_SO_TIEN_THAM_NHUNG, P_TAI_SAN_THAM_NHUNG, P_TS_QUY_DOI_THANH_TIEN, P_DAT_THAM_NHUNG, P_SO_NGUOI_LIEN_QUAN, P_FILE_QUYET_DINH, P_FILE_NOI_DUNG_THANH_TRA, P_NGAY_KY_QUYET_DINH);
      P_VAL:='THANH_CONG';
    END IF;
  ELSE
    UPDATE TT_THANH_TRA_LINH_VUC_S
    SET LOAI_HINH_THANH_TRA = P_LOAI_HINH_THANH_TRA, MA_KE_HOACH_TT = P_MA_KE_HOACH_TT, MA_DOAN_TT = P_MA_DOAN_TT, NGAY_BD_THANH_TRA = P_NGAY_BD_THANH_TRA, NGAY_KT_THANH_TRA = P_NGAY_KT_THANH_TRA, NOI_DUNG_THANH_TRA = P_NOI_DUNG_THANH_TRA, TEN_TO_CHUC_CA_NHAN = P_TEN_TO_CHUC_CA_NHAN, LOAI_HINH_DOANH_NGHIEP = P_LOAI_HINH_DOANH_NGHIEP, DIA_CHI_TO_CHUC_CA_NHAN = P_DIA_CHI_TO_CHUC_CA_NHAN, MA_LINH_VUC_THANH_TRA = P_MA_LINH_VUC_THANH_TRA, KET_QUA_THANH_TRA = P_KET_QUA_THANH_TRA, KIEN_NGHI_XU_LY = P_KIEN_NGHI_XU_LY, NOI_DUNG_XU_LY = P_NOI_DUNG_XU_LY, KIEN_NGHI_THU_HOI = P_KIEN_NGHI_THU_HOI, SO_TIEN_THU_HOI = P_SO_TIEN_THU_HOI, KIEN_NGHI_KHAC = P_KIEN_NGHI_KHAC, SO_TIEN_KHAC = P_SO_TIEN_KHAC, MA_CO_QUAN = P_MA_CO_QUAN, NOI_DUNG_KIEN_NGHI_XU_LY = P_NOI_DUNG_KIEN_NGHI_XU_LY, KET_QUA_XU_LY = P_KET_QUA_XU_LY, DAT_PHAI_THU = P_DAT_PHAI_THU,
    SO_TIEN_PHAI_THU = P_SO_TIEN_PHAI_THU, DAT_DA_THU = P_DAT_DA_THU, SO_TIEN_DA_THU = P_SO_TIEN_DA_THU, NOI_DUNG_KIEN_NGHI_THU_HOI = P_NOI_DUNG_KIEN_NGHI_THU_HOI, NOI_DUNG_KIEN_NGHI_KHAC = P_NOI_DUNG_KIEN_NGHI_KHAC, SO_TIEN_THAM_NHUNG = P_SO_TIEN_THAM_NHUNG, TAI_SAN_THAM_NHUNG = P_TAI_SAN_THAM_NHUNG, TS_QUY_DOI_THANH_TIEN = P_TS_QUY_DOI_THANH_TIEN, DAT_THAM_NHUNG = P_DAT_THAM_NHUNG, SO_NGUOI_LIEN_QUAN = P_SO_NGUOI_LIEN_QUAN, FILE_QUYET_DINH = P_FILE_QUYET_DINH, FILE_NOI_DUNG_THANH_TRA = P_FILE_NOI_DUNG_THANH_TRA, NGAY_KY_QUYET_DINH = P_NGAY_KY_QUYET_DINH
    WHERE SO_THANH_TRA = P_SO_THANH_TRA;
    P_VAL:='UP_THANH_CONG';
    END IF;
END;
 

/*PROCEDURE UPDATE_BIEN_BAN_TT_LINH_VUC(P_VAL OUT NVARCHAR2,
                                      P_UPDATE_TYPE NVARCHAR2,
                                      P_SO_THANH_TRA NVARCHAR2,--1
                                      P_LOAI_HINH_THANH_TRA NVARCHAR2,--2
                                      P_MA_KE_HOACH_TT INT, --3
                                      P_MA_DOAN_TT INT,--4                                      
                                      P_NGAY_BD_THANH_TRA NVARCHAR2,--5
                                      P_NGAY_KT_THANH_TRA NVARCHAR2,--6
                                      P_NOI_DUNG_THANH_TRA NVARCHAR2,--7                                      
                                      P_TEN_TO_CHUC_CA_NHAN NVARCHAR2,--8
                                      P_LOAI_HINH_DN INT, --9
                                      P_DIA_CHI_TO_CHUC_CA_NHAN NVARCHAR2,--10
                                      P_LINH_VUC_THANH_TRA NVARCHAR2,--11
                                      P_KET_QUA_THANH_TRA NVARCHAR2,--12
                                      P_KIEN_NGHI_XU_LY INT, --13
                                      P_NOI_DUNG_XU_LY NVARCHAR2,--14                                                                            
                                      P_KIEN_NGHI_THU_HOI NVARCHAR2,--15
                                      P_SO_TIEN_THU_HOI FLOAT,--16
                                      P_KIEN_NGHI_KHAC NVARCHAR2,--17
                                      P_SO_TIEN_KIEN_NGHI_KHAC NVARCHAR2,--18
                                      P_MA_CO_QUAN NVARCHAR2,--19
                                      P_NOI_DUNG_KIEN_NGHI_XU_LY NVARCHAR2,--20
                                      P_KET_QUA_XU_LY INT,--21
                                      P_DAT_PHAI_THU FLOAT,--22
                                      P_SO_TIEN_PHAI_THU FLOAT,--23
                                      P_DAT_DA_THU FLOAT,--24
                                      P_SO_TIEN_DA_THU FLOAT,--25
                                      P_NOI_DUNG_KIEN_NGHI_THU_HOI NVARCHAR2,--26                                                                            
                                      P_NOI_DUNG_KIEN_NGHI_KHAC NVARCHAR2,--27 
                                      P_SO_TIEN_THAM_NHUNG FLOAT,--28
                                      P_TAI_SAN_THAM_NHUNG NVARCHAR2,--29
                                      P_TS_QUY_DOI_THANH_TIEN FLOAT,--30
                                      P_DAT_THAM_NHUNG FLOAT,--31
                                      P_SO_NGUOI_LIEN_QUAN INT,--32
                                      P_FILE_QUYET_DINH NVARCHAR2,--33 
                                      P_FILE_NOI_DUNG_THANH_TRA NVARCHAR2,--34
                                      P_NGAY_KY_QUYET_DINH NVARCHAR2--35                                                                                                                                                                                                                                                                                                                                                                                                
                                      ) AS
  TON_TAI INT;
  BEGIN
    P_VAL:='LOI';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(SO_THANH_TRA) INTO TON_TAI FROM TT_THANH_TRA_LINH_VUC WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      IF TON_TAI > 0 THEN
        P_VAL:='TON_TAI';
      ELSE 
        INSERT INTO TT_THANH_TRA_LINH_VUC(SO_THANH_TRA,--1
                                          LOAI_HINH_THANH_TRA,--2                                                                                    
                                          MA_KE_HOACH_TT,--3
                                          MA_DOAN_TT,--4                                          
                                          NGAY_BD_THANH_TRA,--5
                                          NGAY_KT_THANH_TRA,--6
                                          NOI_DUNG_THANH_TRA,--7
                                          TEN_TO_CHUC_CA_NHAN,--8
                                          LOAI_HINH_DOANH_NGHIEP,--9
                                          DIA_CHI_TO_CHUC_CA_NHAN,--10
                                          MA_LINH_VUC_THANH_TRA,--11                                          
                                          KET_QUA_THANH_TRA,--12
                                          KIEN_NGHI_XU_LY,--13
                                          NOI_DUNG_XU_LY,--14
                                          KIEN_NGHI_THU_HOI,--15
                                          SO_TIEN_THU_HOI,--16
                                          KIEN_NGHI_KHAC,--17
                                          SO_TIEN_KHAC,--18
                                          MA_CO_QUAN,--19
                                          NOI_DUNG_KIEN_NGHI_XU_LY,--20
                                          KET_QUA_XU_LY,--21
                                          DAT_PHAI_THU,--22 
                                          SO_TIEN_PHAI_THU,--23 
                                          DAT_DA_THU, --24
                                          SO_TIEN_DA_THU,--25
                                          NOI_DUNG_KIEN_NGHI_THU_HOI,--26                                          
                                          NOI_DUNG_KIEN_NGHI_KHAC,--27
                                          SO_TIEN_THAM_NHUNG,--28 
                                          TAI_SAN_THAM_NHUNG, --29
                                          TS_QUY_DOI_THANH_TIEN, --30
                                          DAT_THAM_NHUNG,--31                                                                                                                                                                         
                                          SO_NGUOI_LIEN_QUAN,--32                                          
                                          FILE_QUYET_DINH,--33
                                          FILE_NOI_DUNG_THANH_TRA,--34
                                          NGAY_KY_QUYET_DINH--35
                                          )
                        VALUES(P_SO_THANH_TRA,--1
                                      P_LOAI_HINH_THANH_TRA,--2
                                      P_MA_KE_HOACH_TT, --3
                                      P_MA_DOAN_TT,--4                                      
                                      P_NGAY_BD_THANH_TRA,--5
                                      P_NGAY_KT_THANH_TRA,--6
                                      P_NOI_DUNG_THANH_TRA,--7                                      
                                      P_TEN_TO_CHUC_CA_NHAN,--8
                                      P_LOAI_HINH_DN, --9
                                      P_DIA_CHI_TO_CHUC_CA_NHAN,--10
                                      P_LINH_VUC_THANH_TRA,--11
                                      P_KET_QUA_THANH_TRA,--12
                                      P_KIEN_NGHI_XU_LY, --13
                                      P_NOI_DUNG_XU_LY,--14                                                                            
                                      P_KIEN_NGHI_THU_HOI,--15
                                      P_SO_TIEN_THU_HOI,--16
                                      P_KIEN_NGHI_KHAC,--17
                                      P_SO_TIEN_KIEN_NGHI_KHAC,--18
                                      P_MA_CO_QUAN,--19
                                      P_NOI_DUNG_KIEN_NGHI_XU_LY,--20
                                      P_KET_QUA_XU_LY,--21
                                      P_DAT_PHAI_THU,--22
                                      P_SO_TIEN_PHAI_THU,--23
                                      P_DAT_DA_THU,--24
                                      P_SO_TIEN_DA_THU,--25
                                      P_NOI_DUNG_KIEN_NGHI_THU_HOI,--26                                                                            
                                      P_NOI_DUNG_KIEN_NGHI_KHAC,--27 
                                      P_SO_TIEN_THAM_NHUNG,--28
                                      P_TAI_SAN_THAM_NHUNG,--29
                                      P_TS_QUY_DOI_THANH_TIEN,--30
                                      P_DAT_THAM_NHUNG,--31
                                      P_SO_NGUOI_LIEN_QUAN,--32
                                      P_FILE_QUYET_DINH,--33 
                                      P_FILE_NOI_DUNG_THANH_TRA,--34
                                      P_NGAY_KY_QUYET_DINH--35
                                      );
      P_VAL:='THANH_CONG';
     END IF;
    ELSE
      UPDATE TT_THANH_TRA_LINH_VUC SET LOAI_HINH_THANH_TRA = P_LOAI_HINH_THANH_TRA,--2                                                                                  
                                       MA_KE_HOACH_TT = P_MA_KE_HOACH_TT, --3
                                       MA_DOAN_TT = P_MA_DOAN_TT,--4                                       
                                       NGAY_BD_THANH_TRA = P_NGAY_BD_THANH_TRA,--5
                                       NGAY_KT_THANH_TRA = P_NGAY_KT_THANH_TRA,--6
                                       NOI_DUNG_THANH_TRA = P_NOI_DUNG_THANH_TRA,--7
                                       TEN_TO_CHUC_CA_NHAN = P_TEN_TO_CHUC_CA_NHAN,--8
                                       LOAI_HINH_DOANH_NGHIEP = P_LOAI_HINH_DN, --9
                                       DIA_CHI_TO_CHUC_CA_NHAN = P_DIA_CHI_TO_CHUC_CA_NHAN,--10
                                       MA_LINH_VUC_THANH_TRA = P_LINH_VUC_THANH_TRA,--11                                         
                                       KET_QUA_THANH_TRA = P_KET_QUA_THANH_TRA,--12
                                       KIEN_NGHI_XU_LY = P_KIEN_NGHI_XU_LY, --13
                                       NOI_DUNG_XU_LY = P_NOI_DUNG_XU_LY,--14
                                       KIEN_NGHI_THU_HOI = P_KIEN_NGHI_THU_HOI,--15
                                       SO_TIEN_THU_HOI = P_SO_TIEN_THU_HOI,--16
                                       KIEN_NGHI_KHAC = P_KIEN_NGHI_KHAC,--17
                                       SO_TIEN_KHAC = P_SO_TIEN_KIEN_NGHI_KHAC,--18
                                       MA_CO_QUAN = P_MA_CO_QUAN,--19
                                       NOI_DUNG_KIEN_NGHI_XU_LY = P_NOI_DUNG_KIEN_NGHI_XU_LY,--20
                                       KET_QUA_XU_LY = P_KET_QUA_XU_LY,--21
                                       DAT_PHAI_THU = P_DAT_PHAI_THU,--22
                                       SO_TIEN_PHAI_THU = P_SO_TIEN_PHAI_THU,--23
                                       DAT_DA_THU = P_DAT_DA_THU,--24
                                       SO_TIEN_DA_THU = P_SO_TIEN_DA_THU,--25
                                       NOI_DUNG_KIEN_NGHI_THU_HOI = P_NOI_DUNG_KIEN_NGHI_THU_HOI,--26                                         
                                       NOI_DUNG_KIEN_NGHI_KHAC = P_NOI_DUNG_KIEN_NGHI_KHAC,--27
                                       SO_TIEN_THAM_NHUNG = P_SO_TIEN_THAM_NHUNG,--28
                                       TAI_SAN_THAM_NHUNG = P_TAI_SAN_THAM_NHUNG,--29
                                       TS_QUY_DOI_THANH_TIEN = P_TS_QUY_DOI_THANH_TIEN,--30
                                       DAT_THAM_NHUNG = P_DAT_THAM_NHUNG,--31                                                                                                                                                                        
                                       SO_NGUOI_LIEN_QUAN = P_SO_NGUOI_LIEN_QUAN,--32                                         
                                       FILE_QUYET_DINH = P_FILE_QUYET_DINH,--33
                                       FILE_NOI_DUNG_THANH_TRA = P_FILE_NOI_DUNG_THANH_TRA,--34
                                       NGAY_KY_QUYET_DINH = P_NGAY_KY_QUYET_DINH--35
      WHERE SO_THANH_TRA = P_SO_THANH_TRA;
      P_VAL:='UP_THANH_CONG';
    END IF;
 END;*/
 --===========// SELECT 1 BIEN BAN THANH TRA LINH VUC //=============--
PROCEDURE SELECT_BIEN_BAN_TT_LINH_VUC(P_CUR IN OUT MYCUR,P_SO_THANH_TRA NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.* FROM TT_THANH_TRA_LINH_VUC_S A
    WHERE A.SO_THANH_TRA=P_SO_THANH_TRA;
  END;
--===========//SELECT BIEN BAN THANH TRA LINH VUC//=============--
PROCEDURE SELECT_BIEN_BAN_TT_LINH_VUCS(P_CUR IN OUT MYCUR) AS
  BEGIN
    OPEN P_CUR FOR 
    SELECT A.*,B.TEN_KE_HOACH_TT,C.TEN_DOAN_TT 
    FROM TT_THANH_TRA_LINH_VUC_S A
        ,TT_KE_HOACH_THANH_TRA B
        ,TT_DOAN_THANH_TRA C
    WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
    AND A.MA_DOAN_TT=C.MA_DOAN_TT
    UNION 
    SELECT A.*,NULL AS TEN_KE_HOACH_TT,NULL AS TEN_DOAN_THANH_TRA 
    FROM TT_THANH_TRA_LINH_VUC_S A
    WHERE SO_THANH_TRA NOT IN(SELECT A.SO_THANH_TRA 
                              FROM TT_THANH_TRA_LINH_VUC A
                                  ,TT_KE_HOACH_THANH_TRA B
                                  ,TT_DOAN_THANH_TRA C
                              WHERE A.MA_KE_HOACH_TT=B.MA_KE_HOACH_TT
                              AND A.MA_DOAN_TT=C.MA_DOAN_TT
                              );
  END;
--===========//delete 1 BIEN BAN THANH TRA LINH VUC//=============--
PROCEDURE DELETE_BIEN_BAN_TT_LINH_VUC(P_VAL OUT NVARCHAR2,P_SO_THANH_TRA NVARCHAR2,P_NGUOI_XOA INT) AS
  BEGIN
    P_VAL:='CL';
    IF P_NGUOI_XOA =0 THEN
      DELETE FROM TT_THANH_TRA_LINH_VUC_S WHERE SO_THANH_TRA=P_SO_THANH_TRA;
      P_VAL:='THANH_CONG';
    END IF;
  END;
--===========// select dang thanh tra 
PROCEDURE SELECT_DANG_VI_PHAM(P_CUR IN OUT MYCUR, P_MA_LINH_VUC NVARCHAR2,P_SO_THANH_TRA NVARCHAR2, P_MA_CO_QUAN NVARCHAR2) AS
 V_TS INT;
  BEGIN
    BEGIN
    SELECT COUNT(MA_DANG_VI_PHAM) INTO V_TS FROM TT_DANG_VI_PHAM A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
    AND A.MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC
    GROUP BY MA_CO_QUAN;
    EXCEPTION 
      WHEN NO_DATA_FOUND THEN
      V_TS:=0;
    END;
    OPEN P_CUR FOR SELECT MA_DANG_VI_PHAM,TEN_DANG_VI_PHAM,SUM(VI_PHAM) AS VI_PHAM,STT_HIEN_THI,TS FROM (SELECT A.MA_DANG_VI_PHAM,A.TEN_DANG_VI_PHAM,NULL AS VI_PHAM,A.STT_HIEN_THI,V_TS AS TS FROM TT_DANG_VI_PHAM A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN
    AND A.MA_LINH_VUC_THANH_TRA=P_MA_LINH_VUC
    UNION
    SELECT B.MA_DANG_VI_PHAM,C.TEN_DANG_VI_PHAM, B.VI_PHAM,C.STT_HIEN_THI, V_TS AS TS FROM TT_THANH_TRA_CO_DANG_VI_PHAM B,TT_DANG_VI_PHAM C
    WHERE B.MA_DANG_VI_PHAM=C.MA_DANG_VI_PHAM
    AND B.SO_THANH_TRA=P_SO_THANH_TRA) KQ
    GROUP BY MA_DANG_VI_PHAM,TEN_DANG_VI_PHAM,STT_HIEN_THI,TS
    ORDER BY KQ.STT_HIEN_THI;
 END;
PROCEDURE UPDATE_TT_DANG_VI_PHAM(P_MA_DANG_VI_PHAM INT,P_SO_THANH_TRA NVARCHAR2,P_VI_PHAM NVARCHAR2) AS
  BEGIN
    INSERT INTO TT_THANH_TRA_CO_DANG_VI_PHAM(MA_DANG_VI_PHAM,SO_THANH_TRA,VI_PHAM)
                                      VALUES(P_MA_DANG_VI_PHAM,P_SO_THANH_TRA,P_VI_PHAM);
  END;
PROCEDURE DELETE_TT_DANG_VI_PHAM(P_SO_THANH_TRA NVARCHAR2) AS
  BEGIN
    DELETE FROM TT_THANH_TRA_CO_DANG_VI_PHAM
    WHERE SO_THANH_TRA=P_SO_THANH_TRA;
  END;
-----------------------------------------------
PROCEDURE SELECT_LY_DO_TTL(P_CUR IN OUT MYCUR, P_MA_CO_QUAN NVARCHAR2) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.MA_LY_DO_TTL,A.LY_DO_TTL FROM TT_LY_DO_THANH_TRA_LAI A
    WHERE A.MA_CO_QUAN=P_MA_CO_QUAN;
  END;
------------------------------------------------
PROCEDURE UPDATE_THANH_TRA_LAI(P_VAL OUT NVARCHAR2,P_UPDATE_TYPE NVARCHAR2, P_SO_THANH_TRA INT, P_NGAY_TTL NVARCHAR2,P_LAN_TTL NVARCHAR2,
                               P_LY_DO_TTL NVARCHAR2,P_KET_QUA_TTL NVARCHAR2,P_KIEN_NGHI_XU_LY NVARCHAR2, P_NOI_DUNG_KIEN_NGHI_XU_LY NVARCHAR2,
                               P_NOI_DUNG_KIEN_NGHI_THU_HOI NVARCHAR2, P_KIEN_NGHI_THU_HOI NVARCHAR2, P_SO_TIEN_THU_HOI NVARCHAR2, P_NOI_DUNG_KIEN_NGHI_KHAC NVARCHAR2,
                               P_KIEN_NGHI_KHAC NVARCHAR2, P_SO_TIEN_KHAC NVARCHAR2, P_KET_QUA_XU_LY NVARCHAR2, P_NOI_DUNG_XU_LY NVARCHAR2,
                               P_NOI_DUNG_VI_PHAM_KINH_TE NVARCHAR2,P_DAT_VI_PHAM NVARCHAR2,P_SO_TIEN_VI_PHAM NVARCHAR2,P_DAT_DA_THU NVARCHAR2,P_SO_TIEN_DA_THU NVARCHAR2
                               ,P_SO_TIEN_THAM_NHUNG FLOAT, P_TAI_SAN_THAM_NHUNG NVARCHAR2, P_TS_QUY_DOI_THANH_TIEN FLOAT,P_DAT_THAM_NHUNG FLOAT) AS
  V_KT INT;
  BEGIN
    P_VAL:='CL';
    IF P_UPDATE_TYPE='THEM_MOI' THEN
      SELECT COUNT(SO_THANH_TRA) INTO V_KT FROM TT_THANH_TRA_LAI WHERE SO_THANH_TRA=P_SO_THANH_TRA AND LAN_THANH_TRA_LAI=P_LAN_TTL;
      IF V_KT=0 THEN
         INSERT INTO TT_THANH_TRA_LAI(SO_THANH_TRA,NGAY_THANH_TRA_LAI,LAN_THANH_TRA_LAI,LY_DO_THANH_TRA_LAI,KET_QUA_THANH_TRA_LAI,KIEN_NGHI_XU_LY,
                                   NOI_DUNG_KIEN_NGHI_XU_LY,NOI_DUNG_KIEN_NGHI_THU_HOI,KIEN_NGHI_THU_HOI,SO_TIEN_THU_HOI,NOI_DUNG_KIEN_NGHI_KHAC,
                                   KIEN_NGHI_KHAC,SO_TIEN_KHAC,NOI_DUNG_VI_PHAM_KINH_TE,DAT_VI_PHAM,SO_TIEN_VI_PHAM,KET_QUA_XU_LY,NOI_DUNG_XU_LY,DAT_DA_THU,SO_TIEN_DA_THU
                                   ,SO_TIEN_THAM_NHUNG,TAI_SAN_THAM_NHUNG,TS_QUY_DOI_THANH_TIEN,DAT_THAM_NHUNG)
                             VALUES(P_SO_THANH_TRA,P_NGAY_TTL,P_LAN_TTL,P_LY_DO_TTL,P_KET_QUA_TTL,P_KIEN_NGHI_XU_LY,P_NOI_DUNG_KIEN_NGHI_XU_LY,
                                   P_NOI_DUNG_KIEN_NGHI_THU_HOI,P_KIEN_NGHI_THU_HOI,P_SO_TIEN_THU_HOI,P_NOI_DUNG_KIEN_NGHI_KHAC,P_KIEN_NGHI_KHAC,
                                   P_SO_TIEN_KHAC,P_NOI_DUNG_VI_PHAM_KINH_TE,P_DAT_VI_PHAM,P_SO_TIEN_VI_PHAM,P_KET_QUA_XU_LY,P_NOI_DUNG_XU_LY,P_DAT_DA_THU,P_SO_TIEN_DA_THU
                                   ,P_SO_TIEN_THAM_NHUNG, P_TAI_SAN_THAM_NHUNG, P_TS_QUY_DOI_THANH_TIEN, P_DAT_THAM_NHUNG);
         P_VAL:='THANH_CONG';
      ELSE
         P_VAL:='TON_TAI';
      END IF;
    ELSE
      UPDATE TT_THANH_TRA_LAI SET NGAY_THANH_TRA_LAI=P_NGAY_TTL,
                              LY_DO_THANH_TRA_LAI=P_LY_DO_TTL,
                              KET_QUA_THANH_TRA_LAI=P_KET_QUA_TTL,
                              KIEN_NGHI_XU_LY=P_KIEN_NGHI_XU_LY,
                              NOI_DUNG_KIEN_NGHI_XU_LY=P_NOI_DUNG_KIEN_NGHI_XU_LY,
                              NOI_DUNG_KIEN_NGHI_THU_HOI=P_NOI_DUNG_KIEN_NGHI_THU_HOI,
                              KIEN_NGHI_THU_HOI=P_KIEN_NGHI_THU_HOI,
                              SO_TIEN_THU_HOI=P_SO_TIEN_THU_HOI,
                              NOI_DUNG_KIEN_NGHI_KHAC=P_NOI_DUNG_KIEN_NGHI_KHAC,
                              KIEN_NGHI_KHAC=P_KIEN_NGHI_KHAC,
                              SO_TIEN_KHAC=P_SO_TIEN_KHAC,
                              NOI_DUNG_VI_PHAM_KINH_TE=P_NOI_DUNG_VI_PHAM_KINH_TE,
                              DAT_VI_PHAM=P_DAT_VI_PHAM,
                              SO_TIEN_VI_PHAM=P_SO_TIEN_VI_PHAM,
                              KET_QUA_XU_LY=P_KET_QUA_XU_LY,
                              NOI_DUNG_XU_LY=P_NOI_DUNG_XU_LY,
                              DAT_DA_THU=P_DAT_DA_THU,
                              SO_TIEN_DA_THU=P_SO_TIEN_DA_THU
                              ,SO_TIEN_THAM_NHUNG=P_SO_TIEN_THAM_NHUNG,
                              TAI_SAN_THAM_NHUNG=P_TAI_SAN_THAM_NHUNG,
                              TS_QUY_DOI_THANH_TIEN=P_TS_QUY_DOI_THANH_TIEN,
                              DAT_THAM_NHUNG=P_DAT_THAM_NHUNG
        WHERE SO_THANH_TRA=P_SO_THANH_TRA AND LAN_THANH_TRA_LAI=P_LAN_TTL;
       P_VAL:='THANH_CONG';
    END IF;
  END;
PROCEDURE SELECT_THANH_TRA_LAI(P_CUR OUT MYCUR,P_SO_THANH_TRA INT,P_LAN_TTL INT) AS
  BEGIN
    OPEN P_CUR FOR SELECT A.* FROM TT_THANH_TRA_LAI A
    WHERE A.SO_THANH_TRA=P_SO_THANH_TRA
    AND (A.LAN_THANH_TRA_LAI=P_LAN_TTL OR P_LAN_TTL IS NULL)
    AND LAN_THANH_TRA_LAI =( SELECT MAX(LAN_THANH_TRA_LAI) FROM TT_THANH_TRA_LAI A
                           WHERE A.SO_THANH_TRA=P_SO_THANH_TRA
                           AND (A.LAN_THANH_TRA_LAI=P_LAN_TTL OR P_LAN_TTL IS NULL));
  END;
END TNMT_THANHTRA_S;
/

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

end TNMT_THANHTRA_TK;
/

prompt
prompt Creating trigger TRI_CAN_BO
prompt ===========================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_CAN_BO" BEFORE INSERT ON CB_CAN_BO REFERENCING NEW AS NEW FOR EACH ROW 
BEGIN SELECT SEQ_CAN_BO.NEXTVAL    INTO :NEW.MA_CAN_BO    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_DM_LOP_TAP_HUAN
prompt ====================================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_DM_LOP_TAP_HUAN" BEFORE INSERT ON DM_LOP_TAP_HUAN REFERENCING NEW AS NEW FOR EACH ROW
BEGIN SELECT SEQ_DM_LOP_TAP_HUAN.NEXTVAL    INTO :NEW.MA_LOP_TAP_HUAN    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_GIAI_QUYET
prompt ===============================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_GIAI_QUYET" BEFORE INSERT ON KNTC_GIAI_QUYET REFERENCING NEW AS NEW FOR EACH ROW 
BEGIN SELECT SEQ_GIAI_QUYET.NEXTVAL    INTO :NEW.MA_GIAI_QUYET    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_KNTC_HO_SO
prompt ===============================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_KNTC_HO_SO" BEFORE INSERT ON KNTC_HO_SO REFERENCING NEW AS NEW FOR EACH ROW 
BEGIN SELECT SEQ_KNTC_HO_SO.NEXTVAL    INTO :NEW.MA_HO_SO_KNTC    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_LICH_CONG_TAC
prompt ==================================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_LICH_CONG_TAC" BEFORE INSERT ON LICH_CONG_TAC REFERENCING NEW AS NEW FOR EACH ROW
BEGIN SELECT SEQ_LICH_CONG_TAC.NEXTVAL    INTO :NEW.STT    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_TO_CHUYEN_MON
prompt ==================================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_TO_CHUYEN_MON" BEFORE INSERT ON CB_TO_CHUYEN_MON REFERENCING NEW AS NEW FOR EACH ROW 
BEGIN SELECT SEQ_TO_CHUYEN_MON.NEXTVAL    INTO :NEW.MA_TO_CHUYEN_MON    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_TT_DOAN_THANH_TRA
prompt ======================================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_TT_DOAN_THANH_TRA" BEFORE INSERT ON TT_DOAN_THANH_TRA REFERENCING NEW AS NEW FOR EACH ROW
BEGIN SELECT SEQ_TT_DOAN_THANH_TRA.NEXTVAL    INTO :NEW.MA_DOAN_TT    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_TT_KE_HOACH_TT
prompt ===================================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_TT_KE_HOACH_TT" BEFORE INSERT ON TT_KE_HOACH_THANH_TRA REFERENCING NEW AS NEW FOR EACH ROW
BEGIN SELECT SEQ_TT_KE_HOACH_TT.NEXTVAL    INTO :NEW.MA_KE_HOACH_TT    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_TT_QUYET_DINH
prompt ==================================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_TT_QUYET_DINH" BEFORE INSERT ON TT_QUYET_DINH REFERENCING NEW AS NEW FOR EACH ROW
BEGIN SELECT SEQ_TT_QUYET_DINH.NEXTVAL    INTO :NEW.MA_QUYET_DINH    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_TT_THANH_TRA
prompt =================================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_TT_THANH_TRA" BEFORE INSERT ON TT_THANH_TRA REFERENCING NEW AS NEW FOR EACH ROW
BEGIN SELECT SEQ_TT_THANH_TRA.NEXTVAL    INTO :NEW.STT_THANH_TRA    FROM DUAL; END;
/

prompt
prompt Creating trigger TRI_TT_THANH_TRA_LINH_VUC
prompt ==========================================
prompt
CREATE OR REPLACE TRIGGER "HAI"."TRI_TT_THANH_TRA_LINH_VUC" BEFORE INSERT ON TT_THANH_TRA_LINH_VUC REFERENCING NEW AS NEW FOR EACH ROW
BEGIN SELECT SEQ_TT_THANH_TRA_LINH_VUC.NEXTVAL    INTO :NEW.STT_THANH_TRA    FROM DUAL; END;
/


spool off
