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

$maindb = "actran";
$formname = "Expenses Register";
$file = "expensereg_fr.php";

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

$expense = $_POST["expense"];

$expensecond = "";

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

if (($_GET["fdt"]) != '') {
    $mfrmdate = $_GET["fdt"];
    $mtodate = $_GET["tdt"];
} else {

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        echo "<script>window.close()</script>";
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        echo "<script>window.close()</script>";
    }
}

$getpmain = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`date`, '%d-%m-%Y')as mydt, b.name, c.type as mode, c.remark1, c.remark2
                                               FROM `actran` a 
                                               LEFT JOIN accmast b ON a.accode = b.accode 
                                               LEFT JOIN cashpay c ON a.vouchno = c.vouchno 
                                               WHERE a.`date` >= '$mfrmdate' AND a.`date` <= '$mtodate' 
                                               AND a.source = 'CP' AND a.amount < 0 $expensecond
                                               AND a.accode IN (SELECT accode FROM accmast WHERE grpcode = '012') ") or die(mysqli_error($conn));

$get = mysqli_num_rows($getpmain);

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

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <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 ">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:8%">Voucher</th>
                                <th style="width:8%">Date</th>
                                <th style="width:20%">Expense</th>
                                <th style="width:10%" class="text-right">Amount</th>
                                <th style="width:20%">Remark</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totpairs = 0;
                            $totamt = 0;

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

                                $amount = abs($pmain['amount']); 

                                $ctr++;
                                echo "<tr id='boldfont' class='clickable-row2' data-href='cashpay.php?vouchno=$pmain[vouchno]&mode=edit' data-target='_blank'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$pmain[name]</td>";
                                echo "<td class='text-right'>$amount</td>";
                                echo "<td>$pmain[remark1] $pmain[remark2] </td>";
                                echo "</tr>";

                                $totpairs += $pmain['pairs'];
                                $totamt += $amount;

                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<th class='text-right' style='color: red;'>Grand Total</td>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($totamt, 2) . "</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, Customer, Mode, Pairs, Amount, Remark \n";

    $ctr = 0;
    $totpairs = 0;
    $totamt = 0;

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

        $typeOptions = [
            'R' => 'Credit',
            'U' => 'UPI / Bank',
            'C' => 'Cash'
        ];
        $typeCode = $pmain['type'];
        $typeLabel = isset($typeOptions[$typeCode]) ? $typeOptions[$typeCode] : 'Unknown';


        $ctr++;
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[custname]" . ",";
        $contents .= "$typeLabel" . ",";
        $contents .= "$pmain[pairs]" . ",";
        $contents .= "$pmain[amount]" . ",";
        $contents .= "$pmain[remark]" . "\n";

        $totpairs += $pmain['pairs'];
        $totamt += $pmain['amount'];

    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$totpairs" . ",";
    $contents .= "$totamt" . ",";
    $contents .= "" . "\n";


    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname " . "" . date('d-m-Y') . ".csv");
    print $contents;

}
?>