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

$formname = 'Deposit';
$maindb = 'deporet';
$accdb = 'actran';
$billdb = '';

$file = 'deporet.php';
$grid = '';
$source = 'DP';

$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'deporeg.php';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    $a = 1;
    $username = $_SESSION["username_" . $dbname];

    $vouchdt = $_POST["vouchdt"];
    $code = $_POST["code"];
    $accode = $_POST["accode"];
    $tenant = trim($_POST["tenant"]);
    $type = trim($_POST["type"]);
    $flatno = trim($_POST["flatno"]);
    $deposit = $_POST["deposit"];
    $narr1 = $_POST["narr1"];
    $narr2 = $_POST["narr2"];
    $billflag = $_POST["billflag"];

    $mvouch = $qvouch;


    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `tenant` WRITE, `$accdb` WRITE") or die(mysqli_error($conn));

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


        ######################## ACCOUNTS PART ######################################

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

//        browse($amount);
        $purcode = '16';
        if ($deposit > 0) {
//            browse('suf');
            $minus_amt = ($deposit * -1);

            $qrygrp = mysqli_query($conn, "select `grpcode`,name from tenant where `accode` = '$tenant'") or die(mysqli_error($conn));
            $fetgrp = mysqli_fetch_array($qrygrp);
            $pgrp = $fetgrp['grpcode'];
            $tenname = $fetgrp['name'];
            
            
            // cash part
            mysqli_query($conn, "INSERT IGNORE INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount,forac)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$purcode','011', '$deposit','R')") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT IGNORE INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount,forac)
                    VALUES ('$vouchdt','$mvouch', '$source', '002', '$binit', '$tenant', '$pgrp', '$minus_amt','R')") or die(mysqli_error($conn));
        }


        if ($addflag == 1) {
            mysqli_query($conn, "INSERT IGNORE INTO `$maindb`(`vouchno` ,`vouchdt`, `tenant`, `flatno` , `type`, `accode`, `deposit`, `narr1`, `narr2`)
                                       Values ('$qvouch', '$vouchdt', '$tenant', '$flatno', '$type', '$tenant',  '$deposit', '$narr1', '$narr2')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', tenant='$tenname', `accode`='$tenant',
                                                       deposit='$deposit', `flatno`='$flatno', `type`= '$type', narr1='$narr1' ,`narr2`='$narr2'
                                                       where `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        }

        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
        } else {
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

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

    $getbank = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[bankname]' and grpcode = '002'");
    $banknm = mysqli_fetch_array($getbank);

    $getparty = mysqli_query($conn, "SELECT accode , name , flatno FROM `accmast` WHERE `accode` = '$get[accode]'");
    $gtpt = mysqli_fetch_array($getparty);

    $result2 = mysqli_query($conn, "SELECT * FROM `billsett` WHERE source = 'CR' and bnkvouch = '$get[vouchno]'") or die(mysqli_error($conn));
}

/* DELETE */
if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `tenant` WRITE, `$accdb` WRITE, `billsett` WRITE") or die(mysqli_error($conn));

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last", $conn);

    $delsett = " source = '$source' AND bnkvouch = '$_GET[vouchno]'";
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last", $conn);
    delnew($zdb, "billsett", "$delsett", "$file?mode=last", $conn);


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

include("includes/linkshead.php");
?>
<script src="MyScript/get_bankamt.js" type="text/javascript"></script>
<script src="MyScript/cashrecp.js" type="text/javascript"></script>
<script src="MyScript/deposit_search.js" type="text/javascript"></script>

<!--<script src="MyScript/checkdate.js" type="text/javascript"></script>-->
<script src="MyScript/shortkeys.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                    </div>
                    <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 class="panel-body">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4  text-right">
                                <label class="label-control">Receipt No.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad"> 
                                <input type="text" name="vouchno" id="vouchno"  value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>" class="form-control form"  readonly>
                                <input type="hidden" class="form-control form trnflag" name="trnflag" value="<?php Echo ($addflag != 1) ? $get["trnflag"] : ""; ?>" readonly>
                                <!--<input type="hidden" class="form-control form billflag" name="billflag" value="0">-->
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-4 col-xs-4 text-right">
                                <label class="label-control">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad"> 
                                <input type="date" name="vouchdt"   placeholder="date" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('Y-m-d'); ?>" class="form-control form vouchdt" style="height: 30px;" required autofocus="autofocus">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4  text-right">
                                <label class="label-control">Name</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad"> 
                                <select id="tenant" name="tenant" class="form-control form select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $gtpt['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $gtpt['name'] : ""; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT * FROM `accmast` where grpcode = '042' order by `name`") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value='$trow[accode]'>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div> 
                        </div>
                        
                        
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4  text-right">
                                <label class="label-control">Flat no.</label>
                            </div>
                            <div class="col-lg-2 col-md-4 col-sm-6 col-xs-8 pad"> 
                                <select id="flatno" name="flatno" class="form-control form select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['flatno'] : ""; ?>"><?php echo ($addflag != 1) ? $get['flatno'] : ""; ?></option>
                                    <?php
                                    $gflt = mysqli_query($conn, "SELECT * FROM `accmast` where grpcode = '042'") or die(mysqli_error($conn));
                                    while ($tflt = mysqli_fetch_array($gflt)) {
                                        echo "<option value='$tflt[flatno]'>$tflt[flatno]</option>";
                                    }
                                    ?>
                                </select>
                            </div> 
                        </div>
                        
                        <div class="row">
                            <?php 
                            if ($get['type'] == N){
                                
                              $tp='Normal';
                                
                            }else if ($get['type'] == '') {
                                 $tp='';
                            }
                            else{
                                
                                $tp='Heavy';
                            }
                                
                            
                            ?>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right">
                                <label class="label-control ">Type</label>
                            </div>
                            <div class="col-lg-2 col-md-4 col-sm-6 col-xs-8 pad">
                                <select name="type" class="form-control form select2">
                                    <option value="<?php echo ($addflag != 1) ? $tp : ""; ?>"><?php echo ($addflag != 1) ?$tp : ""; ?></option>
                                    <option value="N">Normal</option>
                                    <option value="H">Heavy</option>
                                </select>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right">
                                <label class="control-label">Deposit</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad"> 
                                <input type="number" step="any" name="deposit" required id="deposit" text-right value="<?php echo ($addflag != 1) ? $get['deposit'] : ""; ?>" class="form-control form text-right numeric" required>
                            </div> 
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right">
                                <label class="label-control marg">Remark</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="narr1" value="<?php echo ($addflag != 1) ? $get['narr1'] : ''; ?>" class="form-control form narr1" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="narr2" value="<?php echo ($addflag != 1) ? $get['narr2'] : ''; ?>" class="form-control form narr2" >
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
            <div class="modal fade " tabindex="" id="settmodal" 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">Bill Settlement</h4>
                        </div>
                        <div class="modal-body">
                            <div class=" table-responsive">
                                <div class="toolbar">
                                    <div class="row">
                                        <div class="col-lg-2"><label class="label-control marg" style="color:red">Total Remain</label></div>
                                        <div class="col-lg-2"><input type="text" id="remain" name="remain" class="form-control" readonly style=""></div>
                                        <div class="col-lg-2"><label class="label-control marg" style="color:blue">Total Setoff</label></div>
                                        <div class="col-lg-2"><input type="text" id="totsetoff" name="totsetoff" class="form-control" readonly style=""></div>
                                        <!--<div class="col-lg-2"><input id="submit" type='submit' name='submit' value='<?php // echo ($addflag == 0) ? "Update" : "Save";                  ?>' class="btn btn-primary"></div>-->
                                        <?php
//                                    if (!isset($_GET["method"])) {
                                        ?>
                                        <!--<div class="col-lg-2"><input type='button' name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<?php echo $grid; ?>'"></div>-->
                                        <?php // }   ?>
                                    </div>
                                </div>
                                </header>
                                <div id="div-2" class="accordion-body collapse in body">
                                    <div style="max-height:300px;overflow: auto; border: 0px solid;">
                                        <div id="data"></div>  
                                    </div>
                                </div>    
                            </div>
                        </div>
                        <div class="modal-footer"></div>
                    </div>
                </div>
            </div>
        </div>

    </form>

    <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">
                    <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                    <div class="row">
                        <div class="col-lg-2" >
                            <select class="form-control sfield" >
                                <option value="1">Voucher No.</option>
                                <option value="2">Date</option>
                                <option value="3">Party</option>
                                <option value="4">Deposit</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 No. </option>
                                <option value="2">Date</option>
                                <option value="3">Party</option>
                                <option value="4">Deposit</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 No.</th>
                                    <th width="30%">Date</th>
                                    <th width="25%">Party</th>
                                    <th width="14%">Deposit</th>
                                    <th width="14%">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>
