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

$formname = 'Inward Raw Material';
$maindb = 'inrawmain';
$trandb = 'inrawtran';
$stkdb = 'rawsacn';


$printfile = "inwardraw_print.php";
$file = 'inwardraw.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "IN";


$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];

$stkdb1 = 'tran';
$zroot = $_SESSION["zdir"];
$combstkdb = $stkdb1 . $myyear;

//for adding entry and edit timings
$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
$usertype = $_SESSION["usertype_" . $dbname];
//end

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $qty = $_POST["qty"];
    $total = $_POST["total"];
    $remark = $_POST["remark"];
    $remark2 = $_POST["remark2"];


    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 11;



    if ($a == 1) {

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



        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch' $keycond") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `$keyfld`='$mvouch' and `source`='$source' $keycond") or die(mysqli_error($conn));
//        mysqli_query($conn, "DELETE FROM $zroot.$combstkdb WHERE `$keyfld`='$mvouch' AND `source`='$source' and `compcode`='$compcd'")or die(mysqli_error($conn));


        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $srno = $_POST["srno_$i"];
            $barcode = $_POST["itemcode_$i"];
            $color = $_POST["color_$i"];
            $item = $_POST["item_$i"];
            $trqty = $_POST["trqty_$i"];
            $unit = $_POST["unit_$i"];
            $mrp = $_POST["mrp_$i"];
            $msp = $_POST["msp_$i"];
            $rate = $_POST["rate_$i"];
            $amount = $_POST["amount_$i"];

            if (!empty($item)) {
                $srl++;
                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`, series,  `vouchno`, `vouchdt`, `barcode`, `itemcode`, `trqty`, `unit`, `mrp`, `msp`, `rate`, `amount`,color)
                                        VALUES( '$srl', '$fseries', '$qvouch', '$vouchdt', '$barcode', '$item', '$trqty', '$unit', '$mrp', '$msp', '$rate', '$amount','$color')") or die(mysqli_error($conn));

                ##########sTOCK pART########
                mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, series, `compcode`, `yearno`, `vouchno`, `vouchdt`, `source`, `barcode`, `itemcode`,  `qty`, `unit`, `mrp`, `rate`, `amount`,color)
                                        VALUES( '$srl', '$fseries', '$compcd', '$myyear', '$qvouch', '$vouchdt', '$source', '$barcode', '$item', '$trqty', '$unit', '$mrp', '$rate', '$amount','$color')") or die(mysqli_error($conn));

//                ##########Combine Stock Part########
//                mysqli_query($conn, "INSERT INTO $zroot.$combstkdb(`srno`, `compcode`, `yearno`, `vouchno`, `vouchdt`, `source`, `barcode`, `itemcode`,  `qty`, `unit`, `mrp`, `rate`, `amount`)
//                                        VALUES( '$srl', '$compcd', '$myyear', '$qvouch', '$vouchdt', '$source', '$barcode', '$item', '$trqty', '$unit', '$mrp', '$rate', '$amount')") or die(mysqli_error($conn));
            }
        }

#################################TRAN PART END #################################
############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, series, `vouchdt`, `qty`, `total`, `remark`,`remark2`, `adduser`, `adddate`, `addtime`)
                                     VALUES ('$qvouch', '$fseries', '$vouchdt',    '$qty', '$total','$remark', '$remark2','$adduser','$adddate','$addtime')") or die(mysqli_error($conn));

            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
        } else {
            $mvouch = $qvouch; //1
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `qty`='$qty', `total`='$total', `remark`='$remark', `remark2`='$remark2',`edituser`='$adduser',`edittime`='$edittime',`editdate`='$editdate' WHERE `vouchno`= '$mvouch' $keycond") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
        }
    }
}

if ($addflag != 1) {

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

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

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

if (isset($_GET["delete"])) {
//    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcondn = " vouchno = '$_GET[vouchno]' $keycond";
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' $keycond";
    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last&series=$fseries");
    delnew($zdb, "$trandb", "$delcondn", "$file?mode=last&series=$fseries");
    delnew($zdb, "$stkdb", "$delcond", "$file?mode=last&series=$fseries");
}

include("includes/linkshead.php");
?>
<script src="MyScript/inwardraw.js" type="text/javascript"></script>
<script src="MyScript/inwardraw_search.js" type="text/javascript"></script>
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
<!--                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:inherit"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>-->
                    <div class="panel-heading">
                        <?php include("includes/usermention.php");
                        ?>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>

                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control text-size form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form focusfield"  name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" autofocus>
                            </div>
                        </div>

                        <div class="table-responsive" style="overflow-y: scroll;height: 350px">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead  style="" class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="16%">Barcode</th>
                                        <th width="20%">Raw Material</th>
                                        <th width="10%">Color</th>
                                        <th width="11%"  class='text-right'>Qty.</th>
<!--                                        <th width="11%" >Unit</th>
                                        <th width="10%" class='text-right'>MRP</th>-->
                                        <th width="10%" class='text-right'>Rate</th>
                                        <th width="10%" class='text-right'>Amount</th>
                                        <!--<th width="10%" class='text-right'>Msp</th>-->
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' $keycond ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $getitem1 = mysqli_query($conn, "SELECT * FROM `rawmat` WHERE `code`='$tran[itemcode]' ")or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($getitem1);

                                            $findcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'") or die(mysqli_error($conn));
                                            $fcolor = mysqli_fetch_array($findcolor);


                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno'  name='srno_$ctr'        value='$tran[srno]'></td>";
                                            Echo "<td><input type='text' class='form-control form itemcode'          name='itemcode_$ctr'     value='$tran[itemcode]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form select2 item' name='item_$ctr' style='width:100%'>";
                                            Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                            $gitem = mysqli_query($conn, 'SELECT * FROM `rawmat` order by name')or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                Echo "<option value='$item[code]'>$item[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form select2 color' name='color_$ctr' style='width:100%'>";
                                            Echo "<option value='$fcolor[code]'>$fcolor[name]</option>";
                                            $gcolor = mysqli_query($conn, 'SELECT * FROM `color` order by name')or die(mysqli_error($conn));
                                            while ($color = mysqli_fetch_array($gcolor)) {
                                                Echo "<option value='$color[code]'>$color[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' class='form-control form text-right trqty'  name='trqty_$ctr'       value='$tran[trqty]'>  </td>";
//                                            Echo "<td>";
//                                            Echo "<select class='form-control form unit select2' name='unit_$ctr' style='width:100%'>";
//                                            Echo "<option value='$funit[code]'>$funit[name]</option>";
//                                            $gunit = mysqli_query($conn, 'SELECT `code`,`name` FROM `unit`')or die(mysqli_error($conn));
//                                            while ($unit = mysqli_fetch_array($gunit)) {
//                                                Echo "<option value='$unit[code]'>$unit[name]</option>";
//                                            }
//                                            Echo "</select>";
//                                            Echo "</td>";
//                                            Echo "<td><input type='text' class='form-control form text-right mrp'    name='mrp_$ctr'         value='$tran[mrp]' >   </td>";
                                            Echo "<td><input type='text' class='form-control form text-right rate'   name='rate_$ctr'        value='$tran[rate]'>   </td>";
                                            Echo "<td><input type='text' class='form-control form text-right amount' name='amount_$ctr'      value='$tran[amount]'> </td>";
//                                            Echo "<td><input type='text' class='form-control form text-right msp'    name='msp_$ctr'         value='$tran[msp]'>    </td>";
                                            Echo "</tr >";
                                            $ctr++;
                                            $tqty = $tqty + $tran['trqty'];
                                            $tamt = $tamt + $tran['amount'];
                                        }
                                    } else {
                                        $pctr = 0;
//                                        $yctr = 1;
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove" style="display:none"></i></td> 
                                            <td><input type="text" class="form-control form text-right srno"      name="srno_<?= $pctr ?>"        value="1"></td> 
                                            <td><input type="text" class="form-control form itemcode"             name="itemcode_<?= $pctr ?>"    value=""></td> 
                                            
                                            <td>
                                                <select class="form-control form item " name="item_0" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gitem = mysqli_query($conn, "SELECT * FROM `rawmat` order by name")or die(mysqli_error($conn));
                                                    while ($item = mysqli_fetch_array($gitem)) {
                                                        Echo "<option value='$item[code]'>$item[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                                <input type='hidden' class='msp' name='msp_0' >
                                                <input type='hidden' class='otprowsucs' name='otprowsucs_0' >
                                                <input type='hidden' class='otpvalue' name='otpvalue_0' >
                                            </td>
                                            <td>
                                                <select name="color_<?= $pctr ?>" class="form-control form select2 color" style="width: 100%">
                                                    <option value=""></option>
                                                    <?
                                                    $getcolor = mysqli_query($conn, "SELECT * FROM `color` ORDER BY name")or die(mysqli_error($conn));
                                                    while ($color1 = mysqli_fetch_array($getcolor)) {
                                                        Echo "<option value='$color1[code]'>$color1[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form text-right trqty"     name="trqty_<?= $pctr ?>"       value=""></td> 
    <!--                                            <td>
                                                <select class="form-control form unit select2" name="unit_0" style='width:100%' required>
                                                    <option value=""></option>
                                            <?php
                                            $gunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit`")or die(mysqli_error($conn));
                                            while ($unit = mysqli_fetch_array($gunit)) {
                                                Echo "<option value='$unit[code]'>$unit[name]</option>";
                                            }
                                            ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form text-right mrp"       name="mrp_<?= $pctr ?>"         value="" readonly></td> -->
                                            <td><input type="text" class="form-control form text-right rate"      name="rate_<?= $pctr ?>"        value=""></td> 
                                            <td><input type="text" class="form-control form text-right amount"    name="amount_<?= $pctr ?>"      value="" readonly></td> 
                                            <!--<td><input type="text" class="form-control form text-right msp"       name="msp_<?= $pctr ?>"         value="" readonly></td>--> 
                                        </tr>  
                                        <?php
                                    }
                                    $pctr++;
                                    ?>
                                </tbody>
                            </table>

                        </div>

                        <div class="row">
                            <div class="col-lg-1" >
                                <button type="button" class="btn btn-square form" style="background-color: #78909C"  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 : $pctr; ?>" />  
                            </div> 
                        </div> 

                        <div class="row">
                            <div class="col-lg-5 col-md-5 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Quantity.</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right totqty" name="qty" value="<?php Echo ($addflag != 1) ? $tqty : ""; ?>" readonly>
                            </div>

                            <div class="col-lg-4 col-md-4 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Total</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right totamt" name="total" value="<?php Echo ($addflag != 1) ? $tamt : ""; ?>" readonly>
                            </div>
                        </div>
                        <hr>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Remark</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="remark" value="<?php Echo ($addflag != 1) ? $get["remark"] : ""; ?>" >
                                <input type="text" class="form-control form " name="remark2" value="<?php Echo ($addflag != 1) ? $get["remark2"] : ""; ?>" >
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
        <div class="row" >
            <div class="col-lg-3 col-md-3 col-sm-2 col-xs-2 text-right pad">
<!--            <div class="col-lg-3 col-md-3 col-sm-2 col-xs-2 text-right pad">
                <?php if ($addflag == 1) { ?>
                    <a id='print' class="btn btn-pad btn-square " style="background-color:#5BC0DE;display:none;margin-top:17px; <? Echo $printdisabled ?>" href="inward_print_small.php?vouchno=<?= $qvouch; ?>"  target="_blank">
                        <i class="fa fa-print fa-lg" style="color:#ffffff"></i>
                    </a>
                    <?php
                } else {
                    ?>
                    <a id='print' class="btn btn-pad btn-square " style="background-color:#5BC0DE;display:;margin-top:17px; <? Echo $printdisabled ?>" href="inward_print_small.php?vouchno=<?= $qvouch; ?>"  target="_blank" >
                        <i class="fa fa-print fa-lg" style="color:#ffffff"></i>
                    </a>
                <?php } ?> 
            </div>-->
            </div>
            <div class="col-lg-5 col-md-5 col-sm-9 col-xs-9 pad" >
                <?php include("includes/buttons.php"); ?>
            </div>
        </div>

<!--        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>
        </div>-->
    </div>
</form>
<!--<div class="modal fade bs-example-modal-lg" 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">
                <div class=" table-responsive">
                    <table id="example1" class="table table-striped">
                        <thead>
                            <tr class="mySearchTr" >
                                <th>Voucher</th>
                                <th>Date</th>
                                <th>Qty.</th>
                                <th>Total</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            if ($addflag != 1) {
                                $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` where vouchno != '' $keycond")or die(mysqli_error($conn));
                                while ($row = mysqli_fetch_array($srchresult)) {
                                    echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]&series=$row[series]'>";
                                    echo "<td align='left'>$row[vouchno]</td>";
                                    echo "<td align='left'>$row[vouchdt]</td>";
                                    echo "<td align='left'>$row[qty]</td>";
                                    echo "<td align='left'>$row[total]</td>";
                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]&series=$row[series]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                    echo "</tr>";
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer"></div>
        </div>
    </div>
</div>-->
    
    
    <div class="modal fade bs-example-modal-lg searchModal" 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">
                    <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">Qty.</option>
                                <option value="4">Total</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="">Sort By</option>
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Qty.</option>
                                <option value="4">Total</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">
                    </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="10%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="10%">Qty.</th>
                                    <th width="10%">Total</th>
                                    <th width="5%">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 sprev" 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>
