-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.js
137 lines (105 loc) · 4.12 KB
/
inventory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
let inventory = JSON.parse(localStorage.getItem("inventory")) || [];
function displayInventory() {
const itemList = document.getElementById("itemList");
itemList.innerHTML = "";
inventory.forEach((item, index) => {
itemList.innerHTML += `<li>${item.name} - $${item.price} (Stock: ${item.stock})
<button onclick="sellItem(${index})">Sell</button>
<button onclick="editItem(${index})">Edit</button>
<button onclick="deleteItem(${index})">Delete</button></li>`;
});
}
function addItem() {
const name = prompt("Item Name:");
const price = parseFloat(prompt("Item Price:"));
const stock = parseInt(prompt("Initial Stock:"));
if (!name || isNaN(price) || price <= 0 || isNaN(stock) || stock < 0) {
alert("Invalid input. Please enter valid item details.");
return;
}
inventory.push({ name, price, stock });
localStorage.setItem("inventory", JSON.stringify(inventory));
displayInventory();
}
// Sell an item and record sales
function sellItem(index) {
const item = inventory[index];
const quantity = parseInt(prompt(`Enter quantity to sell (Available: ${item.stock}):`));
if (isNaN(quantity) || quantity <= 0 || quantity > item.stock) {
alert("Invalid quantity");
return;
}
const saleAmount = item.price * quantity;
item.stock -= quantity;
if (item.stock === 0) {
inventory.splice(index, 1);
}
localStorage.setItem("inventory", JSON.stringify(inventory));
displayInventory();
recordSale(saleAmount); // Update sales
}
function editItem(index) {
const item = inventory[index];
const name = prompt("New Name:", item.name);
const price = parseFloat(prompt("New Price:", item.price));
const stock = parseInt(prompt("New Stock:", item.stock));
if (name && !isNaN(price) && !isNaN(stock)) {
inventory[index] = { name, price, stock };
localStorage.setItem("inventory", JSON.stringify(inventory));
displayInventory();
} else {
alert("Invalid input");
}
}
function deleteItem(index) {
inventory.splice(index, 1);
localStorage.setItem("inventory", JSON.stringify(inventory));
displayInventory();
}
displayInventory();
function sellItem(index) {
const customerName = prompt("Enter Customer Name:");
if (!customerName) {
alert("Customer name is required!");
return;
}
const item = inventory[index];
const quantity = parseInt(prompt(`Enter quantity to sell (Available: ${item.stock}):`));
if (isNaN(quantity) || quantity <= 0 || quantity > item.stock) {
alert("Invalid quantity");
return;
}
const saleAmount = item.price * quantity;
item.stock -= quantity;
if (item.stock === 0) {
inventory.splice(index, 1);
}
localStorage.setItem("inventory", JSON.stringify(inventory));
displayInventory();
recordSale(saleAmount); // Update sales
generateReceipt(customerName, [{ name: item.name, price: item.price, quantity }], saleAmount);
}
function generateReceipt(customerName, items, totalAmount) {
const receiptDiv = document.getElementById("receipt");
const itemList = document.getElementById("purchasedItems");
document.getElementById("customerName").textContent = customerName;
document.getElementById("purchaseDate").textContent = new Date().toLocaleString();
document.getElementById("totalAmount").textContent = totalAmount.toFixed(2);
itemList.innerHTML = "";
items.forEach(item => {
itemList.innerHTML += `<li>${item.name} - $${item.price} x ${item.quantity}</li>`;
});
receiptDiv.style.display = "block";
}
function printReceipt() {
const receiptContent = document.getElementById("receipt").outerHTML;
const newWindow = window.open("", "_blank");
newWindow.document.write(`
<html>
<head><title>Print Receipt</title></head>
<body>${receiptContent}</body>
</html>
`);
newWindow.document.close();
newWindow.print();
}