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/PostingItemListener.cs

90 lines
2.6 KiB

using Connected.Logistics.Documents.Receive;
using Connected.Logistics.Stock;
using Connected.Logistics.Types.Serials;
using Connected.Middleware.Annotations;
2 years ago
using Connected.Notifications;
using Connected.Notifications.Events;
using Microsoft.Extensions.Logging;
namespace Connected.Logistics.Documents.Listeners;
2 years ago
/// <summary>
/// Represents the event listener to the <see cref="IReceivePostingDocumentService"/> Updated event.
/// </summary>
/// <remarks>
/// This middleware reacts when the item is inserted and updates the <see cref="IStockItem"/>.
/// </remarks>
[Middleware<IReceivePostingDocumentService>(nameof(IReceivePostingDocumentService.ItemInserted))]
internal sealed class PostingItemListener : EventListener<PrimaryKeyEventArgs<long>>
{
/// <summary>
/// Creates a new instance of the <see cref="PostingItemListener"/>
/// </summary>
public PostingItemListener(ILogger<PostingItemListener> logger, IStockService stock, IReceivePostingDocumentService documents, ISerialService serials)
{
Logger = logger;
Stock = stock;
Documents = documents;
Serials = serials;
}
private ILogger<PostingItemListener> Logger { get; }
private IStockService Stock { get; }
private IReceivePostingDocumentService Documents { get; }
private ISerialService Serials { get; }
protected override async Task OnInvoke()
{
/*
* Stage 1 is to prepare all data neede to perform operation
*
* Load posting item
*/
if (await Documents.SelectItem(Arguments.Id) is not IReceivePostingItem item)
{
Logger.LogWarning("The IReceivePostingItem not found ({id}}.", Arguments.Id);
return;
}
/*
* Now load the serial number
*/
if (await Serials.Select(item.Serial) is not ISerial serial)
{
Logger.LogWarning("The ISerial not found ({id}}.", item.Serial);
return;
}
/*
* Now load the serial number
*/
if (await Documents.SelectPlannedItem(new SelectReceivePlannedItemArgs
{
Document = item.Document,
Entity = serial.Entity,
EntityId = serial.EntityId
}) is not ISerial plannedItem)
{
Logger.LogWarning("The IReceivePlannedItem not found ({entity}, {entityId}).", serial.Entity, serial.EntityId);
return;
}
/*
* The idea here is simple:
* update (increase) the stock for the specified item
* and posted quantity and update the statictics for
* the immediate parents.
*/
await Stock.Update(new UpdateStockArgs
{
Location = item.Location,
Quantity = item.Quantity,
Serial = item.Serial
});
/*
* Now update the planned item with posted quantity
*/
await Documents.UpdatePlannedItem(new UpdateReceivePlannedItemArgs
{
Id = plannedItem.Id,
PostedQuantity = item.Quantity
});
}
}