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/Logistics.Processes.Receive/Listeners/PlannedItemListener.cs

75 lines
2.3 KiB

2 years ago
using Connected.Middleware.Annotations;
using Connected.Notifications;
using Connected.Notifications.Events;
using Connected.ServiceModel;
using Logistics.Documents.Receive;
using Microsoft.Extensions.Logging;
namespace Logistics.Documents.Listeners;
[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) }
}
});
}
}