<?php

include_once '../functions.php';
checklogin();
$conn = opendb();
$id = sanitise('id');
$c = sanitise('c');
$account = sanitise('account');
$value = sanitise('value');
if ($c == 'true') {
    $query = "UPDATE payments SET Reconciled='1' WHERE PaymentID='{$id}' AND UserID='{$user}'";
} else {
    $query = "UPDATE payments SET Reconciled='0' WHERE PaymentID='{$id}' AND UserID='{$user}'";
}
mysql_query($query) or die(mysql_error());
reconcilereport($user, $account);
function statement($display, $user, $order = 1, $account = 0, $offset = 0, $recvalue = 0, $currfield = 'date', $startdate = NULL, $enddate = NULL)
{
    $account = checkAccount($user, $account, 0);
    if ($account != 0) {
        $accountquery = "AND payments.AccountID='" . $account . "' ";
    } else {
        $accountquery = "";
    }
    if (!is_int($offset)) {
        $offset = 0;
    }
    $currentpage = intval($offset / $display) + 1;
    pagination($user, $account, $display, $currentpage);
    if ($currfield == 'otherparty') {
        $field = 'PaymentName';
    } elseif ($currfield == 'desc') {
        $field = 'PaymentDesc';
    } elseif ($currfield == 'out') {
        $field = 'PaymentAmount';
    } elseif ($currfield == 'account') {
        $field = 'AccountName';
    } elseif ($currfield == 'reconciled') {
        $field = 'Reconciled';
    } elseif ($currfield == 'label') {
        $field = 'payments.LabelID';
    } else {
        $field = 'Timestamp';
    }
    if ($startdate != NULL) {
        $between = " AND Timestamp>'{$startdate}' ";
    }
    if ($enddate == NULL) {
        $enddate = time();
    }
    $query = "SELECT * FROM payments LEFT JOIN accounts ON payments.AccountID=accounts.AccountID LEFT JOIN labels ON labels.LabelID=payments.LabelID WHERE payments.UserID='{$user}' AND Deleted='0' AND Timestamp<'{$enddate}' " . $between . $accountquery . "ORDER BY " . $field . " DESC Limit " . $offset . "," . $display;
    $result = mysql_query($query) or die(mysql_error());
    if ($order != 0 && mysql_num_rows($result) != 0) {
        //PLEASE PLEASE find a nicer way to do this!
        while ($row = mysql_fetch_assoc($result)) {
            $paymentids = " OR PaymentID='" . $row['PaymentID'] . "'" . $paymentids;
        }
        $paymentids = "WHERE" . substr($paymentids, 3);
        $query = "SELECT * FROM payments LEFT JOIN accounts ON payments.AccountID=accounts.AccountID LEFT JOIN labels ON labels.LabelID=payments.LabelID " . $paymentids . " AND Deleted='0' ORDER BY " . $field . " ASC";
        $result = mysql_query($query) or die(mysql_error());
    }
    echo "<table id='statement'>\n\t\t\t\t\t<thead>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>Date</th>\n\t\t\t\t\t\t\t<th>To/From</th>\n\t\t\t\t\t\t\t<th>Description</th>\n\t\t\t\t\t\t\t<th>In</th>\n\t\t\t\t\t\t\t<th>Out</th>\n\t\t\t\t\t\t\t<th>Type</th>\n\t\t\t\t\t\t\t<th>Account</th>\n\t\t\t\t\t\t\t<th>Label</th>\n\t\t\t\t\t\t\t<th>Reconciled</th>\n\t\t\t\t\t\t\t<th>Operations</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<form name='sortbuttons'>\n\t\t\t\t\t\t\t<th>";
    sortbuttons('date', $order, $currfield);
    echo "</th><th>";
    sortbuttons('otherparty', $order, $currfield);
    echo "</th><th>";
    sortbuttons('desc', $order, $currfield);
    echo "</th><th></th><th>";
    sortbuttons('out', $order, $currfield);
    echo "</th><th>";
    sortbuttons('type', $order, $currfield);
    echo "</th><th>";
    sortbuttons('account', $order, $currfield);
    echo "</th><th>";
    sortbuttons('label', $order, $currfield);
    echo "</th><th>";
    sortbuttons('reconciled', $order, $currfield);
    echo "</th><th></th>\n\t\t\t\t\t\t\t</form>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</thead>\n\t\t\t\t\t<tbody>";
    while ($row = mysql_fetch_assoc($result)) {
        $amount = $row['PaymentAmount'];
        $amount = displayamount($amount, $user, 1);
        echo "<tr";
        if ($row['Timestamp'] > time()) {
            echo " class='futurepayment'";
        }
        echo " id='payment" . $row['PaymentID'] . "'>\n\t\t\t\t\t\t<td>" . date("d/m/y", $row['Timestamp']) . "</td>\n\t\t\t\t\t\t<td>" . stripslashes($row['PaymentName']) . "</td>\n\t\t\t\t\t\t<td>" . stripslashes($row['PaymentDesc']) . "</td>\n\t\t\t\t\t\t<td class='align_right'>" . $amount . "</td>\n\t\t\t\t\t\t<td>" . stripslashes($row['PaymentType']) . "</td>\n\t\t\t\t\t\t<td>" . stripslashes($row['AccountName']) . "</td>\n\t\t\t\t\t\t<td style='color:" . stripslashes($row['Colour']) . "'>" . stripslashes($row['LabelName']) . "</td>\n\t\t\t\t\t\t<td><input type='checkbox' id='reconciled' onclick=\"reconcile(this, " . $row['PaymentID'] . ")\"";
        if ($row['Reconciled'] == 1) {
            echo "checked='checked'";
        }
        echo "></td>\n\t\t\t\t\t\t<td>\n\t\t\t\t\t\t\t<button onclick=\"confirmDelete('" . $row['PaymentID'] . "')\">Delete</button>\n\t\t\t\t\t\t\t<button onclick=\"editForm('" . $row['PaymentID'] . "')\">Edit</button>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>";
    }
    $endtime = $enddate;
    if ($enddate < time()) {
        $endtime = time();
    }
    $query = "SELECT * FROM payments WHERE UserID='{$user}' AND Deleted='0' AND Timestamp<'" . $endtime . "' " . $accountquery;
    $result = mysql_query($query) or die(mysql_error());
    $total = 0;
    while ($row = mysql_fetch_assoc($result)) {
        $total = $total + $row['PaymentAmount'];
    }
    $total = displayamount($total, $user, 1);
    echo "<tr><td colspan='2'</td><td>Balance</td><td id='balance' class='align_right'>" . $total . "</td></tr></tbody>\n\t\t\t\t</table>";
    echo "<div id='reconcilereport'>";
    reconcilereport($user, $account);
    echo "</div><div id='responsetext'></div>";
}