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

$formname = 'Raw Material';
$maindb = 'rawmat';
$file = 'rawmat.php';
$grid = '';
$keyfld = "code";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'rawmatlst.php';

$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"];
    $itemgrp    = $_POST["itemgrp"];
    $cost       = $_POST["cost"];
    $rate       = $_POST["rate"];
    $mrp        = $_POST["mrp"];
    
    
    
    $mvouch = $qvouch;
    $ratenew = intval($rate);
    $barcode    = $mvouch.$ratenew;
    if ($name == " ") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }
    
    if ($itemgrp == " ") {
        $a = 0;
        browse("Group Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }

    if (isset($_POST["code"])) {
        $result1 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`= '$code' AND $keyfld != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result1) > 0) {
            $a1 = 0;
            browse("Code Already Exist");
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }
    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND $keyfld != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(code, `name`, `itemgrp`, `cost`, `rate`,`mrp`)
                                Values ('$qvouch', '$name', '$itemgrp', '$cost', '$rate', '$mrp')") or die(mysqli_error($conn));
            insmess();
//            $mvouch = $qvouch;
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET code='$qvouch', `name`='$name', `itemgrp`='$itemgrp', `cost`='$cost', `rate`='$rate', `mrp`='$mrp' where $keyfld = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
//            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);

    $grp = mysqli_query($conn, "SELECT * FROM `rawmatgrp` WHERE code='$get[itemgrp]'") or die(mysqli_error($conn));
    $getg = mysqli_fetch_object($grp);
}

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
include("includes/files.php");
?>

<body>
    <form method="POST" class="form-horizontal" role="form" action='' name="raw material">
        <div class="row" style="">
            <div class="col-lg-6" style=''>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5 style="font-family: cipher-regular,helvetica; "><?= $formname ?></h5>
                        <!-- .toolbar -->
                        <div class="toolbar">
                            <ul class="nav">
                                <li>
                                    <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-1">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>
                            </ul>
                        </div>
                        <!-- /.toolbar -->
                    </header>
                    <div id="div-1" class="accordion-body collapse in body" style="height: 277px;font-family: cipher-regular,helvetica;">
                        <div class="form-horizontal"><br>
<!--                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Barcode</label>
                                <div class="col-lg-4">
                                    <input type="text" name="barcode" autofocus="" id="" placeholder="Barcode" value="<?php echo ($addflag != 1) ? $get['barcode'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>-->
                            <div class="form-group">
                                <label class="control-label col-lg-4" id="name" style="width: 150px;font-family: cipher-regular,helvetica;"> Name </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="name" placeholder="Name" value="<?php echo ($addflag != 1) ? $get['name'] : ''; ?>" style="height: 30px;" class="form-control form"  required="">
                                </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4" id="group" style="width: 150px;font-family: cipher-regular,helvetica;"> Group </label>
                                <div class="col-lg-8"> 
                                    <select id="itemgrp" name="itemgrp" class="form-control form" style="height: 33px;" required>
                                        <option value="<?php echo ($addflag != 1) ? $getg->code : " "; ?>"><?php echo ($addflag != 1) ? $getg->name : " "; ?></option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT * FROM `rawmatgrp` order by `name`") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mown)) {
                                            echo "<option value=$trow[code]>$trow[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4" id="cost" style="width: 150px;font-family: cipher-regular,helvetica;"> Cost Price </label>
                                <div class="col-lg-3"> 
                                    <input type="text" style="text-align: right;" name="cost" placeholder="Cost Price" value="<?php echo ($addflag != 1) ? $get['cost'] : ''; ?>" style="height: 30px;" class="form-control form"  >
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4" id="rate" style="width: 150px;font-family: cipher-regular,helvetica;"> Selling Price </label>
                                <div class="col-lg-3"> 
                                    <input type="text" style="text-align: right;" name="rate" placeholder="Selling Price" value="<?php echo ($addflag != 1) ? $get['rate'] : ''; ?>" style="height: 30px;" class="form-control form"  >
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4" id="rate" style="width: 150px;font-family: cipher-regular,helvetica;"> MRP </label>
                                <div class="col-lg-3"> 
                                    <input type="text" style="text-align: right;" name="mrp" placeholder="Mrp" value="<?php echo ($addflag != 1) ? $get['mrp'] : ''; ?>" style="height: 30px;" class="form-control form"  >
                                </div> 
                            </div>
<!--                            <div class="form-group">
                                <label class="control-label col-lg-4" id="trans" style="width: 150px;font-family: cipher-regular,helvetica;"> Transport Chgs.</label>
                                <div class="col-lg-3"> 
                                    <input type="text" style="text-align: right;" name="trans" placeholder="Transport Charges" value="<?php echo ($addflag != 1) ? $get['trans'] : ''; ?>" style="height: 30px;" class="form-control form"  >
                                </div> 
                            </div> -->
                        </div>   
                    </div> 
                </div> 
            </div> 
        </div>
        <div id="formfoot" style="width:50%;">                        
            <h1><?php include("includes/buttons.php"); ?></h1>
        </div>

        <!--SEARCH PART-->

        <div class="modal fade in slacker-modal" id="searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="false">
            <div class="modal-dialog modal-slacker">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myLargeModalLabel"></h4>
                    </div>
                    <div class="modal-body" style="height: 100%;white-space: nowrap;">
                        <div class="row">

                            <div class="col-md-12">

                                <div class="col-lg-12">

                                    <div class="box">
                                        <div id="collapse4" class="body">
                                            <table id="example1" class="table table-bordered table-condensed table-hover table-striped">
                                                <thead>
                                                    <tr>
                                                        <th align='left'>Name</th>
                                                        <th align='left'>Group</th>
                                                        <th align='left'>Selling Price</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld!=''") or die(mysqli_error($conn));
                                                    while ($row = mysqli_fetch_array($srchresult)) {
                                                        $grpresult = mysqli_query($conn, "SELECT * FROM `rawmatgrp` WHERE `code`='$row[itemgrp]'") or die(mysqli_error($conn));
                                                        $row1 = mysqli_fetch_array($grpresult);
                                                        echo "<tr>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[name]</td>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row1[name]</td>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[sellingprice]</td>";
                                                        
                                                        echo "</tr>";
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                            <script>
                                                $(function () {
                                                    metisTable();
                                                    metisSortable();
                                                });
                                            </script>

                                        </div>

                                    </div>

                                </div>

                            </div><!-- /.row -->

                        </div>
                    </div>  
                    <div class="modal-footer" style="bottom: 0px;position: absolute;padding:0px;">
                        <!--"This is where i want to load my salemain.php"-->
                    </div>
                </div>
            </div>
        </div>
    </form>

    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>
</html>