<?php
ob_start();
ini_set('display_errors', 1);
error_reporting(E_ERROR);

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

$maindb = 'onlmain';
$trandb = 'onltran';
$accdb = '';
$imgdb = "";
//$printfile = "purchase_mi_print.php";
$file = 'onlent.php';

$trandbexist = 1;
$comp = $_SESSION["comp_" . $dbname];
$myyear = $_SESSION[$myyear];

$gtype = $_GET['type'];
$formname = 'Online Entry';
$source = "OL";
$stkdb = 'transacn';

$para = "";
$keycond = "";
$keyfld = "vouchno";
$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"]);
    $ttotqty = $_POST["ttotqty"];
    $rem1 = trim($_POST["remark1"]);
    $rem2 = trim($_POST["remark2"]);

    $mvouch = $qvouch;

    $oms = $_POST['oms'];

    $count = $_POST["items"];

    if ($a == 1) {

        // Begin the transaction
        // Lock tables for the transaction
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, $stkdb WRITE ");

        try {
            mysqli_query($conn, "START TRANSACTION");

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

            // Delete old entries
            mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));
            mysqli_query($conn, "DELETE FROM `$stkdb` WHERE vouchno='$mvouch' and source ='$source'") or die(mysqli_error($conn));
            $cnt = 0;
            $cntamt = 0;

            // Insert new transaction items
            for ($i = 0; $i < $count; $i++) {
                $msrno = $_POST["srno_$i"];
                $barcode = $_POST["barcode_$i"];
                $mitem = mysqli_real_escape_string($conn, $_POST["itemcode_$i"]);
                $batch = $_POST["batch_$i"];
                $qty = $_POST["qty_$i"];
                $unit = $_POST["unit_$i"];
                $type = $_POST["type_$i"];
                $bbfmon = $_POST["bbfmon_$i"];
                $bbfyear = $_POST["bbfyear_$i"];
                $weight = $_POST["weight_$i"];
                $group = $source . $mvouch;

                if (!empty($mitem)) {
//                    browse($mitem);
                    $cnt++;

                    $mitemcode = mysqli_query($conn, "SELECT * FROM `product` WHERE `code`='$mitem'") or die(mysqli_error($conn));
                    $fitem = mysqli_fetch_array($mitemcode);
                    $stkunit = $fitem["stkunit"];

                    if ($batch == "") {
                        $batch = $fitem["batch"];
                    }
                    $bf = $fitem["bbf"];

                    if ($cost_post > 0) {
                        $cost = $cost_post;
                    } else {
                        $cost = $itcost;
                    }
                    $bbfmon = date('m', strtotime("+" . $bf . " months", strtotime($vouchdt)));
                    $bbfyear = date('Y', strtotime("+" . $bf . " months", strtotime($vouchdt)));
//                    $weight = $fitem["weight"];


                    $chars = preg_replace('/[^a-zA-Z]/', '', $weight);
                    if (strtoupper($chars) == 'G') {
                        $weignum = floatval($weight / 1000);
                    } else {
                        $weignum = floatval($weight);
                    }
//                    if($mitem == "165"){
//                        browse($weight);
//                        browse($weignum);
//                    }
//                $qty = $sz1;

                    if ($stkunit == 1) {
                        $stkqty = $qty * $weignum * 1000 * -1;
                        $mqty = $qty * $weignum * 1000;
                    } else {
                        $stkqty = $qty * $weignum * -1;
                        $mqty = $qty * $weignum;
                    }
//                $minqty = $qty * $weignum * -1;

                    $aqty = 0;
                    $stkqty = 0;
                    $actqty = $mqty;
                    $batchno = $batch;
                    $prodtype = $fitem['type'];
                    if ($prodtype == "Organic") {
                        $stkdate = "2025-11-18";
                    } else {
                        $stkdate = "2026-01-14";
                    }

                    if ($vouchdt > $stkdate) {

                        $getstk = mysqli_query($conn, "SELECT itemcode, batch, sum(stkqty) as tqty FROM `transacn` WHERE `itemcode` =  '$mitem' 
                                                   GROUP BY itemcode, batch having sum(stkqty) > 0  order by `batchdate` ASC") or die(mysqli_error($conn));
//                        print "SELECT itemcode, batch, sum(stkqty) as tqty FROM `transacn` WHERE `itemcode` =  '$mitem' 
//                                                   GROUP BY itemcode, batch having sum(stkqty) > 0  ";
                        if (mysqli_num_rows($getstk) > 0) {
                            $batchno = "";
                            $sqty = 0;
                            $pqty = $mqty;
                            while ($fetstk = mysqli_fetch_array($getstk)) {
                                $batchno = $fetstk['batch'];
                                $tsqty = $fetstk['tqty'];

                                if ($mqty <= $tsqty) {
                                    $stqty = $mqty * -1;
                                    if ($stkunit == 1) {
                                        $minqty = $stqty / 1000;
                                    } else {
                                        $minqty = $stqty;
                                    }

                                    $nsrno++;
                                    mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `vouchno`, `vouchdt`, `barcode`, `itemcode`,  `cartqty`,
                                                  `qty`, `source`, `group` , `ordno`,`unit`,`stkqty`, batch)
                            VALUES('$cnt', '$myyear', '$mvouch', '$vouchdt','$barcode', '$mitem',  '$cartqty',
                                   '$minqty', '$source', '$group','1', '$stkunit','$stqty', '$batchno')") or die(mysqli_error($conn));

                                    $aqty = $aqty + $mqty;
                                    $mqty = $mqty - $fetstk['tqty'];
                                    break;
                                } else {

                                    $nsrno++;
                                    $stqty = $fetstk['tqty'] * -1;
                                    if ($stkunit == 1) {
                                        $minqty = $stqty / 1000;
                                    } else {
                                        $minqty = $stqty;
                                    }
                                    mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `vouchno`, `vouchdt`, `barcode`, `itemcode`,  `cartqty`,
                                                  `qty`, `source`, `group` , `ordno`,`unit`,`stkqty`, `batch`)
                            VALUES('$cnt', '$myyear', '$mvouch', '$vouchdt','$barcode', '$mitem',  '$cartqty',
                                   '$minqty', '$source', '$group','1', '$stkunit','$stqty', '$batchno')") or die(mysqli_error($conn));

                                    $aqty = $aqty + $fetstk['tqty'];
                                    $mqty = $mqty - $fetstk['tqty'];
                                }
                            }
                        } else {
                            $stqty = $mqty * -1;
                            $nsrno++;
                            mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `vouchno`, `vouchdt`, `barcode`, `itemcode`,  `cartqty`,
                                                  `qty`, `source`, `group` , `ordno`,`unit`,`stkqty`, `batch`)
                            VALUES('$cnt', '$myyear', '$mvouch', '$vouchdt','$barcode', '$mitem',  '$cartqty',
                                   '$minqty', '$source', '$group','1', '$stkunit','$stqty', '')") or die(mysqli_error($conn));

                            $aqty = $aqty + $mqty;
                        }

                        if ($aqty != $actqty) {
                            $stkqty = $actqty - $aqty;

                            $stqty = $stkqty * -1;
                            $nsrno++;
                            mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `vouchno`, `vouchdt`, `barcode`, `itemcode`,  `cartqty`,
                                                  `qty`, `source`, `group` , `ordno`,`unit`,`stkqty`, `batch`)
                            VALUES('$cnt', '$myyear', '$mvouch', '$vouchdt','$barcode', '$mitem',  '$cartqty',
                                   '$minqty', '$source', '$group','1', '$stkunit','$stqty', '')") or die(mysqli_error($conn));

                            $aqty = $aqty + $mqty;
                            //                    browse("Not Mactched");
                        }

                        if ($pouch > 0) {
                            $mitemcode = mysqli_query($conn, "SELECT `code`,`barcode` FROM `product` WHERE `code`='$pouch'") or die(mysqli_error($conn));
                            $icode = mysqli_fetch_array($mitemcode);
                            $pbarcode = $icode['barcode'];
                            $pcqty = $qty * -1;
                            mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `vouchno`, `vouchdt`, `barcode`, `itemcode`,
                                                  `qty`, `source`, `group` , `ordno`,`unit`,`stkqty`)
                            VALUES('$cnt', '$myyear', '$mvouch', '$vouchdt','$pbarcode', '$pouch', 
                                   '$pcqty', '$source', '$group','1', '$stkunit','$pcqty')") or die(mysqli_error($conn));
                        }
                        $ttqty += $qty;
                    }

                    mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `itemcode`, `unitgm`, `qty`, `type`, `batchno`, `bbfmon`, `bbfyear`, `weight`)
                                                                      VALUES('$mvouch', '$vouchdt', '$cnt', '$mitem', '$unit', '$qty', '$type', '$batchno', '$bbfmon', '$bbfyear', '$weight')") or die(mysqli_error($conn));
                }
            }

            ######################### STOCK PART ####################################################
            // Insert or update the main voucher entry
            if ($addflag == 1) {
                mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `rem1`, `rem2`, `ttotqty`)
                                                                  VALUES('$mvouch', '$vouchdt', '$rem1', '$rem2', '$ttotqty')") or die(mysqli_error($conn));
            } else {
                mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `rem1`='$rem1', `rem2`='$rem2', `ttotqty`='$ttotqty' WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
            }

            // Commit the transaction after all queries are successful
            mysqli_query($conn, "COMMIT");

            // Unlock tables
            mysqli_query($conn, "UNLOCK TABLES");

            // Redirect based on the add flag
            if ($addflag == 1) {
                insmess();
                header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
            } else {
                updmess();
//                header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
            }
        } catch (Exception $e) {
            // Rollback the transaction if anything goes wrong
            mysqli_query($conn, "ROLLBACK");
            mysqli_query($conn, "UNLOCK TABLES");
            // Optionally, log the error
            browse("Transaction failed: " . $e->getMessage());
            // You can redirect or show an error message here
        }
    }
}

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

    $delcsh = $get['cashvouch'];

    $printdisabled = "none";
    $rd = "readonly";
}


$senable = "";
$cenable = "";

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

    $delcond2 = " vouchno = '$_GET[vouchno]' ";
    $delconds = " vouchno = '$_GET[vouchno]' and source = '$source'";

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

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

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

<script src="MyScript/onlent.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/search_onl_item.js?b=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/onlent_search.js?c=<?= mt_rand(); ?>" type="text/javascript"></script>
<style>
    .scrollTable {
        height: 300px;
        overflow-y: scroll;
        /* overflow: scroll; */
    }

    .noMarg {
        margin-top: 10px !important;
    }

    .skbtn {
        width: 100%;
        padding: 1px;
        font-size: 2.2em;
        line-height: initial;
    }
</style>
</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="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 ">
                <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">
                        <input type="date" value="<?= $zStartDate ?>" style="display: none;border: none"
                               class="zstdate">
                        <input type="date" value="<?= $zEndDate ?>" style="display: none;border: none" class="zenddate">
                        <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-8 col-sm-8 col-xs-8 pad">
                                <input type="hidden" class="form-control addflag" value="<?php echo $addflag ?>">
                                <input type="hidden" name="voucher" class="form-control form voucher"
                                       value="<?php echo ($addflag != 1) ? $get['voucher'] : '' ?>" tabindex="-1" readonly>
                                <input type="hidden" name="source" class="form-control form source"
                                       value="<?= $source; ?>" id="source">
                                <input type="hidden" name="monno" class="form-control form monno"
                                       value="<?php echo ($addflag != 1) ? $get['monno'] : '' ?>">
                                <input type="hidden" name="type" class="form-control form type gtype"
                                       value="<?php echo ($addflag != 1) ? $get['mattype'] : $gtype ?>">
                                <input type="text" name="vouchno" class="form-control form vouchno"
                                       value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" readonly>
                                <input type="hidden" class="form-control form" name="cashvouch"
                                       value="<?php echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>">
                                <input type="hidden" class="form-control form frmname" name="frmname" value="I">
                                <input type="hidden" name="rd" class="form-control form rd" value="1">


                            </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-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form vouchdt focusfield" name="vouchdt"
                                       value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d") ?>" autofocus
                                       <?= $rd ?>>
                            </div>
                        </div>

                        <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 ">

                            <div class="table-responsive" style='overflow-y:auto;height:400px'>
                                <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: 10%'>Code</th>
                                            <th style="width: 20%;">Item Name</th>
                                            <th style="width: 10%;">Type</th>
                                            <th style="width: 10%;">Batch</th>
                                            <th style="width: 10%;">BBF Mon</th>
                                            <th style="width: 10%;">BBF Year</th>

                                            <th style='width: 5%' class="text-right">Qty.</th>
                                            <th style='width: 5%' class="text-right">Weight</th>
                                            <!--<th style='width: 5%' class="text-left">Unit</th>-->
                                        </tr>
                                    </thead>
                                    <tbody class="tbody">
                                        <?php
                                        if ($addflag != 1) {

                                            // $countQry = mysqli_query($conn, "SELECT COUNT(*) as total FROM `$trandb` WHERE vouchno='$getvouch'");
                                            // $countResult = mysqli_fetch_assoc($countQry);
                                            // if ($countResult['total'] == 0) {
                                            //     echo "<tr><td colspan='11'>No records found</td></tr>";
                                            //     exit;
                                            // }
                                            // $products = [];
                                            // $productQry = mysqli_query($conn, "SELECT `code`, `name` FROM `product` ORDER BY `name`") or die(mysqli_error($conn));
                                            // while ($product = mysqli_fetch_array($productQry)) {
                                            //     $products[] = $product;
                                            // }
                                            // $qry = mysqli_query($conn, "SELECT a.srno, a.itemcode, a.type, a.batchno, a.bbfmon, a.bbfyear, a.qty, a.weight, 
                                            //                             b.code, b.name, b.barcode 
                                            //                             FROM `$trandb` a
                                            //                             LEFT JOIN product b ON a.itemcode = b.code 
                                            //                             WHERE a.vouchno='$getvouch' 
                                            //                             ORDER BY a.srno") or die(mysqli_error($conn));
                                            // $ctr = 0;
                                            // $output = '';
                                            // while ($row = mysqli_fetch_array($qry)) {
                                            //     $output .= "<tr>";
                                            //     $output .= "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            //     $output .= "<td><input type='text' name='srno_$ctr' class='srno form-control form' value='" . htmlspecialchars($row['srno']) . "'></td>";
                                            //     $output .= "<td><input type='text' name='barcode_$ctr' class='barcode form-control form' value='" . htmlspecialchars($row['barcode']) . "' readonly></td>";
                                            //     $output .= "<td><select name='itemcode_$ctr' class='form-control form select2 itemcode' style='width:100%' required>";
                                            //     $output .= "<option value='" . htmlspecialchars($row['code']) . "'>" . htmlspecialchars($row['name']) . "</option>";
                                            //     foreach ($products as $product) {
                                            //         $output .= "<option value='" . htmlspecialchars($product['code']) . "'>" . htmlspecialchars($product['name']) . "</option>";
                                            //     }
                                            //     $output .= "</select></td>";
                                            //     $output .= "<td><input type='text' name='type_$ctr' class='type form-control form' value='" . htmlspecialchars($row['type']) . "' required></td>";
                                            //     $output .= "<td><input type='text' name='batch_$ctr' class='batch form-control form' value='" . htmlspecialchars($row['batchno']) . "' required></td>";
                                            //     $output .= "<td><input type='number' name='bbfmon_$ctr' class='bbfmon form-control form' value='" . htmlspecialchars($row['bbfmon']) . "' required></td>";
                                            //     $output .= "<td><input type='number' name='bbfyear_$ctr' class='bbfyear form-control form' value='" . htmlspecialchars($row['bbfyear']) . "' required></td>";
                                            //     $output .= "<td><input type='number' step='any' name='qty_$ctr' class='qty form-control text-right form' value='" . htmlspecialchars($row['qty']) . "' required></td>";
                                            //     $output .= "<td><input type='text' step='any' name='weight_$ctr' class='weight form-control form text-right' value='" . htmlspecialchars($row['weight']) . "' required></td>";
                                            //     $output .= "</tr>";
                                            //     $ctr++;
                                            // }
                                            // echo $output;


                                            $qry = mysqli_query($conn, "SELECT a.*,b.code,b.name,b.barcode FROM `$trandb` a
                                                                                      LEFT JOIN product b ON a.itemcode = b.code 
                                                                                      WHERE a.vouchno='$getvouch' ORDER BY a.srno") or die(mysqli_error($conn));
                                            $ctr = 0;
                                            while ($row = mysqli_fetch_array($qry)) {

                                                // $getitem = mysqli_query($conn, "SELECT `code`, barcode, `name` FROM `product` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
                                                // $geti = mysqli_fetch_array($getitem);

                                                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><input type='text' name='barcode_$ctr' class='barcode form-control  form' value='$row[barcode]' readonly></td>";
                                                echo "<td>";
                                                echo "<select name='itemcode_$ctr' class='form-control form select2 itemcode' style='width:100%' required>";
                                                echo "<option value='$row[code]'>$row[name]</option>";
                                                // $mownitm = mysqli_query($conn, "SELECT `code`, `name` FROM `product` ORDER BY `name`") or die(mysqli_error($conn));
                                                // while ($trowitm = mysqli_fetch_array($mownitm)) {
                                                //     echo "<option value='$trowitm[code]'>$trowitm[name]</option>";
                                                // }
                                                echo "</td>";
                                                echo "<td><input type='text' name='type_$ctr' class='type form-control form' value='$row[type]' required></td>";
                                                echo "<td><input type='text' name='batch_$ctr' class='batch form-control form' value='$row[batchno]' ></td>";
                                                echo "<td><input type='number' name='bbfmon_$ctr' class='bbfmon form-control form' value='$row[bbfmon]' required></td>";
                                                echo "<td><input type='number' name='bbfyear_$ctr' class='bbfyear form-control form' value='$row[bbfyear]' required></td>";
                                                echo "<td><input type='number' step='any' name='qty_$ctr' class='qty form-control  text-right form' value='$row[qty]' required></td>";
                                                echo "<td><input type='text'   step='any' name='weight_$ctr' class='weight form-control form text-right' value='$row[weight]' required></td>";
                                                // echo "<td><input type='text'  name='unit_$ctr' class='unit form-control form' value='$row[unitgm]' required></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>
                                                    <select name="itemcode_0" class="form-control form itemcode select2" style="width:100%;" required>
                                                        <option value=""></option>
                                                        <?php
                                                        $mritm2 = mysqli_query($conn, "SELECT `code`, `name` FROM `product` ORDER BY `name`") or die(mysqli_error($conn));
                                                        while ($trowitm2 = mysqli_fetch_array($mritm2)) {
                                                            echo "<option value='$trowitm2[code]'>$trowitm2[name]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </td>
                                                <td><input type='text' name='type_0' class='type form-control form' value='' required></td>
                                                <td><input type='text' name='batch_0' class='batch form-control form' value='' required></td>
                                                <td><input type='number' name='bbfmon_0' class='bbfmon form-control form' value='' required></td>
                                                <td><input type='number' name='bbfyear_0' class='bbfyear form-control form' value='' required></td>
                                                <td><input type='number' step='any' name='qty_0' class='qty form-control  text-right form' value='0' required></td>
                                                <td><input type='text' step='any' name='weight_0' class='weight form-control form text-right' value='' required></td>
                                                <!--<td><input type='text' name='unit_0' class='unit 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-6 pad">

                                <div class="col-lg-7 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="<?= ($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="<?= ($addflag != 1) ? $get["rem2"] : ""; ?>">
                                        </div>
                                    </div>
                                </div>

                                <div class="col-lg-5 col-xs-12 pad">
                                    <div class="col-lg-12 col-xs-12 pad">
                                        <div class="col-lg-3 pad"></div>
                                        <div class="col-lg-1 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="ttotqty"
                                                   class="form-control form text-right ttotqty grossamt"
                                                   value="<?php echo ($addflag != 1) ? $get['ttotqty'] : "0"; ?>" readonly
                                                   tabindex="-1">
                                        </div>
                                        <div class="col-lg-2 pad"></div>
                                    </div>
                                </div>

                            </div>
                        </div>

                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <?php
                            if ($addflag != 1) {
                                $barqry = mysqli_query($conn, "Select * from `rejtran` where vouchno='$getvouch'");
                                if (mysqli_num_rows($barqry) > 0) {
                                    ?>
                                    <div class="table-responsive" style='overflow-y:auto;height:400px'>

                                        <table class="table table-bordered myTable">
                                            <thead class="mySearchTr">
                                            <th style="width:10%;">Sr.</th>
                                            <th style="width:80%;">Website Name</th>
                                            <th style="width:10%;">Qty.</th>
                                            </thead>
                                            <tbody class="tbody">
                                                <?php
                                                $ctr = 0;
                                                $mctr = 0;
                                                while ($fetbar = mysqli_fetch_array($barqry)) {
                                                    $mctr++;
                                                    echo "<tr>";
                                                    echo "<td>$mctr</td>";
                                                    echo "<td>$fetbar[lineitem]</td>";
                                                    echo "<td>$fetbar[qty]</td>";
                                                    echo "</tr>";
                                                    $ctr++;
                                                }
                                                ?>
                                            </tbody>

                                        </table>
                                    </div>
                                    <?php
                                }
                            }
                            ?>
                        </div>






                        <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 pad" style="margin-top: 10px;">
                            <?php include("includes/buttons.php"); ?>
                        </div>
                        <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad marg text-left" style="margin-top: 10px;">
                            <button <?php if ($addflag != 1) { ?>
                                    onclick="window.open('thermal_barcode.php?vouchno=<?= $qvouch ?>&vouchdt=<?= $get['vouchdt'] ?>', 'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes')" <?php
                                } else {
                                    echo "disabled";
                                }
                                ?>><i class="fa fa-2x fa-barcode skbtn"></i></button>
                        </div>

                        <div class="col-lg-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">
                        </div>

                    </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 noMarg">
            <div class="modal-header">
                <button class="close" data-dismiss="modal" style="font-size:20px"><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>
                        </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="12%">Barcode</th>
                                <th width="25%">Name</th>
                                <th width="8%">Type</th>
                                <th width="12%">Country Of Origin</th>
                                <th width="18%">Batch Code</th>
                                <th width="8%">(Best Before) Life</th>
                                <!--<th width="8%">Pcs Per Carton</th>-->
                                <!--<th width="7%">Unit</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">Voucher</option>
                            <option value="2">Date</option>
                            <!--<option value="3">Godown</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">Date</option>
                            <!--<option value="3">Godown</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="15%">Date</th>
                                <th width="20%" class="text-right">Total Qty.</th>
                                <th width="10%">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>