<?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];
mysql_select_db($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"];
        
        $gettran = mysql_query("SELECT * FROM `transacn` WHERE `itemcode`='$mitem' ") or die(mysql_error());
        $iget = mysql_fetch_assoc($geti);

        $geti = mysql_query("SELECT * FROM `item` WHERE `name`='$mitem' ") or die(mysql_error());
        $iget = mysql_fetch_assoc($geti);
        $mbarcode = $iget['barcode'];
        $mitemcode = $iget['code'];
        $grp = $iget['group'];
        
        $gettran = mysql_query("SELECT * FROM `transacn` WHERE `itemcode`='$mitemcode' and godown = $godown") or die(mysql_error());
        $itran = mysql_fetch_assoc($gettran);
        $tqty = $itran[qty];
        $cnt = $cnt + 1;

        ################################ CHECK EXISTING ITEMS #################################################

        mysql_query("UPDATE `item` SET `mrp`='$mrp', `rate`='$mcost' WHERE `code`='$mitemcode'")or die(mysql_error());
        if ($tqty == 0){
            mysql_query("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(mysql_error());
        }else{
            
        }
        
    }
    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 = mysql_query("SELECT code, `gdcode` FROM `godown` ") or die(mysql_error());
                                        while ($trow = mysql_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>
