/// <summary>
        /// Sets the visibility of controls based on the status of the Sales Order.
        /// </summary>
        /// <param name="form">The Sales Order Details form.</param>
        /// <param name="args">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public static void OnLoad1Step(ISalesOrderDetails form, EventArgs args)
        {
            ISalesOrder salesOrder = form.CurrentEntity as ISalesOrder;

            if (salesOrder != null)
            {
                IAppIdMappingService oMappingService = Sage.Platform.Application.ApplicationContext.Current.Services.Get <IAppIdMappingService>(true);
                bool closed = false;
                if (salesOrder.Status != null)
                {
                    closed =
                        (salesOrder.Status.ToUpper().Equals(
                             form.GetResource("SalesOrderStatus_Closed").ToString().ToUpper()) ||
                         salesOrder.Status.ToUpper().Equals(
                             form.GetResource("SalesOrderStatus_Transmitted").ToString().ToUpper()));
                }
                //if this is a Sales Order that synced from the accounting system or the Sales Order has been submitted then we disable it
                bool isOpen = false;
                if (!String.IsNullOrEmpty(salesOrder.ERPSalesOrder.ERPStatus))
                {
                    isOpen = (salesOrder.ERPSalesOrder.ERPStatus.Equals(
                                  form.GetResource("erpStatus_Open").ToString()) ||
                              salesOrder.ERPSalesOrder.ERPStatus.Equals(form.GetResource("erpStatus_Rejected").ToString()));
                }
                bool erpSalesOrder = (oMappingService.IsIntegrationEnabled() && (salesOrder.GlobalSyncId.HasValue && !isOpen));

                form.rdgSOType.Enabled            = (!closed || !salesOrder.IsQuote.HasValue) && !erpSalesOrder;
                form.lueAccount.IsReadOnly        = closed || erpSalesOrder;
                form.usrAccountManager.IsReadOnly = closed || erpSalesOrder;
                form.lueOpportunity.IsReadOnly    = closed || erpSalesOrder;
                form.lueRequestedBy.IsReadOnly    = closed || erpSalesOrder;
                form.dtpOrderDate.IsReadOnly      = closed || erpSalesOrder;
                form.dtpPromisedDate.IsReadOnly   = closed || erpSalesOrder;
                form.pklType.IsReadOnly           = closed || erpSalesOrder;
                form.pklType.Enabled           = !closed && !erpSalesOrder;
                form.pklStatus.IsReadOnly      = closed || erpSalesOrder;
                form.pklType.Enabled           = !closed && !erpSalesOrder;
                form.txtCustPO.IsReadOnly      = closed || erpSalesOrder;
                form.btnSaveSalesOrder.Visible = !closed && !erpSalesOrder;
                form.btnReset.Visible          = !closed && !erpSalesOrder;
                form.btnDelete.Visible         = !closed && !erpSalesOrder;
                form.txtComments.IsReadOnly    = closed || erpSalesOrder;
                form.lueERPApplication.Enabled = !closed && !erpSalesOrder;
                form.luePriceList.Enabled      = !closed && !erpSalesOrder;

                //if this is a Sales Order synced from the accounting system then no point executing this, as the Sales Order will be disabled.
                if (!erpSalesOrder && oMappingService.IsIntegrationEnabled())
                {
                    if (!salesOrder.CanChangeOperatingCompany())
                    {
                        form.lueERPApplication.Enabled = false;
                        form.luePriceList.Enabled      = false;
                    }
                    else
                    {
                        form.lueERPApplication.Enabled = true;
                        form.luePriceList.Enabled      = (form.lueERPApplication.LookupResultValue != null);
                    }
                }
                form.ctrlstERPApplication.Visible = oMappingService.IsIntegrationEnabled();
            }
        }
        public static void OnLoad1Step(IInsertSalesOrder form, EventArgs args)
        {
            ISalesOrder so = form.CurrentEntity as ISalesOrder;

            if (so == null)
            {
                return;
            }
            if (String.IsNullOrEmpty(form.rdgSOType.SelectedValue))
            {
                form.rdgSOType.SelectedValue = "SalesOrder";
            }
            Sage.Platform.SData.IAppIdMappingService oMappingService = Sage.Platform.Application.ApplicationContext.Current.Services.Get <Sage.Platform.SData.IAppIdMappingService>(false);
            if (oMappingService != null && oMappingService.IsIntegrationEnabled())
            {
                if (!so.CanChangeOperatingCompany())
                {
                    form.lueERPApplication.Enabled = false;
                    form.luePriceList.Enabled      = false;
                }
                else
                {
                    form.lueERPApplication.Enabled = true;
                    form.luePriceList.Enabled      = (form.lueERPApplication.LookupResultValue != null);
                }
            }
            else
            {
                form.clIntegrationContract.Visible = false;
            }

            ISelectionService srv = ApplicationContext.Current.Services.Get <ISelectionService>(true);

            if (srv != null)
            {
                ISelectionContext sc = srv.GetSelectionContext("QuickInsertAccountContact");
                if (sc != null)
                {
                    List <string> sels = sc.GetSelectedIds();
                    if (sels.Count > 0)
                    {
                        string   newContactId = sels[0];
                        IContact newContact   = Sage.Platform.EntityFactory.GetById <IContact>(newContactId);
                        IAccount newAccount   = newContact.Account;
                        so.Account         = newAccount;
                        so.AccountManager  = newAccount.AccountManager;
                        so.BillingContact  = newContact;
                        so.ShippingContact = newContact;
                        so.BillToName      = newContact.NameLF;
                        so.ShipToName      = newContact.NameLF;

                        if (so.BillingAddress == null)
                        {
                            ISalesOrderAddress billAddr = Sage.Platform.EntityFactory.Create <ISalesOrderAddress>();
                            so.BillingAddress = billAddr;
                        }
                        so.SetSalesOrderBillingAddress(newContact.Address);

                        if (so.ShippingAddress == null)
                        {
                            ISalesOrderAddress shipAddr = Sage.Platform.EntityFactory.Create <ISalesOrderAddress>();
                            so.ShippingAddress = shipAddr;
                        }
                        so.SetSalesOrderShippingAddress(newContact.Address);
                        srv.SetSelectionContext("QuickInsertAccountContact", null);
                    }
                }
            }
        }