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

$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
mysqli_select_db($conn, $zdb);

$formname = 'History of Engine';

$maindb = 'engine';
$trandb = 'transacn';

$grid = 'histengine.php';
    
if (isset($_POST["view"])) {
    $gcond = "";
    
    if (!empty($_POST["engine"])) {
        $gcode = trim($_POST["engine"]);
        $gcond = " and `engine` = '$gcode' ";
    }

    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");
    }
    
    if (empty($gcode)) {
        browse("Engine Name Cannot Be Empty");
        header("refresh:1; $grid");
    }
    
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $getengine = mysqli_query($conn, "SELECT * FROM `$maindb`  WHERE code = '$gcode'") or die(mysqli_error($conn));
    $engine = mysqli_fetch_array($getengine);
    
    $iname = $engine['name'];
     browse($gcode);
    $gettran = mysqli_query($conn, "SELECT * , DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$trandb` WHERE `engine` = '$gcode' 
                            and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and `source`= 'OU' 
                            order by vouchdt, vouchno" ) or die(mysqli_error($conn));
    $tranno = mysqli_fetch_array($gettran);
    
    /*$getopen = mysqli_query($conn, "SELECT sum(qty) as openqty FROM `$trandb` WHERE `itemcode` = '$gname' and `vouchdt` < '$mfrmdate'" ) or die(mysqli_error($conn));
    $openno = mysqli_fetch_array($getopen);
    
    $opqty = $openno['openqty'];
    browse($gname);*/
    ?>
    <!DOCTYPE HTML>
    <html lang="en-US">

        <head>
            <?php
            include("includes/files.php");
            ?>
        </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>$zcomp[compname]</font></b></font><br />";
            echo "<font size='2px'><b> $zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><br /><br />";
            echo "<b><font size='3px'>Date : " . date("d-m-Y") . "</b><br />";
            echo "From Date - " . date("d-m-Y", strtotime($mfrmdate)) . "<br/>
                To Date &nbsp;&nbsp;&nbsp;&nbsp;-  " . date("d-m-Y", strtotime($mtodate)) . "<br/>";
            echo "<b>" . "Total Records - $getno</b><br />";
            echo "<br />";
            echo "<b>" . "Engine - $iname</b><br />";
            echo "</font>";
            ?>

            <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"  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.No<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='25%'>Date<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='10%'>Voucher<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='10%'>Item<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='10%'>Qty.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $grqty = 0;
                                    $ctr = 0;
                                    
                                    
                                    while ($row = mysqli_fetch_array($gettran)) {
                                        
                                        $getitem = mysqli_query($conn, "Select * from `item` where code = $row[itemcode] order by `name`");
                                        $item = mysqli_fetch_array($getitem);
                                        $sqty = $row[qty] * -1;
                                        $ctr++;
                                        echo "<tr> ";
                                        echo "<td align='left'>$ctr</td>";
                                        echo "<td align='left'>$row[vouchdt]</td>";
                                        echo "<td align='left'>$row[vouchno]</td>";
                                        echo "<td align='left'>$item[name]</td>";
                                        echo "<td align='right'>$sqty</td>";
                                        echo "</tr>";
                                    }
                                    $grqty  = $grqty  + $sqty;
                                        
                                    echo "<tr> ";
                                    echo "<td align='right'></td>";
                                    echo "<td align='right'></td>";
                                    echo "<td align='right'></td>";
                                    echo "<td align='right'></td>";
                                    echo "<td align='right'></td>";
//                                    echo "<td align='left'><font size='2px'> Total</font></td>";
//                                    echo "<td align='right'>$grqty</font></td>";
                                    echo "</tr>";
                                    ?>
                                   

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- /.col-lg-6 -->
            </div>  

            <?php
            include("includes/files_footer.php");
            ?>
            <script>
                $(function() {
                    metisTable();
                    metisSortable();
                });
            </script>
        </body>
    </html>

    <?php
}// Submit Tag Close
?>