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

$maindb = "agentin";
$formname = "Agent In Register";
$file = "agentinreg_fr.php";

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

$agent = $_POST["agent"];

$agentcond = "";
if (!empty($agent)) {
    $agentcond = " AND m.`agent`='$agent' ";
}

if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; contreg.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; contreg.php");
}
$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);


$getpmain = mysqli_query($conn, "SELECT m.*, DATE_FORMAT(m.`vouchdt`, '%d-%m-%Y') as mydt, a.accode, a.name AS agent FROM `$maindb`m
                                LEFT JOIN accmast a ON a.accode = m.agent
                                 WHERE m.`vouchdt` >= '$mfrmdate' AND m.`vouchdt` <= '$mtodate' $agentcond")or die(mysqli_error($conn));

$getno = 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; 
        }
    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white" class="table table-condensed table-hover table-bordered">
                        <thead>
                            <tr class="mySearchTr">
                                <th style="width:5%">Sr</th>
                                <th style="width:5%">Voucher</th>
                                <th style="width:7%">Date</th>
                                <th style="width:20%">Agent</th>
                                <th style="width:9%" class="text-right">INR</th>
                                <th style="width:9%" class="text-right">ROE</th>
                                <th style="width:9%" class="text-right">Dollar</th>
                                <th style="width:9%" class="text-right">RMB</th>
                                <th style="width:9%" class="text-right">RMB Amount</th>
                                <th style="width:20%">Remark</th>

                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = $totinr = $totroe = $totdollar = $totrmb = $totrmbamt =0;

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

                                $vouch = $pmain['vouchno'];
                                $ctr++;
                                Echo "<tr id='boldfont' class='clickable-row2' data-href='agentin.php?vouchno=$vouch&mode=edit' data-target='_blank'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$pmain[agent]</td>";
                                Echo "<td class='text-right'>$pmain[inr]</td>";
                                Echo "<td class='text-right'>$pmain[roe]</td>";
                                Echo "<td class='text-right'>$pmain[dollar]</td>";
                                Echo "<td class='text-right'>$pmain[rmb]</td>";
                                Echo "<td class='text-right'>$pmain[rmbamt]</td>";
                                Echo "<td>$pmain[remark1]  $pmain[remark2]</td>";
                                Echo "</tr>";

                                $totinr    = $totinr    + $pmain['inr'];
                                $totdollar = $totdollar + $pmain['dollar'];
                                $totrmbamt = $totrmbamt + $pmain['rmbamt'];
                            }
                            $colr = "green";
                            echo "<tr> ";
                            echo "<td style='color:$colr;' colspan='4' class='text-right'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totinr</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totdollar</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totrmbamt</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, Agent, INR, ROE, Dollar, RMB, RMB Amount, Remark\n";

    $ctr = $totinr = $totroe = $totdollar = $totrmb = $totrmbamt =0;

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

    $ctr++;
        $contents .="$ctr" . ",";
        $contents .="$pmain[vouchno]" . ",";
        $contents .="$pmain[mydt]" . ",";
        $contents .="$pmain[agent]" . ",";
        $contents .="$pmain[inr]" . ",";
        $contents .="$pmain[roe]" . ",";
        $contents .="$pmain[dollar]" . ",";
        $contents .="$pmain[rmb]" . ",";
        $contents .="$pmain[rmbamt]" . ",";
        $contents .="$pmain[remark1]" . "$pmain[remark2]" . "\n";

    $totinr    = $totinr    + $pmain['inr'];
    $totroe    = $totroe    + $pmain['roe'];
    $totdollar = $totdollar + $pmain['dollar'];
    $totrmb    = $totrmb    + $pmain['rmb'];
    $totrmbamt = $totrmbamt + $pmain['rmbamt'];
}

$contents .="Grand Total" . ",,,,";
$contents .="$totinr" . ",";
$contents .="$totroe" . ",";
$contents .="$totdollar" . ",";
$contents .="" . ",";
$contents .="$totrmbamt" . "\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;
}
}
?>

