<?php

function getDBInstance()
{
    $path = $_SERVER['DOCUMENT_ROOT'];
    $path .= "/student_portal/acceptance_pay/includes/MyDbClass.php";
    include_once($path);
    
    return new MyDB();
}
   
function openDatabase()
{  
   $db = getDBInstance();
   if(!$db)
   {
      echo $db->lastErrorMsg();
   } 
   else 
   {
      //echo "Opened database successfully<br/>";
   }
    $sql =<<<EOF
      CREATE TABLE IF NOT EXISTS COLLEGEPAYLOGS
      (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
      TXNREF           TEXT    NOT NULL,
      DATETIME            TEXT     NOT NULL,
      AMOUNT            INT NOT NULL,
      RESPONSEDESC         TEXT,
      RESPONSECODE         TEXT,
      STUDENTNAME         TEXT,
      STUDENTID         TEXT);
EOF;

   $ret = $db->exec($sql);
   if(!$ret)
    {
      echo $db->lastErrorMsg();
   } 
   else 
    {
      //echo "Table created successfully<br/>";
   }
   $db->close();
}

function insertData($txnRef, $dateTime, $amount, $responseDesc, $responseCode, $custName, $studentID)
{   
   $db = getDBInstance();
   
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      //echo "Opened database successfully<br/>";
   }

$sql =<<<EOF
      INSERT INTO COLLEGEPAYLOGS (TXNREF,DATETIME,AMOUNT,RESPONSEDESC,RESPONSECODE,STUDENTNAME,STUDENTID)
      VALUES                ('$txnRef', '$dateTime', $amount , '$responseDesc', '$responseCode', '$custName', '$studentID');
EOF;

   $ret = $db->exec($sql);
   if(!$ret){
      echo $db->lastErrorMsg();
   } else {
      //echo "Records created successfully<br/><br/>";
   }
   $db->close();
}

function updateData($txnRef, $responseCode, $responseDescription)
{
    $db = getDBInstance();
   
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      //echo "Opened database successfully<br/>";
   }
   
   $sql =<<<EOF
      UPDATE COLLEGEPAYLOGS set RESPONSECODE = '$responseCode', RESPONSEDESC = '$responseDescription' where TXNREF='$txnRef';
EOF;
   
   $ret = $db->exec($sql);
   if(!$ret){
      echo $db->lastErrorMsg();
   } else {
      //echo $db->changes(), " Record updated successfully<br/>";
   }
}

function closeDB()
{
    $db = getDBInstance();
    $db->close();
}