From 139413b1ae765b8aca6011360ee9d1a563c758e0 Mon Sep 17 00:00:00 2001 From: Loretta Date: Fri, 21 Nov 2025 07:20:43 +0100 Subject: [PATCH 1/3] improvements --- .../Controllers/CustomOrderController.cs | 411 ++++++++---------- .../Controllers/FruitBankDataController.cs | 8 + .../Domains/DataLayer/ShippingItemDbTable.cs | 3 + 3 files changed, 196 insertions(+), 226 deletions(-) diff --git a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs index 3474ada..c92de77 100644 --- a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs @@ -3,6 +3,7 @@ using AyCode.Core.Loggers; using AyCode.Services.Server.SignalRs; using AyCode.Services.SignalRs; using AyCode.Utils.Extensions; +using DocumentFormat.OpenXml.Spreadsheet; using FluentMigrator.Runner.Generators.Base; using FruitBank.Common.Dtos; using FruitBank.Common.Entities; @@ -18,10 +19,12 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Newtonsoft.Json; using Nop.Core; +using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Customers; using Nop.Core.Domain.Orders; using Nop.Core.Domain.Payments; using Nop.Core.Domain.Shipping; +using Nop.Core.Domain.Stores; using Nop.Core.Domain.Tax; using Nop.Core.Events; using Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Models.Order; @@ -50,8 +53,10 @@ using Nop.Web.Framework.Controllers; using Nop.Web.Framework.Mvc.Filters; using System.Text; using System.Text.Json.Serialization; +using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; +using static Nop.Services.Security.StandardPermission; namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers { @@ -513,99 +518,164 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers ShippingAddressId = customer.ShippingAddressId, PaymentMethodSystemName = "Payments.CheckMoneyOrder", // Default payment method 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 store = _storeContext.GetCurrentStore(); - var productDtosByOrderItemId = await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(x => x.Id).ToArray()).ToDictionaryAsync(k => k.Id, v => v); + var store = await _storeContext.GetCurrentStoreAsync(); + var admin = await _workContext.GetCurrentCustomerAsync(); var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ => { await _orderService.InsertOrderAsync(order); - - order.OrderTotal = 0; - - foreach (var item in orderProducts) - { - var product = await _productService.GetProductByIdAsync(item.Id); - if (product == null) - { - var errorText = $"product == null; productId: {item.Id};"; - - _logger.Error($"{errorText}"); - throw new Exception($"{errorText}"); - } - - var productDto = productDtosByOrderItemId[item.Id]; - var isMeasurable = productDto.IsMeasurable; - - if ((product.StockQuantity + productDto.IncomingQuantity) - item.Quantity < 0) - { - var errorText = $"((product.StockQuantity + productDto.IncomingQuantity) - item.Quantity < 0); productId: {product.Id}; (product.StockQuantity + productDto.IncomingQuantity) - item.Quantity: {(product.StockQuantity + productDto.IncomingQuantity) - item.Quantity}"; - - _logger.Error($"{errorText}"); - throw new Exception($"{errorText}"); - } - - 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); - - - var orderItem = new OrderItem - { - OrderId = order.Id, - ProductId = item.Id, - Quantity = item.Quantity, - - UnitPriceInclTax = unitPriceInclTaxValue, - UnitPriceExclTax = unitPriceExclTaxValue, - - PriceInclTax = isMeasurable ? 0 : unitPriceInclTaxValue * item.Quantity, - PriceExclTax = isMeasurable ? 0 : unitPriceExclTaxValue * item.Quantity, - - OriginalProductCost = await _priceCalculationService.GetProductCostAsync(product, null), - - AttributeDescription = string.Empty, - AttributesXml = string.Empty, - DiscountAmountInclTax = 0, - DiscountAmountExclTax = 0 - }; - - 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, ""); - } - order.CustomOrderNumber = order.Id.ToString(); - order.OrderSubtotalInclTax = order.OrderTotal; - order.OrderSubtotalExclTax = order.OrderTotal; - order.OrderSubTotalDiscountInclTax = order.OrderTotal; - order.OrderSubTotalDiscountExclTax = order.OrderTotal; - - 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); - + await AddOrderItemsThenUpdateOrder(order, orderProducts, true, customer, store, admin); return true; }); - if (transactionSuccess) return RedirectToAction("Edit", "Order", new { id = order.Id }); + if (transactionSuccess) + { + //var orderDto = await _dbContext.OrderDtos.GetByIdAsync(order.Id, true); + //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 }); } - public class OrderProductItem + private async Task AddOrderItemsThenUpdateOrder(Order order, IReadOnlyList 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) + { + _logger.Warning($"Product with ID {orderProductItem.Id} not found"); + 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}"); + throw new Exception($"{errorText}"); + } + + var orderItem = await CreateOrderItem(product, order, orderProductItem, isMeasurable, unitPricesIncludeDiscounts, customer, store); + + await _orderService.InsertOrderItemAsync(orderItem); + 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 CreateOrderItem(Product product, Order order, TOrderProductItem orderProductItem, bool isMeasurable, bool unitPricesIncludeDiscounts, Customer customer = null, Store store = null) + where TOrderProductItem : IOrderProductItemBase + { + if (product.Id != orderProductItem.Id) + throw new Exception($"CustomOrderController->CreateOrderItem; (product.Id != orderProductItem.Id)"); + + store ??= await _storeContext.GetCurrentStoreAsync(); + customer ??= await _workContext.GetCurrentCustomerAsync(); + + var priceCalculation = await _priceCalculationService.GetFinalPriceAsync(product, customer, store, includeDiscounts: unitPricesIncludeDiscounts); + var unitPriceInclTaxValue = priceCalculation.finalPrice; + + // Calculate tax + //var (unitPriceInclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPrice, true, customer); + var (unitPriceExclTaxValue, _) = await _taxService.GetProductPriceAsync(product, unitPriceInclTaxValue, false, customer); + + return new OrderItem + { + OrderId = order.Id, + ProductId = orderProductItem.Id, + Quantity = orderProductItem.Quantity, + + OrderItemGuid = Guid.NewGuid(), + + UnitPriceInclTax = unitPriceInclTaxValue, + UnitPriceExclTax = unitPriceExclTaxValue, + + PriceInclTax = isMeasurable ? 0 : unitPriceInclTaxValue * orderProductItem.Quantity, + PriceExclTax = isMeasurable ? 0 : unitPriceExclTaxValue * orderProductItem.Quantity, + + OriginalProductCost = await _priceCalculationService.GetProductCostAsync(product, null), + + AttributeDescription = string.Empty, + AttributesXml = string.Empty, + + DiscountAmountInclTax = decimal.Zero, + DiscountAmountExclTax = decimal.Zero, + + DownloadCount = 0, + IsDownloadActivated = false, + LicenseDownloadId = 0, + ItemWeight = product.Weight * orderProductItem.Quantity, + RentalStartDateUtc = null, + RentalEndDateUtc = null + }; + } + + public interface IOrderProductItemBase + { + /// + /// ProductId + /// + public int Id { get; set; } + public int Quantity { get; set; } + public decimal Price { get; set; } + } + + public class OrderProductItem : IOrderProductItemBase + { + /// + /// ProductId + /// public int Id { get; set; } public string Name { get; set; } public string Sku { get; set; } @@ -614,10 +684,24 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers public override string ToString() { - return $"{nameof(OrderProductItem)} [Id: {Id}; Name: {Name}; Sku: {Sku}; Quantity: {Quantity}; Price: {Price}]"; + return $"{nameof(OrderProductItem)} [ProductId: {Id}; Name: {Name}; Sku: {Sku}; Quantity: {Quantity}; Price: {Price}]"; } } + public class AddProductModel : OrderProductItem + { + ///// + ///// ProductId + ///// + //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) //{ // return new OrderItem @@ -660,17 +744,23 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers // }; //} - private static OrderNote CreateOrderNote(Order order, string note) + private static OrderNote CreateOrderNote(int orderId, bool displayToCustomer, string note) { return new OrderNote { - CreatedOnUtc = order.CreatedOnUtc, - DisplayToCustomer = true, - OrderId = order.Id, + CreatedOnUtc = DateTime.UtcNow,//order.CreatedOnUtc, + DisplayToCustomer = displayToCustomer, + OrderId = orderId, 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 sourceDictionary) { ArgumentNullException.ThrowIfNull(sourceDictionary); @@ -1203,162 +1293,45 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers _logger.Info($"AddProductToOrder - OrderId: {orderId}, ProductsJson: {productsJson}"); if (!await _permissionService.AuthorizeAsync(StandardPermission.Orders.ORDERS_CREATE_EDIT_DELETE)) - { return Json(new { success = false, message = "Access denied" }); - } - + if (string.IsNullOrEmpty(productsJson)) - { return Json(new { success = false, message = "No products data received" }); - } - + var order = await _orderService.GetOrderByIdAsync(orderId); + if (order == null || order.Deleted) - { return Json(new { success = false, message = "Order not found" }); - } - + // Deserialize products - var products = JsonConvert.DeserializeObject>(productsJson); + var products = productsJson.JsonTo>(); //JsonConvert.DeserializeObject>(productsJson); - if (products == null || !products.Any()) - { + if (products == null || products.Count == 0) 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 store = await _storeContext.GetCurrentStoreAsync(); var admin = await _workContext.GetCurrentCustomerAsync(); + string errorMessage = ""; var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ => { - // 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 - }); - + await AddOrderItemsThenUpdateOrder(order, products, true, customer, store, admin); return true; }); - if(transactionSuccess) + if (transactionSuccess) { _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" }); } - else { + else + { return Json(new { success = false, message = errorMessage }); } } @@ -1368,20 +1341,6 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers 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; } - } - - } } diff --git a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs index b0caa6f..38d33bd 100644 --- a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs @@ -142,6 +142,14 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers return await ctx.ShippingItems.GetAllByShippingDocumentIdAsync(shippingDocumentId, true).ToListAsync(); } + [SignalR(SignalRTags.GetShippingItemsByShippingId)] + public async Task> GetShippingItemsByShippingId(int shippingId) + { + _logger.Detail($"GetShippingItemsByShippingId invoked"); + + return await ctx.ShippingItems.GetAllByShippingIdAsync(shippingId, true).ToListAsync(); + } + [SignalR(SignalRTags.GetShippingItemById)] public async Task GetShippingItemById(int id) { diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemDbTable.cs index d88cc73..14a187f 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingItemDbTable.cs @@ -59,6 +59,9 @@ public class ShippingItemDbTable : MgDbTableBase public IQueryable GetAllByShippingDocumentIdAsync(int shippingDocumentId, bool loadRelations) => GetAll(loadRelations).Where(si => si.ShippingDocumentId == shippingDocumentId); + public IQueryable GetAllByShippingIdAsync(int shippingId, bool loadRelations) + => GetAll(loadRelations).Where(si => si.ShippingDocument.ShippingId == shippingId); + private static void PrepareValues(ShippingItem shippingItem) { if (shippingItem.MeasuringCount < 1) shippingItem.MeasuringCount = 1; From b5057b9a4d2c122c1f764459f719a797c781e046 Mon Sep 17 00:00:00 2001 From: Loretta Date: Fri, 21 Nov 2025 16:29:12 +0100 Subject: [PATCH 2/3] improvements --- .../Controllers/FruitBankDataController.cs | 35 ++++++++++++++++++- .../DataLayer/ShippingDocumentDbTable.cs | 11 ++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs index 38d33bd..4b03e32 100644 --- a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs @@ -115,6 +115,17 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers return await ctx.Shippings.GetByIdAsync(id, true); } + [SignalR(SignalRTags.AddShipping)] + public async Task 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)] public async Task UpdateShipping(Shipping shipping) { @@ -123,7 +134,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers _logger.Detail($"UpdateShipping invoked; id: {shipping.Id}"); await ctx.Shippings.UpdateAsync(shipping); - return await ctx.Shippings.GetByIdAsync(shipping.Id, shipping.ShippingDocuments != null); + return await ctx.Shippings.GetByIdAsync(shipping.Id, true); } [SignalR(SignalRTags.GetShippingItems)] @@ -261,6 +272,28 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers return await ctx.ShippingDocuments.GetByIdAsync(id, true); } + [SignalR(SignalRTags.GetShippingDocumentsByShippingId)] + public async Task> GetShippingDocumentsByShippingId(int shippingId) + { + _logger.Detail($"GetShippingDocumentsByShippingId invoked; shippingId: {shippingId}"); + + return await ctx.ShippingDocuments.GetAllByShippingIdAsync(shippingId, true).ToListAsync(); + } + [SignalR(SignalRTags.GetShippingDocumentsByProductId)] + public async Task> GetShippingDocumentsByProductId(int productId) + { + _logger.Detail($"GetShippingDocumentsByProductId invoked; productId: {productId}"); + + return await ctx.ShippingDocuments.GetAllByProductIdAsync(productId, true).ToListAsync(); + } + [SignalR(SignalRTags.GetShippingDocumentsByPartnerId)] + public async Task> GetShippingDocumentsByPartnerId(int partnerId) + { + _logger.Detail($"GetShippingDocumentsByPartnerId invoked; partnerId: {partnerId}"); + + return await ctx.ShippingDocuments.GetAllByPartnerIdAsync(partnerId, true).ToListAsync(); + } + [SignalR(SignalRTags.AddShippingDocument)] public async Task AddShippingDocument(ShippingDocument shippingDocument) { diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingDocumentDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingDocumentDbTable.cs index 50ce936..c02f3b5 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingDocumentDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/ShippingDocumentDbTable.cs @@ -38,4 +38,15 @@ public class ShippingDocumentDbTable : MgDbTableBase public Task GetByIdAsync(int id, bool loadRelations) => GetAll(loadRelations).FirstOrDefaultAsync(sd => sd.Id == id); + + public IQueryable GetAllByShippingIdAsync(int shippingId, bool loadRelations) + => GetAll(loadRelations).Where(sd => sd.ShippingId == shippingId); + + public IQueryable GetAllByProductIdAsync(int productId, bool loadRelations) + => GetAll(loadRelations).Where(sd => sd.ShippingItems.Any(si => si.ProductId == productId)); + + public IQueryable GetAllByPartnerIdAsync(int partnerId, bool loadRelations) + => GetAll(loadRelations).Where(sd => sd.PartnerId == partnerId); + + } \ No newline at end of file From 7c07811ef63b6ca9ffdbd78d074dd3af3b314de1 Mon Sep 17 00:00:00 2001 From: Loretta Date: Fri, 21 Nov 2025 21:40:29 +0100 Subject: [PATCH 3/3] fromDateUtc --- .../Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs index 65d7d0e..37f0537 100644 --- a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs +++ b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderSignalREndpoint.cs @@ -20,12 +20,14 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers; public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, SignalRSendToClientService sendToClient, IPriceCalculationService customPriceCalculationService,IEventPublisher eventPublisher, IWorkContext workContext, IEnumerable logWriters) : ICustomOrderSignalREndpointServer { + private const int FromOrderDays = -14; private readonly ILogger _logger = new Logger(logWriters.ToArray()); [SignalR(SignalRTags.GetAllOrderDtos)] public async Task> GetAllOrderDtos() { - return await ctx.OrderDtos.GetAll(true).ToListAsync(); + var fromDateUtc = DateTime.UtcNow.Date.AddDays(FromOrderDays); + return await ctx.OrderDtos.GetAll(true).Where(o => o.CreatedOnUtc > fromDateUtc).ToListAsync(); } [SignalR(SignalRTags.GetOrderDtoById)] @@ -73,11 +75,11 @@ public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, SignalRSendToCli return await ctx.OrderItemDtos.GetByIdAsync(orderItemId, true); } - [SignalR(SignalRTags.GetAllOrderItemDtos)] public async Task> GetAllOrderItemDtos() { - return await ctx.OrderItemDtos.GetAll(true).ToListAsync(); + var fromDateUtc = DateTime.UtcNow.Date.AddDays(FromOrderDays); + return await ctx.OrderItemDtos.GetAll(true).Where(oi => oi.OrderDto.CreatedOnUtc > fromDateUtc).ToListAsync(); } [SignalR(SignalRTags.GetAllOrderItemDtoByOrderId)]