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

$maindb = "item";
$trandb = "transacn";
$formname = "Inward Outward History";
$file = "inwoutrep.php";
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];
$bseries = $_SESSION["user_series"];

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


if (isset($_POST["view"])) {
    $itemname = $_POST['itemname'];

    $name = $itemname;
    $a = 1;

    $nmcond = "";
    $nmcond1 = "";
    if (!empty($itemname)) {
        $nmcond = " and `code` = '$itemname'";
        $nmcond1 = " and `itemcode` = '$itemname'";
    } else {
        browse("Item Name Cannot Be Empty");
        $a = 0;
    }


    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        $a = 0;
    }

    if (empty($_POST["todt"])) {
        browse("TO DATE CANNOT BE EMPTY");
        $a = 0;
    }

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

    $mycomp = $_SESSION["comp_" . $dbname];
    $getcomp = mysqli_query($conn, "SELECT *,UCASE(name)as cname FROM $zroot.`company` WHERE `code`='$mycomp'")or die(mysqli_error($conn));
    $comp = mysqli_fetch_array($getcomp);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

    if ($a == 0) {
        echo "<script>window.close();</script>";
    } else {
        $gettran = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS mydate FROM `$trandb` WHERE `itemcode` = '$itemname' 
                                        and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' order by vouchdt, ordno, vouchno") or die(mysqli_error($conn));
        $getno = mysqli_num_rows($gettran);
        include("includes/linkshead.php");
        $getitem = mysqli_query($conn, "Select code, name from item where code = '$itemname'") or die(mysqli_error($conn));
        $fetitem = mysqli_fetch_array($getitem);
        ?>
        <style>
            @media print {
                .noPrint {
                    display:none;
                }
            }

        </style>
        </head>
        <body>
            <div class="container">
                <?php
                include("includes/head_rep.php");
                ?>
                <div class="row">
                    
                    <div class="col-lg-12" style="width:100%;">
                        <div class="box">
                            <div id="sortableTable" class="body collapse in" >
                                <table id="" class="table table-bordered sortableTable table-responsive" table-hover table-striped style="width:100%;">
                                    <thead>
                                        <tr style="height:10px;background-color: #78909C;color: #ffffff;">
                                            <?php
                                            echo "<th align='left' style = 'width:100%'>$fetitem[name]</th>";
                                            ?>  
                                        </tr>
                                    </thead>
                                </table>
                                <table id="" class="table table-bordered sortableTable table-responsive" table-hover table-striped style="width:100%;">
                                    <thead>
                                        <tr style="height:10px;background-color: #78909C ;color: #ffffff;">
                                            <th align="left" width='5%' style="font-family: arial;">Sr.</th>
                                            <th align="left" width='12%'>Date</th>
                                            <th align="left" width='12%'>Voucher</th>
                                            <th align="left" width='12%' style="text-align: right;">Add<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="right" width='12%' style="text-align: right;">Less<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                            <th align="left" width='12%' style="text-align: right;">Balance<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $gradd = 0;
                                        $grless = 0;

                                        $getopen = mysqli_query($conn, "SELECT sum(opening) as sqty FROM `$maindb` WHERE `code` = '$itemname'") or die(mysqli_error($conn));
                                        $iopen = mysqli_fetch_array($getopen);
                                        $openqty1 = $iopen['sqty'];

                                        $getop = mysqli_query($conn, "SELECT sum(qty) as balqty FROM `$trandb` WHERE   `itemcode` = '$itemname' and
                                                          `vouchdt` < '$mfrmdate' ") or die(mysqli_error($conn));
                                        $iop = mysqli_fetch_array($getop);
                                        $opqty = $iop['balqty'];
                                        $openqty = $openqty1 + $opqty;


                                        $ctr = 1;
                                        $balqty = $openqty;
                                        echo "<tr>";
                                        echo "<td align='left'>$ctr</td>";
                                        echo "<td align='left'></td>";
                                        echo "<td align='left'>Opening</td>";

                                        if ($balqty > 0) {
                                            echo "<td align='right'>$balqty</td>";
                                            echo "<td align='right'></td>";
                                            $gradd = $gradd + $balqty;
                                        } else {
                                            $lqty = abs($balqty);
                                            echo "<td align='right'></td>";
                                            echo "<td align='right'>$lqty</td>";
                                            $grless = $grless + $lqty;
                                        }

                                        echo "<td align='right'>$balqty</td>";
                                        echo "</tr>";


                                        while ($row = mysqli_fetch_object($gettran)) {
                                            $qty = $row->qty;

                                            $ctr++;
                                            echo "<tr>";
                                            echo "<td align='left'>$ctr</td>";
                                            echo "<td align='left'>$row->mydate</td>";
                                            echo "<td align='left'>$row->source  $row->vouchno</td>";

                                            if ($qty > 0) {
                                                echo "<td align='right'>$qty</td>";
                                                echo "<td align='right'></td>";
                                                $balqty = $balqty + $qty;
                                                $gradd = $gradd + $qty;
                                            } else {
                                                $lqty = abs($qty);
                                                echo "<td align='right'></td>";
                                                echo "<td align='right'>$lqty</td>";
                                                $balqty = $balqty - $lqty;
                                                $grless = $grless + $lqty;
                                            }

                                            echo "<td align='right'>$balqty</td>";
                                            echo "</tr>";
                                        }
                                        $grbal = $balqty;

                                        echo "<tr style =''> ";
                                        echo "<td align='left'></td>";
                                        echo "<td align='left'></td>";
                                        echo "<td align='left'><font size='3px'><b>Total</b></font></td>";
                                        echo "<td align='right'><font size='3px'><b>$gradd</b></font></td>";
                                        echo "<td align='right'><font size='3px'><b>$grless</b></font></td>";
                                        echo "<td align='right'><font size='3px'><b>$grbal</b></font></td>";
                                        echo "</tr>";
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- /.col-lg-6 -->
                </div>
                <?php
                include("includes/linksfoot.php");
            }
        }
        ?>
    </div>
</body>
</html>