|
| 1 | +document.addEventListener('DOMContentLoaded', () => { |
| 2 | + // DOM Elements |
| 3 | + const monthSelect = document.getElementById('month'); |
| 4 | + const yearInput = document.getElementById('year'); |
| 5 | + const calendarGrid = document.getElementById('calendar-grid'); |
| 6 | + const summaryTonnesEl = document.getElementById('summary-tonnes'); |
| 7 | + const summaryFinalEl = document.getElementById('summary-final'); |
| 8 | + |
| 9 | + let salesData = JSON.parse(localStorage.getItem('salesData')) || {}; |
| 10 | + |
| 11 | + // --- Core Functions --- |
| 12 | + |
| 13 | + function renderCalendar() { |
| 14 | + calendarGrid.innerHTML = ''; // Clear previous grid |
| 15 | + const year = parseInt(yearInput.value); |
| 16 | + const month = parseInt(monthSelect.value); |
| 17 | + const daysInMonth = new Date(year, month + 1, 0).getDate(); |
| 18 | + |
| 19 | + const monthKey = `${year}-${String(month + 1).padStart(2, '0')}`; |
| 20 | + if (!salesData[monthKey]) { |
| 21 | + salesData[monthKey] = {}; |
| 22 | + } |
| 23 | + |
| 24 | + for (let day = 1; day <= daysInMonth; day++) { |
| 25 | + const dayCard = document.createElement('div'); |
| 26 | + dayCard.className = 'day-card'; |
| 27 | + dayCard.setAttribute('data-day', day); |
| 28 | + |
| 29 | + const dayData = salesData[monthKey][day] || { kg20: 0, kg25: 0, kg30: 0 }; |
| 30 | + |
| 31 | + dayCard.innerHTML = ` |
| 32 | + <h4>ថ្ងៃទី ${day}</h4> |
| 33 | + <div class="input-group"> |
| 34 | + <label>ប្រភេទ 20kg (បេ):</label> |
| 35 | + <input type="number" min="0" value="${dayData.kg20}" data-type="kg20"> |
| 36 | + </div> |
| 37 | + <div class="input-group"> |
| 38 | + <label>ប្រភេទ 25kg (បេ):</label> |
| 39 | + <input type="number" min="0" value="${dayData.kg25}" data-type="kg25"> |
| 40 | + </div> |
| 41 | + <div class="input-group"> |
| 42 | + <label>ប្រភេទ 30kg (បេ):</label> |
| 43 | + <input type="number" min="0" value="${dayData.kg30}" data-type="kg30"> |
| 44 | + </div> |
| 45 | + <div class="day-results"> |
| 46 | + <p>ទម្ងន់: <span class="tonnes">0.000</span> តោន</p> |
| 47 | + <p>លទ្ធផល: <span class="final-value">0.00</span></p> |
| 48 | + </div> |
| 49 | + `; |
| 50 | + calendarGrid.appendChild(dayCard); |
| 51 | + calculateDay(day); // Calculate for existing data |
| 52 | + } |
| 53 | + updateMonthlySummary(); |
| 54 | + } |
| 55 | + |
| 56 | + function calculateDay(day) { |
| 57 | + const dayCard = document.querySelector(`.day-card[data-day='${day}']`); |
| 58 | + if (!dayCard) return; |
| 59 | + |
| 60 | + const sacks20 = parseInt(dayCard.querySelector('[data-type="kg20"]').value) || 0; |
| 61 | + const sacks25 = parseInt(dayCard.querySelector('[data-type="kg25"]').value) || 0; |
| 62 | + const sacks30 = parseInt(dayCard.querySelector('[data-type="kg30"]').value) || 0; |
| 63 | + |
| 64 | + const totalWeightKg = (sacks20 * 20) + (sacks25 * 25) + (sacks30 * 30); |
| 65 | + const totalTonnes = totalWeightKg / 1000; |
| 66 | + const finalValue = totalTonnes * 0.07; |
| 67 | + |
| 68 | + dayCard.querySelector('.tonnes').textContent = totalTonnes.toFixed(3); |
| 69 | + dayCard.querySelector('.final-value').textContent = finalValue.toFixed(2); |
| 70 | + } |
| 71 | + |
| 72 | + function updateMonthlySummary() { |
| 73 | + const monthKey = `${yearInput.value}-${String(parseInt(monthSelect.value) + 1).padStart(2, '0')}`; |
| 74 | + const monthData = salesData[monthKey] || {}; |
| 75 | + |
| 76 | + let totalMonthTonnes = 0; |
| 77 | + let totalMonthFinalValue = 0; |
| 78 | + |
| 79 | + for (const day in monthData) { |
| 80 | + const dayData = monthData[day]; |
| 81 | + const totalWeightKg = (dayData.kg20 * 20) + (dayData.kg25 * 25) + (dayData.kg30 * 30); |
| 82 | + const totalTonnes = totalWeightKg / 1000; |
| 83 | + const finalValue = totalTonnes * 0.07; |
| 84 | + |
| 85 | + totalMonthTonnes += totalTonnes; |
| 86 | + totalMonthFinalValue += finalValue; |
| 87 | + } |
| 88 | + |
| 89 | + summaryTonnesEl.textContent = totalMonthTonnes.toFixed(3); |
| 90 | + summaryFinalEl.textContent = totalMonthFinalValue.toFixed(2); |
| 91 | + } |
| 92 | + |
| 93 | + function handleInput(e) { |
| 94 | + if (e.target.tagName !== 'INPUT') return; |
| 95 | + |
| 96 | + const dayCard = e.target.closest('.day-card'); |
| 97 | + const day = parseInt(dayCard.getAttribute('data-day')); |
| 98 | + const type = e.target.getAttribute('data-type'); |
| 99 | + const value = parseInt(e.target.value) || 0; |
| 100 | + |
| 101 | + // Save data |
| 102 | + const monthKey = `${yearInput.value}-${String(parseInt(monthSelect.value) + 1).padStart(2, '0')}`; |
| 103 | + if (!salesData[monthKey][day]) { |
| 104 | + salesData[monthKey][day] = { kg20: 0, kg25: 0, kg30: 0 }; |
| 105 | + } |
| 106 | + salesData[monthKey][day][type] = value; |
| 107 | + localStorage.setItem('salesData', JSON.stringify(salesData)); |
| 108 | + |
| 109 | + // Recalculate and update UI |
| 110 | + calculateDay(day); |
| 111 | + updateMonthlySummary(); |
| 112 | + } |
| 113 | + |
| 114 | + // --- Initialization --- |
| 115 | + function init() { |
| 116 | + // Populate month dropdown |
| 117 | + const months = ["មករា", "កុម្ភៈ", "មីនា", "មេសា", "ឧសភា", "មិថុនា", "កក្កដា", "សីហា", "កញ្ញា", "តុលា", "វិច្ឆិកា", "ធ្នូ"]; |
| 118 | + months.forEach((month, index) => { |
| 119 | + const option = document.createElement('option'); |
| 120 | + option.value = index; |
| 121 | + option.textContent = month; |
| 122 | + monthSelect.appendChild(option); |
| 123 | + }); |
| 124 | + |
| 125 | + // Set current month and year |
| 126 | + const now = new Date(); |
| 127 | + monthSelect.value = now.getMonth(); |
| 128 | + yearInput.value = now.getFullYear(); |
| 129 | + |
| 130 | + renderCalendar(); |
| 131 | + |
| 132 | + // Event Listeners |
| 133 | + monthSelect.addEventListener('change', renderCalendar); |
| 134 | + yearInput.addEventListener('change', renderCalendar); |
| 135 | + calendarGrid.addEventListener('input', handleInput); |
| 136 | + } |
| 137 | + |
| 138 | + init(); |
| 139 | +}); |
0 commit comments