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

$maindb = 'inwdmain';
$maindb_d = 'inwdmain_d';

$trandb = 'inwdtran';
$trandb_d = 'inwdtran_d';

$stkdb = 'transacn';
$stkdb_d = 'transacn_d';

$append_file = 'inward_add.php';
$formname = 'Inward';

$file = 'inward.php';
$grid = 'inwdmain.php';

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

$source = "IN";

mysqli_select_db($conn, $zdb);

if (isset($_GET["mode"]) == "true") {
    $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';
    }
    $gen2 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb_d` LIMIT 0,1 ") or die(mysqli_error($conn));
    if (mysqli_num_rows($gen2) > 0) {
        $cvvouch = mysqli_fetch_assoc($gen2);
        $dvouch = $cvvouch["maxcode"] + 1;
    } else {
        $dvouch = '1';
    }
    $flag = "Add";
} else {
    $addflag = 0;
    $qvouch = $_GET["vouchno"];
    $dvouch = $_GET["vouchno_d"];
    $flag = "Edit";
}

if (isset($_POST["submit"])) {
    $a = 1;
    $billdate    = trim($_POST["vouchdt"]);
    $party       = trim($_POST["cmbparty"]);
    $billamt     = trim($_POST["billamt"]);
    $totqty      = trim($_POST["totqty"]);
    $lrno        = trim($_POST["lrno"]);
    $lrdt        = trim($_POST["lrdt"]);
    $transporter = trim($_POST["transporter"]);

    $rem1 = $_POST["rem1"];
    $rem2 = $_POST["rem2"];

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

    if (empty($party)) {
        $a = 0;
        browse("Party Cannot Be Empty");
//        if ($addflag == 1) {
        //           header("location:javascript://history.go(-1)");
        //      } else {
        //         header("refresh: ; $file?vouchno=$_GET[vouchno]");
        //    }
    }


    if (empty($billdate)) {
        browse("Voucher Date Cannot Be Empty");
        $a = 0;
    }

    if (empty($totqty)) {
        browse("Total Qty. Cannot Be Zero");
        $a = 0;
    }

    if ($a == 1) {
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$trandb_d` WHERE vouchno_d='$mvouch'") or die(mysqli_error($conn));

        $cnt = 0;
        $cntamt = 0;

        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mitem = $_POST["item_$i"];
            $color = $_POST["color_$i"];

            $s5 = $_POST["s5_$i"];
            $s6 = $_POST["s6_$i"];
            $s7 = $_POST["s7_$i"];
            $s8 = $_POST["s8_$i"];
            $s9 = $_POST["s9_$i"];
            $s10 = $_POST["s10_$i"];
            $s11 = $_POST["s11_$i"];
            $s12 = $_POST["s12_$i"];
            $soth = $_POST["soth_$i"];
            $mqty = $_POST["qty_$i"];

            $mrp = $_POST["mrp_$i"];
            $mcost = $_POST["rate_$i"];
            $mamt = $_POST["amt_$i"];
            $selling = $_POST["selling_$i"];
            $costcode = $_POST["costcode_$i"];
            $fav = $_POST["fav_$i"];
            $location = $_POST["location_$i"];
            
            /*browse("ITEM ------------>".$mitem);
            browse("COLOR ------------>".$color);*/

            if (!empty($mitem) && !empty($color)) {
                
                ################################ CHECK EXISTING ITEMS #################################################
                $geti = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`='$mitem' ") or die(mysqli_error($conn));
                $iget = mysqli_fetch_assoc($geti);
                $mbarcode = $iget['barcode'];
                $mitemcode = $iget['code'];
                $grp = $iget['group'];

                $cnt = $cnt + 1;
                $cntamt = $cntamt + $mamt;

                mysqli_query($conn, "UPDATE `item` SET `fav`='$fav', `costcode`='$costcode', `mrp`='$mrp', `rate`='$selling', `cost`='$mcost', selling='$selling' WHERE code = '$mitemcode' ") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchno_d`, `srno`, `barcode`, `itemcode`,  
                                    `color`, s5, s6, s7, s8, s9, s10, s11, s12, soth, qty, mrp, rate, amount, selling, costcode, fav, location)
                        VALUES('$mvouch', '$dvouch', '$cnt', '$mbarcode', '$mitemcode',   
                               '$color', '$s5', '$s6', '$s7', '$s8', '$s9', '$s10', '$s11', '$s12', '$soth', '$mqty',
                               '$mrp', '$mcost', '$mamt', '$selling', '$costcode', '$fav', '$location' )") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `$trandb_d`(`vouchno`, `vouchno_d`, `srno`, `barcode`, `itemcode`, 
                                    `color`, s5, s6, s7, s8, s9, s10, s11, s12, soth, qty, mrp, rate, amount, selling, costcode, fav, location)
                        VALUES('$dvouch', '$mvouch', '$cnt', '$mbarcode', '$mitemcode', 
                               '$color', '$s5', '$s6', '$s7', '$s8', '$s9', '$s10', '$s11', '$s12', '$soth', '$mqty',
                               '$mrp', '$mcost', '$mamt', '$selling', '$costcode', '$fav', '$location')") or die(mysqli_error($conn));
            }
        }
        if ($admin == 1) {
            $mgroup = $source . $qvouch;
        } else {
            $mgroup = $source . $dvouch;
        }

        ######################### STOCK PART ####################################################

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

        $cnt = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mitem = $_POST["item_$i"];
            $color = $_POST["color_$i"];

            $s5 = $_POST["s5_$i"];
            $s6 = $_POST["s6_$i"];
            $s7 = $_POST["s7_$i"];
            $s8 = $_POST["s8_$i"];
            $s9 = $_POST["s9_$i"];
            $s10 = $_POST["s10_$i"];
            $s11 = $_POST["s11_$i"];
            $s12 = $_POST["s12_$i"];
            $soth = $_POST["soth_$i"];
            $mqty = $_POST["qty_$i"];

            $mrp = $_POST["mrp_$i"];
            $mcost = $_POST["cost_$i"];
            $mamt = $_POST["amt_$i"];

            if (!empty($color) && !empty($mitem)) {
                $geti = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`='$mitem' ") or die(mysqli_error($conn));
                $iget = mysqli_fetch_assoc($geti);
                $mbarcode = $iget['barcode'];
                $mitemcode = $iget['code'];
                $grp = $iget['group'];

                $cnt = $cnt + 1;

                mysqli_query($conn, "INSERT INTO `$stkdb`(`vouchno`, `vouchno_d`, `vouchdt`, `source`,  `srno`, 
                                    `barcode`, `itemcode`, `itemgrp`, `color`, 
                                    s5, s6, s7, s8, s9, s10, s11, s12, soth, qty, mrp, rate, amount, accode)
                        VALUES('$mvouch', '$dvouch', '$billdate', '$source', '$cnt', 
                               '$mbarcode', '$mitemcode', '$grp', '$color', 
                               '$s5', '$s6', '$s7', '$s8', '$s9', '$s10', '$s11', '$s12', '$soth', '$mqty',
                               '$mrp', '$mcost', '$mamt', '$party')") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `$stkdb_d`(`vouchno`, `vouchno_d`, `vouchdt`, `source`,  `srno`, 
                                    `barcode`, `itemcode`, `itemgrp`, `color`, 
                                     s5, s6, s7, s8, s9, s10, s11, s12, soth, qty, mrp, rate, amount, accode)
                        VALUES('$dvouch', '$mvouch', '$billdate', '$source', '$cnt', 
                               '$mbarcode', '$mitemcode', '$grp', '$color', 
                               '$s5', '$s6', '$s7', '$s8', '$s9', '$s10', '$s11', '$s12', '$soth', '$mqty',
                               '$mrp', '$mcost', '$mamt', '$party')") or die(mysqli_error($conn));
            }
        }

        $billamt = $cntamt;

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, vouchno_d, `vouchdt`, `name`, totqty, netamt, `rem1`, `rem2`, 
                                      `lrno`, `lrdt`, `transporter`)
                                VALUES('$qvouch', '$dvouch', '$billdate', '$party', '$totqty', '$billamt', '$rem1', '$rem2', 
                                       '$lrno', '$lrdt', '$transporter')") or die(mysqli_error($conn));
            mysqli_query($conn, "INSERT INTO `$maindb_d`(vouchno, vouchno_d, `vouchdt`, `name`, totqty, netamt, `rem1`, `rem2`, 
                                       `lrno`, `lrdt`, `transporter`)
                                VALUES('$dvouch', '$qvouch', '$billdate', '$party', '$totqty', '$billamt', '$rem1', '$rem2', 
                                       '$lrno', '$lrdt', '$transporter')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?vouchno=$mvouch");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb`   SET `vouchdt`='$billdate', `name`='$party', `rem1`='$rem1', `rem2`='$rem2', 
                                 `lrno`='$lrno', `lrdt`='$lrdt', `transporter`='$transporter', `totqty`='$totqty', 
                                `netamt`='$billamt' WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
            mysqli_query($conn, "UPDATE `$maindb_d`   SET `vouchdt`='$billdate', `name`='$party', `rem1`='$rem1', `rem2`='$rem2', 
                                 `lrno`='$lrno', `lrdt`='$lrdt', `transporter`='$transporter', `totqty`='$totqty', 
                                `netamt`='$billamt' WHERE vouchno_d = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?vouchno=$mvouch");
        }
    }
}

if ($addflag == 0) {
    $getvouch = $_GET["vouchno"];
    if ($admin == 1) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE vouchno='$getvouch'") or die(mysqli_error($conn));
        $get = mysqli_fetch_array($result);
    } else {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb_d` WHERE vouchno_d='$getvouch'") or die(mysqli_error($conn));
        $get = mysqli_fetch_array($result);
    }
    $getpname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`= '$get[name]'") or die(mysqli_error($conn));
    $getp = mysqli_fetch_array($getpname);

    $gettrans = mysqli_query($conn, "SELECT * FROM `transporter` WHERE `code`= '$get[transporter]'") or die(mysqli_error($conn));
    $trans = mysqli_fetch_array($gettrans);
}
?>

<!DOCTYPE HTML>
<html lang="en-US">

    <head>
        <meta charset="UTF-8">
        <title><?php echo $formname; ?></title>
        <meta name="msapplication-TileColor" content="#5bc0de"/>
        <meta name="msapplication-TileImage" content="assets/img/metis-tile.png"/>

        <link rel="stylesheet" href="assets/lib/bootstrap/css/bootstrap.css">
        <link rel="stylesheet" href="assets/css/main.css"/>
        <link rel="stylesheet" href="assets/lib/Font-Awesome/css/font-awesome.css"/>

        <link rel="stylesheet" href="assets/css/theme.css">


        <link rel="stylesheet" href="assets/lib/uniform/themes/default/css/uniform.default.css">
        <link rel="stylesheet" href="assets/lib/inputlimiter/jquery.inputlimiter.1.0.css">
        <link rel="stylesheet" href="assets/lib/chosen/chosen.min.css">
        <link rel="stylesheet" href="assets/lib/colorpicker/css/colorpicker.css">
        <link rel="stylesheet" href="assets/lib/tagsinput/jquery.tagsinput.css">
        <link rel="stylesheet" href="assets/lib/daterangepicker/daterangepicker-bs3.css">
        <link rel="stylesheet" href="assets/lib/datepicker/css/datepicker.css">
        <link rel="stylesheet" href="assets/lib/timepicker/css/bootstrap-timepicker.min.css">

        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

        <script>
            (function(i, s, o, g, r, a, m) {
                i['GoogleAnalyticsObject'] = r;
                i[r] = i[r] || function() {
                    (i[r].q = i[r].q || []).push(arguments)
                }, i[r].l = 1 * new Date();
                a = s.createElement(o),
                        m = s.getElementsByTagName(o)[0];
                a.async = 1;
                a.src = g;
                m.parentNode.insertBefore(a, m)
            })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

            ga('create', 'UA-1669764-16', 'onokumus.com');
            ga('send', 'pageview');

        </script>

        <script src="assets/lib/modernizr-build.min.js"></script>
        <script type="text/javascript">
            function keyPressed(evt)
            {
                var theEvent = evt || window.event;
                var k = theEvent.keyCode || theEvent.which;
                if (k == 89 || k == 78 || k == 8 || k == 46)
                    return true;
                else {
                    $.msgbox("Invalid Key. Press Y / N ");
                    // alert("Your Message");
                    evt.preventDefault();
                    return false;
                }
            }
        </script>
        <script type='text/javascript'>
            $(document).ready(function() {
                $('#data').on('keyup', '.s5, .s6, .s7, .s8, .s9, .s10, .s11, .s12, .soth, .qty, .rate', calculateRow);
                $('#data').on('keyup', '.item', getitems);
                $('#data').on('change', '.item', showdtl);
                $('#data').on('keyup', '.mrp', calculatemrp);
                $("#main_container" ).on( "click", '.remove', remove )


                function getitems() {

                    var ctr = parseFloat($('#items').val());
                    var mctr = ctr - 1;
                    //  alert(mctr);
                    var $row = $(this).closest("tr");
                    var item = $row.find('.item').val();
                    $.get('get_item_from_combo.php', {item: item}, function(data) {
                        $row.find('#item_' + mctr).html(data);
                    });
                }
                ;
                
                 function remove(){
                    $(this).closest("tr").remove();
                }

                /*
                 $(".item").keyup(function(){
                 var ctr = parseFloat($('#items').val());
                 var mctr = ctr -1;
                 var $row = $(this).closest("tr");
                 var item = $row.find('.item').val(); 
                 $.get('get_item_from_combo.php', {item: item}, function(data) {
                 $row.find('#item_'+mctr).html(data);
                 });
                 });
                 */



                function showdtl() {
                    var $row = $(this).closest("tr");
                    var item = $row.find('.item').val();
                    //alert(item);

                    $.ajax({
                        type: 'GET',
                        dataType: 'json',
                        url: "show_item_dtl.php",
                        data: "name=" + item + "",
                        success: function(response) {
                            /*   alert(response.code);*/
                            var code = response.code;
                            if (code === "0") {
                                $($row.find('.s5')).attr("readonly", true);
                                $($row.find('.s6')).attr("readonly", true);
                                $($row.find('.s7')).attr("readonly", true);
                                $($row.find('.s8')).attr("readonly", true);
                                $($row.find('.s9')).attr("readonly", true);
                                $($row.find('.s10')).attr("readonly", true);
                                $($row.find('.s11')).attr("readonly", true);
                                $($row.find('.s12')).attr("readonly", true);
                                $($row.find('.color')).attr("tabindex", 1);
                                $($row.find('.soth')).attr("tabindex", 2);
                                $($row.find('.qty')).attr("tabindex", 3);
                                $($row.find('.rate')).attr("tabindex", 4);
                                $($row.find('.amt')).attr("tabindex", 5);

                            } else {
                                $($row.find('.s5')).removeAttr("readonly");
                                $($row.find('.s6')).removeAttr("readonly");
                                $($row.find('.s7')).removeAttr("readonly");
                                $($row.find('.s8')).removeAttr("readonly");
                                $($row.find('.s9')).removeAttr("readonly");
                                $($row.find('.s10')).removeAttr("readonly");
                                $($row.find('.s11')).removeAttr("readonly");
                                $($row.find('.s12')).removeAttr("readonly");
                                $($row.find('.mrp')).val(response.mrp);
                                $($row.find('.rate')).val(response.rate);

                            }


                            /*alert(response.code);*/
                            /*  var itemcode = response.code;
                             var itemname = response.name;
                             var itemcost = response.cost;
                             
                             $row.find('.item').html('<option value="' + itemcode + '">' + itemname + '</option>');
                             $row.find('.cost').val(itemcost);
                             $row.find('.qty').val('1');
                             $row.find('.amt').val(itemcost);*/
                        },
                        error: function(request, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                        //dataType:'json'
                    });

                    calculateRow();
                }
                function addrows() {
                    var ctr = $('#items').val();
                    $.post('<?php echo $append_file; ?>', {ctr: ctr}, function(data) {
                        $(data).appendTo('#data');
                        ctr++;
                        $('#items').val(ctr);
                    });
                }

                $('#addnew').click(function() {
                    addrows();
                });
                $("#freight").keyup(function() {
                    var value = parseFloat($("#total").val());
                    var value2 = parseFloat($("#freight").val());
                    var tot = value + value2;
                    $('#billamt').val(tot.toFixed(2));
                });
                $("#taxes").keyup(function() {
                    var value = parseFloat($("#total").val());
                    var value2 = parseFloat($("#taxes").val());
                    var value3 = parseFloat($("#freight").val());
                    var tot = value + value2 + value3;
                    $('#billamt').val(tot.toFixed(2));
                });

                function calculatemrp() {
                    var $row = $(this).closest("tr");
                    var mrp = parseFloat($row.find('.mrp').val());
                    var tot = mrp * 22 / 100;
                    var fintot = mrp - tot;
                    $row.find('.selling').val(fintot.toFixed(2));

                }

                function calculateTot() {
                    var amt = 0;
                    var val = 0;

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

                function calculateQty() {
                    var qty = 0;
                    var val1 = 0;

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

                function calculateRow() {
                    var mtot = 0;
                    var $row = $(this).closest("tr");
                    var s5 = parseFloat($row.find('.s5').val());
                    var s6 = parseFloat($row.find('.s6').val());
                    var s7 = parseFloat($row.find('.s7').val());
                    var s8 = parseFloat($row.find('.s8').val());
                    var s9 = parseFloat($row.find('.s9').val());
                    var s10 = parseFloat($row.find('.s10').val());
                    var s11 = parseFloat($row.find('.s11').val());
                    var s12 = parseFloat($row.find('.s12').val());
                    var soth = parseFloat($row.find('.soth').val());

                    var tempqty = s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + soth;

                    parseFloat($row.find('.qty').val(tempqty));
                    var qty = tempqty;
                    var cost = parseFloat($row.find('.rate').val());
                    var fintot = qty * cost;
//                    console.log(fintot);
                    if (isNaN(fintot)) {
                        $row.find('.amt').val("0");
                    } else {
                        $row.find('.amt').val(fintot.toFixed(1));
                    }
                    calculateTot();
                    calculateQty();
                }
            });


        </script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#packing").blur(function() {
                    var value = parseFloat($("#total").val());
                    var value2 = parseFloat($("#packing").val());
                    //var newValue = parseInt(value) - parseInt(value2);
                    //alert('newValue');
                    $('#billamt').val(value + value2).toFixed(2);
                });

            });

            $(document).keydown(function(event) {
                //19 for Mac Command+S
                if (!(String.fromCharCode(event.which).toLowerCase() == 's' && event.ctrlKey) && !(event.which == 19))
                    return true;
                //alert("Ctrl-s pressed");
                $("#submit").click();
                event.preventDefault();
                return false;
            });

            $(document).keydown(function(event) {
                //19 for Mac Command+S
                if (!(String.fromCharCode(event.which).toLowerCase() == 'l' && event.ctrlKey) && !(event.which == 19))
                    return true;
                //alert("Ctrl-s pressed");
                $("#addnew").click();
                event.preventDefault();
                return false;
            });
        </script>
        <style>
            /*  input[type='text']{
                  border-radius: 5px;
                  height: 20px;
                  border: 1px solid #ccc;
              }
              #data td{
                  height:10px;
              }*/
        </style>

    </head>

    <body onLoad="document.boq.vouchdt.focus()" id="main_container">
        <form action=''  name="invoice" method="POST">
            <div class="row">
                <div class="col-lg-6" style='width:100%;float:left;'>
                    <div class="box dark">
                        <header>
                            <div class="icons"><i class="fa fa-edit"></i></div>
                            <h5><?php echo $flag . " " . $formname; ?></h5>
                            <!-- .toolbar -->
                            <div class="toolbar">
                                <ul class="nav">
                                     <?php
                                    if ($addflag == 0) {
                                        ?>
                                        <li><a href="#" onclick="window.open('barcodelbl.php?vouchno=<?= $qvouch ?>', 'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"><i class="fa fa-th-large btn" style="color:black;font-size:2.5em;"></i></a></li>
<!--                                        <li><a href="#" onclick="window.open('thermal_barcode.php?vouchno=<?= $qvouch ?>&para=1', 'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"><i class="fa fa-barcode btn" style="color:black;font-size:2.5em;"></i></a></li>-->
                                        
                                        <?php
                                    }
                                    ?>
                                    <li><input type='button' value='Blank Print' class="btn btn-primary" onclick="window.open('blanklr.php', 'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"></li>                                    
                                    <li><input id="submit" type='submit' name='submit' value='<?php echo ($addflag == 0) ? "Update" : "Save"; ?>' class="btn btn-primary"></li>
                                    <?php 
                                        if(!isset($_GET["method"])){
                                    ?>
                                    <li><input type='button' name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<?php echo $grid; ?>'"></li>
                                        <?php } ?>
                                    <li>
                                        <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-2">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                    </li>
                                </ul>            </div>
                            <!-- /.toolbar -->
                        </header>
                        <div id="div-1" class="accordion-body collapse in body">
                            <div class="form-horizontal">
                                <input type="hidden" size="6" maxlength="6" id="mvouch" maxlength="6" name="mvouch" value="<?php echo ($addflag == 0) ? $get['vouchno'] : $qvouch; ?>" />      
                                <table border="0" align="center" width="100%">
                                    <tr>
                                        <td align="center"><label class="control-label">Voucher No.</label></td>
                                        <td align="center"><label class="control-label">Voucher Date.</label></td>
                                        <td align="center"><label class="control-label">Party Name</label></td>
                                        <td align="center"><label class="control-label">Lr. No.</label></td>
                                        <td align="center"><label class="control-label">Lr. Date</label></td>
                                        <td align="center"><label class="control-label">Transporter</label></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <input type="text" size="6" maxlength="6" id="vouchno" name="vouchno" value="<?php echo ($addflag == 0) ? $get['vouchno'] : $qvouch; ?>"  class="form-control" disabled />  
                                        </td>
                                        <td>
                                            <input type="date" size="" id="invdt" name="vouchdt" value="<?php echo ($addflag == 0) ? $get['vouchdt'] : date("Y-m-d"); ?>" class="form-control" /> 
                                        </td>
                                        <td>
                                            <select data-placeholder="Party" name="cmbparty" class="form-control" style='height:33px;'>
                                                <option value="<?php echo ($addflag == 0) ? $getp['accode'] : " "; ?>"><?php echo ($addflag == 0) ? $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>

                                        </td>


                                        <td>
                                            <input type="text" size="" name="lrno" value="<?php echo ($addflag == 0) ? $get['lrno'] : ""; ?>" class="form-control" />  
                                        </td>
                                        <td>
                                            <input type="date" size="" id="lrdt" name="lrdt" value="<?php echo ($addflag == 0) ? $get['lrdt'] : date("Y-m-d"); ?>" class="form-control" />  
                                        </td>
                                        <td>
                                            <select name="transporter" style="height: 33px;" class="form-control"> 
                                                <option value='<?php echo ($addflag == 0) ? $trans['code'] : ""; ?>'><?php echo ($addflag == 0) ? $trans['name'] : ""; ?></option>
                                                <?php
                                                $gett = mysqli_query($conn, "SELECT code, `name` FROM `transporter` ORDER BY `name`") or die(mysqli_error($conn));
                                                while ($row = mysqli_fetch_array($gett)) {
                                                    echo "<option value=$row[code]>$row[name]</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        
                                         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<!--                                        <td>
                                            <input type="checkbox" name="fav" value="<?php echo ($addflag == 0) ? $get['fav'] : "1"; ?>" class="form-control"/>
                                        </td> -->

                                </table>
                                <br/>
                                <table id="data" align="center" border="1px" width="100%">

                                    <tr>
                                        <td width="60px" align="center"> <label class="control-label"></label></td>
                                        <td width="60px" align="center"> <label class="control-label">Sr.</label></td>
                                        <td width="250px" align="center"><label class="control-label">Item Name <i class="fa fa-edit" onclick="window.open('item.php?mode=true&method=inward', 'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"></i></label></td>    
                                        <td width="180px" align="center"><label class="control-label">Colour</label></td>
                                        <td width="60px" align="center"><label class="control-label">5</label></td>
                                        <td width="60px" align="center"><label class="control-label">6</label></td>
                                        <td width="60px" align="center"><label class="control-label">7</label></td>
                                        <td width="60px" align="center"><label class="control-label">8</label></td>
                                        <td width="60px" align="center"><label class="control-label">9</label></td>
                                        <td width="60px" align="center"><label class="control-label">10</label></td>
                                        <td width="60px" align="center"><label class="control-label">11</label></td>
                                        <td width="60px" align="center"><label class="control-label">12</label></td>
                                        <td width="80x" align="center"><label class="control-label">Other</label></td>
                                        <td width="80px" align="center"><label class="control-label">Total Qty.</label></td>
                                        <td width="120px" align="center"><label class="control-label">MRP</label></td>
                                        <td width="120px" align="center"><label class="control-label">Pur.Price</label></td>
                                        <td width="120px" align="center"><label class="control-label">Amount</label></td>
                                        <td width="110px" align="center"><label class="control-label">Sale Price</label></td>
                                        <td width="100px" align="center"><label class="control-label">Cost Code</label></td>
                                        <td width="40px" align="center"><label class="control-label">FAV</label></td>
                                        <td width="40px" align="center"><label class="control-label">Location</label></td>
                                    </tr>
                                    <?php
                                    if ($addflag == 0) {
                                        if ($admin == 1) {
                                            $qry = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
//                                            $qry = mysqli_query($conn, "SELECT a.*, b.name as item FROM `$trandb` a, item b WHERE a.vouchno='$getvouch' AND a.itemcode = b.code ORDER BY a.srno") or die(mysqli_error($conn));
                                        } else {
                                            $qry = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno_d='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
//                                            $qry = mysqli_query($conn, "SELECT a.*, b.name as item FROM `$trandb` a, item b WHERE a.vouchno_d = '$getvouch' AND a.itemcode = b.code ORDER BY a.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));
                                            $gitem = mysqli_fetch_array($getitem);

                                            $getcolr = mysqli_query($conn, "SELECT * FROM `color` WHERE code='$row[color]'") or die(mysqli_error($conn));
                                            $colr = mysqli_fetch_object($getcolr);

                                            $getloc = mysqli_query($conn, "SELECT * FROM `location` WHERE code='$row[location]'") or die(mysqli_error($conn));
                                            $loc = mysqli_fetch_object($getloc);

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

                                            echo "<td align='left'>";
                                            echo "<input list='item_$ctr' name='item_$ctr' class='item form-control'  value='$gitem[name]' style='height:33px;'>";
                                            echo "<datalist id='item_$ctr'>";
                                            echo "<option value='$gitem->name'>";
                                            $mitem = mysqli_query($conn, "SELECT `name` FROM `item` ORDER BY `name`") or die(mysqli_error($conn));
                                            while ($trow1 = mysqli_fetch_array($mitem)) {
                                                echo "<option value='$trow1[name]'>";
                                            }
                                            echo "</datalist>";
                                            echo "</td>";
                                            echo "<td align='center' valign='top'>";
                                            echo "<select name='color_$ctr' style='height:34px;' class='form-control'>";
                                            echo "<option value='$colr->code'>$colr->name</option>";
                                            $getcolors = mysqli_query($conn, "SELECT `code`, `name` FROM `color` WHERE `name`!='' ORDER BY `name`") or die(mysqli_error($conn));
                                            while ($wcol = mysqli_fetch_array($getcolors)) {
                                                echo "<option value='$wcol[code]' >$wcol[name]</option>";
                                            }
                                            echo "</select>";
                                            echo "</td>";

                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4' name='s5_$ctr' class='s5 form-control' style='text-align:right;height:33px;' value='$row[s5]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s6_$ctr' id='cost' class='s6 form-control' style='text-align:right;height:33px;' value='$row[s6]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s7_$ctr' class='s7 form-control'  style='text-align:right;height:33px;' value='$row[s7]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s8_$ctr' class='s8 form-control'  style='text-align:right;height:33px;' value='$row[s8]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s9_$ctr' class='s9 form-control'  style='text-align:right;height:33px;' value='$row[s9]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s10_$ctr' class='s10 form-control'  style='text-align:right;height:33px;' value='$row[s10]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s11_$ctr' class='s11 form-control'  style='text-align:right;height:33px;' value='$row[s11]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s12_$ctr' class='s12 form-control'  style='text-align:right;height:33px;' value='$row[s12]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='soth_$ctr' class='soth form-control'  style='text-align:right;height:33px;' value='$row[soth]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4'  name='qty_$ctr' class='qty form-control'  style='text-align:right;height:33px;' value='$row[qty]' readonly/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' name='mrp_$ctr' class='mrp form-control'  style='text-align:right;height:33px;' value='$row[mrp]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4' name='rate_$ctr' class='rate form-control'  style='text-align:right;height:33px;' value='$row[rate]'/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4'  name='amt_$ctr' class='amt form-control'  style='text-align:right;height:33px;' value='$row[amount]' readonly/></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4'  name='selling_$ctr' class='selling form-control'  style='text-align:right;height:33px;' value='$row[selling]' /></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4'  name='costcode_$ctr' class='costcode form-control'  style='text-align:right;height:33px;' value='$row[costcode]' /></td>";
                                            echo "<td align='center' valign='top'><input type='text' size='4'  name='fav_$ctr' class='fav form-control'  style='text-align:right;height:33px;' value='$row[fav]' /></td>"; 
                                            
                                            echo "<td align='center' valign='top'>";
                                            echo "<select name='location_$ctr' style='height:34px;' class='form-control'>";
                                            echo "<option value='$loc->code'>$loc->name</option>";
                                            $getlocation = mysqli_query($conn, "SELECT `code`, `name` FROM `location` ORDER BY `name`") or die(mysqli_error($conn));
                                            while ($wloc = mysqli_fetch_array($getlocation)) {
                                                echo "<option value='$wloc[code]' >$wloc[name]</option>";
                                            }
                                            echo "</select>";
                                            echo "</td>";

                                            
                                            echo "</tr>";
                                            $ctr++;
                                        }
                                        //browse($ctr);
                                    } else {
                                        ?>
                                        <tr>
                                            <td align="center" valign="top"><input type="image" src="delbut.png" name="remove"  value="remove" class="remove"/></td>
                                            <td align="left" valign="top"><input type="text" size="6" maxlength="6" maxlength="6" name="srno_0" class="sno form-control" style="height:33px;" value="1"/></td>

                                            <td align="left">
                                                <input list="item_0" name="item_0"  class="item form-control" style="height:33px;">
                                                <datalist id="item_0"> 
                                                    <option value=""></option>
                                                    <?php
                                                    $mitem = mysqli_query($conn, "SELECT `name` FROM `item` ORDER BY `name`") or die(mysqli_error($conn));
                                                    while ($trow1 = mysqli_fetch_array($mitem)) {
                                                        echo "<option value='$trow1[name]'>";
                                                    }
                                                    ?>
                                                </datalist>
                                            </td>

                                            <td align="center" valign="top">
                                                <select name="color_0" class="color form-control" style='height:34px;'> 
                                                    <option value=""></option>
                                                    <?php
                                                    $getcolors = mysqli_query($conn, "SELECT `code`, `name` FROM `color` WHERE `name`!='' ORDER BY `name`") or die(mysqli_error($conn));
                                                    while ($wcol = mysqli_fetch_array($getcolors)) {
                                                        echo "<option value='$wcol[code]' >$wcol[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4" name="s5_0" class="s5 form-control" style='text-align:right;height:33px;' value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s6_0" id="cost" class="s6 form-control" style='text-align:right;height:33px;' value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s7_0" class="s7 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s8_0" class="s8 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s9_0" class="s9 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s10_0" class="s10 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s11_0" class="s11 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="s12_0" class="s12 form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4" maxlength="4"  name="soth_0" class="soth form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4"  name="qty_0" class="qty form-control"  style="text-align:right;height:33px;" value="0" readonly/></td>
                                            <td align="center" valign="top"><input type="text" size="4"  name="mrp_0" class="mrp form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4"  name="rate_0" class="rate form-control"  style="text-align:right;height:33px;" value="0"/></td>
                                            <td align="center" valign="top"><input type="text" size="4"  name="amt_0" class="amt form-control"  style="text-align:right;height:33px;" value="0" readonly/></td>
                                            <td align="center" valign="top"><input type="text" size="4"  name="selling_0" class="selling form-control"  style="text-align:right;height:33px;" value="0" /></td>
                                            <td align="center" valign="top"><input type="text" size="4"  name="costcode_0" class="costcode form-control"  style="text-align:right;height:33px;" value="0" /></td>
                                            <td align="center" valign="top"><input type="text" size="4"  name="fav_0" class="fav form-control"  style="text-align:right;height:33px;" value="0" /></td>
                                            
                                            <td align="center" valign="top">
                                                <select name="location_0" class="color form-control" style='height:34px;'> 
                                                    <option value=""></option>
                                                    <?php
                                                    $getloc = mysqli_query($conn, "SELECT `code`, `name` FROM `location` ORDER BY `name`") or die(mysqli_error($conn));
                                                    while ($wloc = mysqli_fetch_array($getloc)) {
                                                        echo "<option value='$wloc[code]' >$wloc[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            
                                        </tr>
                                    <?php } ?>
                                </table>
                            </div>  

                        </div>
                        <table border='0' width='90%' align="center">


                            <tr>
                                <td width="150px">
                                    <input type="button" id="addnew" class="btn btn-success btn-lg" name="addnew" value="+" />
                                    <input type="hidden" id="items" name="items" value="<?php echo ($addflag == 0) ? $ctr : 1; ?>" />  
                                </td><td width="240px"></td><td width="140px"></td><td width="200px" align="right"></td>
                                <td width="200px"></td>
                            </tr>
                            <tr>
                                <td width="150px"></td>
                                <td width="100px" align="right"><label for=""><font color="009900" size="3px">Total Qty.</font></label></td>
                                <td><input id="totqty" type="text" name="totqty" class="form-control"  value="<?php echo ($addflag == 0) ? $get['totqty'] : "0.00"; ?>" readonly style='text-align:right'></td>
                                <td width="100px" align="right"><label for=""><font color="009900" size="3px">Net Amount (Rs.)</font><span></span></label></td>
                                <td><input id="billamt" type="text" name="billamt" class="form-control"  value="<?php echo ($addflag == 0) ? $get['netamt'] : "0.00"; ?>" readonly style='text-align:right'></td>
                            </tr>
                            <tr>
                                <td align="center" valign="top"><b>Remarks</b></td>
                                <td colspan="2">
                                    <input type="text" name="rem1" value="<?php echo ($addflag == 0) ? $get["rem1"] : ""; ?>" class="form-control">
                                    <input type="text" name="rem2" value="<?php echo ($addflag == 0) ? $get["rem2"] : ""; ?>" class="form-control">
                                </td>
                                <td></td>
                                <td></td>
                            </tr>

                        </table>

                    </div>

                    <!--<div class="accordion" id="accordionid">
                     
                      <div class="accordion-group">
                        <div class="accordion-heading">
                          <a class="box primary" data-toggle="collapse" data-parent="#accordionid" href="index.html#four" style='background:#2a6496;color:white;text-decoration: none;'>
                         <center>   <h5>L.R Details <i class='fa fa-bars'></i></h5></center>
                          </a>
                        </div>
                        <div id="four" class="collapse">
                          <div class="accordion-inner">
                    
                          </div>
                        </div>
                      </div>
                      
                    
                        </div>-->


                </div>
            </div>        
        </div>        

    </form>

    <script>window.jQuery || document.write('<script src="assets/lib/jquery.min.js"><\/script>')</script> 
    <script src="assets/lib/bootstrap/js/bootstrap.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <script src="assets/lib/uniform/jquery.uniform.min.js"></script>
    <script src="assets/lib/jquery.dualListbox-1.3/jquery.dualListBox-1.3.min.js"></script>
    <script src="assets/lib/autosize/jquery.autosize.min.js"></script>
    <script src="assets/js/main.js"></script>

    <script>
        $(function() {
            formGeneral();
        });
    </script>


</body>
</html>