From 6735765821777de3a7191308434f24ab5ce7b514 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 26 Oct 2025 02:48:12 +0200 Subject: [PATCH] small fixes --- .../Controllers/CustomOrderController.cs | 271 +++++++++++++++++- .../Services/InnVoiceOrderService.cs | 3 +- 2 files changed, 272 insertions(+), 2 deletions(-) diff --git a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs index a6c9707..91439e8 100644 --- a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/CustomOrderController.cs @@ -7,6 +7,7 @@ using FruitBank.Common.Server.Interfaces; using FruitBank.Common.SignalRs; using Mango.Nop.Core.Extensions; using Mango.Nop.Core.Loggers; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Nop.Core; @@ -22,6 +23,8 @@ using Nop.Plugin.Misc.FruitBankPlugin.Models.Orders; using Nop.Services.Catalog; using Nop.Services.Common; using Nop.Services.Customers; +using Nop.Services.ExportImport; +using Nop.Services.Helpers; using Nop.Services.Localization; using Nop.Services.Logging; using Nop.Services.Messages; @@ -35,6 +38,7 @@ using Nop.Web.Areas.Admin.Models.Orders; using Nop.Web.Framework; using Nop.Web.Framework.Controllers; using Nop.Web.Framework.Mvc.Filters; +using System.Text; using System.Text.Json.Serialization; using System.Xml; using System.Xml.Serialization; @@ -61,10 +65,36 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers protected readonly IEventPublisher _eventPublisher; protected readonly ILocalizationService _localizationService; protected readonly ICustomerActivityService _customerActivityService; + protected readonly IExportManager _exportManager; + protected readonly IGiftCardService _giftCardService; + protected readonly IImportManager _importManager; + protected readonly IDateTimeHelper _dateTimeHelper; + private static readonly char[] _separator = [',']; // ... other dependencies private readonly Mango.Nop.Core.Loggers.ILogger _logger; + protected virtual async ValueTask HasAccessToOrderAsync(Order order) + { + return order != null && await HasAccessToOrderAsync(order.Id); + } + + protected virtual async Task HasAccessToOrderAsync(int orderId) + { + if (orderId == 0) + return false; + + var currentVendor = await _workContext.GetCurrentVendorAsync(); + if (currentVendor == null) + //not a vendor; has access + return true; + + var vendorId = currentVendor.Id; + var hasVendorProducts = (await _orderService.GetOrderItemsAsync(orderId, vendorId: vendorId)).Any(); + + return hasVendorProducts; + } + public CustomOrderController(FruitBankDbContext fruitBankDbContext, IOrderService orderService, IPriceCalculationService priceCalculationService, @@ -80,7 +110,11 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers IWorkContext workContext, IEventPublisher eventPublisher, ILocalizationService localizationService, - ICustomerActivityService customerActivityService) + ICustomerActivityService customerActivityService, + IExportManager exportManager, + IGiftCardService giftCardService, + IImportManager importManager, + IDateTimeHelper dateTimeHelper) { _logger = new Logger(logWriters.ToArray()); @@ -99,6 +133,12 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers _eventPublisher = eventPublisher; _localizationService = localizationService; _customerActivityService = customerActivityService; + + _exportManager = exportManager; + _giftCardService = giftCardService; + _importManager = importManager; + _dateTimeHelper = dateTimeHelper; + // ... initialize other deps } @@ -676,6 +716,235 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers // } //} + + //THE REST + + #region Export / Import + + [HttpPost, ActionName("ExportXml")] + [FormValueRequired("exportxml-all")] + [CheckPermission(StandardPermission.Orders.ORDERS_IMPORT_EXPORT)] + public virtual async Task ExportXmlAll(OrderSearchModel model) + { + var startDateValue = model.StartDate == null ? null + : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.StartDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync()); + + var endDateValue = model.EndDate == null ? null + : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.EndDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync()).AddDays(1); + + //a vendor should have access only to his products + var currentVendor = await _workContext.GetCurrentVendorAsync(); + if (currentVendor != null) + { + model.VendorId = currentVendor.Id; + } + + var orderStatusIds = model.OrderStatusIds != null && !model.OrderStatusIds.Contains(0) + ? model.OrderStatusIds.ToList() + : null; + var paymentStatusIds = model.PaymentStatusIds != null && !model.PaymentStatusIds.Contains(0) + ? model.PaymentStatusIds.ToList() + : null; + var shippingStatusIds = model.ShippingStatusIds != null && !model.ShippingStatusIds.Contains(0) + ? model.ShippingStatusIds.ToList() + : null; + + var filterByProductId = 0; + var product = await _productService.GetProductByIdAsync(model.ProductId); + if (product != null && (currentVendor == null || product.VendorId == currentVendor.Id)) + filterByProductId = model.ProductId; + + //load orders + var orders = await _orderService.SearchOrdersAsync(storeId: model.StoreId, + vendorId: model.VendorId, + productId: filterByProductId, + warehouseId: model.WarehouseId, + paymentMethodSystemName: model.PaymentMethodSystemName, + createdFromUtc: startDateValue, + createdToUtc: endDateValue, + osIds: orderStatusIds, + psIds: paymentStatusIds, + ssIds: shippingStatusIds, + billingPhone: model.BillingPhone, + billingEmail: model.BillingEmail, + billingLastName: model.BillingLastName, + billingCountryId: model.BillingCountryId, + orderNotes: model.OrderNotes); + + //ensure that we at least one order selected + if (!orders.Any()) + { + _notificationService.ErrorNotification(await _localizationService.GetResourceAsync("Admin.Orders.NoOrders")); + return RedirectToAction("List"); + } + + try + { + var xml = await _exportManager.ExportOrdersToXmlAsync(orders); + return File(Encoding.UTF8.GetBytes(xml), MimeTypes.ApplicationXml, "orders.xml"); + } + catch (Exception exc) + { + await _notificationService.ErrorNotificationAsync(exc); + return RedirectToAction("List"); + } + } + + [HttpPost] + [CheckPermission(StandardPermission.Orders.ORDERS_IMPORT_EXPORT)] + public virtual async Task ExportXmlSelected(string selectedIds) + { + var orders = new List(); + if (selectedIds != null) + { + var ids = selectedIds + .Split(_separator, StringSplitOptions.RemoveEmptyEntries) + .Select(x => Convert.ToInt32(x)) + .ToArray(); + orders.AddRange(await (await _orderService.GetOrdersByIdsAsync(ids)) + .WhereAwait(HasAccessToOrderAsync).ToListAsync()); + } + + try + { + var xml = await _exportManager.ExportOrdersToXmlAsync(orders); + return File(Encoding.UTF8.GetBytes(xml), MimeTypes.ApplicationXml, "orders.xml"); + } + catch (Exception exc) + { + await _notificationService.ErrorNotificationAsync(exc); + return RedirectToAction("List"); + } + } + + [HttpPost, ActionName("ExportExcel")] + [FormValueRequired("exportexcel-all")] + [CheckPermission(StandardPermission.Orders.ORDERS_IMPORT_EXPORT)] + public virtual async Task ExportExcelAll(OrderSearchModel model) + { + var startDateValue = model.StartDate == null ? null + : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.StartDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync()); + + var endDateValue = model.EndDate == null ? null + : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.EndDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync()).AddDays(1); + + //a vendor should have access only to his products + var currentVendor = await _workContext.GetCurrentVendorAsync(); + if (currentVendor != null) + { + model.VendorId = currentVendor.Id; + } + + var orderStatusIds = model.OrderStatusIds != null && !model.OrderStatusIds.Contains(0) + ? model.OrderStatusIds.ToList() + : null; + var paymentStatusIds = model.PaymentStatusIds != null && !model.PaymentStatusIds.Contains(0) + ? model.PaymentStatusIds.ToList() + : null; + var shippingStatusIds = model.ShippingStatusIds != null && !model.ShippingStatusIds.Contains(0) + ? model.ShippingStatusIds.ToList() + : null; + + var filterByProductId = 0; + var product = await _productService.GetProductByIdAsync(model.ProductId); + if (product != null && (currentVendor == null || product.VendorId == currentVendor.Id)) + filterByProductId = model.ProductId; + + //load orders + var orders = await _orderService.SearchOrdersAsync(storeId: model.StoreId, + vendorId: model.VendorId, + productId: filterByProductId, + warehouseId: model.WarehouseId, + paymentMethodSystemName: model.PaymentMethodSystemName, + createdFromUtc: startDateValue, + createdToUtc: endDateValue, + osIds: orderStatusIds, + psIds: paymentStatusIds, + ssIds: shippingStatusIds, + billingPhone: model.BillingPhone, + billingEmail: model.BillingEmail, + billingLastName: model.BillingLastName, + billingCountryId: model.BillingCountryId, + orderNotes: model.OrderNotes); + + //ensure that we at least one order selected + if (!orders.Any()) + { + _notificationService.ErrorNotification(await _localizationService.GetResourceAsync("Admin.Orders.NoOrders")); + return RedirectToAction("List"); + } + + try + { + var bytes = await _exportManager.ExportOrdersToXlsxAsync(orders); + return File(bytes, MimeTypes.TextXlsx, "orders.xlsx"); + } + catch (Exception exc) + { + await _notificationService.ErrorNotificationAsync(exc); + return RedirectToAction("List"); + } + } + + [HttpPost] + [CheckPermission(StandardPermission.Orders.ORDERS_IMPORT_EXPORT)] + public virtual async Task ExportExcelSelected(string selectedIds) + { + var orders = new List(); + if (selectedIds != null) + { + var ids = selectedIds + .Split(_separator, StringSplitOptions.RemoveEmptyEntries) + .Select(x => Convert.ToInt32(x)) + .ToArray(); + orders.AddRange(await (await _orderService.GetOrdersByIdsAsync(ids)).WhereAwait(HasAccessToOrderAsync).ToListAsync()); + } + + try + { + var bytes = await _exportManager.ExportOrdersToXlsxAsync(orders); + return File(bytes, MimeTypes.TextXlsx, "orders.xlsx"); + } + catch (Exception exc) + { + await _notificationService.ErrorNotificationAsync(exc); + return RedirectToAction("List"); + } + } + + [HttpPost] + [CheckPermission(StandardPermission.Orders.ORDERS_IMPORT_EXPORT)] + public virtual async Task ImportFromXlsx(IFormFile importexcelfile) + { + //a vendor cannot import orders + if (await _workContext.GetCurrentVendorAsync() != null) + return AccessDeniedView(); + + try + { + if (importexcelfile != null && importexcelfile.Length > 0) + { + await _importManager.ImportOrdersFromXlsxAsync(importexcelfile.OpenReadStream()); + } + else + { + _notificationService.ErrorNotification(await _localizationService.GetResourceAsync("Admin.Common.UploadFile")); + return RedirectToAction("List"); + } + + _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Orders.Imported")); + + return RedirectToAction("List"); + } + catch (Exception exc) + { + await _notificationService.ErrorNotificationAsync(exc); + return RedirectToAction("List"); + } + } + + #endregion + } } diff --git a/Nop.Plugin.Misc.AIPlugin/Services/InnVoiceOrderService.cs b/Nop.Plugin.Misc.AIPlugin/Services/InnVoiceOrderService.cs index 02f224d..b12ff60 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/InnVoiceOrderService.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/InnVoiceOrderService.cs @@ -18,7 +18,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services private readonly string _password; private readonly string _baseUrl; - public InnVoiceOrderService(string companyName = "apiteszt", string username = "apiteszt", string password = "dsjfluio4324hjhjfdhkjskjh213kjgsd", string baseUrl = "https://api.innvoice.hu") + public InnVoiceOrderService(string companyName = "fruitbank", string username = "fruitbank", string password = "YkKzM1dwJax0HTIPWIMABSqdSA", string baseUrl = "https://api.innvoice.hu") { _companyName = companyName ?? throw new ArgumentNullException(nameof(companyName)); _username = username ?? throw new ArgumentNullException(nameof(username)); @@ -54,6 +54,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Services try { var response = await _httpClient.PostAsync(url, content); + Console.WriteLine($"InnVoice API CreateOrdersAsync response status: {response.StatusCode}"); response.EnsureSuccessStatusCode(); var responseContent = await response.Content.ReadAsStringAsync();