initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public class CreateDeliveryOrderRequest
|
||||
{
|
||||
public DateTime? DeliveryDate { get; set; }
|
||||
public Data.Enums.DeliveryOrderStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesOrderId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateDeliveryOrderResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateDeliveryOrderCommand(CreateDeliveryOrderRequest Data) : IRequest<CreateDeliveryOrderResponse>;
|
||||
|
||||
public class CreateDeliveryOrderHandler : IRequestHandler<CreateDeliveryOrderCommand, CreateDeliveryOrderResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateDeliveryOrderHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateDeliveryOrderResponse> Handle(CreateDeliveryOrderCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.DeliveryOrder);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.DeliveryOrder
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
DeliveryDate = request.Data.DeliveryDate,
|
||||
Status = request.Data.Status,
|
||||
Description = request.Data.Description,
|
||||
SalesOrderId = request.Data.SalesOrderId
|
||||
};
|
||||
|
||||
_context.DeliveryOrder.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateDeliveryOrderResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
AutoNumber = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public class CreateDeliveryOrderValidator : AbstractValidator<CreateDeliveryOrderRequest>
|
||||
{
|
||||
public CreateDeliveryOrderValidator()
|
||||
{
|
||||
RuleFor(x => x.DeliveryDate).NotEmpty().WithMessage("Delivery Date is required");
|
||||
RuleFor(x => x.SalesOrderId).NotEmpty().WithMessage("Sales Order reference is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public record DeleteDeliveryOrderByIdCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteDeliveryOrderByIdHandler : IRequestHandler<DeleteDeliveryOrderByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteDeliveryOrderByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteDeliveryOrderByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.DeliveryOrder
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
var transactions = await _context.InventoryTransaction
|
||||
.Where(x => x.ModuleId == entity.Id && x.ModuleName == nameof(Data.Entities.DeliveryOrder))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
_context.InventoryTransaction.RemoveRange(transactions);
|
||||
_context.DeliveryOrder.Remove(entity);
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public record DeleteDeliveryOrderItemCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteDeliveryOrderItemHandler : IRequestHandler<DeleteDeliveryOrderItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteDeliveryOrderItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteDeliveryOrderItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.InventoryTransaction
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.InventoryTransaction.Remove(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public class DeliveryOrderInvenTransResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? ProductName { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
public string? WarehouseName { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public class GetDeliveryOrderByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? DeliveryDate { get; set; }
|
||||
public Data.Enums.DeliveryOrderStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesOrderId { get; set; }
|
||||
public string? SalesOrderNumber { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
public List<DeliveryOrderInvenTransResponse> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetDeliveryOrderByIdQuery(string Id) : IRequest<GetDeliveryOrderByIdResponse?>;
|
||||
|
||||
public class GetDeliveryOrderByIdHandler : IRequestHandler<GetDeliveryOrderByIdQuery, GetDeliveryOrderByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetDeliveryOrderByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetDeliveryOrderByIdResponse?> Handle(GetDeliveryOrderByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.DeliveryOrder
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesOrder)
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (master == null) return null;
|
||||
|
||||
var details = await _context.InventoryTransaction
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Product)
|
||||
.Include(x => x.Warehouse)
|
||||
.Where(x => x.ModuleId == request.Id && x.ModuleName == nameof(Data.Entities.DeliveryOrder))
|
||||
.Select(x => new DeliveryOrderInvenTransResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
ProductId = x.ProductId,
|
||||
ProductName = x.Product!.Name,
|
||||
WarehouseId = x.WarehouseId,
|
||||
WarehouseName = x.Warehouse!.Name,
|
||||
Movement = x.Movement
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return new GetDeliveryOrderByIdResponse
|
||||
{
|
||||
Id = master.Id,
|
||||
AutoNumber = master.AutoNumber,
|
||||
DeliveryDate = master.DeliveryDate,
|
||||
Status = master.Status,
|
||||
Description = master.Description,
|
||||
SalesOrderId = master.SalesOrderId,
|
||||
SalesOrderNumber = master.SalesOrder?.AutoNumber,
|
||||
CreatedAt = master.CreatedAt,
|
||||
CreatedBy = master.CreatedBy,
|
||||
UpdatedAt = master.UpdatedAt,
|
||||
UpdatedBy = master.UpdatedBy,
|
||||
Items = details
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public class GetDeliveryOrderListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? DeliveryDate { get; set; }
|
||||
public string? SalesOrderNumber { get; set; }
|
||||
public Data.Enums.DeliveryOrderStatus Status { get; set; }
|
||||
}
|
||||
|
||||
public record GetDeliveryOrderListQuery() : IRequest<List<GetDeliveryOrderListResponse>>;
|
||||
|
||||
public class GetDeliveryOrderListHandler : IRequestHandler<GetDeliveryOrderListQuery, List<GetDeliveryOrderListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetDeliveryOrderListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetDeliveryOrderListResponse>> Handle(GetDeliveryOrderListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.DeliveryOrder
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesOrder)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetDeliveryOrderListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
DeliveryDate = x.DeliveryDate,
|
||||
SalesOrderNumber = x.SalesOrder!.AutoNumber,
|
||||
Status = x.Status
|
||||
}).ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public class DeliveryOrderLookupResponse
|
||||
{
|
||||
public List<LookupItem> SalesOrders { get; set; } = new();
|
||||
public List<LookupItem> Warehouses { get; set; } = new();
|
||||
public List<LookupItem> Products { get; set; } = new();
|
||||
public List<LookupStatusItem> Statuses { get; set; } = new();
|
||||
}
|
||||
|
||||
public class LookupItem
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public class LookupStatusItem
|
||||
{
|
||||
public int Value { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record GetDeliveryOrderLookupQuery() : IRequest<DeliveryOrderLookupResponse>;
|
||||
|
||||
public class GetDeliveryOrderLookupHandler : IRequestHandler<GetDeliveryOrderLookupQuery, DeliveryOrderLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetDeliveryOrderLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<DeliveryOrderLookupResponse> Handle(GetDeliveryOrderLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new DeliveryOrderLookupResponse();
|
||||
|
||||
response.SalesOrders = await _context.SalesOrder.AsNoTracking()
|
||||
.Where(x => x.OrderStatus >= SalesOrderStatus.Confirmed)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.AutoNumber })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.Warehouses = await _context.Warehouse.AsNoTracking()
|
||||
.Where(x => x.SystemWarehouse == false)
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.Name })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.Products = await _context.Product.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.Name })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.Statuses = Enum.GetValues(typeof(DeliveryOrderStatus))
|
||||
.Cast<DeliveryOrderStatus>()
|
||||
.Select(x => new LookupStatusItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.GetDescription()
|
||||
}).ToList();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public class UpdateDeliveryOrderRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? DeliveryDate { get; set; }
|
||||
public Data.Enums.DeliveryOrderStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesOrderId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateDeliveryOrderCommand(UpdateDeliveryOrderRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateDeliveryOrderHandler : IRequestHandler<UpdateDeliveryOrderCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateDeliveryOrderHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateDeliveryOrderCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.DeliveryOrder
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.DeliveryDate = request.Data.DeliveryDate;
|
||||
entity.Status = request.Data.Status;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.SalesOrderId = request.Data.SalesOrderId;
|
||||
|
||||
var transactions = await _context.InventoryTransaction
|
||||
.Where(x => x.ModuleId == entity.Id && x.ModuleName == nameof(Data.Entities.DeliveryOrder))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var trans in transactions)
|
||||
{
|
||||
trans.MovementDate = entity.DeliveryDate ?? DateTime.Now;
|
||||
trans.ModuleNumber = entity.AutoNumber;
|
||||
trans.Status = (Data.Enums.InventoryTransactionStatus)entity.Status;
|
||||
_context.CalculateInvenTrans(trans);
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public class UpdateDeliveryOrderItemRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateDeliveryOrderItemCommand(UpdateDeliveryOrderItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateDeliveryOrderItemHandler : IRequestHandler<UpdateDeliveryOrderItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateDeliveryOrderItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateDeliveryOrderItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.InventoryTransaction
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.WarehouseId = request.Data.WarehouseId;
|
||||
entity.ProductId = request.Data.ProductId;
|
||||
entity.Movement = request.Data.Movement;
|
||||
|
||||
_context.CalculateInvenTrans(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Sales.DeliveryOrder.Cqrs;
|
||||
|
||||
public class UpdateDeliveryOrderValidator : AbstractValidator<UpdateDeliveryOrderRequest>
|
||||
{
|
||||
public UpdateDeliveryOrderValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.SalesOrderId).NotEmpty();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user