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

$maindb = "agentout";
$formname = "Agent Out Register";
$file = "agentoutreg_fr.php";

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

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

$agentcond = "";
$partycond = "";

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

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, b.accode, b.name AS party FROM `$maindb`m
                                LEFT JOIN accmast a ON a.accode = m.agent
                                LEFT JOIN accmast b ON b.accode = m.party
                                 WHERE m.`vouchdt` >= '$mfrmdate' AND m.`vouchdt` <= '$mtodate' $agentcond $partycond")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:20%">Party</th>
                                <th style="width:9%" class="text-right">RMB</th>
                                <th style="width:9%" class="text-right">ROE</th>
                                <th style="width:9%" class="text-right">INR</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='agentout.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>$pmain[party]</td>";
                                Echo "<td class='text-right'>$pmain[rmb]</td>";
                                Echo "<td class='text-right'>$pmain[roe]</td>";
                                Echo "<td class='text-right'>$pmain[inr]</td>";
                                Echo "<td>$pmain[remark1]  $pmain[remark2]</td>";
                                Echo "</tr>";

                                $totinr    = $totinr    + $pmain['inr'];
                            }
                            $colr = "green";
                            echo "<tr> ";
                            echo "<td style='color:$colr;' colspan='5' class='text-right'><h4><b>Grand Total</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></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 "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .="Sr, Voucher, Date, Agent, Party, RMB, ROE, INR, Remark\n";

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

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

    $vouch = $pmain['vouchno'];

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

    $totrmb    = $totrmb    + $pmain['rmb'];
    $totinr    = $totinr    + $pmain['inr'];
}
    $contents .="Grand Total" . ",,,,,";
    $contents .="" . ",";
    $contents .="" . ",";
    $contents .="$totinr" . ",";
    $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;
}
}
?>

