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

$maindb = 'stkentmain';
$trandb = 'stkenttran';
$printfile = "stkentry_print.php";
$file = 'stkentry.php';
$trandbexist = 1;
$keyfld = "vouchno";
$myyear = $_SESSION["tit_" . $dbname];

date_default_timezone_set('Asia/Kolkata');
$time = date('H:i:s');

$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];

$formname = 'Stock Entry';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");


if (isset($_POST["submit"])) {

    $a = 1;

    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $mvouch = $qvouch;

    if ($vouchdt == '0000-00-00') {
        browse("Voucher Cannot Be Empty");
        $a = 0;
    }

    if ($vouchdt == '') {
        browse("Voucher Cannot Be Empty");
        $a = 0;
    }


    if ($a == 1) {


        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno+0) AS maxcode FROM `$maindb` where vouchno != '' $keycond LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        }

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));

        $cnt = 0;
        $cntamt = 0;

        if (!empty($_POST["item"])) {
            for ($i = 0; $i < count($_POST["item"]); $i++) {
                $msrno = $_POST["srno"][$i];
                $item = $_POST["item"][$i];
                $qty = $_POST["qty"][$i];
                $unit = $_POST["unit"][$i];

                $cnt++;
                $group = $source . $mvouch;

                if (!empty($item)) {
                    $tqty = $qty * -1;

                    mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, vouchdt, srno, item, qty, unit)
                                                    VALUES('$mvouch', '$vouchdt', '$cnt', '$item', '$qty', '$unit' )") or die(mysqli_error($conn));

                }
            }
        }

        if ($addflag == 1) {
            $newvoucher = $mono . $mvouch;

            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, adduser)
                                                VALUES('$mvouch', '$vouchdt', '$username')") or die(mysqli_error($conn));


            $_SESSION['print_voucher'] = $mvouch;
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt', edituser='$username' WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        }


        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        } else {
            updmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        }
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'  ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

}

$printdisabled = "none";

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE") or die(mysqli_error($conn));

    $delcond2 = " vouchno = '$_GET[vouchno]'";

    delnew($zdb, "$maindb", "$delcond2", "$file?mode=last");
    delnew($zdb, "$trandb", "$delcond2", "$file?mode=last");

    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script src="MyScript/stkentry.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/stkentry_search.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>

</head>

<body>
    <form action='' method='POST'>
        <div class="col-lg-3 pad text-center" id="overlay_form" style="display:none;z-index:999;">
            <button type="button" id="close_form" style="float: right;font-weight: bold;font-size: 18px;color:red;"><i
                    class="fa fa-close"></i></button>
            <div style='width:100%;' id='browdata'>
            </div>
        </div>
        <!-- Default box -->
        <div>
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-heading" style="height:40px">
                        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10">
                            <h3 class="panel-title" style="color:#78909C"><a class="myPrint"><?php echo $formname; ?></a></h3>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2  col-xs-2">
                            <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                            <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                            <h4 class="panel-title" style="float:right"><b><?php echo $get['username'] ?></b></h4>
                        </div>
                    </div>

                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" class="form-control form vouchdt focusfield" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required>
                                <input type="hidden" name="vouchno" class="form-control form vouchno"  value="<?php echo ($addflag != 1) ? $get['vouchno'] : "$qvouch" ?>">
                            </div>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table-striped table-condensed table-bordered tab" width="100%">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width: 1%"></th>
                                    <th style="width: 2%">Sr.</th>
                                    <th style="width: 22%;">Item</th>
                                    <th style='width: 5%' class="text-right">Qty.</th>
                                    <th style='width: 5%' class="text-left">Unit</th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                if ($addflag != 1) {
                                    $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$get[vouchno]' ORDER BY srno") or die(mysqli_error($conn));
                                    $ctr = 0;
                                    while ($row = mysqli_fetch_array($qry)) {

                                        $getitem = mysqli_query($conn, "SELECT accode, name FROM `accmast` WHERE accode = '$row[item]'") or die(mysqli_error($conn));
                                        $fitem = mysqli_fetch_array($getitem);

                                        echo "<tr>";
                                        echo "<td class='text-center'><i class='fa fa-close remove' style='display:none;'></i></td>";
                                        echo "<td><input type='text' name='srno[]' class='srno form-control  form'  value='$row[srno]' readonly></td>";
                                        echo "<td>";
                                        echo "<select class='form-control form item select2' name='item[]' style='width:100%'>";
                                        echo "<option value='$fitem[accode]'>$fitem[name]</option>";
                                        $gitem = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode` = '012' order by name ") or die(mysqli_error($conn));
                                        while ($item = mysqli_fetch_array($gitem)) {
                                            echo "<option value='$item[accode]'>$item[name]</option>";
                                        }
                                        echo "</select>";
                                        echo "</td>";

                                        echo "<td><input type='number' step='any' name='qty[]' class='qty form-control  text-right form' value='$row[qty]' readonly></td>";
                                        echo "<td><input type='text'  name='unit[]' class='unit form-control form' value='$row[unit]' readonly></td>";
                                        echo "</tr>";
                                      
                                        $ctr++;
                                    }
                                } else {
                                    ?>
                                    <tr>
                                        <td class='text-center'><i class='fa fa-close remove'></i></td>
                                        <td><input type='text' name='srno[]' class='srno form-control form' value='1' readonly>
                                        </td>
                                        <td>
                                            <select class="form-control form item select2" name="item[]"
                                                style='width:100%;'>
                                                <option value=""></option>
                                                <?php
                                                $gitem2 = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE grpcode = '012' order by `name` ") or die(mysqli_error($conn));
                                                while ($item2 = mysqli_fetch_array($gitem2)) {
                                                    echo "<option value='$item2[accode]'>$item2[name]</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td><input type="number" step="any" name='qty[]' class='qty form-control text-right form' value='0'></td>
                                        <td><input type="text" name='unit[]' class='unit form-control form' value='' readonly></td>
                                    </tr>
                                        
                                    <?php
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>

                    <div class="col-lg-12 pad">
                        <button type="button" class="btn btn-primary btn-square form" id="addnew">
                            <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>
                        </button>
                        <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>">
                    </div>
                </div>

                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <?php include("includes/buttons.php"); ?>
                        </div>
                    </div>
                </div>
            </div>


        </div>
    </form>


    <div class="modal fade itemModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" value="<?php // Echo $fseries                                                                                                      
                    ?>" class="form-control series">
                    <input type="hidden" value="<?php echo $get['vouchno']; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <!--<input type="hidden" value="<?php echo $para ?>" class="form-control para">-->
                    <div class="row">
                        <div class="col-lg-3">
                            <select class="form-control ifield">
                                <option value="1">Name</option>
                                <option value="2">Barcode</option>
                                <option value="3">Itemcode</option>
                                <!--                            <option value="4">Gram</option>
                                                        <option value="5">Unit</option>-->
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control ioperator">
                                <option value=""></option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" value="" class="form-control isearch">
                        </div>
                        <div class="col-lg-3">
                            <input type="button" class="btn btn-primary iload" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 iloader text-center">
                        <img src="images/search.gif" alt="loading">
                    </div>
                    <div class="col-lg-12 inoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th width="8%">Itemcode</th>
                                    <th width="49%">Name</th>
                                    <!--                                <th width="17%">Barcode</th>
                                <th width="8%">Gram</th>
                                <th width="8%">Unit</th>-->
                                    <th width="8%" class="text-right">Rate</th>
                                    <th width="10%">Stk.</th>

                                </tr>
                            </thead>
                            <tbody class="iTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary iprevious" value="Previous">
                            <input type="button" class="btn btn-primary inext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <div class="modal fade searchModal bs-example-modal-lg" tabindex="-1" role="dialog"
        aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg" style="margin-top: 15%">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <input type="hidden" value="<?php echo $fseries ?>" class="form-control series">
                    <input type="hidden" value="<?php echo $get['vouchno']; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php echo $gtype ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Date</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Date</option>
                                ">Party</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search">
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <img src="images/search.gif" alt="loading">
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <!-- <th width="15%">Voucher</th> -->
                                    <th width="30%">Date</th>
                                    <th width="15%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary sprevious" value="Previous">
                            <input type="button" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>

    <?php include("includes/linksfoot.php"); ?>
</body>

</html>