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

$root = $_SESSION["zdbroot"];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

$formname = 'Purchase Due';
$maindb = 'purmain';
$trandb = '';
$printfile = "";
$file = '';
$trandbexist = 0;
$grid = '';
$keyfld = "vouchno";
$keycond = " ";
$focusfield = "name";
mysqli_select_db($conn, $zdb);
include("includes/linkshead.php");

if (isset($_POST["submit"])) {
    $count = $_POST["items"];
//    browse($count);
    for ($i = 0; $i <= $count; $i++) {
        $vouchno = $_POST["vvouchno_$i"];
        $complete = $_POST["complete_$i"];
//        browse($vouchno);
//        browse($complete);
        if ($complete == '1') {
            mysqli_query($conn, "UPDATE `$maindb` SET `pay`='1' WHERE `vouchno`='$vouchno'") or die(mysqli_error($conn));
        }
    }
    print '<script type="text/javascript">';
    print " top.window.location = 'mainmenu.php?action=purchalert'";
    print '</script>';
}
?>
</head>
<!--<script src="MyScript/purchase_alert.js?a=<?= mt_rand(); ?>" type="text/javascript"></script>-->
<style>
    .font {
        font-size: 18px;
    }
</style>

<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-body table-responsive" style="width:100%">
                        <div class="row text-center">
                            <div class="col-lg-12 col-md-1 col-sm-3 col-xs-3 pad">

                                <div class="col-lg-4 col-md-1 col-sm-3 col-xs-3 pad">
                                </div>

                                <div class="col-lg-4 col-md-1 col-sm-3 col-xs-3 pad">
                                    <h4 style="color: red;font-size: 20px">Purchase Due </h4>
                                </div>

                                <div class="col-lg-3 col-md-1 col-sm-3 col-xs-3 pad">
                                </div>


                                <div class="col-lg-1 pad text-center">
                                    <input type="submit" name="submit" class="btn btn-primary btn-square button1 skbtn visasave" value="Update" style="line-height:1;background-color:#257180;font-size:12px">
                                    <!-- <input type="button" class="custom-btn btn-1 visasave" value="save"> -->
                                </div>

                            </div>
                        </div>



                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width: 5%;">Voucher</th>
                                    <th style="width: 8%;">Date</th>
                                    <th style="width: 20%;">Supplier</th>
                                    <th style="width: 5%;">Invoice</th>
                                    <th style="width: 20%;">Due Date</th>
                                    <th style="width: 20%;">Amount</th>
                                    <th style="width: 8%;" class="text-center">Clear</th>
                                </tr>
                            </thead>
                            <tbody class="visatable">
                                <?php
                                $tdate = date("Y-m-d");

                                $pctr = 0;
                                $ctr2 = 0;
                                $srchresult = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') AS vouchdt,DATE_FORMAT(a.`duedate`, '%d-%m-%Y') AS duedate, b.accode, b.name FROM $maindb a 
                                                                    LEFT JOIN accmast b ON a.party=b.accode
                                                                    WHERE `duedate`<='$tdate' AND `pay`='' ") or die(mysqli_error($conn));
                                while ($r = mysqli_fetch_array($srchresult)) {
                                    echo "<tr>";
                                    echo "<td class='font'>$r[vouchno]</td>";
                                    echo "<td class='font'>$r[vouchdt]</td>";
                                    echo "<td class='font'>$r[name]</td>";
                                    echo "<td class='font'>$r[invno]</td>";
                                    echo "<td class='font'>$r[duedate]</td>";
                                    echo "<td class='font'>$r[totalamount]</td>";
                                    echo "<td style='text-align:center'><input type='checkbox' name='complete_$pctr'  style='border:none;position: inherit;height:25px;width:25px;' value='1' class='visachk'></td>";
                                    echo "<td hidden><input type='hidden' value='$r[vouchno]' name='vvouchno_$pctr' class='form-control visavno'></td>";
                                    echo "</tr>";
                                    $pctr++;
                                }
                                ?>
                            <input type="hidden" name="items" value="<?= $pctr ?>">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <?php include("includes/linksfoot.php"); ?>

</body>

</html>