improvements, fixes, etc...

This commit is contained in:
Loretta 2025-10-06 07:44:56 +02:00
parent fae1e37946
commit 66c934e950
3 changed files with 76 additions and 23 deletions

View File

@ -22,6 +22,7 @@ using Nop.Services.Common;
using System.Transactions;
using FruitBank.Common.Dtos;
using Mango.Nop.Core.Dtos;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
@ -160,7 +161,8 @@ public class FruitBankDbContext : MgDbContextBase,
public Task<bool> UpdateMeasuredShippingItemSafeAsync(ShippingItem shippingItem)
{
if (shippingItem.IsValidMeasuringValues()) return UpdateShippingItemSafeAsync(shippingItem);
if (shippingItem.IsValidMeasuringValues() && shippingItem.ShippingItemPallets?.Count == shippingItem.PalletsOnDocument)
return UpdateShippingItemSafeAsync(shippingItem);
Logger.Error("shippingItem.IsMeasurable && !shippingItem.IsValidMeasuringValues()");
return Task.FromResult(false);
@ -195,15 +197,20 @@ public class FruitBankDbContext : MgDbContextBase,
shippingItem.IsMeasurable = productIsMeasurable;
if (shippingItem.MeasuredQuantity < 0) shippingItem.MeasuredQuantity = 0;
if (!shippingItem.IsMeasurable || shippingItem.MeasuredNetWeight < 0) shippingItem.MeasuredNetWeight = 0;
if (!shippingItem.IsMeasurable || shippingItem.MeasuredGrossWeight < 0) shippingItem.MeasuredGrossWeight = 0;
if (shippingItem.ShippingItemPallets is { Count: > 0 }) await AddOrUpdateShippingItemPalletAsync(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();
//Update előtt kivesszük a korábbi ShippingItem-et a db-ből! - J.
var dbShippingItem = await ShippingItems.GetByIdAsync(shippingItem.Id);
var dbShippingItem = await ShippingItems.GetByIdAsync(shippingItem.Id, false);
if (dbShippingItem == null) throw new Exception($"dbShippingItem == null; shippingItem.Id: {shippingItem.Id}");
shippingItem.IsMeasured = product != null && shippingItem.IsValidMeasuringValues();
var isMeasuredPrerequisite = product != null && shippingItem.PalletsOnDocument == shippingItem.ShippingItemPallets.Count
&& shippingItem.ShippingItemPallets.All(x => x.IsMeasured && x.IsValidMeasuringValues(shippingItem.IsMeasurable));
SetupShippingItemMeasuringValues(shippingItem, isMeasuredPrerequisite);
shippingItem.IsMeasured = isMeasuredPrerequisite && shippingItem.IsValidMeasuringValues();
await ShippingItems.UpdateAsync(shippingItem, shippingItem.IsMeasured != dbShippingItem.IsMeasured);
@ -218,23 +225,24 @@ public class FruitBankDbContext : MgDbContextBase,
return true;
}
var productIdUnchanged = shippingItem.ProductId == dbShippingItem.ProductId;
var productIdChanged = shippingItem.ProductId != dbShippingItem.ProductId;
if (shippingItem.IsMeasured)
{
product!.StockQuantity += productIdUnchanged ? shippingItem.MeasuredQuantity - dbShippingItem.MeasuredQuantity : shippingItem.MeasuredQuantity;
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>(product.Id,
productIdUnchanged ? shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight,
productIdUnchanged ? shippingItem.MeasuredGrossWeight - dbShippingItem.MeasuredGrossWeight : shippingItem.MeasuredGrossWeight,
productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight,
productIdChanged ? shippingItem.MeasuredGrossWeight : shippingItem.MeasuredGrossWeight - dbShippingItem.MeasuredGrossWeight,
shippingItem.IsMeasurable, true);
}
if (productIdUnchanged || !dbShippingItem.IsMeasured) return true;
//if (productIdUnchanged || !dbShippingItem.IsMeasured) return true;
if (!productIdChanged && (shippingItem.IsMeasured || !dbShippingItem.IsMeasured)) return true;
product = await Products.GetByIdAsync(dbShippingItem.ProductId);
@ -264,9 +272,31 @@ public class FruitBankDbContext : MgDbContextBase,
});
}
private static void SetupShippingItemMeasuringValues(ShippingItem shippingItem, bool shippingItemIsMeasured)
{
shippingItem.MeasuredQuantity = 0;
shippingItem.MeasuredNetWeight = 0;
shippingItem.MeasuredGrossWeight = 0;
if (!shippingItemIsMeasured) return;
foreach (var shippingItemPallet in shippingItem.ShippingItemPallets!.Where(x => x.IsMeasured && x.IsValidMeasuringValues(shippingItem.IsMeasurable)))
{
shippingItem.MeasuredQuantity += shippingItemPallet.Quantity;
if (!shippingItem.IsMeasurable) continue;
shippingItem.MeasuredNetWeight += shippingItemPallet.NetWeight;
shippingItem.MeasuredGrossWeight += shippingItemPallet.GrossWeight;
}
//if (shippingItem.MeasuredQuantity < 0) shippingItem.MeasuredQuantity = 0;
//if (!shippingItem.IsMeasurable || shippingItem.MeasuredNetWeight < 0) shippingItem.MeasuredNetWeight = 0;
//if (!shippingItem.IsMeasurable || shippingItem.MeasuredGrossWeight < 0) shippingItem.MeasuredGrossWeight = 0;
}
public async Task<ShippingItemPallet?> AddShippingItemPalletAsync(ShippingItemPallet shippingItemPallet)
{
if (!await ShippingItemPalletSetup(shippingItemPallet)) return null;
if (!await SetupShippingItemPalletMeauringValues(shippingItemPallet)) return null;
await ShippingItemPallets.InsertAsync(shippingItemPallet);
return shippingItemPallet;
@ -274,16 +304,37 @@ public class FruitBankDbContext : MgDbContextBase,
public async Task<ShippingItemPallet?> UpdateShippingItemPalletAsync(ShippingItemPallet shippingItemPallet)
{
if (!await ShippingItemPalletSetup(shippingItemPallet)) return null;
if (!await SetupShippingItemPalletMeauringValues(shippingItemPallet)) return null;
await ShippingItemPallets.UpdateAsync(shippingItemPallet);
return shippingItemPallet;
}
private async Task<bool> ShippingItemPalletSetup(ShippingItemPallet shippingItemPallet)
public async Task AddOrUpdateShippingItemPalletAsync(List<ShippingItemPallet> shippingItemPallets, ShippingItem parentShippingItem)
{
var shippingItem = await ShippingItems.GetByIdAsync(shippingItemPallet.ShippingItemId, false);
if (shippingItem == null) return false;
foreach (var shippingItemPallet in shippingItemPallets)
{
shippingItemPallet.ShippingItem = parentShippingItem;
if (await AddOrUpdateShippingItemPalletAsync(shippingItemPallet) == null)
throw new Exception($"AddOrUpdateShippingItemPalletAsync->AddOrUpdateShippingItemPalletAsync() == null");
}
}
public Task AddOrUpdateShippingItemPalletAsync(ShippingItem shippingItem)
=> AddOrUpdateShippingItemPalletAsync(shippingItem.ShippingItemPallets!, shippingItem);
public async Task<ShippingItemPallet?> AddOrUpdateShippingItemPalletAsync(ShippingItemPallet shippingItemPallet)
{
if (shippingItemPallet.Id <= 0) return await AddShippingItemPalletAsync(shippingItemPallet);
return await UpdateShippingItemPalletAsync(shippingItemPallet);
}
private async Task<bool> SetupShippingItemPalletMeauringValues(ShippingItemPallet shippingItemPallet)
{
var shippingItem = shippingItemPallet.ShippingItem ?? await ShippingItems.GetByIdAsync(shippingItemPallet.ShippingItemId, false);
if (shippingItem == null || shippingItemPallet.ShippingItemId != shippingItem.Id) return false;
if (!shippingItem.IsMeasurable)
{
@ -291,6 +342,8 @@ public class FruitBankDbContext : MgDbContextBase,
shippingItemPallet.GrossWeight = 0;
}
if (shippingItemPallet.Quantity < 0 || shippingItemPallet.NetWeight < 0 || shippingItemPallet.GrossWeight < 0) return false;
shippingItemPallet.IsMeasured = shippingItemPallet.IsValidMeasuringValues(shippingItem.IsMeasurable);
return true;
}

View File

@ -39,7 +39,7 @@ public class ShippingItemDbTable : MgDbTableBase<ShippingItem>
.LoadWith(si => si.ShippingDocument).ThenLoad(sd => sd.ShippingDocumentToFiles).ThenLoad(sdtof => sdtof.ShippingDocumentFile)
.LoadWith(si => si.ShippingItemPallets)
.LoadWith(si => si.Product)
: GetAll();
: GetAll().LoadWith(si => si.ShippingItemPallets).LoadWith(si => si.Product);
}
public IQueryable<ShippingItem> GetAllNotMeasured(bool loadRelations)

View File

@ -51,13 +51,13 @@ public class FruitBankEventConsumer(IHttpContextAccessor httpContextAccessor, Fr
Logger.Info($"HandleEventAsync EntityUpdatedEvent<ShippingItem>; id: {eventMessage.Entity.Id}");
var shippingItem = eventMessage.Entity;
var isMeasured = shippingItem.IsValidMeasuringValues();
//var isMeasured = shippingItem.IsValidMeasuringValues();
if (shippingItem.IsMeasured != isMeasured)
{
shippingItem.IsMeasured = isMeasured;
await ctx.ShippingItems.UpdateAsync(shippingItem, false);
}
//if (shippingItem.IsMeasured != isMeasured)
//{
// shippingItem.IsMeasured = isMeasured;
// await ctx.ShippingItems.UpdateAsync(shippingItem, false);
//}
await UpdateShippingDocumentIsAllMeasuredAsync(shippingItem);
}