<?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    = 1;
$keyfld         = "code";
$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;
    $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"];
    $count      = $_POST["items"];


    $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?$keyfld=$qvouch&mode=last");
        }
    }

    if ($a == 1) {

        #################################TRAN PART #################################

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `code`='$mvouch'") 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"];
            
            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));
            }
        }

         #################################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   = $mgrpcode . $mvouch . $mbatch;
            
                mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, `group`, `source`, `type`,`vouchno`,`vouchdt`, `srno`, `brand`, `itemcode`,`itemname`,
                            `batch`, `taka`, `qty`, `totqty`, `flag`, `flagqty`, `actqty`, `godown`, `actbatch`,`ord`,`salemode`, `shade`)
                            VALUES('$yearno','$group','$mgrpcode','$type','$mvouch','$vouchdt', '$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, `quality`, `shade`, `yarn`, `name`, `type`, `unit`, `brand`, `salemode`, `cost`, `selling`)
                                        VALUES($qvouch, '$quality', '$shade', '$yarn', '$name', '$type', '$unit', '$brand', '$salemode', '$cost', '$selling')") 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 `quality`='$quality', `shade`='$shade',`yarn`='$yarn', `name`='$name', `type`='$type', `unit`='$unit',
                         `brand`='$brand', `salemode`='$salemode', `cost`='$cost', `selling`='$selling' WHERE code = '$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);

    $findtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$get[type]'") or die(mysqli_error($conn));
    $ftype = mysqli_fetch_array($findtype);
    
    $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"])) {
    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>
</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-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form" name="code" value="<?php Echo ($addflag != 1) ? $get["code"] : " $qvouch"; ?>" required 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">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 focusfield 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 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">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</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <select class="form-control 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`")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 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] == "P") {
                                            Echo "<option value='P'>PCS</option>";
                                            Echo "<option value='T'>TAKA</option>";
                                        } elseif($get[salemode] == "T") {
                                            Echo "<option value='T'>TAKA</option>";
                                            Echo "<option value='P'>PCS</option>";
                                        }
                                    }else{
                                        Echo "<option value=''></option>";
                                        Echo "<option value='P'>PCS</option>";
                                        Echo "<option value='T'>TAKA</option>";
                                    }
                                    ?>
<!--                                    <option value="P">PCS</option>
                                    <option value="T">TAKA</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 class="col-lg-7 col-md-12 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">Item Details</h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered" width="100%">
                                <thead style="background: #1ABC9C;">
                                    <tr>
                                        <th width="3%"></th>
                                        <th width="7%" class='numeric'>Sr.</th>
                                        <th width="10%">Godown</th>
                                        <th width="10%" class='numeric'>Taka</th>
                                        <th width="10%" class='numeric'>Batch</th>
                                        <th width="10%" class='numeric'>Qty.</th>
                                        <th width="10%" class='numeric'>Tot. Qty.</th>
                                        <th width="10%" class='numeric'>Flag</th>
                                        <th width="10%" class='numeric'>Flag Qty.</th>
                                        <th width="10%" class='numeric'>Act Qty.</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 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 numeric srno' name='srno_$ctr' value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form godown detailpart_$ctr' name='godown_$ctr' required>";
                                            Echo "<option value='$tran[godown]'>$tran[godown]</option>";
                                            $ggodown = mysqli_query($conn, 'SELECT * FROM `godown`')or die(mysqli_error($conn));
                                            while ($godown = mysqli_fetch_array($ggodown)) {
                                                Echo "<option value='$godown[gocode]'>$godown[gocode]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' class='form-control form numeric taka'      name='taka_$ctr'        value='$tran[taka]'></td>";
                                            Echo "<td><input type='text' class='form-control form numeric batch'     name='batch_$ctr'       value='$tran[batch]'></td>";
                                            Echo "<td><input type='text' class='form-control form numeric qty'       name='qty_$ctr'         value='$tran[qty]'></td>";
                                            Echo "<td><input type='text' class='form-control form numeric totqty'    name='totqty_$ctr'      value='$tran[totqty]'></td>";
                                            Echo "<td><input type='text' class='form-control form numeric flag'      name='flag_$ctr'        value='$tran[flag]'></td>";
                                            Echo "<td><input type='text' class='form-control form numeric flagqty'   name='flagqty_$ctr'     value='$tran[flagqty]'></td>";
                                            Echo "<td><input type='text' class='form-control form numeric actqty'    name='actqty_$ctr'      value='$tran[actqty]'></td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td width="3%"><i class="fa fa-close remove"></i></td>
                                            <td width="4%"><input type="text" class="form-control form numeric srno" name="srno_0" value="1"></td>
                                            <td width="10%">
                                                <select class="form-control form godown select2" name="godown_0" style='width:100%'  required>
                                                    <option value=""></option>
                                                    <?php
                                                    $ggodown = mysqli_query($conn, "SELECT * FROM `godown`")or die(mysqli_error($conn));
                                                    while ($godown = mysqli_fetch_array($ggodown)) {
                                                        Echo "<option value='$godown[gocode]'>$godown[gocode]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td width="10%"><input type="text" class="form-control form numeric taka"    name="taka_0"       value="0"></td>
                                            <td width="10%"><input type="text" class="form-control form numeric batch"   name="batch_0"      value="0"></td>
                                            <td width="12%"><input type="text" class="form-control form numeric qty"     name="qty_0"        value="0"></td>
                                            <td width="12%"><input type="text" class="form-control form numeric totqty"  name="totqty_0"     value="0"></td>
                                            <td width="12%"><input type="text" class="form-control form numeric flag"    name="flag_0"       value="0"></td>
                                            <td width="12%"><input type="text" class="form-control form numeric flagqty" name="flagqty_0"    value="0"></td>
                                            <td width="15%"><input type="text" class="form-control form numeric actqty"  name="actqty_0"     value="0"></td>
                                        </tr>  
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad">
<!--                            <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />-->
                            <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) ? $ctr : 1; ?>" />  
                        </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">
                            <thead>
                                <tr style="background-color: #1ABC9C;">
                                    <th>Code</th>
                                    <th>Name</th>
                                    <th>Type</th>
                                    <th>Unit</th>
                                    <th>Yarn</th>
                                    <th>Shade</th>
                                    <th>Quality</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT `code`,`name`, `type`, `unit`,`yarn`,`shade`,quality FROM `$maindb` ORDER BY `code`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='center'>$row[code]</td>";
                                        echo "<td align='left'>$row[name]</td>";
                                        echo "<td align='left'>$row[type]</td>";
                                        echo "<td align='left'>$row[unit]</td>";
                                        echo "<td align='left'>$row[yarn]</td>";
                                        echo "<td align='left'>$row[shade]</td>";
                                        echo "<td align='left'>$row[quality]</td>";
                                        echo "<td align='left'><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>
