76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '../../../db/config.php';
|
|
|
|
//
|
|
// ## Trial Balance Report
|
|
//
|
|
// Purpose: Prove that the double-entry system balances.
|
|
//
|
|
// ---
|
|
//
|
|
// **Rule (Absolute):** This report reads ONLY from the General Ledger (`journal_entries`, `journal_lines`, `accounts`).
|
|
// It NEVER reads from operational tables like `invoices` or `payments`.
|
|
//
|
|
|
|
$pdo = db();
|
|
|
|
try {
|
|
// The query calculates the total debits and credits for each account from posted journal entries.
|
|
// It also computes the final balance based on the account's normal balance.
|
|
$sql = <<<SQL
|
|
SELECT
|
|
a.account_code,
|
|
a.account_name,
|
|
a.account_type,
|
|
a.normal_balance,
|
|
COALESCE(SUM(jl.debit_amount), 0) as total_debits,
|
|
COALESCE(SUM(jl.credit_amount), 0) as total_credits,
|
|
CASE
|
|
WHEN a.normal_balance = 'debit' THEN COALESCE(SUM(jl.debit_amount), 0) - COALESCE(SUM(jl.credit_amount), 0)
|
|
ELSE COALESCE(SUM(jl.credit_amount), 0) - COALESCE(SUM(jl.debit_amount), 0)
|
|
END as balance
|
|
FROM
|
|
accounts a
|
|
LEFT JOIN
|
|
journal_lines jl ON a.account_code = jl.account_code
|
|
LEFT JOIN
|
|
journal_entries je ON jl.journal_entry_id = je.id AND je.status = 'posted'
|
|
GROUP BY
|
|
a.account_code, a.account_name, a.account_type, a.normal_balance
|
|
ORDER BY
|
|
a.account_code;
|
|
SQL;
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute();
|
|
$report_lines = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Calculate totals for debits, credits to verify balance
|
|
$total_debits = 0;
|
|
$total_credits = 0;
|
|
foreach ($report_lines as $line) {
|
|
$total_debits += $line['total_debits'];
|
|
$total_credits += $line['total_credits'];
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'report_name' => 'Trial Balance',
|
|
'generated_at' => date('c'),
|
|
'data' => $report_lines,
|
|
'summary' => [
|
|
'total_debits' => $total_debits,
|
|
'total_credits' => $total_credits,
|
|
'is_balanced' => abs($total_debits - $total_credits) < 0.0001 // Use a small tolerance for floating point comparison
|
|
]
|
|
]);
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Database error: ' . $e->getMessage()
|
|
]);
|
|
}
|