<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
$formname = 'Packing Material Stock';
$maindb = 'packsacn';
//$printfile = "openstkitem_print_fr.php";
$trandbexist = 0;
$grid = '';
$keyfld = "srno";
$focusfield = "name";
$file = 'openstk_pack.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"];
    $count = $_POST["items"];

    $mvouch = $qvouch;


    if ($a == 1) {

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

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

            $msrno = $_POST["srno_$i"];
            $mitemname = $_POST["itemname_$i"];
            $hsncode = $_POST["hsncode_$i"];
            $gstperc = $_POST["gstperc_$i"];
            $unit = $_POST["unit_$i"];
            $qty = $_POST["qty_$i"];
            $idno = $_POST["idno_$i"];
            $supplier = $_POST["supplier_$i"];



            $itemcode = mysqli_query($conn, "SELECT code FROM `packing` WHERE `name`='$mitemname'") or die(mysqli_error($conn));
            $icode = mysqli_fetch_array($itemcode);
            $itcode = $icode[code];
            $mgroup = 'OP' . $itcode;
//            browse($mitem);
            if (!empty($mitemname)) {
                $ctn++;
                mysqli_query($conn, "INSERT INTO `$maindb`(`srno`,`godown`, `itemcode`, `itemname`, `unit`,
                            `qty`, `source`, `group`,`gstperc`,`hsncode`, `idno`, `accode`)
                            VALUES('$ctn','$godown', '$itcode', '$mitemname',  '$unit',
                                   '$qty', 'OP', '$mgroup', '$gstperc', '$hsncode', '$idno', '$supplier')") 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;
    }
}

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

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

include("includes/linkshead.php");
?>
<script src="MyScript/openstkpack.js" type="text/javascript"></script>
<script src="MyScript/search_raw.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
</head>
<body>
    <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">
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-4 pad text-right">
                                <input type='hidden'  class='type form-control  form' value='P'>
                                <label style='color:#037D95;font-size: 20px;' class="label-control marg">Godown</label>
                            </div>
                            <div class="col-lg-3" pad>
                                <select class="form-control godown form select2" name="godown" autofocus 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>
                        <table class="table-striped table-condensed table-bordered tab"  width="100%">
                            <thead  class="mySearchTr">
                                <tr>
                                    <th style="width: 1%"></th>
                                    <th style="width: 5%">Sr.</th>
                                    <th style="width: 22%;">Item Name</th> 
                                    <th style="width: 10%;">Hsncode</th> 
                                    <th style="width: 7%;">Gst %</th> 
                                    <th style='width: 7%'>Unit</th>
                                    <th style='width: 10%' class="text-right">Open Qty.</th>
                                    <th style='width: 10%' class="text-right">ID No.</th>
                                    <th style='width: 20%' >Supplier</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='itemname_0' required='true' class='itemname form-control  form' value='' readonly required>
                                    </td>
                                    <td><input type='text' name='hsncode_0' class='hsncode form-control  form' value='' readonly></td>
                                    <td><input type='text' name='gstperc_0' class='gstperc form-control  form' value='' readonly></td>
                                    <td><input type='text' name='unit_0' class='unit form-control  form' value='' readonly></td>
                                    <td><input type="number" step="any" name='qty_0' class='qty form-control text-right form ' value='0' ></td> 
                                    <td><input type="number" step="any" name='idno_0' class='qty form-control text-right form ' value='0' ></td> 
                                    <td><select class="form-control  select2 supplier" name="supplier_0" style="width: 100%">
                                            <option value=""></option>
                                            <?php
                                            $gsupplier = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where grpcode = '009' GROUP by name ORDER by name")or die(mysqli_error($conn));
                                            while ($fsupp = mysqli_fetch_array($gsupplier)) {
                                                $scode = $fsupp['accode'];
                                                $sname = $fsupp['name'];

                                                Echo "<option value='$scode'>$sname</option>";
                                            }
                                            ?>
                                        </select>
                                    </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; ?>">  
                        </div>
                    </div>
                    <div class="panel-footer">
                        <div style="overflow: auto; ">
                            <table class="table-condensed" style="margin: 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">
                <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">Hsncode</option>
                                <!--<option value="3">Itemcode</option>-->
                                <option value="4">GST %</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%">Hsncode</th>
                                    <!--<th width="15%">Itemcode</th>-->
                                    <th width="15%">GST %</th>
                                    <th width="10%">Unit</th>
                                    <th width="10%">Cost Per Unit</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>
