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

$formname = 'Product';
$maindb = 'product';
$trandb = 'prodhist';
$file = 'product.php';

$grid = '';
$keyfld = "code";
$printfile = 'product_print.php';
$trandbexist = 1;
$focusfld = "name";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    $a = 1;
    $qvouch = $_POST["code"];
    $prodcode = $_POST["prodcode"];
    $name = trim($_POST["name"]);

    $prodgrp = $_POST["prodgrp"];
    $unit = $_POST["unit"];
    $cost = trim($_POST["cost"]);
    $sellprice = trim($_POST["rate"]);
    
    $open = $_POST["open"];
    $minstock = $_POST["minstock"];
    $hsncode = $_POST["hsncode"];
    $gstperc = $_POST["gstperc"];

    $count = $_POST["items"];
       
    $mvouch = $qvouch;
    
    if (empty($name)) {
        browse("Name Cannot Be Empty");
        $a = 0;
    }

    if (isset($_POST["prodcode"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `prodcode` = '$prodcode' and code != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Product Code Already Exist");
            header("refresh: 1; $file?code=$qvouch&mode=last");
        }
    }
    
    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code` != '$mvouch' AND `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) {

        mysqli_query($conn, "LOCK TABLES `$maindb`,$trandb WRITE ");
        
        
        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM `$maindb` where code != '' $keycond 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`, `prodcode`, `name`, `prodgrp` ,`unit`, `cost`, `rate`,
                                               `open`, `minstock`, `hsncode`, `gstperc`, `adduser`, `adddate`, `addtime`)
                           VALUES('$mvouch', '$prodcode', '$name', '$prodgrp' ,'$unit', '$cost' , '$sellprice',
                                  '$open', '$minstock', '$hsncode', '$gstperc', '$adduser', '$adddate', '$addtime')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");
            mysqli_query($conn, "UPDATE `$maindb` SET  `name`='$name', `prodgrp`='$prodgrp', `unit`='$unit', `cost`='$cost', `rate`='$sellprice',
                                               `open`='$open', `minstock`='$minstock', `hsncode`='$hsncode',`gstperc`='$gstperc',
                                               `edituser`='$adduser', `editdate`='$editdate', `edittime`='$edittime'  WHERE code = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `code`='$qvouch'") or die(mysqli_error($conn));

        $mctr = 0;
        for ($i = 0; $i <= $count; $i++) {
            $mctr++;
            $msrno = $_POST["srno_$i"];
            $date = $_POST["date_$i"];
            $rate = $_POST["rate_$i"];
            
            if (!empty($date)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`code`, `srno`, `date`,`rate`)
                                            VALUES('$qvouch', '$mctr', '$date', '$rate')") or die(mysqli_error($conn));
            }
        }
        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);

    $gpgrp = mysqli_query($conn, "SELECT * FROM `prodgrp` WHERE code='$get[prodgrp]'") or die(mysqli_error($conn));
    $fprodgrp = mysqli_fetch_array($gpgrp);

    $getgst = mysqli_query($conn, "SELECT * FROM `gsttype` WHERE per='$get[gstperc]'") or die(mysqli_error($conn));
    $fetgst = mysqli_fetch_array($getgst);
    
    $rd = "readonly";

    $prodcode = $get['prodcode'];
    // browse($aa);
}

$senable = "";
$cenable = "";
if (isset($_GET["delete"])) {
    $chkresult = mysqli_query($conn, "SELECT prodcode FROM `transacn` WHERE prodcode='$prodcode'") or die(mysqli_error($conn));
    $fetget = mysqli_num_rows($chkresult);
    if ($fetget > 0) {
        browse('Product Exist in Transaction');
    } else {

        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    }
}
$versionToken = mt_rand();
include("includes/linkshead.php");
?>

<script src="MyScript/product.js<?= '?version=' . $versionToken ?>" type="text/javascript"></script>
<script src="MyScript/product_search.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>

        <!-- Default box -->

        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <?php include("includes/usermention.php"); ?>

                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 numeric 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="text" class="form-control form prodcode" name="prodcode" value="<?php Echo ($addflag != 1) ? $get["prodcode"] : ""; ?>" <?= $rd ?>>

                                <input type="hidden" class="form-control form code" maxlength="6" name="code" value="<?php Echo ($addflag != 1) ? $get["code"] : "$qvouch"; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 numeric text-right  pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-6 col-md-9 col-sm-9 col-xs-6 pad">
                                <input type="text" class="form-control form prodname" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 numeric text-right pad">
                                <label class="label-control marg">Group</label>
                            </div>
                            <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                                <select class="form-control form select2 prodgrp" name="prodgrp" style="width:100%" required>
                                    <option value="<?php echo ($addflag != 1) ? $fprodgrp['code'] : ""; ?>"><? echo ($addflag != 1) ? $fprodgrp['name'] : ""; ?></option>
                                    <?php
                                    $gprodgrp = mysqli_query($conn, "SELECT * FROM `prodgrp`")or die(mysqli_error($conn));
                                    while ($prodgrp = mysqli_fetch_object($gprodgrp)) {
                                        Echo "<option value='$prodgrp->code'>$prodgrp->name</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 numeric text-right  pad">
                                <label class="label-control marg">Cost Price</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-2 col-xs-6 pad">
                                <input type="text" class="form-control form text-right cost" name="cost" value="<?php Echo ($addflag != 1) ? $get["cost"] : "0"; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 numeric text-right  pad">
                                <label class="label-control marg">Selling Rate</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-2 col-xs-6 pad">
                                <input type="text" class="form-control form rate text-right" name="rate" value="<?php Echo ($addflag != 1) ? $get["rate"] : "0"; ?>" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 numeric text-right pad">
                                <label class="label-control marg">Unit</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form select2" name="unit" style="width:100%" required> 
                                    <option value=" <?php echo $get['unit'] ?>"><?php echo $get['unit'] ?></option>
                                    <?php
                                    $gunit = mysqli_query($conn, "SELECT * FROM `unit`")or die(mysqli_error($conn));
                                    while ($unit1 = mysqli_fetch_array($gunit)) {
                                        Echo "<option value='$unit1[name]'>$unit1[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 numeric text-right pad">
                                <label class="label-control marg">HSN Code</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form hsncode " name="hsncode" value="<?php Echo ($addflag != 1) ? $get["hsncode"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 numeric text-right pad">
                                <label class="label-control marg">GST %</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-6 col-xs-6 pad">
                                <select class="form-control form select2 gstperc" name="gstperc" style="width:100%" >
                                    <option value="<?php echo ($addflag != 1) ? $fetgst['per'] : ""; ?>"><? echo ($addflag != 1) ? $fetgst['name'] : ""; ?></option>
                                    <?php
                                    $ggsttype = mysqli_query($conn, "SELECT * FROM `gsttype` order by per")or die(mysqli_error($conn));
                                    while ($fgsttype = mysqli_fetch_object($ggsttype)) {
                                        Echo "<option value='$fgsttype->per'>$fgsttype->name</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <br>
                        <table><tr>
                                <td style="border-bottom: 1px solid gray; width: 49%">&nbsp;</td>
                                <td style="vertical-align:middle;text-align:center;font-size: 16px" rowspan="2">&nbsp;Stock&nbsp;</td>
                                <td style="border-bottom: 1px solid gray; width: 49%">&nbsp;</td>
                            </tr><tr>
                                <td>&nbsp</td>
                                <td>&nbsp;</td>
                            </tr></table>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 numeric text-right pad">
                                <label class="label-control marg">Opening</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form open " name="open" value="<?php Echo ($addflag != 1) ? $get["open"] : ""; ?>" >
                            </div>
                            <div class="col-lg-3 col-md-1 col-sm-1 col-xs-1 numeric text-right pad">
                                <label class="label-control marg">Minimum</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form ministock " name="minstock" value="<?php Echo ($addflag != 1) ? $get["minstock"] : ""; ?>" >
                            </div>
                        </div>

                    </div>

                </div>
                <!--                <div class="panel-footer">
                <?php include("includes/buttons.php"); ?>
                            </div>-->
            </div>
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="nav-tabs-custom">
                            <ul class="nav nav-pills">
                                <!--<li class="active"><a href="#tab_2" data-toggle="pill" aria-expanded="true">Opening Stock</a></li>-->
                                <li class="active"><a href="#tab_3" data-toggle="pill" aria-expanded="false">Rate History</a></li>
                            </ul>
                            <div class="tab-content">

                                <div class="tab-pane active" id="tab_3">

                                    <div class="secondary">
                                        <table class="table table-striped table-condensed table-bordered">
                                            <thead>
                                                <tr style="background-color: #26969a;">
                                                    <th width="1%"></th>
                                                    <th width="2%">Sr</th>
                                                    <th width="5%">Date</th>
                                                    <th width="5%">Rate</th>
                                                </tr>
                                            </thead>
                                            <tbody class="tbody2">
                                                <?php
                                                if ($addflag != 1) {
                                                    $ctr2 = 0;
                                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `code`='$get[code]' ORDER BY `srno`")or die(mysqli_error($conn));
                                                    while ($tran = mysqli_fetch_array($gettran)) {


                                                        Echo "<tr>";
                                                        Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                                        Echo "<td><input type='text' class='form-control form text-right srno'    name='srno_$ctr2'      value='$tran[srno]'></td>";
                                                        Echo "<td><input type='date'  class='form-control form date'   name='date_$ctr2'     value='$tran[date]'></td>";
                                                        Echo "<td><input type='text'  class='form-control form rate'   name='rate_$ctr2'   value='$tran[rate]'></td>";
                                                        Echo "</tr>";
                                                        $ctr2++;
                                                    }
                                                } else {
                                                    ?>
                                                    <tr>
                                                        <td><i class='fa fa-close remove'></i></td>
                                                        <td><input type='text' class='form-control form text-right srno'    name='srno_0'      value='1'></td>
                                                        <td><input type='date'  class='form-control form  date'    name='date_0'      value=''></td>
                                                        <td><input type='text'  class='form-control form  rate'   name='rate_0'     value=''></td>
                                                    </tr>
                                                    <?
                                                }
                                                ?>
                                            </tbody>
                                        </table>

                                        <button type="button"  class="btn btn-success btn-square form"  id="addnew">
                                            <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                                        </button>
                                        <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr2 : 1; ?>" />
                                    </div>
                                </div>
                                                              
                            </div>
                            
                        </div>
                    </div>
                </div>

                <!-- /.tab-content -->
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-footer">
                            <?php include("includes/buttons.php"); ?>
                        </div>
                    </div>
                </div>
            </div>

            <!--            <div class="panel-footer">
            <?php include("includes/buttons.php"); ?>
                        </div>-->
        </div>

    </form>

    <div class="modal fade bs-example-modal-lg searchModal" 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="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Code</option>
                                <option value="2">Name</option>
                                <option value="3">Rate</option>
                                <option value="4">Unit</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Code</option>
                                <option value="2">Name</option>
                                <option value="3">Rate</option>
                                <option value="4">Unit</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>

                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr" style="background-color:#1ABC9C">
                                    <th width="10%">Code</th>
                                    <th width="30%">Name</th>
                                    <th width="10%">Rate</th>
                                    <th width="10%">Unit</th>
                                    <th width="8%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprev" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>

    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
