//private void importExcel(string dir, string file)
        //{
        //    IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);
        //    string Excelstrcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + file + @";Extended Properties=""Excel 8.0;HDR=YES;""";

        //    string SQLstr = "SELECT * FROM [Sheet1$]";
        //    OleDbConnection ExcelCon = new OleDbConnection(Excelstrcon);
        //    ExcelCon.Open();

        //    OleDbCommand dbCommand = new OleDbCommand(SQLstr, ExcelCon);
        //    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);

        //    DataTable dTable = new DataTable();
        //    try
        //    {
        //        dataAdapter.Fill(dTable);
        //        int idx = 1;
        //        DateTime NgayChet = DateTime.MinValue;
        //        DateTime currNgay = DateTime.MinValue;
        //        string StrIDCaSau = "";
        //        string StrDaBung = "";
        //        string StrDaPhanLoai = "";
        //        string StrDau = "";
        //        string LyDo = "";
        //        foreach (DataRow r in dTable.Rows)
        //        {
        //            NgayChet = DateTime.Parse(r["Ngay"].ToString() + "/" + r["Thang"].ToString() + "/" + r["Nam"].ToString() + " 22:00:00", culture);

        //            if (NgayChet != currNgay)
        //            {
        //                if (StrIDCaSau != "")
        //                {
        //                    string fail = csCont.CaSauChet(StrIDCaSau, currNgay, UserId, LyDo, StrDaBung, StrDaPhanLoai, StrDau);
        //                    if (fail != "")
        //                    {
        //                        int sidx = idx - 1;
        //                        lblMessage.Text += "<br/>Dòng " + sidx.ToString() + ":" + fail;
        //                    }
        //                    StrIDCaSau = "";
        //                    StrDaBung = "";
        //                    StrDaPhanLoai = "";
        //                    StrDau = "";
        //                }
        //                currNgay = NgayChet;
        //            }
        //            string s = csCont.CaSau_Chet_GetCaByLoaiCaByChuongAtDate(Convert.ToInt32(r["LoaiCa"]), r["Chuong"].ToString(), Convert.ToInt32(r["SL"]), NgayChet);
        //            string sTemp = s.Replace("@", "");
        //            if ((s.Length - sTemp.Length) / 2 != Convert.ToInt32(r["SL"]))
        //            {
        //                lblMessage.Text += "<br/>Dòng " + idx.ToString() + " không import do số lượng cá không đủ.";
        //                idx++;
        //                continue;
        //            }

        //            StrIDCaSau += s;
        //            if (r["SanPham"] != DBNull.Value && r["SanPham"].ToString() != "")
        //            {
        //                if(r["SanPham"].ToString() == "-1")
        //                {
        //                    for(int i = 0; i<Convert.ToInt32(r["SL"]); i++)
        //                    {
        //                        StrDaBung += "@0@";
        //                        StrDaPhanLoai += "@4@";
        //                        StrDau += "@1@";
        //                    }
        //                }
        //                else
        //                {
        //                    for (int i = 0; i < Convert.ToInt32(r["SL"]); i++)
        //                    {
        //                        StrDaBung += "@" + r["SanPham"].ToString() + "@";
        //                        StrDaPhanLoai += "@4@";
        //                        StrDau += "@0@";
        //                    }
        //                }
        //            }
        //            else
        //            {
        //                for (int i = 0; i < Convert.ToInt32(r["SL"]); i++)
        //                {
        //                    StrDaBung += "@0@";
        //                    StrDaPhanLoai += "@4@";
        //                    StrDau += "@0@";
        //                }
        //            }

        //            idx++;
        //        }

        //        if (StrIDCaSau != "")
        //        {
        //            string fail = csCont.CaSauChet(StrIDCaSau, currNgay, UserId, LyDo, StrDaBung, StrDaPhanLoai, StrDau);
        //            if (fail != "")
        //            {
        //                int sidx = idx - 1;
        //                lblMessage.Text += "<br/>Dòng " + sidx.ToString() + ":" + fail;
        //            }
        //        }

        //        // dispose used objects
        //        dTable.Dispose();
        //        dataAdapter.Dispose();
        //        dbCommand.Dispose();
        //        ExcelCon.Close();
        //        ExcelCon.Dispose();

        //        lblMessage.Text += "<br/>Đã import xong!";
        //    }
        //    catch (Exception ex)
        //    {
        //        // dispose used objects
        //        dTable.Dispose();
        //        dataAdapter.Dispose();
        //        dbCommand.Dispose();
        //        ExcelCon.Close();
        //        ExcelCon.Dispose();
        //        Response.Write(ex.ToString());
        //    }
        //}

        private void importExcel(string dir, string file)
        {
            IFormatProvider culture     = new System.Globalization.CultureInfo("vi-VN", true);
            string          Excelstrcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + file + @";Extended Properties=""Excel 8.0;HDR=YES;""";

            string          SQLstr   = "SELECT * FROM [Sheet1$]";
            OleDbConnection ExcelCon = new OleDbConnection(Excelstrcon);

            ExcelCon.Open();

            OleDbCommand     dbCommand   = new OleDbCommand(SQLstr, ExcelCon);
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);

            DataTable dTable = new DataTable();

            try
            {
                dataAdapter.Fill(dTable);
                int      idx           = 1;
                DateTime NgayChet      = DateTime.MinValue;
                string   StrIDCaSau    = "";
                string   StrDaBung     = "";
                string   StrDaPhanLoai = "";
                string   StrDau        = "";
                string   StrPPM        = "";
                string   StrLDC        = "";
                string   StrKL         = "";
                string   LyDo          = "";
                int      hh            = 11;
                int      mm            = 10;
                int      ss            = 10;
                string   TenChuong     = "";
                int      So            = 0;
                string   BienBan       = "";
                foreach (DataRow r in dTable.Rows)
                {
                    //NgayChet = DateTime.Parse(r["Ngay"].ToString() + "/" + r["Thang"].ToString() + "/" + r["Nam"].ToString() + " 22:00:00", culture);
                    NgayChet = DateTime.Parse(r["Ngay"].ToString() + "/" + r["Thang"].ToString() + "/" + r["Nam"].ToString() + " " + hh.ToString() + ":" + mm.ToString() + ":" + ss.ToString(), culture);
                    ss++; if (ss > 59)
                    {
                        ss = 10; mm++; if (mm > 59)
                        {
                            mm = 10; hh++;
                        }
                    }
                    csCont.ParseChuong(r["Chuong"].ToString().Trim(), out TenChuong, out So);
                    string s     = csCont.CaSau_Chet_GetCaByLoaiCaByChuongAtDate(Convert.ToInt32(r["LoaiCa"]), TenChuong, So, Convert.ToInt32(r["SL"]), NgayChet, Convert.ToBoolean(r["Giong"]));
                    string sTemp = s.Replace("@", "");
                    if ((s.Length - sTemp.Length) / 2 != Convert.ToInt32(r["SL"]))
                    {
                        lblMessage.Text += "<br/>Dòng " + idx.ToString() + " không import do số lượng cá không đủ.";
                        idx++;
                        continue;
                    }
                    if (r["BienBan"] != DBNull.Value)
                    {
                        BienBan = r["BienBan"].ToString();
                    }
                    else
                    {
                        BienBan = "";
                    }
                    StrIDCaSau = s;
                    if (r["SanPham"] != DBNull.Value && r["SanPham"].ToString() != "")
                    {
                        if (r["SanPham"].ToString() == "-1")
                        {
                            for (int i = 0; i < Convert.ToInt32(r["SL"]); i++)
                            {
                                StrDaBung     += "@0@";
                                StrDaPhanLoai += "@4@";
                                StrDau        += "@1@";
                                StrPPM        += "@CL@";
                                StrLDC        += "@" + r["LyDoChet"].ToString() + "@";
                                StrKL         += "@" + r["KhoiLuong"].ToString().Replace(",", ".") + "@";
                            }
                        }
                        else
                        {
                            for (int i = 0; i < Convert.ToInt32(r["SL"]); i++)
                            {
                                StrDaBung     += "@" + r["SanPham"].ToString() + "@";
                                StrDaPhanLoai += "@" + r["PhanLoai"].ToString() + "@";
                                StrDau        += "@0@";
                                StrPPM        += "@" + r["PPM"].ToString() + "@";
                                StrLDC        += "@" + r["LyDoChet"].ToString() + "@";
                                StrKL         += "@" + r["KhoiLuong"].ToString().Replace(",", ".") + "@";
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < Convert.ToInt32(r["SL"]); i++)
                        {
                            StrDaBung     += "@0@";
                            StrDaPhanLoai += "@4@";
                            StrDau        += "@0@";
                            StrPPM        += "@CL@";
                            StrLDC        += "@" + r["LyDoChet"].ToString() + "@";
                            StrKL         += "@" + r["KhoiLuong"].ToString().Replace(",", ".") + "@";
                        }
                    }

                    if (StrIDCaSau != "")
                    {
                        string fail = csCont.CaSauChet(StrIDCaSau, NgayChet, UserId, StrDaBung, StrDaPhanLoai, StrDau, StrPPM, StrLDC, StrKL, BienBan, "-1", "");
                        if (fail != "")
                        {
                            lblMessage.Text += "<br/>Dòng " + idx.ToString() + ":" + fail;
                        }
                        StrIDCaSau    = "";
                        StrDaBung     = "";
                        StrDaPhanLoai = "";
                        StrDau        = "";
                        StrPPM        = "";
                        StrLDC        = "";
                        StrKL         = "";
                    }
                    else
                    {
                        StrDaBung     = "";
                        StrDaPhanLoai = "";
                        StrDau        = "";
                        StrPPM        = "";
                        StrLDC        = "";
                        StrKL         = "";
                    }

                    idx++;
                }

                // dispose used objects
                dTable.Dispose();
                dataAdapter.Dispose();
                dbCommand.Dispose();
                ExcelCon.Close();
                ExcelCon.Dispose();

                lblMessage.Text += "<br/>Đã import xong!";
            }
            catch (Exception ex)
            {
                // dispose used objects
                dTable.Dispose();
                dataAdapter.Dispose();
                dbCommand.Dispose();
                ExcelCon.Close();
                ExcelCon.Dispose();
                Response.Write(ex.ToString());
            }
        }
        protected void btnChuyen_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] == "chuyentrangthai")
            {
                if (Session["DSCaSauChuyenTrangThai"] != null && Session["DSCaSauChuyenTrangThai"].ToString() != "")
                {
                    IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);
                    if (DateTime.Parse(txtThoiDiemChuyen.Text, culture) < Config.NgayKhoaSo())
                    {
                        Page.ClientScript.RegisterStartupScript(typeof(string), "chuyenfail", "<script language=javascript>alert('Ngày chuyển không được trước ngày khóa sổ');</script>", false);
                        return;
                    }
                    if (ddlTrangThai.SelectedValue == "-3")
                    {
                        string res = csCont.ChuyenTrangThai_CaBan(Session["DSCaSauChuyenTrangThai"].ToString(), DateTime.Parse(txtThoiDiemChuyen.Text, culture), UserId);
                        if (res != "")
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "fail", "<script language=javascript>alert('Một số cá không chuyển trạng thái được có ID: " + res.Substring(1, res.Length - 2).Replace("@@", ", ") + "');</script>", false);
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "updated", "<script language=javascript>alert('Đã chuyển xong!');window.opener.finishEdit();self.close();</script>", false);
                        }
                    }
                    else if (ddlTrangThai.SelectedValue == "-1" || ddlTrangThai.SelectedValue == "-4")
                    {
                        string StrDaBung     = "";
                        string StrDaPhanLoai = "";
                        string StrDau        = "";
                        string StrPPM        = "";
                        string StrLDC        = "";
                        string StrKL         = "";
                        string LyDo          = "";
                        string StrVatTu      = "";
                        foreach (GridViewRow r in grvSPTH.Rows)
                        {
                            CheckBox     chkDau        = (CheckBox)(r.FindControl("chkDau"));
                            TextBox      txtDaBung     = (TextBox)(r.FindControl("txtDaBung"));
                            DropDownList ddlDaPhanLoai = (DropDownList)(r.FindControl("ddlDaPhanLoai"));
                            DropDownList ddlPPM        = (DropDownList)(r.FindControl("ddlPPM"));
                            DropDownList ddlLyDoChet   = (DropDownList)(r.FindControl("ddlLyDoChet"));
                            TextBox      txtKhoiLuong  = (TextBox)(r.FindControl("txtKhoiLuong"));
                            if (txtDaBung.Text == "")
                            {
                                txtDaBung.Text = "0";
                            }
                            StrDaBung     += "@" + txtDaBung.Text + "@";
                            StrDaPhanLoai += "@" + ddlDaPhanLoai.SelectedValue + "@";
                            StrDau        += "@" + Convert.ToInt32(chkDau.Checked).ToString() + "@";
                            StrPPM        += "@" + ddlPPM.SelectedValue + "@";
                            StrLDC        += "@" + ddlLyDoChet.SelectedValue + "@";
                            StrKL         += "@" + txtKhoiLuong.Text.Replace(",", ".") + "@";
                            PlaceHolder dsVatTu = (PlaceHolder)(r.FindControl("dsVatTu"));
                            string      sVatTu  = CreateSVatTu(dsVatTu, false);
                            StrVatTu += "*" + sVatTu + "*";
                        }
                        string res = csCont.CaSauChet(Session["DSCaSauChuyenTrangThai"].ToString(), DateTime.Parse(txtThoiDiemChuyen.Text, culture), UserId, StrDaBung, StrDaPhanLoai, StrDau, StrPPM, StrLDC, StrKL, txtBienBan.Text.Trim(), ddlTrangThai.SelectedValue, StrVatTu);

                        if (res != "")
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "fail", "<script language=javascript>alert('Một số cá không chuyển trạng thái được có ID: " + res.Substring(1, res.Length - 2).Replace("@@", ", ") + "');</script>", false);
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "updated", "<script language=javascript>alert('Đã chuyển xong!');window.opener.finishEdit();self.close();</script>", false);
                        }
                    }
                    else
                    {
                        string res = csCont.ChuyenTrangThai_CaSong(Session["DSCaSauChuyenTrangThai"].ToString(), int.Parse(ddlTrangThai.SelectedValue), DateTime.Parse(txtThoiDiemChuyen.Text, culture), UserId);
                        if (res != "")
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "fail", "<script language=javascript>alert('Một số cá không chuyển trạng thái được có ID: " + res.Substring(1, res.Length - 2).Replace("@@", ", ") + "');</script>", false);
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "updated", "<script language=javascript>alert('Đã chuyển xong!');window.opener.finishEdit();self.close();</script>", false);
                        }
                    }
                }
            }
            else if (Request.QueryString["type"] == "editchuyentrangthai")
            {
                if (hdIDCaSau.Value != "0")
                {
                    IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);
                    if (DateTime.Parse(txtThoiDiemChuyen.Text, culture) < Config.NgayKhoaSo())
                    {
                        Page.ClientScript.RegisterStartupScript(typeof(string), "chuyenfail", "<script language=javascript>alert('Ngày chuyển không được trước ngày khóa sổ');</script>", false);
                        return;
                    }
                    if (ddlTrangThai.SelectedValue == "-3")
                    {
                        int res = csCont.EditChuyenTrangThai_CaBan(int.Parse(hdIDBienDong.Value), int.Parse(hdIDCaSau.Value), DateTime.Parse(txtThoiDiemChuyen.Text, culture), UserId);
                        if (res == 0)
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "fail", "<script language=javascript>alert('Không chuyển trạng thái được!');</script>", false);
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "updated", "<script language=javascript>alert('Đã chuyển xong!');window.opener.finishEdit();self.close();</script>", false);
                        }
                    }
                    else if (ddlTrangThai.SelectedValue == "-1" || ddlTrangThai.SelectedValue == "-4")
                    {
                        int     RealDaBung     = 0;
                        int     RealDaPhanLoai = 0;
                        int     RealDau        = 0;
                        string  RealPPM        = "";
                        int     RealLDC        = 0;
                        decimal RealKL         = 0;
                        string  LyDo           = "";
                        string  sVatTu         = "";
                        foreach (GridViewRow r in grvSPTH.Rows)
                        {
                            CheckBox     chkDau        = (CheckBox)(r.FindControl("chkDau"));
                            TextBox      txtDaBung     = (TextBox)(r.FindControl("txtDaBung"));
                            DropDownList ddlDaPhanLoai = (DropDownList)(r.FindControl("ddlDaPhanLoai"));
                            DropDownList ddlPPM        = (DropDownList)(r.FindControl("ddlPPM"));
                            DropDownList ddlLyDoChet   = (DropDownList)(r.FindControl("ddlLyDoChet"));
                            TextBox      txtKhoiLuong  = (TextBox)(r.FindControl("txtKhoiLuong"));
                            if (txtDaBung.Text == "")
                            {
                                txtDaBung.Text = "0";
                            }
                            RealDaBung     = int.Parse(txtDaBung.Text);
                            RealDaPhanLoai = int.Parse(ddlDaPhanLoai.SelectedValue);
                            RealDau        = Convert.ToInt32(chkDau.Checked);
                            RealPPM        = ddlPPM.SelectedValue;
                            RealLDC        = int.Parse(ddlLyDoChet.SelectedValue);
                            RealKL         = decimal.Parse(txtKhoiLuong.Text.Trim());
                            PlaceHolder dsVatTu = (PlaceHolder)(r.FindControl("dsVatTu"));
                            sVatTu = CreateSVatTu(dsVatTu, false);
                        }
                        int res = csCont.EditCaSauChet(int.Parse(hdIDBienDong.Value), int.Parse(hdIDCaSau.Value), DateTime.Parse(txtThoiDiemChuyen.Text, culture), RealDaBung, RealDaPhanLoai, RealDau, RealPPM, RealLDC, RealKL, UserId, txtBienBan.Text.Trim(), ddlTrangThai.SelectedValue, sVatTu);
                        if (res == 0)
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "fail", "<script language=javascript>alert('Không chuyển trạng thái được!');</script>", false);
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "updated", "<script language=javascript>alert('Đã chuyển xong!');window.opener.finishEdit();self.close();</script>", false);
                        }
                    }
                    else
                    {
                        int res = csCont.EditChuyenTrangThai_CaSong(int.Parse(hdIDBienDong.Value), int.Parse(hdIDCaSau.Value), int.Parse(ddlTrangThai.SelectedValue), DateTime.Parse(txtThoiDiemChuyen.Text, culture), UserId);
                        if (res == 0)
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "fail", "<script language=javascript>alert('Không chuyển trạng thái được!');</script>", false);
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(typeof(string), "updated", "<script language=javascript>alert('Đã chuyển xong!');window.opener.finishEdit();self.close();</script>", false);
                        }
                    }
                }
            }
            else if (Request.QueryString["type"] == "editchuyentrangthaigroup")
            {
                if (hdIDBienDongGroup.Value != "0")
                {
                    IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);
                    if (DateTime.Parse(txtThoiDiemChuyen.Text, culture) < Config.NgayKhoaSo())
                    {
                        Page.ClientScript.RegisterStartupScript(typeof(string), "chuyenfail", "<script language=javascript>alert('Ngày chuyển không được trước ngày khóa sổ');</script>", false);
                        return;
                    }
                    string res = csCont.UpdateBienDongGroup(int.Parse(hdIDBienDongGroup.Value), DateTime.Parse(txtThoiDiemChuyen.Text, culture), ddlTrangThai.SelectedValue, UserId);
                    if (res != "")
                    {
                        Page.ClientScript.RegisterStartupScript(typeof(string), "fail", "<script language=javascript>alert('Một số cá không chuyển được:" + res.Substring(1, res.Length - 2).Replace("@@", ", ") + "');</script>", false);
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(typeof(string), "updated", "<script language=javascript>alert('Đã chuyển xong!');window.opener.finishEdit();self.close();</script>", false);
                    }
                }
            }
        }