58 lines
2.1 KiB
C#
58 lines
2.1 KiB
C#
using Indotalent.ConfigBackEnd.Extensions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
|
|
|
public class CreateDeliveryOrderItemRequest
|
|
{
|
|
public string? DeliveryOrderId { get; set; }
|
|
public string? WarehouseId { get; set; }
|
|
public string? ProductId { get; set; }
|
|
public double? Movement { get; set; }
|
|
}
|
|
|
|
public record CreateDeliveryOrderItemCommand(CreateDeliveryOrderItemRequest Data) : IRequest<bool>;
|
|
|
|
public class CreateDeliveryOrderItemHandler : IRequestHandler<CreateDeliveryOrderItemCommand, bool>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
public CreateDeliveryOrderItemHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<bool> Handle(CreateDeliveryOrderItemCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var parent = await _context.DeliveryOrder
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.DeliveryOrderId, cancellationToken);
|
|
|
|
if (parent == null) return false;
|
|
|
|
var ivtEntityName = nameof(Data.Entities.InventoryTransaction);
|
|
var autoNo = await _context.GenerateAutoNumberAsync(
|
|
entityName: ivtEntityName,
|
|
prefixTemplate: $"{ivtEntityName.ToShortNameConsonant(3)}/{{Year}}/",
|
|
ct: cancellationToken
|
|
);
|
|
|
|
var entity = new Data.Entities.InventoryTransaction
|
|
{
|
|
AutoNumber = autoNo,
|
|
ModuleId = parent.Id,
|
|
ModuleName = nameof(Data.Entities.DeliveryOrder),
|
|
ModuleCode = "DO",
|
|
ModuleNumber = parent.AutoNumber,
|
|
MovementDate = parent.DeliveryDate ?? DateTime.Now,
|
|
Status = (Data.Enums.InventoryTransactionStatus)parent.Status,
|
|
WarehouseId = request.Data.WarehouseId,
|
|
ProductId = request.Data.ProductId,
|
|
Movement = request.Data.Movement
|
|
};
|
|
|
|
_context.CalculateInvenTrans(entity);
|
|
_context.InventoryTransaction.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return true;
|
|
}
|
|
} |