Example #1
0
        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();
        }