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

$formname = 'Purchase Return';
$maindb = 'debmain';
$trandb = 'debtran';
$accdb = 'actran';
$stkdb = 'transacn';

$file = 'debnote.php';
$grid = '';
$keyfld = "vouchno";
$trandbexist = 1;
$focusfld = "party";
$printfile = '';
$source = "PR";
$append_file = 'debnote_add.php';
$printfile = 'purretprin.php';

$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;
    $party = trim($_POST["cmbparty"]);
    $godown = trim($_POST["cmbgodown"]);
    $billdate = trim($_POST["vouchdt"]);
    $type = trim($_POST["type"]);
    $billno = trim($_POST["invno"]);
    $billdt = trim($_POST["invdt"]);
    $cashcred = trim($_POST["cashcred"]);
    $subtotal = trim($_POST["subtotal"]);
    $vatperc = trim($_POST["vatperc"]);
    $vat = trim($_POST["vat"]);
    $chkstock = trim($_POST["chkstock"]);
    $chkledg = trim($_POST["chkledg"]);
    $discperc = trim($_POST["discperc"]);
    $discount = trim($_POST["discount"]);
    $billamt = trim($_POST["netamt"]);
    $packing = trim($_POST["packing"]);
    $advance = trim($_POST["advance"]);
    $balance = trim($_POST["balance"]);
    $saletype = trim($_POST["saletype"]);
    $nobox = trim($_POST["nobox"]);
    $subqty = trim($_POST["subqty"]);

    $rem1 = trim($_POST["rem1"]);
    $rem2 = trim($_POST["rem2"]);

    $count = $_POST["items"];
    $mvouch = $qvouch;

    $salecode = '';
    if ($type == 1) {
        $salecode = 14;
    } else {
        $salecode = 15;
    }

    if ($billamt <= 0) {
        browse("Bill Amount Cannot Be Zero");
        $a = 0;
    }

    if (empty($party)) {
        $a = 0;
        browse("Party Cannot Be Empty");
    }


    if ($a == 1) {

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

        $cnt = 0;
        $cntamt = 0;

        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mitem = $_POST["item_$i"];
            $barcode = $_POST["barcode_$i"];
            $qty = $_POST["qty_$i"];
            $rate = $_POST["rate_$i"];
            $sell = $_POST["selling_$i"];
            $mrp = $_POST["mrp_$i"];
            $amt = $_POST["amt_$i"];
            $discperc = $_POST["discperc_$i"];
            $discamt = $_POST["discamt_$i"];
            $vatperc = $_POST["vatperc_$i"];
            $vatamt = $_POST["vatamt_$i"];
            $final = $_POST["final_$i"];

            if (!empty($mitem)) {
                $mitemcode = mysqli_query($conn, "SELECT code FROM `item` WHERE name='$mitem'") or die(mysqli_error($conn));
                $icode = mysqli_fetch_array($mitemcode);
//                browse('1');
                mysqli_query($conn, "INSERT INTO `$trandb`(vouchno, vouchdt, srno, itemcode, `barcode`, qty, rate, mrp, amt, discperc, discamt, vatperc,
                                                    vatamt, totamt)
                            VALUES('$mvouch', '$billdate', '$msrno', '$icode[code]', '$barcode', '$qty', '$rate', '$mrp', '$amt', '$discperc', '$discamt',  
                                    '$vatperc', '$vatamt', '$final')") or die(mysqli_error($conn));
            }
        }

        ######################### STOCK PART ####################################################
        $chkstock = 1;

        IF ($chkstock == 1) {
            mysqli_query($conn, "DELETE FROM `$stkdb` WHERE vouchno='$mvouch' AND `source`='$source'") or die(mysqli_error($conn));

            for ($i = 0; $i < $count; $i++) {
                $msrno = $_POST["srno_$i"];
                $mitem = $_POST["item_$i"];
                $mqty = $_POST["qty_$i"] * -1;
                $mrate = $_POST["rate_$i"];
                $mamt = $_POST["amt_$i"];
                $mcost = $_POST["cost_$i"];
                $msell = $_POST["selling_$i"];
                $mmrp = $_POST["mrp_$i"];

                if (!empty($mitem)) {

                    $mtrancode = mysqli_query($conn, "SELECT code FROM `item` WHERE name='$mitem'") or die(mysqli_error($conn));
                    $itrcode = mysqli_fetch_array($mtrancode);

                    mysqli_query($conn, "INSERT INTO `$stkdb`(vouchno, `godown`, `vouchdt`, `source`,  srno, itemcode,  qty,
                                                    rate, amount,  mrp )
                            VALUES('$mvouch','$godown', '$billdate','$source',  '$msrno', '$itrcode[code]', '$mqty',
                                   '$mrate', '$mamt', '$mmrp')") or die(mysqli_error($conn));
                }
            }
        }
############################### Accounts Part ######################################################
//        IF ($chkledg == 1) {
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mvouch' AND `source` = '$source'") OR die(mysqli_error($conn));
        if ($billamt > 0) {
            $minus_amt = ($billamt * -1);

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

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                     VALUES ('$billdate','$mvouch', '$source', '002', '$party','009', '$minus_amt')");
        }
//        }
//        $billamt = round($total - $discount + $vat + $packing);
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `accode`, godown, `type`, `billno`, `billdt` , `chkstock`, `chkledg`,
                                                    `cashcred`, subtotal, subqty, discount, vat,  billamt, `rem1`, `rem2`, `packing`, `nobox`)
                                          VALUES('$mvouch', '$billdate', '$party', '$godown', '$type', '$billno', '$billdt', '$chkstock', '$chkledg',
                                              '$cashcred', '$subtotal', '$subqty', '$discount', '$vat', '$billamt', '$rem1', '$rem2', '$packing', '$nobox')") or die(mysqli_error($conn));
            insmess();
//            $mvouch = $qvouch;
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `accode`='$party', `type`='$type', `billno`='$billno', `vouchdt`='$billdate', `billdt`='$billdt', 
                                                godown='$godown', cashcred='$cashcred', subtotal='$subtotal', subqty='$subqty', discount='$discount', vat='$vat',
                                                `chkstock`='$chkstock', billamt='$billamt', `rem1`='$rem1', `rem2`='$rem2', `packing`='$packing',
                                                `chkledg`='$chkledg', `nobox`='$nobox' WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

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


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

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

    $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`= '$get[godown]'") or die(mysqli_error($conn));
    $getgd = mysqli_fetch_array($getgodown);
}


/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$stkdb", "$delcond", "$file?mode=last");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");
}
include("includes/files.php");
include("includes/combo2.php");
?>

<script src="MyScript/checkdate.js" type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function () {
        $("body").on("keyup, blur", '.qty, .rate, .discperc', calculateRow);
        $("body").on("keyup, blur", '.subtotal, .discount, .vat, .packing', calculat)
        $("#main_container").on("click", '.remove', remove);
        $('#data').on('change', '.item', showdtl);
        $('#data').on('blur', '.mrp', addrow2);
        $('#data').on('keyup', '.qty', totqty);

        //getting rate from item
        function showdtl() {
            var $row = $(this).closest("tr");
            var name = $row.find('.item').val();
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "show_item_dtl.php",
                data: {name: name},
                success: function (response) {
                    var barcode = response.barcode;
                    var rate = response.rate;
                    $($row.find('.barcode')).val(barcode);
                    $($row.find('.rate')).val(rate);
                },
                error: function (request, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        }

//for calculating Row values                
        function calculateRow() {
            var fintot = 0;

            var $row = $(this).closest("tr");
            $row.find('.total').val("0");
            $row.find('.amt').val("0");
            $row.find('.discamt').val("0");
            $row.find('.vatamt').val("0");
            $row.find('.final').val("0");

            var qty = parseFloat($row.find('.qty').val());
            var rate = parseFloat($row.find('.rate').val());
            var mrp = parseFloat($row.find('.mrp').val());
            var discperc = parseFloat($row.find('.discperc').val());
            var discamt = parseFloat($row.find('.discamt').val());
//            var packamt = parseFloat($row.find('.packing').val());

            var totamt = qty * rate;
            $row.find('.amt').val(totamt);

            parseFloat($row.find('.final').val(totamt));
            //            var final = parseFloat($row.find('.final').val());
            var mamt = $row.find('.amt').val();

            var mdisc = mamt * discperc / 100;
            parseFloat($row.find('.discamt').val(mdisc)).toFixed(2);
            var tdisc = parseInt($row.find('.discamt').val());

            var totdisc = mamt - tdisc;

            fintot = totamt - tdisc;


            if (isNaN(fintot)) {
                $row.find('.final').val("0");
            } else {
                $row.find('.final').val(fintot);
                /*$row.find('.final').val(fintot.toFixed(2));-*/
            }
            calculateTot();
        }

        function calculateTot() {
            var amt = 0;
            var val = 0;
            var val1 = 0;
            var val2 = 0;
            var disc = 0;
            var vata = 0;
            /*********Total Part************/

            $(".amt").each(function () {
                if (!isNaN(this.value) && this.value.length != 0) {
                    val += parseFloat(this.value);
                }
            });
            amt = parseFloat(val);

            $("#subtotal").val(amt.toFixed(2));

            $(".discamt").each(function () {
                if (!isNaN(this.value) && this.value.length != 0) {
                    val1 += parseFloat(this.value);
                }
            });
            disc = parseFloat(val1);

            $("#discount").val(disc.toFixed(2));

            calculat();
        }

//        $("#packing").keyup(function () {
//            var value = parseFloat($("#netamt").val());
//            var value2 = parseFloat($("#packing").val());
//
//            if (value2 === 0) {
//                calculat();
//            } else {
//                var tot = value + value2;
//                $('#netamt').val(tot.toFixed(2));
//            }
//        });

        function calculat() {
            var mamt = parseFloat($("#subtotal").val());
            var mdisc = parseFloat($("#discount").val());
            var mpack = parseFloat($("#packing").val());

//            $('#discount').removeAttr("readonly");



            var mnet = mamt - mdisc;
            $('#netamt').val(mnet.toFixed(2));

        }

        //        for deleting single row
        function remove() {
            $(this).closest("tr").remove();
        }

        /*Hidind delte part*/
        function show_hide_delete() {
            if ($("#del_all").prop("checked")) {
                $(".remove").hide();
            } else {
                $(".remove").show();
            }

        }

        $("#del_all").attr("checked", true);
        show_hide_delete();
        $("#del_all").click(function () {
            show_hide_delete();
        });

        //                for adding new rows
        function addrows() {
            var ctr = $('#items').val();
            var hide_check = "";
            if ($("#del_all").prop("checked")) {
                hide_check = 0;
            } else {
                hide_check = 1;
            }
            var detail = ".fetchpart_" + ctr;


            $.post('debnote_add.php', {ctr: ctr, hide: hide_check}, function (data) {
                $(data).appendTo('#data');
                ctr++;
                $('#items').val(ctr);
                $(detail).select2().trigger("change");

            });
        }

        $('#addnew').click(function () {
            addrows();
        });

        function addrow2() {
            if ($(this).closest("tr").is(":last-child")) {
                addrows();
            }
        }


        function totqty() {
            var val = 0;
            var amt = 0;
            $(".qty").each(function () {
                if (!isNaN(this.value) && this.value.length != 0) {
                    val += parseFloat(this.value);
                }
            });
            amt = parseFloat(val);
            $("#subqty").val(amt.toFixed(2));
        }


    });
</script>
</head>
<body onLoad="" id="main_container">
    <form name="purchase" action="" method="POST">

        <div class="row" style="">
            <div class="col-lg-12" style=''>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5 style="font-family: cipher-regular,helvetica; "><?= $formname ?></h5>
                        <!-- .toolbar -->
                        <div class="toolbar">
                            <ul class="nav">
                                <li>
                                    <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-1">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>
                            </ul>
                        </div>
                        <!-- /.toolbar --> 
                    </header>
                    <div id="div-1" class="accordion-body collapse in body" style="font-family: cipher-regular,helvetica;">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-lg-1"  style="font-family: cipher-regular,helvetica;">Voucher</label>
                                <div class="col-lg-1">
                                    <input type="text" name="vouchno" id='vouchno' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>" style="width:150px;" class="form-control form"  readonly>
                                    <input type="date" value="<?= $zStartDate ?>" style="display: none" class="zstdate">
                                    <input type="date" value="<?= $zEndDate ?>" style="display: none" class="zenddate">
                                </div>
                                <label class="control-label col-lg-1"  style="width:135px;font-family: cipher-regular,helvetica;">Date</label>
                                <div class="col-lg-2">
                                    <input type="date" name="vouchdt" id='vouchdt' placeholder="" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date("Y-m-d"); ?>" style="width:175px;" class="form-control form vouchdt" >
                                </div> 
                                <label class="control-label col-lg-1"  style="font-family: cipher-regular,helvetica;">Invoice No.</label>
                                <div class="col-lg-1">
                                    <input type="text" name="invno" id='invno' placeholder="" value="<?php echo ($addflag != 1) ? $get['billno'] : ''; ?>" style="width:150px;" class="form-control form">                                    
                                </div>
                                <label class="control-label col-lg-1"  style="width:134px;font-family: cipher-regular,helvetica;">Date</label>
                                <div class="col-lg-2">
                                    <input type="date" name="invdt" id='invdt' placeholder="" value="<?php echo ($addflag != 1) ? $get['billdt'] : date("Y - m - d"); ?>" style="width:175px;" class="form-control form" >                                    
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-1"  style="font-family: cipher-regular,helvetica;">Party Name</label>
                                <div class="col-lg-4">
                                    <select id="party" name="cmbparty" class="form-control form" style='height:33px;' required>
                                        <option value="<?php echo ($addflag != 1) ? $getp['accode'] : " "; ?>"><?php echo ($addflag != 1) ? $getp['name'] : " "; ?></option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` WHERE grpcode='009' ORDER BY name") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mown)) {
                                            echo "<option value=$trow[accode]>$trow[name]</option>";
                                        }
                                        ?>
                                    </select> 
                                </div>
                                <label class="control-label col-sm-1"  style="width:134px;font-family: cipher-regular,helvetica;">Godown</label>
                                <div class="col-lg-1">
                                    <select id="godown" name="cmbgodown" class="form-control form" style='height:33px;'>
                                        <option value="<?php echo ($addflag != 1) ? $getgd['code'] : " "; ?>"><?php echo ($addflag != 1) ? $getgd['gdcode'] : " "; ?></option>
                                        <?php
                                        $mown1 = mysqli_query($conn, "SELECT code, `gdcode` FROM `godown` ORDER BY name") or die(mysqli_error($conn));
                                        while ($trow1 = mysqli_fetch_array($mown1)) {
                                            echo "<option value=$trow1[code]>$trow1[gdcode]</option>";
                                        }
                                        ?>
                                    </select> 
                                </div>
                                <label class="control-label col-lg-1"  style="width:135px;font-family: cipher-regular,helvetica;">Type</label>
                                <div class="col-lg-1">
                                    <select name='cashcred' id="cashcred"  class="form-control form" style='height:33px;width:150px;'>
                                        <option value='R'<?php
                                        if ($addflag != 1) {
                                            if ($get["cashcred"] == "R") {
                                                echo "selected='selected'";
                                            }
                                        }
                                        ?> >CREDIT</option>
                                        <option value='C'<?php
                                        if ($addflag != 1) {
                                            if ($get["cashcred"] == "C") {
                                                echo "selected='selected'";
                                            }
                                        }
                                        ?>>CASH</option>
                                    </select> 
    <!--                            <select name="type" class="form-control form" style="width:175px;">
                                    <option value="<?php // echo ($addflag != 1) ? $get['type'] : " ";                        ?>"><?php echo ($addflag != 1) ? $get['type'] : " "; ?></option>
                                    <option value=""></option>
                                    <option value=" Cash  ">Cash</option>
                                    <option value=" Credit">Credit</option>                                            
                                </select>-->
                                </div>




                            </div> 

                            <div class="table-responsive">
                                <table id="data" align="center" border="1px" width="100%" class="table-bordered">
                                    <thead>
                                        <tr>
                                            <th width="2%"  valign="center" style="padding-left: 15px;padding-bottom: 5px;"> <label class="control-label"><input type="checkbox" id="del_all" class=" form"></label>
                                            </th>
                                            <?php
                                            $dtlbkcol = '#d59fcaff';
                                            ?>
                                            <th width="4%" style="font-family: arial-regular;background-color: <?= $dtlbkcol; ?>; " align="center"><label class="control-label">Sr.</label></th>
                                            <th width="15%" style="font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;" align="center"><label class="control-label">Barcode</label></th>
                                            <th width="25%" style="font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;" align="center"><label class="control-label">Item Name</label></th>
                                            <th width="7%" align="right" style="text-align:right;font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;"><label class="control-label">Qty.</label></th>
                                            <th width="7%" align="right" style="text-align:right;font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;"><label class="control-label">Rate</label></th>
                                            <th width="8%" align="right" style="text-align:right;color:#008BEE;font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;"><label class="control-label">Amount</label></th>
                                            <th width="6%" align="right" style="text-align:right;font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;"><label class="control-label">Disc. %</label></th>
                                            <th width="9%" align="right" style="text-align:right;font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;"><label class="control-label">Discount</label></th>
                                            <th width="10%" align="right" style="text-align:right;color:#CA195A;font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;"><label class="control-label">Total</label></th>
                                            <th width="10%" align="right" style="text-align:right;font-family: arial-regular;background-color: <?= $dtlbkcol; ?>;"><label class="control-label">MRP</label></th>
                                        </tr>
                                    </thead>
                                    <?php
                                    if ($addflag != 1) {
                                        $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));

                                        $ctr = 0;
                                        while ($row = mysqli_fetch_array($qry)) {
                                            $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
                                            $geti = mysqli_fetch_array($getitem);

                                            echo "<tr>";
                                            echo '<td style="height:30px;" align="center" valign="top"><input type="image" src="delbut.png" name="remove"  value="remove" class="remove"/></td>';
                                            echo "<td style='height:30px;' align='left' valign='center'><input type='text' size='6' maxlength='6' name='srno_" . $ctr . "' class='srno form-control form' style='height:29px;' value='$row[srno]'/></td>";
                                            echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='barcode_$ctr' class='barcode form-control  form' style='height:29px;text-align:left;' value='$row[barcode]'/></td>";

                                            echo "<td style='height:30px;' align='left' valign='center'>";
                                            echo "<select name='item_$ctr' style='height:29px;width:100%' class='item form-control form'>";
                                            echo "<option value='$geti[name]'>$geti[name]</option>";
                                            $mitem = mysqli_query($conn, "SELECT * FROM `item` ORDER BY `name`") or die(mysqli_error($conn));
                                            while ($trow1 = mysqli_fetch_array($mitem)) {
                                                echo "<option value='$trow1[name]' >$trow1[name]</option>";
                                            }
                                            echo "</select>";
                                            echo "</td>";
                                            echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='qty_$ctr' class='qty form-control form' style='height:29px;text-align:right;' value='$row[qty]'/></td>";
                                            echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='rate_$ctr' class='rate form-control form' style='height:29px;text-align:right;' value='$row[rate]'/></td>";
                                            echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='amt_$ctr' class='amt form-control form' style='height:29px;text-align:right;' value='$row[amt]'  readonly/></td>";
                                            echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='discperc_$ctr' class='discperc form-control form' style='height:29px;text-align:right;' value='$row[discperc]'/></td>";
                                            echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='discamt_$ctr' class='discamt form-control form' style='height:29px;text-align:right;' value='$row[discamt]' readonly/></td>";
                                            echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='final_$ctr' class='final form-control form' style='height:29px;text-align:right;' value='$row[totamt]'  readonly/></td>";
                                            echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='mrp_$ctr' class='mrp form-control form' style='height:29px;text-align:right;' value='$row[mrp]'/></td>";
                                            echo "</tr>";
                                            $ctr++;
                                        }
                                        //browse($ctr);
                                    } else {
                                        $pctr = 0;
                                        $yctr = 0;
                                        for ($i = $pctr; $i < 10; $i++) {
                                            $yctr++;
                                            echo "<tr>";
                                            echo '<td align="center" style="height:30px;" valign="center"><input type="image" src="delbut.png" name="remove"  value="remove" class="remove"/></td>';
                                            echo "<td style='height:30px;' align='left' valign='center'><input type='text' size='6' maxlength='6' name='srno_$pctr' class='sno form-control form' style='height:29px;' value='$yctr'/></td>";

                                            echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='barcode_$pctr' class='barcode form-control form' style='height:29px;text-align:left;' value='' /></td>";
                                            echo "<td align='left' style='height:30px;'>";
                                            ?>
                                            <select name = "item_<?= $pctr ?>" class = "item form-control form" style = 'height:29px;width:100%;'>
                                                <option value = ""></option>
                                                <?php
                                                $mitem = mysqli_query($conn, "SELECT * FROM `item` ORDER BY `name`") or die(mysqli_error($conn));
                                                while ($trow1 = mysqli_fetch_array($mitem)) {
                                                    echo "<option value='$trow1[name]' >$trow1[name]</option>";
                                                }
                                                ?>
                                            </select>
                                            <?php
                                            echo "</td>";


                                            echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='qty_$pctr' class='qty form-control form' style='height:29px;text-align:right;' value='0' /></td>";
                                            echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='rate_$pctr' class='rate form-control form' style='height:29px;text-align:right;' value='0'/></td>";
                                            echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='amt_$pctr' class='amt form-control form' style='height:29px;text-align:right;' value='0'  readonly/></td>";
                                            echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='discperc_$pctr' class='discperc form-control form' style='height:29px;text-align:right;' value='0'/></td>";
                                            echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='discamt_$pctr' class='discamt form-control form' style='height:29px;text-align:right;' value='0' readonly/></td>";
                                            echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='final_$pctr' class='final form-control form' style='height:29px;text-align:right;' value='0'  readonly/></td>";
                                            echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='mrp_$pctr' class='mrp form-control form' style='height:29px;text-align:right;' value='0'/></td>";
                                            echo "</tr>";
                                            $pctr++;
                                        }
                                    }
                                    ?>
                                </table>
                                <!--                        <label class="control-label col-lg-3"></label>
                                                        <div class="col-lg-8">
                                                            <button class="btn btn-lg btn-success form" data-toggle="modal" data-target=".slacker-modal" style="width: 100%;">Detail</button>
                                                        </div>-->
                            </div>
                            <div class="form-group" style="padding-left: 13px;">
                                <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />

                                <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $pctr; ?>" />  
                                <!--<input type="hidden" id="items" name="items" value="<?php // echo ($addflag != 1) ? $ctr : 1;                        ?>" />-->  

                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-5"></label>
                                <label class="control-label col-lg-5" style="color:green;">Sub Total</label>
                                <div class="col-lg-2">
                                    <input type="number" step="any" name="subtotal" id='subtotal' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['subtotal'] : ''; ?>" style="text-align: right;" class="form-control form" readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-5"></label>
                                <label class="control-label col-lg-5" style="color:green;">Total Qty</label>
                                <div class="col-lg-2">
                                    <input type="number" step="any" name="subqty" id='subqty' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['subqty'] : ''; ?>" style="text-align: right;" class="form-control form" readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-5"></label>
                                <label class="control-label col-lg-5">Less Discount</label>
                                <div class="col-lg-2">
                                    <input type="number" step="any" name="discount" id='discount' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['discount'] : ''; ?>" style="text-align: right;" class="form-control form" readonly>
                                </div>
                            </div>
                            <!--                            <div class="form-group">
                                                            <label class="control-label col-lg-6"></label>
                                                            <label class="control-label col-lg-2">No. of Boxes</label>
                                                            <div class="col-lg-1">
                                                                <input type="number" step="any" name="nobox" id="nobox" autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['nobox'] : '0'; ?>" style="text-align: right;" class="form-control form" >
                                                            </div>
                                                            <label class="control-label col-lg-1">Packing</label>
                                                            <div class="col-lg-2">
                                                                <input type="number" step="any" name="packing" id="packing" autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['packing'] : '0'; ?>" style="text-align: right;" class="packing form-control form">
                                                            </div>
                                                        </div>-->
                            <!--                            <div class="form-group">
                                                            <label class="control-label col-lg-4"></label>
                                                            <label class="control-label col-lg-2">No. of Boxes</label>
                                                            <div class="col-lg-1">
                                                                <input type="number" step="any" name="nobox" id='nobox' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['nobox'] : ''; ?>" style="text-align: right;" class="form-control form">
                                                            </div>
                                                            <label class="control-label col-lg-3">Packing</label>
                                                            <div class="col-lg-2">
                                                                <input type="number" step="any" name="packing" id='packing' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['packing'] : ''; ?>" style="text-align: right;" class="form-control form">
                                                            </div>
                                                        </div>-->
                            <!--                            <div class="form-group">
                                                            <label class="control-label col-lg-4"></label>
                                                            <label class="control-label col-lg-5">Add VAT</label> 
                                                            <div class="col-md-1" style="width: 82px;">
                                                                <input type="number" step="any" name="percvat" id="percvat" autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['percvat'] : ''; ?>" style="text-align: right;width: 67px;" class="form-control form" disabled="disabled">
                                                            </div>
                                                            <label style="width:10px;" class="control-label col-lg-1">%</label>
                                                            <div class="col-lg-2">
                                                                <input type="number" step="any" name="vat" id="vat" autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['vat'] : ''; ?>" style="text-align: right;" class="form-control form" readonly="" disabled="disabled">
                                                            </div>
                                                        </div>-->
                            <!--                            <div class="form-group">
                                                            <label class="control-label col-lg-3"></label>
                                                            <label class="control-label col-lg-5">Add Vat</label> 
                                                            <div class="col-md-1" style="width: 25px;">
                                                                <input type="number" step="any" name="percvat" id='percvat' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['percvat'] : ''; ?>" style="text-align: right;width:20px;" class="form-control form">
                                                            </div>
                                                            <div class="col-lg-2">
                                                                <input type="number" step="any" name="vat" id='vat' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['vat'] : ''; ?>" style="text-align: right;" class="form-control form" readonly>
                                                            </div>
                                                        </div>-->
                            <!--                            <div class="form-group">
                            
                                                            <label class="control-label col-lg-5"></label>
                                                            <label class="control-label col-lg-5">Add Packing</label>
                                                            <div class="col-lg-2">
                                                                <input type="number" step="any" name="packing" id='packing' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['packing'] : '0'; ?>" style="text-align: right;" class="form-control form">
                                                            </div>
                                                        </div>-->
                            <div class="form-group">
                                <label class="control-label col-lg-5"></label>
                                <label class="control-label col-lg-5" style="color:red;">Net Amount</label>
                                <div class="col-lg-2">
                                    <input type="number" step="any" name="netamt" id='netamt' autofocus="" placeholder="" value="<?php echo ($addflag != 1) ? $get['billamt'] : ''; ?>" style="text-align: right;" class="form-control form" readonly>
                                </div>
                            </div>
                            <!--                    <div class="form-group">
                                                    <label class="control-label col-lg-2"  style="width: 150px;font-family: cipher-regular,helvetica;"></label>
                                                    <div class="col-lg-8">
                                                        <textarea rows="3" cols="50" name="word" class="form-control form" id="word"  ><?php rsword($get['netamt']); ?></textarea>
                                                    </div>
                                                </div>-->
                            <div style="" class="form-group">
                                <label class="control-label col-lg-2"  style="width: 150px;font-family: cipher-regular,helvetica;">Remarks</label>
                                <div class="col-lg-4">
                                    <input type="text" name="rem1" id="rem1" value="<?php echo ($addflag != 1) ? $get['rem1'] : ''; ?>" class="form-control form">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-2"  style="width: 150px;font-family: cipher-regular,helvetica;"></label>
                                <div class="col-lg-4">
                                    <input type="text" name="rem2" id="rem2" value="<?php echo ($addflag != 1) ? $get['rem2'] : ''; ?>" class="form-control form">
                                </div>
                            </div>

                        </div>
                    </div>
                    <div class="box-footer" style=""> 
                        <?php include("includes/buttons.php"); ?>
                    </div>
                    <!--/box-end--> 


                </div>
            </div>
        </div>
    </form>
</body>
<!--SEARCH PART-->
<div class="modal fade in slacker-modal" id="searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="false">
    <div class="modal-dialog modal-slacker">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myLargeModalLabel"></h4>
            </div>
            <div class="modal-body" style="height: 100%;white-space: nowrap;">
                <div class="row">

                    <div class="col-md-12">

                        <div class="col-lg-12">

                            <div class="box">
                                <div id="collapse4" class="body">
                                    <table  class="table table-bordered table-condensed table-hover table-striped">
                                        <thead>
                                            <tr>
                                                <th align='left'>vouchno</th>
                                                <th align='left'>vouchdt</th>
                                                <th align='left'>Godown</th>
                                                <th align='left'>Party</th>
                                                <th align='left'>Type</th>
                                                <th align='left'>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld!=''") or die(mysqli_error($conn));
                                            while ($row = mysqli_fetch_array($srchresult)) {
                                                $getgo = mysqli_query($conn, "SELECT * FROM `godown` where `code`='$row[godown]'")or die(mysqli_error($conn));
                                                $go = mysqli_fetch_array($getgo);
                                                $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$row[accode]'")or die(mysqli_error($conn));
                                                $party = mysqli_fetch_array($getparty);
                                                $cashcr = "";
                                                if ($row["cashcred"] == "C") {
                                                    $cashcr = "Cash";
                                                } else {
                                                    $cashcr = "Credit";
                                                }
                                                echo "<tr>";
                                                echo "<td align='left'>$row[vouchno]</td>";
                                                echo "<td align='left'>$row[vouchdt]</td>";
                                                echo "<td align='left'>$go[gdcode]</td>";
                                                echo "<td align='left'>$party[name]</td>";
                                                echo "<td align='left'>$cashcr</td>";
                                                echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><img src='images/edit.png'></a></td>";
                                                echo "</tr>";
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                    <script>
                                        $(function () {
                                            metisTable();
                                            metisSortable();
                                        });
                                    </script>

                                </div>

                            </div>

                        </div>

                    </div><!-- /.row -->

                </div>
            </div>  
            <div class="modal-footer" style="bottom: 0px;position: absolute;padding:0px;">
                <!--"This is where i want to load my salemain.php"-->
            </div>
        </div>
    </div>
</div>

</html>        

<?php // include("includes/footlinks.php");         ?>
<!--<script>
    $(function() {
        formGeneral();
    });
</script>-->
