<?php
session_start();
include("header.php");
setlocale(LC_MONETARY, "en_US");


if(trim($_SESSION['accountName'])=="" &&  trim($_SESSION['accountID'])=="" && trim($_SESSION['pgid'])!="i3g3e3nious7")
{
	echo '<script>';
	echo 'location.replace("index.php");';
	echo '</script>';
}
else
{

$sy=0;
$school=0;
$sDesc="";
$mDesc="";
$action=$_POST["action"];

if(trim($_POST["sy"])!="")
	$sy=$_POST["sy"];
if(trim($_POST["school"])!="")
	$school=$_POST["school"];

if(strtoupper($action)== "SUBMIT")
{
			$totSale="0.00";
			$strSql =@mysql_query("SELECT sum(a.total) tot_sale FROM tr_orders a left join tr_student b on b.student_id=a.student_id where b.sy='".$sy."' and b.school_id='".$school."' and a.paid_status='1'");
			
			if($school==0)
			{
				$strSql =@mysql_query("SELECT sum(a.total) tot_sale FROM tr_orders a left join tr_student b on b.student_id=a.student_id where b.sy='".$sy."' and a.paid_status='1'"); 

				$cnt=@mysql_num_rows($strSql);		
				if($cnt>0)
				{
					$rs1=@mysql_fetch_array($strSql);
					if(trim($rs1["tot_sale"])!="")
					{
						//$totSale=FormatCurrency($rs["tot_sale"],2);
						$totSale=number_format($rs1["tot_sale"], 2, '.', ',');
					}
				}
			}
			
				if($school!='0' && $sy!='0')
				{
					$strSql2 =@mysql_query("SELECT syName FROM ma_schoolyear where sstatus=1 and syID='".$sy."'");
					$cntr=@mysql_num_rows($strSql2);
					
					If($cntr>0)
					{
						$rs=@mysql_fetch_array($strSql2);
						$yYear=substr(trim($rs["syName"]),0,4);
						$jMonth="";
						$jYear="";
						//For i = 1 To 10
						for($i=1;$i<=10;$i++)
						{

								$fdate=strtotime($yYear.'-08-01');

								$odate=strtotime("+".$i." months",$fdate);
								$OrderDate=date("Y-m-d",$odate);
								$ordt=explode("-",$OrderDate);
								$ordtf=$ordt[0]."-".$ordt[1]."-% 00:00:00";
								
	
							//$jMonth = Month(DateAdd("m",$i,"1-Aug-".$yYear));
							//$jMonth = Month(DateAdd("m",$i,"1-Aug-".$yYear));
							//$jYear = Year(DateAdd("m",$i,"1-Aug-".$yYear));

							$ttSale="0.00";
							
							$strSql1 =@mysql_query("SELECT sum(a.total) ttSale FROM tr_orders a left join tr_student b on b.student_id=a.student_id where b.sy=".$sy." and b.school_id=".$school." and a.order_date Like '".$ordtf."' and a.paid_status='1'");
							$cntrs=@mysql_num_rows($strSql1);
							if($cntrs>0)
							{
								$rs2=@mysql_fetch_array($strSql1);
								if(trim($rs2["ttSale"])!="")
								{
									$ttSale=number_format($rs2["ttSale"], 2, '.', ',');
									$totSal+=$rs2["ttSale"];
									$totSale=number_format($totSal, 2, '.', ',');
									
								}
							}
							
							$mDesc=$mDesc."<tr class='fs14 black'><td align='right'>".date('F',strtotime($OrderDate))." ".$ordt[0].":&nbsp;</td><td><b>$".$ttSale ."</b></td></tr>";
						}			
					}
					
				}
			//}
			$sDesc="<tr><td colspan='2'>&nbsp;</td></tr><tr class='fs14 black'><td align='right'>Total Sales:&nbsp;</td><td><u><b>$".$totSale ."</b></u></td></tr>";
			$sDesc=$sDesc."<tr><td colspan='2'><hr/></td></tr>";
}
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr><td></td><td align="center" class="fs14 black"><u><b>School Order Sales</b></u></td></tr>
	<tr><td width="200" class="fs12" valign="top">
	<?php
	include("sidemenu.php");
	?>
	</td>
		<td width="700" class="fs12" valign="top">
		<form action="schoolOrderSales.php" method="post" name="frm">
			<table width="680" cellpadding="1" cellspacing="1" border="0">
				<tr><td height="5" width="50%"></td><td height="5" width="50%"></td></tr>
				<tr><td colspan="2" class="errmsg"><hr/><?=$errmsg?></td></tr>
				<tr><td align="right">School Year:&nbsp;</td>
					<td><select name="sy" class="formSelect">
					<?php
						$strSql=@mysql_query("select syID,syName from ma_schoolyear where sstatus=1 order by syID desc");
						$cntr=@mysql_num_rows($strSql);
						if($cntr>0)
						{
								while($rs=@mysql_fetch_array($strSql))
								{
									
									if($sy==$rs["syID"])
									{
										echo "<option value=".$rs["syID"]." selected>".$rs["syName"]."</option>";
									}
									else
									{
										echo "<option value=".$rs["syID"].">".$rs["syName"]."</option>";
									}
								}
								
						}
						
						?>
						</select></td></tr>
				<tr><td align="right">Select School:&nbsp;</td>
					<td><select name="school" class="formSelect">

					<?php			
						$strSql=@mysql_query("select school_id,school_name from tr_schools where status=1");
						$cnt2=@mysql_num_rows($strSql);
						if($cnt2>0)
						{
							echo '<option value="0">--All Schools--</option>';
							while($rs=@mysql_fetch_array($strSql))
							{
								if($school==$rs['school_id'])
									echo "<option value=".$rs['school_id']." selected>".$rs['school_name']."</option>";
								else
									echo "<option value=".$rs['school_id'].">".$rs['school_name']."</option>";

							}
						}

					?>
						</select></td></tr>
				<tr><td></td><td align="center"><input class="fs12" type="submit" name="action" value="SUBMIT" /></td></tr>
				<tr><td colspan="2"><hr/></td></tr>
				<tr><td colspan="2" height="15"></td></tr>
				<?=$mDesc?>
				<?=$sDesc?>
			</table>
		</form></td></tr>
</table>
<?php
include("footer.php");
}
?>