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

$maindb = 'bankrecp';
$maindb2 = 'cashrecp';
$formname = "Payment Received Register";
$file = "payreg_fr.php";

$ztit = $_SESSION["tit_" . $dbname];
$tourcode = $_SESSION['tourcode'];
$tourname = $_SESSION['tourname'];
$cust = 'accmast';
$acc = 'actran';
$accdb = $acc;
$custdb = $cust;
$bankdb = 'bankrecp';
$cashdb = 'cashrecp';
$tourcond = " and a.tour = '$tourcode' ";

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

$paytype = $_POST["paytype"];
$bankname = $_POST["bankname"];
$recpagnst = $_POST["recpagnst"];
//$tour = $_POST["tour"];
$class = $_POST["class"];

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

$classcond = "";
// $tourcond = "";
$banknamecond = "";
$paytypecond = "";

if (!empty($bankname)) {
    $banknamecond = " AND `bankname`='$bankname' ";
    $banknamecond1 = " AND `bankcode`='$bankname' ";
}
//if (!empty($tour)) {
//    $tourcond = " AND b.`tourno`='$tour' ";
//}
if (!empty($class)) {
    $classcond = " AND b.`batchno`='$class' ";
}

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; payreg.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; payreg.php");
}

if (!empty($recpagnst)) {
    switch ($recpagnst) {
        case "F":
            $paytypecond = " and a.`recpagnst`='F'";
            break;

        case "U":
            $paytypecond = " and a.`recpagnst`='U'";
            break;

        default:
            break;
    }
}

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

if ($paytype == "" || $paytype == "B") {
    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, DATE_FORMAT(a.`chqdate`, '%d-%m-%Y') as chqdt, 
                                                b.tourno, b.batchno as classno , c.name as partyname, d.name as bankname, e.name as tourname 
                                                FROM `$bankdb` a LEFT JOIN bookform b ON a.regnno = b.regnno and a.tour = b.tourno
                                                LEFT JOIN accmast c ON a.accode = c.accode
                                                LEFT JOIN accmast d ON a.bankcode = d.accode
                                                LEFT JOIN tour e ON b.tourno = e.code
                                                  WHERE a.`vouchdt` >= '$mfrmdate' 
                            AND a.`vouchdt` <= '$mtodate' $banknamecond1 $paytypecond $tourcond $classcond ORDER BY a.vouchdt,a.vouchno") or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
}
//print table($getpmain);
if ($paytype == "" || $paytype == "C") {

    $getpmain1 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, 
                                                b.tourno, b.batchno as classno, c.name as partyname, d.name as cashname , e.name as tourname
                                                FROM `$cashdb` a LEFT JOIN bookform b ON a.regnno = b.regnno and a.tour = b.tourno
                                                LEFT JOIN accmast c ON a.party = c.accode
                                                LEFT JOIN accmast d ON a.bankname = d.accode
                                                LEFT JOIN tour e ON b.tourno = e.code
                                                WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $banknamecond $paytypecond $tourcond $classcond ORDER BY a.vouchdt,a.vouchno") or die(mysqli_error($conn));
    $get1 = mysqli_num_rows($getpmain1);
}

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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .bankrow {
            background-color: #cdb4db;
        }

        .cashrow {
            background-color: #ffc8dd;
        }

        @media print {
            .noPrint {
                display: none;
            }
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>

    <body>
        <div class="col-lg-12 pad">
            <?php include("includes/head_rep.php"); ?>
            <?php
            if ($get > 0) {
                ?>

                <div class="row bankrow">
                    <div class="col-lg-12">
                        <h2><b>Bank Receipt</b></h2>
                    </div>
                </div>
                <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                    <thead class="mySearchTr">
                        <tr>
                            <th style="width:3%">Sr</th>
                            <th style="width:8%">Date</th>
                            <th style="width:4%">Voucher.</th>
                            <th style="width:4%">Against</th>
                            <th style="width:4%">Regn. No.</th>
                            <th style="width:4%">Class</th>
                            <th style="width:20%">Party</th>
                            <th style="width:15%">Bank Name</th>
                            <th style="width:8%">Chq.Date</th>
                            <th style="width:6%">Chq. No.</th>
                            <th style="width:6%">RTGS</th>
                            <th style="width:8%" class="text-right"> Ticket</th>
                            <th style="width:8%" class="text-right">Tour</th>
                            <th style="width:15%" class="text-center">Tour Name</th>
                            <th style="width:5%" class="noPrint">Action</th>

                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $totamt = 0;
                        $totpack = 0;
                        $totbill = 0;
                        $totitmpr = 0;
                        $ttickamt = 0;
                        $ttouramt = 0;

                        $style = '';
                        $status = '';
                        $trge = '';

                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $tickamt = 0;
                            $touramt = 0;
                            $postchq = $pmain['postchq'];
                            $retdat = $pmain['retdate'];
                            $recpagnst = $pmain['recpagnst'];
                            $payfor = $pmain['payfor'];
                            if ($postchq != '1') {
                                $status = 'Pending';
                            }
                            if ($recpagnst == 'F') {
                                $against = 'FIT';
                            } else if ($recpagnst == 'U') {
                                $against = 'UMRAH';
                            } else {
                                $against = '';
                            }
                            if ($payfor == 'T') {
                                $tickamt = $pmain["amount"];
                            } else if ($payfor == 'U') {
                                $touramt = $pmain["amount"];
                            }
                            $trge = "<a target='_blank' href='bankrecp.php?vouchno=$pmain[vouchno]&mode=edit'><i class='fa fa-pencil'></i></a>";

                            $ctr++;
                            echo "<tr id='boldfont'>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[mydt]</td>";
                            echo "<td>$pmain[vouchno]</td>";
                            echo "<td>$against</td>";
                            echo "<td>$pmain[regnno]</td>";
                            echo "<td>$pmain[classno]</td>";
                            echo "<td>$pmain[partyname]</td>";
                            echo "<td>$pmain[bankname]</td>";
                            echo "<td>$pmain[chqdt]</td>";
                            echo "<td>$pmain[chqno]</td>";
                            echo "<td>$pmain[rtgsno]</td>";
                            echo "<td class='text-right'>$tickamt</td>";
                            echo "<td class='text-right'>$touramt</td>";
                            echo "<td class='text-center'>$pmain[tourname]</td>";
                            echo "<td class='noPrint'>$trge</td>";

                            echo "</tr>";
                            $style = '';
                            $status = '';
                            $ttickamt = $ttickamt + $tickamt;
                            $ttouramt = $ttouramt + $touramt;
                        }
                        $colr = "grey";
                        echo "<tr> ";
                        echo "<td align='right' style='color:$colr;' id='font' colspan='10'><h4><b>Grand Total</h4></b></td>";
                        echo "<td align='right' style='color:$colr;' id='font'><h4><b>$ttickamt</b></h4></td>";
                        echo "<td align='right' style='color:$colr;' id='font'><h4><b>$ttouramt</b></h4></td>";
                        echo "<td colspan='2'></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
                <br>
                <?php
            }
            if ($get1 > 0) {
                ?>
                <div class="row cashrow">
                    <div class="col-lg-12">
                        <h2><b>Cash Receipt</b></h2>
                    </div>
                </div>
                <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                    <thead class="mySearchTr">
                        <tr>
                            <th style="width:3%">Sr</th>
                            <th style="width:8%">Date</th>
                            <th style="width:4%">Voucher</th>
                            <th style="width:4%">Against</th>
                            <th style="width:4%">Regn. No.</th>
                            <th style="width:4%">Class</th>
                            <th style="width:19%">Party</th>
                            <th style="width:15%">Cash Name</th>
                            <th style="width:10%" class="text-right"> Ticket</th>
                            <th style="width:10%" class="text-right">Tour</th>
                            <th style="width:10%" class="text-center">Tour Name</th>
                            <th style="width:5%" class="noPrint">Action</th>

                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr1 = 0;
                        $totnetamt1 = 0;
                        $trge1 = '';

                        while ($pmain1 = mysqli_fetch_array($getpmain1)) {

                            $trge1 = "<a target='_blank' href='cashrecp.php?vouchno=$pmain1[vouchno]&mode=edit'><i class='fa fa-pencil'></i></a>";

                            $tickamt = 0;
                            $touramt = 0;
                            $postchq = $pmain1['postchq'];
                            $retdat = $pmain1['retdate'];
                            $recpagnst = $pmain1['recpagnst'];
                            $payfor = $pmain1['payfor'];
                            if ($postchq != '1') {
                                $status = 'Pending';
                            }
                            if ($recpagnst == 'F') {
                                $against = 'FIT';
                            } else if ($recpagnst == 'U') {
                                $against = 'UMRAH';
                            } else {
                                $against = '';
                            }
                            if ($payfor == 'T') {
                                $tickamt = $pmain1["amount"];
                            } else if ($payfor == 'U') {
                                $touramt = $pmain1["amount"];
                            }
                            $ctr1++;
                            echo "<tr id='boldfont'>";
                            echo "<td>$ctr1</td>";
                            echo "<td>$pmain1[mydt]</td>";
                            echo "<td>$pmain1[vouchno]</td>";
                            echo "<td>$against</td>";
                            echo "<td>$pmain1[regnno]</td>";
                            echo "<td>$pmain1[classno]</td>";
                            echo "<td>$pmain1[partyname]</td>";
                            echo "<td>$pmain1[cashname]</td>";
                            echo "<td class='text-right'>$tickamt</td>";
                            echo "<td class='text-right'>$touramt</td>";
                            echo "<td class='text-center'>$pmain1[tourname]</td>";
                            echo "<td class='noPrint'>$trge1</td>";
                            echo "</tr>";

                            $ttickamt1 = $ttickamt + $tickamt;
                            $ttouramt1 = $ttouramt + $touramt;
                        }
                        $colr = "grey";
                        echo "<tr> ";
                        echo "<td align='right' colspan='7' style='color:$colr;' id='font'><h4><b>Grand Total</h4></b></td>";
                        echo "<td align='right' style='color:$colr;' id='font'><h4><b>$ttickamt</b></h4></td>";
                        echo "<td align='right' style='color:$colr;' id='font'><h4><b>$ttouramt</b></h4></td>";
                        echo "<td colspan='2'></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
            <?php } ?>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
    <?php
}
?>