imporvements, fixes, etc...

This commit is contained in:
Loretta 2025-10-31 05:37:35 +01:00
parent 0a64131a8e
commit d3e2675dc9
5 changed files with 126 additions and 25 deletions

View File

@ -12,6 +12,7 @@ using Mango.Nop.Core.Repositories;
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.Events;
@ -63,6 +64,8 @@ public class FruitBankDbContext : MgDbContextBase,
public IRepository<CustomerCustomerRoleMapping> CustomerRoleMappings { get; set; }
public IRepository<CustomerAddressMapping> CustomerAddressMappings { get; set; }
public IRepository<GenericAttribute> GenericAttributes { 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,
@ -74,7 +77,9 @@ public class FruitBankDbContext : MgDbContextBase,
IRepository<Customer> customerRepository,
IRepository<CustomerCustomerRoleMapping> customerCustomerRoleMappingRepository,
IRepository<CustomerAddressMapping> customerAddressMappingRepository,
IRepository<CustomerRole> customerRoleRepository,IEventPublisher eventPublisher,
IRepository<CustomerRole> customerRoleRepository,
IRepository<GenericAttribute> genericAttributes,
IEventPublisher eventPublisher,
IEnumerable<IAcLogWriterBase> logWriters) : base(productRepository, orderRepository, orderItemRepository, dataProvider, lockService, new Logger<FruitBankDbContext>(logWriters.ToArray()))
{
_eventPublisher = eventPublisher;
@ -103,6 +108,8 @@ public class FruitBankDbContext : MgDbContextBase,
CustomerRoles = customerRoleRepository;
CustomerRoleMappings = customerCustomerRoleMappingRepository;
CustomerAddressMappings = customerAddressMappingRepository;
GenericAttributes = genericAttributes;
}
public IQueryable<Customer> GetCustomersBySystemRoleName(string systemRoleName)
@ -193,7 +200,7 @@ public class FruitBankDbContext : MgDbContextBase,
ProductDto? productDto = null;
var productIsMeasurable = false;
if (shippingItem.ProductId > 0)
if (shippingItem.ProductId.GetValueOrDefault(0) > 0)
{
productDto = await ProductDtos.GetByIdAsync(shippingItem.ProductId!.Value, true);
@ -263,7 +270,7 @@ public class FruitBankDbContext : MgDbContextBase,
//if (productIdUnchanged || !dbShippingItem.IsMeasured) return true;
if (!productIdChanged && (shippingItem.IsMeasured || !dbShippingItem.IsMeasured)) return true;
productDto = await ProductDtos.GetByIdAsync(dbShippingItem.ProductId);
productDto = await ProductDtos.GetByIdAsync(dbShippingItem.ProductId!.Value, true);
if (productDto != null)
{
@ -453,6 +460,43 @@ public class FruitBankDbContext : MgDbContextBase,
return orderDto;
}
public Task DeleteOrderItemConstraintsSafeAsync(OrderItem orderItem, bool publishEvent = false)
{
return TransactionSafeAsync(async _ =>
{
await DeleteOrderItemConstraintsAsync(orderItem, publishEvent);
return true;
});
}
public async Task DeleteOrderItemConstraintsAsync(OrderItem orderItem, bool publishEvent = false)
{
//Itt nincs már OrderItemDto!!!! - J.
var storeId = _storeContext.GetCurrentStore().Id;
var orderItemGenericAttributes = await GenericAttributes.Table.Where(x => x.EntityId == orderItem.Id && x.KeyGroup == nameof(OrderItem) && x.StoreId == storeId).ToListAsync();
var validOrderItemNetWeight = orderItemGenericAttributes.GetValueOrDefault<double>(nameof(IMeasuringNetWeight.NetWeight), 0);
if (validOrderItemNetWeight != 0)
{
var productDto = await ProductDtos.GetByIdAsync(orderItem.ProductId, true);
if (productDto != null && productDto.IsMeasurable)
{
var newProductNetWeight = productDto.NetWeight + validOrderItemNetWeight;
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(productDto.Id, nameof(IMeasuringNetWeight.NetWeight), newProductNetWeight);
Logger.Info($"DeleteOrderItemConstraints; Product netWeight updated! productId: {productDto.Id}; newNetWeight: {newProductNetWeight}; oldNetWeight: {productDto.NetWeight}; deleted orderItemNetWeight: {validOrderItemNetWeight}; orderItem.Id: {orderItem.Id};");
}
}
await _fruitBankAttributeService.DeleteGenericAttributesAsync(orderItemGenericAttributes);
var deletedPalletCount = await OrderItemPallets.DeleteAsync(x => x.OrderItemId == orderItem.Id);
//await OrderItems.DeleteAsync(orderItem, publishEvent);
Logger.Info($"DeleteOrderItemConstraints; OrderItem constraints deleted! deletedPalletCount: {deletedPalletCount}; orderItem.Id: {orderItem.Id};");
}
public async Task<OrderItemPallet?> AddOrderItemPalletAsync(OrderItemPallet orderItemPallet)
{
if (!await SetupOrderItemPalletMeauringValues(orderItemPallet)) return null;
@ -504,7 +548,7 @@ public class FruitBankDbContext : MgDbContextBase,
private async Task<bool> UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent)
{
var productDto = await ProductDtos.GetByIdAsync(productDtoId);
var productDto = await ProductDtos.GetByIdAsync(productDtoId, false);
if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent);
Logger.Error($"product == null; id: {productDtoId}");

View File

@ -29,19 +29,21 @@ public class FruitBankEventConsumer :
IConsumer<EntityDeletedEvent<ShippingItemPallet>>,
IConsumer<EntityInsertedEvent<OrderItem>>,
IConsumer<EntityUpdatedEvent<OrderItem>>
IConsumer<EntityUpdatedEvent<OrderItem>>,
IConsumer<EntityDeletedEvent<OrderItem>>
{
private readonly CustomPriceCalculationService _customPriceCalculationService;
//private readonly CustomPriceCalculationService _customPriceCalculationService;
private readonly FruitBankDbContext _ctx;
private readonly MeasurementService _measurementService;
private readonly FruitBankAttributeService _fruitBankAttributeService;
public FruitBankEventConsumer(IHttpContextAccessor httpContextAcc, IPriceCalculationService customPriceCalculationService, FruitBankDbContext ctx, FruitBankAttributeService fruitBankAttributeService, IEnumerable<IAcLogWriterBase> logWriters) : base(ctx, httpContextAcc, logWriters)
public FruitBankEventConsumer(IHttpContextAccessor httpContextAcc, FruitBankDbContext ctx, MeasurementService measurementService,
FruitBankAttributeService fruitBankAttributeService, IEnumerable<IAcLogWriterBase> logWriters) : base(ctx, httpContextAcc, logWriters)
{
_ctx = ctx;
_measurementService = measurementService;
_fruitBankAttributeService = fruitBankAttributeService;
_customPriceCalculationService = customPriceCalculationService as CustomPriceCalculationService;
}
public override async Task HandleEventAsync(EntityUpdatedEvent<Product> eventMessage)
@ -257,24 +259,22 @@ public class FruitBankEventConsumer :
await _ctx.ShippingItemPallets.DeleteAsync(sp => sp.ShippingItemId == eventMessage.Entity.Id, false);
}
public async Task HandleEventAsync(EntityDeletedEvent<OrderItem> eventMessage)
{
await _measurementService.DeleteOrderItemConstraintsAsync(eventMessage.Entity);
}
public async Task HandleEventAsync(EntityUpdatedEvent<OrderItem> eventMessage)
{
await CheckAndUpdateOrderItemFinalPricesAsync(eventMessage.Entity);
await _measurementService.OrderItemInsertedOrUpdatedPostProcess(eventMessage.Entity);
}
public async Task HandleEventAsync(EntityInsertedEvent<OrderItem> eventMessage)
{
await CheckAndUpdateOrderItemFinalPricesAsync(eventMessage.Entity);
await _measurementService.OrderItemInsertedOrUpdatedPostProcess(eventMessage.Entity);
}
private async Task CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem)
{
if (await _customPriceCalculationService.CheckAndUpdateOrderItemFinalPricesAsync(orderItem))
{
var order = await _ctx.Orders.GetByIdAsync(orderItem.OrderId);
await _customPriceCalculationService.CheckAndUpdateOrderTotalPrice(order);
}
}
}
#endregion Delete

View File

@ -71,10 +71,10 @@ public class CustomPriceCalculationService : PriceCalculationService
return (finalPriceInclTax, finalPriceExclTax);
}
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem)
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem, OrderItemDto? orderItemDtoHelper = null)
{
var orderItemDto = await _dbContext.OrderItemDtos.GetByIdAsync(orderItem.Id, true);
return await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto.IsMeasurable, orderItemDto.NetWeight);
orderItemDtoHelper ??= await _dbContext.OrderItemDtos.GetByIdAsync(orderItem.Id, true);
return await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDtoHelper.IsMeasurable, orderItemDtoHelper.NetWeight);
}
//public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItemDto orderItemDto)

View File

@ -166,6 +166,12 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute
var ga = await GetGenericAttributeAsync<TEntity>(entityId, key, storeId);
if (ga == null) return;
await genericAttributeService.DeleteAttributeAsync(ga);
await DeleteGenericAttributeAsync(ga);
}
public async Task DeleteGenericAttributeAsync(GenericAttribute genericAttribute)
=> await genericAttributeService.DeleteAttributeAsync(genericAttribute);
public async Task DeleteGenericAttributesAsync(IList<GenericAttribute> genericAttributes)
=> await genericAttributeService.DeleteAttributesAsync(genericAttributes);
}

View File

@ -1,8 +1,59 @@
using FruitBank.Common.Services;
using AyCode.Core.Loggers;
using FruitBank.Common.Dtos;
using FruitBank.Common.Interfaces;
using FruitBank.Common.Server.Services.SignalRs;
using FruitBank.Common.Services;
using Mango.Nop.Core.Extensions;
using Mango.Nop.Core.Loggers;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Orders;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using Nop.Services.Catalog;
namespace Nop.Plugin.Misc.FruitBankPlugin.Services;
public class MeasurementService : MeasurementServiceBase, IMeasurementService
#nullable enable
public class MeasurementService : MeasurementServiceBase<Logger>, IMeasurementService
{
private readonly FruitBankDbContext _dbContext;
private readonly SignalRSendToClientService _signalRSendToClientService;
private readonly CustomPriceCalculationService _customPriceCalculationService;
public MeasurementService(FruitBankDbContext dbContext, SignalRSendToClientService signalRSendToClientService, FruitBankAttributeService fruitBankAttributeService,
IPriceCalculationService customPriceCalculationService, IEnumerable<IAcLogWriterBase> logWriters) : base(new Logger<MeasurementService>(logWriters.ToArray()))
{
_dbContext = dbContext;
_signalRSendToClientService = signalRSendToClientService;
_customPriceCalculationService = (CustomPriceCalculationService)customPriceCalculationService;
}
public async Task DeleteOrderItemConstraintsAsync(int orderItemId) => await DeleteOrderItemConstraintsAsync(await _dbContext.OrderItems.GetByIdAsync(orderItemId));
public async Task DeleteOrderItemConstraintsAsync(OrderItem orderItem)
{
Logger.Info($"DeleteOrderItemConstraintsAsync invoked; orderItem.Id: {orderItem?.Id}");
if (orderItem == null) return;
await _dbContext.DeleteOrderItemConstraintsSafeAsync(orderItem);
await _signalRSendToClientService.SendOrderItemDeleted(orderItem);
}
public async Task OrderItemInsertedOrUpdatedPostProcess(OrderItem orderItem)
{
var orderItemDto = await _dbContext.OrderItemDtos.GetByIdAsync(orderItem.Id, true);
await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto);
await _signalRSendToClientService.SendOrderItemChanged(orderItemDto);
}
public async Task CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem, OrderItemDto? orderItemDtoHelper = null)
{
if (await _customPriceCalculationService.CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDtoHelper))
{
var order = await _dbContext.Orders.GetByIdAsync(orderItem.OrderId);
await _customPriceCalculationService.CheckAndUpdateOrderTotalPrice(order);
}
}
}