de Fútbol Sala
4.3 Producción de la Final de la Copa de España Comunidad de Madrid
4.3.1.3 Plan de financiación
Create a class that inherits from the base commerce runtime
WorkflowRequestHandler<TRequest, TResponse> class. The following example displays the GetPromotionsResponse class from the Promotions workflow:
namespace Extensions.Promotions.Workflow {
using System;
using System.Collections.Generic; using System.Linq;
using Extensions.Promotions.Data; using Extensions.Promotions.Service;
using Microsoft.Dynamics.Commerce.Runtime.DataModel; using Microsoft.Dynamics.Commerce.Runtime.Workflow;
using Microsoft.Dynamics.Commerce.Runtime.Workflow.Composition; /// <summary>
/// Helper Class to call neccessary Services and perform Data Manipulation on it. /// </summary>
[RequestHandlerMetadata(HandledRequestType = typeof(GetPromotionsRequest))] internal class PromotionsWorkflowRequestHandlerRaw :
WorkflowRequestHandler<GetPromotionsRequest, GetPromotionsResponse> {
/// <summary>
/// Executes the workflow to get the promotions. /// </summary>
/// <param name="request">The request.</param> /// <returns>The response.</returns>
protected override GetPromotionsResponse Process(GetPromotionsRequest request) {
if (request == null) {
throw new ArgumentNullException("request"); }
var serviceRequest = new GetPromotionsServiceRequest(this.Context);
IPromotions promotionsService = this.Context.Runtime.GetService<IPromotions>(); List<PromotionDetails> response = new List<PromotionDetails>();
List<ChannelPromotionsSummarizedData> summarizedResponse = new
List<ChannelPromotionsSummarizedData>();
serviceRequest.ChannelId = request.ChannelId; serviceRequest.CatalogId = request.CatalogId; foreach (PeriodicDiscountOfferType discount in
Enum.GetValues(typeof(PeriodicDiscountOfferType))) {
serviceRequest.PromotionType = discount; var serviceReply =
promotionsService.Execute<GetPromotionsServiceResponse>(serviceRequest).Promotions; if (request.HasPromotionData) { summarizedResponse.AddRange(ConvertToSummarizedData(serviceReply, discount)); } else { response.AddRange(serviceReply); } } if (request.HasPromotionData)
Customization
{var summarizedResult = from s in summarizedResponse orderby s.DiscountName
select s;
return new GetPromotionsResponse(summarizedResult); }
else
{
var result = from s in response orderby s.DiscountName
select s;
return new GetPromotionsResponse(result); }
}
/// <summary>
/// Iterates through a list of promotions for:
/// 1. Generating a list of discount codes associated with an Item. This generates duplicate records where the only difference is the discount codes.
/// 2. Removing above duplicates.
/// 3. Summarize the Promotion data into English Text for every unique record being considered.
/// </summary>
/// <param name="promotionDetailsCollection">List containing promotion details.</param>
/// <param name="typeOfDiscount">Discount Type associated with the list (eg. MultiBuy/ MixNMatch, etc).</param>
/// <returns>A collection of summarized promotions.</returns> private static List<ChannelPromotionsSummarizedData>
ConvertToSummarizedData(IEnumerable<PromotionDetails> promotionDetailsCollection,
PeriodicDiscountOfferType typeOfDiscount) {
var returnObject = new List<ChannelPromotionsSummarizedData>(); var allDiscountOfferNames = promotionDetailsCollection
.Select(i => new { i.DiscountName }) .Distinct();
foreach (var currentOffer in allDiscountOfferNames) {
var discountLineItems = from s in promotionDetailsCollection where
s.DiscountName == currentOffer.DiscountName orderby s.ItemId select s;
var lineItemsAfterPass1 = Pass1ConsolidateDiscountCodes(discountLineItems); var lineItemsAfterPass2 = Pass2GenerateSummarizedPromotionStatement(lineItemsAfterPass1, typeOfDiscount); var lineItemsAfterPass3 = Pass3GenerateConsolidatedPromotionStatement(lineItemsAfterPass2, typeOfDiscount); returnObject.AddRange(lineItemsAfterPass3); } return returnObject; } /// <summary>
/// Consolidate all the discount codes available for a discount and then remove redundant line items.
/// </summary>
/// <param name="offerCollection">Contains set of all Line Items for a particular discount Offer.</param>
/// <returns>A collection of promotion details..</returns> private static List<PromotionDetails>
Pass1ConsolidateDiscountCodes(IEnumerable<PromotionDetails> offerCollection) {
var returnObject = new List<PromotionDetails>(); var element = new PromotionDetails();
var iterator = offerCollection.ToList();
{
element = iterator.ElementAt(counter); if (element.IsDiscountCodeRequired) {
int index = 1;
while (counter + index < offerCollection.Count() &&
iterator.ElementAt(counter + index).ItemId == element.ItemId && iterator.ElementAt(counter + index).IsDiscountCodeRequired == element.IsDiscountCodeRequired)
{
element.DiscountCode += ", " + iterator.ElementAt(counter + index).DiscountCode; index++; } counter += index; } returnObject.Add(element); } return returnObject; } /// <summary>
/// Generates a Summarized Promotion Statement for every line item, But it is still not usable by the End-User.
/// </summary>
/// <param name="offerCollection">Contains set of all Line Items for a particular discount Offer.</param>
/// <param name="typeOfDiscount">Is it MultiBuy/ Mix-n-Match/ Discount/ Price Adjustment.</param>
/// <returns>A collection of summarized promotions.</returns> private static List<ChannelPromotionsSummarizedData>
Pass2GenerateSummarizedPromotionStatement(IEnumerable<PromotionDetails> offerCollection,
PeriodicDiscountOfferType typeOfDiscount) {
var returnObject = new List<ChannelPromotionsSummarizedData>(); ChannelPromotionsSummarizedData element;
foreach (var lineItem in offerCollection) {
element = new ChannelPromotionsSummarizedData(); element.OfferId = lineItem.OfferId;
element.DiscountName = lineItem.DiscountName;
element.DiscountDescription = lineItem.DiscountDescription; element.Disclaimer = lineItem.Disclaimer;
element.ProductName = lineItem.ProductName;
element.ProductOfferDescription = lineItem.ProductOfferDescription; element.ProductImageLink = lineItem.ProductImageLink;
element.IsDiscountCodeRequired = lineItem.IsDiscountCodeRequired; element.DiscountCodes = lineItem.DiscountCode;
switch (lineItem.DiscountMethod) {
case 0: // Percentage Discount
if (lineItem.DiscountPercent == 0) {
element.ProductPromotionStatement = String.Empty; }
else if (lineItem.DiscountPercent == 100) {
element.ProductPromotionStatement = "Get " + element.ProductName
+ " for free";
} else
{
element.ProductPromotionStatement = (typeOfDiscount ==
PeriodicDiscountOfferType.MultipleBuy) ? "Buy atleast " +
Customization
element.ProductPromotionStatement += "Get " +
decimal.ToInt32(lineItem.DiscountPercent) + "% Off on all " + lineItem.ProductName + "(s)"; }
break;
case 1: // Discount Amount
element.ProductPromotionStatement = "Get each " + lineItem.ProductName + " for $" + decimal.Round(lineItem.DiscountAmount, 2, MidpointRounding.AwayFromZero) + " less";
break;
case 2: // Offer Price
element.ProductPromotionStatement = (typeOfDiscount ==
PeriodicDiscountOfferType.MultipleBuy) ? "Buy atleast " + lineItem.MinimumQuantity + " " : "Buy ";
element.ProductPromotionStatement = lineItem.ProductName + "(s) for $" + decimal.Round(lineItem.OfferPrice, 2, MidpointRounding.AwayFromZero) + "each";
break;
case 3: // Offer Price Inclusive Of Tax
bool isDealPrice = ((from s in offerCollection where s.DiscountMethod
!= 3 select s).Count() == 0) ? true : false;
element.ProductPromotionStatement = "Get " + (isDealPrice ? lineItem.DiscountName : lineItem.ProductName) +
" for $" + decimal.Round(lineItem.OfferPriceInclusiveOfTax, 2, MidpointRounding.AwayFromZero) + "(Inclusive of Taxes)";
break; } returnObject.Add(element); } return returnObject; } /// <summary>
/// Consolidates and converts the Summarized Promotion Statement to an End-User understandable format.
/// </summary>
/// <param name="offerCollection">Contains set of all Line Items for a particular discount Offer.</param>
/// <param name="typeOfDiscount">Is it MultiBuy/ Mix-n-Match/ Discount/ Price Adjustment.</param>
/// <returns>A collection of summarized promotions.</returns> private static List<ChannelPromotionsSummarizedData>
Pass3GenerateConsolidatedPromotionStatement(IEnumerable<ChannelPromotionsSummarizedData> offerCollection, PeriodicDiscountOfferType typeOfDiscount)
{
var returnObject = new List<ChannelPromotionsSummarizedData>(); var iterator = offerCollection.ToList();
string appendText = " when you buy a "; string productStatement = String.Empty; bool appendFlag = false;
for (int counter = 0; counter < offerCollection.Count(); counter++) {
if
(string.IsNullOrEmpty(iterator.ElementAt(counter).ProductPromotionStatement)) {
appendFlag = true;
appendText += ((string.Compare(appendText, " when you buy a ", StringComparison.OrdinalIgnoreCase) == 0) ? String.Empty : " and ") +
iterator.ElementAt(counter).ProductName; } productStatement = (string.IsNullOrEmpty(iterator.ElementAt(counter).ProductPromotionStatement)) ? productStatement : iterator.ElementAt(counter).ProductPromotionStatement; }
{
lineItem.ProductPromotionStatement = appendFlag ? productStatement + appendText : lineItem.ProductPromotionStatement;
returnObject.Add(lineItem); } return returnObject; } } }