/// <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 ExecutePostQuoteDiscountCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity quoteDiscountEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; try { QuoteDiscountHandler quoteDiscountHandler = new QuoteDiscountHandler(service, trace); EntityCollection quoteDiscountCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_quotediscount", "gsc_cmn_quotediscountid", quoteDiscountEntity.Id, service, null, OrderType.Ascending, new[] { "gsc_discountamount", "gsc_quoteid" }); if (quoteDiscountCollection.Entities.Count > 0) { quoteDiscountHandler.SetQuoteTotalDiscountAmount(quoteDiscountCollection.Entities[0], message); } } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat(ex.Message)); } }
/// <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 ExecutePreValidateQuoteDiscountDelete(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; var quoteDiscountEntity = (EntityReference)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; if (context.Depth > 1) { return; } try { QuoteDiscountHandler quoteDiscounthandler = new QuoteDiscountHandler(service, trace); #region Calling SetQuoteTotalDiscountAmount method EntityCollection quoteDiscountRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_quotediscount", "gsc_cmn_quotediscountid", quoteDiscountEntity.Id, service, null, OrderType.Ascending, new[] { "gsc_quoteid", "gsc_discountamount", "gsc_applyamounttodp", "gsc_applyamounttoaf", "gsc_applyamounttoup" }); if (quoteDiscountRecords != null && quoteDiscountRecords.Entities.Count > 0) { Entity quoteDiscount = quoteDiscountRecords.Entities[0]; quoteDiscounthandler.SetQuoteTotalDiscountAmount(quoteDiscount, message); } #endregion } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); } }
public void SetTotalDiscountAmountUnitTest() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Quote Discount EntityCollection var QuoteDiscountCollection = new EntityCollection { EntityName = "gsc_cmn_quotediscount", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_cmn_quotediscount", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "gsc_quoteid", new EntityReference("quote", Guid.NewGuid()) { Name = "Sample Quote" } }, { "gsc_discountamount", new Money((Decimal)100000.00) } } } } }; #endregion #region Quote EntityCollection double dppercentage = 40; double afpercentage = 40; double uppercentage = 20; var QuoteCollection = new EntityCollection { EntityName = "quote", Entities = { new Entity { Id = QuoteDiscountCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_quoteid").Id, LogicalName = "quote", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "totaldiscountamount", new Money(0) }, { "gsc_applytodppercentage", dppercentage }, { "gsc_applytodpamount", new Money() }, { "gsc_applytoafpercentage", afpercentage }, { "gsc_applytoafamount", new Money() }, { "gsc_applytouppercentage", uppercentage }, { "gsc_applytoupamount", new Money() }, { "statecode", new OptionSetValue(0) } } } } }; #endregion orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == QuoteDiscountCollection.EntityName) ))).Returns(QuoteDiscountCollection); orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == QuoteCollection.EntityName) ))).Returns(QuoteCollection); orgServiceMock.Setup(service => service.Retrieve( It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <ColumnSet>())).Returns(QuoteCollection.Entities[0]); #endregion #region 2. Call/Action var QuoteDiscountHandler = new QuoteDiscountHandler(orgService, orgTracing); Entity quote = QuoteDiscountHandler.SetQuoteTotalDiscountAmount(QuoteDiscountCollection.Entities[0], "Create"); #endregion #region 3. Verify Assert.AreEqual(QuoteDiscountCollection.Entities[0].GetAttributeValue <Money>("gsc_discountamount").Value, QuoteCollection.Entities[0].GetAttributeValue <Money>("totaldiscountamount").Value); Assert.AreEqual(40000, QuoteCollection.Entities[0].GetAttributeValue <Money>("gsc_applytodpamount").Value); Assert.AreEqual(40000, QuoteCollection.Entities[0].GetAttributeValue <Money>("gsc_applytoafamount").Value); Assert.AreEqual(20000, QuoteCollection.Entities[0].GetAttributeValue <Money>("gsc_applytoupamount").Value); #endregion }
public void ComputeDeductedTotalDiscountAmountUnitTest() { #region 1. Setup / Arrange var orgServiceMock = new Mock <IOrganizationService>(); var orgService = orgServiceMock.Object; var orgTracingMock = new Mock <ITracingService>(); var orgTracing = orgTracingMock.Object; #region Quote EntityCollection var QuoteCollection = new EntityCollection { EntityName = "quote", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "quote", EntityState = EntityState.Created, Attributes = new AttributeCollection { { "totaldiscountamount", "" }, { "statecode", new OptionSetValue(0) } } } } }; #endregion #region Quote Discount EntityCollection var QuoteDiscountCollection = new EntityCollection { EntityName = "gsc_cmn_quotediscount", Entities = { new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_cmn_quotediscount", EntityState = EntityState.Changed, Attributes = new AttributeCollection { { "gsc_quoteid", new EntityReference("quote", QuoteCollection.Entities[0].Id) { Name = "Sample Quote 1" } }, { "gsc_discountamount", new Money((Decimal)100000.00) } } }, new Entity { Id = Guid.NewGuid(), LogicalName = "gsc_cmn_appliedpricelist", EntityState = EntityState.Changed, Attributes = new AttributeCollection { { "gsc_quoteid", new EntityReference("quote", QuoteCollection.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 == QuoteDiscountCollection.EntityName) ))).Returns(QuoteDiscountCollection); orgServiceMock.Setup((service => service.RetrieveMultiple( It.Is <QueryExpression>(expression => expression.EntityName == QuoteCollection.EntityName) ))).Returns(QuoteCollection); orgServiceMock.Setup(service => service.Retrieve( It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <ColumnSet>())).Returns(QuoteCollection.Entities[0]); #endregion #region 2. Call/Action var QuoteDiscountHandler = new QuoteDiscountHandler(orgService, orgTracing); Entity quote = QuoteDiscountHandler.SetQuoteTotalDiscountAmount(QuoteDiscountCollection.Entities[1], "Delete"); #endregion #region 3. Verify Assert.AreEqual(QuoteDiscountCollection.Entities[1].GetAttributeValue <Money>("gsc_discountamount").Value, QuoteCollection.Entities[0].GetAttributeValue <Money>("totaldiscountamount").Value); #endregion }
/// <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 ExecutePostQuoteDiscountUpdate(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 quoteDiscountEntity = (Entity)context.InputParameters["Target"]; if (quoteDiscountEntity.LogicalName != "gsc_cmn_quotediscount") { return; } if (context.Mode == 0) //Synchronous Plugin { try { #region Pre-images var preImageQuoteId = preImageEntity.Contains("gsc_quoteid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_quoteid").Id : Guid.Empty; var preImagePriceListId = preImageEntity.Contains("gsc_pricelistid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_pricelistid").Id : Guid.Empty; var preImageDiscountAmount = preImageEntity.Contains("gsc_discountamount") ? 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 postImageQuoteId = postImageEntity.Contains("gsc_quoteid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_quoteid").Id : Guid.Empty; var postImagePriceListId = postImageEntity.Contains("gsc_pricelistid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_pricelistid").Id : Guid.Empty; var postImageDiscountAmount = postImageEntity.Contains("gsc_discountamount") ? 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; QuoteDiscountHandler quoteDiscountHandler = new QuoteDiscountHandler(service, trace); if (preImagePriceListId != postImagePriceListId) { quoteDiscountHandler.ReplicateDiscountInformation(quoteDiscountEntity, message); } //Functions triggered on Sales Order, Apply Amount to DP, Apply Amount to AF, Apply Amount to DP change if (preImageQuoteId != postImageQuoteId || preImageApplyAmountToDp != postImageApplyAmountToDp || preImageApplyAmountToAf != postImageApplyAmountToAf || preImageApplyAmountToUp != postImageApplyAmountToUp) { quoteDiscountHandler.SetQuoteTotalDiscountAmount(postImageEntity, message); } } catch (Exception ex) { if (ex.Message.Contains("Cannot associate discount. Vehicle is missing in Quotation Record.")) { throw new InvalidPluginExecutionException("Cannot associate discount. Vehicle is missing in Quotation Record."); } else if (ex.Message.Contains("The Promo selected is not applicable for the product of this Quote.")) { throw new InvalidPluginExecutionException("The Promo selected is not applicable for the product of this Quote."); } else { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace)); } } } }