/// <summary>
 /// Sets the status to Open if the dialog is cancelled.  This is limited because it does not know the 
 /// prior status so it resets to a pre-determined value.
 /// </summary>
 /// <param name="form">the instance of the OpportunityClosedWon dialog</param>
 /// <param name="args">empty</param>
 public static void cmdCancel_OnClickStep1( IOpportunityClosedWon form,  EventArgs args)
 {
     IOpportunity opportunity = form.CurrentEntity as IOpportunity;
     object resOpen = System.Web.HttpContext.GetGlobalResourceObject("Opportunity", "Opp_Status_Open");
     if(resOpen != null)
         opportunity.Status = resOpen.ToString(); //"Open";
 }
        /// <summary>
        /// Sets the status to Open if the dialog is cancelled.  This is limited because it does not know the
        /// prior status so it resets to a pre-determined value.
        /// </summary>
        /// <param name="form">the instance of the OpportunityClosedWon dialog</param>
        /// <param name="args">empty</param>
        public static void cmdCancel_OnClickStep1(IOpportunityClosedWon form, EventArgs args)
        {
            IOpportunity opportunity = form.CurrentEntity as IOpportunity;

            if (!(opportunity.Closed ?? false))
            {
                object resOpen = form.GetResource("Opp_Status_Open").ToString();
                if (resOpen != null)
                {
                    opportunity.Status = resOpen.ToString();
                }
            }
        }
        /// <summary>
        /// Sets the flag, LostReplaced, on the opportunity competitor indicating that this opportunity 
        /// competitor was associated as a competitor replaced.
        /// </summary>
        /// <param name="form">The opportunity closed won form.</param>
        /// <param name="args">The event arguments.<see cref="System.EventArgs"/> instance containing the event data.</param>
        public static void lueCompetitorReplaced_OnChange(IOpportunityClosedWon form,  EventArgs args)
        {
            ICompetitor competitor = form.lueCompetitorReplaced.LookupResultValue as ICompetitor;
            if (competitor != null)
            {
                IOpportunity opportunity = form.CurrentEntity as IOpportunity;
                opportunity.SetOppCompetitorReplacedFlag(competitor);
            }

            Sage.Platform.WebPortal.Services.IPanelRefreshService panelRefresh = form.Services.Get<Sage.Platform.WebPortal.Services.IPanelRefreshService>();
            if(panelRefresh != null)
                panelRefresh.RefreshMainWorkspace();
        }
        /// <summary>
        /// Sets the status to Open if the dialog is cancelled.  This is limited because it does not know the
        /// prior status so it resets to a pre-determined value.
        /// </summary>
        /// <param name="form">the instance of the OpportunityClosedWon dialog</param>
        /// <param name="args">empty</param>
        public static void cmdCancel_OnClickStep1(IOpportunityClosedWon form, EventArgs args)
        {
            IOpportunity opportunity = form.CurrentEntity as IOpportunity;

            if (!(opportunity.Closed ?? false))
            {
                object resOpen = System.Web.HttpContext.GetGlobalResourceObject("Opportunity", "Opp_Status_Open");
                if (resOpen != null)
                {
                    opportunity.Status = resOpen.ToString(); //"Open";
                }
            }
        }
        /// <summary>
        /// Sets the flag, LostReplaced, on the opportunity competitor indicating that this opportunity
        /// competitor was associated as a competitor replaced.
        /// </summary>
        /// <param name="form">The opportunity closed won form.</param>
        /// <param name="args">The event arguments.<see cref="System.EventArgs"/> instance containing the event data.</param>
        public static void lueCompetitorReplaced_OnChange(IOpportunityClosedWon form, EventArgs args)
        {
            ICompetitor competitor = form.lueCompetitorReplaced.LookupResultValue as ICompetitor;

            if (competitor != null)
            {
                IOpportunity opportunity = form.CurrentEntity as IOpportunity;
                opportunity.SetOppCompetitorReplacedFlag(competitor);
            }

            Sage.Platform.WebPortal.Services.IPanelRefreshService panelRefresh = form.Services.Get <Sage.Platform.WebPortal.Services.IPanelRefreshService>();
            if (panelRefresh != null)
            {
                panelRefresh.RefreshMainWorkspace();
            }
        }
        public static void cmdOK_OnClick( IOpportunityClosedWon form,  EventArgs args)
        {
            Sage.Entity.Interfaces.ICompetitor competitor = form.lueCompReplaced.LookupResultValue as Sage.Entity.Interfaces.ICompetitor;
            Sage.Entity.Interfaces.IOpportunity opportunity = form.CurrentEntity as Sage.Entity.Interfaces.IOpportunity;

            opportunity.ValidateOpportunity(opportunity);

            if ((opportunity != null) && (competitor != null))
            {
                Sage.Platform.Repository.IRepository<Sage.Entity.Interfaces.IOpportunityCompetitor> oppCompetitor = Sage.Platform.EntityFactory.GetRepository<Sage.Entity.Interfaces.IOpportunityCompetitor>();
                Sage.Platform.Repository.IQueryable query = (Sage.Platform.Repository.IQueryable)oppCompetitor;
                Sage.Platform.Repository.IExpressionFactory expression = query.GetExpressionFactory();
                Sage.Platform.Repository.ICriteria criteria = query.CreateCriteria();
                Sage.Platform.Repository.IProjections proj = query.GetProjectionsFactory();

                criteria.Add(expression.Eq("Competitor.Id", competitor.Id))
                    .Add(expression.Eq("Opportunity.Id", opportunity.Id))
                    .SetProjection(proj.Count("Competitor.Id"));

                int count = criteria.UniqueResult<int>();

                if (count == 0)
                {
                    Sage.Entity.Interfaces.IOpportunityCompetitor oc = Sage.Platform.EntityFactory.Create<Sage.Entity.Interfaces.IOpportunityCompetitor>();
                    oc.Competitor = competitor;
                    oc.Opportunity = opportunity;
                    oc.Incumbent = false;
                    oc.Notes = competitor.Notes;
                    oc.Rating = competitor.Rating;
                    oc.Strategy = competitor.Strategy;
                    oc.Strengths = competitor.Strengths;

                    opportunity.Competitors.Add(oc);

                    oc.Save();
                    opportunity.Save();
                }
            }

            if (opportunity.Products != null)
            {
                foreach (Sage.Entity.Interfaces.IOpportunityProduct op in opportunity.Products)
                {
                    Sage.Entity.Interfaces.IAccountProduct ap = Sage.Platform.EntityFactory.Create<Sage.Entity.Interfaces.IAccountProduct>();
                    ap.Product = op.Product;
                    ap.Account = op.Opportunity.Account;
                    ap.ActualId = op.Product.ActualId;
                    ap.Returned = false;
                    ap.Evaluation = false;
                    ap.Opportunity = op.Opportunity;
                    ap.Quantity = op.Quantity;
                    ap.ProductName = op.Product.Name;
                    ap.Notes = op.Notes;
                    ap.AssetCode = null;
                    ap.Location = null;
                    ap.Environment = null;
                    ap.AssetVersion = null;
                    ap.ProductDescription = op.Product.Description;
                    ap.Save();
                }
            }
        }