<?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 Spare Parts';

$maindb = 'item';
$trandb = 'transacn';

$grid = 'histspare.php';
    
if (isset($_POST["view"])) {
    $gcond = "";
    
    if (!empty($_POST["item"])) {
        $gname = trim($_POST["item"]);
        $gcond = " and `itemcode` = '$gname' ";
    }

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

    $getitem = mysqli_query($conn, "SELECT * FROM `$maindb`  WHERE code = '$gname'") or die(mysqli_error($conn));
    $getno = mysqli_fetch_array($getitem);
    
    $iname = $getno['name'];
     
    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `itemcode` = '$gname' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' order by vouchdt,
                            source,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>" . "Item - $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='5%'>Source<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%'>Add<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" width='10%'>Less<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='10%'>Balance<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    
                                        echo "<tr> ";
                                        echo "<td align='left'></td>";
                                        echo "<td align='left'></td>";
                                        echo "<td align='left'>OP</td>";
                                        echo "<td align='left'>Opening</td>";
                                        if ($opqty > 0) {
                                            echo "<td align='right'>$opqty</td>";   
                                            echo "<td align='right'></td>";   
                                            $openqty = $openqty + $opqty ;
                                        } else {
                                            echo "<td align='right'></td>";   
                                            echo "<td align='right'>$opqty</td>";   
                                            $openqty = $openqty + abs($opqty) ;
                                        }
                                        echo "<td align='right'></td>";
                                        echo "</tr>";
                                  
                                    $gropen = 0;
                                    $gradd  = 0;
                                    $grless = 0;
                                    $grbal  = 0;
                                    $balqty = openqty;
                                    $addqty = 0;
                                    $lessqty = 0;
                                    $ctr = 0;
                                    
                                    
                                    while ($row = mysqli_fetch_array($gettran)) {
                                        $ctr++;
                                        $sqty = $row[qty];
                                        echo "<tr> ";
                                        echo "<td align='left'>$ctr</td>";
                                        echo "<td align='left'>$row[vouchdt]</td>";
                                        echo "<td align='left'>$row[source]</td>";
                                        echo "<td align='left'>$row[vouchno]</td>";
                                        if ($sqty > 0) {
                                            echo "<td align='right'>$sqty</td>";   
                                            echo "<td align='right'></td>";   
                                            $addqty = $addqty + abs($sqty) ;
                                        } else {
                                            echo "<td align='right'></td>";   
                                            echo "<td align='right'>$sqty</td>";   
                                            $lessqty = abs($lessqty) + abs($sqty) ;
                                        }
                                        $balqty = $addqty - $lessqty;
                                        echo "<td align='right'>$balqty</td>";
                                        echo "</tr>";
                                    }
                                    $gradd  = $gradd  + $addqty;
                                    $grless = $grless   + $lessqty;
                                    $grbal  = $grbal  + $balqty;
                                        
                                    echo "<tr> ";
                                    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'>$gradd</td>";
                                    echo "<td align='right'>$grless</td>";
                                    echo "<td align='right'>$grbal</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
?>