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

$maindb = 'bankpay';
$actran = 'actran';
$formname = "Expense report";
$file = "expenserep_fr.php";

ini_set('display_errors', 1);
error_reporting(E_ERROR);

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




$cust = $_POST["cust"];
$fdate = $_POST["fdate"];
$tdate = $_POST["tdate"];
$orderby = $_POST["orderby"];

$gcond = '';
$mcond = '';
$cucond = '';
$stcond = '';
$fdatecond = '';
$tdatecond = '';

if (!empty($cust)) {
    $cucond = " AND `party`='$cust' ";
}
$dcond = " and `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate' ";

if (empty($fdate)) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; expenserep.php");
}

if (empty($tdate)) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; expenserep.php");
}

if ($orderby == "1") {
    $getcash = mysqli_query($conn, "SELECT party,count(*) as norec, sum(netamt) as tamt FROM `rawmain` 
                                               WHERE party != '' $cucond $dcond group by party order by norec desc") or die(mysqli_error($conn));
} else {
    $getcash = mysqli_query($conn, "SELECT party,count(*) as norec, sum(netamt) as tamt FROM `rawmain` 
                                               WHERE party != '' $cucond $dcond group by party order by tamt desc") or die(mysqli_error($conn));
}

$mfdt = date("d-m-Y", strtotime($fdate));
$mtdt = date("d-m-Y", strtotime($tdate));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    <style>
        @media print {

            .no-print,
            .no-print * {
                display: none !important;
            }
        }
    </style>
    </head>

    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            ?>
            <div class="row">
                <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                    <label> <?php echo $cashsale; ?></label>
                </div>
            </div>
            <div class="row">

                <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:#78909C">
                                <th style="width:10%;">Sr.No</th>
                                <th style="width:10%;">Visit</th>
                                <th style="width:40%;">Name </th>
                                <th style="width:30%;" class='text-right'>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            while ($cmain = mysqli_fetch_array($getcash)) {

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


                                $mamt = $cmain['tamt'];
                                $ctr++;
                                echo "<tr>";
                                echo "<td>$ctr</td>";
                                echo "<td>$cmain[norec]</td>";
                                echo "<td>$showparty[name]</td>";
                                echo "<td class='text-right'>$mamt</td>";
                                echo "</tr>";
                                $totamt = $totamt + $mamt;
                            }

                            echo "<tr>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td class='text-right'><b>Total</b></td>";
                            echo "<td class='text-right'><b>$totamt</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php
        include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}

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

    $cust = $_POST["cust"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $orderby = $_POST["orderby"];

    $gcond = '';
    $mcond = '';
    $cucond = '';
    $stcond = '';
    $fdatecond = '';
    $tdatecond = '';

    if (!empty($cust)) {
        $cucond = " AND `accode`='$cust' ";
    }


    $dcond = " and `vouchdt` >= '$fdate' and `vouchdt` <= '$tdate' ";

    if (empty($fdate)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }

    if (empty($tdate)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }




    if ($orderby == "1") {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(netamt) as tamt from `salemain` where party != '' $dcond group by party order by norec desc") or die(mysqli_error($conn));
    } else {
        $getcash = mysqli_query($conn, "select party,count(*) as norec, sum(netamt) as tamt from `salemain` where party != '' $dcond group by party order by tamt desc") or die(mysqli_error($conn));
    }
    //    print table($getcash);
    $mfdt = date("d-m-Y", strtotime($fdate));
    $mtdt = date("d-m-Y", strtotime($tdate));

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

    $ctr = 1;
    $totamt = 0;
    while ($cmain = mysqli_fetch_array($getcash)) {

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


        $mamt = $cmain['tamt'];

        $cnorec = $cmain['norec'];
        $spname = $showparty['name'];


        $contents .= $ctr . ",";
        $contents .= $cnorec . ",";
        $contents .= $spname . ",";
        $contents .= $mamt . "\n";

        $ctr++;

        $totamt = $totamt + $mamt;
    }


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