initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class CreateNegativeAdjustmentRequest
|
||||
{
|
||||
public DateTime? AdjustmentDate { get; set; }
|
||||
public Data.Enums.AdjustmentStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreateNegativeAdjustmentResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateNegativeAdjustmentCommand(CreateNegativeAdjustmentRequest Data) : IRequest<CreateNegativeAdjustmentResponse>;
|
||||
|
||||
public class CreateNegativeAdjustmentHandler : IRequestHandler<CreateNegativeAdjustmentCommand, CreateNegativeAdjustmentResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateNegativeAdjustmentHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateNegativeAdjustmentResponse> Handle(CreateNegativeAdjustmentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.NegativeAdjustment);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.NegativeAdjustment
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
AdjustmentDate = request.Data.AdjustmentDate,
|
||||
Status = request.Data.Status,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.NegativeAdjustment.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateNegativeAdjustmentResponse
|
||||
{
|
||||
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.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class CreateNegativeAdjustmentItemRequest
|
||||
{
|
||||
public string? NegativeAdjustmentId { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public record CreateNegativeAdjustmentItemCommand(CreateNegativeAdjustmentItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class CreateNegativeAdjustmentItemHandler : IRequestHandler<CreateNegativeAdjustmentItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateNegativeAdjustmentItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(CreateNegativeAdjustmentItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.NegativeAdjustment
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.NegativeAdjustmentId, cancellationToken);
|
||||
|
||||
if (master == null) return false;
|
||||
|
||||
var ivtEntityName = nameof(Data.Entities.InventoryTransaction);
|
||||
var autoNoIVT = await _context.GenerateAutoNumberAsync(
|
||||
entityName: ivtEntityName,
|
||||
prefixTemplate: $"{ivtEntityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.InventoryTransaction
|
||||
{
|
||||
ModuleId = master.Id,
|
||||
ModuleName = nameof(Data.Entities.NegativeAdjustment),
|
||||
ModuleCode = "ADJ-",
|
||||
ModuleNumber = master.AutoNumber,
|
||||
MovementDate = master.AdjustmentDate!.Value,
|
||||
Status = (Data.Enums.InventoryTransactionStatus)master.Status,
|
||||
AutoNumber = autoNoIVT,
|
||||
WarehouseId = request.Data.WarehouseId,
|
||||
ProductId = request.Data.ProductId,
|
||||
Movement = Math.Abs(request.Data.Movement ?? 0)
|
||||
};
|
||||
|
||||
_context.CalculateInvenTrans(entity);
|
||||
_context.InventoryTransaction.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class CreateNegativeAdjustmentValidator : AbstractValidator<CreateNegativeAdjustmentRequest>
|
||||
{
|
||||
public CreateNegativeAdjustmentValidator()
|
||||
{
|
||||
RuleFor(x => x.AdjustmentDate).NotEmpty().WithMessage("Adjustment Date is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public record DeleteNegativeAdjustmentByIdCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteNegativeAdjustmentByIdHandler : IRequestHandler<DeleteNegativeAdjustmentByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteNegativeAdjustmentByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteNegativeAdjustmentByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.NegativeAdjustment
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
var items = await _context.InventoryTransaction
|
||||
.Where(x => x.ModuleId == request.Id && x.ModuleName == nameof(Data.Entities.NegativeAdjustment))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
_context.InventoryTransaction.RemoveRange(items);
|
||||
_context.NegativeAdjustment.Remove(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public record DeleteNegativeAdjustmentItemCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteNegativeAdjustmentItemHandler : IRequestHandler<DeleteNegativeAdjustmentItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteNegativeAdjustmentItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteNegativeAdjustmentItemCommand 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,75 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class NegativeAdjustmentItemResponse
|
||||
{
|
||||
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 GetNegativeAdjustmentByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? AdjustmentDate { get; set; }
|
||||
public Data.Enums.AdjustmentStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
public List<NegativeAdjustmentItemResponse> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetNegativeAdjustmentByIdQuery(string Id) : IRequest<GetNegativeAdjustmentByIdResponse?>;
|
||||
|
||||
public class GetNegativeAdjustmentByIdHandler : IRequestHandler<GetNegativeAdjustmentByIdQuery, GetNegativeAdjustmentByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetNegativeAdjustmentByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetNegativeAdjustmentByIdResponse?> Handle(GetNegativeAdjustmentByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.NegativeAdjustment
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (master == null) return null;
|
||||
|
||||
var items = await _context.InventoryTransaction
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Product)
|
||||
.Include(x => x.Warehouse)
|
||||
.Where(x => x.ModuleId == request.Id && x.ModuleName == nameof(Data.Entities.NegativeAdjustment))
|
||||
.Select(x => new NegativeAdjustmentItemResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
ProductId = x.ProductId,
|
||||
ProductName = x.Product!.Name,
|
||||
WarehouseId = x.WarehouseId,
|
||||
WarehouseName = x.Warehouse!.Name,
|
||||
Movement = x.Movement
|
||||
}).ToListAsync(cancellationToken);
|
||||
|
||||
return new GetNegativeAdjustmentByIdResponse
|
||||
{
|
||||
Id = master.Id,
|
||||
AutoNumber = master.AutoNumber,
|
||||
AdjustmentDate = master.AdjustmentDate,
|
||||
Status = master.Status,
|
||||
Description = master.Description,
|
||||
CreatedAt = master.CreatedAt,
|
||||
CreatedBy = master.CreatedBy,
|
||||
UpdatedAt = master.UpdatedAt,
|
||||
UpdatedBy = master.UpdatedBy,
|
||||
Items = items
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class GetNegativeAdjustmentListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? AdjustmentDate { get; set; }
|
||||
public Data.Enums.AdjustmentStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetNegativeAdjustmentListQuery() : IRequest<List<GetNegativeAdjustmentListResponse>>;
|
||||
|
||||
public class GetNegativeAdjustmentListHandler : IRequestHandler<GetNegativeAdjustmentListQuery, List<GetNegativeAdjustmentListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetNegativeAdjustmentListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetNegativeAdjustmentListResponse>> Handle(GetNegativeAdjustmentListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.NegativeAdjustment
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetNegativeAdjustmentListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
AdjustmentDate = x.AdjustmentDate,
|
||||
Status = x.Status,
|
||||
Description = x.Description
|
||||
}).ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class NegativeAdjustmentLookupResponse
|
||||
{
|
||||
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 GetNegativeAdjustmentLookupQuery() : IRequest<NegativeAdjustmentLookupResponse>;
|
||||
|
||||
public class GetNegativeAdjustmentLookupHandler : IRequestHandler<GetNegativeAdjustmentLookupQuery, NegativeAdjustmentLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetNegativeAdjustmentLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<NegativeAdjustmentLookupResponse> Handle(GetNegativeAdjustmentLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new NegativeAdjustmentLookupResponse();
|
||||
|
||||
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()
|
||||
.Where(x => x.Physical == true)
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.Name })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.Statuses = Enum.GetValues(typeof(AdjustmentStatus))
|
||||
.Cast<AdjustmentStatus>()
|
||||
.Select(x => new LookupStatusItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.GetDescription()
|
||||
}).ToList();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class UpdateNegativeAdjustmentRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? AdjustmentDate { get; set; }
|
||||
public Data.Enums.AdjustmentStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateNegativeAdjustmentCommand(UpdateNegativeAdjustmentRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateNegativeAdjustmentHandler : IRequestHandler<UpdateNegativeAdjustmentCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateNegativeAdjustmentHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateNegativeAdjustmentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.NegativeAdjustment
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.AdjustmentDate = request.Data.AdjustmentDate;
|
||||
entity.Status = request.Data.Status;
|
||||
entity.Description = request.Data.Description;
|
||||
|
||||
var details = await _context.InventoryTransaction
|
||||
.Where(x => x.ModuleId == entity.Id && x.ModuleName == nameof(Data.Entities.NegativeAdjustment))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var item in details)
|
||||
{
|
||||
item.MovementDate = entity.AdjustmentDate ?? DateTime.Now;
|
||||
item.Status = (Data.Enums.InventoryTransactionStatus)entity.Status;
|
||||
item.ModuleNumber = entity.AutoNumber;
|
||||
_context.CalculateInvenTrans(item);
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class UpdateNegativeAdjustmentItemRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateNegativeAdjustmentItemCommand(UpdateNegativeAdjustmentItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateNegativeAdjustmentItemHandler : IRequestHandler<UpdateNegativeAdjustmentItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateNegativeAdjustmentItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateNegativeAdjustmentItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.InventoryTransaction
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.ProductId = request.Data.ProductId;
|
||||
entity.WarehouseId = request.Data.WarehouseId;
|
||||
entity.Movement = Math.Abs(request.Data.Movement ?? 0);
|
||||
|
||||
_context.CalculateInvenTrans(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Inventory.NegativeAdjustment.Cqrs;
|
||||
|
||||
public class UpdateNegativeAdjustmentValidator : AbstractValidator<UpdateNegativeAdjustmentRequest>
|
||||
{
|
||||
public UpdateNegativeAdjustmentValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.AdjustmentDate).NotEmpty().WithMessage("Adjustment Date is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user