<?php
/************************************************************************/
/*     PROGRAMMER     :  SMA                                            */
/*     SCRIPT NAME    :  graph_returndays.php                           */
/*     CREATED ON     :  08/JULY/2006                                   */
/*     PURPOSE        :  Graph showing when an Affiliate makes a sale  */
/************************************************************************/

require_once("graphs.php"); // Make sure this includes the modernized class
session_start();

$MERCHANTID = isset($_POST['merchants']) ? intval(trim($_POST['merchants'])) : 0;
$affiliate  = isset($_POST['affiliates']) ? trim($_POST['affiliates']) : 'All';

$graphobj = new graphs();
$result = $graphobj->GetAllMerchants();
?>

<form name="ReturnDays" method="post" action="">
    <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" align="center" width="80%">
        <tr>
            <td colspan="2" align="center">
                <table border="0" cellpadding="0" cellspacing="0" align="center" width="100%">
                    <tr>
                        <td align="center" height="25" class="tdhead" width="95%">Return Day Analysis</td>
                        <td align="right" class="tdhead" width="5%">
                            <a href="#" onclick="popWin('../help_returnDays.php?user=Admin');">
                                <img src="../images/help.jpg" border="0">&nbsp;Help&nbsp;
                            </a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr>
            <td width="50%" align="right" height="25"><b>Merchant</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td width="50%" align="left" height="25">
                <select name="merchants" onchange="document.ReturnDays.submit()">
                    <option value="">Select One Merchant</option>
                    <?php
                    if ($result) {
                        foreach ($graphobj->merId as $index => $id) {
                            $selected = ($MERCHANTID == $id) ? "selected='selected'" : "";
                            $company = htmlspecialchars($graphobj->merCompany[$index]);
                            echo "<option value=\"$id\" $selected>$company</option>";
                        }
                    }
                    ?>
                </select>
            </td>
        </tr>

        <?php if ($MERCHANTID != 0): ?>
        <?php
            $result_aff = $graphobj->GetAffiliates($MERCHANTID);
        ?>
        <tr>
            <td width="50%" align="right" height="25"><b>Affiliate</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td width="50%" align="left" height="25">
                <select name="affiliates" onchange="document.ReturnDays.submit()">
                    <option value="All">All Affiliates</option>
                    <?php
                    if ($result_aff) {
                        foreach ($graphobj->aff_id as $index => $aid) {
                            $selected = ($affiliate == $aid) ? "selected='selected'" : "";
                            $company = htmlspecialchars($graphobj->aff_comp[$index]);
                            echo "<option value=\"" . htmlspecialchars($aid) . "\" $selected>$company</option>";
                        }
                    }
                    ?>
                </select>
            </td>
        </tr>
        <?php endif; ?>

        <tr><td colspan="2">&nbsp;</td></tr>
    </table>
    <br>

<?php
if ($MERCHANTID != 0) {
    // Get return day values
    $graphobj->FindReturnDays($affiliate, $MERCHANTID);
    
    $sameday        = ($graphobj->total_sameday * 360) / 100;
    $within15       = ($graphobj->total_15Day * 360) / 100;
    $withinOneMonth = ($graphobj->total_1Month * 360) / 100;
    $within2Month   = ($graphobj->total_2Month * 360) / 100;
    $total          = $graphobj->total;

    $within15       = $sameday + $within15;
    $withinOneMonth = $within15 + $withinOneMonth;
    $within2Month   = $withinOneMonth + $within2Month;
?>
    <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" align="center" width="80%">
        <tr>
            <td width="80%" align="center">
                <table width="100%" align="center" height="100">
                    <tr>
                        <td>
                            <table cellpadding="0" cellspacing="5" align="left">
                                <tr>
                                    <td><b>Colors</b></td>
                                    <td width="50"><b>Percentage</b></td>
                                    <td><b>Color Description</b></td>
                                </tr>
                                <tr>
                                    <td bgcolor="#cc0033" width="50" height="15">&nbsp;</td>
                                    <td align="center"><?= round($graphobj->total_sameday, 2) ?></td>
                                    <td>Sales on the same day from click</td>
                                </tr>
                                <tr>
                                    <td bgcolor="#ffcc00" width="50" height="15">&nbsp;</td>
                                    <td align="center"><?= round($graphobj->total_15Day, 2) ?></td>
                                    <td>Sales within 15 days from click</td>
                                </tr>
                                <tr>
                                    <td bgcolor="#3300ff" width="50" height="15">&nbsp;</td>
                                    <td align="center"><?= round($graphobj->total_1Month, 2) ?></td>
                                    <td>Sales within 1 Month from click</td>
                                </tr>
                                <tr>
                                    <td bgcolor="#009900" width="50" height="15">&nbsp;</td>
                                    <td align="center"><?= round($graphobj->total_2Month, 2) ?></td>
                                    <td>Sales within 2 months from click</td>
                                </tr>
                                <tr>
                                    <td bgcolor="#00ccff" width="50" height="15">&nbsp;</td>
                                    <td align="center"><?= round($graphobj->total_After, 2) ?></td>
                                    <td>Sales after 2 months from click</td>
                                </tr>
                                <tr>
                                    <td bgcolor="#C0C0C0" width="50" height="15">&nbsp;</td>
                                    <td align="center"><?= ($total == 0 ? "100" : "0") ?></td>
                                    <td>Affiliate has no Sale</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <img src="returndayanalysis.php?alt=0&day1=<?= round($sameday) ?>&day15=<?= round($within15) ?>&month1=<?= round($withinOneMonth) ?>&month2=<?= round($within2Month) ?>&total=<?= intval($total) ?>" alt="" border="0">
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
<?php } ?>
</form>
<br>

<script type="text/javascript">
    function popWin(url) {
        let nw = window.open(url, 'new', 'height=400,width=400,scrollbars=yes');
        if (nw) nw.focus();
    }
</script>
