
<?php
#!/usr/bin/php -q


/*

  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/sms_application_top.php');
	require(DIR_WS_CLASSES . 'currencies.php');

	$currencies=new currencies();

require(DIR_WS_CLASSES . 'fulfiller.php');
require(DIR_WS_CLASSES . 'xml_parser.php');
require(DIR_WS_CLASSES . 'order.php');

$parser=new xml_parser_class;
$parser->store_positions=0;
$parser->case_folding=0;
$parser->target_encoding="ISO-8859-1";
$parser->simplified_xml=0;
$parser->fail_on_non_simplified_xml=0;

$fulfiller= new fulfiller('oneworld');

	$replace_array=array();
	$details=array();

	//get the subscription templates
	$email_query=tep_db_query("SELECT * from " . TABLE_EMAIL_MESSAGES . " where message_type in('PST')");
	while($email_array=tep_db_fetch_array($email_query)){
		$template=&new mesContent();
		$template->content=$email_array["message_text"];
		$template->subject=$email_array["message_subject"];
		$template->sendtime=$email_array["message_send"];
		$template->format=$email_array["message_format"];
	}

	// check order statuschange
	$sql="select orders_id,fulfiller_status from " . TABLE_ORDERS . " where orders_status!=3 and fulfiller_status!='SHIPPED'";
	$select_query=tep_db_query($sql);
	$success='';
	while($row=tep_db_fetch_array($select_query)){
		$result_status=$fulfiller->process_status($row['orders_id']);
		if ($result_status["error"]!='' || $result_status["status"]==""){
			if ($result_status["error"]=="") $result_status["error"]="Fulfiller Interface error. Mail sending failed";
			break;
		}
	    if ($result_status["status"]!=$row['fulfiller_status']){
			$order = new order($row['orders_id']);
			$sql="update " . TABLE_ORDERS . " set fulfiller_status='" . $result_status["status"] ."' where orders_id='" . $row['orders_id'] . "'";
			tep_db_query($sql);
			tep_send_status_change_email($order,$row['orders_id']);
		}
	}
	if ($result_status["status"]){
		echo "Mail sent successfully";
	} else {
		error_notification($result_status["error"]);
		echo $result_status["error"];
	}
require('includes/application_bottom.php');

function get_status_id($s_id)
{
	$id=$s_id;
	return constant($id);
}
function tep_send_status_change_email(&$order,$ord_id){
	global $currencies,$languages_id,$template;
	

	$merge_details[CUST_CF]=$order->customer['firstname'];
	$merge_details[CUST_CL]=$order->customer['lastname'];
	$merge_details[CUST_CM]=$order->customer['company'];
	$merge_details[TEXT_SN]=STORE_OWNER;
	$merge_details[TEXT_SM]=STORE_NAME;
	$merge_details[TEXT_SE]=STORE_EMAIL;
	$merge_details[TEXT_AL]=STORE_EMAIL;
	$merge_details[CUST_CT]=$order->customer['street_address'];
	$merge_details[CUST_CS]=$order->customer['suburb'];
	$merge_details[CUST_CC]=$order->customer['city'];
	$merge_details[CUST_CP]=$order->customer['postcode'];
	$merge_details[CUST_CE]=$order->customer['state'];
	$merge_details[CUST_CU]=$order->customer['country']['title'];
	$merge_details[CUST_CO]=$order->customer['telephone'];
	$merge_details[CUST_CA]=$order->customer['email_address'];
	

	/*$merge_details[TEXT_PN]=
	$merge_details[TEXT_PP]=
	$merge_details[TEXT_P_Q]=
	$merge_details[TEXT_P_STATUS]=*/
	$merge_details[ORDR_NO]=$insert_id;
	$merge_details[ORDR_OL]=tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $insert_id, 'SSL', false);
	$merge_details[ORDR_OP]=strftime(DATE_FORMAT_LONG);
	$merge_details[ORDR_OM]=$order->info['comments'] . $comments;
	$merge_details[ORDR_PO]=$products_ordered;
	
	$merge_details[ORDR_NO]=$ord_id;
	$merge_details[ORDR_OP]=$order->info['date_purchased'];
	$merge_details[ORDR_OL]=tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $ord_id, 'SSL', false);
	$merge_details[ORDR_PO]="";
	$merge_details[ORDR_OM]=$order->info['comments'];
	$merge_details[ORDR_OT]=$order->info['total'];
	$merge_details[ORDR_PM]=$order->info['payment_method'];

		$details=array();
		$details['format']=$template->format;
		$details['html_text']=$template->content;
		$details['text']=$template->content;
		$details['subject']=$template->subject;

		//replace content
		tep_replace_template($details,$merge_details);
		tep_strip_html($details);
		// get the address to send
		$details['to_name']=$order->customer['firstname'] . ' ' . $order->customer['lastname'];
		$details['to_email']=$order->customer['email_address'];
		$details['from_name']=STORE_OWNER;
		$details['from_email']=STORE_OWNER_EMAIL_ADDRESS;
		
		tep_send_email($details);	
}
function error_notification($error){
	global $HTTP_SERVER_VARS;
	$error_array = array(
						'cron_jobs_error_action' => basename($HTTP_SERVER_VARS['PHP_SELF']),
						'cron_jobs_error_text' => $error,
						'cron_jobs_error_date' => 'now()'
						);
	tep_db_perform(TABLE_CRON_JOBS_ERROR,$error_array);
}
class mesContent{
	var $content;
	var $subject;
	var $sendtime;
	var $format;
}

?>