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

$formname = 'Sale';
$maindb = 'salemain';
$trandb = 'saletran';
$stkdb = 'transacn';
$accdb = 'actran';
$printfile = "sale_invoice_print.php";
$file = 'sale.php';
$trandbexist = 1;
$keyfld = "vouchno";
//$mgrpcode       = "SL";
$source = "SL";
$salecode = "15";

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

mysqli_select_db($conn, $zdb) or die(mysqli_error($conn));
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"];
    $broker = $_POST["broker"];
    $transporter = $_POST["transporter"];
    $lrno = $_POST["lrno"];
    $lrdate = $_POST["lrdate"];
    $type = $_POST["type"];
    $godown = $_POST["godown"];
    $orderno = $_POST["orderno"];
    $orderdt = $_POST["orderdt"];
    $gtype = $_POST["gtype"];
    $subtotal = $_POST["subtotal"];
    $discperc = $_POST["discperc"];
    $discount = $_POST["discount"];
    $frgperc = $_POST["frgperc"];
    $freight = $_POST["freight"];
    $netamt = $_POST["netamt"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $remark3 = $_POST["remark3"];
    $remark4 = $_POST["remark4"];
    $prnsuit = $_POST["prnsuit"];
    $tqty = $_POST["tqty"];
    $tweight = $_POST["tweight"];
    $prnbrand = $_POST["prnbrand"];
    $labour = $_POST["labour"];
    $tempo = $_POST["tempo"];
    $packing = $_POST["packing"];
    $allchrg = $_POST["allchrg"];

    $count = $_POST["items"];

    $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) {

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

        $group = $source . $mvouch;

        $tctr = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mbrand = $_POST["brand_$i"];
            $mitem = $_POST["item_$i"];
            $itcode = $_POST["itemcode_$i"];
            $mbatch = $_POST["batch_$i"];
            $mactbatch = $_POST["actbatch_$i"];
            $mtaka = $_POST["taka_$i"];
            $macttaka = $_POST["acttaka_$i"];
            $mbaleqty = $_POST["baleqty_$i"];
            $mqty = $_POST["qty_$i"];
            $containerno = $_POST["containerno_$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)) {
                $getbr = mysqli_query($conn, "SELECT code,name FROM `brand` WHERE `name`='$mbrand'") or die(mysqli_error($conn));
                if (mysqli_num_rows($getbr) > 0) {
                    $bcode = mysqli_fetch_array($getbr);
                    $brcode = $bcode['code'];
                } else {
                    $genbr = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM `brand` where code != '' LIMIT 0,1 ") or die(mysqli_error($conn));
                    if (mysqli_num_rows($genbr) > 0) {
                        $gbr = mysqli_fetch_assoc($genbr);
                        $brcode = $gbr["maxcode"] + 1;
                    } else {
                        $brcode = '1';
                    }
                    mysqli_query($conn, "INSERT INTO `brand`(`code`, `name`)
                           VALUES('$brcode', '$mbrand')") or die(mysqli_error($conn));
                }


                $getit = mysqli_query($conn, "SELECT barcode, code,name FROM `item` WHERE `code`='$itcode' and brand = '$brcode' ") or die(mysqli_error($conn));
                if (mysqli_num_rows($getit) > 0) {
                    $icode = mysqli_fetch_array($getit);
                    $ibcode = $icode['barcode'];
                } else {
                    $genit = mysqli_query($conn, "SELECT MAX(barcode) AS maxcode FROM `item` where code != '' LIMIT 0,1 ") or die(mysqli_error($conn));
                    if (mysqli_num_rows($genbr) > 0) {
                        $git = mysqli_fetch_assoc($genit);
                        $ibcode = $git["maxcode"] + 1;
                    } else {
                        $ibcode = '1';
                    }
                    mysqli_query($conn, "INSERT INTO `item`(code, `name`, `unit`, `brand`, `salemode`, `cost`, `selling`,barcode)
                                        VALUES('$itcode', '$mitem', '$unit', '$brcode', 'T', '$mrate', '$selling','$ibcode')") or die(mysqli_error($conn));
                }

                $tctr++;

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`,`vouchdt`, `srno`, `brand`, `itemcode`,`containerno`,`actbatch`,  `qty`, `totqty`, `baleqty`, `rate`, `amount`)
                            VALUES('$mvouch','$vouchdt', '$msrno','$brcode', '$ibcode', '$containerno','$containerno', '$mqty', '$mtotqty',  '$mbaleqty','$mrate', '$mamount')") or die(mysqli_error($conn));

                $mqty = $mqty * -1;
                $mbaleqty = $mbaleqty * -1;
                $mtotqty = $mtotqty * -1;

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

        ############################### Accounts Part ######################################################

        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','005', '$netamt')");

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

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

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `invno`, `vouchdt`, `party`,`broker`,`transporter`,`lrno`,`lrdate`, `type`, 
                                               `godown`,`orderno`,`orderdt`,`gtype`,`subtotal`,`discperc`,`discount`,`frgperc`,`freight`,
                                               `netamt`,`remark1`,`remark2`,`remark3`,`remark4`,`prnsuit`,`prnbrand`, `labour`, `tempo`,  `packing`, `allchrg`, `tqty`, `tweight`)
                                            VALUES($qvouch, '$invno', '$vouchdt', '$party', '$broker', '$transporter', '$lrno', '$lrdate', '$type',
                                               '$godown','$orderno','$orderdt','$gtype','$subtotal','$discperc','$discount','$frgperc','$freight',
                                               '$netamt','$remark1','$remark2','$remark3','$remark4','$prnsuit','$prnbrand', '$labour', '$tempo', '$packing', '$allchrg', '$tqty', '$tweight')") 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',`vouchdt`='$vouchdt', `invno`='$invno', `party`='$party', `broker`='$broker',
                        `transporter`='$transporter', `lrno`='$lrno', `lrdate`='$lrdate', `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', `remark3`='$remark3', `remark4`='$remark4', `prnsuit`='$prnsuit', 
                        `prnbrand`='$prnbrand', `labour`='$labour', `tempo`='$tempo', `packing`='$packing', `allchrg`='$allchrg', `tqty`='$tqty',`tweight`='$tweight'   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);
    $prnsuit = $get['prnsuit'];
    $prnbrand = $get['prnbrand'];
    $prnafter = "&prnsuit=$prnsuit&prnbrand=$prnbrand";

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

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

    $findbroker = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$get[broker]'") or die(mysqli_error($conn));
    $lbroker = mysqli_fetch_array($findbroker);

    $findtransporter = mysqli_query($conn, "SELECT `name` FROM `transport` WHERE `code`='$get[transporter]'") or die(mysqli_error($conn));
    $ltransporter = mysqli_fetch_array($findtransporter);
}

$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, "dumstock", "$delcond", "$file?mode=last");
    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/sale.js" type="text/javascript"></script>
<script src="MyScript/sale_search.js" type="text/javascript"></script>
<script src="MyScript/shortkeys.js" type="text/javascript"></script>

</head>

<body>
    <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 vouchno" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required readonly>
                                <input type="hidden" class="source" value="<?php echo $source; ?>">
                            </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" tabindex="1" 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-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form" tabindex="2" 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 customer" tabindex="3" 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`='010'") or die(mysqli_error($conn));
                                    while ($party = mysqli_fetch_array($gparty)) {
                                        echo "<option value='$party[accode]'>$party[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-left pad">
                                <label class="label-control marg">Currency : INR</label>
                            </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" style="height:500px;overflow-y: scroll">
                            <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="7%" class='text-left'>Brand/Logo</th>
                                        <th width="5%">Code</th>
                                        <th width="8%">Description</th>
                                        <th width="6%" class='text-left'>Container No.</th>
                                        <th width="6%" class='text-right'>Qty.</th>
                                        <th width="6%" class='text-right'>Bale Qty.(Kgs)</th>
                                        <th width="6%" class='text-right'>Total Qty.(Kgs)</th>
                                        <th width="6%" 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 `barcode`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($finditem);

                                            // browse($tran['brand']);

                                            $findbrand = mysqli_query($conn, "SELECT code,name FROM `brand` WHERE `code`='$tran[brand]'") or die(mysqli_error($conn));
                                            // print table($findbrand);
                                            // die();
                                            $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 name='brand_$ctr' class='brand form-control form ' style='width:100%'>";
                                            echo "<option value= '$lbrand[name]'>$lbrand[name]</option>";
                                            $getitem1 = mysqli_query($conn, "SELECT code, name FROM `brand` order by name") or die(mysqli_error($conn));
                                            while ($geti1 = mysqli_fetch_array($getitem1)) {
                                                echo "<option value= '$geti1[name]'>$geti1[name]</option>";
                                            }
                                            echo "</select>";
                                            echo "</td>";

                                            echo "<td class=''>";
                                            echo "<select name='itemcode_$ctr' class='itemcode form-control form ' style='width:100%'>";
                                            echo "<option value= '$fitem[code]'>$fitem[code]</option>";
                                            $gitcd = mysqli_query($conn, "SELECT * FROM `item` where brand = '$tran[brand]'") or die(mysqli_error($conn));
                                            while ($fitcd = mysqli_fetch_array($gitcd)) {
                                                echo "<option value='$fitcd[code]'>$fitcd[code]</option>";
                                            }
                                            echo "</select>";

                                            echo "</td>";

                                            echo "<td>";
                                            echo "<input list='item' name='item_$ctr' class='item form-control form ' autocomplete='off' value='$fitem[name]'> ";
                                            echo "<datalist id='item' char='#'>";
                                            echo "<option>";

                                            $gitem = mysqli_query($conn, 'SELECT * FROM `item`') or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                echo "<option value='$item[name]'></option>";
                                            }
                                            echo "</datalist>";
                                            echo "</td>";
                                            echo "<td><input type='text' class='form-control form text-left containerno'       name='containerno_$ctr'     readonly    value='$tran[containerno]'></td>";
                                            echo "<td><input type='text' class='form-control form text-right qty'       name='qty_$ctr'         value='$tran[qty]'></td>";
                                            Echo "<td>";
                                            echo "<input list='baleqty' name='baleqty_$ctr' class='baleqty form-control form text-right' autocomplete='off' value='$tran[baleqty]'>";
                                            echo "<datalist id='baleqty' char='#'>";
                                            echo"<option>";
                                            $gitem1 = mysqli_query($conn, 'SELECT * FROM `balewgt`')or die(mysqli_error($conn));
                                            while ($item1 = mysqli_fetch_array($gitem1)) {
                                                Echo "<option value='$item1[balewgt]'></option>";
                                            }
                                            echo "</datalist>";
                                            Echo "</td>";
//                                            echo "<td><input type='text' class='form-control form text-right baleqty'       name='baleqty_$ctr'         value='$tran[baleqty]'></td>";
                                            echo "<td><input type='text' class='form-control form text-right totqty'    name='totqty_$ctr'      value='$tran[totqty]' 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 "</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 id='brand' name='brand_0' class='brand form-control form ' style="width:100%" required>
                                                    <option value= ''></option>
                                                    <?php
                                                    $getitem1 = mysqli_query($conn, "SELECT code, name FROM `brand`") or die(mysqli_error($conn));
                                                    while ($geti1 = mysqli_fetch_array($getitem1)) {
                                                        echo "<option value= '$geti1[name]'>$geti1[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>

                                            <td class="">
                                                <select id='itemcode' name='itemcode_0' class='itemcode form-control form ' style="width:100%" required>
                                                    <option value= ''></option>
                                                    <?php
                                                    $gitemcode = mysqli_query($conn, 'SELECT * FROM `item` order by code') or die(mysqli_error($conn));
                                                    while ($fitemcode = mysqli_fetch_array($gitemcode)) {
                                                        echo "<option value='$fitemcode[code]'>$fitemcode[code]</option>";
                                                    }
                                                    ?>
                                                </select>

                                                                        <!--<input type='text' class='form-control form itemcode'     name='itemcode_0'       value=''>-->
                                            </td>

                                            <td>
                                                <input list='item' name='item_0' class='item form-control form ' autocomplete='off' value=''>
                                                <datalist id='item' char='#'>

                                                    <?php
                                                    $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[name]'></option>";
                                                    }
                                                    ?>
                                                </datalist>
                                            </td>
                                            <td><input type="text" class="form-control form text-left containerno" name="containerno_0" readonly value=""></td>
                                            <td><input type="text" class="form-control form text-right qty" name="qty_0" value="0"></td>
                                            <td>
                                                <input list='baleqty' name='baleqty_0' class='baleqty form-control form text-right' autocomplete='off' value=''>
                                                <datalist id='baleqty' char='#'>

                                                    <?php
                                                    $gitem2 = mysqli_query($conn, 'SELECT * FROM `balewgt`')or die(mysqli_error($conn));
                                                    while ($item2 = mysqli_fetch_array($gitem2)) {
                                                        Echo "<option value='$item2[balewgt]'></option>";
                                                    }
                                                    ?>
                                                </datalist>
                                            </td>
                                <!--<td><input type="text" class="form-control form text-right baleqty" name="baleqty_0" value="0"></td>-->
                                            <td><input type="text" class="form-control form text-right totqty" name="totqty_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" readonly></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" tabindex="26" 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-3 col-xs-6 text-right pad">
                                <input type='text' tabindex="29" class='form-control form remark1' name='remark1' value='<?php echo ($addflag != 1) ? $get['remark1'] : ''; ?>'>
                            </div>
                            <div class="col-lg-1 col-xs-1 text-right pad">
                                <label class='label-control marg'>Total</label>
                            </div>
                            <div class="col-lg-1 col-xs-6 text-right pad">
                                <input type='text' class='form-control form tqty' name='tqty' value='<?php echo ($addflag != 1) ? $get['tqty'] : ''; ?>'>
                            </div>
                            <div class="col-lg-1 col-xs-1 text-right pad">
                                <label class='label-control marg'></label>
                            </div>
                            <div class="col-lg-1 col-xs-6 text-right pad">
                                <input type='text' class='form-control form tweight' name='tweight' value='<?php echo ($addflag != 1) ? $get['tweight'] : ''; ?>'>
                            </div>
                            <div class="col-lg-1 col-xs-2 text-right pad">
                                <label class="label-control marg"><i class="fa fa-inr" aria-hidden="true"></i> SubTotal</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <input type="text" class="form-control form subtotal numeric" value="<? echo ($addflag != 1) ? $get['subtotal'] : "0"; ?>" 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-3 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-3 col-xs-6 text-right pad">
                            </div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg"><i class="fa fa-inr" aria-hidden="true"></i> Less Discount</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <div class="col-lg-5 pad">
                                    <input type="text" tabindex="27" class="form-control form discperc numeric" value="<? echo ($addflag != 1) ? $get['discperc'] : "0"; ?> " 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 numeric" value="<? echo ($addflag != 1) ? $get['discount'] : "0"; ?>" name="discount" readonly>
                                </div>
                            </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 remark3' name='remark3' value='<?php echo ($addflag != 1) ? $get['remark3'] : ''; ?>'>-->
                            </div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg"><i class="fa fa-inr" aria-hidden="true"></i> 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 numeric" 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" tabindex="28" class="form-control form freight numeric" value="<? echo ($addflag != 1) ? $get['freight'] : "0"; ?>" name="freight">
                                </div>
                            </div>
                        </div>
                        <div class="row text-right pad">
                            <div class="col-lg-7 col-xs-1 text-right pad">
                                <label class='label-control marg'>&nbsp;</label>
                            </div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg"><i class="fa fa-inr" aria-hidden="true"></i> Labour</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <div class="col-lg-5 pad">
                                    <!--<input type="text" class="form-control form frgperc numeric" 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" tabindex="" class="form-control form labour numeric" value="<? echo ($addflag != 1) ? $get['labour'] : "0"; ?>" name="labour">
                                </div>
                            </div>
                        </div>
                        <div class="row text-right pad">
                            <div class="col-lg-7 col-xs-1 text-right pad">
                                <label class='label-control marg'>&nbsp;</label>
                            </div>

                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg"><i class="fa fa-inr" aria-hidden="true"></i> Tempo</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <div class="col-lg-5 pad">
                                    <!--<input type="text" class="form-control form frgperc numeric" 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" tabindex="" class="form-control form tempo numeric" value="<? echo ($addflag != 1) ? $get['tempo'] : "0"; ?>" name="tempo">
                                </div>
                            </div>
                        </div>
                        <div class="row text-right pad">
                            <div class="col-lg-7 col-xs-1 text-right pad">
                                <label class='label-control marg'>&nbsp;</label>
                            </div>

                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg"><i class="fa fa-inr" aria-hidden="true"></i> Packing</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <div class="col-lg-5 pad">
                                    <!--<input type="text" class="form-control form frgperc numeric" 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" tabindex="" class="form-control form packing numeric" value="<? echo ($addflag != 1) ? $get['packing'] : "0"; ?>" name="packing">
                                </div>
                            </div>
                        </div>
                        <div class="row text-right pad">
                            <div class="col-lg-7 col-xs-1 text-right pad">
                                <label class='label-control marg'>&nbsp;</label>
                            </div>

                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg"><i class="fa fa-inr" aria-hidden="true"></i> All</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <div class="col-lg-5 pad">
                                    <!--<input type="text" class="form-control form frgperc numeric" 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" tabindex="" class="form-control form allchrg numeric" value="<? echo ($addflag != 1) ? $get['allchrg'] : "0"; ?>" name="allchrg">
                                </div>
                            </div>
                        </div>
                        <div class="row text-right pad">
                            <div class="col-lg-7 col-xs-1 text-right pad">
                                <label class='label-control marg'>&nbsp;</label>
                            </div>

                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg"><i class="fa fa-inr" aria-hidden="true"></i> Net Amount(Rs.)</label>
                            </div>
                            <div class="col-lg-3 col-xs-3">
                                <input type="text" class="form-control form netamt numeric" value="<? echo ($addflag != 1) ? $get['netamt'] : "0"; ?>" name="netamt" 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'>Print Suiting</label>
                                                    </div>
                                                    <div class="col-lg-1 col-xs-1 text-right pad">
                                                        <input type='text' style="width: 25px" class='form-control form prnsuit' name='prnsuit' value='<?php echo ($addflag != 1) ? $get['prnsuit'] : "Y"; ?>'>
                                                    </div>
                        
                                                    <div class="col-lg-2 col-xs-2 text-right pad">
                                                        <label class="label-control marg">Print Brand</label>
                                                    </div>
                                                    <div class="col-lg-1 col-xs-1">
                                                        <input type="text" style="width: 25px" class="form-control form prnbrand" name="prnbrand" value="<? echo ($addflag != 1) ? $get['prnbrand'] : "Y"; ?>" >
                                                    </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 panel-body">
                    <div class="col-lg-12">

                        <!--                        <div class="col-lg-3">
                                                    <label class="control-label col-lg-5"></label>
                                                    <label class="control-label col-lg-5" style="color:red;"></label>
                                                    <div class="col-xs-4 pad text-right">
                                                        <img src="conew.png" alt="" style="width: 45px;position: absolute;left: 440px;top: -5px;cursor:pointer;"  onclick="window.open('sale_invhead_print.php?vouchno=<?= $qvouch . $prnafter; ?>', 'popUponclickWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');">
                                                    </div>
                                                </div>-->

                        <!--                        <div class="col-lg-3 col-xs-4 pad text-right">
                                                    <td class=" buttonform tooltip-bottom" id="hprint" style="display: <? echo $printdisabled ?>" data-toggle="tooltip" data-placement="top" title="" data-original-title="Print">
                                                        <img src="MyStyle/pdficon.png" title="Invoice Print" class="btnpdf pdf" style="height:50px;position: absolute;left: 440px;top: 1px;cursor:pointer;display: <?= $editdisabled ?>" onclick="window.open('sale_invhead_print.php?vouchno=<?= $qvouch . $prnafter; ?>', 'popUponclickWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes')">                            
                        
                                                    </td>                               
                                                </div>  -->
                        <!--<div class=" col-lg-6">-->
                        <?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-bordered">
                                <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 `vouchno`, date_format(`vouchdt`,'%d-%m-%Y') as vouchdt,`party`,`type`,`godown`,`discount`,`freight`,`netamt` FROM `$maindb`") or die(mysqli_error($conn));
//                                    while ($row = mysqli_fetch_array($srchresult)) {
//                                        $getsparty = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$row[party]'") or die(mysqli_error($conn));
//                                        $sparty = mysqli_fetch_array($getsparty);
//                                        //                                        $getstype = mysqli_query($conn, "SELECT `name` 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='center'>$row[vouchno]</td>";
//                                        echo "<td align='center'>$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>-->


    <div class="modal fade bs-example-modal-lg searchModal" 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="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher</option>
                                <option value="2">Invoice No.</option>
                                <option value="3">Date</option>
                                <option value="4">Party</option>
                                <option value="5">Net Amount</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">Invoice No.</option>
                                <option value="3">Date</option>
                                <option value="4">Party</option>
                                <option value="5">Net Amount</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" style="background-color:#1ABC9C;border-radius: 5px" 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;border-radius: 5px !important">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color:#1ABC9C;">
                                    <th width="3%">Voucher</th>
                                    <th width="5%">Inv No.</th>
                                    <th width="6%">Date</th>
                                    <th width="16%">Party</th>
                                    <th width="8%" style="text-align:right">Net Amount</th>
                                    <th width="2%">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 sprev" style="background-color:#1ABC9C;border-radius: 5px" value="Previous">
                            <input type="button" class="btn btn-primary snext" style="background-color:#1ABC9C;border-radius: 5px" value="Next">
                        </div>
                    </div>

                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>


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

</html>