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

   Copyright (C) 2008 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("../tableeditor.class.php");
 require_once("../user.inc.php");

 user_auth_required('committee', 'admin');
 
 include ("fundraising_sponsorship_handler.inc.php");
 include ("fundraising_types_handler.inc.php");

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

require_once("../dialog.inc.php");

 //first, insert any defaults
 $q=mysql_query("SELECT * FROM fundraising WHERE year='".$config['FAIRYEAR']."'");
 if(!mysql_num_rows($q)) {
	 $q=mysql_query("SELECT * FROM fundraising WHERE year='-1'");
	 while($r=mysql_fetch_object($q)) {
		 mysql_query("INSERT INTO fundraising (`type`,`name`,`description`,`system`,`goal`,`year`) VALUES ('$r->type','".mysql_real_escape_string($r->name)."','".mysql_real_escape_string($r->description)."','$r->system','$r->goal','".$config['FAIRYEAR']."')");
	 }
 }

//this table is eventually going to be massive, and probably not in a tableview format, it'll show goals as well as all ongoing fund pledges, probabilities, etc as well as over/under, etc, all prettily colour coded.. basically a good overview of the total fundraising status of the fair.
 $q=mysql_query("SELECT * FROM fundraising WHERE year='{$config['FAIRYEAR']}' ORDER BY system DESC,type");
 echo "<table class=\"fundraisingtable\">";

 while($r=mysql_fetch_object($q)) {
	 echo "<tr>";
	 echo "<th><a title=\"".i18n("Edit fund details")."\" onclick=\"return SFIABDialog(event,'fundraising_types.php?id=$r->id',400,250)\" href=\"#\"><img border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/edit.".$config['icon_extension']."\"></a>";
	 if($r->system=="no") {
		echo "<a title=\"".i18n("Remove Fund")."\" onclick=\"return confirmClick('Are you sure you want to remove this fund and all sponsorships inside it?')\" href=\"fundraising.php?action=funddelete&delete=$r->id\"><img border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/button_cancel.".$config['icon_extension']."\"></a>";
	 }

	 echo "</th>\n";
	 echo "<th colspan=\"5\">".i18n($r->name)."</th>\n";
	 echo "<th style=\"text-align: right\">".format_money($r->goal)."</th>\n";
 	 echo "</tr>\n";

	if($r->type=="general") 
		$orsql.="OR fundraising_type IS NULL";

	$typetotal=0;
	$sq=mysql_query("SELECT sponsorships.id, sponsors.organization, sponsorships.value, sponsorships.status, sponsorships.probability
		 FROM sponsorships
		 JOIN sponsors ON sponsorships.sponsors_id=sponsors.id
		  WHERE (sponsorships.fundraising_type='$r->type' $orsql) 
		  AND sponsorships.year='{$config['FAIRYEAR']}'
		  ORDER BY status DESC, probability DESC, organization");
	while($sr=mysql_fetch_object($sq)) {
		echo "<tr id=\"sponsorships_$sr->id\" class=\"fundraising{$sr->status}\">";
		echo "<td><a title=\"".i18n("Edit sponsorship details")."\" onclick=\"return SFIABDialog(event,'fundraising_sponsorship.php?id=$sr->id&fundraising_type=$r->type',400,250)\" href=\"#\"><img border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/edit.".$config['icon_extension']."\"></a>";
			echo "<a title=\"".i18n("Remove sponsorship")."\" onclick=\"return confirmClick('Are you sure you want to remove this sponsorship?')\" href=\"fundraising.php?action=sponsorshipdelete&delete=$sr->id\"><img border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/button_cancel.".$config['icon_extension']."\"></a>";

		echo "</td>";
		echo "<td>$sr->organization</td>\n";
		/*
		echo "<a href=\"communication.php?action=edit&edit=$r->id\"><img border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/edit.".$config['icon_extension']."\"></a>";

		//only user emails can be deleted, system ones are required and cannot be removed
		if($r->type=="user")
		{
			echo "&nbsp;";
			echo "<a onclick=\"return confirmClick('Are you sure you want to remove email?')\" href=\"communication.php?action=delete&delete=$r->id\"><img border=0 src=\"".$config['SFIABDIRECTORY']."/images/16/button_cancel.".$config['icon_extension']."\"></a>";
			echo "&nbsp;";
			echo "<a href=\"communication.php?action=send&send=$r->id\">Send</a>";
		}


		*/
		echo "<td>$sr->status</td>";
		echo "<td>";
		if($sr->status=="pending")
		{
			echo "$sr->probability%";
			echo "</td>";
			echo "<td>".format_money($sr->value)."</td>";
		}
		else
			echo "</td><td></td>\n";

		$probval=$sr->probability/100*$sr->value; 
		echo "<td style=\"text-align: right\">".format_money($probval)."</td>";
		echo "<td></td>\n";
		echo "</tr>\n";
		$typetotal+=$probval;
	}
	 echo "<tr>";
	 echo "<td><a onclick=\"return SFIABDialog(event,'fundraising_sponsorship.php?fundraising_type=$r->type',400,250)\" href=\"#\">add</a></td>";
	 echo "<td colspan=\"4\" style=\"text-align: right; font-weight: bold;\">".i18n("%1 Total",array($r->name),array("Fundraising type total, eg) Award Sponsorship Total"))."</td>\n";
	 echo "<td style=\"font-weight: bold; text-align: right;\">".format_money($typetotal)."</td>\n";
	$typediff=$typetotal-$r->goal;
	 echo "<td style=\"font-weight: bold; text-align: right;\">".format_money($typediff)."</td>\n";
 	 echo "</tr>\n";

	$totalgoal+=$r->goal;
	$totaldiff+=$typediff;
	echo "<tr><td colspan=\"7\">&nbsp;</td></tr>\n";
 }
 echo "<tr>";
 echo "<td colspan=\"2\"><a onclick=\"return SFIABDialog(event,'fundraising_types.php',400,250)\" href=\"#\">add fund type</a></td>";
 echo "<td colspan=\"4\" style=\"font-weight: bold; text-align: right;\">".i18n("Total Net Position")."</td><td style=\"text-align: right; font-weight: bold;\">".format_money($totaldiff)."</td></tr>\n";
 echo "</table>\n";
 echo "<br />\n";
 echo "<br />\n";
 echo "<a href=\"sponsorship_levels.php\">Manage Sponsorship Levels</a>\n";
 echo "<br />\n";
 echo "<a href=\"sponsors.php\">Manage Sponsors</a>\n";
 echo "<br />\n";

 send_footer();
?>
