// When leaving a work order need to check if changes were made and if so prompt to save.
        private bool ProcessLeavingWorkOrder(Control.State processState)
        {
            bool result = true;

            if (processState == Control.State.New)
            {
                // BV doesn't prompt to save when leaving new orders,
                // so keeping it the same here.
            }
            else if (processState == Control.State.Update)
            {
                string       wo = txtWorkOrderNo.Text;
                DialogResult dr = MessageBox.Show(
                    String.Format("Do you wish to save your changes to the order details for {0}", wo),
                    "Work Order Details", MessageBoxButtons.YesNoCancel);
                if (dr == DialogResult.Yes)
                {
                    ProcessUpdateWorkOrder(Control.State.Update);
                    result = true;
                }
                else if (dr == DialogResult.No)
                {
                    result = true;
                }
                else if (dr == DialogResult.Cancel)
                {
                    result = false;
                }
            }

            return(result);
        }
        // Expected state return value: New, Error
        private Control.State ProcessCopyWorkOrder(Control.State processState)
        {
            if (processState == Control.State.Copy)
            {
                string newWO = BVConnection.GetNewWorkOrderNo().ToString();

                txtWorkOrderNo.Text = newWO;
                SetStatusText(Control.Status.Okay);

                return(ProcessAddNewWorkOrder(Control.State.New));
            }
            else
            {
                return(Control.State.Error);
            }
        }
        // Expected state return value: Active, Error.
        private Control.State ProcessOpenWorkOrder(Control.State processState, string workOrderNo)
        {
            if (processState == Control.State.Open)
            {
                DataProcess order = new DataProcess(ActiveSession.CompanyODBC);
                order.SelectOrder(workOrderNo);

                txtWorkOrderNo.Text = order.WorkOrderNo.ToString();
                Utilities.SetComboBoxSelectedIndex(ref cboProductType, order.ProductionType);
                Utilities.SetComboBoxSelectedIndex(ref cboProductionLine, order.ProductionLine);
                Utilities.SetComboBoxSelectedIndex(ref cboWarehouse, order.ProductWarehouse);
                Utilities.SetComboBoxSelectedIndex(ref cboItemCode, order.ProductNumber);
                txtItemDescription.Text = order.ProductDescription;

                txtPlannedUOM.Text      = order.PlannedUOM;
                txtPlannedQuantity.Text = order.PlannedQuantity.ToString();
                dtpPlannedDate.Value    = order.PlannedDate;
                txtPlannedShift.Text    = order.PlannedShift.ToString();

                txtProductionLineCapacity.Text = order.ProdLineCapacity.ToString();
                txtReOrderPoint.Text           = order.ReOrderPoint.ToString();
                txtBatchNo.Text   = order.NoBatches.ToString();
                txtBatchSize.Text = order.BatchSize.ToString();

                Utilities.SetComboBoxSelectedIndex(ref cboCustomerOrderNo, order.CustomerOrderNo);
                Utilities.SetComboBoxSelectedIndex(ref cboFGWorkOrderNo, order.FGWorkOrderNo);

                txtStatus.Text = order.Status;
                txtActualManufacturedQty.Text = order.ActualMftQty.ToString();
                txtActualShift.Text           = order.ActualShift.ToString();
                dtpActualDate.Value           = (DateTime)order.ActualDate;

                txtComments.Text         = order.Comments;
                txtCreatedUser.Text      = order.CreationUser;
                txtCreatedDate.Text      = order.CreationDate;
                txtLastModifiedUser.Text = order.LastModifiedUser;
                txtLastModifiedDate.Text = order.LastModifiedDate;

                return(Control.State.Active);
            }
            else
            {
                return(Control.State.Error);
            }
        }
        // Expected state return value: Active, Error
        private Control.State ProcessFirstWorkOrder(Control.State processState)
        {
            if (processState == Control.State.Open)
            {
                string wo = BVConnection.GetFirstWorkOrderNo();

                // "0" will be returned when the first record is active
                if (wo != "0")
                {
                    ProcessOpenWorkOrder(Control.State.Open, wo);
                }
                return(Control.State.Active);
            }
            else
            {
                return(Control.State.Error);
            }
        }
        // Can only delete work order if Status is Okay.
        private void tsbDeleteWorkOrder_Click(object sender, EventArgs e)
        {
            Control.State currentState = ActiveState;

            //if (ActiveState == Control.State.Active || ActiveState == Control.State.Update)
            if (txtStatus.Text == nameof(Control.Status.Okay))
            {
                Log = string.Format("Work Order {0} Deleted", txtWorkOrderNo.Text);

                ActiveState = ProcessDeleteWorkOrder(Control.State.Delete, currentState);

                ProcessStartNewWorkOrder();
            }
            else if (ActiveState == Control.State.New)
            {
                ProcessStartNewWorkOrder();
            }

            SetStateDisplay(ActiveState);
        }
        // This process requires an extra parameter currentState to return if the Delete fails
        // Expected state return value: currentState*, New, Error
        private Control.State ProcessDeleteWorkOrder(Control.State processState, Control.State currentState)
        {
            Control.State result = processState;

            if (processState == Control.State.Delete)
            {
                if (txtWorkOrderNo.Text != "")
                {
                    DataProcess order  = new DataProcess(ActiveSession.CompanyODBC);
                    string      status = txtStatus.Text;
                    int         won;

                    if (int.TryParse(txtWorkOrderNo.Text, out won))
                    {
                        if (order.DeleteOrder(won, status) == true)
                        {
                            //***Add Log update here
                            Utilities.ShowMessage(string.Format("Work Order {0} Deleted", won), "Delete");
                            result = Control.State.New;
                        }
                        else
                        {
                            Utilities.ShowMessage(string.Format("Failed to delete Work Order {0}", won), "Delete");
                            result = currentState;
                        }
                    }

                    else
                    {
                        Utilities.ShowError("Invalid Work Order", "Delete");
                        result = currentState;
                    }
                }
            }
            else
            {
                result = Control.State.Error;
            }

            return(result);
        }
        // Can only be completed if status is Issued.
        private void tspComplete_Click(object sender, EventArgs e)
        {
            Control.State processState = ActiveState;

            if (txtStatus.Text == nameof(Control.Status.Issued))
            {
                DialogResult dr = MessageBox.Show(
                    "Complete the Work Order and move it to history?",
                    "Complete", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    SetStatusText(Control.Status.Completed);
                    SetStateDisplay(Control.State.Completed);
                    Log = string.Format("Work Order {0} Completed", txtWorkOrderNo.Text);

                    processState = ProcessCompleteWorkOrder(Control.State.Completed);

                    if (processState != Control.State.Error)
                    {
                        ActiveState = ProcessStartNewWorkOrder();
                    }
                    else
                    {
                        SetStatusText(Control.Status.Issued);
                        ActiveState = Control.State.Active;
                    }
                    SetStateDisplay(ActiveState);
                }
                else if (dr == DialogResult.No)
                {
                }
            }
            else
            {
            }
        }
 // Try to ensure that what goes into the tool strip label is a Control.Status value.
 // This is for display only, form interaction should use the private Control.State ActiveState value.
 private void SetStateDisplay(Control.State state)
 {
     tslState.Text = state.ToString();
 }
        // Field validation is run before creating a new work order. If their are
        // invalid fields, state New is returned. If validation passes, create the work
        // order and return state Active.
        // Expected state return value: New, Active, Error
        private Control.State ProcessAddNewWorkOrder(Control.State processState)
        {
            Control.State result = Control.State.Error;

            if (processState == Control.State.New)
            {
                if (cboItemCode.SelectedItem == null)
                {
                    erpItemCode.SetError(cboItemCode, "An Item must be selected");
                    result = Control.State.New;
                }
                else
                {
                    erpItemCode.Clear();

                    DataProcess newOrder = new DataProcess(ActiveSession.CompanyODBC);

                    string won = txtWorkOrderNo.Text;
                    string prt = cboProductType.SelectedItem.ToString();
                    string prl = cboProductionLine.SelectedItem.ToString();

                    string whs = cboWarehouse.SelectedItem.ToString();
                    string itc = cboItemCode.SelectedItem.ToString();
                    string itd = txtItemDescription.Text;

                    string plu = txtPlannedUOM.Text;
                    string plq = txtPlannedQuantity.Text;
                    string pld = dtpPlannedDate.Text;
                    string pls = txtPlannedShift.Text;

                    string plc = txtProductionLineCapacity.Text;
                    string mil = txtReOrderPoint.Text;
                    string bas = txtBatchSize.Text;
                    string nob = txtBatchNo.Text;

                    string cun = (cboCustomerOrderNo.SelectedIndex == -1) ? "" : cboCustomerOrderNo.SelectedItem.ToString();
                    string fgo = (cboFGWorkOrderNo.SelectedIndex == -1) ? "" : cboFGWorkOrderNo.SelectedItem.ToString();
                    string sta = nameof(Control.Status.Okay);//txtStatus.Text;
                    string amq = txtActualManufacturedQty.Text;
                    string acs = txtActualShift.Text;
                    string acd = dtpActualDate.Text;

                    string com = txtComments.Text;
                    string crd = txtCreatedDate.Text;
                    string cru = txtCreatedUser.Text;
                    string lmd = txtLastModifiedDate.Text;
                    string lmu = txtLastModifiedUser.Text;

                    List <string> validateErrors = newOrder.Validate(
                        won, prt, prl,
                        whs, itc, itd,
                        plu, plq, pld, pls,
                        plc, mil, nob, bas,
                        cun, fgo, sta, amq, acs, acd,
                        com, crd, cru, lmd, lmu
                        );

                    if (validateErrors.Count > 0)
                    {
                        string errors = "The following fields did not pass data validation:\n";
                        foreach (string s in validateErrors)
                        {
                            errors = string.Format("{0}\n{1}", errors, s);
                        }
                        Utilities.ShowError(errors, "Data Validation Error");
                        result = Control.State.New;
                    }
                    else
                    {
                        newOrder.AddOrder();
                        ProcessRecordLogHistory(newOrder, Log);
                        Utilities.ShowMessage(string.Format("Work Order {0} Added.", won), "Add");
                        result = Control.State.Active;
                    }
                }
            }
            else
            {
                result = Control.State.Error;
            }

            return(result);
        }
        // Issues button can swap between Okay/Issued/UnIssed status.
        private void tspIssue_Click(object sender, EventArgs e)
        {
            Control.State processState = ActiveState;

            if (txtStatus.Text == nameof(Control.Status.Okay))
            {
                DialogResult dr = MessageBox.Show(
                    "Issue the Work Order?",
                    "Issue", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    SetStatusText(Control.Status.Issued);
                    Log          = string.Format("Work Order {0} Issued", txtWorkOrderNo.Text);
                    processState = Control.State.Update;

                    ActiveState = ProcessUpdateWorkOrder(processState);
                }
                else if (dr == DialogResult.No)
                {
                }
            }

            else if (txtStatus.Text == nameof(Control.Status.Issued))
            {
                DialogResult dr = MessageBox.Show(
                    "Un-issue the Work Order?",
                    "Un-issue", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    SetStatusText(Control.Status.UnIssued);
                    Log          = string.Format("Work Order {0} Un-Issued", txtWorkOrderNo.Text);
                    processState = Control.State.Update;

                    ActiveState = ProcessUpdateWorkOrder(processState);
                }
                else if (dr == DialogResult.No)
                {
                }
            }
            else if (txtStatus.Text == nameof(Control.Status.UnIssued))
            {
                DialogResult dr = MessageBox.Show(
                    "Reissue the Work Order?",
                    "Reissue", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    SetStatusText(Control.Status.Issued);
                    Log          = string.Format("Work Order {0} Reissued", txtWorkOrderNo.Text);
                    processState = Control.State.Update;

                    ActiveState = ProcessUpdateWorkOrder(processState);
                }
                else if (dr == DialogResult.No)
                {
                }
            }
            else
            {
            }

            SetStateDisplay(ActiveState);
        }