<?
/* 
   This file is part of the 'Science Fair In A Box' project
   SFIAB Website: http://www.sfiab.ca

   Copyright (C) 2009 James Grant <james@lightbox.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/
?>
<?
 require("../common.inc.php");
 require_once("../user.inc.php");

 user_auth_required('committee', 'admin');
 
 send_header("Fundraising",
 		array('Committee Main' => 'committee_main.php',
			'Administration' => 'admin/index.php',
			'Fundraising' => 'admin/fundraising.php'),
            "fundraising"
			);

?>
<script type="text/javascript">
$(document).ready(function() {
});
</script>

<h3><?=i18n("Fundraising Goals and Progress Year to Date")?></h3>
<?
$q=mysql_query("SELECT * FROM fundraising_goals WHERE fiscalyear='{$config['FISCALYEAR']}' ORDER BY deadline");

?>
<table class="tableview">
 <tr>
	<th><?=i18n("Goal")?></th>
	<th><?=i18n("Budget Amount")?></th>
	<th><?=i18n("Amount Received")?></th>
	<th><?=i18n("% to Budget")?></th>
	<th><?=i18n("Deadline")?></th>
 </tr>
 <?
 while($r=mysql_fetch_object($q)) {
     //lookup all donations made towards this goal
     $recq=mysql_query("SELECT SUM(value) AS received FROM fundraising_donations WHERE fundraising_goal='$r->goal' AND fiscalyear='{$config['FISCALYEAR']}' AND status='received'");
     echo mysql_error();
     $recr=mysql_fetch_object($recq);
     $received=$recr->received;
     if($r->budget)
         $percent=round($received/$r->budget*100,1);
     else
         $percent=0;

     echo "<tr><td>$r->name</td>";
     echo "<td style=\"text-align: right;\">".format_money($r->budget,false)."</td>";
     echo "<td style=\"text-align: right;\">".format_money($received,false)."</td>";
     $col=colour_to_percent($percent);
     echo "<td style=\"text-align: center; background-color: $col;\">{$percent}%</td>";
     echo "<td>".format_date($r->deadline)."</td></tr>\n";
 }
 ?>
</table>
<br />

<h3><?=i18n("Current Campaigns")?></h3>
<table class="tableview">
 <tr>
	<th><?=i18n("Name")?></th>
	<th><?=i18n("Type")?></th>
	<th><?=i18n("Start Date")?></th>
	<th><?=i18n("End Date")?></th>
	<th><?=i18n("Goal")?></th>
	<th><?=i18n("Target($)")?></th>
	<th><?=i18n("Received")?></th>
	<th><?=i18n("% to Budget")?></th>
 </tr>

<?
 $q=mysql_query("SELECT * FROM fundraising_campaigns WHERE fiscalyear='{$config['FISCALYEAR']}'");

 while($r=mysql_fetch_object($q)) {

    $goalq=mysql_query("SELECT * FROM fundraising_goals WHERE goal='{$r->fundraising_goal}' AND fiscalyear='{$config['FISCALYEAR']}'");
    $goalr=mysql_fetch_object($goalq);
    $recq=mysql_query("SELECT SUM(value) AS received FROM fundraising_donations WHERE fundraising_campaigns_id='$r->id' AND fiscalyear='{$config['FISCALYEAR']}' AND status='received'");
    echo mysql_error();
    $recr=mysql_fetch_object($recq);
    $received=$recr->received;
    if($r->budget)
        $percent=round($received/$r->target*100,1);
    else
        $percent=0;
    $col=colour_to_percent($percent);

    echo "<tr>\n";
    echo "  <td>$r->name</td>\n";
    echo "  <td>$r->type</td>\n";
    echo "  <td>".format_date($r->startdate)."</td>\n";
    echo "  <td>".format_date($r->enddate)."</td>";
    echo "  <td>$goalr->name</td>";
    echo "  <td style=\"text-align: right;\">".format_money($r->target,false)."</td>\n";
    echo "  <td style=\"text-align: right;\">".format_money($received,false)."</td>\n";
    echo "  <td style=\"text-align: center; background-color: $col;\">{$percent}%</td>\n";
    echo "</tr>\n";
 }
 ?>
 </tr>
</table>
<br />

<h3><?=i18n("To Do List")?></h3>
<h4>Thank You's</h4>
<?
$q=mysql_query("SELECT value, thanked, status, users_uid, sponsors_id, datereceived
    FROM fundraising_donations
    WHERE thanked='no' AND status='received'
    AND fiscalyear='{$config['FISCALYEAR']} 
    ORDER BY datereceived'
    ");
    //AND (users.year IS NULL OR users.year='{$config['FISCALYEAR']}')
echo mysql_error();
if(mysql_num_rows($q)) {
    echo "<table class=\"tableview\">";
    echo "<tr><th>".i18n("Name")."</th>\n";
    echo "<th>".i18n("Date Received")."</th>\n";
    echo "<th>".i18n("Amount")."</th>\n";
    echo "<th>".i18n("Generate Thank You")."</th>\n";
    echo "</tr>\n";

    while($r=mysql_fetch_object($q)) {
        if($r->users_uid) {
            $dq=mysql_query("SELECT CONCAT(firstname,' ',lastname) AS name FROM users WHERE uid='$r->users_uid' ORDER BY fairyear DESC LIMIT 1");

        }
        else if ($r->sponsors_id) {
            $dq=mysql_query("SELECT organization AS name FROM sponsors WHERE id='$r->sponsors_id'");
        }
        $dr=mysql_fetch_object($dq);
        echo "<tr>";
        echo " <td>$dr->name</td>";
        echo " <td>".format_date($r->datereceived)."</td>";
        echo " <td style=\"text-align: right;\">".format_money($r->value)."</td>";
        echo " <td style=\"text-align: center;\">";
        echo "<a href=\"#\" onclick=\"return false;\">".i18n("Generate Thank You")."</a></td>";
        echo "</tr>\n";
    }
    echo "</table>\n";
}else {
     echo i18n("No Thank You's pending");
    echo "<br />\n";
}
?>

<br />
<h4>Campaign Follow-Ups</h4>
<?
$q=mysql_query("SELECT * FROM fundraising_campaigns WHERE followupdate>=NOW() ORDER BY followupdate LIMIT 5");
echo mysql_error();
if(mysql_num_rows($q)) {
    echo "<table class=\"tableview\">";
    echo "<tr>";
    echo " <th>".i18n("Campaign Name")."</th>\n";
    echo " <th>".i18n("Start Date")."</th>\n";
    echo " <th>".i18n("Follow-Up Date")."</th>\n";
    echo "</tr>\n";
    while($r=mysql_fetch_object($q)) {
        echo "<tr><td>$r->name</td><td>".format_date($r->startdate)."</td><td>".format_date($r->followupdate)."</td></tr>\n";
    }
    echo "</table>\n";
} else {
    echo i18n("No campaign follow-ups");
    echo "<br />\n";
}

?>
<br />
<h4>Upcoming Proposals</h4>
<?
$q=mysql_query("SELECT * FROM sponsors WHERE fundingselectiondate>=NOW() ORDER BY fundingselectiondate LIMIT 5");
echo mysql_error();
if(mysql_num_rows($q)) {
    echo "<table class=\"tableview\">";
    echo "<tr>";
    echo " <th>".i18n("Name")."</th>\n";
    echo " <th>".i18n("Funding Selection Date")."</th>\n";
    echo "</tr>\n";
    while($r=mysql_fetch_object($q)) {
        echo "<tr><td>$r->organization</td><td>".format_date($r->fundingselectiondate)."</td></tr>\n";
    }
    echo "</table>\n";
} else {
    echo i18n("No proposals upcoming");
}

 send_footer();
?>
