<?php
ob_start();
//echo "1";
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
//browse(1);
$formname = 'Damaged';
$maindb = 'dammain';
$trandb = 'damtran';
$stkdb = 'transacn';

$printfile = "damaged_print.php";
$file = 'damage.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "DM";
$myyear = $_SESSION["tit_" . $dbname];

date_default_timezone_set('Asia/Kolkata');
$time = date('H:i:s');

$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];

//browse($username);

$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);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $qvouch = trim($_POST["vouchno"]);

    $mono = $_POST["monno"];
    $mono = $_POST["monno"];

    $invno = $_POST["invno"];
    $paidby = trim($_POST["paidby"]);
    $totamount = trim($_POST["totamount"]);
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];


    date_default_timezone_set("Asia/Dubai");
    $usertime = date("Y-m-d H:i:sa");

    $count = $_POST["items"];

    $mvouch = $qvouch;



    if ($vouchdt == '0000-00-00') {
        browse("Date Cannot Be Empty");
        $a = 0;
    }

    if ($qvouch == '') {
        browse("Voucher Cannot Be Empty");
        $a = 0;
    }


    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `product` WRITE, `$stkdb` WRITE ");

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

            $time = strtotime($vouchdt);
            $month = date("m", $time);
            $intmonth = intval($month);
            if ($intmonth < 10) {
                $intmonth = '0' . $intmonth;
            }
            $voucher = $intmonth . $mvouch;
        }


        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"];
            $prodcode = $_POST["prodcode_$i"];
            $mitem = mysqli_real_escape_string($conn, $_POST["prodname_$i"]);
            $qty = $_POST["qty_$i"];
            $unit = $_POST["unit_$i"];
            $rate = $_POST["rate_$i"];
            $amt = $_POST["amount_$i"];
            $cost = 0;
            $accode = 0;

            if (!empty($mitem)) {
                $cnt++;
                $group = $source . $mvouch;
                $tqty = $qty * -1;

                mysqli_query($conn, "INSERT INTO `$trandb`(vouchno, vouchdt, srno, prodcode, prodname, qty, amount, rate ,unit)
                                                    VALUES('$mvouch', '$vouchdt', '$cnt', '$prodcode', '$mitem', '$qty', '$amt','$rate','$unit')") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `source`, `vouchno`,`vouchdt`, `prodcode`, godown,
                                                  `qty`,  rate, amount, accode, `cost`, `unit`,  `group`, `ordno` )
                                            VALUES('$cnt', '$myyear', '$source', '$mvouch', '$vouchdt', '$prodcode', '0',
                                                   '$tqty', '$rate', '$amt', '$accode', '$cost', '$unit', '$group', '8') ") or die(mysqli_error($conn));
            }
        }

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `totamount`, `remark1`, `remark2` )
                                      VALUES('$mvouch', '$vouchdt','$totamount','$remark1' , '$remark2' )") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt', `totamount` = '$totamount', 
                                                       `remark1` = '$remark1', `remark2` = '$remark2'  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&form=$form");
        } else {
            updmess();
        }
    }
}

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);

}

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `$stkdb` WRITE ")or die(mysqli_error($conn));

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]'";
    $delcond2 = " vouchno = '$_GET[vouchno]'";

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

    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script src="MyScript/damaged.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/damage_search.js" type="text/javascript"></script>
<!--<script src="MyScript/search_item_sale.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>-->
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<!--<script>
    $(document).ready(function () {
      
    })
</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: 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-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading" style="height:40px">
                        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-12">
                            <h3 class="panel-title" style="color:black"><a class="myPrint"><?php echo $formname; ?></a></h3>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
                            <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                            <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                            <h4 class="panel-title" style="float:right"><b><?php echo $get['username'] ?></b></h4>
                        </div>

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

                        </div>

                    </div>

                    <!--                        <div class="row">
                                                <div class="col-lg-1 col-xs-2 pad text-right">
                                                    <label class="label-control marg">Paid By</label>
                                                </div>-->
                    <!--                            <div class="col-lg-1 col-xs-4 pad">
                                                    <select id="paidby" name="paidby" class="form-control form select2 paidby" required>
                                                        <option 
                    <?php
                    if ($get['paidby'] == 'C') {
                        echo "selected='selected'";
                    }
                    ?> value="C">Cash</option>
                                                        <option
                    <?php
                    if ($get['paidby'] == 'R') {
                        echo "selected='selected'";
                    }
                    ?> value="R">Credit</option>
                    
                    
                                                    </select>
                                                </div>-->
                    <!--                        </div>-->



                    <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 style="width: 1%" class="text-left">Sr.</th>
                                    <th style='width: 2%' class="text-left">Prod.Code</th>                                        
                                    <th style='width: 4%' class="text-left">Product Name</th>                                        
                                    <th style='width: 2%' class="text-center">Qty.</th>
                                    <th style='width: 2%' class="text-center">Unit</th>
                                    <th style='width: 2%' class="text-center">Rate</th>
                                    <th style='width: 2%' class="text-center">Amount</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)) {
                                        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 text-right form-control  form'  value='$row[srno]'></td>";
                                        Echo "<td><input type='text' class='form-control form prodcode'           name='prodcode_$ctr'  value='$row[prodcode]'></td>";
                                        Echo "<td><input type='text' class='form-control form prodname'           name='prodname_$ctr'  value='$row[prodname]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right qty'  name='qty_$ctr'    value='$row[qty]' ></td>";
                                        Echo "<td><input type='text' class='form-control form text-right unit'    name='unit_$ctr'      value='$row[unit]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right rate'    name='rate_$ctr'      value='$row[rate]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right amount'    name='amount_$ctr'      value='$row[amount]'></td>";
                                        Echo "</tr>";
                                        $ctr++;
                                    }
                                } else {
                                    ?>
                                    <tr>                                    
                                        <td><i class='fa fa-close remove'></i></td>
                                        <td><input type='text' name='srno_0' class='srno text-right form-control form' value='1'></td>
                                        <td><input type='text' name='prodcode_0' class='prodcode form-control form' value='' ></td>
                                        <td><input type='text' name='prodname_0' class='prodname form-control form' value='' ></td>
                                        <td><input type='text' name='qty_0' class='qty text-right form-control form' value='' ></td>
                                        <td><input type='text' name='unit_0' class='unit text-right form-control form' value='' ></td>
                                        <td><input type='text' name='rate_0' class='rate text-right form-control form' value='' ></td>
                                        <td><input type='text' name='amount_0' class='amount text-right form-control form' value='' ></td>
                                    </tr>
                                    <?php
                                }
                                ?>
                            <input type="hidden" id="items" name="items" value="<?php Echo ($addflag != 1) ? $ctr : 1; ?>">  

                            </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" class="mandatoryId" name="manId" value="">  -->
                    </div>
                    <div class="col-lg-12 col-xs-12 pad">
                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-5 pad"></div>
                            <div class="col-lg-5 col-xs-5 pad text-right">
                                <label class="label-control marg">Total</label>
                            </div>
                            <div class="col-lg-2 col-xs-3 pad">
                                <input type="number" step="any" name="totamount" class="form-control form text-right totamount" value="<?php Echo ($addflag != 1) ? $get['totamount'] : "0"; ?>">    
                            </div>
                        </div>   
                    </div>

                    <div class="row">
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-3 text-right pad">
                            <label class="label-control marg">Remarks</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-9 col-xs-9  pad">
                            <input type="text" class="form-control form focusfield" name="remark1" value="<?php Echo ($addflag != 1) ? $get["remark1"] : ""; ?>" required>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-1 col-md-1 col-sm-2 col-xs-3 text-right pad">
                            <label class="label-control marg"></label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-9 col-xs-9  pad">
                            <input type="text" class="form-control form" name="remark2" value="<?php Echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                        </div>
                    </div>

                </div>
                <div class="col-lg-12 col-xs-12 pad">
                    <div class="col-lg-9 pad">

                        <?php
//                                        browse($get['canc_flag']);
                        if ($get['canc_flag'] != '') {
                            ?>
                            <img src="images/cancel.jpg" alt="Cancelled Bill" style="width:100%;height:100%">
                        <?php } ?>

                    </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"); ?>
                <script>
                    $(document).ready(function () {

                        var usertype = <?= $usertype ?>;
                        if (usertype == 3) {
                            $("#hedit").hide();
                        }
                    });
                </script>
            </div>
        </div>
    </div>
</div>
</div>
</form>


<div class="modal fade" id="cancmodel" 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">Cancel Bill</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-12 upload">
                            <form id="upload_form" method="post">

                                <div class="row pad">
                                    <input type="hidden" name="cancvouch" id="cancvouch" value='<?php Echo ($addflag != 1) ? $get['vouchno'] : "$qvouch"; ?>'>
                                    <input type="hidden" name="cashvouch2" id="cashvouch2" value='<?php Echo ($addflag != 1) ? $get['cashvouch'] : ""; ?>'>
                                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-5 text-right">
                                        <label class="label-control marg">Cancel Date</label>
                                    </div>
                                    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-7">

                                        <input type="date" name="cancdate" value="<?php Echo ($addflag != 1) ? $get['cancdate'] : ""; ?>" class="form-control cancdate">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-6 col-md-6 col-sm-7 col-xs-8 text-right">
                                        <input type="submit" value="Cancel"  name="cancbtn" class="btn btn-square" style="background-color: #C0CA33; color: white">
                                    </div>
                                </div>

                            </form>
                        </div>

                    </div>
                </div>
            </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>
                        </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>
                        </select> 
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control orderAs">
                            <option value="asc">Asc</option>
                            <option value="desc">Desc</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 style="background-color:#1ABC9C;">
                                <th width="25%">Voucher No </th>
                                <th width="55%">Voucher Date</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>
