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

$formname = 'Opening Stock';
$maindb = 'transacn';
$trandb = 'item';
$file = 'openstock.php';
$source = "OP";

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

if (isset($_GET["mode"]) == "true") {
    $addflag = 1;
} else {
    $addflag = 0;
}

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


    $godown = $_POST["cmbgodown"];
    $count = $_POST["count"];
    for ($i = 0; $i < $count; $i++) {

        $mitem = $_POST["item_$i"];
        $mcost = $_POST["cost_$i"];
        $mrate = $_POST["rate_$i"];
        $mrp = $_POST["mrp_$i"];
        $mqty = $_POST["qty_$i"];
 

        $geti = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`='$mitem' ") or die(mysqli_error($conn));
        $iget = mysqli_fetch_assoc($geti);
        $mbarcode = $iget['barcode'];
        $mitemcode = $iget['code'];
        $grp = $iget['group'];
        
        $gettran = mysqli_query($conn, "SELECT * FROM `transacn` WHERE `itemcode`='$mitemcode' and godown = $godown and source = 'OP'") or die(mysqli_error($conn));
        $itran = mysqli_fetch_assoc($gettran);
        $tqty = $itran[qty];
        $cnt = $cnt + 1;

        ################################ CHECK EXISTING ITEMS #################################################
        mysqli_query($conn, "DELETE FROM `$maindb` WHERE `itemcode` = '$mitemcode' AND `source` = '$source' and godown='$godown'") OR die(mysqli_error($conn));
        
        mysqli_query($conn, "UPDATE `item` SET `mrp`='$mrp', `rate`='$mrate', `cost`='$mcost'  WHERE `code`='$mitemcode'")or die(mysqli_error($conn));
//        if ($tqty == 0){
        if ($mqty > 0){
            mysqli_query($conn, "INSERT INTO `$maindb`(`source`, `srno`, `barcode`, `itemcode`, `godown`, 
                                `itemgrp`,  qty,  mrp, cost, rate, amount)
                        VALUES('$source', '$cnt', '$mbarcode', '$mitemcode', '$godown', 
                                '$grp', '$mqty', '$mrp', '$mcost', '$mrate', '$mamt')") or die(mysqli_error($conn));
        }
//        
//        else{
//            mysqli_query($conn, "UPDATE `$maindb` SET `cost`='$cost', `rate`='$rate', `mrp`='$mrp', `trans`='$trans' where $keyfld = '$mvouch' ") or die(mysqli_error($conn));
//            updmess();
//        }
        
    }
//    insmess();
        header("refresh: 1; $file");
}
include('includes/files.php');
include('includes/combo2.php');
?>
<script>
    $(document).ready(function () {

        $("#cmbgodown").change(function () {
            var cmbgodown = $('#cmbgodown').val();

            $.ajax({
                type: "GET",
                url: "opnstksrch.php",
                data: {code: cmbgodown},
                success: function (result) {
                    $("#data").html(result);
                }
            });
        });
        $('#submit').click(function () {

            $('#bill').submit();
        });

    });


    $(document).keydown(function (event) {
        //19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 's' && event.ctrlKey) && !(event.which == 19))
            return true;
        //alert("Ctrl-s pressed");
        $("#submit").click();
        event.preventDefault();
        return false;
    });

</script>
</head>
<body>
    <!--BEGIN INPUT TEXT FIELDS-->
    <form action=''  name="openstock" method="POST">

        <div class="row">
            <div class="col-lg-12" style='width:100%;float:left;'>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5><?= $formname ?></h5>
                        <!-- .toolbar -->
                        <div class="toolbar">
                            <ul class="nav">
                                <!--  <li><a href="#">Link</a></li>
                                  <li class="dropdown">
                                      <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                                          <i class="fa fa-th-large"></i>
                                      </a>
                                      <ul class="dropdown-menu">
                                          <li><a href="">q</a></li>
                                          <li><a href="">a</a></li>
                                          <li><a href="">b</a></li>
                                      </ul>
                                  </li>-->
                                <li><input id="submit" type='submit' name='submit' id='save' value='Save' class="btn btn-primary"></li>
                                <li><input class="btn btn-danger" type="button" onClick="window.location.href = '<?= $grid ?>'" value="Exit" /></li>
                                <li>
                                    <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-1">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>

                            </ul>
                        </div>
                        <!-- /.toolbar -->
                    </header>
                    <div id="div-1" class="accordion-body collapse in body">
                        <div class="form-horizontal">

                            <div class="form-group">
                                <label class="control-label col-lg-1">Godown</label>

                                <div class="col-lg-2">
                                    <select id="cmbgodown" name="cmbgodown" style="height:35px;"  class="godown form-control">
                                        <option></option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT code, `gdcode` FROM `godown` ") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mown)) {
                                            echo "<option value=$trow[code]>$trow[gdcode]</option>";
                                        }
                                        ?>

                                    </select>
                                    <input type="text" id="custhidden" hidden>
                                </div>
                            </div>
                            <!--<h4><b>Item List</b></h4>-->
                            <br/>
                        </div>
                    <!--</div>-->
<!--                </div>
            </div>-->
<!--            <div class="col-lg-6" style='width:100%;float:left;'>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5>Item List</h5>
                         .toolbar 
                        <div class="toolbar">
                            <ul class="nav">


                                <li>
                                    <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-2">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>
                            </ul>
                        </div>
                         /.toolbar 
                    </header>-->
                    <!--<div id="div-2" class="accordion-body collapse in body">-->
                        <div style="overflow: auto; border: 0px solid;">
                            <div style="float:left;">
                                <div id='data'></div>                  
                            </div> 
                        </div> 

                    </div>
                </div>        
            </div>    
        </div>
    </form>

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