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

$formname = 'Change of Rates';
$maindb = 'item';

$append_file = 'open_add.php';
$file = 'openstock.php';
$source = "OP";

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

if (isset($_GET["mode"]) == "true") {
    $addflag = 1;
    $gen1 = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM `$maindb` 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';
    }
    $flag = "Add";
} else {
    $addflag = 0;
    $qvouch = $_GET["vouchno"];
    $flag = "Edit";
}

if (isset($_POST["submit"])) {
    $a = 1;
    $count = $_POST["counter"];
//    browse($count);
    $mvouch = $qvouch;
    $cnt = 0;
    for ($i = 0; $i <= $count; $i++) {
        $cost = $_POST["cost_$i"];
        $selling = $_POST["selling_$i"];
        $msp = $_POST["msp_$i"];
        $mrp = $_POST["mrp_$i"];
        $opening = $_POST["opening_$i"];
        $code = $_POST["code_$i"];

        mysqli_query($conn, "UPDATE `$maindb` SET  `cost`='$cost', `rate`='$selling',`msp`='$msp',`mrp`='$mrp',`opening`='$opening' WHERE `code`='$code'") or die(mysqli_error($conn));
    }

    updmess();
    header("Refresh: 1; openstock.php");
}
include("includes/linkshead.php");
?>
</head>

<body>
    <form action=''  name="openstock" method="POST">
        <div class="row">
            <div class="col-lg-6" style='width:100%;float:left;'>
                <div class="box dark">
                    <header>
                        <div class="toolbar" style="float:right;">
                            <ul class="nav">
                                <input type='submit' id="save" name='submit' value='<?php echo ($addflag != 1) ? "Save" : "Save"; ?>' class="btn btn-primary" >
                            </ul>     
                        </div>
                    </header>
                    <div class="row">
                        <div class="col-lg-2">
                            <select id="cmbitemgrp" name="cmbitemgrp" style="height:35px;"  class="itemgrp form-control">
                                <option></option>
                                <?php
                                $mown = mysqli_query($conn, "SELECT code, `name` FROM `itemgrp` ") or die(mysqli_error($conn));
                                while ($trow = mysqli_fetch_array($mown)) {
                                    echo "<option value=$trow[code]>$trow[name]</option>";
                                }
                                ?>

                            </select>
                            <input type="text" id="custhidden" hidden>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-2">
                            <select id="cmbsubgrp" name="cmbsubgrp" style="height:35px;"  class="subgrp form-control">
                                <option></option>
                                <?php
                                $mown1 = mysqli_query($conn, "SELECT code, `name` FROM `itemsubgrp` ") or die(mysqli_error($conn));
                                while ($trow1 = mysqli_fetch_array($mown1)) {
                                    echo "<option value=$trow1[code]>$trow1[name]</option>";
                                }
                                ?>

                            </select>
                            <input type="text" id="custhidden" hidden>
                        </div>
                    </div>
                    <!--                        <div class='row table-responsive' style="overflow-y:scroll; height: 400px">
                                                <table id="data" align="left" border="1px" width="50%" class="table table-bordered table-sortable">
                                                    <tr>
                                                        <td width="5%"  align="center"><label class="control-label"><b>Sr.</b></label></td>
                                                        <td width="10%" align="center"><label class="control-label"><b>Barcode</b></label></td>
                                                        <td width="35%" align="center"><label class="control-label"><b>Name</b></label></td>
                                                        <td width="10%" align="center"><label class="control-label"><b>Cost</b></label></td>
                                                        <td width="10%" align="center"><label class="control-label"><b>Rate</b></label></td>
                                                        <td width="10%" align="center"><label class="control-label"><b>MSP</b></label></td>
                                                        <td width="10%" align="center"><label class="control-label"><b>MRP</b></label></td>
                                                        <td width="10%" align="center"><label class="control-label"><b>Opening</b></label></td>
                                                    </tr>
                    <?php
                    $ctr = 0;
                    $mctr = 0;
                    $getdata = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`!='' ORDER BY `name`")or die(mysqli_error($conn));
                    while ($row = mysqli_fetch_object($getdata)) {
                        $ctr++;
                        echo "<tr>";
                        echo "<td align='center'>";
                        echo $ctr;
                        echo "</td>";
                        echo "<td>";
                        echo "<input type='text' class='form-control' name='code_$mctr' value='$row->code' style='text-align:left;' readonly>";
                        echo "</td>";
                        echo "<td  align='center'>";
                        echo "<input type='text' class='form-control' name='name_$mctr' value='$row->name' style='text-align:left;' readonly>";
                        echo "</td>";
                        echo "<td>";
                        echo "<input type='text' class='form-control' name='cost_$mctr' value='$row->cost' style='text-align:right;'>";
                        echo "</td>";
                        echo "<td>";
                        echo "<input type='text' class='form-control' name='selling_$mctr' value='$row->rate' style='text-align:right;'>";
                        echo "</td>";
                        echo "<td>";
                        echo "<input type='text' class='form-control' name='msp_$mctr' value='$row->msp' style='text-align:right;'>";
                        echo "</td>";
                        echo "<td>";
                        echo "<input type='text' class='form-control' name='mrp_$mctr' value='$row->mrp' style='text-align:right;'>";
                        echo "</td>";
                        echo "<td>";
                        echo "<input type='text' class='form-control' name='opening_$mctr' value='$row->opening' style='text-align:right;'>";
                        echo "</td>";
                        echo "</tr>";
                        $mctr++;
                    }
                    ?>
                                                </table>   
                                                <input type="hidden" name="counter" value="<?php echo $ctr; ?>">
                                            </div>-->
                    <!--</div>-->

                    <!--</div>-->
                </div>
            </div>
        </div>        
    </div>        

</form>



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


</body>
</html>
