示例#1
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();
        }