private void btnOk_Click(object sender, EventArgs e) { if (!Utils.IsValidGuid(cboxLaboratory.SelectedValue)) { MessageBox.Show("You must select a laboratory first"); return; } if (gridOrders.SelectedRows.Count < 1) { MessageBox.Show("You must select an order first"); return; } if (!mAssignment.ApprovedLaboratory) { MessageBox.Show("Can not add samples to orders that is not approved by laboratory"); return; } if (!mAssignment.ApprovedCustomer) { MessageBox.Show("Can not add samples to orders that is not approved by customer"); return; } if (mAssignment.WorkflowStatusId != WorkflowStatus.Construction) { MessageBox.Show("Can not add samples to orders that is not under construction"); return; } if (treeOrderLines.SelectedNode == null) { MessageBox.Show("You must select a order line first"); return; } TreeNode tnode = treeOrderLines.SelectedNode; if (tnode.Level != 0) { MessageBox.Show("You must select a top level order line for this sample"); return; } SqlConnection conn = null; SqlTransaction trans = null; try { Guid astId = Guid.Parse(tnode.Name); AssignmentSampleType ast = mAssignment.SampleTypes.Find(x => x.Id == astId); if (ast == null) { throw new Exception("No assignment sample type found with id " + ast.Id.ToString()); } conn = DB.OpenConnection(); trans = conn.BeginTransaction(); AstInfo astInfo = tnode.Tag as AstInfo; int nSamplesFree = DB.GetAvailableSamplesOnAssignmentSampleType(conn, trans, astId); if (nSamplesFree <= 0) { MessageBox.Show("This order line is full"); return; } string[] items = tnode.Text.Split(new[] { " -> " }, StringSplitOptions.RemoveEmptyEntries); if (items.Length < 2) { throw new Exception("Invalid sample type found in assignment: " + tnode.Text); } string st1 = items[1]; string st2 = mSample.GetSampleTypePath(conn, trans); if (!st2.StartsWith(st1)) { MessageBox.Show("Wrong sample type for sample " + mSample.Number); return; } if (astInfo.SampleComponentId != Guid.Empty && astInfo.SampleComponentId != mSample.SampleComponentId) { MessageBox.Show("Wrong sample component for sample " + mSample.Number); return; } Guid labId = Utils.MakeGuid(cboxLaboratory.SelectedValue); SelectedOrderId = mAssignment.Id; SelectedOrderName = mAssignment.Name; foreach (AssignmentPreparationMethod apm in ast.PreparationMethods) { if (apm.PreparationLaboratoryId != mAssignment.LaboratoryId) { // Check that external preparations exists TreeNode[] tn = treeOrderLines.Nodes.Find(apm.Id.ToString(), true); if (tn.Length < 1) { throw new Exception("No assignment preparation methods found with id " + apm.Id.ToString()); } if (tn[0].Tag == null) { MessageBox.Show("You must specify external preparations for this order line"); return; } } } if (mSample.HasOrder(conn, trans, SelectedOrderId)) { MessageBox.Show("Sample " + mSample.Number + " is already added to order " + mAssignment.Name); return; } int nextPrepNum = DB.GetNextPreparationNumber(conn, trans, mSample.Id); foreach (AssignmentPreparationMethod apm in ast.PreparationMethods) { List <Guid> exIds = null; if (apm.PreparationLaboratoryId != mAssignment.LaboratoryId) { // External preparations TreeNode[] tn = treeOrderLines.Nodes.Find(apm.Id.ToString(), true); if (tn.Length < 1) { throw new Exception("No assignment preparation method node found in tree with id " + apm.Id.ToString()); } exIds = tn[0].Tag as List <Guid>; } for (int i = 0; i < apm.PreparationMethodCount; i++) { Preparation p = GetNextPreparation(apm, labId, ref exIds, ref nextPrepNum); int nextAnalNum = DB.GetNextAnalysisNumber(conn, trans, p.Id); foreach (AssignmentAnalysisMethod aam in apm.AnalysisMethods) { for (int j = 0; j < aam.AnalysisMethodCount; j++) { Analysis a = new Analysis(); a.PreparationId = p.Id; a.AnalysisMethodId = aam.AnalysisMethodId; a.InstanceStatusId = InstanceStatus.Active; a.WorkflowStatusId = WorkflowStatus.Construction; a.Number = nextAnalNum++; a.AssignmentId = SelectedOrderId; a.LaboratoryId = labId; p.Analyses.Add(a); } } } } mSample.ConnectToOrderLine(conn, trans, ast.Id); mSample.StoreToDB(conn, trans); string json = JsonConvert.SerializeObject(mSample); DB.AddAuditMessage(conn, trans, "sample", mSample.Id, AuditOperationType.Update, json, ""); trans.Commit(); DialogResult = DialogResult.OK; } catch (Exception ex) { trans?.Rollback(); Common.Log.Error(ex); MessageBox.Show(ex.Message); DialogResult = DialogResult.Abort; } finally { conn?.Close(); } Close(); }
private void btnOk_Click(object sender, EventArgs e) { if (!Utils.IsValidGuid(cboxPrepMethods.SelectedValue)) { MessageBox.Show("Preparation method is mandatory"); return; } if (String.IsNullOrEmpty(tbCount.Text.Trim())) { MessageBox.Show("Count is mandatory"); return; } int count = Convert.ToInt32(tbCount.Text.Trim()); if (count == 0) { MessageBox.Show("Count can not be zero"); return; } SqlConnection connection = null; SqlTransaction transaction = null; try { connection = DB.OpenConnection(); transaction = connection.BeginTransaction(); int nextPrepNumber = DB.GetNextPreparationNumber(connection, transaction, mSample.Id); SqlCommand cmd = new SqlCommand("csp_insert_preparation", connection, transaction); cmd.CommandType = CommandType.StoredProcedure; while (count > 0) { Guid newPrepId = Guid.NewGuid(); cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@id", newPrepId); cmd.Parameters.AddWithValue("@sample_id", mSample.Id, Guid.Empty); cmd.Parameters.AddWithValue("@number", nextPrepNumber++); cmd.Parameters.AddWithValue("@assignment_id", DBNull.Value); cmd.Parameters.AddWithValue("@laboratory_id", Common.LabId, Guid.Empty); cmd.Parameters.AddWithValue("@preparation_geometry_id", DBNull.Value); cmd.Parameters.AddWithValue("@preparation_method_id", cboxPrepMethods.SelectedValue, Guid.Empty); cmd.Parameters.AddWithValue("@workflow_status_id", 1); cmd.Parameters.AddWithValue("@amount", DBNull.Value); cmd.Parameters.AddWithValue("@prep_unit_id", 0); cmd.Parameters.AddWithValue("@quantity", DBNull.Value); cmd.Parameters.AddWithValue("@quantity_unit_id", 0); cmd.Parameters.AddWithValue("@fill_height_mm", DBNull.Value); cmd.Parameters.AddWithValue("@instance_status_id", InstanceStatus.Active); cmd.Parameters.AddWithValue("@comment", DBNull.Value); cmd.Parameters.AddWithValue("@create_date", DateTime.Now); cmd.Parameters.AddWithValue("@create_id", Common.UserId, Guid.Empty); cmd.Parameters.AddWithValue("@update_date", DateTime.Now); cmd.Parameters.AddWithValue("@update_id", Common.UserId, Guid.Empty); cmd.Parameters.AddWithValue("@volume_l", DBNull.Value); cmd.Parameters.AddWithValue("@preprocessing_volume_l", DBNull.Value); cmd.ExecuteNonQuery(); count--; } mSample.LoadFromDB(connection, transaction, mSample.Id); string json = JsonConvert.SerializeObject(mSample); DB.AddAuditMessage(connection, transaction, "sample", mSample.Id, AuditOperationType.Update, json, ""); transaction.Commit(); DialogResult = DialogResult.OK; } catch (Exception ex) { transaction?.Rollback(); Common.Log.Error(ex); DialogResult = DialogResult.Abort; MessageBox.Show(ex.Message); } finally { connection?.Close(); } Close(); }