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

$formname = 'Manual Rent Receipt - Commercial';
//$maindb = 'bill';
$maindb = 'invmaincom';
$trandb = ""; 
$accdb = "actran";
$printfile = "manrcmprin.php";
$file = 'manrecpcom.php';
$trandbexist = 0;
$keyfld = "vouchno";
$focusfield = "name";
//$mgrpcode = '010';
$source = 'RCM';
$para = $_GET['para'];

$keycond = "";
$keycond1 = "AND `gentype`='$para'";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$zyear = right($zdb, 4);

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

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

    $a = 1;

    $accode = $_POST["accode"];
    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $tenant = $_POST["tenant"];
    $monthno = $_POST["monthno"];
    $fromdate = $_POST["fromdate"];
    $todate = $_POST["todate"];
    $nomonths = $_POST["nomonths"];
    $flatno = $_POST["flatno"];

    $block = $_POST["block"];
    $floortype = $_POST["floortype"];
    $floorno = $_POST["floorno"];
    $room = $_POST["room"];
    $newroom = $_POST["newroom"];
    $addprin = $_POST["addprin"];

    $monrent = $_POST["monrent"];
    $rent = $_POST["rent"];
    $proptax = $_POST["proptax"];
    $repairtax = $_POST["repairtax"];
    $othertax = $_POST["othertax"];
    $total = $_POST["total"];
    $cgstperc = $_POST["cgstperc"];
    $sgstperc = $_POST["sgstperc"];
    $cgstamt = $_POST["cgstamt"];
    $sgstamt = $_POST["sgstamt"];
    $taxable = $_POST["taxable"];
    $narr1 = $_POST["narr1"];
    $narr2 = $_POST["narr2"];
    $rentfor = $_POST["rentfor"];

    $mvouch = $qvouch;
    $count = $_POST["items"];

switch ($monthno) {
            case "01":
                $monname = 'January';

                break;
            case "02":
                $monname = 'February';

                break;
            case "03":
                $monname = 'March';

                break;
            case "04":
                $monname = 'April';

                break;
            case "05":
                $monname = 'May';

                break;
            case "06":
                $monname = 'June';

                break;
            case "07":
                $monname = 'July';

                break;
            case "08":
                $monname = 'August';

                break;
            case "09":
                $monname = 'September';

                break;
            case "10":
                $monname = 'October';

                break;
            case "11":
                $monname = 'November';

                break;
            case "12":
                $monname = 'December';

                break;

            default:
                break;
        }


    if ($a == 1) {

        mysqli_query($conn, "LOCK TABLE `$maindb` WRITE, $accdb  WRITE");



//        ------------------------------Accounts Part--------------------------------
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `source`='$source' AND `vouchno`='$vouchno' and `rentfor`='$rentfor'")or die(mysqli_error($conn));

        $minus_amt = $netamt * -1;
        $purcode = '000015';

        $gstamt = $netamt - $total;


        mysqli_query($conn, "INSERT IGNORE INTO `$accdb`(`vouchno`, `date`, `source`, `trnno`, `accode`, `grpcode`, `amount`, `rentfor`, forac, flatno)
                                            VALUES('$qvouch', '$vouchdt', '$source', '001', '$purcode', '011', '$total', '$rentfor', 'R', '$flatno')")or die(mysqli_error($conn));


        if ($gstamt > 0) {
            mysqli_query($conn, "INSERT IGNORE INTO `$accdb`(`vouchno`, `date`, `source`, `trnno`, `accode`, `grpcode`, `amount`, `rentfor`, forac, flatno)
                                            VALUES('$qvouch', '$vouchdt', '$source', '002', '36', '002', '$gstamt', '$rentfor', 'R', '$flatno')")or die(mysqli_error($conn));
        }

        mysqli_query($conn, "INSERT IGNORE INTO `$accdb`(`vouchno`, `date`, `source`, `trnno`, `accode`, `grpcode`, `amount`, `rentfor`, forac, flatno)
                                            VALUES('$qvouch', '$vouchdt', '$source', '003', '$tenant', '010', '$minus_amt', '$rentfor', 'R', '$flatno')")or die(mysqli_error($conn));

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

            mysqli_query($conn, "INSERT IGNORE INTO `$maindb`(year, `accode`, `vouchno`, `vouchdt`,`tenant`, `monthno`, `cgstperc`, `sgstperc`,`cgstamt`,`sgstamt`,`taxable`,`monname`,
                                                       monrent, `rent`, `proptax`, `repairtax`, `othertax`, `total`, `narr1`, `narr2`,rentfor, gentype,`fromdate`, `todate`,nomonths,flatno)
                                        VALUES('$zyear','$tenant', '$qvouch', '$vouchdt','$tenant', '$monthno', '$cgstperc', '$sgstperc','$cgstamt','$sgstamt','$taxable','$monname',
                                               '$monrent','$rent','$proptax', '$repairtax', '$othertax', '$total', '$narr1', '$narr2','$rentfor','$para','$fromdate', '$todate','$nomonths','$flatno')") or die(mysqli_error($conn));
//            insmess();

            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch&para=$para");
        } else {

            mysqli_query($conn, "UPDATE `$maindb` SET year='$zyear',`accode`='$tenant',`tenant`='$tenant', `monthno`='$monthno',`vouchdt`='$vouchdt',
                                                      monrent='$monrent', `rent`='$rent', `proptax`='$proptax', `repairtax`='$repairtax', `othertax`='$othertax',
                                                      `total`='$total', `narr1`='$narr1', `narr2`='$narr2',`monname`='$monname',
                                                      `cgstperc`='$cgstperc', `sgstperc`='$sgstperc',`cgstamt`='$cgstamt',`sgstamt`='$sgstamt',
                                                      `taxable`='$taxable',`fromdate`='$fromdate', `todate`='$todate',flatno='$flatno',nomonths='$nomonths' where $keyfld = '$mvouch' $keycond") or die(mysqli_error($conn));

//            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch&para=$para");
        }
        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' $keycond1") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);



    $getbuild = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`= '$get[tenant]'") or die(mysqli_error($conn));
    $fbuild = mysqli_fetch_array($getbuild);

    
    $qryfl = mysqli_query($conn, "SELECT  name,block,wing,floor,area,unit,mobile FROM `accmast` where `flatno` ='$get[flatno]' and grpcode = '010' ") or die(mysqli_error($conn));
    $fetfl = mysqli_fetch_array($qryfl);
    
    $blk = $fetfl['block'];
    
    $wing = $fetfl['wing'];
    $flrno = $fetfl['floor'];
}

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

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

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last&para=$para", $conn);
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last&para=$para", $conn);
}

include("includes/linkshead.php");
?>
<script src="MyScript/manrecpcom.js" type="text/javascript"></script>
<!--<script src="MyScript/tenant_search.js" type="text/javascript"></script>-->
<style>
    .disc{
        width:80px;
    }
    .disc1{
        width:50px;
    }
    .fntsiz{
        font-size: 16px;
    }
</style>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->

        <div class="col-lg-5 col-md-6 col-sm-10 col-xs-10 pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h5 class="panel-title"><b><?php Echo $formname; ?></b></h5>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Receipt No.</label>
                        </div>
                        <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form focusfield vouchno text-right" name="vouchno" value="<?php Echo ($addflag != 1) ? $qvouch : $qvouch; ?>" readonly>
                        </div>
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Date</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad">
                            <input type="date" class="form-control form vouchdt" name="vouchdt"  value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus >
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Party Name</label>
                        </div>
                        <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 pad">
                            <select name="tenant" class="form-control form tenant select2" required>
                                <option value="<?php echo ($addflag != 1) ? $get['tenant'] : ""; ?>"><?php echo ($addflag != 1) ? $fbuild['name'] : " "; ?></option>
                                <?php
                                $mownt = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where grpcode='042' and tenanttype = 'C'  ORDER BY name") or die(mysqli_error($conn));
                                while ($trowt = mysqli_fetch_array($mownt)) {
                                    echo "<option value='$trowt[accode]'>$trowt[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Block</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                            <input type="text" class="form-control form alphanumeric block" name="block" value="<?php Echo ($addflag != 1) ? $blk : ""; ?>" readonly tabindex="-1">
                            <input type="hidden" class="form-control form alphanumeric rentfor" name="rentfor" value="<?php Echo ($addflag != 1) ? $get["rentfor"] : ""; ?>" readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Wing</label>
                        </div>
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 pad">                            
                            <input type="text" class="form-control form alphanumeric wingno" name="wingno" value="<?php Echo ($addflag != 1) ? "$wing" : ""; ?>" readonly  tabindex="-1">
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg lblfloor">Floor No.</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-2 pad">                            
                            <input type="text" class="form-control form alphanumeric floorno" name="floorno" value="<?php Echo ($addflag != 1) ? "$flrno" : ""; ?>" readonly  tabindex="-1">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Flat No.</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 pad">                            
                            <input type="text" class="form-control form alphanumeric flatno" name="flatno" value="<?php Echo ($addflag != 1) ? $get['flatno'] : ""; ?>" readonly  tabindex="-1">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg blue" >No. of Months</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 blue pad">                            
                            <input type="text" class="form-control form alphanumeric nomonths " name="nomonths" value="<?php Echo ($addflag != 1) ? $get["nomonths"] : "1"; ?>" >
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">From Date</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">                            
                            <input type="date" class="form-control form frodate" name="fromdate" value="<?php Echo ($addflag != 1) ? $get["fromdate"] : ""; ?>" >
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">To Date</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">                            
                            <input type="date" class="form-control form toodate" name="todate" value="<?php Echo ($addflag != 1) ? $get["todate"] : ""; ?>" readonly>
                        </div>
                    </div>
                    
                    <div class="" style="background-color: #C99C;">
                        <div class="row">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: white">Rent</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 text-right pad">
                                <input type="number" step="any" class="form-control form text-right rent fntsiz" name="rent" value="<?php Echo ($addflag != 1) ? $get["rent"] : 0; ?>" >
                                <input type="hidden" step="any" class="form-control form text-right monrent fntsiz" name="monrent" value="<?php Echo ($addflag != 1) ? $get["monrent"] : 0; ?>" >
                            </div>
                        </div>
<!--                        <div class="row">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: white">Property Tax</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 text-right pad">
                                <input type="number" step="any" class="form-control form text-right proptax fntsiz" name="proptax" value="<?php Echo ($addflag != 1) ? $get["proptax"] : 0; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: white">Repair Cess</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 text-right pad">
                                <input type="number" step="any" class="form-control form text-right repairtax fntsiz" name="repairtax" value="<?php Echo ($addflag != 1) ? $get["repairtax"] : 0; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: white">Others</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 text-right pad">
                                <input type="number" step="any" class="form-control form text-right othertax fntsiz" name="othertax" value="<?php Echo ($addflag != 1) ? $get["othertax"] : 0; ?>" >
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: white">Total</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 text-right pad">
                                <input type="number" step="any" class="form-control form text-right taxable fntsiz" tabindex="-1" name="taxable" value="<?php Echo ($addflag != 1) ? $get["taxable"] : 0; ?>" readonly>
                            </div>
                        </div>-->
                        <div class="row gstpart">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: white">CGST</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 text-right pad">
                                <input type="number" step="any" class="form-control form text-right cgstperc fntsiz" tabindex="-1" name="cgstperc" value="<?php Echo ($addflag != 1) ? $get["cgstperc"] : 0; ?>" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2">
                                <label class="label-control marg" style="color: white">%</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 text-right pad">
                                <input type="number" step="any" class="form-control form text-right cgstamt fntsiz" tabindex="-1" name="cgstamt" value="<?php Echo ($addflag != 1) ? $get["cgstamt"] : 0; ?>" readonly>
                            </div>
                        </div>
                        <div class="row gstpart">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: white">SGST</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 text-right pad">
                                <input type="number" step="any" class="form-control form text-right sgstperc fntsiz" tabindex="-1" name="sgstperc" value="<?php Echo ($addflag != 1) ? $get["sgstperc"] : 0; ?>" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2">
                                <label class="label-control marg" style="color: white">%</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 text-right pad">
                                <input type="number" step="any" class="form-control form text-right sgstamt fntsiz" tabindex="-1" name="sgstamt" value="<?php Echo ($addflag != 1) ? $get["sgstamt"] : 0; ?>" readonly>
                            </div>
                        </div>
                        <div class="row gstpart">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: #A40353;font-size: 18px;font-weight: bold">Net Amount</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 text-right pad">
                                <input type="number" step="any" style="color: #A40353;font-size: 18px;font-weight: bold" tabindex="-1" class="form-control form text-right total fntsiz" name="total" value="<?php Echo ($addflag != 1) ? $get["total"] : 0; ?>" readonly>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Remark</label>
                        </div>
                        <div class="col-lg-7 col-md-7 col-sm-8 col-xs-8 text-right pad">
                            <input type="text" class="form-control form" name="narr1" value="<?php Echo ($addflag != 1) ? $get["narr1"] : ""; ?>" >
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                        </div>
                        <div class="col-lg-7 col-md-7 col-sm-8 col-xs-8 text-right pad">
                            <input type="text" class="form-control form" name="narr2" value="<?php Echo ($addflag != 1) ? $get["narr2"] : ""; ?>" >
                        </div>
                    </div>
                    
                </div>
            </div>
            <div class="panel-footer">
                <?php include("includes/buttons.php"); ?>
            </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">
                <div class="row">
                    <div class="col-lg-2">
                        <select class="form-control sfield">
                            <option value="1">Party</option>
                            <option value="2">Date</option>
                            <option value="3">Bill No.</option>
                            <option value="4">Month No.</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">Party</option>
                            <option value="2">Date</option>
                            <option value="3">Bill No.</option>
                            <option value="4">Month No.</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="35%">Party</th>
                                <th width="10%">Date</th>
                                <th width="10%">Bill No.</th>
                                <th width="10%">Month No.</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>
