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

ini_set('display_errors', 1); // Enable error display
error_reporting(E_ERROR); // Report all errors and warnings

$formname = "Received Register";
$file = "recreg_fr.php";
$ztit = $_SESSION["tit_" . $dbname];

$maindb = 'recpent';

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

$party   = $_POST["party"];
$account  = $_POST["account"];

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

$partycond = "";
$accountcond = "";

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


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

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

$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $partycond $accountcond") or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

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

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

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

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

    <body>
        <div class="container">
            <?php include("includes/head_rep.php");
            ?>

            <div class="row">
                <div class="col-lg-12 pad">

                    <b>Receipt</b>
                    <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:4%">Voucher</th>
                                <th style="width:8%">Date</th>
                                <th style="width:15%">Customer</th>
                                <th style="width:5%" class="text-right">Amount</th>
                                <th style="width:15%">Account</th>
                                <th style="width:23%">Remark</th>
                                <th style="width:5%" class="noPrint">Action</th>

                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $totpack = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $style = '';
                            $status = '';
                            $trge = '';

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

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

                                $getacc = mysqli_query($conn, "SELECT * FROM `accmast` where `accode`='$pmain[bankname]'") or die(mysqli_error($conn));
                                $fetacc = mysqli_fetch_array($getacc);

                                $date = $pmain['mydt']; // your date value (YYYY-MM-DD)
                                $dayName = date("l", strtotime($date)); // "l" gives full day name

                                $trge = "<a target='_blank' href='recp_ent.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[vouchno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$party[name]</td>";
                                echo "<td class='text-right'>$pmain[amount]</td>";
                                echo "<td>$fetacc[name]</td>";
                                echo "<td class='noPrint'>$pmain[remark1] $pmain[remark2]</td>";
                                echo "<td class='noPrint'>$trge</td>";

                                echo "</tr>";
                                $style = '';
                                $status = '';
                                $totnetamt = $totnetamt + $pmain['amount'];
                            }
                            $colr = "grey";
                            echo "<tr> ";
                            echo "<td align='right' style='color:$colr;' id='font' colspan='4'><h4><b>Grand Total</h4></b></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totnetamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font' colspan='4'><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 .= "Sr, Voucher, Date,Customer, Amount, Account, Remark \n";

    $ctr = 0;
    $totamt = 0;
    $totpack = 0;
    $totbill = 0;
    $totitmpr = 0;
    $style = '';
    $status = '';
    $trge = '';

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

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

        $getproject = mysqli_query($conn, "SELECT * FROM `projmain` WHERE `code`='$pmain[project]'") or die(mysqli_error($conn));
        $project = mysqli_fetch_array($getproject);

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

        if ($pmain['paidby'] == 'A') {
            $payby = 'Agent';
        } else {
            $payby = 'Direct';
        }

        $date = $pmain['mydt']; // your date value (YYYY-MM-DD)
        $dayName = date("l", strtotime($date)); // "l" gives full day name

        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$party[name]" . ",";
        $contents .= "$pmain[amount]" . ",";
        $contents .= "$fetacc[name]" . ",";
        $contents .= "$pmain[remark1]" . "\n";

        $style = '';
        $status = '';
        $totnetamt = $totnetamt + $pmain['amount'];
    }

    
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$totnetamt" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $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;
    }
}
?>