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

$formname = 'Item Group';
$maindb = 'itemgrp';

$myyear = $_SESSION[$myyear];


$printfile = "itemgrp_print.php";
$file = 'itemgrpmain.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";
$focusfield = "name";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

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

//$maindb1 = 'apps4rea_msp_101_' . $myyear;
//$maindb2 = 'apps4rea_msp_102_' . $myyear;
//$maindb3 = 'apps4rea_msp_103_' . $myyear;
//$maindb4 = 'apps4rea_msp_104_' . $myyear;
//$maindb5 = 'apps4rea_msp_105_' . $myyear;


include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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

    $a = 1;
    $name = $_POST["name"];
    $hsncode = $_POST["hsncode"];
    $gstperc = $_POST["gstperc"];
    $chg = $_POST["chg"];


    $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?code=$qvouch&mode=last");
        }
    }

    if ($a == 1) {


        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(code, `name`,`hsncode`, `gstperc`, `chg`)
                                        VALUES($qvouch, '$name','$hsncode', '$gstperc', '$chg')") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$maindb1`.`$maindb`(code, `name`,`hsncode`, `gstperc`)
//                                        VALUES($qvouch, '$name','$hsncode', '$gstperc')") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$maindb2`.`$maindb`(code, `name`,`hsncode`, `gstperc`)
//                                        VALUES($qvouch, '$name','$hsncode', '$gstperc')") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$maindb3`.`$maindb`(code, `name`,`hsncode`, `gstperc`)
//                                        VALUES($qvouch, '$name','$hsncode', '$gstperc')") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$maindb4`.`$maindb`(code, `name`,`hsncode`, `gstperc`)
//                                        VALUES($qvouch, '$name','$hsncode', '$gstperc')") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$maindb5`.`$maindb`(code, `name`,`hsncode`, `gstperc`)
//                                        VALUES($qvouch, '$name','$hsncode', '$gstperc')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1
            mysqli_query($conn, "UPDATE $maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc', `chg`='$chg' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            if ($compcd == 101) {
//                mysqli_query($conn, "UPDATE $maindb2.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb3.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb4.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb5.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            }
//            if ($compcd == 102) {
//                mysqli_query($conn, "UPDATE $maindb1.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb3.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb4.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb5.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            }
//            if ($compcd == 103) {
//                mysqli_query($conn, "UPDATE $maindb1.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb2.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb4.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb5.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            }
//            if ($compcd == 104) {
//                mysqli_query($conn, "UPDATE $maindb1.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb2.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb3.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb5.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            }
//            if ($compcd == 105) {
//                mysqli_query($conn, "UPDATE $maindb1.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb2.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb3.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE $maindb4.$maindb SET `name`='$name', `hsncode`='$hsncode', `gstperc`='$gstperc' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            }

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, "UPDATE `item` SET `hsncode`='$hsncode', `gstperc`='$gstperc'  WHERE `itemgrp` = '$mvouch' ") or die(mysqli_error($conn));

        if ($addflag == 1) {

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

$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {
    if ($getvouch == 1) {
        browse("ITEM GROUP - PERFUME IS FIXED CANNOT BE DELETED");
        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
    } else {
        $resperf = mysqli_query($conn, "SELECT `itemgrp` FROM `item` WHERE `itemgrp`='$getvouch'") or die(mysqli_error($conn));
        $perfet = mysqli_num_rows($resperf);
        if ($perfet > 0) {
            browse("ITEM GROUP AVAILABLE IN ITEM");
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
        } else {
            delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//            delete($maindb1, "$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");
//            delete($maindb4, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//            delete($maindb5, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        }
    }
}
//if (isset($_GET["delete"])) {
//    $resperf = mysqli_query($conn, "SELECT `itemgrp` FROM `item` WHERE `itemgrp`='$getvouch'") or die(mysqli_error($conn));
//    $perfet = mysqli_num_rows($resperf);
//    if ($getvouch == 1) {
//        $resperf1 = mysqli_query($conn, "SELECT `code`,`name` FROM `$maindb` WHERE `code`='$getvouch'") or die(mysqli_error($conn));
//        $perfet1 = mysqli_num_rows($resperf1);
//    }
//    browse($perfet1);
//    if ($perfet1 > 0) {
//        browse("ITEM GROUP - PERFUME IS FIXED CANNOT BE DELETED");
//        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
//    } else if ($perfet > 0) {
//        browse("ITEM GROUP AVAILABLE IN ITEM");
//        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
//    } else {
//        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    }
//}

include("includes/linkshead.php");
?>
<!--<script src="MyScript/challan.js" type="text/javascript"></script>-->
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6">
                <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>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <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">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 focusfield" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </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">HSN Code</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-8 pad">
                                <input type="text" class="form-control form " name="hsncode" value="<?php Echo ($addflag != 1) ? $get["hsncode"] : ""; ?>" required>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-8 pad">
                                <label class="label-control marg">GST %</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="number" step="any"  maxlength="9" class="form-control form text-right" name="gstperc" value="<?php Echo ($addflag != 1) ? $get["gstperc"] : ""; ?>" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">GST Change</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="checkbox"  class="form-control form chg" <?php
                                if ($get['chg'] == "1") {
                                    Echo "checked";
                                }
                                ?> value="1" name="chg">                            
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class=" table-responsive">
                        <table id="example1" class="table table-striped table-bordered">
                            <thead>
                                <tr class="mySearchTr" style="background-color: #78909C;">
                                    <th>Name</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[name]</td>";
                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
