<?php
/***************************************************************************************************
 PROGRAM DESCRIPTION : PROGRAM TO VIEW FORPERIOD REPORTS
***************************************************************************************************/

// JS for date picker
?>
<script>
function from_date() {
    gfPop.fStartPop(document.trans.txtfrom, Date);
}
function to_date() {
    gfPop.fStartPop(document.trans.txtto, Date);
}
</script>

<?php
include_once '../includes/constants.php';
include_once '../includes/functions.php';
include_once '../includes/session.php';

$partners = new partners();
$partners->connection($host, $user, $pass, $db);

// Get merchants
$merchantQuery = "SELECT * FROM partners_merchant";
$merchants = mysqli_query($partners->conn, $merchantQuery);

// Get affiliates
$affiliateQuery = "SELECT * FROM partners_affiliate";
$affiliates = mysqli_query($partners->conn, $affiliateQuery);

// Input vars
$total     = isset($_GET['total']) ? trim($_GET['total']) : '';
$click     = isset($_GET['click']) ? trim($_GET['click']) : '';
$nclick    = isset($_GET['nclick']) ? trim($_GET['nclick']) : '';
$lead      = isset($_GET['lead']) ? trim($_GET['lead']) : '';
$nlead     = isset($_GET['nlead']) ? trim($_GET['nlead']) : '';
$sale      = isset($_GET['sale']) ? trim($_GET['sale']) : '';
$nsale     = isset($_GET['nsale']) ? trim($_GET['nsale']) : '';
$from      = isset($_GET['from']) ? trim($_GET['from']) : '';
$to        = isset($_GET['to']) ? trim($_GET['to']) : '';
$merchant  = isset($_GET['merchant']) ? (int) $_GET['merchant'] : 0;
$affiliate = isset($_GET['affiliate']) ? (int) $_GET['affiliate'] : 0;
$msg       = isset($_GET['err']) ? trim($_GET['err']) : '';
$sub       = isset($_GET['sub']) ? trim($_GET['sub']) : '';
$impression = isset($_REQUEST['impression']) ? trim($_REQUEST['impression']) : '';

$sale = explode("~", $sale);
$total = explode("~", $total);

$heading = (!empty($from) && !empty($to)) ? "$from - $to" : '';
?>

<iframe width="168" height="175" name="gToday:normal:agenda.js" id="gToday:normal:agenda.js"
        src="cal/ipopeng.htm" scrolling="no" frameborder="0"
        style="border:2px ridge; visibility:visible; z-index:999; position:absolute; left:-500px; top:0px;">
</iframe>

<form name="trans" method="post" action="forperiod_process.php">
    <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" width="75%">
        <tr><td colspan="8" class="tdhead" align="center"><b>Statistics For Custom Period</b></td></tr>
        <tr><td colspan="8" align="center" class="textred"><?= htmlspecialchars($msg) ?></td></tr>
        <tr><td>&nbsp;</td><td colspan="3" align="center"><b>For Period</b></td><td colspan="4">&nbsp;</td></tr>
        <tr><td colspan="8">&nbsp;</td></tr>

        <tr>
            <td>&nbsp;</td>
            <td align="right">From</td><td>&nbsp;</td>
            <td align="left"><input type="text" name="txtfrom" size="18" value="<?= htmlspecialchars($from) ?>" onfocus="from_date();return false;"></td>
            <td>&nbsp;</td>
            <td align="right">Merchants</td><td>&nbsp;</td>
            <td align="left">
                <select name="Mname">
                    <option value="All">All Merchants</option>
                    <?php while ($row = mysqli_fetch_object($merchants)): ?>
                        <option value="<?= $row->merchant_id ?>" <?= ($merchant == $row->merchant_id) ? 'selected' : '' ?>>
                            <?= htmlspecialchars(stripslashes($row->merchant_company)) ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </td>
        </tr>

        <tr><td colspan="8">&nbsp;</td></tr>

        <tr>
            <td>&nbsp;</td>
            <td align="right">To</td><td>&nbsp;</td>
            <td align="left"><input type="text" name="txtto" size="18" value="<?= htmlspecialchars($to) ?>" onfocus="to_date();return false;"></td>
            <td>&nbsp;</td>
            <td align="right">Affiliates</td><td>&nbsp;</td>
            <td align="left">
                <select name="Aname">
                    <option value="All">All Affiliates</option>
                    <?php while ($row = mysqli_fetch_object($affiliates)): ?>
                        <option value="<?= $row->affiliate_id ?>" <?= ($affiliate == $row->affiliate_id) ? 'selected' : '' ?>>
                            <?= htmlspecialchars(stripslashes($row->affiliate_company)) ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </td>
        </tr>

        <tr><td colspan="8">&nbsp;</td></tr>

        <tr>
            <td colspan="8" align="center"><input type="submit" name="sub" value="View"></td>
        </tr>
    </table>
</form>

<?php
if (!empty($sub)) {
    $selDate = $heading;
    $values = $sale[3] . "~" . $impression . "~" . $nclick . "~" . $click . "~" . $nlead . "~" . $lead . "~" . $nsale . "~" . $sale[0];
    ?>

    <p align="right">
        <a href="#" onclick="window.open('../print_forperiod.php?mid=<?= $merchant ?>&aid=<?= $affiliate ?>&mode=admin&date=<?= urlencode($selDate) ?>&values=<?= urlencode($values) ?>','new','400,400,scrollbars=1,resizable=1'); return false;"><b>Print Report</b></a>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <a href="../csv_forperiod.php?mid=<?= $merchant ?>&aid=<?= $affiliate ?>&mode=admin&date=<?= urlencode($selDate) ?>&values=<?= urlencode($values) ?>"><b>Export as CSV</b></a>
    </p>

    <table border="0" cellpadding="0" cellspacing="0" width="75%" class="tablebdr">
        <tr><td align="center" class="tdhead"><b>Statistics<br><?= $heading ?></b></td></tr>
        <tr><td>
            <br>
            <table width="85%" align="center" class="tablebdr">
                <tr>
                    <td class="tdhead">Transactions</td>
                    <td class="tdhead">Number</td>
                    <td class="tdhead">Commissions</td>
                </tr>
                <tr>
                    <td class="grid1">Impressions <img src="../images/impression.gif" alt="" width="10" height="10"></td>
                    <td class="grid1"><?= (int) $sale[3] ?></td>
                    <td class="grid1"><?= $currSymbol . round($impression, 2) ?></td>
                </tr>
                <tr>
                    <td class="grid1">Clicks <img src="../images/click.gif" alt="" width="10" height="10"></td>
                    <td class="grid1"><?= (int) $nclick ?></td>
                    <td class="grid1"><?= $currSymbol . round($click, 2) ?></td>
                </tr>
                <tr>
                    <td class="grid1">Leads <img src="../images/lead.gif" alt="" width="10" height="10"></td>
                    <td class="grid1"><?= (int) $nlead ?></td>
                    <td class="grid1"><?= $currSymbol . round($lead, 2) ?></td>
                </tr>
                <tr>
                    <td class="grid1">Sales <img src="../images/sale.gif" alt="" width="10" height="10"></td>
                    <td class="grid1"><?= (int) $nsale ?></td>
                    <td class="grid1"><?= $currSymbol . round($sale[0], 2) ?></td>
                </tr>
                <tr>
                    <td class="grid1" colspan="3" align="center"><b>Impressions: <?= (int) $sale[3] ?></b></td>
                </tr>
            </table>
        </td></tr>

        <tr><td>
            <?php viewRawTrans($sale[4] ?? '', $sale[5] ?? ''); ?>
            <br>
            <table width="100%" align="center" class="tablewbdr">
                <tr>
                    <td class="tdhead" align="center">Pending</td>
                    <td class="tdhead" align="center">Reversed</td>
                </tr>
                <tr>
                    <td align="center"><?= $currSymbol . round($total[1], 2) ?></td>
                    <td align="center"><?= $currSymbol . round($total[3], 2) ?></td>
                </tr>
            </table>
        </td></tr>
    </table>

    <?php
}
?>
<br>
