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.GoodsReceiveManager.Commands;
|
||||
|
||||
public class CreateGoodsReceiveResult
|
||||
{
|
||||
public GoodsReceive? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateGoodsReceiveRequest : IRequest<CreateGoodsReceiveResult>
|
||||
{
|
||||
public DateTime? ReceiveDate { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? PurchaseOrderId { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateGoodsReceiveValidator : AbstractValidator<CreateGoodsReceiveRequest>
|
||||
{
|
||||
public CreateGoodsReceiveValidator()
|
||||
{
|
||||
RuleFor(x => x.ReceiveDate).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
RuleFor(x => x.PurchaseOrderId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateGoodsReceiveHandler : IRequestHandler<CreateGoodsReceiveRequest, CreateGoodsReceiveResult>
|
||||
{
|
||||
private readonly ICommandRepository<GoodsReceive> _deliveryOrderRepository;
|
||||
private readonly ICommandRepository<PurchaseOrderItem> _purchaseOrderItemRepository;
|
||||
private readonly ICommandRepository<Warehouse> _warehouseRepository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
private readonly InventoryTransactionService _inventoryTransactionService;
|
||||
|
||||
public CreateGoodsReceiveHandler(
|
||||
ICommandRepository<GoodsReceive> deliveryOrderRepository,
|
||||
ICommandRepository<PurchaseOrderItem> purchaseOrderItemRepository,
|
||||
ICommandRepository<Warehouse> warehouseRepository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService,
|
||||
InventoryTransactionService inventoryTransactionService
|
||||
)
|
||||
{
|
||||
_deliveryOrderRepository = deliveryOrderRepository;
|
||||
_purchaseOrderItemRepository = purchaseOrderItemRepository;
|
||||
_warehouseRepository = warehouseRepository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
_inventoryTransactionService = inventoryTransactionService;
|
||||
}
|
||||
|
||||
public async Task<CreateGoodsReceiveResult> Handle(CreateGoodsReceiveRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new GoodsReceive();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Number = _numberSequenceService.GenerateNumber(nameof(GoodsReceive), "", "GR");
|
||||
entity.ReceiveDate = request.ReceiveDate;
|
||||
entity.Status = (GoodsReceiveStatus)int.Parse(request.Status!);
|
||||
entity.Description = request.Description;
|
||||
entity.PurchaseOrderId = request.PurchaseOrderId;
|
||||
|
||||
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 _purchaseOrderItemRepository
|
||||
.GetQuery()
|
||||
.ApplyIsDeletedFilter(false)
|
||||
.Where(x => x.PurchaseOrderId == entity.PurchaseOrderId)
|
||||
.Include(x => x.Product)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item?.Product?.Physical ?? false)
|
||||
{
|
||||
await _inventoryTransactionService.GoodsReceiveCreateInvenTrans(
|
||||
entity.Id,
|
||||
defaultWarehouse.Id,
|
||||
item.ProductId,
|
||||
item.Quantity,
|
||||
entity.CreatedById,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new CreateGoodsReceiveResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.InventoryTransactionManager;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.GoodsReceiveManager.Commands;
|
||||
|
||||
public class DeleteGoodsReceiveResult
|
||||
{
|
||||
public GoodsReceive? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteGoodsReceiveRequest : IRequest<DeleteGoodsReceiveResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteGoodsReceiveValidator : AbstractValidator<DeleteGoodsReceiveRequest>
|
||||
{
|
||||
public DeleteGoodsReceiveValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteGoodsReceiveHandler : IRequestHandler<DeleteGoodsReceiveRequest, DeleteGoodsReceiveResult>
|
||||
{
|
||||
private readonly ICommandRepository<GoodsReceive> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly InventoryTransactionService _inventoryTransactionService;
|
||||
|
||||
public DeleteGoodsReceiveHandler(
|
||||
ICommandRepository<GoodsReceive> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
InventoryTransactionService inventoryTransactionService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_inventoryTransactionService = inventoryTransactionService;
|
||||
}
|
||||
public async Task<DeleteGoodsReceiveResult> Handle(DeleteGoodsReceiveRequest 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(GoodsReceive),
|
||||
entity.ReceiveDate,
|
||||
(InventoryTransactionStatus?)entity.Status,
|
||||
entity.IsDeleted,
|
||||
entity.UpdatedById,
|
||||
null,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
return new DeleteGoodsReceiveResult
|
||||
{
|
||||
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.GoodsReceiveManager.Commands;
|
||||
|
||||
public class UpdateGoodsReceiveResult
|
||||
{
|
||||
public GoodsReceive? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateGoodsReceiveRequest : IRequest<UpdateGoodsReceiveResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public DateTime? ReceiveDate { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? PurchaseOrderId { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateGoodsReceiveValidator : AbstractValidator<UpdateGoodsReceiveRequest>
|
||||
{
|
||||
public UpdateGoodsReceiveValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.ReceiveDate).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
RuleFor(x => x.PurchaseOrderId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateGoodsReceiveHandler : IRequestHandler<UpdateGoodsReceiveRequest, UpdateGoodsReceiveResult>
|
||||
{
|
||||
private readonly ICommandRepository<GoodsReceive> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly InventoryTransactionService _inventoryTransactionService;
|
||||
|
||||
public UpdateGoodsReceiveHandler(
|
||||
ICommandRepository<GoodsReceive> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
InventoryTransactionService inventoryTransactionService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_inventoryTransactionService = inventoryTransactionService;
|
||||
}
|
||||
|
||||
public async Task<UpdateGoodsReceiveResult> Handle(UpdateGoodsReceiveRequest 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.ReceiveDate = request.ReceiveDate;
|
||||
entity.Status = (GoodsReceiveStatus)int.Parse(request.Status!);
|
||||
entity.Description = request.Description;
|
||||
entity.PurchaseOrderId = request.PurchaseOrderId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
await _inventoryTransactionService.PropagateParentUpdate(
|
||||
entity.Id,
|
||||
nameof(GoodsReceive),
|
||||
entity.ReceiveDate,
|
||||
(InventoryTransactionStatus?)entity.Status,
|
||||
entity.IsDeleted,
|
||||
entity.UpdatedById,
|
||||
null,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
return new UpdateGoodsReceiveResult
|
||||
{
|
||||
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.GoodsReceiveManager.Queries;
|
||||
|
||||
public record GetGoodsReceiveListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Number { get; init; }
|
||||
public DateTime? ReceiveDate { get; init; }
|
||||
public GoodsReceiveStatus? Status { get; init; }
|
||||
public string? StatusName { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? PurchaseOrderId { get; init; }
|
||||
public string? PurchaseOrderNumber { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveListProfile : Profile
|
||||
{
|
||||
public GetGoodsReceiveListProfile()
|
||||
{
|
||||
CreateMap<GoodsReceive, GetGoodsReceiveListDto>()
|
||||
.ForMember(
|
||||
dest => dest.PurchaseOrderNumber,
|
||||
opt => opt.MapFrom(src => src.PurchaseOrder != null ? src.PurchaseOrder.Number : string.Empty)
|
||||
)
|
||||
.ForMember(
|
||||
dest => dest.StatusName,
|
||||
opt => opt.MapFrom(src => src.Status.HasValue ? src.Status.Value.ToFriendlyName() : string.Empty)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveListResult
|
||||
{
|
||||
public List<GetGoodsReceiveListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveListRequest : IRequest<GetGoodsReceiveListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetGoodsReceiveListHandler : IRequestHandler<GetGoodsReceiveListRequest, GetGoodsReceiveListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetGoodsReceiveListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetGoodsReceiveListResult> Handle(GetGoodsReceiveListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.GoodsReceive
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.Include(x => x.PurchaseOrder)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetGoodsReceiveListDto>>(entities);
|
||||
|
||||
return new GetGoodsReceiveListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.GoodsReceiveManager.Queries;
|
||||
|
||||
|
||||
public class GetGoodsReceiveSingleProfile : Profile
|
||||
{
|
||||
public GetGoodsReceiveSingleProfile()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveSingleResult
|
||||
{
|
||||
public GoodsReceive? Data { get; init; }
|
||||
public List<InventoryTransaction>? TransactionList { get; init; }
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveSingleRequest : IRequest<GetGoodsReceiveSingleResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveSingleValidator : AbstractValidator<GetGoodsReceiveSingleRequest>
|
||||
{
|
||||
public GetGoodsReceiveSingleValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveSingleHandler : IRequestHandler<GetGoodsReceiveSingleRequest, GetGoodsReceiveSingleResult>
|
||||
{
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetGoodsReceiveSingleHandler(
|
||||
IQueryContext context
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetGoodsReceiveSingleResult> Handle(GetGoodsReceiveSingleRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var queryData = _context
|
||||
.GoodsReceive
|
||||
.AsNoTracking()
|
||||
.Include(x => x.PurchaseOrder)
|
||||
.ThenInclude(x => x.Vendor)
|
||||
.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(GoodsReceive))
|
||||
.AsQueryable();
|
||||
|
||||
var transactionList = await queryTransactionList.ToListAsync(cancellationToken);
|
||||
|
||||
return new GetGoodsReceiveSingleResult
|
||||
{
|
||||
Data = data,
|
||||
TransactionList = transactionList
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Enums;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.GoodsReceiveManager.Queries;
|
||||
|
||||
public record GetGoodsReceiveStatusListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveStatusListProfile : Profile
|
||||
{
|
||||
public GetGoodsReceiveStatusListProfile()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveStatusListResult
|
||||
{
|
||||
public List<GetGoodsReceiveStatusListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetGoodsReceiveStatusListRequest : IRequest<GetGoodsReceiveStatusListResult>
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public class GetGoodsReceiveStatusListHandler : IRequestHandler<GetGoodsReceiveStatusListRequest, GetGoodsReceiveStatusListResult>
|
||||
{
|
||||
|
||||
public GetGoodsReceiveStatusListHandler()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<GetGoodsReceiveStatusListResult> Handle(GetGoodsReceiveStatusListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var statuses = Enum.GetValues(typeof(GoodsReceiveStatus))
|
||||
.Cast<GoodsReceiveStatus>()
|
||||
.Select(status => new GetGoodsReceiveStatusListDto
|
||||
{
|
||||
Id = ((int)status).ToString(),
|
||||
Name = status.ToFriendlyName()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await Task.CompletedTask;
|
||||
|
||||
return new GetGoodsReceiveStatusListResult
|
||||
{
|
||||
Data = statuses
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user