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

$formname = 'Item';
$maindb = 'item';
$printfile = "itemprint.php";
$file = 'itemmain.php';
$trandbexist = 1;
$trandb = 'itemtran';
$keyfld = "code";
$mgrpcode = "OP";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION['$myyear'];
$zroot = $_SESSION["zdir"];
$compcd = $_SESSION["comp_" . $dbname];
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"];
    $pcsper = $_POST["pcsper"];
    $totpcs = $_POST["totpcs"];
    $pcspera = $_POST["pcspera"];
    $totpcsa = $_POST["totpcsa"];
    $itemgrp = $_POST["itemgrp"];
    $itemsubgrp = $_POST["itemsubgrp"];
    $cost = $_POST["cost"];
    $selrate = $_POST["selrate"];
    $msp = $_POST["msp"];
    $mrp = $_POST["mrp"];
    $opening = $_POST["opening"];
    $nmlabels = $_POST["nmlabels"];
    $hsncode = $_POST["hsncode"];
    $gstperc = $_POST["gstperc"];
    $nameprint = $_POST["nameprint"];
    $totamt = $_POST["totamt"];
    $subname = $_POST["subname"];
    $kargname = $_POST["kargname"];

    $count = $_POST["items"];


    $mvouch = $qvouch;
//BROWSE('YAHA');
    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");
        }
    }
//BROWSE('FIR');
    if ($a == 1) {

        $path = "admin/itemimage/";

        $fileName = $qvouch . ".jpg";
        $img_file = $path . ($fileName);

//      $fileName = $_FILES["file1"]["name"]; // The file name
        $fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
        $fileType = $_FILES["file1"]["type"]; // The type of file it is
        $fileSize = $_FILES["file1"]["size"]; // File size in bytes
        $fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
//        browse($fileSize);
        if ($fileSize > 1000000) {
            browse('File size must be less than 1 MB');
        }
//        if (!$fileTmpLoc) { // if file not chosen
//            echo "ERROR: Please browse for a file before clicking the upload button.";
//            exit();
//        }



        if ($fileSize < 1000000) {

            if (!empty($fileTmpLoc)) {
                if (move_uploaded_file($fileTmpLoc, "$img_file")) {
                    mysqli_query($conn, "DELETE FROM `itemimage` WHERE vouchno='$qvouch' ") or die(mysqli_error($conn));

                    browse("$fileName upload is complete");

###########################SAVE PART################################################   
                    $imgp = mysqli_query($conn, "SELECT * FROM `itemimage`")or die(mysqli_error($conn));
                    $gimg = mysqli_fetch_array($imgp);


                    if ($gimg[vouchno] == $qvouch) {
//        print " UPDATE `itemimage` SET `vouchno`='$qvouch', `imgpath`='$img_file' WHERE `vouchno`='$qvouch' ";
                        mysqli_query($conn, "UPDATE `itemimage` SET `vouchno`='$qvouch', `imgpath`='$img_file' WHERE `vouchno`='$qvouch' ") or die(mysqli_error($conn));
                    }

                    mysqli_query($conn, "INSERT INTO `itemimage`(`vouchno`,`imgpath`)
                            VALUES('$qvouch', '$img_file')") or die(mysqli_error($conn));
                } else {
                    browse("move_uploaded_file function failed");
                }
            }
        }
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE itemcode='$qvouch' ") or die(mysqli_error($conn));


        $cnt = 0;
        $cntamt = 0;
        for ($i = 0; $i <= $count; $i++) {
            $cnt++;
            $msrno = $_POST["srno_$i"];
            $material = $_POST["material_$i"];
            $qty = $_POST["qty_$i"];
            $unit = $_POST["unit_$i"];
            $rate = $_POST["rate_$i"];
            $amount = $_POST["amount_$i"];

            if ($material != '') {
                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`, code, `itemcode`, `material`, `qty`, `unit`, `rate`,amount)
                                    VALUES('$cnt', '$qvouch', '$qvouch', '$material', '$qty', '$unit', '$rate','$amount')") or die(mysqli_error($conn));
            }
        }
############################## MAIN  ######################################################
        if ($addflag == 1) {
//            BROWSE('1');
            mysqli_query($conn, "INSERT INTO $maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
                                               `nmlabels`, `hsncode`,`gstperc`,`nameprint`,totpcs,pcsper,totpcsa,pcspera,totamt,subname,kargname)
                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$selrate', '$msp','$mrp','$opening',
                                               '$nmlabels','$hsncode','$gstperc','$nameprint','$totpcs', '$pcsper','$totpcsa', '$pcspera','$totamt','$subname','$kargname')") 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`='$selrate', `msp`='$msp', `mrp`='$mrp', `opening`='$opening', `nmlabels`='$nmlabels',totamt='$totamt',
                                 `hsncode`='$hsncode', `gstperc`='$gstperc',`nameprint`='$nameprint',totpcs='$totpcs',pcsper='$pcsper',totpcsa='$totpcsa',
                                  pcspera='$pcspera',subname='$subname',kargname='$kargname' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
        }

        ######################### TRAN ###############################





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

    $getimg = mysqli_query($conn, "SELECT * FROM `itemimage` WHERE `vouchno`='$qvouch'")or die(mysqli_error($conn));
    $img = mysqli_fetch_array($getimg);
    $imgpath = $img['imgpath'] . "?a=" . mt_rand();
}

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, transacn WRITE")or die(mysqli_error($conn));
    $chkdel = mysqli_query($conn, "Select itemcode from `transacn` where `itemcode`='$_GET[$keyfld]'") or die(mysqli_error($conn));
    $cntdel = mysqli_num_rows($chkdel);
    if ($cntdel > 0) {
        browse("Item Exist in Transactions");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        unlink($_GET[$keyfld], $context);
    }
}

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>
<script src="MyScript/itemmain.js" type="text/javascript"></script>

</head>
<body>
    <form action='' method='POST' enctype="multipart/form-data">

        <!-- Default box -->
        <div class="col-lg-6 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">Sub 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 " name="subname" value="<?php Echo ($addflag != 1) ? $get["subname"] : ""; ?>" required >
                        </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 numeric pad">
                            <label class="label-control marg">Name to Print</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                            <input type="text" class="form-control form " name="nameprint" value="<?php Echo ($addflag != 1) ? $get["nameprint"] : ""; ?>" >
                        </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">Karigar</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                            <input type="text" class="form-control form " name="kargname" value="<?php Echo ($addflag != 1) ? $get["kargname"] : ""; ?>" >
                        </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-1 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-2 col-md-2 col-sm-1 col-xs-1 text-right pad">
                            <label class="label-control marg  text-right" style="color:#337AB7">MATERIAL</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">PCS Per</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <select class="form-control form select2" name="pcsper" style="width:100%"> 
                                <option value="<?php Echo ($addflag != 1) ? $get["pcsper"] : ""; ?>">
                                    <?php
                                    if ($get["pcsper"] == "Y") {
                                        Echo "YARD";
                                    } elseif ($get["pcsper"] == "M") {
                                        Echo "METERS";
                                    }
                                    ?>
                                </option>
                                <option value="Y">YARD</option>
                                <option value="M">METERS</option>
                            </select>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pad">
                            <input type="number" step="any" class="form-control text-right form cost totpcs" name="totpcs" value="<? Echo ($addflag != 1 ) ? $get['totpcs'] : 0.00; ?>">
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pad">
                            <label class="label-control marg">PCS</label>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-2 col-md-2 col-sm-1 col-xs-1  text-right pad">
                            <label class="label-control marg  text-right" style="color:#337AB7">ASTAR</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">PCS Per</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <select class="form-control form select2" name="pcspera" style="width:100%"> 
                                <option value="<?php Echo ($addflag != 1) ? $get["pcspera"] : ""; ?>">
                                    <?php
                                    if ($get["pcspera"] == "Y") {
                                        Echo "YARD";
                                    } elseif ($get["pcspera"] == "M") {
                                        Echo "METERS";
                                    }
                                    ?>
                                </option>
                                <option value="Y">YARD</option>
                                <option value="M">METERS</option>
                            </select>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pad">
                            <input type="number" step="any" class="form-control text-right form totpcsa" name="totpcsa" value="<? Echo ($addflag != 1 ) ? $get['totpcsa'] : 0.00; ?>">
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 pad">
                            <label class="label-control marg">PCS</label>
                        </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  numeric" name="selrate" 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('blankbar.php?barcode=<?= $get['itemcode'] ?>&number=' + document.invoice.nolabel.value + '&name=' + document.invoice.name.value + '&mrp=' + document.invoice.mrp.value +  '&barcode=' + document.invoice.code.value + '&msp=' + document.invoice.msp.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 class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                                <button <?php if ($addflag != 1) { ?> onclick="window.open('thermal_barcode3.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-body">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="float: right">
                        <h4>MATERIAL CONSUMPTION</h4>
                        <table class="table table-striped table-condensed table-bordered" width="50%">
                            <thead  style="" class="mySearchTr">
                                <tr>
                                    <th width="2%"></th>
                                    <th width="2%">Sr No.</th>
                                    <th width="45%">Material.</th>
                                    <th width="10%" class='text-right'>Qty</th>
                                    <th width="10%"class='text-right'>Unit</th>
                                    <th width="15%"class='text-right'>Rate</th>
                                    <th width="15%"class='text-right'>Amount</th>

                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                if ($addflag != 1) {

                                    $gettran = mysqli_query($conn, "SELECT * FROM $trandb WHERE itemcode = '$get[code]' ORDER BY srno")or die(mysqli_error($conn));
                                    $count = mysqli_num_rows($gettran);
                                    if ($count > 0) {
                                        $ctr = 0;
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $findmaterial = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` WHERE `code`='$tran[material]'") or die(mysqli_error($conn));
                                            $fmat = mysqli_fetch_array($findmaterial);

                                            $getunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit` WHERE `code`='$tran[unit]'") or die(mysqli_error($conn));
                                            $funit = mysqli_fetch_array($getunit);
                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form material select2' name='material_$ctr' style='width:100%'>";
                                            Echo "<option value='$fmat[code]'>$fmat[name]</option>";
                                            $gmat = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat`  where inactive != 1 order by name")or die(mysqli_error($conn));
                                            while ($material = mysqli_fetch_array($gmat)) {
                                                Echo "<option value='$material[code]'>$material[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type = 'text' class = 'form-control form text-right qty' name = 'qty_$ctr' value = '$tran[qty]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form unit select2' name='unit_$ctr' style='width:100%'>";
                                            Echo "<option value='$funit[code]'>$funit[name]</option>";
                                            $gunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit` order by name")or die(mysqli_error($conn));
                                            while ($unit = mysqli_fetch_array($gunit)) {
                                                Echo "<option value='$unit[code]'>$unit[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "<td><input type = 'text' class = 'form-control form text-right rate' name = 'rate_$ctr' value = '$tran[rate]'></td>";
                                            Echo "<td><input type = 'text' class = 'form-control form text-right amount' name = 'amount_$ctr' value = '$tran[amount]'></td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    }
                                } else {
                                    $pctr = 0;
                                    ?>
                                    <tr>
                                        <td><i class="fa fa-close remove"></i></td>
                                        <td><input type="text" class="form-control form text-right srno" name="srno_0" value="1"></td>
                                        <td>
                                            <select class="form-control form material select2" name="material_0" style='width:100%'>
                                                <option value=""></option>
                                                <?php
                                                $gmat = mysqli_query($conn, "SELECT * FROM `rawmat` where inactive != 1 ORDER BY name")or die(mysqli_error($conn));
                                                while ($material = mysqli_fetch_array($gmat)) {
                                                    Echo "<option value='$material[code]'>$material[name]</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td><input type="text" class="form-control form text-right qty" name="qty_0" value="0"></td>
                                        <td>
                                            <select class="form-control form unit select2" name="unit_0" style='width:100%'>
                                                <option value=""></option>
                                                <?php
                                                $gunit = mysqli_query($conn, "SELECT * FROM `unit` ORDER BY name")or die(mysqli_error($conn));
                                                while ($unit = mysqli_fetch_array($gunit)) {
                                                    Echo "<option value='$unit[code]'>$unit[name]</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td><input type="text" class="form-control form text-right rate" name="rate_0" value="0"></td>
                                        <td><input type="text" class="form-control form text-right amount" name="amount_0" value="0"></td>
                                    </tr>  
                                    <?
                                    $pctr++;
                                    ?>

                                <? } ?>
                            </tbody>
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />  
                        </table>
                        <div class="col-lg-12">

                            <div class="col-lg-6">
                                <div class="col-lg-1 col-xs-1 pad">
                                    <button type="button" class="btn btn-primary btn-square form"  id="addnew">
                                        <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                                    </button>

                                </div>
                            </div>
                            <div class="col-lg-6">
                                <div class="col-lg-6 pad text-right">
                                    <label class="label-control marg" style='color:#337AB7'>Total</label>
                                </div>
                                <div class="col-lg-5 pad">
                                    <input type="text" name="totamt" class="form-control form text-right totamt"  value="<?php echo ($addflag != 1) ? $get['totamt'] : "0.00"; ?>" readonly>
                                </div>
                            </div>


                        </div>
                    </div>


                </div>
                <div class="row">

                    <div class="col-lg-1 pad text-right">
                        <a id='print' class="btn btn-pad btn-square " style="background-color:#337AB7;" href="itemdet_print.php?vouchno=<?= $get['code']; ?>" target="_blank">
                            <i class="fa fa-suitcase" style="color:#ffffff"></i>
                        </a>

                    </div>
                    <div class="col-lg-11 pad text-right">

                        <?php include("includes/buttons.php");
                        ?>
                    </div>

                </div>
            </div>
        </div>
        <div class="col-lg-6 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  style="" class="mySearchTr">
                                <tr>
                                    <th width="5%">Bill No.</th>
                                    <th width="15%" class=''>Date</th>
                                    <th width="40%"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="tbody2">
                                <?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>

                    </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">Choose File</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <input type="file" name="file1" id="file1" class="form btn btn-primary">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12">
                            <img id="preview" src="<?php Echo $imgpath; ?>" alt="your image"/>
                        </div>
                    </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 table-bordered">
                            <thead>
                                <tr style="" 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">Sub Name</th>
                                    <th style="width:70px;  text-align:left">Group</th>
                                    <th style="width:70px;  text-align:left">Sub Group</th>
                                    <th style="width:70px;  text-align:left">Karigar</th>
                                    <th style="width:70px;  text-align:right">Cost</th>
                                    <th style="width:10px;  text-align:right">Rate</th>
                                    <th style="width:10px;  text-align:left">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` ORDER BY `name`")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;'>$row[subname]</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;'>$row[kargname]</td>";
                                        echo "<td style='width:10px;  text-align:right; padding-left:20px;'>$row[cost]</td>";
                                        echo "<td style='width:10px;  text-align:right; padding-left:20px;'>$row[rate]</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>
