Inline add-stock prompt when label scan finds existing inventory item
When a scanned label matches an item already in the tenant's inventory, the scanner now opens an inline modal asking the user to add stock to the existing item rather than navigating away or creating a duplicate. - InventoryController.AddStock: new POST endpoint that creates a Purchase transaction, updates QuantityOnHand, and optionally updates UnitCost / LastPurchasePrice when a new cost is provided. Returns new balance as JSON. - InventoryController.ScanLabel: extends the duplicate-detection response to include existingQuantityOnHand and existingUnitOfMeasure so the modal can display current stock level. - _LabelScanModal.cshtml: adds #addStockModal with quantity (+ UOM label), optional unit cost (pre-filled from scan), optional notes, Add Stock CTA, and an escape hatch to create a new entry instead. - inventory-label-scan.js: when scan returns existingInventoryId the JS opens addStockModal instead of a warning banner. Submitting POSTs to /Inventory/AddStock and shows the updated balance in a success bar with a link to the item. The 'new entry instead' path hides the modal and pre-fills the create form with a softer duplicate warning. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -46,11 +46,33 @@
|
||||
const processingEl = document.getElementById('scan-processing');
|
||||
const processingMsgEl= document.getElementById('scan-processing-msg');
|
||||
|
||||
// Add-stock modal elements
|
||||
const addStockModalEl = document.getElementById('addStockModal');
|
||||
const bsAddStockModal = addStockModalEl ? new bootstrap.Modal(addStockModalEl) : null;
|
||||
const addStockItemName = document.getElementById('add-stock-item-name');
|
||||
const addStockCurrentQty= document.getElementById('add-stock-current-qty');
|
||||
const addStockUomLabel = document.getElementById('add-stock-uom-label');
|
||||
const addStockQtyInput = document.getElementById('add-stock-qty');
|
||||
const addStockCostInput = document.getElementById('add-stock-cost');
|
||||
const addStockNotesInput= document.getElementById('add-stock-notes');
|
||||
const addStockStatusEl = document.getElementById('add-stock-status');
|
||||
const addStockConfirmBtn= document.getElementById('add-stock-confirm-btn');
|
||||
|
||||
let _addStockItemId = null;
|
||||
let _lastScanData = null;
|
||||
|
||||
if (!modalEl || !videoEl || !canvasEl) return;
|
||||
|
||||
scanBtn.addEventListener('click', openScanner);
|
||||
modalEl.addEventListener('hide.bs.modal', onModalClose);
|
||||
if (shutterBtn) shutterBtn.addEventListener('click', captureFrame);
|
||||
if (addStockConfirmBtn) addStockConfirmBtn.addEventListener('click', submitAddStock);
|
||||
// "Create new entry instead" hides the add-stock modal and pre-fills the create form
|
||||
const addStockNewBtn = document.getElementById('add-stock-new-btn');
|
||||
if (addStockNewBtn) addStockNewBtn.addEventListener('click', () => {
|
||||
bsAddStockModal?.hide();
|
||||
if (_lastScanData) fillFromScan(_lastScanData, /* skipDuplicatePrompt */ true);
|
||||
});
|
||||
window.addEventListener('beforeunload', releaseCamera);
|
||||
|
||||
// Pre-warm camera if browser has already granted permission (no prompt risk)
|
||||
@@ -301,7 +323,15 @@
|
||||
}
|
||||
|
||||
bsModal.hide();
|
||||
fillFromScan(data);
|
||||
|
||||
if (data.existingInventoryId) {
|
||||
// Product already in inventory — show inline add-stock prompt
|
||||
_lastScanData = data;
|
||||
_addStockItemId = data.existingInventoryId;
|
||||
openAddStockModal(data);
|
||||
} else {
|
||||
fillFromScan(data);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
hideProcessing();
|
||||
@@ -309,9 +339,80 @@
|
||||
}
|
||||
}
|
||||
|
||||
// ── Add-stock modal ───────────────────────────────────────────────────
|
||||
|
||||
function openAddStockModal(data) {
|
||||
if (!bsAddStockModal) { fillFromScan(data); return; }
|
||||
|
||||
const uom = data.existingUnitOfMeasure || 'lbs';
|
||||
if (addStockItemName) addStockItemName.textContent = data.existingInventoryName || data.colorName || 'This product';
|
||||
if (addStockCurrentQty) addStockCurrentQty.textContent = `${(data.existingQuantityOnHand ?? 0).toFixed(2)} ${uom}`;
|
||||
if (addStockUomLabel) addStockUomLabel.textContent = uom;
|
||||
if (addStockQtyInput) addStockQtyInput.value = '';
|
||||
if (addStockCostInput) addStockCostInput.value = data.unitPrice > 0 ? data.unitPrice : '';
|
||||
if (addStockNotesInput) addStockNotesInput.value = '';
|
||||
if (addStockStatusEl) { addStockStatusEl.className = 'd-none'; addStockStatusEl.textContent = ''; }
|
||||
if (addStockConfirmBtn) addStockConfirmBtn.disabled = false;
|
||||
|
||||
bsAddStockModal.show();
|
||||
}
|
||||
|
||||
async function submitAddStock() {
|
||||
const qty = parseFloat(addStockQtyInput?.value);
|
||||
if (!qty || qty <= 0) {
|
||||
showAddStockStatus('danger', 'Please enter a quantity greater than zero.');
|
||||
return;
|
||||
}
|
||||
|
||||
addStockConfirmBtn.disabled = true;
|
||||
addStockConfirmBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-1"></span>Saving…';
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
inventoryItemId: _addStockItemId,
|
||||
quantity: qty,
|
||||
});
|
||||
const cost = parseFloat(addStockCostInput?.value);
|
||||
if (cost > 0) params.append('unitCost', cost);
|
||||
const notes = addStockNotesInput?.value?.trim();
|
||||
if (notes) params.append('notes', notes);
|
||||
|
||||
const resp = await fetch('/Inventory/AddStock?' + params.toString(), { method: 'POST' });
|
||||
if (!resp.ok) throw new Error(`Server error ${resp.status}`);
|
||||
const data = await resp.json();
|
||||
|
||||
if (!data.success) {
|
||||
showAddStockStatus('danger', data.errorMessage || 'Failed to add stock.');
|
||||
addStockConfirmBtn.disabled = false;
|
||||
addStockConfirmBtn.innerHTML = '<i class="bi bi-plus-circle me-1"></i>Add Stock';
|
||||
return;
|
||||
}
|
||||
|
||||
// Success — close modal and show confirmation on the form
|
||||
bsAddStockModal.hide();
|
||||
showFormStatus('success',
|
||||
`<i class="bi bi-check-circle-fill me-1"></i>` +
|
||||
`Added <strong>${qty.toFixed(2)} ${data.unitOfMeasure}</strong> to <strong>${data.itemName}</strong>. ` +
|
||||
`New stock: ${(data.newQuantityOnHand ?? 0).toFixed(2)} ${data.unitOfMeasure}. ` +
|
||||
`<a href="/Inventory/Details/${_addStockItemId}" class="alert-link">View item</a>`
|
||||
);
|
||||
|
||||
} catch (err) {
|
||||
showAddStockStatus('danger', 'Error: ' + err.message);
|
||||
addStockConfirmBtn.disabled = false;
|
||||
addStockConfirmBtn.innerHTML = '<i class="bi bi-plus-circle me-1"></i>Add Stock';
|
||||
}
|
||||
}
|
||||
|
||||
function showAddStockStatus(type, msg) {
|
||||
if (!addStockStatusEl) return;
|
||||
addStockStatusEl.className = `alert alert-${type} py-2 small`;
|
||||
addStockStatusEl.textContent = msg;
|
||||
}
|
||||
|
||||
// ── Fill the inventory form from scan result ───────────────────────────
|
||||
|
||||
function fillFromScan(data) {
|
||||
function fillFromScan(data, skipDuplicatePrompt = false) {
|
||||
const filled = [];
|
||||
|
||||
function setIf(id, value, label) {
|
||||
@@ -404,14 +505,16 @@
|
||||
? ' <span class="badge bg-success ms-1">Added to platform catalog</span>'
|
||||
: '';
|
||||
|
||||
if (data.existingInventoryId) {
|
||||
if (data.existingInventoryId && !skipDuplicatePrompt) {
|
||||
// Duplicate handled by add-stock modal — don't show a banner here
|
||||
} else if (data.existingInventoryId && skipDuplicatePrompt) {
|
||||
const itemName = data.existingInventoryName || data.colorName || 'This product';
|
||||
const filledNote = filled.length > 0 ? ` Fields pre-filled from scan.` : '';
|
||||
showFormStatus('warning',
|
||||
`<i class="bi bi-exclamation-triangle-fill me-1"></i>` +
|
||||
`<strong>${itemName}</strong> is already in your inventory. ` +
|
||||
`<a href="/Inventory/Details/${data.existingInventoryId}" class="alert-link fw-semibold">View existing item</a>` +
|
||||
` — or continue below to add a new entry (e.g. a new lot or bag size).${filledNote}${catalogNote}`
|
||||
`Creating a new entry — <strong>${itemName}</strong> already exists. ` +
|
||||
`<a href="/Inventory/Details/${data.existingInventoryId}" class="alert-link">View existing item</a>` +
|
||||
`${filledNote}${catalogNote}`
|
||||
);
|
||||
} else if (filled.length > 0) {
|
||||
showFormStatus('success', `Filled from label scan: ${filled.join(', ')}.${catalogNote}`);
|
||||
|
||||
Reference in New Issue
Block a user