initial commit
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using Application.Common.Extensions;
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.InventoryTransactionManager;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.DeliveryOrderManager.Commands;
|
||||
|
||||
public class CreateDeliveryOrderResult
|
||||
{
|
||||
public DeliveryOrder? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateDeliveryOrderRequest : IRequest<CreateDeliveryOrderResult>
|
||||
{
|
||||
public DateTime? DeliveryDate { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? SalesOrderId { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateDeliveryOrderValidator : AbstractValidator<CreateDeliveryOrderRequest>
|
||||
{
|
||||
public CreateDeliveryOrderValidator()
|
||||
{
|
||||
RuleFor(x => x.DeliveryDate).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
RuleFor(x => x.SalesOrderId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateDeliveryOrderHandler : IRequestHandler<CreateDeliveryOrderRequest, CreateDeliveryOrderResult>
|
||||
{
|
||||
private readonly ICommandRepository<DeliveryOrder> _deliveryOrderRepository;
|
||||
private readonly ICommandRepository<SalesOrderItem> _salesOrderItemRepository;
|
||||
private readonly ICommandRepository<Warehouse> _warehouseRepository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
private readonly InventoryTransactionService _inventoryTransactionService;
|
||||
|
||||
public CreateDeliveryOrderHandler(
|
||||
ICommandRepository<DeliveryOrder> deliveryOrderRepository,
|
||||
ICommandRepository<SalesOrderItem> salesOrderItemRepository,
|
||||
ICommandRepository<Warehouse> warehouseRepository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService,
|
||||
InventoryTransactionService inventoryTransactionService
|
||||
)
|
||||
{
|
||||
_deliveryOrderRepository = deliveryOrderRepository;
|
||||
_salesOrderItemRepository = salesOrderItemRepository;
|
||||
_warehouseRepository = warehouseRepository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
_inventoryTransactionService = inventoryTransactionService;
|
||||
}
|
||||
|
||||
public async Task<CreateDeliveryOrderResult> Handle(CreateDeliveryOrderRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new DeliveryOrder();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Number = _numberSequenceService.GenerateNumber(nameof(DeliveryOrder), "", "DO");
|
||||
entity.DeliveryDate = request.DeliveryDate;
|
||||
entity.Status = (DeliveryOrderStatus)int.Parse(request.Status!);
|
||||
entity.Description = request.Description;
|
||||
entity.SalesOrderId = request.SalesOrderId;
|
||||
|
||||
await _deliveryOrderRepository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
var defaultWarehouse = await _warehouseRepository
|
||||
.GetQuery()
|
||||
.ApplyIsDeletedFilter(false)
|
||||
.Where(x => x.SystemWarehouse == false)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (defaultWarehouse != null)
|
||||
{
|
||||
var items = await _salesOrderItemRepository
|
||||
.GetQuery()
|
||||
.ApplyIsDeletedFilter(false)
|
||||
.Where(x => x.SalesOrderId == entity.SalesOrderId)
|
||||
.Include(x => x.Product)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item?.Product?.Physical ?? false)
|
||||
{
|
||||
await _inventoryTransactionService.DeliveryOrderCreateInvenTrans(
|
||||
entity.Id,
|
||||
defaultWarehouse.Id,
|
||||
item.ProductId,
|
||||
item.Quantity,
|
||||
entity.CreatedById,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new CreateDeliveryOrderResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.InventoryTransactionManager;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.DeliveryOrderManager.Commands;
|
||||
|
||||
public class DeleteDeliveryOrderResult
|
||||
{
|
||||
public DeliveryOrder? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteDeliveryOrderRequest : IRequest<DeleteDeliveryOrderResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteDeliveryOrderValidator : AbstractValidator<DeleteDeliveryOrderRequest>
|
||||
{
|
||||
public DeleteDeliveryOrderValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteDeliveryOrderHandler : IRequestHandler<DeleteDeliveryOrderRequest, DeleteDeliveryOrderResult>
|
||||
{
|
||||
private readonly ICommandRepository<DeliveryOrder> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly InventoryTransactionService _inventoryTransactionService;
|
||||
|
||||
public DeleteDeliveryOrderHandler(
|
||||
ICommandRepository<DeliveryOrder> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
InventoryTransactionService inventoryTransactionService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_inventoryTransactionService = inventoryTransactionService;
|
||||
}
|
||||
|
||||
public async Task<DeleteDeliveryOrderResult> Handle(DeleteDeliveryOrderRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.DeletedById;
|
||||
|
||||
_repository.Delete(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
await _inventoryTransactionService.PropagateParentUpdate(
|
||||
entity.Id,
|
||||
nameof(DeliveryOrder),
|
||||
entity.DeliveryDate,
|
||||
(InventoryTransactionStatus?)entity.Status,
|
||||
entity.IsDeleted,
|
||||
entity.UpdatedById,
|
||||
null,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
return new DeleteDeliveryOrderResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.InventoryTransactionManager;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.DeliveryOrderManager.Commands;
|
||||
|
||||
public class UpdateDeliveryOrderResult
|
||||
{
|
||||
public DeliveryOrder? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateDeliveryOrderRequest : IRequest<UpdateDeliveryOrderResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public DateTime? DeliveryDate { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? SalesOrderId { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateDeliveryOrderValidator : AbstractValidator<UpdateDeliveryOrderRequest>
|
||||
{
|
||||
public UpdateDeliveryOrderValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.DeliveryDate).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
RuleFor(x => x.SalesOrderId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateDeliveryOrderHandler : IRequestHandler<UpdateDeliveryOrderRequest, UpdateDeliveryOrderResult>
|
||||
{
|
||||
private readonly ICommandRepository<DeliveryOrder> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly InventoryTransactionService _inventoryTransactionService;
|
||||
|
||||
public UpdateDeliveryOrderHandler(
|
||||
ICommandRepository<DeliveryOrder> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
InventoryTransactionService inventoryTransactionService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_inventoryTransactionService = inventoryTransactionService;
|
||||
}
|
||||
|
||||
public async Task<UpdateDeliveryOrderResult> Handle(UpdateDeliveryOrderRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.UpdatedById;
|
||||
|
||||
entity.DeliveryDate = request.DeliveryDate;
|
||||
entity.Status = (DeliveryOrderStatus)int.Parse(request.Status!);
|
||||
entity.Description = request.Description;
|
||||
entity.SalesOrderId = request.SalesOrderId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
await _inventoryTransactionService.PropagateParentUpdate(
|
||||
entity.Id,
|
||||
nameof(DeliveryOrder),
|
||||
entity.DeliveryDate,
|
||||
(InventoryTransactionStatus?)entity.Status,
|
||||
entity.IsDeleted,
|
||||
entity.UpdatedById,
|
||||
null,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
return new UpdateDeliveryOrderResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.DeliveryOrderManager.Queries;
|
||||
|
||||
public record GetDeliveryOrderListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Number { get; init; }
|
||||
public DateTime? DeliveryDate { get; init; }
|
||||
public DeliveryOrderStatus? Status { get; init; }
|
||||
public string? StatusName { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? SalesOrderId { get; init; }
|
||||
public string? SalesOrderNumber { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderListProfile : Profile
|
||||
{
|
||||
public GetDeliveryOrderListProfile()
|
||||
{
|
||||
CreateMap<DeliveryOrder, GetDeliveryOrderListDto>()
|
||||
.ForMember(
|
||||
dest => dest.SalesOrderNumber,
|
||||
opt => opt.MapFrom(src => src.SalesOrder != null ? src.SalesOrder.Number : string.Empty)
|
||||
)
|
||||
.ForMember(
|
||||
dest => dest.StatusName,
|
||||
opt => opt.MapFrom(src => src.Status.HasValue ? src.Status.Value.ToFriendlyName() : string.Empty)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderListResult
|
||||
{
|
||||
public List<GetDeliveryOrderListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderListRequest : IRequest<GetDeliveryOrderListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetDeliveryOrderListHandler : IRequestHandler<GetDeliveryOrderListRequest, GetDeliveryOrderListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetDeliveryOrderListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetDeliveryOrderListResult> Handle(GetDeliveryOrderListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.DeliveryOrder
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.Include(x => x.SalesOrder)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetDeliveryOrderListDto>>(entities);
|
||||
|
||||
return new GetDeliveryOrderListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.DeliveryOrderManager.Queries;
|
||||
|
||||
|
||||
public class GetDeliveryOrderSingleProfile : Profile
|
||||
{
|
||||
public GetDeliveryOrderSingleProfile()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderSingleResult
|
||||
{
|
||||
public DeliveryOrder? Data { get; init; }
|
||||
public List<InventoryTransaction>? TransactionList { get; init; }
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderSingleRequest : IRequest<GetDeliveryOrderSingleResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderSingleValidator : AbstractValidator<GetDeliveryOrderSingleRequest>
|
||||
{
|
||||
public GetDeliveryOrderSingleValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderSingleHandler : IRequestHandler<GetDeliveryOrderSingleRequest, GetDeliveryOrderSingleResult>
|
||||
{
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetDeliveryOrderSingleHandler(
|
||||
IQueryContext context
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetDeliveryOrderSingleResult> Handle(GetDeliveryOrderSingleRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var queryData = _context
|
||||
.DeliveryOrder
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesOrder)
|
||||
.ThenInclude(x => x.Customer)
|
||||
.Where(x => x.Id == request.Id)
|
||||
.AsQueryable();
|
||||
|
||||
var data = await queryData.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
var queryTransactionList = _context
|
||||
.InventoryTransaction
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(false)
|
||||
.Include(x => x.Product)
|
||||
.Include(x => x.Warehouse)
|
||||
.Include(x => x.WarehouseFrom)
|
||||
.Include(x => x.WarehouseTo)
|
||||
.Where(x => x.ModuleId == request.Id && x.ModuleName == nameof(DeliveryOrder))
|
||||
.AsQueryable();
|
||||
|
||||
var transactionList = await queryTransactionList.ToListAsync(cancellationToken);
|
||||
|
||||
return new GetDeliveryOrderSingleResult
|
||||
{
|
||||
Data = data,
|
||||
TransactionList = transactionList
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Enums;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.DeliveryOrderManager.Queries;
|
||||
|
||||
public record GetDeliveryOrderStatusListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderStatusListProfile : Profile
|
||||
{
|
||||
public GetDeliveryOrderStatusListProfile()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderStatusListResult
|
||||
{
|
||||
public List<GetDeliveryOrderStatusListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderStatusListRequest : IRequest<GetDeliveryOrderStatusListResult>
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public class GetDeliveryOrderStatusListHandler : IRequestHandler<GetDeliveryOrderStatusListRequest, GetDeliveryOrderStatusListResult>
|
||||
{
|
||||
|
||||
public GetDeliveryOrderStatusListHandler()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<GetDeliveryOrderStatusListResult> Handle(GetDeliveryOrderStatusListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var statuses = Enum.GetValues(typeof(DeliveryOrderStatus))
|
||||
.Cast<DeliveryOrderStatus>()
|
||||
.Select(status => new GetDeliveryOrderStatusListDto
|
||||
{
|
||||
Id = ((int)status).ToString(),
|
||||
Name = status.ToFriendlyName()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await Task.CompletedTask;
|
||||
|
||||
return new GetDeliveryOrderStatusListResult
|
||||
{
|
||||
Data = statuses
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user