File size: 6,970 Bytes
89496fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

//////////////////////////////////////////////////////////
let monthlyChart;

async function renderMonthlyChart(transactionsParam = null) {
  const transactions = transactionsParam || await (await fetch('/api/accounting')).json();

  const contribution = Array(12).fill(0);
  const donation = Array(12).fill(0);
  const sale = Array(12).fill(0);
  const expenses = Array(12).fill(0);

  transactions.forEach(tx => {
    if (!tx.month) return;
    const parts = tx.month.split('-');
    if (parts.length !== 2) return;
    const monthIndex = parseInt(parts[1], 10) - 1;
    if (monthIndex < 0 || monthIndex > 11) return;

    if (tx.category === 'contribution') contribution[monthIndex] += tx.amount;
    else if (tx.category === 'donation') donation[monthIndex] += tx.amount;
    else if (tx.category === 'sale') sale[monthIndex] += tx.amount;
    else if (tx.category === 'expense') expenses[monthIndex] += tx.amount;
  });

  const ctx = document.getElementById('monthlyChart').getContext('2d');
  if (monthlyChart) monthlyChart.destroy();

  monthlyChart = new Chart(ctx, {
    type: 'bar',
    data: {
      labels: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
      datasets: [
        { label: 'Contribution', data: contribution, backgroundColor: 'rgba(75,192,192,0.7)' },
        { label: 'Donation', data: donation, backgroundColor: 'rgba(54,162,235,0.7)' },
        { label: 'Sale', data: sale, backgroundColor: 'rgba(255,206,86,0.7)' },
        { label: 'Expense', data: expenses, backgroundColor: 'rgba(255,99,132,0.7)' }
      ]
    },
    options: { responsive: true, plugins: { legend: { position: 'top' }, title: { display: true, text: 'Monthly Income & Expenses by Category' } }, scales: { y: { beginAtZero: true } } }
  });
}

async function loadTransactions() {
  const res = await fetch('/api/accounting', { cache: 'no-store' });
  let transactions = await res.json();

  if (filters.start || filters.end) {
    const startDate = filters.start ? new Date(filters.start + "-01") : null;
    const endDate = filters.end ? new Date(filters.end + "-01") : null;

    transactions = transactions.filter(tx => {
      if (!tx.month) return false;
      const txDate = new Date(tx.month + "-01");
      return (!startDate || txDate >= startDate) && (!endDate || txDate <= endDate);
    });
  }

  const tbody = document.getElementById('accountingBody');
  tbody.innerHTML = transactions.map((t, i) => `

    <tr class="${t.category === 'expense' ? 'expense' : 'income'}">

      <td>${i + 1}</td>

      <td>${t.payerType === 'member' ? t.memberId : t.payerName}</td>

      <td>${t.payerType}</td>

      <td>${t.month}</td>

      <td>${t.amount.toFixed(2)}</td>

      <td>${t.category}</td>

      <td>${t.description || ''}</td>

    </tr>

  `).join('');

  updateAccountingSummary(transactions);
  renderMonthlyChart(transactions);
  renderIncomeExpenseChart(transactions);
}



// Call this on page load and after adding a transaction
renderMonthlyChart();
//////////////////////////////////////////
let filters = { start: null, end: null };

async function loadTransactions() {
  const res = await fetch('/api/accounting');
  let transactions = await res.json();

  // Apply filter if set
  if (filters.start || filters.end) {
    transactions = transactions.filter(tx => {
      if (!tx.month) return false;
      const txDate = new Date(tx.month + "-01");
      const startDate = filters.start ? new Date(filters.start + "-01") : null;
      const endDate = filters.end ? new Date(filters.end + "-01") : null;

      return (!startDate || txDate >= startDate) &&
             (!endDate || txDate <= endDate);
    });
  }

  const tbody = document.getElementById('accountingBody');
  tbody.innerHTML = transactions
    .map((t, i) => `

      <tr class="${t.category === 'expense' ? 'expense' : 'income'}">

        <td>${i + 1}</td>

        <td>${t.payerType === 'member' ? t.memberId : t.payerName}</td>

        <td>${t.payerType}</td>

        <td>${t.month}</td>

        <td>${t.amount}</td>

        <td>${t.category}</td>

        <td>${t.description || ''}</td>

      </tr>

    `).join('');
  
  // Refresh charts/summary with filtered data
  updateAccountingSummary(transactions);
  renderMonthlyChart(transactions);
  renderIncomeExpenseChart(transactions);
}

let incomeExpenseChart;

async function renderIncomeExpenseChart(transactionsParam = null) {
  const transactions = transactionsParam || await (await fetch('/api/accounting')).json();

  let totalIncome = 0, totalExpenses = 0;

  transactions.forEach(tx => {
    if (tx.category === 'expense') totalExpenses += tx.amount;
    else totalIncome += tx.amount;
  });

  const ctx = document.getElementById('incomeExpenseChart').getContext('2d');
  if (incomeExpenseChart) incomeExpenseChart.destroy();

  incomeExpenseChart = new Chart(ctx, {
    type: 'pie',
    data: {
      labels: ['Income', 'Expenses'],
      datasets: [{
        data: [totalIncome, totalExpenses],
        backgroundColor: ['rgba(75,192,192,0.7)', 'rgba(255,99,132,0.7)']
      }]
    },
    options: { responsive: true, plugins: { legend: { position: 'top' }, title: { display: true, text: 'Income vs Expenses' } } }
  });
}


document.getElementById('applyFilters').addEventListener('click', () => {
  filters.start = document.getElementById('filterStart').value || null;
  filters.end = document.getElementById('filterEnd').value || null;
  loadTransactions();
});

document.getElementById('clearFilters').addEventListener('click', () => {
  filters.start = null;
  filters.end = null;
  document.getElementById('filterStart').value = '';
  document.getElementById('filterEnd').value = '';
  loadTransactions();
});


document.getElementById("exportPdf").addEventListener("click", async () => {
  const { jsPDF } = window.jspdf;
  const pdf = new jsPDF("p", "pt", "a4");

  // Title
  pdf.setFontSize(18);
  pdf.text("Church Accounting Report", 40, 40);

  // Capture summary table
  const summaryElement = document.getElementById("summaryTable"); 
  const summaryCanvas = await html2canvas(summaryElement);
  const summaryImg = summaryCanvas.toDataURL("image/png");
  pdf.addImage(summaryImg, "PNG", 40, 60, 500, 0);

  pdf.addPage();

  // Capture monthly chart
  const chartCanvas = document.getElementById("monthlyChart");
  const chartImg = chartCanvas.toDataURL("image/png");
  pdf.text("Monthly Breakdown", 40, 40);
  pdf.addImage(chartImg, "PNG", 40, 60, 500, 300);

  pdf.addPage();

  // Capture income vs expense pie
  const pieCanvas = document.getElementById("incomeExpenseChart");
  const pieImg = pieCanvas.toDataURL("image/png");
  pdf.text("Income vs Expense", 40, 40);
  pdf.addImage(pieImg, "PNG", 40, 60, 400, 300);

  // Save file
  pdf.save("accounting-report.pdf");
});