diff --git a/src/PowderCoating.Web/Controllers/InventoryController.cs b/src/PowderCoating.Web/Controllers/InventoryController.cs index 63be478..17e0a35 100644 --- a/src/PowderCoating.Web/Controllers/InventoryController.cs +++ b/src/PowderCoating.Web/Controllers/InventoryController.cs @@ -30,6 +30,7 @@ public class InventoryController : Controller private readonly IInventoryAiLookupService _aiLookupService; private readonly ISubscriptionService _subscriptionService; private readonly UserManager _userManager; + private readonly IAccountBalanceService _accountBalanceService; public InventoryController( IUnitOfWork unitOfWork, @@ -39,7 +40,8 @@ public class InventoryController : Controller IMeasurementConversionService measurementService, IInventoryAiLookupService aiLookupService, ISubscriptionService subscriptionService, - UserManager userManager) + UserManager userManager, + IAccountBalanceService accountBalanceService) { _unitOfWork = unitOfWork; _mapper = mapper; @@ -49,6 +51,7 @@ public class InventoryController : Controller _aiLookupService = aiLookupService; _subscriptionService = subscriptionService; _userManager = userManager; + _accountBalanceService = accountBalanceService; } /// @@ -1552,6 +1555,14 @@ public class InventoryController : Controller await _unitOfWork.InventoryTransactions.AddAsync(txn); await _unitOfWork.SaveChangesAsync(); + // GL: DR COGS, CR Inventory Asset — no-op if accounts not configured on the item + if (item.CogsAccountId.HasValue && item.InventoryAccountId.HasValue) + { + var cost = quantity * (item.AverageCost > 0 ? item.AverageCost : item.UnitCost); + await _accountBalanceService.DebitAsync(item.CogsAccountId, cost); + await _accountBalanceService.CreditAsync(item.InventoryAccountId, cost); + } + // PowderUsageLog requires a specific JobItem + Coat FK — scan-based logging // doesn't have that context, so we rely on the InventoryTransaction alone // for the audit trail. Coat-level PowderUsageLogs are created by the job workflow. diff --git a/src/PowderCoating.Web/Controllers/JobsController.cs b/src/PowderCoating.Web/Controllers/JobsController.cs index c837a88..fb7d2b6 100644 --- a/src/PowderCoating.Web/Controllers/JobsController.cs +++ b/src/PowderCoating.Web/Controllers/JobsController.cs @@ -37,6 +37,7 @@ public class JobsController : Controller private readonly IJobItemAssemblyService _jobItemAssemblyService; private readonly IHubContext _hub; private readonly IHubContext _shopHub; + private readonly IAccountBalanceService _accountBalanceService; public JobsController( IUnitOfWork unitOfWork, @@ -52,7 +53,8 @@ public class JobsController : Controller IPricingCalculationService pricingService, IJobItemAssemblyService jobItemAssemblyService, IHubContext hub, - IHubContext shopHub) + IHubContext shopHub, + IAccountBalanceService accountBalanceService) { _unitOfWork = unitOfWork; _mapper = mapper; @@ -68,6 +70,7 @@ public class JobsController : Controller _jobItemAssemblyService = jobItemAssemblyService; _hub = hub; _shopHub = shopHub; + _accountBalanceService = accountBalanceService; } /// @@ -2726,6 +2729,14 @@ public class JobsController : Controller inventoryItem.QuantityOnHand -= deductNow; await _unitOfWork.InventoryItems.UpdateAsync(inventoryItem); + // GL: DR COGS, CR Inventory Asset (accrual) — no-op if accounts not configured + if (inventoryItem.CogsAccountId.HasValue && inventoryItem.InventoryAccountId.HasValue) + { + var cost = deductNow * (inventoryItem.AverageCost > 0 ? inventoryItem.AverageCost : inventoryItem.UnitCost); + await _accountBalanceService.DebitAsync(inventoryItem.CogsAccountId, cost); + await _accountBalanceService.CreditAsync(inventoryItem.InventoryAccountId, cost); + } + _logger.LogInformation( "Deducted {Lbs} lbs (net of pre-logged) of {Item} from inventory for Job {JobNumber}. New quantity: {NewQty}", deductNow, inventoryItem.Name, job.JobNumber, inventoryItem.QuantityOnHand); diff --git a/tests/PowderCoating.UnitTests/QuoteAndReworkControllerFlowTests.cs b/tests/PowderCoating.UnitTests/QuoteAndReworkControllerFlowTests.cs index 7f56144..a7f3be9 100644 --- a/tests/PowderCoating.UnitTests/QuoteAndReworkControllerFlowTests.cs +++ b/tests/PowderCoating.UnitTests/QuoteAndReworkControllerFlowTests.cs @@ -138,7 +138,8 @@ public class QuoteAndReworkControllerFlowTests Mock.Of(), new JobItemAssemblyService(), Mock.Of>(), - Mock.Of>()); + Mock.Of>(), + Mock.Of()); var result = await controller.AddReworkRecord(new CreateReworkRecordDto {