Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
@@ -2,15 +2,18 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
2 |
const productForm = document.getElementById('productForm');
|
3 |
const productTable = document.getElementById('productTable').getElementsByTagName('tbody')[0];
|
4 |
const searchInput = document.getElementById('searchInput');
|
|
|
5 |
const totalSoldElement = document.getElementById('totalSold');
|
6 |
const totalRevenueElement = document.getElementById('totalRevenue');
|
7 |
|
8 |
let totalSold = 0; // Общее количество проданных товаров
|
9 |
let totalRevenue = 0; // Общая выручка
|
|
|
10 |
|
11 |
// Загрузка данных из localStorage при загрузке страницы
|
12 |
loadProducts();
|
13 |
loadStats();
|
|
|
14 |
|
15 |
// Обработка добавления товара
|
16 |
productForm.addEventListener('submit', function (e) {
|
@@ -72,7 +75,8 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
72 |
<td>${product.salePrice}</td>
|
73 |
<td>${product.quantity}</td>
|
74 |
<td class="actions">
|
75 |
-
<
|
|
|
76 |
<button class="add-stock-btn" onclick="addStock(${product.id})">Приход</button>
|
77 |
<button class="delete-btn" onclick="deleteProduct(${product.id})">Удалить</button>
|
78 |
</td>
|
@@ -94,65 +98,83 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
94 |
updateStatsDisplay();
|
95 |
}
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
// Функция обновления отображения статистики
|
98 |
function updateStatsDisplay() {
|
99 |
totalSoldElement.textContent = totalSold;
|
100 |
totalRevenueElement.textContent = totalRevenue.toFixed(2);
|
101 |
}
|
102 |
|
103 |
-
// Функция
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
// Обновляем таблицу и статистику
|
118 |
-
productTable.innerHTML = '';
|
119 |
-
loadProducts();
|
120 |
-
updateStatsDisplay();
|
121 |
-
} else {
|
122 |
-
alert('Товар закончился на складе.');
|
123 |
-
}
|
124 |
-
};
|
125 |
-
|
126 |
-
// Функция добавления остатков
|
127 |
-
window.addStock = function (productId) {
|
128 |
-
const quantityToAdd = prompt('Введите количество для прихода:');
|
129 |
-
if (quantityToAdd && !isNaN(quantityToAdd) && quantityToAdd > 0) {
|
130 |
-
let products = JSON.parse(localStorage.getItem('products')) || [];
|
131 |
-
const productIndex = products.findIndex(p => p.id === productId);
|
132 |
-
|
133 |
-
if (productIndex !== -1) {
|
134 |
-
products[productIndex].quantity += parseInt(quantityToAdd);
|
135 |
-
localStorage.setItem('products', JSON.stringify(products));
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
}
|
141 |
} else {
|
142 |
alert('Пожалуйста, введите корректное количество.');
|
143 |
}
|
144 |
};
|
145 |
|
146 |
-
// Функция удаления товара
|
147 |
-
window.
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
156 |
}
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
2 |
const productForm = document.getElementById('productForm');
|
3 |
const productTable = document.getElementById('productTable').getElementsByTagName('tbody')[0];
|
4 |
const searchInput = document.getElementById('searchInput');
|
5 |
+
const cartTable = document.getElementById('cartTable').getElementsByTagName('tbody')[0];
|
6 |
const totalSoldElement = document.getElementById('totalSold');
|
7 |
const totalRevenueElement = document.getElementById('totalRevenue');
|
8 |
|
9 |
let totalSold = 0; // Общее количество проданных товаров
|
10 |
let totalRevenue = 0; // Общая выручка
|
11 |
+
let cart = []; // Корзина
|
12 |
|
13 |
// Загрузка данных из localStorage при загрузке страницы
|
14 |
loadProducts();
|
15 |
loadStats();
|
16 |
+
loadCart();
|
17 |
|
18 |
// Обработка добавления товара
|
19 |
productForm.addEventListener('submit', function (e) {
|
|
|
75 |
<td>${product.salePrice}</td>
|
76 |
<td>${product.quantity}</td>
|
77 |
<td class="actions">
|
78 |
+
<input type="number" min="1" max="${product.quantity}" class="quantity-input" placeholder="Количество">
|
79 |
+
<button class="add-to-cart-btn" onclick="addToCart(${product.id})">Добавить в корзину</button>
|
80 |
<button class="add-stock-btn" onclick="addStock(${product.id})">Приход</button>
|
81 |
<button class="delete-btn" onclick="deleteProduct(${product.id})">Удалить</button>
|
82 |
</td>
|
|
|
98 |
updateStatsDisplay();
|
99 |
}
|
100 |
|
101 |
+
// Функция загрузки корзины из localStorage
|
102 |
+
function loadCart() {
|
103 |
+
cart = JSON.parse(localStorage.getItem('cart')) || [];
|
104 |
+
updateCartDisplay();
|
105 |
+
}
|
106 |
+
|
107 |
// Функция обновления отображения статистики
|
108 |
function updateStatsDisplay() {
|
109 |
totalSoldElement.textContent = totalSold;
|
110 |
totalRevenueElement.textContent = totalRevenue.toFixed(2);
|
111 |
}
|
112 |
|
113 |
+
// Функция обновления отображения корзины
|
114 |
+
function updateCartDisplay() {
|
115 |
+
cartTable.innerHTML = ''; // Очищаем корзину перед обновлением
|
116 |
+
cart.forEach(item => {
|
117 |
+
const row = cartTable.insertRow();
|
118 |
+
row.innerHTML = `
|
119 |
+
<td>${item.name}</td>
|
120 |
+
<td>${item.quantity}</td>
|
121 |
+
<td>${item.salePrice}</td>
|
122 |
+
<td>${item.quantity * item.salePrice}</td>
|
123 |
+
<td><button class="delete-btn" onclick="removeFromCart(${item.id})">Удалить</button></td>
|
124 |
+
`;
|
125 |
+
});
|
126 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
// Функция добавления товара в корзину
|
129 |
+
window.addToCart = function (productId) {
|
130 |
+
const quantityInput = document.querySelector(`tr[data-id="${productId}"] .quantity-input`);
|
131 |
+
const quantity = parseInt(quantityInput.value);
|
132 |
+
|
133 |
+
if (quantity && quantity > 0) {
|
134 |
+
const products = JSON.parse(localStorage.getItem('products')) || [];
|
135 |
+
const product = products.find(p => p.id === productId);
|
136 |
+
|
137 |
+
if (product && product.quantity >= quantity) {
|
138 |
+
const cartItem = cart.find(item => item.id === productId);
|
139 |
+
|
140 |
+
if (cartItem) {
|
141 |
+
cartItem.quantity += quantity;
|
142 |
+
} else {
|
143 |
+
cart.push({
|
144 |
+
id: productId,
|
145 |
+
name: product.name,
|
146 |
+
salePrice: product.salePrice,
|
147 |
+
quantity: quantity
|
148 |
+
});
|
149 |
+
}
|
150 |
+
|
151 |
+
localStorage.setItem('cart', JSON.stringify(cart));
|
152 |
+
updateCartDisplay();
|
153 |
+
} else {
|
154 |
+
alert('Недостаточно товара на складе.');
|
155 |
}
|
156 |
} else {
|
157 |
alert('Пожалуйста, введите корректное количество.');
|
158 |
}
|
159 |
};
|
160 |
|
161 |
+
// Функция удаления товара из корзины
|
162 |
+
window.removeFromCart = function (productId) {
|
163 |
+
cart = cart.filter(item => item.id !== productId);
|
164 |
+
localStorage.setItem('cart', JSON.stringify(cart));
|
165 |
+
updateCartDisplay();
|
166 |
+
};
|
167 |
|
168 |
+
// Функция продажи товаров из корзины
|
169 |
+
window.sellCart = function () {
|
170 |
+
if (cart.length === 0) {
|
171 |
+
alert('Корзина пуста.');
|
172 |
+
return;
|
173 |
}
|
174 |
+
|
175 |
+
const products = JSON.parse(localStorage.getItem('products')) || [];
|
176 |
+
|
177 |
+
cart.forEach(cartItem => {
|
178 |
+
const product = products.find(p => p.id === cartItem.id);
|
179 |
+
|
180 |
+
if (product && product.quantity >=
|