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

$maindb = "deposit";
$formname = "Deposit Register";
$file = "deporeg_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);


$party = $_POST["party"];
$partycond = "";
if (!empty($party)) {
    $partycond = " AND `accode`='$party' ";
}

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; purreg.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; purreg.php");
}

$getdepo = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt , DATE_FORMAT(`rtndate`, '%d-%m-%Y') as rtndate FROM `$maindb`
                         WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond")or die(mysqli_error($conn));
$get = mysqli_num_rows($getdepo);
//print table($getdepo);
if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    <style src="MyStyle/repdtl.css" type="text/css"></style>
    </head>
    <body>
        <div class="container" style="">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad" >
                    <table style="background: white; width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th width="10%">Date</th>
                                <th width="5%">Voucher</th>
                                <th width="30%">Name</th>
                                <th width="10%">Flat</th>
                                <th width="10%">Type</th>
                                <th width="10%" style="text-align: right;">Deposit Amt.</th>
                                <th width="10%">Return Date</th>
                                <th width="10%">Returned Amount</th>
                                <th width="10%" class=text-right>Difference</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totdepo = 0; 
                           
                            

                            while ($dmain = mysqli_fetch_array($getdepo)) {
                                $gettenant = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$dmain[accode]'")or die(mysqli_error($conn));
                                $ftenant = mysqli_fetch_array($gettenant);
                                $tenant = $ftenant['name'];
                                
                                
                                if ($dmain['type'] == N){
                                
                                $tp='Normal';
                                
                                 }else if ($dmain['type'] == '') {
                                 $tp='';
                                    }
                                    else{
                                
                                        $tp='Heavy';
                                        }
                                
//                            browse($tp);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$dmain[mydt]</td>";
                                Echo "<td>$dmain[vouchno]</td>";
                                Echo "<td>$tenant</td>";
                                Echo "<td>$dmain[flatno]</td>";
                                Echo "<td>$tp</td>";
                                Echo "<td style='text-align: right;'>$dmain[deposit]</td>";
                                Echo "<td>$dmain[rtndate]</td>";
                                Echo "<td style='text-align: right;'>$dmain[deporet]</td>";
                                Echo "<td style='text-align: right;'>$dmain[diff]</td>";
                                Echo "</tr>";
                                $totdepo = $totdepo + $dmain[deposit];
                                $totdeporet = $totdeporet + $dmain[deporet];
                                $totdiff = $totdiff + $dmain[diff];
                            }

                            Echo "<tr  style='font-weight: bolder; text-align: right; color: red;'>";
                            Echo "<td colspan='5'><b>Total</b></td>";
                            Echo "<td><b>$totdepo</b></td>";
                            Echo "<td><b></b></td>";
                            Echo "<td><b>$totdeporet</b></td>";
                            Echo "<td><b>$totdiff</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

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


//    browse($getdepo);
    $contents .= "Date,Voucher,Name,Flat,Type,Deposit Amt,Return Date,Returned Amount,Difference.\n";

    $ctr = 0;
    $totdepo = 0;

    while ($dmain = mysqli_fetch_array($getdepo)) {
        $gettenant = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$dmain[accode]'")or die(mysqli_error($conn));
        $ftenant = mysqli_fetch_array($gettenant);
        $tenant = $ftenant[name];
                                   if ($dmain['type'] == N){
                                
                                $tp='Normal';
                                
                                 }else if ($dmain['type'] == '') {
                                 $tp='';
                                    }
                                    else{
                                
                                        $tp='Heavy';
                                        }

        $ctr++;

        $contents .= "$dmain[mydt]" . ",";
        $contents .= "$dmain[vouchno]" . ",";
        $contents .= "$tenant" . ",";
        $contents .= "$dmain[flatno]" . ",";
        $contents .= "$tp" . ",";
        $contents .= "$dmain[deposit]" . ",";
        $contents .= "$dmain[rtndate]" . ",";
        $contents .= "$dmain[deporet]" . ",";
        $contents .= "$dmain[diff]" . "\n";

        $totdepo = $totdepo + $dmain[deposit];
        $totdeporet = $totdeporet + $dmain[deporet];
        $totdiff = $totdiff + $dmain[diff];
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totdepo" . ",";
    $contents .= "" . ",";
    $contents .= "$totdeporet" . ",";
    $contents .= "$totdiff" . ",";


    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>
