|
|
|
|
@ -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,6 +272,82 @@ 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 SetupShippingItemPalletMeauringValues(shippingItemPallet)) return null;
|
|
|
|
|
|
|
|
|
|
await ShippingItemPallets.InsertAsync(shippingItemPallet);
|
|
|
|
|
return shippingItemPallet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ShippingItemPallet?> UpdateShippingItemPalletAsync(ShippingItemPallet shippingItemPallet)
|
|
|
|
|
{
|
|
|
|
|
if (!await SetupShippingItemPalletMeauringValues(shippingItemPallet)) return null;
|
|
|
|
|
|
|
|
|
|
await ShippingItemPallets.UpdateAsync(shippingItemPallet);
|
|
|
|
|
return shippingItemPallet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AddOrUpdateShippingItemPalletAsync(List<ShippingItemPallet> shippingItemPallets, ShippingItem parentShippingItem)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
shippingItemPallet.NetWeight = 0;
|
|
|
|
|
shippingItemPallet.GrossWeight = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shippingItemPallet.Quantity < 0 || shippingItemPallet.NetWeight < 0 || shippingItemPallet.GrossWeight < 0) return false;
|
|
|
|
|
|
|
|
|
|
shippingItemPallet.IsMeasured = shippingItemPallet.IsValidMeasuringValues(shippingItem.IsMeasurable);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<bool> UpdateProductStockQuantityAsync(int productId, bool publishEvent)
|
|
|
|
|
{
|
|
|
|
|
var product = await Products.GetByIdAsync(productId);
|
|
|
|
|
|