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

$formname = 'Dispatch';
$maindb = 'dispmain';
$trandb = 'disptran';
$stkdbprod = 'prodsacn';
$printfile = "midelv_print.php";
$file = 'midelv.php';
$trandbexist = 1;
$source = "DPS";
$keyfld = "vouchno";
$keycond = "";
$para = "";
$myyear = $_SESSION["tit_" . $dbname];


$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;
    $vouchdt = trim($_POST["vouchdt"]);
    $voucher = trim($_POST["voucher"]);
    $godown = trim($_POST["tgodwn"]);
    $vehicle = trim($_POST["vehicle"]);
    $transmode = trim($_POST["transmode"]);
    $plsply = trim($_POST["plsply"]);
    $tqty = trim($_POST["totqty"]);
    $rem1 = trim($_POST["remark1"]);
    $rem2 = trim($_POST["remark2"]);
    $monno = trim($_POST["monno"]);

    $count = $_POST["items"];


//    if ($godown == $fseries) {
//        $a = 0;
//        browse("Godown Cannot be Same");
//    }


    if ($a == 1) {

        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, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `$stkdbprod` WRITE, `product` WRITE ")or die(mysqli_error($conn));

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

        $cnt = 0;
        $cntamt = 0;

        for ($i = 0; $i < $count; $i++) {
            $cnt++;
            $msrno = $_POST["srno_$i"];
            $mbarcode = $_POST["barcode_$i"];
            $mprodcode = $_POST["prodcode_$i"];
            $mdescrip = $_POST["descrip_$i"];
            $mmrp = $_POST["mrp_$i"];
            $mnetwt = $_POST["netwt_$i"];
            $mbatchno = $_POST["batchno_$i"];
            $mqty = $_POST["qty_$i"];
            $actpcs = $mqty * -1;
            $mhsncode = $_POST["hsncode_$i"];

            $mpcode = mysqli_query($conn, "SELECT code,cost,rate,prodgrp FROM `product` WHERE `prodcode`='$procode'") or die(mysqli_error($conn));
            $pcode = mysqli_fetch_array($mpcode);
            $itcode = $pcode[code];
            $cost = $pcode[cost];
            $rate = $pcode[rate];
            $unit = $pcode[prodgrp];
            $group = $source . $mvouch;

            if (!empty($mprodcode)) {

                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`,`vouchno`, `vouchdt`, `barcode`, `prodcode`, `descrip`, `mrp`, `netwt`, `batchno`, `qty`, `hsncode`, `monno`)
                             VALUES('$cnt', '$mvouch', '$vouchdt', '$mbarcode', '$mprodcode', '$mdescrip','$mmrp', '$mnetwt','$mbatchno', '$mqty','$mhsncode', '$monno')") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `$stkdbprod`(`srno`, `yearno`, `godown`, `vouchno`, `vouchdt`, `itemcode`,  `unit`,
                                                  `qty`, `source`, amount, accode, `group` , `rate`, `cost`, `itemgrp`, `ordno`, `monno`)
                            VALUES('$cnt', '$myyear', '$godown', '$mvouch', '$vouchdt', '$mprodcode', '$unit',
                                   '$actpcs', '$source', '', '', '$group', '$rate', '$cost', '', '', '$monno')") or die(mysqli_error($conn));
            }
        }

        if ($addflag == 1) {
            $newvoucher = $monno . $mvouch;
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `voucher`,`tgodwn`, `vehicle`, `transmode`, `plsply`,  `totqty`, `rem1`, `rem2`)
                                      VALUES('$mvouch', '$vouchdt', '$voucher', '$godown', '$vehicle', '$transmode', '$plsply', '$tqty','$rem1', '$rem2')") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `voucher`='$voucher', `tgodwn`='$godown', `vehicle`='$vehicle',
                                           `transmode`='$transmode', `plsply`='$plsply', `totqty`='$tqty',`rem1`='$rem1', `rem2`='$rem2' WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        }

        mysqli_query($conn, "UNLOCK TABLES")or die(mysqli_error($conn));
        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);

    $getgo = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`= '$get[tgodwn]'") or die(mysqli_error($conn));
    $getgodown = mysqli_fetch_array($getgo);
}


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

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$stkdbprod", "$delcond", "$file?mode=last");

    mysqli_query($conn, "UNLOCK TABLES")or die(mysqli_error($conn));
}

include("includes/linkshead.php");
?>

<script src="MyScript/search_item.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/midelv.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/midelv_search.js" 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: text-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">
                        <h3 class="panel-title" style="color:#78909C"><a class="myPrint"><?php Echo $formname; ?></a></h3>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" name="vouchno" class="form-control form voucher" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" tabindex="-1" readonly>
                                <input type="hidden" name="monno" class="form-control form monno" value="<?php echo ($addflag != 1) ? $get['monno'] : '' ?>" >
                                <input type="hidden" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : '' ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-8 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt"  value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus style="width: 110%">
                                   <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                            <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                            </div>


                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">To Godown</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-1 col-xs-1 pad">
                                <select name="tgodwn" class="form-control form godown select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $getgodown['code'] : " "; ?>"><?php echo ($addflag != 1) ? $getgodown['gcode'] : " "; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT * FROM `godown`") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value=$trow[code]>$trow[gcode]</option>";
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="col-lg-2 col-md-1 text-right">
                                <label class="label-control marg">Transport Mode</label>
                            </div>
                            <div class="col-lg-2 col-md-2" >
                                <select name="transmode" class="form-control form transmode select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['transmode'] : " "; ?>"><?php echo ($addflag != 1) ? $getgodown['gcode'] : " "; ?></option>
                                    <?php
                                    $gtrans = mysqli_query($conn, "SELECT `code`,`name` FROM `transport`") or die(mysqli_error($conn));
                                    while ($trans = mysqli_fetch_array($gtrans)) {
                                        echo "<option value=$trans[code]>$trans[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Vehicle No.</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" name="vehicle" class="form-control form vhclno" value="<?php echo ($addflag != 1) ? $get['vehicle'] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Place Of Supply</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-1 col-xs-1 pad">
                                <input type="text" name="plsply" class="form-control form plsply" value="<?php echo ($addflag != 1) ? $get['plsply'] : ''; ?>">
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table-striped table-condensed table-bordered" width="100%">
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th style="width: 2%;"></th>
                                        <th style="width: 3%;">Sr.</th>
                                        <th style="width: 10%;">Barcode</th>
                                        <th style="width: 10%;">Item Code</th>
                                        <th style="width: 25%;">Decription Of Goods</th> 
                                        <th style='width: 10%;'>Hsncode</th>
                                        <th style="width: 15%;">Mrp</th> 
                                        <th style='width: 10%;'>Net Wt.</th>
                                        <th style='width: 10%;'>Batch No.</th>
                                        <th style='width: 10%;'>Qty</th>

                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch'  ORDER BY srno") or die(mysqli_error($conn));
                                        $ctr = 0;
                                        while ($row = mysqli_fetch_array($qry)) {

                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' name='srno_$ctr' class='srno form-control  form'  value='$row[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<input type='text' name='barcode_$ctr' class='barcode form-control form' value='$row[barcode]' required>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<input type='text' name='prodcode_$ctr' class='itemcode form-control form' value='$row[prodcode]' required>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<input type='text' name='descrip_$ctr' required='true' class='itemcode form-control  form' value='$row[descrip]' readonly required>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' name='hsncode_$ctr' class='hsncode form-control   form' value='$row[hsncode]' ></td>";
                                            Echo "<td>";
                                            Echo "<input type='text' name='mrp_$ctr' required='true' class='mrp form-control  form' value='$row[mrp]' readonly required>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<input type='text' name='netwt_$ctr' required='true' class='gram form-control  form' value='$row[netwt]' readonly required>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' name='batchno_$ctr' class='batch form-control  form' value='$row[batchno]' readonly></td>";
                                            Echo "<td><input type='text' name='qty_$ctr' class='qty form-control  form' value='$row[qty]' readonly></td>";

                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>                                    
                                            <td><i class='fa fa-close remove'></i></td>
                                            <td><input type='text' name='srno_0' class='srno form-control form' value='1'></td>
                                            <td><input type='text' name='barcode_0' class='barcode  form-control form' value='' required></td> 
                                            <td><input type='text' name='prodcode_0' class='itemcode  form-control form' value='' required></td> 
                                            <td>
                                                <input type='text' name='descrip_0' required='true' class='descrip form-control form' value='' required>
                                            </td>
                                            <td><input type='text' name='hsncode_0' class='hsncode form-control form' value='' ></td>
                                            <td>
                                                <input type='text' name='mrp_0' required='true' class='mrp form-control form' value='' readonly required>
                                            </td>
                                            <td>
                                                <input type='text' name='netwt_0' required='true' class='gram form-control form' value='' readonly required>
                                            </td>
                                            <td><input type='text' name='batchno_0' class='batch form-control form' value='' required></td>
                                            <td><input type='text' name='qty_0' class='qty form-control form' value='' required></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 class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-6 col-xs-12 pad">
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Remarks</label>
                                    </div>
                                    <div class="col-lg-9 pad">
                                        <input type="text" name="remark1" class="form-control form " value="<? Echo ($addflag != 1) ? $get["rem1"] : ""; ?>">
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right"></div>
                                    <div class="col-lg-9 pad">
                                        <input type="text" name="remark2" class="form-control form " value="<? Echo ($addflag != 1) ? $get["rem2"] : ""; ?>">
                                    </div>
                                </div>
                            </div>

                            <div class="col-lg-6 col-xs-12 pad">
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Total Qty.</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="number" step="any" name="totqty" class="form-control form text-right totqty" value="<?php echo ($addflag != 1) ? $get['totqty'] : ""; ?>" readonly>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </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>
    </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">Product Code</option>
                                <option value="2">Hsncode</option>
                                <option value="3">Mrp</option>
                                <option value="4">Net Wt.</option>
                                <option value="5">Unit</option>
                                <option value="6">Rate</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="15%">Item Code</th>
                                    <th width="40%">Item Name</th>
                                    <th width="15%">Barcode</th>
                                    <th width="10%">Net Wt.</th>
                                    <th width="10%">Unit</th>
                                    <th width="10%">Rate</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 $para ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher</option>  
                                <option value="2">Godown</option>
                                <option value="3">Transport Mode</option>
                                <option value="4">Vehicle No.</option>
                                <option value="5">Place of Supply</option>
                                <option value="6">Total Qty.</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">Voucher</option>  
                                <option value="2">Godown</option>
                                <option value="3">Transport Mode</option>
                                <option value="4">Vehicle No.</option>
                                <option value="5">Place of Supply</option>
                                <option value="6">Total Qty.</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="13%">Voucher</th>
                                    <th width="10%">Godown</th>
                                    <th width="16%">Transport Mode</th>
                                    <th width="13%">Vehicle No.</th>
                                    <th width="20%">Place of Supply</th>
                                    <th width="15%">Total Qty.</th>
                                    <th width="13%">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>
</html>