diff --git a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs
index 9a213bd..e485604 100644
--- a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs
+++ b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs
@@ -21,6 +21,7 @@ using Mango.Nop.Core.Loggers;
using Mango.Nop.Core.Models;
using Nop.Core;
using Nop.Core.Domain.Customers;
+using Nop.Core.Domain.Orders;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using Nop.Plugin.Misc.FruitBankPlugin.Factories;
using Nop.Plugin.Misc.FruitBankPlugin.Services;
@@ -358,6 +359,49 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers
return await ctx.EkaerHistories.GetByIdAsync(ekaerHistory.Id) ?? ekaerHistory;
}
+ ///
+ /// A megadott dátumtól kezdődő, EKÁER-rekord nélküli szállítólevelekre Pending rekordot hoz létre.
+ /// User-vezérelt (toolbar gomb) — szándékosan NINCS automata rekord-érzékelés: a létrehozás explicit,
+ /// idempotens, és bármikor újrafuttatható (maga a gomb a "reconciliation").
+ ///
+ [SignalR(SignalRTags.CreateMissingEkaerHistories)]
+ public async Task CreateMissingEkaerHistories(DateTime fromDate)
+ {
+ _logger.Detail($"CreateMissingEkaerHistories invoked; fromDate: {fromDate:yyyy-MM-dd}");
+
+ // Bejövő: rekord nélküli szállítólevelek a dátumtól.
+ var missingInbound = await ctx.ShippingDocuments.GetAll(false)
+ .Where(sd => sd.ShippingDate >= fromDate)
+ .Where(sd => !ctx.EkaerHistories.Table.Any(eh => eh.ForeignKey == sd.Id && !eh.IsOutgoing))
+ .Select(sd => new EkaerHistory { ForeignKey = sd.Id, IsOutgoing = false, StatusId = (int)EkaerStatus.Pending })
+ .ToListAsync();
+
+ // Kimenő: rekord nélküli, lezárt (Complete) rendelések, DateOfReceipt a dátumtól (jövőbeli is — előre-bejelentés).
+ var missingOutgoing = await ctx.OrderDtos.GetAllByOrderStatus(OrderStatus.Complete, false)
+ .Where(o => o.GenericAttributes.Any(ga => ga.Key == nameof(OrderDto.DateOfReceipt) && DateTime.Parse(ga.Value) >= fromDate.Date))
+ .Where(o => !ctx.EkaerHistories.Table.Any(eh => eh.ForeignKey == o.Id && eh.IsOutgoing))
+ .Select(o => new EkaerHistory { ForeignKey = o.Id, IsOutgoing = true, StatusId = (int)EkaerStatus.Pending })
+ .ToListAsync();
+
+ var createdCount = 0;
+
+ foreach (var ekaerHistory in missingInbound.Concat(missingOutgoing))
+ {
+ try
+ {
+ await ctx.EkaerHistories.InsertAsync(ekaerHistory);
+ createdCount++;
+ }
+ catch (Exception ex)
+ {
+ _logger.Error($"CreateMissingEkaerHistories; insert failed; ForeignKey: {ekaerHistory.ForeignKey}; IsOutgoing: {ekaerHistory.IsOutgoing}", ex);
+ }
+ }
+
+ _logger.Info($"CreateMissingEkaerHistories; created: {createdCount} (inbound: {missingInbound.Count}, outgoing: {missingOutgoing.Count}); fromDate: {fromDate:yyyy-MM-dd}");
+ return createdCount;
+ }
+
[SignalR(SignalRTags.GetShippings)]
public async Task> GetShippings()
{