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

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

$trandb = "actran";
$myear = $_SESSION["tit_" . $dbname];
mysqli_select_db($conn, $zdb);

if (isset($_POST["view"])) {
    $acond = "";
    $scond = "";

    if (!empty($_POST["acname"])) {
        $accode = trim($_POST["acname"]);
        $acond = "AND a.`accode`='$accode'";
    }
    if (!empty($_POST["cmbstation"])) {
        $station = trim($_POST["cmbstation"]);
        $scond = "AND a.`station`='$station'";
    }

    $mtodate = $_POST["frmdt"];

    if ($_POST["dtype"] == 1) {
        $repname = "$formname - Summary";
        $dtype = 1;
    } else {
        $repname = "$formname - Detail";
        $dtype = 2;
    }
    if ($_POST["hiddenval"] == "R") {
        $mfile = "salemain";
        $mtype = "S";
        $mdate = 'billdate';
        
        $grid = "osbill.php?para=R";
    } else {
        $mfile = "purchmain";
        $mtype = "P";
        
        $mdate = 'vouchdt';
        $grid = "osbill.php?para=P";
    }
    ?>
    <!DOCTYPE HTML>
    <html lang="en-US">
        <head>
            <meta charset="UTF-8">
            <title><?php echo $formname; ?></title>
            <meta name="msapplication-TileColor" content="#5bc0de"/>
            <meta name="msapplication-TileImage" content="assets/img/metis-tile.png"/>
            <link rel="stylesheet" href="assets/lib/bootstrap/css/bootstrap.css">
            <link rel="stylesheet" href="assets/css/main.css"/>
            <link rel="stylesheet" href="assets/lib/Font-Awesome/css/font-awesome.css"/>
            <link rel="stylesheet" href="assets/css/theme.css">
            <link rel="stylesheet" href="assets/lib/uniform/themes/default/css/uniform.default.css">
            <link rel="stylesheet" href="assets/lib/inputlimiter/jquery.inputlimiter.1.0.css">
            <link rel="stylesheet" href="assets/lib/chosen/chosen.min.css">
            <link rel="stylesheet" href="assets/lib/colorpicker/css/colorpicker.css">
            <link rel="stylesheet" href="assets/lib/tagsinput/jquery.tagsinput.css">
            <link rel="stylesheet" href="assets/lib/daterangepicker/daterangepicker-bs3.css">
            <link rel="stylesheet" href="assets/lib/datepicker/css/datepicker.css">
            <link rel="stylesheet" href="assets/lib/timepicker/css/bootstrap-timepicker.min.css">
            <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.css">
            <script type="text/javascript" src="js/jquery.js"></script>
            <script>
                (function (i, s, o, g, r, a, m) {
                    i['GoogleAnalyticsObject'] = r;
                    i[r] = i[r] || function () {
                        (i[r].q = i[r].q || []).push(arguments)
                    }, i[r].l = 1 * new Date();
                    a = s.createElement(o),
                            m = s.getElementsByTagName(o)[0];
                    a.async = 1;
                    a.src = g;
                    m.parentNode.insertBefore(a, m)
                })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
                ga('create', 'UA-1669764-16', 'onokumus.com');
                ga('send', 'pageview');
                jQuery(document).ready(function ($) {
                    $(".clickableRow").click(function () {
                        window.document.location = $(this).attr("href");
                    });
                });

            </script>
        </head>
        <body>
            <div class="row">
                <div class="col-lg-6" style="width:100%;">
                    <div class="box">
                        <header>
                            <h5><?php echo $formname; ?></h5>
                            <div class="toolbar">
                                <div class="btn-group">		  
                                    <div class="btn-group">
                                        <a href="#sortableTable" data-toggle="collapse" class="btn btn-default btn-sm minimize-box">
                                            <i class="fa fa-angle-up"></i>
                                        </a>
                                        <input type='button' name='submit' class='btn btn-sm btn-danger' onClick="window.location.href = '<?php echo $grid; ?>'" value="Exit"/> 
                                    </div>
                                </div>
                            </div>
                        </header>         
                        <div id="sortableTable" class="body collapse in">
                            <table class="table table-bordered sortableTable responsive-table">
                                <thead>

                                </thead>
                                <tbody>
                                    <?php
                                    $ctr = 0;
                                    $totamt = 0;
                                    $acc = mysqli_query($conn, "SELECT a.*, b.name FROM $mfile a, accmast b WHERE a.vouchno!='' AND a.accode = b.accode $acond $scond GROUP BY a.accode order by b.name") or die(mysqli_error($conn));
                                    while ($getac = mysqli_fetch_array($acc)) {

                                        $maccode = $getac["accode"];
                                        $macname = $getac["name"];


                                        $sub1 = mysqli_query($conn, "SELECT  SUM(`billamt`) AS amt1 FROM $mfile WHERE `accode` = '$maccode' AND `cashcred` != 'C'") or die(mysqli_error($conn));
                                        $sub2 = mysqli_query($conn, "SELECT  SUM(`billamt`) AS amt2 FROM `prevbill` WHERE `accode` = '$maccode' AND `cashcred` != 'C'") or die(mysqli_error($conn));
                                        $msub1 = mysqli_fetch_array($sub1);
                                        $msub2 = mysqli_fetch_array($sub2);
                                        $msamt1 = $msub1["amt1"];
                                        $msamt2 = $msub2["amt2"];
                                        $qamt1 = $msamt1 + $msamt2;
//                                        browse($qamt1);

                                        $trn1 = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$maccode' AND `type`='$mtype'") or die(mysqli_error($conn));
                                        $gtrn1 = mysqli_fetch_object($trn1);
                                        $msetoff1 = $gtrn1->setoff;

                                        $mbalamt1 = $qamt1 - $msetoff1;

                                        if ($mbalamt1 > 0) {
                                            echo "<tr>";
                                            echo "<th style=\"text-align:left;\" colspan='5'><b>" . strtoupper($macname) . "</b></th>";
                                            echo "<th style=\"text-align:right;\"><b>";
                                            if ($dtype == 1) {
                                                echo "$mbalamt1";
                                            }
                                            echo "</b></th>";
                                            echo "</tr>";

                                            $totamt = $totamt + $mbalamt1;

                                            if ($dtype == 2) {
                                                ?>        
                                                <tr>
                                                    <th align="left" width="15%">Date<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="left" width="15%">Bill No.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="right" width="20%" style="text-align:right;">Bill Amount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="right" width="20%" style="text-align:right;">Recd. Amount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="right" width="20%" style="text-align:right;">Balance<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                    <th align="right" width="9%" style="text-align:right;">Days<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                   
                                                </tr>
                                                <?php
                                                $subtmp1 = mysqli_query($conn, "SELECT '$myear' AS `yearno`, '$mtype' AS `type`, `vouchno`, `billno`,
                                                                DATE_FORMAT($mdate, '%d-%m-%Y') AS `billdate`, `billamt`, `accode` 
                                                                FROM $mfile WHERE `accode` = '$maccode' AND `cashcred` != 'C' 
                                                                UNION
                                                                SELECT `yearno`,`type`,`vouchno`, `billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                                                               `billdate`, `billamt`, `accode` FROM `prevbill` WHERE `accode` = '$maccode' 
                                                                AND `type` = '$mtype'") or die(mysqli_error($conn));
                                                $totbillamt = 0;
                                                $totrecdamt = 0;
                                                $totbalamt = 0;

                                                while ($murl = mysqli_fetch_object($subtmp1)) {

                                                    $qyear = $murl->yearno;
                                                    $qbilldate = $murl->billdate;
                                                    $qbill = $murl->billno;
                                                    $qvouch = $murl->vouchno;
                                                    $qamt = $murl->billamt;
                                                    $qparty = $murl->accode;


                                                    $dt1 = date_create($mtodate);
                                                    $dt2 = date_create($qbilldate);
                                                    $mldays = subtractDays($qbilldate, $mtodate);


                                                    $trn = mysqli_query($conn, "SELECT SUM(setoff) AS setoff FROM `billsett` WHERE  `party`= '$qparty' AND `yearno` ='$qyear' AND 
                                                                       `vouchno`='$qvouch' AND `type`='$mtype'") or die(mysqli_error($conn));
                                                    $gtrn = mysqli_fetch_object($trn);
                                                    $msetoff = $gtrn->setoff;
                                                    $mbalamt = $qamt - $msetoff;

                                                    if ($mbalamt > 0) {
                                                        $totbillamt = $totbillamt + $qamt;
                                                        $totrecdamt = $totrecdamt + $msetoff;
                                                        $totbalamt = $totbalamt + $mbalamt;
                                                        echo "<tr>";
                                                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"300\">$qbilldate</td>";
                                                        echo "<td style=\"text-align:left;\" scope=\"col\" width=\"100\">$qbill</td>";
                                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"300\">$qamt</td>";
                                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$msetoff</td>";
                                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$mbalamt</td>";
                                                        echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\">$mldays</td>";
                                                        echo "</tr>";
                                                    }
                                                }
                                                echo "<tr>";
                                                echo "<td style=\"text-align:left;\" scope=\"col\" width=\"300\"></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"100\"><b>Total</b></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"300\"><b>$totbillamt</b></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><b>$totrecdamt</b></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"><b>$totbalamt</b></td>";
                                                echo "<td style=\"text-align:right;\" scope=\"col\" width=\"200\"></td>";
                                                echo "</tr>";
                                            }
                                        }
                                    }
                                    $colr = "blue";
                                    echo "<tr> ";
                                    echo "<td align='right' colspan='5' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
                                    echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totamt</b></h4></td>";
                                    echo "</tr>";
                                    ?>
                                </tbody>



<!--                                <tfoot>

                                    <tr>

                                        <td colspan="9">

                                            <hr style="border-top: 1px solid #dddddd;"/>

                                        </td>

                                    </tr>

                                </tfoot>-->

                            </table>

                        </div>

                    </div>

                </div>

            </div>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
            <script>window.jQuery || document.write('<script src="assets/lib/jquery.min.js"><\/script>')</script> 
            <script src="assets/lib/bootstrap/js/bootstrap.js"></script>
            <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
            <script src="assets/lib/datatables/jquery.dataTables.js"></script>
            <script src="assets/lib/datatables/DT_bootstrap.js"></script>
            <script src="assets/lib/tablesorter/js/jquery.tablesorter.min.js"></script>
            <script src="assets/lib/touch-punch/jquery.ui.touch-punch.min.js"></script>
            <script src="assets/js/main.js"></script>
            <script>
                                            $(function () {
                                                metisTable();
                                                metisSortable();
                                            });
            </script>
        </body>
    </html>
<?php } ?>        



