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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class CreatePositiveAdjustmentRequest
|
||||
{
|
||||
public DateTime? AdjustmentDate { get; set; }
|
||||
public Data.Enums.AdjustmentStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePositiveAdjustmentResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreatePositiveAdjustmentCommand(CreatePositiveAdjustmentRequest Data) : IRequest<CreatePositiveAdjustmentResponse>;
|
||||
|
||||
public class CreatePositiveAdjustmentHandler : IRequestHandler<CreatePositiveAdjustmentCommand, CreatePositiveAdjustmentResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreatePositiveAdjustmentHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreatePositiveAdjustmentResponse> Handle(CreatePositiveAdjustmentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.PositiveAdjustment);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.PositiveAdjustment
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
AdjustmentDate = request.Data.AdjustmentDate,
|
||||
Status = request.Data.Status,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.PositiveAdjustment.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreatePositiveAdjustmentResponse
|
||||
{
|
||||
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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class CreatePositiveAdjustmentItemRequest
|
||||
{
|
||||
public string? PositiveAdjustmentId { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public record CreatePositiveAdjustmentItemCommand(CreatePositiveAdjustmentItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class CreatePositiveAdjustmentItemHandler : IRequestHandler<CreatePositiveAdjustmentItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreatePositiveAdjustmentItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(CreatePositiveAdjustmentItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.PositiveAdjustment
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.PositiveAdjustmentId, 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.PositiveAdjustment),
|
||||
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 = request.Data.Movement
|
||||
};
|
||||
|
||||
_context.CalculateInvenTrans(entity);
|
||||
_context.InventoryTransaction.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Inventory.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class CreatePositiveAdjustmentValidator : AbstractValidator<CreatePositiveAdjustmentRequest>
|
||||
{
|
||||
public CreatePositiveAdjustmentValidator()
|
||||
{
|
||||
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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public record DeletePositiveAdjustmentByIdCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeletePositiveAdjustmentByIdHandler : IRequestHandler<DeletePositiveAdjustmentByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeletePositiveAdjustmentByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeletePositiveAdjustmentByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.PositiveAdjustment
|
||||
.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.PositiveAdjustment))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
_context.InventoryTransaction.RemoveRange(items);
|
||||
_context.PositiveAdjustment.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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public record DeletePositiveAdjustmentItemCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeletePositiveAdjustmentItemHandler : IRequestHandler<DeletePositiveAdjustmentItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeletePositiveAdjustmentItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeletePositiveAdjustmentItemCommand 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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class PositiveAdjustmentItemResponse
|
||||
{
|
||||
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 GetPositiveAdjustmentByIdResponse
|
||||
{
|
||||
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<PositiveAdjustmentItemResponse> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetPositiveAdjustmentByIdQuery(string Id) : IRequest<GetPositiveAdjustmentByIdResponse?>;
|
||||
|
||||
public class GetPositiveAdjustmentByIdHandler : IRequestHandler<GetPositiveAdjustmentByIdQuery, GetPositiveAdjustmentByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetPositiveAdjustmentByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetPositiveAdjustmentByIdResponse?> Handle(GetPositiveAdjustmentByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.PositiveAdjustment
|
||||
.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.PositiveAdjustment))
|
||||
.Select(x => new PositiveAdjustmentItemResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
ProductId = x.ProductId,
|
||||
ProductName = x.Product!.Name,
|
||||
WarehouseId = x.WarehouseId,
|
||||
WarehouseName = x.Warehouse!.Name,
|
||||
Movement = x.Movement
|
||||
}).ToListAsync(cancellationToken);
|
||||
|
||||
return new GetPositiveAdjustmentByIdResponse
|
||||
{
|
||||
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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class GetPositiveAdjustmentListResponse
|
||||
{
|
||||
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 GetPositiveAdjustmentListQuery() : IRequest<List<GetPositiveAdjustmentListResponse>>;
|
||||
|
||||
public class GetPositiveAdjustmentListHandler : IRequestHandler<GetPositiveAdjustmentListQuery, List<GetPositiveAdjustmentListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetPositiveAdjustmentListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetPositiveAdjustmentListResponse>> Handle(GetPositiveAdjustmentListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.PositiveAdjustment
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetPositiveAdjustmentListResponse
|
||||
{
|
||||
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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class PositiveAdjustmentLookupResponse
|
||||
{
|
||||
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 GetPositiveAdjustmentLookupQuery() : IRequest<PositiveAdjustmentLookupResponse>;
|
||||
|
||||
public class GetPositiveAdjustmentLookupHandler : IRequestHandler<GetPositiveAdjustmentLookupQuery, PositiveAdjustmentLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetPositiveAdjustmentLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<PositiveAdjustmentLookupResponse> Handle(GetPositiveAdjustmentLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new PositiveAdjustmentLookupResponse();
|
||||
|
||||
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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class UpdatePositiveAdjustmentRequest
|
||||
{
|
||||
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 UpdatePositiveAdjustmentCommand(UpdatePositiveAdjustmentRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdatePositiveAdjustmentHandler : IRequestHandler<UpdatePositiveAdjustmentCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdatePositiveAdjustmentHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdatePositiveAdjustmentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.PositiveAdjustment
|
||||
.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.PositiveAdjustment))
|
||||
.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.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class UpdatePositiveAdjustmentItemRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public record UpdatePositiveAdjustmentItemCommand(UpdatePositiveAdjustmentItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdatePositiveAdjustmentItemHandler : IRequestHandler<UpdatePositiveAdjustmentItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdatePositiveAdjustmentItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdatePositiveAdjustmentItemCommand 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 = request.Data.Movement;
|
||||
|
||||
_context.CalculateInvenTrans(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Inventory.PositiveAdjustment.Cqrs;
|
||||
|
||||
public class UpdatePositiveAdjustmentValidator : AbstractValidator<UpdatePositiveAdjustmentRequest>
|
||||
{
|
||||
public UpdatePositiveAdjustmentValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.AdjustmentDate).NotEmpty().WithMessage("Adjustment Date is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user