Refactor/main flow #54
@@ -16,6 +16,8 @@ public class AppDBContext(DbContextOptions<AppDBContext> options) : DbContext(op
|
||||
public DbSet<Uom> Uoms { get; set; }
|
||||
public DbSet<UomConversion> UomConversions { get; set; }
|
||||
public DbSet<Item> Items { get; set; }
|
||||
public DbSet<ItemBatch> ItemBatches { get; set; }
|
||||
public DbSet<ItemSerialNumber> ItemSerialNumbers { get; set; }
|
||||
public DbSet<SaleRoute> SaleRoutes { get; set; }
|
||||
public DbSet<ShopPO> ShopPOs { get; set; }
|
||||
public DbSet<ShopPOItem> ShopPOItems { get; set; }
|
||||
@@ -47,6 +49,8 @@ public class AppDBContext(DbContextOptions<AppDBContext> options) : DbContext(op
|
||||
public DbSet<PurchaseOrderItem> PurchaseOrderItems { get; set; }
|
||||
public DbSet<GoodsReceipt> GoodsReceipts { get; set; }
|
||||
public DbSet<GoodsReceiptItem> GoodsReceiptItems { get; set; }
|
||||
public DbSet<GoodsReceiptItemBatch> GoodsReceiptItemBatches { get; set; }
|
||||
public DbSet<GoodsReceiptItemSerial> GoodsReceiptItemSerials { get; set; }
|
||||
public DbSet<PurchaseReturn> PurchaseReturns { get; set; }
|
||||
public DbSet<PurchaseReturnItem> PurchaseReturnItems { get; set; }
|
||||
public DbSet<SaleReturn> SaleReturns { get; set; }
|
||||
@@ -83,8 +87,15 @@ public class AppDBContext(DbContextOptions<AppDBContext> options) : DbContext(op
|
||||
|
||||
// --- Unique indexes ---
|
||||
|
||||
// NOTE: MySQL treats each NULL as distinct in a unique index, so this does not by itself
|
||||
// prevent duplicate rows for untracked items (both FK columns null) — StockRepository's
|
||||
// get-then-update pattern is what keeps those to one row per (Item, Location), same as before.
|
||||
modelBuilder.Entity<StockBalance>()
|
||||
.HasIndex(sb => new { sb.FK_ItemId, sb.LocationId })
|
||||
.HasIndex(sb => new { sb.FK_ItemId, sb.LocationId, sb.FK_ItemBatchId, sb.FK_ItemSerialNumberId })
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<ItemSerialNumber>()
|
||||
.HasIndex(s => new { s.FK_ItemId, s.SerialNumber })
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<PriceListItem>()
|
||||
@@ -316,6 +327,79 @@ public class AppDBContext(DbContextOptions<AppDBContext> options) : DbContext(op
|
||||
modelBuilder.Entity<GoodsReceiptItem>()
|
||||
.HasIndex(i => i.FK_PurchaseOrderItemId);
|
||||
|
||||
// --- ItemBatch / ItemSerialNumber ---
|
||||
|
||||
modelBuilder.Entity<ItemBatch>()
|
||||
.HasOne(b => b.Item)
|
||||
.WithMany()
|
||||
.HasForeignKey(b => b.FK_ItemId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<ItemBatch>()
|
||||
.HasIndex(b => new { b.FK_ItemId, b.BatchNumber });
|
||||
|
||||
modelBuilder.Entity<ItemSerialNumber>()
|
||||
.HasOne(s => s.Item)
|
||||
.WithMany()
|
||||
.HasForeignKey(s => s.FK_ItemId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
// --- StockBalance / StockMovement batch & serial links ---
|
||||
|
||||
modelBuilder.Entity<StockBalance>()
|
||||
.HasOne(sb => sb.ItemBatch)
|
||||
.WithMany()
|
||||
.HasForeignKey(sb => sb.FK_ItemBatchId)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired(false);
|
||||
|
||||
modelBuilder.Entity<StockBalance>()
|
||||
.HasOne(sb => sb.ItemSerialNumber)
|
||||
.WithMany()
|
||||
.HasForeignKey(sb => sb.FK_ItemSerialNumberId)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired(false);
|
||||
|
||||
modelBuilder.Entity<StockMovement>()
|
||||
.HasOne(sm => sm.ItemBatch)
|
||||
.WithMany()
|
||||
.HasForeignKey(sm => sm.FK_ItemBatchId)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired(false);
|
||||
|
||||
modelBuilder.Entity<StockMovement>()
|
||||
.HasOne(sm => sm.ItemSerialNumber)
|
||||
.WithMany()
|
||||
.HasForeignKey(sm => sm.FK_ItemSerialNumberId)
|
||||
.OnDelete(DeleteBehavior.Restrict)
|
||||
.IsRequired(false);
|
||||
|
||||
// --- GoodsReceiptItemBatch / GoodsReceiptItemSerial ---
|
||||
|
||||
modelBuilder.Entity<GoodsReceiptItemBatch>()
|
||||
.HasOne(b => b.GoodsReceiptItem)
|
||||
.WithMany(i => i.Batches)
|
||||
.HasForeignKey(b => b.FK_GoodsReceiptItemId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<GoodsReceiptItemBatch>()
|
||||
.HasOne(b => b.ItemBatch)
|
||||
.WithMany()
|
||||
.HasForeignKey(b => b.FK_ItemBatchId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<GoodsReceiptItemSerial>()
|
||||
.HasOne(s => s.GoodsReceiptItem)
|
||||
.WithMany(i => i.Serials)
|
||||
.HasForeignKey(s => s.FK_GoodsReceiptItemId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<GoodsReceiptItemSerial>()
|
||||
.HasOne(s => s.ItemSerialNumber)
|
||||
.WithMany()
|
||||
.HasForeignKey(s => s.FK_ItemSerialNumberId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
// --- PurchaseReturn ---
|
||||
|
||||
modelBuilder.Entity<PurchaseReturn>()
|
||||
@@ -478,7 +562,13 @@ public class AppDBContext(DbContextOptions<AppDBContext> options) : DbContext(op
|
||||
.HasMaxLength(100);
|
||||
|
||||
modelBuilder.Entity<SysConfig>().HasData(
|
||||
new SysConfig { Key = "InvoiceCreationMode", Value = "ModeB", UpdatedAt = new DateTime(2026, 9, 17), UpdatedBy = null }
|
||||
new SysConfig { Key = "InvoiceCreationMode", Value = "ModeB", UpdatedAt = new DateTime(2026, 9, 17), UpdatedBy = null },
|
||||
new SysConfig { Key = Distribution.Features.SysConfig.SysConfigService.KeySerialNumberTrackingEnabled, Value = "false", UpdatedAt = new DateTime(2026, 10, 1), UpdatedBy = null },
|
||||
new SysConfig { Key = Distribution.Features.SysConfig.SysConfigService.KeyBatchTrackingEnabled, Value = "false", UpdatedAt = new DateTime(2026, 10, 1), UpdatedBy = null },
|
||||
new SysConfig { Key = Distribution.Features.SysConfig.SysConfigService.KeyExpiryDateTrackingEnabled, Value = "false", UpdatedAt = new DateTime(2026, 10, 1), UpdatedBy = null },
|
||||
new SysConfig { Key = Distribution.Features.SysConfig.SysConfigService.KeyWarrantyTrackingEnabled, Value = "false", UpdatedAt = new DateTime(2026, 10, 1), UpdatedBy = null },
|
||||
new SysConfig { Key = Distribution.Features.SysConfig.SysConfigService.KeyCustomWarrantyOverrideEnabled, Value = "false", UpdatedAt = new DateTime(2026, 10, 1), UpdatedBy = null },
|
||||
new SysConfig { Key = Distribution.Features.SysConfig.SysConfigService.KeyDefaultWarrantyPeriodDays, Value = "0", UpdatedAt = new DateTime(2026, 10, 1), UpdatedBy = null }
|
||||
);
|
||||
|
||||
Seed.RoleAndPermissionSeed.Apply(modelBuilder);
|
||||
|
||||
@@ -12,7 +12,13 @@ public record CreateItemRequest(
|
||||
Guid BaseUomId,
|
||||
decimal? SalePrice,
|
||||
List<UomConversionRequest>? Conversions,
|
||||
decimal? ReorderLevel = null);
|
||||
decimal? ReorderLevel = null,
|
||||
bool TracksSerialNumbers = false,
|
||||
bool TracksBatches = false,
|
||||
bool TracksExpiryDate = false,
|
||||
bool HasWarranty = false,
|
||||
int? WarrantyPeriodDays = null,
|
||||
bool AllowCustomWarrantyPeriod = false);
|
||||
|
||||
public record UpdateItemRequest(
|
||||
string Name,
|
||||
@@ -23,7 +29,13 @@ public record UpdateItemRequest(
|
||||
Guid BaseUomId,
|
||||
decimal? SalePrice,
|
||||
bool IsActive = true,
|
||||
decimal? ReorderLevel = null);
|
||||
decimal? ReorderLevel = null,
|
||||
bool TracksSerialNumbers = false,
|
||||
bool TracksBatches = false,
|
||||
bool TracksExpiryDate = false,
|
||||
bool HasWarranty = false,
|
||||
int? WarrantyPeriodDays = null,
|
||||
bool AllowCustomWarrantyPeriod = false);
|
||||
|
||||
public record ReplaceConversionsRequest(List<UomConversionRequest> Conversions);
|
||||
|
||||
@@ -43,6 +55,12 @@ public record ItemSummaryResponse(
|
||||
string BaseUomName,
|
||||
decimal? SalePrice,
|
||||
decimal? ReorderLevel,
|
||||
bool TracksSerialNumbers,
|
||||
bool TracksBatches,
|
||||
bool TracksExpiryDate,
|
||||
bool HasWarranty,
|
||||
int? WarrantyPeriodDays,
|
||||
bool AllowCustomWarrantyPeriod,
|
||||
Models.SyncSource SyncSource,
|
||||
bool IsActive,
|
||||
DateTime CreatedAt);
|
||||
@@ -62,6 +80,12 @@ public record ItemDetailResponse(
|
||||
string BaseUomName,
|
||||
decimal? SalePrice,
|
||||
decimal? ReorderLevel,
|
||||
bool TracksSerialNumbers,
|
||||
bool TracksBatches,
|
||||
bool TracksExpiryDate,
|
||||
bool HasWarranty,
|
||||
int? WarrantyPeriodDays,
|
||||
bool AllowCustomWarrantyPeriod,
|
||||
Models.SyncSource SyncSource,
|
||||
bool IsActive,
|
||||
DateTime CreatedAt,
|
||||
|
||||
@@ -2,7 +2,7 @@ using Distribution.Infra.UoW;
|
||||
|
||||
namespace Distribution.Features.Item;
|
||||
|
||||
public class ItemService(IItemRepository repo, IUnitOfWork uow)
|
||||
public class ItemService(IItemRepository repo, IUnitOfWork uow, SysConfig.SysConfigService sysConfig)
|
||||
{
|
||||
private static UomConversionResponse MapConversion(Models.UomConversion c) =>
|
||||
new(c.Id, c.FK_FromUomId, c.FromUom.Name, c.FK_ToUomId, c.ToUom.Name, c.Factor);
|
||||
@@ -13,7 +13,10 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
|
||||
i.FK_SubCategoryId, i.SubCategory?.Name,
|
||||
i.FK_BrandId, i.Brand?.Name,
|
||||
i.FK_BaseUomId, i.BaseUom.Name,
|
||||
i.SalePrice, i.ReorderLevel, i.SyncSource, i.IsActive, i.CreatedAt);
|
||||
i.SalePrice, i.ReorderLevel,
|
||||
i.TracksSerialNumbers, i.TracksBatches, i.TracksExpiryDate,
|
||||
i.HasWarranty, i.WarrantyPeriodDays, i.AllowCustomWarrantyPeriod,
|
||||
i.SyncSource, i.IsActive, i.CreatedAt);
|
||||
|
||||
private static ItemDetailResponse MapDetail(Models.Item i) =>
|
||||
new(i.Id, i.Sku, i.Name, i.Description,
|
||||
@@ -21,9 +24,44 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
|
||||
i.FK_SubCategoryId, i.SubCategory?.Name,
|
||||
i.FK_BrandId, i.Brand?.Name,
|
||||
i.FK_BaseUomId, i.BaseUom.Name,
|
||||
i.SalePrice, i.ReorderLevel, i.SyncSource, i.IsActive, i.CreatedAt,
|
||||
i.SalePrice, i.ReorderLevel,
|
||||
i.TracksSerialNumbers, i.TracksBatches, i.TracksExpiryDate,
|
||||
i.HasWarranty, i.WarrantyPeriodDays, i.AllowCustomWarrantyPeriod,
|
||||
i.SyncSource, i.IsActive, i.CreatedAt,
|
||||
i.Conversions.Select(MapConversion).ToList());
|
||||
|
||||
/// <summary>
|
||||
/// Rejects per-item tracking flags that are off system-wide, and warranty-period values that
|
||||
/// don't make sense given HasWarranty/AllowCustomWarrantyPeriod.
|
||||
/// </summary>
|
||||
private async Task ValidateTrackingFlagsAsync(bool tracksSerialNumbers, bool tracksBatches,
|
||||
bool tracksExpiryDate, bool hasWarranty, int? warrantyPeriodDays, bool allowCustomWarrantyPeriod)
|
||||
{
|
||||
if (tracksSerialNumbers && !await sysConfig.GetSerialNumberTrackingEnabledAsync())
|
||||
throw new InvalidOperationException("Serial number tracking is disabled system-wide.");
|
||||
|
||||
if (tracksBatches && !await sysConfig.GetBatchTrackingEnabledAsync())
|
||||
throw new InvalidOperationException("Batch tracking is disabled system-wide.");
|
||||
|
||||
if (tracksExpiryDate && !await sysConfig.GetExpiryDateTrackingEnabledAsync())
|
||||
throw new InvalidOperationException("Expiry date tracking is disabled system-wide.");
|
||||
|
||||
if (hasWarranty && !await sysConfig.GetWarrantyTrackingEnabledAsync())
|
||||
throw new InvalidOperationException("Warranty tracking is disabled system-wide.");
|
||||
|
||||
if (allowCustomWarrantyPeriod && !await sysConfig.GetCustomWarrantyOverrideEnabledAsync())
|
||||
throw new InvalidOperationException("Custom warranty period override is disabled system-wide.");
|
||||
|
||||
if (!hasWarranty && (warrantyPeriodDays is not null || allowCustomWarrantyPeriod))
|
||||
throw new InvalidOperationException("Warranty period/override can only be set when HasWarranty is true.");
|
||||
|
||||
if (warrantyPeriodDays is not null && !allowCustomWarrantyPeriod)
|
||||
throw new InvalidOperationException("WarrantyPeriodDays can only be set when AllowCustomWarrantyPeriod is true.");
|
||||
|
||||
if (warrantyPeriodDays is < 0)
|
||||
throw new InvalidOperationException("WarrantyPeriodDays cannot be negative.");
|
||||
}
|
||||
|
||||
public async Task<List<ItemSummaryResponse>> GetAllAsync(Guid? categoryId = null, Guid? subCategoryId = null, Guid? brandId = null) =>
|
||||
(await repo.GetAllAsync(categoryId, subCategoryId, brandId))
|
||||
.Select(MapSummary)
|
||||
@@ -41,6 +79,9 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
|
||||
if (await repo.SkuExistsAsync(request.Sku))
|
||||
throw new InvalidOperationException($"SKU '{request.Sku}' already exists.");
|
||||
|
||||
await ValidateTrackingFlagsAsync(request.TracksSerialNumbers, request.TracksBatches,
|
||||
request.TracksExpiryDate, request.HasWarranty, request.WarrantyPeriodDays, request.AllowCustomWarrantyPeriod);
|
||||
|
||||
var item = new Models.Item
|
||||
{
|
||||
Sku = request.Sku,
|
||||
@@ -52,6 +93,12 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
|
||||
FK_BaseUomId = request.BaseUomId,
|
||||
SalePrice = request.SalePrice,
|
||||
ReorderLevel = request.ReorderLevel,
|
||||
TracksSerialNumbers = request.TracksSerialNumbers,
|
||||
TracksBatches = request.TracksBatches,
|
||||
TracksExpiryDate = request.TracksExpiryDate,
|
||||
HasWarranty = request.HasWarranty,
|
||||
WarrantyPeriodDays = request.WarrantyPeriodDays,
|
||||
AllowCustomWarrantyPeriod = request.AllowCustomWarrantyPeriod,
|
||||
SyncSource = Models.SyncSource.Manual
|
||||
};
|
||||
|
||||
@@ -78,6 +125,9 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
|
||||
if (item is null) return null;
|
||||
if (item.SyncSource == Models.SyncSource.Erp) return null;
|
||||
|
||||
await ValidateTrackingFlagsAsync(request.TracksSerialNumbers, request.TracksBatches,
|
||||
request.TracksExpiryDate, request.HasWarranty, request.WarrantyPeriodDays, request.AllowCustomWarrantyPeriod);
|
||||
|
||||
item.Name = request.Name;
|
||||
item.Description = request.Description;
|
||||
item.FK_CategoryId = request.CategoryId;
|
||||
@@ -86,6 +136,12 @@ public class ItemService(IItemRepository repo, IUnitOfWork uow)
|
||||
item.FK_BaseUomId = request.BaseUomId;
|
||||
item.SalePrice = request.SalePrice;
|
||||
item.ReorderLevel = request.ReorderLevel;
|
||||
item.TracksSerialNumbers = request.TracksSerialNumbers;
|
||||
item.TracksBatches = request.TracksBatches;
|
||||
item.TracksExpiryDate = request.TracksExpiryDate;
|
||||
item.HasWarranty = request.HasWarranty;
|
||||
item.WarrantyPeriodDays = request.WarrantyPeriodDays;
|
||||
item.AllowCustomWarrantyPeriod = request.AllowCustomWarrantyPeriod;
|
||||
item.IsActive = request.IsActive;
|
||||
|
||||
await uow.BeginAsync(ct);
|
||||
|
||||
@@ -75,6 +75,7 @@ builder.Services.AddScoped<IPurchaseReturnRepository, PurchaseReturnRepository>(
|
||||
builder.Services.AddScoped<IReturnRepository, ReturnRepository>();
|
||||
builder.Services.AddScoped<IClaimRepository, ClaimRepository>();
|
||||
builder.Services.AddScoped<ISupplierClaimRepository, SupplierClaimRepository>();
|
||||
builder.Services.AddScoped<ISysConfigRepository, SysConfigRepository>();
|
||||
builder.Services.AddScoped<IDeliveryTaskRepository, DeliveryTaskRepository>();
|
||||
builder.Services.AddScoped<IPaymentRepository, PaymentRepository>();
|
||||
|
||||
@@ -109,6 +110,7 @@ builder.Services.AddScoped<ClaimService>();
|
||||
builder.Services.AddScoped<SupplierClaimService>();
|
||||
builder.Services.AddScoped<DeliveryTaskService>();
|
||||
builder.Services.AddScoped<PaymentService>();
|
||||
builder.Services.AddScoped<SysConfigService>();
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
|
||||
|
||||
Reference in New Issue
Block a user