Untitled
unknown
csharp
a year ago
5.1 kB
13
Indexable
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace SieversGroup.CRM.FASTLTA.Plugins
{
public class OpportunityProductDiscountCalculation : IPlugin
{
#region Secure/Unsecure Configuration Setup
private string _secureConfig = null;
private string _unsecureConfig = null;
public OpportunityProductDiscountCalculation(string unsecureConfig, string secureConfig)
{
_secureConfig = secureConfig;
_unsecureConfig = unsecureConfig;
}
#endregion
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
// Make sure Plugin Context contains Entity by using (Target is Entity?). Target in Update-Step is usually the Entity that's being updated
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Pull the Entity that's used in the Step, cast it as an "Entity" and save it in "entity"
Entity opportunityProductEntity = (Entity)context.InputParameters["Target"];
// Save logical name of entity
string opportunityProductName = opportunityProductEntity.LogicalName;
if (opportunityProductName == "opportunityproduct")
{
// Retrieve specific fields and GUID of OpportunityProduct
Guid opportunityProductId = opportunityProductEntity.Id;
ColumnSet columns = new ColumnSet(new string[]
{
"baseamount",
"sie_discountcode",
"sie_discountpercent",
"manualdiscountamount"
});
// Retrieve the opportunityProduct entity
Entity opportunityProduct = service.Retrieve(opportunityProductName, opportunityProductId, columns);
// The 4 fields needed for calculations
string Betrag = "baseamount";
string Rabattmethode = "sie_discountcode";
string RabattProzent = "sie_discountpercent";
string ManuellerRabatt = "manualdiscountamount";
// Options for "sie_discountcode"
bool RabattMethodeBetrag = false;
bool RabattMethodeProzentVonBetrag = true;
// Check if the Rabattmethode field is present and is of type bool
if (opportunityProduct.Contains(Rabattmethode) && opportunityProduct[Rabattmethode] is bool rabattmethodeValue)
{
if (rabattmethodeValue == RabattMethodeBetrag)
{
// Logic for RabattMethodeBetrag
if (opportunityProduct.Contains(Betrag) && opportunityProduct.Contains(ManuellerRabatt))
{
decimal betrag = ((Money)opportunityProduct[Betrag]).Value;
decimal manuellerRabatt = ((Money)opportunityProduct[ManuellerRabatt]).Value;
decimal rabattierterBetrag = betrag - manuellerRabatt;
// Update the field with the new value
opportunityProduct[Betrag] = new Money(rabattierterBetrag);
service.Update(opportunityProduct);
}
}
else if (rabattmethodeValue == RabattMethodeProzentVonBetrag)
{
// Logic for RabattMethodeProzentVonBetrag
if (opportunityProduct.Contains(Betrag) && opportunityProduct.Contains(RabattProzent))
{
decimal betrag = ((Money)opportunityProduct[Betrag]).Value;
decimal rabattProzent = (decimal)opportunityProduct[RabattProzent];
decimal rabattierterBetrag = betrag * (1 - rabattProzent / 100);
// Update the field with the new value
opportunityProduct[Betrag] = new Money(rabattierterBetrag);
service.Update(opportunityProduct);
}
}
}
}
}
}
}
}
Editor is loading...
Leave a Comment