Refactor sales pipeline: ShopPO, Invoice, SysConfig models

Major refactor replacing Sale/Order with ShopPO → Invoice → LoadingSheet flow. Removed legacy Sale/Order models and features; introduced ShopPO, ShopPOItem, Invoice, LoadingSheetOrder, and SysConfig models. Updated related entities, enums, repositories, and controllers. Added new migrations for schema changes and system config. Overhauled business logic and DI registrations. Updated documentation with migration plan.
This commit is contained in:
Kalana B. Thilakarathna
2026-09-17 10:03:22 +05:30
parent 6c40421b2d
commit 93f2f1bd3b
83 changed files with 11274 additions and 2538 deletions
@@ -0,0 +1,8 @@
namespace Distribution.Features.SysConfig;
public interface ISysConfigRepository
{
Task<Models.SysConfig?> GetByKeyAsync(string key);
Task<List<Models.SysConfig>> GetAllAsync();
Task UpsertAsync(Models.SysConfig entity);
}
@@ -0,0 +1,53 @@
using Distribution.Infra.Authorization;
using Distribution.Models;
using Microsoft.AspNetCore.Mvc;
namespace Distribution.Features.SysConfig;
[ApiController]
[Route("api/sys-config")]
public class SysConfigController(SysConfigService service, ICurrentUserService currentUser) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetAll() =>
Ok(await service.GetAllAsync());
// Typed shortcut — must be declared before {key} to take precedence
[HttpGet("invoice-creation-mode")]
public async Task<IActionResult> GetInvoiceCreationMode()
{
var mode = await service.GetInvoiceCreationModeAsync();
return Ok(new { Mode = mode });
}
[HttpPut("invoice-creation-mode")]
public async Task<IActionResult> SetInvoiceCreationMode(SetInvoiceCreationModeRequest request, CancellationToken ct)
{
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
try
{
var result = await service.SetAsync(SysConfigService.KeyInvoiceCreationMode, request.Mode.ToString(), userId, ct);
return Ok(result);
}
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
}
[HttpGet("{key}")]
public async Task<IActionResult> GetByKey(string key)
{
var result = await service.GetByKeyAsync(key);
return result is null ? NotFound() : Ok(result);
}
[HttpPut("{key}")]
public async Task<IActionResult> Set(string key, SetSysConfigRequest request, CancellationToken ct)
{
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
try
{
var result = await service.SetAsync(key, request.Value, userId, ct);
return Ok(result);
}
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
}
}
@@ -0,0 +1,9 @@
using Distribution.Models;
namespace Distribution.Features.SysConfig;
public record SysConfigResponse(string Key, string Value, DateTime UpdatedAt, Guid? UpdatedBy);
public record SetSysConfigRequest(string Value);
public record SetInvoiceCreationModeRequest(InvoiceCreationMode Mode);
@@ -0,0 +1,29 @@
using Distribution.Data;
using Microsoft.EntityFrameworkCore;
namespace Distribution.Features.SysConfig;
public class SysConfigRepository(AppDBContext context) : ISysConfigRepository
{
public async Task<Models.SysConfig?> GetByKeyAsync(string key) =>
await context.SysConfigs.FindAsync(key);
public async Task<List<Models.SysConfig>> GetAllAsync() =>
await context.SysConfigs.OrderBy(c => c.Key).ToListAsync();
public async Task UpsertAsync(Models.SysConfig entity)
{
var existing = await context.SysConfigs.FindAsync(entity.Key);
if (existing is null)
{
await context.SysConfigs.AddAsync(entity);
}
else
{
existing.Value = entity.Value;
existing.UpdatedAt = entity.UpdatedAt;
existing.UpdatedBy = entity.UpdatedBy;
context.SysConfigs.Update(existing);
}
}
}
@@ -0,0 +1,67 @@
using Distribution.Infra.UoW;
using Distribution.Models;
namespace Distribution.Features.SysConfig;
public class SysConfigService(ISysConfigRepository repo, IUnitOfWork uow)
{
public const string KeyInvoiceCreationMode = "InvoiceCreationMode";
public async Task<List<SysConfigResponse>> GetAllAsync()
{
var configs = await repo.GetAllAsync();
return configs.Select(ToResponse).ToList();
}
public async Task<SysConfigResponse?> GetByKeyAsync(string key)
{
var config = await repo.GetByKeyAsync(key);
return config is null ? null : ToResponse(config);
}
public async Task<SysConfigResponse> SetAsync(string key, string value, Guid userId, CancellationToken ct = default)
{
ValidateValue(key, value);
var config = new Models.SysConfig
{
Key = key,
Value = value,
UpdatedAt = DateTime.Now,
UpdatedBy = userId
};
await uow.BeginAsync(ct);
try
{
await repo.UpsertAsync(config);
await uow.CommitAsync(ct);
}
catch { await uow.RollbackAsync(ct); throw; }
var saved = await repo.GetByKeyAsync(key);
return ToResponse(saved!);
}
/// <summary>
/// Returns the current InvoiceCreationMode, defaulting to ModeB if not configured.
/// ModeA = invoice auto-created on ShopPO confirmation.
/// ModeB = invoice created as a separate explicit step.
/// </summary>
public async Task<InvoiceCreationMode> GetInvoiceCreationModeAsync()
{
var config = await repo.GetByKeyAsync(KeyInvoiceCreationMode);
if (config is null) return InvoiceCreationMode.ModeB;
return Enum.TryParse<InvoiceCreationMode>(config.Value, out var mode) ? mode : InvoiceCreationMode.ModeB;
}
private static void ValidateValue(string key, string value)
{
if (key == KeyInvoiceCreationMode && !Enum.TryParse<InvoiceCreationMode>(value, out _))
throw new InvalidOperationException(
$"Invalid value '{value}' for '{key}'. Allowed values: {string.Join(", ", Enum.GetNames<InvoiceCreationMode>())}.");
}
private static SysConfigResponse ToResponse(Models.SysConfig c) =>
new(c.Key, c.Value, c.UpdatedAt, c.UpdatedBy);
}