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

$formname = 'Transfer';
$maindb = 'purmain';
$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 accmast WHERE accode='$row[party]' ") 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="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="7%">Barcode</th>
                                        <th width="15%">Itemname</th>
                                        <th width="6%" class='text-right'>Qty.</th>
                                        <th width="9%">Unit</th>
                                        <th width="8%" class='text-right'>MRP.</th>
                                        <th width="8%" class='text-right'>Rate</th>
                                        <th width="8%" class='text-right'>Amount</th>
                                        <th width="8%" class='text-right'>Discperc</th>
                                        <th width="8%" class='text-right'>Discount</th>
                                        <th width="8%" class='text-right'>Total</th>
                                        <th width="8%" class='text-right'>MSP</th>
                                    </tr>
                                </thead>

                                <?php
                                if ($addflag != 1) {
                                    $ctr = 0;
                                    $gettran = mysqli_query($conn, "SELECT * FROM `purtran` WHERE `vouchno`='$row[vouchno]' ORDER BY `srno`")or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {
                                        $finditem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                        $fitem = mysqli_fetch_array($finditem);

                                        $findunit = mysqli_query($conn, "SELECT * FROM `unit` WHERE `code`='$tran[unit]'") or die(mysqli_error($conn));
                                        $funit = mysqli_fetch_array($findunit);

                                        Echo "<tr>";
                                        Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                        Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$tran[srno]'></td>";
                                        Echo "<td><input type='text' class='form-control form itemcode ' name='barcode_$ctr' value='$tran[itemcode]'></td>";
                                        Echo "<td><input type='text' class='form-control form itemname ' name='itemname_$ctr' value='$fitem[name]'></td>";
//                                        Echo "<td>";
//                                        Echo "<select class='form-control form fetchpart_$ctr item' name='item_$ctr' style='width:100%'>";
//                                        Echo "<option value='$fitem[code]'>$fitem[name]</option>";
//                                        $gitem = mysqli_query($conn, 'SELECT * FROM `item` order by name')or die(mysqli_error($conn));
//                                        while ($item = mysqli_fetch_array($gitem)) {
//                                            Echo "<option value='$item[code]'>$item[name]</option>";
//                                        }
//                                        Echo "</select>";
//                                        Echo "</td>";
                                        Echo "<td><input type='text' class='form-control form text-right qty'  name='qty_$ctr'    value='$tran[qty]'></td>";
                                        Echo "<td><input type='text' class='form-control form unit'            name='unit_$ctr'   value='$funit[name]'></td>";
//                                        Echo "<td>";
//                                        Echo "<select class='form-control form fetchpart_$ctr unit select2' name='unit_$ctr' style='width:100%'>";
//                                        Echo "<option value='$tran[unit]'>$tran[unit]</option>";
//                                        $gunit = mysqli_query($conn, 'SELECT * FROM `unit` order by name')or die(mysqli_error($conn));
//                                        while ($unit = mysqli_fetch_array($gunit)) {
//                                            Echo "<option value='$unit[name]'>$unit[name]</option>";
//                                        }
//                                        Echo "</select>";
//                                        Echo "</td>";
                                        Echo "<td><input type='text' class='form-control form text-right mrp'       name='mrp_$ctr'        value='$tran[mrp]' readonly></td>";
                                        Echo "<td><input type='text' class='form-control form text-right rate'      name='rate_$ctr'        value='$tran[rate]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right amount'    name='amount_$ctr'      value='$tran[amount]' readonly></td>";
                                        Echo "<td><input type='text' class='form-control form text-right discperc'  name='discperc_$ctr'      value='$tran[discperc]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right discount'  name='discount_$ctr'      value='$tran[discount]' readonly></td>";
                                        Echo "<td><input type='text' class='form-control form text-right total'     name='total_$ctr'      value='$tran[total]' readonly></td>";
                                        Echo "<td><input type='text' class='form-control form text-right msp'       name='msp_$ctr'      value='$tran[msp]'></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>
