<?php
ob_start();
include('includes/function.php');
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
//browse(1);
$maindb = 'dipmain';
$trandb = 'diptran';
$trandb2 = '';
$accdb = 'actran';
$stkdb = 'transacn';
$printfile = "";
$file = 'dipreading.php';
$trandbexist = 1;
$source = "";
$keyfld = "vouchno";
$myyear = $_SESSION["tit_" . $dbname];

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

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

//browse($username);
$formname = 'Dip Reading';

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

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

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

    $a = 1;

    $vouchdt = trim($_POST["vouchdt"]);
    $shift = trim($_POST["shift"]);
    $staff = trim($_POST["staff"]);
    $cashamt = trim($_POST["cashamt"]);


    date_default_timezone_set("Asia/Dubai");
    $usertime = date("Y-m-d H:i:sa");

    $oms = $_POST['oms'];

    $count = $_POST["items"];
    $count2 = $_POST["items2"];
    $mvouch = $qvouch;

    $accode = $party;
    $grpcode = '010';

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




    if ($a == 1) {
//        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE,  `$trandb2` WRITE, `product` WRITE, `$stkdb` WRITE, `$accdb` WRITE, `cashrecp` WRITE, `accmast` WRITE");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` where vouchno != ''  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;
//
//        $rows = array();
        $query = "INSERT INTO `$trandb`(`vouchno`, `prodcode`, `tankno`, `unit`, `opendip`, `openltrs`, `adddip`, `addltrs`, `closedip`, `closeltrs`, `saleltrs`) VALUES";
        $tuple = " ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s','%s'),";


        for ($i = 0; $i <= $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $tankno = $_POST["tankno_$i"];
            $prodcode = $_POST["prodcode_$i"];
            $opendip = $_POST["opendip_$i"];
            $openltrs = $_POST["openltrs_$i"];
            $adddip = $_POST["adddip_$i"];
            $addltrs = $_POST["addltrs_$i"];
            $closedip = $_POST["closedip_$i"];
            $closeltrs = $_POST["closeltrs_$i"];
            $saleltrs = $_POST["saleltrs_$i"];


            if (!empty($prodcode)) {
                $cnt++;

                $query .= sprintf($tuple, $mvouch, $prodcode, $tankno, $unit, $opendip, $openltrs, $adddip, $addltrs, $closedip, $closeltrs, $saleltrs);

//                $query3 .= sprintf($tuple3, $cnt2, $myyear, $mvouch, $vouchdt, $itcode, $trate, $tprodcode, $tqty, $source, $tcashamt, $party, $group, $cost, 1);
            }
        }
        $query = rtrim($query, ",");
        mysqli_query($conn, $query) or die(mysqli_error($conn));



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

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

//            $_SESSION['print_voucher'] = $mvouch;
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt', shift='$shift', staffcode='$staff' 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&form=$form");
        } else {
            updmess();
        }
    }
}

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);

    $getstaf = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`= '$get[staffcode]'") or die(mysqli_error($conn));
    $lstaf = mysqli_fetch_array($getstaf);
    $rd = "readonly";
    $delcsh = $get['cashvouch'];
}

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/dipreading.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/dipread_search.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-12 col-md-12 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-12">
                            <h3 class="panel-title" style="color:black"><a class="myPrint"><?php echo $formname; ?></a></h3>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
                            <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-1 col-md-1 col-sm-2 col-xs-3 text-right pad">
                                <label class="label-control marg" >Date</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-4 pad">
                                <input type="hidden" class="form-control form addflg" name="addflg"  value="<?php Echo "$addflag"; ?>">
                                <input type="hidden" class="form-control form billflag" name="billflag"  value="">
                                <input type="hidden" class="form-control form chgflag" name="chgflag"  value="">
                                <input type="hidden" class="form-control form vouchno" name="vouchno"  value="<?php Echo "$qvouch"; ?>">
                                <input type="hidden" class="form-control form cashvouch" name="cashvouch"  value="<?php Echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>">
                                <input type="date" class="form-control form vouchdt focusfield" name="vouchdt"  value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus required <?= $rd ?>>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-3 text-right pad">
                                <label class="label-control marg" >Shift</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-4 pad">
                                <select name="shift" id="shift" class="form-control form shift select2 " required>
                                    <option value="<?php echo ($addflag != 1) ? $get['shift'] : ""; ?>"><?php echo ($addflag != 1) ? $get['shift'] : ""; ?></option>
                                    <?php
                                    if ($addflag == 1) {
                                        $mshift = mysqli_query($conn, "SELECT * FROM shift order by shift") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mshift)) {
                                            echo "<option value='$trow[shift]'>$trow[shift]</option>";
                                        }
                                    }
                                    ?>
                                </select>

                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Staff</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-7 col-xs-9  pad">
                                <select name="staff" id="staff" class="form-control form select2 staff" required>
                                    <option value="<?php echo ($addflag != 1) ? $lstaf['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $lstaf['name'] : ""; ?></option>
                                    <?php
                                    $mstaff = mysqli_query($conn, "SELECT * FROM accmast where grpcode = '040' order by name") or die(mysqli_error($conn));
                                    while ($tsta = mysqli_fetch_array($mstaff)) {
                                        echo "<option value='$tsta[accode]'>$tsta[name]</option>";
                                    }
                                    ?>
                                </select>

                            </div>
                        </div>
                    </div>


                    <div class="sloader3 text-center">
                        <img src="images/search.gif" alt="loading">       
                    </div>
                    <div class="table-responsive readtab">
                        <table class="table-striped table-condensed table-bordered tab" width="100%">
                            <thead  class="mySearchTr">
                                <tr>
                                    <th style="width: 2%">Tank</th>
                                    <th style='width: 10%' class="text-left">Product</th>                                        
                                    <th style='width: 3%' class="text-right">Op.Dip</th>
                                    <th style='width: 2%' class="text-right">Op.Litres</th>
                                    <th style='width: 2%' class="text-right">Add Dip</th>
                                    <th style='width: 2%' class="text-right">Add Litres</th>
                                    <th style='width: 3%' class="text-right">Close Dip</th>                                        
                                    <th style='width: 2%' class="text-right">Close Litres</th>
                                    <th style='width: 3%' class="text-right">Sale Litres</th>                                       

                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                if ($addflag != 1) {
                                    $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch' order by tankno") or die(mysqli_error($conn));
                                    $ctr = 0;
                                    while ($row = mysqli_fetch_array($qry)) {
                                        $getpd = mysqli_query($conn, "SELECT name,prodcode,rate FROM `product` WHERE `prodcode`= '$row[prodcode]'") or die(mysqli_error($conn));
                                        $lpd = mysqli_fetch_array($getpd);
                                        Echo "<tr>";
//                                        Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                        Echo "<td><input type='text' name='tankno_$ctr' class='tank form-control tankno  form'  value='$row[tankno]' readonly tabindex='-1'></td>";
                                        Echo "<td><input type='hidden' class='form-control form prodcode'             name='prodcode_$ctr'  value='$row[prodcode]' readonly tabindex='-1'>"
                                        . "<input type='text' class='form-control form product'               name='product_$ctr' value='$lpd[name]' readonly tabindex='-1'></td>";
                                        Echo "<td><input type='text' class='form-control form opendip'               name='opendip_$ctr'  value='$row[opendip]'></td>";
                                        Echo "<td><input type='text' class='form-control form openltrs'               name='openltrs_$ctr'  value='$row[openltrs]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right adddip'     name='adddip_$ctr' value='$row[adddip]'></td>";
                                        Echo "<td><input type='text' class='form-control form addltrs'               name='addltrs_$ctr'  value='$row[addltrs]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right closedip'    name='closedip_$ctr'  value='$row[closedip]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right closeltrs'  name='closeltrs_$ctr'  value='$row[closeltrs]' ></td>";
                                        Echo "<td><input type='text' class='form-control form text-right saleltrs'               name='saleltrs_$ctr'  value='$row[saleltrs]' readonly tabindex='-1'></td>";
                                        Echo "</tr>";
                                        $ctr++;
                                    }
                                }
                                ?>
                            <input type="hidden" id="items" name="items" value="<?php Echo ($addflag != 1) ? $ctr : 1; ?>">  

                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>
        <div class="row finish">
            <div class="col-lg-10">
            </div>

            <div class="col-lg-1" style="padding-left: 4%;">

                <button type="button" class="form btn btn-default finishtab" style="background-color: #26A69A;color: white;font-size: 15px;width: 150px;">Check & Finish</button>
            </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"); ?>
                    <script>
                        $(document).ready(function () {

                            var usertype = <?= $usertype ?>;
                            if (usertype == 3) {
                                $("#hedit").hide();
                            }
                        });
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
</form>


<div class="modal fade" id="cancmodel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="col-lg-11">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Cancel Bill</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-12 upload">
                            <form id="upload_form" method="post">

                                <div class="row pad">
                                    <input type="hidden" name="cancvouch" id="cancvouch" value='<?php Echo ($addflag != 1) ? $get['vouchno'] : "$qvouch"; ?>'>
                                    <input type="hidden" name="cashvouch2" id="cashvouch2" value='<?php Echo ($addflag != 1) ? $get['cashvouch'] : ""; ?>'>
                                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-5 text-right">
                                        <label class="label-control marg">Cancel Date</label>
                                    </div>
                                    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-7">

                                        <input type="date" name="cancdate" value="<?php Echo ($addflag != 1) ? $get['cancdate'] : ""; ?>" class="form-control cancdate">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-6 col-md-6 col-sm-7 col-xs-8 text-right">
                                        <input type="submit" value="Cancel"  name="cancbtn" class="btn btn-square" style="background-color: #C0CA33; color: white">
                                    </div>
                                </div>

                            </form>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade bs-example-modal-lg searchModal" 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">
                <div class="row">
                    <div class="col-lg-2">
                        <select class="form-control sfield">
                            <option value="1">Date</option>
                            <option value="2">Staff</option>
                            <option value="3">Shift</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>
                            <option value="2">Staff</option>
                            <option value="3">Shift</option>
                        </select> 
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control orderAs">
                            <option value="asc">Asc</option>
                            <option value="desc">Desc</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 style="background-color:#1ABC9C;">
                                <th width="20%">Date</th>
                                <th width="50%">Staff</th>
                                <th width="25%">Shift</th>
                                <th width="5%">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 sprev" 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>

