feat: add HasLockedInvoiceAsync method to check invoice status and prevent scheme application
This commit is contained in:
@@ -11,6 +11,7 @@ public interface ISchemeRepository
|
|||||||
Task<List<Models.SchemeRedemption>> GetRedemptionsAsync(Guid schemeId);
|
Task<List<Models.SchemeRedemption>> GetRedemptionsAsync(Guid schemeId);
|
||||||
Task AddRedemptionAsync(Models.SchemeRedemption entity);
|
Task AddRedemptionAsync(Models.SchemeRedemption entity);
|
||||||
Task<bool> HasRedemptionAsync(Guid schemeId, Guid orderId);
|
Task<bool> HasRedemptionAsync(Guid schemeId, Guid orderId);
|
||||||
|
Task<bool> HasLockedInvoiceAsync(Guid shopPOId);
|
||||||
|
|
||||||
Task<Models.SchemeBundleComponent?> GetBundleComponentAsync(Guid schemeId, Guid itemId);
|
Task<Models.SchemeBundleComponent?> GetBundleComponentAsync(Guid schemeId, Guid itemId);
|
||||||
Task AddBundleComponentAsync(Models.SchemeBundleComponent entity);
|
Task AddBundleComponentAsync(Models.SchemeBundleComponent entity);
|
||||||
|
|||||||
@@ -52,6 +52,10 @@ public class SchemeRepository(AppDBContext context) : ISchemeRepository
|
|||||||
public async Task<bool> HasRedemptionAsync(Guid schemeId, Guid shopPOId) =>
|
public async Task<bool> HasRedemptionAsync(Guid schemeId, Guid shopPOId) =>
|
||||||
await context.SchemeRedemptions.AnyAsync(r => r.FK_SchemeId == schemeId && r.FK_ShopPOId == shopPOId);
|
await context.SchemeRedemptions.AnyAsync(r => r.FK_SchemeId == schemeId && r.FK_ShopPOId == shopPOId);
|
||||||
|
|
||||||
|
// An invoice past Draft (printed or finalized) has a fixed amount, so it can no longer take a discount.
|
||||||
|
public async Task<bool> HasLockedInvoiceAsync(Guid shopPOId) =>
|
||||||
|
await context.Invoices.AnyAsync(i => i.FK_ShopPOId == shopPOId && i.IsActive && i.Status != Models.InvoiceStatus.Draft);
|
||||||
|
|
||||||
public async Task<Models.SchemeBundleComponent?> GetBundleComponentAsync(Guid schemeId, Guid itemId) =>
|
public async Task<Models.SchemeBundleComponent?> GetBundleComponentAsync(Guid schemeId, Guid itemId) =>
|
||||||
await context.SchemeBundleComponents
|
await context.SchemeBundleComponents
|
||||||
.FirstOrDefaultAsync(c => c.FK_SchemeId == schemeId && c.FK_ItemId == itemId);
|
.FirstOrDefaultAsync(c => c.FK_SchemeId == schemeId && c.FK_ItemId == itemId);
|
||||||
|
|||||||
@@ -383,6 +383,9 @@ public class SchemeService(
|
|||||||
if (await repo.HasRedemptionAsync(schemeId, shopPOId))
|
if (await repo.HasRedemptionAsync(schemeId, shopPOId))
|
||||||
throw new InvalidOperationException("This scheme has already been redeemed against this ShopPO.");
|
throw new InvalidOperationException("This scheme has already been redeemed against this ShopPO.");
|
||||||
|
|
||||||
|
if (await repo.HasLockedInvoiceAsync(shopPOId))
|
||||||
|
throw new InvalidOperationException("This order's invoice is already printed or finalized, so a scheme can no longer be applied. Apply schemes before the invoice is printed.");
|
||||||
|
|
||||||
var items = await shopPORepo.GetActiveItemsByShopPOIdAsync(shopPOId);
|
var items = await shopPORepo.GetActiveItemsByShopPOIdAsync(shopPOId);
|
||||||
var eligibility = EvaluateRule(scheme, items);
|
var eligibility = EvaluateRule(scheme, items);
|
||||||
if (!eligibility.Eligible)
|
if (!eligibility.Eligible)
|
||||||
|
|||||||
Reference in New Issue
Block a user