예제 #1
0
        /// <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 ExecutePreValidateOrderDiscountDelete(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            var    salesOrderDiscountEntity = (EntityReference)context.InputParameters["Target"];
            string message = context.MessageName;
            string error   = "";

            if (context.Depth > 1)
            {
                return;
            }

            try
            {
                SalesOrderDiscountHandler salesOrderDiscountHandler = new SalesOrderDiscountHandler(service, trace);

                #region Calling SetOrderTotalDiscountAmount method

                EntityCollection salesOrderDiscountRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_salesorderdiscount", "gsc_cmn_salesorderdiscountid", salesOrderDiscountEntity.Id, service, null, OrderType.Ascending,
                                                                                                     new[] { "gsc_salesorderid", "gsc_discountamount", "gsc_applyamounttodp", "gsc_applyamounttoaf", "gsc_applyamounttoup" });

                if (salesOrderDiscountRecords != null && salesOrderDiscountRecords.Entities.Count > 0)
                {
                    Entity salesOrderDiscount = salesOrderDiscountRecords.Entities[0];
                    salesOrderDiscountHandler.SetOrderTotalDiscountAmount(salesOrderDiscount, message);
                }

                #endregion
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
            }
        }
예제 #2
0
        /// <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 ExecutePostOrderDiscountCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity salesOrderDiscountEntity = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;
            string error   = "";

            try
            {
                SalesOrderDiscountHandler salesOrderDiscountHandler = new SalesOrderDiscountHandler(service, trace);
                salesOrderDiscountHandler.ReplicateDiscountInformation(salesOrderDiscountEntity);
                salesOrderDiscountHandler.SetOrderTotalDiscountAmount(salesOrderDiscountEntity, message);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("The Promo selected is not applicable for the product of this Order."))
                {
                    throw new InvalidPluginExecutionException("The Promo selected is not applicable for the product of this Order.");
                }
                else if (ex.Message.Contains("Cannot associate discount. Vehicle is missing in Order Record."))
                {
                    throw new InvalidPluginExecutionException("Cannot associate discount. Vehicle is missing in Order Record.");
                }
                else
                {
                    throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                }
            }
        }
예제 #3
0
        /// <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 ExecutePreOrderDiscountCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity orderDiscountEntity      = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;

            try
            {
                SalesOrderDiscountHandler orderDiscountHandler = new SalesOrderDiscountHandler(service, trace);
                orderDiscountHandler.CheckifDiscountExists(orderDiscountEntity);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(String.Concat(ex.Message));
            }
        }
예제 #4
0
        public void ComputeDeductedOrderTotalDiscountAmountUnitTest()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;


            #region Sales Order EntityCollection
            var SalesOrderCollection = new EntityCollection
            {
                EntityName = "salesorder",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "salesorder",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_totaldiscountamount", ""                    },
                            { "statecode",               new OptionSetValue(0) }
                        }
                    }
                }
            };
            #endregion

            #region Sales Order Discount EntityCollection
            var SalesOrderDiscountCollection = new EntityCollection
            {
                EntityName = "gsc_cmn_salesorderdiscount",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_salesorderdiscount",
                        EntityState = EntityState.Changed,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_salesorderid",   new EntityReference("salesorder", SalesOrderCollection.Entities[0].Id)
                                {
                                    Name = "Sample Quote 1"
                                } },
                            { "gsc_discountamount", new Money((Decimal)100000.00) }
                        }
                    },

                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_salesorderdiscount",
                        EntityState = EntityState.Changed,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_salesorderid",   new EntityReference("salesorder", SalesOrderCollection.Entities[0].Id)
                                {
                                    Name = "Sample Quote 2"
                                } },
                            { "gsc_discountamount", new Money((Decimal)100000.00) }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == SalesOrderDiscountCollection.EntityName)
                                      ))).Returns(SalesOrderDiscountCollection);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == SalesOrderCollection.EntityName)
                                      ))).Returns(SalesOrderCollection);

            #endregion

            #region 2. Call/Action

            var    SalesOrderDiscountHandler = new SalesOrderDiscountHandler(orgService, orgTracing);
            Entity quote = SalesOrderDiscountHandler.SetOrderTotalDiscountAmount(SalesOrderDiscountCollection.Entities[1], "Delete");
            #endregion

            #region 3. Verify
            Assert.AreEqual(SalesOrderDiscountCollection.Entities[1].GetAttributeValue <Money>("gsc_discountamount").Value, SalesOrderCollection.Entities[0].GetAttributeValue <Money>("gsc_totaldiscountamount").Value);
            #endregion
        }
예제 #5
0
        public void SetLessDiscountFieldValues()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;


            #region Sales Order EntityCollection
            var SalesOrderCollection = new EntityCollection
            {
                EntityName = "salesorder",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "salesorder",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_downpaymentdiscount",    new Money(Decimal.Zero) },
                            { "gsc_discountamountfinanced", new Money(Decimal.Zero) },
                            { "gsc_discount",               new Money(Decimal.Zero) },
                            { "gsc_applytodpamount",        new Money(Decimal.Zero) },
                            { "gsc_applytoafamount",        new Money(Decimal.Zero) },
                            { "gsc_applytoupamount",        new Money(Decimal.Zero) },
                            { "statecode",                  new OptionSetValue(0)   }
                        }
                    }
                }
            };
            #endregion

            #region Sales Order Discount EntityCollection
            var SalesOrderDiscountCollection = new EntityCollection
            {
                EntityName = "gsc_cmn_salesorderdiscount",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_salesorderdiscount",
                        EntityState = EntityState.Changed,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_salesorderid",    new EntityReference("salesorder", SalesOrderCollection.Entities[0].Id)
                                {
                                    Name = "Sample Order Discount 1"
                                } },
                            { "gsc_discountamount",  new Money((Decimal)100000.00) },
                            { "gsc_applyamounttodp", new Money((Decimal)100000.00) },
                            { "gsc_applyamounttoaf", new Money((Decimal)200000.00) },
                            { "gsc_applyamounttoup", new Money((Decimal)300000.00) }
                        }
                    },

                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_salesorderdiscount",
                        EntityState = EntityState.Changed,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_salesorderid",    new EntityReference("salesorder", SalesOrderCollection.Entities[0].Id)
                                {
                                    Name = "Sample Order 2 Discount"
                                } },
                            { "gsc_discountamount",  new Money((Decimal)100000.00) },
                            { "gsc_applyamounttodp", new Money((Decimal)300000.00) },
                            { "gsc_applyamounttoaf", new Money((Decimal)200000.00) },
                            { "gsc_applyamounttoup", new Money((Decimal)100000.00) }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == SalesOrderDiscountCollection.EntityName)
                                      ))).Returns(SalesOrderDiscountCollection);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == SalesOrderCollection.EntityName)
                                      ))).Returns(SalesOrderCollection);

            #endregion

            #region 2. Call/Action

            var SalesOrderDiscountHandler = new SalesOrderDiscountHandler(orgService, orgTracing);
            //Entity salesOrder = SalesOrderDiscountHandler.SetLessDiscountValues(SalesOrderDiscountCollection.Entities[0], "Update");
            #endregion

            #region 3. Verify
            Assert.AreEqual(SalesOrderCollection.Entities[0].GetAttributeValue <Money>("gsc_downpaymentdiscount").Value, 400000);
            Assert.AreEqual(SalesOrderCollection.Entities[0].GetAttributeValue <Money>("gsc_discountamountfinanced").Value, 400000);
            Assert.AreEqual(SalesOrderCollection.Entities[0].GetAttributeValue <Money>("gsc_discount").Value, 400000);
            #endregion
        }
예제 #6
0
        /// <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 ExecutePostOrderDiscountUpdate(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 salesOrderDiscountEntity = (Entity)context.InputParameters["Target"];

            if (salesOrderDiscountEntity.LogicalName != "gsc_cmn_salesorderdiscount")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plugin
            {
                try
                {
                    #region Pre-images
                    var preImageSalesOrderId = preImageEntity.GetAttributeValue <EntityReference>("gsc_salesorderid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("gsc_salesorderid").Id
                        : Guid.Empty;

                    var preImageSalesOrderDiscountAmount = preImageEntity.GetAttributeValue <Money>("gsc_discountamount") != null
                        ? preImageEntity.GetAttributeValue <Money>("gsc_discountamount").Value
                        : Decimal.Zero;

                    var preImageApplyAmountToDp = preImageEntity.GetAttributeValue <Money>("gsc_applyamounttodp") != null
                        ? preImageEntity.GetAttributeValue <Money>("gsc_applyamounttodp").Value
                        : Decimal.Zero;

                    var preImageApplyAmountToAf = preImageEntity.GetAttributeValue <Money>("gsc_applyamounttoaf") != null
                        ? preImageEntity.GetAttributeValue <Money>("gsc_applyamounttoaf").Value
                        : Decimal.Zero;

                    var preImageApplyAmountToUp = preImageEntity.GetAttributeValue <Money>("gsc_applyamounttoup") != null
                        ? preImageEntity.GetAttributeValue <Money>("gsc_applyamounttoup").Value
                        : Decimal.Zero;

                    #endregion

                    #region Post-images
                    var postImageSalesOrderId = postImageEntity.GetAttributeValue <EntityReference>("gsc_salesorderid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("gsc_salesorderid").Id
                        : Guid.Empty;

                    var postImageSalesOrderDiscountAmount = postImageEntity.GetAttributeValue <Money>("gsc_discountamount") != null
                        ? postImageEntity.GetAttributeValue <Money>("gsc_discountamount").Value
                        : Decimal.Zero;

                    var postImageApplyAmountToDp = postImageEntity.GetAttributeValue <Money>("gsc_applyamounttodp") != null
                        ? postImageEntity.GetAttributeValue <Money>("gsc_applyamounttodp").Value
                        : Decimal.Zero;

                    var postImageApplyAmountToAf = postImageEntity.GetAttributeValue <Money>("gsc_applyamounttoaf") != null
                        ? postImageEntity.GetAttributeValue <Money>("gsc_applyamounttoaf").Value
                        : Decimal.Zero;

                    var postImageApplyAmountToUp = postImageEntity.GetAttributeValue <Money>("gsc_applyamounttoup") != null
                        ? postImageEntity.GetAttributeValue <Money>("gsc_applyamounttoup").Value
                        : Decimal.Zero;

                    #endregion

                    string message = context.MessageName;

                    SalesOrderDiscountHandler salesOrderDiscountHandler = new SalesOrderDiscountHandler(service, trace);

                    //Functions triggered on Sales Order, Sales Order Discount Amount, Apply Amount to DP, Apply Amount to AF, Apply Amount to DP change
                    if (preImageSalesOrderId != postImageSalesOrderId || preImageSalesOrderDiscountAmount != postImageSalesOrderDiscountAmount || preImageApplyAmountToDp != postImageApplyAmountToDp || preImageApplyAmountToAf != postImageApplyAmountToAf || preImageApplyAmountToUp != postImageApplyAmountToUp)
                    {
                        salesOrderDiscountHandler.SetOrderTotalDiscountAmount(postImageEntity, message);
                    }
                }
                catch (Exception ex)
                {
                    //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }