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

$formname = 'Purchase/Inward';
$maindb = 'purmain';
$trandb = 'purtran';
$trandb2 = 'purchexp';

$stkdb = 'transacn';
$accdb = 'actran';

$printfile = "new_purch_print.php";
$file = 'purchase.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "PU";

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

if (isset($_POST["submit"])) {
    $a = 1;
    $invno = $_POST["invno"];
    $invdt = $_POST["invdt"];
    $vouchdt = $_POST["vouchdt"];
    $party = $_POST["party"];
    $type = $_POST["type"];
    $subtotal = $_POST["subtotal"];
    $discperc = $_POST["discperc"];
    $discount = $_POST["discount"];
    $frgperc = $_POST["frgperc"];
    $freight = $_POST["freight"];
    $othchgs = $_POST["othchgs"];
    $netamt = $_POST["netamt"];
    $rem1 = $_POST["rem1"];
    $rem2 = $_POST["rem2"];
    $rem3 = $_POST["rem3"];
    $rem4 = $_POST["rem4"];
    $totexpamt = $_POST["totexpamt"];
    $roe = $_POST["roe"];
    $containerno = $_POST["containerno"];
    $billamt = $_POST["billamt"];
    $currency = $_POST["currency"];
    $sealno = $_POST["sealno"];
    $tqty = $_POST["tqty"];
    $tweight = $_POST["tweight"];
    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode_d = 10;   // purchase dollar
    $salecode = 11;   // purchase credit

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

    if ($a == 1) {

        #################################TRAN PART #################################
        #################################STOCK PART #################################
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$trandb2` WHERE vouchno='$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;

        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mbrand = trim($_POST["brand_$i"]);
            $mitem = trim($_POST["item_$i"]);
            $itcode = $_POST["itemcode_$i"];
            $mqty = $_POST["qty_$i"];
            $mbaleqty = $_POST["baleqty_$i"];
            $mtotqty = $_POST["totqty_$i"];
            $mrate = $_POST["rate_$i"];
            $mamount = $_POST["amount_$i"];
            $godown = $_POST["godown_$i"];

            $mactbatch = $source . $mvouch . $mbatch;

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

//                browse($itcode);

                $getit = mysqli_query($conn, "SELECT barcode, code,name FROM `item` WHERE `code`='$itcode' and brand = '$brcode' ") or die(mysqli_error($conn));
//                browse(mysqli_num_rows($getit));
                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($genit) > 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));
                }

//                browse($ibcode);
                $srl = $srl + 1;

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

                mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, `group`, `source`, `type`,`vouchno`,`vouchdt`,
                            `srno`, `brand`, `itemcode`,`itemname`,`batch`, `taka`, `qty`, `cost`, baleqty, 
                            `totqty`, `flag`, `flagqty`, `actqty`, `godown`, `actbatch`,`ord`,`shade`,containerno, `rate`, `currency`)
                     VALUES('$yearno','$group','$source','$type','$qvouch','$vouchdt', '$srl','$brcode',
                            '$ibcode', '$mitem', '$mbatch', '$mtaka', '$mqty', '$mrate', '$mbaleqty', '$mtotqty', 
                            '$mflag', '$mflagqty','$minvqty', '$godown', '$mactbatch','$ord','$mshade','$containerno', '$mrate', 'USD')") or die(mysqli_error($conn));
            }
        }

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mvouch' AND `source`='$source'") OR die(mysqli_error($conn));

        $cnt2 = 0;
        $expamt = 0;
        $count2 = $_POST["items2"];
        $totexp = 0;
        for ($j = 0; $j <= $count2; $j++) {
            $msrno = $_POST["srno_$j"];
            $expense = $_POST["expense_$j"];
            $expamt = $_POST["expamt_$j"];
            $minus_exp = $expamt * -1;

            if ($expamt > 0) {
                $cnt2++;
                mysqli_query($conn, "INSERT INTO `$trandb2`(vouchno  , vouchdt  ,   srno,    expense ,   expamt)
                                                        VALUES('$mvouch', '$vouchdt', '$cnt2', '$expense', '$expamt')") or die(mysqli_error($conn));

                // expense part
                mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, curr, feamt, roe)
                                            VALUES ('$vouchdt','$mvouch', '$source', '$cnt2', '$expense','007', '$minus_exp', 'INR', '$minus_exp', 1)") or die(mysqli_error($conn));
                $totexp = $totexp + $expamt;
            }
        }

        // expense part to person in charge
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, curr, feamt, roe)
                                          VALUES ('$vouchdt','$mvouch', '$source', '$cnt2', '49','017', '$totexp', 'INR', '$totexp', 1)") or die(mysqli_error($conn));

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


        if ($netamt > 0) {
            $minus_amt = ($billamt * -1);
            $minus_exp = ($totexpamt * -1);

// purchase part - dollar
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, curr, feamt, roe)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$salecode_d','004', '$minus_amt', '$currency', '$minus_amt', '$roe')") OR die(mysqli_error($conn));
// party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, curr, feamt, roe)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$party', '009', '$billamt', '$currency', '$billamt', '$roe' )") or die(mysqli_error($conn));
        }


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

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `invno`, `invdt`,  `vouchdt`, `party`, `type`, `godown`,othchgs, tweight, tqty,
                                            `subtotal`,`discperc`,`discount`,`frgperc`,`freight`,`netamt`, 
                                             `rem1`, `rem2`, `rem3`, `rem4`, `sealno`, `containerno`, `totexpamt`, `roe`, `currency`, `billamt`)
                                            VALUES($qvouch, '$invno', '$invdt','$vouchdt', '$party', '$type', '$godown','$othchgs', '$tweight', '$tqty',
                                            '$subtotal','$discperc','$discount','$frgperc','$freight','$netamt',
                                             '$rem1', '$rem2', '$rem3', '$rem4', '$sealno', '$containerno', '$totexpamt', '$roe', '$currency', '$billamt')") 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', `invdt` = '$invdt',`party`='$party', `type`='$type',othchgs='$othchgs',
                        `godown`='$godown',`subtotal`='$subtotal',`discperc`='$discperc',`discount`='$discount', sealno='$sealno', containerno='$containerno',
                        `frgperc`='$frgperc',`freight`='$freight',`netamt`='$netamt', `rem1`='$rem1', `rem2`='$rem2', tweight = '$tweight', tqty = '$tqty',
                        `rem3`='$rem3', `rem4`= '$rem4', `totexpamt`='$totexpamt', `roe`='$roe', `currency`='$currency', `billamt`='$billamt'  WHERE `vouchno`= '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

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

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

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

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

//    $gdgodown = mysqli_query($conn, "SELECT `code`,`gocode` FROM `godown` WHERE `code`='$get[godown]'") or die(mysqli_error($conn));
//    $gtgodown = mysqli_fetch_array($gdgodown);
}

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb2", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$stkdb", "$delcond", "$file?mode=last");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<style type="text/css">
    .col-lg-1{
        width: 8%;
    }
    .stick{
        position: sticky;
        position: -webkit-sticky;
        top: 0;
        background: #1ABC9C;
        z-index: 10;

    }
</style>
<script src="MyScript/purchase.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/purchase_search.js?b=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/shortkeys.js?c=<?= mt_rand() ?>" type="text/javascript"></script>
</head>
<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:#1ABC9C"><?php Echo $formname; ?></h3>
                    </div>

                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required readonly>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">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" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>">
                            </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-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form focusfield select2" name="party" style="width: 90%" required autofocus>
                                    <option value="<?php Echo ($addflag != 1) ? $get["party"] : ""; ?>"><?php Echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `grpcode`='009'") or die(mysqli_error($conn));
                                    while ($party = mysqli_fetch_array($gparty)) {
                                        Echo "<option value='$party[accode]'>$party[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div 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 " name="invno" value="<?php Echo ($addflag != 1) ? $get["invno"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Inv. 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" name="invdt" value="<?php Echo ($addflag != 1) ? $get["invdt"] : date("Y-m-d"); ?>">
                            </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">Container No.</label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form containerno" name="containerno" value="<?php Echo ($addflag != 1) ? $get["containerno"] : ""; ?>" style="width:96%" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad" style="width:7.1%">
                                <label class="label-control marg">Seal No.</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form sealno" name="sealno" value="<?php Echo ($addflag != 1) ? $get["sealno"] : ""; ?>" >
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Currency</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form currency select2" name="currency" style="width: 90%">
                                    <option value="<?php Echo ($addflag != 1) ? $get["currency"] : "2"; ?>"><?php Echo ($addflag != 1) ? $get["currency"] : "USD"; ?></option>
                                    <?php
                                    $getcurr = mysqli_query($conn, "SELECT name FROM `currency`") or die(mysqli_error($conn));
                                    while ($curr = mysqli_fetch_array($getcurr)) {
                                        Echo "<option value='$curr[name]'>$curr[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">ROE</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right roe" name="roe" value="<?php Echo ($addflag != 1) ? $get["roe"] : ""; ?>" required>
                            </div>
                        </div>
                        <!--                        <div class="row">
                                                    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 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 godown select2" name="godown" style="width:100%" required>
                                                            <option value="<?php Echo ($addflag != 1) ? $get["godown"] : ""; ?>"><?php Echo ($addflag != 1) ? $gtgodown["gocode"] : ""; ?></option>
                        <?php
                        $getgodown = mysqli_query($conn, "SELECT `code`,`gocode` FROM `godown` order by name ") or die(mysqli_error($conn));
                        while ($fetgodown = mysqli_fetch_array($getgodown)) {
                            Echo "<option value='$fetgodown[code]'>$fetgodown[gocode]</option>";
                        }
                        ?>
                                                        </select>
                                                    </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">
                                    <div class="table-responsive" style="height:300px;overflow-y: scroll">
                                        <table class="table table-striped table-condensed table-bordered" width="100%">
                                            <thead  style="background: #1ABC9C;">
                                                <tr>
                                                    <th width="1%" class='stick'></th>
                                                    <th width="3%" class='text-right stick'>Sr.</th>
                                                    <th width="7%" class='text-left stick'>Brand/Logo</th>
                                                    <th width="5%" class='stick'>Code</th>
                                                    <th width="8%" class='stick'>Description</th>
                                                    <th width="6%" class='text-right stick'>Qty.</th>
                                                    <!--<th width="6%" class='text-left'>Unit</th>-->
                                                    <th width="6%" class='text-right stick'>Bale Qty.(Kgs)</th>
                                                    <th width="6%" class='text-right stick'>Total Qty.(Kgs)</th>
                                                    <th width="6%" class='text-right stick'>Cost</th>
                                                    <th width="6%" class='text-right stick'>Amount</th>
                                                    <th width="6%" class='text-left stick'>Godown</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);
                                                        $findbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'") or die(mysqli_error($conn));
                                                        $lbrand = mysqli_fetch_array($findbrand);
                                                        $gdgodown = mysqli_query($conn, "SELECT `code`,`gocode` FROM `godown` WHERE `code`='$tran[godown]'") or die(mysqli_error($conn));
                                                        $gtgodown = mysqli_fetch_array($gdgodown);

                                                        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 "<input list='brand' name='brand_$ctr' class='brand form-control form ' autocomplete='off' value='$lbrand[name]'> ";
                                                        echo "<datalist id='brand' char='#'>";
                                                        echo"<option>";

                                                        $gbrand = mysqli_query($conn, 'SELECT * FROM `brand`') or die(mysqli_error($conn));
                                                        while ($brand = mysqli_fetch_array($gbrand)) {
                                                            Echo "<option value='$brand[name]'>";
                                                        }
                                                        echo "</datalist>";
                                                        Echo "</td>";

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

                                                        $gitcd = mysqli_query($conn, "SELECT * FROM `item` where brand = '$row[brand]'") or die(mysqli_error($conn));
                                                        while ($fitcd = mysqli_fetch_array($gitcd)) {
                                                            Echo "<option value='$fitcd[code]'>";
                                                        }
                                                        echo "</datalist>";
                                                        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-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 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 "<td>";
                                                        echo "<select name='godown_$ctr' class='godown form-control form ' style='width:100%'>";
                                                        echo "<option value= '$tran[godown]'>$gtgodown[gocode]</option>";
                                                        $getgodown = mysqli_query($conn, "SELECT `code`,`gocode` FROM `godown` order by name ") or die(mysqli_error($conn));
                                                        while ($fetgodown = mysqli_fetch_array($getgodown)) {
                                                            Echo "<option value='$fetgodown[code]'>$fetgodown[gocode]</option>";
                                                        }
                                                        echo "</select>";
                                                        echo "</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>
                                                            <input list='brand' name='brand_0' class='brand form-control form ' autocomplete='off' value=''>
                                                            <datalist id='brand' char='#'>

                                                                <?php
                                                                $gbrand = mysqli_query($conn, 'SELECT * FROM `brand`') or die(mysqli_error($conn));
                                                                while ($brand = mysqli_fetch_array($gbrand)) {
                                                                    Echo "<option value='$brand[name]'>";
                                                                }
                                                                ?>
                                                            </datalist>
                                                        </td>
                                                        <td class="">
                                                            <input list='itemcode' name='itemcode_0' class=' form-control form itemcode' autocomplete='off' value=''>
                                                            <datalist id='itemcode' char='#'>

                                                                <?php
                                                                $gitemcode = mysqli_query($conn, 'SELECT * FROM `item`') or die(mysqli_error($conn));
                                                                while ($fitemcode = mysqli_fetch_array($gitemcode)) {
                                                                    Echo "<option value='$fitemcode[code]'>";
                                                                }
                                                                ?>
                                                            </datalist>
                                                            <!--<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`') 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-right qty"     name="qty_0"        value="0"></td>
                                                        <!--<td><input type="text" class="form-control form text-left unit"     name="unit_0"        value=""></td>-->
                                                        <!--<td><input type="text" class="form-control form text-right baleqty"     name="baleqty_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 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>
                                                        <td>
                                                            <select name='godown_0' class='godown form-control form ' style='width:100%'>
                                                                <option value= ''></option>
                                                                <?php
                                                                $getgodown = mysqli_query($conn, "SELECT `code`,`gocode` FROM `godown` order by name ") or die(mysqli_error($conn));
                                                                while ($fetgodown = mysqli_fetch_array($getgodown)) {
                                                                    Echo "<option value='$fetgodown[code]'>$fetgodown[gocode]</option>";
                                                                }
                                                                ?>
                                                            </select>
                                                        </td>
                                                    </tr>  
                                                    <?
                                                }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="col-lg-12 pad">
            <!--                            <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />-->
                                        <button type="button" class="btn btn-success btn-square form"  id="addnew">
                                            <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                                        </button>
                                        <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />  
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                            <div class="panel panel-default">

                                <div class="panel-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-4 col-xs-5 text-right pad">
                                            <input type="text" name="rem1" class="form-control form"  value="<? Echo ($addflag != 1) ? $get['rem1'] : ""; ?>">
                                        </div>
                                        <div class="col-lg-1 col-xs-3 text-right pad">
                                            <label class="label-control marg">Total</label>
                                        </div>
                                        <div class="col-lg-1 col-xs-3">
                                            <input type="text" class="form-control form tqty text-right" value="<? Echo ($addflag != 1) ? $get['tqty'] : "0"; ?>" name="tqty" readonly>
                                        </div>
                                        <div class="col-lg-1 col-xs-3">

                                        </div>
                                        <div class="col-lg-1 col-xs-3" style="width:9.5%">
                                            <input type="text" class="form-control form tweight text-right" value="<? Echo ($addflag != 1) ? $get['tweight'] : "0"; ?>" name="tweight" readonly>
                                        </div>
                                        <div class="col-lg-1 col-xs-3 text-right pad">
                                            <label class="label-control marg"><i class="fa fa-usd" aria-hidden="true"></i> Sub Total</label>
                                        </div>
                                        <div class="col-lg-2 col-xs-3">
                                            <input type="text" class="form-control form subtotal text-right" 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"></div>
                                        <div class="col-lg-4 col-xs-5 text-right pad">
                                            <input type="text" name="rem2" class="form-control form" value="<? Echo ($addflag != 1) ? $get['rem2'] : ""; ?>" >
                                        </div>
                                        <div class="col-lg-4 col-xs-3 text-right pad">
                                            <label class="label-control marg"><i class="fa fa-usd" aria-hidden="true"></i> Less Discount</label>
                                        </div>
                                        <div class="col-lg-3 col-xs-3 pad">
                                            <div class="col-lg-5">
                                                <input type="text" class="form-control form discperc text-right" value="<? Echo ($addflag != 1) ? $get['discperc'] : "0"; ?> "  name="discperc">
                                            </div>
                                            <div class="col-lg-1 pad text-center">
                                                <label class="label-control marg">%</label>
                                            </div>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control form discount text-right" 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"></div>
                                        <div class="col-lg-5 col-xs-5 text-right pad">
                                            <!--<input type="text" name="rem3" class="form-control form" value="<? Echo ($addflag != 1) ? $get['rem3'] : ""; ?>">-->
                                        </div>
                                        <div class="col-lg-3 col-xs-3 text-right pad">
                                            <label class="label-control marg"><i class="fa fa-usd" aria-hidden="true"></i> Add Freight</label>
                                        </div>
                                        <div class="col-lg-3 col-xs-3 pad">
                                            <div class="col-lg-6">
                                                <!--<input type="text" class="form-control form frgperc" value="<? Echo ($addflag != 1) ? $get['frgperc'] : ""; ?>"  name="frgperc">-->
                                            </div>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control form freight text-right" 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"></div>
                                        <div class="col-lg-5 col-xs-5 text-right pad">
                                        </div>
                                        <div class="col-lg-3 col-xs-3 text-right pad">
                                            <label class="label-control marg"><i class="fa fa-usd" aria-hidden="true"></i> Other Charges</label>
                                        </div>
                                        <div class="col-lg-3 col-xs-3 pad">
                                            <div class="col-lg-6">
                                            </div>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control form othchgs text-right" value="<? Echo ($addflag != 1) ? $get['othchgs'] : "0"; ?>" name="othchgs" >
                                            </div>
                                        </div>
                                    </div>  
                                    <div class="row text-right pad">
                                        <div class="col-lg-1 col-xs-1 text-right pad"></div>
                                        <div class="col-lg-5 col-xs-5 text-right pad">
                                        </div>
                                        <div class="col-lg-3 col-xs-3 text-right pad">
                                            <label class="label-control marg"><i class="fa fa-usd" aria-hidden="true"></i> Bill Amount (Party)</label>
                                        </div>

                                        <div class="col-lg-3 col-xs-3 pad">
                                            <div class="col-lg-6">
                                            </div>
                                            <div class="col-lg-6">
                                                <input type="text" class="form-control form billamt text-right" value="<? Echo ($addflag != 1) ? $get['billamt'] : "0"; ?>" name="billamt" readonly>
                                            </div>
                                        </div>

                                    </div> 
                                    <div class="col-lg-8 col-md-6 col-sm-12 col-xs-12 pad"></div>
                                    <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12 pad">

                                        <div class="table-responsive" style="width:100%">
                                            <table class="table table-striped table-condensed table-bordered">
                                                <thead style="background: #1ABC9C;">
                                                    <tr>
                                                        <th width="2%"></th>
                                                        <th width="3%" class='text-right'>Sr.</th>
                                                        <th width="10%">Expense</th>
                                                        <th width="7%"class='text-right'>Amount</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="tbodyexp">
                                                    <?php
                                                    if ($addflag != 1) {
                                                        $ctr2 = 0;
                                                        $gettran2 = mysqli_query($conn, "SELECT * FROM `$trandb2` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));
                                                        while ($tran2 = mysqli_fetch_array($gettran2)) {

                                                            $gparty = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`='$tran2[expense]' AND grpcode = 007 ") or die(mysqli_error($conn));
                                                            $getdesc = mysqli_fetch_array($gparty);

                                                            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='srno2_$ctr2' value='$tran2[srno]'></td>";

                                                            Echo "<td>";
                                                            Echo "<select class='form-control form expense select2' name='expense_$ctr2' style='width:100%'>";
                                                            Echo "<option value='$getdesc[accode]'>$getdesc[name]</option>";
                                                            $gexp = mysqli_query($conn, "SELECT * FROM `accmast` WHERE grpcode = 007 order by name ") or die(mysqli_error($conn));
                                                            while ($showexp = mysqli_fetch_array($gexp)) {
                                                                Echo "<option value='$showexp[accode]'>$showexp[name]</option>";
                                                            }
                                                            Echo "</select>";
                                                            Echo "</td>";

                                                            Echo "<td><input type='number' class='form-control form text-right expamt' name='expamt_$ctr2' value='$tran2[expamt]'></td>";
                                                            Echo "</tr>";
                                                            $ctr2++;
                                                        }
                                                    } else {
                                                        $pctr2 = 0;
                                                        $tctr2 = 0;
                                                        // $getexp = mysqli_query($conn, "SELECT * FROM rise2_gausiya_root.`pur_expense`")or die(mysqli_error($conn));
                                                        // while ($showexp = mysqli_fetch_array($getexp)) {
                                                        //     $tctr2++;
                                                        ?>


                                                        <tr>
                                                            <td><i class="fa fa-close remove"></i></td>
                                                            <td><input type="text" class="form-control form text-right srno" name="srno2_<?= $pctr2 ?>" value="1"></td>

                                                            <td>
                                                                <select class="form-control form expense select2" name="expense_<?= $pctr2 ?>" style='width:100%'>
                                                                    <option value=""></option>
                                                                    <?php
                                                                    $gexp = mysqli_query($conn, "SELECT * FROM `accmast` WHERE grpcode = 007 order by name ") or die(mysqli_error($conn));
                                                                    while ($showexp = mysqli_fetch_array($gexp)) {
                                                                        Echo "<option value='$showexp[accode]'>$showexp[name]</option>";
                                                                    }
                                                                    ?>
                                                                </select>
                                                            </td>

                                                            <td><input type="number" step="any" class="form-control form text-right expamt"   name="expamt_<?= $pctr2 ?>" value="0" ></td>
                                                        </tr>  
                                                        <?
                                                        $pctr2++;
                                                    }
// }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="col-lg-12 pad">
                                            <button type="button" class="btn btn-success btn-square form"  id="addnew2">
                                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                                            </button>
                                            <input type="hidden" id="items2" name="items2" value="<?php echo ($addflag != 1) ? $ctr2 : $pctr2; ?>" />  
                                        </div>
                                    </div>
                                    <div class="col-lg-9 col-xs-3 text-right pad">
                                        <label class="label-control marg"><i class="fa fa-usd" aria-hidden="true"></i> Total Expense</label>
                                    </div>
                                    <div class="col-lg-3 col-xs-3">
                                        <input type="text" class="form-control form totexpamt text-right" value="<? Echo ($addflag != 1) ? $get['totexpamt'] : "0"; ?>" name="totexpamt" readonly>
                                    </div>
                                    <div class="col-lg-9 col-xs-3 text-right pad">
                                        <label class="label-control marg"><i class="fa fa-usd" 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 text-right" value="<? Echo ($addflag != 1) ? $get['netamt'] : "0"; ?>" name="netamt" readonly>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <?php include("includes/buttons.php"); ?>
                            </div>
                        </div>
                    </div>
                </div>
                </form>
                <!--                <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                                                <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                                            </div>
                                            <div class="modal-body">
                                                <div class="">
                                                    <table id="example1" class="table table-striped">
                                                        <thead>
                                                            <tr style="background-color: #1ABC9C;">
                                                                <th>Voucher</th>
                                                                <th>Date</th>
                                                                <th>Party</th>
                                                                <th>Discount</th>
                                                                <th>Freight</th>
                                                                <th>Net Amount</th>
                                                                <th>Action</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                <?php
//                if ($addflag != 1) {
//                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`")or die(mysqli_error($conn));
//                    while ($row = mysqli_fetch_array($srchresult)) {
//                        $getsparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$row[party]'")or die(mysqli_error($conn));
//                        $sparty = mysqli_fetch_array($getsparty);
//                        $getstype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$row[type]'")or die(mysqli_error($conn));
//                        $stype = mysqli_fetch_array($getstype);
////                                        $getsgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `gocode`='$row[godown]'")or die(mysqli_error($conn));
////                                        $sgodown    = mysqli_fetch_array($getsgodown);
//                        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='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">Date</option>
                                            <option value="3">Party</option>
                                            <option value="4">Invoice No.</option>
                                            <option value="5">Inv.Date</option>
                                            <option value="6">Container No.</option>
                                            <option value="7">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">Invoice No.</option>
                                            <option value="5">Inv.Date</option>
                                            <option value="6">Container No.</option>
                                            <option value="7">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="6%">Date</th>
                                                <th width="16%">Party</th>
                                                <th width="5%">Invoice No.</th>
                                                <th width="7%">Inv.Date</th>
                                                <th width="10%">Container No.</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>
