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

$maindb = "cashrecp";
$formname = "Receipt Register";
$file = "recpreg_fr.php";

$zroot = $_SESSION["zdir"];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
ini_set('display_errors', 1);
error_reporting(E_ERROR);


$bankname = $_POST["bankname"];
$account = $_POST["account"];
$mode = $_POST["mode"];

$expensecond = "";
$accountcond = "";
$modecond = "";


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

if (!empty($account)) {
    $accountcond = "AND a.`party`='$account'";
}

if (!empty($mode)) {
    $modecond = "AND a.`type`='$mode'";
}


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>";
    }
}

$mainqry = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') AS mydt,
                                       b.name AS bankname, c.name AS partynm FROM `$maindb` a
                                       LEFT JOIN accmast b ON a.bankname = b.accode
                                       LEFT JOIN accmast c ON a.party = c.accode
                                       WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                                       $banknmcond $accountcond $modecond ") or die(mysqli_error($conn));
$get = mysqli_num_rows($mainqry);

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:7%">Date</th>
                                <th style="width:20%">Cash Name</th>
                                <th style="width:25%">Account</th>
                                <th style="width:8%">Mode</th>
                                <th style="width:8%" 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($mainqry)) {
                                $ctr++;

                                $typeOptions = [
                                    'U' => 'UPI / Bank',
                                    'C' => 'Cash'
                                ];

                                $typeCode = $pmain['type'];
                                $typeLabel = isset($typeOptions[$typeCode]) ? $typeOptions[$typeCode] : 'Unknown';

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

                                echo "<tr id='boldfont' class='clickable-row2' data-href='cashrecp.php?vouchno=$pmain[vouchno]&mode=edit' data-target='_blank'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$pmain[bankname]</td>";
                                echo "<td>$pmain[partynm]</td>";
                                echo "<td>$typeLabel</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 "<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, Cash Name, Account, Mode, Amount, Remark \n";

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

    while ($pmain = mysqli_fetch_array($mainqry)) {
        $ctr++;

        $typeOptions = [
            'U' => 'UPI / Bank',
            'C' => 'Cash'
        ];

        $typeCode = $pmain['type'];
        $typeLabel = isset($typeOptions[$typeCode]) ? $typeOptions[$typeCode] : 'Unknown';

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

        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[bankname]" . ",";
        $contents .= "$pmain[partynm]" . ",";
        $contents .= "$typeLabel" . ",";
        $contents .= "$amount" . ",";
        $contents .= "$pmain[remark1]" . "$pmain[remark2]" . "\n";

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

    }

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

    $contents = strip_tags($contents);

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

}
?>