<?php
require_once(LIB_PATH . DS . 'database.php');

date_default_timezone_set('Africa/Lagos');
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 $verified;
     public $verified_date;
     public $verified_by;
     public $statusMessage;

     public $serviceTypeId;
     public $apiKey;
     public $orderId;
     public $rrr;

    




 public static function find_id($id){
        global $database;
        
        $sql = "SELECT * FROM acceptance_tranzact_remita ";
$sql .= " WHERE id= $id " ;	
$sql .= "AND statusMessage = 'Successful' LIMIT 1";
$result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):
false;
    }
    
    
    
    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}' order by id desc LIMIT 0,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}' AND responsecode='00' LIMIT 1";
        $result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):false;


    }

 public static function findacceptanceRemita($formno = "", $email = "")
    {
        global $database;
//$formno= $database->escape_value($formno);
//$email = $database->escape_value($email);
        $sql = "SELECT * FROM acceptance_tranzact_remita ";
        $sql .= " WHERE formno='{$formno}' ";
        $sql .= "AND email='{$email}' LIMIT 1";
        $result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):false;


    }



public static function authenticateattempt($formno = "", $email = "")
    {
        global $database;
//$formno= $database->escape_value($formno);
//$email = $database->escape_value($email);
        $sql = "SELECT * FROM acceptance_tranzact_remita ";
        $sql .= " WHERE formno='{$formno}' ";
        $sql .= "AND email='{$email}' order by id desc LIMIT 1";
        $result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):false;


    }
    
    
    
public static function findacceptanceRemitaSuccess($formno = "", $email = "")
    {
        global $database;
//$formno= $database->escape_value($formno);
//$email = $database->escape_value($email);
        $sql = "SELECT * FROM acceptance_tranzact_remita ";
        $sql .= " WHERE formno='{$formno}' ";
        $sql .= "AND email='{$email}' and statusMessage='Successful' LIMIT 1";
        $result_array = self::find_by_sql($sql);
return !empty($result_array)? array_shift($result_array):false;


    }

public static function confirmacceptance($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}' AND responsecode='00' 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 = "", $txt_ref = "")
    {

        global $database;
        $user_data = Userdata::find_by_id($id);
        
      if ((substr($user_data->formno,2,2)!=18) and ((substr($user_data->formno,2,2)!=19))){
      
      //echo "All new table: ".substr($user_data->formno,2,2);
      
      Switch(substr($user_data->formno,2,2))
      {
          case '20':$table='form_data';
          break;
          
          case '21': $table='form_data21';
          break;
          
          case '22': $table='form_data22';
           
          case '23': $table='form_data23';
      }
      
      $biodata = Biodata2::find_by_id($table, $user_data->formno, $user_data->email);
       
      
        $new_dp = DEPARTMENTCLASS::new_admin_dept($biodata->formno);
        
          if (!isset($new_dp->department)){
          $biodata->courses ='Nil';   
          }else{
        $biodata->courses = $new_dp->department;
          }
      $biodata->programme = $biodata->level;
        $curr_session=$user_data->start_session;
      
    
       
  }else{
 
        $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);

        if (substr($user_data->formno,2,2)=='19'){

        $new_dp = DEPARTMENTCLASS::new_admin_dept($user_data->formno);
        $biodata->courses = $new_dp->department;
        $biodata->programme = $biodata->level; 
        }
       
  }
        $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);

        $alldp = DEPARTMENTCLASS::find_all($dp->faculty);

        foreach ($alldp as $aldp) {
            $faculty = $aldp->faculty;
        }
        if ($faculty == 'Engineering' or $faculty=='Pure and applied Sciences' or $faculty == 'Applied Sciences' or $faculty == 'Enviromental Studies') {
            $pay_cat = 'science';
        } elseif ($faculty == 'Management Sciences') {
            $pay_cat = 'nonscience';
        }
        
        if ($biodata->programme=='Adv. Cert.'){
          $level = $biodata->programme;  
        }else {
        $level = $biodata->programme;
        $level = $level . " " . "I";
        }
         $time_tosave = time();
			$time = strftime("%Y-%m-%d %H:%M:%S", $time_tosave);

      // $time = date('Y-m-d h:i:s');


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

        $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 .= 200 . "' ,'";
        $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_txnref_failed_by_formno($txt = 0)
    {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE formno='{$txt}'and status != 'Approved Successful' LIMIT 0,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_txnref_failed_by_transid($txt = '')
    {
        global $database;
        $result_array = self::find_by_sql("SELECT * FROM  " . self::$table_name . " WHERE txt_ref='{$txt}'and status != 'Approved Successful' LIMIT 0,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();
?>