product event fixex; etc...

This commit is contained in:
Loretta 2025-10-11 17:51:30 +02:00
parent c9fa03a69c
commit ca1f59a5e1
4 changed files with 53 additions and 87 deletions

View File

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc;
using Nop.Core; using Nop.Core;
using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Catalog;
using Nop.Plugin.Misc.FruitBankPlugin.Models; using Nop.Plugin.Misc.FruitBankPlugin.Models;
using Nop.Plugin.Misc.FruitBankPlugin.Services;
using Nop.Services.Common; using Nop.Services.Common;
using Nop.Web.Areas.Admin.Models.Catalog; using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Framework.Components; using Nop.Web.Framework.Components;
@ -14,60 +15,34 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Components
[ViewComponent(Name = "ProductAttributes")] [ViewComponent(Name = "ProductAttributes")]
public class ProductAttributesViewComponent : NopViewComponent public class ProductAttributesViewComponent : NopViewComponent
{ {
private const string NET_WEIGHT_KEY = nameof(IMeasuringAttributeValues.NetWeight); private readonly FruitBankAttributeService _fruitBankAttributeService;
private const string TARE_KEY = nameof(ITare.Tare);
private const string IS_MEASURABLE_KEY = nameof(IMeasuringAttributeValues.IsMeasurable);
private readonly IGenericAttributeService _genericAttributeService;
private readonly IWorkContext _workContext; private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext; private readonly IStoreContext _storeContext;
public ProductAttributesViewComponent( public ProductAttributesViewComponent(FruitBankAttributeService fruitBankAttributeService, IWorkContext workContext, IStoreContext storeContext)
IGenericAttributeService genericAttributeService,
IWorkContext workContext,
IStoreContext storeContext)
{ {
_genericAttributeService = genericAttributeService;
_workContext = workContext; _workContext = workContext;
_storeContext = storeContext; _storeContext = storeContext;
_fruitBankAttributeService = fruitBankAttributeService;
} }
public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData) public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
{ {
if (additionalData is not ProductModel productModel) return Content("");
var model = new ProductAttributesModel { ProductId = productModel.Id };
if (additionalData is not ProductModel product) if (model.ProductId > 0)
return Content("");
var model = new ProductAttributesModel
{ {
ProductId = product.Id var measuringAttributeValues = await _fruitBankAttributeService.GetMeasuringAttributeValuesAsync<Product>(model.ProductId);
}; if (measuringAttributeValues != null)
{
model.IsMeasurable = measuringAttributeValues.IsMeasurable;
model.NetWeight = measuringAttributeValues.NetWeight;
}
//get store scope model.Tare = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, double>(model.ProductId, nameof(ITare.Tare));
var storeScope = await _storeContext.GetCurrentStoreAsync(); model.IncomingQuantity = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, int>(model.ProductId, "IncomingQuantity");
if (product.Id > 0)
{
// Load existing values
var dbProduct = new Core.Domain.Catalog.Product { Id = product.Id };
//var dbMesaurable = await _genericAttributeService.GetAttributeAsync<bool>(dbProduct, IS_MEASURABLE_KEY, storeScope.Id);
//var dbNetWeight = await _genericAttributeService.GetAttributeAsync<decimal?>(dbProduct, NET_WEIGHT_KEY, storeScope.Id);
//var dbIncomingQuantity = await _genericAttributeService.GetAttributeAsync<decimal?>(dbProduct, "IncomingQuantity", storeScope.Id);
model.IsMeasurable = await _genericAttributeService
.GetAttributeAsync<bool>(dbProduct, IS_MEASURABLE_KEY, storeScope.Id);
model.NetWeight = await _genericAttributeService
.GetAttributeAsync<double>(dbProduct, NET_WEIGHT_KEY, storeScope.Id);
model.IncomingQuantity = await _genericAttributeService
.GetAttributeAsync<int?>(dbProduct, "IncomingQuantity", storeScope.Id);
model.Tare = await _genericAttributeService
.GetAttributeAsync<double>(dbProduct, TARE_KEY, storeScope.Id);
} }
return View("~/Plugins/Misc.FruitBankPlugin/Views/ProductAttributes.cshtml", model); return View("~/Plugins/Misc.FruitBankPlugin/Views/ProductAttributes.cshtml", model);

View File

@ -19,8 +19,8 @@ using System.Globalization;
namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.EventConsumers; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.EventConsumers;
public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext, IEnumerable<IAcLogWriterBase> logWriters, IGenericAttributeService genericAttributeService) : public class FruitBankEventConsumer(IHttpContextAccessor httpContextAcc, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IEnumerable<IAcLogWriterBase> logWriters) :
MgEventConsumer(httpContextAccessor, logWriters), MgEventConsumer(httpContextAcc, logWriters),
IConsumer<EntityDeletedEvent<Shipping>>, IConsumer<EntityDeletedEvent<Shipping>>,
IConsumer<EntityInsertedEvent<ShippingItem>>, IConsumer<EntityInsertedEvent<ShippingItem>>,
IConsumer<EntityUpdatedEvent<ShippingItem>>, IConsumer<EntityUpdatedEvent<ShippingItem>>,
@ -36,7 +36,7 @@ public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, Fr
{ {
var product = eventMessage.Entity; var product = eventMessage.Entity;
await SaveCustomAttributesAsync(eventMessage.Entity); await SaveProductCustomAttributesAsync(eventMessage.Entity);
var isMeasurableProduct = await fruitBankAttributeService.IsMeasurableEntityAsync<Product>(product.Id); var isMeasurableProduct = await fruitBankAttributeService.IsMeasurableEntityAsync<Product>(product.Id);
@ -51,61 +51,42 @@ public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, Fr
await base.HandleEventAsync(eventMessage); await base.HandleEventAsync(eventMessage);
} }
public async Task HandleEventAsync(EntityInsertedEvent<Product> eventMessage) public override async Task HandleEventAsync(EntityInsertedEvent<Product> eventMessage)
{ {
await SaveCustomAttributesAsync(eventMessage.Entity); await SaveProductCustomAttributesAsync(eventMessage.Entity);
await base.HandleEventAsync(eventMessage);
} }
private async Task SaveCustomAttributesAsync(Product product) private async Task SaveProductCustomAttributesAsync(Product product)
{ {
if (product == null) if (product == null) return;
return;
var form = httpContextAccessor.HttpContext?.Request?.Form; var form = HttpContextAccessor.HttpContext?.Request?.Form;
if (form == null || !form.Any()) if (form == null || form.Count == 0) return;
return;
var store = await storeContext.GetCurrentStoreAsync(); if (form.ContainsKey(nameof(IMeasurable.IsMeasurable)) && form.ContainsKey(nameof(IMeasuringNetWeight.NetWeight)))
// Save IsMeasurable
if (form.ContainsKey(nameof(IMeasuringAttributeValues.IsMeasurable)))
{ {
var isMeasurable = form[nameof(IMeasuringAttributeValues.IsMeasurable)].ToString().Contains("true"); var isMeasurable = form[nameof(IMeasurable.IsMeasurable)].ToString().Contains("true");
await genericAttributeService.SaveAttributeAsync(product, nameof(IMeasuringAttributeValues.IsMeasurable), isMeasurable, store.Id); //var isMeasurable = CommonHelper.To<bool>(form[nameof(IMeasurable.IsMeasurable)].ToString());
//Akkor ez kell? - Á. var netWeight = CommonHelper.To<double>(form[nameof(IMeasuringNetWeight.NetWeight)].ToString());
//await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(product.Id, 0, 0, isMeasurable, false);
}
// Save NetWeight await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(product.Id, netWeight, 0, isMeasurable, false);
if (form.ContainsKey(nameof(IMeasuringAttributeValues.NetWeight)))
{
var netWeightStr = form[nameof(IMeasuringAttributeValues.NetWeight)].ToString();
if (!string.IsNullOrWhiteSpace(netWeightStr) && double.TryParse(netWeightStr, NumberStyles.Float, CultureInfo.InvariantCulture, out var netWeight))
{
await genericAttributeService.SaveAttributeAsync(product, nameof(IMeasuringAttributeValues.NetWeight), netWeight, store.Id);
//Akkor ez kell? - Á.
//await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(product.Id, 0, 0, , false);
}
}
// Save IncomingQuantity
if (form.ContainsKey("IncomingQuantity"))
{
var incomingQtyStr = form["IncomingQuantity"].ToString();
if (!string.IsNullOrWhiteSpace(incomingQtyStr) && int.TryParse(incomingQtyStr, out var incomingQuantity))
{
await genericAttributeService.SaveAttributeAsync(product, "IncomingQuantity", incomingQuantity, store.Id);
}
} }
if (form.ContainsKey(nameof(ITare.Tare))) if (form.ContainsKey(nameof(ITare.Tare)))
{ {
var tareStr = form[nameof(ITare.Tare)].ToString(); var tare = CommonHelper.To<double>(form[nameof(ITare.Tare)].ToString());
if (!string.IsNullOrWhiteSpace(tareStr) && if (tare < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (tare < 0); productId: {product.Id}; tare: {tare}");
double.TryParse(tareStr, NumberStyles.Float, CultureInfo.InvariantCulture, out var tare))
{ await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(ITare.Tare), tare);
await genericAttributeService.SaveAttributeAsync(product, nameof(ITare.Tare), tare, store.Id); }
}
if (form.ContainsKey("IncomingQuantity"))
{
var incomingQuantity = CommonHelper.To<int>(form["IncomingQuantity"].ToString());
if (incomingQuantity < 0) throw new Exception($"FruitBankEventConsumer->SaveProductCustomAttributesAsync(); (incomingQuantity < 0); productId: {product.Id}; incomingQuantity: {incomingQuantity}");
await fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, int>(product.Id, "IncomingQuantity", incomingQuantity);
} }
} }

View File

@ -176,7 +176,8 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Factories
var orderListModelExtended = new OrderListModelExtended(); var orderListModelExtended = new OrderListModelExtended();
var extendedRows = new List<OrderModelExtended>(orderListModel.RecordsFiltered); var extendedRows = new List<OrderModelExtended>(orderListModel.RecordsFiltered);
PropertyHelper.CopyPublicProperties(orderListModel, orderListModelExtended); //TODO: Megnézni miért száll el az IEnumerable!!! - J.
//PropertyHelper.CopyPublicProperties(orderListModel, orderListModelExtended);
foreach (var orderModel in orderListModel.Data) foreach (var orderModel in orderListModel.Data)
{ {

View File

@ -19,6 +19,15 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute
return (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name)).SingleOrDefault(ga => ga.StoreId == storeId && ga.Key == key); return (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name)).SingleOrDefault(ga => ga.StoreId == storeId && ga.Key == key);
} }
public Task<TPropType?> GetGenericAttributeValueAsync<TEntity, TPropType>(int entityId, string key)
=> GetGenericAttributeValueAsync<TEntity, TPropType>(entityId, key, storeContext.GetCurrentStore().Id);
public async Task<TPropType?> GetGenericAttributeValueAsync<TEntity, TPropType>(int entityId, string key, int storeId)
{
var ga = await GetGenericAttributeAsync<TEntity>(entityId, key, storeId);
return ga == null ? default : CommonHelper.To<TPropType>(ga.Value);
}
public async Task<List<GenericAttribute>?> GetMeasuringAttributesAsync<TEntity>(int entityId, int storeId) public async Task<List<GenericAttribute>?> GetMeasuringAttributesAsync<TEntity>(int entityId, int storeId)
{ {
var measuringAttributes = (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name)) var measuringAttributes = (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name))