Esempio n. 1
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!Utils.IsValidGuid(cboxSampleType.SelectedValue))
            {
                MessageBox.Show("Sample type is mandatory");
                return;
            }

            if (!Utils.IsValidGuid(cboxProjectMain.SelectedValue))
            {
                MessageBox.Show("Main project is mandatory");
                return;
            }

            if (!Utils.IsValidGuid(cboxProjectSub.SelectedValue))
            {
                MessageBox.Show("Sub project is mandatory");
                return;
            }

            if (!Utils.IsValidGuid(cboxLaboratory.SelectedValue))
            {
                MessageBox.Show("Laboratory is mandatory");
                return;
            }

            SqlConnection  conn  = null;
            SqlTransaction trans = null;

            try
            {
                conn  = DB.OpenConnection();
                trans = conn.BeginTransaction();

                DateTime currDate = DateTime.Now;

                Sample sample = new Sample();
                sample.Number       = DB.GetNextSampleCount(conn, trans);
                sample.LaboratoryId = Utils.MakeGuid(cboxLaboratory.SelectedValue);
                sample.SampleTypeId = Utils.MakeGuid(cboxSampleType.SelectedValue);
                if (Utils.IsValidGuid(cboxSampleComponent.SelectedValue))
                {
                    sample.SampleComponentId = Utils.MakeGuid(cboxSampleComponent.SelectedValue);
                }
                sample.ProjectSubId     = Utils.MakeGuid(cboxProjectSub.SelectedValue);
                sample.InstanceStatusId = InstanceStatus.Active;
                sample.CreateDate       = currDate;
                sample.CreateId         = Common.UserId;
                sample.UpdateDate       = currDate;
                sample.UpdateId         = Common.UserId;

                sample.StoreToDB(conn, trans);

                string json = JsonConvert.SerializeObject(sample);
                DB.AddAuditMessage(conn, trans, "sample", sample.Id, AuditOperationType.Insert, json, "");

                trans.Commit();

                SampleId     = sample.Id;
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                trans?.Rollback();
                Common.Log.Error(ex);
                MessageBox.Show(ex.Message);
                DialogResult = DialogResult.Abort;
            }
            finally
            {
                conn?.Close();
            }

            Close();
        }
Esempio n. 2
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Utils.IsValidGuid(cboxComponents.SelectedValue))
            {
                MessageBox.Show("Sample component is required");
                return;
            }

            if (String.IsNullOrEmpty(tbCount.Text.Trim()))
            {
                MessageBox.Show("Count is required");
                return;
            }

            Guid compId = Utils.MakeGuid(cboxComponents.SelectedValue);
            int  count  = 0;

            try
            {
                count = Convert.ToInt32(tbCount.Text.Trim());
            }
            catch
            {
                MessageBox.Show("Invalid number format");
                return;
            }

            if (count < 1 || count > 10000)
            {
                MessageBox.Show("Split count must be between 1 and 10000");
                return;
            }

            SqlConnection  conn  = null;
            SqlTransaction trans = null;

            try
            {
                conn  = DB.OpenConnection();
                trans = conn.BeginTransaction();

                DateTime currDate = DateTime.Now;

                for (int i = 0; i < count; i++)
                {
                    mNewSample.Id                = Guid.NewGuid();
                    mNewSample.Number            = DB.GetNextSampleCount(conn, trans);
                    mNewSample.SampleComponentId = compId;
                    mNewSample.TransformFromId   = mOldSampleId;
                    mNewSample.CreateDate        = currDate;
                    mNewSample.CreateId          = Common.UserId;
                    mNewSample.UpdateDate        = currDate;
                    mNewSample.UpdateId          = Common.UserId;

                    mNewSample.StoreToDB(conn, trans);

                    string json = JsonConvert.SerializeObject(mNewSample);
                    DB.AddAuditMessage(conn, trans, "sample", mNewSample.Id, AuditOperationType.Insert, json, "");
                }

                trans.Commit();
            }
            catch (Exception ex)
            {
                trans?.Rollback();
                Common.Log.Error(ex);
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn?.Close();
            }

            DialogResult = DialogResult.OK;
            Close();
        }
Esempio n. 3
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Utils.IsValidGuid(cboxSampleType.SelectedValue))
            {
                MessageBox.Show("You must select a sample type");
                return;
            }

            Dictionary <string, object> map = new Dictionary <string, object>();

            SqlConnection  conn  = null;
            SqlTransaction trans = null;

            try
            {
                conn  = DB.OpenConnection();
                trans = conn.BeginTransaction();

                DateTime currDate = DateTime.Now;

                Guid   oldSampleId = Utils.MakeGuid(gridSamples.Rows[0].Cells["id"].Value);
                Sample newSample   = new Sample();
                newSample.LoadFromDB(conn, trans, oldSampleId);
                newSample.Id = Guid.NewGuid();
                foreach (Preparation p in newSample.Preparations)
                {
                    p.Analyses.Clear();
                }
                newSample.Preparations.Clear();
                newSample.Parameters.Clear();
                newSample.Number       = DB.GetNextSampleCount(conn, trans);
                newSample.ExternalId   = String.IsNullOrEmpty(tbExternalId.Text.Trim()) ? String.Empty : tbExternalId.Text.Trim();
                newSample.SampleTypeId = Utils.MakeGuid(cboxSampleType.SelectedValue);
                if (Utils.IsValidGuid(cboxSampleComponent.SelectedValue))
                {
                    newSample.SampleComponentId = Utils.MakeGuid(cboxSampleComponent.SelectedValue);
                }
                else
                {
                    newSample.SampleComponentId = Guid.Empty;
                }
                newSample.TransformFromId     = Guid.Empty;
                newSample.MunicipalityId      = Guid.Empty;
                newSample.LocationType        = String.Empty;
                newSample.Location            = String.Empty;
                newSample.StationId           = Guid.Empty;
                newSample.SampleStorageId     = Guid.Empty;
                newSample.SamplerId           = Guid.Empty;
                newSample.SamplingMethodId    = Guid.Empty;
                newSample.Latitude            = newSample.Longitude = newSample.Altitude = null;
                newSample.InstanceStatusId    = InstanceStatus.Active;
                newSample.SamplingDateFrom    = new DateTime(dtSamplingDate.Value.Year, dtSamplingDate.Value.Month, dtSamplingDate.Value.Day, dtSamplingTime.Value.Hour, dtSamplingTime.Value.Minute, dtSamplingTime.Value.Second);
                newSample.SamplingDateTo      = null;
                newSample.ReferenceDate       = new DateTime(dtReferenceDate.Value.Year, dtReferenceDate.Value.Month, dtReferenceDate.Value.Day, dtReferenceTime.Value.Hour, dtReferenceTime.Value.Minute, dtReferenceTime.Value.Second);
                newSample.WetWeight_g         = null;
                newSample.DryWeight_g         = null;
                newSample.LodWeightStart      = null;
                newSample.LodWeightEnd        = null;
                newSample.LodTemperature      = null;
                newSample.LodWaterPercent     = null;
                newSample.LodFactor           = null;
                newSample.LodWeightStartAsh   = null;
                newSample.LodWeightEndAsh     = null;
                newSample.LodTemperatureAsh   = null;
                newSample.LodWaterPercentAsh  = null;
                newSample.LodFactorAsh        = null;
                newSample.LodWeightStartAsh2  = null;
                newSample.LodWeightEndAsh2    = null;
                newSample.LodTemperatureAsh2  = null;
                newSample.LodWaterPercentAsh2 = null;
                newSample.LodFactorAsh2       = null;
                newSample.Comment             = tbComment.Text.Trim();
                newSample.LockedId            = Guid.Empty;
                newSample.CreateDate          = currDate;
                newSample.CreateId            = Common.UserId;
                newSample.UpdateDate          = currDate;
                newSample.UpdateId            = Common.UserId;

                newSample.StoreToDB(conn, trans);

                string json = JsonConvert.SerializeObject(newSample);
                DB.AddAuditMessage(conn, trans, "sample", newSample.Id, AuditOperationType.Insert, json, "");

                SqlCommand cmd = new SqlCommand("update sample set transform_to_id = @transform_to_id where id in(" + mSampleIdsCsv + ")", conn, trans);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@transform_to_id", newSample.Id);
                cmd.ExecuteNonQuery();

                trans.Commit();
            }
            catch (Exception ex)
            {
                trans?.Rollback();
                Common.Log.Error(ex);
                MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                conn?.Close();
            }

            DialogResult = DialogResult.OK;
            Close();
        }
Esempio n. 4
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();
        }
Esempio n. 5
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!Utils.IsValidGuid(cboxLaboratory.SelectedValue))
            {
                MessageBox.Show("Laboratory is mandatory");
                return;
            }

            if (!Utils.IsValidGuid(cboxResponsible.SelectedValue))
            {
                MessageBox.Show("Responsible is mandatory");
                return;
            }

            if (tbDeadline.Tag == null)
            {
                MessageBox.Show("Deadline is mandatory");
                return;
            }

            DateTime dl = (DateTime)tbDeadline.Tag;

            if (dl.Date < DateTime.Now.Date)
            {
                MessageBox.Show("Deadline can not be in the past");
                return;
            }

            if (mCustomer == null)
            {
                MessageBox.Show("Customer is mandatory");
                return;
            }

            SqlConnection  conn  = null;
            SqlTransaction trans = null;

            try
            {
                Guid labId = Utils.MakeGuid(cboxLaboratory.SelectedValue);

                conn  = DB.OpenConnection();
                trans = conn.BeginTransaction();

                string labPrefix  = DB.GetOrderPrefix(conn, trans, labId);
                int    orderCount = DB.GetNextOrderCount(conn, trans, labId);
                OrderName = labPrefix + "-" + DateTime.Now.ToString("yyyy") + "-" + orderCount;

                if (DB.NameExists(conn, trans, "assignment", OrderName, Guid.Empty))
                {
                    Common.Log.Error("Order with name " + OrderName + " already exist");
                    MessageBox.Show("Order with name " + OrderName + " already exist");
                    return;
                }

                DateTime currDate = DateTime.Now;

                Assignment assignment = new Assignment();
                assignment.Name                   = OrderName;
                assignment.LaboratoryId           = labId;
                assignment.AccountId              = Utils.MakeGuid(cboxResponsible.SelectedValue);
                assignment.Deadline               = (DateTime)tbDeadline.Tag;
                assignment.RequestedSigmaAct      = Convert.ToDouble(cboxRequestedSigma.SelectedValue);
                assignment.RequestedSigmaMDA      = Convert.ToDouble(cboxRequestedSigmaMDA.SelectedValue);
                assignment.CustomerCompanyName    = mCustomer.CompanyName;
                assignment.CustomerCompanyEmail   = mCustomer.CompanyEmail;
                assignment.CustomerCompanyPhone   = mCustomer.CompanyPhone;
                assignment.CustomerCompanyAddress = mCustomer.CompanyAddress;
                assignment.CustomerContactName    = mCustomer.ContactName;
                assignment.CustomerContactEmail   = mCustomer.ContactEmail;
                assignment.CustomerContactPhone   = mCustomer.ContactPhone;
                assignment.CustomerContactAddress = mCustomer.ContactAddress;
                assignment.WorkflowStatusId       = WorkflowStatus.Construction;
                assignment.LastWorkflowStatusDate = currDate;
                assignment.LastWorkflowStatusBy   = Common.Username;
                assignment.InstanceStatusId       = InstanceStatus.Active;
                assignment.CreateDate             = currDate;
                assignment.CreateId               = Common.UserId;
                assignment.UpdateDate             = currDate;
                assignment.UpdateId               = Common.UserId;
                assignment.Description            = tbDescription.Text.Trim();

                assignment.StoreToDB(conn, trans);

                string json = JsonConvert.SerializeObject(assignment);
                DB.AddAuditMessage(conn, trans, "assignment", assignment.Id, AuditOperationType.Insert, json, "");

                trans.Commit();

                OrderId      = assignment.Id;
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                trans?.Rollback();
                Common.Log.Error(ex);
                MessageBox.Show(ex.Message);
                DialogResult = DialogResult.Abort;
            }
            finally
            {
                conn?.Close();
            }

            Close();
        }
Esempio n. 6
0
        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();
        }