<?php
require_once(LIB_PATH.DS.'database.php');
class Schoolfees extends DatabaseObject{
	protected static $table_name="schoolfees_tranzact";
	public $id;
	public $surname;
	public $othername;
	public $session;
	public $amount;
	public $datetime;
	public $department;
	public $departmentcode;
	public $email;
	public $faculty;
	public $facultycode;
	public $fno;
	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 t_processing($id=0, $curr_session="", $code=0 ){
	
		
global $database;

$user_data = Userdata::find_by_id($id);
 $biodata = Biodata::find_by_id($user_data->formno,$user_data->email);
 $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";

$pay_settings=Pay_settings::find_by_id(3,$level,$pay_cat);
$time = date('Y-m-d h:i:s');
$a = (substr(rand(1, 10000000000),0,10));
					$txt_ref = $a.(substr( $biodata->surname,0,2));	

$sql = "INSERT INTO ".self ::$table_name ." (";
$sql .= "formno,surname, othername,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 .=$biodata->courses. "', '";
$sql .=$biodata->courses. "', '";
$sql .=$biodata->programme."', '";
$sql .= $biodata->phone."', '";
$sql.=$biodata->email."', '";
$sql .= $curr_session ."', '";
$sql .=$biodata->programme."', '";
$sql .=$txt_ref."' ,'";
$sql.= $code."', '"; 
$sql .=$pay_settings->charge_interswitch + $pay_settings->charge_omoluabi."' ,'";
$sql .=$pay_settings->charge_interswitch + $pay_settings->charge_omoluabi."' ,'";
$sql .= $time. "' ,'" ;
$sql .=$biodata->programme."', '";
$sql .='Approved Successful'.
//$sql .='Successful'.
"')" ;
if ($database->query($sql)){
		//$this->id=$database->insert_id();
		return true;
	}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_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 update(){
	
	global $database;
	$sql = "UPDATE ".self ::$table_name."(";
	$sql .="formno = '". $database->escape_value($this->formno)."',"; 
	$sql .= "email ='". $database->escape_value($this->email)."', ";	
	$sql .= "WHERE id=". $database->escape_value($this->id);
	
	$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
}
$schoolfees = new Schoolfees();
?>