/*
     * Biểu đồ thống kê tình trạng đi muộn về sớm
     * daibx    22/06/2013
     */
    private void BieuDoThongKeTinhTrangDiMuonVeSom(string macb, int thang, int nam, int height)
    {
        try
        {
            string MaDonVi = Session["MaDonVi"].ToString();

            var data = DataController.DataHandler.GetInstance().ExecuteDataTable("ChamCong_GetDiMuonVeSomByMaCB", "@MA_CB", "@Thang", "@Nam", macb, thang, nam);

            string batDauLamSa           = new HeThongController().GetValueByName(SystemConfigParameter.BAT_DAU_LAM_SANG, MaDonVi);
            string ketThucLamSa          = new HeThongController().GetValueByName(SystemConfigParameter.KET_THUC_LAM_SANG, MaDonVi);
            string batDauLamCh           = new HeThongController().GetValueByName(SystemConfigParameter.BAT_DAU_LAM_CHIEU, MaDonVi);
            string ketThucLamCh          = new HeThongController().GetValueByName(SystemConfigParameter.KET_THUC_LAM_CHIEU, MaDonVi);
            string HThucLamT7            = new HeThongController().GetValueByName(SystemConfigParameter.HINH_THUC_LAM_T7, MaDonVi);
            string HThucLamCN            = new HeThongController().GetValueByName(SystemConfigParameter.HINH_THUC_LAM_CN, MaDonVi);
            int    soPhutChoPhepVeSom    = int.Parse(new HeThongController().GetValueByName(SystemConfigParameter.SO_PHUT_CHO_PHEP_VE_SOM, MaDonVi));
            int    soPhutChoPhepDiMuon   = int.Parse(new HeThongController().GetValueByName(SystemConfigParameter.SO_PHUT_CHO_PHEP_DI_MUON, MaDonVi));
            int    soPhutMinCoiLaLamThem = int.Parse(new HeThongController().GetValueByName(SystemConfigParameter.SO_PHUT_TOI_THIEU_DUOC_TINH_LAM_THEM, MaDonVi));
            bool   choPhepLamThemDauGio  = bool.Parse(new HeThongController().GetValueByName(SystemConfigParameter.CHO_PHEP_LAM_THEM_DAU_GIO, MaDonVi));
            int    soPhutDiMuonCoiLaNghi = int.Parse(new HeThongController().GetValueByName(SystemConfigParameter.SO_PHUT_DI_MUON_COI_LA_NGHI, MaDonVi));

            int gioVaoSang   = int.Parse(batDauLamSa.Split(':')[0]);
            int phutVaoSang  = int.Parse(batDauLamSa.Split(':')[1]);
            int gioRaSang    = int.Parse(ketThucLamSa.Split(':')[0]);
            int phutRaSang   = int.Parse(ketThucLamSa.Split(':')[1]);
            int gioVaoChieu  = int.Parse(batDauLamCh.Split(':')[0]);
            int phutVaoChieu = int.Parse(batDauLamCh.Split(':')[1]);
            int gioRaChieu   = int.Parse(ketThucLamCh.Split(':')[0]);
            int phutRaChieu  = int.Parse(ketThucLamCh.Split(':')[1]);

            int      maxDay = DateTime.DaysInMonth(nam, thang);
            object[] tenMoc = new object[maxDay];

            object[] diMuonList = new object[maxDay];
            object[] veSomList  = new object[maxDay];
            for (int i = 0; i < maxDay; i++)
            {
                diMuonList[i] = 0;
                veSomList[i]  = 0;
            }

            for (int i = 0; i < data.Rows.Count; i++)
            {
                var item = data.Rows[i];
                if (item != null)
                {
                    DateTime curDay = new DateTime(nam, thang, int.Parse(item["DAY"].ToString()));
                    int      thu    = (int)curDay.DayOfWeek;
                    if (item["DAY"] != null && item["DAY"].ToString() != "")
                    {
                        // Tính đi muộn, làm thêm đầu giờ
                        if (item["VAO"].ToString() != "")
                        {
                            int diMuon = 0;
                            if (thu == 6)   // thứ 7
                            {
                                switch (HThucLamT7)
                                {
                                case "FULLTIME":
                                    diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                    break;

                                case "LAM_SANG_NGHI_CHIEU":
                                    diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, int.Parse(item["VAO"].ToString().Split(':')[0]), int.Parse(item["VAO"].ToString().Split(':')[1]), int.Parse(item["VAO"].ToString().Split(':')[0]), int.Parse(item["VAO"].ToString().Split(':')[1]));
                                    break;

                                case "LAM_CHIEU_NGHI_SANG":
                                    diMuon = CalculateDiMuonChieu(gioVaoChieu, phutVaoChieu, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                    break;
                                }
                                if (diMuon > soPhutChoPhepDiMuon)
                                {
                                    diMuonList[int.Parse(item["DAY"].ToString()) - 1] = diMuon;
                                }
                            }
                            else if (thu == 0)  // chủ nhật
                            {
                                switch (HThucLamCN)
                                {
                                case "FULLTIME":
                                    diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                    break;

                                case "LAM_SANG_NGHI_CHIEU":
                                    diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, int.Parse(item["VAO"].ToString().Split(':')[0]), int.Parse(item["VAO"].ToString().Split(':')[1]), int.Parse(item["VAO"].ToString().Split(':')[0]), int.Parse(item["VAO"].ToString().Split(':')[1]));
                                    break;

                                case "LAM_CHIEU_NGHI_SANG":
                                    diMuon = CalculateDiMuonChieu(gioVaoChieu, phutVaoChieu, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                    break;
                                }
                                if (diMuon > soPhutChoPhepDiMuon)
                                {
                                    diMuonList[int.Parse(item["DAY"].ToString()) - 1] = diMuon;
                                }
                            }
                            else // ngày thường
                            {
                                diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                if (diMuon > soPhutChoPhepDiMuon)
                                {
                                    diMuonList[int.Parse(item["DAY"].ToString()) - 1] = diMuon;
                                }
                            }
                        }

                        // Tính về sớm, làm thêm cuối giờ
                        if (item["RA"].ToString() != "")
                        {
                            int veSom = 0;
                            if (thu == 6)   // thứ 7
                            {
                                switch (HThucLamT7)
                                {
                                case "FULLTIME":
                                    veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                    break;

                                case "LAM_SANG_NGHI_CHIEU":
                                    veSom = CalculateVeSomSang(gioRaSang, phutRaSang, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                    break;

                                case "LAM_CHIEU_NGHI_SANG":
                                    veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), int.Parse(item["RA"].ToString().Split(':')[0]), int.Parse(item["RA"].ToString().Split(':')[1]), gioVaoChieu, phutVaoChieu, int.Parse(item["RA"].ToString().Split(':')[0]), int.Parse(item["RA"].ToString().Split(':')[1]));
                                    break;
                                }
                                if (veSom > soPhutChoPhepVeSom)
                                {
                                    veSomList[int.Parse(item["DAY"].ToString()) - 1] = veSom;
                                }
                            }
                            else if (thu == 0)  // chủ nhật
                            {
                                switch (HThucLamCN)
                                {
                                case "FULLTIME":
                                    veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                    break;

                                case "LAM_SANG_NGHI_CHIEU":
                                    veSom = CalculateVeSomSang(gioRaSang, phutRaSang, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                    break;

                                case "LAM_CHIEU_NGHI_SANG":
                                    veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), int.Parse(item["RA"].ToString().Split(':')[0]), int.Parse(item["RA"].ToString().Split(':')[1]), gioVaoChieu, phutVaoChieu, int.Parse(item["RA"].ToString().Split(':')[0]), int.Parse(item["RA"].ToString().Split(':')[1]));
                                    break;
                                }
                                if (veSom > soPhutChoPhepVeSom)
                                {
                                    veSomList[int.Parse(item["DAY"].ToString()) - 1] = veSom;
                                }
                            }
                            else// ngày thường
                            {
                                veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutRaSang);
                                if (veSom > soPhutChoPhepVeSom)
                                {
                                    veSomList[int.Parse(item["DAY"].ToString()) - 1] = veSom;
                                }
                            }
                        }
                    }
                }
            }
            string tencb = data.Rows[0]["HO_TEN"].ToString();
            hcFrutas.Title = new Title("Thời gian đi muộn về sớm trong tháng " + thang + "/" + nam + " của nhân viên " + tencb);

            for (int i = 1; i <= maxDay; i++)
            {
                tenMoc[i - 1] = i;
            }

            // ve bieu do
            hcFrutas.YAxis.Add(new YAxisItem {
                title = new Title("Số phút")
            });
            hcFrutas.XAxis.Add(new XAxisItem {
                categories = tenMoc, title = new Title("Ngày trong tháng")
            });

            //dados
            var series = new Collection <Serie>();

            series.Add(new Serie {
                name = "Đi muộn", data = diMuonList
            });
            series.Add(new Serie {
                name = "Về sớm", data = veSomList
            });

            hcFrutas.PlotOptions = new PlotOptionsColumn()
            {
                borderColor  = "#dedede",
                borderRadius = 4,
                dataLabels   = new DataLabels()
                {
                    enabled = true,
                },
            };
            hcFrutas.Height = height;
            hcFrutas.Legend = new Legend()
            {
                layout          = Highcharts.Core.Layout.horizontal,
                align           = Align.left,
                verticalAlign   = Highcharts.Core.VerticalAlign.top,
                x               = 70,
                y               = -5,
                floating        = true,
                shadow          = true,
                backgroundColor = "#FFF",
                enabled         = true,
            };
            hcFrutas.Exporting.enabled = true;
            hcFrutas.DataSource        = series;
            hcFrutas.DataBind();
        }
        catch
        {
            throw;
        }
    }
    public DataTable GetDiMuonVeSom(DataTable data, string MaDonVi, string Nam, string Thang)
    {
        string batDauLamSa           = new HeThongController().GetValueByName(SystemConfigParameter.BAT_DAU_LAM_SANG, MaDonVi);
        string ketThucLamSa          = new HeThongController().GetValueByName(SystemConfigParameter.KET_THUC_LAM_SANG, MaDonVi);
        string batDauLamCh           = new HeThongController().GetValueByName(SystemConfigParameter.BAT_DAU_LAM_CHIEU, MaDonVi);
        string ketThucLamCh          = new HeThongController().GetValueByName(SystemConfigParameter.KET_THUC_LAM_CHIEU, MaDonVi);
        string HThucLamT7            = new HeThongController().GetValueByName(SystemConfigParameter.HINH_THUC_LAM_T7, MaDonVi);
        string HThucLamCN            = new HeThongController().GetValueByName(SystemConfigParameter.HINH_THUC_LAM_CN, MaDonVi);
        int    soPhutChoPhepVeSom    = int.Parse(new HeThongController().GetValueByName(SystemConfigParameter.SO_PHUT_CHO_PHEP_VE_SOM, MaDonVi));
        int    soPhutChoPhepDiMuon   = int.Parse(new HeThongController().GetValueByName(SystemConfigParameter.SO_PHUT_CHO_PHEP_DI_MUON, MaDonVi));
        int    soPhutMinCoiLaLamThem = int.Parse(new HeThongController().GetValueByName(SystemConfigParameter.SO_PHUT_TOI_THIEU_DUOC_TINH_LAM_THEM, MaDonVi));
        int    soPhutMuonCoiLaNghi   = int.Parse(new HeThongController().GetValueByName(SystemConfigParameter.SO_PHUT_DI_MUON_COI_LA_NGHI, MaDonVi));

        int gioVaoSang   = int.Parse(batDauLamSa.Split(':')[0]);
        int phutVaoSang  = int.Parse(batDauLamSa.Split(':')[1]);
        int gioRaSang    = int.Parse(ketThucLamSa.Split(':')[0]);
        int phutRaSang   = int.Parse(ketThucLamSa.Split(':')[1]);
        int gioVaoChieu  = int.Parse(batDauLamCh.Split(':')[0]);
        int phutVaoChieu = int.Parse(batDauLamCh.Split(':')[1]);
        int gioRaChieu   = int.Parse(ketThucLamCh.Split(':')[0]);
        int phutRaChieu  = int.Parse(ketThucLamCh.Split(':')[1]);

        DataTable table     = CreateDataTable();
        int       maxLength = data.Rows.Count;
        int       ngay      = DateTime.DaysInMonth(Convert.ToInt32(Nam), Convert.ToInt32(Thang));

        for (int i = 0; i < maxLength; i++)
        {
            int      thu = 0;
            DateTime curDay;
            var      item = data.Rows[i];
            if (item != null)
            {
                DataRow dr = table.NewRow();
                dr["MA_CB"]     = item["MA_CB"];  // MA_CB
                dr["HO_TEN"]    = item["HO_TEN"]; // HO_TEN
                dr["TEN_PHONG"] = item["TEN_PHONG"];
                string tenphong = item["TEN_PHONG"].ToString();
                string macb = item["MA_CB"].ToString();
                int    tongDiMuon = 0, tongVeSom = 0, solan_dimuon = 0, solan_vesom = 0;
                while (item["MA_CB"].ToString() == macb)
                {
                    if (item["DAY"] != null && item["DAY"].ToString() != "")
                    {
                        if (Convert.ToInt32(item["DAY"]) <= ngay)
                        {
                            curDay = new DateTime(int.Parse(Nam), int.Parse(Thang), int.Parse(item["DAY"].ToString()));
                            thu    = (int)curDay.DayOfWeek;
                        }
                        // Tính đi muộn, làm thêm đầu giờ
                        if (item["VAO"].ToString() != "")
                        {
                            int diMuon = 0;
                            if (thu == 6)   // thứ 7
                            {
                                switch (HThucLamT7)
                                {
                                case "FULLTIME":
                                    diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                    break;

                                case "LAM_SANG_NGHI_CHIEU":
                                    diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, int.Parse(item["VAO"].ToString().Split(':')[0]), int.Parse(item["VAO"].ToString().Split(':')[1]), int.Parse(item["VAO"].ToString().Split(':')[0]), int.Parse(item["VAO"].ToString().Split(':')[1]));
                                    break;

                                case "LAM_CHIEU_NGHI_SANG":
                                    diMuon = CalculateDiMuonChieu(gioVaoChieu, phutVaoChieu, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                    break;
                                }
                                if (diMuon > soPhutChoPhepDiMuon)
                                {
                                    tongDiMuon += diMuon;
                                    solan_dimuon++;
                                    dr["DI_MUON" + item["DAY"].ToString()] = diMuon > 60 ? ((diMuon / 60) + "").Split('.')[0] + "h" + (diMuon % 60) + "'" : diMuon + "'";
                                }
                            }
                            else if (thu == 0)  // chủ nhật
                            {
                                switch (HThucLamCN)
                                {
                                case "FULLTIME":
                                    diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                    break;

                                case "LAM_SANG_NGHI_CHIEU":
                                    diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, int.Parse(item["VAO"].ToString().Split(':')[0]), int.Parse(item["VAO"].ToString().Split(':')[1]), int.Parse(item["VAO"].ToString().Split(':')[0]), int.Parse(item["VAO"].ToString().Split(':')[1]));
                                    break;

                                case "LAM_CHIEU_NGHI_SANG":
                                    diMuon = CalculateDiMuonChieu(gioVaoChieu, phutVaoChieu, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                    break;
                                }
                                if (diMuon > soPhutChoPhepDiMuon)
                                {
                                    tongDiMuon += diMuon;
                                    solan_dimuon++;
                                    dr["DI_MUON" + item["DAY"].ToString()] = diMuon > 60 ? ((diMuon / 60) + "").Split('.')[0] + "h" + (diMuon % 60) + "'" : diMuon + "'";
                                }
                            }
                            else // ngày thường
                            {
                                diMuon = CalculateDiMuon(gioVaoSang, phutVaoSang, item["VAO"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioRaChieu, phutRaChieu);
                                if (diMuon > soPhutChoPhepDiMuon)
                                {
                                    tongDiMuon += diMuon;
                                    solan_dimuon++;
                                    dr["DI_MUON" + item["DAY"].ToString()] = diMuon > 60 ? ((diMuon / 60) + "").Split('.')[0] + "h" + (diMuon % 60) + "'" : diMuon + "'";
                                }
                            }
                        }

                        // Tính về sớm, làm thêm cuối giờ
                        if (item["RA"].ToString() != "")
                        {
                            int veSom = 0;
                            if (thu == 6)   // thứ 7
                            {
                                switch (HThucLamT7)
                                {
                                case "FULLTIME":
                                    veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                    break;

                                case "LAM_SANG_NGHI_CHIEU":
                                    veSom = CalculateVeSomSang(gioRaSang, phutRaSang, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                    break;

                                case "LAM_CHIEU_NGHI_SANG":
                                    veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), int.Parse(item["RA"].ToString().Split(':')[0]), int.Parse(item["RA"].ToString().Split(':')[1]), gioVaoChieu, phutVaoChieu, int.Parse(item["RA"].ToString().Split(':')[0]), int.Parse(item["RA"].ToString().Split(':')[1]));
                                    break;
                                }
                                if (veSom > soPhutChoPhepVeSom)
                                {
                                    tongVeSom += veSom;
                                    solan_vesom++;
                                    dr["VE_SOM" + item["DAY"].ToString()] = veSom > 60 ? ((veSom / 60) + "").Split('.')[0] + "h" + (veSom % 60) + "'" : veSom + "'";
                                }
                            }
                            else if (thu == 0)  // chủ nhật
                            {
                                switch (HThucLamCN)
                                {
                                case "FULLTIME":
                                    veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                    break;

                                case "LAM_SANG_NGHI_CHIEU":
                                    veSom = CalculateVeSomSang(gioRaSang, phutRaSang, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                    break;

                                case "LAM_CHIEU_NGHI_SANG":
                                    veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), int.Parse(item["RA"].ToString().Split(':')[0]), int.Parse(item["RA"].ToString().Split(':')[1]), gioVaoChieu, phutVaoChieu, int.Parse(item["RA"].ToString().Split(':')[0]), int.Parse(item["RA"].ToString().Split(':')[1]));
                                    break;
                                }
                                if (veSom > soPhutChoPhepVeSom)
                                {
                                    tongVeSom += veSom;
                                    solan_vesom++;
                                    dr["VE_SOM" + item["DAY"].ToString()] = veSom > 60 ? ((veSom / 60) + "").Split('.')[0] + "h" + (veSom % 60) + "'" : veSom + "'";
                                }
                            }
                            else// ngày thường
                            {
                                veSom = CalculateVeSom(gioRaChieu, phutRaChieu, item["RA"].ToString(), gioRaSang, phutRaSang, gioVaoChieu, phutVaoChieu, gioVaoSang, phutVaoSang);
                                if (veSom > soPhutChoPhepVeSom)
                                {
                                    tongVeSom += veSom;
                                    solan_vesom++;
                                    dr["VE_SOM" + item["DAY"].ToString()] = veSom > 60 ? ((veSom / 60) + "").Split('.')[0] + "h" + (veSom % 60) + "'" : veSom + "'";
                                }
                            }
                        }
                    }
                    i++;
                    if (i < maxLength)
                    {
                        item = data.Rows[i];
                    }
                    else
                    {
                        break;
                    }
                }
                if (tongDiMuon == 0 && tongVeSom == 0)
                {
                    continue;
                }
                dr["TONG_DIMUON"]  = tongDiMuon > 60 ? ((tongDiMuon / 60) + "").Split('.')[0] + "h" + (tongDiMuon % 60) + "'" : tongDiMuon + "'";
                dr["TONG_VESOM"]   = tongVeSom > 60 ? ((tongVeSom / 60) + "").Split('.')[0] + "h" + (tongVeSom % 60) + "'" : tongVeSom + "'";
                dr["SOLAN_DIMUON"] = solan_dimuon > 0 ? solan_dimuon + " lần" : "";
                dr["SOLAN_VESOM"]  = solan_vesom > 0 ? solan_vesom + " lần" : "";
                table.Rows.Add(dr);
                i--;
            }
        }
        return(table);
    }