<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/

	require('includes/application_top.php');
	require('includes/classes/currencies.php');
	require('includes/functions/message_functions.php');

	$currencies=new currencies();
	
	$action=isset($HTTP_GET_VARS["action"])?$HTTP_GET_VARS["action"]:'';
	$post_action=isset($HTTP_POST_VARS["post_action"])?$HTTP_POST_VARS["post_action"]:'';
	$mode=isset($HTTP_GET_VARS["mode"])?$HTTP_GET_VARS["mode"]:'';
	$found_results=false;
	
	if ($post_action=="pay_selected"){ // store entry for indication of payment request from admin
			if (isset($HTTP_POST_VARS["entry_id"]) && isset($HTTP_POST_VARS["pay_details"])){
				$entry_id=&$HTTP_POST_VARS["entry_id"];
				for ($icnt=0;$icnt<sizeof($entry_id);$icnt++){
					$pay_details=&$HTTP_POST_VARS["pay_details"][$entry_id[$icnt]];
					$sql_array=array(	"sessions_id"=>$pay_details["sessions_id"],
										"events_id"=>$pay_details["events_id"],
										"instructor_id"=>$pay_details["instructor_id"],
										"total_amount"=>$pay_details["total_amount"]
										);
					tep_db_perform(TABLE_EVENTS_INSTRUCTOR_PAYMENTS,$sql_array);
					$ids.=tep_db_insert_id() . ",";
				}
				if ($ids!="") tep_message_payment_request(substr($ids,0,-1));
			}
			tep_redirect(tep_href_link(FILENAME_EVENTS_PAYMENTS,'mode=2'));
	}
	// get search params $mode=1 -> get params from session else get from form parameters
	if ($mode!="1" && session_is_registered("search_payment")){
		//get query option from session
		$search_params=&$HTTP_SESSION_VARS["search_payment"];
		$action=search;
	} else {
		$search_params=array(	"start_date"=>isset($HTTP_POST_VARS["start_date"])?$HTTP_POST_VARS["start_date"]:'',
								"end_date"=>isset($HTTP_POST_VARS["end_date"])?$HTTP_POST_VARS["end_date"]:'',
								"events_id"=>isset($HTTP_POST_VARS["events_id"])?(int)$HTTP_POST_VARS["events_id"]:0
								);
	}


	if ($action=="search") {	// if action is going to search
			$query="SELECT s.events_id,s.start_date,op.products_id,op.products_name,e.instructors_rate_type,e.instructors_rate,e.instructors_base_rate,e.events_instructor from " .
							TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s, " .  TABLE_EVENTS . " e " .
							"where op.products_id=s.sessions_id and s.events_id=e.events_id and op.products_type='E' and op.events_type='R' and e.events_instructor=" . $login_id;
			//get from form submit
			$search_option="";
			if ($search_params["start_date"]!='') $search_option.=" and s.start_date>='" . $search_params["start_date"] . "'";
			if ($search_params["end_date"]!='') $search_option.=" and s.start_date<='" . $search_params["end_date"] . "'";
			if ($search_params["events_id"]!='' && ((int)$search_params["events_id"]>0)) $search_option.=" and s.events_id='" . $search_params["events_id"] . "'";

			$search_option.=" and e.events_instructor='" . $login_id . "'";
			
			if (!$HTTP_SESSION_VARS["search_payment"]) tep_session_register("search_payment");
	
			tep_set_session_value("search_payment",$search_params);
	
			$query.=$search_option . " group by s.sessions_id,s.events_id order by op.products_name,s.start_date";
	
			//query for results
			$payment_query=tep_db_query($query);
	
			if (tep_db_num_rows($payment_query)>0) $found_results=true;
	}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
	<!-- header //-->
	<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
	<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
	<!-- body_text //-->
	<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
	<script language="JavaScript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
	<script language="javascript"><!--
	  var dateStart = new ctlSpiffyCalendarBox("dateStart", "search_payment", "start_date","btnDate1","<?php echo $search_params["start_date"];?>",scBTNMODE_CUSTOMBLUE);
	  var dateEnd = new ctlSpiffyCalendarBox("dateEnd", "search_payment", "end_date","btnDate2","<?php echo $search_params["end_date"];?>",scBTNMODE_CUSTOMBLUE);
	//-->
	</script>
	<td width="100%" valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="0">

		<tr><td class="pageHeading"><?php echo HEADING_FIND_PAYMENTS;?></td></tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
		<td>
		<table border="0" cellspacing="2" cellpadding="2" class="formArea">
			<?php echo tep_draw_form("search_payment",FILENAME_EVENTS_PAYMENTS,"action=search&mode=1");?>
			<tr>
				<td class="main"><?php echo TEXT_START_DATE;?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '1'); ?><script language="javascript">dateStart.writeControl(); dateStart.dateFormat="yyyy-MM-dd";</script></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '60', '1') . TEXT_END_DATE;?></td>
				<td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '1'); ?><script language="javascript">dateEnd.writeControl(); dateEnd.dateFormat="yyyy-MM-dd";</script></td>
			</tr>
			<tr>
				<td class="main"><?php echo TEXT_SELECT_EVENT;?></td>
				<td class="main">
				<?php 
					$events_query=tep_db_query("SELECT ed.events_name,e.events_id from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_ADMIN . " a where e.events_id=ed.events_id and e.events_instructor=a.admin_id and a.admin_id='" . $login_id . "'");
					$events_array=array();
					
					while($events_result=tep_db_fetch_array($events_query)){
						$events_array[]=array("id"=>$events_result["events_id"],"text"=>$events_result["events_name"]);
					}
					echo tep_draw_separator('pixel_trans.gif', '24', '1') . tep_draw_pull_down_menu("events_id",$events_array,$search_params["events_id"]);
				?>
				</td>
				<td colspan="2" class="main" valign="middle"><?php echo tep_draw_separator('pixel_trans.gif', '60', '1') . tep_draw_checkbox_field("hidden",1) . '&nbsp;' . TEXT_SHOW_HIDDEN;?></td>
			</tr>
			<tr>
				<td colspan="4"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
			</tr>
			<tr>
				<td colspan="4" align="left"><?php echo tep_image_submit("button_search.gif",IMAGE_SEARCH_PAYMENTS); ?></td>
			</tr>
			<?php echo "</form>";?>
		</table>
		</td>
		</tr>
	<?php if ($action=="search") { ?>	
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>		
		</tr>
		<tr><td class="pageHeading"><?php echo HEADING_PAYMENTS;?></td></tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<script language="javascript">
			function validatePayForm(frm){
				var error="";
				var icnt;
				if (!frm.select_agree.checked){
					error="<?php echo addslashes(stripslashes(ERR_SELECT_AGREE));?>";
				} else if (frm.elements["entry_id[]"]){
					if (frm.elements["entry_id[]"].length){
						for (icnt=0;icnt<frm.elements["entry_id[]"].length;icnt++){
							if (frm.elements["entry_id[]"][icnt].checked) break;
						}
						if (icnt>=frm.elements["entry_id[]"].length) error="<?php echo addslashes(stripslashes(ERR_SELECT_DATES));?>";
					} else {
						if (!frm.elements["entry_id[]"].checked) error="<?php echo addslashes(stripslashes(ERR_SELECT_DATES));?>";
					}
				} else {
					error="<?php echo addslashes(stripslashes(ERR_SELECT_DATES));?>";
				}
				if (error!=""){
					alert(error);
					return false;
				}
				return true;
			}
		</script>
		<?php if ($found_results){ 
				echo tep_draw_form("pay_selected",FILENAME_EVENTS_PAYMENTS,'','post','onSubmit="javascript:return validatePayForm(this);"');
				echo '<input type=hidden name="post_action" value="pay_selected">';
		?>
		<tr>
		<td>
		<table width="100%" border="0" cellspacing="2" cellpadding="2" class="formArea">
			<tr>
				<td class="tableHeading" width="20"> </td>
				<td class="tableHeading" width="20"> </td>
				<td class="tableHeading" width="60"><?php echo TABLE_DATE;?></td>
				<td class="tableHeading"><?php echo TABLE_EVENT_NAME;?></td>
				<td class="tableHeading" width="80"><?php echo TABLE_AMOUNT;?></td>
				<td class="tableHeading" align="center" width="40"><?php echo TABLE_PAID;?></td>
				<td class="tableHeading" align="center" width="40"><?php echo TABLE_QUERIED;?></td>
				<td class="tableHeading" align="center" width="100"><?php echo TABLE_ATTENDEES;?></td>
			</tr>
			<?php 	$row_cnt=1;
					$entry_cnt=0;
					while($payment_result=tep_db_fetch_array($payment_query)) {
						//check
						$count_query=tep_db_query("SELECT count(*) as total from " . TABLE_EVENTS_ATTENDEES_CONFIRM . " where sessions_id='" . $payment_result["products_id"] . "'");
						$count_result=tep_db_fetch_array($count_query);
						$mode=((int)$count_result["total"]>0?2:1);
						if ($row_cnt%2==0) $class="attributes-even";
						else $class="attributes-odd";
						$table=($mode==1?TABLE_EVENTS_ATTENDEES:TABLE_EVENTS_ATTENDEES_CONFIRM);
						$total_amount=tep_get_instructor_event_rate($payment_result,$payment_result["products_id"],$table);

						$check_query=tep_db_query("SELECT * from " . TABLE_EVENTS_INSTRUCTOR_PAYMENTS . " where instructor_id='" . $payment_result["events_instructor"] . "' and sessions_id='" . $payment_result["products_id"] . "'");
						$pay_request=(tep_db_num_rows($check_query)>0?true:false);
						$check_result=tep_db_fetch_array($check_query);
						$paid=($check_result["paid"]=='Y'?true:false);
						
						$queried_query=tep_db_query("SELECT count(*) as total from " . $table . " where sessions_id='" . $payment_result["products_id"] . "' and queried='Y'");
						$queried_result=tep_db_fetch_array($queried_query);

			?>
					<tr class="<?php echo $class;?>">
						<td class="main" width="20">
							<?php 	if ($mode==2 && !$pay_request && $queried_result["total"]<=0) {
										echo '<input type="checkbox" name="entry_id[]" value="' . $entry_cnt . '">';
										echo '<input type="hidden" name="pay_details[' . $entry_cnt . '][sessions_id]" value="' . $payment_result["products_id"] . '">';
										echo '<input type="hidden" name="pay_details[' . $entry_cnt . '][events_id]" value="' . $payment_result["events_id"] . '">';
										echo '<input type="hidden" name="pay_details[' . $entry_cnt . '][instructor_id]" value="' . $payment_result["events_instructor"] . '">';
										echo '<input type="hidden" name="pay_details[' . $entry_cnt . '][total_amount]" value="' . $total_amount . '">';
										$entry_cnt++;
									}
							?>
						</td>
						<td class="main" valign="top"><?php if (!$paid) echo '<a href="' . tep_href_link(FILENAME_EVENTS_PAYMENTS_DETAIL,'eID=' . $payment_result['events_id'] . '&sID=' . $payment_result['products_id'] . '&mode=' . $mode . '&default_entry=1') . '">' . tep_image(DIR_WS_ICONS . 'image_edit.gif',IMAGE_VIEW_PAYMENT) . '</a>';?></td>
						<td class="main" valign="top"><?php echo format_date($payment_result["start_date"],true);?></td>
						<td class="main" valign="top"><?php echo $payment_result["products_name"];?></td>
						<td class="main" align="right" valign="top"><?php echo $currencies->format($total_amount);?></td>
						<td class="main" align="center" valign="top">
							<?php 	if ($paid)
										echo tep_image(DIR_WS_IMAGES . 'icon_status_green.gif',IMAGE_PAID,'','','align=absmiddle');
									else if ($pay_request)
										echo tep_image(DIR_WS_IMAGES . 'icon_status_yellow.gif',IMAGE_UNPAID_REQUEST,'','','align=absmiddle');
									else
										echo tep_image(DIR_WS_IMAGES . 'icon_status_red.gif',IMAGE_UNPAID_UNREQUEST,'','','align=absmiddle');
							?>
						</td>
						<td class="main" align="center" valign="top">
							<?php 	if ($queried_result["total"]>0) {
										echo tep_image(DIR_WS_IMAGES . 'icon_status_red.gif',IMAGE_QUERY);
									} else {
										echo tep_image(DIR_WS_IMAGES . 'icon_status_red_light.gif',IMAGE_NOT_QUERY);
									}
							?>
						</td>
						<td class="main" align="center" valign="top">
							<?php 
								if ($mode==1) // submitted the attendees for this date
									echo tep_image(DIR_WS_IMAGES . 'icon_status_green_light.gif',IMAGE_NOT_SUBMITED);
								else
									echo tep_image(DIR_WS_IMAGES . 'icon_status_green.gif',IMAGE_NOT_SUBMITED);
							?>
						</td>
					</tr>
			<?php $row_cnt++;} ?>
		</table>
		</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		<tr>
			<td>
			<table width="75%" border="0" cellpadding="0" cellspacing="0">
				<tr valign="top">
					<td class="main" valign="middle"><?php echo tep_draw_checkbox_field("select_agree",1);?></td>
					<td class="smallText" valign="middle"><?php echo TEXT_AGREE_SELECTED;?></td>
				</tr>
				<tr>
					<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
				</tr>
				<tr>
					<td colspan="2"><?php echo tep_image_submit("button_pay_selected.gif",IMAGE_PAY_SELECTED);?></td>
				</tr>
			</table>
		</tr>
		</form>
		<?php 
		} else { // found results ?>
		<tr><td class="main"><?php echo NO_RESULTS;?></td></tr>		
	<?php
		} // found results
	}  //search if
	?>
	</table>
	</td>
	<!-- body_eof //-->
	</tr>
</table>
<!-- body_eof //-->
	<!-- footer //-->
	<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
	<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
