<?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"];
    $cost = $_POST["cost"];
    $act_cost = $_POST["act_cost"];
    $rate = $_POST["rate"];
    $unit = $_POST["unit"];
    $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) {


        $path = "admin/itemimage/";
        $filename1 = $mvouch . ".jpg";
        $img_file1 = $path . ($filename1);

        $image_ftype = pathinfo($img_file1, PATHINFO_EXTENSION);
        $size = $_FILES['image']['size'];

        // Restrict file size to 2MB (2048000 bytes)
        if ($size <= 512000) {
            move_uploaded_file($_FILES["image"]["tmp_name"], $img_file1);
        } else {
            browse("Image size should be less than 500KB. Please try again.");
            header("refresh: 2; $file?mode=last");
            exit();
        }

        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, `image`,`act_cost`,`unit`)
                                        VALUES($qvouch,'$name', '$type','$rate', '$cost', '$hsncode','$gstperc','$rawmatgrp', '$img_file1','$act_cost','$unit')") 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', `unit`='$unit',
                                              `type`='$type' , `rate`='$rate', image='$img_file1',`act_cost`='$act_cost' 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);
    
    $gunit = mysqli_query($conn, "SELECT `code`, `name` FROM `unit` WHERE `code`='$get[unit]'") or die(mysqli_error($conn));
    $funit = mysqli_fetch_array($gunit);
}

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


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

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

    if ($tfdel > 0) {
        browse("Raw Material Exist In Transaction");
        header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
    } else {

        $imgfile = "admin/itemimage/" . $_GET[$keyfld] . ".jpg";
        unlink($imgfile);

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


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

    $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?a=<?= mt_rand() ?>" type="text/javascript"></script>

<style>
    .upload-btn-wrapper {
        position: relative;
        overflow: hidden;
        display: inline-block;
    }

    .skbtn {
        border: 2px solid gray;
        color: gray;
        background-color: white;
        width: 150px;
        height: 40px;
        border-radius: 8px;
        font-size: 15px;
        font-weight: bold;
        margin-left: 30px;
    }

    .upload-btn-wrapper input[type=file] {
        font-size: 100px;
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
    }

    .skimg {
        /* border: #E7B315 solid 1px; */
        width: 50%;
        height: auto;
        margin-left: 10px;
        margin-top: 30px;
        margin-bottom: 30px;
        border-radius: 6px;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    }
</style>
</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` order by name ") 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">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 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 unit form select2" name="unit" required>
                                    <option value="<?php echo ($addflag != 1) ? $funit['code'] : ""; ?>">
                                        <? echo ($addflag != 1) ? $funit['name'] : ""; ?>
                                    </option>
                                    <?php
                                    $getunit = mysqli_query($conn, "SELECT * FROM `unit` order by name ") or die(mysqli_error($conn));
                                    while ($fetunit = mysqli_fetch_object($getunit)) {
                                        echo "<option value='$fetunit->code'>$fetunit->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">Act.</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 act_cost" name="act_cost"
                                    value="<?php echo ($addflag != 1) ? $get["act_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">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">Selling 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-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/buttons.php"); ?>
                    </div>
                </div>
            </div>

            <div class="col-lg-5 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">Image Upload</h3>
                    </div>

                    <div class="panel-body">
                        <div class="row">
                            <div class="upload-btn-wrapper">
                                <button class="skbtn">Choose Image</button>
                                <input type="file" class="form image" name="image" accept="image/*" />
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-12">
                                <img class="view1 skimg"
                                    src="<?php echo ($addflag != 1) ? $get['image'] . "?a=" . mt_rand() : "" ?>" alt="Image" />
                            </div>
                        </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 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">Group</th>
                                    <th style="width:70px;text-align:left">Type</th>
                                    <th style="width:60px;text-align:right">Act.</th>
                                    <th style="width:60px;text-align:right">Cost</th>
                                    <th style="width:60px;text-align:right">Rate</th>
                                    <th style="width:10px;text-align:right">GST%</th>
                                    <th style="width:10px;text-align:left">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $tp = '';
                                    $srchresult = mysqli_query($conn, "SELECT a.`code`, a.`name`, a.type, a.act_cost, a.cost, a.rate, a.gstperc, b.name as rawmatgrpnm 
                                                                             FROM `$maindb` a
                                                                             LEFT JOIN rawmatgrp b ON a.rawmatgrp=b.code ORDER BY a.`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;'>$row[rawmatgrpnm]</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[act_cost]</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:right; padding-left:20px;'>$row[gstperc]</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>