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

$formname = 'Transfer';
$maindb = 'transacn';
$trandb = '';
$printfile = "";
$file = '';
$trandbexist = 0;
$grid = 'autotran.php';
$keyfld = "vouchno";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["exit"])) {
    header("refresh: 1; $grid?mode=last");
}

if ($addflag != 1) {

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

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

    $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE code='$row[compcode]' ") or die(mysqli_error($conn));
    $fetcomp = mysqli_fetch_array($getcomp);
}

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<!--<script src="MyScript/instalot_edit.js" type="text/javascript"></script>-->
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- 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:#1ABC9C"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </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-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control " id="vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $row["vouchno"] : "$qvouch"; ?>" readonly>
                            </div>
                            <div class="col-lg-2 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" style="padding-left: 0px;">
                                <input type="date" class="form-control" name="vouchdt" value="<?php Echo ($addflag != 1) ? $row["vouchdt"] : date('Y-m-d'); ?>" readonly="readonly">
                            </div> 
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">From</label>
                            </div>
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control mobile" style="" name="compcode" value="<?php Echo ($addflag != 1) ? $fetcomp["name"] : ""; ?>" <?php if ($addflag != 1) { ?> readonly <?php } ?>>
                            </div>
                        </div>
                        <div class="">
                            <table id="data" align="center" border="1px" width="100%" class="table-bordered">
                                <thead>
                                    <tr style="background-color:#78909C;">
                                        <th width="10%" class=""><label class="control-label">Sr.</label></th>
                                        <th width="20%" class=""><label class="control-label">Barcode</label></th>
                                        <th width="40%" class=""><label class="control-label">Item</label></th>
                                        <th width="10%" class=""><label class="control-label">Unit</label></th>
                                        <th width="20%" class="text-right"><label class="control-label">Qty.</label></th>
                                    </tr>
                                </thead>

                                <?php
                                if ($addflag != 1) {
                                    $qry = mysqli_query($conn, "SELECT * FROM  `$maindb` WHERE vouchno='$qvouch' and `source`='TR' and `compcode`!='$compcd' ORDER BY srno") or die(mysqli_error($conn));
                                    $rowqry = mysqli_num_rows($qry);
//                                    browse($rowqry);
                                    $ctr = 0;

//                                    if ($rowqry > 0) {
                                    while ($row = mysqli_fetch_array($qry)) {
                                        $getitem = mysqli_query($conn, "SELECT * FROM  `item` WHERE code='$row[itemcode]'") or die(mysqli_error($conn));
                                        $itemn = mysqli_fetch_array($getitem);

                                        $getunit = mysqli_query($conn, "SELECT * FROM  `unit` WHERE code='$row[unit]'") or die(mysqli_error($conn));
                                        $fetunit = mysqli_fetch_array($getunit);


                                        echo "<tr >";
                                        Echo"<td><input type='text' class='form-control  srno'   name='srno_$ctr'     value='$row[srno]' readonly></td>";
                                        Echo"<td><input type='text' class='form-control barcode'   name='barcode_$ctr'     value='$row[barcode]' readonly></td>";
                                        Echo"<td><input type='text' class='form-control item'   name='item_$ctr'     value='$itemn[name]' readonly></td>";
//                                            Echo"<td>";
//                                            Echo"<select class='form-control item select2' name='item_$ctr' style='width:100%'>";
//                                            Echo"<option value='$itemn[code]'>$itemn[name]</option>";
//                                            $getmitem = mysqli_query($conn, "SELECT code, name FROM `item` order by name")or die(mysqli_error($conn));
//                                            while ($mitem = mysqli_fetch_array($getmitem)) {
//                                                Echo "<option value='$mitem[code]'>$mitem[name]</option>";
//                                            }
//                                            Echo"</select>";
//                                            Echo"</td>";
                                        Echo"<td><input type='text' class='form-control unit'                name='unit_$ctr'   value='$fetunit[name]' readonly></td>";
                                        Echo"<td><input type='text' class='form-control text-right qty'      name='qty_$ctr'    value='$row[qty]' readonly></td>";
                                        echo "</tr>";
                                        $ctr++;
                                    }
                                }
//                                } 
                                ?>
                            </table>
                            <div class="col-lg-12 pad">
                                <!--                            <button type="button"  class="btn btn-success btn-square form"  id="addnew6">
                                                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                                                            </button>-->

                                <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : ""; ?>" />  
                            </div>
                            <!--                        <label class="control-label col-lg-3"></label>
                                                    <div class="col-lg-8">
                                                        <button class="btn btn-lg btn-success form" data-toggle="modal" data-target=".slacker-modal" style="width: 100%;">Detail</button>
                                                    </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 style="padding-left: 40%;" class="panel-footer">
                        <a href="autotran_print.php?<?= $keyfld . "=" . $qvouch; ?>" target="_blank" class="btn btn-primary " >Print</a>

                        <!--<input type="submit"  name="submit" id="submit" value="Update" class="btn btn-success" >-->
                        <input type="submit" name="exit" value="Exit" id="exit" class="btn btn-danger" onclick="">
                    </div>
                </div>
            </div>
        </div>
    </form>

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