<?php
require_once(LIB_PATH.DS.'database.php');
class Transaction extends DatabaseObject{
	protected static $table_name="acceptance_tranzact";
	public $id;
	public $surname;
	public $othernames;
	public $session;
	public $amount;
	public $datetime;
	public $department;
	public $departmentcode;
	public $email;
	public $faculty;
	public $facultycode;
	public $formno;
	public $level;
	public $levelcode;
	public $overhead;
	public $phone;
	public $programme;
	public $programmecode;
	public $ptype;
	public $responsecode;
	public $status;
	public $txt_fees;
	public $txt_ref;
public static function authenticate($formno="", $email=""){
global $database;
//$formno= $database->escape_value($formno);
//$email = $database->escape_value($email);
$sql = "SELECT * FROM ".self ::$table_name." ";
$sql .= " WHERE formno='{$formno}' ";
$sql .= "AND email='{$email}' LIMIT 1";
//$sql .= "AND responsecode = 'Successful' LIMIT 1" ;	

$result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):
false;


}
public static function findacceptance($formno="", $email=""){
global $database;
//$formno= $database->escape_value($formno);
//$email = $database->escape_value($email);
$sql = "SELECT * FROM ".self ::$table_name." ";
$sql .= " WHERE formno='{$formno}' ";
$sql .= "AND email='{$email}' LIMIT 1";
//$sql .= "AND responsecode = 'Successful' LIMIT 1" ;	

return self::find_by_sql($sql);
//return !empty($result_array)? array_shift($result_array):
//false;


}
public static function t_processing($id=0, $curr_session="", $txt_ref=""){
	
global $database;
$user_data = Userdata::find_by_id($id);
 $biodata = Biodata::find_by_id($user_data->formno,$user_data->email);
 
 $adm_type = (substr($user_data->formno,0,2));
 
 if (($adm_type=='PU') or ($adm_type=='FT' )){
 $adm_type='regular';
     
 }elseif ($adm_type=='DP'){
  
  $adm_type='dailypt';   
 }
 
 $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
 
$alldp = DEPARTMENTCLASS::find_all($dp->faculty);

foreach ($alldp as $aldp){
	$faculty = $aldp->faculty;
}
if ($faculty=='Engineering'or $faculty=='Applied Sciences'or $faculty=='Enviromental Studies'){
$pay_cat='science';	
}elseif ($faculty=='Management Sciences'){
	$pay_cat='nonscience';
}
$level = $biodata->programme;
 $level = $level." "."I";
$time = date('Y-m-d h:i:s');


$pay_settings=Pay_settings::find_by_id(1,$level,$pay_cat);

$sql = "INSERT INTO ".self ::$table_name ." (";
$sql .= "formno,surname, othernames,faculty,department,programme,phone,email,session,level,txt_ref,amount,txt_fees,overhead,datetime,status,responsecode";

$sql .= ") VALUES ('";
$sql .= $biodata->formno."', '";
$sql .= $biodata->surname."', '";
$sql .= $biodata->othernames. "', '";
$sql .=$faculty. "', '";
$sql .=$biodata->courses. "', '";
$sql .=$biodata->programme."', '";
$sql .= $biodata->phone."', '";
$sql.=$biodata->email."', '";
$sql .= $curr_session ."', '";
$sql .=$biodata->programme."', '";
$sql .=$txt_ref."' ,'";
$sql.= $pay_settings->amount."', '"; 
$sql .= 300 ."' ,'";
$sql .= 500 ."' ,'";
$sql .= $time. "' ,'" ;
$sql .='UNPAID'."', '";
$sql .=''.
"')" ;

if ($database->query($sql)){
		//$this->id=$database->insert_id();
		return true;
		$_SESSION['txt_ref']=$txt_ref;
	}else {
		return false;
	}
	
//$result_array = self::find_by_sql($sql);
//return !empty($result_array)? array_shift($result_array) : false;
//return $result_array;

}
public function fullname(){
	
if (isset($this->formno)&& isset($this->username)){
	return $this->formno ." ". $this->username;	
	} else {
		return "";
	}
}



//Common database Methods
public static function find_all(){
	global $database;
	return self::find_by_sql("SELECT * FROM ".self:: $table_name);
	//return $result_set;
	
}
public static function find_by_id($id=0){
	global $database;
	$result_array = self::find_by_sql("SELECT * FROM  ".self::$table_name." WHERE id={$id} LIMIT 1");
	//$found = $database->fetch_array($result_set);
	//return $found;
	return !empty($result_array)? array_shift($result_array) : false;
	
}

public static function find_by_txnref($txt=0){
	global $database;
	$result_array = self::find_by_sql("SELECT * FROM  ".self::$table_name." WHERE txt_ref='{$txt}' LIMIT 1");
	//$found = $database->fetch_array($result_set);
	//return $found;
	return !empty($result_array)? array_shift($result_array) : false;
	
}

public static function find_by_txnref_failed($txt=0){
	global $database;
	$result_array = self::find_by_sql("SELECT * FROM  ".self::$table_name." WHERE txt_ref='{$txt}'and status != 'Approved Successful' LIMIT 1");
	//$found = $database->fetch_array($result_set);
	//return $found;
//	txt_ref='{$txt}'
	return !empty($result_array)? array_shift($result_array) : false;
	
}

public static function find_by_sql($sql=""){
	global $database;
	$result_set = $database->query($sql);
	$object_array = array();
	while ($row= $database->fetch_array($result_set)){
	$object_array[]=self::instantiate($row);
	}
	return $object_array;
	
}
private static function instantiate($record){
	
	$object = new self;
	foreach ($record as $attribute=>$value){
	if ($object->has_attribute($attribute)){
	$object->$attribute = $value;
	}
		
	}
	return $object;
}
private function has_attribute($attribute){
	
$object_vars = get_object_vars($this);
return array_key_exists($attribute,$object_vars);	
}

public function save(){
return isset($this->id) ?	$this->update() : $this->create();	
}

public function create(){
global $database;
	$sql = "INSERT INTO ".self ::$table_name." (";
	$sql .= "formno, email";
	$sql .= ") 	VALUES ('";
	$sql .= $database->escape_value($this->formno)."', '"	;
	$sql .= $database->escape_value($this->email)."') '"	;
	if ($database->query($sql)){
		$this->id=$database->insert_id();
		return true;
	}else {
		return false;
	}
	
}

public function updateaccept($responsedescription,$responsecode,$txnref){
	
	global $database;
	$sql = "UPDATE ".self ::$table_name." SET status='$responsedescription', responsecode='$responsecode' WHERE txt_ref='$txnref' ";
//	$sql .="status = '". $responsedescription."',"; 
//	$sql .= "responsecode ='".$responsecode ."', ";
//	$sql .= "WHERE txt_ref=".$txnRef;
	
	$database->query($sql);
return ($database->affected_rows()==1)? true: false;
	}
public function delete(){
	
global $database;
$sql = "DELETE FROM ".self ::$table_name." ";
$sql .= "WHERE id=" . $database->escape_value($this->id);
$sql .= " LIMIT 1";	
$database->query($sql);
return ($database->affected_rows() ==1) ? true: false;
}

//Closing tags for user's class
}
$transaction = new Transaction();
?>