File size: 4,216 Bytes
0c47432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41bfe27
0c47432
 
 
 
 
 
 
 
 
74e1ba8
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
// 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();
        }
    });
});