277 lines
12 KiB
C#
277 lines
12 KiB
C#
using FruitBank.Common.Interfaces;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Nop.Core;
|
|
using Nop.Core.Domain.Catalog;
|
|
using Nop.Core.Domain.Orders;
|
|
using Nop.Core.Events;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Models;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Models.Orders;
|
|
using Nop.Services.Catalog;
|
|
using Nop.Services.Common;
|
|
using Nop.Services.Events;
|
|
using Nop.Services.Localization;
|
|
using Nop.Services.Orders;
|
|
using Nop.Services.Plugins;
|
|
using Nop.Web.Framework.Events;
|
|
using Nop.Web.Framework.Menu;
|
|
using Nop.Web.Models.Sitemap;
|
|
using System.Linq;
|
|
|
|
namespace Nop.Plugin.Misc.FruitBankPlugin.Services
|
|
{
|
|
public class EventConsumer : BaseAdminMenuCreatedEventConsumer, IConsumer<OrderPlacedEvent>, IConsumer<EntityUpdatedEvent<Order>>, IConsumer<AdminMenuCreatedEvent>
|
|
{
|
|
private readonly IGenericAttributeService _genericAttributeService;
|
|
private readonly IProductService _productService;
|
|
private readonly ISpecificationAttributeService _specificationAttributeService;
|
|
private readonly IOrderService _orderService;
|
|
private readonly IProductAttributeService _productAttributeService;
|
|
private readonly IWorkContext _workContext;
|
|
private readonly IStoreContext _storeContext;
|
|
private readonly IAdminMenu _adminMenu;
|
|
private readonly ILocalizationService _localizationService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly FruitBankAttributeService _fruitBankAttributeService;
|
|
|
|
public EventConsumer(
|
|
IGenericAttributeService genericAttributeService,
|
|
IProductService productService,
|
|
ISpecificationAttributeService specificationAttributeService,
|
|
IOrderService orderService,
|
|
IProductAttributeService productAttributeService,
|
|
IPluginManager<IPlugin> pluginManager,
|
|
IWorkContext workContext,
|
|
IStoreContext storeContext,
|
|
IAdminMenu adminMenu,
|
|
ILocalizationService localizationService,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
FruitBankAttributeService fruitBankAttributeService) : base(pluginManager)
|
|
{
|
|
_genericAttributeService = genericAttributeService;
|
|
_productService = productService;
|
|
_specificationAttributeService = specificationAttributeService;
|
|
_orderService = orderService;
|
|
_productAttributeService = productAttributeService;
|
|
_workContext = workContext;
|
|
_storeContext = storeContext;
|
|
_adminMenu = adminMenu;
|
|
_localizationService = localizationService;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_fruitBankAttributeService = fruitBankAttributeService;
|
|
}
|
|
|
|
protected override string PluginSystemName => "Misc.FruitBankPlugin";
|
|
|
|
public async Task HandleEventAsync(OrderPlacedEvent eventMessage)
|
|
{
|
|
var order = eventMessage.Order;
|
|
var orderItems = await _orderService.GetOrderItemsAsync(order.Id);
|
|
bool requiresMeasurement = false;
|
|
|
|
foreach (var item in orderItems)
|
|
{
|
|
var product = await _productService.GetProductByIdAsync(item.ProductId);
|
|
|
|
// itt pl. egy custom flag a producton, ami jelzi, hogy mérés kell hozzá
|
|
// akár egy product attribute is lehet, vagy egy saját extension metódus
|
|
if (product != null)
|
|
{
|
|
//var productAttributeMappings = await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(product.Id);
|
|
////Product Attributes
|
|
//foreach (var pam in productAttributeMappings)
|
|
//{
|
|
// var attributes = await _productAttributeService.GetProductAttributeValuesAsync(pam.Id);
|
|
// foreach (var attr in attributes)
|
|
// {
|
|
// // you can check for specific attribute by its name or id
|
|
// if (attr.Name == "NeedsToBeMeasured" && attr.IsPreSelected)
|
|
// {
|
|
// requiresMeasurement = true;
|
|
// break;
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
var productSpecAttributes = await _specificationAttributeService.GetProductSpecificationAttributesAsync(product.Id);
|
|
|
|
foreach (var specAttribute in productSpecAttributes)
|
|
{
|
|
// Get the specification attribute
|
|
var specificationAttribute = await _specificationAttributeService
|
|
.GetSpecificationAttributeByIdAsync(specAttribute.Id);
|
|
|
|
// Get the specification attribute option
|
|
var specificationAttributeOption = await _specificationAttributeService
|
|
.GetSpecificationAttributeOptionByIdAsync(specAttribute.SpecificationAttributeOptionId);
|
|
|
|
System.Diagnostics.Debug.WriteLine($"Spec Attribute: {specificationAttribute.Name}, Option: {specificationAttributeOption.Name}");
|
|
|
|
// Check if this is your "NeedsToBeMeasured" specification attribute
|
|
if (specificationAttribute.Name == "Measureable" && //nameof(MeasuringAttributeValues.IsMeasurable)
|
|
specificationAttributeOption.Name == "Yes") // or whatever value you set
|
|
{
|
|
requiresMeasurement = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if (requiresMeasurement)
|
|
{
|
|
var store = await _storeContext.GetCurrentStoreAsync();
|
|
// itt adjuk hozzá a GenericAttribute flag-et az orderhez
|
|
await _genericAttributeService.SaveAttributeAsync(order,
|
|
nameof(IMeasurable.IsMeasurable), true, store.Id);
|
|
// status pending
|
|
// paymentstatus pending
|
|
}
|
|
}
|
|
|
|
public async Task HandleEventAsync(EntityUpdatedEvent<Order> eventMessage)
|
|
{
|
|
await SaveOrderCustomAttributesAsync(eventMessage.Entity);
|
|
}
|
|
|
|
|
|
private async Task SaveOrderCustomAttributesAsync(Order order)
|
|
{
|
|
if (order == null) return;
|
|
|
|
var form = _httpContextAccessor.HttpContext?.Request?.Form;
|
|
if (form == null || form.Count == 0) return;
|
|
|
|
if (form.ContainsKey(nameof(IMeasurable.IsMeasurable)))
|
|
{
|
|
var isMeasurable = form[nameof(IMeasurable.IsMeasurable)].ToString().Contains("true");
|
|
//var isMeasurable = CommonHelper.To<bool>(form[nameof(IMeasurable.IsMeasurable)].ToString());
|
|
|
|
|
|
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Order, bool>(order.Id, nameof(IMeasurable.IsMeasurable), isMeasurable);
|
|
}
|
|
|
|
if (form.ContainsKey(nameof(IOrderDto.DateOfReceipt)))
|
|
{
|
|
var dateOfReceipt = form[nameof(IOrderDto.DateOfReceipt)];
|
|
|
|
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Order, DateTime>(order.Id, nameof(IOrderDto.DateOfReceipt), DateTime.Parse(dateOfReceipt));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task HandleEventAsync(AdminMenuCreatedEvent eventMessage)
|
|
{
|
|
var rootNode = eventMessage.RootMenuItem;
|
|
|
|
|
|
var shippingsListMenuItem = new AdminMenuItem
|
|
{
|
|
Visible = true,
|
|
SystemName = "FruitBank",
|
|
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.ShippingsList"), // You can localize this with await _localizationService.GetResourceAsync("...")
|
|
IconClass = "fas fa-shipping-fast",
|
|
Url = _adminMenu.GetMenuItemUrl("Shipping", "List")
|
|
};
|
|
|
|
|
|
var createShippingMenuItem = new AdminMenuItem
|
|
{
|
|
Visible = true,
|
|
SystemName = "Shippings.Create",
|
|
Title = "Create Shipping",
|
|
IconClass = "far fa-circle",
|
|
Url = _adminMenu.GetMenuItemUrl("Shipping", "Create")
|
|
};
|
|
|
|
var editShippingMenuItem = new AdminMenuItem
|
|
{
|
|
Visible = true,
|
|
SystemName = "Shippings.Edit",
|
|
Title = "Edit Shipping",
|
|
IconClass = "far fa-circle",
|
|
Url = _adminMenu.GetMenuItemUrl("Shipping", "Edit")
|
|
};
|
|
|
|
// Create a new top-level menu item
|
|
var shippingsMenuItem = new AdminMenuItem
|
|
{
|
|
Visible = true,
|
|
SystemName = "FruitBank",
|
|
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.Shippings"), // You can localize this with await _localizationService.GetResourceAsync("...")
|
|
IconClass = "fas fa-shipping-fast",
|
|
//Url = _adminMenu.GetMenuItemUrl("Shipping", "List")
|
|
ChildNodes = [shippingsListMenuItem, createShippingMenuItem, editShippingMenuItem]
|
|
};
|
|
|
|
var shippingConfigurationItem = rootNode;
|
|
shippingConfigurationItem.ChildNodes.Insert(2, shippingsMenuItem);
|
|
|
|
|
|
var invoiceListMenuItem = new AdminMenuItem
|
|
{
|
|
Visible = true,
|
|
SystemName = "FruitBank",
|
|
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.InvoicesList"), // You can localize this with await _localizationService.GetResourceAsync("...")
|
|
IconClass = "fas fa-file-invoice",
|
|
Url = _adminMenu.GetMenuItemUrl("Invoices", "List")
|
|
};
|
|
|
|
// Create a new top-level menu item
|
|
var invoicesMenuItem = new AdminMenuItem
|
|
{
|
|
Visible = true,
|
|
SystemName = "FruitBank",
|
|
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.Invoices"), // You can localize this with await _localizationService.GetResourceAsync("...")
|
|
IconClass = "fas fa-file-invoice",
|
|
//Url = _adminMenu.GetMenuItemUrl("Shipping", "List")
|
|
ChildNodes = [invoiceListMenuItem]
|
|
};
|
|
|
|
var invoiceConfigurationItem = rootNode;
|
|
invoiceConfigurationItem.ChildNodes.Insert(2, invoicesMenuItem);
|
|
|
|
|
|
var configurationItem = rootNode.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Configuration"));
|
|
if (configurationItem is null)
|
|
return;
|
|
|
|
var shippingItem = configurationItem.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Shipping"));
|
|
var widgetsItem = configurationItem.ChildNodes.FirstOrDefault(node => node.SystemName.Equals("Widgets"));
|
|
if (shippingItem is null && widgetsItem is null)
|
|
return;
|
|
|
|
var index = shippingItem is not null ? configurationItem.ChildNodes.IndexOf(shippingItem) : -1;
|
|
if (index < 0)
|
|
index = widgetsItem is not null ? configurationItem.ChildNodes.IndexOf(widgetsItem) : -1;
|
|
if (index < 0)
|
|
return;
|
|
|
|
configurationItem.ChildNodes.Insert(index + 1, new AdminMenuItem
|
|
{
|
|
|
|
Visible = true,
|
|
SystemName = "FruitBank",
|
|
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.AI"),
|
|
IconClass = "far fa-dot-circle",
|
|
ChildNodes = new List<AdminMenuItem>
|
|
{
|
|
new()
|
|
{
|
|
|
|
Visible = true,
|
|
SystemName = "FruitBank",
|
|
Title = await _localizationService.GetResourceAsync("Plugins.Misc.FruitBankPlugin.Menu.Configure"),
|
|
IconClass = "far fa-circle",
|
|
Url = _adminMenu.GetMenuItemUrl("FruitBankPlugin", "Configure"),
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
}
|
|
}
|