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

$formname = 'Container Entry';
$maindb = 'containerent';
$accdb = 'actran';

// ini_set('display_errors',1);
// error_reporting(E_ERROR);

$file = 'contentry.php';
$grid = '';
$source = 'CNT';
$keyfld = "vouchno";
$trandbexist = 0;
$printfile = 'contrec.php';

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

$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchno   = $_POST["vouchno"]   ?? '';
    $orderno   = $_POST["orderno"]   ?? '';
    $vouchdt   = $_POST["vouchdt"]   ?? '';
    $orderdt   = $_POST["orderdt"]   ?? '';
    $vendor    = $_POST["vendor"]    ?? '';
    $customer  = $_POST["customer"]  ?? '';
    $product   = $_POST["product"]   ?? '';
    $cbm       = $_POST["cbm"]       ?? '';
    $descrip   = $_POST["descrip"]   ?? '';
    $importer  = $_POST["importer"]  ?? '';
    $mode      = $_POST["mode"]      ?? '';
    $dispdate  = $_POST["dispdate"]  ?? '';
    $expdate   = $_POST["expdate"]   ?? '';
    $type      = $_POST["type"]      ?? '';
    $cash      = $_POST["cash"]      ?? '';
    $bank      = $_POST["bank"]      ?? '';
    $cartonsno = $_POST["cartonsno"] ?? '';
    $recddate  = $_POST["recddate"]  ?? '';
    $status  = $_POST["status"]  ?? '';
    $amount  = floatval($_POST["amount"] ?? 0);
    $advance = floatval($_POST["advance"] ?? 0);
    $duedays = floatval($_POST["duedays"] ?? 0);
    $nodays = intval($_POST["nodays"] ?? 0);

    $mvouch = $qvouch;

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$accdb` WRITE, `accmast` WRITE") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        }

        ######################## ACCOUNTS PART ######################################

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

        if ($amount > 0) {
            $minus_amt = ($amount * -1);
            $minus_adv_amt = ($advance * -1);
            if ($type == "C") {
                $qry1 = mysqli_query($conn, "SELECT grpcode, bankinit FROM `accmast` where accode = '$cash' ") or die(mysqli_error($conn));
                $fet1 = mysqli_fetch_array($qry1);
                $grp1 = $fet1['grpcode'];
                $binit1 = $fet1['bankinit'];
                $accode = $cash;
            } else {
                $qry1 = mysqli_query($conn, "SELECT grpcode, bankinit FROM `accmast` where accode = '$bank' ") or die(mysqli_error($conn));
                $fet1 = mysqli_fetch_array($qry1);
                $grp1 = $fet1['grpcode'];
                $binit1 = $fet1['bankinit'];
                $accode = $bank;
            }

            $qry2 = mysqli_query($conn, "SELECT grpcode, bankinit FROM `accmast` where accode = '$vendor' ") or die(mysqli_error($conn));
            $fet2 = mysqli_fetch_array($qry2);
            $grp2 = $fet2['grpcode'];
            $binit2 = $fet2['bankinit'];


            mysqli_query($conn, "INSERT INTO `$accdb`(`date`, vouchno, source, trnno, accode, grpcode, bankinit, amount)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '12', '004', '$binit1', '$minus_amt')") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$accdb`(`date`, vouchno, source, trnno, accode, grpcode, bankinit, amount)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$vendor', '$grp2', '$binit2', '$amount')") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$accdb`(`date`, vouchno, source, trnno, accode, grpcode, bankinit, amount)
                     VALUES ('$vouchdt','$mvouch', '$source', '003', '$vendor', '$grp2', '$binit2', '$minus_adv_amt')") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$accdb`(`date`, vouchno, source, trnno, accode, grpcode, bankinit, amount)
                    VALUES ('$vouchdt','$mvouch', '$source', '004', '$accode', '$grp1', '$binit1', '$advance')") or die(mysqli_error($conn));
        }

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `orderno`, `vouchdt`,`orderdt`, `vendor`, `customer`, `product`, `cbm`, `amount`, `descrip`,
                            `importer`, `item`, `mode`, `dispdt`, `nodays`, `expdate`, `advance`, `type`, `cashacc`, `bankacc`, `cartonsno`, `recddate`, `duedays`,`status`)
                                       Values ('$mvouch', '$orderno', '$vouchdt','$orderdt', '$vendor', '$customer', '$product', '$cbm', '$amount', '$descrip',
                            '$importer', '$descrip','$mode', '$dispdate', '$nodays', '$expdate','$advance', '$type','$cash', '$bank', '$cartonsno', '$recddate', '$duedays','$status')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET orderno = '$orderno',vouchdt = '$vouchdt',`orderdt`='$orderdt',vendor = '$vendor',customer = '$customer',
            product = '$product',cbm = '$cbm',amount = '$amount',descrip = '$descrip',importer = '$importer',
            item = '$descrip',mode = '$mode',dispdt = '$dispdate',nodays = '$nodays',expdate = '$expdate',advance = '$advance',
            `type` = '$type',cashacc = '$cash',bankacc = '$bank',cartonsno = '$cartonsno',recddate = '$recddate',`status`='$status',
            duedays = '$duedays' where vouchno = '$mvouch' ") or die(mysqli_error($conn));
        }
        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            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);

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

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

    $fproduct = mysqli_query($conn, "SELECT `code`,`name` FROM rawmat WHERE code = '$get[product]'") or die(mysqli_error($conn));
    $gproduct = mysqli_fetch_array($fproduct);

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

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

    $paytype = ($get['type'] == 'C') ? "CASH" : "BANK";
    $status = ($get['status'] == 'U') ? "Unpaid" : "Paid";
    $printdisabled = "none";
}

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$accdb` WRITE") or die(mysqli_error($conn));

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");

    mysqli_query($conn, "UNLOCK TABLES");
}
include("includes/linkshead.php");
?>
<script>
    $(document).ready(function() {
        $("body").on("change", '.type', getCB);
        $("body").on("blur change focus", '.dispdate,.nodays', getExpDt);
        $("body").on("blur change focus", '.dispdate,.recddate', getDuedays);

        function getExpDt() {
            var dispatchdt = $('.dispdate').val();
            var nodays = parseInt($('.nodays').val());

            if (dispatchdt && nodays) {
                var dt = new Date(dispatchdt);
                dt.setDate(dt.getDate() + nodays);

                var yyyy = dt.getFullYear();
                var mm = String(dt.getMonth() + 1).padStart(2, '0');
                var dd = String(dt.getDate()).padStart(2, '0');

                var expDt = yyyy + '-' + mm + '-' + dd;
                $('.expdate').val(expDt);
            }
        }

        function getDuedays() {
            var dispatchdt = $('.dispdate').val();
            var recddate = $('.recddate').val();

            if (dispatchdt && recddate) {
                var start = new Date(dispatchdt);
                var end = new Date(recddate);
                var diffTime = end - start;
                var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
                $('.duedays').val(diffDays); 
            }
        }


        getCB();

        function getCB() {
            var paytype = $('.type').val();
            if (paytype == 'C') {
                $('.cashrow').css("display", "block");
                $('.bankrow').css("display", "none");
            } else {
                $('.bankrow').css("display", "block");
                $('.cashrow').css("display", "none");
            }
        }
    });
</script>
<!--<script src="MyScript/shortkeys.js" type="text/javascript"></script>-->
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<!--<script src="MyScript/challan.js" type="text/javascript"></script>-->

<style>
    .bg-row {
        background-color: #c6ac8f;
    }
</style>
</head>

<body>
    <form action='' method='POST'>
        <div class="row">
            <div class="col-lg-8">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>

                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-conrol">Order No</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="hidden" name="vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>" class="form-control form" readonly>
                                <input type="text" name="orderno" value="<?php echo ($addflag != 1) ? $get['orderno'] : $qvouch; ?>" class="form-control focusfield form" required>
                                <input type="hidden" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                                <input type="hidden" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Voucher Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" name="vouchdt" placeholder="date" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('Y-m-d'); ?>" class="form-control form vouchdt" required>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control">Order Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" name="orderdt" placeholder="date" value="<?php echo ($addflag != 1) ? $get['orderdt'] : date('Y-m-d'); ?>" class="form-control form orderdt" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Vendor</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select id="vendor" name="vendor" class="form-control form select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $gvendor['accode'] : " "; ?>"><?php echo ($addflag != 1) ? $gvendor['name'] : " "; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` where `grpcode` = '009' AND `statecode`='101'  ") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value='$trow[accode]'>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Customer</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select id="customer" name="customer" class="form-control form select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $gcustomer['accode'] : " "; ?>"><?php echo ($addflag != 1) ? $gcustomer['name'] : " "; ?></option>
                                    <?php
                                    $mcust = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` where `grpcode` = '010'") or die(mysqli_error($conn));
                                    while ($tcust = mysqli_fetch_array($mcust)) {
                                        echo "<option value='$tcust[accode]'>$tcust[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Product</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select id="product" name="product" class="form-control form select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $gproduct['code'] : " "; ?>"><?php echo ($addflag != 1) ? $gproduct['name'] : " "; ?></option>
                                    <?php
                                    $mitem = mysqli_query($conn, "SELECT code, `name` FROM `rawmat` where `code`!='010'") or die(mysqli_error($conn));
                                    while ($titem = mysqli_fetch_array($mitem)) {
                                        echo "<option value='$titem[code]'>$titem[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">CBM</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="cbm" value="<?php echo ($addflag != 1) ? $get['cbm'] : ''; ?>" class="form-control text-right form">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control"> Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="amount" value="<?php echo ($addflag != 1) ? $get['amount'] : ''; ?>" class="form-control text-right form" required>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Status</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <select id="status" name="status" class="form-control form select2">
                                    <option value="<?php echo ($addflag != 1) ? $get['status'] : "U"; ?>"><?php echo ($addflag != 1) ? $status : "Unpaid"; ?></option>
                                    <option value="U">Unpaid</option>
                                    <option value="P">Paid</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Item / Description</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="descrip" value="<?php echo ($addflag != 1) ? $get['descrip'] : ''; ?>" class="form-control form">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Importer</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input list='importer' name='importer' value="<?php echo ($addflag != 1) ? $get["importer"] : '' ?>" class='importer form-control form ' autocomplete="off">
                                <datalist id='importer' char="#">
                                    <option value=''></option>
                                    <?php
                                    $gimporter = mysqli_query($conn, "SELECT DISTINCT(`importer`) FROM `$maindb` WHERE `vouchno`!='' order by `importer`") or die(mysqli_error($conn));
                                    while ($mimporter = mysqli_fetch_array($gimporter)) {
                                        $aname = $mimporter['importer'];
                                        echo "<option value='$aname'>";
                                    }
                                    ?>
                                </datalist>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Mode</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select id="mode" name="mode" class="form-control form select2">
                                    <option value="<?php echo ($addflag != 1) ? $get['mode'] : " "; ?>"><?php echo ($addflag != 1) ? $get['mode'] : " "; ?></option>
                                    <option value="Sea">Sea</option>
                                    <option value="Air">Air</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Dispatch Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" name="dispdate" value="<?php echo ($addflag != 1) ? $get['dispdt'] : ''; ?>" class="form-control form dispdate">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">No. Of Days</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="number" name="nodays" value="<?php echo ($addflag != 1) ? $get['nodays'] : ''; ?>" class="form-control form nodays">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Expected Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" name="expdate" value="<?php echo ($addflag != 1) ? $get['expdate'] : ''; ?>" class="form-control form expdate">
                            </div>
                        </div>
                        <div class="row bg-row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Advance</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="advance" value="<?php echo ($addflag != 1) ? $get['advance'] : ''; ?>" class="form-control text-right form">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Type</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <select id="type" name="type" class="form-control type form select2">
                                    <option value="<?php echo ($addflag != 1) ? $get['type'] : " "; ?>"><?php echo ($addflag != 1) ? $paytype : " "; ?></option>
                                    <option value="C">Cash</option>
                                    <option value="B">Bank</option>
                                    <option value="U">UPI</option>
                                </select>
                            </div>
                        </div>
                        <div class="row bg-row cashrow">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad cashdiv">
                                <label class="label-control">Cash</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad cashdiv">
                                <select id="cash" name="cash" class="form-control cash form select2">
                                    <option value="<?php echo ($addflag != 1) ? $gcash['accode'] : " "; ?>"><?php echo ($addflag != 1) ? $gcash['name'] : " "; ?></option>
                                    <?php
                                    $mcash = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where `grpcode`='002'") or die(mysqli_error($conn));
                                    while ($tcash = mysqli_fetch_array($mcash)) {
                                        echo "<option value='$tcash[accode]'>$tcash[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row bg-row bankrow">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad bankdiv">
                                <label class="label-control">Bank</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad bankdiv">
                                <select id="bank" name="bank" class="form-control bank form select2">
                                    <option value="<?php echo ($addflag != 1) ? $gbank['accode'] : " "; ?>"><?php echo ($addflag != 1) ? $gbank['name'] : " "; ?></option>
                                    <?php
                                    $mbank = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where `grpcode`='001'") or die(mysqli_error($conn));
                                    while ($tbank = mysqli_fetch_array($mbank)) {
                                        echo "<option value='$tbank[accode]'>$tbank[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">No. Of Cartons</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="cartonsno" value="<?php echo ($addflag != 1) ? $get['cartonsno'] : ''; ?>" class="form-control text-right form">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Recd Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" name="recddate" value="<?php echo ($addflag != 1) ? $get['recddate'] : '0000-00-00'; ?>" class="form-control form recddate">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Due</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="number" name="duedays" value="<?php echo ($addflag != 1) ? $get['duedays'] : ''; ?>" class="form-control form duedays">
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?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 table-responsive table-bordered">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr style="background-color: #78909C">
                                    <th>Order No.</th>
                                    <th>Voucher Date</th>
                                    <th>Order Date</th>
                                    <th>Vendor</th>
                                    <th>Customer</th>
                                    <th>Product</th>
                                    <th class="text-right">CBM</th>
                                    <th class="text-right">Amount</th>
                                    <th>Importer</th>
                                    <th>Mode</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` ") or die(mysqli_error($conn));
                                while ($pmain = mysqli_fetch_array($srchresult)) {

                                    $gvend = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[vendor]'") or die(mysqli_error($conn));
                                    $fvend = mysqli_fetch_array($gvend);

                                    $gcust = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[customer]'") or die(mysqli_error($conn));
                                    $fcust = mysqli_fetch_array($gcust);

                                    $gprod = mysqli_query($conn, "SELECT * FROM `rawmat` WHERE `code`='$pmain[product]'") or die(mysqli_error($conn));
                                    $fprod = mysqli_fetch_array($gprod);

                                    $ctr++;
                                    echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$pmain[$keyfld]'>";
                                    echo "<td class='text-center'>$pmain[orderno]</td>";
                                    echo "<td>$pmain[vouchdt]</td>";
                                    echo "<td>$pmain[orderdt]</td>";
                                    echo "<td>$fvend[name]</td>";
                                    echo "<td>$fcust[name]</td>";
                                    echo "<td>$fprod[name]</td>";
                                    echo "<td class='text-right'>$pmain[cbm]</td>";
                                    echo "<td class='text-right'>$pmain[amount]</td>";
                                    echo "<td>$pmain[importer]</td>";
                                    echo "<td>$pmain[mode]</td>";
                                    echo "<td align='center'><a href=\"$file?mode=edit&$keyfld=$pmain[$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>