<?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 Entry Register';
$file = 'expentryreg_fr.php';

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

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

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

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

if (!empty($payee)) {
    $payeecond = " AND `payee`= '$payee'";
}

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 *,DATE_FORMAT(vouchdt, '%d-%m-%Y') AS mydt FROM `expentry` WHERE `vouchno` != '' $expcond $ptypecond $payeecond ORDER BY 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">
            <?php
            include("includes/head_rep2.php");
            ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <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="6%;">Date</th>
                                <th width="4%;">Time</th>
                                <th width="22%;">Expense</th>
                                <th width="6%;" class="text-right">Qty</th>
                                <th width="6%;" class="text-right">Unit</th>
                                <th width="8%;" class="text-right">Amount</th>
                                <th width="6%;">Paid By</th>
                                <th width="20%;">Payee</th>
                                <th width="26%;">Remark</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $pctr = $tqty = $tamt = 0;

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

                                $getexp = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[expense]'") or die(mysqli_error($conn));
                                $sexp = mysqli_fetch_array($getexp);

                                $getuser = mysqli_query($conn, "SELECT `user` FROM $zroot.`passmain` WHERE `user`='$pmain[payee]'") or die(mysqli_error($conn));
                                $ftuser = mysqli_fetch_array($getuser);

                                if ($pmain['paidby'] == "C") {
                                    $ptyped = "CASH";
                                } else if ($pmain['paidby'] == "R") {
                                    $ptyped = "CREDIT";
                                } else if ($pmain['paidby'] == "U") {
                                    $ptyped = "UPI";
                                }

                                $voucher = $pmain["vouchno"];
                                $mydt = $pmain["mydt"];
                                $time = $pmain["time"];
                                $time = $pmain["time"];
                                $expname = $sexp['name'];
                                $qty = $pmain["qty"];
                                $unit = $pmain["unit"];
                                $amount = $pmain["amount"];
                                $payee = $ftuser['user'];
                                $remark = $pmain['remark'];

                                $pctr++;
                                echo "<tr data-href='expentry.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                echo "<td>$pctr</td>";
                                echo "<td>$mydt</td>";
                                echo "<td>$time</td>";
                                echo "<td>$expname</td>";
                                echo "<td class='text-right'>$qty</td>";
                                echo "<td class='text-right'>$unit</td>";
                                echo "<td class='text-right'>$amount</td>";
                                echo "<td>$ptyped</td>";
                                echo "<td>$payee</td>";
                                echo "<td>$remark</td>";
                                echo "</tr>";

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

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

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

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

        if ($ptype != "") {
            $contents .= "Pay Type" . ",";
            $contents .= "$paytype" . "\n";
        }

        $contents .= "Sr, Date, Time, Expense, Qty, Unit, Amount, Paid By, Payee, Remark \n";

        $pctr = $tqty = $tamt = 0;

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

            $getexp = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[expense]'") or die(mysqli_error($conn));
            $sexp = mysqli_fetch_array($getexp);

            $getuser = mysqli_query($conn, "SELECT `user` FROM $zroot.`passmain` WHERE `user`='$pmain[payee]'") or die(mysqli_error($conn));
            $ftuser = mysqli_fetch_array($getuser);

            if ($pmain['paidby'] == "C") {
                $ptyped = "CASH";
            } else if ($pmain['paidby'] == "R") {
                $ptyped = "CREDIT";
            } else if ($pmain['paidby'] == "U") {
                $ptyped = "UPI";
            }

            $mydt = $pmain["mydt"];
            $time = $pmain["time"];
            $time = $pmain["time"];
            $expname = $sexp['name'];
            $qty = $pmain["qty"];
            $unit = $pmain["unit"];
            $amount = $pmain["amount"];
            $payee = $ftuser['user'];
            $remark = $pmain['remark'];

            $pctr++;
            $contents .= "$pctr" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$time" . ",";
            $contents .= "$expname" . ",";
            $contents .= "$qty" . ",";
            $contents .= "$unit" . ",";
            $contents .= "$amount" . ",";
            $contents .= "$ptyped" . ",";
            $contents .= "$payee" . ",";
            $contents .= "$remark" . "\n";

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

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

        ?>