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

$formname = 'Pending Purchase Order - Party-Wise';
$maindb = 'pordmain';
$trandb = 'pordsacn';
$trandb2 = 'ordelsacn';

$grid = 'pendpord.php';

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

$zcompany = $_SESSION["zcompany"];

mysqli_select_db($conn, $zdb);

if (isset($_POST["view"])) {
    $dtype = 2;
    $ccond = "";
    $pcond = "";
    $cond = "";

    if (!empty($_POST["item"])) {
        $pitem = trim($_POST["item"]);
        $cond = "AND a.descrip LIKE '%$pitem%'";
    }

    if (!empty($_POST["company"])) {
        $pname = trim($_POST["company"]);
        $ccond = "AND a.company = '$pname'";
    }

    if (!empty($_POST["party"])) {
        $pparty = trim($_POST["party"]);
        $pcond = "AND a.party = '$pparty'";
    }

    if (empty($_POST["frmdt"])) {
        browse("FROM DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }

    if (empty($_POST["todt"])) {
        browse("TO DATE CANNOT BE EMPTY");
        header("refresh:1; $grid");
    }

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $get = mysqli_query($conn, "SELECT a.*, b.descrip, b.qty as ordqty, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt 
                        FROM `$maindb` a, `$trandb` b WHERE  a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' 
                        and a.vouchno = b.vouchno ORDER BY a.vouchdt, a.vouchno $pcond $ccond $cond ") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($get);
    ?>
    <!DOCTYPE HTML>
    <html lang="en-US">

        <head>
            <meta charset="UTF-8">
            <title><?php echo $flag . " " . $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>
                (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');

            </script>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <style type="text/css">
                table { page-break-inside:auto }
                tr    { page-break-inside:avoid; page-break-after:always }
                thead { display:table-header-group }
                tfoot { display:table-footer-group }
            </style>




        </head>
        <body>

            <?php
            require("includes/dbconnect.php");
//            $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
//            $zcomp = mysqli_fetch_assoc($comp);
            echo "<font size='4px'><b>$formname</b></font><br />";
//            echo "<font size='4px'><b>$zcomp[compname]</font></b></font><br />";
//            echo "<font size='2px'><b> $zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><br /><br />";

            echo "From Date : " . date("d-m-Y", strtotime($mfrmdate)) . " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  To Date : " . date("d-m-Y", strtotime($mtodate));
            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Total Records : $getno";
            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Report Date : " . date("d-m-Y");
            echo "<br />";
            echo "</font>";
            ?>

            <div class="row">
                <div class="col-lg-6" style="width:100%;">
                    <div class="box">
                        <header>
                            <h5><?php 
                            mysqli_select_db($conn, $zdb); 
                            $getparty = mysqli_query($conn, "SELECT a.*, b.accode, b.name as partyname FROM `$maindb` a, `accmast` b"
                                    . " WHERE a.party = b.accode") or die(mysqli_error($conn));
                            $party = mysqli_fetch_object($getparty);
                        
                        echo "<b font='4px'>Party Name :$party->partyname</b>";
                        ?></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"  value='Save'/> 
                                    </div>
                                </div>
                            </div>
                        </header>
                        <div id="sortableTable" class="body collapse in">
                            <table class="table table-bordered sortableTable responsive-table">
                                <thead>
                                    <tr>
                                        <th align="left"  width='5%'>Sr.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left"  width='10%'>Date<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left"  width='10%'>P.O. No.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left"  width='35%'>Item<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right"  width='10%'>Order<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right"  width='10%'>Received<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right"  width='10%'>Pending<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $totord = 0;
                                    $totrec = 0;
                                    $totpen = 0;

                                    $ctr = 0;
                                    while ($row = mysqli_fetch_object($get)) {
                                        mysqli_select_db($conn, $zdb);
                                        
                                        $grecqty = mysqli_query($conn, "SELECT Sum(qty) as recqty FROM `ordeltran` WHERE ordno='$row->ordno' and descrip = '$row->descrip'")or die(mysqli_error($conn));
                                        $rcdqty = mysqli_fetch_object($grecqty);

                                        $getco = mysqli_query($conn, "SELECT * FROM `branch` WHERE  `code`='$row->company'") or die(mysqli_error($conn));
                                        $company = mysqli_fetch_object($getco);

                                        $getdesc = mysqli_query($conn, "SELECT * FROM `description` WHERE  `code`='$row->descrip'") or die(mysqli_error($conn));
                                        $descrip = mysqli_fetch_object($getdesc);

                                        $ctr++;
                                        $ordqty = $row->ordqty;
                                        $recqty = $rcdqty->recqty;
                                        $pendqty = $ordqty - $recqty;

                                        echo "<tr> ";
                                        echo "<td align='left'>$ctr</td>";
                                        echo "<td align='left'>$row->mydt</td>";
                                        echo "<td align='left'>$row->ordno</td>";
                                        echo "<td align='left'>$descrip->name</td>";
                                        echo "<td align='right'>$ordqty</td>";
                                        echo "<td align='right'>$recqty</td>";
                                        echo "<td align='right'>$pendqty</td>";
                                        echo "</tr>";
                                        
                                        $totord = $totord + $ordqty;
                                        $totrec = $totrec + $recqty;
                                        $totpen = $totpen + $pendqty;
    }
                                    
                                    
                                    echo "<tr> ";
                                    echo "<td align='left'></td>";
                                    echo "<td align='left'></td>";
                                    echo "<td align='left'></td>";
                                    echo "<td align='right'><font><b>Total&nbsp;&nbsp;</font></b></td>";
                                    echo "<td align='right'><font color='green'><b>" . number_format($totord, 2, '.', '') . "</b></font></td>";
                                    echo "<td align='right'><font color='green'><b>" . number_format($totrec, 2, '.', '') . "</b></font></td>";
                                    echo "<td align='right'><font color='green'><b>" . number_format($totpen, 2, '.', '') . "</b></font></td>";
                                    echo "</tr>";
                                    ?>


                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- /.col-lg-6 -->
            </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
}// Submit Tag Close
?>