diff --git a/Nop.Plugin.Misc.AIPlugin/Services/MeasurementService.cs b/Nop.Plugin.Misc.AIPlugin/Services/MeasurementService.cs index 3ed14c6..6333f60 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/MeasurementService.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/MeasurementService.cs @@ -158,26 +158,31 @@ public class MeasurementService : MeasurementServiceBase, IMeasurementSe { double averageWeight = 0; + + //TODO if ismeasurable if (shippingItemPallet.IsValidMeasuringValues(shippingItemPallet.ShippingItem.IsMeasurable)) { //this is only for the current pallet, the average weight should be calculated from all pallets of the product, //so we check if there are any pallets with valid measuring values and calculate the average weight from them, //otherwise we use the current pallet's weight as the average weight - - var allPalletsInThisShippingItem = shippingItemPallet.ShippingItem.ShippingItemPallets; + var shippingItemPallets = await _dbContext.ShippingItemPallets.GetAllByShippingItemIdAsync(shippingItemPallet.ShippingItemId, false).ToListAsync(); + var allOtherPalletsInThisShippingItem = await shippingItemPallets.Where(sp => sp.Id != shippingItemPallet.Id).ToListAsync(); //we need an object to store the netweight and trayquantity for average calculation var validPalletsForAverageCalculation = new List<(double NetWeight, int TrayQuantity)>(); - - //get all pallets with valid measuring values - for (int i = 0; i < allPalletsInThisShippingItem.Count; i++) + if(allOtherPalletsInThisShippingItem.Count != 0) { - if (allPalletsInThisShippingItem[i].IsValidMeasuringValues(shippingItemPallet.ShippingItem.IsMeasurable)) + //get all pallets with valid measuring values + for (int i = 0; i < allOtherPalletsInThisShippingItem.Count; i++) { - validPalletsForAverageCalculation.Add((allPalletsInThisShippingItem[i].NetWeight, allPalletsInThisShippingItem[i].TrayQuantity)); + if (allOtherPalletsInThisShippingItem[i].IsValidMeasuringValues(shippingItemPallet.ShippingItem.IsMeasurable)) + { + validPalletsForAverageCalculation.Add((allOtherPalletsInThisShippingItem[i].NetWeight, allOtherPalletsInThisShippingItem[i].TrayQuantity)); + } } } + //add current pallet to the valid pallets if it has valid measuring values @@ -188,8 +193,12 @@ public class MeasurementService : MeasurementServiceBase, IMeasurementSe var totalTrayQuantity = validPalletsForAverageCalculation.Sum(x => x.TrayQuantity); var TotalAverageWeight = totalTrayQuantity > 0 ? totalNetWeight / totalTrayQuantity : 0; - + averageWeight = TotalAverageWeight; } + + //TODO ELSE - we have to get the net weight from the document, not from measuring + + await SetProductAverageWeight(productDto, averageWeight); await _dbContext.ProductDtos.UpdateAsync(productDto); } @@ -198,6 +207,6 @@ public class MeasurementService : MeasurementServiceBase, IMeasurementSe public async Task SetProductAverageWeight(ProductDto productDto, double averageWeight) { - await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(productDto.Id, nameof(IProductDto.AverageWeight), averageWeight); + await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(productDto.Id, nameof(IProductDto.AverageWeight), averageWeight); } } \ No newline at end of file