-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculationService.cs
More file actions
118 lines (96 loc) · 5.06 KB
/
Copy pathCalculationService.cs
File metadata and controls
118 lines (96 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CMS.Commerce;
using CMS.ContentEngine;
using DancingGoat.Models;
using Kentico.Content.Web.Mvc;
using Kentico.Content.Web.Mvc.Routing;
using Kentico.Membership;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
namespace DancingGoat.Commerce;
public sealed class CalculationService
{
private readonly IHttpContextAccessor httpContextAccessor;
private readonly IPriceCalculationService<DancingGoatPriceCalculationRequest, DancingGoatPriceCalculationResult> priceCalculationService;
private readonly CustomerDataRetriever customerDataRetriever;
private readonly IPreferredLanguageRetriever preferredLanguageRetriever;
public CalculationService(IHttpContextAccessor httpContextAccessor, IPriceCalculationService<DancingGoatPriceCalculationRequest, DancingGoatPriceCalculationResult> priceCalculationService, CustomerDataRetriever customerDataRetriever, IWebPageDataContextRetriever webPageDataContextRetriever, IPreferredLanguageRetriever preferredLanguageRetriever)
{
this.httpContextAccessor = httpContextAccessor;
this.priceCalculationService = priceCalculationService;
this.customerDataRetriever = customerDataRetriever;
this.preferredLanguageRetriever = preferredLanguageRetriever;
}
/// <summary>
/// Calculate total price of all items within shopping cart with specified products with no shipping specified.
/// </summary>
public async Task<DancingGoatPriceCalculationResult> CalculateShoppingCart(ShoppingCartDataModel shoppingCartData, CancellationToken cancellationToken)
{
return await Calculate(shoppingCartData, PriceCalculationMode.ShoppingCart, 0, 0, null, cancellationToken);
}
/// <summary>
/// Calculate total price of all items within shopping cart with specified products with no shipping specified.
/// </summary>
public async Task<DancingGoatPriceCalculationResult> CalculateCatalogPrices(ShoppingCartDataModel shoppingCartData, CancellationToken cancellationToken)
{
return await Calculate(shoppingCartData, PriceCalculationMode.Catalog, 0, 0, null, cancellationToken);
}
/// <summary>
/// Calculate catalog prices for specified products.
/// </summary>
/// <param name="products">Products for prices calculation.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public async Task<ICollection<DancingGoatPriceCalculationResultItem>> CalculateCatalogPrices(IEnumerable<IProductFields> products, CancellationToken cancellationToken)
{
var cartModel = new ShoppingCartDataModel()
{
Items = products.Select(p => new ShoppingCartDataItem
{
ProductIdentifier = new ProductVariantIdentifier() { Identifier = (p as IContentItemFieldsSource).SystemFields.ContentItemID },
Quantity = 1
}).ToList()
};
return (await CalculateCatalogPrices(cartModel, cancellationToken)).Items;
}
/// <summary>
/// Calculate total price of all items within shopping cart with specified products.
/// </summary>
public async Task<DancingGoatPriceCalculationResult> Calculate(ShoppingCartDataModel shoppingCartData, PriceCalculationMode mode, int shippingMethodId, int paymentMethodId, CustomerAddressViewModel customerAddress, CancellationToken cancellationToken)
{
var userManager = httpContextAccessor.HttpContext.RequestServices.GetRequiredService<UserManager<ApplicationUser>>();
var user = await userManager.GetUserAsync(httpContextAccessor.HttpContext?.User);
int memberId = (user != null) ? user.Id : 0;
var calculationRequest = new DancingGoatPriceCalculationRequest
{
Items = [.. shoppingCartData.Items.Select(item => new DancingGoatPriceCalculationRequestItem
{
ProductIdentifier = item.ProductIdentifier,
Quantity = item.Quantity,
})],
ShippingMethodId = shippingMethodId,
PaymentMethodId = paymentMethodId,
BillingAddress = GetCustomerData(customerAddress),
BuyerIdentifier = BuyerIdentifier.FromMemberId(memberId),
LanguageName = preferredLanguageRetriever.Get(),
Mode = mode,
CouponCodes = shoppingCartData.CouponCodes
};
return await priceCalculationService.Calculate(calculationRequest, cancellationToken);
}
private static AddressDto GetCustomerData(CustomerAddressViewModel customerAddress)
{
return customerAddress == null ? null : new AddressDto()
{
Line1 = customerAddress.Line1,
Line2 = customerAddress.Line2,
City = customerAddress.City,
Zip = customerAddress.PostalCode,
CountryID = int.TryParse(customerAddress.CountryId, out var countryId) ? countryId : 0,
StateID = int.TryParse(customerAddress.StateId, out var stateId) ? stateId : 0,
};
}
}