Compare commits

..

No commits in common. "7c07811ef63b6ca9ffdbd78d074dd3af3b314de1" and "f71acbf5621aefbe222f62323ac2117d09ed02eb" have entirely different histories.

5 changed files with 230 additions and 246 deletions

View File

@ -3,7 +3,6 @@ using AyCode.Core.Loggers;
using AyCode.Services.Server.SignalRs; using AyCode.Services.Server.SignalRs;
using AyCode.Services.SignalRs; using AyCode.Services.SignalRs;
using AyCode.Utils.Extensions; using AyCode.Utils.Extensions;
using DocumentFormat.OpenXml.Spreadsheet;
using FluentMigrator.Runner.Generators.Base; using FluentMigrator.Runner.Generators.Base;
using FruitBank.Common.Dtos; using FruitBank.Common.Dtos;
using FruitBank.Common.Entities; using FruitBank.Common.Entities;
@ -19,12 +18,10 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json; using Newtonsoft.Json;
using Nop.Core; using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers; using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Orders; using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Payments; using Nop.Core.Domain.Payments;
using Nop.Core.Domain.Shipping; using Nop.Core.Domain.Shipping;
using Nop.Core.Domain.Stores;
using Nop.Core.Domain.Tax; using Nop.Core.Domain.Tax;
using Nop.Core.Events; using Nop.Core.Events;
using Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Models.Order; using Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Models.Order;
@ -53,10 +50,8 @@ using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Mvc.Filters; using Nop.Web.Framework.Mvc.Filters;
using System.Text; using System.Text;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Xml; using System.Xml;
using System.Xml.Serialization; using System.Xml.Serialization;
using static Nop.Services.Security.StandardPermission;
namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
{ {
@ -518,164 +513,99 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
ShippingAddressId = customer.ShippingAddressId, ShippingAddressId = customer.ShippingAddressId,
PaymentMethodSystemName = "Payments.CheckMoneyOrder", // Default payment method PaymentMethodSystemName = "Payments.CheckMoneyOrder", // Default payment method
CustomerCurrencyCode = "HUF", // TODO: GET Default currency - A. CustomerCurrencyCode = "HUF", // TODO: GET Default currency - A.
OrderTotal = 0,
OrderSubtotalInclTax = 0,
OrderSubtotalExclTax = 0,
OrderSubTotalDiscountInclTax = 0,
OrderSubTotalDiscountExclTax = 0,
}; };
//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 store = await _storeContext.GetCurrentStoreAsync(); var store = _storeContext.GetCurrentStore();
var admin = await _workContext.GetCurrentCustomerAsync(); var productDtosByOrderItemId = await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(x => x.Id).ToArray()).ToDictionaryAsync(k => k.Id, v => v);
var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ => var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ =>
{ {
await _orderService.InsertOrderAsync(order); await _orderService.InsertOrderAsync(order);
order.CustomOrderNumber = order.Id.ToString();
await AddOrderItemsThenUpdateOrder(order, orderProducts, true, customer, store, admin); order.OrderTotal = 0;
return true;
});
if (transactionSuccess) foreach (var item in orderProducts)
{ {
//var orderDto = await _dbContext.OrderDtos.GetByIdAsync(order.Id, true); var product = await _productService.GetProductByIdAsync(item.Id);
//await _sendToClient.SendMeasuringNotification("Módosult a rendelés, mérjétek újra!", orderDto);
return RedirectToAction("Edit", "Order", new { id = order.Id });
}
_logger.Error($"(transactionSuccess == false)");
return RedirectToAction("Error", new { id = order.Id });
}
private async Task AddOrderItemsThenUpdateOrder<TOrderProductItem>(Order order, IReadOnlyList<TOrderProductItem> orderProductItems, bool unitPricesIncludeDiscounts, Customer customer = null, Store store = null, Customer admin = null)
where TOrderProductItem : IOrderProductItemBase
{
store ??= await _storeContext.GetCurrentStoreAsync();
admin ??= await _workContext.GetCurrentCustomerAsync();
customer ??= await _workContext.GetCurrentCustomerAsync();
var helperProductDtosByOrderItemId = await _dbContext.ProductDtos.GetAllByIds(orderProductItems.Select(x => x.Id).ToArray()).ToDictionaryAsync(k => k.Id, v => v);
foreach (var orderProductItem in orderProductItems)
{
var product = await _productService.GetProductByIdAsync(orderProductItem.Id);
if (product == null) if (product == null)
{ {
_logger.Warning($"Product with ID {orderProductItem.Id} not found"); var errorText = $"product == null; productId: {item.Id};";
continue;
//var errorText = $"product == null; productId: {item.Id};";
//_logger.Error($"{errorText}");
//throw new Exception($"{errorText}");
}
//var stockQuantity = await _productService.GetTotalStockQuantityAsync(product);
var productDto = helperProductDtosByOrderItemId[orderProductItem.Id];
var isMeasurable = productDto.IsMeasurable;
if ((product.StockQuantity + productDto.IncomingQuantity) - orderProductItem.Quantity < 0)
{
//errorMessage = $"Nem elérhető készleten!";
var errorText = $"((product.StockQuantity + productDto.IncomingQuantity) - item.Quantity < 0); productId: {product.Id}; (product.StockQuantity + productDto.IncomingQuantity) - item.Quantity: {(product.StockQuantity + productDto.IncomingQuantity) - orderProductItem.Quantity}";
_logger.Error($"{errorText}"); _logger.Error($"{errorText}");
throw new Exception($"{errorText}"); throw new Exception($"{errorText}");
} }
var orderItem = await CreateOrderItem(product, order, orderProductItem, isMeasurable, unitPricesIncludeDiscounts, customer, store); var productDto = productDtosByOrderItemId[item.Id];
var isMeasurable = productDto.IsMeasurable;
await _orderService.InsertOrderItemAsync(orderItem); if ((product.StockQuantity + productDto.IncomingQuantity) - item.Quantity < 0)
await _productService.AdjustInventoryAsync(product, -orderItem.Quantity, orderItem.AttributesXml, string.Format(await _localizationService.GetResourceAsync("Admin.StockQuantityHistory.Messages.PlaceOrder"), order.Id));
var priceCalculation = await _priceCalculationService.GetFinalPriceAsync(product, customer, store, includeDiscounts: false);
var unitPriceInclTaxValue = priceCalculation.finalPrice;
var (unitPriceExclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPriceInclTaxValue, false, customer);
order.OrderSubtotalInclTax += unitPriceInclTaxValue * orderItem.Quantity;
order.OrderSubtotalExclTax += unitPriceExclTaxValue * orderItem.Quantity;
order.OrderSubTotalDiscountInclTax += order.OrderSubtotalInclTax - orderItem.PriceInclTax;
order.OrderSubTotalDiscountExclTax += order.OrderSubtotalExclTax - orderItem.PriceExclTax;
//order.OrderTax
//order.TaxRates
order.OrderTotal += orderItem.PriceInclTax + order.OrderShippingInclTax + order.PaymentMethodAdditionalFeeInclTax;
}
await _orderService.UpdateOrderAsync(order);
await InsertOrderNoteAsync(order.Id, false, $"Products added {orderProductItems.Count} item to order by {admin.FirstName} {admin.LastName}, (CustomerId: {admin.Id})");
}
private async Task<OrderItem> CreateOrderItem<TOrderProductItem>(Product product, Order order, TOrderProductItem orderProductItem, bool isMeasurable, bool unitPricesIncludeDiscounts, Customer customer = null, Store store = null)
where TOrderProductItem : IOrderProductItemBase
{ {
if (product.Id != orderProductItem.Id) var errorText = $"((product.StockQuantity + productDto.IncomingQuantity) - item.Quantity < 0); productId: {product.Id}; (product.StockQuantity + productDto.IncomingQuantity) - item.Quantity: {(product.StockQuantity + productDto.IncomingQuantity) - item.Quantity}";
throw new Exception($"CustomOrderController->CreateOrderItem; (product.Id != orderProductItem.Id)");
store ??= await _storeContext.GetCurrentStoreAsync(); _logger.Error($"{errorText}");
customer ??= await _workContext.GetCurrentCustomerAsync(); throw new Exception($"{errorText}");
}
var priceCalculation = await _priceCalculationService.GetFinalPriceAsync(product, customer, store, includeDiscounts: unitPricesIncludeDiscounts); var valami = await _priceCalculationService.GetFinalPriceAsync(product, customer, store, includeDiscounts: true);
var unitPriceInclTaxValue = priceCalculation.finalPrice; var unitPrice = valami.finalPrice;
// Calculate tax // Calculate tax
//var (unitPriceInclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPrice, true, customer); var (unitPriceInclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPrice, true, customer);
var (unitPriceExclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPriceInclTaxValue, false, customer); var (unitPriceExclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPrice, false, customer);
return new OrderItem
var orderItem = new OrderItem
{ {
OrderId = order.Id, OrderId = order.Id,
ProductId = orderProductItem.Id, ProductId = item.Id,
Quantity = orderProductItem.Quantity, Quantity = item.Quantity,
OrderItemGuid = Guid.NewGuid(),
UnitPriceInclTax = unitPriceInclTaxValue, UnitPriceInclTax = unitPriceInclTaxValue,
UnitPriceExclTax = unitPriceExclTaxValue, UnitPriceExclTax = unitPriceExclTaxValue,
PriceInclTax = isMeasurable ? 0 : unitPriceInclTaxValue * orderProductItem.Quantity, PriceInclTax = isMeasurable ? 0 : unitPriceInclTaxValue * item.Quantity,
PriceExclTax = isMeasurable ? 0 : unitPriceExclTaxValue * orderProductItem.Quantity, PriceExclTax = isMeasurable ? 0 : unitPriceExclTaxValue * item.Quantity,
OriginalProductCost = await _priceCalculationService.GetProductCostAsync(product, null), OriginalProductCost = await _priceCalculationService.GetProductCostAsync(product, null),
AttributeDescription = string.Empty, AttributeDescription = string.Empty,
AttributesXml = string.Empty, AttributesXml = string.Empty,
DiscountAmountInclTax = 0,
DiscountAmountInclTax = decimal.Zero, DiscountAmountExclTax = 0
DiscountAmountExclTax = decimal.Zero,
DownloadCount = 0,
IsDownloadActivated = false,
LicenseDownloadId = 0,
ItemWeight = product.Weight * orderProductItem.Quantity,
RentalStartDateUtc = null,
RentalEndDateUtc = null
}; };
order.OrderTotal += orderItem.PriceInclTax;
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, "");
} }
public interface IOrderProductItemBase order.CustomOrderNumber = order.Id.ToString();
{
/// <summary> order.OrderSubtotalInclTax = order.OrderTotal;
/// ProductId order.OrderSubtotalExclTax = order.OrderTotal;
/// </summary> order.OrderSubTotalDiscountInclTax = order.OrderTotal;
public int Id { get; set; } order.OrderSubTotalDiscountExclTax = order.OrderTotal;
public int Quantity { get; set; }
public decimal Price { get; set; } await _orderService.UpdateOrderAsync(order);
//var orderDto = await _dbContext.OrderDtos.GetByIdAsync(order.Id, true);
//await _sendToClient.SendMeasuringNotification("Módosult a rendelés, mérjétek újra!", orderDto);
return true;
});
if (transactionSuccess) return RedirectToAction("Edit", "Order", new { id = order.Id });
_logger.Error($"(transactionSuccess == false)");
return RedirectToAction("Error", new { id = order.Id });
} }
public class OrderProductItem : IOrderProductItemBase public class OrderProductItem
{ {
/// <summary>
/// ProductId
/// </summary>
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Sku { get; set; } public string Sku { get; set; }
@ -684,24 +614,10 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
public override string ToString() public override string ToString()
{ {
return $"{nameof(OrderProductItem)} [ProductId: {Id}; Name: {Name}; Sku: {Sku}; Quantity: {Quantity}; Price: {Price}]"; return $"{nameof(OrderProductItem)} [Id: {Id}; Name: {Name}; Sku: {Sku}; Quantity: {Quantity}; Price: {Price}]";
} }
} }
public class AddProductModel : OrderProductItem
{
///// <summary>
///// ProductId
///// </summary>
//public int Id { get; set; }
//public string Name { get; set; }
//public string Sku { get; set; }
//public int Quantity { get; set; }
//public decimal Price { get; set; }
public int StockQuantity { get; set; }
public int IncomingQuantity { get; set; }
}
//private static OrderItem CreateOrderItem(ProductToAuctionMapping productToAuction, Order order, decimal orderTotal) //private static OrderItem CreateOrderItem(ProductToAuctionMapping productToAuction, Order order, decimal orderTotal)
//{ //{
// return new OrderItem // return new OrderItem
@ -744,23 +660,17 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
// }; // };
//} //}
private static OrderNote CreateOrderNote(int orderId, bool displayToCustomer, string note) private static OrderNote CreateOrderNote(Order order, string note)
{ {
return new OrderNote return new OrderNote
{ {
CreatedOnUtc = DateTime.UtcNow,//order.CreatedOnUtc, CreatedOnUtc = order.CreatedOnUtc,
DisplayToCustomer = displayToCustomer, DisplayToCustomer = true,
OrderId = orderId, OrderId = order.Id,
Note = note Note = note
}; };
} }
public Task InsertOrderNoteAsync(int orderId, bool displayToCustomer, string note)
{
var orderNote = CreateOrderNote(orderId, displayToCustomer, note);
return _orderService.InsertOrderNoteAsync(orderNote);
}
private static string SerializeCustomValuesToXml(Dictionary<string, object> sourceDictionary) private static string SerializeCustomValuesToXml(Dictionary<string, object> sourceDictionary)
{ {
ArgumentNullException.ThrowIfNull(sourceDictionary); ArgumentNullException.ThrowIfNull(sourceDictionary);
@ -1293,31 +1203,152 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
_logger.Info($"AddProductToOrder - OrderId: {orderId}, ProductsJson: {productsJson}"); _logger.Info($"AddProductToOrder - OrderId: {orderId}, ProductsJson: {productsJson}");
if (!await _permissionService.AuthorizeAsync(StandardPermission.Orders.ORDERS_CREATE_EDIT_DELETE)) if (!await _permissionService.AuthorizeAsync(StandardPermission.Orders.ORDERS_CREATE_EDIT_DELETE))
{
return Json(new { success = false, message = "Access denied" }); return Json(new { success = false, message = "Access denied" });
}
if (string.IsNullOrEmpty(productsJson)) if (string.IsNullOrEmpty(productsJson))
{
return Json(new { success = false, message = "No products data received" }); return Json(new { success = false, message = "No products data received" });
}
var order = await _orderService.GetOrderByIdAsync(orderId); var order = await _orderService.GetOrderByIdAsync(orderId);
if (order == null || order.Deleted) if (order == null || order.Deleted)
{
return Json(new { success = false, message = "Order not found" }); return Json(new { success = false, message = "Order not found" });
}
// Deserialize products // Deserialize products
var products = productsJson.JsonTo<List<AddProductModel>>(); //JsonConvert.DeserializeObject<List<AddProductModel>>(productsJson); var products = JsonConvert.DeserializeObject<List<AddProductModel>>(productsJson);
if (products == null || products.Count == 0) if (products == null || !products.Any())
{
return Json(new { success = false, message = "No products to add" }); return Json(new { success = false, message = "No products to add" });
}
var productDtosByOrderItemId = await _dbContext.ProductDtos.GetAllByIds(products.Select(x => x.Id).ToArray()).ToDictionaryAsync(k => k.Id, v => v);
var customer = await _customerService.GetCustomerByIdAsync(order.CustomerId); var customer = await _customerService.GetCustomerByIdAsync(order.CustomerId);
var store = await _storeContext.GetCurrentStoreAsync(); var store = await _storeContext.GetCurrentStoreAsync();
var admin = await _workContext.GetCurrentCustomerAsync(); var admin = await _workContext.GetCurrentCustomerAsync();
string errorMessage = ""; string errorMessage = "";
var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ => var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ =>
{ {
await AddOrderItemsThenUpdateOrder(order, products, true, customer, store, admin); // Add each product to the order
foreach (var productModel in products)
{
var product = await _productService.GetProductByIdAsync(productModel.Id);
if (product == null)
{
_logger.Warning($"Product with ID {productModel.Id} not found");
continue;
}
// Validate stock
//var stockQuantity = await _productService.GetTotalStockQuantityAsync(product);
var productDto = productDtosByOrderItemId[productModel.Id];
var isMeasurable = productDto.IsMeasurable;
//if (stockQuantity < productModel.Quantity)
//{
// return Json(new
// {
// success = false,
// message = $"Product '{product.Name}' has insufficient stock. Available: {stockQuantity}, Requested: {productModel.Quantity}"
// });
//}
if ((product.StockQuantity + productDto.IncomingQuantity) - productModel.Quantity < 0)
{
errorMessage = $"Nem elérhető készleten!";
var errorText = $"((product.StockQuantity + productDto.IncomingQuantity) - item.Quantity < 0); productId: {product.Id}; (product.StockQuantity + productDto.IncomingQuantity) - item.Quantity: {(product.StockQuantity + productDto.IncomingQuantity) - productModel.Quantity}";
_logger.Error($"{errorText}");
throw new Exception($"{errorText}");
}
// Get or calculate price
//var unitPrice = productModel.Price > 0
// ? productModel.Price
// : (await _priceCalculationService.GetFinalPriceAsync(product, customer, store)).finalPrice;
var valami = await _priceCalculationService.GetFinalPriceAsync(product, customer, store, includeDiscounts: true);
var unitPrice = valami.finalPrice;
// Calculate tax
var (unitPriceInclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPrice, true, customer);
var (unitPriceExclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPrice, false, customer);
// Create order item
var orderItem = new OrderItem
{
OrderItemGuid = Guid.NewGuid(),
OrderId = order.Id,
ProductId = product.Id,
Quantity = productModel.Quantity,
UnitPriceInclTax = unitPriceInclTaxValue,
UnitPriceExclTax = unitPriceExclTaxValue,
PriceInclTax = isMeasurable ? 0 : unitPriceInclTaxValue * productModel.Quantity,
PriceExclTax = isMeasurable ? 0 : unitPriceExclTaxValue * productModel.Quantity,
OriginalProductCost = await _priceCalculationService.GetProductCostAsync(product, null),
//UnitPriceInclTax = unitPriceInclTaxValue,
//UnitPriceExclTax = unitPriceExclTaxValue,
//PriceInclTax = unitPriceInclTaxValue * productModel.Quantity,
//PriceExclTax = unitPriceExclTaxValue * productModel.Quantity,
//OriginalProductCost = await _priceCalculationService.GetProductCostAsync(product, null),
DiscountAmountInclTax = decimal.Zero,
DiscountAmountExclTax = decimal.Zero,
DownloadCount = 0,
IsDownloadActivated = false,
LicenseDownloadId = 0,
ItemWeight = product.Weight * productModel.Quantity,
RentalStartDateUtc = null,
RentalEndDateUtc = null
};
await _orderService.InsertOrderItemAsync(orderItem);
// Adjust inventory
await _productService.AdjustInventoryAsync(
product,
-productModel.Quantity,
orderItem.AttributesXml,
string.Format(await _localizationService.GetResourceAsync("Admin.StockQuantityHistory.Messages.PlaceOrder"), order.Id));
}
// Update order totals
var orderSubtotalInclTax = decimal.Zero;
var orderSubtotalExclTax = decimal.Zero;
var orderItems = await _orderService.GetOrderItemsAsync(order.Id);
foreach (var item in orderItems)
{
orderSubtotalInclTax += item.PriceInclTax;
orderSubtotalExclTax += item.PriceExclTax;
}
order.OrderSubtotalInclTax = orderSubtotalInclTax;
order.OrderSubtotalExclTax = orderSubtotalExclTax;
order.OrderTotal = orderSubtotalInclTax + order.OrderShippingInclTax + order.PaymentMethodAdditionalFeeInclTax - order.OrderDiscount;
await _orderService.UpdateOrderAsync(order);
// Add order note
await _orderService.InsertOrderNoteAsync(new OrderNote
{
OrderId = order.Id,
Note = $"Products added to order by {admin.FirstName} {admin.LastName}, (Id: {admin.Id})",
DisplayToCustomer = false,
CreatedOnUtc = DateTime.UtcNow
});
return true; return true;
}); });
@ -1325,13 +1356,9 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
{ {
_logger.Info($"Successfully added {products.Count} products to order {orderId}"); _logger.Info($"Successfully added {products.Count} products to order {orderId}");
//var orderDto = await _dbContext.OrderDtos.GetByIdAsync(order.Id, true);
//await _sendToClient.SendMeasuringNotification("Módosult a rendelés, mérjétek újra!", orderDto);
return Json(new { success = true, message = "Products added successfully" }); return Json(new { success = true, message = "Products added successfully" });
} }
else else {
{
return Json(new { success = false, message = errorMessage }); return Json(new { success = false, message = errorMessage });
} }
} }
@ -1341,6 +1368,20 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
return Json(new { success = false, message = $"Error: {ex.Message}" }); return Json(new { success = false, message = $"Error: {ex.Message}" });
} }
} }
// Helper model for deserialization
public class AddProductModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Sku { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public int StockQuantity { get; set; }
public int IncomingQuantity { get; set; }
}
} }
} }

View File

@ -20,14 +20,12 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers;
public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, SignalRSendToClientService sendToClient, IPriceCalculationService customPriceCalculationService,IEventPublisher eventPublisher, IWorkContext workContext, IEnumerable<IAcLogWriterBase> logWriters) public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, SignalRSendToClientService sendToClient, IPriceCalculationService customPriceCalculationService,IEventPublisher eventPublisher, IWorkContext workContext, IEnumerable<IAcLogWriterBase> logWriters)
: ICustomOrderSignalREndpointServer : ICustomOrderSignalREndpointServer
{ {
private const int FromOrderDays = -14;
private readonly ILogger _logger = new Logger<CustomOrderSignalREndpoint>(logWriters.ToArray()); private readonly ILogger _logger = new Logger<CustomOrderSignalREndpoint>(logWriters.ToArray());
[SignalR(SignalRTags.GetAllOrderDtos)] [SignalR(SignalRTags.GetAllOrderDtos)]
public async Task<List<OrderDto>> GetAllOrderDtos() public async Task<List<OrderDto>> GetAllOrderDtos()
{ {
var fromDateUtc = DateTime.UtcNow.Date.AddDays(FromOrderDays); return await ctx.OrderDtos.GetAll(true).ToListAsync();
return await ctx.OrderDtos.GetAll(true).Where(o => o.CreatedOnUtc > fromDateUtc).ToListAsync();
} }
[SignalR(SignalRTags.GetOrderDtoById)] [SignalR(SignalRTags.GetOrderDtoById)]
@ -75,11 +73,11 @@ public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, SignalRSendToCli
return await ctx.OrderItemDtos.GetByIdAsync(orderItemId, true); return await ctx.OrderItemDtos.GetByIdAsync(orderItemId, true);
} }
[SignalR(SignalRTags.GetAllOrderItemDtos)] [SignalR(SignalRTags.GetAllOrderItemDtos)]
public async Task<List<OrderItemDto>> GetAllOrderItemDtos() public async Task<List<OrderItemDto>> GetAllOrderItemDtos()
{ {
var fromDateUtc = DateTime.UtcNow.Date.AddDays(FromOrderDays); return await ctx.OrderItemDtos.GetAll(true).ToListAsync();
return await ctx.OrderItemDtos.GetAll(true).Where(oi => oi.OrderDto.CreatedOnUtc > fromDateUtc).ToListAsync();
} }
[SignalR(SignalRTags.GetAllOrderItemDtoByOrderId)] [SignalR(SignalRTags.GetAllOrderItemDtoByOrderId)]

View File

@ -115,17 +115,6 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
return await ctx.Shippings.GetByIdAsync(id, true); return await ctx.Shippings.GetByIdAsync(id, true);
} }
[SignalR(SignalRTags.AddShipping)]
public async Task<Shipping> AddShipping(Shipping shipping)
{
ArgumentNullException.ThrowIfNull(shipping);
_logger.Detail($"AddShipping invoked; id: {shipping.Id}");
await ctx.Shippings.InsertAsync(shipping);
return await ctx.Shippings.GetByIdAsync(shipping.Id, true);
}
[SignalR(SignalRTags.UpdateShipping)] [SignalR(SignalRTags.UpdateShipping)]
public async Task<Shipping> UpdateShipping(Shipping shipping) public async Task<Shipping> UpdateShipping(Shipping shipping)
{ {
@ -134,7 +123,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
_logger.Detail($"UpdateShipping invoked; id: {shipping.Id}"); _logger.Detail($"UpdateShipping invoked; id: {shipping.Id}");
await ctx.Shippings.UpdateAsync(shipping); await ctx.Shippings.UpdateAsync(shipping);
return await ctx.Shippings.GetByIdAsync(shipping.Id, true); return await ctx.Shippings.GetByIdAsync(shipping.Id, shipping.ShippingDocuments != null);
} }
[SignalR(SignalRTags.GetShippingItems)] [SignalR(SignalRTags.GetShippingItems)]
@ -153,14 +142,6 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
return await ctx.ShippingItems.GetAllByShippingDocumentIdAsync(shippingDocumentId, true).ToListAsync(); return await ctx.ShippingItems.GetAllByShippingDocumentIdAsync(shippingDocumentId, true).ToListAsync();
} }
[SignalR(SignalRTags.GetShippingItemsByShippingId)]
public async Task<List<ShippingItem>> GetShippingItemsByShippingId(int shippingId)
{
_logger.Detail($"GetShippingItemsByShippingId invoked");
return await ctx.ShippingItems.GetAllByShippingIdAsync(shippingId, true).ToListAsync();
}
[SignalR(SignalRTags.GetShippingItemById)] [SignalR(SignalRTags.GetShippingItemById)]
public async Task<ShippingItem> GetShippingItemById(int id) public async Task<ShippingItem> GetShippingItemById(int id)
{ {
@ -272,28 +253,6 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
return await ctx.ShippingDocuments.GetByIdAsync(id, true); return await ctx.ShippingDocuments.GetByIdAsync(id, true);
} }
[SignalR(SignalRTags.GetShippingDocumentsByShippingId)]
public async Task<List<ShippingDocument>> GetShippingDocumentsByShippingId(int shippingId)
{
_logger.Detail($"GetShippingDocumentsByShippingId invoked; shippingId: {shippingId}");
return await ctx.ShippingDocuments.GetAllByShippingIdAsync(shippingId, true).ToListAsync();
}
[SignalR(SignalRTags.GetShippingDocumentsByProductId)]
public async Task<List<ShippingDocument>> GetShippingDocumentsByProductId(int productId)
{
_logger.Detail($"GetShippingDocumentsByProductId invoked; productId: {productId}");
return await ctx.ShippingDocuments.GetAllByProductIdAsync(productId, true).ToListAsync();
}
[SignalR(SignalRTags.GetShippingDocumentsByPartnerId)]
public async Task<List<ShippingDocument>> GetShippingDocumentsByPartnerId(int partnerId)
{
_logger.Detail($"GetShippingDocumentsByPartnerId invoked; partnerId: {partnerId}");
return await ctx.ShippingDocuments.GetAllByPartnerIdAsync(partnerId, true).ToListAsync();
}
[SignalR(SignalRTags.AddShippingDocument)] [SignalR(SignalRTags.AddShippingDocument)]
public async Task<ShippingDocument> AddShippingDocument(ShippingDocument shippingDocument) public async Task<ShippingDocument> AddShippingDocument(ShippingDocument shippingDocument)
{ {

View File

@ -38,15 +38,4 @@ public class ShippingDocumentDbTable : MgDbTableBase<ShippingDocument>
public Task<ShippingDocument> GetByIdAsync(int id, bool loadRelations) public Task<ShippingDocument> GetByIdAsync(int id, bool loadRelations)
=> GetAll(loadRelations).FirstOrDefaultAsync(sd => sd.Id == id); => GetAll(loadRelations).FirstOrDefaultAsync(sd => sd.Id == id);
public IQueryable<ShippingDocument> GetAllByShippingIdAsync(int shippingId, bool loadRelations)
=> GetAll(loadRelations).Where(sd => sd.ShippingId == shippingId);
public IQueryable<ShippingDocument> GetAllByProductIdAsync(int productId, bool loadRelations)
=> GetAll(loadRelations).Where(sd => sd.ShippingItems.Any(si => si.ProductId == productId));
public IQueryable<ShippingDocument> GetAllByPartnerIdAsync(int partnerId, bool loadRelations)
=> GetAll(loadRelations).Where(sd => sd.PartnerId == partnerId);
} }

View File

@ -59,9 +59,6 @@ public class ShippingItemDbTable : MgDbTableBase<ShippingItem>
public IQueryable<ShippingItem> GetAllByShippingDocumentIdAsync(int shippingDocumentId, bool loadRelations) public IQueryable<ShippingItem> GetAllByShippingDocumentIdAsync(int shippingDocumentId, bool loadRelations)
=> GetAll(loadRelations).Where(si => si.ShippingDocumentId == shippingDocumentId); => GetAll(loadRelations).Where(si => si.ShippingDocumentId == shippingDocumentId);
public IQueryable<ShippingItem> GetAllByShippingIdAsync(int shippingId, bool loadRelations)
=> GetAll(loadRelations).Where(si => si.ShippingDocument.ShippingId == shippingId);
private static void PrepareValues(ShippingItem shippingItem) private static void PrepareValues(ShippingItem shippingItem)
{ {
if (shippingItem.MeasuringCount < 1) shippingItem.MeasuringCount = 1; if (shippingItem.MeasuringCount < 1) shippingItem.MeasuringCount = 1;