Product/OrderItem Quantity fix

This commit is contained in:
Loretta 2025-10-23 18:39:34 +02:00
parent 8cf260a6d2
commit a43dc391fb
2 changed files with 30 additions and 83 deletions

View File

@ -221,19 +221,24 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
CustomerCurrencyCode = "HUF", // TODO: GET Default currency - A.
};
var productDtosById = await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(op => op.Id)).ToDictionaryAsync(p => p.Id, prodDto => prodDto);
//var productDtosById = await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(op => op.Id)).ToDictionaryAsync(p => p.Id, prodDto => prodDto);
var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ =>
{
await _orderService.InsertOrderAsync(order);
order.CustomOrderNumber = order.Id.ToString();
await _orderService.UpdateOrderAsync(order);
foreach (var item in orderProducts)
{
//var product = await _productService.GetProductByIdAsync(item.Id);
//if (product != null)
var product = await _productService.GetProductByIdAsync(item.Id);
if (product == null)
{
_logger.Error($"(product == null); {item}");
continue;
}
if (productDtosById.TryGetValue(item.Id, out var product))
//if (productDtosById.TryGetValue(item.Id, out var productDto))
{
var orderItem = new OrderItem
{
@ -250,10 +255,14 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
DiscountAmountInclTax = 0,
DiscountAmountExclTax = 0
};
//var valami = product.GenericAttributes.GetValueOrNull<bool>(nameof(IOrderDto.DateOfReceipt)); TEST - A.
await _orderService.InsertOrderItemAsync(orderItem);
//await _productService.AddStockQuantityHistoryEntryAsync(product, -orderItem.Quantity, product.StockQuantity, 1);
await _productService.AdjustInventoryAsync(product, -orderItem.Quantity, orderItem.AttributesXml, "");
//await _productService.BookReservedInventoryAsync(product, 1, item.Quantity, "");
}
else _logger.Error($"(productDtosById.TryGetValue(item.Id, out var product) == false); {item}");
//else _logger.Error($"(productDtosById.TryGetValue(item.Id, out var product) == false); {item}");
}
return true;

View File

@ -16,6 +16,7 @@ using Nop.Web.Framework.Events;
using Nop.Web.Framework.Menu;
using Nop.Web.Models.Sitemap;
using System.Linq;
using Microsoft.IdentityModel.Tokens;
namespace Nop.Plugin.Misc.FruitBankPlugin.Services
{
@ -64,69 +65,6 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
public async Task HandleEventAsync(OrderPlacedEvent eventMessage)
{
var order = eventMessage.Order;
var orderItems = await _orderService.GetOrderItemsAsync(order.Id);
bool requiresMeasurement = false;
foreach (var item in orderItems)
{
var product = await _productService.GetProductByIdAsync(item.ProductId);
// itt pl. egy custom flag a producton, ami jelzi, hogy mérés kell hozzá
// akár egy product attribute is lehet, vagy egy saját extension metódus
if (product != null)
{
//var productAttributeMappings = await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(product.Id);
////Product Attributes
//foreach (var pam in productAttributeMappings)
//{
// var attributes = await _productAttributeService.GetProductAttributeValuesAsync(pam.Id);
// foreach (var attr in attributes)
// {
// // you can check for specific attribute by its name or id
// if (attr.Name == "NeedsToBeMeasured" && attr.IsPreSelected)
// {
// requiresMeasurement = true;
// break;
// }
// }
//}
var productSpecAttributes = await _specificationAttributeService.GetProductSpecificationAttributesAsync(product.Id);
foreach (var specAttribute in productSpecAttributes)
{
// Get the specification attribute
var specificationAttribute = await _specificationAttributeService
.GetSpecificationAttributeByIdAsync(specAttribute.Id);
// Get the specification attribute option
var specificationAttributeOption = await _specificationAttributeService
.GetSpecificationAttributeOptionByIdAsync(specAttribute.SpecificationAttributeOptionId);
System.Diagnostics.Debug.WriteLine($"Spec Attribute: {specificationAttribute.Name}, Option: {specificationAttributeOption.Name}");
// Check if this is your "NeedsToBeMeasured" specification attribute
if (specificationAttribute.Name == "Measureable" && //nameof(MeasuringAttributeValues.IsMeasurable)
specificationAttributeOption.Name == "Yes") // or whatever value you set
{
requiresMeasurement = true;
break;
}
}
}
}
if (requiresMeasurement)
{
var store = await _storeContext.GetCurrentStoreAsync();
// itt adjuk hozzá a GenericAttribute flag-et az orderhez
await _genericAttributeService.SaveAttributeAsync(order,
nameof(IMeasurable.IsMeasurable), true, store.Id);
// status pending
// paymentstatus pending
}
}
public async Task HandleEventAsync(EntityUpdatedEvent<Order> eventMessage)
@ -139,29 +77,29 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services
{
if (order == null) return;
var form = _httpContextAccessor.HttpContext?.Request?.Form;
var hasForm = _httpContextAccessor.HttpContext?.Request?.HasFormContentType ?? false;
var form = hasForm ? _httpContextAccessor.HttpContext.Request.Form : null;
if (form == null || form.Count == 0) return;
if (form.ContainsKey(nameof(IMeasurable.IsMeasurable)))
{
var isMeasurable = form[nameof(IMeasurable.IsMeasurable)].ToString().Contains("true");
//var isMeasurable = CommonHelper.To<bool>(form[nameof(IMeasurable.IsMeasurable)].ToString());
//if (form.ContainsKey(nameof(IMeasurable.IsMeasurable)))
//{
// var isMeasurable = form[nameof(IMeasurable.IsMeasurable)].ToString().Contains("true");
// //var isMeasurable = CommonHelper.To<bool>(form[nameof(IMeasurable.IsMeasurable)].ToString());
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Order, bool>(order.Id, nameof(IMeasurable.IsMeasurable), isMeasurable);
}
// await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Order, bool>(order.Id, nameof(IMeasurable.IsMeasurable), isMeasurable);
//}
if (form.ContainsKey(nameof(IOrderDto.DateOfReceipt)))
{
var dateOfReceipt = form[nameof(IOrderDto.DateOfReceipt)];
var dateOfReceiptString = form[nameof(IOrderDto.DateOfReceipt)];
if (string.IsNullOrWhiteSpace(dateOfReceiptString)) return;
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Order, DateTime>(order.Id, nameof(IOrderDto.DateOfReceipt), DateTime.Parse(dateOfReceipt));
if (DateTime.TryParse(dateOfReceiptString, out var dateOfReceipt))
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Order, DateTime>(order.Id, nameof(IOrderDto.DateOfReceipt), dateOfReceipt);
}
}
public override async Task HandleEventAsync(AdminMenuCreatedEvent eventMessage)
{
var rootNode = eventMessage.RootMenuItem;