= ' . floatval($_GET['amount_min']) . ')'; $amount_min_filter = floatval($_GET['amount_min']); } else { // Default - any $amount_min_query = ''; $amount_min_filter = ''; } if (isset($_GET['amount_max']) && $_GET['amount_max'] != '') { $amount_max_query = 'AND (ABS(transaction_amount) <= ' . floatval($_GET['amount_max']) . ')'; $amount_max_filter = floatval($_GET['amount_max']); } else { // Default - any $amount_max_query = ''; $amount_max_filter = ''; } // Balance only makes sense in chronological order - hide the column under any other sort if ($sort == 'transaction_date') { $balance_visible = true; } else { $balance_visible = false; } if ($account_filter) { // Account details - opening balance feeds the running balance, currency feeds the summary $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM accounts WHERE account_id = $account_filter LIMIT 1")); $account_currency_code = escapeHtml($row['account_currency_code']); $account_opening_balance = floatval($row['opening_balance']); // Transfers are stored as a linked expense (from account) + revenue (to account) pair, // so we detect them via the transfers link table and relabel the row accordingly // The running balance is calculated over the account's full ledger BEFORE any filters, // so the balance per transaction stays accurate no matter what is filtered out $ledger_query = "SELECT *, $account_opening_balance + SUM(transaction_amount) OVER (ORDER BY transaction_date ASC, transaction_created_at ASC, transaction_type ASC, transaction_id ASC) AS transaction_balance FROM ( SELECT CASE WHEN transfer_id IS NOT NULL THEN 'Transfer In' ELSE 'Revenue' END AS transaction_type, revenue_id AS transaction_id, transfer_id AS transaction_transfer_id, 0 AS transaction_invoice_id, revenue_date AS transaction_date, revenue_created_at AS transaction_created_at, CASE WHEN transfer_id IS NOT NULL THEN transfer_notes ELSE revenue_description END AS transaction_description, from_account.account_name AS transaction_other_account, revenue_reference AS transaction_reference, revenue_client_id AS transaction_client_id, CASE WHEN transfer_id IS NOT NULL THEN transfer_method ELSE revenue_payment_method END AS transaction_payment_method, revenue_category_id AS transaction_category_id, category_name AS transaction_category, revenue_amount AS transaction_amount, revenue_currency_code AS transaction_currency_code FROM revenues LEFT JOIN categories ON revenue_category_id = category_id LEFT JOIN transfers ON transfer_revenue_id = revenue_id LEFT JOIN expenses AS transfer_expense ON transfer_expense_id = transfer_expense.expense_id LEFT JOIN accounts AS from_account ON transfer_expense.expense_account_id = from_account.account_id WHERE revenue_account_id = $account_filter AND revenue_archived_at IS NULL UNION ALL SELECT CASE WHEN transfer_id IS NOT NULL THEN 'Transfer Out' ELSE 'Expense' END, expense_id, transfer_id, 0, expense_date, expense_created_at, CASE WHEN transfer_id IS NOT NULL THEN transfer_notes ELSE expense_description END, to_account.account_name, expense_reference, expense_client_id, CASE WHEN transfer_id IS NOT NULL THEN transfer_method ELSE expense_payment_method END, expense_category_id, category_name, -expense_amount, expense_currency_code FROM expenses LEFT JOIN categories ON expense_category_id = category_id LEFT JOIN transfers ON transfer_expense_id = expense_id LEFT JOIN revenues AS transfer_revenue ON transfer_revenue_id = transfer_revenue.revenue_id LEFT JOIN accounts AS to_account ON transfer_revenue.revenue_account_id = to_account.account_id WHERE expense_account_id = $account_filter AND expense_archived_at IS NULL UNION ALL SELECT 'Payment', payment_id, NULL, payment_invoice_id, payment_date, payment_created_at, CONCAT('Payment for Invoice ', invoice_prefix, invoice_number), NULL, payment_reference, invoice_client_id, payment_method, 0, 'Invoice Payment', payment_amount, payment_currency_code FROM payments LEFT JOIN invoices ON payment_invoice_id = invoice_id WHERE payment_account_id = $account_filter AND payment_archived_at IS NULL ) AS ledger"; $transaction_filter_query = "WHERE DATE(transaction_date) BETWEEN '$dtf' AND '$dtt' AND (transaction_description LIKE '%$q%' OR transaction_category LIKE '%$q%' OR transaction_reference LIKE '%$q%' OR transaction_other_account LIKE '%$q%' OR transaction_amount LIKE '%$q%') $type_query $category_query $client_query $payment_method_query $amount_min_query $amount_max_query"; $sql = mysqli_query( $mysqli, "SELECT SQL_CALC_FOUND_ROWS * FROM ( $ledger_query ) AS transactions $transaction_filter_query ORDER BY $sort $order LIMIT $record_from, $record_to" ); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); // Summary - money in / money out / net for the current filtered view, plus current account balance $sql_summary = mysqli_query( $mysqli, "SELECT SUM(CASE WHEN transaction_amount > 0 THEN transaction_amount ELSE 0 END) AS total_in, SUM(CASE WHEN transaction_amount < 0 THEN transaction_amount ELSE 0 END) AS total_out, SUM(transaction_amount) AS total_net FROM ( $ledger_query ) AS transactions $transaction_filter_query" ); $row = mysqli_fetch_assoc($sql_summary); $summary_total_in = floatval($row['total_in']); $summary_total_out = floatval($row['total_out']); $summary_total_net = floatval($row['total_net']); } else { $num_rows = [0]; } ?>

Transactions


Money In

Money Out

">

Net

"> From $transaction_other_account"; } elseif ($transaction_type == 'Transfer Out') { $transaction_category_display = "To $transaction_other_account"; } else { $transaction_category_display = $transaction_category; } // Badge color based on type if ($transaction_type == 'Revenue' || $transaction_type == 'Payment') { $transaction_badge_color = "success"; } elseif ($transaction_type == 'Expense') { $transaction_badge_color = "danger"; } else { $transaction_badge_color = "secondary"; } // Amount text color if ($transaction_amount < 0) { $transaction_amount_color = "text-danger"; } else { $transaction_amount_color = "text-success"; } // Balance text color if ($transaction_balance < 0) { $transaction_balance_color = "text-danger"; } else { $transaction_balance_color = ""; } // Route the date link to the right edit modal based on type if ($transaction_type == 'Transfer In' || $transaction_type == 'Transfer Out') { $transaction_modal_url = "modals/transfer/transfer_edit.php?id=$transaction_transfer_id"; } elseif ($transaction_type == 'Expense') { $transaction_modal_url = "modals/expense/expense_edit.php?id=$transaction_id"; } elseif ($transaction_type == 'Revenue') { $transaction_modal_url = "modals/revenue/revenue_edit.php?id=$transaction_id"; } else { $transaction_modal_url = ""; } ?>
Date Type Category / Description Amount Balance

Select an account above to view its transactions.