<?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"];

$maindb = 'accmast';
$trandb = 'actran';
$formname = 'Expense  Register';

$file = 'expreg_fr.php';
$entfile = "expense.php";

$expense = $_POST["expense"];
$amount = $_POST["amount"];
$source = $_POST["source"];

$dtype = $_POST["dtype"];
$para = $_POST["para"];
// browse ($para);
$gcond = '';
$mcond = '';
$expcond = '';


if (!empty($group)) {
    $gcond = " AND `grpcode`='$group' ";
}

if (!empty($acname)) {
    $mcond = " AND `accode`='$acname' ";
}

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


if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; expreg_dtl_fr.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; expreg_dtl_fr.php");
}

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

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));


$getpmain = mysqli_query($conn, "select * from `accmast` where accode != '' $gcond $mcond $expcond and grpcode in (select grpcode from grpmast where UPPER(type) = 'E' and UPPER(subtype) = 'E' ) order by name ")or die(mysqli_error($conn));
//$getno = 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; 
        }
        .colr{
            background: #26A69A;
            font-size: 17px;
            color:white;
        }

    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="col-lg-12 pad">
                <table style="width:100%;background: white;" class="table table-condensed table-striped table-bordered table-hover">
                    <thead>
                        <tr style="background-color:#b91646">
                            <th style="width:10%;">Sr.No</th>
                            <th style="width:30%;">Name </th>
                            <th style="width:10%;" class='text-right'>Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $totamt = 0;

                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getexp = mysqli_query($conn, "Select accode, sum(amount) as tamt from `actran` where `accode` = '$pmain[accode]' and forac = '$para'  GROUP BY accode")or die(mysqli_error($conn));

                            while ($cmain = mysqli_fetch_array($getexp)) {

                                $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'")or die(mysqli_error($conn));
                                $showparty = mysqli_fetch_array($getparty);

                                $mamt = abs($cmain['tamt']);
                                
                                $ctr++;
                                Echo "<tr>";
                                Echo "<td>$ctr</td>";
                                Echo "<td><a href='expreg_dtl_fr.php?expense=$cmain[accode]&frmdt=$mfrmdate&todt=$mtodate&amount=$mamt&source=$source' target='_blank'><b style='font-size:16px;font-weight:bolder'>$showparty[name]</b></a></td>";
                                Echo "<td class='text-right' style='font-size:15px'>$mamt</td>";
                                Echo "</tr>";
                                $totamt = $totamt + $mamt;
                            }
                        }
                        Echo "<tr>";
                        Echo "<td></td>";
                        Echo "<td class='text-right'  style='font-size:15px'><b>Total</b></td>";
                        Echo "<td class='text-right'  style='font-size:15px'><b>$totamt</b></td>";
                        Echo "</tr>";
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .= "Sr.No,Name,Amount\n";

                        $ctr = 0;
                        $totamt = 0;

                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getexp = mysqli_query($conn, "Select accode, sum(amount) as tamt from `actran` where `accode` = '$pmain[accode]' and forac = '$para' GROUP BY accode")or die(mysqli_error($conn));

                            
                            while ($cmain = mysqli_fetch_array($getexp)) {

                                $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'")or die(mysqli_error($conn));
                                $showparty = mysqli_fetch_array($getparty);


                                $mamt = abs($cmain['tamt']);
                                
                                $ctr++;

                                $contents .= "$ctr" . ",";
                                $contents .= "$showparty[name]" . ",";
                                $contents .= "$mamt" . "\n";
                              
                                $totamt = $totamt + $mamt;
                            }
                        }

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