File size: 2,291 Bytes
e31284f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
/**

 * API Endpoint: Fetch Term Invoice (Fee Breakdown)

 * 

 * Returns the breakdown of fees billed to a student for a specific term/session.

 * Includes billed amount, paid amount, and outstanding balance per fee.

 * 

 * Method: GET

 * URL: /api/students/invoice?student_id=...&academic_session=...&term=...

 */

require_once '../../db_config.php';
require_once '../../includes/PaymentProcessor.php';
require_once '../../includes/ApiValidator.php';
require_once '../../config/api_config.php';

// Initialize core classes
$validator = new ApiValidator($pdo, defined('API_KEYS') ? API_KEYS : []);
$processor = new PaymentProcessor($pdo);

// 1. Validate Request
$validation = $validator->validateRequest(['GET']);
if (!$validation['valid']) {
    http_response_code($validation['http_code']);
    echo json_encode(['status' => 'error', 'message' => $validation['error']]);
    exit;
}

// 2. Validate Input Parameters
$studentId = trim($_GET['student_id'] ?? '');
$session = trim($_GET['academic_session'] ?? '');
$term = trim($_GET['term'] ?? ''); // term_of_session

$errors = [];
if (empty($studentId))
    $errors[] = "Student ID is required";
if (empty($session))
    $errors[] = "Academic Session is required";
if (empty($term))
    $errors[] = "Term is required";

if (!empty($errors)) {
    http_response_code(400);
    echo json_encode(['status' => 'error', 'message' => implode(', ', $errors)]);
    exit;
}

// 3. Validate Student Exists
$studentCheck = $validator->validateStudentExists($studentId);
if (!$studentCheck['valid']) {
    http_response_code($studentCheck['http_code']);
    echo json_encode(['status' => 'error', 'message' => $studentCheck['error']]);
    exit;
}

try {
    // 4. Fetch Data
    $invoice = $processor->getTermInvoice($studentId, $session, $term);

    // 5. Send Response
    echo json_encode([
        'status' => 'success',
        'data' => [
            'student_id' => $studentId,
            'academic_session' => $session,
            'term' => $term,
            'items' => $invoice
        ]
    ]);

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => 'Internal server error: ' . $e->getMessage()]);
}