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

$maindb = "actran";
$formname = "Balnce Sheet Report";
$file = "balsheet_zak_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$fseries = $_SESSION["series_" . $dbname];
$seriescond = "AND `series`='$fseries'";
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

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


if (empty($_POST["frdt"])) {
    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>";
}


$grtn = mysqli_query($conn, "SELECT sum(amount) as tamt FROM `ramzankit`")or die(mysqli_error($conn));
$frtn = mysqli_fetch_array($grtn);
$rtnamt = $frtn['tamt'];

//$getinc = mysqli_query($conn, "SELECT SUM(amount) as lamt FROM `zakatinbank` WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond 
//                               UNION ALL
//                               SELECT SUM(amount) as lamt FROM `zakatincash` WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond ") or die(mysqli_error($conn));
//$pinc = mysqli_fetch_array($getinc);
//$lamt = $pinc['lamt'];
//
//
//$gexp = mysqli_query($conn, "SELECT SUM(amount) as ramt FROM `widowpay` WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond 
//                             union all
//                             SELECT SUM(sancamt) as ramt FROM `medihelp` WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond 
//                             union all
//                             SELECT SUM(sancamt) as ramt FROM `feesapp` WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond 
//                             union all
//                             SELECT SUM(sancamt) as ramt FROM `finanhelp` WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond ") or die(mysqli_error($conn));
//$fetexp = mysqli_fetch_array($gexp);
//$ramt = $fetexp['ramt'];
//
//$fexp = $ramt;
//
//$sumamt = $lamt - $fexp;



$getexpenses = mysqli_query($conn, "SELECT amount,party as memno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as date, 'Widow Pay' as source FROM widowpay WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond
                                  union all
                                  SELECT sancamt as amount,memno,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as date, 'Finance Help' as source FROM finanhelp WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond
                                  union all
                                  SELECT '$rtnamt' as amount,memno,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as date, 'Ramzan Ration' as source FROM rationdonate WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond
                                  union all
                                  SELECT sancamt as amount,memno,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as date, 'Fees Application' as source FROM feesapp WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond
                                  union all
                                  SELECT sancamt as amount,memno,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as date, 'Medical Help' as source FROM medihelp WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond") or die(mysqli_error($conn));

$getincome = mysqli_query($conn, "SELECT amount,memno,'Cash' as source, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as date FROM zakatinbank WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond
                                  union all
                                  SELECT amount,memno,'Bank' as source,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as date FROM zakatincash WHERE vouchdt >= '$mfrmdate' AND vouchdt <= '$mtodate' $seriescond") or die(mysqli_error($conn));


// $get = mysqli_num_rows($getpmain);

include("includes/linkshead.php");

if (isset($_POST["view"])) {
    ?>
    <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 class="table table-condensed table-hover " style="width : 100%" valign="top">
                        <tr>
                            <td style="vertical-align: top;width: 50%;">
                                <div style="font-size: 15px;font-weight:bold"> &nbsp; Income</div>
                                <table class="table table-condensed table-hover table-bordered" style="float: left; margin: 2px">
                                    <thead class="mySearchTr">
                                        <tr>
                                            <th style="width:8%">Date</th>
                                            <th style="width:5%">Source</th>
                                            <th style="width:25%;">Account</th>
                                            <th style="width:12%;text-align:right">Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $ctr = 0;
                                        $sontot = 0;
                                        $totamt = 0;

                                        while ($pincome = mysqli_fetch_array($getincome)) {

                                            $gname = mysqli_query($conn, "SELECT accode,name FROM $zroot.`member` where accode = '$pincome[memno]' ")or die(mysqli_error($conn));
                                            $fname = mysqli_fetch_array($gname);

                                            $name = $fname['name'];

                                            $incomeamt = number_format($pincome['amount'], 2);

                                            $ctr++;
                                            echo "<tr id='boldfont'>";
                                            echo "<td>$pincome[date]</td>";
                                            echo "<td>$pincome[source]</td>";
                                            echo "<td>$name</td>";
                                            echo "<td style='text-align:right;'>$incomeamt</td>";

                                            $intot = $pincome['amount'] + $intot;

                                            $totamt = number_format($intot, 2);

                                            echo "</tr>";
                                        }
                                        echo "<tr id='boldfont' style='font-weight:bold'>";

                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "<td style='text-align:right;'>Total</td>";
                                        echo "<td style='text-align:right;'>$totamt</td>";
                                        echo "</tr>";
                                        ?>
                                    </tbody>
                                </table>
                            </td>


                            <td style="vertical-align: top;width: 50%;">
                                <div style="font-size: 15px;font-weight:bold"> &nbsp; Expenses</div>
                                <table class="table table-condensed table-hover table-bordered" style="float: left; margin: 2px">
                                    <thead class="mySearchTr">
                                        <tr>
                                            <th style="width:8%">Date</th>
                                            <th style="width:5%">Source</th>
                                            <th style="width:25%;">Account</th>
                                            <th style="width:12%;text-align:right">Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $ctr = 0;
                                        $sontot = 0;
                                        $amttot = 0;

                                        while ($pexp = mysqli_fetch_array($getexpenses)) {

                                            $gname = mysqli_query($conn, "SELECT accode,name FROM $zroot.`member` where accode = '$pexp[memno]'")or die(mysqli_error($conn));
                                            $fname = mysqli_fetch_array($gname);

                                            $ename = $fname['name'];

                                            $absamt = abs($pexp['amount']);

                                            $amount = number_format($absamt, 2);

                                            $ctr++;
                                            echo "<tr id='boldfont'>";
                                            echo "<td>$pexp[date]</td>";
                                            echo "<td>$pexp[source]</td>";
                                            echo "<td>$ename</td>";
                                            echo "<td style='text-align:right;'>$amount</td>";
                                            echo "</tr>";

                                            $exptot = $pexp['amount'] + $exptot;

                                            $amttot = number_format(abs($exptot), 2);
                                        }


                                        echo "<tr id='boldfont'>";
                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "<td style='text-align:right;'>Total</td>";
                                        echo "<td style='text-align:right;'>$amttot</td>";
                                        echo "</tr>";
                                        ?>
                                    </tbody>
                            </td>
                    </table>

                    </tr>
                    <tr>
                        <?php
                        $sumamt = $intot - $exptot;
                        if ($sumamt < 0) {
                            $smamt = abs($sumamt);
                            echo "<tr id='boldfont' style='background-color:#FFFF00;color:red'>";
//                            echo "<td></td>";
//                            echo "<td></td>";
                            echo "<td style='text-align:right;'>Balance</td>";
                            echo "<td style='text-align:right;'>" . number_format($smamt, 2) . "</td>";
                            echo "</tr>";
                        } else {
                            echo "<tr id='boldfont' style='background-color:#FFFF00;color:red'>";
//                            echo "<td></td>";
//                            echo "<td></td>";
                            echo "<td style='text-align:right;'>Balance</td>";
                            echo "<td style='text-align:right;'>" . number_format($sumamt, 2) . "</td>";
                            echo "</tr>";
                        }
                        ?>

                    </tr>

                    </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,Daugther 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 .= "$address" . "\n";

        $sontot = $pmain['sonamt'] + $sontot;
        $dautot = $pmain['dauamt'] + $dautot;
    }
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$sontot" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "$dautot" . ",";
    $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;
    }
}
?>