using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Routing; using Nop.Core; using Nop.Core.Domain; using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Common; using Nop.Core.Domain.Customers; using Nop.Core.Domain.Directory; using Nop.Core.Domain.Messages; using Nop.Core.Domain.Orders; using Nop.Core.Domain.Payments; using Nop.Core.Domain.Tax; using Nop.Core.Domain.Vendors; using Nop.Core.Events; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; using Nop.Services.Attributes; using Nop.Services.Blogs; using Nop.Services.Catalog; using Nop.Services.Common; using Nop.Services.Customers; using Nop.Services.Directory; using Nop.Services.Events; using Nop.Services.Helpers; using Nop.Services.Html; using Nop.Services.Localization; using Nop.Services.Logging; using Nop.Services.Messages; using Nop.Services.News; using Nop.Services.Orders; using Nop.Services.Payments; using Nop.Services.Seo; using Nop.Services.Shipping; using Nop.Services.Stores; using Nop.Services.Vendors; using System.ComponentModel.DataAnnotations; using System.Text; namespace Nop.Plugin.Misc.FruitBankPlugin.Infrastructure { public class FruitBankMessageTokenProvider : MessageTokenProvider { private readonly IOrderService _orderService; private readonly IPriceFormatter _priceFormatter; private readonly ICurrencyService _currencyService; private readonly CurrencySettings _currencySettings; private readonly FruitBankDbContext _dbContext; public FruitBankMessageTokenProvider( CatalogSettings catalogSettings, CurrencySettings currencySettings, IActionContextAccessor actionContextAccessor, IAddressService addressService, IAttributeFormatter addressAttributeFormatter, IAttributeFormatter customerAttributeFormatter, IAttributeFormatter vendorAttributeFormatter, IBlogService blogService, ICountryService countryService, ICurrencyService currencyService, ICustomerService customerService, IDateTimeHelper dateTimeHelper, IEventPublisher eventPublisher, IGenericAttributeService genericAttributeService, IGiftCardService giftCardService, IHtmlFormatter htmlFormatter, ILanguageService languageService, ILocalizationService localizationService, ILogger logger, INewsService newsService, IOrderService orderService, IPaymentPluginManager paymentPluginManager, IPaymentService paymentService, IPriceFormatter priceFormatter, IProductService productService, IRewardPointService rewardPointService, IShipmentService shipmentService, IStateProvinceService stateProvinceService, IStoreContext storeContext, IStoreService storeService, IUrlHelperFactory urlHelperFactory, IUrlRecordService urlRecordService, IWorkContext workContext, MessageTemplatesSettings templatesSettings, PaymentSettings paymentSettings, StoreInformationSettings storeInformationSettings, TaxSettings taxSettings, FruitBankDbContext dbContext ) : base( catalogSettings, currencySettings, actionContextAccessor, addressService, addressAttributeFormatter, customerAttributeFormatter, vendorAttributeFormatter, blogService, countryService, currencyService, customerService, dateTimeHelper, eventPublisher, genericAttributeService, giftCardService, htmlFormatter, languageService, localizationService, logger, newsService, orderService, paymentPluginManager, paymentService, priceFormatter, productService, rewardPointService, shipmentService, stateProvinceService, storeContext, storeService, urlHelperFactory, urlRecordService, workContext, templatesSettings, paymentSettings, storeInformationSettings, taxSettings) { _orderService = orderService; _priceFormatter = priceFormatter; _currencyService = currencyService; _currencySettings = currencySettings; _dbContext = dbContext; } public override async Task AddOrderTokensAsync( IList tokens, Order order, int languageId, int vendorId = 0) { // Run base first to populate all other Order.* tokens await base.AddOrderTokensAsync(tokens, order, languageId, vendorId); // Replace the product table token with our custom version var existing = tokens.FirstOrDefault(t => t.Key == "Order.Product(s)"); if (existing != null) tokens.Remove(existing); tokens.Add(new Token("Order.Product(s)", await BuildCustomProductTableAsync(order, languageId), true)); } private async Task BuildCustomProductTableAsync(Order order, int languageId) { var currency = await _currencyService.GetCurrencyByCodeAsync(order.CustomerCurrencyCode) ?? await _currencyService.GetCurrencyByIdAsync(_currencySettings.PrimaryStoreCurrencyId); var items = await _orderService.GetOrderItemsAsync(order.Id); var itemDtos = await _dbContext.OrderItemDtos.GetAllByOrderId(order.Id).ToListAsync(); var sb = new StringBuilder(); sb.AppendLine(@" "); var rowIndex = 0; foreach (var item in itemDtos) { var product = await _orderService.GetProductByOrderItemIdAsync(item.Id); if (product == null) continue; var unitPrice = await _priceFormatter.FormatPriceAsync( item.UnitPriceInclTax, true, currency, languageId, true); var lineTotal = await _priceFormatter.FormatPriceAsync( item.PriceInclTax, true, currency, languageId, true); var rowBg = rowIndex % 2 == 0 ? "#ffffff" : "#f2f7f0"; rowIndex++; if (item.IsMeasurable) { var averageWeight = item.AverageWeight; var approximatePrice = item.Quantity * item.UnitPriceInclTax * (decimal)averageWeight; sb.AppendLine($@" "); } else { sb.AppendLine($@" "); } } var orderTotal = await _priceFormatter.FormatPriceAsync( order.OrderTotal, true, currency, languageId, true); if(itemDtos.Any(i => i.IsMeasurable)) { sb.AppendLine($@" "); } else { sb.AppendLine($@" "); } sb.AppendLine(" \n
Termék Mennyiség Egységár Összesen
{product.Name} {item.Quantity} {unitPrice} Kalkuláció alatt, nagyságrendileg {approximatePrice}
{product.Name} {item.Quantity} {unitPrice} {lineTotal}
Végösszeg: Mérendő termék miatt kalkuláció alatt...
Végösszeg: {orderTotal}
"); return sb.ToString(); } } }