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

$formname = 'Opening Stock';
$maindb = 'transacn';
$printfile = "openstk_print_fr.php";
$trandbexist = 0;
$grid = '';
$keyfld = "id";
$focusfield = "name";
$usertype = $_SESSION["usertype_" . $dbname];
$source = 'OP';
browse($usertype);
$zseries = $_GET["series"];
IF ($zseries == '') {
    $zseries = 'BBR';
}
$file = 'openstk.php';


$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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

    if ($a == 1) {

        #################################TRAN PART #################################
//        mysqli_query($conn, "DELETE FROM `$maindb` WHERE `series`='$zseries'") or die(mysqli_error($conn));
        $ctn = 1;
        for ($i = 0; $i <= $count; $i++) {

            $msrno = $_POST["srno_$i"];
            $mitem = $_POST["item_$i"];
            $micode = $_POST["itemcode_$i"];
            $miname = $_POST["itemname_$i"];
            $msize = $_POST["size_$i"];
            $munit = $_POST["unit_$i"];
//            $mcomp = $_POST["comp_$i"];
            $mactsize = $_POST["actsize_$i"];
            $mopening = $_POST["opening_$i"];
            $mminimum = $_POST["minimum_$i"];
            
            if($usertype == 1){
                $zseries = $series;
            }
            

            if (!empty($miname)) {
//                checking already exist
                $getmyitem = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `itemname`='$miname' and `size`='$msize' and `series`='$zseries'") or die(mysqli_error($conn));
                $openrows = mysqli_num_rows($getmyitem);

                if ($openrows > 0) {
//                    browse($miname . "  with size  " . $msize . "  Already Exist");
                    mysqli_query($conn, "UPDATE `$maindb` SET `opstk`='$mopening', `minstk`='$mminimum'
                                WHERE `itemname` = '$miname' and `size`='$msize'  AND `series` = '$zseries' ") or die(mysqli_error($conn));
                } else {

                    mysqli_query($conn, "INSERT INTO `$maindb`(`id`,`srno`, `itemcode`, `itemname`, `size`, `actsize`, `unit`, `opstk`, `minstk`, `series`)
                            VALUES('', '$ctn', '$micode', '$miname', '$msize', '$mactsize', '$munit', '$mopening', '$mminimum', '$zseries')") or die(mysqli_error($conn));
                }
                $ctn++;
            }
        }

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

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }
}

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
//$get = mysqli_query($conn, "SELECT `itemcode`,`itemname`,`size`,`srno`,`actsize`,`unit`,`opstk`,`minstk` FROM `$maindb` WHERE `series`='$zseries'
//                   order by itemname,size") or die(mysqli_error($conn));
include("includes/linkshead.php");
?>
<script src="MyScript/openstk.js?a=1" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">

            <div class="col-lg-10">
                <div class="panel panel-default">
                    <div class="panel-heading">

                        <h3 class="panel-title" style="color :#05B2EA;"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right" style="margin-right: 50px;">
                            <?php
                            $getmseries = mysqli_query($conn, "SELECT `code`, `name` FROM $zroot.`series` WHERE `code`='$zseries'") or die(mysqli_error($conn));
                            $mseries = mysqli_fetch_array($getmseries);
                            ?>

                            <label class="label-control"><?php Echo $zseries; ?></label> - <label class="label-control"><?php Echo $mseries['name']; ?></label>
                            <?php if ($usertype != 1 && $usertype != 2) { ?>

                                <input type='hidden' class='series' value='<?= $zseries ?>'>
                            <?php } ?>
                        </div>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>

                    <div class="panel-body " >
                        <?php if ($usertype == 1 || $usertype == 2) { ?>
                            <div class="row">
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                    <label class="label-control marg">Series</label>
                                </div>
                                <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                    <select  class="select2 form-control series" style="" name="series" >
                                        <?php
                                        echo "<option value=''></option>";
                                        $getgrop = mysqli_query($conn, "SELECT `code`, `name` FROM $zroot.`series` WHERE code != ''")or die(mysqli_error($conn));
                                        while ($grop = mysqli_fetch_array($getgrop)) {
                                            echo "<option value='$grop[code]'>$grop[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                        <?php } ?>



                        <!--                        <div class="row">
                                                    <div class="col-lg-7"></div>
                                                    <div class="col-lg-2 text-right">
                                                        <label class="label-control">Search</label>
                                                    </div>
                                                    <div class="col-lg-3 text-right">
                                                        <span>
                                                            <input type='text' style="" name='search' class="form-control tsearch">    
                                                            <input type='hidden' style="" name='sersrch' class="form-control sersrch" value="<?php echo $zseries; ?>">    
                                                        </span>
                                                    </div>
                                                </div>-->
<!--                        <div class="row stab">
                            <div class="col-lg-2">
                                <input type='button' value='Search' class="btn btn-info search "> 
                            </div>
                        </div>-->
                        <div class="row searchtab">
                            <div class="col-lg-2">
                                <select class="form-control sfield2">
                                    <option value="1">Item Name</option>
                                </select>
                            </div>
                            <div class="col-lg-2">
                                <select class="form-control operator">
                                    <option value=""></option>
                                    <option value="L">Like</option>
                                    <option value="E">Exactly</option>
                                </select>
                            </div>
                            <div class="col-lg-2">
                                <input type="text" value="" class="form-control ssearch2" placeholder="Search Here">
                            </div>
                            <div class="col-lg-2">
                                <input type="button" class="btn btn-primary  sload2" value="Search" >
                            </div>
                        </div>
                        <div class="table-responsive chdata" style="max-height: 1710px;overflow-y: auto;display:none">
                            <table class="table table-striped table-condensed table-bordered"  style="width: 100%">
                                <thead style="background: #05B2EA; color:white;">
                                    <tr>
                                        <th style="width: 3%"></th>
                                        <th style="width: 5%" class='text-right'>Sr.</th>
                                        <th style="width: 30%">Item Name</th>
<!--                                        <th style="width: 10%">Size</th>
                                        <th style="width: 10%">Act. Size</th>-->
                                        <!--<th style="width: 10%">Company</th>-->
                                        <th style="width: 10%">Unit</th>
                                        <th style="width: 12%" class='text-right'>Opening</th>
                                        <!--<th style="width: 12%" class='text-right'>Minimum</th>-->
                                    </tr>
                                </thead>
                                <tbody class="tabledata" id="data">
                                    <?php
                                    $ctr = 0;
//                                    $gitem = mysqli_query($conn, 'SELECT * FROM `item`')or die(mysqli_error($conn));
//                                    while ($arr = mysqli_fetch_array($gitem)) {
//                                           $arrs[] = $arr; 
//                                    }
//                                    $option = '';
//                                    foreach($arrs as $item){
//                                        $option.="<option value='$item[name]'>$item[name]</option>";
//                                    }
//                                    $mctr = 1;
//                                    while ($tran = mysqli_fetch_object($get)) {
//                                        Echo "<tr>";
//                                        Echo "<td align='center' valign='top'><i class='fa fa-close remove' style='display: none;'></i></td>";
//                                        Echo "<td><input type='text' class='form-control form srno text-right' name='srno_$ctr' value='$mctr' readonly></td>";
////                                        Echo "<td><input type='text' class='form-control form srno text-right' name='srno_$ctr' value='$tran->srno' readonly></td>";
//                                        Echo "<td>";
//                                        Echo "<select class='select2 form form-control detailpart item' name='itemname_$ctr' style='width:100%' readonly>";
//                                        Echo "<option value='$tran->itemname'>$tran->itemname</option>";
////                                        $getditem = mysqli_query($conn, "SELECT `code`, `name` FROM `item` GROUP BY `name` ORDER BY `name`")or die(mysqli_error($conn));
////                                        while ($ditem = mysqli_fetch_array($getditem)) {
////                                            Echo "<option value='$ditem[name]'>$ditem[name]</option>";
////                                        }
//                                        Echo "</select>";
//                                        Echo "<input type='hidden' class='itemcode' name='itemcode_$ctr' value='$tran->itemcode'>";
//                                        Echo "</td>";
//                                        Echo "<td>";
//                                        Echo "<select class='form form-control detailpart size' name='size_$ctr' style='width:100%' readonly>";
//                                        Echo "<option value='$tran->size'>$tran->size</option>";
////                                        $getdsize = mysqli_query($conn, "SELECT `size` FROM `item` where `name`='$tran->itemname' GROUP BY `size` ORDER BY `size` ")or die(mysqli_error($conn));
////                                        while ($dsize = mysqli_fetch_array($getdsize)) {
////                                            Echo "<option value='$dsize[size]'>$dsize[size]</option>";
////                                        }
//                                        Echo "</select>";
//                                        Echo "</td>";
//                                        Echo "<td>";
//                                        Echo "<select class='form form-control detailpart actsize' name='actsize_$ctr' style='width:100%' readonly>";
//                                        Echo "<option value='$tran->actsize'>$tran->actsize</option>";
////                                        $getdsize2 = mysqli_query($conn, "SELECT `size` FROM `item` where `name`='$tran->itemname' GROUP BY `size` ORDER BY `size` ")or die(mysqli_error($conn));
////                                        while ($dsize2 = mysqli_fetch_array($getdsize2)) {
////                                            Echo "<option value='$dsize2[size]'>$dsize2[size]</option>";
////                                        }
//                                        Echo "</select>";
//                                        Echo "</td>";
////                                        Echo "<td><input type='text' class='form-control form company' name='comp_$ctr' value='$tran->comp' readonly></td>";
//                                        Echo "<td><input type='text' class='form-control form  unit' name='unit_$ctr' value='$tran->unit' readonly></td>";
//                                        Echo "<td><input type='text' class='form-control form text-right opening' name='opening_$ctr' value='$tran->opstk'></td>";
//                                        Echo "<td><input type='text' class='form-control form text-right minimum' name='minimum_$ctr' value='$tran->minstk'></td>";
//                                        Echo "</tr>";
//                                        $ctr++;
//                                        $mctr++;
//                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad chdata" style="display:none">
<!--                            <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 : $ctr; ?>" />  
                        </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>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
