<?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');
	
	set_time_limit(0);

	$mail_status=array('E'=>'N','S'=>'N');

	$now=getServerDate(true);
	$curdate=getServerDate();
//12/20/2007 18:16:39
/*	$curdate='2006-12-22';
	$now='2006-12-21 00:00:00';
	define('DEBUGGING_MODE_VIEW',true);
*/	
	$current_time=date("H:i");

	$time_split=(defined('EVENTS_EMAIL_TIMINGS')?split(",",EVENTS_EMAIL_TIMINGS):array());
	$offset=(int)EVENTS_SERVER_DATE_OFFSET;
	
	if (sizeof($time_split)>1 && $offset>=-23 && $offset<=23){
		$start_time="";
		$end_time="";
		if ($time_split[0]>0) $start_time=date("H:i",mktime(date('H')+abs($offset+$time_split[0])));
		if ($time_split[1]>0) $end_time=date("H:i",mktime(date('H')+abs($offset+$time_split[1])));
		$flag=0;
		if ($time_split[0]<=0 || $current_time>=$start_time) $flag++;
		if ($time_split[1]<=0 || $current_time<=$end_time) $flag++;
		if ($flag==2) $mail_status['E']='Y';
	}
	if ((int)EMAIL_ACTIVATE!=1) $mail_status['E']='N';

	// find if the template for the wallet balance reminder email is present
	$template_query=tep_db_query("SELECT * from " . TABLE_EMAIL_MESSAGES . " where message_type='RRE'");
	if (tep_db_num_rows($template_query)<=0) {
		$mail_status["E"]="N";
	}
	if($mail_status["E"]=='Y') {
		$template_result=tep_db_fetch_array($template_query);
		$support_packs_query=tep_db_query("select spl.customer_name,spl.customer_domain,spl.customer_email,opsp.support_minutes,opsp.subscription_name,spl.license_code,spl.support_packs_id,spl.email_sent from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS_PRODUCTS_SUPPORT_PACKS . " opsp, " . TABLE_SUPPORT_PACKS_LICENSE . " spl where o.orders_id=op.orders_id and op.orders_id=opsp.orders_id and o.orders_status='2' and op.support_packs_type='S' and opsp.support_packs_id=spl.support_packs_id and spl.activate='Y'");
		while($support_packs_result=tep_db_fetch_array($support_packs_query)){
			$usage_query=tep_db_query("select sum(minutes_used) as minutes_used from " . TABLE_SUPPORT_PACKS_ORDER_HISTORY . " spoh where support_pack_id='" . $support_packs_result['support_packs_id'] . "' and license_code='" . $support_packs_result['license_code'] . "' group by license_code");
			$usage_result=tep_db_fetch_array($usage_query);
			$minutes_used=0;
			if($support_packs_result['support_minutes']>0) 
				$minutes_used=(($usage_result['minutes_used']/$support_packs_result['support_minutes'])*100);
				
			
				if((int)$minutes_used >= 80 && (int)$minutes_used < 90){
					$mused=80;
				}else if((int)$minutes_used >= 90 && (int)$minutes_used < 100){
					$mused=90;
				}else if((int)$minutes_used >= 100 && (int)$minutes_used < 105){
					$mused=100;
				}else if((int)$minutes_used >= 105 && (int)$minutes_used < 120){
					$mused=110;
				}else if((int)$minutes_used >= 120){
					$mused=120;
				} else $mused=0;
					
				if( $support_packs_result['email_sent'] < $mused ) {
					$replace_array=array(TEXT_LC=>sprintf('%05d',$support_packs_result['license_code']),
										TEXT_DN=>$support_packs_result['customer_domain'],
										TEXT_DE=>$support_packs_result['customer_email'],
										TEXT_DU=>$support_packs_result['customer_name'],
										TEXT_SM=>STORE_NAME,
										TEXT_SN=>STORE_OWNER,
										TEXT_SE=>STORE_OWNER_EMAIL_ADDRESS,
										TEXT_TST=>$support_packs_result['support_minutes'],
										TEXT_PU=>number_format($minutes_used,2));
					$details['html_text'] = $template_result['message_text'];
					tep_replace_template($details,$replace_array);
					$details['html_text'] = str_replace("\\","",$details['html_text']);
					$mail_details = array();
					$mail_details['format'] = $template_result["message_format"];
					
					$mail_details['to_name'] = $support_packs_result['customer_name'];
					$mail_details['to_email'] = $support_packs_result['customer_email'];
					$mail_details['from_name'] = STORE_OWNER;
					$mail_details['from_email'] = STORE_OWNER_EMAIL_ADDRESS;		
					$mail_details['subject'] = $message_result['message_subject'];
					$mail_details['html_text'] = $details['html_text'];
					$mail_details['text'] = strip_tags($details['html_text']);
					tep_db_query("update " . TABLE_SUPPORT_PACKS_LICENSE . " set email_sent='" . $mused . "' where license_code='" . $support_packs_result['license_code'] . "'");
					tep_send_email($mail_details);
					}	
		}
	}
	require('includes/application_bottom.php');
?>