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.StockCount.Cqrs;
|
||||
|
||||
public class CreateStockCountRequest
|
||||
{
|
||||
public DateTime? CountDate { get; set; }
|
||||
public Data.Enums.StockCountStatus Status { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? WarehouseId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateStockCountResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateStockCountCommand(CreateStockCountRequest Data) : IRequest<CreateStockCountResponse>;
|
||||
|
||||
public class CreateStockCountHandler : IRequestHandler<CreateStockCountCommand, CreateStockCountResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateStockCountHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateStockCountResponse> Handle(CreateStockCountCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.StockCount);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.StockCount
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
CountDate = request.Data.CountDate,
|
||||
Status = request.Data.Status,
|
||||
Description = request.Data.Description,
|
||||
WarehouseId = request.Data.WarehouseId
|
||||
};
|
||||
|
||||
_context.StockCount.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateStockCountResponse
|
||||
{
|
||||
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.StockCount.Cqrs;
|
||||
|
||||
public class CreateStockCountItemRequest
|
||||
{
|
||||
public string? StockCountId { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public double? QtySCCount { get; set; }
|
||||
}
|
||||
|
||||
public record CreateStockCountItemCommand(CreateStockCountItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class CreateStockCountItemHandler : IRequestHandler<CreateStockCountItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateStockCountItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(CreateStockCountItemCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.StockCount
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.StockCountId, 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.StockCount),
|
||||
ModuleCode = "COUNT",
|
||||
ModuleNumber = master.AutoNumber,
|
||||
MovementDate = master.CountDate!.Value,
|
||||
Status = (Data.Enums.InventoryTransactionStatus)master.Status,
|
||||
AutoNumber = autoNoIVT,
|
||||
WarehouseId = master.WarehouseId,
|
||||
ProductId = request.Data.ProductId,
|
||||
QtySCCount = Math.Abs(request.Data.QtySCCount ?? 0)
|
||||
};
|
||||
|
||||
_context.CalculateInvenTrans(entity);
|
||||
_context.InventoryTransaction.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Inventory.StockCount.Cqrs;
|
||||
|
||||
public class CreateStockCountValidator : AbstractValidator<CreateStockCountRequest>
|
||||
{
|
||||
public CreateStockCountValidator()
|
||||
{
|
||||
RuleFor(x => x.CountDate).NotEmpty().WithMessage("Count 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.StockCount.Cqrs;
|
||||
|
||||
public record DeleteStockCountByIdCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteStockCountByIdHandler : IRequestHandler<DeleteStockCountByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteStockCountByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteStockCountByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.StockCount
|
||||
.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.StockCount))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
_context.InventoryTransaction.RemoveRange(items);
|
||||
_context.StockCount.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.StockCount.Cqrs;
|
||||
|
||||
public record DeleteStockCountItemCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteStockCountItemHandler : IRequestHandler<DeleteStockCountItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteStockCountItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteStockCountItemCommand 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,79 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Inventory.StockCount.Cqrs;
|
||||
|
||||
public class StockCountItemResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? ProductName { get; set; }
|
||||
public double? QtySCSys { get; set; }
|
||||
public double? QtySCCount { get; set; }
|
||||
public double? QtySCDelta { get; set; }
|
||||
}
|
||||
|
||||
public class GetStockCountByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? CountDate { get; set; }
|
||||
public Data.Enums.StockCountStatus 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<StockCountItemResponse> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetStockCountByIdQuery(string Id) : IRequest<GetStockCountByIdResponse?>;
|
||||
|
||||
public class GetStockCountByIdHandler : IRequestHandler<GetStockCountByIdQuery, GetStockCountByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetStockCountByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetStockCountByIdResponse?> Handle(GetStockCountByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var master = await _context.StockCount
|
||||
.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.StockCount))
|
||||
.Select(x => new StockCountItemResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
ProductId = x.ProductId,
|
||||
ProductName = x.Product!.Name,
|
||||
QtySCSys = x.QtySCSys,
|
||||
QtySCCount = x.QtySCCount,
|
||||
QtySCDelta = x.QtySCDelta
|
||||
}).ToListAsync(cancellationToken);
|
||||
|
||||
return new GetStockCountByIdResponse
|
||||
{
|
||||
Id = master.Id,
|
||||
AutoNumber = master.AutoNumber,
|
||||
CountDate = master.CountDate,
|
||||
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.StockCount.Cqrs;
|
||||
|
||||
public class GetStockCountListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? CountDate { get; set; }
|
||||
public Data.Enums.StockCountStatus Status { get; set; }
|
||||
public string? WarehouseName { get; set; }
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public record GetStockCountListQuery() : IRequest<List<GetStockCountListResponse>>;
|
||||
|
||||
public class GetStockCountListHandler : IRequestHandler<GetStockCountListQuery, List<GetStockCountListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetStockCountListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetStockCountListResponse>> Handle(GetStockCountListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.StockCount
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Warehouse)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetStockCountListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
CountDate = x.CountDate,
|
||||
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.StockCount.Cqrs;
|
||||
|
||||
public class StockCountLookupResponse
|
||||
{
|
||||
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 GetStockCountLookupQuery() : IRequest<StockCountLookupResponse>;
|
||||
|
||||
public class GetStockCountLookupHandler : IRequestHandler<GetStockCountLookupQuery, StockCountLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetStockCountLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<StockCountLookupResponse> Handle(GetStockCountLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new StockCountLookupResponse();
|
||||
|
||||
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(StockCountStatus))
|
||||
.Cast<StockCountStatus>()
|
||||
.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.StockCount.Cqrs;
|
||||
|
||||
public class UpdateStockCountRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? CountDate { get; set; }
|
||||
public Data.Enums.StockCountStatus 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 UpdateStockCountCommand(UpdateStockCountRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateStockCountHandler : IRequestHandler<UpdateStockCountCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateStockCountHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateStockCountCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.StockCount
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.CountDate = request.Data.CountDate;
|
||||
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.StockCount))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
foreach (var item in details)
|
||||
{
|
||||
item.MovementDate = entity.CountDate ?? 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.StockCount.Cqrs;
|
||||
|
||||
public class UpdateStockCountItemRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public double? QtySCCount { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateStockCountItemCommand(UpdateStockCountItemRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateStockCountItemHandler : IRequestHandler<UpdateStockCountItemCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateStockCountItemHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateStockCountItemCommand 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.QtySCCount = Math.Abs(request.Data.QtySCCount ?? 0);
|
||||
|
||||
_context.CalculateInvenTrans(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Inventory.StockCount.Cqrs;
|
||||
|
||||
public class UpdateStockCountValidator : AbstractValidator<UpdateStockCountRequest>
|
||||
{
|
||||
public UpdateStockCountValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.CountDate).NotEmpty().WithMessage("Count Date is required");
|
||||
RuleFor(x => x.WarehouseId).NotEmpty().WithMessage("Warehouse is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user