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

$maindb = "sagai";
$formname = 'Sagai Register';
$file = "sagaireg_fr.php";

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

$sonname = $_POST["sonname"];
$dauname = $_POST["dauname"];

$mfrmdate = trim($_POST["fromdt"]);
$mtodate = trim($_POST["todt"]);


$sonnamecond = "";


if (empty($_POST["fromdt"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}

if (!empty($sonname)) {
    $sonnamecond = " AND `sonname`='$sonname ' ";
}
if (!empty($dauname)) {
    $daunamecond = " AND `dauname`='$dauname ' ";
}



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

if (isset($_POST["view"])) {
include("includes/linkshead.php");

    ?>
    <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:4%">Voucher</th>
                                <th style="width:7%">Date</th>
                                <th style="width:12%;">Father Name</th>
                                <th style="width:12%;">Son Name</th>
                                <th style="width:5%;text-align:right">Amount</th>
                                <th style="width:12%;">Father Name</th>
                                <th style="width:12%;">Daugther Name</th>
                                <th style="width:5%;text-align:right">Amount</th>
                                <th style="width:10%;">English Date</th>
                                <th style="width:20%;">Nikah Location</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $sontot = 0;
                            $dautot = 0;

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

                                $time = date('h:i A', strtotime($pmain['engtime']));

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

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[vouchdt]</td>";
                                echo "<td>$pmain[memno]  $pmain[memname]</td>";
                                echo "<td>$pmain[sonno]  $pmain[sonname]</td>";
                                echo "<td style='text-align:right;'>$pmain[sonamt]</td>";
                                echo "<td>$pmain[fathno]  $pmain[fathname]</td>";
                                echo "<td>$pmain[dauno] $pmain[dauname]</td>";
                                echo "<td style='text-align:right;'>$pmain[dauamt]</td>";
                                echo "<td>$pmain[englishdt] <br> $pmain[engday] <br> $time</td>";
                                echo "<td>$pmain[add1]   $pmain[add2] </td>";

                                $sontot = $pmain['sonamt'] + $sontot;
                                $dautot = $pmain['dauamt'] + $dautot;

                                $sonamt = number_format($sontot, 2);
                                $dauamt = number_format($dautot, 2);

                                echo "</tr>";
                            }
                            echo "<tr id='boldfont'>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td style='text-align:right;'>Total</td>";
                            echo "<td style='text-align:right;'>$sonamt</td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td style='text-align:right;'>$sonamt</td>";
                            echo "<td></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, Father Name, Son Name, Amount, Father Name, Daughter Name, Amount, English Date, Nikah Location\n";

$ctr = 0;
$sontot = 0;
$dautot = 0;

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

    $time = date('h:i A', strtotime($pmain['engtime']));

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

    $ctr++;

$contents .="$pmain[vouchno]" . ",";
$contents .="$pmain[vouchdt]" . ",";
$contents .="$pmain[memno]" . " " ."$pmain[memname]" . ",";
$contents .="$pmain[sonno]" . " " . "$pmain[sonname]" . ",";
$contents .="$pmain[sonamt]" . ",";
$contents .="$pmain[fathno]" . " " . "$pmain[fathname]" . ",";
$contents .="$pmain[dauno]" . " " . "$pmain[dauname]" . ",";
$contents .="$pmain[dauamt]" . ",";
$contents .="$pmain[englishdt]" . " " . "$pmain[engday]" . " " . "$time" . ",";
$contents .="$pmain[add1]" . "" . "$pmain[add2]" . "".  "\n";


$sontot = $pmain['sonamt'] + $sontot;
    $dautot = $pmain['dauamt'] + $dautot;

    $sonamt = number_format($sontot, 2);
    $dauamt = number_format($dautot, 2);

}
$contents .="".",";
$contents .="".",";
$contents .="".",";
$contents .="Total".",";
$contents .="$sontot".",";
$contents .="".",";
$contents .="".",";
$contents .="$sontot ".",";
$contents .="".",";
$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;
    }
}
?>