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

$maindb = 'donationrecvd';

$formname = "Donation Received Register";
$file = "donationrecvdreg_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];

$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";

mysqli_select_db($conn, $zdb);

$supp = $_POST["supp"];
$branch = $_POST["company"];
$bank = $_POST["bank"];

$suppcond = "";
$branchcond = "";
$bankcond = "";

$mfrmdate = trim($_POST["fromdt"]);
$mtodate = trim($_POST["todt"]);
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($supp)) {
    $suppcond = " AND `party` = '$supp' ";
}

if (!empty($branch)) {
    $branchcond = " AND `compname`='$branch' ";
}

if (!empty($bank)) {
    $bankcond = " AND `bankname`='$bank' ";
}

$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt FROM `$maindb` WHERE
                                  vouchno !='' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $keycond
                                   $branchcond $bankcond $suppcond ") or die(mysqli_error($conn));


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

    include("includes/linkshead.php");
?>
    <style>
        @media print {

            .no-print,
            .no-print * {
                display: none !important;
            }
        }

        .th1 {
            background-color: #78909C;
        }

        .th2 {
            background-color: #85AFC3;
        }
    </style>
    </head>

    <body>
        <div class="container">

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


            <div class="col-lg-12 col-sm-12 pad">
                <table style="width:100%;background: white;" class="table table-condensed table-hover table-striped table-bordered">
                    <thead>
                        <tr class="mySearchTr">
                            <th style="width:3%;">Sr</th>
                            <th style="width:6%;">Voucher</th>
                            <th style="width:7%;">Date</th>
                            <th style="width:10%;">Bank/Cash  Name</th>
                            <th style="width:10%;">Account Name</th>
                            <th style="width:7%;text-align: right">Amount</th>
                            <th style="width:10%" colspan="2" >Remarks</th> 
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $gtot = 0;


                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $gparty = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[party]'") or die(mysqli_error($conn));
                            $sparty = mysqli_fetch_array($gparty);

                            $gbank = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[bankname]'") or die(mysqli_error($conn));
                            $sbank = mysqli_fetch_array($gbank);

                            $ctr++;

                            echo "<tr>";
                            echo "<td><b>$ctr</b></td>";
                            echo "<td><b>$pmain[vouchno]</b></td>";
                            echo "<td><b>$pmain[vouchdt]</b></td>";
                            echo "<td><b>$sbank[name]</b></td>";
                            echo "<td><b>$sparty[name]</b></td>";
                            echo "<td class='text-right'><b>$pmain[amount]</b></td>";
                            echo "<td class=''><b>$pmain[remark1] $pmain[remark2]</b></td>";
                            echo "</tr>";

                            $gtot = $gtot + $pmain['amount'];
                        }
                        echo "<tr>";
                        echo "<td class='text-right text-danger' colspan='5'><b>Total</b></td>";
                        echo "<td class='text-right'><b>$gtot</b></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php include("includes/linksfoot.php"); ?>
    </body>

    </html>
<?php
}


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

        $contents .= "Sr,Voucher,Date,Bank/Cash,Account Name,Amonut,Remarks\n";
        $ctr = 0;
        $gtot = 0;


        while ($pmain = mysqli_fetch_array($getpmain)) {
            $gparty = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[party]'") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);

            $gbank = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[bankname]'") or die(mysqli_error($conn));
            $sbank = mysqli_fetch_array($gbank);

    $ctr++;

    $contents .= "$ctr" . ",";
    $contents .= "$pmain[vouchno]" . ",";
    $contents .= "$pmain[vouchdt]" . ",";
    $contents .= "$sbank[name]" . ",";
    $contents .= "$sparty[name]" . ",";
    $contents .= "$pmain[amount]" . ",";
    $contents .= "$pmain[remark1]" . "". "$pmain[remark2]".  "\n";

    $gtot = $gtot + $pmain['amount'];
}
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$gtot" . "\n";

    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
    print $contents;
}
