Added stock management
This commit is contained in:
@@ -72,11 +72,12 @@ async function loadItems() {
|
||||
tbody.innerHTML += `
|
||||
<tr>
|
||||
<td style="font-weight: 600;">${item.name}</td>
|
||||
<td><span style="background: rgba(255,255,255,0.05); padding: 0.25rem 0.6rem; border-radius: 999px; font-size: 0.8rem; border: 1px solid var(--border);">${item.category}</span></td>
|
||||
<td style="font-family: monospace; font-size: 1.05rem;">${item.total_quantity}</td>
|
||||
<td><span class="badge">${item.category}</span></td>
|
||||
<td style="font-family: monospace; font-size: 1.05rem;">${item.total_quantity || 0}</td>
|
||||
<td style="text-align: right;">
|
||||
<button class="btn btn-secondary" style="width: auto; padding: 0.4rem 0.8rem; font-size: 0.85rem; border-color: var(--success); color: var(--success);" onclick='openStockModal(${JSON.stringify(item).replace(/'/g, "'")})'>Stock</button>
|
||||
<button class="btn btn-secondary" style="width: auto; padding: 0.4rem 0.8rem; font-size: 0.85rem;" onclick='editItem(${JSON.stringify(item).replace(/'/g, "'")})'>Edit</button>
|
||||
<button class="btn btn-secondary danger-btn" style="width: auto; padding: 0.4rem 0.8rem; font-size: 0.85rem;" onclick="deleteItem(${item.id})">Delete</button>
|
||||
<button class="btn btn-secondary danger-btn" style="width: auto; padding: 0.4rem 0.8rem; font-size: 0.85rem;" onclick="deleteItem(${item.id})">Del</button>
|
||||
</td>
|
||||
</tr>
|
||||
`;
|
||||
@@ -200,8 +201,9 @@ async function loadProjects() {
|
||||
<td style="font-weight: 600;">${proj.name}</td>
|
||||
<td style="color: var(--text-muted);">${proj.description}</td>
|
||||
<td style="text-align: right;">
|
||||
<button class="btn btn-secondary" style="width: auto; padding: 0.4rem 0.8rem; font-size: 0.85rem; border-color: var(--accent); color: var(--accent);" onclick='openAssociationModal(${JSON.stringify(proj).replace(/'/g, "'")})'>Items</button>
|
||||
<button class="btn btn-secondary" style="width: auto; padding: 0.4rem 0.8rem; font-size: 0.85rem;" onclick='editProject(${JSON.stringify(proj).replace(/'/g, "'")})'>Edit</button>
|
||||
<button class="btn btn-secondary danger-btn" style="width: auto; padding: 0.4rem 0.8rem; font-size: 0.85rem;" onclick="deleteProject(${proj.id})">Delete</button>
|
||||
<button class="btn btn-secondary danger-btn" style="width: auto; padding: 0.4rem 0.8rem; font-size: 0.85rem;" onclick="deleteProject(${proj.id})">Del</button>
|
||||
</td>
|
||||
</tr>
|
||||
`;
|
||||
@@ -244,4 +246,142 @@ async function deleteProject(id) {
|
||||
await apiRequest(`/api/project?id=${id}`, 'DELETE');
|
||||
loadProjects();
|
||||
}
|
||||
}
|
||||
|
||||
// ---- STOCK ----
|
||||
async function openStockModal(item) {
|
||||
document.getElementById('stock-modal-title').innerText = `Stock: ${item.name}`;
|
||||
document.getElementById('stock-item-id').value = item.id;
|
||||
document.getElementById('stock-qty').value = '';
|
||||
document.getElementById('stock-modal').classList.add('show');
|
||||
|
||||
await reloadStockTable(item.id);
|
||||
|
||||
const locData = await apiRequest('/api/location');
|
||||
const locSelect = document.getElementById('stock-location');
|
||||
locSelect.innerHTML = '<option value="">Select Location...</option>';
|
||||
if (locData.locations) {
|
||||
locData.locations.forEach(loc => {
|
||||
locSelect.innerHTML += `<option value="${loc.id}">${loc.name}</option>`;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function reloadStockTable(itemId) {
|
||||
const tbody = document.getElementById('stock-table-body');
|
||||
tbody.innerHTML = '<tr><td colspan="3" style="text-align:center;">Loading...</td></tr>';
|
||||
|
||||
try {
|
||||
const data = await apiRequest(`/api/stock?item_id=${itemId}`);
|
||||
tbody.innerHTML = '';
|
||||
if (!data.stock || data.stock.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="3" style="text-align: center;">No stock entries.</td></tr>';
|
||||
return;
|
||||
}
|
||||
|
||||
data.stock.forEach(st => {
|
||||
tbody.innerHTML += `
|
||||
<tr>
|
||||
<td>${st.location_name || `Location #${st.location_id}`}</td>
|
||||
<td><span class="badge success">${st.quantity}</span></td>
|
||||
<td style="text-align: right;">
|
||||
<button class="btn btn-secondary danger-btn" style="padding: 0.2rem 0.5rem; font-size: 0.75rem;" onclick="deleteStock(${st.id}, ${itemId})">Del</button>
|
||||
</td>
|
||||
</tr>
|
||||
`;
|
||||
});
|
||||
} catch (e) {
|
||||
tbody.innerHTML = '<tr><td colspan="3" style="text-align: center;">Failed to load.</td></tr>';
|
||||
}
|
||||
}
|
||||
|
||||
async function saveStock(event) {
|
||||
event.preventDefault();
|
||||
const itemId = document.getElementById('stock-item-id').value;
|
||||
const payload = {
|
||||
item_id: parseInt(itemId, 10),
|
||||
location_id: parseInt(document.getElementById('stock-location').value, 10),
|
||||
quantity: parseInt(document.getElementById('stock-qty').value, 10)
|
||||
};
|
||||
|
||||
await apiRequest('/api/stock', 'POST', payload);
|
||||
document.getElementById('stock-qty').value = '';
|
||||
await reloadStockTable(itemId);
|
||||
loadItems();
|
||||
}
|
||||
|
||||
async function deleteStock(stockId, itemId) {
|
||||
if (confirm("Remove this stock entry?")) {
|
||||
await apiRequest(`/api/stock?id=${stockId}`, 'DELETE');
|
||||
await reloadStockTable(itemId);
|
||||
loadItems();
|
||||
}
|
||||
}
|
||||
|
||||
// ---- ASSOCIATIONS ----
|
||||
async function openAssociationModal(project) {
|
||||
document.getElementById('association-modal-title').innerText = `Items for: ${project.name}`;
|
||||
document.getElementById('assoc-project-id').value = project.id;
|
||||
document.getElementById('assoc-qty').value = '';
|
||||
document.getElementById('association-modal').classList.add('show');
|
||||
|
||||
await reloadAssociationTable(project.id);
|
||||
|
||||
const itemData = await apiRequest('/api/item');
|
||||
const itemSelect = document.getElementById('assoc-item');
|
||||
itemSelect.innerHTML = '<option value="">Select Item...</option>';
|
||||
if (itemData.items) {
|
||||
itemData.items.forEach(it => {
|
||||
itemSelect.innerHTML += `<option value="${it.id}">${it.name} (Avail: ${it.total_quantity})</option>`;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function reloadAssociationTable(projectId) {
|
||||
const tbody = document.getElementById('association-table-body');
|
||||
tbody.innerHTML = '<tr><td colspan="3" style="text-align:center;">Loading...</td></tr>';
|
||||
|
||||
try {
|
||||
const data = await apiRequest(`/api/association?project_id=${projectId}`);
|
||||
tbody.innerHTML = '';
|
||||
if (!data.associations || data.associations.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="3" style="text-align: center;">No allocated items.</td></tr>';
|
||||
return;
|
||||
}
|
||||
|
||||
data.associations.forEach(asc => {
|
||||
tbody.innerHTML += `
|
||||
<tr>
|
||||
<td>${asc.item_name || `Item #${asc.item_id}`}</td>
|
||||
<td><span class="badge success">${asc.quantity}</span></td>
|
||||
<td style="text-align: right;">
|
||||
<button class="btn btn-secondary danger-btn" style="padding: 0.2rem 0.5rem; font-size: 0.75rem;" onclick="deleteAssociation(${asc.id}, ${projectId})">Del</button>
|
||||
</td>
|
||||
</tr>
|
||||
`;
|
||||
});
|
||||
} catch (e) {
|
||||
tbody.innerHTML = '<tr><td colspan="3" style="text-align: center;">Failed to load.</td></tr>';
|
||||
}
|
||||
}
|
||||
|
||||
async function saveAssociation(event) {
|
||||
event.preventDefault();
|
||||
const projectId = document.getElementById('assoc-project-id').value;
|
||||
const payload = {
|
||||
project_id: parseInt(projectId, 10),
|
||||
item_id: parseInt(document.getElementById('assoc-item').value, 10),
|
||||
quantity: parseInt(document.getElementById('assoc-qty').value, 10)
|
||||
};
|
||||
|
||||
await apiRequest('/api/association', 'POST', payload);
|
||||
document.getElementById('assoc-qty').value = '';
|
||||
await reloadAssociationTable(projectId);
|
||||
}
|
||||
|
||||
async function deleteAssociation(assocId, projectId) {
|
||||
if (confirm("Remove this item from the project?")) {
|
||||
await apiRequest(`/api/association?id=${assocId}`, 'DELETE');
|
||||
await reloadAssociationTable(projectId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user