diff --git a/myproject/pos/static/pos/js/terminal.js b/myproject/pos/static/pos/js/terminal.js index 8c67a24..322e2c4 100644 --- a/myproject/pos/static/pos/js/terminal.js +++ b/myproject/pos/static/pos/js/terminal.js @@ -826,6 +826,69 @@ document.getElementById('customerSelectBtn').addEventListener('click', () => { alert('Функция выбора клиента будет реализована позже'); }); +// Смена склада +const changeWarehouseBtn = document.getElementById('changeWarehouseBtn'); +if (changeWarehouseBtn) { + changeWarehouseBtn.addEventListener('click', () => { + const modal = new bootstrap.Modal(document.getElementById('selectWarehouseModal')); + modal.show(); + }); +} + +// Обработка выбора склада из списка +document.addEventListener('click', async (e) => { + const warehouseItem = e.target.closest('.warehouse-item'); + if (!warehouseItem) return; + + const warehouseId = warehouseItem.dataset.warehouseId; + const warehouseName = warehouseItem.dataset.warehouseName; + + // Проверяем, есть ли товары в корзине + if (cart.size > 0) { + const confirmed = confirm(`При смене склада корзина будет очищена.\n\nПереключиться на склад "${warehouseName}"?`); + if (!confirmed) return; + } + + try { + // Отправляем запрос на смену склада + const response = await fetch(`/pos/api/set-warehouse/${warehouseId}/`, { + method: 'POST', + headers: { + 'X-CSRFToken': getCsrfToken() + } + }); + + const data = await response.json(); + + if (data.success) { + // Перезагружаем страницу для обновления данных + location.reload(); + } else { + alert(`Ошибка: ${data.error}`); + } + } catch (error) { + console.error('Ошибка при смене склада:', error); + alert('Произошла ошибка при смене склада'); + } +}); + +// Вспомогательная функция для получения CSRF токена +function getCsrfToken() { + const name = 'csrftoken'; + let cookieValue = null; + if (document.cookie && document.cookie !== '') { + const cookies = document.cookie.split(';'); + for (let i = 0; i < cookies.length; i++) { + const cookie = cookies[i].trim(); + if (cookie.substring(0, name.length + 1) === (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; +} + // Инициализация renderCategories(); renderProducts(); diff --git a/myproject/pos/templates/pos/terminal.html b/myproject/pos/templates/pos/terminal.html index 1b1efe6..11d5798 100644 --- a/myproject/pos/templates/pos/terminal.html +++ b/myproject/pos/templates/pos/terminal.html @@ -32,6 +32,21 @@