kingkay000 commited on
Commit
44bc0f1
·
verified ·
1 Parent(s): 2f6ce82

Upload process.php

Browse files
Files changed (1) hide show
  1. easypay/api/payments/process.php +91 -4
easypay/api/payments/process.php CHANGED
@@ -204,11 +204,98 @@ try {
204
  $result = $processor->processPayment($paymentParams);
205
 
206
  if ($result['success']) {
207
- // Generate receipt
208
- $result['data']['student_name'] = $student['full_name'];
209
- $result['data']['level_name'] = $student['level_name'] ?? ''; // Pass level name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  $generator = new ReceiptGenerator();
211
- $receiptBase64 = $generator->generateBase64($result['data']);
212
 
213
  $response = [
214
  'status' => 'success',
 
204
  $result = $processor->processPayment($paymentParams);
205
 
206
  if ($result['success']) {
207
+ // Fetch ALL fees for comprehensive receipt (matching web interface behavior)
208
+ // This ensures the receipt shows complete fee picture, not just current transaction
209
+ $studentId = $result['data']['student_id'];
210
+ $paymentDate = $result['data']['payment_date'];
211
+ $receiptNo = $result['data']['receipt_no'];
212
+
213
+ // Fetch all fees from receivables
214
+ $sqlFees = "SELECT ar.fee_id, ar.actual_value as amount_billed,
215
+ ar.academic_session, ar.term_of_session,
216
+ sf.description as fee_description
217
+ FROM tb_account_receivables ar
218
+ JOIN tb_account_school_fees sf ON ar.fee_id = sf.id
219
+ WHERE ar.student_id = :sid
220
+ ORDER BY ar.academic_session ASC, ar.term_of_session ASC";
221
+
222
+ $stmtFees = $pdo->prepare($sqlFees);
223
+ $stmtFees->execute(['sid' => $studentId]);
224
+ $allFees = $stmtFees->fetchAll(PDO::FETCH_ASSOC);
225
+
226
+ $allocations = [];
227
+ $receiptTotalPaid = 0;
228
+
229
+ foreach ($allFees as $fee) {
230
+ // Calculate Paid To Date (up to this receipt's date)
231
+ $sqlPaid = "SELECT SUM(amount_paid) as total_paid
232
+ FROM tb_account_payment_registers
233
+ WHERE student_id = :sid
234
+ AND fee_id = :fid
235
+ AND academic_session = :as
236
+ AND term_of_session = :ts
237
+ AND payment_date <= :pd";
238
+
239
+ $stmtPaid = $pdo->prepare($sqlPaid);
240
+ $stmtPaid->execute([
241
+ 'sid' => $studentId,
242
+ 'fid' => $fee['fee_id'],
243
+ 'as' => $fee['academic_session'],
244
+ 'ts' => $fee['term_of_session'],
245
+ 'pd' => $paymentDate
246
+ ]);
247
+ $paidResult = $stmtPaid->fetch(PDO::FETCH_ASSOC);
248
+ $paidToDate = floatval($paidResult['total_paid'] ?? 0);
249
+
250
+ // Calculate Amount paid IN THIS RECEIPT (for total calculation)
251
+ $sqlReceiptPay = "SELECT SUM(amount_paid) as receipt_paid
252
+ FROM tb_account_payment_registers
253
+ WHERE receipt_no = :rno
254
+ AND fee_id = :fid
255
+ AND academic_session = :as
256
+ AND term_of_session = :ts";
257
+ $stmtReceiptPay = $pdo->prepare($sqlReceiptPay);
258
+ $stmtReceiptPay->execute([
259
+ 'rno' => $receiptNo,
260
+ 'fid' => $fee['fee_id'],
261
+ 'as' => $fee['academic_session'],
262
+ 'ts' => $fee['term_of_session']
263
+ ]);
264
+ $receiptPayResult = $stmtReceiptPay->fetch(PDO::FETCH_ASSOC);
265
+ $paidInReceipt = floatval($receiptPayResult['receipt_paid'] ?? 0);
266
+
267
+ $receiptTotalPaid += $paidInReceipt;
268
+ $balance = floatval($fee['amount_billed']) - $paidToDate;
269
+
270
+ // Show if (Balance > 0) OR (PaidInReceipt > 0)
271
+ // This filters out old fully paid fees but keeps current payments
272
+ if ($balance > 0.001 || $paidInReceipt > 0.001) {
273
+ $allocations[] = [
274
+ 'description' => $fee['fee_description'],
275
+ 'academic_session' => $fee['academic_session'],
276
+ 'term_of_session' => $fee['term_of_session'],
277
+ 'amount_billed' => floatval($fee['amount_billed']),
278
+ 'amount' => $paidInReceipt,
279
+ 'total_paid_to_date' => $paidToDate,
280
+ 'balance' => $balance
281
+ ];
282
+ }
283
+ }
284
+
285
+ // Prepare comprehensive receipt data
286
+ $receiptData = [
287
+ 'receipt_no' => $receiptNo,
288
+ 'student_name' => $student['full_name'],
289
+ 'student_code' => $student['student_code'],
290
+ 'level_name' => $student['level_name'] ?? '',
291
+ 'payment_date' => $paymentDate,
292
+ 'total_paid' => $receiptTotalPaid,
293
+ 'allocations' => $allocations
294
+ ];
295
+
296
+ // Generate receipt with complete fee information
297
  $generator = new ReceiptGenerator();
298
+ $receiptBase64 = $generator->generateBase64($receiptData);
299
 
300
  $response = [
301
  'status' => 'success',