Ejemplo n.º 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_tco_org_pk = Request["l_tco_org_pk"];
        string p_work_mon   = Request["l_work_mon"];
        string p_emp_id     = Request["l_emp_id"];
        string p_from_dt    = Request["dt_from"];
        string p_to_dt      = Request["dt_to"];
        string p_nation     = Request["l_nation"];
        string p_sal_period = Request["l_sal_period"];

        string TemplateFile = "rpt_hrti01700_0.xls";
        string TempFile     = "../../../../system/temp/rpt_hrti01700_0_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;


        string    para      = "'" + p_emp_id + "','" + p_work_mon + "','" + p_tco_org_pk + "','" + p_nation + "','" + p_sal_period + "'";
        DataTable dt_detail = CtlLib.TableReadOpenCursor("ST_HR_RPT_HRTI01700_0", para);

        string sql_day
            = "select substr(c.car_date,-2), c.hol_type from thr_calendar c  " +
              "where c.del_if=0  " +
              "and c.car_date between '" + p_from_dt + "' and '" + p_to_dt + "' " +
              "and c.tco_company_pk = (select g.tco_company_pk " +
              "                                       from tco_org g " +
              "                                      where g.del_if = 0 and g.pk = '" + p_tco_org_pk + "') " +
              " order by c.car_date ";

        DataTable dt_day = CtlLib.TableReadOpen(sql_day);

        string sql_date
            = "select  " +
              "    to_char(to_date('" + p_work_mon + "', 'yyyymm'), 'mm yyyy')  " +
              "  from dual ";

        DataTable dt_date = CtlLib.TableReadOpen(sql_date);

        int row_loop = 10;
        int l_col    = dt_detail.Columns.Count - 31;
        int row_emp  = dt_detail.Rows.Count;
        int row_day  = dt_day.Rows.Count;
        int count    = 0;

        int s_row      = 9;
        int pos        = 9;
        int s_col      = 6;
        int temp       = 0;
        int l_col_pass = 11;

        double[] grd_total = new double[l_col];

        exSheet.Cells["A6"].Value = "THÁNG " + dt_date.Rows[0][0];
        // do du lieu header ngay
        for (int d = 0; d < dt_day.Rows.Count; d++)
        {
            exSheet.Cells[9, s_col + d].Value = dt_day.Rows[d][0].ToString();
        }



        for (int l_addrow = 1; l_addrow < dt_detail.Rows.Count; l_addrow++)
        {
            exSheet.Range[row_loop + l_addrow, 50].Rows.EntireRow.Insert(); //insert row new of sheet
        }

        // xu ly du lieu do vao excel
        if (dt_detail.Rows.Count > 0)
        {
            for (int l_row = 0; l_row < dt_detail.Rows.Count; l_row++)
            {
                count++;
                exSheet.Cells[row_loop + l_row, 1].Value = count;

                // do du lieu vao cac dong
                for (int col = 2; col < l_col; col++)
                {
                    if (IsNumeric(dt_detail.Rows[l_row][col].ToString()) && col != 3)
                    {
                        grd_total[col] += double.Parse(dt_detail.Rows[l_row][col].ToString());
                        exSheet.Cells[row_loop + l_row, col].Value = double.Parse(dt_detail.Rows[l_row][col].ToString());
                    }
                    else
                    {
                        exSheet.Cells[row_loop + l_row, col].Value = dt_detail.Rows[l_row][col].ToString();
                    }
                }
                pos++;

                //TO MAU CHO NHUNG NGAY OFF VA NGAY LE
                for (int col = s_col; col < s_col + 31; col++)
                {
                    if (dt_detail.Rows[l_row][col + l_col_pass + 31].ToString() == "SUN")
                    {
                        exSheet.Range[row_loop + l_row, col].Interior.Color = Color.SkyBlue;
                    }
                    if (dt_detail.Rows[l_row][col + l_col_pass + 31].ToString() == "HOL")
                    {
                        exSheet.Range[row_loop + l_row, col].Interior.Color = Color.Pink;

                        if (dt_detail.Rows[l_row][col].ToString() == "")
                        {
                            exSheet.Cells[row_loop + l_row, col].Value = "PH";
                        }
                    }
                }
            }
            //do du lieu vao grand total
            for (int k = 36; k < l_col; k++)
            {
                exSheet.Cells[pos + 1, k].Value          = grd_total[k];
                exSheet.Cells[pos + 1, k].Rows.Font.Bold = true;
            }
        }

        //exSheet.Cells[row_loop + count + 2, 9].Value = "Ngày "+ dt_master.Rows[0]["ins_day"].ToString() +" Tháng "+ dt_master.Rows[0]["ins_mon"].ToString() + " Năm "+ dt_master.Rows[0]["ins_year"].ToString();

        for (int row = 0; row < row_day; row++)
        {
            temp = 31 - row_day;
            switch (temp)
            {
            case 3:

                exSheet.Range["AH1"].Columns.Hidden = true;
                exSheet.Range["AI1"].Columns.Hidden = true;
                exSheet.Range["AJ1"].Columns.Hidden = true;
                break;

            case 2:
                exSheet.Range["AI1"].Columns.Hidden = true;
                exSheet.Range["AJ1"].Columns.Hidden = true;
                break;

            case 1:
                exSheet.Range["AJ1"].Columns.Hidden = true;
                break;
            }
            switch (dt_day.Rows[row][1].ToString())
            {
            case "SUN":
                exSheet.Range[s_row, row + s_col].Interior.Color = Color.SkyBlue;
                break;

            case "HOL":
                exSheet.Range[s_row, row + s_col, s_row, row + s_col].Interior.Color = Color.Pink;
                break;
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        // range.Rows.Hidden = true;

        // font bold header

        /*range = exSheet.Range["A1:AC1"];
         * range.Rows[4].Font.Bold = true;*/

        exBook.SaveAs(TempFile);
        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        //Response.WriteFile(pdfFilePath);
        Response.WriteFile(TempFile);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept_pk       = Request["l_tco_dept_pk"].ToString();
        string p_group_pk      = Request["l_thr_group_pk"].ToString();
        string p_from_dt       = Request["l_from_dt"].ToString();
        string p_to_dt         = Request["l_to_dt"].ToString();
        string p_search        = Request["l_search_by"].ToString();
        string p_input         = Request["l_search_tmp"].ToString();
        string p_meal_emp_type = Request["l_meal_emp_type"].ToString();
        string p_meal_type     = Request["l_meal_type"].ToString();
        string p_meal_times    = Request["l_meal_times"].ToString();

        string TemplateFile = "rpt_chrm00010_02.xls";
        string TempFile     = "../../../../system/temp/rpt_chrm00010_02_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];


        string SQL
            = "SELECT * " +
              "FROM (  " +
              "SELECT 1, DIV_0 DIV_0,SUM(TO_SO_1) TONG_SO_1,SUM(CO_MAT) CO_MAT_2,SUM(VANG_MAT) VANG_MAT_3 " +
              ",SUM(MEAL_01) MEAL_01,SUM(MEAL_02) MEAL_02,SUM(MEAL_03) MEAL_03,SUM(MEAL_04) MEAL_04 " +
              ",SUM(MEAL_05) MEAL_05 " +
              "FROM( " +
              "SELECT (    SELECT g.ORG_NM  " +
              "              FROM TCO_ORG g  " +
              "             WHERE g.del_if = 0 AND G.ORG_TYPE = '06'  " +
              "        START WITH g.pk = e.pk  " +
              "        CONNECT BY PRIOR g.p_pk = g.pk) DIV_0 " +
              ",1 TO_SO_1,(CASE WHEN NVL(F.PK,0)=0 THEN 0 ELSE 1 END) CO_MAT " +
              ",(CASE WHEN NVL(F.PK,0)=0 THEN 1 ELSE 0 END) VANG_MAT " +
              ",NVL(MEAL.MEAL_01,0) MEAL_01,NVL(MEAL.MEAL_02,0) MEAL_02,NVL(MEAL.MEAL_03,0) MEAL_03,NVL(MEAL.MEAL_04,0) MEAL_04 " +
              ",NVL(MEAL.MEAL_05,0) MEAL_05          " +
              "FROM THR_EMPLOYEE A,THR_WG_SCH_DETAIL D,tco_org e,THR_TIME_MACHINE F " +
              "    ,(SELECT M1.THR_EMP_PK,  " +
              "               M1.WORK_DT_OF_SHIFT,  " +
              "               SUM(CASE WHEN M1.MEAL_TIMES = '01' THEN 1 ELSE 0 END) MEAL_01,  " +
              "               SUM (CASE WHEN M1.MEAL_TIMES = '02' THEN 1 ELSE 0 END) MEAL_02,  " +
              "               SUM (CASE WHEN M1.MEAL_TIMES = '03' THEN 1 ELSE 0 END) MEAL_03,  " +
              "               SUM (CASE WHEN M1.MEAL_TIMES = '04' THEN 1 ELSE 0 END) MEAL_04,  " +
              "               SUM (CASE WHEN M1.MEAL_TIMES = '05' THEN 1 ELSE 0 END) MEAL_05  " +
              "               FROM THR_MEAL_CHECKING M1   " +
              "               WHERE M1.DEL_IF=0   " +
              "               AND M1.WORK_DT_OF_SHIFT ='" + p_from_dt + "' AND M1.MEAL_EMP_TYPE='IN' " +
              "               AND M1.THR_EMP_PK IS NOT NULL   " +
              "               GROUP BY M1.THR_EMP_PK,M1.WORK_DT_OF_SHIFT) MEAL " +
              "WHERE A.DEL_IF=0 AND D.DEL_IF=0 AND E.DEL_IF=0 AND F.DEL_IF(+)=0 AND D.THR_EMP_PK=F.THR_EMP_PK(+) " +
              "AND A.TCO_ORG_PK=E.PK AND A.PK=D.THR_EMP_PK AND D.WORK_DT=F.WORK_DT(+)  " +
              "AND D.WORK_DT='" + p_from_dt + "' AND D.THR_EMP_PK=MEAL.THR_EMP_PK(+) AND D.WORK_DT=MEAL.WORK_DT_OF_SHIFT(+) " +
              "AND A.JOIN_DT<='" + p_from_dt + "') HR " +
              "GROUP BY DIV_0 " +
              "UNION ALL " +
              "SELECT 2, 'OUT OF COMPANY',0,0,0,  " +
              "SUM(CASE WHEN M1.MEAL_TIMES = '01' THEN 1 ELSE 0 END) MEAL_01,  " +
              "SUM(CASE WHEN M1.MEAL_TIMES = '02' THEN 1 ELSE 0 END) MEAL_02,  " +
              "SUM(CASE WHEN M1.MEAL_TIMES = '03' THEN 1 ELSE 0 END) MEAL_03,  " +
              "SUM(CASE WHEN M1.MEAL_TIMES = '04' THEN 1 ELSE 0 END) MEAL_04,  " +
              "0 MEAL_05  " +
              "FROM THR_MEAL_CHECKING M1   " +
              "WHERE M1.DEL_IF=0   " +
              "AND M1.WORK_DT ='" + p_from_dt + "' AND M1.MEAL_EMP_TYPE='OUT' " +
              "GROUP BY M1.WORK_DT " +
              "UNION ALL " +
              "SELECT 3, 'VISITOR',0,0,0,  " +
              "SUM(NVL(M1.MEAL_QTY,1)*(CASE WHEN M1.MEAL_TIMES = '01' THEN 1 ELSE 0 END)) MEAL_01,  " +
              "SUM(NVL(M1.MEAL_QTY,1)*(CASE WHEN M1.MEAL_TIMES = '02' THEN 1 ELSE 0 END)) MEAL_02,  " +
              "SUM(NVL(M1.MEAL_QTY,1)*(CASE WHEN M1.MEAL_TIMES = '03' THEN 1 ELSE 0 END)) MEAL_03,  " +
              "SUM(NVL(M1.MEAL_QTY,1)*(CASE WHEN M1.MEAL_TIMES = '04' THEN 1 ELSE 0 END)) MEAL_04,  " +
              "0 MEAL_05  " +
              "FROM THR_MEAL_VISITOR M1   " +
              "WHERE M1.DEL_IF=0   " +
              "AND M1.WORK_DT ='" + p_from_dt + "' " +
              "GROUP BY M1.WORK_DT) " +
              "ORDER BY 1 ";


        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);

        int irow = dt_emp.Rows.Count;
        int icol = dt_emp.Columns.Count;

        if (irow == 0)
        {
            Response.Write("There is no data.");
            Response.End();
        }



        //Khai bao bien
        int i, j, pos = 5;

        double[] grant_total = new double[icol + 1];

        //Insert new row

        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[pos + i + 1, 1].Rows.EntireRow.Insert();
        }

        //Title value
        exSheet.Cells["E2"].Value = p_from_dt.Substring(6, 2).ToString() + "/" + p_from_dt.Substring(4, 2).ToString() + "/" + p_from_dt.Substring(0, 4).ToString();


        //Show data
        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[pos + i, 1].Value = i + 1;
            for (j = 1; j < icol; j++)
            {
                if (j >= 2)
                {
                    exSheet.Cells[pos + i, j + 1].Value = double.Parse(dt_emp.Rows[i][j].ToString());
                    grant_total[j] += double.Parse(dt_emp.Rows[i][j].ToString());
                }
                else
                {
                    exSheet.Cells[pos + i, j + 1].Value = dt_emp.Rows[i][j].ToString();
                }
            }
        }

        for (j = 2; j < icol; j++)
        {
            exSheet.Cells[pos + irow, j + 1].Value = grant_total[j];
        }



        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept, p_group, p_status, p_type, p_pay_type, p_emp_id, p_work_mon, p_m_type, p_sal_period, p_emp_type;

        p_dept       = Request["l_dept"].ToString();
        p_group      = Request["l_group"].ToString();
        p_status     = Request["l_status"].ToString();
        p_type       = Request["l_type"].ToString();
        p_pay_type   = Request["l_pay_type"].ToString();
        p_emp_id     = Request["l_emp_id"].ToString();
        p_work_mon   = Request["l_work_mon"].ToString();
        p_sal_period = Request["l_sal_period"].ToString();
        p_emp_type   = Request["l_Emp_Type"].ToString();
        p_m_type     = Request.QueryString["l_m_type"].ToString();

        string TemplateFile = "rpt_hrpm00700_2.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00700_2_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        string SQL
            = "SELECT A.FULL_NAME full_nm,   " +
              "         A.EMP_ID emp_id,   " +
              "         a.DEPT_NM dept_nm,   " +
              "         TO_CHAR (TO_DATE (A.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy') join_dt,   " +
              "         (CASE   " +
              "             WHEN NVL (a.CONFIRM_DT, a.from_dt) > a.from_dt   " +
              "             THEN   " +
              "                ROUND (NVL (A.SALARY_LEVEL1, 0), 10)   " +
              "             ELSE   " +
              "                0   " +
              "          END)   " +
              "            sal_level1_7,   " +
              "         (CASE   " +
              "             WHEN NVL (a.CONFIRM_DT, a.to_dt) > a.to_DT THEN 0   " +
              "             ELSE ROUND (NVL (A.SALARY_LEVEL2, 0), 10)   " +
              "          END)   " +
              "            sal_level2,   " +
              "         ROUND (NVL (A.ALLOW_AMT1, 0), 10) allow_amt6,   " +
              "         ROUND (NVL (A.ALLOW_AMT2, 0), 10) allow_amt1,   " +
              "         ROUND (NVL (A.ALLOW_AMT3, 0), 10) allow_amt1,   " +
              "         ROUND (NVL (A.ALLOW_AMT4, 0), 10) allow_amt4,   " +
              "         ROUND (NVL (A.ALLOW_AMT5, 0), 10) allow_amt5,   " +
              "         ROUND (NVL (A.ALLOW_AMT6, 0), 10) allow_amt6,   " +
              "         ROUND (NVL (A.ALLOW_AMT7, 0), 10) allow_amt7,   " +
              "         ROUND (NVL (A.ALLOW_AMT8, 0), 10) allow_amt8,   " +
              "         ROUND (NVL (A.ALLOW_AMT9, 0), 10) allow_amt9,   " +
              "         ROUND (NVL (A.ALLOW_AMT10, 0), 10) allow_amt10,   " +
              "         ROUND (NVL (A.ALLOW_AMT11, 0), 10) allow_amt11,   " +
              "         ROUND (NVL (A.ALLOW_AMT12, 0), 10) allow_amt12,'',   " +
              "         ROUND (NVL (A.WT_L1_AMT, 0)+ NVL (a.HOL_DAY_L1_AMT, 0)+ NVL (A.WT_L2_AMT, 0)+ NVL (a.HOL_DAY_L2_AMT, 0),3) total_wd_amt_23,  " +
              "         ROUND (NVL (A.ABS_ALE_L1_AMT, 0) + NVL (A.ABS_ALE_L2_AMT, 0),3) abs_ale_amt,   " +
              "         ROUND (NVL (A.ABS_OTHER_PAY_L1_AMT, 0) + NVL (A.ABS_OTHER_PAY_L2_AMT, 0),3) abs_other_amt,   " +
              "         ROUND (NVL (A.OT_L1_AMT, 0) + NVL (A.OT_L2_AMT, 0), 3) ot_amt,   " +
              "         ROUND (NVL (A.ST_L1_AMT, 0) + NVL (A.OST_L1_AMT, 0) + NVL (A.OST_L2_AMT, 0) + NVL (A.ST_L2_AMT, 0), 3) st_amt,   " +
              "         ROUND (NVL (A.HT_L1_AMT, 0) + NVL (A.OHT_L1_AMT, 0) + NVL (A.HT_L2_AMT, 0) + NVL (A.OHT_L2_AMT, 0), 3) hol_amt,   " +
              "         ROUND (NVL (A.NT_30_L1_AMT, 0) + NVL (A.NT_30_L2_AMT, 0), 3) nt30_amt,   " +
              "         ROUND (NVL (A.NT_45_L1_AMT, 0) + NVL (A.NT_45_L2_AMT, 0) + NVL (A.NT_45_2_L1_AMT, 0) + NVL (A.NT_45_2_L2_AMT, 0), 3) nt45_amt,   " +
              "         ROUND (NVL (A.NT_60_L1_AMT, 0) + NVL (A.NT_60_L2_AMT, 0), 3) nt60_amt,   " +
              "         ROUND (NVL (A.NT_90_L1_AMT, 0) + NVL (A.NT_90_L2_AMT, 0), 3) nt90_amt_34, " +
              "         ROUND (NVL (A.RETURN_AMT, 0), 3) return_amt_36, " +
              "         ROUND (NVL (A.INC_AMT, 0) + NVL (A.BONUS_YEARLY_AMT, 0), 3) inc_amt, " +
              "         ROUND (NVL (A.ALE_STOP_AMT, 0), 3) ale_stop_amt,     " +
              "         ROUND (NVL (A.SEVERANCE_AMT, 0), 3) severance_amt, " +
              "         ROUND (NVL (A.GROSS_AMT, 0), 3) gross_amt,   " +
              "         ROUND (NVL (A.BREAK_CONTRACT_AMT, 0), 3) break_day_amt,   " +
              "         ROUND (NVL (A.SOCIAL_AMT, 0), 3) total_ins_amt, " +
              "         ROUND (NVL (A.HEALTH_AMT, 0), 3) health_amt, " +
              "         ROUND (NVL (A.UNEMP_INS, 0), 3) unemp_amt,   " +
              "         ROUND (NVL (A.ADVANCE_AMT, 0), 3) advance_amt,   " +
              "         ROUND (NVL (A.EQUIP1_AMT, 0), 3) uniform_amt,   " +
              "         ROUND (NVL (A.OT_TAX_L1_AMT, 0) + NVL (A.OT_TAX_L2_AMT, 0), 3) ot_100_amt, " +
              "         ROUND (NVL (A.DEDUCT_PIT, 0), 3) deduct_pit, " +
              "         ROUND (NVL (A.INCOME_BEFORE_TAX, 0), 3) income_amt, " +
              "         ROUND (NVL (A.INCOME_TAX, 0), 3) income_tax,   " +
              "         ROUND (NVL (A.UNION_AMT, 0), 3) union_amt,   " +
              "         ROUND (NVL (A.RETURN_NO_PIT, 0) , 3) return_no_pit,   " +
              "         ROUND (NVL (A.ADVANCE_NO_PIT, 0), 3) advance_no_pit,  " +
              "         0 pit_final,  " +
              "         ROUND (NVL (A.NET_AMT, 0), 2) net_amt_46,   " +
              "         ROUND (NVL (A.WT_L1, 0) / 8  + NVL (a.HOL_DAY_L1, 0),0) wd_l1,   " +
              "         ROUND (NVL (A.ABS_ALE_L1, 0) / 8,0) ale_l1, " +
              "         ROUND (NVL (A.ABS_OTHER_PAY_L1, 0)/8,0) abs_other_l1, " +
              "         ROUND (NVL (A.OT_L1, 0),0) ot_l1, " +
              "         ROUND (NVL (A.ST_L1, 0) + NVL (A.OST_L1, 0),0) st_l1,  " +
              "         ROUND (NVL (A.HT_L1, 0) + NVL (A.OHT_L1, 0),0) ht_l1,  " +
              "         ROUND (NVL (A.NT_30_L1, 0), 0) ,  " +
              "         ROUND (NVL (A.NT_45_L1, 0) + NVL (A.NT_45_2_L1, 0), 0) ,  " +
              "         ROUND (NVL (A.NT_60_L1, 0), 0) ,  " +
              "         ROUND (NVL (A.NT_90_L1, 0), 0) ,     " +
              "         ROUND (NVL (A.WT_L2, 0) / 8 + NVL (a.HOL_DAY_L2, 0) , 2) wd_l2, " +
              "         ROUND (NVL (A.ABS_ALE_L2, 0) / 8, 2) ale_l2, " +
              "         ROUND (NVL (A.ABS_OTHER_PAY_L2, 0)/8,3) abs_other_l2, " +
              "         ROUND (NVL (A.OT_L2, 0), 3) ot_l2, " +
              "         ROUND (NVL (A.OST_L2, 0) + NVL (A.ST_L2, 0),3) ot_l2, " +
              "         ROUND (NVL (A.HT_L2, 0) + NVL (A.OHT_L2, 0), 3) ot_hol_52,   " +
              "         ROUND (NVL (A.NT_30_L2, 0), 3) nt30,  " +
              "         ROUND (NVL (A.NT_45_L2, 0) + NVL (A.NT_45_2_L2, 0), 3) nt45,  " +
              "         ROUND (NVL (A.NT_60_L2, 0), 3) nt60,  " +
              "         ROUND (NVL (A.NT_90_L2, 0), 3) nt90,  " +
              "         ROUND (NVL (A.ALE_STOP, 0), 3) ale_stop, " +
              "         ROUND (NVL (A.SEVERANCE_MONTH, 0), 3) severance_month " +
              "    FROM thr_month_salary a,   " +
              "         tco_org b, thr_work_group c   " +
              "   WHERE     a.del_if = 0 and c.del_if =0   " +
              "         AND a.work_mon = '" + p_work_mon + "'   " +
              "         AND b.del_if = 0   " +
              "         AND a.tco_dept_pk = b.pk   " +
              "         AND A.THR_GROUP_PK = c.pk " +
              "         AND (a.tco_dept_pk IN " +
              "                 (    SELECT g.pk " +
              "                        FROM tco_org g " +
              "                       WHERE g.del_if = 0 " +
              "                  START WITH g.pk = " +
              "                                DECODE ('" + p_dept + "', " +
              "                                        'ALL', 0, " +
              "                                        '" + p_dept + "') " +
              "                  CONNECT BY PRIOR g.pk = g.p_pk) " +
              "              OR '" + p_dept + "' = 'ALL') " +
              "         AND DECODE ('" + p_group + "', 'ALL', 'ALL', a.thr_group_pk) = '" + p_group + "' " +
              "         AND a.emp_id LIKE '%" + p_emp_id + "%' " +
              "         AND DECODE ('" + p_status + "', 'ALL', 'ALL', a.status) = '" + p_status + "' " +
              "         AND DECODE ('" + p_pay_type + "', 'ALL', 'ALL', a.pay_type) = '" + p_pay_type + "' " +
              "         and decode('" + p_emp_type + "' ,'ALL','ALL',A.EMPLOYEE_TYPE)='" + p_emp_type + "' " +
              "         AND DECODE ('" + p_m_type + "', 'ALL', 'ALL', a.MONEY_KIND) = '" + p_m_type + "' " +
              "         AND DECODE ('" + p_sal_period + "', 'ALL', 'ALL', A.SAL_PERIOD) = '" + p_sal_period + "' " +
              "         AND NVL (a.net_amt, 0) " + p_type +
              "ORDER BY C.WORKGROUP_NM,nvl(b.seq,0),A.DEPT_NM, a.emp_id   ";

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);
        int       irow   = dt_emp.Rows.Count;
        int       icol   = dt_emp.Columns.Count;

        if (irow == 0)
        {
            Response.Write("There is no data of salary.");
            Response.End();
        }

        string    para_com = "'" + p_dept + "','" + p_work_mon + "'";
        DataTable dt_com   = CtlLib.TableReadOpenCursor("ST_HR_RPT_CO_COMPANY", para_com);


        DataTable dt_allow = CtlLib.TableReadOpenCursor("ST_HR_RPT_CO_ALLOW_SALARY");


        //Khai bao bien
        int    i, j;
        int    loop_row = 51, stt = 0;
        int    r_allow   = 9;
        string allow_a   = "";
        double allow_amt = 0;

        //Insert new row

        for (i = 1; i < irow; i++)
        {
            //exSheet.Range[2, 7, 54, 7].Rows.EntireRow.Insert();
            exSheet.Range["A1:G" + loop_row].Copy(exSheet.Range["A" + (loop_row * i + 1) + ":G" + (loop_row + loop_row * i)], XlPasteType.xlPasteAll);

            for (j = 1; j < loop_row; j++)
            {
                exSheet.Range[loop_row * i + j, 1].RowHeight = exSheet.Range[j, 1].RowHeight;
            }
        }

        //Show data
        for (i = 0; i < irow; i++)
        {
            //insert picture
            if (dt_com.Rows[0]["image"].ToString() != "")
            {
                byte[] MyData = new byte[0];
                MyData = (byte[])dt_com.Rows[0]["image"];
                MemoryStream stream = new MemoryStream(MyData);

                Bitmap b = new Bitmap(stream);
                exSheet.Cells[1 + loop_row * i, 1].Select();
                exSheet.Shapes.AddPicture(ResizeImage(b, 140, 60));
            }
            stt++;

            exSheet.Cells["B" + (2 + loop_row * i)].Value = "PAY-SLIP IN " + dt_com.Rows[0][4].ToString();
            exSheet.Cells["B" + (3 + loop_row * i)].Value = "PHIẾU LƯƠNG THÁNG " + dt_com.Rows[0][5].ToString();

            exSheet.Cells["A" + (4 + loop_row * i)].Value = string.Format("Họ tên: {0}", dt_emp.Rows[i]["full_nm"].ToString());
            exSheet.Cells["B" + (5 + loop_row * i)].Value = dt_emp.Rows[i]["emp_id"].ToString();
            exSheet.Cells["F" + (5 + loop_row * i)].Value = dt_emp.Rows[i]["dept_nm"].ToString();
            exSheet.Cells["B" + (6 + loop_row * i)].Value = dt_emp.Rows[i]["join_dt"].ToString();
            exSheet.Cells["D" + (8 + loop_row * i)].Value = double.Parse(dt_emp.Rows[i]["sal_level1_7"].ToString());
            exSheet.Cells["F" + (8 + loop_row * i)].Value = double.Parse(dt_emp.Rows[i]["sal_level2"].ToString());

            //ten phu cap

            for (j = 0; j < 12; j++)
            {
                exSheet.Cells["A" + (r_allow + j + loop_row * i)].Value = dt_allow.Rows[0][24 + j].ToString();
                exSheet.Cells["B" + (r_allow + j + loop_row * i)].Value = string.Format("({0})", dt_allow.Rows[0][0 + j].ToString());
            }

            //cot tien tu dong 9 den dong 47
            int k = 6;
            for (j = 0; j < 43; j++)
            {
                if (j != 12)
                {
                    exSheet.Cells["G" + (9 + j + loop_row * i)].Value = double.Parse(dt_emp.Rows[i][j + k].ToString());
                }
            }

            //Ngay cong, gio tang ca l1
            k += 43;
            for (j = 0; j < 10; j++)
            {
                exSheet.Cells["D" + (22 + j + loop_row * i)].Value = double.Parse(dt_emp.Rows[i][j + k].ToString());
            }
            //Ngay cong, gio tang ca l1
            k += 10;
            for (j = 0; j < 10; j++)
            {
                exSheet.Cells["E" + (22 + j + loop_row * i)].Value = double.Parse(dt_emp.Rows[i][j + k].ToString());
            }

            k += 10;
            exSheet.Cells["D" + (34 + loop_row * i)].Value = double.Parse(dt_emp.Rows[i]["ale_stop"].ToString());
            exSheet.Cells["D" + (35 + loop_row * i)].Value = double.Parse(dt_emp.Rows[i]["severance_month"].ToString());

            //Insert page break
            IPageBreaks pbs       = exBook.Worksheets[1].HPageBreaks;
            IRange      usedrange = exBook.Worksheets[1].UsedRange;
            //pbs.Reset();
            pbs.Add(usedrange.Rows[loop_row + loop_row * i + 1]);
            //end insert page beak

            //Hide allowance row allowance
            for (j = 0; j < 12; j++)
            {
                if (dt_allow.Rows[0][j + 12].ToString() != "1")
                {
                    exSheet.Range["A" + (r_allow + j + loop_row * i)].Rows.Hidden = true;
                }
            }
        }

        //Hide row


        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }

        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());
        string l_repayment_pk = Request["p_tht_repayment_pk"];
        string p_employee_pk  = Request["p_employee_pk"];

        string TemplateFile = "rpt_60250170_Guest_Invoice.xls";
        string TempFile     = "../../../../system/temp/rpt_60250170_Guest_Invoice_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        //bind data to excel file
        string    para = "";
        DataTable dt;

        para = "'" + l_repayment_pk + "'";
        dt   = CtlLib.TableReadOpenCursor("ht_rpt_60250170_repayment", para);
        string SQL = "";

        SQL = "select max(full_name) from thr_employee where pk='" + p_employee_pk + "'";
        DataTable dtEmp = new DataTable();

        dtEmp = CtlLib.TableReadOpen(SQL);
        exSheet.Cells[33, 5].Value = dtEmp.Rows[0][0].ToString();         //full_name login sofware
        //-----header---

        //---end--header---
        //--set-----DataBind--in---Master---
        if (dt.Rows.Count == 0)
        {
            exSheet.Cells[8, 4].Value = "There is not data!";
        }
        if (dt.Rows.Count > 0)
        {
            string l_status_title = "GUEST INVOICE";


            exSheet.Cells[7, 3].Value = l_status_title;
            //exSheet.Cells[ 8, 1].Value = dt.Rows[0]["confirm_msg"].ToString();
            exSheet.Cells[9, 3].Value  = dt.Rows[0]["guest_name"].ToString();
            exSheet.Cells[9, 6].Value  = dt.Rows[0]["print_time"].ToString();
            exSheet.Cells[10, 3].Value = dt.Rows[0]["partner_name"].ToString();
            exSheet.Cells[10, 6].Value = dt.Rows[0]["room_no"].ToString();
            //exSheet.Cells[10, 7].Value = l_folio_type;
            exSheet.Cells[11, 3].Value = dt.Rows[0]["customer_address"].ToString();
            exSheet.Cells[11, 6].Value = dt.Rows[0]["invoice_no"].ToString();
            exSheet.Cells[12, 3].Value = dt.Rows[0]["tax_code"].ToString();
            exSheet.Cells[12, 6].Value = dt.Rows[0]["arrival_dt"].ToString();
            //exSheet.Cells[13, 3].Value = dt.Rows[0]["guest_name"].ToString(); //total guest
            exSheet.Cells[13, 6].Value = dt.Rows[0]["checkout_dt"].ToString();
            exSheet.Cells[14, 4].Value = dt.Rows[0]["paymethod"].ToString();

            exSheet.Cells[17, 5].Value = double.Parse(dt.Rows[0]["total_due_vnd_pp"].ToString());                    //
            exSheet.Cells[17, 7].Value = float.Parse(dt.Rows[0]["total_due_usd_pp"].ToString());
            //exSheet.Cells[18, 5].Value = dt.Rows[0]["paymethod"].ToString();TTDB  VND
            //exSheet.Cells[18, 7].Value = dt.Rows[0]["paymethod"].ToString();TTDB  USD
            exSheet.Cells[19, 5].Value = int.Parse(dt.Rows[0]["svc_amt"].ToString());
            exSheet.Cells[19, 7].Value = float.Parse(dt.Rows[0]["svc_amt_usd"].ToString());

            exSheet.Cells[20, 5].Value = int.Parse(dt.Rows[0]["total_vnd"].ToString());
            exSheet.Cells[20, 7].Value = float.Parse(dt.Rows[0]["total_usd"].ToString());

            exSheet.Cells[21, 5].Value = int.Parse(dt.Rows[0]["vat_amt"].ToString());
            exSheet.Cells[21, 7].Value = float.Parse(dt.Rows[0]["vat_amt_usd"].ToString());

            //exSheet.Cells[22, 5].Value = int.Parse(dt.Rows[0]["total_discount_amt"].ToString());
            //exSheet.Cells[22, 7].Value = float.Parse(dt.Rows[0]["total_discount_amt_usd"].ToString());

            //exSheet.Cells[22, 5].Value = int.Parse(dt.Rows[0]["advance_amt"].ToString());
            //exSheet.Cells[22, 7].Value = float.Parse(dt.Rows[0]["advance_amt_usd"].ToString());

            exSheet.Cells[23, 5].Value = int.Parse(dt.Rows[0]["total_due_end_vnd"].ToString());
            exSheet.Cells[23, 7].Value = float.Parse(dt.Rows[0]["total_due_usd"].ToString());

            exSheet.Cells[24, 3].Value = int.Parse(dt.Rows[0]["cash_vnd"].ToString());
            exSheet.Cells[24, 5].Value = float.Parse(dt.Rows[0]["cash_usd"].ToString());

            exSheet.Cells[25, 3].Value = int.Parse(dt.Rows[0]["credit_vnd"].ToString());
            exSheet.Cells[25, 5].Value = float.Parse(dt.Rows[0]["credit_usd"].ToString());
            if (int.Parse(dt.Rows[0]["cash_vnd"].ToString()) == 0 && float.Parse(dt.Rows[0]["cash_usd"].ToString()) == 0)
            {
                range             = exSheet.Range["A24"];
                range.Rows.Hidden = true;
            }
            if (int.Parse(dt.Rows[0]["credit_vnd"].ToString()) == 0 && float.Parse(dt.Rows[0]["credit_usd"].ToString()) == 0)
            {
                range             = exSheet.Range["A25"];
                range.Rows.Hidden = true;
            }
            string inword_amt, inword_amt_usd;
            inword_amt                 = CommondLib.Num2VNText(dt.Rows[0]["total_due_end_vnd"].ToString(), "VND");
            inword_amt_usd             = CommondLib.Num2EngText(dt.Rows[0]["total_due_usd"].ToString(), "USD");
            exSheet.Cells[26, 3].Value = inword_amt.ToString();
            exSheet.Cells[27, 3].Value = inword_amt_usd.ToString();
        }

        /*if (l_svc < 1)//not svc then hide row svc
         * {
         *  range = exSheet.Range["A15"];
         *  // hide row A15
         *  range.Rows.Hidden = true;
         * }*/

        //----------------------
        ////end-set-----DataBind--in---Master---

        for (int l_addrow = 1; l_addrow < dt.Rows.Count; l_addrow++)
        {
            exSheet.Range["A16"].Rows.EntireRow.Insert();//insert row new of sheet
        }
        //set date title of year
        //exSheet.Cells[2, 1].Value = "Income Statement Year" + " " + l_year;

        //set data to detail.
        // loop detail not percent
        int l_cnt = dt.Rows.Count;

        if (dt.Rows.Count > 0)
        {
            for (int l_row = 1; l_row <= dt.Rows.Count; l_row++)
            {
                for (int col = 0; col <= 6; col++)
                {
                    if (col == 1 || col == 2 || col == 3)
                    {
                        exSheet.Cells[15 + l_row, col + 1].Value = dt.Rows[l_row - 1][col].ToString();
                        exSheet.Cells[l_row + 15, col + 1].HorizontalAlignment = XlHAlign.xlHAlignLeft;
                        exSheet.Range[16, col + 1, l_row + 15, 7].Font.Bold    = false;
                    }
                    else
                    {
                        if (col == 6)
                        {
                            if (dt.Rows[l_row - 1][col].ToString() != "")
                            {
                                exSheet.Cells[15 + l_row, col + 1].Value = float.Parse(dt.Rows[l_row - 1][col].ToString());
                                exSheet.Cells[15, col + 1, l_row + 15, col + 1].NumberFormat = "#,##0.00";
                                exSheet.Cells[l_row + 15, col + 1].HorizontalAlignment       = XlHAlign.xlHAlignRight;
                            }
                        }
                        else
                        {
                            if (dt.Rows[l_row - 1][col].ToString() != "")
                            {
                                exSheet.Cells[15 + l_row, col + 1].Value = long.Parse(dt.Rows[l_row - 1][col].ToString());
                                exSheet.Cells[15, col + 1, l_row + 15, col + 1].NumberFormat = "#,##0";
                                exSheet.Cells[l_row + 15, col + 1].HorizontalAlignment       = XlHAlign.xlHAlignRight;
                            }
                        }
                        exSheet.Range[16, col + 1, l_row + 15, 7].Font.Bold = false;
                    }

                    //exSheet.Cells[l_row + 12, 1].HorizontalAlignment = XlHAlign.xlHAlignCenter;
                }
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        range = exSheet.Range["A1"];
        // hide row A5
        range.Rows.Hidden = true;

        // font bold header

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");
        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 5
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser("APP_DBUSER");

        string p_tco_org_pk          = Request["p1"].ToString();
        string p_thr_group_pk        = Request["p2"].ToString();
        string p_nation              = Request["p3"].ToString();
        string p_search_emp          = Request["p5"].ToString();
        string p_from_joindate       = Request["p6"].ToString();
        string p_to_joindate         = Request["p7"].ToString();
        string p_insurance_yn        = Request["p8"].ToString();
        string p_status              = Request["p9"].ToString();
        string p_from_leftdate       = Request["p10"].ToString();
        string p_to_leftdate         = Request["p11"].ToString();
        string p_social_book         = Request["p12"].ToString();
        string p_social_yn           = Request["p13"].ToString();
        string p_health_yn           = Request["p14"].ToString();
        string p_unemp_yn            = Request["p15"].ToString();
        string p_social_no           = Request["p16"].ToString();
        string p_from_begin_contract = Request["p17"].ToString();
        string p_to_begin_contract   = Request["p18"].ToString();

        string TemplateFile = "rpt_hrem00200_2.xls";
        string TempFile     = "../../../../system/temp/rpt_hrem00200_2_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];
        IRange     range;

        string SQL
            = "  SELECT to_char(sysdate,'dd/mm/yyyy'),'',d.org_nm, " +
              "         b.org_nm, " +
              "         c.workgroup_nm, " +
              "         a.EMP_ID,A.OLD_ID, " +
              "         a.FULL_NAME, " +
              "         TO_CHAR (TO_DATE (a.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy') JOIN_DT, " +
              "         DECODE(A.STATUS,'A','Active','Resign') status, " +
              "         TO_CHAR (TO_DATE (A.LEFT_DT, 'yyyymmdd'), 'dd/mm/yyyy') left_dt, " +
              "         SOCIAL_YN, " +
              "         SOCIAL_NO_OLD, " +
              "         SOCIAL_NO, " +
              "         (select CODE_NM from vhr_hr_code where id='HR0013' and code = A.SOCIAL_PLACE) social_place, " +
              "         TO_CHAR (TO_DATE (SOCIAL_DT, 'yyyymm'), 'mm/yyyy') social_dt, " +
              "         HEALTH_YN, " +
              "         a.HEALTH_NO, " +
              "         (select CODE_NM from vhr_hr_code where id='HR0012' and code = A.HEALTH_PLACE) HEALTH_PLACE, " +
              "         TO_CHAR (TO_DATE (HEALTH_DT, 'yyyymm'), 'mm/yyyy') health_dt, " +
              "         a.UNEMP_YN, " +
              "         TO_CHAR (TO_DATE (UNEMP_DT, 'yyyymm'), 'mm/yyyy') unemp_dt, " +
              "         (select CODE_NM from vhr_hr_code where id='HR0175' and code = NVL (a.social_book, '01')) social_book, " +
              "         a.BOOK_SUBMIT_DT, " +
              "         a.BOOK_RETURN_DT, " +
              "         a.MONTH_INS, " +
              "         nvl(a.insurance_sal,0), " +
              "         a.NOTE_INS_01, " +
              "         a.NOTE_INS_02 " +
              "    FROM thr_employee a, tco_org b, thr_work_group c, tco_org d " +
              "   WHERE     a.del_if = 0  and d.del_if =0 " +
              "         AND b.del_if = 0 " +
              "         AND c.del_if = 0 " +
              "         AND a.THR_wg_pk = c.pk " +
              "         AND a.TCO_org_PK = b.pk AND b.p_pk = d.pk  " +
              "         AND DECODE ('" + p_thr_group_pk + "', 'ALL', 'ALL', c.pk) = '" + p_thr_group_pk + "' " +
              "         AND DECODE ('" + p_nation + "', 'ALL', 'ALL', NVL (a.NATION, '01')) = '" + p_nation + "' " +
              "         AND ( '" + p_search_emp + "' is null " +
              "              OR (  a.emp_id  LIKE '%' || UPPER ('" + p_search_emp + "') || '%') " +
              "              OR (  a.old_id  LIKE '%' || UPPER ('" + p_search_emp + "') || '%') " +
              "              OR (  a.full_name  LIKE '%' || UPPER ('" + p_search_emp + "') || '%')) " +
              "         AND (   ('" + p_from_begin_contract + "' IS NULL AND '" + p_to_begin_contract + "' IS NULL) " +
              "              OR (a.begin_contract BETWEEN '" + p_from_begin_contract + "' " +
              "                                       AND '" + p_to_begin_contract + "')) " +
              "         AND (   ('" + p_from_joindate + "' IS NULL AND '" + p_to_joindate + "' IS NULL) " +
              "              OR (a.join_dt BETWEEN '" + p_from_joindate + "' AND '" + p_to_joindate + "')) " +
              "         AND DECODE ('" + p_insurance_yn + "', 'ALL', 'ALL', NVL (a.SOCIAL_YN, 'N')) = '" + p_insurance_yn + "' " +
              "         AND DECODE ('" + p_status + "', 'ALL', 'ALL', a.status) = '" + p_status + "' " +
              "         AND DECODE ('" + p_social_yn + "', 'ALL', 'ALL', a.social_yn) = '" + p_social_yn + "' " +
              "         AND DECODE ('" + p_health_yn + "', 'ALL', 'ALL', a.health_yn) = '" + p_health_yn + "' " +
              "         AND DECODE ('" + p_unemp_yn + "', 'ALL', 'ALL', a.unemp_yn) = '" + p_unemp_yn + "' " +
              "         AND (   ('" + p_social_no + "' = 'Y' AND a.social_no IS NOT NULL) " +
              "              OR ('" + p_social_no + "' = 'N' AND a.social_no IS NULL) " +
              "              OR ('" + p_social_no + "' = 'ALL' AND 1 = 1)) " +
              "         AND DECODE ('" + p_social_book + "', 'ALL', 'ALL', NVL (a.social_book, '0')) = '" + p_social_book + "' " +
              "         AND (   ('" + p_from_leftdate + "' IS NULL AND '" + p_to_leftdate + "' IS NULL) " +
              "              OR (a.LEFT_DT BETWEEN '" + p_from_leftdate + "' AND '" + p_to_leftdate + "')) " +
              "         AND (   a.tco_org_pk IN " +
              "                    (    SELECT g.pk " +
              "                           FROM tco_org g " +
              "                          WHERE g.del_if = 0 " +
              "                     START WITH g.pk = " +
              "                                   DECODE ('" + p_tco_org_pk + "', " +
              "                                           'ALL', 0, " +
              "                                           '" + p_tco_org_pk + "') " +
              "                     CONNECT BY PRIOR g.pk = g.p_pk) " +
              "              OR '" + p_tco_org_pk + "' = 'ALL') " +
              "ORDER BY d.org_nm,NVL (b.seq, 0),b.org_nm, NVL (c.seq, 0), A.EMP_ID ";

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);

        int irow = dt_emp.Rows.Count;
        int icol = dt_emp.Columns.Count;

        if (irow == 0)
        {
            Response.Write("There is no data.");
            Response.End();
        }

        int i, j;
        int pos = 7;

        //title
        exSheet.Cells["C2"].Value = Session["User_ID"].ToString();
        exSheet.Cells["C3"].Value = dt_emp.Rows[0][0].ToString();

        //Insert new row

        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[pos + 1, 1].Rows.EntireRow.Insert();
        }


        //Show data
        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[pos + i, 1].Value = i + 1;
            for (j = 2; j < icol; j++)
            {
                if (j == icol - 3)
                {
                    exSheet.Cells[pos + i, j].Value = double.Parse(dt_emp.Rows[i][j].ToString());
                }
                else
                {
                    exSheet.Cells[pos + i, j].Value = dt_emp.Rows[i][j].ToString();
                }
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string TemplateFile = "rpt_hrti01300_0.xls";
        string TempFile     = "../../../../system/temp/rpt_hrti01300_0_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet   exSheet = exBook.Worksheets[1];
        IRange       range;
        MemoryStream stream;
        Bitmap       b;

        string p_tco_dept_pk, p_thr_group_pk, p_status, p_emp_id, p_work_mon, p_from_dt, p_to_dt, p_dept_nm, p_salary_period, p_nation, p_lstEmpID;

        p_tco_dept_pk   = Request["l_tco_dept_pk"].ToString();
        p_thr_group_pk  = Request["l_thr_group_pk"].ToString();
        p_status        = Request["l_status"].ToString();
        p_emp_id        = Request["l_emp_id"].ToString();
        p_work_mon      = Request["l_work_mon"].ToString();
        p_to_dt         = Request["l_to_dt"].ToString();
        p_from_dt       = Request["l_from_dt"].ToString();
        p_dept_nm       = Request["l_dept_nm"].ToString();
        p_salary_period = Request["l_SalaryPeriod"].ToString();
        p_nation        = Request["l_nation"].ToString();
        p_lstEmpID      = Request["l_lstEmpID"].ToString();

        //-----------------information of company-----------------
        string SQL_Com
            = "select  a.PARTNER_LNAME,a.ADDR1,a.PHONE_NO,b.data,to_char(to_date('" + p_work_mon + "','yyyymm'),'MON-YYYY'),to_char(to_date('" + p_work_mon + "','yyyymm'),'mm/YYYY') " +
              "from tco_company a, tco_bpphoto b " +
              "where a.DEL_IF=0  and b.del_if(+) = 0 and a.TCO_BPPHOTO_PK = b.pk(+)" +
              "and a.pk in ( select tco_company_pk from  " +
              "               tco_org  f " +
              "               where  f.pk IN ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_tco_dept_pk + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_tco_dept_pk + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_tco_dept_pk + "' = 'ALL') " +
              "and rownum=1 ";

        DataTable dt_Com = CtlLib.TableReadOpen(SQL_Com);

        exSheet.Range["C1"].Value = dt_Com.Rows[0][0].ToString();
        exSheet.Range["C2"].Value = dt_Com.Rows[0][1].ToString();
        exSheet.Range["C3"].Value = dt_Com.Rows[0][2].ToString();
        exSheet.Range["T1"].Value = "WORKING TIME REPORT ON " + dt_Com.Rows[0][4].ToString();
        exSheet.Range["W3"].Value = "(BẢNG CÔNG THÁNG " + dt_Com.Rows[0][5].ToString() + ")";

        if (!string.IsNullOrEmpty(dt_Com.Rows[0][3].ToString()))
        {
            byte[] MyData = new byte[0];
            MyData = (byte[])dt_Com.Rows[0][3];
            stream = new MemoryStream(MyData);

            b = new Bitmap(stream);
            exSheet.Cells["B1"].Select();
            exSheet.Shapes.AddPicture(CtlLib.ResizeImage(b, 75, 40));
        }

        //-----------------working time-----------------

        string l_Emp_id = "";

        if (p_lstEmpID != "")
        {
            l_Emp_id = " and a.emp_id in ('" + p_lstEmpID + "')";
        }
        else
        {
            p_emp_id = p_emp_id.ToUpper();
            l_Emp_id = " and a.emp_id like '%" + p_emp_id + "%'";
        }

        string SQL = "";

        SQL = "select a.dept_NM,a.EMP_ID,a.full_name,to_char(to_date(a.JOIN_DT,'yyyymmdd'),'dd/mm/yyyy') JOIN_DT " +
              ",a.D1	,a.D2	,a.D3	,a.D4	,a.D5	,a.D6	,a.D7	,a.D8	,a.D9	,a.D10	 "+
              ",a.D11	,a.D12	,a.D13	,a.D14	,a.D15	,a.D16	,a.D17	,a.D18	,a.D19	,a.D20	 "+
              ",a.D21	,a.D22	,a.D23	,a.D24	,a.D25	,a.D26	,a.D27	,a.D28	,a.D29	,a.D30	,a.D31 "+
              ",decode(b.D1,0,'',b.D1) || decode(nvl(b.N1,0),0,'',' N' || b.N1)|| decode(nvl(b.NT2_1,0),0,'',' N+' || b.NT2_1) N1	 "+
              ",decode(b.D2,0,'',b.D2) || decode(nvl(b.N2,0),0,'',' N' || b.N2)|| decode(nvl(b.NT2_2,0),0,'',' N+' || b.NT2_2)	N2 "+
              ",decode(b.D3,0,'',b.D3) || decode(nvl(b.N3,0),0,'',' N' || b.N3)|| decode(nvl(b.NT2_3,0),0,'',' N+' || b.NT2_3)	N3 "+
              ",decode(b.D4,0,'',b.D4) || decode(nvl(b.N4,0),0,'',' N' || b.N4)|| decode(nvl(b.NT2_4,0),0,'',' N+' || b.NT2_4)	N4  "+
              ",decode(b.D5,0,'',b.D5) || decode(nvl(b.N5,0),0,'',' N' || b.N5)|| decode(nvl(b.NT2_5,0),0,'',' N+' || b.NT2_5)	 N5  "+
              ",decode(b.D6,0,'',b.D6) || decode(nvl(b.N6,0),0,'',' N' || b.N6)|| decode(nvl(b.NT2_6,0),0,'',' N+' || b.NT2_6)	N6  "+
              ",decode(b.D7,0,'',b.D7) || decode(nvl(b.N7,0),0,'',' N' || b.N7)|| decode(nvl(b.NT2_7,0),0,'',' N+' || b.NT2_7)	N7   "+
              ",decode(b.D8,0,'',b.D8) || decode(nvl(b.N8,0),0,'',' N' || b.N8)|| decode(nvl(b.NT2_8,0),0,'',' N+' || b.NT2_8)	N8   "+
              ",decode(b.D9,0,'',b.D9) || decode(nvl(b.N9,0),0,'',' N' || b.N9)|| decode(nvl(b.NT2_9,0),0,'',' N+' || b.NT2_9)	N9   "+
              ",decode(b.D10,0,'',b.D10) || decode(nvl(b.N10,0),0,'',' N' || b.N10)|| decode(nvl(b.NT2_10,0),0,'',' N+' || b.NT2_10)	N10  "+
              ",decode(b.D11,0,'',b.D11) || decode(nvl(b.N11,0),0,'',' N' || b.N11)|| decode(nvl(b.NT2_11,0),0,'',' N+' || b.NT2_11)	N11   "+
              ",decode(b.D12,0,'',b.D12) || decode(nvl(b.N12,0),0,'',' N' || b.N12)|| decode(nvl(b.NT2_12,0),0,'',' N+'|| b.NT2_12)	N12  " +
              ",decode(b.D13,0,'',b.D13) || decode(nvl(b.N13,0),0,'',' N' || b.N13)|| decode(nvl(b.NT2_13,0),0,'',' N+' || b.NT2_13)	N13  "+
              ",decode(b.D14,0,'',b.D14) || decode(nvl(b.N14,0),0,'',' N' || b.N14)|| decode(nvl(b.NT2_14,0),0,'',' N+' || b.NT2_14)	N14  "+
              ",decode(b.D15,0,'',b.D15) || decode(nvl(b.N15,0),0,'',' N' || b.N15)|| decode(nvl(b.NT2_15,0),0,'',' N+' || b.NT2_15)	N15  "+
              ",decode(b.D16,0,'',b.D16) || decode(nvl(b.N16,0),0,'',' N' || b.N16)|| decode(nvl(b.NT2_16,0),0,'',' N+' || b.NT2_16)	N16  "+
              ",decode(b.D17,0,'',b.D17) || decode(nvl(b.N17,0),0,'',' N' || b.N17)|| decode(nvl(b.NT2_17,0),0,'',' N+' || b.NT2_17)	N17  "+
              ",decode(b.D18,0,'',b.D18) || decode(nvl(b.N18,0),0,'',' N' || b.N18)|| decode(nvl(b.NT2_18,0),0,'',' N+' || b.NT2_18)	N18  "+
              ",decode(b.D19,0,'',b.D19) || decode(nvl(b.N19,0),0,'',' N' || b.N19)|| decode(nvl(b.NT2_19,0),0,'',' N+' || b.NT2_19)	N19  "+
              ",decode(b.D20,0,'',b.D20) || decode(nvl(b.N20,0),0,'',' N' || b.N20)|| decode(nvl(b.NT2_20,0),0,'',' N+' || b.NT2_20)	N20  "+
              ",decode(b.D21,0,'',b.D21) || decode(nvl(b.N21,0),0,'',' N' || b.N21)|| decode(nvl(b.NT2_21,0),0,'',' N+' || b.NT2_21)	N21  "+
              ",decode(b.D22,0,'',b.D22) || decode(nvl(b.N22,0),0,'',' N' || b.N22)|| decode(nvl(b.NT2_22,0),0,'',' N+' || b.NT2_22)	N22  "+
              ",decode(b.D23,0,'',b.D23) || decode(nvl(b.N23,0),0,'',' N' || b.N23)|| decode(nvl(b.NT2_23,0),0,'',' N+' || b.NT2_23)	N23  "+
              ",decode(b.D24,0,'',b.D24) || decode(nvl(b.N24,0),0,'',' N' || b.N24)|| decode(nvl(b.NT2_24,0),0,'',' N+' || b.NT2_24)	N24  "+
              ",decode(b.D25,0,'',b.D25) || decode(nvl(b.N25,0),0,'',' N' || b.N25)|| decode(nvl(b.NT2_25,0),0,'',' N+' || b.NT2_25)	N25 "+
              ",decode(b.D26,0,'',b.D26) || decode(nvl(b.N26,0),0,'',' N' || b.N26)|| decode(nvl(b.NT2_26,0),0,'',' N+' || b.NT2_26)	N26  "+
              ",decode(b.D27,0,'',b.D27) || decode(nvl(b.N27,0),0,'',' N' || b.N27)|| decode(nvl(b.NT2_27,0),0,'',' N+' || b.NT2_27)	N27  "+
              ",decode(b.D28,0,'',b.D28) || decode(nvl(b.N28,0),0,'',' N' || b.N28)|| decode(nvl(b.NT2_28,0),0,'',' N+' || b.NT2_28)	N28  "+
              ",decode(b.D29,0,'',b.D29) || decode(nvl(b.N29,0),0,'',' N' || b.N29)|| decode(nvl(b.NT2_29,0),0,'',' N+' || b.NT2_29)	N29  "+
              ",decode(b.D30,0,'',b.D30) || decode(nvl(b.N30,0),0,'',' N' || b.N30)|| decode(nvl(b.NT2_30,0),0,'',' N+' || b.NT2_30)	N30  "+
              ",decode(b.D31,0,'',b.D31) || decode(nvl(b.N31,0),0,'',' N' || b.N31)|| decode(nvl(b.NT2_31,0),0,'',' N+' || b.NT2_31)	N31  "+
              ",round(nvl(a.TOTAL_WD,0)/8,3) TOTAL_WD " +
              ",b.TOTAL_OT " +
              ",b.TOTAL_NT30 " +
              ",b.TOTAL_NT45 " +
              ",b.TOTAL_NT60 " +
              ",b.TOTAL_NT90 " +
              ",b.TOTAL_ST " +
              ",b.TOTAL_HT " +
              ",a.ale_have,a.total_ale_use,a.remain_ale,a.abs_ale " +
              "from thr_work_mon a,thr_extra_month b, thr_work_group c,thr_employee e " +
              "where a.del_if=0 and b.del_if=0 and c.del_if=0 and e.del_if=0 and c.pk=a.thr_group_pk " +
              "and a.thr_emp_pk=b.thr_emp_pk  and e.pk=a.thr_emp_pk " +
              "and a.work_mon = b.work_mon " +
              "and a.work_mon='" + p_work_mon + "' " +
              "and b.work_mon='" + p_work_mon + "' " +
              "and (a.tco_dept_pk in ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_tco_dept_pk + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_tco_dept_pk + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_tco_dept_pk + "' = 'ALL') " +
              "and decode('" + p_thr_group_pk + "','ALL','ALL',a.thr_group_pk)='" + p_thr_group_pk + "' " +
              l_Emp_id +
              "and decode('" + p_status + "','ALL','ALL',a.status)='" + p_status + "' " +
              " and decode('" + p_salary_period + "','ALL','ALL',nvl(a.sal_period,' '))='" + p_salary_period + "' " +
              " and decode('" + p_nation + "','ALL','ALL',nvl(e.nation,'01'))='" + p_nation + "' " +
              "order by a.dept_nm,a.emp_id ";

        // string para = "'" + p_work_mon + "','" + p_salary_period + "','" + p_lstEmpID + "'";

        //Response.Write(SQL);
        //  Response.End();

        DataTable dt_Emp = CtlLib.TableReadOpen(SQL);

        //DataTable dt_Emp = CtlLib.TableReadOpen(SQL);
        int irow_emp;

        irow_emp = dt_Emp.Rows.Count;
        if (irow_emp == 0)
        {
            Response.Write("There is no data of working time");
            Response.End();
        }

        int iDate_Num;

        //-----------------calendar-----------------
        string SQL_Cal
            = "select substr(t.car_date,-2),t.hol_type " +
              "from thr_calendar T,thr_close m " +
              "where t.del_if=0 and m.del_if=0 and m.id='" + p_salary_period + "' and m.month_close='" + p_work_mon + "' and m.tco_company_pk=t.tco_company_pk " +
              "   and m.tco_company_pk=(select tco_company_pk from tco_org g where g.del_if=0 and DECODE('" + p_tco_dept_pk + "','ALL','" + p_tco_dept_pk + "',G.PK)= '" + p_tco_dept_pk + "' ) " +
              "    and t.car_date between m.from_dt and '" + p_to_dt + "' order by t.car_date ";
        DataTable dt_Cal = CtlLib.TableReadOpen(SQL_Cal);

        iDate_Num = dt_Cal.Rows.Count;
        if (iDate_Num == 0)
        {
            Response.Write("There is no data of calendar");
            Response.End();
        }

        int row_emp = dt_Emp.Rows.Count;
        int row_cal = dt_Cal.Rows.Count;

        int s_col  = 7;
        int s_row  = 6;
        int tt_col = row_cal + 19;

        if (row_emp <= 0)
        {
            Response.Write("There is no data !!!");
            Response.End();
        }

        //header columns
        for (int row = 0; row < row_cal; row++)
        {
            exSheet.Range[4, s_col + row, 5, s_col + row].Value = double.Parse(dt_Cal.Rows[row][0].ToString());
        }

        s_row = 6;
        //insert new row
        for (int row = 1; row < row_emp; row++)
        {
            exSheet.Range["A8:AX9"].EntireRow.Insert();
        }

        if (row_emp > 1)
        {
            exSheet.Range["A6:AX7"].Rows.Copy(exSheet.Range["A" + (s_row + 2) + ":" + "AX" + (((row_emp - 1) * 2) + s_row - 1 + 2)], XlPasteType.xlPasteAll);
        }

        for (int row = 0; row < row_emp; row++)
        {
            exSheet.Range["A" + (s_row + row) + ":A" + (s_row + row + 1)].Value = row + 1;
            exSheet.Range["B" + (s_row + row) + ":B" + (s_row + row + 1)].Value = dt_Emp.Rows[row]["DEPT_NM"].ToString();
            exSheet.Range["C" + (s_row + row) + ":C" + (s_row + row + 1)].Value = dt_Emp.Rows[row]["EMP_ID"].ToString();
            exSheet.Range["D" + (s_row + row) + ":D" + (s_row + row + 1)].Value = dt_Emp.Rows[row]["FULL_NAME"].ToString();
            exSheet.Range["E" + (s_row + row) + ":E" + (s_row + row + 1)].Value = dt_Emp.Rows[row]["JOIN_DT"].ToString();

            GeneralData(exSheet, "1", "G", s_row + row, row, "D1", dt_Emp);
            GeneralData(exSheet, "1", "G", s_row + row + 1, row, "N1", dt_Emp);
            GeneralData(exSheet, "1", "H", s_row + row, row, "D2", dt_Emp);
            GeneralData(exSheet, "1", "H", s_row + row + 1, row, "N2", dt_Emp);
            GeneralData(exSheet, "1", "I", s_row + row, row, "D3", dt_Emp);
            GeneralData(exSheet, "1", "I", s_row + row + 1, row, "N3", dt_Emp);
            GeneralData(exSheet, "1", "J", s_row + row, row, "D4", dt_Emp);
            GeneralData(exSheet, "1", "J", s_row + row + 1, row, "N4", dt_Emp);
            GeneralData(exSheet, "1", "K", s_row + row, row, "D5", dt_Emp);
            GeneralData(exSheet, "1", "K", s_row + row + 1, row, "N5", dt_Emp);
            GeneralData(exSheet, "1", "L", s_row + row, row, "D6", dt_Emp);
            GeneralData(exSheet, "1", "L", s_row + row + 1, row, "N6", dt_Emp);
            GeneralData(exSheet, "1", "M", s_row + row, row, "D7", dt_Emp);
            GeneralData(exSheet, "1", "M", s_row + row + 1, row, "N7", dt_Emp);
            GeneralData(exSheet, "1", "N", s_row + row, row, "D8", dt_Emp);
            GeneralData(exSheet, "1", "N", s_row + row + 1, row, "N8", dt_Emp);
            GeneralData(exSheet, "1", "O", s_row + row, row, "D9", dt_Emp);
            GeneralData(exSheet, "1", "O", s_row + row + 1, row, "N9", dt_Emp);
            GeneralData(exSheet, "1", "P", s_row + row, row, "D10", dt_Emp);
            GeneralData(exSheet, "1", "P", s_row + row + 1, row, "N10", dt_Emp);

            GeneralData(exSheet, "1", "Q", s_row + row, row, "D11", dt_Emp);
            GeneralData(exSheet, "1", "Q", s_row + row + 1, row, "N11", dt_Emp);
            GeneralData(exSheet, "1", "R", s_row + row, row, "D12", dt_Emp);
            GeneralData(exSheet, "1", "R", s_row + row + 1, row, "N12", dt_Emp);
            GeneralData(exSheet, "1", "S", s_row + row, row, "D13", dt_Emp);
            GeneralData(exSheet, "1", "S", s_row + row + 1, row, "N13", dt_Emp);
            GeneralData(exSheet, "1", "T", s_row + row, row, "D14", dt_Emp);
            GeneralData(exSheet, "1", "T", s_row + row + 1, row, "N14", dt_Emp);
            GeneralData(exSheet, "1", "U", s_row + row, row, "D15", dt_Emp);
            GeneralData(exSheet, "1", "U", s_row + row + 1, row, "N15", dt_Emp);
            GeneralData(exSheet, "1", "V", s_row + row, row, "D16", dt_Emp);
            GeneralData(exSheet, "1", "V", s_row + row + 1, row, "N16", dt_Emp);
            GeneralData(exSheet, "1", "W", s_row + row, row, "D17", dt_Emp);
            GeneralData(exSheet, "1", "W", s_row + row + 1, row, "N17", dt_Emp);
            GeneralData(exSheet, "1", "X", s_row + row, row, "D18", dt_Emp);
            GeneralData(exSheet, "1", "X", s_row + row + 1, row, "N18", dt_Emp);
            GeneralData(exSheet, "1", "Y", s_row + row, row, "D19", dt_Emp);
            GeneralData(exSheet, "1", "Y", s_row + row + 1, row, "N19", dt_Emp);
            GeneralData(exSheet, "1", "Z", s_row + row, row, "D20", dt_Emp);
            GeneralData(exSheet, "1", "Z", s_row + row + 1, row, "N20", dt_Emp);

            GeneralData(exSheet, "1", "AA", s_row + row, row, "D21", dt_Emp);
            GeneralData(exSheet, "1", "AA", s_row + row + 1, row, "N21", dt_Emp);
            GeneralData(exSheet, "1", "AB", s_row + row, row, "D22", dt_Emp);
            GeneralData(exSheet, "1", "AB", s_row + row + 1, row, "N22", dt_Emp);
            GeneralData(exSheet, "1", "AC", s_row + row, row, "D23", dt_Emp);
            GeneralData(exSheet, "1", "AC", s_row + row + 1, row, "N23", dt_Emp);
            GeneralData(exSheet, "1", "AD", s_row + row, row, "D24", dt_Emp);
            GeneralData(exSheet, "1", "AD", s_row + row + 1, row, "N24", dt_Emp);
            GeneralData(exSheet, "1", "AE", s_row + row, row, "D25", dt_Emp);
            GeneralData(exSheet, "1", "AE", s_row + row + 1, row, "N25", dt_Emp);
            GeneralData(exSheet, "1", "AF", s_row + row, row, "D26", dt_Emp);
            GeneralData(exSheet, "1", "AF", s_row + row + 1, row, "N26", dt_Emp);
            GeneralData(exSheet, "1", "AG", s_row + row, row, "D27", dt_Emp);
            GeneralData(exSheet, "1", "AG", s_row + row + 1, row, "N27", dt_Emp);
            GeneralData(exSheet, "1", "AH", s_row + row, row, "D28", dt_Emp);
            GeneralData(exSheet, "1", "AH", s_row + row + 1, row, "N28", dt_Emp);
            GeneralData(exSheet, "1", "AI", s_row + row, row, "D29", dt_Emp);
            GeneralData(exSheet, "1", "AI", s_row + row + 1, row, "N29", dt_Emp);
            GeneralData(exSheet, "1", "AJ", s_row + row, row, "D30", dt_Emp);
            GeneralData(exSheet, "1", "AJ", s_row + row + 1, row, "N30", dt_Emp);

            GeneralData(exSheet, "1", "AK", s_row + row, row, "D31", dt_Emp);
            GeneralData(exSheet, "1", "AK", s_row + row + 1, row, "N31", dt_Emp);

            GeneralData(exSheet, "2", "AL", s_row + row, row, "TOTAL_WD", dt_Emp);
            GeneralData(exSheet, "2", "AM", s_row + row, row, "TOTAL_OT", dt_Emp);
            GeneralData(exSheet, "2", "AN", s_row + row, row, "TOTAL_NT30", dt_Emp);
            GeneralData(exSheet, "2", "AO", s_row + row, row, "TOTAL_NT45", dt_Emp);
            GeneralData(exSheet, "2", "AP", s_row + row, row, "TOTAL_NT60", dt_Emp);
            GeneralData(exSheet, "2", "AQ", s_row + row, row, "TOTAL_NT90", dt_Emp);
            GeneralData(exSheet, "2", "AR", s_row + row, row, "TOTAL_HT", dt_Emp);
            GeneralData(exSheet, "2", "AS", s_row + row, row, "TOTAL_ST", dt_Emp);

            GeneralData(exSheet, "2", "AT", s_row + row, row, "ale_have", dt_Emp);
            GeneralData(exSheet, "2", "AU", s_row + row, row, "total_ale_use", dt_Emp);
            GeneralData(exSheet, "2", "AV", s_row + row, row, "remain_ale", dt_Emp);
            GeneralData(exSheet, "2", "AW", s_row + row, row, "ABS_ALE", dt_Emp);


            s_row++;
        }

        // draw color
        s_row = 6;
        int temp = 0;

        for (int row = 0; row < row_cal; row++)
        {
            temp = 31 - row_cal;
            switch (temp)
            {
            case 3:
                exSheet.Range["AI1"].Columns.Hidden = true;
                break;

            case 2:
                exSheet.Range["AJ1"].Columns.Hidden = true;
                break;

            case 1:
                exSheet.Range["AK1"].Columns.Hidden = true;
                break;
            }

            switch (dt_Cal.Rows[row][1].ToString())
            {
            case "SUN":
                exSheet.Range[s_row, row + s_col, ((row_emp * 2) + s_row - 1), row + s_col].Interior.Color = Color.SkyBlue;
                break;

            case "HOL":
                exSheet.Range[s_row, row + s_col, ((row_emp * 2) + s_row - 1), row + s_col].Interior.Color = Color.Pink;
                break;
            }
        }



        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string l_wgroup        = Request.QueryString["l_wgroup"].ToString();
        string l_bn_times      = Request.QueryString["l_bn_times"].ToString();
        string l_tco_org       = Request.QueryString["l_tco_org"].ToString();
        string l_year          = Request.QueryString["l_year"].ToString();
        string l_emp_type      = Request.QueryString["l_emp_type"].ToString();
        string l_nation        = Request.QueryString["l_nation"].ToString();
        string l_contract      = Request.QueryString["l_contract"].ToString();
        string p_input         = Request.QueryString["l_Temp"].ToString();
        string l_j_from        = Request.QueryString["l_j_from"].ToString();
        string l_j_to          = Request.QueryString["l_j_to"].ToString();
        string l_l_from        = Request.QueryString["l_l_from"].ToString();
        string l_l_to          = Request.QueryString["l_l_to"].ToString();
        string l_type          = Request.QueryString["l_type"].ToString();
        string l_Senority_from = Request.QueryString["l_Senority_from"].ToString();
        string l_Senority_to   = Request.QueryString["l_Senority_to"].ToString();

        string TemplateFile = "rpt_hrpm00900_0.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00900_0_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        string para = "'" + l_wgroup + "','" + l_bn_times + "','" + l_tco_org + "','" + l_year + "','" + l_emp_type + "','" + l_nation + "','" + l_contract + "','";

        para = para + p_input + "','" + l_j_from + "','" + l_j_to + "','" + l_l_from + "','" + l_l_to + "','" + l_type + "','" + l_Senority_from + "','" + l_Senority_to + "'";
        DataTable dt_emp = CtlLib.TableReadOpenCursor("ST_HR_rpt_hrpm00900_0", para);

        if (dt_emp.Rows.Count == 0)
        {
            Response.Write("There is no data of bonus amount.");
            Response.End();
        }

        string SQL
            = "select A.REMARK " +
              "from thr_bonus_master a " +
              "where a.del_if = 0 " +
              "and A.TIMES = '" + l_bn_times + "' " +
              "and A.YEAR = '" + l_year + "' ";

        DataTable dt_remark = CtlLib.TableReadOpen(SQL);

        //Khai bao bien
        int pos = 5;
        int i, j, n_dept = 0, emp_dept = 0;

        Double[] subtotal = new Double[2] {
            0, 0
        };
        Double[] grandtotal = new Double[2] {
            0, 0
        };
        //Insert new row
        for (i = 0; i < dt_emp.Rows.Count - 1; i++)
        {
            if (dt_emp.Rows[i][1].ToString() != dt_emp.Rows[i + 1][1].ToString()) //khac bo phan
            {
                n_dept++;
            }
        }

        for (i = 0; i < dt_emp.Rows.Count + n_dept - 1; i++)
        {
            exSheet.Range[pos + 1 + i, 1].Rows.EntireRow.Insert();
        }

        exSheet.Cells[2, 5].Value = "Bonus Year: " + l_year;
        exSheet.Cells[2, 7].Value = "Times: " + l_bn_times + " - " + dt_remark.Rows[0][0].ToString();
        //Show data
        for (i = 0; i < dt_emp.Rows.Count; i++)
        {
            subtotal[0]   = subtotal[0] + Double.Parse(dt_emp.Rows[i][6].ToString());
            subtotal[1]   = subtotal[1] + Double.Parse(dt_emp.Rows[i][8].ToString());
            grandtotal[0] = grandtotal[0] + Double.Parse(dt_emp.Rows[i][6].ToString());
            grandtotal[1] = grandtotal[1] + Double.Parse(dt_emp.Rows[i][8].ToString());
            emp_dept++;
            exSheet.Cells[pos + i, 1].Value = emp_dept;
            for (j = 1; j <= 8; j++)
            {
                if (j >= 6)
                {
                    exSheet.Cells[pos + i, j + 1].Value = Double.Parse(dt_emp.Rows[i][j].ToString());
                }
                else
                {
                    exSheet.Cells[pos + i, j + 1].Value = dt_emp.Rows[i][j].ToString();
                }
            }

            if (i != dt_emp.Rows.Count - 1 && dt_emp.Rows[i][1].ToString() != dt_emp.Rows[i + 1][1].ToString()) //khac bo phan
            {
                exSheet.Cells[pos + i + 1, 2, pos + i + 1, 5].Merge();
                exSheet.Cells[pos + i + 1, 2].Value = "Sub Total " + dt_emp.Rows[i][1].ToString() + " - " + emp_dept + " employee(s)";
                exSheet.Cells[pos + i + 1, 2, pos + i + 1, 9].Rows.Font.Bold = true;
                exSheet.Cells[pos + i + 1, 2].HorizontalAlignment            = XlHAlign.xlHAlignCenter;
                exSheet.Cells[pos + i + 1, 7].Value = subtotal[0];
                exSheet.Cells[pos + i + 1, 9].Value = subtotal[1];
                //Insert page break
                IPageBreaks pbs       = exBook.Worksheets[1].HPageBreaks;
                IRange      usedrange = exBook.Worksheets[1].UsedRange;
                //pbs.Reset();
                pbs.Add(usedrange.Rows[pos + i + 2]);
                subtotal[0] = 0;
                subtotal[1] = 0;
                pos         = pos + 1;
                emp_dept    = 0;
            }
            else if (i == dt_emp.Rows.Count - 1)
            {
                exSheet.Cells[pos + i + 1, 2].Value = "Sub Total " + dt_emp.Rows[i][1].ToString() + " - " + emp_dept + " employee(s)";
                exSheet.Cells[pos + i + 2, 1].Value = "Grand Total - " + dt_emp.Rows.Count + " employee(s)";

                exSheet.Cells[pos + i + 1, 7].Value = subtotal[0];
                exSheet.Cells[pos + i + 1, 9].Value = subtotal[1];

                exSheet.Cells[pos + i + 2, 7].Value = grandtotal[0];
                exSheet.Cells[pos + i + 2, 9].Value = grandtotal[1];
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //CtlLib.SetUser("hr");
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_user, p_tco_org_pk, p_thr_group_pk, p_status, p_lday, p_ale, p_month, p_input, p_nation, p_from, p_to, p_lang;
        int    p_sql;

        p_user         = Request["p_user"].ToString();
        p_thr_group_pk = Request["p_thr_group_pk"].ToString();
        p_status       = Request["p_status"].ToString();
        p_lday         = Request["p_lday"].ToString();
        p_ale          = Request["p_ale"].ToString();
        p_month        = Request["p_month"].ToString();
        p_input        = Request["p_input"].ToString();
        p_nation       = Request["p_nation"].ToString();
        p_tco_org_pk   = Request["p_tco_org_pk"].ToString();
        p_from         = Request["p_from"].ToString();
        p_to           = Request["p_to"].ToString();
        p_lang         = Request["p_lang"].ToString();

        string p_left_from = Request["p_left_from"].ToString();
        string p_left_to   = Request["p_left_to"].ToString();

        p_sql = int.Parse(Request["sql"]);
        string SQL2   = "select 1 from  vhr_hr_code where id='HR0122' and code='02'";
        int    option = 0;
        string SQL    = "";

        string TemplateFile;

        if (p_lang == "ENG")
        {
            TemplateFile = "rpt_hrti01200_0_eng.xls";
        }
        else
        {
            TemplateFile = "rpt_hrti01200_0_vie.xls";
        }

        string TempFile = "../../../../system/temp/rpt_hrti01200_0_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;
        string para = "'" + p_tco_org_pk + "','" + p_thr_group_pk + "','" + p_status + "','" + p_lday + "','" + p_ale
                      + "','" + p_month + "','" + p_input + "','" + p_nation + "','" + p_from + "','" + p_to + "','" + p_sql + "'";

        if (p_sql == 1)
        {
            SQL
                = "SELECT    " +
                  "               al.dept_nm, al.group_nm, al.emp_id, al.full_nm, " +
                  "               TO_CHAR (TO_DATE (al.join_date, 'YYYYMMDD'), 'DD/MM/YYYY') " +
                  "               ,nvl(al.total_unpay,0) as ale_last_year " +
                  "               ,nvl(al.annual_leave_days_in_year,0) " +
                  "           ,nvl(al.ale_plus,0) " +
                  " ,case when al.Left_dt is null or substr(nvl(al.left_dt,'30000101'),1,6)> '" + p_month + "' then  " +
                  "            HR_F_CAL_ALE('" + p_month + "',AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "            else   HR_F_CAL_ALE_STOP(al.Left_dt,AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) end   AS al_real " +
                  "               ,nvl(yearsum,0) AS used, " +
                  "               case when al.Left_dt is null or substr(nvl(al.left_dt,'30000101'),1,6)>'" + p_month + "' then  " +
                  "            HR_F_CAL_ALE('" + p_month + "',AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "            else    HR_F_CAL_ALE_STOP(al.Left_dt,AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk)  end   " +
                  "       - NVL(yearsum,0)+nvl(al.total_unpay,0)  AS UNUSED, " +
                  "      case when al.Left_dt is null or substr(nvl(al.left_dt,'30000101'),1,6)> SUBSTR('" + p_month + "',1,4)||'12' then  " +
                  "    HR_F_CAL_ALE(SUBSTR('" + p_month + "',1,4)||'12',AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "     else       HR_F_CAL_ALE_STOP(al.Left_dt,AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "    end - NVL(yearsum,0)+nvl(al.total_unpay,0) AS unused_in_yeaar, " +
                  "               0 as pay, " +
                  "   case when al.Left_dt is null or substr(nvl(al.left_dt,'30000101'),1,6)>'" + p_month + "' then  " +
                  "            HR_F_CAL_ALE('" + p_month + "',AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "          else  HR_F_CAL_ALE_STOP(al.Left_dt,AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "     end   - NVL(yearsum,0)+nvl(al.total_unpay,0) AS UNPAY,                " +
                  "               decode(mon01,null,0,mon01), decode(mon02,null,0,mon02), " +
                  "               decode(mon03,null,0,mon03), decode(mon04,null,0,mon04), " +
                  "               decode(mon05,null,0,mon05), decode(mon06,null,0,mon06), " +
                  "               decode(mon07,null,0,mon07), decode(mon08,null,0,mon08), " +
                  "               decode(mon09,null,0,mon09), decode(mon10,null,0,mon10), " +
                  "               decode(mon11,null,0,mon11), decode(mon12,null,0,mon12) ,to_char(sysdate,'dd/mm/YYYY') " +
                  "               FROM (SELECT b.pk AS group_pk, b.workgroup_nm AS group_nm, " +
                  "                       a.emp_id AS emp_id, a.full_name AS full_nm, " +
                  "                       a.ale_day_in_year AS annual_leave_days_in_year, a.ale_plus as ale_plus " +
                  "                       ,a.join_dt AS join_date, a.pk AS emp_pk " +
                  "                       ,c.UNUSED as ale_unused " +
                  "                       ,c.ale_pay as ale_pay,a.al_pay_yn as al_pay_yn,c.total_unpay as total_unpay " +
                  "                       ,decode(nvl(a.AL_PAY_YN,'N'),'Y','-1','0') as pay " +
                  "                       ,a.LEFT_DT,d.org_nm as dept_nm " +
                  "                  FROM thr_employee a, thr_work_group b,thr_annual_leave c,tco_org d " +
                  "            WHERE a.del_if = 0 and (a.LEFT_DT is null or substr(nvl(a.LEFT_DT,'30001010'),1,4) >= substr('" + p_month + "',1,4) ) " +
                  "                   AND b.del_if = 0 and d.del_if=0 and d.pk=a.tco_org_pk " +
                  "                   AND c.del_if(+)=0  " +
                  "                   and a.pk=c.thr_emp_pk(+) " +
                  "                   and c.an_year(+)=TO_CHAR(ADD_MONTHS(TO_DATE('" + p_month + "','YYYYMM'),-12),'YYYY')  " +
                  "                   AND a.thr_wg_pk = b.pk " +
                  "                   AND (DECODE ('" + p_status + "', 'ALL', '" + p_status + "', case when a.LEFT_DT is null or substr(a.LEFT_DT,1,6)>='" + p_month + "' then 'A'else 'R' end ) ='" + p_status + "') " +
                  "           AND (a.left_dt is not null and  to_date(a.left_dt,'yyyymmdd') between to_date('" + p_left_from + "','yyyymmdd') and to_date('" + p_left_to + "','yyyymmdd')  " +
                  "             or '" + p_left_from + "' is null or '" + p_left_to + "' is null) " +

                  "                   AND (DECODE ('" + p_nation + "','ALL', '" + p_nation + "',a.nation) = '" + p_nation + "') " +
                  "                   AND (DECODE ('" + p_thr_group_pk + "','ALL', '" + p_thr_group_pk + "',a.thr_wg_pk) = '" + p_thr_group_pk + "') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "                   AND (a.tco_org_pk IN ( " +
                  "                              SELECT     g.pk " +
                  "                                    FROM TCO_ORG g " +
                  "                                   WHERE g.del_if = 0 " +
                  "                              START WITH g.pk =DECODE ('" + p_tco_org_pk + "','ALL', 0,'" + p_tco_org_pk + "') " +
                  "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
                  "                        OR '" + p_tco_org_pk + "' = 'ALL' " +
                  "                       )) al, " +
                  " (SELECT   e.emp_id emp_id, e.pk emp_pk, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'01', round(a.ale_hours/8,2),0)) mon01, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'02', round(a.ale_hours/8,2),0)) mon02, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'03', round(a.ale_hours/8,2),0)) mon03, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'04', round(a.ale_hours/8,2),0)) mon04, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'05', round(a.ale_hours/8,2),0)) mon05, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'06', round(a.ale_hours/8,2),0)) mon06, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'07', round(a.ale_hours/8,2),0)) mon07, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'08', round(a.ale_hours/8,2),0)) mon08, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'09', round(a.ale_hours/8,2),0)) mon09, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'10', round(a.ale_hours/8,2),0)) mon10, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'11', round(a.ale_hours/8,2),0)) mon11, " +
                  "                         SUM (DECODE (SUBSTR (a.work_mon, 5, 2),'12', round(a.ale_hours/8,2),0)) mon12, " +
                  "                         round(SUM (nvl(a.ale_hours,0)/8),2) yearsum " +
                  "                    FROM thr_current_ale a,thr_employee e " +
                  "                   WHERE a.del_if=0 and e.del_if=0 and a.thr_emp_pk=e.pk  " +
                  "                    and a.work_mon <= '" + p_month + "' " +
                  "                     AND SUBSTR (a.work_mon, 1, 4) = SUBSTR ('" + p_month + "', 1, 4) " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(E.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(E.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(E.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "                     AND e.TCO_ORG_PK IN ( " +
                  "                              SELECT     G.PK " +
                  "                                    FROM TCO_ORG G " +
                  "                                   WHERE G.DEL_IF = 0 " +
                  "                              START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                              CONNECT BY PRIOR G.PK = G.P_PK) " +
                  "                GROUP BY e.emp_id, e.pk) alsum " +

/*
 *               "               (SELECT   a.emp_id emp_id, a.thr_emp_pk emp_pk, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'01', a.mon_cnt,0)) mon01, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'02', a.mon_cnt,0)) mon02, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'03', a.mon_cnt,0)) mon03, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'04', a.mon_cnt,0)) mon04, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'05', a.mon_cnt,0)) mon05, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'06', a.mon_cnt,0)) mon06, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'07', a.mon_cnt,0)) mon07, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'08', a.mon_cnt,0)) mon08, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'09', a.mon_cnt,0)) mon09, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'10', a.mon_cnt,0)) mon10, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'11', a.mon_cnt,0)) mon11, " +
 *               "                         SUM (DECODE (SUBSTR (a.yearmon, 5, 2),'12', a.mon_cnt,0)) mon12, " +
 *               "                         SUM (a.mon_cnt) yearsum " +
 *               "                    FROM vhr_annual_leave a " +
 *               "                   WHERE a.yearmon <= '" + p_month + "' " +
 *               "                     AND SUBSTR (a.yearmon, 1, 4) = SUBSTR ('" + p_month + "', 1, 4) " +
 *               "                     AND (DECODE ('" + p_search + "','1', UPPER (a.emp_id),'2', a.id_num,'3', UPPER (a.full_name)) LIKE '%' || UPPER ('" + p_input + "') " +
 *               "                          OR '" + p_input + "' IS NULL " +
 *               "                         ) " +
 *               "                     AND (   a.tco_org_pk IN ( " +
 *               "                                SELECT     g.pk " +
 *               "                                      FROM TCO_ORG g " +
 *               "                                     WHERE g.del_if = 0 " +
 *               "                                START WITH g.pk =DECODE ('" + p_tco_org_pk + "','ALL', 0,'" + p_tco_org_pk + "') " +
 *               "                                CONNECT BY PRIOR g.pk = g.p_pk) " +
 *               "                          OR '" + p_tco_org_pk + "' = 'ALL' " +
 *               "                         ) " +
 *               "                GROUP BY a.emp_id, a.thr_emp_pk) alsum " +
 */
                  "         WHERE al.emp_pk = alsum.emp_pk(+) " +
                  "           AND (   ( case when al.Left_dt is null or substr(nvl(al.left_dt,'30000101'),1,6)>='" + p_month + "' then  " +
                  "           HR_F_CAL_ALE('" + p_month + "',AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "            else   HR_F_CAL_ALE_STOP(al.Left_dt,AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "          end   -NVL (yearsum,0)+nvl(al.total_unpay,0)>= '" + p_ale + "' " +
                  "           AND '" + p_lday + "' = '1'   ) " +
                  "                OR ( case when al.Left_dt is null or substr(nvl(al.left_dt,'30000101'),1,6)>='" + p_month + "' then  " +
                  "            HR_F_CAL_ALE('" + p_month + "',AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "            else   HR_F_CAL_ALE_STOP(al.Left_dt,AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "             end    - NVL (yearsum, 0)+nvl(al.total_unpay,0) = '" + p_ale + "' " +
                  "            AND '" + p_lday + "' = '2' ) " +
                  "                OR ( case when al.Left_dt is null or substr(nvl(al.left_dt,'30000101'),1,6)>='" + p_month + "' then  " +
                  "            HR_F_CAL_ALE('" + p_month + "',AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "            else     HR_F_CAL_ALE_STOP(al.Left_dt,AL.JOIN_DATE,NVL(AL.ANNUAL_LEAVE_DAYS_IN_YEAR,0),NVL(AL.ALE_PLUS,0),al.emp_pk) " +
                  "                end   - NVL (yearsum, 0)+nvl(al.total_unpay,0) <= '" + p_ale + "' " +
                  "           AND '" + p_lday + "' = '3' " +
                  "          ) " +
                  "                OR '" + p_ale + "' IS NULL " +
                  "               ) " +
                  "           AND al.join_date <=TO_CHAR (LAST_DAY (TO_DATE ('" + p_month + "', 'YYYYMM')), 'YYYYMMDD') " +
                  "           AND (to_date(al.join_date,'yyyymmdd') between to_date('" + p_from + "','yyyymmdd') and to_date('" + p_to + "','yyyymmdd')  " +
                  "             or '" + p_from + "' is null or '" + p_to + "' is null) " +

                  "      ORDER BY al.dept_nm, al.group_nm, al.emp_id ";
        }



        else if (p_sql == 2)
        {
            SQL
                = " SELECT  A.org_nm,A.GROUP_NM,A.EMP_ID,A.FULL_NAME " +
                  "        ,TO_CHAR(TO_DATE(A.JOIN_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "        ,nvl(A.ALE_LAST_YEAR,0) " +
                  "        ,nvl(A.ALE_IN_YEAR,0) " +
                  "        ,nvl(A.ALE_PLUS,0) " +
                  "        ,nvl(A.ALE_REAL,0) " +
                  "        ,nvl(A.USED,0) " +
                  "        ,nvl(A.UNUSED,0)  " +
                  "        ,nvl(A.UNUSED,0)        " +
                  "        ,case when nvl(A.EMPLOYEE_PAY,0) >0 then -1* nvl(A.EMPLOYEE_PAY,0) else nvl(A.ALE_PAY,0) end " +
                  "        ,nvl(A.ALE_UNPAY,0) " +
                  "        ,nvl(DECODE(A.JAN,0,NULL,A.JAN),0),nvl(DECODE(A.FEB,0,NULL,A.FEB),0) " +
                  "        ,nvl(DECODE(A.MAR,0,NULL,A.MAR),0),nvl(DECODE(A.APR,0,NULL,A.APR),0) " +
                  "        ,nvl(DECODE(A.MAY,0,NULL,A.MAY),0),nvl(DECODE(A.JUN,0,NULL,A.JUN),0) " +
                  "        ,nvl(DECODE(A.JUL,0,NULL,A.JUL),0),nvl(DECODE(A.AUG,0,NULL,A.AUG),0) " +
                  "        ,nvl(DECODE(A.SEP,0,NULL,A.SEP),0),nvl(DECODE(A.OCT,0,NULL,A.OCT),0) " +
                  "        ,nvl(DECODE(A.NOV,0,NULL,A.NOV),0),nvl(DECODE(A.DEC,0,NULL,A.DEC),0) " +
                  "      ,to_char(sysdate,'dd/mm/YYYY') " +
                  "  FROM THR_ANNUAL_LEAVE A,THR_ANNUAL_LEAVE B,THR_EMPLOYEE C,tco_org d " +
                  "  WHERE A.DEL_IF=0 AND B.DEL_IF(+)=0 AND C.DEL_IF=0 AND A.THR_EMP_PK=C.PK and d.del_if=0 and a.TCO_org_PK=d.pk  " +
                  "    AND (DECODE ('" + p_thr_group_pk + "','ALL', '" + p_thr_group_pk + "',a.thr_Group_pk) = '" + p_thr_group_pk + "') " +
                  "   AND B.AN_YEAR(+)=TO_CHAR(ADD_MONTHS(TO_DATE('" + p_month + "','YYYYMM'),-12),'YYYY') " +
                  "   AND A.THR_EMP_PK=B.THR_EMP_PK(+) " +
                  "   AND A.AN_YEAR=SUBSTR('" + p_month + "',1,4) " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  " AND (   a.tco_org_pk IN ( " +
                  "                              SELECT     g.pk " +
                  "                                    FROM TCO_ORG g " +
                  "                                   WHERE g.del_if = 0 " +
                  "                              START WITH g.pk =DECODE ('" + p_tco_org_pk + "','ALL', 0,'" + p_tco_org_pk + "') " +
                  "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
                  "                        OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "  AND (( a.ale_unpay >='" + p_ale + "' AND '" + p_lday + "'='1') " +
                  "    OR (a.ale_unpay='" + p_ale + "' AND '" + p_lday + "'='2') " +
                  "    OR (a.ale_unpay<='" + p_ale + "' AND '" + p_lday + "'='3') " +
                  "    OR '" + p_ale + "' IS NULL) " +
                  "    AND (DECODE ('" + p_status + "', 'ALL', '" + p_status + "', a.status) ='" + p_status + "') " +
                  "    AND (DECODE ('" + p_nation + "', 'ALL', '" + p_nation + "', c.nation) ='" + p_nation + "') " +
                  "           AND (to_date(c.join_dt,'yyyymmdd') between to_date('" + p_from + "','yyyymmdd') and to_date('" + p_to + "','yyyymmdd')  " +
                  "             or '" + p_from + "' is null or '" + p_to + "' is null) " +
                  "           AND (c.left_dt is not null and  to_date(c.left_dt,'yyyymmdd') between to_date('" + p_left_from + "','yyyymmdd') and to_date('" + p_left_to + "','yyyymmdd')  " +
                  "             or '" + p_left_from + "' is null or '" + p_left_to + "' is null) " +

                  " ORDER BY  A.org_nm,A.GROUP_NM,A.EMP_ID ";
        }
        else
        {
            SQL
                = " SELECT  A.org_nm,A.GROUP_NM,A.EMP_ID,A.FULL_NAME " +
                  "        ,TO_CHAR(TO_DATE(A.JOIN_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "        ,nvl(A.ALE_LAST_YEAR,0) " +
                  "        ,nvl(A.ALE_IN_YEAR,0) " +
                  "        ,nvl(A.ALE_PLUS,0) " +
                  "        ,nvl(A.ALE_REAL,0) " +
                  "        ,nvl(A.USED,0) " +
                  "        ,nvl(A.UNUSED,0)  " +
                  "        ,nvl(A.UNUSED,0)        " +
                  "        ,case when nvl(A.EMPLOYEE_PAY,0) >0 then -1* nvl(A.EMPLOYEE_PAY,0) else nvl(A.ALE_PAY,0) end " +
                  "        ,nvl(A.ALE_UNPAY,0) " +
                  "        ,nvl(DECODE(A.JAN,0,NULL,A.JAN),0),nvl(DECODE(A.FEB,0,NULL,A.FEB),0) " +
                  "        ,nvl(DECODE(A.MAR,0,NULL,A.MAR),0),nvl(DECODE(A.APR,0,NULL,A.APR),0) " +
                  "        ,nvl(DECODE(A.MAY,0,NULL,A.MAY),0),nvl(DECODE(A.JUN,0,NULL,A.JUN),0) " +
                  "        ,nvl(DECODE(A.JUL,0,NULL,A.JUL),0),nvl(DECODE(A.AUG,0,NULL,A.AUG),0) " +
                  "        ,nvl(DECODE(A.SEP,0,NULL,A.SEP),0),nvl(DECODE(A.OCT,0,NULL,A.OCT),0) " +
                  "        ,nvl(DECODE(A.NOV,0,NULL,A.NOV),0),nvl(DECODE(A.DEC,0,NULL,A.DEC),0) " +
                  "      ,to_char(sysdate,'dd/mm/YYYY') " +
                  "  FROM THR_ANNUAL_LEAVE A,THR_ANNUAL_LEAVE B,THR_EMPLOYEE C,tco_org d " +
                  "  WHERE A.DEL_IF=0 AND B.DEL_IF(+)=0 AND C.DEL_IF=0 AND A.THR_EMP_PK=C.PK and d.del_if=0 and a.TCO_org_PK=d.pk  " +
                  "  AND (DECODE ('" + p_thr_group_pk + "','ALL', '" + p_thr_group_pk + "',a.thr_group_pk) = '" + p_thr_group_pk + "') " +
                  "  AND (DECODE ('" + p_nation + "','ALL', '" + p_nation + "',c.nation) = '" + p_nation + "') " +
                  "   AND B.AN_YEAR(+)='" + p_month + "'-1" +
                  "   AND A.THR_EMP_PK=B.THR_EMP_PK(+) " +
                  "   AND A.AN_YEAR='" + p_month + "' " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  " and ( '" + p_status + "'='ALL'   " +
                  "            or('" + p_status + "'='A' and nvl(a.status,'A')='A' and nvl(a.resign_pay_yn,'N')='N')  " +
                  "            or('" + p_status + "'='R' and nvl(a.status,'A')='R' )  " +
                  "            or('" + p_status + "'='Y' and nvl(a.resign_pay_yn,'N')='Y' )  " +
                  "     )      " +
                  " AND (   a.tco_org_pk IN ( " +
                  "                              SELECT     g.pk " +
                  "                                    FROM TCO_ORG g " +
                  "                                   WHERE g.del_if = 0 " +
                  "                              START WITH g.pk =DECODE ('" + p_tco_org_pk + "','ALL', 0,'" + p_tco_org_pk + "') " +
                  "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
                  "                        OR '" + p_tco_org_pk + "' = 'ALL') " +
                  " ORDER BY A.org_nm,A.GROUP_NM,A.EMP_ID ";
        }


        // Response.Write(SQL);
        // Response.End();
        DataTable dt_total  = CtlLib.TableReadOpen(SQL);
        DataTable dt_option = CtlLib.TableReadOpen(SQL2);

        if (dt_option.Rows.Count > 0)
        {
            option = 1;
        }

        //Response.End();
        int i;
        int irow, icol;

        irow = dt_total.Rows.Count;
        icol = dt_total.Columns.Count;
        if (irow == 0)
        {
            Response.Write("There is no data");
            Response.End();
        }

        Boolean p   = (p_sql == 1 || p_sql == 2);
        string  str = (p) ? p_month.Substring(4, 2) + "/" + p_month.Substring(0, 4) : p_month;

        if (p_lang == "ENG")
        {
            exSheet.Cells[1, 1].Value = "ANNUAL LEAVE REPORT IN " + str;
        }
        else
        {
            exSheet.Cells[1, 1].Value = "BÁO CÁO PHÉP NĂM TRONG" + str;
        }
        exSheet.Cells[2, 3].Value = p_user;
        exSheet.Cells[3, 3].Value = dt_total.Rows[0][26].ToString();


        //Insert new row

        int row_pos = 7;

        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[row_pos + i, 1].Rows.EntireRow.Insert();
        }

        double[] total = new double[60];
        for (int j = 5; j < icol - 1; j++)
        {
            total[j] = 0;
        }


        //show data
        row_pos = 6;
        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[row_pos + i, 1].Value = i + 1;
            for (int j = 0; j < icol - 1; j++)
            {
                if (IsNumeric(dt_total.Rows[i][j].ToString()) && j != 2)
                {
                    exSheet.Cells[row_pos + i, j + 2].Value = Double.Parse(dt_total.Rows[i][j].ToString());
                    total[j] = total[j] + Double.Parse(dt_total.Rows[i][j].ToString());
                }
                else
                {
                    exSheet.Cells[row_pos + i, j + 2].Value = dt_total.Rows[i][j].ToString();
                }
            }
        }
        // show total
        for (int j = 5; j < icol - 1; j++)
        {
            exSheet.Cells[row_pos + irow, j + 2].Value = total[j];
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept, p_group, p_status, pos, lsttmp, p_input, dtyear, fromjoin, tojoin, lstmoney, contract;

        p_dept   = Request["org"].ToString();
        p_group  = Request["wg"].ToString();
        p_status = Request["status"].ToString();
        pos      = Request["pos"].ToString();
        p_input  = Request["txttmp"].ToString();
        dtyear   = Request["dtyear"].ToString();
        fromjoin = Request["fromjoin"].ToString();
        tojoin   = Request["tojoin"].ToString();
        lstmoney = Request["lstmoney"].ToString();
        contract = Request["contract"].ToString();

        //string p_m_type = Request.QueryString["l_m_type"].ToString();

        // if (p_type == "ALL")
        //    p_type = ">-999999999";


        string TemplateFile = "rpt_hrpm00300_2.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00300_2_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];
        IRange     range;

        DataTable myDT   = new DataTable();
        DataTable dt_Com = new DataTable();


        string SQL_Com
            = " select  a.PARTNER_LNAME c1,a.ADDR1 c2,a.PHONE_NO c3,a.TCO_BPPHOTO_PK c4,to_char(to_date('" + dtyear + "','yyyy'),'YYYY') c5,to_char(to_date('" + dtyear + "','yyyy'),'YYYY') c6" +
              " from tco_company a " +
              " where a.DEL_IF=0 " +
              " and a.pk in ( select tco_company_pk from  " +
              "               tco_org  f " +
              "               where  f.pk IN ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_dept + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_dept + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_dept + "' = 'ALL') " +
              " and rownum=1 ";

        //Response.Write(SQL_Com);
        //Response.End();
        dt_Com = CtlLib.TableReadOpen(SQL_Com);
        int irow_com;

        irow_com = dt_Com.Rows.Count;
        if (irow_com == 0)
        {
            Response.Write("There is no data of company");
            Response.End();
        }
        //	int pk = int.Parse(dt_Com.Rows[0][3].ToString());


        string SQL
            = "SELECT   b.org_nm, c.workgroup_nm, a.emp_id, a.full_name, " +
              "               TO_CHAR (TO_DATE (a.join_dt, 'yyyymmdd'), 'dd-mm-yyyy'), " +
              "               pos.code_FNM, d.code_nm " +
              " ,nvl(e.m01,0) " +
              " ,nvl(e.m01_2,0) " +
              " ,nvl(e.m02,0) " +
              " ,nvl(e.m02_2,0) " +
              " ,nvl(e.m03,0) " +
              "  ,nvl(e.m03_2,0) " +
              " ,nvl(e.m04,0) " +
              " ,nvl(e.m04_2,0) " +
              " ,nvl(e.m05,0) " +
              " ,nvl(e.m05_2,0) " +
              " ,nvl(e.m06,0) " +
              " ,nvl(e.m06_2,0) " +
              " ,nvl(e.m07,0) " +
              " ,nvl(e.m07_2,0) " +
              " ,nvl(e.m08,0) " +
              " ,nvl(e.m08_2,0) " +
              " ,nvl(e.m09,0) " +
              " ,nvl(e.m09_2,0) " +
              " ,nvl(e.m10,0) " +
              " ,nvl(e.m10_2,0) " +
              " ,nvl(e.m11,0) " +
              " ,nvl(e.m11_2,0) " +
              " ,nvl(e.m12,0) " +
              " ,nvl(e.m12_2,0)" +
              "          FROM thr_employee a, " +
              "               tco_org b, " +
              "               thr_work_group c, " +
              "               (select thr_emp_pk ,  " +
              " sum(decode(substr(work_mon,5,2),'01',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '01')  then  salary_level1  else 0 end,0)) as m01, " +
              " sum(decode(substr(work_mon,5,2),'02',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '02')  then salary_level1  else 0 end,0)) as m02, " +
              " sum(decode(substr(work_mon,5,2),'03',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '03')  then salary_level1  else 0 end,0)) as m03, " +
              " sum(decode(substr(work_mon,5,2),'04',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '04')  then salary_level1  else 0 end,0)) as m04, " +
              " sum(decode(substr(work_mon,5,2),'05',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '05')  then salary_level1  else 0 end,0)) as m05, " +
              " sum(decode(substr(work_mon,5,2),'06',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '06')  then salary_level1  else 0 end,0)) as m06, " +
              " sum(decode(substr(work_mon,5,2),'07',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '07')  then salary_level1  else 0 end,0)) as m07, " +
              " sum(decode(substr(work_mon,5,2),'08',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '08')  then salary_level1  else 0 end,0)) as m08, " +
              " sum(decode(substr(work_mon,5,2),'09',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '09')  then salary_level1  else 0 end,0)) as m09, " +
              " sum(decode(substr(work_mon,5,2),'10',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '10')  then salary_level1  else 0 end,0)) as m10, " +
              " sum(decode(substr(work_mon,5,2),'11',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '11')  then salary_level1  else 0 end,0)) as m11, " +
              " sum(decode(substr(work_mon,5,2),'12',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) > (select E.FROM_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '12')  then salary_level1  else 0 end,0)) as m12,     " +
              " sum(decode(substr(work_mon,5,2),'01',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '01')  then salary_level2 else 0 end,0)) as m01_2, " +
              " sum(decode(substr(work_mon,5,2),'02',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '02')  then salary_level2 else 0 end,0)) as m02_2, " +
              " sum(decode(substr(work_mon,5,2),'03',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '03')  then salary_level2 else 0 end,0)) as  m03_2, " +
              " sum(decode(substr(work_mon,5,2),'04',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '04')  then salary_level2 else 0 end,0)) as m04_2, " +
              " sum(decode(substr(work_mon,5,2),'05',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '05')  then salary_level2 else 0 end,0)) as m05_2, " +
              " sum(decode(substr(work_mon,5,2),'06',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '06')  then salary_level2 else 0 end,0)) as m06_2, " +
              " sum(decode(substr(work_mon,5,2),'07',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '07')  then salary_level2 else 0 end,0)) as m07_2, " +
              " sum(decode(substr(work_mon,5,2),'08',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '08')  then salary_level2 else 0 end,0)) as m08_2, " +
              " sum(decode(substr(work_mon,5,2),'09',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '09')  then salary_level2 else 0 end,0)) as m09_2, " +
              " sum(decode(substr(work_mon,5,2),'10',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '10')  then salary_level2 else 0 end,0)) as m10_2, " +
              " sum(decode(substr(work_mon,5,2),'11',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '11')  then salary_level2 else 0 end,0)) as m11_2, " +
              " sum(decode(substr(work_mon,5,2),'12',case when nvl(confirm_dt,' ')=' ' or substr(confirm_dt,1,8) <= (select E.TO_DT from thr_close e where e.del_if =0 and substr(E.MONTH_CLOSE,1,4)='" + dtyear + "' and substr(E.MONTH_CLOSE,5,2)= '12')  then salary_level2 else 0 end,0)) as m12_2 " +
              "               from thr_salary_manage " +
              "               where del_if=0 and substr(work_mon,1,4)='" + dtyear + "' " +
              "               group by thr_emp_pk " +
              "               ) e, " +
              "               (SELECT code, code_nm " +
              "                  FROM vhr_hr_code " +
              "                 WHERE ID = 'HR0001') d " +
              "               , " +
              "               (SELECT vh.code, vh.code_fnm " +
              "                  FROM vhr_hr_code vh " +
              "                 WHERE vh.ID = 'HR0008') pos " +
              "         WHERE a.del_if = 0 " +
              "           AND b.del_if = 0 " +
              "           AND c.del_if = 0 " +
              "           AND a.thr_wg_pk = c.pk " +
              "           AND a.tco_org_pk = b.pk " +
              "           AND e.thr_emp_pk = a.pk " +
              "           AND a.contract_type = d.code(+) " +
              "           and pos.code(+)=a.POS_TYPE " +
              " AND DECODE ('" + p_group + "', 'ALL', '" + p_group + "', a.thr_wg_pk) ='" + p_group + "'" +
              "           AND (   a.tco_org_pk IN ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_dept + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_dept + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_dept + "' = 'ALL') " +
              "           AND DECODE ('" + pos + "', 'ALL', '" + pos + "', a.pos_type) = '" + pos + "' " +
              "      AND DECODE ('" + p_status + "', 'ALL', '" + p_status + "', a.status) = '" + p_status + "' " +
              "           AND DECODE ('" + contract + "', " +
              "                       'ALL', '" + contract + "', " +
              "                       a.contract_type " +
              "                      ) = '" + contract + "' " +
              "              AND ('" + p_input + "' IS NULL  " +
              "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
              "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
              "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
              "           AND (   '" + fromjoin + "' IS NULL " +
              "                OR '" + tojoin + "' IS NULL " +
              "                OR a.join_dt BETWEEN '" + fromjoin + "' AND '" + tojoin + "' " +
              "               ) " +
              "   AND DECODE('" + lstmoney + "','ALL','" + lstmoney + "',A.MONEY_KIND)='" + lstmoney + "'" +
              "      ORDER BY NVL(B.SEQ,0),B.org_nm, C.workgroup_nm, A.emp_id ";


        //Response.Write(SQL);
        //Response.End();

        myDT = CtlLib.TableReadOpen(SQL);

        if (myDT.Rows.Count <= 0)
        {
            Response.Write("There is no data !!!");
            Response.End();
        }


        exSheet.Cells["B1"].Value = dt_Com.Rows[0]["c1"].ToString();
        exSheet.Cells["B2"].Value = dt_Com.Rows[0][1].ToString();
        exSheet.Cells["B3"].Value = dt_Com.Rows[0]["c3"].ToString();
        exSheet.Cells["G1"].Value = "SALARY SUMMARY IN THE YEAR " + int.Parse(dt_Com.Rows[0]["c5"].ToString());
        exSheet.Cells["H2"].Value = " TỔNG HỢP LƯƠNG NĂM " + int.Parse(dt_Com.Rows[0]["c5"].ToString());

        int l_row = myDT.Rows.Count;
        int l_col = myDT.Columns.Count;
        int k;

        double[] grant_total = new double[l_col + 1];
        int      l_pos       = 6;

        for (k = 0; k < l_col + 1; k++)
        {
            grant_total[k] = 0;
        }


        for (int i = 0; i < l_row - 1; i++)
        {
            exSheet.Range[l_pos + i + 1, l_col].Rows.EntireRow.Insert(); //insert dong moi
        }

        for (int i = 0; i < l_row; i++)
        {
            exSheet.Cells[l_pos + i, 1].Value = i + 1;
            for (int j = 1; j < 32; j++)
            {
                if (j <= 7)
                {
                    exSheet.Cells[l_pos + i, j + 1].Value = myDT.Rows[i][j - 1].ToString();
                }
                else
                {
                    grant_total[j] += Double.Parse(myDT.Rows[i][j - 1].ToString());
                    exSheet.Cells[l_pos + i, j + 1].Value = float.Parse(myDT.Rows[i][j - 1].ToString());
                }
            }
            if (i == l_row - 1)
            {
                exSheet.Cells[l_pos + i + 1, 1].Value = "Total: " + myDT.Rows.Count + " Employee(s)";
                for (k = 9; k < 32; k++)
                {
                    exSheet.Cells[l_pos + i + 1, k].Value = grant_total[k - 1];
                }
            }

            //  exSheet.Range["A" + l_dong + ":K" + l_dong].Rows.Borders[XlBordersIndex.xlInsideAll].LineStyle = XlLineStyle.xlContinuous;
        }
        //grand total



        // exSheet.Range["A" + (l_pos - 1) + ":AQ" + (l_pos - 1)].Rows.Borders[XlBordersIndex.xlAround].LineStyle = XlLineStyle.xlContinuous;
        //exSheet.Range["A" + (l_pos + l_row) + ":AQ" + (l_pos + l_row)].Rows.Borders[XlBordersIndex.xlAround].LineStyle = XlLineStyle.xlContinuous;



        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string l_group    = Request.QueryString["l_group"].ToString();
        string l_pay_type = Request.QueryString["l_pay_type"].ToString();
        string l_m_type   = Request.QueryString["l_m_type"].ToString();
        string l_bn_times = Request.QueryString["l_bn_times"].ToString();
        string l_emp_id   = Request.QueryString["l_emp_id"].ToString();
        string l_dept     = Request.QueryString["l_dept"].ToString();
        string l_year     = Request.QueryString["l_year"].ToString();

        string TemplateFile = "rpt_hrpm00700_12.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00700_12_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;
        string SQL
            = "  SELECT '', " +
              "         '', " +
              "         org.org_nm dept_nm, " +
              "         te.EMP_ID, " +
              "         te.FULL_NAME, " +
              "         TO_CHAR (TO_DATE (te.JOIN_DT, 'yyyymmdd'), 'dd-mm-yyyy') join_dt, " +
              "         ROUND (NVL (tb.SENIORITY, 0), 2), " +
              "         ROUND (NVL (tb.SALARY, 0), 2), " +
              "         ROUND (NVL (tb.RATIO, 0), 2), " +
              "         CASE " +
              "            WHEN tb.MONEY_KIND = '02' " +
              "            THEN " +
              "               ROUND (tb.BN_AMT * NVL (tb.BN_EXT_RATE, 0), 3) " +
              "            ELSE " +
              "               ROUND (NVL (tb.BN_AMT, 0), 0) " +
              "         END " +
              "            AS bonus_amt " +
              "    FROM thr_bonus tb, " +
              "         thr_work_group tg, " +
              "         thr_employee te, " +
              "         tco_org org " +
              "   WHERE     tb.DEL_IF = 0 " +
              "         AND org.del_if = 0 " +
              "         AND org.pk = te.tco_org_pk " +
              "         AND NVL (tb.BN_AMT, 0) > 0 " +
              "         AND te.DEL_IF = 0 " +
              "         AND tg.DEL_IF = 0 " +
              "         AND tb.THR_EMP_PK = te.pk " +
              "         AND tg.PK = te.THR_wG_PK " +
              "         AND tb.BN_YEAR = '" + l_year + "' " +
              "    and decode('" + l_group + "','ALL','" + l_group + "',te.THR_wg_PK)='" + l_group + "'  " +
              "    and decode('" + l_pay_type + "','ALL','" + l_pay_type + "',tb.PAY_TYPE)='" + l_pay_type + "'  " +
              "    and decode('" + l_m_type + "','ALL','" + l_m_type + "',tb.MONEY_KIND)='" + l_m_type + "'  " +
              "    and decode('" + l_bn_times + "','ALL','" + l_bn_times + "',tb.BN_TIMES)='" + l_bn_times + "'  " +
              "    and (te.EMP_ID like  '%'||'" + l_emp_id + "'||'%' or '" + l_emp_id + "' is null) " +
              "    and (te.TCO_org_PK in   (  " +
              "                              SELECT     g.pk  " +
              "                                    FROM tco_org g  " +
              "                                   WHERE g.del_if = 0  " +
              "                              START WITH g.pk =  " +
              "                                            DECODE ('" + l_dept + "',  " +
              "                                                    'ALL', 0,  " +
              "                                                    '" + l_dept + "'  " +
              "                                                   )  " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "            OR '" + l_dept + "' = 'ALL')  " +
              "ORDER BY org.org_nm, te.EMP_ID ";


        // Response.Write(SQL);
        // Response.End();


        DataTable dt_emp = CtlLib.TableReadOpen(SQL);
        int       irow_emp, icol_emp;

        irow_emp = dt_emp.Rows.Count;
        icol_emp = dt_emp.Columns.Count;
        if (irow_emp == 0)
        {
            Response.Write("There is no data of salary");
            Response.End();
        }



        //Khai bao bien
        int    i, j;
        int    pos = 5;
        int    emp_dept = 0, n_dept = 0;
        double sub_total1 = 0, sub_total2 = 0, grand_total1 = 0, grand_total2 = 0;

        //-----header---
        exSheet.Cells["F2"].Value = "Bonus Year: " + l_year;
        exSheet.Cells["H2"].Value = "Times " + l_bn_times;

        for (i = 0; i < irow_emp - 1; i++)
        {
            j = i + 1;
            if (dt_emp.Rows[i]["dept_nm"].ToString() != dt_emp.Rows[j]["dept_nm"].ToString())
            {
                n_dept++;
            }
        }
        //Insert new row

        for (i = 0; i < irow_emp - 1 + n_dept; i++)
        {
            exSheet.Range[pos + 1, 1].Rows.EntireRow.Insert();
        }
        //Show data
        for (i = 0; i < irow_emp; i++)
        {
            sub_total1   = sub_total1 + double.Parse(dt_emp.Rows[i][7].ToString());
            sub_total2   = sub_total2 + double.Parse(dt_emp.Rows[i][9].ToString());
            grand_total1 = grand_total1 + double.Parse(dt_emp.Rows[i][7].ToString());
            grand_total2 = grand_total2 + double.Parse(dt_emp.Rows[i][9].ToString());

            emp_dept++;

            exSheet.Cells[pos + i, 1].Value = emp_dept;

            for (j = 2; j < icol_emp; j++)
            {
                if (j <= 5)
                {
                    exSheet.Cells[pos + i, j].Value = dt_emp.Rows[i][j].ToString();
                }
                else
                {
                    exSheet.Cells[pos + i, j].Value = double.Parse(dt_emp.Rows[i][j].ToString());
                }
            }

            if (i != irow_emp - 1 && (dt_emp.Rows[i]["dept_nm"].ToString() != dt_emp.Rows[i + 1]["dept_nm"].ToString()))
            {
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, 5].Merge();
                exSheet.Cells[pos + i + 1, 1].Value = "Sub Total " + dt_emp.Rows[i]["dept_nm"].ToString() + " (" + emp_dept + " employees)";
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, icol_emp - 1].Rows.Font.Bold = true;
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, icol_emp - 1].Interior.Color = Color.FromArgb(204, 255, 204);    //to mau cho sub

                exSheet.Cells[pos + i + 1, 7].Value = sub_total1;
                exSheet.Cells[pos + i + 1, 9].Value = sub_total2;

                sub_total1 = sub_total2 = 0;

                //Insert page break
                IPageBreaks pbs       = exBook.Worksheets[1].HPageBreaks;
                IRange      usedrange = exBook.Worksheets[1].UsedRange;
                //pbs.Reset();
                pbs.Add(usedrange.Rows[pos + i + 2]);
                //end insert page beak
                pos      = pos + 1;
                emp_dept = 0;
            }
            else if (i == irow_emp - 1)
            {
                exSheet.Cells[pos + i + 1, 1].Value = "Total " + dt_emp.Rows[i]["dept_nm"].ToString() + ": " + emp_dept + " employee(s)";
                exSheet.Cells[pos + i + 2, 1].Value = "Grant Total: " + irow_emp + " employee(s)";

                exSheet.Cells[pos + i + 1, 7].Value = sub_total1;
                exSheet.Cells[pos + i + 1, 9].Value = sub_total2;
                exSheet.Cells[pos + i + 2, 7].Value = grand_total1;
                exSheet.Cells[pos + i + 2, 9].Value = grand_total2;
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 11
0
    protected void Page_Load(object sender, EventArgs e)
    {
        //CtlLib.SetUser("hr");
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_user, p_tco_org_pk, p_wg, p_from, p_to;
        string p_thr_ws_pk, p_search, p_input, p_opt, p_opt_text, p_m1, p_m2, p_nation;


        p_tco_org_pk = Request["p_tco_org_pk"].ToString();
        p_wg         = Request["p_wg"].ToString();
        p_from       = Request["p_from"].ToString();
        p_to         = Request["p_to"].ToString();
        p_nation     = Request["p_nation"].ToString();


        string TemplateFile = "rpt_hrti00800_4.xls";
        string TempFile     = "../../../../system/temp/rpt_hrti00800_4_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;


        string SQL
            = "SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY'),HR.WG,SUM(HR.WD),SUM(HR.T15),SUM(HR.T2),SUM(HR.T3) " +
              "FROM( " +
              "SELECT B.workgroup_NM WG,MAX(NVL(D.WORK_TIME,0)) WD  " +
              "            ,SUM(CASE WHEN E.OT_TYPE='OT' AND E.HOL_TYPE IS NULL THEN NVL(E.OT_TIME,0) " +
              "                    ELSE 0 END) T15 " +
              "            ,SUM(CASE WHEN E.OT_TYPE IN('OT','HT')  AND NVL(E.HOL_TYPE,' ')='SUN' THEN NVL(E.OT_TIME,0) " +
              "                    ELSE 0 END) T2 " +
              "            ,SUM(CASE WHEN E.OT_TYPE IN('OT','HT')  AND NVL(E.HOL_TYPE,' ')='HOL' THEN NVL(E.OT_TIME,0) " +
              "                    ELSE 0 END) T3         " +
              "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_TIME_MACHINE D,THR_EXTRA_TIME E,tco_org F  " +
              "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND E.DEL_IF(+)=0 AND F.DEL_IF=0  " +
              "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK  " +
              "            AND D.THR_EMP_PK=E.THR_EMP_PK(+) AND D.WORK_DT=E.WORK_DT(+)  " +
              "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
              "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
              "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
              "                   AND (    A.TCO_ORG_PK IN (  " +
              "                       SELECT     G.PK  " +
              "                             FROM TCO_ORG G  " +
              "                            WHERE G.DEL_IF = 0            " +
              "                       START WITH G.PK =  " +
              "                                     DECODE ('" + p_tco_org_pk + "',  " +
              "                                             'ALL', 0,  " +
              "                                             '" + p_tco_org_pk + "'  " +
              "                                            )  " +
              "                       CONNECT BY PRIOR G.PK = G.P_PK)  " +
              "                 OR '" + p_tco_org_pk + "' = 'ALL')  " +
              "            GROUP BY B.workgroup_NM,D.WORK_DT,d.thr_emp_pk) HR " +
              " GROUP BY HR.WG " +
              " ORDER BY HR.WG ";

        //----------------------------------

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_total = CtlLib.TableReadOpen(SQL);
        int       irow, icol;

        irow = dt_total.Rows.Count;
        icol = dt_total.Columns.Count;
        if (irow == 0)
        {
            Response.Write("There is no data");
            Response.End();
        }

        exSheet.Cells[1, 1].Value = "TIME SUMMARY ";
        exSheet.Cells[2, 2].Value = "Work date: ";
        exSheet.Cells[2, 3].Value = p_from.Substring(6, 2) + "/" + p_from.Substring(4, 2) + "/" + p_from.Substring(0, 4);
        exSheet.Cells[2, 4].Value = "~";
        exSheet.Cells[2, 5].Value = p_to.Substring(6, 2) + "/" + p_to.Substring(4, 2) + "/" + p_to.Substring(0, 4);



        int i;
        int row_pos = 8;

        //insert row
        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[row_pos + i, 1].Rows.EntireRow.Insert();
        }

        exSheet.Range[row_pos - 1, 1, row_pos - 1, 6].Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
        exSheet.Range[row_pos + irow - 1, 1, row_pos + irow - 1, 6].Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;

        //show data
        row_pos = 7;
        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[row_pos + i, 1].Value = i + 1;
            for (int j = 1; j < icol; j++)
            {
                if (j == 2 || j == 3 || j == 4 || j == 5)
                {
                    exSheet.Cells[row_pos + i, j + 1].Value = Double.Parse(dt_total.Rows[i][j].ToString());
                }
                else
                {
                    exSheet.Cells[row_pos + i, j + 1].Value = dt_total.Rows[i][j].ToString();
                }
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept, p_group, p_status, p_type, p_pay_type, p_emp_id, p_work_mon, p_sal_period, p_m_type;

        p_dept       = Request["l_dept"].ToString();
        p_group      = Request["l_group"].ToString();
        p_status     = Request["l_status"].ToString();
        p_type       = Request["l_type"].ToString();
        p_pay_type   = Request["l_pay_type"].ToString();
        p_emp_id     = Request["l_emp_id"].ToString();
        p_work_mon   = Request["l_work_mon"].ToString();
        p_m_type     = Request.QueryString["l_m_type"].ToString();
        p_sal_period = Request["l_sal_period"].ToString();

        DateTime p_month_dt = new DateTime(int.Parse(p_work_mon.Substring(0, 4).ToString()), int.Parse(p_work_mon.Substring(4, 2).ToString()), 1);

        string TemplateFile = "rpt_hrpm00700_9.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00700_9_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];
        IRange     range;

        string SQL_Code = "select num_1,num_2 from vhr_hr_code " +
                          "where id='HR0045' " +
                          "order by num_1 ";
        DataTable dt_Code = CtlLib.TableReadOpen(SQL_Code);
        int       irow_code;

        irow_code = dt_Code.Rows.Count;
        if (irow_code == 0)
        {
            Response.Write("There is no data of money code");
            Response.End();
        }

        for (int j = 0; j < irow_code; j++)   //chay cac cot loai tien
        {
            exSheet.Range[4, 5].Copy(exSheet.Range[4, 5 + j], XlPasteType.xlPasteAll);
            exSheet.Cells[4, 5 + j].Value = double.Parse(dt_Code.Rows[j][1].ToString());
        }

        //-----------------------ket loai tien--------------
        string sql_temp = "";

        for (int i = 1; i <= irow_code; i++)
        {
            sql_temp += ",HR_CLASSIFICATION_MONEY_DETAIL(sum(round(a.net_amt,3))," + i.ToString() + ")";
        }
        //-------------------------main sql-------------------------------
        string SQL
            = "select a.dept_NM,  " +                                    //1
              "count(*),  " +                                            //2
              "round(sum(NVL(A.net_amt,0)),3)  ";                        //54

        SQL = SQL + sql_temp +
              "from thr_month_salary a " +
              "where a.del_if=0 and a.work_mon='" + p_work_mon + "'  " +
              " and (a.tco_dept_pk in ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_dept + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_dept + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_dept + "' = 'ALL') " +
              " and decode('" + p_group + "','ALL','ALL',a.thr_group_pk)='" + p_group + "'" +
              " and a.emp_id like '%" + p_emp_id + "%' " +
              " and decode('" + p_status + "','ALL','ALL',a.status)='" + p_status + "'" +
              " and decode('" + p_pay_type + "','ALL','ALL',a.pay_type)='" + p_pay_type + "'" +
              " and decode('" + p_m_type + "','ALL','ALL',a.MONEY_KIND)='" + p_m_type + "'" +
              " and nvl(a.net_amt,0) " + p_type +
              " and decode('" + p_sal_period + "','ALL','ALL',a.sal_period)='" + p_sal_period + "'" +
              " group by a.dept_nm" +
              " order by a.dept_nm";
        //Response.Write(SQL);
        //Response.End();
        DataTable dt_Emp = CtlLib.TableReadOpen(SQL);
        int       irow_emp, icol_emp;

        irow_emp = dt_Emp.Rows.Count;
        icol_emp = dt_Emp.Columns.Count;
        if (irow_emp == 0)
        {
            Response.Write("There is no data of salary");
            Response.End();
        }

        //--------------------company information-----------------------
        string SQL_Com
            = "select  a.PARTNER_LNAME,a.ADDR1,a.PHONE_NO,f.data,to_char(to_date('" + p_work_mon + "','yyyymm'),'MON-YYYY'),to_char(to_date('" + p_work_mon + "','yyyymm'),'MM-YYYY') " +
              "from tco_company a, tc_fsbinary f " +
              "where a.DEL_IF=0 and a.TCO_BPPHOTO_PK = f.pk(+) " +
              "and a.pk in ( select tco_company_pk from  " +
              "               tco_org  f " +
              "               where  f.pk IN ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_dept + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_dept + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_dept + "' = 'ALL') " +
              "and rownum=1 ";

        //Response.Write(SQL_Com);
        //Response.End();
        DataTable dt_Com = CtlLib.TableReadOpen(SQL_Com);
        int       irow_com;

        irow_com = dt_Com.Rows.Count;


        exSheet.Cells["B1"].Value = dt_Com.Rows[0][0].ToString();
        exSheet.Cells["B2"].Value = dt_Com.Rows[0][1].ToString();
        exSheet.Cells["B3"].Value = dt_Com.Rows[0][2].ToString();

        MemoryStream stream;
        Bitmap       b;

        if (dt_Com.Rows[0][3].ToString().Length != 0)
        {
            byte[] MyData = new byte[0];
            MyData = (byte[])dt_Com.Rows[0][3];
            stream = new MemoryStream(MyData);

            b = new Bitmap(stream);
            exSheet.Cells["A1"].Select();
            exSheet.Shapes.AddPicture(CtlLib.ResizeImage(b, 70, 70));
        }

        exSheet.Cells["I1"].Value = string.Format("CLASSIFICATION OF MONEY SUMMARY IN {0}", dt_Com.Rows[0][4].ToString());
        exSheet.Cells["I2"].Value = string.Format("TỔNG HỢP PHÂN LOẠI TIỀN THÁNG {0}", dt_Com.Rows[0][5].ToString());

        int s_row = 5;
        int s_col = 5;

        for (int i = 0; i < irow_emp; i++)
        {
            exSheet.Cells["A" + s_row].Value = i + 1;
            exSheet.Cells["B" + s_row].Value = dt_Emp.Rows[i][0].ToString();
            exSheet.Cells["C" + s_row].Value = dt_Emp.Rows[i][1].ToString();
            exSheet.Cells["D" + s_row].Value = double.Parse(dt_Emp.Rows[i][2].ToString());

            s_col = 5;
            for (int j = 0; j < irow_code; j++)       //chay cac cot loai tien
            {
                exSheet.Cells[s_row, 5].Copy(exSheet.Range[s_row, s_col], XlPasteType.xlPasteAll);
                exSheet.Cells[s_row, s_col].Value = double.Parse(dt_Emp.Rows[i][3 + j].ToString());
                s_col++;
            }

            s_row++;
            if (i < irow_emp - 1)
            {
                exSheet.Range["A" + s_row].Rows.EntireRow.Insert();
            }
        }

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_year, p_from_mon, p_to_mon, p_tco_dept_pk;

        p_year        = Request["p_year"].ToString();
        p_from_mon    = Request["p_month_from"].ToString();
        p_to_mon      = Request["p_month_to"].ToString();
        p_tco_dept_pk = "ALL";

        string TemplateFile = "rpt_hrpm00700_22.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00700_22_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        int i, j, k;


        //-----------------information of company-----------------
        string SQL_Com
            = "select  a.PARTNER_LNAME,a.ADDR1,a.PHONE_NO,a.TCO_BPPHOTO_PK,to_char(to_date('" + p_from_mon + "','yyyymm'),'MM/YYYY'),to_char(to_date('" + p_to_mon + "','yyyymm'),'MM/YYYY') " +
              "from tco_company a " +
              "where a.DEL_IF=0 " +
              "and a.pk in ( select tco_company_pk from  " +
              "               tco_org  f " +
              "               where  f.pk IN ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_tco_dept_pk + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_tco_dept_pk + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_tco_dept_pk + "' = 'ALL') " +
              "and rownum=1 ";

        //Response.Write(SQL_Com);
        //Response.End();
        DataTable dt_com = CtlLib.TableReadOpen(SQL_Com);
        int       irow_com;

        irow_com = dt_com.Rows.Count;
        if (irow_com == 0)
        {
            Response.Write("There is no data of company");
            Response.End();
        }

        //Dem so thang user muon xuat
        string SQL
            = "select months_between(to_date('" + p_to_mon + "','yyyymm'),to_date('" + p_from_mon + "','yyyymm')) + 1 from dual";

        DataTable dt_mon    = CtlLib.TableReadOpen(SQL);
        int       month_num = int.Parse(dt_mon.Rows[0][0].ToString());

        DateTime[] aDate    = new DateTime[month_num];
        DateTime   Cur_Date = new DateTime(int.Parse(p_from_mon.Substring(0, 4).ToString()), int.Parse(p_from_mon.Substring(4, 2).ToString()), 1);

        SQL
            = "SELECT a.pk " +
              "       ,(select decode(code,'01','Vietnamese','Foreigner') from vhr_hr_code where id='HR0009' and code = a.nation) nation " +
              "       ,a.emp_id " +
              "       ,a.full_name,a.PIT_TAX_NO,A.PERSON_ID " +
              "       ,TO_CHAR (TO_DATE (a.join_dt, 'yyyymmdd'), 'dd/mm/yyyy') ";

        for (i = 0; i < month_num; i++)
        {
            SQL = SQL + "       ,SUM(NVL(case when b.work_mon = TO_CHAR(ADD_MONTHS(to_date('" + p_from_mon + "','yyyymm')," + i + "),'yyyymm') then ROUND(NVL(B.INCOME_AMT_2, 0),3) end,0)) income_amt_2 " +
                  "       ,SUM(NVL(case when b.work_mon = TO_CHAR(ADD_MONTHS(to_date('" + p_from_mon + "','yyyymm')," + i + "),'yyyymm') then ROUND(NVL(B.DEDUCT_PIT, 0),3) END,0)) deduct_pit " +
                  "       ,SUM(NVL(case when b.work_mon = TO_CHAR(ADD_MONTHS(to_date('" + p_from_mon + "','yyyymm')," + i + "),'yyyymm') then 0 end,0)) tu_thien " +
                  "       ,SUM(NVL(case when b.work_mon = TO_CHAR(ADD_MONTHS(to_date('" + p_from_mon + "','yyyymm')," + i + "),'yyyymm') then ROUND(NVL (B.SOCIAL_AMT, 0) + NVL (B.HEALTH_AMT, 0) + NVL (B.UNEMP_INS, 0),3) end,0)) ins " +
                  "       ,SUM(NVL(case when b.work_mon = TO_CHAR(ADD_MONTHS(to_date('" + p_from_mon + "','yyyymm')," + i + "),'yyyymm') then ROUND(NVL (B.INCOME_BEFORE_TAX, NVL (B.INCOME_AMT, 0)),3) end,0)) income_amt " +
                  "       ,SUM(NVL(case when b.work_mon = TO_CHAR(ADD_MONTHS(to_date('" + p_from_mon + "','yyyymm')," + i + "),'yyyymm') then ROUND(NVL(B.INCOME_TAX, 0),3) end,0)) tax ";
            aDate[i] = Cur_Date.AddMonths(i);
        }

        SQL = SQL +
              "            ,0,MAX(NVL(c.income_amt_2,0)),MAX(NVL(c.deduct_pit,0)),MAX(NVL(c.tu_thien,0)) " +
              "            ,MAX(NVL(c.ins,0)),MAX(NVL(c.income_amt,0)),MAX(NVL(c.tax,0)) " +
              "  FROM thr_employee a, thr_month_salary b " +
              "        ,(SELECT a.pk emp_pk, " +
              "             ROUND (SUM (NVL (B.INCOME_AMT_2, 0)), 3) income_amt_2, " +
              "             ROUND (SUM (NVL (B.DEDUCT_PIT, 0)), 3) deduct_pit, " +
              "             0 tu_thien, " +
              "             ROUND (SUM (NVL (B.SOCIAL_AMT, 0)+ NVL (B.HEALTH_AMT, 0)+ NVL (B.UNEMP_INS, 0)),3) ins, " +
              "             ROUND (SUM (NVL (B.INCOME_BEFORE_TAX, NVL (B.INCOME_AMT, 0))), 3) income_amt, " +
              "             ROUND (SUM (NVL (B.INCOME_TAX, 0)), 3) tax " +
              "        FROM thr_employee a, thr_month_salary b " +
              "       WHERE     a.del_if = 0 " +
              "             AND b.del_if = 0 " +
              "             AND a.pk = b.thr_emp_pk " +
              "             AND b.work_mon BETWEEN '" + p_from_mon + "' AND '" + p_to_mon + "' " +
              "    GROUP BY a.pk) c " +
              " WHERE     a.del_if = 0 " +
              "       AND b.del_if = 0 " +
              "       AND a.pk = b.thr_emp_pk " +
              "       AND a.pk = c.emp_pk(+) " +
              "       AND b.work_mon between '" + p_from_mon + "' and '" + p_to_mon + "' " +
              "  GROUP by a.pk,a.nation,a.emp_id,a.full_name,a.join_dt,a.PIT_TAX_NO,A.PERSON_ID " +
              "  ORDER BY nation, a.emp_id        ";


        //Response.Write(SQL);
        //Response.End();

        DataTable dt_emp = CtlLib.TableReadOpen(SQL);
        int       irow_emp, icol_emp;

        irow_emp = dt_emp.Rows.Count;

        if (irow_emp == 0)
        {
            Response.Write("There is no data of salary");
            Response.End();
        }

        SQL
            = "  SELECT DECODE (code, '01', 'Vietnamese', 'Foreigner') " +
              "    FROM vhr_hr_code " +
              "   WHERE id = 'HR0009' " +
              "GROUP BY DECODE (code, '01', 'Vietnamese', 'Foreigner') " +
              "ORDER BY DECODE (code, '01', 'Vietnamese', 'Foreigner') ";

        DataTable dt_nation = CtlLib.TableReadOpen(SQL);

        int pos = 7, p_col = 13, n_array = month_num * 6 + 7;   //cong them 6 cot total phia sau
        int emp_dept = 0, n_dept = 0, row = 0;
        int count = 0, ncount = 0;

        double[] sub_total   = new double[n_array];
        double[] grant_total = new double[n_array];


        string[] atype = new string[] { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI" };

/*        string[] acol = new string[] { "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
 *                                  ,"AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL","AM","AN","AO","AP","AQ","AR","AS","AT","AU","AV","AW","AX","AY","AZ"
 *                                  ,"BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL","BM","BN","BO","BP","BQ","BR","BS","BT","BU","BV","BW","BX","BY","BZ"
 *                                  ,"CA", "CB", "CC", "CD", "CE", "CF", "CG", "CH", "CI", "CJ", "CK", "CL","CM","CN","CO","CP","CQ","CR","CS","CT","CU","CV","CW","CX","CY","CZ"};
 */

        //bind data to excel file
        //-----header---
        exSheet.Cells["A1"].Value = "BẢNG KÊ THU NHẬP CHỊU THUẾ NĂM " + p_year;

        //---end--header---

        for (k = 0; k < n_array; k++)
        {
            sub_total[k]   = 0;
            grant_total[k] = 0;
        }

        //Insert new column
        for (i = 0; i < (month_num - 1) * 6; i++)
        {
            exSheet.Range[1, p_col].Rows.EntireColumn.Insert();
        }

        for (i = 0; i < month_num - 1; i++)
        {
            exSheet.Range[2, p_col - 6, 6, p_col - 1].Copy(exSheet.Range[2, p_col + i * 6, 6, p_col + 5 + i * 6], XlPasteType.xlPasteAll);
        }

        for (i = 0; i < month_num; i++)
        {
            exSheet.Range[2, 7 + i * 6].Value = aDate[i].ToString("MMM");
        }

        //Insert new row

        for (i = 0; i < irow_emp - 1 + dt_nation.Rows.Count * 2; i++)
        {
            exSheet.Range[pos + 1, 1].Rows.EntireRow.Insert();
        }

        //Show data
        for (j = 0; j < dt_nation.Rows.Count; j++)
        {
            exSheet.Cells[pos + j + count, 1].Value = atype[j];
            exSheet.Cells[pos + j + count, 2].Value = dt_nation.Rows[j][0].ToString();
            exSheet.Cells[pos + j + count, 1, pos + j + count, 2].Rows.Font.Bold = true;
            for (i = 0; i < irow_emp; i++)
            {
                ncount++;
                if (dt_emp.Rows[i][1].ToString() == dt_nation.Rows[j][0].ToString())
                {
                    for (k = 0; k < n_array; k++)
                    {
                        sub_total[k] = sub_total[k] + double.Parse(dt_emp.Rows[i][7 + k].ToString());
                    }

                    count++;
                    exSheet.Cells[pos + j + count, 1].Value = ncount;
                    for (k = 2; k <= 6 + month_num * 6 + 6; k++)    // cong thêm 6 cot total phia sau
                    {
                        if (k <= 6)
                        {
                            exSheet.Cells[pos + j + count, k].Value = dt_emp.Rows[i][k].ToString();
                        }
                        else
                        {
                            exSheet.Cells[pos + j + count, k].Value = double.Parse(dt_emp.Rows[i][k].ToString());
                        }
                    }
                }
                else
                {
                    ncount = 0;
                }
            }
            //Them vao sub_total khi vua ket thuc nation
            exSheet.Cells[pos + j + count + 1, 2, pos + j + count + 1, 3].Merge();
            exSheet.Cells[pos + j + count + 1, 2].Value = "Sub Total ";
            exSheet.Cells[pos + j + count + 1, 1, pos + j + count + 1, 12 + n_array].Rows.Font.Bold = true;
            //exSheet.Cells[pos + j + count + 1, 1, pos + j + count + 1, 7 + n_array].Interior.Color = Color.FromArgb(255, 255, 0);
            for (k = 0; k < n_array; k++)
            {
                exSheet.Cells[pos + j + count + 1, 7 + k].Value = sub_total[k];
                sub_total[k] = 0;
            }
            pos = pos + 1;
        }

        pos = 8;    //lay lai vi tri ban dau
        for (i = 0; i < irow_emp; i++)
        {
            for (k = 0; k < n_array; k++)
            {
                grant_total[k] += Double.Parse(dt_emp.Rows[i][7 + k].ToString());
            }
        }

        for (k = 0; k < n_array; k++)
        {
            exSheet.Cells[pos + dt_emp.Rows.Count + dt_nation.Rows.Count + 1, 7 + k].Value = grant_total[k];
        }


        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept, p_group, p_status, p_type, p_pay_type, p_emp_id, p_work_mon, p_m_type, p_emp_type, p_nation;

        p_dept     = Request["l_dept"].ToString();
        p_group    = Request["l_group"].ToString();
        p_status   = Request["l_status"].ToString();
        p_type     = Request["l_type"].ToString();
        p_pay_type = Request["l_pay_type"].ToString();
        p_emp_id   = Request["l_emp_id"].ToString();
        p_work_mon = Request["l_work_mon"].ToString();
        p_m_type   = Request.QueryString["l_m_type"].ToString();
        p_emp_type = Request["l_Emp_Type"].ToString();
        p_nation   = "ALL";//Request.QueryString["l_nation"].ToString();

        if (p_type == "ALL")
        {
            p_type = "=nvl(s.net_amt,0)";
        }

        string TemplateFile = "rpt_hrpm00700_10.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00700_10_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];
        IRange     range;
        DataTable  myDT;

        myDT = new DataTable();
        string SQL = "SELECT S.FULL_NAME,S.EMP_ID,decode(nvl(S.salary_si,0),0,nvl(s.salary_level2,0),nvl(S.salary_si,0)) " +
                     ",NVL(S.SOCIAL_AMT,0) AS SOCIAL_3 " +
                     ",NVL(S.UNEMP_INS,0) AS unemp_wor_5  " +
                     ",NVL(S.HEALTH_AMT,0) AS HEALTH_4  " +
                     ",NVL(S.SOCIAL_AMT,0)+NVL(S.HEALTH_AMT,0)+NVL(S.UNEMP_INS,0) AS tt6  " +
                     ",NVL(S.SOCIAL_COMPANY,0)+NVL(S.SOCIAL_COM_NET,0) AS SOCIAL_COM_7 " +
                     ",NVL(S.UNEMP_COM_INS,0) AS unemp_COM_8 " +
                     ",NVL(S.HEALTH_COMPANY,0)+NVL(S.HEALTH_COM_NET,0) AS HEALTH_COM_9  " +
                     ",NVL(S.HEALTH_COMPANY,0)+NVL(S.HEALTH_COM_NET,0)+NVL(S.SOCIAL_COMPANY,0)+NVL(S.SOCIAL_COM_NET,0)+NVL(S.UNEMP_COM_INS,0) AS tt10  " +
                     ",NVL(S.HEALTH_COMPANY,0)+NVL(S.HEALTH_COM_NET,0)+NVL(S.SOCIAL_COMPANY,0)+NVL(S.SOCIAL_COM_NET,0)+NVL(S.UNEMP_COM_INS,0) +  " +
                     " NVL(S.SOCIAL_AMT,0)+NVL(S.HEALTH_AMT,0)+NVL(S.UNEMP_INS,0) AS gran_tt11  " +
                     " ,to_char(to_date('" + p_work_mon + "','yyyymm'),'MM-YYYY') as wm_12 " +
                     " from thr_month_salary S, thr_employee a " +
                     " where S.del_if=0 and a.del_if=0 and s.thr_emp_pk=a.pk and S.work_mon='" + p_work_mon + "' " +
                     " and (a.tco_org_pk in ( " +
                     "                              SELECT     g.pk " +
                     "                                    FROM tco_org g " +
                     "                                   WHERE g.del_if = 0 " +
                     "                              START WITH g.pk = " +
                     "                                            DECODE ('" + p_dept + "', " +
                     "                                                    'ALL', 0, " +
                     "                                                    '" + p_dept + "' " +
                     "                                                   ) " +
                     "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
                     "                        OR '" + p_dept + "' = 'ALL') " +
                     " and decode('" + p_group + "','ALL','ALL',a.thr_wg_pk)='" + p_group + "'" +
                     " and S.emp_id like '%" + p_emp_id + "%' " +
                     " and decode('" + p_status + "','ALL','ALL',S.status)='" + p_status + "'" +
                     " and decode('" + p_pay_type + "','ALL','ALL',S.pay_type)='" + p_pay_type + "'" +
                     " and decode('" + p_m_type + "','ALL','ALL',S.MONEY_KIND)='" + p_m_type + "'" +
                     " and decode('" + p_emp_type + "','ALL','ALL',s.EMPLOYEE_TYPE)='" + p_emp_type + "'" +
                     " and decode('" + p_nation + "','ALL','ALL',s.Nation)='" + p_nation + "'" +
                     " and nvl(S.HEALTH_AMT,0)+nvl(S.HEALTH_COM_NET,0)+NVL(S.SOCIAL_AMT,0)+NVL(S.SOCIAL_COM_NET,0) > 0 " +
                     " and nvl(s.net_amt,0) " + p_type +
                     " order by S.emp_id";

        //Response.Write(SQL);
        //Response.End();
        DataTable data = CtlLib.TableReadOpen(SQL);

        if (data.Rows.Count == 0)
        {
            Response.Write("There is no data");
            Response.End();
        }

        string sql_rate1 = "SELECT NVL(A.NUM_4,0),NVL(A.NUM_5,0),TO_NUMBER(NVL(A.CHAR_2,0)),TO_NUMBER(NVL(A.CHAR_3,0)) " +
                           " FROM VHR_HR_CODE A " +
                           " WHERE A.ID='HR0043' AND A.CHAR_1=SUBSTR('" + p_work_mon + "',1,4)";
        DataTable data_1 = CtlLib.TableReadOpen(sql_rate1);
        string    r_social = "", r_health = "", r_unemp = "";
        string    r_social_c = "", r_health_c = "", r_unemp_c = "";


        r_social   = data_1.Rows[0][1].ToString();
        r_health   = data_1.Rows[0][0].ToString();
        r_social_c = data_1.Rows[0][3].ToString();
        r_health_c = data_1.Rows[0][2].ToString();


        string sql_rate2 = "SELECT NVL(A.NUM_2,0),NVL(A.NUM_3,0) " +
                           " FROM VHR_HR_CODE A " +
                           " WHERE A.ID='HR0067' AND A.CHAR_1=SUBSTR('" + p_work_mon + "',1,4)";
        DataTable data_2 = CtlLib.TableReadOpen(sql_rate2);


        r_unemp   = data_2.Rows[0][0].ToString();
        r_unemp_c = data_2.Rows[0][1].ToString();

        exSheet.Range["A2"].Value = string.Format("MONTH : {0}", data.Rows[0][12].ToString());
        exSheet.Cells["E5"].Value = string.Format("Social {0}% (Worker)", r_social);
        exSheet.Cells["F5"].Value = string.Format("UI {0}% (Worker)", r_unemp);
        exSheet.Cells["G5"].Value = string.Format("Health {0}% (Worker)", r_health);
        exSheet.Cells["H5"].Value = string.Format("Total Ins {0}% (Worker)", double.Parse(r_social) + double.Parse(r_health) + double.Parse(r_unemp));
        exSheet.Cells["I5"].Value = string.Format("Social {0}% (Company)", r_social_c);
        exSheet.Cells["J5"].Value = string.Format("UI {0}% (Company)", r_unemp_c);
        exSheet.Cells["K5"].Value = string.Format("Health {0}% (Company)", r_health_c);
        exSheet.Cells["L5"].Value = string.Format("Total Ins {0}% (Company)", double.Parse(r_social_c) + double.Parse(r_health_c) + double.Parse(r_unemp_c));

        int s_row = 6, s_col = 2;

        double[] total = new double[10];

        for (int j = 0; j < total.Length; j++)
        {
            total[j] = 0;
        }

        for (int i = 0; i < data.Rows.Count; i++)
        {
            exSheet.Cells["A" + s_row].Value = i + 1;
            s_col = 2;
            for (int j = 0; j < data.Columns.Count - 1; j++)
            {
                if (j < 2)
                {
                    exSheet.Cells[s_row, s_col].Value = data.Rows[i][j].ToString();
                }
                else
                {
                    total[j - 2] += double.Parse(data.Rows[i][j].ToString());
                    exSheet.Cells[s_row, s_col].Value = double.Parse(data.Rows[i][j].ToString());
                }
                s_col++;
            }

            s_row++;

            if (i < data.Rows.Count - 1)
            {
                exSheet.Range["A" + s_row].Rows.EntireRow.Insert();
            }
        }

        exSheet.Cells["D" + s_row].Value = total[0];
        exSheet.Cells["E" + s_row].Value = total[1];
        exSheet.Cells["F" + s_row].Value = total[2];
        exSheet.Cells["G" + s_row].Value = total[3];
        exSheet.Cells["H" + s_row].Value = total[4];
        exSheet.Cells["I" + s_row].Value = total[5];
        exSheet.Cells["J" + s_row].Value = total[6];
        exSheet.Cells["K" + s_row].Value = total[7];
        exSheet.Cells["L" + s_row].Value = total[8];
        exSheet.Cells["M" + s_row].Value = total[9];

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_tco_org_pk = Request["l_tco_org_pk"];
        string p_work_mon   = Request["l_work_mon"];
        string p_emp_id     = Request["l_emp_id"];
        string p_from_dt    = Request["dt_from"];
        string p_to_dt      = Request["dt_to"];
        string p_nation     = Request["l_nation"];
        string p_sal_period = Request["l_sal_period"];

        string TemplateFile = "rpt_hrti01700_1.xls";
        string TempFile     = "../../../../system/temp/rpt_hrti01700_1_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        List <IWorksheet> listWB = new List <IWorksheet>();

        //Add worksheet
        IWorksheet exSheet;

        string sql_day
            = "select substr(c.car_date,-2), c.hol_type from thr_calendar c  " +
              "where c.del_if=0  " +
              "and c.car_date between '" + p_from_dt + "' and '" + p_to_dt + "' " +
              "and c.tco_company_pk = (select g.tco_company_pk " +
              "                                       from tco_org g " +
              "                                      where g.del_if = 0 and g.pk = '" + p_tco_org_pk + "') " +
              " order by c.car_date ";

        DataTable dt_day = CtlLib.TableReadOpen(sql_day);

        string sql_date
            = "select  " +
              "    to_char(to_date('" + p_work_mon + "', 'yyyymm'), 'mm yyyy')  " +
              "  from dual ";

        DataTable dt_date = CtlLib.TableReadOpen(sql_date);

        string sql_org
            = "select b.pk, substr(b.org_nm, 1, 30) org_nm from thr_work_month a, tco_org b " +
              "where a.del_if=0 and b.del_if=0 " +
              "and a.outlet_pk = b.pk " +
              "and a.work_mon = '" + p_work_mon + "' " +
              "and (a.emp_id = '" + p_emp_id + "' or '" + p_emp_id + "' is null) " +
              "and a.outlet_pk in ( select  g.pk " +
              "                                from tco_org g " +
              "                               where g.del_if = 0 " +
              "                          start with g.pk =(case when '" + p_tco_org_pk + "'='ALL' then g.pk else to_number('" + p_tco_org_pk + "') end) " +
              "                          connect by prior g.pk = g.p_pk) " +
              "group by b.pk, b.org_nm " +
              "order by b.org_nm ";

        DataTable dt_org = CtlLib.TableReadOpen(sql_org);

        int    count_org = dt_org.Rows.Count;
        string sheet_nm  = string.Empty;

        int row_loop = 7;

        for (int i = 0; i < count_org; i++)
        {
            //Create a new workbook
            exBook.Worksheets.Add();

            //Add worksheet
            exSheet = exBook.Worksheets[i + 1];

            if (i < dt_org.Rows.Count)
            {
                sheet_nm = dt_org.Rows[i][1].ToString();

                exSheet.Name = sheet_nm;
                listWB.Add(exSheet);
            }
            if (i == 0)
            {
                listWB[0].Cells["A6"].Value = string.Format("THÁNG {0} ", dt_date.Rows[0][0]);

                IInterior interior = listWB[0].Cells["E5:E5"].Interior;
                interior.Color = System.Drawing.Color.FromArgb(0, 204, 204);
                //listWB[0].Cells["A3"].Value = "Bộ phận: " + sheet_nm;

                /*
                 * for (int j = 0; j < dt_cal.Rows.Count; j++) //to mau cn cho dong tieu de
                 * {
                 * int tmp = int.Parse(dt_cal.Rows[j][0].ToString()) + 4;
                 * if(dt_cal.Rows[j][1].ToString()=="HOL")
                 * listWB[0].Cells[row_loop - 1, tmp].Rows.Font.Color = Color.FromArgb(255, 000, 000);
                 * else
                 * listWB[0].Cells[row_loop - 1, tmp].Interior.Color = Color.FromArgb(141, 180, 226);
                 *
                 *
                 *
                 * } */
            }
            else
            {
                listWB[0].Range["A1:AT30"].Copy(listWB[i].Range["A1:AT30"], XlPasteType.xlPasteAll);
                listWB[i].Cells["A6"].Value = string.Format("THÁNG {0} ", dt_date.Rows[0][0]);
                //listWB[i].Cells["E5"].Rows.Font.Color = Color.FromArgb(0, 153, 153);
                listWB[i].Cells["A1"].Rows.RowHeight = 20.25;
                listWB[i].Cells["A2"].Rows.RowHeight = 15.75;
                listWB[i].Cells["A3"].Rows.RowHeight = 18;
                listWB[i].Cells["A4"].Rows.RowHeight = 15;
                listWB[i].Cells["A5"].Rows.RowHeight = 22.5;
                listWB[i].Cells["A6"].Rows.RowHeight = 42;

                IInterior interior = listWB[i].Cells["E5:E5"].Interior;
                interior.Color = System.Drawing.Color.FromArgb(0, 204, 204);

                /*
                 *      listWB[i].FreezePanes = listWB[0].FreezePanes;
                 *      listWB[i].PageSetup.LeftMargin = listWB[0].PageSetup.LeftMargin;
                 *      listWB[i].PageSetup.Orientation = listWB[0].PageSetup.Orientation;
                 *      listWB[i].PageSetup.FitToPagesWide = 1;
                 *      listWB[i].Zoom = 120;
                 *      listWB[i].PageSetup.PrintTitleRows = listWB[0].PageSetup.PrintTitleRows;
                 *      listWB[i].PageSetup.RightMargin = listWB[0].PageSetup.RightMargin;
                 *      listWB[i].PageSetup.HeaderMargin = listWB[0].PageSetup.HeaderMargin;
                 *      listWB[i].PageSetup.FooterMargin = listWB[0].PageSetup.FooterMargin;
                 *      listWB[i].PageSetup.FitToPages = true;
                 *      listWB[i].PageSetup.CenterFooter = listWB[0].PageSetup.CenterFooter;
                 *      listWB[i].PageSetup.PaperSize = listWB[0].PageSetup.PaperSize;
                 *      listWB[i].PageSetup.TopMargin = listWB[0].PageSetup.TopMargin;
                 *      listWB[i].PageSetup.CenterHorizontally = listWB[0].PageSetup.CenterHorizontally;
                 */
            }

            // do du lieu header ngay
            for (int d = 0; d < dt_day.Rows.Count; d++)
            {
                listWB[i].Cells[6, 5 + d].Value = dt_day.Rows[d][0].ToString();
            }
        }

        // binding data
        string SQL_EMP = string.Empty;
        long   outlet_pk;

        for (int i = 0; i < count_org; i++)
        {
            outlet_pk = long.Parse(dt_org.Rows[i][0].ToString());

            SQL_EMP
                = "select 0, " +
                  "              1, " +
                  "              a.emp_id, " +
                  "              e.full_name, " +
                  "              (select initcap(v.code_nm) from vhr_hr_code v where v.id='HR0008' and v.code = a.pos_type ), " +
                  "              nullif(a.wt1,'0'), " +
                  "              nullif(a.wt2,'0'), " +
                  "              nullif(a.wt3,'0'), " +
                  "              nullif(a.wt4,'0'), " +
                  "              nullif(a.wt5,'0'), " +
                  "              nullif(a.wt6,'0'), " +
                  "              nullif(a.wt7,'0'), " +
                  "              nullif(a.wt8,'0'), " +
                  "              nullif(a.wt9,'0'), " +
                  "              nullif(a.wt10,'0'), " +
                  "              nullif(a.wt11,'0'), " +
                  "              nullif(a.wt12,'0'), " +
                  "              nullif(a.wt13,'0'), " +
                  "              nullif(a.wt14,'0'), " +
                  "              nullif(a.wt15,'0'), " +
                  "              nullif(a.wt16,'0'), " +
                  "              nullif(a.wt17,'0'), " +
                  "              nullif(a.wt18,'0'), " +
                  "              nullif(a.wt19,'0'), " +
                  "              nullif(a.wt20,'0'), " +
                  "              nullif(a.wt21,'0'), " +
                  "              nullif(a.wt22,'0'), " +
                  "              nullif(a.wt23,'0'), " +
                  "              nullif(a.wt24,'0'), " +
                  "              nullif(a.wt25,'0'), " +
                  "              nullif(a.wt26,'0'), " +
                  "              nullif(a.wt27,'0'), " +
                  "              nullif(a.wt28,'0'), " +
                  "              nullif(a.wt29,'0'), " +
                  "              nullif(a.wt30,'0'), " +
                  "              nullif(a.wt31,'0'), " +
                  "              nullif(round(nvl(a.total_wd, 0) + nvl(a.total_hol, 0), 2), 0), " +
                  "              nullif(round(nvl(a.abs1_pay, 0)/8, 2), 0),  " +
                  "              nullif(round(nvl(a.abs2_pay, 0)/8, 2), 0),  " +
                  "              nullif(round(nvl(a.abs3_pay, 0)/8, 2), 0),  " +
                  "              nullif(round(nvl(a.abs4_pay, 0)/8, 2), 0),  " +
                  "              nullif((round(nvl(a.total_wd, 0) + nvl(a.total_hol, 0), 2) + round(nvl(a.abs1_pay, 0)/8, 2) + round(nvl(a.abs2_pay, 0)/8, 2) " +
                  "                                                    + round(nvl(a.abs3_pay, 0)/8, 2) + round(nvl(a.abs4_pay, 0)/8, 2)), 0) as total_day_wt_abs, " +
                  "              nullif(nvl(a.total_ot, 0) , 0) ,                                       " +
                  "              nullif(nvl(a.total_st, 0) + nvl(a.total_ost, 0) , 0) , " +
                  "              nullif(nvl(a.total_ht, 0) + nvl(a.total_oht, 0) , 0) , " +
                  "              nullif(round((nvl(a.total_rate_ot, 0) + nvl(a.total_rate_st, 0) + nvl(a.total_rate_ost, 0) + nvl(a.total_rate_ht, 0) + nvl(a.total_rate_oht, 0))/8, 2), 0) total_day_ot, " +
                  "              nullif((round(nvl(a.total_wd, 0) + nvl(a.total_hol, 0), 2) + round(nvl(a.abs1_pay, 0)/8, 2) + round(nvl(a.abs2_pay, 0)/8, 2) " +
                  "                                                    + round(nvl(a.abs3_pay, 0)/8, 2) + round(nvl(a.abs4_pay, 0)/8, 2)) " +
                  "                      + round((nvl(a.total_rate_ot, 0) + nvl(a.total_rate_st, 0) + nvl(a.total_rate_ost, 0) + nvl(a.total_rate_ht, 0) + nvl(a.total_rate_oht, 0))/8, 2)                              " +
                  "                     , 0) as total_day_wt_abs_ot " +
                  "       from thr_work_month a, tco_org o, thr_employee e " +
                  "       where a.del_if=0 and o.del_if=0 and e.del_if=0 " +
                  "       and a.thr_emp_pk = e.pk " +
                  "       and a.tco_org_pk = o.pk " +
                  "       and a.work_mon = '" + p_work_mon + "' " +
                  "		and a.outlet_pk ='"+ outlet_pk + "' " +
                  "		and (a.emp_id = '"+ p_emp_id + "' or '" + p_emp_id + "' is null) " +
                  "       and decode('" + p_nation + "','ALL','ALL',e.nation )= '" + p_nation + "' " +
                  "       and a.sal_period  = '" + p_sal_period + "' " +
                  "       order by a.work_mon, a.emp_id ";

            DataTable dt_detail;
            dt_detail = CtlLib.TableReadOpen(SQL_EMP);

            int col_emp = dt_detail.Columns.Count;
            int row_emp = dt_detail.Rows.Count;
            int count   = 0;
            for (int l_addrow = 1; l_addrow < dt_detail.Rows.Count; l_addrow++)
            {
                listWB[i].Range[row_loop + l_addrow, 50].Rows.EntireRow.Insert();                 //insert row new of sheet
            }

            // xu ly du lieu do vao excel
            if (dt_detail.Rows.Count > 0)
            {
                for (int l_row = 0; l_row < dt_detail.Rows.Count; l_row++)
                {
                    count++;
                    listWB[i].Cells[row_loop + l_row, 1].Value = count;

                    // do du lieu vao cac dong
                    for (int col = 2; col < col_emp; col++)
                    {
                        if (IsNumeric(dt_detail.Rows[l_row][col].ToString()) && col != 2)
                        {
                            listWB[i].Cells[row_loop + l_row, col].Value = double.Parse(dt_detail.Rows[l_row][col].ToString());
                        }
                        else
                        {
                            listWB[i].Cells[row_loop + l_row, col].Value = dt_detail.Rows[l_row][col].ToString();
                        }
                    }
                }
            }
        }
        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        // range.Rows.Hidden = true;

        // font bold header

        /*range = exSheet.Range["A1:AC1"];
         * range.Rows[4].Font.Bold = true;*/

        exBook.SaveAs(TempFile);
        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        //Response.WriteFile(pdfFilePath);
        Response.WriteFile(TempFile);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 16
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());
        string p_tco_org_pk, p_wg, p_from, p_to, p_txtID, p_work_mon, p_lang;

        p_tco_org_pk = Request["p_tco_org_pk"].ToString();
        p_wg         = Request["p_wg"].ToString();
        p_from       = Request["p_from_dt"].ToString();
        p_to         = Request["p_to_dt"].ToString();
        p_txtID      = Request["p_emp_search"].ToString();
        p_work_mon   = Request["p_month"].ToString();
        p_lang       = Request["p_lang"].ToString();

        string TemplateFile;

        if (p_lang == "ENG")
        {
            TemplateFile = "rpt_hrti00400_2_eng.xls";
        }
        else
        {
            TemplateFile = "rpt_hrti00400_2_vie.xls";
        }

        string TempFile = "../../../../system/temp/rpt_hrti00400_2_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;


        string    para      = "'" + p_tco_org_pk + "','" + p_wg + "','" + p_work_mon + "','" + p_from + "','" + p_to + "','" + p_txtID + "'";
        DataTable dt_detail = CtlLib.TableReadOpenCursor("ST_HR_RPT_HRTI00400_2", para);

        string sql_day
            = "select substr(c.car_date,-2), c.hol_type from thr_calendar c  " +
              "where c.del_if=0  " +
              "and c.car_date between '" + p_from + "' and '" + p_to + "' " +
              "and c.tco_company_pk = (select g.tco_company_pk " +
              "                                       from tco_org g " +
              "                                      where g.del_if = 0 and g.pk = '" + p_tco_org_pk + "') " +
              " order by c.car_date ";

        DataTable dt_day = CtlLib.TableReadOpen(sql_day);

        string sql_date
            = "select  " +
              "    to_char(to_date('" + p_work_mon + "', 'yyyymm'), 'mm yyyy')  " +
              "  from dual ";

        DataTable dt_date = CtlLib.TableReadOpen(sql_date);

        if (dt_detail.Rows.Count == 0)
        {
            Response.Write("Không có dữ liệu!");
            Response.End();
        }
        int row_loop = 5;
        int l_col    = dt_detail.Columns.Count - 31;
        int row_emp  = dt_detail.Rows.Count;
        int row_day  = dt_day.Rows.Count;
        int count    = 0;

        int s_row      = 4;
        int pos        = 4;
        int s_col      = 8;
        int temp       = 0;
        int l_col_pass = 6;

        for (int d = 0; d < dt_day.Rows.Count; d++)
        {
            exSheet.Cells[4, s_col + d].Value = dt_day.Rows[d][0].ToString();
        }
        for (int l_addrow = 1; l_addrow < dt_detail.Rows.Count; l_addrow++)
        {
            exSheet.Range[row_loop + l_addrow, 1].Rows.EntireRow.Insert(); //insert row new of sheet
        }
        if (p_from != "" && p_to != "")
        {
            if (p_lang == "ENG")
            {
                exSheet.Cells["A2"].Value = "From Date " + dt_detail.Rows[0][0].ToString() + " -----" + " To Date " + dt_detail.Rows[0][1].ToString();
            }
            else
            {
                exSheet.Cells["A2"].Value = "Từ ngày " + dt_detail.Rows[0][0].ToString() + " -----" + " Đến ngày " + dt_detail.Rows[0][1].ToString();
            }
        }
        // xu ly du lieu do vao excel
        if (dt_detail.Rows.Count > 0)
        {
            for (int l_row = 0; l_row < dt_detail.Rows.Count; l_row++)
            {
                count++;
                exSheet.Cells[row_loop + l_row, 1].Value = count;
                // do du lieu vao cac dong
                for (int col = 2; col < l_col; col++)
                {
                    if (IsNumeric(dt_detail.Rows[l_row][col].ToString()) && col != 2)
                    {
                        exSheet.Cells[row_loop + l_row, col].Value = double.Parse(dt_detail.Rows[l_row][col].ToString());
                    }
                    else
                    {
                        exSheet.Cells[row_loop + l_row, col].Value = dt_detail.Rows[l_row][col].ToString();
                    }
                }
                pos++;
                for (int col = s_col; col < s_col + 31; col++)
                {
                    if (dt_detail.Rows[l_row][col + 31].ToString() == "SUN")
                    {
                        exSheet.Range[row_loop + l_row, col].Interior.Color = Color.SkyBlue;
                    }
                    if (dt_detail.Rows[l_row][col + 31].ToString() == "HOL")
                    {
                        exSheet.Range[row_loop + l_row, col].Interior.Color = Color.Pink;

                        if (dt_detail.Rows[l_row][col].ToString() == "")
                        {
                            exSheet.Cells[row_loop + l_row, col].Value = "PH";
                        }
                    }
                }
            }
        }
        for (int row = 0; row < row_day; row++)
        {
            temp = 31 - row_day;
            switch (temp)
            {
            case 3:

                exSheet.Range["AJ1"].Columns.Hidden = true;
                exSheet.Range["AK1"].Columns.Hidden = true;
                exSheet.Range["AL1"].Columns.Hidden = true;
                break;

            case 2:
                exSheet.Range["AK1"].Columns.Hidden = true;
                exSheet.Range["AL1"].Columns.Hidden = true;
                break;

            case 1:
                exSheet.Range["AL1"].Columns.Hidden = true;
                break;
            }
            switch (dt_day.Rows[row][1].ToString())
            {
            case "SUN":
                exSheet.Range[s_row, row + s_col].Interior.Color = Color.Green;
                break;

            case "HOL":
                exSheet.Range[s_row, row + s_col, s_row, row + s_col].Interior.Color = Color.Pink;
                break;
            }
        }

        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        range = exSheet.Range["A1"];
        // hide row A5
        // range.Rows.Hidden = true;

        // font bold header

        /*range = exSheet.Range["A1:AC1"];
         * range.Rows[4].Font.Bold = true;*/

        exBook.SaveAs(TempFile);
        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        //Response.WriteFile(pdfFilePath);
        Response.WriteFile(TempFile);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 17
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        String module       = Request["module"];
        string TemplateFile = "rpt_gsau00600_0.xls";
        string TempFile     = "../../../../system/temp/rpt_gsau00600_0_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        string SQL
            = "SELECT MN.PARENT_ID,MN.PARENT_NM,MN.MENU_ID,MN.form_nm,FORM_LNM , FORM_FNM,MN.MN_LEVEL,MN.FORM_URL,FORM_DESC, MN.PK,MN.parent_pk " +
              "     FROM(select TF.PK PK,TF.MENU_ID,tf.form_nm,tf.FORM_LNM , tf. FORM_FNM, level MN_LEVEL,TF.FORM_URL ,tf. FORM_DESC" +
              "                ,(select a.menu_id from tes_obj a where a.del_if=0 and a.pk=tf.p_pk) parent_id " +
              "                ,(select a.form_nm from tes_obj a where a.del_if=0 and a.pk=tf.p_pk) parent_nm " +
              "                ,(select a.PK from tes_obj a where a.del_if=0 and a.pk=tf.p_pk) parent_pk " +
              "                                   from tes_obj tf " +
              "                                  where tf.del_if = 0  and nvl(TF.USE_YN,'N') = 'Y' ";

        if (module == null || module == "")
        {
            SQL += "                             start with TF.p_pk = 0 and tf.del_if = 0 and nvl(TF.USE_YN,'N')='Y' and p_pk is not null ";
        }
        else
        {
            SQL += "                             start with TF.pk = " + module + " and tf.del_if = 0 and nvl(TF.USE_YN,'N')='Y' and p_pk is not null ";
        }

        SQL += "                             connect by prior tf.pk = tf.p_pk and tf.del_if = 0  and nvl(TF.USE_YN,'N') = 'Y' " +
               "                              order siblings by no) MN  where  parent_id is not null";



        // Response.Write(SQL);
        //Response.End();

        DataTable dt_emp = CtlLib.TableReadOpen(SQL);
        int       irow, icol_emp;

        irow     = dt_emp.Rows.Count;
        icol_emp = dt_emp.Columns.Count - 2;
        if (irow == 0)
        {
            Response.Write("There is no data");
            Response.End();
        }

        int i, j, k, i_allow;
        int pos = 3;

        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[pos + i + 1, 1].Rows.EntireRow.Insert();
        }

        //Show data
        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[pos + i, 1].Value = i + 1;
            for (j = 0; j < icol_emp; j++)
            {
                exSheet.Cells[pos + i, j + 2].Value = dt_emp.Rows[i][j].ToString();
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }



        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 18
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string TemplateFile = "rpt_hrti01300_2.xls";
        string TempFile     = "../../../../system/temp/rpt_hrti01300_2_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet   exSheet = exBook.Worksheets[1];
        IRange       range;
        MemoryStream stream;
        Bitmap       b;

        string p_tco_dept_pk, p_thr_group_pk, p_status, p_emp_id, p_work_mon, p_from_dt, p_to_dt, p_dept_nm, p_salary_period, p_company_pk;

        p_tco_dept_pk   = Request["l_tco_dept_pk"].ToString();
        p_thr_group_pk  = Request["l_thr_group_pk"].ToString();
        p_status        = Request["l_status"].ToString();
        p_emp_id        = Request["l_emp_id"].ToString();
        p_work_mon      = Request["l_work_mon"].ToString();
        p_to_dt         = Request["l_to_dt"].ToString();
        p_from_dt       = Request["l_from_dt"].ToString();
        p_dept_nm       = Request["l_dept_nm"].ToString();
        p_salary_period = Request["l_SalaryPeriod"].ToString();
        //p_company_pk = Request["l_company_pk"].ToString();

        //-----------------information of company-----------------
        string SQL_Com
            = "select  a.PARTNER_LNAME,a.ADDR1,a.PHONE_NO,b.data,to_char(to_date('201210','yyyymm'),'MON-YYYY') " +
              "from tco_company a, tco_bpphoto b " +
              "where a.DEL_IF=0  and b.del_if(+) = 0 and a.TCO_BPPHOTO_PK = b.pk(+)" +
              "and a.pk in ( select tco_company_pk from  " +
              "               tco_org  f " +
              "               where  f.pk IN ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_tco_dept_pk + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_tco_dept_pk + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_tco_dept_pk + "' = 'ALL') " +
              "and rownum=1 ";

        DataTable dt_Com = CtlLib.TableReadOpen(SQL_Com);

        exSheet.Range["C1"].Value = dt_Com.Rows[0][0].ToString();
        exSheet.Range["C2"].Value = dt_Com.Rows[0][1].ToString();
        exSheet.Range["C3"].Value = dt_Com.Rows[0][2].ToString();

        if (!string.IsNullOrEmpty(dt_Com.Rows[0][3].ToString()))
        {
            byte[] MyData = new byte[0];
            MyData = (byte[])dt_Com.Rows[0][3];
            stream = new MemoryStream(MyData);

            b = new Bitmap(stream);
            exSheet.Cells["B1"].Select();
            exSheet.Shapes.AddPicture(CtlLib.ResizeImage(b, 75, 40));
        }

        //-----------------working time-----------------


        string SQL = "select c.org_NM,b.emp_id,b.full_name  " +
                     ",to_char(to_date(b.join_dt,'yyyymmdd'),'dd/mm/yyyy') join_dt " +
                     ",MAX(DECODE(A.ABSENCE_DT,'" + p_from_dt + "',abs_type.char_1 || nvl(absence_time,8),'')) as in_1  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+1,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_2  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+2,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_3  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+3,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_4  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+4,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_5  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+5,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_6  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+6,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_7  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+7,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_8  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+8,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_9  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+9,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_10  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+10,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_11  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+11,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_12  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+12,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_13  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+13,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_14  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+14,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_15  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+15,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_16  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+16,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_17  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+17,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_18  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+18,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_19  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+19,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_20  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+20,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_21  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+21,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_22  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+22,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_23  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+23,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_24  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+24,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_25  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+25,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_26  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+26,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_27  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+27,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_28  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+28,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_29  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+29,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_30  " +
                     ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+30,'YYYYMMDD'),abs_type.char_1 || nvl(absence_time,8),'')) as in_31  " +
                     " , NVL (c.seq, 0), SUM (NVL (a.absence_time, 0) / 8) AS total_abs  " +
                     "from thr_absence a,thr_employee b,tco_org c  " +
                     ",(select d.code,d.char_1 from vhr_hr_code d where id='HR0003') abs_type " +
                     "where a.del_if=0 and b.del_if=0 and c.del_if=0  " +
                     "and a.THR_EMP_PK=b.pk and b.tco_org_pk=c.pk " +
                     "and a.ABSENCE_TYPE=abs_type.code  " +
                     "and a.absence_dt between '" + p_from_dt + "' and '" + p_to_dt + "' " +
                     "and (b.tco_org_pk in ( " +
                     "                              SELECT     g.pk " +
                     "                                    FROM tco_org g " +
                     "                                   WHERE g.del_if = 0 " +
                     "                              START WITH g.pk = " +
                     "                                            DECODE ('" + p_tco_dept_pk + "', " +
                     "                                                    'ALL', 0, " +
                     "                                                    '" + p_tco_dept_pk + "' " +
                     "                                                   ) " +
                     "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
                     "                        OR '" + p_tco_dept_pk + "' = 'ALL') " +
                     "and decode('" + p_thr_group_pk + "','ALL','ALL',b.thr_wg_pk)='" + p_thr_group_pk + "'" +
                     " and decode('" + p_salary_period + "','ALL','ALL',nvl(b.sal_period,' '))='" + p_salary_period + "' " +
                     "and b.emp_id like '%" + p_emp_id + "%' " +
                     "and decode('" + p_status + "','ALL','ALL',b.status)='" + p_status + "'" +
                     " GROUP BY A.THR_EMP_PK,c.org_NM,b.emp_id,b.full_name,b.join_dt,NVL (c.seq, 0) " +
                     " order by NVL (c.seq, 0),c.org_NM,b.emp_id ";

        string SQL1 = "select c.org_NM,b.emp_id,b.full_name  " +
                      ",to_char(to_date(b.join_dt,'yyyymmdd'),'dd/mm/yyyy') join_dt  " +
                      ",MAX(DECODE(A.ABSENCE_DT,'" + p_from_dt + "',abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ) ,'')) as in_1  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+1,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_2  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+2,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_3  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+3,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_4  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+4,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_5  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+5,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_6  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+6,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_7  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+7,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_8  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+8,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_9  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+9,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_10  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+10,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_11  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+11,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_12  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+12,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_13  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+13,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_14  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+14,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_15  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+15,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_16  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+16,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_17  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+17,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_18  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+18,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_19  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+19,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_20  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+20,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_21  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+21,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_22  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+22,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_23  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+23,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_24  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+24,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_25  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+25,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_26  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+26,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_27  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+27,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_28  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+28,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_29  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+29,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_30  " +
                      ",MAX(DECODE(A.ABSENCE_DT,TO_CHAR(TO_DATE('" + p_from_dt + "','YYYYMMDD')+30,'YYYYMMDD'),abs_type.char_1 || decode(nvl(absence_time,8),8,'','(' || absence_time || ')' ),'')) as in_31  " +
                      " , NVL (c.seq, 0), SUM (NVL (a.absence_time, 0) / 8) AS total_abs  " +
                      "from thr_absence a,thr_employee b,tco_org c  " +
                      ",(select d.code,d.char_1 from vhr_hr_code d where id='HR0003') abs_type " +
                      "where a.del_if=0 and b.del_if=0 and c.del_if=0  " +
                      "and a.THR_EMP_PK=b.pk and b.tco_org_pk=c.pk " +
                      "and a.ABSENCE_TYPE=abs_type.code  " +
                      "and a.absence_dt between '" + p_from_dt + "' and '" + p_to_dt + "' " +
                      "and B.LEFT_DT between '" + p_from_dt + "' and '" + p_to_dt + "' " +
                      "and B.LEFT_DT IS NOT NULL " +
                      "and (b.tco_org_pk in ( " +
                      "                              SELECT     g.pk " +
                      "                                    FROM tco_org g " +
                      "                                   WHERE g.del_if = 0 " +
                      "                              START WITH g.pk = " +
                      "                                            DECODE ('" + p_tco_dept_pk + "', " +
                      "                                                    'ALL', 0, " +
                      "                                                    '" + p_tco_dept_pk + "' " +
                      "                                                   ) " +
                      "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
                      "                        OR '" + p_tco_dept_pk + "' = 'ALL') " +
                      "and decode('" + p_thr_group_pk + "','ALL','ALL',b.thr_wg_pk)='" + p_thr_group_pk + "'" +
                      "and b.emp_id like '%" + p_emp_id + "%' " +
                      "and decode('" + p_status + "','ALL','ALL',b.status)='" + p_status + "'" +
                      " and decode('" + p_salary_period + "','ALL','ALL',nvl(b.sal_period,' '))='" + p_salary_period + "' " +
                      " GROUP BY A.THR_EMP_PK,c.org_NM,b.emp_id,b.full_name,b.join_dt,NVL (c.seq, 0) " +
                      " order by NVL (c.seq, 0),c.org_NM,b.emp_id ";

        if (p_status == "R")
        {
            SQL = SQL1;
        }
        //Response.Write(SQL);
        //Response.End();
        DataTable dt_Emp = CtlLib.TableReadOpen(SQL);

        string SQL_Cal = "select substr(t.car_date,-2),t.hol_type " +
                         "from thr_calendar T,thr_close m " +
                         "where t.del_if=0 and m.del_if=0 and m.id='" + p_salary_period + "' and m.month_close='" + p_work_mon + "' " +
                         " and t.tco_company_pk=(select tco_company_pk from tco_org g where g.del_if=0 and g.pk='" + p_tco_dept_pk + "' ) " +
                         "    and t.car_date between m.from_dt and '" + p_to_dt + "' order by t.car_date ";

        //Response.Write(SQL_Cal);
        //Response.End();
        DataTable dt_Cal = CtlLib.TableReadOpen(SQL_Cal);

        int row_emp = dt_Emp.Rows.Count;
        int row_cal = dt_Cal.Rows.Count;

        int s_col = 7;
        int s_row = 6;

        exSheet.Range["U2"].Value = string.Format("ABSENCE SUMMARY - {0}", p_dept_nm);
        exSheet.Range["T3"].Value = dt_Com.Rows[0][4].ToString();

        //header columns
        for (int row = 0; row < row_cal; row++)
        {
            exSheet.Range[4, s_col + row, 5, s_col + row].Value = double.Parse(dt_Cal.Rows[row][0].ToString());
        }

        s_row = 6;
        //insert new row
        for (int row = 1; row < row_emp; row++)
        {
            exSheet.Range["A7"].EntireRow.Insert();
        }

        //exSheet.Range["A6:AM6"].Rows.Copy(exSheet.Range["A" + (s_row) + ":" + "AM" + ((row_emp -1) + s_row)], XlPasteType.xlPasteAll);

        for (int row = 0; row < row_emp; row++)
        {
            exSheet.Range["A" + (s_row + row) + ":A" + (s_row + row)].Value = row + 1;
            exSheet.Range["B" + (s_row + row) + ":B" + (s_row + row)].Value = dt_Emp.Rows[row]["ORG_NM"].ToString();
            exSheet.Range["C" + (s_row + row) + ":C" + (s_row + row)].Value = dt_Emp.Rows[row]["EMP_ID"].ToString();
            exSheet.Range["D" + (s_row + row) + ":D" + (s_row + row)].Value = dt_Emp.Rows[row]["FULL_NAME"].ToString();
            exSheet.Range["E" + (s_row + row) + ":E" + (s_row + row)].Value = dt_Emp.Rows[row]["JOIN_DT"].ToString();

            GeneralData(exSheet, "1", "G", s_row + row, row, "IN_1", dt_Emp);
            GeneralData(exSheet, "1", "H", s_row + row, row, "IN_2", dt_Emp);
            GeneralData(exSheet, "1", "I", s_row + row, row, "IN_3", dt_Emp);
            GeneralData(exSheet, "1", "J", s_row + row, row, "IN_4", dt_Emp);
            GeneralData(exSheet, "1", "K", s_row + row, row, "IN_5", dt_Emp);
            GeneralData(exSheet, "1", "L", s_row + row, row, "IN_6", dt_Emp);
            GeneralData(exSheet, "1", "M", s_row + row, row, "IN_7", dt_Emp);
            GeneralData(exSheet, "1", "N", s_row + row, row, "IN_8", dt_Emp);
            GeneralData(exSheet, "1", "O", s_row + row, row, "IN_9", dt_Emp);
            GeneralData(exSheet, "1", "P", s_row + row, row, "IN_10", dt_Emp);
            GeneralData(exSheet, "1", "Q", s_row + row, row, "IN_11", dt_Emp);
            GeneralData(exSheet, "1", "R", s_row + row, row, "IN_12", dt_Emp);
            GeneralData(exSheet, "1", "S", s_row + row, row, "IN_13", dt_Emp);
            GeneralData(exSheet, "1", "T", s_row + row, row, "IN_14", dt_Emp);
            GeneralData(exSheet, "1", "U", s_row + row, row, "IN_15", dt_Emp);
            GeneralData(exSheet, "1", "V", s_row + row, row, "IN_16", dt_Emp);
            GeneralData(exSheet, "1", "W", s_row + row, row, "IN_17", dt_Emp);
            GeneralData(exSheet, "1", "X", s_row + row, row, "IN_18", dt_Emp);
            GeneralData(exSheet, "1", "Y", s_row + row, row, "IN_19", dt_Emp);
            GeneralData(exSheet, "1", "Z", s_row + row, row, "IN_20", dt_Emp);
            GeneralData(exSheet, "1", "AA", s_row + row, row, "IN_21", dt_Emp);
            GeneralData(exSheet, "1", "AB", s_row + row, row, "IN_22", dt_Emp);
            GeneralData(exSheet, "1", "AC", s_row + row, row, "IN_23", dt_Emp);
            GeneralData(exSheet, "1", "AD", s_row + row, row, "IN_24", dt_Emp);
            GeneralData(exSheet, "1", "AF", s_row + row, row, "IN_26", dt_Emp);
            GeneralData(exSheet, "1", "AG", s_row + row, row, "IN_27", dt_Emp);
            GeneralData(exSheet, "1", "AH", s_row + row, row, "IN_28", dt_Emp);
            GeneralData(exSheet, "1", "AI", s_row + row, row, "IN_29", dt_Emp);
            GeneralData(exSheet, "1", "AJ", s_row + row, row, "IN_30", dt_Emp);
            GeneralData(exSheet, "1", "AK", s_row + row, row, "IN_31", dt_Emp);
            GeneralData(exSheet, "1", "AL", s_row + row, row, "TOTAL_ABS", dt_Emp);

            //s_row++;
        }

        // draw color
        s_row = 6;
        int temp = 0;

        temp = 31 - row_cal;
        switch (temp)
        {
        case 3:
            exSheet.Range["AI1"].Columns.Hidden = true;
            break;

        case 2:
            exSheet.Range["AJ1"].Columns.Hidden = true;
            break;

        case 1:
            exSheet.Range["AK1"].Columns.Hidden = true;
            break;
        }

        for (int row = 0; row < row_cal; row++)
        {
            switch (dt_Cal.Rows[row][1].ToString())
            {
            case "SUN":
                exSheet.Range[s_row, row + s_col, ((row_emp) + s_row - 1), row + s_col].Interior.Color = Color.SkyBlue;
                break;

            case "HOL":
                exSheet.Range[s_row, row + s_col, ((row_emp) + s_row - 1), row + s_col].Interior.Color = Color.Pink;
                break;
            }
        }

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 19
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept_pk       = Request["l_tco_dept_pk"].ToString();
        string p_group_pk      = Request["l_thr_group_pk"].ToString();
        string p_from_dt       = Request["l_from_dt"].ToString();
        string p_to_dt         = Request["l_to_dt"].ToString();
        string p_search        = Request["l_search_by"].ToString();
        string p_input         = Request["l_search_tmp"].ToString();
        string p_meal_emp_type = Request["l_meal_emp_type"].ToString();
        string p_meal_type     = Request["l_meal_type"].ToString();
        string p_meal_times    = Request["l_meal_times"].ToString();

        string TemplateFile = "rpt_chrm00010_01.xls";
        string TempFile     = "../../../../system/temp/rpt_chrm00010_01_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];


        string SQL
            = "SELECT (    SELECT g.ORG_NM " +
              "              FROM TCO_ORG g " +
              "             WHERE g.del_if = 0 AND G.ORG_TYPE = '06' " +
              "        START WITH g.pk = e.pk " +
              "        CONNECT BY PRIOR g.p_pk = g.pk) C0, e.org_nm C1, " +
              "         b.WORKGROUP_NM C2, " +
              "         a.EMP_ID AS emp_id, " +
              "         a.FULL_NAME AS full_name, " +
              "         to_char(to_date(a.join_dt,'yyyymmdd'),'dd/mm/yyyy') AS join_dt, " +
              "         to_char(to_date(d.WORK_DT,'yyyymmdd'),'dd/mm/yyyy') AS work_dt, " +
              "         C.REMARK C7, F.TIME_IN,F.TIME_OUT C9 " +
              ",NULLIF(MEAL.MEAL_01,'00:00'),NULLIF(MEAL.MEAL_02,'00:00'),NULLIF(MEAL.MEAL_03,'00:00'),NULLIF(MEAL.MEAL_04,'00:00'),NULLIF(MEAL.MEAL_05,'00:00') " +
              "    FROM THR_EMPLOYEE A, THR_WORK_GROUP B,THR_WORK_SHIFT C,THR_WG_SCH_DETAIL D,tco_org e,THR_TIME_MACHINE F " +
              " ,(SELECT M1.THR_EMP_PK, " +
              "               M1.WORK_DT_OF_SHIFT, " +
              "               MAX (CASE WHEN M1.MEAL_TIMES = '01' THEN M1.TIME_IN ELSE '00:00' END) MEAL_01, " +
              "               MAX (CASE WHEN M1.MEAL_TIMES = '02' THEN M1.TIME_IN ELSE '00:00' END) MEAL_02, " +
              "               MAX (CASE WHEN M1.MEAL_TIMES = '03' THEN M1.TIME_IN ELSE '00:00' END) MEAL_03, " +
              "               MAX (CASE WHEN M1.MEAL_TIMES = '04' THEN M1.TIME_IN ELSE '00:00' END) MEAL_04, " +
              "               MAX (CASE WHEN M1.MEAL_TIMES = '05' THEN M1.TIME_IN ELSE '00:00' END) MEAL_05 " +
              "               FROM THR_MEAL_CHECKING M1  " +
              "               WHERE M1.DEL_IF=0  " +
              "               AND M1.WORK_DT_OF_SHIFT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
              "               AND M1.THR_EMP_PK IS NOT NULL  " +
              "               GROUP BY M1.THR_EMP_PK,M1.WORK_DT_OF_SHIFT) MEAL " +
              "   WHERE A.DEL_IF = 0 AND B.DEL_IF = 0 " +
              "         AND C.DEL_IF = 0 " +
              "         AND D.del_if = 0 AND E.DEL_IF=0 AND F.DEL_IF=0 AND D.THR_EMP_PK=F.THR_EMP_PK AND D.WORK_DT=F.WORK_DT " +
              "         AND A.THR_WG_PK = B.PK " +
              "         AND A.pk = D.thr_emp_pk " +
              "         AND a.tco_org_pk = e.pk AND D.THR_WS_PK=C.PK AND D.WORK_DT=MEAL.WORK_DT_OF_SHIFT(+) AND D.THR_EMP_PK=MEAL.THR_EMP_PK(+) " +
              "         AND D.WORK_DT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
              "         AND DECODE ('" + p_group_pk + "', 'ALL', 'ALL', B.PK) = '" + p_group_pk + "' " +
              "         AND  A.TCO_ORG_PK IN " +
              "                    (    SELECT g.pk " +
              "                           FROM TCO_ORG g " +
              "                          WHERE g.del_if = 0 " +
              "                     START WITH g.pk = case when '" + p_dept_pk + "' = 'ALL' then g.pk else to_number('" + p_dept_pk + "') end " +
              "                     CONNECT BY PRIOR g.pk = g.p_pk) " +
              "         AND (   DECODE ('" + p_search + "', " +
              "                         '1', UPPER (A.EMP_ID), " +
              "                         '2', A.ID_NUM, " +
              "                         '3', UPPER (A.FULL_NAME)) LIKE " +
              "                    '%' || UPPER ('" + p_input + "') || '%' " +
              "              OR '" + p_input + "' IS NULL) " +
              "ORDER BY D.WORK_DT,E.ORG_NM,emp_id ";

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);

        int irow = dt_emp.Rows.Count;
        int icol = dt_emp.Columns.Count;

        if (irow == 0)
        {
            Response.Write("There is no data.");
            Response.End();
        }



        //Khai bao bien
        int i, j, pos = 5;

        //Insert new row

        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[pos + i + 1, 1].Rows.EntireRow.Insert();
        }

        //Title value
        exSheet.Cells["F2"].Value = p_from_dt.Substring(6, 2).ToString() + "/" + p_from_dt.Substring(4, 2).ToString() + "/" + p_from_dt.Substring(0, 4).ToString();
        exSheet.Cells["I2"].Value = p_to_dt.Substring(6, 2).ToString() + "/" + p_to_dt.Substring(4, 2).ToString() + "/" + p_to_dt.Substring(0, 4).ToString();

        //Show data
        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[pos + i, 1].Value = i + 1;
            for (j = 0; j < icol; j++)
            {
                // if(j>=10)
                //   exSheet.Cells[pos + i, j+2].Value =double.Parse(dt_emp.Rows[i][j].ToString());
                //else
                exSheet.Cells[pos + i, j + 2].Value = dt_emp.Rows[i][j].ToString();
            }
        }



        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept_pk, p_work_mon, p_from_dt, p_to_dt, p_nation, p_group_pk, p_meal_emp_type;

        p_dept_pk       = Request["l_tco_dept_pk"].ToString();
        p_group_pk      = Request["l_thr_group_pk"].ToString();
        p_work_mon      = Request["l_work_mon"].ToString();
        p_to_dt         = Request["l_to_dt"].ToString();
        p_from_dt       = Request["l_from_dt"].ToString();
        p_meal_emp_type = Request["l_meal_emp_type"].ToString();

        string TemplateFile = "rpt_chrm00020_2.xls";
        string TempFile     = "../../../../system/temp/rpt_chrm00020_2_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        string SQL_CODE
            = "SELECT MAX(CASE WHEN NUM_1 = 1 THEN NVL(NUM_2,0) ELSE 0 END) " +
              "          ,MAX(CASE WHEN NUM_1 = 2 THEN NVL(NUM_2,0) ELSE 0 END) " +
              "    FROM vhr_hr_code c " +
              "   WHERE id = 'HR0191' ";

        DataTable dt_hr_code = CtlLib.TableReadOpen(SQL_CODE);

        double meal_main_amt  = double.Parse(dt_hr_code.Rows[0][0].ToString());
        double snack_time_amt = double.Parse(dt_hr_code.Rows[0][1].ToString());

        int ncal = int.Parse(p_to_dt.Substring(6, 2).ToString()) - int.Parse(p_from_dt.Substring(6, 2).ToString()) + 1;
        int i;

        //======================================================================================================================
        string SQL_1
            = "  SELECT 1 seq,'',MAX((    SELECT g.ORG_NM  " +
              "              FROM TCO_ORG g  " +
              "             WHERE g.del_if = 0 AND G.ORG_TYPE = '06'  " +
              "        START WITH g.pk = b.pk  " +
              "        CONNECT BY PRIOR g.p_pk = g.pk)) div_nm " +
              "         ,B.ORG_NM org_nm " +
              "         ,C.WORKGROUP_NM group_nm " +
              "         ,D.EMP_ID EMP_ID " +
              "         ,D.FULL_NAME " +
              "         ,TO_CHAR (TO_DATE (D.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy') join_dt " +
              "         ,SUM (CASE WHEN F.WORK_DT_OF_SHIFT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
              "         ,SUM (CASE WHEN F.WORK_DT_OF_SHIFT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
              "         ,SUM (CASE WHEN F.WORK_DT_OF_SHIFT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
              "         ,SUM (CASE WHEN F.WORK_DT_OF_SHIFT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";

        for (i = 1; i < ncal; i++)
        {
            SQL_1 = SQL_1 + "         ,SUM (CASE WHEN F.WORK_DT_OF_SHIFT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
                    "         ,SUM (CASE WHEN F.WORK_DT_OF_SHIFT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
                    "         ,SUM (CASE WHEN F.WORK_DT_OF_SHIFT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
                    "         ,SUM (CASE WHEN F.WORK_DT_OF_SHIFT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";
        }

        SQL_1 = SQL_1 + "    FROM THR_MEAL_CHECKING F, TCO_ORG B, THR_WORK_GROUP C, THR_EMPLOYEE D " +
                "   WHERE  F.DEL_IF = 0 AND B.DEL_IF = 0 AND C.DEL_IF =0 AND D.DEL_IF =0  " +
                "         AND F.THR_EMP_PK = d.pk " +
                "         AND F.TCO_ORG_PK = b.pk " +
                "         and D.THR_WG_PK = c.pk  " +
                "         AND F.WORK_DT_OF_SHIFT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
                "         AND F.MEAL_TIMES IS NOT NULL " +
                "         AND F.MEAL_EMP_TYPE = 'IN' " +
                "         AND DECODE ('" + p_group_pk + "', 'ALL', 'ALL', D.THR_WG_PK) = '" + p_group_pk + "'  " +
                "         AND D.TCO_ORG_PK IN  " +
                "              (    SELECT g.pk  " +
                "                     FROM TCO_ORG g  " +
                "                    WHERE g.del_if = 0  " +
                "               START WITH g.pk =  " +
                "                             CASE  " +
                "                                WHEN '" + p_dept_pk + "' = 'ALL' THEN g.pk  " +
                "                                ELSE TO_NUMBER ('" + p_dept_pk + "')  " +
                "                             END  " +
                "               CONNECT BY PRIOR g.pk = g.p_pk)  " +
                "     GROUP BY D.EMP_ID,B.ORG_NM,C.WORKGROUP_NM,D.FULL_NAME,D.JOIN_DT " +
                " ORDER BY seq,div_nm,org_nm,EMP_ID     ";

        //======================================================================================================================
        string SQL_2
            = "  SELECT 2 seq,'','' div_nm " +
              "         ,'' org_nm " +
              "         ,'' group_nm " +
              "         ,F.EMP_ID EMP_ID " +
              "         ,F.FULL_NAME " +
              "         ,'' join_dt " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";

        for (i = 1; i < ncal; i++)
        {
            SQL_2 = SQL_2 + "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
                    "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
                    "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
                    "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";
        }
        SQL_2 = SQL_2 + "    FROM THR_MEAL_CHECKING F " +
                "   WHERE  F.DEL_IF = 0   " +
                "         AND F.WORK_DT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
                "         AND F.MEAL_TIMES IS NOT NULL " +
                "         AND F.MEAL_EMP_TYPE = 'OUT' " +
                "     GROUP BY F.EMP_ID,F.FULL_NAME " +
                " ORDER BY seq,div_nm,org_nm,EMP_ID     ";

        //======================================================================================================================
        string SQL_3
            = "  SELECT 3 seq,'','' div_nm " +
              "         ,'' org_nm " +
              "         ,'' group_nm " +
              "         ,'' EMP_ID " +
              "         ,F.VISITOR_NM " +
              "         ,'' join_dt " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN NVL(F.MEAL_QTY,0) ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN NVL(F.MEAL_QTY,0) ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN NVL(F.MEAL_QTY,0) ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN NVL(F.MEAL_QTY,0) ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";

        for (i = 1; i < ncal; i++)
        {
            SQL_3 = SQL_3 + "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN NVL(F.MEAL_QTY,0) ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
                    "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN NVL(F.MEAL_QTY,0) ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
                    "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN NVL(F.MEAL_QTY,0) ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
                    "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN NVL(F.MEAL_QTY,0) ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";
        }
        SQL_3 = SQL_3 + "    FROM THR_MEAL_VISITOR F " +
                "   WHERE  F.DEL_IF = 0   " +
                "         AND F.WORK_DT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
                "         AND F.MEAL_TIMES IS NOT NULL " +
                "    GROUP BY F.VISITOR_NM " +
                " ORDER BY seq,div_nm,org_nm,EMP_ID     ";

        //======================================================================================================================
        string SQL_ALL
            = "  SELECT 1 seq,'',MAX((    SELECT g.ORG_NM  " +
              "              FROM TCO_ORG g  " +
              "             WHERE g.del_if = 0 AND G.ORG_TYPE = '06'  " +
              "        START WITH g.pk = b.pk  " +
              "        CONNECT BY PRIOR g.p_pk = g.pk)) div_nm " +
              "         ,B.ORG_NM org_nm " +
              "         ,C.WORKGROUP_NM group_nm " +
              "         ,D.EMP_ID EMP_ID " +
              "         ,D.FULL_NAME " +
              "         ,TO_CHAR (TO_DATE (D.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy') join_dt " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
              "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";

        for (i = 1; i < ncal; i++)
        {
            SQL_ALL = SQL_ALL + "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";
        }

        SQL_ALL = SQL_ALL + "    FROM THR_MEAL_CHECKING F, TCO_ORG B, THR_WORK_GROUP C, THR_EMPLOYEE D " +
                  "   WHERE  F.DEL_IF = 0 AND B.DEL_IF = 0 AND C.DEL_IF =0 AND D.DEL_IF =0  " +
                  "         AND F.THR_EMP_PK = d.pk " +
                  "         AND F.TCO_ORG_PK = b.pk " +
                  "         and D.THR_WG_PK = c.pk  " +
                  "         AND F.WORK_DT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
                  "         AND F.MEAL_TIMES IS NOT NULL " +
                  "         AND F.MEAL_EMP_TYPE = 'IN' " +
                  "         AND DECODE ('" + p_group_pk + "', 'ALL', 'ALL', D.THR_WG_PK) = '" + p_group_pk + "'  " +
                  "         AND D.TCO_ORG_PK IN  " +
                  "              (    SELECT g.pk  " +
                  "                     FROM TCO_ORG g  " +
                  "                    WHERE g.del_if = 0  " +
                  "               START WITH g.pk =  " +
                  "                             CASE  " +
                  "                                WHEN '" + p_dept_pk + "' = 'ALL' THEN g.pk  " +
                  "                                ELSE TO_NUMBER ('" + p_dept_pk + "')  " +
                  "                             END  " +
                  "               CONNECT BY PRIOR g.pk = g.p_pk)  " +
                  "     GROUP BY D.EMP_ID,B.ORG_NM,C.WORKGROUP_NM,D.FULL_NAME,D.JOIN_DT " +
                  "    UNION ALL " +
                  "    SELECT 2 seq,'','' div_nm " +
                  "         ,'' org_nm " +
                  "         ,'' group_nm " +
                  "         ,F.EMP_ID EMP_ID " +
                  "         ,F.FULL_NAME " +
                  "         ,'' join_dt " +
                  "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
                  "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
                  "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
                  "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";
        for (i = 1; i < ncal; i++)
        {
            SQL_ALL = SQL_ALL + "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN 1 ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN 1 ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";
        }
        SQL_ALL = SQL_ALL + "    FROM THR_MEAL_CHECKING F " +
                  "   WHERE  F.DEL_IF = 0   " +
                  "         AND F.WORK_DT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
                  "         AND F.MEAL_TIMES IS NOT NULL " +
                  "         AND F.MEAL_EMP_TYPE = 'OUT' " +
                  "     GROUP BY F.EMP_ID,F.FULL_NAME " +
                  "    UNION ALL " +
                  "    SELECT 3 seq,'','' div_nm " +
                  "         ,'' org_nm " +
                  "         ,'' group_nm " +
                  "         ,'' EMP_ID " +
                  "         ,F.VISITOR_NM " +
                  "         ,'' join_dt " +
                  "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN NVL(F.MEAL_QTY,0) ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
                  "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN NVL(F.MEAL_QTY,0) ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
                  "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN NVL(F.MEAL_QTY,0) ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
                  "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + 0,'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN NVL(F.MEAL_QTY,0) ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";
        for (i = 1; i < ncal; i++)
        {
            SQL_ALL = SQL_ALL + "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN NVL(F.MEAL_QTY,0) ELSE 0 END ELSE 0 END) MEAL_MAIN_QTY " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 1 THEN NVL(F.MEAL_QTY,0) ELSE 0 END * " + meal_main_amt + " ELSE 0 END) MEAL_MAIN_AMT " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN NVL(F.MEAL_QTY,0) ELSE 0 END ELSE 0 END)  SNACK_TIME_QTY " +
                      "         ,SUM (CASE WHEN F.WORK_DT = TO_CHAR(TO_DATE('" + p_from_dt + "','yyyymmdd') + " + i + ",'yyyymmdd') THEN CASE WHEN F.MEAL_TYPE = 2 THEN NVL(F.MEAL_QTY,0) ELSE 0 END * " + snack_time_amt + " ELSE 0 END) SNACK_TIME_AMT ";
        }
        SQL_ALL = SQL_ALL + "    FROM THR_MEAL_VISITOR F " +
                  "   WHERE  F.DEL_IF = 0   " +
                  "         AND F.WORK_DT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
                  "         AND F.MEAL_TIMES IS NOT NULL " +
                  "    GROUP BY F.VISITOR_NM " +
                  " ORDER BY seq,div_nm,org_nm,EMP_ID     ";

        string SQL;

        if (p_meal_emp_type == "IN")
        {
            SQL = SQL_1;
        }
        else if (p_meal_emp_type == "OUT")
        {
            SQL = SQL_2;
        }
        else if (p_meal_emp_type == "VIS")
        {
            SQL = SQL_3;
        }
        else
        {
            SQL = SQL_ALL;
        }

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);

        int irow = dt_emp.Rows.Count;
        int icol = dt_emp.Columns.Count;

        if (irow == 0)
        {
            Response.Write("There is no data.");
            Response.End();
        }


        //Khai bao bien
        int j, k, pos = 6;
        int ntotal = icol - 8, nseq = 0;        //8 cot khong sum

        double[] s_total = new double[ntotal];
        double[] g_total = new double[ntotal];
        int      ncount  = 0;

        for (j = 0; j < ntotal; j++)
        {
            s_total[j] = 0;
            g_total[j] = 0;
        }

        //Dem so luong thay doi meal type
        for (i = 0; i < irow - 1; i++)
        {
            if (dt_emp.Rows[i]["seq"].ToString() != dt_emp.Rows[i + 1]["seq"].ToString())
            {
                nseq++;
            }
        }
        //Insert new row

        for (i = 0; i < irow - 1 + nseq; i++)
        {
            exSheet.Range[pos + 1, 1].Rows.EntireRow.Insert();
        }

        //Title value
        exSheet.Cells["BL2"].Value = "Month: " + p_work_mon.Substring(4, 2).ToString() + "/" + p_work_mon.Substring(0, 4).ToString();

        //Show data
        for (i = 0; i < irow; i++)
        {
            for (k = 0; k < ntotal; k++)
            {
                s_total[k] = s_total[k] + double.Parse(dt_emp.Rows[i][k + 8].ToString());
                g_total[k] = g_total[k] + double.Parse(dt_emp.Rows[i][k + 8].ToString());
            }
            ncount++;
            exSheet.Cells[pos + i, 1].Value = ncount;
            for (j = 2; j < icol; j++)
            {
                if (j <= 7)
                {
                    exSheet.Cells[pos + i, j].Value = dt_emp.Rows[i][j].ToString();
                }
                else
                {
                    exSheet.Cells[pos + i, j].Value = double.Parse(dt_emp.Rows[i][j].ToString());
                }
            }

            //Dong sub total
            if (i == irow - 1)
            {
                exSheet.Cells[pos + i + 1, 1].Value = "Total " + ncount + " employees";
                for (k = 0; k < ntotal; k++)
                {
                    exSheet.Cells[pos + i + 1, k + 8].Value = s_total[k];
                    exSheet.Cells[pos + i + 2, k + 8].Value = g_total[k];
                }
            }
            else if (dt_emp.Rows[i]["seq"].ToString() != dt_emp.Rows[i + 1]["seq"].ToString())
            {
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, 7].Merge();
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, icol].Font.Bold = true;
                exSheet.Cells[pos + i + 1, 1].HorizontalAlignment          = XlHAlign.xlHAlignCenter;
                exSheet.Cells[pos + i + 1, 1].Value = "Total " + ncount + " employees";

                for (k = 0; k < ntotal; k++)
                {
                    exSheet.Cells[pos + i + 1, k + 8].Value = s_total[k];
                    s_total[k] = 0;
                }

                ncount = 0;
                pos++;
            }
        }

        //Xóa cột dư ở những tháng không đủ 31 ngày
        int ndel = 0;   //so luong ngày can xoa
        int cdel = 0;   //vi tri cot can xoa

        ndel = 31 - ncal;
        cdel = ncal * 4 + 7;

        for (i = 0; i < ndel * 4; i++)
        {
            exSheet.Range[1, cdel + 1].Columns.EntireColumn.Delete();
            exSheet.Range[1, cdel + 1].Columns.EntireColumn.Delete();
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept_pk = Request["l_tco_dept_pk"].ToString();
        string p_group_pk = Request["l_thr_group_pk"].ToString();
        string p_from_dt = Request["l_from_dt"].ToString();
        string p_to_dt = Request["l_to_dt"].ToString();
        string p_search = Request["l_search_by"].ToString();
        string p_input = Request["l_search_tmp"].ToString();
        string p_meal_emp_type = Request["l_meal_emp_type"].ToString();
        string p_meal_type = Request["l_meal_type"].ToString();
        string p_meal_times = Request["l_meal_times"].ToString();

        string TemplateFile = "rpt_chrm00010_05.xls";
        string TempFile = "../../../../system/temp/rpt_chrm00010_05_" + Session["User_ID"].ToString() + ".xls";
        TemplateFile = Server.MapPath(TemplateFile);
        TempFile = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];


        string SQL
        = "SELECT '','',e.org_nm, " +
        "       (    SELECT g.ORG_NM " +
        "              FROM TCO_ORG g " +
        "             WHERE g.del_if = 0 AND G.ORG_TYPE = '06' " +
        "        START WITH g.pk = e.pk " +
        "        CONNECT BY PRIOR g.p_pk = g.pk) " +
        "          DIV_NM, " +
        "         b.WORKGROUP_NM AS group_nm, " +
        "         a.EMP_ID AS emp_id, " +
        "         a.FULL_NAME AS full_name, " +
        "         to_char(to_date(a.join_dt,'yyyymmdd'),'dd/mm/yyyy') AS join_dt, " +
        "         to_char(to_date(d.WORK_DT_OF_SHIFT,'yyyymmdd'),'dd/mm/yyyy') AS work_dt, " +
        "         d.TIME_IN AS time, " +
        "         (select CODE_NM from vhr_hr_code where id='HR0191' and code =D.MEAL_TIMES) meal_times, " +
        "         decode(NVL(D.MOD_BY_HAND,'N'),'N','NO','YES'), " +
        "         decode(d.meal_emp_type,'IN','Employee of Company','Out of Company') " +
        "    FROM THR_EMPLOYEE A, " +
        "         THR_WORK_GROUP B, " +
        "         THR_MEAL_DUPLICATE D, " +
        "         tco_org e " +
        "   WHERE     A.DEL_IF = 0 AND D.DEL_IF = 0 " +
        "         AND B.DEL_IF = 0 " +
        "         AND e.del_if = 0 " +
        "         AND A.THR_WG_PK = B.PK " +
        "         AND A.pk = D.thr_emp_pk " +
        "         AND a.tco_org_pk = e.pk " +
        "         AND D.WORK_DT_OF_SHIFT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
        "         AND D.MEAL_EMP_TYPE = 'IN' " +
        "         AND DECODE ('" + p_group_pk + "', 'ALL', 'ALL', B.PK) = '" + p_group_pk + "' " +
        "         AND  A.TCO_ORG_PK IN " +
        "                    (    SELECT g.pk " +
        "                           FROM TCO_ORG g " +
        "                          WHERE g.del_if = 0 " +
        "                     START WITH g.pk = case when '" + p_dept_pk + "' = 'ALL' then g.pk else to_number('" + p_dept_pk + "') end " +
        "                     CONNECT BY PRIOR g.pk = g.p_pk) " +
        "         AND (   DECODE ('" + p_search + "', " +
        "                         '1', UPPER (A.EMP_ID), " +
        "                         '2', A.ID_NUM, " +
        "                         '3', UPPER (A.FULL_NAME)) LIKE " +
        "                    '%' || UPPER ('" + p_input + "') || '%' " +
        "              OR '" + p_input + "' IS NULL) " +
        "         AND DECODE ('" + p_meal_emp_type + "', 'ALL', 'ALL', d.meal_emp_type) = " +
        "                '" + p_meal_emp_type + "' " +
        "         AND DECODE ('" + p_meal_type + "', 'ALL', 'ALL', D.MEAL_TYPE) = '" + p_meal_type + "' " +
        "         AND DECODE ('" + p_meal_times + "', 'ALL', 'ALL', D.MEAL_TIMES) = '" + p_meal_times + "' " +
        "    UNION ALL " +
        "    SELECT '','','' DIV_NM,'' org_nm, " +
        "         '' AS group_nm, " +
        "         d.EMP_ID AS emp_id, " +
        "         D.FULL_NAME AS full_name, " +
        "         '' AS join_dt, " +
        "         to_char(to_date(d.WORK_DT_OF_SHIFT,'yyyymmdd'),'dd/mm/yyyy') AS work_dt, " +
        "         d.TIME_IN AS time, " +
        "         (select CODE_NM from vhr_hr_code where id='HR0191' and code =D.MEAL_TIMES) meal_times, " +
        "         decode(NVL(D.MOD_BY_HAND,'N'),'N','NO','YES'), " +
        "         decode(d.meal_emp_type,'IN','Employee of Company','Out of Company') " +
        "    FROM THR_MEAL_DUPLICATE D " +
        "   WHERE     D.DEL_IF = 0 " +
        "         AND D.WORK_DT_OF_SHIFT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
        "         AND D.MEAL_EMP_TYPE = 'OUT' " +
        "         AND (   DECODE ('" + p_search + "', " +
        "                         '1', UPPER (D.EMP_ID), " +
        "                         '3', UPPER (D.FULL_NAME)) LIKE " +
        "                    '%' || UPPER ('" + p_input + "') || '%' " +
        "              OR '" + p_input + "' IS NULL) " +
        "         AND DECODE ('" + p_meal_emp_type + "', 'ALL', 'ALL', d.meal_emp_type) = " +
        "                '" + p_meal_emp_type + "' " +
        "         AND DECODE ('" + p_meal_type + "', 'ALL', 'ALL', D.MEAL_TYPE) = '" + p_meal_type + "' " +
        "         AND DECODE ('" + p_meal_times + "', 'ALL', 'ALL', D.MEAL_TIMES) = '" + p_meal_times + "'      " +
        "ORDER BY DIV_NM,org_nm,group_nm,emp_id,work_dt ";

        //Response.Write(SQL);
        //Response.End();   
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);

        int irow = dt_emp.Rows.Count;
        int icol = dt_emp.Columns.Count;

        if (irow == 0)
        {
            Response.Write("There is no data.");
            Response.End();
        }



        //Khai bao bien
        int i, j, pos = 5;
        
        //Insert new row

        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[pos + 1, 1].Rows.EntireRow.Insert();
        }

        //Title value
        exSheet.Cells["F2"].Value = p_from_dt.Substring(6, 2).ToString() + "/" + p_from_dt.Substring(4, 2).ToString() + "/" + p_from_dt.Substring(0, 4).ToString();
        exSheet.Cells["I2"].Value = p_to_dt.Substring(6, 2).ToString() + "/" + p_to_dt.Substring(4, 2).ToString() + "/" + p_to_dt.Substring(0, 4).ToString();

        //Show data
        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[pos + i, 1].Value = i + 1;
            for (j = 2; j < icol; j++)
            {
                exSheet.Cells[pos + i, j].Value = dt_emp.Rows[i][j].ToString();
            }

        }



        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts 

        //range = exSheet.Range["A1"];
        // hide row A5 
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client 
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute  
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept, p_group, p_status, p_type, p_pay_type, p_emp_id, p_work_mon, p_times, p_m_type, p_sal_period;

        p_dept       = Request["l_dept"].ToString();
        p_group      = Request["l_group"].ToString();
        p_status     = Request["l_status"].ToString();
        p_type       = Request["l_type"].ToString();
        p_pay_type   = Request["l_pay_type"].ToString();
        p_emp_id     = Request["l_emp_id"].ToString();
        p_work_mon   = Request["l_work_mon"].ToString();
        p_sal_period = Request["l_sal_period"].ToString();
        p_m_type     = Request.QueryString["l_m_type"].ToString();

        string TemplateFile = "rpt_hrpm00700_5.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00700_5_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];
        IRange     range;
        DataTable  myDT;
        string     SQL = "";

        /*
         * myDT = new DataTable();
         * SQL = "SELECT TO_CHAR(SYSDATE,'dd/mm/yyyy') FROM DUAL";
         * myDT = CtlLib.TableReadOpen(SQL);
         * exSheet.Cells["A2"].Value = string.Format("(SALARY CYCLE: TO 26/07/2012 25/08/2012)", myDT.Rows[0][0].ToString());
         */

        myDT = new DataTable();
        string SQL_Com = "select  a.PARTNER_LNAME,a.ADDR1,a.PHONE_NO,f.data,to_char(to_date('" + p_work_mon + "','yyyymm'),'MON-YYYY'),to_char(to_date('" + p_work_mon + "','yyyymm'),'MM-YYYY') " +
                         "from tco_company a, tc_fsbinary f " +
                         "where a.DEL_IF=0 and a.TCO_BPPHOTO_PK = f.pk " +
                         "and a.pk in ( select tco_company_pk from  " +
                         "               tco_org  f " +
                         "               where  f.pk IN ( " +
                         "                              SELECT     g.pk " +
                         "                                    FROM tco_org g " +
                         "                                   WHERE g.del_if = 0 " +
                         "                              START WITH g.pk = " +
                         "                                            DECODE ('" + p_dept + "', " +
                         "                                                    'ALL', 0, " +
                         "                                                    '" + p_dept + "' " +
                         "                                                   ) " +
                         "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
                         "                        OR '" + p_dept + "' = 'ALL') " +
                         "and rownum=1 ";

        DataTable dt_Com = CtlLib.TableReadOpen(SQL_Com);
        int       irow_com;

        irow_com = dt_Com.Rows.Count;


        MemoryStream stream;
        Bitmap       b;

        if (dt_Com.Rows[0][3].ToString().Length != 0)
        {
            byte[] MyData = new byte[0];
            MyData = (byte[])dt_Com.Rows[0][3];
            stream = new MemoryStream(MyData);

            b = new Bitmap(stream);
            exSheet.Cells["A1"].Select();
            exSheet.Shapes.AddPicture(CtlLib.ResizeImage(b, 100, 70));
        }
        exSheet.Cells["B1"].Value = dt_Com.Rows[0][0].ToString();
        exSheet.Cells["B2"].Value = dt_Com.Rows[0][1].ToString();
        exSheet.Cells["E1"].Value = string.Format("SEVERANCE AMOUNT SUMMARY IN {0}", dt_Com.Rows[0][4].ToString());
        exSheet.Cells["E2"].Value = string.Format("TỔNG TIỀN TRỢ CẤP THÔI VIỆC THÁNG {0}", dt_Com.Rows[0][5].ToString());
        exSheet.Cells["B3"].Value = string.Format("Phone: {0}", dt_Com.Rows[0][2].ToString());

        SQL = "select a.THR_GROUP_NM,  " +                               //1
              "A.EMP_ID,  " +                                            //2
              "A.FULL_NAME,  " +                                         //3
              "to_char(to_date(A.JOIN_DT,'yyyymmdd'),'dd/mm/yyyy'),  " + //4
              "A.POS_NM,  " +                                            //6
              "NVL(A.SALARY_LEVEL1,0),  " +                              //7
              "NVL(A.SALARY_LEVEL2,0),  " +                              //8
              "round(NVL(A.severance_month,3)),  " +                     //53
              "round(NVL(A.severance_amt,3))  " +                        //54
              "from thr_month_salary a " +
              "where a.del_if=0 and a.work_mon='" + p_work_mon + "' " +
              "and NVL(A.severance_amt,0) > 0 " +
              " and (a.tco_dept_pk in ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_dept + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_dept + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_dept + "' = 'ALL') " +
              " and decode('" + p_group + "','ALL','ALL',a.thr_group_pk)='" + p_group + "'" +
              " and a.emp_id like '%" + p_emp_id + "%' " +
              " and decode('" + p_status + "','ALL','ALL',a.status)='" + p_status + "'" +
              " and decode('" + p_pay_type + "','ALL','ALL',a.pay_type)='" + p_pay_type + "'" +
              " and decode('" + p_m_type + "','ALL','ALL',a.MONEY_KIND)='" + p_m_type + "'" +
              " and nvl(a.net_amt,0) " + p_type + " and rownum <=10 " +
              "order by a.dept_nm,a.thr_group_nm,a.emp_id";

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_Emp = CtlLib.TableReadOpen(SQL);
        int       irow_emp, icol_emp;

        irow_emp = dt_Emp.Rows.Count;
        icol_emp = dt_Emp.Columns.Count;
        if (irow_emp == 0)
        {
            Response.Write("There is no data of salary");
            Response.End();
        }

        double tt_1 = 0, tt_2 = 0, tt_3 = 0, tt_4 = 0;

        int s_row = 6;

        for (int i = 0; i < dt_Emp.Rows.Count; i++)
        {
            exSheet.Cells["A" + s_row].Value = i + 1;
            exSheet.Cells["B" + s_row].Value = dt_Emp.Rows[i][0].ToString();
            exSheet.Cells["C" + s_row].Value = dt_Emp.Rows[i][1].ToString();
            exSheet.Cells["D" + s_row].Value = dt_Emp.Rows[i][2].ToString();
            exSheet.Cells["E" + s_row].Value = dt_Emp.Rows[i][3].ToString();
            exSheet.Cells["F" + s_row].Value = dt_Emp.Rows[i][4].ToString();

            tt_1 += double.Parse(dt_Emp.Rows[i][5].ToString());
            tt_2 += double.Parse(dt_Emp.Rows[i][6].ToString());
            tt_3 += double.Parse(dt_Emp.Rows[i][7].ToString());
            tt_4 += double.Parse(dt_Emp.Rows[i][8].ToString());

            exSheet.Cells["G" + s_row].Value = double.Parse(dt_Emp.Rows[i][5].ToString());
            exSheet.Cells["H" + s_row].Value = double.Parse(dt_Emp.Rows[i][6].ToString());
            exSheet.Cells["I" + s_row].Value = double.Parse(dt_Emp.Rows[i][7].ToString());
            exSheet.Cells["J" + s_row].Value = double.Parse(dt_Emp.Rows[i][8].ToString());

            s_row++;

            if (i < dt_Emp.Rows.Count - 1)
            {
                exSheet.Range["A" + s_row].Rows.EntireRow.Insert();
            }
        }

        exSheet.Cells["A" + s_row].Value = string.Format("Total {0} Employee(s)", dt_Emp.Rows.Count);
        exSheet.Cells["G" + s_row].Value = tt_1;
        exSheet.Cells["H" + s_row].Value = tt_2;
        exSheet.Cells["I" + s_row].Value = tt_3;
        exSheet.Cells["J" + s_row].Value = tt_4;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 23
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept_pk, p_work_mon, p_from_dt, p_to_dt, p_nation, p_group_pk, p_meal_emp_type;

        p_dept_pk       = Request["l_tco_dept_pk"].ToString();
        p_group_pk      = Request["l_thr_group_pk"].ToString();
        p_work_mon      = Request["l_work_mon"].ToString();
        p_to_dt         = Request["l_to_dt"].ToString();
        p_from_dt       = Request["l_from_dt"].ToString();
        p_meal_emp_type = Request["l_meal_emp_type"].ToString();

        string TemplateFile = "rpt_chrm00020_3.xls";
        string TempFile     = "../../../../system/temp/rpt_chrm00020_3_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        string SQL_CODE
            = "SELECT code,code_nm " +
              "    FROM vhr_hr_code c " +
              "   WHERE id = 'HR0191' ";

        DataTable dt_hr_code = CtlLib.TableReadOpen(SQL_CODE);


        //======================================================================================================================
        string SQL_1
            = "SELECT 1 seq, " +
              "       NVL (A.MEAL_EMP_TYPE, ' ') meal_emp_type, " +
              "       (    SELECT g.ORG_NM " +
              "              FROM TCO_ORG g " +
              "             WHERE g.del_if = 0 AND G.ORG_TYPE = '06' " +
              "        START WITH g.pk = b.pk " +
              "        CONNECT BY PRIOR g.p_pk = g.pk) " +
              "          DIV_NM, " +
              "       B.ORG_NM dept_nm, c.WORKGROUP_NM group_nm, " +
              "       A.EMP_ID, " +
              "       A.FULL_NM, " +
              "       TO_CHAR (TO_DATE (A.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy'), " +
              "       NVL(A.MEAL_TIME_01,0), " +
              "       NVL(A.MEAL_TIME_01_AMT,0),  " +
              "       NVL(A.MEAL_TIME_02,0), " +
              "       NVL(A.MEAL_TIME_02_AMT,0), " +
              "       NVL(A.MEAL_TIME_03,0), " +
              "       NVL(A.MEAL_TIME_03_AMT,0), " +
              "       NVL(A.MEAL_TIME_04,0), " +
              "       NVL(A.MEAL_TIME_04_AMT,0),  " +
              "       NVL (A.MEAL_MAIN_QTY, 0) + NVL (A.SNACK_TIME_QTY, 0), " +
              "       NVL (A.MEAL_MAIN_AMT, 0) + NVL (A.SNACK_TIME_AMT, 0) " +
              "  FROM thr_meal_month a, tco_org b, thr_work_group c " +
              " WHERE     a.del_if = 0 and c.del_if = 0 " +
              "       AND b.del_if = 0 " +
              "       AND A.TCO_ORG_PK = B.PK and a.thr_wg_pk = c.pk " +
              "       AND A.MEAL_EMP_TYPE = 'IN' " +
              "       AND A.WORK_MON = '" + p_work_mon + "' " +
              "       AND DECODE ('" + p_group_pk + "', 'ALL', 'ALL', A.THR_WG_PK) = '" + p_group_pk + "' " +
              "       AND A.TCO_ORG_PK IN " +
              "              (    SELECT g.pk " +
              "                     FROM TCO_ORG g " +
              "                    WHERE g.del_if = 0 " +
              "               START WITH g.pk = " +
              "                             CASE " +
              "                                WHEN '" + p_dept_pk + "' = 'ALL' THEN g.pk " +
              "                                ELSE TO_NUMBER ('" + p_dept_pk + "') " +
              "                             END " +
              "               CONNECT BY PRIOR g.pk = g.p_pk) " +
              " ORDER BY seq, " +
              "         DIV_NM, " +
              "         dept_nm, " +
              "         EMP_ID ";

        //======================================================================================================================
        string SQL_2
            = "SELECT 2 seq, " +
              "       NVL (A.MEAL_EMP_TYPE, ' '), " +
              "       '' DIV_NM, " +
              "       '' dept_nm,'' group_nm, " +
              "       A.EMP_ID, " +
              "       A.FULL_NM, " +
              "       TO_CHAR (TO_DATE (A.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy'), " +
              "       NVL(A.MEAL_TIME_01,0), " +
              "       NVL(A.MEAL_TIME_01_AMT,0),  " +
              "       NVL(A.MEAL_TIME_02,0), " +
              "       NVL(A.MEAL_TIME_02_AMT,0), " +
              "       NVL(A.MEAL_TIME_03,0), " +
              "       NVL(A.MEAL_TIME_03_AMT,0), " +
              "       NVL(A.MEAL_TIME_04,0), " +
              "       NVL(A.MEAL_TIME_04_AMT,0),  " +
              "       NVL (A.MEAL_MAIN_QTY, 0) + NVL (A.SNACK_TIME_QTY, 0), " +
              "       NVL (A.MEAL_MAIN_AMT, 0) + NVL (A.SNACK_TIME_AMT, 0) " +
              "  FROM thr_meal_month a " +
              " WHERE a.del_if = 0 AND A.MEAL_EMP_TYPE = 'OUT' AND A.WORK_MON = '" + p_work_mon + "' ";

        //======================================================================================================================
        string SQL_3
            = "SELECT 3 seq, " +
              "       NVL (A.MEAL_EMP_TYPE, ' '), " +
              "       '' DIV_NM, " +
              "       '' dept_nm,'' group_nm, " +
              "       A.EMP_ID, " +
              "       A.FULL_NM, " +
              "       TO_CHAR (TO_DATE (A.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy'), " +
              "       NVL(A.MEAL_TIME_01,0), " +
              "       NVL(A.MEAL_TIME_01_AMT,0),  " +
              "       NVL(A.MEAL_TIME_02,0), " +
              "       NVL(A.MEAL_TIME_02_AMT,0), " +
              "       NVL(A.MEAL_TIME_03,0), " +
              "       NVL(A.MEAL_TIME_03_AMT,0), " +
              "       NVL(A.MEAL_TIME_04,0), " +
              "       NVL(A.MEAL_TIME_04_AMT,0),  " +
              "       NVL (A.MEAL_MAIN_QTY, 0) + NVL (A.SNACK_TIME_QTY, 0), " +
              "       NVL (A.MEAL_MAIN_AMT, 0) + NVL (A.SNACK_TIME_AMT, 0) " +
              "  FROM thr_meal_month a " +
              " WHERE a.del_if = 0 AND A.MEAL_EMP_TYPE IS NULL AND A.WORK_MON = '" + p_work_mon + "' ";

        //======================================================================================================================
        string SQL_ALL
            = "SELECT 1 seq, " +
              "       NVL (A.MEAL_EMP_TYPE, ' ') meal_emp_type, " +
              "       (    SELECT g.ORG_NM " +
              "              FROM TCO_ORG g " +
              "             WHERE g.del_if = 0 AND G.ORG_TYPE = '06' " +
              "        START WITH g.pk = b.pk " +
              "        CONNECT BY PRIOR g.p_pk = g.pk) " +
              "          DIV_NM, " +
              "       B.ORG_NM dept_nm,c.WORKGROUP_NM group_nm, " +
              "       A.EMP_ID, " +
              "       A.FULL_NM, " +
              "       TO_CHAR (TO_DATE (A.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy'), " +
              "       NVL(A.MEAL_TIME_01,0), " +
              "       NVL(A.MEAL_TIME_01_AMT,0),  " +
              "       NVL(A.MEAL_TIME_02,0), " +
              "       NVL(A.MEAL_TIME_02_AMT,0), " +
              "       NVL(A.MEAL_TIME_03,0), " +
              "       NVL(A.MEAL_TIME_03_AMT,0), " +
              "       NVL(A.MEAL_TIME_04,0), " +
              "       NVL(A.MEAL_TIME_04_AMT,0),  " +
              "       NVL (A.MEAL_MAIN_QTY, 0) + NVL (A.SNACK_TIME_QTY, 0), " +
              "       NVL (A.MEAL_MAIN_AMT, 0) + NVL (A.SNACK_TIME_AMT, 0) " +
              "  FROM thr_meal_month a, tco_org b, thr_work_group c " +
              " WHERE     a.del_if = 0 and c.del_if = 0 " +
              "       AND b.del_if = 0 " +
              "       AND A.TCO_ORG_PK = B.PK and a.thr_wg_pk = c.pk " +
              "       AND A.MEAL_EMP_TYPE = 'IN' " +
              "       AND A.WORK_MON = '" + p_work_mon + "' " +
              "       AND DECODE ('ALL', 'ALL', 'ALL', A.THR_WG_PK) = 'ALL' " +
              "       AND A.TCO_ORG_PK IN " +
              "              (    SELECT g.pk " +
              "                     FROM TCO_ORG g " +
              "                    WHERE g.del_if = 0 " +
              "               START WITH g.pk = " +
              "                             CASE " +
              "                                WHEN '" + p_dept_pk + "' = 'ALL' THEN g.pk " +
              "                                ELSE TO_NUMBER ('" + p_dept_pk + "') " +
              "                             END " +
              "               CONNECT BY PRIOR g.pk = g.p_pk) " +
              "UNION ALL " +
              "SELECT 2 seq, " +
              "       NVL (A.MEAL_EMP_TYPE, ' '), " +
              "       '' DIV_NM, " +
              "       '' dept_nm,'' group_nm, " +
              "       A.EMP_ID, " +
              "       A.FULL_NM, " +
              "       TO_CHAR (TO_DATE (A.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy'), " +
              "       NVL(A.MEAL_TIME_01,0), " +
              "       NVL(A.MEAL_TIME_01_AMT,0),  " +
              "       NVL(A.MEAL_TIME_02,0), " +
              "       NVL(A.MEAL_TIME_02_AMT,0), " +
              "       NVL(A.MEAL_TIME_03,0), " +
              "       NVL(A.MEAL_TIME_03_AMT,0), " +
              "       NVL(A.MEAL_TIME_04,0), " +
              "       NVL(A.MEAL_TIME_04_AMT,0),  " +
              "       NVL (A.MEAL_MAIN_QTY, 0) + NVL (A.SNACK_TIME_QTY, 0), " +
              "       NVL (A.MEAL_MAIN_AMT, 0) + NVL (A.SNACK_TIME_AMT, 0) " +
              "  FROM thr_meal_month a " +
              " WHERE a.del_if = 0 AND A.MEAL_EMP_TYPE = 'OUT' AND A.WORK_MON = '" + p_work_mon + "' " +
              "UNION ALL " +
              "SELECT 3 seq, " +
              "       NVL (A.MEAL_EMP_TYPE, ' '), " +
              "       '' DIV_NM, " +
              "       '' dept_nm,'' group_nm, " +
              "       A.EMP_ID, " +
              "       A.FULL_NM, " +
              "       TO_CHAR (TO_DATE (A.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy'), " +
              "       NVL(A.MEAL_TIME_01,0), " +
              "       NVL(A.MEAL_TIME_01_AMT,0),  " +
              "       NVL(A.MEAL_TIME_02,0), " +
              "       NVL(A.MEAL_TIME_02_AMT,0), " +
              "       NVL(A.MEAL_TIME_03,0), " +
              "       NVL(A.MEAL_TIME_03_AMT,0), " +
              "       NVL(A.MEAL_TIME_04,0), " +
              "       NVL(A.MEAL_TIME_04_AMT,0),  " +
              "       NVL (A.MEAL_MAIN_QTY, 0) + NVL (A.SNACK_TIME_QTY, 0), " +
              "       NVL (A.MEAL_MAIN_AMT, 0) + NVL (A.SNACK_TIME_AMT, 0) " +
              "  FROM thr_meal_month a " +
              " WHERE a.del_if = 0 AND A.MEAL_EMP_TYPE IS NULL AND A.WORK_MON = '" + p_work_mon + "' " +
              "ORDER BY seq, " +
              "         DIV_NM, " +
              "         dept_nm, " +
              "         EMP_ID ";

        string SQL;

        if (p_meal_emp_type == "IN")
        {
            SQL = SQL_1;
        }
        else if (p_meal_emp_type == "OUT")
        {
            SQL = SQL_2;
        }
        else if (p_meal_emp_type == "VIS")
        {
            SQL = SQL_3;
        }
        else
        {
            SQL = SQL_ALL;
        }

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);

        int irow = dt_emp.Rows.Count;
        int icol = dt_emp.Columns.Count;

        if (irow == 0)
        {
            Response.Write("There is no data.");
            Response.End();
        }



        //Khai bao bien
        int i, j, k, pos = 6;
        int ntotal = icol - 8, nseq = 0, ndiv = 0;        //8 cot khong sum

        double[] s_total = new double[ntotal];
        double[] g_total = new double[ntotal];
        double[] d_total = new double[ntotal];      //div_total
        int      ncount = 0, scount = 0;

        for (j = 0; j < ntotal; j++)
        {
            s_total[j] = 0;
            g_total[j] = 0;
            d_total[j] = 0;
        }

        //Dem so luong thay doi meal type
        for (i = 0; i < irow - 1; i++)
        {
            if (dt_emp.Rows[i]["seq"].ToString() != dt_emp.Rows[i + 1]["seq"].ToString())
            {
                nseq++;
            }

            if (dt_emp.Rows[i]["div_nm"].ToString() != dt_emp.Rows[i + 1]["div_nm"].ToString() && dt_emp.Rows[i]["div_nm"].ToString() != "")
            {
                ndiv++;
            }
        }
        //Insert new row

        for (i = 0; i < irow - 1 + nseq + ndiv; i++)
        {
            exSheet.Range[pos + 1, 1].Rows.EntireRow.Insert();
        }

        //Title value
        exSheet.Cells["F2"].Value = p_from_dt.Substring(6, 2).ToString() + "/" + p_from_dt.Substring(4, 2).ToString() + "/" + p_from_dt.Substring(0, 4).ToString();
        exSheet.Cells["I2"].Value = p_to_dt.Substring(6, 2).ToString() + "/" + p_to_dt.Substring(4, 2).ToString() + "/" + p_to_dt.Substring(0, 4).ToString();

        exSheet.Cells["H4"].Value = dt_hr_code.Rows[0][1].ToString();
        exSheet.Cells["J4"].Value = dt_hr_code.Rows[1][1].ToString();
        exSheet.Cells["L4"].Value = dt_hr_code.Rows[2][1].ToString();
        exSheet.Cells["N4"].Value = dt_hr_code.Rows[3][1].ToString();

        //Show data
        for (i = 0; i < irow; i++)
        {
            for (k = 0; k < ntotal; k++)
            {
                s_total[k] = s_total[k] + double.Parse(dt_emp.Rows[i][k + 8].ToString());
                g_total[k] = g_total[k] + double.Parse(dt_emp.Rows[i][k + 8].ToString());
                d_total[k] = d_total[k] + double.Parse(dt_emp.Rows[i][k + 8].ToString());
            }
            ncount++;
            scount++;
            exSheet.Cells[pos + i, 1].Value = ncount;
            for (j = 2; j < icol; j++)
            {
                if (j <= 7)
                {
                    exSheet.Cells[pos + i, j].Value = dt_emp.Rows[i][j].ToString();
                }
                else
                {
                    exSheet.Cells[pos + i, j].Value = double.Parse(dt_emp.Rows[i][j].ToString());
                }
            }

            //Dong sub total
            if (i == irow - 1)
            {
                exSheet.Cells[pos + i + 1, 1].Value = "Total " + ncount + " employees";
                for (k = 0; k < ntotal; k++)
                {
                    exSheet.Cells[pos + i + 1, k + 8].Value = s_total[k];
                    exSheet.Cells[pos + i + 2, k + 8].Value = g_total[k];
                }
            }

            else
            {
                if (dt_emp.Rows[i]["seq"].ToString() != dt_emp.Rows[i + 1]["seq"].ToString())
                {
                    if (dt_emp.Rows[i]["div_nm"].ToString() != "")
                    {
                        exSheet.Cells[pos + i + 1, 1, pos + i + 1, 7].Merge();
                        exSheet.Cells[pos + i + 1, 1, pos + i + 1, icol].Font.Bold = true;
                        exSheet.Cells[pos + i + 1, 1].HorizontalAlignment          = XlHAlign.xlHAlignCenter;
                        exSheet.Cells[pos + i + 1, 1].Value = "Total " + dt_emp.Rows[i]["div_nm"].ToString() + " " + ncount + " employees";

                        for (k = 0; k < ntotal; k++)
                        {
                            exSheet.Cells[pos + i + 1, k + 8].Value = d_total[k];
                            d_total[k] = 0;
                        }

                        ncount = 0;
                        pos++;
                    }

                    exSheet.Cells[pos + i + 1, 1, pos + i + 1, 7].Merge();
                    exSheet.Cells[pos + i + 1, 1, pos + i + 1, icol].Font.Bold = true;
                    exSheet.Cells[pos + i + 1, 1].HorizontalAlignment          = XlHAlign.xlHAlignCenter;
                    exSheet.Cells[pos + i + 1, 1].Value = "Total " + scount + " employees";

                    for (k = 0; k < ntotal; k++)
                    {
                        exSheet.Cells[pos + i + 1, k + 8].Value = s_total[k];
                        s_total[k] = 0;
                    }

                    ncount = 0;
                    pos++;
                }
                else if (dt_emp.Rows[i]["div_nm"].ToString() != dt_emp.Rows[i + 1]["div_nm"].ToString() && dt_emp.Rows[i]["div_nm"].ToString() != "")
                {
                    exSheet.Cells[pos + i + 1, 1, pos + i + 1, 7].Merge();
                    exSheet.Cells[pos + i + 1, 1, pos + i + 1, icol].Font.Bold = true;
                    exSheet.Cells[pos + i + 1, 1].HorizontalAlignment          = XlHAlign.xlHAlignCenter;
                    exSheet.Cells[pos + i + 1, 1].Value = "Total " + dt_emp.Rows[i]["div_nm"].ToString() + " " + ncount + " employees";

                    for (k = 0; k < ntotal; k++)
                    {
                        exSheet.Cells[pos + i + 1, k + 8].Value = d_total[k];
                        d_total[k] = 0;
                    }

                    ncount = 0;
                    pos++;
                }
            }
        }

        //Xóa cột dư ở những tháng không đủ 31 ngày


        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 24
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());
        //CtlLib.SetUser("SALE");

        string TemplateFile = "rpt_bisc00171_ST03.xls";
        string TempFile     = "../../../../system/temp/rpt_bisc00171_ST03_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        //bind data to excel file

        string p_lang          = Request.QueryString["p_lang"];
        string p_wh_name       = Request.QueryString["p_wh_name"];
        string p_wh_type       = Request.QueryString["p_wh_type"];
        string p_wh_pk         = Request.QueryString["p_wh_pk"];
        string p_item_group_pk = Request.QueryString["p_item_group_pk"];
        string p_item_group_nm = Request.QueryString["p_item_group_nm"];
        string p_item          = Request.QueryString["p_item"];
        string p_date          = Request.QueryString["p_date"];



        string SQL_WH
            = "select b.wh_type a0,wh_t.code_nm a1" +
              " from tlg_lg_wh_item_group a,tlg_in_warehouse b,tlg_it_itemgrp c " +
              ",(SELECT   a.code, a.code_nm FROM tlg_lg_code a WHERE a.del_if = 0 AND tlg_lg_code_group_pk = '14') wh_t " +
              "where a.del_if=0 and b.del_if=0 and c.del_if=0 " +
              "and A.TLG_IN_WAREHOUSE_PK=b.pk " +
              "and A.TLG_IT_ITEMGRP_PK=c.pk " +
              "and c.pk='" + p_item_group_pk + "' " +
              "and (b.wh_type = '" + p_wh_type + "' or '" + p_wh_type + "' is null)" +
              "and wh_t.code=b.wh_type " +
              "group by b.wh_type,wh_t.code_nm order by a0,a1";


        DataTable dt_WH    = CtlLib.TableReadOpen(SQL_WH);
        int       i_row_wh = dt_WH.Rows.Count;

        if (i_row_wh == 0)
        {
            Response.Write("There is no data of warehouse type");
            Response.End();
        }
        string SQL
            = "select max(t.item_code) a0 " +
              ",max(t.item_name) a1 " +
              ",lg_f_get_spec_nm_by_seq(a1.tlg_it_item_pk,1) " +
              ",lg_f_get_spec_nm_by_seq(a1.tlg_it_item_pk,2) " +
              ",lg_f_get_spec_nm_by_seq(a1.tlg_it_item_pk,3) " +
              ",(select sum(g.min_qty) from TLG_IT_SECURE g where g.del_if=0 and g.tlg_it_item_pk=a1.tlg_it_item_pk and (TLG_IN_WAREHOUSE_PK='" + p_wh_pk + "' or '" + p_wh_pk + "' is null ) ) ";

        for (int i = 0; i < dt_WH.Rows.Count; i++)
        {
            SQL = SQL + "                               ,sum(decode(a1.wh_type,'" + dt_WH.Rows[i][0].ToString() + "',a1.end_qty)) wh_" + dt_WH.Rows[i][0].ToString();
        }
        SQL = SQL +
              "                               from ( " +
              "                                 select wh.wh_type " +
              "                                ,wh.pk " +
              "                                ,tr.tlg_it_item_pk " +
              "                                ,tr.end_qty " +
              "                                    from (SELECT tlg_in_warehouse_pk, " +
              "                                           tlg_it_item_pk, " +
              "                                           nvl(SUM (begin_qty),0) + nvl(SUM (in_qty),0) - nvl(SUM (out_qty),0) end_qty " +
              "                                      FROM (SELECT a.tlg_in_warehouse_pk, " +
              "                                                   b.tlg_it_item_pk, " +
              "                                                   b.end_qty begin_qty, " +
              "                                                   NULL in_qty, " +
              "                                                   NULL out_qty " +
              "                                              FROM tlg_sa_stock_closing_m a, " +
              "                                                   tlg_sa_stock_closing_d b,tlg_it_item c " +
              "                                             WHERE     a.del_if = 0 and c.del_if=0 and b.tlg_it_item_pk=c.pk" +
              "                                                   AND (a.tlg_in_warehouse_pk, " +
              "                                                        a.stock_date) IN " +
              "                                                          (  SELECT b.tlg_in_warehouse_pk, " +
              "                                                                    MAX ( " +
              "                                                                       b.stock_date) " +
              "                                                               FROM tlg_sa_stock_closing_m b " +
              "                                                              WHERE     b.del_if = 0 " +
              "                                                                    AND b.stock_date < " +
              "                                                                           '" + p_date + "' " +
              "                                                           GROUP BY b.tlg_in_warehouse_pk) " +
              "                                                   AND b.del_if = 0 " +
              "                                                   AND a.pk = " +
              "                                                          b.tlg_sa_stock_closing_m_pk " +
              "													and (c.tlg_it_itemgrp_pk = '"+ p_item_group_pk + "' or '" + p_item_group_pk + "' is null) " +
              "                                                   AND NVL (b.end_qty, 0) <> 0 " +
              "                                            UNION ALL " +
              "                                            SELECT a.tlg_in_warehouse_pk, " +
              "                                                   a.tlg_it_item_pk, " +
              "                                                   NULL begin_qty, " +
              "                                                   a.input_qty in_qty, " +
              "                                                   a.output_qty out_qty " +
              "                                              FROM tlg_in_stocktr a,tlg_it_item b " +
              "                                             WHERE     a.del_if = 0 and b.del_if=0 and a.tlg_it_item_pk=b.pk " +
              "                                                   AND a.tr_date > " +
              "                                                          NVL ( " +
              "                                                             (SELECT MAX ( " +
              "                                                                        b.stock_date) " +
              "                                                                FROM tlg_sa_stock_closing_m b " +
              "                                                               WHERE     b.del_if = " +
              "                                                                            0 " +
              "                                                                     AND b.stock_date < " +
              "                                                                            '" + p_date + "' " +
              "                                                                     AND b.tlg_in_warehouse_pk = " +
              "                                                                            a.tlg_in_warehouse_pk), " +
              "                                                             '0') " +
              "													and (b.tlg_it_itemgrp_pk = '"+ p_item_group_pk + "' or '" + p_item_group_pk + "' is null) " +
              "                                                   AND a.tr_date <= '" + p_date + "')  " +
              "                                  GROUP BY tlg_in_warehouse_pk, " +
              "                                           tlg_it_item_pk ) tr,tlg_in_warehouse wh " +
              "                                  where wh.del_if=0  " +
              "								   and (wh.wh_type ='"+ p_wh_type + "' or '" + p_wh_type + "' is null) " +
              "								   and (wh.pk ='"+ p_wh_pk + "' or '" + p_wh_pk + "' is null) " +
              "                                  and tr.tlg_in_warehouse_pk =wh.pk)a1,tlg_it_item t  " +
              "                                  where t.del_if=0 and a1.tlg_it_item_pk=t.pk  " +
              "                            group by a1.tlg_it_item_pk order by a0     " +
              "                                   ";
        //Response.Write(SQL);
        //Response.End();
        DataTable dt_Detail = CtlLib.TableReadOpen(SQL);

        int i_row = dt_Detail.Rows.Count;
        int i_col = dt_Detail.Columns.Count;

        if (i_row == 0)
        {
            Response.Write("There is no data!");
            Response.End();
        }
        exSheet.Cells["E3"].Value = p_item_group_nm;
        exSheet.Range["A1:A" + (i_col + 2)].Columns.Merge();
        //exSheet.Cells["A1"].Value = "END STOCK BY ITEM GROUP";
        exSheet.Cells["E5"].Value = p_date.Substring(6, 2) + "/" + p_date.Substring(4, 2) + "/" + p_date.Substring(0, 4);
        //---------

        ////end-set-----DataBind--in---Master---
        int i_header_row = 7;
        int i_header_col = 8;
        int i_b_row      = 9;

        Double[] grant_total = new Double[i_col + 1];
        Double   sub_total   = 0;

        //-------------bind header--------------
        for (int i = 0; i < i_row_wh; i++)
        {
            if (i != 0)
            {
                exSheet.Range[i_header_row, i_header_col + i, i_header_row, i_header_col + i].Columns.EntireColumn.Insert();           //insert row new of sheet
            }
            exSheet.Range[i_header_row, i_header_col + i, i_header_row, i_header_col + i].Value = dt_WH.Rows[i][1].ToString();
        }
        //insert new row
        for (int i = 1; i < i_row; i++)
        {
            exSheet.Range["A" + (i_b_row + i)].EntireRow.Insert();
        }
        for (int i = 0; i < i_col + 1; i++)
        {
            grant_total[i] = 0;
        }
        for (int i = 0; i < i_row; i++)
        {
            sub_total = 0;
            exSheet.Range[i_b_row + i, 1, i_b_row + i, 1].Value = i + 1;
            for (int j = 0; j < i_col; j++)
            {
                if (j <= 5)
                {
                    exSheet.Range[i_b_row + i, 2 + j, i_b_row + i, j + 2].Value = dt_Detail.Rows[i][j].ToString();
                }
                else
                {
                    if (dt_Detail.Rows[i][j].ToString() != "")
                    {
                        exSheet.Range[i_b_row + i, 2 + j, i_b_row + i, j + 2].Value = Double.Parse(dt_Detail.Rows[i][j].ToString());
                        sub_total      += Double.Parse(dt_Detail.Rows[i][j].ToString());
                        grant_total[j] += Double.Parse(dt_Detail.Rows[i][j].ToString());
                    }
                }
            }
            exSheet.Range[i_b_row + i, 2 + i_col, i_b_row + i, 2 + i_col].Value = sub_total;
            grant_total[i_col] += sub_total;
            if (sub_total != 0 && dt_Detail.Rows[i][5].ToString() != "")
            {
                exSheet.Range[i_b_row + i, 3 + i_col, i_b_row + i, 3 + i_col].Value = Double.Parse(dt_Detail.Rows[i][5].ToString()) / sub_total;
            }
            else
            {
                exSheet.Range[i_b_row + i, 3 + i_col, i_b_row + i, 3 + i_col].Value = 0;
            }
        }
        for (int j = 6; j < i_col + 1; j++)
        {
            exSheet.Range[i_b_row + i_row, 2 + j, i_b_row + i_row, j + 2].Value = grant_total[j];
        }
        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        // range.Rows.Hidden = true;

        // font bold header

        /*range = exSheet.Range["A1:AC1"];
         * range.Rows[4].Font.Bold = true;*/

        exBook.SaveAs(TempFile);
        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        //Response.WriteFile(pdfFilePath);
        Response.WriteFile(TempFile);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 25
0
    protected void Page_Load(object sender, EventArgs e)
    {
        //CtlLib.SetUser("hr");
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept, p_group, p_status, p_type, p_pay_type, p_emp_id, p_work_mon, p_m_type, p_sal_period, p_emp_type;

        p_dept       = Request["l_dept"].ToString();
        p_group      = Request["l_group"].ToString();
        p_status     = Request["l_status"].ToString();
        p_type       = Request["l_type"].ToString();
        p_pay_type   = Request["l_pay_type"].ToString();
        p_emp_id     = Request["l_emp_id"].ToString();
        p_work_mon   = Request["l_work_mon"].ToString();
        p_sal_period = Request["l_sal_period"].ToString();
        p_emp_type   = Request["l_Emp_Type"].ToString();
        p_m_type     = Request.QueryString["l_m_type"].ToString();
        //p_from_leftdate = Request["l_from_leftdate"].ToString();
        //p_to_leftdate = Request["l_to_leftdate"].ToString();

        string TemplateFile = "rpt_hrpm00700_0.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00700_0_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        if (p_type == "ALL")
        {
            p_type = "= NVL (a.net_amt, 0)";
        }

        string SQL
            = "    SELECT A.TCO_DEPT_PK dept_pk,'',  " +
              "                 a.DEPT_NM dept_nm,  " +
              "                 A.EMP_ID emp_id_2,  " +
              "                 A.FULL_NAME full_nm_3,  " +
              "                 TO_CHAR (TO_DATE (A.JOIN_DT, 'yyyymmdd'), 'dd/mm/yyyy') join_dt_4,  " +
              "                 TO_CHAR (TO_DATE (A.left_DT, 'yyyymmdd'), 'dd/mm/yyyy') left_dt_5,  " +
              "                 A.POS_NM pos_nm_6,  " +
              "                 (SELECT code_nm FROM vhr_hr_code  WHERE id = 'HR0117' AND code = a.times) times_74,  " +
              "                 (CASE  WHEN NVL (a.CONFIRM_DT, a.from_dt) > a.from_dt  THEN ROUND (NVL (A.SALARY_LEVEL1, 0), 10) ELSE  0 END)  sal_level1_7,  " +
              "                 (CASE  WHEN NVL (a.CONFIRM_DT, a.to_dt) > a.to_DT THEN 0  ELSE ROUND (NVL (A.SALARY_LEVEL2, 0), 10)  END) sal_level2_8,  " +
              "                 ROUND (NVL (A.ALLOW_K1_AMT, 0), 10) allow_amt1_9,  " +
              "                 ROUND (NVL (A.ALLOW_K2_AMT, 0), 10) allow_amt2_10,  " +
              "                 ROUND (NVL (A.ALLOW_K3_AMT, 0), 10) allow_amt3_11,  " +
              "                 ROUND (NVL (A.ALLOW_K4_AMT, 0), 10) allow_amt4_12,  " +
              "                 ROUND (NVL (A.ALLOW_K5_AMT, 0), 10) allow_amt5_13,  " +
              "                 ROUND (NVL (A.ALLOW_K6_AMT, 0), 10) allow_amt6_14,  " +
              "                 ROUND (NVL (A.ALLOW_K7_AMT, 0), 10) allow_amt7_15,  " +
              "                 ROUND (NVL (A.ALLOW_K8_AMT, 0), 10) allow_amt8_16,  " +
              "                 ROUND (NVL (A.ALLOW_K9_AMT, 0), 10) allow_amt1_9,  " +
              "                 ROUND (NVL (A.ALLOW_K10_AMT, 0), 10) allow_amt1_9,  " +
              "                 ROUND (NVL (A.ALLOW_K11_AMT, 0), 10) allow_amt1_9,  " +
              "                 ROUND (NVL (A.ALLOW_K12_AMT, 0), 10) allow_amt1_9,  " +
              "                 ROUND (NVL (A.TOTAL_ALLOW_K_AMT, 0) , 10) total_allow,  " +
              "                 ROUND ((NVL (A.WT_L1, 0) + NVL (a.HOL_DAY_L1, 0)) / 8 , 10) wd1_17,  " +
              "                 ROUND ((NVL (A.WT_L2, 0) + NVL (a.HOL_DAY_L2, 0)) / 8 , 10) wd2_18,  " +
              "                 ROUND (NVL (A.WT_L1_AMT, 0) + NVL (A.HOL_DAY_L1_AMT, 0) , 10) wd1_17,  " +
              "                 ROUND (NVL (A.WT_L2_AMT, 0) + NVL (A.HOL_DAY_L2_AMT, 0) , 10) wd2_18,  " +
              "                 ROUND (NVL (A.ABS_ALE_L1, 0) / 8, 10) abs_ale1_19,  " +
              "                 ROUND (NVL (A.ABS_ALE_L2, 0) / 8, 10) abs_ale2_20,  " +
              "                 ROUND (NVL (A.ABS_ALE_L1_AMT, 0), 10) abs_ale1_19,  " +
              "                 ROUND (NVL (A.ABS_ALE_L2_AMT, 0), 10) abs_ale2_20,  " +
              "                 ROUND (NVL (A.ABS_OTHER_PAY_L1, 0) / 8, 10) abs_other1_21,  " +
              "                 ROUND (NVL (A.ABS_OTHER_PAY_L2, 0) / 8, 10) abs_other2_22,  " +
              "                 ROUND (NVL (A.ABS_OTHER_PAY_L1_AMT, 0), 10) abs_other1_21,  " +
              "                 ROUND (NVL (A.ABS_OTHER_PAY_L2_AMT, 0), 10) abs_other2_22,  " +
              "                 ROUND (NVL (A.TOTAL_WT_HT_ABS_L1_AMT, 0) + NVL (A.TOTAL_WT_HT_ABS_L2_AMT, 0) , 10) sal_wd_23,  " +
              "                 ROUND (NVL (A.OT_L1, 0), 10) ot_l1_24,   " +
              "                 ROUND (NVL (A.OT_L2, 0), 10) ot_l2_25,   " +
              "                 ROUND (NVL (A.OT_L1_amt, 0), 10) ot_l1_amt_24,   " +
              "                 ROUND (NVL (A.OT_L2_amt, 0), 10) ot_l2_amt_25,   " +
              "                 ROUND (NVL (A.ST_L1, 0) + NVL(A.OST_L1, 0), 10) st_l1_24,   " +
              "                 ROUND (NVL (A.ST_L2, 0) + NVL(A.OST_L2, 0), 10) st_l2_25,   " +
              "                 ROUND (NVL (A.ST_L1_amt, 0) + NVL(A.OST_L1_AMT, 0), 10) st_l1_amt_24,   " +
              "                 ROUND (NVL (A.ST_L2_amt, 0) + NVL(A.OST_L2_AMT, 0), 10) st_l2_amt_25,   " +
              "                 ROUND (NVL (A.HT_L1, 0) + NVL(A.OHT_L1, 0), 10) ht_l1_24,   " +
              "                 ROUND (NVL (A.HT_L2, 0) + NVL(A.OHT_L2, 0), 10) ht_l2_25,   " +
              "                 ROUND (NVL (A.HT_L1_amt, 0) + NVL(A.OHT_L1_AMT, 0), 10) ht_l1_amt_24,   " +
              "                 ROUND (NVL (A.HT_L2_amt, 0) + NVL(A.OHT_L2_AMT, 0), 10) ht_l2_amt_25,   " +
              "                 ROUND (NVL (A.NT_30_L1, 0), 10) nt_30_l1_28,   " +
              "                 ROUND (NVL (A.NT_30_L2, 0), 10) nt_30_l2_29,   " +
              "                 ROUND (NVL (A.NT_30_L1_amt, 0), 10) nt_30_l1_amt_28,   " +
              "                 ROUND (NVL (A.NT_30_L2_amt, 0), 10) nt_30_l2_amt_29,   " +
              "                 ROUND (NVL (A.NT_45_L1, 0), 10) nt_45_l1_30,   " +
              "                 ROUND (NVL (A.NT_45_L2, 0), 10) nt_45_l2_31,   " +
              "                 ROUND (NVL (A.NT_45_L1_amt, 0), 10) nt_45_l1_amt_30,   " +
              "                 ROUND (NVL (A.NT_45_L2_amt, 0), 10) nt_45_l2_amt_31,  " +
              "                 ROUND (NVL (A.NT_60_L1, 0), 10) nt_60_l1_32,   " +
              "                 ROUND (NVL (A.NT_60_L2, 0), 10) nt_60_l2_33,   " +
              "                 ROUND (NVL (A.NT_60_L1_amt, 0), 10) nt_60_l1_amt_32,   " +
              "                 ROUND (NVL (A.NT_60_L2_amt, 0), 10) nt_60_l2_amt_33,   " +
              "                 ROUND (NVL (A.NT_90_L1, 0), 10) nt_90_l1_34,   " +
              "                 ROUND (NVL (A.NT_90_L2, 0), 10) nt_90_l2_35,   " +
              "                 ROUND (NVL (A.NT_90_L1_amt, 0), 10) nt_90_l1_amt_34,   " +
              "                 ROUND (NVL (A.NT_90_L2_amt, 0), 10) nt_90_l2_amt_35,   " +
              "                 ROUND (NVL (A.TOTAL_OT_L1_AMT, 0) + NVL (A.TOTAL_OT_L2_AMT, 0), 10)  total_ot_amt_36,  " +
              "                 ROUND (NVL (A.OT_TAX_L1_AMT, 0) + NVL (A.OT_TAX_L2_AMT, 0), 10)  ot_tax_amt_37,  " +
              "                 ROUND (NVL (A.RETURN_TAX_AMT1, 0), 10) return_amt_46,  " +
              "                 ROUND (NVL (A.RETURN_TAX_AMT2, 0), 10) return_amt_46, " +
              "                 ROUND (NVL (A.RETURN_TAX_AMT3, 0), 10) return_amt_46, " +
              "                 ROUND (NVL (A.RETURN_TAX_AMT4, 0), 10) return_amt_46, " +
              "                 ROUND (NVL (A.RETURN_TAX_AMT5, 0), 10) return_amt_46, " +
              "                 ROUND (NVL (A.ALE_STOP, 0), 10) ale_stop_48,  " +
              "                 ROUND (NVL (A.ALE_STOP_AMT, 0), 10) ale_stop_amt_49,  " +
              "                 ROUND (NVL (A.SEVERANCE_MONTH, 0), 10) serverance_mon_50,  " +
              "                 ROUND (NVL (A.SEVERANCE_AMT, 0), 10) serverance_amt_51,  " +
              "                 ROUND (NVL (A.GROSS_AMT, 0), 10) gross_amt_52,  " +
              "                 ROUND (NVL (A.DEDUCT_PIT, 0), 10) deduct_pit_55,  " +
              "                 ROUND (NVL (A.INCOME_AMT_2, 0), 10) income_amt_56,  " +
              "                 ROUND (NVL (A.INCOME_BEFORE_TAX, NVL (A.INCOME_AMT, 0)), 10)  income_bef_amt_57,  " +
              "                 ROUND (NVL (A.INCOME_TAX, 0), 10) income_tax_58,  " +
              "                 ROUND (NVL (A.BREAK_CONTRACT_AMT, 0), 10) break_con_amt_53,  " +
              "                 ROUND (NVL (A.UNION_AMT, 0), 10) union_amt_59,  " +
              "                 ROUND (NVL (A.SOCIAL_AMT, 0), 10) social_amt_60,  " +
              "                 ROUND (NVL (A.HEALTH_AMT, 0), 10) health_amt_61,  " +
              "                 ROUND (NVL (A.UNEMP_INS, 0), 10) unemp_ins_amt_62,  " +
              "                 ROUND (NVL (A.health_card_amt, 0), 10) break_con_amt_53,  " +
              "                 ROUND (NVL (A.DEDUCT_NOTAX_AMT1, 0), 10) equip1_amt_66,  " +
              "                 ROUND (NVL (A.DEDUCT_NOTAX_AMT2, 0), 10) equip2_amt_67,  " +
              "                 ROUND (NVL (A.DEDUCT_NOTAX_AMT3, 0), 10) equip3_amt_68,  " +
              "                 ROUND (NVL (A.DEDUCT_NOTAX_AMT4, 0), 10) equip4_amt_69,  " +
              "                 ROUND (NVL (A.DEDUCT_NOTAX_AMT5, 0), 10) equip5_amt_70,  " +
              "                 ROUND (NVL (A.TOTAL_DEDUCT_NOTAX_AMT, 0), 10) equip6_amt_71,  " +
              "                 ROUND (NVL (A.RETURN_NOTAX_AMT1, 0), 10) equip7_amt_72,  " +
              "                 ROUND (NVL (A.RETURN_NOTAX_AMT2, 0), 10) equip8_amt_73, " +
              "                 ROUND (NVL (A.RETURN_NOTAX_AMT3, 0), 10) equip8_amt_73, " +
              "                 ROUND (NVL (A.RETURN_NOTAX_AMT4, 0), 10) equip8_amt_73, " +
              "                 ROUND (NVL (A.RETURN_NOTAX_AMT5, 0), 10) RETURN_NOTAX_AMT5, " +
              "                 ROUND (NVL (A.TOTAL_RETURN_NOTAX_AMT, 0), 10) TOTAL_RETURN_NOTAX_AMT, " +
              "                 DECODE (' p_m_type + ',   'ALL', ROUND (NVL (A.NET_AMT, 0), 0),   '01', ROUND (NVL (A.NET_AMT, 0), 0),  ROUND (NVL (A.NET_AMT, 0), 10))   net_amt_65, " +
              "                 '' SIGNATURE,  " +
              "                 ROUND (NVL (A.SOCIAL_COMPANY, 0), 10) social_com,   " +
              "                 ROUND (NVL (A.HEALTH_COMPANY, 0), 10) health_com,   " +
              "                 ROUND (NVL (A.UNEMP_COM_INS, 0), 10) unemp_com,   " +
              "                 ROUND (NVL (A.UNION_COMPANY, 0), 10) union_com " +
              "    FROM thr_month_salary a, tco_org b, thr_work_group c " +
              "   WHERE     a.del_if = 0 " +
              "         AND a.work_mon = '" + p_work_mon + "' " +
              "         AND b.del_if = 0 " +
              "         AND c.del_if = 0 " +
              "         AND a.tco_dept_pk = b.pk " +
              "         AND a.thr_group_pk = c.pk " +
              "         AND (a.tco_dept_pk IN " +
              "                 (    SELECT g.pk " +
              "                        FROM tco_org g " +
              "                       WHERE g.del_if = 0 " +
              "                  START WITH g.pk = " +
              "                                DECODE ('" + p_dept + "', " +
              "                                        'ALL', 0, " +
              "                                        '" + p_dept + "') " +
              "                  CONNECT BY PRIOR g.pk = g.p_pk) " +
              "              OR '" + p_dept + "' = 'ALL') " +
              "         AND DECODE ('" + p_group + "', 'ALL', 'ALL', a.thr_group_pk) = '" + p_group + "' " +
              "         AND a.emp_id LIKE '%" + p_emp_id + "%' " +
              "         AND DECODE ('" + p_status + "', 'ALL', 'ALL', a.status) = '" + p_status + "' " +
              "         AND DECODE ('" + p_pay_type + "', 'ALL', 'ALL', a.pay_type) = '" + p_pay_type + "' " +
              "         and decode('" + p_emp_type + "' ,'ALL','ALL',A.EMPLOYEE_TYPE)='" + p_emp_type + "' " +
              "         AND DECODE ('" + p_m_type + "', 'ALL', 'ALL', a.MONEY_KIND) = '" + p_m_type + "' " +
              "         AND DECODE ('" + p_sal_period + "', 'ALL', 'ALL', A.SAL_PERIOD) = '" + p_sal_period + "' " +
              "         AND NVL (a.net_amt, 0) " + p_type +
              "ORDER BY A.DEPT_NM, a.emp_id ";

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);
        int       irow_emp, icol_emp;

        irow_emp = dt_emp.Rows.Count;
        icol_emp = dt_emp.Columns.Count;
        if (irow_emp == 0)
        {
            Response.Write("There is no data of salary");
            Response.End();
        }

        string    para_com = "'" + p_dept + "','" + p_work_mon + "'";
        DataTable dt_com   = CtlLib.TableReadOpenCursor("ST_HR_RPT_CO_COMPANY", para_com);


        DataTable dt_allow = CtlLib.TableReadOpenCursor("ST_HR_RPT_CO_ALLOW_SALARY");

        DataTable dt_allow_k = CtlLib.TableReadOpenCursor("ST_HR_RPT_CO_ALLOW_K_SALARY");

        DataTable dt_equip = CtlLib.TableReadOpenCursor("ST_HR_RPT_CO_EQUIP");

        para_com = "'" + p_work_mon + "'";
        DataTable dt_adj = CtlLib.TableReadOpenCursor("ST_HR_RPT_ADJUST_SALARY", para_com);

        SQL
            = "SELECT NT_RATE nt30, " +
              "       NOT_RATE nt45, " +
              "       NOT_2_RATE nt45_2, " +
              "       OST_RATE ost, " +
              "       NOST_RATE nost, " +
              "       OHT_RATE oht, " +
              "       NOHT_RATE noht, " +
              "       NST_RATE nt60, " +
              "       NHT_RATE nt90 " +
              "  FROM THR_MONTH_OT_RATE " +
              " WHERE del_if = 0 AND work_mon = '" + p_work_mon + "' ";

        DataTable dt_rate = CtlLib.TableReadOpen(SQL);

        int i, j, k, i_allow, i_return_tax, i_deduct_notax, i_return_notax;
        int pos = 7;
        int allow = 11, c_return_tax = 67, c_deduct_notax = 87, c_return_notax = 93, allow_k = 24, equip = 125;     //vi tri phu cap
        int pos_nt = 49;
        int emp_dept = 0, n_dept = 0;

        Double[] sub_total   = new Double[icol_emp];
        Double[] grant_total = new Double[icol_emp];

        //bind data to excel file
        //-----header---
        exSheet.Cells[1, 1].Select();
        exSheet.Cells["C1"].Value      = dt_com.Rows[0][0].ToString();
        exSheet.Cells["C2"].Value      = dt_com.Rows[0][1].ToString();
        exSheet.Cells["C3"].Value      = "Phone: " + dt_com.Rows[0][2].ToString();
        exSheet.Cells[1, pos_nt].Value = "EMPLOYEE'S PAYROLL IN " + dt_com.Rows[0][4].ToString();
        exSheet.Cells[2, pos_nt].Value = "BẢNG LƯƠNG NHÂN VIÊN THÁNG " + dt_com.Rows[0][5].ToString();

        exSheet.Cells[4, pos_nt].Value = string.Format("NT {0}%", dt_rate.Rows[0]["NT30"].ToString());
        exSheet.Cells[5, pos_nt].Value = string.Format("Trợ cấp đêm", dt_rate.Rows[0]["NT30"].ToString());
        pos_nt += 2;
        exSheet.Cells[4, pos_nt].Value = string.Format("NT {0}% Amount", dt_rate.Rows[0]["NT30"].ToString());
        exSheet.Cells[5, pos_nt].Value = string.Format("Tiền trợ cấp đêm {0}%", dt_rate.Rows[0]["NT30"].ToString());
        pos_nt += 2;
        exSheet.Cells[4, pos_nt].Value = string.Format("NOT {0}%", dt_rate.Rows[0]["NT45"].ToString());
        exSheet.Cells[5, pos_nt].Value = string.Format("Trợ cấp T.C đêm", dt_rate.Rows[0]["NT45"].ToString());
        pos_nt += 2;
        exSheet.Cells[4, pos_nt].Value = string.Format("NOT {0}% Amount", dt_rate.Rows[0]["NT45"].ToString());
        exSheet.Cells[5, pos_nt].Value = string.Format("Tiền trợ T.C cấp đêm {0}%", dt_rate.Rows[0]["NT45"].ToString());
        pos_nt += 2;


        exSheet.Cells[4, pos_nt].Value = string.Format("NST {0}%", dt_rate.Rows[0]["NT60"].ToString());
        exSheet.Cells[5, pos_nt].Value = string.Format("Trợ cấp đêm chủ nhật", dt_rate.Rows[0]["NT60"].ToString());
        pos_nt += 2;
        exSheet.Cells[4, pos_nt].Value = string.Format("NST {0}% Amount", dt_rate.Rows[0]["NT60"].ToString());
        exSheet.Cells[5, pos_nt].Value = string.Format("Tiền trợ cấp đêm chủ nhật {0}%", dt_rate.Rows[0]["NT60"].ToString());
        pos_nt += 2;


        exSheet.Cells[4, pos_nt].Value = string.Format("NHT {0}%", dt_rate.Rows[0]["NT90"].ToString());
        exSheet.Cells[5, pos_nt].Value = string.Format("Trợ cấp đêm lễ", dt_rate.Rows[0]["NT90"].ToString());
        pos_nt += 2;
        exSheet.Cells[4, pos_nt].Value = string.Format("NHT {0}% Amount", dt_rate.Rows[0]["NT90"].ToString());
        exSheet.Cells[5, pos_nt].Value = string.Format("Tiền trợ cấp đêm lễ {0}%", dt_rate.Rows[0]["NT90"].ToString());



        for (i_allow = 0; i_allow < 12; i_allow++)
        {
            if (dt_allow.Rows[0][i_allow + 12].ToString() == "1")
            {
                exSheet.Cells[4, allow + i_allow].Value = dt_allow.Rows[0][i_allow].ToString();
                exSheet.Cells[5, allow + i_allow].Value = dt_allow.Rows[0][24 + i_allow].ToString();
            }
        }
        for (i_return_tax = 0; i_return_tax < 5; i_return_tax++)
        {
            if (dt_adj.Rows[0][i_return_tax].ToString() != "")
            {
                exSheet.Cells[4, c_return_tax + i_return_tax].Value = dt_adj.Rows[0][i_return_tax].ToString();
            }
        }
        //deduct notax
        for (i_deduct_notax = 0; i_deduct_notax < 5; i_deduct_notax++)
        {
            if (dt_adj.Rows[0][i_deduct_notax + 5].ToString() != "")
            {
                exSheet.Cells[4, c_deduct_notax + i_deduct_notax].Value = dt_adj.Rows[0][i_deduct_notax + 5].ToString();
            }
        }
        //return notax
        for (i_return_notax = 0; i_return_notax < 5; i_return_notax++)
        {
            if (dt_adj.Rows[0][i_return_notax + 10].ToString() != "")
            {
                exSheet.Cells[4, c_return_notax + i_return_notax].Value = dt_adj.Rows[0][i_return_notax + 10].ToString();
            }
        }

        //insert picture
        if (dt_com.Rows[0][3].ToString() != "")
        {
            byte[] MyData = new byte[0];
            MyData = (byte[])dt_com.Rows[0][3];
            MemoryStream stream = new MemoryStream(MyData);

            Bitmap b = new Bitmap(stream);

            exSheet.Shapes.AddPicture(ResizeImage(b, 115, 85));
        }

        //---end--header---

        for (k = 0; k < icol_emp; k++)
        {
            sub_total[k]   = 0;
            grant_total[k] = 0;
        }

        for (i = 0; i < irow_emp - 1; i++)
        {
            j = i + 1;
            if (dt_emp.Rows[i]["dept_pk"].ToString() != dt_emp.Rows[j]["dept_pk"].ToString())
            {
                n_dept++;
            }
        }

        //Insert new row

        for (i = 0; i < irow_emp - 1 + n_dept; i++)
        {
            exSheet.Range[pos + 1, 1].Rows.EntireRow.Insert();
        }

        //Show data
        for (i = 0; i < irow_emp; i++)
        {
            /* for (k = 9; k < icol_emp - 1; k++)
             * {
             *   if (k == icol_emp - 6)
             *   {
             *       continue;
             *   }
             *   else
             *   {
             *       sub_total[k] = sub_total[k] + double.Parse(dt_emp.Rows[i][k].ToString());
             *       grant_total[k] = grant_total[k] + double.Parse(dt_emp.Rows[i][k].ToString());
             *   }
             * } */
            emp_dept++;

            exSheet.Cells[pos + i, 1].Value = emp_dept;
            for (j = 2; j < icol_emp; j++)
            {
                if (IsNumeric(dt_emp.Rows[i][j].ToString()) && j != 3)
                {
                    exSheet.Cells[pos + i, j].Value = double.Parse(dt_emp.Rows[i][j].ToString());
                }
                else
                {
                    exSheet.Cells[pos + i, j].Value = dt_emp.Rows[i][j].ToString();
                }
            }


            if (i != irow_emp - 1 && (dt_emp.Rows[i]["dept_pk"].ToString() != dt_emp.Rows[i + 1]["dept_pk"].ToString()))
            {
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, 4].Merge();
                exSheet.Cells[pos + i + 1, 1].Value = "Total " + dt_emp.Rows[i]["dept_nm"].ToString() + " (" + emp_dept + " employees)";
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, icol_emp - 1].Rows.Font.Bold = true;
                for (k = 9; k < icol_emp - 1; k++)
                {
                    exSheet.Cells[pos + i + 1, k].Value = sub_total[k];
                    sub_total[k] = 0;
                }
                //Insert page break
                IPageBreaks pbs       = exBook.Worksheets[1].HPageBreaks;
                IRange      usedrange = exBook.Worksheets[1].UsedRange;
                //pbs.Reset();
                pbs.Add(usedrange.Rows[pos + i + 2]);
                //end insert page beak
                pos      = pos + 1;
                emp_dept = 0;
            }
            else if (i == irow_emp - 1)
            {
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, 4].Merge();
                exSheet.Cells[pos + i + 1, 1].Value = "Total " + dt_emp.Rows[i]["dept_nm"].ToString() + ": " + emp_dept + " employee(s)";
                exSheet.Cells[pos + i + 1, 1, pos + i + 1, icol_emp - 1].Rows.Font.Bold = true;
                exSheet.Cells[pos + i + 2, 1, pos + i + 2, 4].Merge();
                exSheet.Cells[pos + i + 2, 1].Value = "Grant Total: " + dt_emp.Rows.Count + " employee(s)";
                exSheet.Cells[pos + i + 2, 1, pos + i + 2, icol_emp - 1].Rows.Font.Bold = true;
                for (k = 9; k < icol_emp - 1; k++)
                {
                    exSheet.Cells[pos + i + 1, k].Value = sub_total[k];
                    exSheet.Cells[pos + i + 2, k].Value = grant_total[k];
                }
            }
        }

        //Hide allowance column
        for (i_allow = 0; i_allow < 12; i_allow++)
        {
            if (dt_allow.Rows[0][i_allow + 12].ToString() != "1")
            {
                exSheet.Range[1, allow + i_allow].Columns.Hidden = true;
            }
        }
        //Hide adj return tax
        for (i_return_tax = 0; i_return_tax < 5; i_return_tax++)
        {
            if (dt_adj.Rows[0][i_return_tax].ToString() == "")
            {
                exSheet.Range[1, c_return_tax + i_return_tax].Columns.Hidden = true;
            }
        }
        //Hide adj deduct notax
        for (i_deduct_notax = 0; i_deduct_notax < 5; i_deduct_notax++)
        {
            if (dt_adj.Rows[0][i_deduct_notax + 5].ToString() == "")
            {
                exSheet.Range[1, c_deduct_notax + i_deduct_notax].Columns.Hidden = true;
            }
        }
        //Hide adj return notax
        for (i_deduct_notax = 0; i_deduct_notax < 5; i_deduct_notax++)
        {
            if (dt_adj.Rows[0][i_deduct_notax + 5].ToString() == "")
            {
                exSheet.Range[1, c_deduct_notax + i_deduct_notax].Columns.Hidden = true;
            }
        }
        //Hide adj return notax
        for (i_return_notax = 0; i_return_notax < 5; i_return_notax++)
        {
            if (dt_adj.Rows[0][i_return_notax + 10].ToString() == "")
            {
                exSheet.Range[1, c_return_notax + i_return_notax].Columns.Hidden = true;
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string TemplateFile = "rpt_hrem00201_0.xls";
        string TempFile     = "../../../../system/temp/rpt_hrem00201_0_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        string p_tco_org_pk, p_nation, p_emp_search, p_select_date, p_from_date, p_to_date, p_status, p_salary_yn, p_position;
        string p_user, p_sal_security;
        string p_data, p_lang;

        p_user         = Request["p_user"].ToString();
        p_tco_org_pk   = Request["p_tco_org_pk"].ToString();
        p_nation       = Request["p_nation"].ToString();
        p_emp_search   = Request["p_emp_search"].ToString();
        p_select_date  = Request["p_select_date"].ToString();
        p_from_date    = Request["p_from_date"].ToString();
        p_to_date      = Request["p_to_date"].ToString();
        p_salary_yn    = Request["p_salary_yn"].ToString();
        p_position     = Request["p_position"].ToString();
        p_status       = Request["p_status"].ToString();
        p_sal_security = Request["p_sal_security"].ToString();
        p_data         = Request["p_data"].ToString();
        p_lang         = Request["p_lang"].ToString();

        string SQL
            = "select E.COLUMN_NUM,E.TITLE_NM,E.TITLE_FIELD,E.TITLE_FNM,E.TITLE_FIELD_FNM " +
              "from thr_title_emp e " +
              "where del_if = 0 ";

        DataTable dt_data = CtlLib.TableReadOpen(SQL);

        DataTable dt_allow = CtlLib.TableReadOpenCursor("ST_HR_RPT_HREM00201_0");

        int i, j, k, l;

        //int pic;

        int[]    pos_allow = new int[8];
        string[] allow_nm  = new string[8];
        string[] allow_fnm = new string[8];
        string[] p         = p_data.Split('|');
        //Response.Write(p_field);
        string s_field = "";
        string s_name  = "";



        //Response.Write(p[k].ToString() + " -- " + k.ToString() +" /n ");
        for (l = 0; l < p.Length; l++)
        {
            for (k = 0; k < dt_data.Rows.Count; k++)
            {
                /*               if (int.Parse(p[l].ToString()) == allow && allow < 54)
                 *             {
                 *                 pos_allow[k] = l + 2;  //ghi nhan lai vi tri column cua Allowance
                 *
                 *
                 *             }
                 */
                if (p[l].ToString() == dt_data.Rows[k][0].ToString())
                {
                    //Response.Write(p[l].ToString());
                    if (p_lang == "ENG")
                    {
                        s_field = s_field + dt_data.Rows[k][2].ToString() + ",";

                        if (int.Parse(p[l].ToString()) == 46)
                        {
                            s_name = s_name + dt_allow.Rows[0][0].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 47)
                        {
                            s_name = s_name + dt_allow.Rows[0][1].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 48)
                        {
                            s_name = s_name + dt_allow.Rows[0][2].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 49)
                        {
                            s_name = s_name + dt_allow.Rows[0][3].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 50)
                        {
                            s_name = s_name + dt_allow.Rows[0][4].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 51)
                        {
                            s_name = s_name + dt_allow.Rows[0][5].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 52)
                        {
                            s_name = s_name + dt_allow.Rows[0][6].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 53)
                        {
                            s_name = s_name + dt_allow.Rows[0][7].ToString() + "|";
                        }
                        else
                        {
                            s_name = s_name + dt_data.Rows[k][1].ToString() + "|";
                        }

                        if (dt_data.Rows[k][0].ToString() == "86") //picture
                        {
                            pic = l;                               //lay vi tri cua hinh
                        }
                    }
                    else
                    {
                        s_field = s_field + dt_data.Rows[k][4].ToString() + ",";

                        if (int.Parse(p[l].ToString()) == 46)
                        {
                            s_name = s_name + dt_allow.Rows[0][16].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 47)
                        {
                            s_name = s_name + dt_allow.Rows[0][17].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 48)
                        {
                            s_name = s_name + dt_allow.Rows[0][18].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 49)
                        {
                            s_name = s_name + dt_allow.Rows[0][19].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 50)
                        {
                            s_name = s_name + dt_allow.Rows[0][20].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 51)
                        {
                            s_name = s_name + dt_allow.Rows[0][21].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 52)
                        {
                            s_name = s_name + dt_allow.Rows[0][22].ToString() + "|";
                        }
                        else if (int.Parse(p[l].ToString()) == 53)
                        {
                            s_name = s_name + dt_allow.Rows[0][23].ToString() + "|";
                        }
                        else
                        {
                            s_name = s_name + dt_data.Rows[k][3].ToString() + "|";
                        }

                        if (dt_data.Rows[k][0].ToString() == "86") //picture
                        {
                            pic = l;                               //lay vi tri cua hinh
                        }
                    }
                }
            }
        }
        //Response.Write(pic);
        //Response.Write(s_name);
        //Response.End();
        s_field = s_field.Substring(0, s_field.Length - 1);
        SQL     = " select  " + s_field;
        SQL     = SQL + " from thr_employee a, tco_org c,thr_work_group d,THR_F_PASSPORT p,THR_F_LABOUR_LICENCE l,THR_F_VISA f, thr_f_residence g, TAC_ABPL pl, thr_family fa, thr_family fa1,  ";
        SQL     = SQL + "(SELECT edu1.thr_emp_pk,edu1.E_NAME, edu1.Major, edu1.graduate_year " +
                  "             FROM thr_emp_edu edu1,(SELECT edu2.THR_EMP_PK ,MAX (graduate_year) AS graduate_year from thr_emp_edu edu2 where edu2.del_if = 0 group by edu2.THR_EMP_PK ) edu_mas         " +
                  "             where edu1.del_if = 0 and edu_mas.THR_EMP_PK = edu1.THR_EMP_PK and  edu_mas.graduate_year = edu1.graduate_year  " +
                  "             )edu   ";
        SQL = SQL + "         ,(select G.THR_EMPLOYEE_PK emp_pk,G.COM_NM com_nm, G.DUTIES duties,g.start_dt ,g.end_dt,HR_F_GET_PERIOD_PCSS(g.THR_EMPLOYEE_PK) period ";
        SQL = SQL + "            from thr_experience g, ";
        SQL = SQL + "            (select F.THR_EMPLOYEE_PK emp_pk, max(f.END_DT) end_dt ";
        SQL = SQL + "            from thr_experience f ";
        SQL = SQL + "            where f.del_if = 0  ";
        SQL = SQL + "            group by F.THR_EMPLOYEE_PK) h ";
        SQL = SQL + "            where G.DEL_IF = 0 and G.THR_EMPLOYEE_PK = h.emp_pk ";
        SQL = SQL + "            and G.END_DT = h.end_dt) i         ";
        SQL = SQL + "where a.del_if=0   and c.del_if=0 and d.del_if=0 and fa.del_if(+) = 0 and fa1.del_if(+) = 0 ";
        SQL = SQL + "and edu.thr_emp_pk(+) =a.pk  ";
        SQL = SQL + "and a.tco_org_pk=c.pk and a.thr_wg_pk=d.pk and pl.pk(+)=a.TAC_ABPL_PK and A.PK = i.emp_pk(+) and fa.thr_employee_pk(+) = a.pk and fa1.thr_employee_pk(+) = a.pk and nvl(fa.tutor(+),'N') = 'Y' AND NVL(fa1.family_other(+),'N') = 'Y' ";
        SQL = SQL + "AND A.THR_F_PASSPORT_PK = p.pk(+) AND A.THR_F_LABOUR_LICENCE_PK = l.pk(+) AND A.THR_F_VISA_PK = f.pk(+) AND A.THR_F_RESIDENCE_PK = g.pk(+)  ";
        SQL = SQL + "and decode('" + p_nation + "','ALL','ALL',a.NATION )='" + p_nation + "'";
        SQL = SQL + "     and ('" + p_emp_search + "' is null  " +
              "                 or  (upper(a.emp_id) like  '%' || upper('" + p_emp_search + "')|| '%') " +
              "                 or  (upper(a.old_id ) like  '%' || upper('" + p_emp_search + "')|| '%') " +
              "                 or  (upper(a.full_name) like  '%' || upper('" + p_emp_search + "')|| '%')) " +
              "     and (('" + p_from_date + "' is null and '" + p_to_date + "' is null) " +
              "            or ('" + p_select_date + "' = 1 and (decode(length(a.birth_dt),4,a.birth_dt || '0101',a.birth_dt) between '" + p_from_date + "' and '" + p_to_date + "'))  " +
              "            or ('" + p_select_date + "' = 2 and a.join_dt between '" + p_from_date + "' and '" + p_to_date + "') " +
              "            or ('" + p_select_date + "' = 3 and a.left_dt between '" + p_from_date + "' and '" + p_to_date + "') " +
              "         ) " +
              "     and decode('" + p_salary_yn + "','ALL','ALL',a.salary_yn )='" + p_salary_yn + "' " +
              "     and decode('" + p_position + "','ALL','ALL',a.pos_type )='" + p_position + "' " +
              "     and decode('" + p_status + "','ALL','ALL',a.status)='" + p_status + "' " +
              "      and (   a.tco_org_pk in ( " +
              "                              select     g.pk " +
              "                                    from tco_org g " +
              "                                   where g.del_if = 0 " +
              "                              start with g.pk = " +
              "                                            decode ('" + p_tco_org_pk + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_tco_org_pk + "' " +
              "                                                   ) " +
              "                              connect by prior g.pk = g.p_pk) " +
              "                        or '" + p_tco_org_pk + "' = 'ALL') " +
              "      order by nvl(c.seq,0) ,org_nm  ,a.emp_id ";



        //Response.Write(SQL);
        //Response.End();

        DataTable dt_emp = CtlLib.TableReadOpen(SQL);



        SQL
            = "select to_char(sysdate,'dd/mm/yyyy') from dual ";
        DataTable dt_sysdt = CtlLib.TableReadOpen(SQL);


        string[] s = s_name.Split('|');
        //Insert column
        for (i = 3; i < s.Length - 1; i++)
        {
            exSheet.Range[1, 5, 1, 5].Columns.EntireColumn.Insert();
        }

        for (i = 0; i < s.Length - 1; i++)
        {
            exSheet.Cells[5, 2 + i].Value = s[i];
        }
        //Header
        exSheet.Range[1, 1, 1, s.Length].Merge();
        if (p_lang == "ENG")
        {
            exSheet.Cells[1, 1].Value = "LIST OF EMPLOYEES";
            exSheet.Cells[2, 2].Value = "Reporter";
            exSheet.Cells[3, 2].Value = "Report Date";
        }
        else
        {
            exSheet.Cells[1, 1].Value = "DANH SÁCH THÔNG TIN NHÂN VIÊN";
            exSheet.Cells[2, 2].Value = "Người báo cáo";
            exSheet.Cells[3, 2].Value = "Ngày báo cáo";
        }

        exSheet.Cells[2, 3].Value = Session["User_ID"].ToString();
        exSheet.Cells[3, 3].Value = dt_sysdt.Rows[0][0].ToString();

        //Insert Row
        for (i = 0; i < dt_emp.Rows.Count - 1; i++)
        {
            if (pic != 1000)
            {
                exSheet.Range[6, 1].Rows.RowHeight = 70;
            }

            exSheet.Range[7, 1, 7, 1].Rows.EntireRow.Insert();
            //exSheet.Range[4, 1, 5,11].Copy(exSheet.Range[6,1,7,11], XlPasteType.xlPasteAll);
        }

        for (i = 0; i < dt_emp.Rows.Count; i++)
        {
            exSheet.Cells[6 + i, 1].Value = i + 1;
            for (j = 2; j <= s.Length; j++)
            {
                //if (IsNumber(dt_emp.Rows[i][j - 2].ToString()) == false || int.Parse(p[j-2].ToString()) <= 14 )
                if (j - 2 == pic && pic != 1000)
                {
                    exSheet.Cells[6 + i, j].Select();
                    if (dt_emp.Rows[i][j - 2].ToString() != "")
                    {
                        byte[] MyData = new byte[0];
                        MyData = (byte[])dt_emp.Rows[i][j - 2];
                        MemoryStream stream = new MemoryStream(MyData);

                        Bitmap b = new Bitmap(stream);

                        exSheet.Shapes.AddPicture(ResizeImage(b, 105, 93));
                    }
                }

                exSheet.Cells[6 + i, j].Value = dt_emp.Rows[i][j - 2].ToString();
                //else
                //{
                //    exSheet.Cells[6 + i, j].NumberFormat = "_(* #,##0_);_(* (#,##0);_(* " + "-" + "_);_(@_)";
                //    exSheet.Cells[6 + i, j].Value = Double.Parse(dt_emp.Rows[i][j - 2].ToString());
                //}
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }

        //Xoa column theo security
        int tmp = 0, tmp1 = 0;

        if (p_sal_security == "N")
        {
            for (i = 0; i < s.Length - 1; i++)
            {
                if (int.Parse(p[i].ToString()) >= 44 && int.Parse(p[i].ToString()) <= 53)
                {
                    tmp++;
                    if (tmp1 == 0)
                    {
                        tmp1 = i;
                    }
                }
            }
            tmp1 = tmp1 + 2;        //trong report bat dau bang colunm thu 2
            for (i = 0; i < tmp; i++)
            {
                exSheet.Range[1, tmp1].Columns.EntireColumn.Delete();
            }
        }

        exSheet.Cells.Columns.Autofit();


        exBook.SaveAs(TempFile);
        //ESysLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept_pk, p_work_mon, p_from_dt, p_to_dt, p_nation, p_group_pk, p_meal_emp_type;

        p_dept_pk       = Request["l_tco_dept_pk"].ToString();
        p_group_pk      = Request["l_thr_group_pk"].ToString();
        p_work_mon      = Request["l_work_mon"].ToString();
        p_to_dt         = Request["l_to_dt"].ToString();
        p_from_dt       = Request["l_from_dt"].ToString();
        p_meal_emp_type = Request["l_meal_emp_type"].ToString();

        string TemplateFile = "rpt_chrm00020_4.xls";
        string TempFile     = "../../../../system/temp/rpt_chrm00020_4_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        string SQL_CODE
            = "SELECT code,code_nm " +
              "    FROM vhr_hr_code c " +
              "   WHERE id = 'HR0191' ";

        DataTable dt_hr_code = CtlLib.TableReadOpen(SQL_CODE);


        //======================================================================================================================
        string SQL
            = "SELECT '','', to_char(to_date(a.work_dt,'yyyymmdd'),'dd/mm/yyyy'), " +
              "       NVL(A.MEAL_TIME_01,0),  " +
              "       NVL(A.MEAL_TIME_02,0),  " +
              "       NVL(A.MEAL_TIME_03,0),  " +
              "       NVL(A.MEAL_TIME_04,0),  " +
              "       NVL (A.MEAL_MAIN_QTY, 0) + NVL (A.SNACK_TIME_QTY, 0),  " +
              "       NVL (A.MEAL_MAIN_AMT, 0) + NVL (A.SNACK_TIME_AMT, 0)  " +
              "  FROM THR_MEAL_DAILY_SUMMARY a " +
              " WHERE     a.del_if = 0 AND A.WORK_DT BETWEEN '" + p_from_dt + "' AND '" + p_to_dt + "' " +
              "ORDER BY a.work_dt ";

        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);

        int irow = dt_emp.Rows.Count;
        int icol = dt_emp.Columns.Count;

        if (irow == 0)
        {
            Response.Write("There is no data.");
            Response.End();
        }



        //Khai bao bien
        int i, j, k, pos = 4;
        int ntotal = 6;

        double[] s_total = new double[ntotal];

        for (j = 0; j < ntotal; j++)
        {
            s_total[j] = 0;
        }

        //Insert new row

        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[pos + 1, 1].Rows.EntireRow.Insert();
        }

        //Title value

        exSheet.Cells["A1"].Value = "THEO DÕI SUẤT ĂN THÁNG " + p_work_mon.Substring(4, 2).ToString() + "/" + p_work_mon.Substring(0, 4).ToString();

        //Show data
        for (i = 0; i < irow; i++)
        {
            for (k = 0; k < ntotal; k++)
            {
                s_total[k] = s_total[k] + double.Parse(dt_emp.Rows[i][k + 3].ToString());
            }

            exSheet.Cells[pos + i, 1].Value = i + 1;
            for (j = 2; j < icol; j++)
            {
                if (j <= 2)
                {
                    exSheet.Cells[pos + i, j].Value = dt_emp.Rows[i][j].ToString();
                }
                else
                {
                    exSheet.Cells[pos + i, j].Value = double.Parse(dt_emp.Rows[i][j].ToString());
                }
            }

            //Dong sub total
        }

        for (k = 0; k < ntotal; k++)
        {
            exSheet.Cells[pos + irow, k + 3].Value = s_total[k];
        }

        //Xóa cột dư ở những tháng không đủ 31 ngày


        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser("APP_DBUSER");


        string p_user, p_tco_org_pk, p_wg, p_work_shift, p_search_by, p_input;
        string p_from_date, p_to_date, p_from_date_text, p_to_date_text, p_date_type, p_nation, p_reg, p_data_type, p_confirm;


        p_tco_org_pk = Request["p_tco_org_pk"].ToString();
        p_wg         = Request["p_wg"].ToString();
        p_date_type  = Request["p_date_type"].ToString();
        p_input      = Request["p_search_temp"].ToString();
        p_work_shift = Request["p_work_shift"].ToString();
        p_from_date  = Request["p_from_date"].ToString();
        p_to_date    = Request["p_to_date"].ToString();
        p_user       = Request["p_user"].ToString();
        p_nation     = Request["p_nation"].ToString();
        p_reg        = Request["p_reg"].ToString();
        p_data_type  = Request["p_data_type"].ToString();
        p_confirm    = Request["p_confirm"].ToString();

        int p_tmp;

        string SQL_Com
            = "SELECT A.NUM_4 FROM VHR_HR_CODE A " +
              " WHERE A.ID='HR0006' AND A.CODE='11'";

        DataTable dt_Com = CtlLib.TableReadOpen(SQL_Com);
        int       irow_com;

        irow_com = dt_Com.Rows.Count;
        if (irow_com == 0)
        {
            p_tmp = 0;
        }
        else
        {
            p_tmp = int.Parse(dt_Com.Rows[0][0].ToString());
        }

        string SQL = "";

        #region statement query

        if (p_data_type == "5")
        {
            SQL = "      SELECT c.ORG_NM C0,gp.WORKGROUP_NM C1,A.EMP_ID C2, A.FULL_NAME C3,TO_CHAR(TO_DATE(GW.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') C4 " +
                  "      ,WS.SHIFT C5,WKD.TIME_IN C6,WKD.TIME_OUT C7,NULLIF(WKD.WORK_TIME,0) C8 " +
                  "       ,NULLIF(WKD.OVT,0) C9,NULLIF(WKD.NT,0) C10,NULLIF(WKD.NT2,0) C11,NULLIF(WKD.NT3,0) C11_2,NULLIF(WKD.HT,0) C12 " +
                  "       ,(SELECT V.CODE_fNM FROM VHR_HR_CODE V WHERE V.ID='HR0003' AND V.CODE=ABS.CODE) C13 " +
                  "       , NULLIF(ABS.ABSENCE_TIME,0) c14 " +
                  "       ,gw.note_att c15 " +
                  "      FROM THR_EMPLOYEE A, TCO_ORG C,THR_WORK_GROUP GP,THR_WG_SCH_DETAIL GW,THR_WORK_SHIFT WS " +
                  "             ,(SELECT D.THR_EMP_PK AS THR_EMP_PK,D.WORK_DT AS W_DATE,MAX( D.TIME_IN) AS TIME_IN, MAX(D.TIME_OUT) AS TIME_OUT, MAX(D.WORK_TIME) AS WORK_TIME " +
                  "           ,MAX(D.PK) AS PK,MAX(D.THR_WS_PK) AS THR_WS_PK " +
                  "           ,SUM(DECODE(H.OT_TYPE,'OT',NVL(H.OT_TIME,0),0)) AS OVT " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT',NVL(H.OT_TIME,0),0)) AS NT " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT2',NVL(H.OT_TIME,0),0)) AS NT2 " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT3',NVL(H.OT_TIME,0),0)) AS NT3 " +
                  "            ,SUM(DECODE(H.OT_TYPE,'HT',NVL(H.OT_TIME,0),0)) AS HT " +
                  "             ,SUM(DECODE(H.OT_TYPE,'OT',H.PK,0)) AS OVT_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'NT',H.PK,0)) AS NT_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'NT2',H.PK,0)) AS NT2_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'HT',H.PK,0)) AS HT_PK " +
                  "             ,MAX( D.DATE_IN) AS DATE_IN " +
                  "             ,MAX( D.DATE_OUT) AS DATE_OUT " +
                  "             ,MAX(DECODE(NVL(D.MOD_BYHAND_YN,'N'),'N',NVL(D.MOD_AUTO_YN,'N'),'Y')) AS BYHAND_YN " +
                  "             ,MAX(DECODE(NVL(H.MOD_BYHAND_YN,'N'),'N',NVL(H.MOD_AUTO_YN,'N'),'Y')) AS BYHAND_YN2 " +
                  "             ,D.NO_SCAN_FLAG AS NO_SCAN " +
                  "                FROM THR_TIME_MACHINE D,THR_EXTRA_TIME H,THR_EMPLOYEE V " +
                  "              WHERE D.DEL_IF = 0 AND V.DEL_IF=0  " +
                  "              AND H.DEL_IF(+)=0 AND D.THR_EMP_PK=V.PK   " +
                  "              AND D.THR_EMP_PK=H.THR_EMP_PK(+) " +
                  "              AND D.WORK_DT=H.WORK_DT(+) " +
                  "              AND D.WORK_DT BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(V.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(V.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(V.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "               AND V.TCO_ORG_PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  "              GROUP BY  D.THR_EMP_PK, D.WORK_DT,D.NO_SCAN_FLAG) WKD " +
                  "           ,(SELECT E.ABSENCE_DT AS ABS_DATE,E.THR_EMP_PK, E.ABSENCE_TYPE AS CODE, E.ABSENCE_TIME,E.PK " +
                  "               FROM THR_ABSENCE E,THR_EMPLOYEE F " +
                  "              WHERE E.DEL_IF = 0 AND F.DEL_IF=0  AND F.PK=E.THR_EMP_PK " +
                  "                AND E.ABSENCE_DT  BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(F.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(F.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(F.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "                AND F.TCO_ORG_PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  "            ) ABS " +
                  "     WHERE A.DEL_IF = 0 AND C.DEL_IF=0 AND GP.DEL_IF=0 AND GW.DEL_IF=0 AND WS.DEL_IF=0  " +
                  "       AND A.PK=GW.THR_EMP_PK AND GW.THR_WS_PK=WS.PK  " +
                  "       AND NVL(A.OLD_JOIN_DT,A.JOIN_DT) <= '" + p_to_date + "' " +
                  "       AND (A.LEFT_DT > TO_CHAR(TO_DATE(GW.WORK_DT,'YYYYMMDD')-'" + p_tmp + "','YYYYMMDD') OR A.LEFT_DT IS NULL) " +
                  "       AND A.TCO_ORG_PK = C.PK AND A.THR_WG_PK=GP.PK " +
                  "       AND GW.WORK_DT BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "       AND GW.THR_EMP_PK = WKD.THR_EMP_PK(+) " +
                  "       AND GW.THR_EMP_PK = ABS.THR_EMP_PK(+) " +
                  "       AND GW.WORK_DT=WKD.W_DATE(+) " +
                  "       AND GW.WORK_DT=ABS.ABS_DATE(+) " +
                  "     AND DECODE ('" + p_wg + "', 'ALL', '" + p_wg + "', GP.PK) = '" + p_wg + "'" +
                  "   AND DECODE ('" + p_work_shift + "', 'ALL', '" + p_work_shift + "', WS.PK) = '" + p_work_shift + "'" +
                  "   AND DECODE ('" + p_nation + "', 'ALL', '" + p_nation + "', a.nation) = '" + p_nation + "'" +
                  "   AND ('" + p_reg + "'='ALL' OR A.PK IN(SELECT R.THR_EMP_PK " +
                  "                                         FROM THR_REGULATION R  " +
                  "                                         WHERE R.DEL_IF=0 AND R.THR_EMP_PK=A.PK " +
                  "                                            AND GW.WORK_DT>=R.START_DT AND NVL(R.END_DT,GW.WORK_DT)>=GW.WORK_DT) " +
                  "        ) " +
                  "       AND C.PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  "       AND A.PK NOT IN(SELECT AB.THR_EMP_PK FROM THR_ABSENCE_REG AB WHERE AB.DEL_IF=0 " +
                  "                    AND GW.WORK_DT BETWEEN AB.START_DT AND AB.END_DT AND AB.THR_EMP_PK=A.PK ) " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  " and decode('" + p_confirm + "','ALL','ALL',nvl(GW.APPROVE_YN,'N')) = '" + p_confirm + "'   " +
                  "    ORDER BY C.ORG_NM,A.EMP_ID,GW.WORK_DT ";
        }
        else if (p_data_type == "1")
        {
            SQL = "      SELECT c.ORG_NM C0,gp.WORKGROUP_NM C1,A.EMP_ID C2, A.FULL_NAME C3,TO_CHAR(TO_DATE(GW.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') C4 " +
                  "      ,WS.SHIFT C5,NULL C6,NULL C7,NULL C8 " +
                  "       ,NULL C9,NULL C10,NULL C11,NULL C11_2,NULL C12 " +
                  "       ,(SELECT V.CODE_fNM FROM VHR_HR_CODE V WHERE V.ID='HR0003' AND V.CODE=ABS.CODE) C13 " +
                  "       , NULLIF(ABS.ABSENCE_TIME,0) " +
                  "       ,gw.note_att c15 " +
                  "      FROM THR_EMPLOYEE A, TCO_ORG C,THR_WORK_GROUP GP,THR_WG_SCH_DETAIL GW,THR_WORK_SHIFT WS " +
                  "           ,(SELECT E.ABSENCE_DT AS ABS_DATE,E.THR_EMP_PK, E.ABSENCE_TYPE AS CODE, E.ABSENCE_TIME,E.PK " +
                  "               FROM THR_ABSENCE E,THR_EMPLOYEE F " +
                  "              WHERE E.DEL_IF = 0 AND F.DEL_IF=0  AND F.PK=E.THR_EMP_PK " +
                  "                AND E.ABSENCE_DT  BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(F.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(F.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(F.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "                AND F.TCO_ORG_PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  "            ) ABS " +
                  "     WHERE A.DEL_IF = 0 AND C.DEL_IF=0 AND GP.DEL_IF=0 AND GW.DEL_IF=0 AND WS.DEL_IF=0  " +
                  "       AND A.PK=GW.THR_EMP_PK AND GW.THR_WS_PK=WS.PK  " +
                  "       AND NVL(A.OLD_JOIN_DT,A.JOIN_DT) <= '" + p_to_date + "' " +
                  "       AND (A.LEFT_DT > TO_CHAR(TO_DATE(GW.WORK_DT,'YYYYMMDD')-'" + p_tmp + "','YYYYMMDD') OR A.LEFT_DT IS NULL) " +
                  "       AND A.TCO_ORG_PK = C.PK AND A.THR_WG_PK=GP.PK " +
                  "       AND GW.WORK_DT BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "       AND GW.THR_EMP_PK = ABS.THR_EMP_PK " +
                  "       AND GW.WORK_DT=ABS.ABS_DATE " +
                  "     AND DECODE ('" + p_wg + "', 'ALL', '" + p_wg + "', GP.PK) = '" + p_wg + "'" +
                  "   AND DECODE ('" + p_work_shift + "', 'ALL', '" + p_work_shift + "', WS.PK) = '" + p_work_shift + "'" +
                  "   AND DECODE ('" + p_nation + "', 'ALL', '" + p_nation + "', a.nation) = '" + p_nation + "'" +
                  "   AND ('" + p_reg + "'='ALL' OR A.PK IN(SELECT R.THR_EMP_PK " +
                  "                                         FROM THR_REGULATION R  " +
                  "                                         WHERE R.DEL_IF=0 AND R.THR_EMP_PK=A.PK " +
                  "                                            AND GW.WORK_DT>=R.START_DT AND NVL(R.END_DT,GW.WORK_DT)>=GW.WORK_DT) " +
                  "        ) " +
                  "       AND C.PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  "       AND A.PK NOT IN(SELECT AB.THR_EMP_PK FROM THR_ABSENCE_REG AB WHERE AB.DEL_IF=0 " +
                  "                    AND GW.WORK_DT BETWEEN AB.START_DT AND AB.END_DT AND AB.THR_EMP_PK=A.PK ) " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  " and decode('" + p_confirm + "','ALL','ALL',nvl(GW.APPROVE_YN,'N')) = '" + p_confirm + "'   " +
                  "    ORDER BY C.ORG_NM,A.EMP_ID,GW.WORK_DT ";
        }
        else if (p_data_type == "2")
        {
            SQL = "      SELECT c.ORG_NM C0,gp.WORKGROUP_NM C1,A.EMP_ID C2, A.FULL_NAME C3,TO_CHAR(TO_DATE(GW.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') C4 " +
                  "      ,WS.SHIFT C5,WKD.TIME_IN C6,WKD.TIME_OUT C7,NULLIF(WKD.WORK_TIME,0) C8 " +
                  "       ,NULLIF(WKD.OVT,0) C9,NULLIF(WKD.NT,0) C10,NULLIF(WKD.NT2,0) C11,NULLIF(WKD.NT3,0) C11_2,NULLIF(WKD.HT,0) C12 " +
                  "       ,NULL C13 " +
                  "       , NULL " +
                  "       ,gw.note_att c15 " +
                  "      FROM THR_EMPLOYEE A, TCO_ORG C,THR_WORK_GROUP GP,THR_WG_SCH_DETAIL GW,THR_WORK_SHIFT WS " +
                  "             ,(SELECT D.THR_EMP_PK AS THR_EMP_PK,D.WORK_DT AS W_DATE,MAX( D.TIME_IN) AS TIME_IN, MAX(D.TIME_OUT) AS TIME_OUT, MAX(D.WORK_TIME) AS WORK_TIME " +
                  "           ,MAX(D.PK) AS PK,MAX(D.THR_WS_PK) AS THR_WS_PK " +
                  "           ,SUM(DECODE(H.OT_TYPE,'OT',NVL(H.OT_TIME,0),0)) AS OVT " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT',NVL(H.OT_TIME,0),0)) AS NT " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT2',NVL(H.OT_TIME,0),0)) AS NT2 " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT3',NVL(H.OT_TIME,0),0)) AS NT3 " +
                  "            ,SUM(DECODE(H.OT_TYPE,'HT',NVL(H.OT_TIME,0),0)) AS HT " +
                  "             ,SUM(DECODE(H.OT_TYPE,'OT',H.PK,0)) AS OVT_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'NT',H.PK,0)) AS NT_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'NT2',H.PK,0)) AS NT2_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'HT',H.PK,0)) AS HT_PK " +
                  "             ,MAX( D.DATE_IN) AS DATE_IN " +
                  "             ,MAX( D.DATE_OUT) AS DATE_OUT " +
                  "             ,MAX(DECODE(NVL(D.MOD_BYHAND_YN,'N'),'N',NVL(D.MOD_AUTO_YN,'N'),'Y')) AS BYHAND_YN " +
                  "             ,MAX(DECODE(NVL(H.MOD_BYHAND_YN,'N'),'N',NVL(H.MOD_AUTO_YN,'N'),'Y')) AS BYHAND_YN2 " +
                  "             ,D.NO_SCAN_FLAG AS NO_SCAN, max(h.OT_PLUS) as OT_PLUS " +
                  "                FROM THR_TIME_MACHINE D,THR_EXTRA_TIME H,THR_EMPLOYEE V " +
                  "              WHERE D.DEL_IF = 0 AND V.DEL_IF=0  " +
                  "              AND H.DEL_IF(+)=0 AND D.THR_EMP_PK=V.PK   " +
                  "              AND D.THR_EMP_PK=H.THR_EMP_PK(+) " +
                  "              AND D.WORK_DT=H.WORK_DT(+) " +
                  "              AND D.WORK_DT BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(V.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(V.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(V.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "               AND V.TCO_ORG_PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  " AND D.TIME_IN IS NOT NULL AND D.TIME_OUT IS NOT NULL " +
                  "              GROUP BY  D.THR_EMP_PK, D.WORK_DT,D.NO_SCAN_FLAG) WKD " +
                  "     WHERE A.DEL_IF = 0 AND C.DEL_IF=0 AND GP.DEL_IF=0 AND GW.DEL_IF=0 AND WS.DEL_IF=0  " +
                  "       AND A.PK=GW.THR_EMP_PK AND GW.THR_WS_PK=WS.PK  " +
                  "       AND NVL(A.OLD_JOIN_DT,A.JOIN_DT) <= '" + p_to_date + "' " +
                  "       AND (A.LEFT_DT > TO_CHAR(TO_DATE(GW.WORK_DT,'YYYYMMDD')-'" + p_tmp + "','YYYYMMDD') OR A.LEFT_DT IS NULL) " +
                  "       AND A.TCO_ORG_PK = C.PK AND A.THR_WG_PK=GP.PK " +
                  "       AND GW.WORK_DT BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "       AND GW.THR_EMP_PK = WKD.THR_EMP_PK " +
                  "       AND GW.WORK_DT=WKD.W_DATE " +
                  "     AND DECODE ('" + p_wg + "', 'ALL', '" + p_wg + "', GP.PK) = '" + p_wg + "'" +
                  "   AND DECODE ('" + p_work_shift + "', 'ALL', '" + p_work_shift + "', WS.PK) = '" + p_work_shift + "'" +
                  "   AND DECODE ('" + p_nation + "', 'ALL', '" + p_nation + "', a.nation) = '" + p_nation + "'" +
                  "   AND ('" + p_reg + "'='ALL' OR A.PK IN(SELECT R.THR_EMP_PK " +
                  "                                         FROM THR_REGULATION R  " +
                  "                                         WHERE R.DEL_IF=0 AND R.THR_EMP_PK=A.PK " +
                  "                                            AND GW.WORK_DT>=R.START_DT AND NVL(R.END_DT,GW.WORK_DT)>=GW.WORK_DT) " +
                  "        ) " +
                  "       AND C.PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  "       AND A.PK NOT IN(SELECT AB.THR_EMP_PK FROM THR_ABSENCE_REG AB WHERE AB.DEL_IF=0 " +
                  "                    AND GW.WORK_DT BETWEEN AB.START_DT AND AB.END_DT AND AB.THR_EMP_PK=A.PK ) " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  " and decode('" + p_confirm + "','ALL','ALL',nvl(GW.APPROVE_YN,'N')) = '" + p_confirm + "'   " +
                  "    ORDER BY C.ORG_NM,A.EMP_ID,GW.WORK_DT ";
        }
        else if (p_data_type == "3")
        {
            SQL = "      SELECT c.ORG_NM C0,gp.WORKGROUP_NM C1,A.EMP_ID C2, A.FULL_NAME C3,TO_CHAR(TO_DATE(GW.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') C4 " +
                  "      ,WS.SHIFT C5,WKD.TIME_IN C6,WKD.TIME_OUT C7,NULLIF(WKD.WORK_TIME,0) C8 " +
                  "       ,NULLIF(WKD.OVT,0) C9,NULLIF(WKD.NT,0) C10,NULLIF(WKD.NT2,0) C11,NULLIF(WKD.NT3,0) C11_2,NULLIF(WKD.HT,0) C12 " +
                  "       ,NULL C13 " +
                  "       ,NULL " +
                  "       ,gw.note_att c15 " +
                  "      FROM THR_EMPLOYEE A, TCO_ORG C,THR_WORK_GROUP GP,THR_WG_SCH_DETAIL GW,THR_WORK_SHIFT WS " +
                  "             ,(SELECT D.THR_EMP_PK AS THR_EMP_PK,D.WORK_DT AS W_DATE,MAX( D.TIME_IN) AS TIME_IN, MAX(D.TIME_OUT) AS TIME_OUT, MAX(D.WORK_TIME) AS WORK_TIME " +
                  "           ,MAX(D.PK) AS PK,MAX(D.THR_WS_PK) AS THR_WS_PK " +
                  "           ,SUM(DECODE(H.OT_TYPE,'OT',NVL(H.OT_TIME,0),0)) AS OVT " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT',NVL(H.OT_TIME,0),0)) AS NT " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT2',NVL(H.OT_TIME,0),0)) AS NT2 " +
                  "           ,SUM(DECODE(H.OT_TYPE,'NT2',NVL(H.OT_TIME,0),0)) AS NT3 " +
                  "            ,SUM(DECODE(H.OT_TYPE,'HT',NVL(H.OT_TIME,0),0)) AS HT " +
                  "             ,SUM(DECODE(H.OT_TYPE,'OT',H.PK,0)) AS OVT_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'NT',H.PK,0)) AS NT_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'NT2',H.PK,0)) AS NT2_PK " +
                  "             ,SUM(DECODE(H.OT_TYPE,'HT',H.PK,0)) AS HT_PK " +
                  "             ,MAX( D.DATE_IN) AS DATE_IN " +
                  "             ,MAX( D.DATE_OUT) AS DATE_OUT " +
                  "             ,MAX(DECODE(NVL(D.MOD_BYHAND_YN,'N'),'N',NVL(D.MOD_AUTO_YN,'N'),'Y')) AS BYHAND_YN " +
                  "             ,MAX(DECODE(NVL(H.MOD_BYHAND_YN,'N'),'N',NVL(H.MOD_AUTO_YN,'N'),'Y')) AS BYHAND_YN2 " +
                  "             ,D.NO_SCAN_FLAG AS NO_SCAN, max(h.OT_PLUS) as OT_PLUS " +
                  "                FROM THR_TIME_MACHINE D,THR_EXTRA_TIME H,THR_EMPLOYEE V " +
                  "              WHERE D.DEL_IF = 0 AND V.DEL_IF=0  " +
                  "              AND H.DEL_IF(+)=0 AND D.THR_EMP_PK=V.PK   " +
                  "              AND D.THR_EMP_PK=H.THR_EMP_PK(+) " +
                  "              AND D.WORK_DT=H.WORK_DT(+) " +
                  "              AND D.WORK_DT BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(V.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(V.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(V.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "               AND V.TCO_ORG_PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  " AND ( (D.TIME_IN IS NULL AND D.TIME_OUT IS NOT NULL) " +
                  "        OR (D.TIME_IN IS NOT NULL AND D.TIME_OUT IS NULL) " +
                  "       ) " +
                  "              GROUP BY  D.THR_EMP_PK, D.WORK_DT,D.NO_SCAN_FLAG) WKD " +
                  "     WHERE A.DEL_IF = 0 AND C.DEL_IF=0 AND GP.DEL_IF=0 AND GW.DEL_IF=0 AND WS.DEL_IF=0  " +
                  "       AND A.PK=GW.THR_EMP_PK AND GW.THR_WS_PK=WS.PK  " +
                  "       AND NVL(A.OLD_JOIN_DT,A.JOIN_DT) <= '" + p_to_date + "' " +
                  "       AND (A.LEFT_DT > TO_CHAR(TO_DATE(GW.WORK_DT,'YYYYMMDD')-'" + p_tmp + "','YYYYMMDD') OR A.LEFT_DT IS NULL) " +
                  "       AND A.TCO_ORG_PK = C.PK AND A.THR_WG_PK=GP.PK " +
                  "       AND GW.WORK_DT BETWEEN '" + p_from_date + "' AND '" + p_to_date + "' " +
                  "       AND GW.THR_EMP_PK = WKD.THR_EMP_PK " +
                  "       AND GW.WORK_DT=WKD.W_DATE " +
                  "     AND DECODE ('" + p_wg + "', 'ALL', '" + p_wg + "', GP.PK) = '" + p_wg + "'" +
                  "   AND DECODE ('" + p_work_shift + "', 'ALL', '" + p_work_shift + "', WS.PK) = '" + p_work_shift + "'" +
                  "   AND DECODE ('" + p_nation + "', 'ALL', '" + p_nation + "', a.nation) = '" + p_nation + "'" +
                  "   AND ('" + p_reg + "'='ALL' OR A.PK IN(SELECT R.THR_EMP_PK " +
                  "                                         FROM THR_REGULATION R  " +
                  "                                         WHERE R.DEL_IF=0 AND R.THR_EMP_PK=A.PK " +
                  "                                            AND GW.WORK_DT>=R.START_DT AND NVL(R.END_DT,GW.WORK_DT)>=GW.WORK_DT) " +
                  "        ) " +
                  "       AND C.PK IN ( " +
                  "                                  SELECT     G.PK " +
                  "                                        FROM TCO_ORG G " +
                  "                                       WHERE G.DEL_IF = 0 " +
                  "                                  START WITH G.PK =(CASE WHEN '" + p_tco_org_pk + "'='ALL' THEN G.PK ELSE TO_NUMBER('" + p_tco_org_pk + "') END) " +
                  "                                  CONNECT BY PRIOR G.PK = G.P_PK) " +
                  "       AND A.PK NOT IN(SELECT AB.THR_EMP_PK FROM THR_ABSENCE_REG AB WHERE AB.DEL_IF=0 " +
                  "                    AND GW.WORK_DT BETWEEN AB.START_DT AND AB.END_DT AND AB.THR_EMP_PK=A.PK ) " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  " and decode('" + p_confirm + "','ALL','ALL',nvl(GW.APPROVE_YN,'N')) = '" + p_confirm + "'   " +
                  "    ORDER BY C.ORG_NM,A.EMP_ID,GW.WORK_DT ";
        }


        #endregion
        //Response.Write(SQL);
        //Response.End();
        DataTable dt_total = CtlLib.TableReadOpen(SQL);
        int       irow, icol;
        irow = dt_total.Rows.Count;
        icol = dt_total.Columns.Count;
        if (irow == 0)
        {
            Response.Write("There is no data");
            Response.End();
        }


        string TemplateFile = "rpt_hrti00700_0.xls";
        string TempFile     = "../../../../system/temp/rpt_hrti00700_0_" + Session["User_ID"].ToString() + ".xls";
        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets["Sheet1"];
        //begin export data here
        int      pos = 7;
        int      i;
        double[] grant_total = new double[icol + 1];
        for (int k = 0; k < icol; k++)
        {
            grant_total[k] = 0;
        }

        //Insert new row
        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[pos + i + 1, 1].Rows.EntireRow.Insert();
        }

        //begin export data here
        exSheet.Cells["A2"].Value = string.Format("From date: {0} To Date: {1}", p_from_date.Substring(6, 2) + "/" + p_from_date.Substring(4, 2) + "/" + p_from_date.Substring(0, 4), p_to_date.Substring(6, 2) + "/" + p_to_date.Substring(4, 2) + "/" + p_to_date.Substring(0, 4));

        exSheet.Cells["P3"].Value = p_user;
        exSheet.Cells["P4"].Value = DateTime.Now.ToString("dd/MM/yyyy");

        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[pos + i, 1].Value = i + 1;
            for (int j = 0; j < icol; j++)
            {
                if (IsNumeric(dt_total.Rows[i][j].ToString()))
                {
                    exSheet.Cells[pos + i, j + 2].Value = double.Parse(dt_total.Rows[i][j].ToString());
                    grant_total[j] += double.Parse(dt_total.Rows[i][j].ToString());
                }
                else
                {
                    exSheet.Cells[pos + i, j + 2].Value = dt_total.Rows[i][j].ToString();
                }
            }
        }

        for (int j = 0; j < icol; j++)
        {
            if (j == 15 || (j >= 8 && j <= 13))
            {
                exSheet.Cells[pos + irow, j + 2].Value = grant_total[j];
            }
        }

        //end export data
        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        string pdfFilePath = TempFile.Replace(".xls", ".pdf");
        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 29
0
    protected void Page_Load(object sender, EventArgs e)
    {
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_dept, p_group, p_status, p_type, p_pay_type, p_emp_id, p_work_mon, p_m_type, p_sal_period, p_emp_type;

        p_dept       = Request["l_dept"].ToString();
        p_group      = Request["l_group"].ToString();
        p_status     = Request["l_status"].ToString();
        p_type       = Request["l_type"].ToString();
        p_pay_type   = Request["l_pay_type"].ToString();
        p_emp_id     = Request["l_emp_id"].ToString();
        p_work_mon   = Request["l_work_mon"].ToString();
        p_sal_period = Request["l_sal_period"].ToString();
        p_emp_type   = Request["l_Emp_Type"].ToString();
        p_m_type     = Request.QueryString["l_m_type"].ToString();
        //p_from_leftdate = Request["l_from_leftdate"].ToString();
        //p_to_leftdate = Request["l_to_leftdate"].ToString();

        string TemplateFile = "rpt_hrpm00700_1.xls";
        string TempFile     = "../../../../system/temp/rpt_hrpm00700_1_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;

        if (p_type == "ALL")
        {
            p_type = "= NVL (a.net_amt, 0)";
        }

        string SQL
            = "SELECT A.TCO_DEPT_PK dept_pk,'',a.DEPT_NM dept_nm,   " +
              "    A.EMP_ID,   " +
              "    A.FULL_NAME,   " +
              "    to_char(to_date(A.JOIN_DT,'yyyymmdd'),'dd/mm/yyyy') join_dt,   " +
              "    to_char(to_date(A.left_DT,'yyyymmdd'),'dd/mm/yyyy') left_dt,   " +
              "    A.POS_NM,   " +
              "    ROUND(NVL(A.SALARY_LEVEL1,0),10),   " +
              "    ROUND(NVL(A.SALARY_LEVEL2,0),10),   " +
              "    ROUND(NVL(A.SALARY_LEVEL1,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND(NVL(A.SALARY_LEVEL2,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND(NVL(A.ALLOW_AMT1,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND(NVL(A.ALLOW_AMT2,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND(NVL(A.ALLOW_AMT3,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND (NVL (A.ALLOW_AMT1, 0) + NVL (A.ALLOW_AMT2, 0) + NVL (A.ALLOW_AMT3, 0), 0) total_allow, " +
              "    ROUND ((NVL (A.ALLOW_AMT1, 0) + NVL (A.ALLOW_AMT2, 0) + NVL (A.ALLOW_AMT3, 0))*NVL(A.EXCHANGE_RATE,1), 0) total_allow_vnd, " +
              "    ROUND(NVL(A.WT_L1,0)/8 + NVL(a.HOL_DAY_L1,0),10),  " +
              "    ROUND(NVL(A.WT_L2,0)/8 + NVL(a.HOL_DAY_L2,0),10),  " +
              "    ROUND(NVL(A.ABS_ALE_L1,0)/8,10),  " +
              "    ROUND(NVL(A.ABS_ALE_L2,0)/8,10),  " +
              "    ROUND(NVL(A.ABS_OTHER_PAY_L1,0)/8,10),  " +
              "    ROUND(NVL(A.ABS_OTHER_PAY_L2,0)/8,10),  " +
              "    ROUND((NVL(A.WT_L1_AMT,0) + nvl(A.WT_L2_AMT,0) + nvl(A.TOTAL_ABS_PAY_L1_AMT,0) + nvl(A.TOTAL_ABS_PAY_L2_AMT,0)+ NVL(a.HOL_DAY_L1_AMT,0)+ NVL(a.HOL_DAY_L2_AMT,0))*NVL(A.EXCHANGE_RATE,1),0) total_wd_amt,   " +
              "    ROUND(NVL(A.OT_L1,0),10),  " +
              "    ROUND(NVL(A.OT_L2,0),10),  " +
              "    ROUND(NVL(A.ST_L1,0) + nvl(A.HT_L1,0)+ nvl(A.OHT_L1,0),10),  " +
              "    ROUND(NVL(A.ST_L2,0) + nvl(A.HT_L2,0)+ nvl(A.OHT_L2,0),10),  " +
              "    ROUND(NVL(A.NT_30_L1,0),10),  " +
              "    ROUND(NVL(A.NT_30_L2,0),10),  " +
              "    ROUND(NVL(A.NT_45_L1,0),10),  " +
              "    ROUND(NVL(A.NT_45_L2,0),10),  " +
              "    ROUND(NVL(A.NT_60_L1,0),10),  " +
              "    ROUND(NVL(A.NT_60_L2,0),10),  " +
              "    ROUND(NVL(A.NT_90_L1,0),10),  " +
              "    ROUND(NVL(A.NT_90_L2,0),10),  " +
              "    ROUND((NVL(A.TOTAL_OT_L1_AMT,0) + nvl(A.TOTAL_OT_L2_AMT,0))*NVL(A.EXCHANGE_RATE,1),0) total_ot_amt,  " +
              "    ROUND((NVL(A.OT_TAX_L1_AMT,0) + nvl(A.OT_TAX_L2_AMT,0))*NVL(A.EXCHANGE_RATE,1),0) total_ot_tax,  " +
              "    ROUND((NVL(A.RETURN_AMT,0) + nvl(A.INC_AMT,0))*NVL(A.EXCHANGE_RATE,1),0),  " +
              "    ROUND(NVL(A.ALE_STOP,0),10),   " +
              "    ROUND((NVL(A.ALE_STOP_AMT,0))*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND(NVL(A.SEVERANCE_MONTH,0),10),   " +
              "    ROUND(NVL(A.SEVERANCE_AMT,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND(NVL(A.GROSS_AMT,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND(NVL(A.NET_AMT,0)*NVL(A.EXCHANGE_RATE,1),0), " +
              "    ROUND(NVL(A.ADVANCE_AMT,0)*NVL(A.EXCHANGE_RATE,1),0),  " +
              "    ROUND(NVL(A.DEDUCT_PIT,0)*NVL(A.EXCHANGE_RATE,1),0),  " +
              "    ROUND(NVL(A.HEALTH_COM_NET,0)*NVL(A.EXCHANGE_RATE,1) ,0),   " +
              "    ROUND(NVL(A.INCOME_AMT_AFTER,0)*NVL(A.EXCHANGE_RATE,1) ,0),   " +
              "    ROUND(NVL(A.ALLOW_AMT4,0)*NVL(A.EXCHANGE_RATE,1),0) house_company, " +
              "    ROUND(NVL(A.HOUSING_FEE,0)*NVL(A.EXCHANGE_RATE,1),0),  " +
              "    ROUND((NVL(A.NET_AMT,0) + NVL(A.HOUSING_FEE,0))*NVL(A.EXCHANGE_RATE,1),0),  " +
              "    ROUND(NVL(A.INCOME_BEFORE_TAX,0)*NVL(A.EXCHANGE_RATE,1),0),  " +
              "    ROUND(NVL(A.INCOME_TAX_NET,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND(NVL(A.INS_SALARY,0)*NVL(A.EXCHANGE_RATE,1),0), " +
              "    ROUND(NVL(A.HEALTH_45_AMT,0)*NVL(A.EXCHANGE_RATE,1),0),   " +
              "    ROUND((NVL(A.NET_AMT,0) + NVL(A.HOUSING_FEE,0) + NVL(A.INCOME_TAX_NET,0) + NVL(A.HEALTH_45_AMT,0))*NVL(A.EXCHANGE_RATE,1),0) total_cost  " +
              "    FROM THR_MONTH_SALARY A  " +
              "    WHERE A.DEL_IF=0 AND A.WORK_MON='" + p_work_mon + "'  " +
              "    and (a.tco_dept_pk in ( " +
              "                              SELECT     g.pk " +
              "                                    FROM tco_org g " +
              "                                   WHERE g.del_if = 0 " +
              "                              START WITH g.pk = " +
              "                                            DECODE ('" + p_dept + "', " +
              "                                                    'ALL', 0, " +
              "                                                    '" + p_dept + "' " +
              "                                                   ) " +
              "                              CONNECT BY PRIOR g.pk = g.p_pk) " +
              "                        OR '" + p_dept + "' = 'ALL') " +
              "   and decode('" + p_group + "','ALL','ALL',a.thr_group_pk)='" + p_group + "'" +
              "   and a.emp_id like '%" + p_emp_id + "%' " +
              "   and decode('" + p_status + "','ALL','ALL',a.status)='" + p_status + "'" +
              "   and decode('" + p_pay_type + "','ALL','ALL',a.pay_type)='" + p_pay_type + "'" +
              "   and decode('" + p_m_type + "','ALL','ALL',a.MONEY_KIND)='" + p_m_type + "'" +
              "   and decode('" + p_emp_type + "','ALL','ALL',A.EMPLOYEE_TYPE)='" + p_emp_type + "'" +
              "     AND NVL(A.NET_AMT,0) " + p_type +
              " ORDER BY A.DEPT_NM,A.THR_GROUP_NM,A.EMP_ID ";


        //Response.Write(SQL);
        //Response.End();
        DataTable dt_emp = CtlLib.TableReadOpen(SQL);
        int       irow_emp, icol_emp;

        irow_emp = dt_emp.Rows.Count;
        icol_emp = dt_emp.Columns.Count;
        if (irow_emp == 0)
        {
            Response.Write("There is no data of salary");
            Response.End();
        }

        string    para_com = "'" + p_dept + "','" + p_work_mon + "'";
        DataTable dt_com   = CtlLib.TableReadOpenCursor("ST_HR_RPT_CO_COMPANY", para_com);


        SQL
            = "SELECT NT_RATE nt30, " +
              "       NOT_RATE nt45, " +
              "       NOT_2_RATE nt45_2, " +
              "       OST_RATE ost, " +
              "       NOST_RATE nost, " +
              "       OHT_RATE oht, " +
              "       NOHT_RATE noht, " +
              "       NST_RATE nt60, " +
              "       NHT_RATE nt90 " +
              "  FROM THR_MONTH_OT_RATE " +
              " WHERE del_if = 0 AND work_mon = '" + p_work_mon + "' ";

        DataTable dt_rate = CtlLib.TableReadOpen(SQL);

        int i, j, k, i_allow;
        int pos = 6;
        int allow = 11, allow_k = 23, equip = 112;     //vi tri phu cap
        int pos_nt = 23;
        int emp_dept = 0, n_dept = 0;

        Double[] sub_total   = new Double[icol_emp];
        Double[] grant_total = new Double[icol_emp];

        //bind data to excel file
        //-----header---
        exSheet.Cells[1, 1].Select();
        exSheet.Cells["C1"].Value      = dt_com.Rows[0][0].ToString();
        exSheet.Cells["C2"].Value      = dt_com.Rows[0][1].ToString();
        exSheet.Cells["C3"].Value      = "Phone: " + dt_com.Rows[0][2].ToString();
        exSheet.Cells[1, pos_nt].Value = "EMPLOYEE'S PAYROLL IN " + dt_com.Rows[0][4].ToString();
        exSheet.Cells[2, pos_nt].Value = "BẢNG LƯƠNG NHÂN VIÊN THÁNG " + dt_com.Rows[0][5].ToString();



        //---end--header---

        for (k = 0; k < icol_emp; k++)
        {
            sub_total[k]   = 0;
            grant_total[k] = 0;
        }

        for (i = 0; i < irow_emp - 1; i++)
        {
            j = i + 1;
            if (dt_emp.Rows[i]["dept_pk"].ToString() != dt_emp.Rows[j]["dept_pk"].ToString())
            {
                n_dept++;
            }
        }

        //Insert new row

        for (i = 0; i < irow_emp - 1; i++)
        {
            exSheet.Range[pos + i + 1, 1].Rows.EntireRow.Insert();
        }

        //Show data
        for (i = 0; i < irow_emp; i++)
        {
            for (k = 8; k < icol_emp; k++)
            {
                grant_total[k] = grant_total[k] + double.Parse(dt_emp.Rows[i][k].ToString());
            }
            emp_dept++;

            exSheet.Cells[pos + i, 1].Value = emp_dept;
            for (j = 2; j < icol_emp; j++)
            {
                if (j < 8)
                {
                    exSheet.Cells[pos + i, j].Value = dt_emp.Rows[i][j].ToString();
                }
                else
                {
                    exSheet.Cells[pos + i, j].Value = double.Parse(dt_emp.Rows[i][j].ToString());
                }
            }


            if (i == irow_emp - 1)
            {
                exSheet.Cells[pos + i + 1, 1].Value = "Grant Total: " + dt_emp.Rows.Count + " employee(s)";
                //exSheet.Cells[pos + i + 2, 1, pos + i + 2, icol_emp - 1].Rows.Font.Bold = true;
                for (k = 8; k < icol_emp; k++)
                {
                    exSheet.Cells[pos + i + 1, k].Value = grant_total[k];
                }
            }
        }


        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }
Ejemplo n.º 30
0
    protected void Page_Load(object sender, EventArgs e)
    {
        //CtlLib.SetUser("hr");
        CtlLib.SetUser(Session["APP_DBUSER"].ToString());

        string p_user, p_tco_org_pk, p_wg, p_from, p_to;
        string p_thr_ws_pk, p_search, p_input, p_opt, p_opt_text, p_m1, p_m2, p_nation;


        p_tco_org_pk = Request["p_tco_org_pk"].ToString();
        p_wg         = Request["p_wg"].ToString();
        p_from       = Request["p_from"].ToString();
        p_to         = Request["p_to"].ToString();
        p_thr_ws_pk  = Request["p_thr_ws_pk"].ToString();
        p_input      = Request["p_input"].ToString();
        p_user       = Request["p_user"].ToString();
        p_opt        = Request["p_opt"].ToString();
        p_opt_text   = Request["p_opt_text"].ToString();
        p_m1         = Request["p_m1"].ToString();
        p_m2         = Request["p_m2"].ToString();
        p_nation     = Request["p_nation"].ToString();
        string SQL;


        string TemplateFile = "rpt_hrti00800_0.xls";
        string TempFile     = "../../../../system/temp/rpt_hrti00800_0_" + Session["User_ID"].ToString() + ".xls";

        TemplateFile = Server.MapPath(TemplateFile);
        TempFile     = Server.MapPath(TempFile);

        //Create a new workbook
        IWorkbook exBook = NativeExcel.Factory.OpenWorkbook(TemplateFile);

        //Add worksheet
        IWorksheet exSheet = exBook.Worksheets[1];

        IRange range;


        if (p_opt == "5")
        {
            SQL = "SELECT     TO_CHAR(SYSDATE,'DD/MM/YYYY') " +
                  "            ,F.org_NM " +
                  "            ,B.workgroup_NM " +
                  "            ,A.EMP_ID " +
                  "            ,A.FULL_NAME " +
                  "            ,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "            ,C.REMARK " +
                  "            ,D.TIME_IN,D.TIME_OUT " +
                  "            ,MAX(DECODE(D.HOL_TYPE, NULL, D.WT, '')) AS WT " +
                  "            ,MAX(D.OT) AS OT " +
                  "            ,MAX(D.NT) AS NT " +
                  "            ,MAX(D.ONT) AS NT2 " +
                  "            ,MAX(DECODE(D.HOL_TYPE, 'SUN', D.WT, '')) AS ST3 " +
                  "            ,MAX(DECODE(D.HOL_TYPE,'HOL', D.WT, '')) AS HT " +
                  "           ,(case when MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >=0 then NULL " +
                  "       else " +
                  "           ABS(MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK))) " +
                  "     END ) " +
                  "        AS LATE_IN " +
                  ",(case when MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=0 then null " +
                  "    else " +
                  "        abs(MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK))) " +
                  "  end)   AS EARLY_OUT " +
                  "            ,MAX(D.MOD_BYHAND_YN ) as by_hand " +
                  "            ,MAX(D.MOD_BY) as mod_by " +
                  "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_WORK_DATE D,tco_org F " +
                  "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND F.DEL_IF=0 " +
                  "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK " +
                  "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
                  "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
                  "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
                  "            AND DECODE('" + p_thr_ws_pk + "','ALL', '" + p_thr_ws_pk + "',C.PK)= '" + p_thr_ws_pk + "' ";
            SQL = SQL + "                   AND (    A.TCO_ORG_PK IN ( ";
            SQL = SQL + "                       SELECT     G.PK ";
            SQL = SQL + "                             FROM TCO_ORG G ";
            SQL = SQL + "                            WHERE G.DEL_IF = 0           ";
            SQL = SQL + "                       START WITH G.PK = ";
            SQL = SQL + "                                     DECODE ('" + p_tco_org_pk + "', ";
            SQL = SQL + "                                             'ALL', 0, ";
            SQL = SQL + "                                             '" + p_tco_org_pk + "' ";
            SQL = SQL + "                                            ) ";
            SQL = SQL + "                       CONNECT BY PRIOR G.PK = G.P_PK) ";
            SQL = SQL + "                 OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "            GROUP BY F.org_NM,B.workGROUP_NM,D.WORK_DT,A.EMP_ID,A.FULL_NAME,C.REMARK,D.TIME_IN,D.TIME_OUT,D.WT " +
                  "            ORDER BY F.org_NM,B.workGROUP_NM,D.WORK_DT,A.EMP_ID ";
        }
        else if (p_opt == "1")
        {
            SQL = "SELECT     TO_CHAR(SYSDATE,'DD/MM/YYYY') " +
                  "            ,F.org_NM " +
                  "            ,B.workgroup_NM " +
                  "            ,A.EMP_ID " +
                  "            ,A.FULL_NAME " +
                  "            ,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "            ,C.REMARK " +
                  "            ,D.TIME_IN,D.TIME_OUT " +
                  "            ,MAX(DECODE(D.HOL_TYPE, NULL, D.WT, '')) AS WT " +
                  "            ,MAX(D.OT) AS OT " +
                  "            ,MAX(D.NT) AS NT " +
                  "            ,MAX(D.ONT) AS NT2 " +
                  "            ,MAX(DECODE(D.HOL_TYPE, 'SUN', D.WT, '')) AS ST3 " +
                  "            ,MAX(DECODE(D.HOL_TYPE,'HOL', D.WT, '')) AS HT " +
                  "           ,(case when MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >=0 then NULL " +
                  "       else " +
                  "           ABS(MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK))) " +
                  "     END ) " +
                  "        AS LATE_IN " +
                  ",(case when MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=0 then null " +
                  "    else " +
                  "        abs(MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK))) " +
                  "  end)   AS EARLY_OUT " +
                  "            ,MAX(D.MOD_BYHAND_YN ) as by_hand " +
                  "            ,MAX(D.MOD_BY) as mod_by " +
                  "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_WORK_DATE D,tco_org F " +
                  "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND F.DEL_IF=0 " +
                  "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK " +
                  "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
                  "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
                  "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
                  "            AND DECODE('" + p_thr_ws_pk + "','ALL', '" + p_thr_ws_pk + "',C.PK)= '" + p_thr_ws_pk + "' ";
            SQL = SQL + "                   AND (    A.TCO_ORG_PK IN ( ";
            SQL = SQL + "                       SELECT     G.PK ";
            SQL = SQL + "                             FROM TCO_ORG G ";
            SQL = SQL + "                            WHERE G.DEL_IF = 0           ";
            SQL = SQL + "                       START WITH G.PK = ";
            SQL = SQL + "                                     DECODE ('" + p_tco_org_pk + "', ";
            SQL = SQL + "                                             'ALL', 0, ";
            SQL = SQL + "                                             '" + p_tco_org_pk + "' ";
            SQL = SQL + "                                            ) ";
            SQL = SQL + "                       CONNECT BY PRIOR G.PK = G.P_PK) ";
            SQL = SQL + "                 OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "            AND (D.TIME_IN IS NULL OR D.TIME_OUT IS NULL)          " +
                  "            GROUP BY F.org_NM,B.workGROUP_NM,D.WORK_DT,A.EMP_ID,A.FULL_NAME,C.REMARK,D.TIME_IN,D.TIME_OUT,D.WT " +
                  "            ORDER BY F.org_NM,B.workGROUP_NM,D.WORK_DT,A.EMP_ID ";
        }
        else if (p_opt == "2")
        {
            SQL = "SELECT     TO_CHAR(SYSDATE,'DD/MM/YYYY') " +
                  "            ,F.org_NM " +
                  "            ,B.workgroup_NM " +
                  "            ,A.EMP_ID " +
                  "            ,A.FULL_NAME " +
                  "            ,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "            ,C.REMARK " +
                  "            ,D.TIME_IN,D.TIME_OUT " +
                  "            ,MAX(DECODE(D.HOL_TYPE, NULL, D.WT, '')) AS WT " +
                  "            ,MAX(D.OT) AS OT " +
                  "            ,MAX(D.NT) AS NT " +
                  "            ,MAX(D.ONT) AS NT2 " +
                  "            ,MAX(DECODE(D.HOL_TYPE, 'SUN', D.WT, '')) AS ST3 " +
                  "            ,MAX(DECODE(D.HOL_TYPE,'HOL', D.WT, '')) AS HT " +
                  "           ,(case when MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >=0 then NULL " +
                  "       else " +
                  "           ABS(MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK))) " +
                  "     END ) " +
                  "        AS LATE_IN " +
                  ",(case when MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=0 then null " +
                  "    else " +
                  "        abs(MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK))) " +
                  "  end)   AS EARLY_OUT " +
                  "            ,MAX(D.MOD_BYHAND_YN ) as by_hand " +
                  "            ,MAX(D.MOD_BY) as mod_by " +
                  "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_WORK_DATE D,tco_org F " +
                  "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND F.DEL_IF=0 " +
                  "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK " +
                  "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
                  "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
                  "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
                  "            AND DECODE('" + p_thr_ws_pk + "','ALL', '" + p_thr_ws_pk + "',C.PK)= '" + p_thr_ws_pk + "' ";
            SQL = SQL + "                   AND (    A.TCO_ORG_PK IN ( ";
            SQL = SQL + "                       SELECT     G.PK ";
            SQL = SQL + "                             FROM TCO_ORG G ";
            SQL = SQL + "                            WHERE G.DEL_IF = 0           ";
            SQL = SQL + "                       START WITH G.PK = ";
            SQL = SQL + "                                     DECODE ('" + p_tco_org_pk + "', ";
            SQL = SQL + "                                             'ALL', 0, ";
            SQL = SQL + "                                             '" + p_tco_org_pk + "' ";
            SQL = SQL + "                                            ) ";
            SQL = SQL + "                       CONNECT BY PRIOR G.PK = G.P_PK) ";
            SQL = SQL + "                 OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "           AND (HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK) < 0  )  " +
                  "       AND (Abs(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >= '" + p_m1 + "' OR '" + p_m1 + "' is null) " +
                  "       AND (Abs(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) <= '" + p_m2 + "' OR '" + p_m2 + "' is null) " +
                  "            GROUP BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID,A.FULL_NAME,C.REMARK,D.TIME_IN,D.TIME_OUT,D.WT " +
                  "            ORDER BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID ";
        }
        else if (p_opt == "3")
        {
            SQL = "SELECT     TO_CHAR(SYSDATE,'DD/MM/YYYY') " +
                  "            ,F.org_NM " +
                  "            ,B.workgroup_NM " +
                  "            ,A.EMP_ID " +
                  "            ,A.FULL_NAME " +
                  "            ,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "            ,C.REMARK " +
                  "            ,D.TIME_IN,D.TIME_OUT " +
                  "            ,MAX(DECODE(D.HOL_TYPE, NULL, D.WT, '')) AS WT " +
                  "            ,MAX(D.OT) AS OT " +
                  "            ,MAX(D.NT) AS NT " +
                  "            ,MAX(D.ONT) AS NT2 " +
                  "            ,MAX(DECODE(D.HOL_TYPE, 'SUN', D.WT, '')) AS ST3 " +
                  "            ,MAX(DECODE(D.HOL_TYPE,'HOL', D.WT, '')) AS HT " +
                  "           ,(case when MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >=0 then NULL " +
                  "       else " +
                  "           ABS(MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK))) " +
                  "     END ) " +
                  "        AS LATE_IN " +
                  ",(case when MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=0 then null " +
                  "    else " +
                  "        abs(MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK))) " +
                  "  end)   AS EARLY_OUT " +
                  "            ,MAX(D.MOD_BYHAND_YN ) as by_hand " +
                  "            ,MAX(D.MOD_BY) as mod_by " +
                  "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_WORK_DATE D,tco_org F " +
                  "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND F.DEL_IF=0 " +
                  "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK " +
                  "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
                  "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
                  "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
                  "            AND DECODE('" + p_thr_ws_pk + "','ALL', '" + p_thr_ws_pk + "',C.PK)= '" + p_thr_ws_pk + "' ";
            SQL = SQL + "                   AND (    A.TCO_ORG_PK IN ( ";
            SQL = SQL + "                       SELECT     G.PK ";
            SQL = SQL + "                             FROM TCO_ORG G ";
            SQL = SQL + "                            WHERE G.DEL_IF = 0           ";
            SQL = SQL + "                       START WITH G.PK = ";
            SQL = SQL + "                                     DECODE ('" + p_tco_org_pk + "', ";
            SQL = SQL + "                                             'ALL', 0, ";
            SQL = SQL + "                                             '" + p_tco_org_pk + "' ";
            SQL = SQL + "                                            ) ";
            SQL = SQL + "                       CONNECT BY PRIOR G.PK = G.P_PK) ";
            SQL = SQL + "                 OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "              AND (HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK) < 0 )             " +
                  "       AND (Abs(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >= '" + p_m1 + "' OR '" + p_m1 + "' is null) " +
                  "       AND (Abs(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) <= '" + p_m2 + "' OR '" + p_m2 + "' is null) " +
                  "            GROUP BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID,A.FULL_NAME,C.REMARK,D.TIME_IN,D.TIME_OUT,D.WT " +
                  "            ORDER BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID ";
        }
        else if (p_opt == "4")
        {
            SQL = "SELECT     TO_CHAR(SYSDATE,'DD/MM/YYYY') " +
                  "            ,F.org_NM " +
                  "            ,B.workgroup_NM " +
                  "            ,A.EMP_ID " +
                  "            ,A.FULL_NAME " +
                  "            ,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "            ,C.REMARK " +
                  "            ,D.TIME_IN,D.TIME_OUT " +
                  "            ,MAX(DECODE(D.HOL_TYPE, NULL, D.WT, '')) AS WT " +
                  "            ,MAX(D.OT) AS OT " +
                  "            ,MAX(D.NT) AS NT " +
                  "            ,MAX(D.ONT) AS NT2 " +
                  "            ,MAX(DECODE(D.HOL_TYPE, 'SUN', D.WT, '')) AS ST3 " +
                  "            ,MAX(DECODE(D.HOL_TYPE,'HOL', D.WT, '')) AS HT " +
                  "           ,(case when MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >=0 then NULL " +
                  "       else " +
                  "           ABS(MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK))) " +
                  "     END ) " +
                  "        AS LATE_IN " +
                  ",(case when MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=0 then null " +
                  "    else " +
                  "        abs(MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK))) " +
                  "  end)   AS EARLY_OUT " +
                  "            ,MAX(D.MOD_BYHAND_YN ) as by_hand " +
                  "            ,MAX(D.MOD_BY) as mod_by " +
                  "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_WORK_DATE D,tco_org F " +
                  "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND F.DEL_IF=0 " +
                  "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK " +
                  "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
                  "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
                  "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
                  "            AND DECODE('" + p_thr_ws_pk + "','ALL', '" + p_thr_ws_pk + "',C.PK)= '" + p_thr_ws_pk + "' ";
            SQL = SQL + "                   AND (    A.TCO_ORG_PK IN ( ";
            SQL = SQL + "                       SELECT     G.PK ";
            SQL = SQL + "                             FROM TCO_ORG G ";
            SQL = SQL + "                            WHERE G.DEL_IF = 0           ";
            SQL = SQL + "                       START WITH G.PK = ";
            SQL = SQL + "                                     DECODE ('" + p_tco_org_pk + "', ";
            SQL = SQL + "                                             'ALL', 0, ";
            SQL = SQL + "                                             '" + p_tco_org_pk + "' ";
            SQL = SQL + "                                            ) ";
            SQL = SQL + "                       CONNECT BY PRIOR G.PK = G.P_PK) ";
            SQL = SQL + "                 OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "		AND (NVL(D.MOD_BYHAND_YN,'N')='Y' ) "+
                  "            GROUP BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID,A.FULL_NAME,C.REMARK,D.TIME_IN,D.TIME_OUT,D.WT " +
                  "            ORDER BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID ";
        }

/*		OR NVL(E.MOD_BYHAND_YN,'N')='Y' " +
 *          "        OR NVL(D.MOD_AUTO_YN,'N')='Y' OR NVL(E.MOD_AUTO_YN,'N')='Y'*/
        else if (p_opt == "6")
        {
            SQL = "SELECT     TO_CHAR(SYSDATE,'DD/MM/YYYY') " +
                  "            ,F.org_NM " +
                  "            ,B.workgroup_NM " +
                  "            ,A.EMP_ID " +
                  "            ,A.FULL_NAME " +
                  "            ,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "            ,C.REMARK " +
                  "            ,D.TIME_IN,D.TIME_OUT " +
                  "            ,MAX(DECODE(D.HOL_TYPE, NULL, D.WT, '')) AS WT " +
                  "            ,MAX(D.OT) AS OT " +
                  "            ,MAX(D.NT) AS NT " +
                  "            ,MAX(D.ONT) AS NT2 " +
                  "            ,MAX(DECODE(D.HOL_TYPE, 'SUN', D.WT, '')) AS ST3 " +
                  "            ,MAX(DECODE(D.HOL_TYPE,'HOL', D.WT, '')) AS HT " +
                  "           ,(case when MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >=0 then NULL " +
                  "       else " +
                  "           ABS(MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK))) " +
                  "     END ) " +
                  "        AS LATE_IN " +
                  ",(case when MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=0 then null " +
                  "    else " +
                  "        abs(MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK))) " +
                  "  end)   AS EARLY_OUT " +
                  "            ,MAX(D.MOD_BYHAND_YN ) as by_hand " +
                  "            ,MAX(D.MOD_BY) as mod_by " +
                  "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_WORK_DATE D,tco_org F " +
                  "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND F.DEL_IF=0 " +
                  "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK " +
                  "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
                  "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
                  "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
                  "            AND DECODE('" + p_thr_ws_pk + "','ALL', '" + p_thr_ws_pk + "',C.PK)= '" + p_thr_ws_pk + "' ";
            SQL = SQL + "                   AND (    A.TCO_ORG_PK IN ( ";
            SQL = SQL + "                       SELECT     G.PK ";
            SQL = SQL + "                             FROM TCO_ORG G ";
            SQL = SQL + "                            WHERE G.DEL_IF = 0           ";
            SQL = SQL + "                       START WITH G.PK = ";
            SQL = SQL + "                                     DECODE ('" + p_tco_org_pk + "', ";
            SQL = SQL + "                                             'ALL', 0, ";
            SQL = SQL + "                                             '" + p_tco_org_pk + "' ";
            SQL = SQL + "                                            ) ";
            SQL = SQL + "                       CONNECT BY PRIOR G.PK = G.P_PK) ";
            SQL = SQL + "                 OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  " AND (HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK) < 0  " +
                  "or HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK) < 0 ) " +
                  "AND ( ( " +
                  "(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK) < 0  and (abs(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >= '" + p_m1 + "' OR  '" + p_m1 + "'  is null) " +
                  "AND (abs(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) <= '" + p_m2 + "' OR '" + p_m2 + "'  is null)) " +
                  ") or (  " +
                  "HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)  < 0  and  (abs(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=  '" + p_m1 + "'  OR  '" + p_m1 + "'  is null) " +
                  "AND (abs(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) <=  '" + p_m2 + "'  OR  '" + p_m2 + "'  is null) " +
                  ")) " +
                  "            GROUP BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID,A.FULL_NAME,C.REMARK,D.TIME_IN,D.TIME_OUT,D.WT " +
                  "            ORDER BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID ";
        }
        else if (p_opt == "7")
        {
            SQL = "SELECT     TO_CHAR(SYSDATE,'DD/MM/YYYY') " +
                  "            ,F.org_NM " +
                  "            ,B.workgroup_NM " +
                  "            ,A.EMP_ID " +
                  "            ,A.FULL_NAME " +
                  "            ,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "            ,C.REMARK " +
                  "            ,D.TIME_IN,D.TIME_OUT " +
                  "            ,MAX(DECODE(D.HOL_TYPE, NULL, D.WT, '')) AS WT " +
                  "            ,MAX(D.OT) AS OT " +
                  "            ,MAX(D.NT) AS NT " +
                  "            ,MAX(D.ONT) AS NT2 " +
                  "            ,MAX(DECODE(D.HOL_TYPE, 'SUN', D.WT, '')) AS ST3 " +
                  "            ,MAX(DECODE(D.HOL_TYPE,'HOL', D.WT, '')) AS HT " +
                  "           ,(case when MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >=0 then NULL " +
                  "       else " +
                  "           ABS(MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK))) " +
                  "     END ) " +
                  "        AS LATE_IN " +
                  ",(case when MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=0 then null " +
                  "    else " +
                  "        abs(MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK))) " +
                  "  end)   AS EARLY_OUT " +
                  "            ,MAX(D.MOD_BYHAND_YN ) as by_hand " +
                  "            ,MAX(D.MOD_BY) as mod_by " +
                  "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_WORK_DATE D,tco_org F " +
                  "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND F.DEL_IF=0 " +
                  "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK " +
                  "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
                  "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
                  "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
                  "            AND DECODE('" + p_thr_ws_pk + "','ALL', '" + p_thr_ws_pk + "',C.PK)= '" + p_thr_ws_pk + "' ";
            SQL = SQL + "                   AND (    A.TCO_ORG_PK IN ( ";
            SQL = SQL + "                       SELECT     G.PK ";
            SQL = SQL + "                             FROM TCO_ORG G ";
            SQL = SQL + "                            WHERE G.DEL_IF = 0           ";
            SQL = SQL + "                       START WITH G.PK = ";
            SQL = SQL + "                                     DECODE ('" + p_tco_org_pk + "', ";
            SQL = SQL + "                                             'ALL', 0, ";
            SQL = SQL + "                                             '" + p_tco_org_pk + "' ";
            SQL = SQL + "                                            ) ";
            SQL = SQL + "                       CONNECT BY PRIOR G.PK = G.P_PK) ";
            SQL = SQL + "                 OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "           AND D.TIME_IN IS NOT NULL  " +
                  "    AND D.TIME_IN BETWEEN NVL('" + p_m1 + "',D.TIME_IN) AND NVL('" + p_m2 + "',D.TIME_IN)" +
                  "            GROUP BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID,A.FULL_NAME,C.REMARK,D.TIME_IN,D.TIME_OUT,D.WT " +
                  "            ORDER BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID ";
        }
        else //if (p_opt=="8")
        {
            SQL = "SELECT     TO_CHAR(SYSDATE,'DD/MM/YYYY') " +
                  "            ,F.org_NM " +
                  "            ,B.workgroup_NM " +
                  "            ,A.EMP_ID " +
                  "            ,A.FULL_NAME " +
                  "            ,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD'),'DD/MM/YYYY') " +
                  "            ,C.REMARK " +
                  "            ,D.TIME_IN,D.TIME_OUT " +
                  "            ,MAX(DECODE(D.HOL_TYPE, NULL, D.WT, '')) AS WT " +
                  "            ,MAX(D.OT) AS OT " +
                  "            ,MAX(D.NT) AS NT " +
                  "            ,MAX(D.ONT) AS NT2 " +
                  "            ,MAX(DECODE(D.HOL_TYPE, 'SUN', D.WT, '')) AS ST3 " +
                  "            ,MAX(DECODE(D.HOL_TYPE,'HOL', D.WT, '')) AS HT " +
                  "           ,(case when MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK)) >=0 then NULL " +
                  "       else " +
                  "           ABS(MAX(HR_LATELY_EARLY(D.WORK_DT,C.START_TIME,D.DATE_IN,D.TIME_IN,'IN',A.PK))) " +
                  "     END ) " +
                  "        AS LATE_IN " +
                  ",(case when MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK)) >=0 then null " +
                  "    else " +
                  "        abs(MAX(HR_LATELY_EARLY(D.DATE_OUT,D.TIME_OUT,DECODE(C.DURING_DAY,1,D.WORK_DT,TO_CHAR(TO_DATE(D.WORK_DT,'YYYYMMDD')+1,'YYYYMMDD')),C.END_TIME,'OUT',A.PK))) " +
                  "  end)   AS EARLY_OUT " +
                  "            ,MAX(D.MOD_BYHAND_YN ) as by_hand " +
                  "            ,MAX(D.MOD_BY) as mod_by " +
                  "            FROM THR_EMPLOYEE A,THR_work_group B, THR_WORK_SHIFT C,THR_WORK_DATE D,tco_org F " +
                  "            WHERE A.DEL_IF=0 AND B.DEL_IF=0 AND C.DEL_IF=0 AND D.DEL_IF=0 AND F.DEL_IF=0 " +
                  "            AND A.THR_wg_PK=B.PK AND A.PK=D.THR_EMP_PK AND C.PK=D.THR_WS_PK AND A.TCO_org_PK=F.PK " +
                  "            AND D.WORK_DT BETWEEN '" + p_from + "' AND  '" + p_to + "' " +
                  "            AND DECODE('" + p_wg + "' ,'ALL', '" + p_wg + "',B.PK) =  '" + p_wg + "'" +
                  "            AND DECODE('" + p_nation + "' ,'ALL', '" + p_nation + "',a.nation) =  '" + p_nation + "'" +
                  "            AND DECODE('" + p_thr_ws_pk + "','ALL', '" + p_thr_ws_pk + "',C.PK)= '" + p_thr_ws_pk + "' ";
            SQL = SQL + "                   AND (    A.TCO_ORG_PK IN ( ";
            SQL = SQL + "                       SELECT     G.PK ";
            SQL = SQL + "                             FROM TCO_ORG G ";
            SQL = SQL + "                            WHERE G.DEL_IF = 0           ";
            SQL = SQL + "                       START WITH G.PK = ";
            SQL = SQL + "                                     DECODE ('" + p_tco_org_pk + "', ";
            SQL = SQL + "                                             'ALL', 0, ";
            SQL = SQL + "                                             '" + p_tco_org_pk + "' ";
            SQL = SQL + "                                            ) ";
            SQL = SQL + "                       CONNECT BY PRIOR G.PK = G.P_PK) ";
            SQL = SQL + "                 OR '" + p_tco_org_pk + "' = 'ALL') " +
                  "              AND ('" + p_input + "' IS NULL  " +
                  "                     OR  (UPPER(A.EMP_ID) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.OLD_ID ) LIKE  '%' || UPPER('" + p_input + "')|| '%') " +
                  "                     OR  (UPPER(A.FULL_NAME) LIKE  '%' || UPPER('" + p_input + "')|| '%')) " +
                  "           AND D.TIME_OUT IS NOT NULL  " +
                  "    AND D.TIME_OUT BETWEEN NVL('" + p_m1 + "',D.TIME_OUT) AND NVL('" + p_m2 + "',D.TIME_OUT)" +
                  "            GROUP BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID,A.FULL_NAME,C.REMARK,D.TIME_IN,D.TIME_OUT,D.WT " +
                  "            ORDER BY F.ORG_NM,B.WORKGROUP_NM,D.WORK_DT,A.EMP_ID ";
        }


        //----------------------------------


        //Response.Write(SQL);
        //Response.End();
        DataTable dt_total = CtlLib.TableReadOpen(SQL);
        int       irow, icol;

        irow = dt_total.Rows.Count;
        icol = dt_total.Columns.Count;
        if (irow == 0)
        {
            Response.Write("There is no data");
            Response.End();
        }

        exSheet.Cells[1, 1].Value = "TIME SUMMARY ";
        exSheet.Cells[2, 2].Value = "Work date: ";
        exSheet.Cells[2, 3].Value = p_from.Substring(6, 2) + "/" + p_from.Substring(4, 2) + "/" + p_from.Substring(0, 4);
        exSheet.Cells[2, 4].Value = "~";
        exSheet.Cells[2, 5].Value = p_to.Substring(6, 2) + "/" + p_to.Substring(4, 2) + "/" + p_to.Substring(0, 4);

        exSheet.Cells[2, 9].Value  = "Reporter";
        exSheet.Cells[2, 11].Value = p_user;
        exSheet.Cells[3, 9].Value  = "Report Date";
        exSheet.Cells[3, 11].Value = dt_total.Rows[0][0].ToString();

        if (p_opt != "5")
        {
            Response.Write("Option:   ");
        }
        if (p_opt != "5")
        {
            Response.Write(p_opt_text);
        }

        int i;
        int row_pos = 8;

        //insert row
        for (i = 0; i < irow - 1; i++)
        {
            exSheet.Range[row_pos + i, 1].Rows.EntireRow.Insert();
        }

        // exSheet.Range[row_pos, 1, row_pos, 18].Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
        //exSheet.Range[row_pos + irow , 1, row_pos + irow , 18].Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;

        //show data
        row_pos = 7;
        for (i = 0; i < irow; i++)
        {
            exSheet.Cells[row_pos + i, 1].Value = i + 1;
            for (int j = 1; j < icol; j++)
            {
                //if (j >= 9 && j < 14)
                //{
                //    exSheet.Cells[row_pos + i, j + 1].Value = Double.Parse(dt_total.Rows[i][j].ToString());
                //}
                //else
                //{

                if (IsNumeric(dt_total.Rows[i][j].ToString()))
                {
                    exSheet.Cells[row_pos + i, j + 1].Value = Double.Parse(dt_total.Rows[i][j].ToString());
                }
                else
                {
                    exSheet.Cells[row_pos + i, j + 1].Value = dt_total.Rows[i][j].ToString();
                }
                // }
            }
        }

        // end loop detail percent
        if (File.Exists(TempFile))
        {
            File.Delete(TempFile);
        }


        // hide row firts

        //range = exSheet.Range["A1"];
        // hide row A5
        //range.Rows.Hidden = true;

        exBook.SaveAs(TempFile);
        //CtlLib.ExcelToPdf(TempFile);
        //string pdfFilePath = TempFile.Replace(".xls", ".pdf");

        //write out to client broswer
        System.IO.FileInfo file = new System.IO.FileInfo(TempFile);
        //System.IO.FileInfo file = new System.IO.FileInfo(pdfFilePath);
        Response.Clear();
        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        //Add header, give a default file name for "File Download/Store as"
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        //Add header, set file size to enable browser display download progress
        Response.AddHeader("Content-Length", file.Length.ToString());
        //Set the return string is unavailable reading for client, and must be downloaded
        Response.ContentType = "application/ms-exSheet";
        //Response.ContentType = "application/pdf";
        //Send file string to client
        Response.WriteFile(TempFile);
        //Response.WriteFile(pdfFilePath);
        //Stop execute
        Response.End();
    }