/// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePostOrderPlanningDetailUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;

            Entity preImageEntity  = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

            IOrganizationService service = localContext.OrganizationService;
            ITracingService      trace   = localContext.TracingService;

            if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity))
            {
                return;
            }

            Entity orderPlanningeEntity = (Entity)context.InputParameters["Target"];

            if (orderPlanningeEntity.LogicalName != "gsc_sls_orderplanningdetail")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plugin
            {
                try
                {
                    OrderPlanningDetailHandler orderPlanningHandler = new OrderPlanningDetailHandler(service, trace);

                    var preEndingInv = preImageEntity.Contains("gsc_endinginventory")
                        ? preImageEntity.GetAttributeValue <Double>("gsc_endinginventory")
                        : 0;
                    var postEndingInv = postImageEntity.Contains("gsc_endinginventory")
                        ? postImageEntity.GetAttributeValue <Double>("gsc_endinginventory")
                        : 0;

                    if (preEndingInv != postEndingInv)
                    {
                        orderPlanningHandler.ComputeStockMonth(postImageEntity);
                        orderPlanningHandler.CreatDetailsForNextMonth(postImageEntity);
                    }
                }

                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }
예제 #2
0
        public void ComputeStockMonthZero()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Order Planning Details Entity Collection
            var OrderPlanningDetailCollection = new EntityCollection()
            {
                EntityName = "gsc_sls_orderplanningdetail",
                Entities   =
                {
                    new Entity
                    {
                        Id          = new Guid(),
                        LogicalName = "gsc_sls_orderplanningdetail",
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_endinginventory",    10.00 },
                            { "gsc_retailaveragesales",   0.0 },
                            { "gsc_stockmonth",           0.0 }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup(service => service.Retrieve(
                                     It.IsAny <string>(),
                                     It.IsAny <Guid>(),
                                     It.IsAny <ColumnSet>())).Returns(OrderPlanningDetailCollection.Entities[0]);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == OrderPlanningDetailCollection.Entities[0].LogicalName)))).Callback <Entity>(s => OrderPlanningDetailCollection.Entities[0] = s);

            #endregion

            #region 2. Call/Action
            var OrderPlanningHandler = new OrderPlanningDetailHandler(orgService, orgTracing);
            OrderPlanningHandler.ComputeStockMonth(OrderPlanningDetailCollection.Entities[0]);
            #endregion

            #region 3. Verify
            Assert.AreEqual(0, OrderPlanningDetailCollection.Entities[0].GetAttributeValue <Double>("gsc_stockmonth"));
            #endregion
        }