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

$formname = 'Item';
$maindb = 'item';
$trandb = 'itemsub';
$stkdb = 'transacn';

$printfile = "itemprint.php";
$file = 'item.php';
$trandbexist = 0;

$keyfld = "barcode";
$keycond = "";
$mgrpcode = "OP";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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

    $a = 1;
    $qvouch = $_POST["barcode"];
    $code = $_POST["code"];
    $quality = $_POST["quality"];
    $shade = $_POST["shade"];
    $yarn = $_POST["yarn"];
    $name = $_POST["name"];
    $type = $_POST["type"];
    $unit = $_POST["unit"];
    $brand = $_POST["brand"];
    $salemode = $_POST["salemode"];
    $cost = $_POST["cost"];
    $selling = $_POST["selling"];
    $hsncode = $_POST["hsncode"];
    $gstperc = $_POST["gstperc"];
    $ofs = $_POST["ofs"];
    $count = $_POST["items"];

    $mvouch = $qvouch;

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `brand`= '$brand' AND code = '$code' and barcode != '$mvouch' ") or die(mysqli_error($conn));
    if (mysqli_num_rows($result) > 0) {
        $a = 0;
        browse("Same Code Already Exist For Same Brand");
        header("refresh: 1; $file?$keyfld=$qvouch&mode=last");
    }

    if ($a == 1) {

#################################TRAN PART #################################
//        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `code`='$mvouch'") or die(mysqli_error($conn));
#################################STOCK PART #################################
//        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `vouchno`='$mvouch' AND `source`='$mgrpcode'")or die(mysqli_error($conn));
//        for ($i = 0; $i < $count; $i++) {
//            $msrno = $_POST["srno_$i"];
//            $mgodown = $_POST["godown_$i"];
//            $mtaka = $_POST["taka_$i"];
//            $mbatch = $_POST["batch_$i"];
//            $mqty = $_POST["qty_$i"];
//            $mtotqty = $_POST["totqty_$i"];
//            $mflag = $_POST["flag_$i"];
//            $mflagqty = $_POST["flagqty_$i"];
//            $mactqty = $_POST["actqty_$i"];
//
//            $yearno = $myyear;
//            $group = $mgrpcode . $mvouch;
//            $vouchdt = date("Y-m-d");
//            $ord = "0";
//            $actbatch = $mbatch;
//
//            if (!empty($mgodown)) {
//                mysqli_query($conn, "INSERT INTO `$trandb`(`code`,`srno`, `godown`, `taka`, `batch`,`totqty`,`qty`, `flag`, `flagqty`,`actqty`)
//                            VALUES('$mvouch', '$msrno','$mgodown','$mtaka', '$mbatch','$mtotqty','$mqty', '$mflag', '$mflagqty','$mactqty')") or die(mysqli_error($conn));
//
//                mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, `group`, `source`, `type`,`vouchno`,`srno`, `brand`, `itemcode`,`itemname`,
//                            `batch`, `taka`, `qty`, `totqty`, `flag`, `flagqty`, `actqty`, `godown`, `actbatch`,`ord`,`salemode`, `shade`)
//                            VALUES('$yearno','$group','$mgrpcode','$type','$mvouch', '$msrno','$brand', '$mvouch', '$name', '$mbatch', '$mtaka',
//                            '$mqty', '$mtotqty', '$mflag', '$mflagqty','$mactqty', '$mgodown', '$actbatch','$ord','$salemode', '$shade')") or die(mysqli_error($conn));
//            }
//        }
############################## MAIN  ######################################################

        if ($addflag == 1) {


            mysqli_query($conn, "INSERT INTO `$maindb`(code, `name`, `unit`, `brand`, `salemode`, `cost`, `selling`,barcode)
                                        VALUES('$code', '$name', '$unit', '$brand', 'T', '$cost', '$selling','$mvouch')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `code`='$code', `name`='$name', `unit`='$unit',`brand`='$brand',
                                                      `salemode`='T', `cost`='$cost', `selling`='$selling'  WHERE barcode = '$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;
    }

    $result1 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result1);

    $findbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$get[brand]'") or die(mysqli_error($conn));
    $fbrand = mysqli_fetch_array($findbrand);
}

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

if (isset($_GET["delete"])) {
    $chk = mysqli_query($conn, "SELECT itemcode FROM `transacn` WHERE `itemcode`='$_GET[$keyfld]'") or die(mysqli_error($conn));
    $mno = 0;
    if (!empty($chk)) {
        $mno = mysqli_num_rows($chk);
    }
    if ($mno > 0) {
        browse("Cannot Delete Used in Transaction");
        header("refresh: 1; $file?$keyfld=$_GET[$keyfld]&mode=edit");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    }
//    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    delnew($zdb, "$stkdb", " vouchno='$getvouch' AND source='$mgrpcode' ", "$file?mode=last&series=$zseries");
}

include("includes/linkshead.php");
?>
<script src="MyScript/item.js" type="text/javascript"></script>
<script src="MyScript/item_search.js" type="text/javascript"></script>
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-5 col-md-12 col-sm-12 col-xs-12  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 numeric pad">
                                <label class="label-control marg">Code</label>
                            </div>
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control focusfield form" name="code" value="<?php Echo ($addflag != 1) ? $get["code"] : ""; ?>" required>
                                <input type="hidden" class="form-control " name="barcode" value="<?php Echo ($addflag != 1) ? $get["barcode"] : "$qvouch"; ?>" required>
                            </div>
                            <!--                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                                                            <label class="label-control marg">Order for Stock</label>
                                                        </div>
                                                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                                            <input type="text" class="form-control form text-right" name="ofs" value="<?php Echo ($addflag != 1) ? $get["ofs"] : ""; ?>" >
                                                        </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">Quality</label>
                                                    </div>
                                                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                                        <input type="text" class="form-control form  quality"  name="quality" value="<?php Echo ($addflag != 1) ? $get["quality"] : ""; ?>" required>
                                                    </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">Shade</label>
                                                    </div>
                                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                                        <input type="text" class="form-control form shade" name="shade" value="<?php Echo ($addflag != 1) ? $get["shade"] : ""; ?>" required>
                                                    </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">Yarn</label>
                                                    </div>
                                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                                        <input type="text" class="form-control form yarn" name="yarn" onkeypress="return blockSpecialChar(event)"  value="<?php Echo ($addflag != 1) ? $get["yarn"] : ""; ?>" required>
                                                    </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">Name</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form item" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </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-3 col-sm-3 col-xs-3 pad">
                                                        <select class="form-control form select2" name="type" style='width:100%'>
                                                            <option value="<?php echo ($addflag != 1) ? $ftype['code'] : ""; ?>"><? echo ($addflag != 1) ? $ftype['name'] : ""; ?></option>
                        <?php
                        $gtype = mysqli_query($conn, "SELECT * FROM `type`")or die(mysqli_error($conn));
                        while ($type = mysqli_fetch_object($gtype)) {
                            ?>
                                                                                            <option value="<? Echo $type->code; ?>"><? Echo $type->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-3 col-md-3 col-sm-3 col-xs-3 pad">
                                                        <input type="text" class="form-control form" name="unit" value="<?php Echo ($addflag != 1) ? $get["unit"] : ""; ?>" required>
                                                    </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">Brand / Logo</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <select class="form-control brand form select2" name="brand" style="width:100%">
                                    <option value="<?php echo ($addflag != 1) ? $fbrand['code'] : ""; ?>"><? echo ($addflag != 1) ? $fbrand['name'] : ""; ?></option>
                                    <?php
                                    $gbrand = mysqli_query($conn, "SELECT * FROM `brand` order by name")or die(mysqli_error($conn));
                                    while ($brand = mysqli_fetch_object($gbrand)) {
                                        Echo "<option value='$brand->code'>$brand->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" style="color: #1ABC9C">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"] : ""; ?>" readonly> 
                                                    </div>
                                                    <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                                        <label class="label-control marg" style="color: #1ABC9C">GST &nbsp; %</label>
                                                    </div>
                                                    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-3 pad">
                                                        <input type="text" step="any" class="form-control form gstperc" name="gstperc" value="<?php Echo ($addflag != 1) ? $get["gstperc"] : ""; ?>" readonly> 
                                                    </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">Sale Mode</label>
                                                    </div>
                                                    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                                        <select class="form-control form select2" name="salemode" style="width:100%">
                        <?php
                        if ($addflag != 1) {
                            if ($get['salemode'] == "T") {
                                Echo "<option value='T'>TAKA</option>";
                            }
                        } else {
                            Echo "<option value='T'>TAKA</option>";
                            Echo "<option value=''></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">Cost</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" class="form-control form cost numeric" name="cost" value="<? Echo ($addflag != 1 ) ? $get['cost'] : ""; ?>">
                            </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">Selling</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" class="form-control form selling numeric" name="selling" value="<? Echo ($addflag != 1 ) ? $get['selling'] : ""; ?>">
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </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">Unit</option>-->
                                <option value="4">Brand</option>
                                <!--                                <option value="5">Cost</option>                                
                                                                <option value="6">Selling</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">Unit</option>-->
                                <option value="4">Brand</option>
                                <!--                                <option value="5">Cost</option>                                
                                                                <option value="6">Selling</option>                                -->

                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="asc">Asc</option>
                                <option value="desc">Desc</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 style="background-color:#1abc9c;">
<!--                                    <th width="10%">Code</th>-->
                                    <th width="10%">Code</th>
                                    <th width="20%">Name</th>
                                    <!--<th width="5%">Unit</th>-->
                                    <th width="10%">Brand</th>
                                    <th width="10%">Cost</th>
                                    <th width="10%">Selling</th>
                                    <th width="5%">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>
