<?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
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  require(DIR_WS_CLASSES . 'payment.php');
  $currencies = new currencies();

	$reserve_id=(isset($HTTP_POST_VARS['reservation_id'])?$HTTP_POST_VARS['reservation_id']:array());
	$complete_payment=(isset($HTTP_POST_VARS['complete_payment'])?$HTTP_POST_VARS['complete_payment']:'');
	$action=(isset($HTTP_GET_VARS['action'])?$HTTP_GET_VARS['action']:'');

	if (sizeof($reserve_id)<=0 && $complete_payment=="")
		tep_redirect(tep_href_link(FILENAME_PENDING_WAITLIST_PAYMENTS));
		

	$rows=0;
	$selected_sessions=array();
	for ($icnt=0;$icnt<sizeof($reserve_id);$icnt++){
		$selected_sessions[$reserve_id[$icnt]]=1;
	}
	$orders_id=(isset($HTTP_POST_VARS["orders_id"])?(int)$HTTP_POST_VARS["orders_id"]:0);
	$events_id=(isset($HTTP_POST_VARS["events_id"])?(int)$HTTP_POST_VARS["events_id"]:0);
	
	// get event related details
	$events_query=tep_db_query("SELECT session_style from " . TABLE_EVENTS . " where events_id='" . (int)$events_id ."'");
	$events_result=tep_db_fetch_array($events_query);

	//select customer name and pending confirm amount
	$customer_query=tep_db_query("SELECT customers_name,date_purchased from " . TABLE_ORDERS . " where orders_id='" . (int)$orders_id . "'");
	$customer_result=tep_db_fetch_array($customer_query);

	// fetch the total amount for the event
   
?>
<!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">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
	<!-- body_text //-->	  
	<td width=100% align=left valign="top">
	<table border="0" cellspacing="0" cellpadding="2">
		<tr>
			<td align=left class='pageHeading' colspan=8><?php echo HEADING_TITLE; ?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
      <?php echo tep_draw_form('pending_waitlist_payments_confirm',FILENAME_PENDING_WAITLIST_NEW_ORDER); 
	  		echo '<input type=hidden name="orders_id" value="' . $orders_id . '">';
	  ?>
			<tr>
				<td>
				<table border="0" cellpadding="2" cellspacing="0">
					<tr>
						<td class="tableHeading"><?php echo TEXT_CUSTOMER_NAME;?></td>
						<td class="main">&nbsp;<?php echo $customer_result['customers_name'];?></td>
					</tr>
				</table>
				<td>
			</tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
			</tr>
<?php if ($events_result['session_style']=='E'){ 
	$final_amount=0.0;
	$waitlist_amount=0.0;
	$total_amount=0.0;

	$session_count=0;
	$session_amounts=array();
	$waitlist_query=tep_db_query("select sum(op.final_price) as total from " . 
					TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s  where op.orders_id='" . (int)$orders_id . "' and s.events_id='" . (int)$events_id . "' and op.products_id=s.sessions_id and op.products_type='E' group by op.orders_id");
	$waitlist_result=tep_db_fetch_array($waitlist_query);
	$waitlist_amount=(float)$waitlist_result["total"];
	
	$search_query=	"select op.orders_products_id,op.final_price,op.events_fees,op.products_tax,op.orders_products_id,op.events_type from " . 
					TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s  where op.orders_id='" . (int)$orders_id . "' and s.events_id='" . (int)$events_id . "' and op.products_id=s.sessions_id and op.products_type='E' and ((op.events_type='R' and op.waitlist_type='P') or op.events_type='C')";
	$result_query=tep_db_query($search_query);
	while($result=tep_db_fetch_array($result_query)){
		$session_amount=$result['events_fees'];
		$products_tax=$result['products_tax'];
		if (isset($selected_sessions[$result['orders_products_id']]) || $complete_payment!="") {
			if ($result["events_type"]!='C'){
				$total_amount+=$result['events_fees'];
				$session_amounts[]=array('id'=>$result['orders_products_id'],'current_price'=>$result['final_price'],'original_price'=>$session_amount);
				$session_count++;
			}
		}
	}

	$confirm_amounts=array();
	$confirm_amount=0.0;
	$current_amount=0;
	$confirm_order_query=tep_db_query("SELECT sum(events_fees) as confirm_total,sum(final_price) as confirm_paid,events_fees,count(op.products_id) as confirm_count,op.products_tax,o.date_purchased from " . TABLE_ORDERS_PRODUCTS  . " op," . TABLE_ORDERS . " o, " . TABLE_EVENTS_SESSIONS  . " s where op.waitlist_orders_id='" . (int)$orders_id . "' and s.events_id='" . (int)$events_id . "' and op.products_id=s.sessions_id and op.orders_id=o.orders_id group by op.orders_id");
	while($confirm_result=tep_db_fetch_array($confirm_order_query)){
		$current_amount=$confirm_result['confirm_total']-$confirm_result['confirm_paid'];
		$confirm_amount+=$current_amount;
		if ($confirm_amount>=$waitlist_amount) $confirm_amount=$waitlist_amount;
		$confirm_amounts[]=array('text'=>sprintf(TEXT_CONFIRM_AMOUNT,$confirm_result['confirm_count'],$confirm_result['confirm_count'],$currencies->format(tep_add_tax($confirm_result['events_fees'],$products_tax))),'amount'=>$current_amount,'date'=>$confirm_result['date_purchased']);

		if ($confirm_amount==$waitlist_amount) break;
	}
	if ($total_amount>($waitlist_amount-$confirm_amount))
		$final_amount=$total_amount-($waitlist_amount-$confirm_amount);
?>
			<tr>
				<td>
				<table border="0" cellspacing="0" cellpadding="4" class=formArea>
					<tr>
						<td class="main" width="250"><b><?php echo sprintf(TEXT_TO_BE_PAID_AMOUNT,$session_count,$session_count,$currencies->format(tep_add_tax($session_amount,$products_tax))); ?></b></td>
						<td class="main" width="100" align="right"><?php echo $currencies->format(tep_add_tax($session_count*$session_amount,$products_tax)); ?></td>
					</tr>
					<tr>
						<td class="main" width="250"><b><?php echo '<a href="#waitlist-details" class="blue">' . TEXT_REMAIN_AMOUNT . '<a>'; ?></b></td>
						<td class="main" width="100" align="right"><?php echo ($waitlist_amount-$confirm_amount>0?"-":"") . $currencies->format(tep_add_tax($waitlist_amount-$confirm_amount,$products_tax)); ?></td>
					</tr>
					<tr>
						<td class="main" width="250"></td>
						<td class="main" width="100" align="right"><?php echo tep_draw_separator('pixel_black.gif','60%',1); ?></td>
					</tr>
					<tr>
						<td class="main" width="250"><b><?php echo TEXT_FINAL_AMOUNT; ?></b></td>
						<td class="main" width="100" align="right"><?php echo $currencies->format(tep_add_tax($final_amount,$products_tax)); ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<?php 	$calculate_amount=0; // actual calculated amount
					$prepared_amount=$final_amount; //final prepared amount to be paid
					$updated_amount=$waitlist_amount-$confirm_amount;  // remaining waitlist amount
					for ($icnt=0;$icnt<sizeof($session_amounts);$icnt++){
						$calculate_amount+=$session_amounts[$icnt]['original_price'];
						echo '<input type="hidden" name=orders[' . $icnt . '][id] value="' . $session_amounts[$icnt]['id'] . '">';
						if ($calculate_amount<=$updated_amount){
							$new_price=0;
						} else if ($prepared_amount>$session_amounts[$icnt]['original_price']) {
							$new_price=$session_amounts[$icnt]['original_price'];
							$prepared_amount-=$session_amounts[$icnt]['original_price'];
						} else {
							$new_price=$prepared_amount;
						}
						echo '<input type="hidden" name=orders[' . $icnt . '][amount] value="' . $new_price . '">';
						if ($new_price<=0){
							echo '<input type="hidden" name=orders[' . $icnt . '][type] value="P">';
						} else {
							echo '<input type="hidden" name=orders[' . $icnt . '][type] value="U">';
						}
					}
			?>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
			</tr>
			<tr>
				<td align=left>
					<?php 	echo '<a href="' . tep_href_link(FILENAME_PENDING_WAITLIST_PAYMENTS,'events_id=' . $events_id) . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;';
							echo tep_image_submit('button_confirm.gif',IMAGE_PAY_AMOUNT_CONFIRM); 
			
					?>
				</td>
			</tr>
			<tr height="350">
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td class="pageHeading"><?php echo TEXT_WAITLIST_HEADING;?></td>
			</tr>
			<tr id="waitlist-details">
				<td>
				<table border="0" cellspacing="0" cellpadding="4" class=formArea>
					<tr>
						<td class="main" width="100"><b><?php echo format_date($order_result['date_purchased']); ?></b></td>
						<td class="main" width="250"><b><?php echo TEXT_WAITLIST_AMOUNT; ?></b></td>
						<td class="main" width="100" align="right"><?php echo $currencies->format(tep_add_tax($waitlist_amount,$products_tax)); ?></td>
					</tr>
					<?php for ($icnt=0;$icnt<sizeof($confirm_amounts);$icnt++) {?>
						<tr>
							<td class="main" width="100"><b><?php echo format_date($confirm_amounts[$icnt]['date']); ?></b></td>
							<td class="main" width="300"><b><?php echo $confirm_amounts[$icnt]['text']; ?></b></td>
							<td class="main" width="100" align="right">-<?php echo $currencies->format(tep_add_tax($confirm_amounts[$icnt]['amount'],$products_tax)); ?></td>
						</tr>
					<?php }?>
					<tr>
						<td class="main" width="100">&nbsp;</td>
						<td class="main" width="250"><b><?php echo TEXT_REMAIN_AMOUNT; ?></b></td>
						<td class="main" width="100" align="right"><?php echo $currencies->format(tep_add_tax($waitlist_amount-$confirm_amount,$products_tax)); ?></td>
					</tr>
				</table>
				</td>
			</tr>
<?php } else { // events_style
	$final_amount=0.0;
	$waitlist_amount=0.0;
	$total_amount=0.0;

	$session_count=0;
	$session_amounts=array();
	$search_query=	"select op.orders_products_id,op.final_price,op.events_fees,op.products_tax,op.orders_products_id from " . 
					TABLE_ORDERS_PRODUCTS . " op where op.orders_id='" . (int)$orders_id . "' and op.products_type='E' and op.events_type='R' and op.waitlist_type='P'";
	$result_query=tep_db_query($search_query);
	while($result=tep_db_fetch_array($result_query)){
		$session_amount=$result['events_fees'];
		$products_tax=$result['products_tax'];
		if (isset($selected_sessions[$result['orders_products_id']]) || $complete_payment!="") {
			$waitlist_amount+=$result['final_price'];
			$total_amount+=$result['events_fees'];
			$session_amounts[]=array('id'=>$result['orders_products_id'],'current_price'=>$result['final_price'],'original_price'=>$session_amount);
			$session_count++;
		}
	}
	$final_amount=$total_amount-$waitlist_amount;
?>
			<tr>
				<td>
				<table border="0" cellspacing="0" cellpadding="4" class=formArea>
					<tr>
						<td class="main" width="250"><b><?php echo sprintf(TEXT_TO_BE_PAID_AMOUNT,$session_count,$session_count,$currencies->format(tep_add_tax($session_amount,$products_tax))); ?></b></td>
						<td class="main" width="100" align="right"><?php echo $currencies->format(tep_add_tax($session_count*$session_amount,$products_tax)); ?></td>
					</tr>
					<tr>
						<td class="main" width="250"><b><?php echo TEXT_WAITLIST_SESSION; ?></b></td>
						<td class="main" width="100" align="right"><?php echo ($waitlist_amount-$confirm_amount>0?"-":"") . $currencies->format(tep_add_tax($waitlist_amount-$confirm_amount,$products_tax)); ?></td>
					</tr>
					<tr>
						<td class="main" width="250"></td>
						<td class="main" width="100" align="right"><?php echo tep_draw_separator('pixel_black.gif','60%',1); ?></td>
					</tr>
					<tr>
						<td class="main" width="250"><b><?php echo TEXT_FINAL_AMOUNT; ?></b></td>
						<td class="main" width="100" align="right"><?php echo $currencies->format(tep_add_tax($final_amount,$products_tax)); ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<?php 	$calculate_amount=$final_amount;
					$new_price=0;
					for ($icnt=0;$icnt<sizeof($session_amounts);$icnt++){
						echo '<input type="hidden" name=orders[' . $icnt . '][id] value="' . $session_amounts[$icnt]['id'] . '">';
						$new_price=$session_amounts[$icnt]['original_price']-$session_amounts[$icnt]['current_price'];
						echo '<input type="hidden" name=orders[' . $icnt . '][amount] value="' . $new_price . '">';
						echo '<input type="hidden" name=orders[' . $icnt . '][type] value="U">';
						$calculate_amount-=$new_price;
					}
			?>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
			</tr>
			<tr>
				<td align=left>
					<?php 	echo '<a href="' . tep_href_link(FILENAME_PENDING_WAITLIST_PAYMENTS,'events_id=' . $events_id) . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;';
							echo tep_image_submit('button_confirm.gif',IMAGE_PAY_AMOUNT_CONFIRM); 
			
					?>
				</td>
			</tr>
<?php }?>
		</table>
		</td>
	</tr>
	</form>
	</table>
	</td>
</tr>
</table>
<!-- body_text_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
<?php

function update_totals($order_id)
{
	$order = new order($order_id);
	
	$RunningSubTotal = 0;
	$RunningTax = 0;
	
	for ($i=0; $i<sizeof($order->products); $i++)
	{
	$RunningSubTotal += ($order->products[$i]['qty'] * $order->products[$i]['final_price']);
	$RunningTax += (($order->products[$i]['tax'] / 100) * ($order->products[$i]['qty'] * $order->products[$i]['final_price']));			
	}
	
		$Query = "update " . TABLE_ORDERS_TOTAL . " set
		text = '\$" . number_format($RunningTax, 2, '.', ',') . "',
		value = '" . $RunningTax . "'
		where class='ot_tax' and orders_id=$order_id";
	tep_db_query($Query);
	
	// Sub-Total
	$Query = "update " . TABLE_ORDERS_TOTAL . " set
		text = '\$" . number_format($RunningSubTotal, 2, '.', ',') . "',
		value = '" . $RunningSubTotal . "'
		where class='ot_subtotal' and orders_id=$order_id";
	tep_db_query($Query);
	
	// Total
	$Query = "select sum(value) as total_value from " . TABLE_ORDERS_TOTAL . " where class != 'ot_total' and orders_id=$order_id";
	$result = tep_db_query($Query);
	$row = tep_db_fetch_array($result);
	$Total = $row["total_value"];
	
	//$Total=$RunningSubTotal+$RunningTax;
	$Query = "update " . TABLE_ORDERS_TOTAL . " set
		text = '<b>\$" . number_format($Total, 2, '.', ',') . "</b>',
		value = '" . $Total . "'
		where class='ot_total' and orders_id=$order_id";
	tep_db_query($Query);
}
?>