0"; $tabs = [ 'open' => ['label' => 'To pay', 'icon' => 'fa-hourglass-half', 'where' => $open_sql], 'paid' => ['label' => 'Paid', 'icon' => 'fa-check', 'where' => "NOT ($open_sql) AND invoice_status NOT IN ('Cancelled', 'Non-Billable')"], 'all' => ['label' => 'All', 'icon' => 'fa-list', 'where' => '1 = 1'], ]; $q = trim((string) ($_GET['q'] ?? '')); $where = "invoice_client_id = $session_client_id AND invoice_status != 'Draft'"; if ($q !== '') { $q_sql = escapeSql($q); $where .= " AND (CONCAT(invoice_prefix, invoice_number) LIKE '%$q_sql%' OR invoice_scope LIKE '%$q_sql%')"; } $count_sql = []; foreach ($tabs as $key => $t) { $count_sql[] = "COALESCE(SUM({$t['where']}), 0) AS `$key`"; } $counts = array_map('intval', mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT " . implode(', ', $count_sql) . " FROM invoices WHERE $where"))); // The To pay tab when there is something to pay, else all of them $tab = isset($tabs[$_GET['tab'] ?? '']) ? $_GET['tab'] : ($counts['open'] ? 'open' : 'all'); $total = $counts[$tab]; $pages = max(1, (int) ceil($total / $per_page)); $page = min(max(1, intval($_GET['page'] ?? 1)), $pages); $offset = ($page - 1) * $per_page; $invoices = mysqli_query($mysqli, "SELECT invoice_id, invoice_prefix, invoice_number, invoice_scope, invoice_date, invoice_due, invoice_status, invoice_amount, invoice_currency_code, $paid_sql AS paid FROM invoices WHERE $where AND {$tabs[$tab]['where']} ORDER BY " . ($tab === 'open' ? 'invoice_due ASC, invoice_number ASC' : 'invoice_date DESC, invoice_number DESC') . " LIMIT $per_page OFFSET $offset"); // What is owed, over every open invoice whatever the search $owed = 0.0; $late = 0.0; $currency = ''; foreach (billingClientOpenInvoices($session_client_id) as $open) { $left = round(floatval($open['invoice_amount']) - floatval($open['paid']), 2); $owed += $left; $currency = $currency ?: (string) $open['invoice_currency_code']; if ($open['invoice_due'] < date('Y-m-d')) { $late += $left; } } $cards = billingStripeProvider() !== null; /** This page's address with some of its settings changed. */ function invoicesUrl(array $changes) { global $tab, $q; $params = array_merge(['tab' => $tab, 'q' => $q !== '' ? $q : null], $changes); $params = array_filter($params, function ($v) { return $v !== null && $v !== ''; }); return 'invoices.php' . ($params ? '?' . http_build_query($params) : ''); } ?>
0) { ?> You owe = escapeHtml(billingMoney($owed, $currency)) ?> 0) { ?> - = escapeHtml(billingMoney($late, $currency)) ?> of it past due Nothing is owed - thank you