diff --git a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs index 3db022c..9f56f9f 100644 --- a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs @@ -15,6 +15,7 @@ using Mango.Nop.Core.Models; using Nop.Core; using Nop.Core.Domain.Customers; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; +using Nop.Plugin.Misc.FruitBankPlugin.Factories; using Nop.Services.Customers; using Nop.Services.Localization; using Nop.Web.Framework.Controllers; @@ -154,7 +155,8 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers _logger.Detail($"AddShippingItemPallet invoked; {shippingItemPallet}"); - return await ctx.AddShippingItemPalletAsync(shippingItemPallet); + if (!await ctx.AddShippingItemPalletSafeAsync(shippingItemPallet)) return null; + return await ctx.ShippingItemPallets.GetByIdAsync(shippingItemPallet.Id, false); } [SignalR(SignalRTags.UpdateShippingItemPallet)] @@ -164,25 +166,39 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers _logger.Detail($"UpdateShippingItemPallet invoked; {shippingItemPallet}"); - return await ctx.UpdateShippingItemPalletAsync(shippingItemPallet); + if (!await ctx.UpdateShippingItemPalletSafeAsync(shippingItemPallet)) return null; + return await ctx.ShippingItemPallets.GetByIdAsync(shippingItemPallet.Id, false); } [SignalR(SignalRTags.AddOrUpdateMeasuredShippingItemPallets)] public async Task AddOrUpdateMeasuredShippingItemPallets(List shippingItemPallets) { - ArgumentNullException.ThrowIfNull(shippingItemPallets); + // ArgumentNullException.ThrowIfNull(shippingItemPallets); - _logger.Detail($"AddOrUpdateMeasuredShippingItemPallets invoked; count: {shippingItemPallets.Count}"); + // _logger.Detail($"AddOrUpdateMeasuredShippingItemPallets invoked; count: {shippingItemPallets.Count}"); - if (shippingItemPallets.Count == 0) return null; + // if (shippingItemPallets.Count == 0) return null; - var shippingItemId = shippingItemPallets.FirstOrDefault()!.ShippingItemId; - if (shippingItemId <= 0 || shippingItemPallets.Any(x => x.ShippingItemId != shippingItemId)) return null; + // var shippingItemId = shippingItemPallets.FirstOrDefault()!.ShippingItemId; + // if (shippingItemId <= 0 || shippingItemPallets.Any(x => x.ShippingItemId != shippingItemId)) return null; - var shippingItem = await ctx.ShippingItems.GetByIdAsync(shippingItemId, false); - shippingItem.ShippingItemPallets = shippingItemPallets.Where(sip => sip.IsValidMeasuringValues(shippingItem.IsMeasurable)).ToList(); + // var shippingItem = await ctx.ShippingItems.GetByIdAsync(shippingItemId, false); + // shippingItem.ShippingItemPallets = shippingItemPallets.Where(sip => sip.IsValidMeasuringValues(shippingItem.IsMeasurable)).ToList(); - return await UpdateMeasuredShippingItem(shippingItem); + // return await UpdateMeasuredShippingItem(shippingItem); + + return null; + } + + [SignalR(SignalRTags.AddOrUpdateMeasuredShippingItemPallet)] + public async Task AddOrUpdateMeasuredShippingItemPallet(ShippingItemPallet shippingItemPallet) + { + ArgumentNullException.ThrowIfNull(shippingItemPallet); + + _logger.Detail($"AddOrUpdateMeasuredShippingItemPallet invoked; {shippingItemPallet}"); + + if (!await ctx.AddOrUpdateShippingItemPalletSafeAsync(shippingItemPallet)) return null; + return await ctx.ShippingItemPallets.GetByIdAsync(shippingItemPallet.Id, false); } [SignalR(SignalRTags.GetShippingDocuments)] diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs index f0e3278..514892e 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs @@ -32,6 +32,7 @@ public class FruitBankDbContext : MgDbContextBase, IShippingDocumentDbSet, IShippingItemDbSet, IShippingItemPalletDbSet, + IOrderItemPalletDbSet, IShippingDocumentToFilesDbSet, IFilesDbSet { @@ -46,6 +47,8 @@ public class FruitBankDbContext : MgDbContextBase, public ShippingItemDbTable ShippingItems { get; set; } public ShippingItemPalletDbTable ShippingItemPallets { get; set; } + public OrderItemPalletDbTable OrderItemPallets { get; set; } + public FilesDbTable Files { get; set; } public ShippingDocumentToFilesDbTable ShippingDocumentToFiles { get; set; } @@ -53,7 +56,7 @@ public class FruitBankDbContext : MgDbContextBase, public IRepository Customers { get; set; } public IRepository CustomerRoles { get; set; } public IRepository CustomerRoleMappings { get; set; } - + public FruitBankDbContext(INopDataProvider dataProvider, ILockService lockService, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext, PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable, ShippingItemPalletDbTable shippingItemPalletDbTable, FilesDbTable filesDbTable, ShippingDocumentToFilesDbTable shippingDocumentToFilesDbTable, @@ -150,15 +153,6 @@ public class FruitBankDbContext : MgDbContextBase, }); } - public async Task DeleteShippingItemPalletSafeAsync(ShippingItemPallet shippingItemPallet) - { - await TransactionSafeAsync(async tr => - { - await ShippingItemPallets.DeleteAsync(shippingItemPallet, false); - return true; - }); - } - public Task UpdateMeasuredShippingItemSafeAsync(ShippingItem shippingItem) { if (shippingItem.IsValidMeasuringValues()) return UpdateShippingItemSafeAsync(shippingItem); @@ -168,108 +162,101 @@ public class FruitBankDbContext : MgDbContextBase, } public Task UpdateShippingItemSafeAsync(ShippingItem shippingItem) + => TransactionSafeAsync(async _ => await UpdateShippingItemAsync(shippingItem)); + + public async Task UpdateShippingItemAsync(ShippingItem shippingItem) { - if (shippingItem == null) + try { - Logger.Error("shippingItem == null"); - return Task.FromResult(false); - } + Product? product = null; + var productIsMeasurable = false; - return TransactionSafeAsync(async tr => - { - try + if (shippingItem.ProductId > 0) { - //Mi van ha nem jött meg a termék? Nem fogják tudni menteni... - J. + product = await Products.GetByIdAsync(shippingItem.ProductId); - Product? product = null; - var productIsMeasurable = false; + if (product == null) + throw new Exception($"shippingItem.ProductId > 0 && product == null; shippingItem.ProductId: {shippingItem.ProductId}"); - if (shippingItem.ProductId > 0) - { - product = await Products.GetByIdAsync(shippingItem.ProductId); + productIsMeasurable = await _fruitBankAttributeService.IsMeasurableEntityAsync(product.Id); + } - if (product == null) - throw new Exception($"shippingItem.ProductId > 0 && product == null; shippingItem.ProductId: {shippingItem.ProductId}"); + shippingItem.IsMeasurable = productIsMeasurable; - productIsMeasurable = await _fruitBankAttributeService.IsMeasurableEntityAsync(product.Id); - } + //if (shippingItem.ShippingItemPallets is { Count: > 0 }) await AddOrUpdateShippingItemPalletAsync(shippingItem); - shippingItem.IsMeasurable = productIsMeasurable; - - if (shippingItem.ShippingItemPallets is { Count: > 0 }) await AddOrUpdateShippingItemPalletAsync(shippingItem); - //await AddOrUpdateShippingItemPalletAsync(shippingItem.ShippingItemPallets.Where(x => x.IsValidMeasuringValues(shippingItem.IsMeasurable)), shippingItem); + //await AddOrUpdateShippingItemPalletAsync(shippingItem.ShippingItemPallets.Where(x => x.IsValidMeasuringValues(shippingItem.IsMeasurable)), shippingItem); - //Nem tudhatjuk, h minden Pallet-et tartalmaz-e a shippingItem paraméter! A Biztonság kedvéért lekérjük db-ből! - J. - shippingItem.ShippingItemPallets = await ShippingItemPallets.GetAllByShippingItemIdAsync(shippingItem.Id, false).ToListAsync(); + //Nem tudhatjuk, h minden Pallet-et tartalmaz-e a shippingItem paraméter! A Biztonság kedvéért lekérjük db-ből! - J. + shippingItem.ShippingItemPallets = await ShippingItemPallets.GetAllByShippingItemIdAsync(shippingItem.Id, false).ToListAsync(); - //Update előtt kivesszük a korábbi ShippingItem-et a db-ből! - J. - var dbShippingItem = await ShippingItems.GetByIdAsync(shippingItem.Id, false); - if (dbShippingItem == null) throw new Exception($"dbShippingItem == null; shippingItem.Id: {shippingItem.Id}"); + //Update előtt kivesszük a korábbi ShippingItem-et a db-ből! - J. + var dbShippingItem = await ShippingItems.GetByIdAsync(shippingItem.Id, false); + if (dbShippingItem == null) throw new Exception($"dbShippingItem == null; shippingItem.Id: {shippingItem.Id}"); - var isMeasuredPrerequisite = product != null && shippingItem.PalletsOnDocument == shippingItem.ShippingItemPallets.Count - && shippingItem.ShippingItemPallets.All(x => x.IsMeasuredAndValid(shippingItem.IsMeasurable)); + var isMeasuredPrerequisite = product != null && shippingItem.PalletsOnDocument == shippingItem.ShippingItemPallets.Count + && shippingItem.ShippingItemPallets.All(x => x.IsMeasuredAndValid(shippingItem.IsMeasurable)); - SetupShippingItemMeasuringValues(shippingItem, isMeasuredPrerequisite); - shippingItem.IsMeasured = isMeasuredPrerequisite && shippingItem.IsValidMeasuringValues(); + SetupShippingItemMeasuringValues(shippingItem, isMeasuredPrerequisite); + shippingItem.IsMeasured = isMeasuredPrerequisite && shippingItem.IsValidMeasuringValues(); - await ShippingItems.UpdateAsync(shippingItem, shippingItem.IsMeasured != dbShippingItem.IsMeasured); - - if (shippingItem.ProductId == dbShippingItem.ProductId && - shippingItem.IsMeasured == dbShippingItem.IsMeasured && shippingItem.IsMeasurable == dbShippingItem.IsMeasurable && - shippingItem.MeasuredQuantity == dbShippingItem.MeasuredQuantity && - // ReSharper disable once CompareOfFloatsByEqualityOperator - shippingItem.MeasuredNetWeight == dbShippingItem.MeasuredNetWeight && - // ReSharper disable once CompareOfFloatsByEqualityOperator - shippingItem.MeasuredGrossWeight == dbShippingItem.MeasuredGrossWeight) - { - return true; - } - - var productIdChanged = shippingItem.ProductId != dbShippingItem.ProductId; - - if (shippingItem.IsMeasured) - { - product!.StockQuantity += productIdChanged ? shippingItem.MeasuredQuantity : shippingItem.MeasuredQuantity - dbShippingItem.MeasuredQuantity; - - if (!await UpdateProductStockQuantityAsync(product, true)) - throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {product.Id}"); - - if (productIsMeasurable) - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, - productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight, - productIdChanged ? shippingItem.MeasuredGrossWeight : shippingItem.MeasuredGrossWeight - dbShippingItem.MeasuredGrossWeight, - shippingItem.IsMeasurable, true); - } - - //if (productIdUnchanged || !dbShippingItem.IsMeasured) return true; - if (!productIdChanged && (shippingItem.IsMeasured || !dbShippingItem.IsMeasured)) return true; - - product = await Products.GetByIdAsync(dbShippingItem.ProductId); - - if (product != null) - { - productIsMeasurable = await _fruitBankAttributeService.IsMeasurableEntityAsync(product.Id); - product.StockQuantity -= dbShippingItem.MeasuredQuantity; - - if (!await UpdateProductStockQuantityAsync(product, true)) - throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {product.Id}"); - - if (!productIsMeasurable) return true; - - var measuringValues = new MeasuringAttributeValues(product.Id, -dbShippingItem.MeasuredNetWeight, -dbShippingItem.MeasuredGrossWeight, dbShippingItem.IsMeasurable); - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(measuringValues, true); - - } - else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}"); - //else //TODO: productIdUnchanged-et lekezelni! - J. + await ShippingItems.UpdateAsync(shippingItem, shippingItem.IsMeasured != dbShippingItem.IsMeasured); + if (shippingItem.ProductId == dbShippingItem.ProductId && + shippingItem.IsMeasured == dbShippingItem.IsMeasured && shippingItem.IsMeasurable == dbShippingItem.IsMeasurable && + shippingItem.MeasuredQuantity == dbShippingItem.MeasuredQuantity && + // ReSharper disable once CompareOfFloatsByEqualityOperator + shippingItem.MeasuredNetWeight == dbShippingItem.MeasuredNetWeight && + // ReSharper disable once CompareOfFloatsByEqualityOperator + shippingItem.MeasuredGrossWeight == dbShippingItem.MeasuredGrossWeight) + { return true; } - catch (Exception ex) + + var productIdChanged = shippingItem.ProductId != dbShippingItem.ProductId; + + if (shippingItem.IsMeasured) { - throw new Exception($"UpdateShippingItemSafeAsync->TransactionSafeAsync error! shippingItem.Id: {shippingItem.Id}; ex: {ex.Message}", ex); + product!.StockQuantity += productIdChanged ? shippingItem.MeasuredQuantity : shippingItem.MeasuredQuantity - dbShippingItem.MeasuredQuantity; + + if (!await UpdateProductStockQuantityAsync(product, true)) + throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {product.Id}"); + + if (productIsMeasurable) + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, + productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight, + productIdChanged ? shippingItem.MeasuredGrossWeight : shippingItem.MeasuredGrossWeight - dbShippingItem.MeasuredGrossWeight, + shippingItem.IsMeasurable, true); } - }); + + //if (productIdUnchanged || !dbShippingItem.IsMeasured) return true; + if (!productIdChanged && (shippingItem.IsMeasured || !dbShippingItem.IsMeasured)) return true; + + product = await Products.GetByIdAsync(dbShippingItem.ProductId); + + if (product != null) + { + productIsMeasurable = await _fruitBankAttributeService.IsMeasurableEntityAsync(product.Id); + product.StockQuantity -= dbShippingItem.MeasuredQuantity; + + if (!await UpdateProductStockQuantityAsync(product, true)) + throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {product.Id}"); + + if (!productIsMeasurable) return true; + + var measuringValues = new MeasuringAttributeValues(product.Id, -dbShippingItem.MeasuredNetWeight, -dbShippingItem.MeasuredGrossWeight, dbShippingItem.IsMeasurable); + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(measuringValues, true); + + } + else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}"); + //else //TODO: productIdUnchanged-et lekezelni! - J. + + return true; + } + catch (Exception ex) + { + throw new Exception($"UpdateShippingItemAsync error! {shippingItem}; ex: {ex.Message}", ex); + } } private static void SetupShippingItemMeasuringValues(ShippingItem shippingItem, bool shippingItemIsMeasured) @@ -342,8 +329,8 @@ public class FruitBankDbContext : MgDbContextBase, }); } - public Task AddOrUpdateShippingItemPalletSafeAsync(ShippingItem shippingItem) => AddOrUpdateShippingItemPalletSafeAsync(shippingItem.ShippingItemPallets!, shippingItem); - public Task AddOrUpdateShippingItemPalletSafeAsync(IEnumerable shippingItemPallets, ShippingItem parentShippingItem) + public Task AddOrUpdateShippingItemPalletsSafeAsync(ShippingItem shippingItem) => AddOrUpdateShippingItemPalletsSafeAsync(shippingItem.ShippingItemPallets!, shippingItem); + public Task AddOrUpdateShippingItemPalletsSafeAsync(IEnumerable shippingItemPallets, ShippingItem parentShippingItem) { return TransactionSafeAsync(async tr => { @@ -354,7 +341,7 @@ public class FruitBankDbContext : MgDbContextBase, } catch (Exception ex) { - throw new Exception($"AddOrUpdateShippingItemPalletSafeAsync->TransactionSafeAsync error! ex: {ex.Message}", ex); + throw new Exception($"AddOrUpdateShippingItemPalletsSafeAsync->TransactionSafeAsync error! ex: {ex.Message}", ex); } }); } @@ -370,6 +357,8 @@ public class FruitBankDbContext : MgDbContextBase, throw new Exception($"AddOrUpdateShippingItemPalletAsync->AddOrUpdateShippingItemPalletAsync() == null"); } } + public async Task AddOrUpdateShippingItemPalletSafeAsync(ShippingItemPallet shippingItemPallet) + => await TransactionSafeAsync(async tr => await AddOrUpdateShippingItemPalletAsync(shippingItemPallet) != null); public async Task AddOrUpdateShippingItemPalletAsync(ShippingItemPallet shippingItemPallet) { @@ -378,6 +367,15 @@ public class FruitBankDbContext : MgDbContextBase, return await UpdateShippingItemPalletAsync(shippingItemPallet); } + public async Task DeleteShippingItemPalletSafeAsync(ShippingItemPallet shippingItemPallet) + { + await TransactionSafeAsync(async tr => + { + await ShippingItemPallets.DeleteAsync(shippingItemPallet, false); + return true; + }); + } + private async Task SetupShippingItemPalletMeauringValues(ShippingItemPallet shippingItemPallet) { var shippingItem = shippingItemPallet.ShippingItem ?? await ShippingItems.GetByIdAsync(shippingItemPallet.ShippingItemId, false); @@ -385,6 +383,7 @@ public class FruitBankDbContext : MgDbContextBase, if (!shippingItem.IsMeasurable) { + shippingItemPallet.TareWeight = 0; shippingItemPallet.PalletWeight = 0; shippingItemPallet.GrossWeight = 0; } diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/Interfaces/IOrderItemPalletDbSet.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/Interfaces/IOrderItemPalletDbSet.cs new file mode 100644 index 0000000..da94aa6 --- /dev/null +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/Interfaces/IOrderItemPalletDbSet.cs @@ -0,0 +1,10 @@ +using FruitBank.Common.Entities; +using Mango.Nop.Core.Interfaces; +using Nop.Data; + +namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces; + +public interface IOrderItemPalletDbSet : IMgDbTableBase where TDbTable : IRepository +{ + public TDbTable OrderItemPallets { get; set; } +} \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/MeasuringItemPalletBaseDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/MeasuringItemPalletBaseDbTable.cs new file mode 100644 index 0000000..ffa1279 --- /dev/null +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/MeasuringItemPalletBaseDbTable.cs @@ -0,0 +1,21 @@ +using FruitBank.Common.Interfaces; +using Mango.Nop.Core.Entities; +using Mango.Nop.Core.Repositories; +using Nop.Core.Caching; +using Nop.Core.Configuration; +using Nop.Core.Events; +using Nop.Data; +using Nop.Services.Logging; + +namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; + +public class MeasuringItemPalletBaseDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings, ILogger logger) : MgDbTableBase(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger) where TEntity + : MgEntityBase, IMeasuringItemPalletBase +{ + protected virtual void BeforeInsertOrUpdate(IMeasuringItemPalletBase measuringItemPalletBase) + { + if (measuringItemPalletBase.IsValidSafeMeasuringValues()) return; + + throw new Exception($"MeasuringItemPalletBaseDbTable->BeforeInsertOrUpdate(); Invalid measuring value(s); {measuringItemPalletBase}"); + } +} \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemPalletDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemPalletDbTable.cs new file mode 100644 index 0000000..43a8c48 --- /dev/null +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemPalletDbTable.cs @@ -0,0 +1,44 @@ +using FruitBank.Common.Entities; +using LinqToDB; +using Nop.Core.Caching; +using Nop.Core.Configuration; +using Nop.Core.Events; +using Nop.Data; +using Nop.Services.Logging; + +namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; + +public class OrderItemPalletDbTable : MeasuringItemPalletBaseDbTable +{ + public OrderItemPalletDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings, ILogger logger) + : base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger) + { + } + + protected override void OnUpdate(OrderItemPallet entity) + { + BeforeInsertOrUpdate(entity); + base.OnUpdate(entity); + } + + protected override void OnInsert(OrderItemPallet entity) + { + BeforeInsertOrUpdate(entity); + base.OnInsert(entity); + } + + public override IQueryable GetAll() => base.GetAll(); + + public IQueryable GetAll(bool loadRelations) + { + return loadRelations + ? GetAll().LoadWith(oip => oip.OrderItem) + : GetAll(); + } + + public Task GetByIdAsync(int id, bool loadRelations) + => GetAll(loadRelations).FirstOrDefaultAsync(oip => oip.Id == id); + + public IQueryable GetAllByOrderItemIdAsync(int orderItemId, bool loadRelations) + => GetAll(loadRelations).Where(oip => oip.OrderItemId == orderItemId); +} \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemPalletDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemPalletDbTable.cs index e23fc54..a99e61e 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemPalletDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemPalletDbTable.cs @@ -9,7 +9,7 @@ using Nop.Services.Logging; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; -public class ShippingItemPalletDbTable : MgDbTableBase +public class ShippingItemPalletDbTable : MeasuringItemPalletBaseDbTable { public ShippingItemPalletDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings, ILogger logger) : base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger) @@ -47,11 +47,4 @@ public class ShippingItemPalletDbTable : MgDbTableBase public IQueryable GetAllByShippingItemIdAsync(int shippingItemId, bool loadRelations) => GetAll(loadRelations).Where(sip => sip.ShippingItemId == shippingItemId); - - private static void BeforeInsertOrUpdate(ShippingItemPallet shippingItemPallet) - { - if (shippingItemPallet.IsValidSafeMeasuringValues()) return; - - throw new Exception($"ShippingItemPalletDbTable->BeforeInsertOrUpdate(); Invalid measuring value(s); shippingItemPallet: {shippingItemPallet}"); - } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs index 6c7f39b..5c79013 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs @@ -1,6 +1,5 @@ using AyCode.Core.Loggers; using AyCode.Interfaces.Entities; -using DevExpress.XtraPrinting.Native; using FruitBank.Common.Entities; using FruitBank.Common.Interfaces; using FruitBank.Common.Loggers; @@ -19,15 +18,22 @@ using Nop.Services.Events; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.EventConsumers; -public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext, IEnumerable logWriters, IGenericAttributeService genericAttributeService) : - MgEventConsumer(httpContextAccessor, logWriters), IConsumer>, IConsumer> +public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext, IEnumerable logWriters) : + MgEventConsumer(httpContextAccessor, logWriters), + IConsumer>, + IConsumer>, + IConsumer>, + IConsumer>, + IConsumer>, + IConsumer>, + IConsumer>, + IConsumer>, + IConsumer>, + IConsumer> { public override async Task HandleEventAsync(EntityUpdatedEvent eventMessage) { var product = eventMessage.Entity; - - await SaveCustomAttributesAsync(eventMessage.Entity); - var isMeasurableProduct = await fruitBankAttributeService.IsMeasurableEntityAsync(product.Id); var shippingItems = await ctx.ShippingItems.Table @@ -41,58 +47,36 @@ public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, Fr await base.HandleEventAsync(eventMessage); } - - public async Task HandleEventAsync(EntityInsertedEvent eventMessage) + public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { - await SaveCustomAttributesAsync(eventMessage.Entity); + Logger.Info($"HandleEventAsync EntityInsertedEvent; id: {eventMessage.Entity.Id}"); + + await UpdateShippingItemMeasuringValuesAsync(eventMessage.Entity); } - private async Task SaveCustomAttributesAsync(Product product) + public async Task HandleEventAsync(EntityUpdatedEvent eventMessage) { - if (product == null) - return; + Logger.Info($"HandleEventAsync EntityUpdatedEvent; id: {eventMessage.Entity.Id}"); - var form = httpContextAccessor.HttpContext?.Request?.Form; - if (form == null || !form.Any()) - return; - - var isMeasurable = form["IsMeasurable"].ToString().Contains("true"); - - // Save IsMeasurable - if (form.ContainsKey("IsMeasurable")) - { - await genericAttributeService.SaveAttributeAsync(product, "IsMeasurable", isMeasurable); - //Akkor ez kell? - Á. - //await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, 0, 0, isMeasurable, false); - } - - // Save NetWeight - if (form.ContainsKey("NetWeight")) - { - var netWeightStr = form["NetWeight"].ToString(); - if (!string.IsNullOrWhiteSpace(netWeightStr) && decimal.TryParse(netWeightStr, out var netWeight)) - { - await genericAttributeService.SaveAttributeAsync(product, "NetWeight", netWeight); - - //await fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(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); - } - } + await UpdateShippingItemMeasuringValuesAsync(eventMessage.Entity); } + public async Task HandleEventAsync(EntityDeletedEvent eventMessage) + { + Logger.Info($"HandleEventAsync EntityDeletedEvent; id: {eventMessage.Entity.Id}"); + + await UpdateShippingItemMeasuringValuesAsync(eventMessage.Entity); + } + + private async Task UpdateShippingItemMeasuringValuesAsync(ShippingItemPallet shippingItemPallet) + { + var shippingItem = await ctx.ShippingItems.GetByIdAsync(shippingItemPallet.ShippingItemId, false); + await ctx.UpdateShippingItemAsync(shippingItem); + } public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { - Logger.Info($"HandleEventAsync EntityInsertedEvent; id: {eventMessage.Entity.Id}"); + Logger.Info($"HandleEventAsync EntityInsertedEvent; id: {eventMessage.Entity.Id}"); await UpdateShippingDocumentIsAllMeasuredAsync(eventMessage.Entity); } diff --git a/Nop.Plugin.Misc.AIPlugin/Mapping/NameCompatibility.cs b/Nop.Plugin.Misc.AIPlugin/Mapping/NameCompatibility.cs index dbd82f3..ef598ad 100644 --- a/Nop.Plugin.Misc.AIPlugin/Mapping/NameCompatibility.cs +++ b/Nop.Plugin.Misc.AIPlugin/Mapping/NameCompatibility.cs @@ -18,6 +18,7 @@ public partial class NameCompatibility : INameCompatibility { typeof(ShippingDocument), FruitBankConstClient.ShippingDocumentDbTableName }, { typeof(ShippingItem), FruitBankConstClient.ShippingItemDbTableName}, { typeof(ShippingItemPallet), FruitBankConstClient.ShippingItemPalletDbTableName}, + { typeof(OrderItemPallet), FruitBankConstClient.OrderItemPalletDbTableName}, { typeof(ShippingDocumentToFiles), FruitBankConstClient.ShippingDocumentToFilesDbTableName}, };