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

$maindb = "subscription";
$formname = "Subscription Receipt Register";
$file = "subscriptionreg_fr.php";

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

$vouchdt = $_POST["voudate"];
$eventdt = $_REQUEST["event"];
$hallname = $_POST["hallname"];
$partyname = $_REQUEST["party"];
$staff = $_REQUEST["staffs"];

$datecond = "";
$eventdatecond = "";
$hallcond = "";
$partycond = "";
$staffcond = "";

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

if (!empty($vouchdt)) {
    $datecond = " AND `vouchdt`='$vouchdt' ";
}
if (!empty($eventdt)) {
    $eventdatecond = " AND `eventdt`='$eventdt ' ";
}

if (!empty($hallname)) {
    $hallcond = " AND `hall`='$hallname' ";
}

if (!empty($partyname)) {
    $partycond = " AND `name`='$partyname' ";
}

if (!empty($staff)) {
    $staffcond = " AND `staff`='$staff' ";
}

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt FROM $maindb WHERE vouchno != '' ")or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

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>

    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:7%">Voucher</th>
                                <th style="width:10%">Date</th>
                                <th style="width:10%;">Member No.</th>
                                <th style="width:17%;">Member Name</th>
                                <th style="width:10%;" class="text-right">Subscription</th>
                                <th style="width:20%;">Narration</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {



                                $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                // Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[vouchdt]</td>";
                                Echo "<td>$pmain[memno]</td>";
                                Echo "<td>$pmain[memname]</td>";
                                Echo "<td style='text-align:right'>$pmain[subscription]</td>";
                                Echo "<td>$pmain[narr1]   $pmain[narr2] </td>";

                                $mgtot = $pmain['subscription'] + $mgtot;

                                $amt = number_format($mgtot, 2);

                                Echo "</tr>";
                            }
                            Echo "<tr id='boldfont'>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td style='font-weight:bold;text-align:right;'>Total</td>";
                            Echo "<td style='text-align:right'>$amt</td>";
                            Echo "<td> </td>";

                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
   
$contents .="Voucher, Date, Member No., Member Name, Subscription, Narration \n";

$ctr = 0;
$mtqty = 0;
$mgtot = 0;

while ($pmain = mysqli_fetch_array($getpmain)) {



    $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

    $ctr++;
$contents .="$pmain[vouchno]" . ",";
$contents .="$pmain[vouchdt]" . ",";
$contents .="$pmain[memno]" . ",";
$contents .="$pmain[memname]" . ",";
$contents .="$pmain[subscription]" . ",";
$contents .="$pmain[narr1]" . " " . "$pmain[narr2]". "\n";

$mgtot = $pmain['subscription'] + $mgtot;
}    
$amt = number_format($mgtot, 2);
$contents .="" . ",";
    $contents .="" . ",";
    $contents .="" . ",";
    $contents .="Total" . ",";
    $contents .="$mgtot" . ",";
    $contents .="" . "\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;
    }
}
?>