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

$formname = 'Debitnote/Purchase Returns';
$maindb = 'debmain';
$trandb = 'debtran';
$stkdb = 'transacn';
$accdb = 'actran';
$printfile = "debnote_print.php";
$file = 'debnote.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "PR";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $invno = $_POST["invno"];
    $vouchdt = $_POST["vouchdt"];
    $party = $_POST["party"];
    $type = $_POST["type"];
    $godown = $_POST["godown"];
    $orderno = $_POST["orderno"];
    $orderdt = $_POST["orderdt"];
//    $gtype = $_POST["gtype"];
    $gtype = "G";
    
    $subtotal = $_POST["subtotal"];
    $discperc = $_POST["discperc"];
    $discount = $_POST["discount"];
    $frgperc = $_POST["frgperc"];
    $freight = $_POST["freight"];
    $netamt = $_POST["netamt"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];

    $count = $_POST["items"];

    $salecode = '12';
    $mvouch = $qvouch;

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND vouchno != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?$keyfld=$qvouch&mode=last");
        }
    }

    if ($a == 1) {
        #################################TRAN PART #################################
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        #################################STOCK PART #################################
        mysqli_query($conn, "DELETE FROM `dumstock`WHERE `vouchno`='$mvouch' AND `source`='$source' ")or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `$keyfld`='$mvouch' AND `source`='$source'")or die(mysqli_error($conn));

        for ($i = 0; $i < $count; $i++) {
            $yearno = $myyear;
            $group = $source . $mvouch;
            $ord = "3";
            $msrno = $_POST["srno_$i"];
            $mbrand = $_POST["brand_$i"];
            $mitem = $_POST["item_$i"];
            $mbatch = $_POST["batch_$i"];
            $mactbatch = $_POST["actbatch_$i"];
            $mtaka = $_POST["taka_$i"];
            $macttaka = $_POST["acttaka_$i"];
            $mqty = $_POST["qty_$i"];
            $mtotqty = $_POST["totqty_$i"];
            $mflag = $_POST["flag_$i"];
            $mflagqty = $_POST["flagqty_$i"];
            $minvqty = $_POST["invqty_$i"];
            $mrate = $_POST["rate_$i"];
            $mamount = $_POST["amount_$i"];

            if (!empty($mitem)) {
                $getmitemname = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$mitem'") or die(mysqli_error($conn));
                $gmitemname = mysqli_fetch_array($getmitemname);
                $mitemname = $gmitemname['name'];
                $mshade = $gmitemname['shade'];

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`,`vouchdt`, `srno`, `brand`, `itemcode`,`batch`,`actbatch`, `taka`,`acttaka`, `qty`, `totqty`, `flag`, `flagqty`, `invqty`, `rate`, `amount`)
                            VALUES('$mvouch','$vouchdt', '$msrno','$mbrand', '$mitem', '$mbatch','$mactbatch', '$mtaka','$macttaka', '$mqty', '$mtotqty', '$mflag', '$mflagqty','$minvqty', '$mrate', '$mamount')") or die(mysqli_error($conn));

                $mtaka = $_POST["taka_$i"] * -1;
                $macttaka = $_POST["acttaka_$i"] * -1;
                $mqty = $_POST["qty_$i"] * -1;
                $mtotqty = $_POST["totqty_$i"] * -1;
                $mflag = $_POST["flag_$i"] * -1;
                $mflagqty = $_POST["flagqty_$i"] * -1;
                $minvqty = $_POST["invqty_$i"] * -1;

                mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, `group`, `source`, `type`,`vouchno`,`vouchdt`,
                            `srno`, `brand`, `itemcode`,`itemname`, `batch`, `shade`, 
                            `taka`, `qty`, `totqty`, `flag`, `flagqty`, `actqty`, `godown`, `actbatch`,`ord`)
                     VALUES('$yearno','$group','$source','$type','$mvouch','$vouchdt', '$msrno','$mbrand', 
                            '$mitem', '$mitemname', '$mbatch', '$mshade', '$mtaka', '$mqty', '$mtotqty', 
                            '$mflag', '$mflagqty','$minvqty', '$godown', '$mactbatch','$ord')") or die(mysqli_error($conn));
            }
        }

        ############################### Accounts Part ######################################################
//        IF ($chkledg == 1) {
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mvouch' AND `source` = '$source'") OR die(mysqli_error($conn));
        if ($netamt > 0) {
            $minus_amt = ($netamt * -1);

            // sale part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$salecode','004', '$netamt')");

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$party','009', '$minus_amt')");
        }


        ############################## MAIN ######################################################

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `invno`, `vouchdt`, `party`, `type`, `godown`,`orderno`,`orderdt`,`gtype`,
                                            `subtotal`,`discperc`,`discount`,`frgperc`,`freight`,`netamt`, `remark1`, `remark2`)
                                            VALUES($qvouch, '$invno', '$vouchdt', '$party', '$type', '$godown','$orderno','$orderdt','$gtype',
                                            '$subtotal','$discperc','$discount','$frgperc','$freight','$netamt','$remark1','$remark2')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchno`='$mvouch', `invno`='$invno', `party`='$party', `type`='$type',`orderno`='$orderno',`orderdt`='$orderdt',`gtype`='$gtype',
                        `godown`='$godown',`subtotal`='$subtotal',`discperc`='$discperc',`discount`='$discount',
                        `frgperc`='$frgperc',`freight`='$freight',`netamt`='$netamt',`remark1`='$remark1',`remark2`='$remark2' WHERE `vouchno`= '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

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

    $findtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$get[type]'") or die(mysqli_error($conn));
    $ltype = mysqli_fetch_array($findtype);

    $findparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$get[party]'") or die(mysqli_error($conn));
    $lparty = mysqli_fetch_array($findparty);
}

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

if (isset($_GET["delete"])) {
    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, "$stkdb", "$delcond", "$file?mode=last");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");
}
if (isset($_GET["cancel"])) {
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "dumstock", "$delcond", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/debnote.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'>
        <div class="col-lg-6 pad text-center" id="overlay_form" style="display:none;z-index:999;">
            <button type="button" id="close_form" style="float: right;font-weight: bold;font-size: 18px;color:red;" ><i class="fa fa-close"></i></button>
            <div style='width:100%;' id='browdata'>
            </div>
        </div>
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <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-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Invoice No.</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form focusfield" name="invno" value="<?php Echo ($addflag != 1) ? $get["invno"] : " $qvouch"; ?>"  autofocus>
                            </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" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Party</label>
                            </div>
                            <div class="col-lg-4 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2" name="party" style="width: 90%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["party"] : ""; ?>"><?php Echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `grpcode`='009'")or die(mysqli_error($conn));
                                    while ($party = mysqli_fetch_array($gparty)) {
                                        Echo "<option value='$party[accode]'>$party[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">Order No.</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form orderno" value="<?php Echo ($addflag != 1) ? $get["orderno"] : ""; ?>" name="orderno">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Order 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 orderdt" value="<?php Echo ($addflag != 1) ? $get["orderdt"] : date("Y-m-d"); ?>" name="orderdt">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2" name="type" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["type"] : ""; ?>"><?php Echo ($addflag != 1) ? $ltype["name"] : ""; ?></option>
                                    <?php
                                    $gtype = mysqli_query($conn, "SELECT * FROM `type`")or die(mysqli_error($conn));
                                    while ($type = mysqli_fetch_array($gtype)) {
                                        Echo "<option value='$type[code]'>$type[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Godown</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 godown" name="godown" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["godown"] : ""; ?>"><?php Echo ($addflag != 1) ? $get["godown"] : ""; ?></option>
                                    <?php
                                    $ggodown = mysqli_query($conn, "SELECT * FROM `godown`")or die(mysqli_error($conn));
                                    while ($godown = mysqli_fetch_array($ggodown)) {
                                        Echo "<option value='$godown[gocode]'>$godown[gocode]</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">Goods Type</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2" name="gtype" style="width:100%" required>
                                    <?php
/*                                    if ($addflag != 1) {
                                        if ($get[gtype] == "R") {
                                            Echo "<option value='R'>GOODS RETURN</option>";
                                            Echo "<option value='O'>OTHERS</option>";
                                        } elseif ($get[gtype] == "O") {
                                            Echo "<option value='O'>OTHERS</option>";
                                            Echo "<option value='R'>GOODS RETURN</option>";
                                        }
                                    } else {
                                        Echo "<option value=''></option>";
                                        Echo "<option value='R'>GOODS RETURN</option>";
                                        Echo "<option value='O'>OTHERS</option>";
                                    }*/
                                    ?>
                                </select>
                            </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-heading">
                        <!--                        <h3 class="panel-title">Item Details</h3>-->
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered" width="100%">
                                <thead  style="background: #1ABC9C;">
                                    <tr>
                                        <th width="1%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="12%" class='text-left'>Brand</th>
                                        <th width="12%">Item</th>
                                        <th width="4%" class='text-right'>Batch</th>
                                        <th width="4%" class='text-right'>Act Batch</th>
                                        <th width="4%" class='text-right'>Taka</th>
                                        <th width="4%" class='text-right'>Act Taka</th>
                                        <th width="4%" class='text-right'>Qty.</th>
                                        <th width="4%" class='text-right'>Tot. Qty.</th>
                                        <th width="4%" class='text-right'>Flag</th>
                                        <th width="4%" class='text-right'>Flag Qty.</th>
                                        <th width="4%" class='text-right'>Inv. Qty.</th>
                                        <th width="5%" class='text-right'>Rate</th>
                                        <th width="6%" class='text-right'>Amount</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[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);
                                            $findbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'") or die(mysqli_error($conn));
                                            $lbrand = mysqli_fetch_array($findbrand);
                                            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>";
                                            Echo "<select class='form-control form brand select2' name='brand_$ctr' style='width:100%'>";
                                            Echo "<option value='$lbrand[code]'>$lbrand[name]</option>";
                                            $gbrand = mysqli_query($conn, 'SELECT * FROM `brand`')or die(mysqli_error($conn));
                                            while ($brand = mysqli_fetch_array($gbrand)) {
                                                Echo "<option value='$brand[code]'>$brand[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<select class=' select2 form-control form item' name='item_$ctr' style='width:100%'>";
                                            Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                            $gitem = mysqli_query($conn, 'SELECT * FROM `item`')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 batch'     name='batch_$ctr'       value='$tran[batch]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right actbatch'  name='actbatch_$ctr'    value='$tran[actbatch]' readonly></td>";
                                            Echo "<td><input type='text' class='form-control form text-right taka'      name='taka_$ctr'        value='$tran[taka]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right acttaka'   name='acttaka_$ctr'     value='$tran[acttaka]'></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 text-right totqty'    name='totqty_$ctr'      value='$tran[totqty]' readonly><input type='hidden' class='dtotqty' name='dtotqty_$ctr' value='$tran[dtotqty]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right flag'      name='flag_$ctr'        value='$tran[flag]'><input type='hidden' class='dflag'      name='dflag_$ctr' value='$tran[dflag]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right flagqty'   name='flagqty_$ctr'     value='$tran[flagqty]' readonly><input type='hidden' class='dflagqty'   name='dflagqty_$ctr'     value='$tran[dflagqty]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right invqty'    name='invqty_$ctr'      value='$tran[invqty]' 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]'></td>";
                                            Echo "</tr >";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove"></i></td>
                                            <td><input type="text" class="form-control form text-right srno" name="srno_0" value="1"></td>
                                            <td>
                                                <select class="form-control form brand select2" name="brand_0" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gbrand = mysqli_query($conn, "SELECT * FROM `brand`")or die(mysqli_error($conn));
                                                    while ($brand = mysqli_fetch_array($gbrand)) {
                                                        Echo "<option value='$brand[code]'>$brand[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td>
                                                <select class="form-control form item select2" name="item_0" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gitem = mysqli_query($conn, "SELECT * FROM `item`")or die(mysqli_error($conn));
                                                    while ($item = mysqli_fetch_array($gitem)) {
                                                        Echo "<option value='$item[code]'>$item[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form text-left  batch"    name="batch_0"      value=""></td>
                                            <td><input type="text" class="form-control form text-left  actbatch" name="actbatch_0"   value="" readonly></td>
                                            <td><input type="text" class="form-control form text-right taka"     name="taka_0"       value="0"></td>
                                            <td><input type="text" class="form-control form text-right acttaka"  name="acttaka_0"    value="0"></td>
                                            <td><input type="text" class="form-control form text-right qty"      name="qty_0"        value="0"></td>
                                            <td><input type="text" class="form-control form text-right totqty"   name="totqty_0"     value="0" readonly required><input type="hidden" class="dtotqty"   name="dtotqty_0"></td>
                                            <td><input type="text" class="form-control form text-right flag"     name="flag_0"       value="0" ><input type="hidden" class="dflag" name="dflag_0"></td>
                                            <td><input type="text" class="form-control form text-right flagqty"  name="flagqty_0"    value="0" readonly><input type="hidden" class="dflagqty" name="dflagqty_0"></td>
                                            <td><input type="text" class="form-control form text-right invqty"   name="invqty_0"     value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right rate"     name="rate_0"       value="0"></td>
                                            <td><input type="text" class="form-control form text-right amount"   name="amount_0"     value="0"></td>
                                        </tr>
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad">
<!--                            <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />-->
                            <button type="button" class="btn btn-success btn-square form"  id="addnew">
                                <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 : 1; ?>" />  
                        </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-heading">
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row text-right pad">
                            <div class="col-lg-1 col-xs-1 text-right pad">
                                <label class='label-control marg'>Remark</label>
                            </div>
                            <div class="col-lg-6 col-xs-6 text-right pad">
                                <input type='text' class='form-control form remark1' name='remark1' value='<?php Echo ($addflag != 1) ? $get[remark1] : ''; ?>'>
                            </div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg">SubTotal</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <input type="text" class="form-control form subtotal text-right" value="<? Echo ($addflag != 1) ? $get[subtotal] : ""; ?>" name="subtotal" readonly>
                            </div>
                        </div>
                        <div class="row text-right pad">
                            <div class="col-lg-1 col-xs-1 text-right pad">
                                <label class='label-control marg'>&nbsp;</label>
                            </div>
                            <div class="col-lg-6 col-xs-6 text-right pad">
                                <input type='text' class='form-control form remark2' name='remark2' value='<?php Echo ($addflag != 1) ? $get[remark2] : ''; ?>'>
                            </div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Less Discount</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <div class="col-lg-5 pad">
                                    <input type="text" class="form-control form discperc text-right" value="<? Echo ($addflag != 1) ? $get[discperc] : ""; ?> "  name="discperc" style="width: 80%">
                                </div>
                                <div class="col-lg-1 pad text-left">
                                    <label class="label-control marg">%</label>
                                </div>
                                <div class="col-lg-6 pad">
                                    <input type="text" class="form-control form discount text-right" value="<? Echo ($addflag != 1) ? $get[discount] : ""; ?>" name="discount" readonly>
                                </div>
                            </div>
                        </div>
                        <div class="row text-right pad">
                            <div class="col-lg-4 col-xs-4 text-right pad"></div>
                            <div class="col-lg-3 col-xs-3 text-right pad"></div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Add Freight</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <div class="col-lg-5 pad">
                                    <input type="text" class="form-control form frgperc text-right" value="<? Echo ($addflag != 1) ? $get[frgperc] : ""; ?>"  name="frgperc" style="width: 80%">
                                </div>
                                <div class="col-lg-1 pad text-left">
                                    <label class="label-control marg">%</label>
                                </div>
                                <div class="col-lg-6 pad">
                                    <input type="text" class="form-control form freight text-right" value="<? Echo ($addflag != 1) ? $get[freight] : ""; ?>" name="freight" readonly>
                                </div>
                            </div>
                        </div>
                        <div class="row text-right pad">
                            <div class="col-lg-4 col-xs-4 text-right pad"></div>
                            <div class="col-lg-3 col-xs-3 text-right pad"></div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Net Amount(Rs.)</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <input type="text" class="form-control form netamt text-right" value="<? Echo ($addflag != 1) ? $get[netamt] : ""; ?>" name="netamt" readonly>
                            </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/buttsale.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr style="background-color: #1ABC9C;">
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Party</th>
                                    <th>Type</th>
                                    <th>Godown</th>
                                    <th>Discount</th>
                                    <th>Freight</th>
                                    <th>Net Amount</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        $getsparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$row[party]'")or die(mysqli_error($conn));
                                        $sparty = mysqli_fetch_array($getsparty);
                                        $getstype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$row[type]'")or die(mysqli_error($conn));
                                        $stype = mysqli_fetch_array($getstype);
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='right'>$row[vouchno]</td>";
                                        echo "<td align='right'>$row[vouchdt]</td>";
                                        echo "<td align='left'>$sparty[name]</td>";
                                        echo "<td align='left'>$stype[name]</td>";
                                        echo "<td align='left'>$row[godown]</td>";
                                        echo "<td align='right'>$row[discount]</td>";
                                        echo "<td align='right'>$row[freight]</td>";
                                        echo "<td align='right'>$row[netamt]</td>";
                                        echo "<td align='center'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
