-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (127 loc) · 4.11 KB
/
script.js
File metadata and controls
163 lines (127 loc) · 4.11 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
let products = JSON.parse(localStorage.getItem("products")) || [];
let totalRevenue = Number(localStorage.getItem("totalRevenue")) || 0;
document.addEventListener("DOMContentLoaded", () => {
renderProducts();
updateSummary();
updateSaleOptions();
});
function saveData() {
localStorage.setItem("products", JSON.stringify(products));
localStorage.setItem("totalRevenue", totalRevenue);
}
function addProduct() {
const name = document.getElementById("productName").value.trim();
const stock = Number(document.getElementById("productStock").value);
const price = Number(document.getElementById("productPrice").value);
if (!name || stock <= 0 || price <= 0) {
alert("Lütfen tüm alanları doğru şekilde doldurun.");
return;
}
products.push({
name,
stock,
price,
sold: 0
});
saveData();
renderProducts();
updateSummary();
updateSaleOptions();
document.getElementById("productName").value = "";
document.getElementById("productStock").value = "";
document.getElementById("productPrice").value = "";
}
function renderProducts() {
const list = document.getElementById("productList");
if (products.length === 0) {
list.innerHTML = "<p>Henüz ürün yok.</p>";
return;
}
list.innerHTML = "";
products.forEach((product, index) => {
const div = document.createElement("div");
div.className = "product-item";
const lowStockText =
product.stock <= 5
? '<span class="low">⚠ Düşük stok</span>'
: "";
div.innerHTML = `
<strong>${product.name}</strong><br>
Stok: ${product.stock} adet<br>
Fiyat: ${product.price}₺<br>
Satılan: ${product.sold} adet<br>
${lowStockText}
<button onclick="deleteProduct(${index})">Ürünü Sil</button>
`;
list.appendChild(div);
});
}
function deleteProduct(index) {
products.splice(index, 1);
saveData();
renderProducts();
updateSummary();
updateSaleOptions();
}
function updateSaleOptions() {
const select = document.getElementById("saleProduct");
select.innerHTML = "";
products.forEach((product, index) => {
const option = document.createElement("option");
option.value = index;
option.textContent = `${product.name} (${product.stock} adet)`;
select.appendChild(option);
});
}
function makeSale() {
const index = Number(document.getElementById("saleProduct").value);
const quantity = Number(document.getElementById("saleQuantity").value);
if (products.length === 0) {
alert("Önce ürün eklemelisin.");
return;
}
if (!quantity || quantity <= 0) {
alert("Geçerli bir satış adedi gir.");
return;
}
const product = products[index];
if (quantity > product.stock) {
alert("Yeterli stok yok.");
return;
}
product.stock -= quantity;
product.sold += quantity;
totalRevenue += quantity * product.price;
saveData();
renderProducts();
updateSummary();
updateSaleOptions();
document.getElementById("saleQuantity").value = "";
}
function updateSummary() {
const totalProducts = products.length;
const lowStock = products.filter(p => p.stock <= 5).length;
document.getElementById("totalProducts").innerText = totalProducts;
document.getElementById("totalRevenue").innerText = totalRevenue + "₺";
document.getElementById("lowStock").innerText = lowStock;
}
function analyzeStock() {
const aiText = document.getElementById("aiText");
if (products.length === 0) {
aiText.innerText = "Analiz için ürün bulunmuyor.";
return;
}
const lowStockProducts = products.filter(p => p.stock <= 5);
const topSeller = [...products].sort((a, b) => b.sold - a.sold)[0];
let analysis = `Toplam ${products.length} ürün analiz edildi.\n\n`;
if (lowStockProducts.length > 0) {
analysis += `⚠ Düşük stoklu ürün sayısı: ${lowStockProducts.length}\n`;
} else {
analysis += "✅ Kritik seviyede stok bulunmuyor.\n";
}
if (topSeller && topSeller.sold > 0) {
analysis += `🏆 En çok satan ürün: ${topSeller.name}\n`;
}
analysis += "\nÖneri:\nStok seviyesi düşük ürünler için yeniden sipariş planlaması yap.";
aiText.innerText = analysis;
}