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

$formname = 'Raw Material';
$maindb = 'rawmat';
$stkdb = 'transacn';
$printfile = "rawmat_print.php";
$file = 'rawmat.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";

//$focusfield = "name";

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

$username = $_SESSION["username_" . $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"];
    $type = $_POST["type"];
    $unit = $_POST["unit"];
    $cost = $_POST["cost"];
    $rate = $_POST["rate"];
    $hsncode = $_POST["hsncode"];
    $gstperc = $_POST["gstperc"];
    $rawmatgrp = $_POST["rawmatgrp"];

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


    if ($a == 1) {


        //        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE") or die(mysqli_error($conn));
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE") or die(mysqli_error($conn));
        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;

            mysqli_query($conn, "INSERT INTO `$maindb`(code,`name`, `type`,`rate`, cost, `hsncode`,`gstperc`,rawmatgrp, `adduser`)
                                        VALUES($qvouch,'$name', '$type','$rate', '$cost', '$hsncode','$gstperc','$rawmatgrp', '$username')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name',`hsncode`='$hsncode', `gstperc`='$gstperc', cost='$cost', rawmatgrp='$rawmatgrp',
                                              `type`='$type' , `rate`='$rate', `edituser`='$username' where `code` = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, "UNLOCK TABLES");
    }
}

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

    $getgrank = mysqli_query($conn, "SELECT `code`, `name` FROM `rawmatgrp` WHERE `code`='$get[rawmatgrp]'") or die(mysqli_error($conn));
    $frawmatgrp = mysqli_fetch_array($getgrank);
}

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


if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, rawtran WRITE") or die(mysqli_error($conn));

    //    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, LOCK TABLES `$itmdb` WRITE, LOCK TABLES `$stkdb` WRITE") or die(mysqli_error($conn));

    $getdel = mysqli_query($conn, "SELECT item FROM `rawtran` WHERE `item`='$qvouch'") or die(mysqli_error($conn));
    //    $getdel1 = mysqli_query($conn, "SELECT rawmatcode FROM `sordsacn` WHERE `rawmatcode`='$qvouch'") or die(mysqli_error($conn));
    $fdel = mysqli_num_rows($getdel);
    //    $fdel1 = mysqli_num_rows($getdel1);
    $tfdel = $fdel;

    //    browse($tfdel);
    if ($tfdel > 0) {
        browse("Raw Material Exist In Transaction");
        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    }
    mysqli_query($conn, "UNLOCK TABLES");
}

$repeatVoucher = "";
if (isset($_GET["code"])) {
    $repeatVoucher = $_GET["code"];
}
if ($_GET["mode"] == "last") {
    $repeatVoucher = $qvouch;
}

if (isset($_GET["repeat"])) {
    $addflag = 0;

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

    //    $getgcust = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE `accode`='$get[party]'") or die(mysqli_error($conn));
    //    $gparty = mysqli_fetch_object($getgcust);


    $getgrank = mysqli_query($conn, "SELECT `code`, `name` FROM `rawmatgrp` WHERE `code`='$get[rawmatgrp]'") or die(mysqli_error($conn));
    $frawmatgrp = mysqli_fetch_array($getgrank);

    $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));

    if (mysqli_num_rows($gen1) > 0) {
        $vvouch = mysqli_fetch_assoc($gen1);
        $rqvouch = $vvouch["maxcode"] + 1;
    } else {
        $rqvouch = '1';
    }
    $qvouch = $rqvouch;
}

include("includes/linkshead.php");
if ($addflag != 1) {
    $read = 'readonly';
}
?>

<script>
    $(document).ready(function () {
<?php
if (isset($_GET["repeat"])) {
    ?>
            $('body').find('.form').removeAttr('disabled', 'disabled');
    <?php
}
?>
    });
</script>
<script src="MyScript/rawmat.js" type="text/javascript"></script>
</head>

<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <div class="row">
            <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>
                            <!--<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">Code</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <!--<input type="code" class="form-control form focusfield" name="name" value="<?php // Echo ($addflag != 1) ? $get["name"] : "";                                     
?>" >-->
                                <input type="text" class="form-control form " name="code" value="<?php echo ($addflag != 1) ? $qvouch : "$qvouch"; ?>" readonly>
                            </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">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8  pad">
                                <input type="text" class="form-control form focusfield" name="name" value="<?php echo ($addflag != 1) ? $get["name"] : ""; ?>" autofocus maxlength="50">
                            </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">Raw Material Group</label>
                            </div>
                            <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                                <select class="form-control rawmatgrp form select2" name="rawmatgrp" required>
                                    <option value="<?php echo ($addflag != 1) ? $frawmatgrp['code'] : ""; ?>"><? echo ($addflag != 1) ? $frawmatgrp['name'] : ""; ?></option>
                                    <?php
                                    $gitemgrp = mysqli_query($conn, "SELECT * FROM `rawmatgrp`") 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">Unit</label>
                            </div>
                            <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                                <select class="form-control unit form select2" name="unit" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['unit'] : ""; ?>"><? echo ($addflag != 1) ? $get['unit'] : ""; ?></option>
                        <?php
                        $gitemunit = mysqli_query($conn, "SELECT * FROM `unit` ") or die(mysqli_error($conn));
                        while ($itemunit = mysqli_fetch_object($gitemunit)) {
                            echo "<option value='$itemunit->name'>$itemunit->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">Cost</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-6 col-xs-6  pad">
                                <input type="number" step="any" class="form-control form  text-right cost" name="cost" value="<?php echo ($addflag != 1) ? $get["cost"] : ""; ?>">
                            </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">Rate</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-6 col-xs-6  pad">
                                <input type="number" step="any" class="form-control form  text-right" name="rate" value="<?php echo ($addflag != 1) ? $get["rate"] : ""; ?>">
                            </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" 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="number" step="any" maxlength="9" step="any" class="form-control form text-right gstperc" name="gstperc" value="<?php echo ($addflag != 1) ? $get["gstperc"] : ""; ?>">
                                </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">
                                    <th style="width:10px;  text-align:left">Code</th>
                                    <th style="width:70px;  text-align:left">Name</th>
                                    <th style="width:60px;  text-align:right">Rate</th>
                                    <th style="width:10px;  text-align:left">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $tp = '';
                                    $srchresult = mysqli_query($conn, "SELECT `code`,`name`,rate,gstperc FROM `$maindb` ORDER BY `name`") 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 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: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>

</html>