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

$formname = 'Donation for Samuh Lagan';
$maindb = 'donsamlagan';
$file = "donsamlaganreg_fr.php";

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

$name =$_POST['party'];
$type =$_POST['type'];

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

$typecond = "";
$partycond = "";


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($type)) {
    $typecond = " AND `type`='$type ' ";
}


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


$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt FROM $maindb WHERE vouchno != ''
                                        AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  
                                        $partycond $typecond ")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:7%">Voucher</th>
                                <th style="width:10%">Date</th>
                                <th style="width:5%;">Code</th>
                                <th style="width:14%;">Name</th>
                                <th style="width:7%;" class="text-right">Amount</th>
                                <th style="width:10%;">Gift</th>
                                <th style="width:5%;" class="text-right">Qty</th>
                                <th style="width:20%;">Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                         
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[vouchdt]</td>";
                                Echo "<td>$pmain[memcode]</td>";
                                Echo "<td>$pmain[memname]</td>";
                                Echo "<td style='text-align:right'>$pmain[amount]</td>";
                                Echo "<td>$pmain[item]</td>";
                                Echo "<td style='text-align:right'>$pmain[qty]</td>";
                                Echo "<td>$pmain[remark1]  $pmain[remark2]</td>";

                                $mgtot = $pmain['amount'] + $mgtot;
                                $mgqty = $pmain['qty'] + $mgqty;

                                $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='font-weight:bold;text-align:right;'>$amt</td>";
                            Echo "<td></td>";
                            Echo "<td style='font-weight:bold;text-align:right;'>$mgqty</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,From,Account,Amount,Remark\n";

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

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

        $ctr++;
        $pvouchno = $pmain['vouchno'];
        $pdate = $pmain['vouchdt'];
        $bankname = $pmain['bankname'];
        $account = $pmain['account'];
        $amount = $pmain['amount'];
        $premark = $pmain['remark1'] . $pmain['remark2'];

        $contents .= $ctr . ",";
        $contents .= $pvouchno . ",";
        $contents .= $pdate . ",";
        $contents .= $bankname . ",";
        $contents .= $account . ",";
        $contents .= $amount . ",";
        $contents .= $premark . "\n";

        $mtqty = $pmain[qty] + $mtqty;
        $mgtot = $pmain[amount] + $mgtot;

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