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

$maindb = 'inwmfmain';
$trandb = 'inwmftran';
$accdb = 'actran';
$stkdb = 'transacn';
$imgdb = "purchimage";
$formname = 'Inward';
$printfile = "inward_print.php";
$file = 'inward_mi.php';

// order for stock // 1 - opening, 2- inwar, 3- purchase, 4 purchase return, 5 outward,  6 - sale, 7 -  sale return, 8 - excess, 9 - damage
$ordno = 2 ;    

$trandbexist = 1;
$comp = $_SESSION["comp_" . $dbname];
$myyear = $_SESSION[$myyear];

$source = "IN";
$para = "";
$keycond = "";

$keyfld = "vouchno";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
mysqli_select_db($conn, $zdb);
$date = date("Y-m-d");
include("includes/vouchno_generate.php");
include("includes/prev&next.php");
if (isset($_POST["submit"])) {

    $a = 1;

    $vouchdt = trim($_POST["vouchdt"]);
    $voucher = trim($_POST["voucher"]);
    $totqty = trim($_POST["totqty"]);
    $godown = $_POST['godown'];
    $mvouch = $qvouch;
    $count = $_POST["items"];
    $rem1 = $_POST["rem1"];
    $rem2 = $_POST["rem2"];



    if ($a == 1) {

        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `item` WRITE, `$stkdb` WRITE");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` where vouchno != '' 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';
            }
            $mvouch = $qvouch;
        }



        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));

        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `vouchno`='$mvouch' AND `source`='$source' ") or die(mysqli_error($conn));


        $cnt = 0;
        $cntamt = 0;

        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $unit = $_POST["unit_$i"];
            $qty = $_POST["qty_$i"];
            $location = $_POST["location_$i"];
            $itemcode = $_POST["itemcode_$i"];

            $cnt++;
            $group = $source . $mvouch;
            if (!empty($itemcode)) {

                mysqli_query($conn, "INSERT INTO `$trandb`(vouchno,  vouchdt, srno, itemcode,  unit, qty, location)
                                                  VALUES('$mvouch', '$vouchdt', '$cnt', '$itemcode', '$unit', '$qty', '$location')") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, godown, source,vouchno,  vouchdt, srno, itemcode,  unit, qty, location, barcode, ordno)
                                                  VALUES('$myyear','$godown', '$source', '$mvouch', '$vouchdt', '$cnt', '$itemcode', '$unit', '$qty', '$location', '$itemcode', '$ordno'  )") or die(mysqli_error($conn));
            }
        }

        ######################### STOCK PART ####################################################
############################### Accounts Part ######################################################
#
#        

        if ($addflag == 1) {
            $newvoucher = $mono . $mvouch;
            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `godown`,`totqty`,`rem1`,`rem2`)
                                      VALUES('$mvouch', '$vouchdt', '$godown', '$totqty', '$rem1', '$rem2')") or die(mysqli_error($conn));
        } else {

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `totqty`='$totqty', `godown`='$godown', `rem1`='$rem1', `rem2`='$rem2' WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        }
        mysqli_query($conn, "UNLOCK TABLES");
        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch&type=$type");
        } else {
            updmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch&type=$type");
        }
    }
}
if ($addflag != 1) {

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

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`= '$get[godown]'") or die(mysqli_error($conn));
    $getgd = mysqli_fetch_array($getgodown);
}



if (isset($_GET["delete"])) {
    $delcond2 = " vouchno = '$_GET[vouchno]' ";
    $delcond = " vouchno = '$_GET[vouchno]'  and source = '$source'";



    delnew($zdb, "$maindb", "$delcond2", "$file?mode=last");
    delnew($zdb, "$trandb", "$delcond2", "$file?mode=last");
    delnew($zdb, "$stkdb", "$delcond", "$file?mode=last");
}

include("includes/linkshead.php");
?>

<script src="MyScript/inward_mi.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/inward_mi_search.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>


</head>
<body>
    <form action='' method='POST'>
        <div class="col-lg-3 pad text-center" id="overlay_form" style="display:none;z-index:999;">
            <button type="button" id="close_form" style="float: text-right;font-weight: bold;font-size: 18px;color:red;" ><i class="fa fa-close"></i></button>
            <div style='width:100%;' id='browdata'>
            </div>
        </div>
        <!-- Default box -->
        <div>
            <div class="col-lg-8 col-md-7 col-sm-6 col-xs-6  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:#78909C"><a class="myPrint"><?php Echo $formname; ?></a></h3>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Date</label>
                        </div>
                        <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="date" class="form-control form vouchdt focusfield" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : "$date" ?>" autofocus>
                            <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                            <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                        </div>
                        <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Voucher</label>
                        </div>
                        <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="hidden" class="form-control addflag addflg" value="<?php echo $addflag ?>" >
                            <input type="text" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" readonly>
                            <input type="hidden" name="voucher" class="form-control form voucher"  value="<?php echo ($addflag != 1) ? $get['vouchno'] : '' ?>" tabindex="-1" readonly>
                            <input type="hidden" name="source" class="form-control form source" value="<?= $source; ?>" id="source">
                            <input type="hidden" name="monno" class="form-control form monno" value="<?php echo ($addflag != 1) ? $get['monno'] : '' ?>" >
                            <input type="hidden" name="type" class="form-control form type" value="<?php echo ($addflag != 1) ? $get['mattype'] : $gtype ?>" >
                            <input type="hidden" class="form-control form" name="cashvouch" value="<?php Echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>" >
                        </div>
                        

                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Godown</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <select name="godown" class="form-control form godown select2" required>
                                <!--<option value="<?php echo ($addflag != 1) ? $getgd['code'] : ""; ?>"><?php echo ($addflag != 1) ? $getgd['name'] : ""; ?></option>-->
                                <?php
                                $mowng = mysqli_query($conn, "SELECT * FROM `godown`") or die(mysqli_error($conn));
                                while ($trowg = mysqli_fetch_array($mowng)) {
                                    echo "<option value=$trowg[code]>$trowg[gcode]</option>";
                                }
                                ?>
                            </select>
                        </div>

                    </div>
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                        <div class="panel panel-default">
                            <div class="table-responsive">
                                <table class="table-striped table-condensed table-bordered tab" width="100%">
                                    <thead  class="mySearchTr">
                                        <tr>
                                            <th style="width: 1%"></th>
                                            <th class="srno" style="width: 1%">Sr.</th>
                                            <!--<th style="width: 4%">Item Code</th>-->               
                                            <th style="width: 13%;">Item Name</th> 
                                            <th style='width: 3%' class="text-right">Qty.</th>
                                            <th style='width: 8%' >Unit</th>
                                            <th style='width: 8%' >Location.</th>
                                        </tr>
                                    </thead>
                                    <tbody class="tbody">
                                        <?php
                                        if ($addflag != 1) {
                                            $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
                                            $ctr = 0;
                                            while ($row = mysqli_fetch_array($qry)) {

                                                $getitem = mysqli_query($conn, "SELECT code, name FROM `item` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
                                                $geti = mysqli_fetch_array($getitem);

                                                $getloc = mysqli_query($conn, "SELECT code, name FROM `location` WHERE code = '$row[location]'") or die(mysqli_error($conn));
                                                $fetloc = mysqli_fetch_array($getloc);

                                                Echo "<tr>";
                                                Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                                Echo "<td><input type='text' name='srno_$ctr' class='srno form-control  form'  value='$row[srno]'></td>";
                                                Echo "<td>";
                                                Echo "<select class='form-control form item select2' name='itemcode_$ctr' style='width:100%'  required>";
                                                Echo "<option value='$geti[code]'>$geti[name]</option>";
                                                $git = mysqli_query($conn, "SELECT `code`,`name` FROM `item` ORDER BY `name`")or die(mysqli_error($conn));
                                                while ($itm = mysqli_fetch_array($git)) {
                                                    Echo "<option value='$itm[code]'>$itm[name]</option>";
                                                }
                                                Echo "</select>";
                                                Echo "</td>";
                                                Echo "<td><input type='number' step='any' name='qty_$ctr' class='qty form-control  text-right form' value='$row[qty]'></td>";
                                                Echo "<td><input type='text' name='unit_$ctr' class='unit form-control  form' value='$row[unit]'></td>";
                                                Echo "<td>";
                                                Echo "<select class='form-control form location select2' name='location_$ctr' style='width:100%' >";
                                                Echo "<option value='$fetloc[code]'>$fetloc[name]</option>";
                                                $gloc = mysqli_query($conn, "SELECT `code`,`name` FROM `location` order by name")or die(mysqli_error($conn));
                                                while ($loc = mysqli_fetch_array($gloc)) {
                                                    Echo "<option value='$loc[code]'>$loc[name]</option>";
                                                }
                                                Echo "</select>";
                                                Echo "</td>";
                                                Echo "</tr>";
                                                $ctr++;
                                            }
                                        } else {
                                            ?>
                                            <tr>                                    
                                                <td><i class='fa fa-close remove'></i></td>
                                                <td><input type='text' name='srno_0' class='srno form-control form' value='1'></td>
                                                <td>
                                                    <select class="form-control form item select2" name="itemcode_0" style='width:100%' required>
                                                        <option value=""></option>
                                                        <?php
                                                        $git = mysqli_query($conn, "SELECT * FROM `item` order by name")or die(mysqli_error($conn));
                                                        while ($itm = mysqli_fetch_array($git)) {
                                                            Echo "<option value='$itm[code]'>$itm[name]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </td>
                                                <td><input type='text' name='qty_0' class='qty form-control form text-right pad' value='0'></td> 
                                                <td><input type='text' name='unit_0' class='unit form-control form pad' value='0'></td> 
                                                <td>
                                                    <select class="form-control form location select2" name="location_0" style='width:100%' required>
                                                        <option value=""></option>
                                                        <?php
                                                        $gloc = mysqli_query($conn, "SELECT * FROM `location` order by name")or die(mysqli_error($conn));
                                                        while ($loc = mysqli_fetch_array($gloc)) {
                                                            Echo "<option value='$loc[code]'>$loc[name]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>

                            <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 class="col-lg-12 col-xs-12 pad">
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Total Qty</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="number" name="totqty" class="form-control form text-right totqty" id="totqty" value="<?php Echo ($addflag != 1) ? $get['totqty'] : "0"; ?>" readonly>
                                    </div>
                                </div>
                            </div>  
                            <div class="col-lg-12 col-xs-12 pad">
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-1 pad">
                                        <label class="label-control marg">Remarks</label>
                                    </div>
                                    <div class="col-lg-6 pad">
                                        <input type="text" name="rem1" class="form-control form rem1" id="rem1" value="<?php Echo ($addflag != 1) ? $get['rem1'] : ''; ?>" >
                                    </div>
                                    <div class="col-lg-6 pad">
                                        <input type="text" name="rem2" class="form-control form rem2" id="rem2" value="<?php Echo ($addflag != 1) ? $get['rem2'] : ''; ?>" >
                                    </div>
                                </div>
                            </div>  
                        </div>
                    </div>
                </div>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>    
            </div>
        </div>
        <div class="col-lg-7 col-md-12 col-sm-12 col-xs-12 pad">

        </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-2">
                            <select class="form-control ifield">
                                <option>Sort By Field</option>
                                <option value="1">Name</option>
                                <option value="2">Hsncode</option>
                                <option value="3">GST %</option>
                                <option value="4">Unit</option>
                                <option value="5">Cost Per 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-2">
                            <select class="form-control isortBy">
                                <option>Sort By</option>
                                <option value="1">Name</option>
                                <option value="2">Hsncode</option>
                                <option value="3">GST %</option>
                                <option value="4">Unit</option>
                                <option value="5">Cost Per Unit</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control iorderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control isearch">
                        </div>
                        <div class="col-lg-2">
                            <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%">GST %</th>
                                    <th width="15%">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>
    <div class="modal fade" id="imageUpload" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="col-lg-11">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Article Image Upload</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-lg-12 upload">
                                <form id="upload_form" enctype="multipart/form-data" method="post">

                                    <div class="col-lg-3 pad">
                                        <span class="btn btn-default btn-file btn-square btn-warning">
                                            Choose File <input type="file" name="file1" id="file1">
                                        </span>
                                        <input type="hidden" name="code" id="vouchno" value='<?php Echo ($addflag != 1) ? $get['vouchno'] : "$qvouch"; ?>'>
                                        <input type="button" value="Upload File"  id="uploadbtn" class="btn btn-square" style="background-color: #C0CA33; color: white">
                                    </div>
                                    <div class="col-lg-6">
                                        <div class="progress">
                                            <progress id="progressBar" class="progress-bar"  role="progressbar" value="0" max="100" style="width:100%;color: red;"></progress>
                                        </div>
                                        <h3 id="status"></h3>
                                        <p id="loaded_n_total"></p>
                                    </div>
                                </form>
                            </div>
                            <div class="col-lg-10 upload">
                                <img id="preview" src="" alt="your image"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade searchModal bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg" style="margin-top: 15%">
            <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-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher</option>  
                                <option value="2">Date</option>
                                <option value="3">Godown</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                              <option value="1">Voucher</option>  
                                <option value="2">Date</option>
                                <option value="3">Godown</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>

                    <div class="col-lg-12 sloader text-center">
                        <img src="images/search.gif" alt="loading">       
                    </div>
                    <div class="col-lg-12 snoRecord 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="5%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="10%">Godown</th>
                                    <th width="15%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprevious" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>

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