<?php

include("../includes/site_config.php");
include('../session_handler.inc');
if (isset($_GET['lstYear'])) $lstYear=$_GET['lstYear']; else $lstYear=date('Y');
if (isset($_GET['WMonth'])) $WMonth=$_GET['WMonth']; else $WMonth=date('m');
# retrieve the template
$area = 'member';

$Year=date("Y");
$Year1=$Year-1;
$Year2=$Year1-1;
$Year3=$Year1-2;
$Year4=$Year1-3;
if (isset($WMonth) and $WMonth != '-Choose-' and $lstYear != '-Choose-') {
    $query="SELECT *, DATE_FORMAT(rec_buydate,'%d-%M-%Y') AS buydate ,aff_surname, aff_forename FROM receipts LEFT JOIN affiliates ON (rec_affuserid=aff_userid) WHERE MONTH(rec_paydate) = '$WMonth' AND YEAR(rec_paydate) = '$lstYear' ORDER BY aff_surname, aff_forename, rec_paydate ASC";
    $result=mysql_query($query,$link) or die(mysql_error());
    $qrynum=mysql_num_rows($result);
} else {
    $hdnmonth="";
    $hdnyear="";
}
?>

<?php include("include/header.php"); ?>
 <?php
 if (isset($WMonth) and $WMonth != '-Choose-' and $lstYear != '-Choose-') {
    print("<div align='center'><center>
             <table border='0' cellpadding='0' width='610' cellspacing='0'>
            <tr>
              <td width='610' height='20' colspan='4'>".PRN_PAYMENTS_TITLE." - $WMonth/$lstYear</td>
            </tr>
            <tr>
              <td width='610' height='20' colspan='4'><hr></td>
            </tr>                <tr>
                     <td width='152' height='20'><b>".FRN_PAYMENTS_AFFILIATE."</b></td>
                     <td width='152' height='20'><b>".AFF_PAYMENT_TRANS_CODE."</b></td>
                     <td width='153' height='20'><b>".AFF_PAYMENT_TRANS_DATE."</b></td>
                     <td width='153' height='20'><b>".FRN_PAYMENTS_AMOUNT."</b></td>
                </tr><tr>
              <td width='610' height='20' colspan='4'><hr></td>
            </tr>");
    while ($sql_array = mysql_fetch_object($result) ) {
        print("<tr>
                  <td width='152' height='20'>$sql_array->aff_surname, $sql_array->aff_forename</td>
                  <td width='152' height='20'>$sql_array->rec_transid</td>
                  <td width='153' height='20'>".date($CONST_FORMAT_DATE_SHORT,strtotime($sql_array->buydate))."</td>
                  <td width='153' height='20'>$CONST_SYMBOL $sql_array->rec_affamount</td>
                </tr> ");
    }
    $query="SELECT SUM(rec_affamount) AS affamount, aff_surname, aff_forename FROM receipts LEFT JOIN affiliates ON (rec_affuserid=aff_userid) WHERE MONTH(rec_paydate) = '$WMonth' AND YEAR(rec_paydate) = '$lstYear' GROUP BY aff_surname, aff_forename";
    $result=mysql_query($query,$link) or die(mysql_error());
    print("<tr>
              <td width='610' height='20' colspan='4'><hr></td>
            </tr>
            <tr>
              <td width='610' height='20' colspan='4'><b>".PRN_PAYMENTS_SUMMARY."</b></td>
            </tr>
            <tr>
              <td width='610' height='20' colspan='4'><hr></td>
            </tr>
             ");
    while ($sql_array = mysql_fetch_object($result) ) {
        $sql_array->affamount=number_format($sql_array->affamount,2);
        print("<tr>
                  <td width='152' height='20'>$sql_array->aff_surname, $sql_array->aff_forename</td>
                  <td width='152' height='20'>$CONST_SYMBOL $sql_array->affamount</td>
                  <td width='153' height='20'></td>
                  <td width='153' height='20'></td>
                </tr> ");
    }
    print("<tr>
              <td width='610' height='20' colspan='4'><hr></td>
            </tr>
    </table>
    </center></div>");
}
mysql_close($link);
?>
<?php include("include/footer.php"); ?>


