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

$cashdb = "cashrecp";
$bankdb = "bankrecp";
$formname = "Received Register";

$saleexec = $_SESSION["saleexec"];
$usertype = $_SESSION["usertype"];
$tourcode = $_SESSION["tourcode"];
$username = strtoupper($_SESSION["username_" . $dbname]);

$scope = $_POST['scope'];
$fromdate = $_POST['frmdt'];
$todate = $_POST['todt'];

if ($scope == 1) {
    $mainqry = mysqli_query($conn, "select 'CR' as source, vouchdt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, vouchno, payfor, amount, bankname, regnno, narr1, narr2, recdby, party, 
                                     '' as rtgsno, '' as chqno, '' as chqdate, '' as drawbank, recpagnst 
                                     FROM `cashrecp` 
                                        WHERE recpagnst = 'F' and tour = $tourcode and vouchdt >= '$fromdate' and vouchdt <= '$todate' order by vouchdt, source, vouchno") or die(mysqli_error($conn));
} elseif ($scope == 2) {
    $mainqry = mysqli_query($conn, "SELECT 'BR' as source, vouchdt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, vouchno, payfor, amount, bankcode as bankname, regnno, narr1, narr2, recdby, accode as party,
                                rtgsno, chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as chqdate, drawbank , recpagnst
                                        FROM `bankrecp` 
                                        WHERE recpagnst = 'F' and tour = $tourcode and vouchdt >= '$fromdate' and vouchdt <= '$todate' order by vouchdt, source, vouchno") or die(mysqli_error($conn));
} else {
    $scopecond = "";

    $mainqry = mysqli_query($conn, "select a.* from 
                                (SELECT 'CR' as source, vouchdt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, vouchno, payfor, amount, bankname, regnno, narr1, narr2, recdby, party, 
                                     '' as rtgsno, '' as chqno, '' as chqdate, '' as drawbank , recpagnst
                                     FROM `cashrecp` 
                                        WHERE recpagnst = 'F' and tour = $tourcode and vouchdt >= '$fromdate' and vouchdt <= '$todate'
                                union all
                                SELECT 'BR' as source, vouchdt, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, vouchno, payfor, amount, bankcode as bankname, regnno, narr1, narr2, recdby, accode as party,
                                rtgsno, chqno, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as chqdate, drawbank , recpagnst
                                        FROM `bankrecp` 
                                        WHERE recpagnst = 'F' and tour = $tourcode and vouchdt >= '$fromdate' and vouchdt <= '$todate') as a 
                                order by vouchdt") or die(mysqli_error($conn));
}

if (isset($_POST["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" style="width:100%">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:6%">Date</th>
                                <th style="width:5%">Source</th>
                                <th style="width:5%">Vouchno</th>
                                <th style="width:17%">Bookname</th>
                                <th style="width:17%">Account</th>
                                <th style="width:10%" class="text-right">Amount</th>
                                <th style="width:8%">Received By</th>
                                <th style="width:8%">Chq No.</th>
                                <th style="width:6%">Chq Date</th>
                                <th style="width:6%">RTGS No.</th>
                                <th style="width:12%">Drawee Bank</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $discamt = 0;
                            $cashamt = 0;
                            $totamt = 0;
                            while ($fmain = mysqli_fetch_array($mainqry)) {
                                $voucher = $fmain['vouchno'];
                                $getname = mysqli_query($conn, "SELECT * FROM accmast WHERE accode = '$fmain[bankname]'") or die(mysqli_error($conn));
                                $fetname = mysqli_fetch_array($getname);

                                $getacname = mysqli_query($conn, "SELECT * FROM accmast WHERE accode = '$fmain[party]'") or die(mysqli_error($conn));
                                $fetacname = mysqli_fetch_array($getacname);

                                if ($fmain['source'] == "CR") {
                                    $sour = "CASH";
                                    echo "<tr id='boldfont' data-href='cashrecp.php?vouchno=$fmain[vouchno]&mode=edit' class='clickable-row2'></a>";
                                } else {
                                    $sour = "BANK";
                                    echo "<tr id='boldfont' data-href='bankrecp.php?vouchno=$fmain[vouchno]&mode=edit' class='clickable-row2'></a>";
                                }

                                $ctr++;
                                echo "<td>$fmain[mydt]</td>";
                                echo "<td>$sour</td>";
                                echo "<td>$voucher</td>";
                                echo "<td>$fetname[name]</td>";
                                echo "<td>$fetacname[name]</td>";
                                echo "<td class='text-right'>$fmain[amount]</td>";
                                echo "<td>$fmain[recdby]</td>";
                                echo "<td>$fmain[chqno]</td>";
                                echo "<td>$fmain[chqdate]</td>";
                                echo "<td>$fmain[rtgsno]</td>";
                                echo "<td>$fmain[drawbank]</td>";
                                echo "</tr>";
                                $totamt += $fmain['amount'];
                            }
                            $colr = "red";
                            echo "<tr style='color:$colr'>";
                            echo "<td class='text-right' colspan='5'><h4>Total</h4></td>";
                            echo "<td class='text-right'><h4>$totamt</h4></td>";
                            echo "<td class='text-right' colspan='5'><h4><b></b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}

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

    $contents .= "Date, Source, Vouchno, Bookname, Account, Amount, Received By, Chq No., Chq Date, RTGS No., Drawee Bank \n";

    $ctr = 0;
    $discamt = 0;
    $cashamt = 0;
    $totamt = 0;
    while ($fmain = mysqli_fetch_array($mainqry)) {
        $voucher = $fmain['vouchno'];
        $getname = mysqli_query($conn, "SELECT * FROM accmast WHERE accode = '$fmain[bankname]'") or die(mysqli_error($conn));
        $fetname = mysqli_fetch_array($getname);

        $getacname = mysqli_query($conn, "SELECT * FROM accmast WHERE accode = '$fmain[party]'") or die(mysqli_error($conn));
        $fetacname = mysqli_fetch_array($getacname);

        if ($fmain['source'] == "CR") {
            $sour = "CASH";
        } else {
            $sour = "BANK";
        }

        $ctr++;
        $contents .= "$fmain[mydt]" . ",";
        $contents .= "$sour" . ",";
        $contents .= "$voucher" . ",";
        $contents .= "$fetname[name]" . ",";
        $contents .= "$fetacname[name]" . ",";
        $contents .= "$fmain[amount]" . ",";
        $contents .= "$fmain[recdby]" . ",";
        $contents .= "$fmain[chqno]" . ",";
        $contents .= "$fmain[chqdate]" . ",";
        $contents .= "$fmain[rtgsno]" . ",";
        $contents .= "$fmain[drawbank]" . "\n";

        $totamt += $fmain['amount'];
    }
    $colr = "red";
    $contents .= "" . ",,,,";
    $contents .= "Total" . ",";
    $contents .= "$totamt" . ",";
    $contents .= "" . ",,,";
    $contents .= "" . "\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;
    }
}
?>