<?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];
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"];
    $rate = $_POST["rate"];
    $hsncode = $_POST["hsncode"];
    $gstperc = $_POST["gstperc"];
    $inactive = $_POST["inactive"];

    $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`, `hsncode`,`gstperc`,inactive)
                                        VALUES($qvouch,'$name', '$type','$rate','$hsncode','$gstperc','$inactive')") 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',inactive='$inactive',
                                              `type`='$type' , `rate`='$rate' 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);
}

$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_object($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-5 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>
                            </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">Type</label>
                            </div>
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 pad">
                                <select class="form-control form " name="type">
                                    <option value=''></option>
                                    <option value='A'<?php
                                    if ($addflag != 1) {
                                        if ($get["type"] == "A") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?> >ACCESSORY</option>

                                    <option value='F'<?php
                                    if ($addflag != 1) {
                                        if ($get["type"] == "F") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?> >FABRIC</option>
                                    <option value='O'<?php
                                    if ($addflag != 1) {
                                        if ($get["type"] == "O") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?> >OTHER</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">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>
                        <div class="row">

                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 numeric pad">
                                <label class="label-control marg" style="color:red;font-size:18px">Inactive</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="checkbox" class="form-control form inactive" style="" name="inactive" value="1" <?
                                if ($get['inactive'] == 1) {
                                    Echo "checked";
                                }
                                ?>>
                            </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="number" step="any"  maxlength="9" step="any" class="form-control form gstperc" name="gstperc" value="<?php Echo ($addflag != 1) ? $get["gstperc"] : ""; ?>"> 
                                                    </div>
                                                </div>-->

                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttmast.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:70px;  text-align:left">Type</th>
                                    <th style="width:70px;  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`,type,rate FROM `$maindb` ORDER BY `name`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        if ($row["type"] == 'A') {
                                            $tp = 'Accesory';
                                        } else if ($row["type"] == 'F') {
                                            $tp = 'Fabric';
                                        } else {
                                            $tp = 'Other';
                                        }

                                        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;'>$tp</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>
