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

$formname = 'Raw Material Group';
$maindb = 'rawmatgrp';

$myyear = $_SESSION[$myyear];


$printfile = "rawmatgrp_print.php";
$file = 'rawmatgrp.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));

            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));
            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"])) {

    $resperf = mysqli_query($conn, "SELECT `rawmatgrp` FROM `rawmat` WHERE `rawmatgrp`='$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");
    }
}


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>
                        <?php if ($_SESSION["compchng"] > 0) { ?>

                            <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> 
                        <?php } ?>

                    </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="">
                                    <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>
