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

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

$formname = 'Expense Summary';
$file = 'expheadsummrep_fr.php';

$expense = $_POST["expense"];
$ptype = $_POST["ptype"];
$payee = $_POST["payee"];

$expcond = '';
$ptypecond = '';

if (!empty($expense)) {
    $expcond = "AND a.`expense`= '$expense'";
}

if (!empty($ptype)) {
    $ptypecond = " AND a.`paidby`= '$ptype'";
}


if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
    
} else {

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

    if (empty($_POST["frmdt"])) {
        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>";
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

$getpmain2 = mysqli_query($conn, "SELECT a.*, sum(a.amount) AS tamt, b.name  
                                  FROM `expentry_pe` a
                                  LEFT JOIN accmast_pe b ON a.expense = b.accode
                                  WHERE a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' 
                                  $expcond $ptypecond GROUP BY a.expense ORDER BY a.vouchdt") or die(mysqli_error($conn));
$numrecs = mysqli_num_rows($getpmain2);

if (isset($_POST["view"]) || ($_GET["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-fluid">
            <div class="col-lg-6 pad">
                <?php
                include("includes/head_rep2.php");
                ?>
                <div class="row">
                    <table class="table table-condesed table-bordered">
                        <tr>
                            <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                Total No. of Record :&nbsp;&nbsp;<b><?php echo $numrecs; ?></b>
                            </td>
                        </tr>
                    </table>

                    <table style="background: white;width: 100%"
                        class="table table-condensed table-striped table-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th width="3%;">Sr</th>
                                <th width="22%;">Expense</th>
                                <th width="8%;" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $pctr = $tqty = $tamt = 0;

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

                                $mydt = $pmain["mydt"];
                                $expname = $pmain['name'];
                                $amount = $pmain["tamt"];
                                $remark = $pmain['remark'];

                                $pctr++;
                                echo "<tr>";
                                echo "<td>$pctr</td>";
                                echo "<td>$expname</td>";
                                echo "<td class='text-right'>$amount</td>";
                                echo "</tr>";

                                $totqty += $qty;
                                $tamt += $amount;
                            }

                            echo "<tr style='background-color: #98F9F0'>";
                            echo "<td></td>";
                            echo "<td class='text-right'><b>Total</b></td>";
                            echo "<td class='numeric text-right'><b>" . number_format($tamt,2) . "</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <?php
}

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

    $contents .= "Total No. of Records : $numrecs \n";

    $contents .= "Sr., Expense, Amount \n";

    $pctr = $tqty = $tamt = 0;

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

        $mydt = $pmain["mydt"];
        $expname = $pmain['name'];
        $amount = $pmain["amount"];

        $pctr++;
        $contents .= "$pctr" . ",";
        $contents .= "$expname" . ",";
        $contents .= "$amount" . "\n";

        $tamt += $amount;
    }

    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$tamt" . "\n";

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }
}

?>