Aleksmorshen commited on
Commit
e1c0916
·
verified ·
1 Parent(s): 0606380

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +66 -50
script.js CHANGED
@@ -5,10 +5,12 @@ document.addEventListener('DOMContentLoaded', function () {
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();
@@ -45,6 +47,25 @@ document.addEventListener('DOMContentLoaded', function () {
45
  }
46
  });
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  // Функция добавления товара
49
  function addProduct(name, purchasePrice, salePrice, quantity) {
50
  const product = {
@@ -74,12 +95,6 @@ document.addEventListener('DOMContentLoaded', function () {
74
  <td>${product.purchasePrice}</td>
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>
83
  `;
84
  }
85
 
@@ -125,26 +140,24 @@ document.addEventListener('DOMContentLoaded', function () {
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
 
@@ -156,6 +169,42 @@ document.addEventListener('DOMContentLoaded', function () {
156
  } else {
157
  alert('Пожалуйста, введите корректное количество.');
158
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  };
160
 
161
  // Функция удаления товара из корзины
@@ -196,37 +245,4 @@ document.addEventListener('DOMContentLoaded', function () {
196
  loadProducts();
197
  updateStatsDisplay();
198
  };
199
-
200
- // Функция добавления остатков
201
- window.addStock = function (productId) {
202
- const quantityToAdd = prompt('Введите количество для прихода:');
203
- if (quantityToAdd && !isNaN(quantityToAdd) && quantityToAdd > 0) {
204
- let products = JSON.parse(localStorage.getItem('products')) || [];
205
- const productIndex = products.findIndex(p => p.id === productId);
206
-
207
- if (productIndex !== -1) {
208
- products[productIndex].quantity += parseInt(quantityToAdd);
209
- localStorage.setItem('products', JSON.stringify(products));
210
-
211
- // Обновляем таблицу
212
- productTable.innerHTML = '';
213
- loadProducts();
214
- }
215
- } else {
216
- alert('Пожалуйста, введите корректное количество.');
217
- }
218
- };
219
-
220
- // Функция удаления товара
221
- window.deleteProduct = function (productId) {
222
- if (confirm('Вы уверены, что хотите удалить этот товар?')) {
223
- let products = JSON.parse(localStorage.getItem('products')) || [];
224
- products = products.filter(p => p.id !== productId);
225
- localStorage.setItem('products', JSON.stringify(products));
226
-
227
- // Обновляем таблицу
228
- productTable.innerHTML = '';
229
- loadProducts();
230
- }
231
- };
232
  });
 
5
  const cartTable = document.getElementById('cartTable').getElementsByTagName('tbody')[0];
6
  const totalSoldElement = document.getElementById('totalSold');
7
  const totalRevenueElement = document.getElementById('totalRevenue');
8
+ const contextMenu = document.getElementById('contextMenu');
9
 
10
  let totalSold = 0; // Общее количество проданных товаров
11
  let totalRevenue = 0; // Общая выручка
12
  let cart = []; // Корзина
13
+ let selectedProductId = null; // ID выбранного товара
14
 
15
  // Загрузка данных из localStorage при загрузке страницы
16
  loadProducts();
 
47
  }
48
  });
49
 
50
+ // Открытие контекстного меню при нажатии на товар
51
+ productTable.addEventListener('click', function (e) {
52
+ const row = e.target.closest('tr');
53
+ if (row) {
54
+ selectedProductId = parseInt(row.getAttribute('data-id'));
55
+ const rect = row.getBoundingClientRect();
56
+ contextMenu.style.display = 'block';
57
+ contextMenu.style.top = `${rect.bottom}px`;
58
+ contextMenu.style.left = `${rect.left}px`;
59
+ }
60
+ });
61
+
62
+ // Закрытие контекстного меню при клике вне его
63
+ document.addEventListener('click', function (e) {
64
+ if (!contextMenu.contains(e.target) && !e.target.closest('tr')) {
65
+ contextMenu.style.display = 'none';
66
+ }
67
+ });
68
+
69
  // Функция добавления товара
70
  function addProduct(name, purchasePrice, salePrice, quantity) {
71
  const product = {
 
95
  <td>${product.purchasePrice}</td>
96
  <td>${product.salePrice}</td>
97
  <td>${product.quantity}</td>
 
 
 
 
 
 
98
  `;
99
  }
100
 
 
140
  });
141
  }
142
 
143
+ // Обработчик добавления в корзину
144
+ window.handleAddToCart = function () {
145
+ const quantity = prompt('Введите количество для добавления в корзину:');
146
+ if (quantity && !isNaN(quantity) && quantity > 0) {
 
 
147
  const products = JSON.parse(localStorage.getItem('products')) || [];
148
+ const product = products.find(p => p.id === selectedProductId);
149
 
150
  if (product && product.quantity >= quantity) {
151
+ const cartItem = cart.find(item => item.id === selectedProductId);
152
 
153
  if (cartItem) {
154
+ cartItem.quantity += parseInt(quantity);
155
  } else {
156
  cart.push({
157
+ id: selectedProductId,
158
  name: product.name,
159
  salePrice: product.salePrice,
160
+ quantity: parseInt(quantity)
161
  });
162
  }
163
 
 
169
  } else {
170
  alert('Пожалуйста, введите корректное количество.');
171
  }
172
+ contextMenu.style.display = 'none';
173
+ };
174
+
175
+ // Обработчик прихода товара
176
+ window.handleAddStock = function () {
177
+ const quantityToAdd = prompt('Введите количество для прихода:');
178
+ if (quantityToAdd && !isNaN(quantityToAdd) && quantityToAdd > 0) {
179
+ let products = JSON.parse(localStorage.getItem('products')) || [];
180
+ const productIndex = products.findIndex(p => p.id === selectedProductId);
181
+
182
+ if (productIndex !== -1) {
183
+ products[productIndex].quantity += parseInt(quantityToAdd);
184
+ localStorage.setItem('products', JSON.stringify(products));
185
+
186
+ // Обновляем таблицу
187
+ productTable.innerHTML = '';
188
+ loadProducts();
189
+ }
190
+ } else {
191
+ alert('Пожалуйста, введите корректное количество.');
192
+ }
193
+ contextMenu.style.display = 'none';
194
+ };
195
+
196
+ // Обработчик удаления товара
197
+ window.handleDeleteProduct = function () {
198
+ if (confirm('Вы уверены, что хотите удалить этот товар?')) {
199
+ let products = JSON.parse(localStorage.getItem('products')) || [];
200
+ products = products.filter(p => p.id !== selectedProductId);
201
+ localStorage.setItem('products', JSON.stringify(products));
202
+
203
+ // Обновляем таблицу
204
+ productTable.innerHTML = '';
205
+ loadProducts();
206
+ }
207
+ contextMenu.style.display = 'none';
208
  };
209
 
210
  // Функция удаления товара из корзины
 
245
  loadProducts();
246
  updateStatsDisplay();
247
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  });