You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Logistics/Connected.Logistics.Process.../Listeners/PlannedItemListener.cs

75 lines
2.3 KiB

using Connected.Logistics.Documents.Receive;
using Connected.Middleware.Annotations;
2 years ago
using Connected.Notifications;
using Connected.Notifications.Events;
using Connected.ServiceModel;
using Microsoft.Extensions.Logging;
namespace Connected.Logistics.Documents.Listeners;
2 years ago
[Middleware<IReceivePostingDocumentService>(nameof(IReceivePostingDocumentService.PlannedItemUpdated))]
internal sealed class PlannedItemListener : EventListener<PrimaryKeyEventArgs<long>>
{
public PlannedItemListener(ILogger<PlannedItemListener> logger, IReceivePostingDocumentService documents, IReceiveDocumentService receiveDocuments)
{
Logger = logger;
Documents = documents;
ReceiveDocuments = receiveDocuments;
}
private ILogger<PlannedItemListener> Logger { get; }
private IReceivePostingDocumentService Documents { get; }
private IReceiveDocumentService ReceiveDocuments { get; }
protected override async Task OnInvoke()
{
if (await Documents.SelectPlannedItem(Arguments.Id) is not IReceivePlannedItem item)
{
Logger.LogWarning("The IReceivePlannedItem not found ({id}}.", Arguments.Id);
return;
}
if (await ReceiveDocuments.SelectItem(item.Item) is not IReceiveItem receiveItem)
{
Logger.LogWarning("The IReceiveItem not found ({id}}.", item.Item);
return;
}
if (await Documents.Select(item.Document) is not IReceivePostingDocument document)
{
Logger.LogWarning("The IReceivePostingDocument not found ({id}}.", item.Document);
return;
}
await UpdateOpenItems(document);
await UpdatePostedQuantity(receiveItem);
}
private async Task UpdateOpenItems(IReceivePostingDocument document)
{
var items = await Documents.QueryPlannedItems(new PrimaryKeyArgs<int> { Id = document.Id });
await Documents.Patch(new PatchArgs<int>
{
Id = document.Id,
Properties = new Dictionary<string, object>
{
{nameof(IReceivePostingDocument.OpenItemCount), items.Count(f => f.PostedQuantity < f.Quantity) }
}
});
}
private async Task UpdatePostedQuantity(IReceiveItem item)
{
var items = await Documents.QueryPlannedItems(new PrimaryKeyArgs<long> { Id = item.Id });
await Documents.PatchPlanedItem(new PatchArgs<long>
{
Id = item.Id,
Properties = new Dictionary<string, object>
{
{nameof(IReceiveItem.PostedQuantity), items.Sum(f => f.PostedQuantity) }
}
});
}
}