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

$formname = 'Item';
$maindb = 'item';
//$trandb = 'itemsub';
//$stkdb = 'transacn';



$printfile = "itemprint.php";
$file = 'item.php';
$trandbexist = 0;
$keyfld = "code";
$mgrpcode = "OP";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
//browse($zroot);
$myyear = $_SESSION[$myyear];


$compcd = $_SESSION["comp_" . $dbname];

//$maindb1 = 'apps4rea_msp_1_' . $myyear;
//$maindb2 = 'apps4rea_msp_2_' . $myyear;
//$maindb3 = 'apps4rea_msp_3_' . $myyear;

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

if (isset($_POST["submit"])) {

    $a = 1;
    $name = $_POST["name"];
    $unit = $_POST["unit"];
    $prodtype = $_POST["prodtype"];
    $itemgrp = $_POST["itemgrp"];
    $itemsubgrp = $_POST["itemsubgrp"];
    $cost = $_POST["cost"];
    $rate = $_POST["rate"];
    $msp = $_POST["msp"];
    $mrp = $_POST["mrp"];
    $opening = $_POST["opening"];
    $nmlabels = $_POST["nmlabels"];
    $hsncode = $_POST["hsncode"];
    $gstperc = $_POST["gstperc"];

    $count = $_POST["items"];


    $mvouch = $qvouch;

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

    if ($a == 1) {

//        #################################TRAN PART #################################
//
//        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `code`='$mvouch'") or die(mysqli_error($conn));
//
//        for ($i = 0; $i < $count; $i++) {
//            $msrno = $_POST["srno_$i"];
//            $mgodown = $_POST["godown_$i"];
//            $mbatch = $_POST["batch_$i"];
//            $mqty = $_POST["qty_$i"];
//
//            if (!empty($mgodown)) {
//                mysqli_query($conn, "INSERT INTO `$trandb`(`code`,`srno`, `godown`, `batch`,`qty` )
//                                          VALUES('$mvouch', '$msrno', '$mgodown', '$mbatch', '$mqty')") or die(mysqli_error($conn));
//            }
//        }
//        #################################STOCK PART #################################
//
//        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `vouchno`='$mvouch' AND `source`='$mgrpcode'")or die(mysqli_error($conn));
//
//        for ($i = 0; $i < $count; $i++) {
//            $msrno = $_POST["srno_$i"];
//            $mgodown = $_POST["godown_$i"];
//            $mbatch = $_POST["batch_$i"];
//            $mqty = $_POST["qty_$i"];
//
//            $yearno = $myyear;
//            $group = $mgrpcode . $mvouch;
//            $vouchdt = date("Y-m-d");
//            $ord = "0";
//            $actbatch = $mgrpcode . $mvouch . $mbatch;
//
//            mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, `group`, `source`, `vouchno`,`srno`, `itemgrp`, `itemcode`,`itemname`,
//                            `batch`, `qty`, `godown`, `actbatch`, `ord`)
//                            VALUES('$yearno','$group','$mgrpcode', '$mvouch', '$msrno', '$itemgrp', '$mvouch', '$name', 
//                            '$mbatch', '$mqty', '$mgodown', '$mbatch', '$ord')") or die(mysqli_error($conn));
//    }
        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO $maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
                                               `nmlabels`, `hsncode`,`gstperc`)
                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$maindb1`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$maindb2`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$maindb3`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "UPDATE $maindb SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
                                 `cost`='$cost', `rate`='$rate', `msp`='$msp', `mrp`='$mrp', `opening`='$opening', `nmlabels`='$nmlabels',
                                 `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));

//            if ($compcd == 1) {
//                $itemqry = mysqli_query($conn, "select * from $maindb2.item WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                $itemcount = mysqli_num_rows($itemqry);
//                if ($itemcount > 0) {
//                    mysqli_query($conn, "UPDATE `$maindb2`.$maindb SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
//                                    `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = $mvouch") or die(mysqli_error($conn));
//                } else {
//                    mysqli_query($conn, "INSERT INTO `$maindb2`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//                }
//
//                $itemqry3 = mysqli_query($conn, "select * from $maindb3.item WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                $itemcount3 = mysqli_num_rows($itemqry3);
//                if ($itemcount3 > 0) {
//                    mysqli_query($conn, "UPDATE `$maindb3`.$maindb SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
//                                    `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                } else {
//                    mysqli_query($conn, "INSERT INTO `$maindb3`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//                }
//            }
//
//            if ($compcd == 2) {
//                $itemqry = mysqli_query($conn, "select * from $maindb1.item WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                $itemcount = mysqli_num_rows($itemqry);
//                if ($itemcount > 0) {
//                    mysqli_query($conn, "UPDATE `$maindb1`.$maindb SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
//                                    `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = $mvouch") or die(mysqli_error($conn));
//                } else {
//                    mysqli_query($conn, "INSERT INTO `$maindb1`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//                }
//
//                $itemqry3 = mysqli_query($conn, "select * from $maindb3.item WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                $itemcount3 = mysqli_num_rows($itemqry3);
//                if ($itemcount3 > 0) {
//                    mysqli_query($conn, "UPDATE `$maindb3`.$maindb SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
//                                    `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                } else {
//                    mysqli_query($conn, "INSERT INTO `$maindb3`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//                }
//            }
//            if ($compcd == 3) {
//
//                $itemqry = mysqli_query($conn, "select * from $maindb1.item WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                $itemcount = mysqli_num_rows($itemqry);
//                if ($itemcount > 0) {
//                    mysqli_query($conn, "UPDATE `$maindb1`.$maindb SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
//                                    `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = $mvouch") or die(mysqli_error($conn));
//                } else {
//                    mysqli_query($conn, "INSERT INTO `$maindb1`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//                }
//
//                $itemqry3 = mysqli_query($conn, "select * from $maindb2.item WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                $itemcount3 = mysqli_num_rows($itemqry3);
//                if ($itemcount3 > 0) {
//                    mysqli_query($conn, "UPDATE `$maindb2`.$maindb SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
//                                    `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                } else {
//                    mysqli_query($conn, "INSERT INTO `$maindb2`.$maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
//                                               `nmlabels`, `hsncode`,`gstperc`)
//                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening',
//                                               '$nmlabels','$hsncode','$gstperc')") or die(mysqli_error($conn));
//                }
//            }
        }

        if ($addflag == 1) {
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}


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

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

    $getunit = mysqli_query($conn, "SELECT * FROM `unit` WHERE `code`='$get[unit]'") or die(mysqli_error($conn));
    $gunit = mysqli_fetch_array($getunit);

    $finditemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE `code`='$get[itemgrp]'") or die(mysqli_error($conn));
    $fitemgrp = mysqli_fetch_array($finditemgrp);

    $finditemsub = mysqli_query($conn, "SELECT * FROM `itemsubgrp` WHERE `code`='$get[itemsubgrp]'") or die(mysqli_error($conn));
    $fitemsub = mysqli_fetch_array($finditemsub);
}
$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    delete($maindb2, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    delete($maindb3, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script  type="text/javascript">
    $(document).ready(function () {
        $("body").on("change", ".itemgrp", gethsn);

        function gethsn() {
            var itemgrp = $('.itemgrp').val();
            $.post('gethsn.php', {itemgrp: itemgrp}, function (data) {
                $('.hsncode').val(data.hsn);
                $('.gstperc').val(data.gst);
            }, 'json');
        }
    });
</script>
</head>
<body>
    <form action='' name="invoice" method='POST'>
        <!-- Default box -->
        <div class="col-lg-5 col-md-12 col-sm-12 col-xs-12  pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php Echo $formname; ?></h3>
                    <div class="actions pull-right">
                        <i class="fa fa-chevron-down"></i>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Code</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <input type="text" class="form-control form" name="code" value="<?php Echo ($addflag != 1) ? $get["code"] : " $qvouch"; ?>" required readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Name</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                            <input type="text" class="form-control form item" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required autofocus>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Item Group</label>
                        </div>
                        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                            <select class="form-control itemgrp form select2" name="itemgrp" required>
                                <option value="<?php echo ($addflag != 1) ? $fitemgrp['code'] : ""; ?>"><? echo ($addflag != 1) ? $fitemgrp['name'] : ""; ?></option>
                                <?php
                                $gitemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp`")or die(mysqli_error($conn));
                                while ($itemgrp = mysqli_fetch_object($gitemgrp)) {
                                    Echo "<option value='$itemgrp->code'>$itemgrp->name</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Item Sub Group</label>
                        </div>
                        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                            <select class="form-control form select2" name="itemsubgrp" required >
                                <option value="<?php echo ($addflag != 1) ? $fitemsub['code'] : ""; ?>"><? echo ($addflag != 1) ? $fitemsub['name'] : ""; ?></option>
                                <?php
                                $gitemsub = mysqli_query($conn, "SELECT * FROM `itemsubgrp`")or die(mysqli_error($conn));
                                while ($mitemsub = mysqli_fetch_object($gitemsub)) {
                                    Echo "<option value='$mitemsub->code'>$mitemsub->name</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg" style="color: #78909C">HSN Code</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" step="any" class="form-control form hsncode" name="hsncode" value="<?php Echo ($addflag != 1) ? $get["hsncode"] : ""; ?>"> 
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg" style="color: #78909C">GST &nbsp; %</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-3 pad">
                            <input type="text" step="any" class="form-control form gstperc" name="gstperc" value="<?php Echo ($addflag != 1) ? $get["gstperc"] : ""; ?>"> 
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Unit</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <select class="form-control form select2" name="unit" style="width:100%" required="required"> 
                                <option value=" <?php echo $gunit['code'] ?>"><?php echo $gunit['name'] ?></option>
                                <?php
                                $funit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit`")or die(mysqli_error($conn));
                                while ($unit1 = mysqli_fetch_object($funit)) {
                                    Echo "<option value='$unit1->code'>$unit1->name</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Product Type</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <select class="form-control form select2" name="prodtype" style="width:100%"> 
                                <option value="<?php Echo ($addflag != 1) ? $get["prodtype"] : ""; ?>">
                                    <?php
                                    if ($get["prodtype"] == "P") {
                                        Echo "PERFUME";
                                    } elseif ($get["prodtype"] == "O") {
                                        Echo "OTHERS";
                                    }
                                    ?>
                                </option>
                                <option value="P">PERFUMES</option>
                                <option value="O">OTHERS</option>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Cost</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form cost numeric" name="cost" value="<? Echo ($addflag != 1 ) ? $get[cost] : ""; ?>">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Rate</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form rate numeric" name="rate" value="<? Echo ($addflag != 1 ) ? $get[rate] : ""; ?>">
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control text-danger marg">Minimum Selling Price</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form rate numeric" name="msp" value="<? Echo ($addflag != 1 ) ? $get[msp] : ""; ?>">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control text-danger marg">MRP</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form rate numeric" name="mrp" value="<? Echo ($addflag != 1 ) ? $get[mrp] : ""; ?>">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <hr>
                        </div>
                        <b>STOCK</b>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <hr>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control text-danger marg">Opening</label>
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form rate numeric" name="opening" value="<? Echo ($addflag != 1 ) ? $get[opening] : ""; ?>">
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control text-danger marg">No. Of Labels</label>
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type='text' id="nolabel" name='nolabel' maxlength="4" class='form-control' style='width:70px;'<?php echo ($addflag != 1) ? '' : 'readonly'; ?>>                                                       
                        </div>

                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                            <button <?php if ($addflag != 1) { ?> onclick="window.open('thermal_barcode.php?vouchno=<?= $qvouch ?>&vouchdt=<?= $vouchdt ?>&para=3&number=' + document.invoice.nolabel.value, 'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');" <?php
                             } else {
                                echo "disabled";
                            }
                            ?> ><i class="fa fa-th-large btn"></i></button>
                        </div>

                    </div>
                </div>
                <div class="panel-footer">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>
        </div>
        <div class="col-lg-7 pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <!--<h3 class="panel-title"><?php // Echo $formname;                ?></h3>-->
                    <div class="actions pull-right">
                        <i class="fa fa-chevron-down"></i>

                    </div>
                </div>
                <div class="panel-body">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="float: right">
                        <h4>SALE</h4>
                        <table class="table table-striped table-condensed table-bordered" width="50%">
                            <thead class="mySearchTr">
                                <tr style="background-color: #337ab7">
                                    <th width="10%">Bill No.</th>
                                    <th width="15%" class=''>Date</th>
                                    <th width="35%"class=''>Party</th>
                                    <th width="10%"class='text-right'>Qty.</th>
                                    <th width="15%"class='text-right'>Rate</th>
                                    <th width="15%"class='text-right'>Amount</th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                $gettran = mysqli_query($conn, "SELECT a.*,b.*,DATE_FORMAT(a.vouchdt,'%d-%m-%Y')as date  FROM `saletran` a,`salemain` b WHERE a.`item`='$qvouch'  and a.vouchno = b.vouchno
                                                            order by b.vouchdt DESC limit 0,3")or die(mysqli_error($conn));
                                $count = mysqli_num_rows($gettran);
                                if ($count > 0) {
                                    while ($tran = mysqli_fetch_array($gettran)) {
                                        $gitem = mysqli_query($conn, "SELECT code,name FROM `item` WHERE `code`='$tran[item]'") or die(mysqli_error($conn));
                                        $fitem = mysqli_fetch_array($gitem);

                                        $gparty = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`='$tran[party]'") or die(mysqli_error($conn));
                                        $fparty = mysqli_fetch_array($gparty);

                                        Echo "<tr>";
                                        Echo "<td>$tran[vouchno]</td>";
                                        Echo "<td>$tran[date]</td>";
                                        Echo "<td>$fparty[name]</td>";
                                        Echo "<td class='text-right'>$tran[qty]</td>";
                                        Echo "<td class='text-right'>$tran[rate]</td>";
                                        Echo "<td class='text-right'>$tran[amount]</td>";
                                        Echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                        <!-- purchase -->
                        <h4>PURCHASE</h4>
                        <table class="table table-striped table-condensed table-bordered" width="50%">
                            <thead style="" class="mySearchTr">
                                <tr>
                                    <th width="10%">Bill No.</th>
                                    <th width="15%" class=''>Date</th>
                                    <th width="35%"class=''>Party</th>
                                    <th width="10%"class='text-right'>Qty.</th>
                                    <th width="15%"class='text-right'>Rate</th>
                                    <th width="15%" class='text-right'>Amount</th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                $gettran2 = mysqli_query($conn, "SELECT a.* , b.*,DATE_FORMAT(a.vouchdt,'%d-%m-%Y')as date FROM `purtran` a,`purmain` b WHERE a.`itemcode`='$qvouch' and a.vouchno = b.vouchno
                                                               order by b.vouchdt DESC limit 0,3")or die(mysqli_error($conn));
                                $count2 = mysqli_num_rows($gettran2);
                                if ($count2 > 0) {
                                    while ($tran2 = mysqli_fetch_array($gettran2)) {
                                        $gitem2 = mysqli_query($conn, "SELECT code,name FROM `item` WHERE `code`='$tran2[itemcode]'") or die(mysqli_error($conn));
                                        $fitem2 = mysqli_fetch_array($gitem2);

                                        $gparty2 = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`='$tran2[party]'") or die(mysqli_error($conn));
                                        $fparty2 = mysqli_fetch_array($gparty2);

                                        Echo "<tr>";
                                        Echo "<td>$tran2[vouchno]</td>";
                                        Echo "<td>$tran2[date]</td>";
                                        Echo "<td>$fparty2[name]</td>";
                                        Echo "<td class='text-right'>$tran2[qty]</td>";
                                        Echo "<td class='text-right'>$tran2[rate]</td>";
                                        Echo "<td class='text-right'>$tran2[amount]</td>";
                                        Echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                        <!-- transfer -->
                        <h4>TRANSFER</h4>
                        <table class="table table-striped table-condensed table-bordered" width="50%">
                            <thead style="" class="mySearchTr">
                                <tr >
                                    <th width="10%">Bill No.</th>
                                    <th width="15%" class=''>Date</th>
                                    <th width="35%"class=''>Party</th>
                                    <th width="10%"class='text-right'>Qty.</th>
                                    <th width="15%"class='text-right'>Rate</th>
                                    <th width="15%" class='text-right'>Amount</th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
//                                $gettran2 = mysqli_query($conn, "SELECT a.* , b.*,DATE_FORMAT(a.vouchdt,'%d-%m-%Y')as date FROM `TRAN` a,`purmain` b WHERE a.`itemcode`='$qvouch' and a.vouchno = b.vouchno
//                                                               order by b.vouchdt DESC limit 0,3")or die(mysqli_error($conn));
                                $gettran3 = mysqli_query($conn, "SELECT * , DATE_FORMAT(vouchdt,'%d-%m-%Y')as date FROM `transacn` WHERE `itemcode`='$qvouch' and source = 'TR' and qty > 0
                                                               order by vouchdt DESC limit 0,3")or die(mysqli_error($conn));
                                $count3 = mysqli_num_rows($gettran3);
                                if ($count3 > 0) {
                                    while ($tran3 = mysqli_fetch_array($gettran3)) {
                                        $gitem2 = mysqli_query($conn, "SELECT code,name FROM `item` WHERE `code`='$tran3[itemcode]'") or die(mysqli_error($conn));
                                        $fitem2 = mysqli_fetch_array($gitem2);

                                        $gparty2 = mysqli_query($conn, "SELECT code,name FROM $zroot.`company` WHERE `code`='$tran3[fromcomp]'") or die(mysqli_error($conn));
                                        $fparty2 = mysqli_fetch_array($gparty2);

                                        Echo "<tr>";
                                        Echo "<td>$tran3[vouchno]</td>";
                                        Echo "<td>$tran3[date]</td>";
                                        Echo "<td>$fparty2[name]</td>";
                                        Echo "<td class='text-right'>$tran3[qty]</td>";
                                        Echo "<td class='text-right'>$tran3[rate]</td>";
                                        Echo "<td class='text-right'>$tran3[amount]</td>";
                                        Echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>

                    </div>
                </div>

            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class=" table-responsive">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr class="mySearchTr">
                                    <th style="width:10px;  text-align:left">Code</th>
                                    <th style="width:70px;  text-align:left">Name</th>
                                    <th style="width:70px;  text-align:left">Group</th>
                                    <th style="width:70px;  text-align:left">Sub Group</th>
                                    <th style="width:10px;  text-align:left">Unit</th>
                                    <th style="width:10px;  text-align:left">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT `code`,`name`,itemgrp, itemsubgrp,`unit` FROM `$maindb` ORDER BY `code`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        $srcunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit` where `code`='$row[unit]' ORDER BY `name`")or die(mysqli_error($conn));
                                        $sunit = mysqli_fetch_array($srcunit);

                                        $srcgroup = mysqli_query($conn, "SELECT `code`,`name` FROM `itemgrp` where `code`='$row[itemgrp]' ORDER BY `name`")or die(mysqli_error($conn));
                                        $sgroup = mysqli_fetch_array($srcgroup);

                                        $srcsubgroup = mysqli_query($conn, "SELECT `code`,`name` FROM `itemsubgrp` where `code`='$row[itemsubgrp]' ORDER BY `name`")or die(mysqli_error($conn));
                                        $ssubgroup = mysqli_fetch_array($srcsubgroup);

                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$row[code]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$row[name]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$sgroup[name]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$ssubgroup[name]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$sunit[name]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'><a href=\"$file?mode=edit&$keyfld=$row[code]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
