// Global functions for deal management function openDealModal() { const modal = document.querySelector('custom-deal-modal').shadowRoot.getElementById('dealModal'); modal.classList.remove('hidden'); document.body.classList.add('overflow-hidden'); feather.replace(); } function closeDealModal() { const modal = document.querySelector('custom-deal-modal').shadowRoot.getElementById('dealModal'); modal.classList.add('hidden'); document.body.classList.remove('overflow-hidden'); } function createNewDeal() { // Get all form data const client = document.querySelector('custom-deal-modal').shadowRoot.getElementById('client').value; const dealDate = document.querySelector('custom-deal-modal').shadowRoot.getElementById('dealDate').value; const supplier = document.querySelector('custom-deal-modal').shadowRoot.getElementById('supplier').value; const status = document.querySelector('custom-deal-modal').shadowRoot.getElementById('status').value; const notes = document.querySelector('custom-deal-modal').shadowRoot.getElementById('notes').value; // Get product table data const productTable = document.querySelector('custom-product-table').shadowRoot.querySelector('#productTable'); const rows = productTable.querySelectorAll('tbody tr'); const items = []; rows.forEach((row, index) => { items.push({ item_number: index + 1, product_name: row.querySelector('input[type="text"]').value, quantity: parseInt(row.querySelector('.qty').value), purchase_price: parseFloat(row.querySelector('.purchase-price').value), margin: parseFloat(row.querySelector('.margin').value), sale_price: parseFloat(row.querySelector('.sale-price').value), supplier_id: row.querySelector('select').value, delivered: row.querySelector('input[type="checkbox"]').checked }); }); // Create deal object const deal = { client_id: client, deal_date: dealDate, supplier_id: supplier, status: status, notes: notes, items: items }; // Send to API fetch('/api/deals.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(deal) }) .then(response => response.json()) .then(data => { if(data.success) { alert(`Сделка ${data.deal_number} успешно создана!`); closeDealModal(); window.location.reload(); } else { alert('Ошибка при создании сделки: ' + data.error); } }) .catch(error => { console.error('Error:', error); alert('Произошла ошибка при создании сделки'); }); } function applyMarginToAll() { const margin = prompt('Введите наценку в процентах:', '30'); if (margin && !isNaN(margin)) { const productTable = document.querySelector('custom-product-table').shadowRoot.querySelector('#productTable'); const rows = productTable.querySelectorAll('tbody tr'); rows.forEach(row => { const purchasePrice = parseFloat(row.querySelector('.purchase-price').value); const salePriceCell = row.querySelector('.sale-price'); const marginCell = row.querySelector('.margin'); if (purchasePrice && !isNaN(purchasePrice)) { const newSalePrice = purchasePrice * (1 + (parseFloat(margin)/100)); salePriceCell.value = newSalePrice.toFixed(2); marginCell.value = margin; } }); } } // Initialize app when DOM is loaded document.addEventListener('DOMContentLoaded', function() { feather.replace(); // Close modal when clicking outside document.addEventListener('click', function(event) { const modal = document.querySelector('custom-deal-modal'); if (modal && !modal.contains(event.target) && event.target.closest('custom-deal-modal') === null) { closeDealModal(); } }); });