Fix material usage logging: remaining weight mode, edit modal, and consolidate duplicate logic
- InventoryController: extract RecordInventoryUsageAsync helper; both LogUsage (scan page) and LogMaterial (jobs modal, moved from JobsController) call it — no more duplicate save/GL logic across two controllers - Log Material modal: replace radio buttons with prominent toggle buttons so the active mode (Amount Used vs Amount Remaining) is always visually obvious; add always-visible preview line showing exactly what will be logged before saving - Edit Usage modal: add quantity field (pre-populated from existing transaction) with delta adjustment to InventoryItem.QuantityOnHand on save; include completed/terminal jobs in the dropdown so entries can be corrected after a job is marked done - Scan page job picker: include jobs completed within the last 7 days (marked with '(completed)') so usage can be logged after a job is finished Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1642,8 +1642,10 @@ public class InventoryController : Controller
|
||||
|
||||
var userId = _userManager.GetUserId(User);
|
||||
|
||||
var recentCutoff = DateTime.UtcNow.AddDays(-7);
|
||||
|
||||
var myJobs = (await _unitOfWork.Jobs.FindAsync(
|
||||
j => !j.JobStatus.IsTerminalStatus && j.AssignedUserId == userId,
|
||||
j => (!j.JobStatus.IsTerminalStatus || j.UpdatedAt >= recentCutoff) && j.AssignedUserId == userId,
|
||||
false,
|
||||
j => j.Customer,
|
||||
j => j.JobStatus))
|
||||
@@ -1651,7 +1653,7 @@ public class InventoryController : Controller
|
||||
.Select(j => new ScanJobOption
|
||||
{
|
||||
Id = j.Id,
|
||||
JobNumber = j.JobNumber,
|
||||
JobNumber = j.JobNumber + (j.JobStatus.IsTerminalStatus ? " (completed)" : ""),
|
||||
CustomerName = j.Customer != null
|
||||
? (j.Customer.CompanyName ?? j.Customer.ContactFirstName + " " + j.Customer.ContactLastName)
|
||||
: "No Customer"
|
||||
@@ -1660,7 +1662,7 @@ public class InventoryController : Controller
|
||||
|
||||
var myJobIds = myJobs.Select(j => j.Id).ToHashSet();
|
||||
var otherJobs = (await _unitOfWork.Jobs.FindAsync(
|
||||
j => !j.JobStatus.IsTerminalStatus && !myJobIds.Contains(j.Id),
|
||||
j => (!j.JobStatus.IsTerminalStatus || j.UpdatedAt >= recentCutoff) && !myJobIds.Contains(j.Id),
|
||||
false,
|
||||
j => j.Customer,
|
||||
j => j.JobStatus))
|
||||
@@ -1669,7 +1671,7 @@ public class InventoryController : Controller
|
||||
.Select(j => new ScanJobOption
|
||||
{
|
||||
Id = j.Id,
|
||||
JobNumber = j.JobNumber,
|
||||
JobNumber = j.JobNumber + (j.JobStatus.IsTerminalStatus ? " (completed)" : ""),
|
||||
CustomerName = j.Customer != null
|
||||
? (j.Customer.CompanyName ?? j.Customer.ContactFirstName + " " + j.Customer.ContactLastName)
|
||||
: "No Customer"
|
||||
@@ -1686,9 +1688,64 @@ public class InventoryController : Controller
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records powder usage logged via the mobile scan page. Creates a JobUsage
|
||||
/// InventoryTransaction (and PowderUsageLog) when a job is selected, or an
|
||||
/// Adjustment transaction when logging without a job. Updates QuantityOnHand.
|
||||
/// Core inventory usage recording logic shared by LogUsage (scan page) and LogMaterial (modal).
|
||||
/// Deducts quantityUsed from QuantityOnHand, writes an InventoryTransaction, and posts GL entries.
|
||||
/// </summary>
|
||||
private async Task<InventoryUsageResult> RecordInventoryUsageAsync(
|
||||
int inventoryItemId, int? jobId, decimal quantityUsed,
|
||||
InventoryTransactionType transactionType, string? notes)
|
||||
{
|
||||
var item = await _unitOfWork.InventoryItems.GetByIdAsync(inventoryItemId);
|
||||
if (item == null)
|
||||
return new InventoryUsageResult(false, "Inventory item not found.", 0, "", "");
|
||||
|
||||
string? reference = null;
|
||||
if (jobId.HasValue)
|
||||
{
|
||||
var job = await _unitOfWork.Jobs.GetByIdAsync(jobId.Value);
|
||||
reference = job != null ? $"Job {job.JobNumber}" : null;
|
||||
}
|
||||
|
||||
item.QuantityOnHand -= quantityUsed;
|
||||
item.UpdatedAt = DateTime.UtcNow;
|
||||
await _unitOfWork.InventoryItems.UpdateAsync(item);
|
||||
|
||||
var txn = new InventoryTransaction
|
||||
{
|
||||
InventoryItemId = item.Id,
|
||||
TransactionType = transactionType,
|
||||
Quantity = -quantityUsed,
|
||||
UnitCost = item.UnitCost,
|
||||
TotalCost = quantityUsed * item.UnitCost,
|
||||
TransactionDate = DateTime.UtcNow,
|
||||
BalanceAfter = item.QuantityOnHand,
|
||||
JobId = jobId,
|
||||
Reference = reference,
|
||||
Notes = notes?.Trim(),
|
||||
CompanyId = item.CompanyId,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
await _unitOfWork.InventoryTransactions.AddAsync(txn);
|
||||
await _unitOfWork.CompleteAsync();
|
||||
|
||||
if (item.CogsAccountId.HasValue && item.InventoryAccountId.HasValue)
|
||||
{
|
||||
var cost = quantityUsed * (item.AverageCost > 0 ? item.AverageCost : item.UnitCost);
|
||||
await _accountBalanceService.DebitAsync(item.CogsAccountId, cost);
|
||||
await _accountBalanceService.CreditAsync(item.InventoryAccountId, cost);
|
||||
}
|
||||
|
||||
return new InventoryUsageResult(
|
||||
true,
|
||||
$"Logged {quantityUsed:N2} {item.UnitOfMeasure} of {item.Name}. New balance: {item.QuantityOnHand:N2} {item.UnitOfMeasure}.",
|
||||
item.QuantityOnHand,
|
||||
item.UnitOfMeasure,
|
||||
item.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records powder usage from the mobile scan page. Resolves the used quantity
|
||||
/// (caller already converts "remaining weight" to delta before posting) and redirects to ScanSuccess.
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
@@ -1697,55 +1754,26 @@ public class InventoryController : Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
var item = await _unitOfWork.InventoryItems.GetByIdAsync(inventoryItemId);
|
||||
if (item == null) return NotFound();
|
||||
|
||||
if (quantity <= 0)
|
||||
{
|
||||
TempData["ScanError"] = "Quantity must be greater than zero.";
|
||||
return RedirectToAction(nameof(Scan), new { id = inventoryItemId });
|
||||
}
|
||||
|
||||
var userId = _userManager.GetUserId(User) ?? string.Empty;
|
||||
// Scan-based logging always records as JobUsage; Adjustment is for manual stock corrections only
|
||||
var txnType = InventoryTransactionType.JobUsage;
|
||||
var result = await RecordInventoryUsageAsync(
|
||||
inventoryItemId, jobId, quantity,
|
||||
InventoryTransactionType.JobUsage, notes);
|
||||
|
||||
item.QuantityOnHand -= quantity;
|
||||
item.UpdatedAt = DateTime.UtcNow;
|
||||
await _unitOfWork.InventoryItems.UpdateAsync(item);
|
||||
|
||||
var txn = new InventoryTransaction
|
||||
if (!result.Success)
|
||||
{
|
||||
InventoryItemId = item.Id,
|
||||
TransactionType = txnType,
|
||||
Quantity = -quantity,
|
||||
UnitCost = item.UnitCost,
|
||||
TotalCost = quantity * item.UnitCost,
|
||||
TransactionDate = DateTime.UtcNow,
|
||||
BalanceAfter = item.QuantityOnHand,
|
||||
JobId = jobId,
|
||||
Reference = jobId.HasValue ? $"Job #{jobId}" : null,
|
||||
Notes = notes?.Trim()
|
||||
};
|
||||
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);
|
||||
TempData["ScanError"] = result.Message;
|
||||
return RedirectToAction(nameof(Scan), new { id = inventoryItemId });
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
||||
TempData["ScanSuccess"] = $"Logged {quantity:N2} {item.UnitOfMeasure} of {item.Name}. New balance: {item.QuantityOnHand:N2} {item.UnitOfMeasure}.";
|
||||
TempData["ScanSuccess"] = result.Message;
|
||||
TempData["ScanItemId"] = inventoryItemId.ToString();
|
||||
TempData["ScanJobId"] = jobId?.ToString();
|
||||
TempData["ScanItemName"] = item.Name;
|
||||
TempData["ScanItemName"] = result.ItemName;
|
||||
return RedirectToAction(nameof(ScanSuccess));
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -1756,6 +1784,43 @@ public class InventoryController : Controller
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records manual material usage from the job details modal. Accepts JSON, resolves
|
||||
/// the amount used (caller sends the already-computed used quantity), and returns JSON
|
||||
/// so the modal can close and refresh inline.
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> LogMaterial([FromBody] LogMaterialRequest req)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (req.QuantityUsed <= 0)
|
||||
return Json(new { success = false, message = "Quantity used must be greater than zero." });
|
||||
|
||||
var txnType = req.TransactionType == "Waste"
|
||||
? InventoryTransactionType.Waste
|
||||
: InventoryTransactionType.JobUsage;
|
||||
|
||||
var result = await RecordInventoryUsageAsync(
|
||||
req.InventoryItemId, req.JobId, req.QuantityUsed, txnType, req.Notes);
|
||||
|
||||
return Json(new
|
||||
{
|
||||
success = result.Success,
|
||||
message = result.Message,
|
||||
newBalance = result.NewBalance,
|
||||
unitOfMeasure = result.UnitOfMeasure,
|
||||
itemName = result.ItemName
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error logging material for job {JobId}", req.JobId);
|
||||
return Json(new { success = false, message = "An error occurred. Please try again." });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Success screen shown after a usage log is saved. Offers "Log Another Item for
|
||||
/// This Job" and "Done" options.
|
||||
@@ -2003,7 +2068,7 @@ public class InventoryController : Controller
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current values of a JobUsage InventoryTransaction plus a list of active
|
||||
/// jobs so the edit modal can be pre-populated without a full page reload.
|
||||
/// jobs (plus the currently assigned job even if terminal) for the edit modal.
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetUsageForEdit(int id)
|
||||
@@ -2034,10 +2099,27 @@ public class InventoryController : Controller
|
||||
})
|
||||
.ToList();
|
||||
|
||||
// If the assigned job has terminal status it won't appear in the active list; insert it at the top
|
||||
// so the dropdown pre-selects correctly and the user can see the existing job assignment.
|
||||
if (txn.JobId.HasValue && jobs.All(j => j.Id != txn.JobId.Value))
|
||||
{
|
||||
var assignedJob = await _unitOfWork.Jobs.GetByIdAsync(txn.JobId.Value, false, j => j.Customer);
|
||||
if (assignedJob != null)
|
||||
jobs.Insert(0, new ScanJobOption
|
||||
{
|
||||
Id = assignedJob.Id,
|
||||
JobNumber = assignedJob.JobNumber,
|
||||
CustomerName = assignedJob.Customer != null
|
||||
? (assignedJob.Customer.CompanyName ?? $"{assignedJob.Customer.ContactFirstName} {assignedJob.Customer.ContactLastName}".Trim())
|
||||
: "No Customer"
|
||||
});
|
||||
}
|
||||
|
||||
return Json(new
|
||||
{
|
||||
transactionId = txn.Id,
|
||||
jobId = txn.JobId,
|
||||
quantity = Math.Abs(txn.Quantity),
|
||||
notes = txn.Notes,
|
||||
transactionDate = txn.TransactionDate.ToString("yyyy-MM-ddTHH:mm"),
|
||||
itemName = txn.InventoryItem?.Name,
|
||||
@@ -2046,14 +2128,15 @@ public class InventoryController : Controller
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves edits to a JobUsage InventoryTransaction's job assignment, notes, and date.
|
||||
/// Quantity and balance are not changed.
|
||||
/// Saves edits to a JobUsage InventoryTransaction: job assignment, quantity, notes, and date.
|
||||
/// When quantity changes the InventoryItem.QuantityOnHand is adjusted by the delta so the
|
||||
/// ledger balance remains consistent.
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> EditUsageTransaction(int id, int? jobId, string? notes, DateTime transactionDate)
|
||||
public async Task<IActionResult> EditUsageTransaction(int id, int? jobId, string? notes, DateTime transactionDate, decimal? quantity)
|
||||
{
|
||||
var txn = await _unitOfWork.InventoryTransactions.GetByIdAsync(id);
|
||||
var txn = await _unitOfWork.InventoryTransactions.GetByIdAsync(id, false, t => t.InventoryItem);
|
||||
if (txn == null) return NotFound();
|
||||
if (txn.TransactionType != InventoryTransactionType.JobUsage
|
||||
&& txn.TransactionType != InventoryTransactionType.Adjustment)
|
||||
@@ -2075,6 +2158,28 @@ public class InventoryController : Controller
|
||||
if (jobId.HasValue && txn.TransactionType == InventoryTransactionType.Adjustment)
|
||||
txn.TransactionType = InventoryTransactionType.JobUsage;
|
||||
|
||||
// Adjust inventory when the logged quantity is changed.
|
||||
// txn.Quantity is stored as a negative number for usage (e.g. -3.5 for 3.5 lbs used).
|
||||
if (quantity.HasValue && quantity.Value > 0)
|
||||
{
|
||||
var oldUsed = Math.Abs(txn.Quantity);
|
||||
var newUsed = quantity.Value;
|
||||
if (oldUsed != newUsed)
|
||||
{
|
||||
var item = txn.InventoryItem ?? await _unitOfWork.InventoryItems.GetByIdAsync(txn.InventoryItemId);
|
||||
if (item != null)
|
||||
{
|
||||
// Positive delta means less was actually used → restore the difference to inventory.
|
||||
item.QuantityOnHand += oldUsed - newUsed;
|
||||
item.UpdatedAt = DateTime.UtcNow;
|
||||
await _unitOfWork.InventoryItems.UpdateAsync(item);
|
||||
txn.BalanceAfter = item.QuantityOnHand;
|
||||
}
|
||||
txn.Quantity = -newUsed;
|
||||
txn.TotalCost = newUsed * txn.UnitCost;
|
||||
}
|
||||
}
|
||||
|
||||
txn.Notes = notes?.Trim();
|
||||
txn.TransactionDate = transactionDate.Kind == DateTimeKind.Utc
|
||||
? transactionDate : DateTime.SpecifyKind(transactionDate, DateTimeKind.Utc);
|
||||
@@ -2094,3 +2199,21 @@ public class ScanJobOption
|
||||
public string JobNumber { get; set; } = string.Empty;
|
||||
public string CustomerName { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>Result returned by RecordInventoryUsageAsync.</summary>
|
||||
public record InventoryUsageResult(
|
||||
bool Success,
|
||||
string Message,
|
||||
decimal NewBalance,
|
||||
string UnitOfMeasure,
|
||||
string ItemName);
|
||||
|
||||
/// <summary>JSON body for the LogMaterial endpoint (job details modal).</summary>
|
||||
public class LogMaterialRequest
|
||||
{
|
||||
public int JobId { get; set; }
|
||||
public int InventoryItemId { get; set; }
|
||||
public decimal QuantityUsed { get; set; }
|
||||
public string TransactionType { get; set; } = "JobUsage";
|
||||
public string? Notes { get; set; }
|
||||
}
|
||||
|
||||
@@ -4399,75 +4399,7 @@ public class JobsController : Controller
|
||||
_logger.LogInformation("Recorded first job creation for company {CompanyId}", companyId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs manual material usage from the job details page. Mirrors the QR scan LogUsage
|
||||
/// flow in InventoryController but returns JSON so the modal can close and refresh inline.
|
||||
/// Quantity is always the amount USED (caller converts from remaining if needed).
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> LogMaterial([FromBody] LogMaterialRequest req)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (req.QuantityUsed <= 0)
|
||||
return Json(new { success = false, message = "Quantity used must be greater than zero." });
|
||||
|
||||
var item = await _unitOfWork.InventoryItems.GetByIdAsync(req.InventoryItemId);
|
||||
if (item == null) return Json(new { success = false, message = "Inventory item not found." });
|
||||
|
||||
var job = await _unitOfWork.Jobs.GetByIdAsync(req.JobId);
|
||||
if (job == null) return Json(new { success = false, message = "Job not found." });
|
||||
|
||||
var txnType = req.TransactionType == "Waste"
|
||||
? InventoryTransactionType.Waste
|
||||
: InventoryTransactionType.JobUsage;
|
||||
|
||||
item.QuantityOnHand -= req.QuantityUsed;
|
||||
item.UpdatedAt = DateTime.UtcNow;
|
||||
await _unitOfWork.InventoryItems.UpdateAsync(item);
|
||||
|
||||
var txn = new PowderCoating.Core.Entities.InventoryTransaction
|
||||
{
|
||||
InventoryItemId = item.Id,
|
||||
TransactionType = txnType,
|
||||
Quantity = -req.QuantityUsed,
|
||||
UnitCost = item.UnitCost,
|
||||
TotalCost = req.QuantityUsed * item.UnitCost,
|
||||
TransactionDate = DateTime.UtcNow,
|
||||
BalanceAfter = item.QuantityOnHand,
|
||||
JobId = req.JobId,
|
||||
Reference = $"Job {job.JobNumber}",
|
||||
Notes = req.Notes?.Trim(),
|
||||
CompanyId = item.CompanyId,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
await _unitOfWork.InventoryTransactions.AddAsync(txn);
|
||||
await _unitOfWork.CompleteAsync();
|
||||
|
||||
// GL: DR COGS, CR Inventory Asset
|
||||
if (item.CogsAccountId.HasValue && item.InventoryAccountId.HasValue)
|
||||
{
|
||||
var cost = req.QuantityUsed * (item.AverageCost > 0 ? item.AverageCost : item.UnitCost);
|
||||
await _accountBalanceService.DebitAsync(item.CogsAccountId, cost);
|
||||
await _accountBalanceService.CreditAsync(item.InventoryAccountId, cost);
|
||||
}
|
||||
|
||||
return Json(new
|
||||
{
|
||||
success = true,
|
||||
message = $"Logged {req.QuantityUsed:N2} {item.UnitOfMeasure} of {item.Name}.",
|
||||
newBalance = item.QuantityOnHand,
|
||||
unitOfMeasure = item.UnitOfMeasure,
|
||||
itemName = item.Name
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error logging material for job {JobId}", req.JobId);
|
||||
return Json(new { success = false, message = "An error occurred. Please try again." });
|
||||
}
|
||||
}
|
||||
// LogMaterial has been consolidated into InventoryController.LogMaterial.
|
||||
|
||||
/// <summary>
|
||||
/// Inline-edits description, quantity, and unit price on a single job line item.
|
||||
@@ -4554,14 +4486,6 @@ public class PatchJobItemRequest
|
||||
public decimal Quantity { get; set; }
|
||||
public decimal UnitPrice { get; set; }
|
||||
}
|
||||
public class LogMaterialRequest
|
||||
{
|
||||
public int JobId { get; set; }
|
||||
public int InventoryItemId { get; set; }
|
||||
public decimal QuantityUsed { get; set; }
|
||||
public string TransactionType { get; set; } = "JobUsage";
|
||||
public string? Notes { get; set; }
|
||||
}
|
||||
public class CreateReworkJobRequest
|
||||
{
|
||||
public int ReworkRecordId { get; set; }
|
||||
|
||||
Reference in New Issue
Block a user