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

$maindb = "commiss";
//$trandb = "folwtran";
$formname = "Commission Register";
$file = "commiss_print_fr.php";
$entfile = "commiss.php";

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

//$howknowcond = '';
if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; commiss_print.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; commiss_print.php");
}
$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$mode = $_POST['mode'];
$name = $_POST['name'];
$roomtype = $_POST['roomtype'];

$modecond = "";
$namecond = "";
$roomcond = "";

if (!empty($mode)) {
    $modecond = " AND `mode`='$mode' ";
}
if (!empty($name)) {
    $namecond = " AND `name`='$name' ";
}
if (!empty($roomtype)) {
    $roomcond = " AND `roomtype`='$roomtype' ";
}

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchno` !='' AND `vouchdt` >= '$mfrmdate' 
                                    AND `vouchdt` <= '$mtodate'  $modecond $namecond $roomcond ORDER BY `vouchno`")or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    </head>
    <body>
        <div class="container">

            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-bordered table-condensed">
                        <tr class="mySearchTr" >
                            <th>Sr.</th>
                            <th>Voucher</th>
                            <th>Date</th>
                            <th>Party Name</th>
                            <th>Contact No.</th>
                            <th>Room Type</th>
                            <th>Building No.</th>
                            <th>Flat No.</th>
                            <th>Deal </th>
                            <th>Token</th>
                            <th>Mode</th>
                            <th>Salesman</th>
                            <th>Commission</th>
                        </tr>
                        <?php
                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            $interest = $pmain['interest'];
                            $mode = $pmain['mode'];

                            if ($interest == 'Y') {
                                $interest = 'Yes';
                            } else {
                                $interest = 'No';
                            }

                            if ($mode == 'F') {
                                $mode = 'Full Down Payment';
                            } else {
                                $mode = 'Bank Loan';
                            }

                            $ctr++;
                            Echo "<tr class=' clickable-row2' data-href='$entfile?mode=edit&vouchno=$pmain[vouchno]' [target]='_blank'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[vouchno]</td>";
                            Echo "<td>$pmain[mydt]</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td>$pmain[contact]</td>";
                            Echo "<td>$pmain[roomtype]</td>";
                            Echo "<td>$pmain[bldg]</td>";
                            Echo "<td>$pmain[flatno]</td>";
                            Echo "<td>$pmain[deal]</td>";
                            Echo "<td>$pmain[token]</td>";
                            Echo "<td>$mode</td>";
                            Echo "<td>$pmain[salesman]</td>";
                            Echo "<td>$pmain[commission]</td>";
                            Echo "</tr>";


//                            if ($_POST["dtype"] == 2) {
//
//                                Echo "<tr style='height:10px;'>";
//                                Echo "<td colspan='25'>";
//                                Echo "<table style='width:100%;' id='tabital' class='table table-condensed  table-bordered'>";
//                                Echo "<thead>";
//                                Echo "<tr class='' style='background-color:#BABFC2'>";
//                                Echo "<th style='width:5%;'>Sr</th>";
//                                Echo "<th style='width:8%;'>Date</th>";
//                                Echo "<th style='width:40%;'>Description</th>";
//                                Echo "<th style='width:8%;'>Reminder Date</th>";
//                                Echo "</tr>";
//                                Echo "<thead>";
//
//                                $ctr2 = 0;
//
//                                $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`trandt`,'%d-%m-%Y') as trandt ,DATE_FORMAT(`remdate`,'%d-%m-%Y') as remdate FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
//                                while ($tran = mysqli_fetch_array($gettran)) {
//
//
//                                    $ctr2++;
//                                    Echo "<tr>";
//                                    Echo "<td>$ctr2</td>";
//                                    Echo "<td>$tran[trandt]</td>";
//                                    Echo "<td>$tran[descrip]</td>";
//                                    Echo "<td>$tran[remdate]</td>";
//                                    Echo "</tr>";
//                                }
//                            }
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

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

    $contents .= "Sr., Voucher, Date, Party Name, Contact No., Room Type, Building No., Flat No., Deal, Token, Mode, Salesman, Commission \n";

    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $interest = $pmain['interest'];

        if ($interest == 'Y') {
            $interest = 'Yes';
        } else {
            $interest = 'No';
        }

        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[name]" . ",";
        $contents .= "$pmain[contact]" . ",";
        $contents .= "$pmain[roomtype]" . ",";
        $contents .= "$pmain[bldg]" . ",";
        $contents .= "$pmain[flatno]" . ",";
        $contents .= "$pmain[deal]" . ",";
        $contents .= "$pmain[token]" . ",";
        $contents .= "$pmain[mode]" . ",";
        $contents .= "$pmain[salesman]" . ",";
        $contents .= "$pmain[commission]" . "\n";

//        if ($_POST["dtype"] == 2) {
//
//
//
//            $contents .= "Sr., Date, Description, Reminder Date\n";
//
//            $ctr2 = 0;
//
//            $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`trandt`,'%d-%m-%Y') as trandt ,DATE_FORMAT(`remdate`,'%d-%m-%Y') as remdate FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
//            while ($tran = mysqli_fetch_array($gettran)) {
//
//
//                $ctr2++;
//                $contents .= "$ctr2" . ",";
//                $contents .= "$tran[trandt]" . ",";
//                $contents .= "$tran[descrip]" . ",";
//                $contents .= "$tran[remdate]" . "\n";
//
//              
//            }
//        }
    }
    $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;
    }
}
?>