Compare commits

..

No commits in common. "f2e99f30506b3eb1f6cb176631920861a54dda29" and "f72a031a7e78acb510b0a018213b38b0702f4378" have entirely different histories.

2 changed files with 43 additions and 141 deletions

View File

@ -6,18 +6,15 @@ using FruitBank.Common.Dtos;
using FruitBank.Common.Entities;
using FruitBank.Common.Interfaces;
using FruitBank.Common.Models;
using Mango.Nop.Core.Entities;
using Mango.Nop.Core.Extensions;
using Mango.Nop.Core.Loggers;
using Mango.Nop.Data.Repositories;
using Microsoft.EntityFrameworkCore;
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Shipping;
using Nop.Core.Events;
using Nop.Data;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces;
@ -68,8 +65,6 @@ public class FruitBankDbContext : MgDbContextBase,
public IRepository<CustomerAddressMapping> CustomerAddressMappings { get; set; }
public IRepository<GenericAttribute> GenericAttributes { get; set; }
public IRepository<StockQuantityHistory> StockQuantityHistories { get; set; }
public IRepository<StockQuantityHistoryExt> StockQuantityHistoriesExt { get; set; }
public FruitBankDbContext(INopDataProvider dataProvider, ILockService lockService, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext,
PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable,
@ -84,8 +79,6 @@ public class FruitBankDbContext : MgDbContextBase,
IRepository<CustomerAddressMapping> customerAddressMappingRepository,
IRepository<CustomerRole> customerRoleRepository,
IRepository<GenericAttribute> genericAttributes,
IRepository<StockQuantityHistory> stockQuantityHistories,
IRepository<StockQuantityHistoryExt> stockQuantityHistoriesExt,
IEventPublisher eventPublisher,
IEnumerable<IAcLogWriterBase> logWriters) : base(productRepository, orderRepository, orderItemRepository, dataProvider, lockService, new Logger<FruitBankDbContext>(logWriters.ToArray()))
{
@ -117,9 +110,6 @@ public class FruitBankDbContext : MgDbContextBase,
CustomerAddressMappings = customerAddressMappingRepository;
GenericAttributes = genericAttributes;
StockQuantityHistories = stockQuantityHistories;
StockQuantityHistoriesExt = stockQuantityHistoriesExt;
}
public IQueryable<Customer> GetCustomersBySystemRoleName(string systemRoleName)
@ -292,42 +282,38 @@ public class FruitBankDbContext : MgDbContextBase,
if (shippingItem.IsMeasured)
{
var quantityInc = productIdChanged ? shippingItem.MeasuredQuantity : shippingItem.MeasuredQuantity - dbShippingItem.MeasuredQuantity;
productDto!.StockQuantity += quantityInc;
//productDto!.StockQuantity += quantityInc;
//if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
// throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {productDto.Id}");
if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {productDto.Id}");
var incomingQuantity = productDto!.GenericAttributes.GetValueOrNull<int>(nameof(IIncomingQuantity.IncomingQuantity));
var incomingQuantity = productDto.GenericAttributes.GetValueOrNull<int>(nameof(IIncomingQuantity.IncomingQuantity));
if (incomingQuantity != null)
{
await _fruitBankAttributeService.UpdateGenericAttributeAsync<Product, int>
(productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value - quantityInc);
}
var weightToChange = 0d;
if (productIsMeasurable)
{
weightToChange = productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight;
//await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(productDto.Id, weightToChange, shippingItem.IsMeasurable, true);
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(productDto.Id,
productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight,
shippingItem.IsMeasurable, true);
}
await UpdateStockQuantityAndWeightAsync(productDto, quantityInc, $"Bejövő mérés, shippingItem: #{shippingItem.Id}", weightToChange);
//productDto!.StockQuantity += quantityInc;
}
//if (productIdUnchanged || !dbShippingItem.IsMeasured) return true;
if (!dbShippingItem.ProductId.HasValue || (!productIdChanged && (shippingItem.IsMeasured || !dbShippingItem.IsMeasured))) return true;
productDto = await ProductDtos.GetByIdAsync(dbShippingItem.ProductId!.Value, true);
if (productDto != null)
{
productIsMeasurable = productDto.IsMeasurable;
productDto.StockQuantity -= dbShippingItem.MeasuredQuantity;
//productDto.StockQuantity -= dbShippingItem.MeasuredQuantity;
//if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
// throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {productDto.Id}");
if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {productDto.Id}");
var incomingQuantity = productDto.GenericAttributes.GetValueOrNull<int>(nameof(IIncomingQuantity.IncomingQuantity));
if (incomingQuantity != null)
@ -336,17 +322,10 @@ public class FruitBankDbContext : MgDbContextBase,
(productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value + dbShippingItem.MeasuredQuantity);
}
//if (productIsMeasurable)
//{
// var measuringValues = new MeasuringAttributeValues(productDto.Id, -dbShippingItem.MeasuredNetWeight, dbShippingItem.IsMeasurable);
// await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true);
//}
if (!productIsMeasurable) return true;
await UpdateStockQuantityAndWeightAsync(productDto, -dbShippingItem.MeasuredQuantity,
$"Bejövő mérés, ShippingItem.Id: #{shippingItem.Id}. Product.Id megváltozott, #{productDto.Id}->#{shippingItem.ProductId}!",
-dbShippingItem.MeasuredNetWeight);
//productDto!.StockQuantity -= dbShippingItem.MeasuredQuantity;
var measuringValues = new MeasuringAttributeValues(productDto.Id, -dbShippingItem.MeasuredNetWeight, dbShippingItem.IsMeasurable);
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true);
}
else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}");
//else //TODO: productIdUnchanged-et lekezelni! - J.
@ -487,7 +466,7 @@ public class FruitBankDbContext : MgDbContextBase,
orderDto.OrderStatus = OrderStatus.Complete;
await OrderDtos.UpdateAsync(orderDto);
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Order, int>(orderDto.Id, nameof(IOrderDto.RevisorId), revisorId);
foreach (var orderItemDto in orderDto.OrderItemDtos)
@ -502,18 +481,14 @@ public class FruitBankDbContext : MgDbContextBase,
if (!orderItemDto.IsMeasurable) continue;
var prevOrderItemNetWeightFromGa = orderItemDto.GenericAttributes.GetValueOrDefault<double>(nameof(IMeasuringNetWeight.NetWeight), 0);
var prevNetWeightFromGa = orderItemDto.GenericAttributes.GetValueOrDefault<double>(nameof(IMeasuringNetWeight.NetWeight), 0);
//var gaNetWeight = CommonHelper.To<double>(orderItemDto.GenericAttributes.FirstOrDefault(x => x.Key == nameof(IMeasuringNetWeight.NetWeight))?.Value ?? "0");
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<OrderItem, double>(orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight);
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<OrderItem, double>
(orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight);
var productWeightToChange = -(orderItemDto.NetWeight - prevOrderItemNetWeightFromGa);
//await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(orderItemDto.ProductId, productWeightToChange, orderItemDto.IsMeasurable, true);
await UpdateStockQuantityAndWeightAsync(orderItemDto.ProductId, 0,
$"Kimenő mérés, OrderStatus set to complete. Rendelés: #{orderDto.Id}, rendelés tétel: #{orderItemDto.Id}",
productWeightToChange);
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>
(orderItemDto.ProductId, -(orderItemDto.NetWeight - prevNetWeightFromGa), orderItemDto.IsMeasurable, true);
}
//await _eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus));
@ -577,7 +552,7 @@ public class FruitBankDbContext : MgDbContextBase,
public async Task<OrderItemPallet?> AddOrderItemPalletAsync(OrderItemPallet orderItemPallet)
{
if (!await SetupOrderItemPalletMeauringValues(orderItemPallet)) return null;
await OrderItemPallets.InsertAsync(orderItemPallet);
return orderItemPallet;
}
@ -596,7 +571,7 @@ public class FruitBankDbContext : MgDbContextBase,
public async Task<OrderItemPallet?> AddOrUpdateOrderItemPalletAsync(OrderItemPallet orderItemPallet)
{
if (orderItemPallet.Id <= 0) return await AddOrderItemPalletAsync(orderItemPallet);
return await UpdateOrderItemPalletAsync(orderItemPallet);
}
@ -615,7 +590,7 @@ public class FruitBankDbContext : MgDbContextBase,
if (orderItemDto == null || orderItemPallet.OrderItemId != orderItemDto.Id || //orderItemDto.IsOtherMeasuringInProgress(orderItemPallet.CreatorId) ||
orderItemPallet.TrayQuantity > orderItemDto.Quantity || !orderItemPallet.IsValidSafeMeasuringValues()) return false;
orderItemDto.OrderItemPallets.UpdateCollection(orderItemPallet, false);
if (orderItemDto.TrayQuantity > orderItemDto.Quantity) return false;
@ -623,102 +598,32 @@ public class FruitBankDbContext : MgDbContextBase,
return true;
}
//private async Task<bool> UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent)
//{
// var productDto = await ProductDtos.GetByIdAsync(productDtoId, false);
// if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent);
// Logger.Error($"product == null; id: {productDtoId}");
// return await Task.FromResult(false);
//}
//private async Task<bool> UpdateProductDtoStockQuantityAsync(ProductDto productDto, bool publishEvent)
//{
// //TODO: !!!!!!!!!!!!!!!! - J.
// //await _productService.AdjustInventoryAsync(product, quantityInc, string.Empty, "");
// await ProductDtos.UpdateAsync(productDto, publishEvent);
// return await Task.FromResult(true);
// //var updatedRowsCount = await DataProvider.ExecuteNonQueryAsync($"update product set {nameof(Product.StockQuantity)} = {product.StockQuantity} where {nameof(Product.Id)} = {product.Id}");
// //if (updatedRowsCount == 1) return await Task.FromResult(true);
// //Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}");
// //return await Task.FromResult(false);
//}
public async Task UpdateStockQuantityAndWeightAsync(int productId, int quantityToChange, string message, double weightToChange = 0)
private async Task<bool> UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent)
{
if (quantityToChange == 0 && weightToChange == 0) return;
var productDto = await ProductDtos.GetByIdAsync(productDtoId, false);
if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent);
var product = await Products.GetByIdAsync(productId);
await UpdateStockQuantityAndWeightAsync(product, quantityToChange, message, weightToChange);
Logger.Error($"product == null; id: {productDtoId}");
return await Task.FromResult(false);
}
public async Task UpdateStockQuantityAndWeightAsync(ProductDto productDto, int quantityToChange, string message, double weightToChange = 0)
private async Task<bool> UpdateProductDtoStockQuantityAsync(ProductDto productDto, bool publishEvent)
{
if (quantityToChange == 0 && weightToChange == 0) return;
//Itt mi legyen? RollBack? - J.
if (productDto.StockQuantity < 0)
Logger.Error($"productDto.StockQuantity < 0; Id: {productDto.Id}; StockQuantity: {productDto.StockQuantity}");
await UpdateStockQuantityAndWeightAsync(productDto.Id, quantityToChange, message, weightToChange);
productDto.StockQuantity += quantityToChange;
//TODO: !!!!!!!!!!!!!!!! - J.
//await _productService.AdjustInventoryAsync(product, quantityInc, string.Empty, "");
if (weightToChange == 0) return;
productDto.GenericAttributes = await GenericAttributes.Table.Where(x => x.EntityId == productDto.Id && x.KeyGroup == nameof(Product) && x.StoreId == _storeContext.GetCurrentStore().Id).ToListAsync();
}
await ProductDtos.UpdateAsync(productDto, publishEvent);
return await Task.FromResult(true);
public async Task UpdateStockQuantityAndWeightAsync(Product product, int quantityToChange, string message, double weightToChange = 0)
{
weightToChange = double.Round(weightToChange, 1);
if (quantityToChange == 0 && weightToChange == 0) return;
//var updatedRowsCount = await DataProvider.ExecuteNonQueryAsync($"update product set {nameof(Product.StockQuantity)} = {product.StockQuantity} where {nameof(Product.Id)} = {product.Id}");
//if (updatedRowsCount == 1) return await Task.FromResult(true);
var latStockQuantityHistoryId = 0;
if (quantityToChange != 0) await _productService.AdjustInventoryAsync(product, quantityToChange, string.Empty, message);
else if (weightToChange != 0)
{
//Vizsgálja, h a quantityToChange != 0... - J.
//await _productService.AddStockQuantityHistoryEntryAsync(product, 0, product.StockQuantity, product.WarehouseId, message);
var historyEntry = new StockQuantityHistory
{
ProductId = product.Id,
CombinationId = null,
WarehouseId = product.WarehouseId > 0 ? (int?)product.WarehouseId : null,
QuantityAdjustment = 0,
StockQuantity = product.StockQuantity,
Message = message,
CreatedOnUtc = DateTime.UtcNow
};
await StockQuantityHistories.InsertAsync(historyEntry);
latStockQuantityHistoryId = historyEntry.Id;
}
if (weightToChange == 0) return;
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(product.Id, weightToChange, true, true);
if (latStockQuantityHistoryId <= 0)
{
//A LastOrDefaultAsync elszáll! - J.
latStockQuantityHistoryId = await StockQuantityHistories.Table.Where(x => x.ProductId == product.Id).MaxAsync(x => x.Id);
if (latStockQuantityHistoryId == 0)
{
Logger.Error($"UpdateProductDtoStockQuantityAndWeightAsync (latStockQuantityHistory == 0). product.Id: {product.Id}");
return;
}
}
var stockWeight = double.Round(await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, double>(product.Id, nameof(IMeasuringNetWeight.NetWeight)), 1);
var stockQuantityHistoryExt = new StockQuantityHistoryExt
{
StockQuantityHistoryId = latStockQuantityHistoryId,
NetWeightAdjustment = weightToChange,
NetWeight = stockWeight,
};
await StockQuantityHistoriesExt.InsertAsync(stockQuantityHistoryExt, false);
//Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}");
//return await Task.FromResult(false);
}
public async Task<List<ShippingDocument>> GetShippingDocumentsByShippingIdAsync(int shippingId)

View File

@ -45,7 +45,7 @@ public class FruitBankEventConsumer :
_measurementService = measurementService;
_fruitBankAttributeService = fruitBankAttributeService;
}
public override async Task HandleEventAsync(EntityUpdatedEvent<Product> eventMessage)
{
var product = await CheckAndUpdateProductManageInventoryMethodToManageStock(eventMessage.Entity);
@ -117,10 +117,7 @@ public class FruitBankEventConsumer :
var productDtoNetWeight = productDto?.GenericAttributes.GetValueOrNull<double>(nameof(IMeasuringNetWeight.NetWeight));
if (productDtoNetWeight == null || double.Round(productDtoNetWeight.Value, 1) != netWeight)
{
//await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight);
await _ctx.UpdateStockQuantityAndWeightAsync(productDto, 0, $"Manuális készlet súly változtatás az admin felületen.", netWeight - productDtoNetWeight.GetValueOrDefault(0));
}
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight);
//Tára
var tare = double.Round(CommonHelper.To<double>(form[nameof(ITare.Tare)].ToString()), 1);
@ -277,7 +274,7 @@ public class FruitBankEventConsumer :
await _measurementService.OrderItemInsertedOrUpdatedPostProcess(eventMessage.Entity);
}
}
#endregion Delete