From fdfca7cf5ee89382e29c767dd8f099f1419cb01f Mon Sep 17 00:00:00 2001 From: DeepnaPooja Date: Thu, 22 Oct 2026 10:40:43 +0530 Subject: [PATCH] feat: add HasLockedInvoiceAsync method to check invoice status and prevent scheme application --- Distribution/Features/Scheme/ISchemeRepository.cs | 1 + Distribution/Features/Scheme/SchemeRepository.cs | 4 ++++ Distribution/Features/Scheme/SchemeService.cs | 3 +++ 3 files changed, 8 insertions(+) diff --git a/Distribution/Features/Scheme/ISchemeRepository.cs b/Distribution/Features/Scheme/ISchemeRepository.cs index 518e3c5..39d4b86 100644 --- a/Distribution/Features/Scheme/ISchemeRepository.cs +++ b/Distribution/Features/Scheme/ISchemeRepository.cs @@ -11,6 +11,7 @@ public interface ISchemeRepository Task> GetRedemptionsAsync(Guid schemeId); Task AddRedemptionAsync(Models.SchemeRedemption entity); Task HasRedemptionAsync(Guid schemeId, Guid orderId); + Task HasLockedInvoiceAsync(Guid shopPOId); Task GetBundleComponentAsync(Guid schemeId, Guid itemId); Task AddBundleComponentAsync(Models.SchemeBundleComponent entity); diff --git a/Distribution/Features/Scheme/SchemeRepository.cs b/Distribution/Features/Scheme/SchemeRepository.cs index 2d22e62..12f6ee1 100644 --- a/Distribution/Features/Scheme/SchemeRepository.cs +++ b/Distribution/Features/Scheme/SchemeRepository.cs @@ -52,6 +52,10 @@ public class SchemeRepository(AppDBContext context) : ISchemeRepository public async Task HasRedemptionAsync(Guid schemeId, Guid 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 HasLockedInvoiceAsync(Guid shopPOId) => + await context.Invoices.AnyAsync(i => i.FK_ShopPOId == shopPOId && i.IsActive && i.Status != Models.InvoiceStatus.Draft); + public async Task GetBundleComponentAsync(Guid schemeId, Guid itemId) => await context.SchemeBundleComponents .FirstOrDefaultAsync(c => c.FK_SchemeId == schemeId && c.FK_ItemId == itemId); diff --git a/Distribution/Features/Scheme/SchemeService.cs b/Distribution/Features/Scheme/SchemeService.cs index e17f810..cf39dd7 100644 --- a/Distribution/Features/Scheme/SchemeService.cs +++ b/Distribution/Features/Scheme/SchemeService.cs @@ -383,6 +383,9 @@ public class SchemeService( if (await repo.HasRedemptionAsync(schemeId, shopPOId)) 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 eligibility = EvaluateRule(scheme, items); if (!eligibility.Eligible)