private void tsmUpdateBarcode_Click(object sender, EventArgs e)
 {
     try
     {
         if (TResultDetail.CreateQuery().WHERE(TResultDetail.Columns.TestId, grdTestInfo.GetValue("Test_ID")).GetRecordCount() > 0)
         {
             Utility.ShowMsg("Đăng ký đã có kết quả. Không được sửa barcode !");
             return;
         }
         frmInput_Update_Barcode oForm = new frmInput_Update_Barcode();
         if (SysPara.AutoGenerateBarcode == 2)
         {
             oForm.vTestType_ID = Utility.Int32Dbnull(grdTestInfo.GetValue("TestType_ID"));
         }
         oForm.txtBarcode.Text       = Utility.sDbnull(grdTestInfo.GetValue("Barcode"));
         oForm.txtTestType_Name.Text = Utility.sDbnull(grdTestInfo.GetValue("TestType_Name"));
         oForm.vPatient_ID           = patientId;
         oForm.ShowDialog();
         if (string.IsNullOrEmpty(oForm.txtBarcode.Text) | oForm.txtBarcode.Text == Utility.sDbnull(grdTestInfo.GetValue("Barcode")))
         {
             return;
         }
         new Update(TTestInfo.Schema.Name).Set(TTestInfo.Columns.Barcode).EqualTo(oForm.txtBarcode.Text).
         Where(TTestInfo.Columns.TestId).IsEqualTo(Utility.Int32Dbnull(grdTestInfo.GetValue("Test_ID"))).
         Execute();
         new Update(TRegList.Schema.Name).Set(TTestInfo.Columns.Barcode).EqualTo(oForm.txtBarcode.Text).
         Where(TRegList.Columns.TestId).IsEqualTo(Utility.Int32Dbnull(grdTestInfo.GetValue("Test_ID"))).
         Execute();
         grdTestInfo.CurrentRow.Cells["Barcode"].Value = oForm.txtBarcode.Text;
     }
     catch (Exception ex)
     {
         Utility.ShowMsg(ex.Message);
     }
 }
 private void tsmUpdateBarcode_Click(object sender, EventArgs e)
 {
     try
     {
         if (TResultDetail.CreateQuery().WHERE(TResultDetail.Columns.TestId,grdTestInfo.GetValue("Test_ID")).GetRecordCount() > 0)
         {
             Utility.ShowMsg("Đăng ký đã có kết quả. Không được sửa barcode !");
             return;
         }
         frmInput_Update_Barcode oForm = new frmInput_Update_Barcode();
         if (SysPara.AutoGenerateBarcode == 2)
             oForm.vTestType_ID = Utility.Int32Dbnull(grdTestInfo.GetValue("TestType_ID"));
         oForm.txtBarcode.Text = Utility.sDbnull(grdTestInfo.GetValue("Barcode"));
         oForm.txtTestType_Name.Text = Utility.sDbnull(grdTestInfo.GetValue("TestType_Name"));
         oForm.vPatient_ID = patientId;
         oForm.ShowDialog();
         if (string.IsNullOrEmpty(oForm.txtBarcode.Text) | oForm.txtBarcode.Text == Utility.sDbnull(grdTestInfo.GetValue("Barcode")))
         {
             return;
         }
         new Update(TTestInfo.Schema.Name).Set(TTestInfo.Columns.Barcode).EqualTo(oForm.txtBarcode.Text).
             Where(TTestInfo.Columns.TestId).IsEqualTo(Utility.Int32Dbnull(grdTestInfo.GetValue("Test_ID"))).
             Execute();
         new Update(TRegList.Schema.Name).Set(TTestInfo.Columns.Barcode).EqualTo(oForm.txtBarcode.Text).
             Where(TRegList.Columns.TestId).IsEqualTo(Utility.Int32Dbnull(grdTestInfo.GetValue("Test_ID"))).
             Execute();
         grdTestInfo.CurrentRow.Cells["Barcode"].Value = oForm.txtBarcode.Text;
     }
     catch (Exception ex)
     {
         Utility.ShowMsg(ex.Message);
     }
 }
        /// <summary>
        /// Hàm xử lý click vào nút đăng ký xn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnTestTypeClick(object sender, EventArgs e)
        {
            try
            {
                int count;
                var btn = (UIButton) sender;
                int vTestTypeId = Utility.Int32Dbnull(btn.Tag);

                count = TTestInfo.CreateQuery().WHERE(TTestInfo.Columns.PatientId, patientId).
                        WHERE(TTestInfo.Columns.TestTypeId, vTestTypeId).GetRecordCount();
                if (count > 0)
                {
                    //Utility.ShowMsg("Loại Xét Nghiệm đã được đăng ký");
                    //Todo: focus vào test đã đăng ký
                    for (int i = 0; i < grdTestInfo.RowCount; i++)
                    {
                        if (Utility.sDbnull(grdTestInfo.GetRow(i).Cells["TestType_ID"].Value) == (string)btn.Tag)
                        {
                            grdTestInfo.MoveTo(i);
                            break;
                        }
                    }
                    return;
                }

                string vBarcode = "";
                DataRow drTestType = Utility.GetDataRow(dtTestTypeList, TTestTypeList.Columns.TestTypeId, btn.Tag);

                if (SysPara.AutoGenerateBarcode == 2 | (SysPara.AutoGenerateBarcode == 0 & dtTestInfo.Rows.Count == 0))
                {
                    frmInput_Update_Barcode oForm = new frmInput_Update_Barcode();
                    if (dtTestInfo.Rows.Count > 0)
                    {
                        oForm.txtBarcode.Text = Utility.sDbnull(grdTestInfo.GetValue("Barcode"));
                        oForm.txtBarcode.SelectAll();
                    }
                    oForm.vTestType_ID = vTestTypeId;
                    oForm.txtTestType_Name.Text = Utility.sDbnull(drTestType["TestType_Name"]);
                    oForm.ShowDialog();
                    if (string.IsNullOrEmpty(oForm.txtBarcode.Text)) return;
                    vBarcode = oForm.txtBarcode.Text;
                }
                else if (dtTestInfo.Rows.Count > 0)
                {
                    vBarcode = Utility.sDbnull(grdTestInfo.GetValue("Barcode")).Trim();
                    //count = new Select().From(TTestInfo.Schema).Where(TTestInfo.Columns.TestTypeId).IsEqualTo(vTestTypeId).
                    //                And(TTestInfo.Columns.Barcode).IsEqualTo(vBarcode).GetRecordCount();
                    //if (count > 0)
                    //{
                    //    Utility.ShowMsg("Barcode tồn tại. Mời nhập lại.");
                    //    return;
                    //}
                }
                else
                {
                    vBarcode = BarcodeInfo.GetBarcodeForPatient(patientId, vTestTypeId, drTestType[TTestTypeList.Columns.IntOrder].ToString());
                    if (vBarcode == "-1")
                    {
                        Utility.ShowMsg("Không tìm được Barcode");
                        return;
                    }
                }
                string canlamsangid;
                LPatientInfo lPatientInfo = new Select().From(LPatientInfo.Schema.Name).Where(LPatientInfo.Columns.PatientId).IsEqualTo(patientId).ExecuteSingle<LPatientInfo>();
                canlamsangid=lPatientInfo.CanLamSangId;
                var obj = new TTestInfo();
                obj.TestTypeId = vTestTypeId;
                obj.Barcode = vBarcode;
                obj.PatientId = patientId;
                obj.TestDate = DateTime.Now;
                obj.RequireDate = DateTime.Now;
                obj.AssignId = Utility.Int32Dbnull(cboAssignDoctor.SelectedValue);
                obj.CanLamSangId = "OO" + DateTime.Now.ToString("yyMMdd")+"."+canlamsangid;
                obj.IsNew = true;
                obj.Save();

                DataRow dr = dtTestInfo.NewRow();
                dr[TTestInfo.Columns.TestId] = TTestInfo.CreateQuery().WHERE(TTestInfo.Columns.PatientId, patientId).
                    WHERE(TTestInfo.Columns.TestTypeId, vTestTypeId).GetMax(TTestInfo.Columns.TestId);
                dr[TTestInfo.Columns.TestDate] = obj.TestDate;
                dr[TTestInfo.Columns.Barcode] = obj.Barcode;
                dr[TTestInfo.Columns.PatientId] = obj.PatientId;
                dr[TTestInfo.Columns.TestTypeId] = obj.TestTypeId;
                dr[TTestTypeList.Columns.TestTypeName] = drTestType[TTestTypeList.Columns.TestTypeName].ToString();
                dr[TTestInfo.Columns.AssignId] = obj.AssignId;
                dr["AssignDoctor_Name"] = Utility.Int32Dbnull(cboAssignDoctor.SelectedValue) == -1
                                              ? ""
                                              : cboAssignDoctor.Text;

                dtTestInfo.Rows.InsertAt(dr, 0);
                grdTestInfo.SelectionChanged -= grdTestInfo_SelectionChanged;
                dtTestInfo.AcceptChanges();
                grdTestInfo.SelectionChanged += grdTestInfo_SelectionChanged;
                grdTestInfo.MoveFirst();

                // Todo: xử lý sau khi nhấn nút đăng ký
                //Nếu ô đăng ký test có dl thì chuyển sang
                //if (flpTestGroup.Controls.Count > 0) flpTestGroup.Focus();
                //else flpStandardTest.Focus();

            }
            catch (Exception ex)
            {
                Utility.ShowMsg(ex.Message);
            }
            finally {FocusButtonDetail();}
        }
        /// <summary>
        /// Hàm xử lý click vào nút đăng ký xn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnTestTypeClick(object sender, EventArgs e)
        {
            try
            {
                int count;
                var btn         = (UIButton)sender;
                int vTestTypeId = Utility.Int32Dbnull(btn.Tag);

                count = TTestInfo.CreateQuery().WHERE(TTestInfo.Columns.PatientId, patientId).
                        WHERE(TTestInfo.Columns.TestTypeId, vTestTypeId).GetRecordCount();
                if (count > 0)
                {
                    //Utility.ShowMsg("Loại Xét Nghiệm đã được đăng ký");
                    //Todo: focus vào test đã đăng ký
                    for (int i = 0; i < grdTestInfo.RowCount; i++)
                    {
                        if (Utility.sDbnull(grdTestInfo.GetRow(i).Cells["TestType_ID"].Value) == (string)btn.Tag)
                        {
                            grdTestInfo.MoveTo(i);
                            break;
                        }
                    }
                    return;
                }

                string  vBarcode   = "";
                DataRow drTestType = Utility.GetDataRow(dtTestTypeList, TTestTypeList.Columns.TestTypeId, btn.Tag);

                if (SysPara.AutoGenerateBarcode == 2 | (SysPara.AutoGenerateBarcode == 0 & dtTestInfo.Rows.Count == 0))
                {
                    frmInput_Update_Barcode oForm = new frmInput_Update_Barcode();
                    if (dtTestInfo.Rows.Count > 0)
                    {
                        oForm.txtBarcode.Text = Utility.sDbnull(grdTestInfo.GetValue("Barcode"));
                        oForm.txtBarcode.SelectAll();
                    }
                    oForm.vTestType_ID          = vTestTypeId;
                    oForm.txtTestType_Name.Text = Utility.sDbnull(drTestType["TestType_Name"]);
                    oForm.ShowDialog();
                    if (string.IsNullOrEmpty(oForm.txtBarcode.Text))
                    {
                        return;
                    }
                    vBarcode = oForm.txtBarcode.Text;
                }
                else if (dtTestInfo.Rows.Count > 0)
                {
                    vBarcode = Utility.sDbnull(grdTestInfo.GetValue("Barcode")).Trim();
                    //count = new Select().From(TTestInfo.Schema).Where(TTestInfo.Columns.TestTypeId).IsEqualTo(vTestTypeId).
                    //                And(TTestInfo.Columns.Barcode).IsEqualTo(vBarcode).GetRecordCount();
                    //if (count > 0)
                    //{
                    //    Utility.ShowMsg("Barcode tồn tại. Mời nhập lại.");
                    //    return;
                    //}
                }
                else
                {
                    vBarcode = BarcodeInfo.GetBarcodeForPatient(patientId, vTestTypeId, drTestType[TTestTypeList.Columns.IntOrder].ToString());
                    if (vBarcode == "-1")
                    {
                        Utility.ShowMsg("Không tìm được Barcode");
                        return;
                    }
                }
                string       canlamsangid;
                LPatientInfo lPatientInfo = new Select().From(LPatientInfo.Schema.Name).Where(LPatientInfo.Columns.PatientId).IsEqualTo(patientId).ExecuteSingle <LPatientInfo>();
                canlamsangid = lPatientInfo.CanLamSangId;
                var obj = new TTestInfo();
                obj.TestTypeId   = vTestTypeId;
                obj.Barcode      = vBarcode;
                obj.PatientId    = patientId;
                obj.TestDate     = DateTime.Now;
                obj.RequireDate  = DateTime.Now;
                obj.AssignId     = Utility.Int32Dbnull(cboAssignDoctor.SelectedValue);
                obj.CanLamSangId = "OO" + DateTime.Now.ToString("yyMMdd") + "." + canlamsangid;
                obj.IsNew        = true;
                obj.Save();

                DataRow dr = dtTestInfo.NewRow();
                dr[TTestInfo.Columns.TestId] = TTestInfo.CreateQuery().WHERE(TTestInfo.Columns.PatientId, patientId).
                                               WHERE(TTestInfo.Columns.TestTypeId, vTestTypeId).GetMax(TTestInfo.Columns.TestId);
                dr[TTestInfo.Columns.TestDate]         = obj.TestDate;
                dr[TTestInfo.Columns.Barcode]          = obj.Barcode;
                dr[TTestInfo.Columns.PatientId]        = obj.PatientId;
                dr[TTestInfo.Columns.TestTypeId]       = obj.TestTypeId;
                dr[TTestTypeList.Columns.TestTypeName] = drTestType[TTestTypeList.Columns.TestTypeName].ToString();
                dr[TTestInfo.Columns.AssignId]         = obj.AssignId;
                dr["AssignDoctor_Name"] = Utility.Int32Dbnull(cboAssignDoctor.SelectedValue) == -1
                                              ? ""
                                              : cboAssignDoctor.Text;


                dtTestInfo.Rows.InsertAt(dr, 0);
                grdTestInfo.SelectionChanged -= grdTestInfo_SelectionChanged;
                dtTestInfo.AcceptChanges();
                grdTestInfo.SelectionChanged += grdTestInfo_SelectionChanged;
                grdTestInfo.MoveFirst();

                // Todo: xử lý sau khi nhấn nút đăng ký
                //Nếu ô đăng ký test có dl thì chuyển sang
                //if (flpTestGroup.Controls.Count > 0) flpTestGroup.Focus();
                //else flpStandardTest.Focus();
            }
            catch (Exception ex)
            {
                Utility.ShowMsg(ex.Message);
            }
            finally { FocusButtonDetail(); }
        }