<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$formname = "BANK RECONCILLATION";
$file = "bankrecon_fr.php";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];

$usertype = $_SESSION["usertype_" . $dbname];
$username = $_SESSION["username_" . $dbname];
mysqli_select_db($conn, $zdb);

$acname = $_POST["acname"];
$mfrmdate = $_POST['frmdate'];
$mtodate = $_POST['todate'];
$dtype = $_POST['dtype'];
$scope = $_POST['scope'];

$dtypecond = '';

if (!empty($acname)) {
    $acnamecond = " AND `accode`='$acname'";
}
if (!empty($dtype)) {
    $dtypecond = " AND `source`='$dtype' ";
}

if (!empty($scope)) {
    switch ($scope) {
        case 'A':
            $scopecond = "";
            break;
        case 'C':
            $scopecond = " AND `clrdate`!='0000-00-00' ";
            break;
        case 'N':
            $scopecond = " AND `clrdate`='0000-00-00' ";
            break;
        default:
            "";
    }
}

$result = mysqli_query($conn, "SELECT vouchno, date , accode, source, date, chqno, chqdate, clrdate, amount,recpno FROM `actran` where `date` >= '$mfrmdate'
                       and `date` <= '$mtodate' $acnamecond $dtypecond $scopecond
                       group by source,vouchno order by source,date,vouchno ") or die(mysqli_error($conn));

$ctr = 0;

echo '<table id="bankrecon" class="table table-bordered table-condensed table-striped" >';
echo '<tr style="background-color: #26A69A;">';
echo '<th style="width:10%" class="bckgrnd" ><b>TYPE</b></th>';
echo '<th style="width:7%" class="bckgrnd" ><b>VOUCHER</b></th>';
echo '<th style="width:6%" class="bckgrnd" ><b>DATE</b></th>';
echo '<th style="width:8%" class="bckgrnd" ><b>RECEIPT NO.</b></th>';
echo '<th style="width:8%" class="bckgrnd" ><b>CHEQUE NO.</b></th>';
echo '<th style="width:8%" class="bckgrnd" ><b>CHEQUE DATE</b></th>';
echo '<th style="width:7%" class="bckgrnd" ><b>CLEAR DATE</b></th>';
echo '<th style="width:8%; text-align: right;"class="bckgrnd" ><b>AMOUNT</b></th>';
echo "</tr>";

$mctr = 0;
while ($row = mysqli_fetch_array($result)) {
    $amount = $row['amount'];
    $fsource = $row['source'];
    $recpno = $row['recpno'];
    switch ($fsource) {
        case 'BR':
            $sour = 'Bank Receipt';
            break;
        case 'BP':
            $sour = 'Bank Payment';
            break;
        case 'TR':
            $sour = 'Contra';
            break;

        default:
            break;
    }
    $amt = abs($amount);
    $mctr++;
    echo "<tr>";
    echo "<td><input type='text' style='border: none;' name='source_$ctr' value='$sour' readonly></td>";
    echo "<td><input type='text' style='border: none;' name='vouchno_$ctr' value='$row[vouchno]' readonly><input type='hidden' name='code_$ctr' value='$row[accode]' readonly></td>";
    echo "<td><input type='date' class='vouchdt' style='border: none;height:15px;' name='vouchdt' value='$row[date]' readonly></td>";
    echo "<td><input type='text' style='border: none;' name='recpno_$ctr' value='$recpno' readonly></td>";
    echo "<td><input type='text' style='border: none;' name='chqno_$ctr' value='$row[chqno]' readonly></td>";
    echo "<td><input type='date' style='border: none;height:15px;' name='chqdate' value='$row[chqdate]' readonly></td>";
    echo "<td><input type='date' class='clrdate' style='height:15px;color: green; border: none;'  border-bottom:none' name='clrdate_$ctr' value='$row[clrdate]'></td>";
    echo "<td class='text-right'><input type='text' style='border: none;' name='amount' class='text-right' value='$amt' readonly></td>";
    echo "</tr>";

    $ctr++;
}
echo "<input type='hidden' name='count' value='$ctr'>";
echo "</table>";
?>
