<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
$formname = 'Product Stock';
$maindb = 'transacn';
$trandbexist = 0;
$grid = '';
$keyfld = "srno";
$focusfield = "name";
$file = 'openstk_prod.php';


$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdbroot"];
$myyear = substr($_SESSION["zdbyear_" . $dbname], -4);

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

if (isset($_POST["submit"])) {
    $a = 1;
    $godown = $_POST["godown"];
    $series = $_POST["series"];
    $count = $_POST["items"];


    $mvouch = $qvouch;


    if ($a == 1) {

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

        mysqli_query($conn, "DELETE FROM `$maindb` WHERE `godown`='$godown' and `series`='$series' and `source`='OP'") or die(mysqli_error($conn));
        $ctn = 0;
        for ($i = 0; $i < $count; $i++) {

            $msrno = $_POST["srno_$i"];
            $mbarcode = $_POST["barcode_$i"];
            $mitemcode = $_POST["itemcode_$i"];
            $mitemname = $_POST["itemname_$i"];
            $gram = $_POST["gram_$i"];
            $unit = $_POST["unit_$i"];
            $batch = $_POST["batch_$i"];
            $qty = $_POST["qty_$i"];



            $itemcode = mysqli_query($conn, "SELECT code FROM `product` WHERE barcode='$mbarcode'") or die(mysqli_error($conn));
            $icode = mysqli_fetch_array($itemcode);
            $itcode = $icode[code];
            $mgroup = 'OP' . $itcode;
//            browse($mitem);
            if (!empty($mbarcode)) {
                $ctn++;
                mysqli_query($conn, "INSERT INTO `$maindb`(`srno`,`godown`, `series`, `itemcode`, `barcode`, `gram`, `unit`, `yearno`,
                            `qty`, `source`, `batch`,`group`)
                            VALUES('$ctn','$godown', '$series', '$mitemcode', '$mbarcode', '$gram', '$unit', '$myyear',
                                   '$qty', 'OP', '$batch','$mgroup')") or die(mysqli_error($conn));
            }
        }

//############################## MAIN  ######################################################
        if ($addflag == 1) {
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?mode=last");
        } else {
            $mvouch = $qvouch; //1
            updmess();
            header("refresh: 1; $file?mode=last");
        }
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }
    $get = mysqli_query($conn, "SELECT * FROM `$maindb`") or die(mysqli_error($conn));
}

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/openstkprod.js" type="text/javascript"></script>
<script src="MyScript/search_item.js?A=<?= mt_rand(); ?>" 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>-->
    <!--<span style="display: inline-block;width: 85px;padding: 10px;background: #037D95;color: white;text-align: center;position: absolute;top: 50%;left: 36%;z-index: 99;" class="raj-ajax-loader">Loading...</span>-->
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color :#037D95;"><?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-2 pad text-right">
                                <label style='color:#037D95;font-size: 20px;' class="label-control marg">Godown</label>
                            </div>
                            <div class="col-lg-2" pad>
                                <select class="form-control godown form select2" autofocus name="godown" required style="height: 32px;">
                                    <option></option>
                                    <?php
                                    $getseries = mysqli_query($conn, "SELECT * FROM `godown` order by code")or die(mysqli_error($conn));
                                    while ($fetseries = mysqli_fetch_array($getseries)) {
                                        Echo "<option value='$fetseries[code]'>$fetseries[gcode]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-2 pad text-right">
                                <label style='color:#037D95;font-size: 20px;' class="label-control marg">Series</label>
                            </div>
                            <div class="col-lg-2" pad>
                                <select class="form-control series form select2" name="series" required style="height: 32px;">
                                    <option></option>
                                    <option value="LO">Local</option>
                                    <option value="DE">Deemed</option>
                                    <option value="EX">Export</option>
                                </select>
                            </div>
                        </div>
                        <table class="table-striped table-condensed table-bordered tab"  width="100%">
                            <thead  class="mySearchTr">
                                <tr>
                                    <th style="width: 1%"></th>
                                    <th style="width: 2%">Sr.</th>
                                    <th style="width: 12%;">Barcode</th>
                                    <th style="width: 8%;">Item code</th> 
                                    <th style="width: 12%;">Item Name</th> 
                                    <th style='width: 6%'>Gram</th>
                                    <th style='width: 5%'>Unit</th>
                                    <th style='width: 5%'>Batch</th>
                                    <th style='width: 3%' class="text-right">Open Qty.</th>

                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <tr>                                    
                                    <td><i class='fa fa-close remove'></i></td>
                                    <td><input type='text' name='srno_0' class='srno form-control form' value='1' readonly></td>
                                    <td><input type='text' name='barcode_0' class='barcode  form-control form' value='' required></td> 
                                    <td>
                                        <input type='text' name='itemcode_0' required='true' class='itemcode form-control  form' value='' readonly required>
                                    </td>
                                    <td>
                                        <input type='text' name='itemname_0' required='true' class='itemname form-control  form' value='' readonly required>
                                    </td>

                                    <td><input type='text' name='gram_0' class='gram form-control  form' value='' readonly></td>
                                    <td><input type='text' name='unit_0' class='unit form-control  form' value='' readonly></td>
                                    <td><input type='text' name='batch_0' class='batch form-control   form' value='' ></td>
                                    <td><input type="number" step="any" name='qty_0' class='qty form-control text-right form ' value='0' ></td> 
                                </tr>
                            </tbody>
                        </table>
                        <div class="col-lg-12 pad">
                            <button type="button" class="btn btn-primary btn-square form"  id="addnew">
                                <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items" name="items" value="<?php Echo ($addflag != 1) ? $ctr : 1; ?>">  
                            <!--<input type="hidden" class="mandatoryId" name="manId" value="">-->  
                        </div>
                    </div>
                    <div class="panel-footer">
                        <div style="overflow: auto; ">
                            <table class="table-condensed" style="margin: 0 auto;">
                                <tr>
                                    <td class=" buttonform tooltip-bottom" id="hedit" style="display: 
                                        <? Echo $editdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Edit Record">
                                        <button id='edit' type="button" style="background-color:#F0AD4E; " class="btn btn-pad btn-square">
                                            <i class="fa fa-pencil" style="color:#ffffff"></i>
                                        </button>
                                    </td>
                                    <td class=" buttonform tooltip-bottom" id="hexit" style="display: <? Echo $exitdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Exit">
                                        <button id='exit' type="button" class="btn btn-pad btn-square " style="background-color:silver; border-color: #CA195A; "  onClick="window.location.href = '<?= 'default.php' ?>'">
                                            <i class="fa fa-power-off" style="color:#CA195A"></i>
                                        </button>
                                    </td>
                                    <td class=" buttonform tooltip-bottom" id="ssubmit" style="display: 
                                        <? Echo $savedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Save Record">
                                        <button id='submit' type='submit' name='submit' value='<?php echo ($addflag == 0) ? "Update" : "Save"; ?>' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                                            <i class="fa fa-check" style="color:#ffffff"></i>
                                        </button>
                                    </td>
                                    <td class=" buttonform tooltip-bottom" id="scancel" style="display: 
                                        <? Echo $canceldisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Cancel Record">
                                        <button id='cancel' type="button" class="btn btn-pad btn-square " style="background-color:#39CCCC; "  onClick="window.location.href = '<?= $file . "?mode=last&series=$zseries" ?>'">
                                            <i class="fa fa-times" style="color:#ffffff"></i>
                                        </button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade itemModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content" style="margin-top: 0px !important;">
                <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">
                    <!--<input type="hidden" value="<?php // Echo $fseries    ?>" class="form-control series">-->
                    <input type="hidden" value="<?php Echo $get[vouchno]; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php Echo $para ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-3">
                            <select class="form-control ifield">
                                <option value="1">Name</option>
                                <option value="2">Barcode</option>
                                <option value="3">Itemcode</option>
                                <option value="4">Gram</option>
                                <option value="5">Unit</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control ioperator">
                                <option value=""></option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" value="" class="form-control isearch">
                        </div>
                        <div class="col-lg-3">
                            <input type="button" class="btn btn-primary iload" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 iloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 inoRecord 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">
                                    <th width="20%">Name</th>
                                    <th width="20%">Barcode</th>
                                    <th width="15%">Itemcode</th>
                                    <th width="15%">Gram</th>
                                    <th width="10%">Unit</th>
                                    <th width="10%">Rate</th>
                                </tr>
                            </thead>
                            <tbody class="iTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary iprevious" value="Previous">
                            <input type="button"  class="btn btn-primary inext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
