<?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
*/
 // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); 

define('MES_FTN','First Name');
define('MES_LTN','Last Name');
define('MES_PHN','Phone Number');
define('MES_STD','Start Date');
define('MES_END','End Date');
define('MES_PAD','Paid');
define('MES_PAT','Payment Type');
define('MES_EVA','Event Name');
define('MES_SAT','Session');
define('MES_INS','Instructor');
define('QUR_ORD','Order Id');
define('QUR_EVA','Order Event Name');
define('QUR_STD','Order Start Date');
define('QUR_END','Order End Date');

	// send internal message regarding new customer
	function tep_message_add_customer($entry_ids){
		global $events_id,$sessions_id,$login_groups_type,$top_admin_email,$login_email,$languages_id;
		if ($entry_ids=="") return;

		$fetch_query=tep_db_query(	"SELECT events_name,e.events_instructor from " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS .  " e where ed.events_id='" . $events_id . "' and language_id='" . $languages_id . "' and " .
									" e.events_id=ed.events_id");


		$fetch_result=tep_db_fetch_array($fetch_query);
		$events_name=$fetch_result["events_name"];
		$message_to=array();
		if ($login_groups_type==TEXT_RESERVATIONMANAGER_ENTRY){
			$message_to[]=tep_get_instructor_email($fetch_result["events_instructor"]);
		} else {
			$admin_query=tep_db_query("SELECT a.admin_email_address from " . TABLE_ADMIN . " a, " . TABLE_ADMIN_GROUPS . " ag where a.admin_groups_id=ag.admin_groups_id and ag.admin_groups_name='" . TEXT_RESERVATIONMANAGER_ENTRY . "'");
			while($admin_result=tep_db_fetch_array($admin_query)){
				$message_to[]=$admin_result["admin_email_address"];
			}
		}
	
		$fetch_query=tep_db_query("SELECT start_date from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . $sessions_id . "'");
		$fetch_result=tep_db_fetch_array($fetch_query);
		$start_date=$fetch_result["start_date"];

		$customer_query=tep_db_query("SELECT * from " . TABLE_EVENTS_ATTENDEES_CONFIRM . " where entry_id in(" . $entry_ids . ")");
		if (tep_db_num_rows($customer_query)<=0) return;
		
		$mes_text=&tep_get_message_string("ADC");
		$sql_array=array(	"message_from"=>$login_email,
							"message_to"=>"",
							"message_subject"=>"New Customer in " . $events_name,
							"message_date"=>'now()',
							"message_unread"=>"Y");
		
		while($customer_result=tep_db_fetch_array($customer_query)){
			$replace_array=array(	MES_FTN=>$customer_result["customers_firstname"],
									MES_LTN=>$customer_result["customers_lastname"],
									MES_PHN=>$customer_result["customers_telephone"],
									MES_PAD=>($customer_result["customers_paid"]=="Y"?"Yes":"No"),
									MES_PAT=>$customer_result["customers_paid_type"],
									MES_EVA=>$events_name,
									MES_SAT=>format_date($start_date)
								);
			$temp_text=$mes_text;			
			while(list($key,$value)=each($replace_array)){
				$temp_text=str_replace("%%" . $key . "%%",$value,$temp_text);
			}
			$sql_array["message_text"]=$temp_text;
			for ($icnt=0;$icnt<sizeof($message_to);$icnt++){
				$sql_array["message_to"]=$message_to[$icnt];
				tep_db_perform(TABLE_EVENTS_ADMIN_MESSAGES,$sql_array);
			}
		}
	}
	// send internal message regarding payment made
	function tep_message_paid_amount($entry_ids){
		global $languages_id,$currencies,$login_email;
		if ($entry_ids=="") return;
		$paid_query=tep_db_query("SELECT * from " . TABLE_EVENTS_INSTRUCTOR_PAYMENTS . " where entry_id in(" . $entry_ids . ")");
		if (tep_db_num_rows($paid_query)<=0) return;
		
		$mes_text=&tep_get_message_string("PAI");
		$sql_array=array("message_from"=>$login_email,
						"message_subject"=>"Payment Made",
						"message_date"=>'now()',
						"message_unread"=>"Y");	

		
		while($paid_result=tep_db_fetch_array($paid_query)){
			$admin_query=tep_db_query("SELECT concat(a.admin_firstname,' ',a.admin_lastname) as admin_name,a.admin_email_address,ed.events_name,e.events_instructor from " . 
										TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_ADMIN . " a where e.events_id='" . $paid_result["events_id"] . "' and e.events_id=ed.events_id and e.events_instructor=a.admin_id");
			$admin_result=tep_db_fetch_array($admin_query);
							
			$fetch_query=tep_db_query("SELECT start_date from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . $paid_result["sessions_id"] . "'");
			$fetch_result=tep_db_fetch_array($fetch_query);
			$session_date=$fetch_result["start_date"];

			$replace_array=array(	MES_INS=>$admin_result["admin_name"],	
									MES_EVA=>$admin_result["events_name"],
									MES_SAT=>format_date($session_date),
									MES_PAD=>$currencies->format($paid_result["total_amount"]),
									MES_PAT=>$paid_result["payment_type"],
								);
			$temp_text=$mes_text;			
			while(list($key,$value)=each($replace_array)){
				$temp_text=str_replace("%%" . $key . "%%",$value,$temp_text);
			}
			$sql_array["message_to"]=$admin_result["admin_email_address"];
			$sql_array["message_text"]=$temp_text;
			tep_db_perform(TABLE_EVENTS_ADMIN_MESSAGES,$sql_array);
		}
	}
	// send message regarding payment request
	function tep_message_payment_request($entry_ids){
		global $languages_id,$currencies,$login_email,$top,$top_admin_email;
		if ($entry_ids=="") return;
		$paid_query=tep_db_query("SELECT * from " . TABLE_EVENTS_INSTRUCTOR_PAYMENTS . " where entry_id in(" . $entry_ids . ")");
		if (tep_db_num_rows($paid_query)<=0) return;
		
		$admin_query=tep_db_query("SELECT a.admin_email_address from " . TABLE_ADMIN . " a, " . TABLE_ADMIN_GROUPS . " ag where a.admin_groups_id=ag.admin_groups_id and ag.admin_groups_name='" . TEXT_RESERVATIONMANAGER_ENTRY . "'");
		while($admin_result=tep_db_fetch_array($admin_query)){
			$message_to[]=$admin_result["admin_email_address"];
		}
		$mes_text=&tep_get_message_string("PAR");
		$sql_array=array("message_from"=>$login_email,
						"message_subject"=>"Payment Request",
						"message_date"=>'now()',
						"message_unread"=>"Y",
						"message_to"=>$message_to,
						"message_text"=>"Payment Request<br>"
						);	

		
		while($paid_result=tep_db_fetch_array($paid_query)){
			$admin_query=tep_db_query("SELECT concat(a.admin_firstname,' ',a.admin_lastname) as admin_name,a.admin_email_address,ed.events_name,e.events_instructor from " . 
										TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_ADMIN . " a where e.events_id='" . $paid_result["events_id"] . "' and e.events_id=ed.events_id and e.events_instructor=a.admin_id");
			$admin_result=tep_db_fetch_array($admin_query);
							
			$fetch_query=tep_db_query("SELECT start_date from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . $paid_result["sessions_id"] . "'");
			$fetch_result=tep_db_fetch_array($fetch_query);
			$session_date=$fetch_result["start_date"];

			$replace_array=array(	MES_INS=>$admin_result["admin_name"],	
									MES_EVA=>$admin_result["events_name"],
									MES_SAT=>format_date($session_date),
									MES_PAD=>$currencies->format($paid_result["total_amount"]),
									MES_PAT=>$paid_result["payment_type"],
								);
			$temp_text=$mes_text;			
			while(list($key,$value)=each($replace_array)){
				$temp_text=str_replace("%%" . $key . "%%",$value,$temp_text);
			}
			$sql_array["message_text"].=$temp_text;
		}
		for ($icnt=0;$icnt<sizeof($message_to);$icnt++){
			$sql_array['message_to']=$message_to[$icnt];
			tep_db_perform(TABLE_EVENTS_ADMIN_MESSAGES,$sql_array);
		}
	}
	// send internal message regarding comments on event session
	function tep_message_comments(&$comments){
		global $events_id,$sessions_id,$login_groups_type,$top_admin_email,$login_email,$languages_id;

		$fetch_query=tep_db_query(	"SELECT events_name,e.events_instructor from " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS .  " e where ed.events_id='" . $events_id . "' and language_id='" . $languages_id . "' and " .
									" e.events_id=ed.events_id");


		$fetch_result=tep_db_fetch_array($fetch_query);
		$events_name=$fetch_result["events_name"];
		if ($login_groups_type==TEXT_RESERVATIONMANAGER_ENTRY){
			$message_to[]=tep_get_instructor_email($fetch_result["events_instructor"]);
		} else {
			//$message_to=$top_admin_email;
			$admin_query=tep_db_query("SELECT a.admin_email_address from " . TABLE_ADMIN . " a, " . TABLE_ADMIN_GROUPS . " ag " . " where a.admin_groups_id=ag.admin_groups_id and ag.admin_groups_name='" . TEXT_RESERVATIONMANAGER_ENTRY . "'");
			while($admin_result=tep_db_fetch_array($admin_query)){
				$message_to[]=$admin_result["admin_email_address"];
			}
		}
		
		$fetch_query=tep_db_query("SELECT start_date from " . TABLE_EVENTS_SESSIONS . " where sessions_id='" . $sessions_id . "'");
		$fetch_result=tep_db_fetch_array($fetch_query);
		$start_date=$fetch_result["start_date"];
				
		$sql_array=array(	"message_from"=>$login_email,
							"message_to"=>$message_to,
							"message_text"=>$comments,
							"message_subject"=>"Comment on Event " . $events_name . ", " . format_date($start_date),
							"message_date"=>'now()',
							"message_unread"=>"Y");

		for ($icnt=0;$icnt<sizeof($message_to);$icnt++){
			$sql_array['message_to']=$message_to[$icnt];
			tep_db_perform(TABLE_EVENTS_ADMIN_MESSAGES,$sql_array);
		}	}
	// Get Template for sending internal message
	function tep_get_message_string($type){
		global $login_name;
		$result="";
		switch($type){
			case "ADC": // add customer
				$result="<b>New Customer</b><br>First Name: %%First Name%%<br>Last Name: %%Last Name%%<br>" .
						"Phone Number: %%Phone Number%%<br>" . 
						"Paid: %%Paid%%<br>Payment Type: %%Payment Type%%<br>Event Attended: %%Event Name%%<br>" . 
						"Session Attended: %%Session%%";
				break;
			case "PAI":	// pay instructor
				$result="Payment Made to: %%Instructor%%<br>Regarding:<br>Event Name :%%Event Name%%<br>" .
						"Session Date: %%Session%%<br>Total Payment: %%Paid%%<br>Payment Type : %%Payment Type%%";
				break;
			case "PAR":	// pay request instructor
				$result="Event Name :%%Event Name%%<br>" .
						"Session Date: %%Session%%<br>Total Payment: %%Paid%%<br><br>";
				break;
			case "ADQ": // add customer from another event
			case "ANQ":
				$result="<b>Added as " . ($type=="ADQ"?"extra attendee":"new customer"). " by %%Instructor%%</b><br>First Name: %%First Name%%<br>Last Name: %%Last Name%%<br>" .
						"Phone Number: %%Phone Number%%<br>" . 
						"Paid: %%Paid%%<br>Payment Type: %%Payment Type%%<br>Event Attended: %%Event Name%%<br>" . 
						"Session Attended: %%Session%%<br>";
				if ($type=="ADQ"){
					$result.="<b>Reservation Details</b><br>" . 
							"Order Id: %%Order Id%%<br>" .
							"Event Name: %%Order Event Name%%<br>";
				}
		}
		return $result;
	}
?>
