html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Sales & Inventory System</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
background: #f4f6f8;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
max-width: 900px;
margin: 30px auto;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
padding: 30px 20px;
}
h1 {
text-align: center;
color: #2d3e50;
margin-bottom: 30px;
}
.section {
margin-bottom: 35px;
}
h2 {
color: #007bff;
margin-bottom: 10px;
}
form {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 15px;
}
form input, form select, form button {
padding: 8px;
font-size: 16px;
border: 1px solid #ddd;
border-radius: 4px;
}
form button {
background: #007bff;
color: #fff;
border: none;
cursor: pointer;
transition: background 0.2s;
}
form button:hover {
background: #0056b3;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 8px;
}
th, td {
padding: 8px 6px;
border-bottom: 1px solid #e0e0e0;
text-align: left;
}
th {
background: #007bff;
color: #fff;
}
tr:nth-child(even) {
background: #f9f9f9;
}
@media (max-width: 700px) {
.container { padding: 10px 2px; }
form { flex-direction: column; }
form input, form select, form button { width: 100%; }
}
</style>
</head>
<body>
<div class="container">
<h1>Sales & Inventory Management</h1>
<!-- Add Product -->
<div class="section">
<h2>Add Product</h2>
<form id="addProductForm" autocomplete="off">
<input type="text" id="productName" placeholder="Product Name" required>
<input type="number" id="costPrice" placeholder="Cost Price" min="0" step="0.01" required>
<input type="number" id="sellPrice" placeholder="Selling Price" min="0" step="0.01" required>
<input type="number" id="productQty" placeholder="Quantity" min="1" required>
<button type="submit">Add Product</button>
</form>
</div>
<!-- Record Sale -->
<div class="section">
<h2>Record Sale</h2>
<form id="addSaleForm" autocomplete="off">
<select id="saleProduct" required>
<option value="">Select Product</option>
</select>
<input type="number" id="saleQty" placeholder="Quantity Sold" min="1" required>
<button type="submit">Record Sale</button>
</form>
</div>
<!-- Inventory Table -->
<div class="section">
<h2>Inventory</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>Cost Price</th>
<th>Selling Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody id="inventoryTable"></tbody>
</table>
</div>
<!-- Sales Table -->
<div class="section">
<h2>Sales</h2>
<table>
<thead>
<tr>
<th>Product</th>
<th>Qty Sold</th>
<th>Total Revenue</th>
<th>Total Cost</th>
<th>Profit/Loss</th>
<th>Margin (%)</th>
</tr>
</thead>
<tbody id="salesTable"></tbody>
</table>
</div>
</div>
<script>
// Data storage
let products = [];
let sales = [];
// Load from localStorage
function loadData() {
products = JSON.parse(localStorage.getItem('products') || '[]');
sales = JSON.parse(localStorage.getItem('sales') || '[]');
}
// Save to localStorage
function saveData() {
localStorage.setItem('products', JSON.stringify(products));
localStorage.setItem('sales', JSON.stringify(sales));
}
// Update Inventory Table
function updateInventoryTable() {
const tbody = document.getElementById('inventoryTable');
tbody.innerHTML = '';
products.forEach(prod => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${prod.name}</td>
<td>$${prod.cost.toFixed(2)}</td>
<td>$${prod.sell.toFixed(2)}</td>
<td>${prod.qty}</td>
`;
tbody.appendChild(tr);
});
}
// Update Sales Table
function updateSalesTable() {
const tbody = document.getElementById('salesTable');
tbody.innerHTML = '';
sales.forEach(sale => {
const margin = sale.revenue === 0 ? 0 : (sale.profit / sale.revenue) * 100;
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${sale.product}</td>
<td>${sale.qty}</td>
<td>$${sale.revenue.toFixed(2)}</td>
<td>$${sale.cost.toFixed(2)}</td>
<td style="color:${sale.profit>=0?'green':'red'}">$${sale.profit.toFixed(2)}</td>
<td>${margin.toFixed(2)}%</td>
`;
tbody.appendChild(tr);
});
}
// Update Product Dropdown
function updateProductDropdown() {
const select = document.getElementById('saleProduct');
select.innerHTML = '<option value="">Select Product</option>';
products.forEach((prod, idx) => {
if (prod.qty > 0) {
const opt = document.createElement('option');
opt.value = idx;
opt.textContent = prod.name;
select.appendChild(opt);
}
});
}
// Add Product
document.getElementById('addProductForm').onsubmit = function(e) {
e.preventDefault();
const name = document.getElementById('productName').value.trim();
const cost = parseFloat(document.getElementById('costPrice').value);
const sell = parseFloat(document.getElementById('sellPrice').value);
const qty = parseInt(document.getElementById('productQty').value);
if (!name || isNaN(cost) || isNaN(sell) || isNaN(qty) || qty < 1) return;
// Check if product exists
const existing = products.find(p => p.name.toLowerCase() === name.toLowerCase());
if (existing) {
existing.qty += qty;
existing.cost = cost; // Optionally update cost/sell price
existing.sell = sell;
} else {
products.push({ name, cost, sell, qty });
}
saveData();
updateInventoryTable();
updateProductDropdown();
this.reset();
};
// Add Sale
document.getElementById('addSaleForm').onsubmit = function(e) {
e.preventDefault();
const prodIdx = document.getElementById('saleProduct').value;
const qty = parseInt(document.getElementById('saleQty').value);
if (prodIdx === "" || isNaN(qty) || qty < 1) return;
const prod = products[prodIdx];
if (prod.qty < qty) {
alert('Not enough stock!');
return;
}
// Update inventory
prod.qty -= qty;
// Calculate sale
const revenue = prod.sell * qty;
const cost = prod.cost * qty;
const profit = revenue - cost;
sales.push({
product: prod.name,
qty,
revenue,
cost,
profit
});
saveData();
updateInventoryTable();
updateSalesTable();
updateProductDropdown();
this.reset();
};
// Initial load
function init() {
loadData();
updateInventoryTable();
updateSalesTable();
updateProductDropdown();
}
window.onload = init;
</script>
</body>
</html>