<?php
session_start();
include("header.php");

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

$dayAdd=0;
$prv = "";
$nxt = "";
$school=0;

if(trim($_POST["school"])!="")
	$school=$_POST["school"];
else if(trim($_GET["schl"])!="")
	$school=$_GET["schl"];

if(trim($_GET["da"])!="")
{
	$dayAdd = $_GET["da"];
	//$OrderDate = FormatDateTime(DateAdd("d",$dayAdd,Date()),1);
}

if($dayAdd > 0)
{
	$Addday=$dayAdd-1;
	$prv = "<a href='schoolPlist.php?schl=".$school."&da=".$Addday."'>prev</a>";
}
if($dayAdd <= 30)
{
	$Addday=$dayAdd+1;
	$nxt = "<a href='schoolPlist.php?schl=".$school."&da=".$Addday."'>next</a>"; 
}

$fdate=strtotime(date('Y-m-d'));
	$odate=strtotime("+".$dayAdd." day",$fdate);
	$OrderDate=date("Y-m-d",$odate);
	
	$dayn=date("l",strtotime($OrderDate));

	$ccdate=explode("-",$OrderDate);

	$monthNum = $ccdate[1];
	$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));

	$sDate =$dayn.", ". $monthName ." ". $ccdate[2].", ".$ccdate[0];
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
	<tr><td></td><td colspan="2" align="center" class="fs14 black"><u><b>School Packing List</b></u></td></tr>
	<form action="schoolPlist.php?schl=<?=$school?>&da=<?=$dayAdd?>" method="POST" name="frm">
	<tr><td rowspan="4" width="200" class="fs12" valign="top"><?php include("sidemenu.php");?></td>
		<td width="350" align="right" class="fs12"><b>School Name:&nbsp;&nbsp;</b></td>
		<td width="350"><select name="school" class="formSelect" onChange="javascript:{if(this.value!='') submit();}">
		<?php
		$strSql = mysql_query("select school_id,school_name from tr_schools where status=1");
		$resc=@mysql_num_rows($strSql);
		if($resc>0)
		{
			echo "<option value='0'>--Select--</option>";

			while($rs=@mysql_fetch_array($strSql))
			{		
				if($school==$rs['school_id'])
					echo "<option value='".trim($rs['school_id'])."' selected>".$rs['school_name']."</option>";
				else
					echo "<option value='".trim($rs['school_id'])."' >".$rs['school_name']."</option>";
			}
		}
		else
		{
			$school=0;
		}
		?></select></td></tr>
	</form>
	<tr class="fs12"><td align="right"><b>Order Date:&nbsp;&nbsp;</b></td><td><b><?=$sDate?></b></td></tr>
	<tr class="fs12"><td align="center" colspan="2"><?=$prv?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?=$nxt?></td></tr>
	<tr><td width="700" class="fs12" colspan="2">
			<table width="680" cellpadding="1" cellspacing="1" border="0">
				<tr valign="top"><td width="15%" height="10"></td><td width="50%"></td><td width="28%"></td><td width="7%" height="10"></td></tr>
				<tr valign="top"><td colspan="4"><hr /></td></tr>
				<tr><td><b>Menu Item</b></td><td><b>Description</b></td><td><b>Notes</b></td><td align="center"><b>Orders</b></td></tr>
				<tr><td colspan="4" bgcolor="#000000" height="1"></td></tr>
				<?php

				$ordate1=$OrderDate." 00:01:01";
				$birthd="%-".$ccdate[1]."-".$ccdate[2]." 00:00:00";

			$strSql =@mysql_query("SELECT count(*) cnt,a.service_date,a.item_ordered FROM tr_orderdetails a join tr_orders b on b.order_id=a.order_id join tr_student c on c.student_id=b.student_id where a.service_date='".$ordate1."' and c.school_id='". $school."' and b.paid_status='1' group by a.service_date,a.item_ordered order by a.item_ordered");

			$cntr=@mysql_num_rows($strSql);
			if($cntr>0)
			{
				$tCnt = 0;
				$cCmplt = 0;
				$bDayDcnt=0;
				

				$strSql1 = @mysql_query("SELECT count(*) cCountBDAY FROM tr_orderdetails a left join tr_orders b on b.order_id=a.order_id left join tr_student c on c.student_id=b.student_id where c.school_id=".$school." and a.service_date='".$ordate1."' and c.date_birth='".$birthd."' and b.paid_status='1'");

				$cntr1=@mysql_num_rows($strSql1);
				
				if($cntr1>0)
				{
					$rs1=@mysql_fetch_array($strSql1);
					$bDayDcnt=$rs1["cCountBDAY"];
				}
				
				while($rs=@mysql_fetch_array($strSql))
				{
						$tCnt=$tCnt+$rs["cnt"];
						
						$srtSql2 =@mysql_query("SELECT count(*) cnt,a.item_type,b.menu_name,b.menu_code FROM tr_orderdetails a join tr_menu b on b.menu_id=a.item_ordered join tr_orders c on c.order_id=a.order_id join tr_student d on d.student_id=c.student_id where a.service_date='".$ordate1."' and d.school_id=".$school." and a.item_ordered=".$rs["item_ordered"]." and c.paid_status='1' and item_type in(1,2) group by service_date,item_ordered,item_type order by item_type");
						$cntr2=@mysql_num_rows($srtSql2);
						if($cntr2>0)
						{
							//$rs2=@mysql_fetch_array($srtSql2);
							$rc=0;
							$i=1;
							while($rs2=@mysql_fetch_array($srtSql2))
							{
								if($i==1)
								{
									echo "<tr valign='top'><td>".trim(strtoupper($rs2["menu_code"]))."</td><td>".trim($rs2["menu_name"])."</td><td>Regular & Complete</td>";
								}
								if($rs2["item_type"]==2)
								{
									$cCmplt=$cCmplt+$rs2["cnt"];
								
								}
								$rc=$rc+$rs2["cnt"];
								$i++;
								
							}
							echo "<td align='center'>".$rc."</td></tr>";
						}
						
						$strSql3 =@mysql_query("SELECT count(*) cnt,b.menu_name,b.menu_code FROM tr_orderdetails a join tr_menu b on b.menu_id=a.item_ordered join tr_orders c on c.order_id=a.order_id join tr_student d on d.student_id=c.student_id where service_date='".$ordate1."' and d.school_id='".$school."' and item_ordered='".$rs['item_ordered']."' and c.paid_status='1' and item_type=3 group by service_date,item_ordered,item_type order by item_type");
						$cntr3=@mysql_num_rows($strSql3);
						if($cntr3>0)
						{
							$rs3=@mysql_fetch_array($strSql3);
							$cCmplt=$cCmplt+$rs3["cnt"];
							echo "<tr valign='top'><td>".strtoupper($rs3["menu_code"])."</td><td>".trim($rs3["menu_name"])."</td><td>Extra Large Complete</td><td align='center'>".$rs3["cnt"]."</td></tr>";
						}
						
						$srtSql4 =@mysql_query("SELECT count(*) cnt,a.item_type,b.menu_name,b.menu_code FROM tr_orderdetails a join tr_menu b on b.menu_id=a.item_ordered join tr_orders c on c.order_id=a.order_id join tr_student d on d.student_id=c.student_id where a.service_date='".$ordate1."' and d.school_id='".$school."' and a.item_ordered=".$rs["item_ordered"]." and c.paid_status='1' and item_type in(4,5) group by service_date,item_ordered,item_type order by item_type");
						$cntr4=@mysql_num_rows($srtSql4);
						if($cntr4>0)
						{
							$rc=0;
							$j=1;
							while($rs4=@mysql_fetch_array($srtSql4))
							{
								if($j==1)
								{
									echo "<tr valign='top'><td>".strtoupper($rs4["menu_code"])."</td><td>".trim($rs4["menu_name"])."</td><td>Regular & Complete Halal</td>";
								}
								if($rs4["item_type"]==5)
								{
									$cCmplt=$cCmplt+$rs4["cnt"];
									
								}
								$rc=$rc+$rs4["cnt"];
								$j++;
								
							}
							echo "<td align='center'>".$rc."</td></tr>";
						}
						
						$strSql5 =@mysql_query("SELECT count(*) cnt,b.menu_name,b.menu_code FROM tr_orderdetails a join tr_menu b on b.menu_id=a.item_ordered join tr_orders c on c.order_id=a.order_id join tr_student d on d.student_id=c.student_id where service_date='".$ordate1."' and d.school_id=".$school." and item_ordered=".$rs["item_ordered"]." and c.paid_status='1' and item_type=6 group by service_date,item_ordered,item_type order by item_type");
						$cntr5=@mysql_num_rows($strSql5);
						if($cntr5>0)
						{
							$rs5=@mysql_fetch_array($strSql5);
							$cCmplt=$cCmplt+$rs1["cnt"];
							echo "<tr valign='top'><td>".strtoupper($rs5["menu_code"])."</td><td>".trim($rs5["menu_name"])."</td><td>Extra Large Complete Halal</td><td align='center'>".$rs5["cnt"]."</td></tr>";
						}
						
				}
				
				$strSqls =@mysql_query("SELECT count(*) cnts,a.service_date,a.item_snack FROM tr_orderdetails a join tr_orders b on b.order_id=a.order_id join tr_student c on c.student_id=b.student_id where a.service_date='".$ordate1."' and b.paid_status='1' and c.school_id=".$school." group by a.service_date,a.item_snack order by a.item_snack");
				echo "<tr><td colspan='4'><hr /></td></tr>";
				echo "<tr><td colspan='2'></><td colspan='2'><b>Snacks</b></td></tr>";
				echo "<tr><td colspan='4' bgcolor='#000000' height='1'></td></tr>";
				$cnts=@mysql_num_rows($strSqls);
				if($cnts>0)
				{
					while($rss=@mysql_fetch_array($strSqls))
					{
						echo "<tr valign='top'><td colspan='2'></td><td>".getSD($rss["item_snack"])."</td><td align='center'>".$rss["cnts"]."</td></tr>";
						
					}
				}
				
				$strSqld =@mysql_query("SELECT count(*) cnts,a.service_date,a.item_drink FROM tr_orderdetails a join tr_orders b on b.order_id=a.order_id join tr_student c on c.student_id=b.student_id where a.service_date='".$ordate1."' and b.paid_status='1' and c.school_id=".$school." group by a.service_date,a.item_drink order by a.item_drink");
				echo "<tr><td colspan='4'><hr /></td></tr>";
				echo "<tr><td colspan='2'></><td colspan='2'><b>Drinks</b></td></tr>";
				echo "<tr><td colspan='4' bgcolor='#000000' height='1'></td></tr>";
				$cntd=@mysql_num_rows($strSqld);
				if($cntd>0)
				{
					while($rsd=@mysql_fetch_array($strSqld))
					{
						echo "<tr valign='top'><td colspan='2'></td><td>".getSD($rsd["item_drink"])."</td><td align='center'>".$rsd["cnts"]."</td></tr>";
						
					}
				}
				
				echo "<tr><td colspan='4'><hr/></td></tr>";
				echo "<tr><td colspan='4' bgcolor='#000000' height='1'></td></tr>";
				echo "<tr valign='top'><td></td><td><a href='#' onClick='javascript:window.open(\"schoolPlistReport.php?schl=".$school."&da=".$dayAdd."\",\"Print Report\",\"menubar=1,resizable=1,width=800,height=800,scrollbars=1\");' >View Print Report</a>&nbsp;&nbsp;</td>";
				echo "<td align='right'><b>Total Orders:</b>&nbsp;&nbsp;</td><td align='center'><b>".$tCnt."</b></td></tr>";
				echo "<tr valign='top'><td colspan='2'></td><td align='right'><b>Total Complete Count:</b>&nbsp;&nbsp;</td><td align='center'><b>".$cCmplt ."</b></td></tr>";

				echo "<tr valign='top'><td colspan='2'></td><td align='right'><b>Total Birthday Count:</b>&nbsp;&nbsp;</td><td align='center'><b>".$bDayDcnt."</b></td></tr>";
			}
			else
			{
				echo "<tr><td colspan='4' align='center' height='100'>There's no Order for this Date!</td></tr>";
				echo "<tr><td colspan='4'><hr /></td></tr>";
			}
			 ?>
			</table></td></tr>
</table>
<?php
include("footer.php");
}
?>