initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Scrapping.Cqrs;
|
||||
|
||||
public class CreateScrappingRequest
|
||||
{
|
||||
public DateTime? ScrappingDate { get; set; }
|
||||
public Data.Enums.ScrappingStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateScrappingResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateScrappingCommand(CreateScrappingRequest Data) : IRequest<CreateScrappingResponse>;
|
||||
|
||||
public class CreateScrappingHandler : IRequestHandler<CreateScrappingCommand, CreateScrappingResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateScrappingHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateScrappingResponse> Handle(CreateScrappingCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.Scrapping);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Scrapping
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
ScrappingDate = request.Data.ScrappingDate,
|
||||
Status = request.Data.Status,
|
||||
Description = request.Data.Description,
|
||||
WarehouseId = request.Data.WarehouseId
|
||||
};
|
||||
|
||||
_context.Scrapping.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateScrappingResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
AutoNumber = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Scrapping.Cqrs;
|
||||
|
||||
public class CreateScrappingItemRequest
|
||||
{
|
||||
public string? ScrappingId { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public record CreateScrappingItemCommand(CreateScrappingItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class CreateScrappingItemHandler : IRequestHandler<CreateScrappingItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateScrappingItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(CreateScrappingItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.Scrapping
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.ScrappingId, 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.Scrapping),
|
||||
ModuleCode = "SCRP",
|
||||
ModuleNumber = master.AutoNumber,
|
||||
MovementDate = master.ScrappingDate!.Value,
|
||||
Status = (Data.Enums.InventoryTransactionStatus)master.Status,
|
||||
AutoNumber = autoNoIVT,
|
||||
WarehouseId = master.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,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Scrapping.Cqrs;
|
||||
|
||||
public class CreateScrappingValidator : AbstractValidator<CreateScrappingRequest>
|
||||
{
|
||||
public CreateScrappingValidator()
|
||||
{
|
||||
RuleFor(x => x.ScrappingDate).NotEmpty().WithMessage("Scrapping Date is required");
|
||||
RuleFor(x => x.WarehouseId).NotEmpty().WithMessage("Warehouse is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Scrapping.Cqrs;
|
||||
|
||||
public record DeleteScrappingByIdCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteScrappingByIdHandler : IRequestHandler<DeleteScrappingByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteScrappingByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteScrappingByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Scrapping
|
||||
.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.Scrapping))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
_context.InventoryTransaction.RemoveRange(items);
|
||||
_context.Scrapping.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.Scrapping.Cqrs;
|
||||
|
||||
public record DeleteScrappingItemCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteScrappingItemHandler : IRequestHandler<DeleteScrappingItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteScrappingItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteScrappingItemCommand 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.Scrapping.Cqrs;
|
||||
|
||||
public class ScrappingItemResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? ProductName { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public class GetScrappingByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? ScrappingDate { get; set; }
|
||||
public Data.Enums.ScrappingStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
public string? WarehouseName { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
public List<ScrappingItemResponse> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetScrappingByIdQuery(string Id) : IRequest<GetScrappingByIdResponse?>;
|
||||
|
||||
public class GetScrappingByIdHandler : IRequestHandler<GetScrappingByIdQuery, GetScrappingByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetScrappingByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetScrappingByIdResponse?> Handle(GetScrappingByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.Scrapping
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Warehouse)
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (master == null) return null;
|
||||
|
||||
var items = await _context.InventoryTransaction
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Product)
|
||||
.Where(x => x.ModuleId == request.Id && x.ModuleName == nameof(Data.Entities.Scrapping))
|
||||
.Select(x => new ScrappingItemResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
ProductId = x.ProductId,
|
||||
ProductName = x.Product!.Name,
|
||||
Movement = x.Movement
|
||||
}).ToListAsync(cancellationToken);
|
||||
|
||||
return new GetScrappingByIdResponse
|
||||
{
|
||||
Id = master.Id,
|
||||
AutoNumber = master.AutoNumber,
|
||||
ScrappingDate = master.ScrappingDate,
|
||||
Status = master.Status,
|
||||
Description = master.Description,
|
||||
WarehouseId = master.WarehouseId,
|
||||
WarehouseName = master.Warehouse?.Name,
|
||||
CreatedAt = master.CreatedAt,
|
||||
CreatedBy = master.CreatedBy,
|
||||
UpdatedAt = master.UpdatedAt,
|
||||
UpdatedBy = master.UpdatedBy,
|
||||
Items = items
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Scrapping.Cqrs;
|
||||
|
||||
public class GetScrappingListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? ScrappingDate { get; set; }
|
||||
public Data.Enums.ScrappingStatus Status { get; set; }
|
||||
public string? WarehouseName { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetScrappingListQuery() : IRequest<List<GetScrappingListResponse>>;
|
||||
|
||||
public class GetScrappingListHandler : IRequestHandler<GetScrappingListQuery, List<GetScrappingListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetScrappingListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetScrappingListResponse>> Handle(GetScrappingListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Scrapping
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Warehouse)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetScrappingListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
ScrappingDate = x.ScrappingDate,
|
||||
Status = x.Status,
|
||||
WarehouseName = x.Warehouse!.Name,
|
||||
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.Scrapping.Cqrs;
|
||||
|
||||
public class ScrappingLookupResponse
|
||||
{
|
||||
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 GetScrappingLookupQuery() : IRequest<ScrappingLookupResponse>;
|
||||
|
||||
public class GetScrappingLookupHandler : IRequestHandler<GetScrappingLookupQuery, ScrappingLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetScrappingLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<ScrappingLookupResponse> Handle(GetScrappingLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new ScrappingLookupResponse();
|
||||
|
||||
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(ScrappingStatus))
|
||||
.Cast<ScrappingStatus>()
|
||||
.Select(x => new LookupStatusItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.GetDescription()
|
||||
}).ToList();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Scrapping.Cqrs;
|
||||
|
||||
public class UpdateScrappingRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? ScrappingDate { get; set; }
|
||||
public Data.Enums.ScrappingStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateScrappingCommand(UpdateScrappingRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateScrappingHandler : IRequestHandler<UpdateScrappingCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateScrappingHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateScrappingCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Scrapping
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.ScrappingDate = request.Data.ScrappingDate;
|
||||
entity.Status = request.Data.Status;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.WarehouseId = request.Data.WarehouseId;
|
||||
|
||||
var details = await _context.InventoryTransaction
|
||||
.Where(x => x.ModuleId == entity.Id && x.ModuleName == nameof(Data.Entities.Scrapping))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var item in details)
|
||||
{
|
||||
item.MovementDate = entity.ScrappingDate ?? DateTime.Now;
|
||||
item.Status = (Data.Enums.InventoryTransactionStatus)entity.Status;
|
||||
item.WarehouseId = entity.WarehouseId;
|
||||
item.ModuleNumber = entity.AutoNumber;
|
||||
_context.CalculateInvenTrans(item);
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Scrapping.Cqrs;
|
||||
|
||||
public class UpdateScrappingItemRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public double? Movement { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateScrappingItemCommand(UpdateScrappingItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateScrappingItemHandler : IRequestHandler<UpdateScrappingItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateScrappingItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateScrappingItemCommand 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.Movement = Math.Abs(request.Data.Movement ?? 0);
|
||||
|
||||
_context.CalculateInvenTrans(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Inventory.Scrapping.Cqrs;
|
||||
|
||||
public class UpdateScrappingValidator : AbstractValidator<UpdateScrappingRequest>
|
||||
{
|
||||
public UpdateScrappingValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.ScrappingDate).NotEmpty().WithMessage("Scrapping Date is required");
|
||||
RuleFor(x => x.WarehouseId).NotEmpty().WithMessage("Warehouse is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user