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

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

$formname = "No Payment - Datewise";
$file = "custnopay.php";

$maindb = "accmast";
$trandb = "actran";

mysqli_select_db($conn, $zdb);


$mfrmdate = $_GET["frmdt"];
$mtodate = $_GET["todt"];
$notfound = $_POST["notfnd"];


if (empty($mfrmdate)) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close();</script>";
    exit();
}

if (empty($mtodate)) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close();</script>";
    exit();
}

// if ($mfrmdate > $mtodate) {
//     browse("To Date Cannot Be Less Than From Date");
//     echo "<script>window.close();</script>";
//     exit();
// }

if (isset($_POST["view"]) || isset($_GET["view"]) ) {


    $gettran = mysqli_query($conn, "SELECT accode,name,city,mobile FROM `accmast` WHERE grpcode = '010' and accode NOT IN (SELECT accode FROM actran
                                                  WHERE date >= '$mfrmdate' and date <= '$mtodate' and source = 'CR' )  order by name") or die(mysqli_error($conn));

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style>
        table th{
            background-color: #337AB7;
        }
    </style> 
</head>

    <body id="ledger">   
        
        <form name="myform" method="post">
            <div class="row" style="padding : 1%">
                <div class="col-lg-1"></div>
                <div class="col-lg-10">
                    <?php include ("includes/head_rep.php"); ?>
                    <div class="box">
                        
                        <div id="sortableTable" class="body collapse in">
                            
                            <table class="table table-bordered sortableTable responsive-table">

                                <thead>
                                    <tr>
                                        <th style="width:5%">Sr.</th>
                                        <th style="width:25%">Customer Name</th>
                                        <th style="width:7%">Mobile No.</th>
                                        <th style="width:7%">City</th>
                                        <th style="width:10%">Last Paid</th>
                                        <th style="width:10%" class="text-right">Bill Amount</th>
                                        
                                    </tr>
                                </thead>
                                <tbody class="tbody">

                                    <?php
                                    $totcash = $totbank = $tottrf = $mnctr = 0;

                                    while ($fetacc = mysqli_fetch_array($gettran)) {

                                        $accode = $fetacc['accode'];
                                        $name = $fetacc['name'];

                                        $getbill = mysqli_query($conn, "SELECT *, DATE_FORMAT(date,'%d-%m-%Y') as mydt FROM actran WHERE accode = '$accode' 
                                                                                      AND date < '$mfrmdate' AND source = 'CR' and amount > 0 order by date desc limit 1") or die(mysqli_error($conn));
                                        $fetbill = mysqli_fetch_array($getbill);
                                        $norows = mysqli_num_rows($getbill);
                                        $city = $fetacc['city'];
                                        $mobile = $fetacc['mobile'];

                                        if ($norows > 0) {
                                            $vouchno = $fetbill['vouchno'];
                                            $billdate = $fetbill['mydt'];
                                            $billamt = abs($fetbill['amount']);
                                        } else {
                                            if ($notfound == "Y") {
                                                continue;
                                            } else {
                                                $vouchno = 'Not Found';
                                                $billdate = '';
                                                $billamt = 0;
                                            }
                                        }
                                        
                                        $mnctr++;

                                        echo "<tr>";
                                        echo "<td>$mnctr</td>";
                                        echo "<td>$name</td>";
                                        echo "<td>$mobile</td>";
                                        echo "<td>$city</td>";
                                        echo "<td>$billdate</td>";
                                        echo "<td class='text-right'>" . number_format($billamt, 2) . "</td>";
                                        echo "</tr>";
                              
                                }
                                ?>
                                </tbody>
                            </table>    
                        </div>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php"); ?>
        </form>
    </body>
    </html>
    <?php
}

?>