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

$formname = 'Damaged Raw Material';
$maindb = 'dammain_mi';
$trandb = 'damtran_mi';
$stkdb = 'rawsacn';


$printfile = "damaged_mi_print.php";
$file = 'damaged_mi.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "DMRM";

$keycond1 = "";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION["tit_" . $dbname];

$zroot = $_SESSION["zdir"];

$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');

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

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = trim($_POST["vouchdt"]);
    $voucher = trim($_POST["voucher"]);
    $mono = $_POST["monno"];
    $godown = trim($_POST["godown"]);
    $tqty = trim($_POST["totqty"]);
    $rem1 = trim($_POST["remark1"]);
    $rem2 = trim($_POST["remark2"]);
    $type = trim($_POST["type"]);


    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 11;



    if ($a == 1) {

        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, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `$stkdb` WRITE, `$accdb` WRITE ");

        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++) {
            $cnt++;
            $msrno = $_POST["srno_$i"];
            $mitem = mysqli_real_escape_string($conn, $_POST["itemname_$i"]);

            $unit = $_POST["unit_$i"];
            $qty = $_POST["qty_$i"];
            $rate = $_POST["rate_$i"];
            $amt = $_POST["amount_$i"];
            $hsncode = $_POST["hsncode_$i"];
            $stkqty = $qty * -1;
            $group = $source . $mvouch;

            if ($type == 'R') {
                $mitemcode = mysqli_query($conn, "SELECT code,cost FROM `item` WHERE `name`='$mitem'") or die(mysqli_error($conn));
                $icode = mysqli_fetch_array($mitemcode);
            } else if ($type == 'P') {
                $mitemcode = mysqli_query($conn, "SELECT code,cost FROM `packing` WHERE `name`='$mitem'") or die(mysqli_error($conn));
                $icode = mysqli_fetch_array($mitemcode);
            }


            $itcode = $icode['code'];
            $cost = $icode['cost'];
            if (!empty($mitem)) {

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



                mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `godown`, `vouchno`, `vouchdt`, `itemcode`,  `unit`,
                                                  `qty`, `source`, amount, accode, `group` , `rate`, `cost`, `itemgrp`, `ordno`, `monno`)
                            VALUES('$cnt', '$myyear', '$godown', '$mvouch', '$vouchdt', '$itcode',  '$unit',
                                   '$stkqty', '$source', '$amt', '$party', '$group', '$rate', '$cost', '$itemgrp', '7', '$mono')") or die(mysqli_error($conn));
            }
        }
#################################TRAN PART END #################################
############################## MAIN ######################################################
        if ($addflag == 1) {
            $newvoucher = $mono . $mvouch;

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `monno`, `voucher`, `godown`,  `totqty`, `remark1`, `remark2`, `type`)
                                      VALUES('$mvouch', '$vouchdt', '$mono', '$newvoucher', '$godown', '$tqty', '$rem1', '$rem2', '$type')") or die(mysqli_error($conn));
//            insmess();
            $mvouch = $qvouch; //1
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

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

mysqli_query($conn, "UNLOCK TABLES");
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);

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

//if ($addflag == 1) {
//    $mlen = strlen($qvouch);
//    $myvouch = str_repeat("0", 6 - $mlen) . $qvouch;
//}

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

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

include("includes/linkshead.php");
?>
<script src="MyScript/damaged_mi.js?A=<?= mt_rand(0, 500); ?>" type="text/javascript"></script>
<script src="MyScript/search_raw.js?A=<?= mt_rand(0, 500); ?>" type="text/javascript"></script>
<script src="MyScript/damaged_mi_search.js?A=<?= mt_rand(0, 500); ?>" type="text/javascript"></script>
<script src="MyScript/checkdate.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-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-1 col-md-1 col-sm-1 col-xs-8 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt"  value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus style="width: 110%">
                                   <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-1 col-sm-1 col-xs-1 pad">
                                <input type="text" name="voucher" class="form-control form voucher" value="<?php echo ($addflag != 1) ? $get['voucher'] : '' ?>" tabindex="-1" readonly>
                                <input type="hidden" name="monno" class="form-control form monno" value="<?php echo ($addflag != 1) ? $get['monno'] : '' ?>" >
                                <input type="hidden" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Godown</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <select name="godown" class="form-control form godown" required>
                                    <option value="<?php echo ($addflag != 1) ? $getgodown['code'] : " "; ?>"><?php echo ($addflag != 1) ? $getgodown['gcode'] : " "; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT * FROM `godown`") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value=$trow[code]>$trow[gcode]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 pad text-right">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-2 pad">
                                <select  name="type" class="type form-control form" required>
                                    <option value=""></option>
                                    <option 
                                    <?php
                                    if ($get[type] == 'R') {
                                        echo "selected='selected'";
                                    }
                                    ?> value="R">Raw Material</option>
                                    <option
                                    <?php
                                    if ($get[type] == 'P') {
                                        echo "selected='selected'";
                                    }
                                    ?> value="P">Packing Material</option>
                                </select>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table-striped table-condensed table-bordered" width="100%">
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th style="width: 1%"></th>
                                        <th style="width: 2%">Sr.</th>
                                        <th style="width: 14%;">Item Name</th> 
                                        <th style='width: 4%'>Hsncode</th>
                                        <th style='width: 4%' class="text-right">Unit</th>
                                        <th style='width: 4%' class="text-right">Qty.</th>
                                        <th style='width: 4%' class="text-right">Rate</th>
                                </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)) {

                                            if ($get[type] == 'R') {
                                                $getitem = mysqli_query($conn, "SELECT code, name FROM `item` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
                                                $geti = mysqli_fetch_array($getitem);
                                            } elseif ($get[type] == 'P') {
                                                $getitem = mysqli_query($conn, "SELECT code, name FROM `packing` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
                                                $geti = mysqli_fetch_array($getitem);
                                            }
                                            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 "<input type='text' name='itemname_$ctr' required='true' class='itemname form-control  form' value='$geti[name]' readonly required>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' name='hsncode_$ctr' class='hsncode form-control  form' value='$row[hsncode]' readonly></td>";
                                            Echo "<td><input type='text' name='unit_$ctr' class='unit form-control  form' value='$row[unit]' readonly></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='number' step='any' name='rate_$ctr' class='rate form-control text-right  form' value='$row[rate]' ></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><input type='text' name='itemname_0' class='itemname form-control form' value='' required ></td>
                                            <td><input type='text' name='hsncode_0' class='hsncode form-control form' value='' required 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 zero' value='0' ></td> 
                                            <td><input type="number" step="any" name='rate_0' class='rate form-control text-right form zero' value='0' ></td> 
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad">
                            <button type="button" class="btn btn-primary btn-square form"  id="addnew" tabindex="-1">
                                <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; ?>">  
                            <input type="hidden" class="mandatoryId" name="manId" value="">  
                        </div>

                        <div class="row">
                            <div class="col-lg-10 col-md-10 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Total 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="totqty" value="<?php Echo ($addflag != 1) ? $get["totqty"] : " "; ?>" 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-5 col-md-5 col-sm-8 col-xs-8 pad">
                                <input type="text" name="remark1" class="form-control form " value="<? Echo ($addflag != 1) ? $get["remark1"] : ""; ?>">
                                <input type="text" name="remark2" class="form-control form " value="<? Echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                            </div>
                        </div>

                    </div>
                </div>
            </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 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 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">Godown</option>
                            <option value="4">Total Qty</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">Godown</option>
                            <option value="4">Total Qty</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="15%">Voucher</th>
                                <th width="15%">Date</th>
                                <th width="20%">Godown</th>
                                <th width="20%">Total Qty.</th>
                                <th width="10%">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>
