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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user