<?php
include "../../classes/autoload.php";
?>

<h2>Payment History</h2>
<table class="width_100">
    <tr>
        <th>Date</th>
        <th>Amount</th>
    </tr>
    <?php
    $credit_total = 0;
    $payment_history_res = $db->query("payments", "SELECT * FROM payments WHERE invoice_id = {$_GET['invoice_id']}");
    $payments_total = 0;
    if ($payment_history_res->num_rows > 0) {


        $credit_details_res = $db->query("credit_notes", "SELECT * FROM credit_notes WHERE invoice_id = {$_GET['invoice_id']}");
        if ($credit_details_res->num_rows > 0) {
            while ($credit_details = $credit_details_res->fetch_assoc()) {
                $list_res = $db->query("credit_notes_list", "SELECT SUM(amount) as totals FROM credit_notes_list WHERE credit_note_id = {$credit_details['record_id']}");
                $list = $list_res->fetch_assoc();
                $credit_total += (float) $list['totals'];
            }
        }

        echo "<tr>";
        echo "<td><b>CREDIT TOTAL<b></td>";
        echo "<td><b>R " . number_format($credit_total, 2) . "</b></td>";
        echo "</tr>";

        $invoice_details_res = $db->query("invoice_list", "SELECT * FROM invoice_list WHERE invoice_id = {$_GET['invoice_id']}");
        $invoice_total = 0;
        if ($invoice_details_res->num_rows > 0) {
            while ($invoice_list = $invoice_details_res->fetch_assoc()) {
                $invoice_total += (float) $invoice_list['price'] * (float) $invoice_list['qty'];
            }
        }

        echo "<tr>";
        echo "<td><b>INVOICE TOTAL<b></td>";
        echo "<td><b>R " . number_format(($invoice_total - $credit_total) * 1.15, 2) . "</b></td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td colspan='2'><u>PAYMENTS:</u></td>";
        echo "</tr>";
        
        while ($payment_his = $payment_history_res->fetch_assoc()) {
            echo "<tr>";
            echo "<td>{$payment_his['date_time']}</td>";
            echo "<td>R " . number_format($payment_his['amount'], 2) . "</td>";
            echo "</tr>";
            $payments_total += $payment_his['amount'];
        }

        echo "<tr>";
        echo "<td><b>PAYMENTS TOTAL</b></td>";
        echo "<td><b>R " . number_format($payments_total, 2) . "</b></td>";
        echo "</tr>";


        echo "<tr>";
        echo "<td><b>NETT</b></td>";
        echo "<td><b id='nett'>R " . number_format((($invoice_total - $credit_total) * 1.15) - $payments_total, 2) . "</b></td>";
        echo "</tr>";

    } else {
        ?>

        <?php
        $credit_total = 0;
        $credit_details_res = $db->query("credit_notes", "SELECT * FROM credit_notes WHERE invoice_id = {$_GET['invoice_id']}");
        if ($credit_details_res->num_rows > 0) {
            while ($credit_details = $credit_details_res->fetch_assoc()) {
                $list_res = $db->query("credit_notes_list", "SELECT SUM(amount) as totals FROM credit_notes_list WHERE credit_note_id = {$credit_details['record_id']}");
                $list = $list_res->fetch_assoc();
                $credit_total += (float) $list['totals'];
            }
        }

        echo "<tr>";
        echo "<td><b>CREDIT TOTAL<b></td>";
        echo "<td><b>R " . number_format($credit_total, 2) . "</b></td>";
        echo "</tr>";


        $invoice_details_res = $db->query("invoice_list", "SELECT * FROM invoice_list WHERE invoice_id = {$_GET['invoice_id']}");
        $invoice_total = 0;
        if ($invoice_details_res->num_rows > 0) {
            while ($invoice_list = $invoice_details_res->fetch_assoc()) {
                $invoice_total += (float) $invoice_list['price'] * (float) $invoice_list['qty'];
            }
        }

        echo "<tr>";
        echo "<td><b>INVOICE TOTAL<b></td>";
        echo "<td><b>R " . number_format(($invoice_total - $credit_total) * 1.15, 2) . "</b></td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td><b>INVOICE TOTAL<b></td>";
        echo "<td><b>R " . number_format(($invoice_total - $credit_total), 2) . "</b></td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td colspan='2'><u>PAYMENTS:</u></td>";
        echo "</tr>";
        ?>
        <tr>
            <td colspan="2">NO PAYMENTS YET</td>
        </tr>
        <?php
        echo "<tr>";
        echo "<td><b>NETT</b></td>";
        echo "<td><b id='nett'>R " . number_format(($invoice_total - $credit_total) * 1.15, 2) . "</b></td>";
        echo "</tr>";
    }

    ?>
</table>