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

$formname = 'Sale Gst';
$maindb = 'salemain';
$trandb = 'saletran';
$stkdb = 'transacn';
$accdb = 'actran';
$printfile = "salegstprint2_print.php";
$file = 'salegst2.php';
$trandbexist = 1;
$keyfld = "vouchno";
//$mgrpcode       = "SL";
$source = "SL";
$salecode = "15";
$para = $_GET['para'];
$gstperc = $para / 2;

$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"];
    $prnbrand = $_POST["prnbrand"];
    $oms = $_POST["oms"];
    $taxamt = $_POST["taxamt"];
    $totcgst = $_POST["totcgst"];
    $totsgst = $_POST["totsgst"];
    $totigst = $_POST["totigst"];
    $sgstperc = $_POST["sgstperc"];
    $cgstperc = $_POST["cgstperc"];
    $igstperc = $_POST["igstperc"];

    $count = $_POST["items"];

    $mvouch = $qvouch;
    
    if ($oms == 1) {
        $gstperc = $igstperc;
    } else {
        $gstperc =  $cgstperc + $sgstperc ;
    }
    
    

    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"];
            $mhsncode = $_POST["hsncode_$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'];
                $tctr++;
                
                $disctran = ($mamount * $discount/$subtotal) ;
                $gsttran  = ($mamount - $disctran) * ($gstperc/100) ;

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

                $mtaka = $mtaka * -1;
                $macttaka = $macttaka * -1;
                $mqty = $mqty * -1;
                $mtotqty = $mtotqty * -1;
                $mflag = $mflag * -1;
                $mflagqty = $mflagqty * -1;
                $minvqty = $minvqty * -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 ######################################################

        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`,`oms`, `cgstperc`, `sgstperc`, `igstperc`, 
                                               `totcgst`, `totsgst`, `totigst`, `tottaxamt`)
                                            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', '$oms', '$cgstperc', '$sgstperc', '$igstperc',
                                               '$totcgst', '$totsgst', '$totigst', '$taxamt')") 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', `oms`='$oms',`cgstperc`='$cgstperc',
                            `sgstperc`='$sgstperc', `igstperc`='$igstperc', `totcgst`='$totcgst', `totsgst`='$totsgst', `totigst`='$totigst', `tottaxamt`='$taxamt',
                        `prnbrand`='$prnbrand' 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/salegst.js" type="text/javascript"></script>
<script src="MyScript/salegst_search.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-2 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>
                        <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" tabindex="4" 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" tabindex="5" value="<?php Echo ($addflag != 1) ? $get["orderdt"] : date("Y-m-d"); ?>" name="orderdt">
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Broker</label>
                            </div>
                            <div class="col-lg-4 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 broker" tabindex="6" name="broker" style="width: 90%">
                                    <option value="<?php Echo ($addflag != 1) ? $get["broker"] : ""; ?>"><?php Echo ($addflag != 1) ? $lbroker["name"] : ""; ?></option>
                                    <?php
                                    $gbroker = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `grpcode`='022'")or die(mysqli_error($conn));
                                    while ($broker = mysqli_fetch_array($gbroker)) {
                                        Echo "<option value='$broker[accode]'>$broker[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">L.R.No.</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" tabindex="7" class="form-control form lrno" value='<?php Echo ($addflag != 1) ? $get[lrno] : ''; ?>'  name="lrno">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">L.R Date</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" tabindex="8" class="form-control form lrdate" value="<?php Echo ($addflag != 1) ? $get["lrdate"] : date("Y-m-d"); ?>" name="lrdate">
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Transporter</label>
                            </div>
                            <div class="col-lg-4 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2" name="transporter" tabindex="9" style="width: 90%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["transporter"] : ""; ?>"><?php Echo ($addflag != 1) ? $ltransporter["name"] : ""; ?></option>
                                    <?php
                                    $gtransporter = mysqli_query($conn, "SELECT * FROM `transport`")or die(mysqli_error($conn));
                                    while ($transporter = mysqli_fetch_array($gtransporter)) {
                                        Echo "<option value='$transporter[code]'>$transporter[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">Type</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2" tabindex="10" 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" tabindex="11" 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 class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Out Of State</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="checkbox"  class="form-control form oms" <?php
                                if ($get[oms] == "1") {
                                    Echo "checked";
                                }
                                ?> value="1" name="oms">                            
                            </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="10%" class='text-left'>Brand</th>
                                        <th width="11%">Item</th>
                                        <th width="5%">Hsn Code</th>
                                        <th width="4%" class='text-left'>Batch</th>
                                        <th width="6%" class='text-right'>Act Batch</th>
                                        <th width="3%" class='text-right'>Taka</th>
                                        <th width="3%" 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="3%" 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 `srno`,`itemcode`,`brand`,`batch`,`hsncode`,`actbatch`, `taka`,
                                                               `acttaka`,`qty`,`totqty`, `flag`, `flagqty`, `invqty`, `rate`,
                                                               `amount` 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 code,name FROM `item` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($finditem);

                                            $findbrand = mysqli_query($conn, "SELECT code,name FROM `brand` WHERE `code`='$tran[brand]'") or die(mysqli_error($conn));
                                            $lbrand = mysqli_fetch_array($findbrand);

//                                            browse($tran['brand']);
                                            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 code,name 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 code, name FROM `item` order by name')or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                Echo "<option value='$item[code]'>$item[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' class='form-control form hsncode'                name='hsncode_$ctr'       value='$tran[hsncode]'></td>";
                                            Echo "<td><input type='text' class='form-control form 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" tabindex="12" name="srno_0" value="1"></td>
                                            <td>
                                                <select class="form-control form brand select2" tabindex="13" name="brand_0" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gbrand = mysqli_query($conn, "SELECT code, name FROM `brand` order by name ")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" tabindex="14" name="item_0" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gitem = mysqli_query($conn, "SELECT code, name FROM `item` order by name")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 hsncode"  tabindex="15"   name="hsncode_0"      value=""></td>
                                            <td><input type="text" class="form-control form text-left batch"  tabindex="15"   name="batch_0"      value=""></td>
                                            <td><input type="text" class="form-control form text-left actbatch" tabindex="16" name="actbatch_0"   value="" readonly></td>
                                            <td><input type="text" class="form-control form text-right taka" tabindex="17"    name="taka_0"       value="0"></td>
                                            <td><input type="text" class="form-control form text-right acttaka" tabindex="18" name="acttaka_0"    value="0"></td>
                                            <td><input type="text" class="form-control form text-right qty"   tabindex="19"   name="qty_0"        value="0"></td>
                                            <td><input type="text" class="form-control form text-right totqty" tabindex="20"  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"  tabindex="21"   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" tabindex="22" 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" tabindex="23"  name="invqty_0"     value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right rate"   tabindex="24"  name="rate_0"       value="0"></td>
                                            <td><input type="text" class="form-control form text-right amount" tabindex="25"  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" 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-6 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-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 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-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" 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">Taxable Value</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 taxamt numeric" value="<? Echo ($addflag != 1) ? $get[tottaxamt] : "0"; ?>" name="taxamt" 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 remark4' name='remark4' value='<?php Echo ($addflag != 1) ? $get[remark4] : ''; ?>'>
                            </div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Add CGST</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 cgstperc numeric" value="<? Echo ($addflag != 1) ? $get[cgstperc] : "0"; ?> "  name="cgstperc" style="width: 80%" readonly>
                                </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 cgstamt numeric" value="<? Echo ($addflag != 1) ? $get[totcgst] : "0"; ?>" name="totcgst" 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">
                            </div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Add SGST</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 sgstperc numeric" value="<? Echo ($addflag != 1) ? $get[sgstperc] : "0"; ?> "  name="sgstperc" style="width: 80%" readonly>
                                </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 sgstamt numeric" value="<? Echo ($addflag != 1) ? $get[totsgst] : "0"; ?>" name="totsgst" 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">
                            </div>
                            <div class="col-lg-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Add IGST</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 igstperc numeric" value="<? Echo ($addflag != 1) ? $get[igstperc] : "0"; ?> "  name="igstperc" style="width: 80%" readonly>
                                </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 igstamt numeric" value="<? Echo ($addflag != 1) ? $get[totigst] : "0"; ?>" name="totigst" 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">

                            </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 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-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">

                            </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 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">
                    <div class="panel-body">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
            <div class="row pull-center">
                <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: 50px;position: absolute;left: 80px;top: -30px;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>
    </form>
    <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">Party</option>
                                <option value="2">Voucher</option>
                                <option value="3">Date</option>
                                <option value="4">Type</option>                                
                                <option value="5">Godown</option>                                
                                <option value="6">Discount</option>                                
                                <option value="7">Freight</option>                                
                                <option value="8">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">Date</option>
                                <option value="3">Party</option>
                                <option value="4">Type</option>                                
                                <option value="5">Godown</option>                                
                                <option value="6">Discount</option>                                
                                <option value="7">Freight</option>                                
                                <option value="8">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" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color:#1abc9c;">
<!--                                    <th width="10%">Code</th>-->
                                    <th width="5%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="30%">Party</th>
                                    <th width="10%">Type</th>
                                    <th width="5%">Godown</th>
                                    <th width="10%">Discount</th>
                                    <th width="10%">Freight</th>
                                    <th width="17%">Net Amount</th>
                                    <th width="3%">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" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>

                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <!--    <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 `vouchno`, `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='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>
