
<?php
session_start();
require_once 'includes/database.php';

if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
else {$user = $_SESSION['reusername'];}

    $db=new MySQLDatabase();
    $db->open_connection();
        
 if (isset($_GET['r'])){  
    $recpno =  $_GET['r'];    
    $query="SELECT * FROM recptrans WHERE recpno = '$recpno'"; 
    $sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
    while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
            $amount = -1*floatval($row['amount']); 
            $pmode = $row['pmode']; 
            $rmks = $row['rmks']; 
            $cname = $row['cname']; 
            $ts = date("Y-m-d H:i:s");
            $bacc = $row['bacc'];
            $chqdet = $row['chqdet']; 
            $chqno = $row['chqno']; 
            $rtype = $row['rtype'];        
            $rdate = $row['rdate'];
            $newno = $recpno.'R';
            $revsd = intval($row['revsd']);
      } 
 
    if( $revsd < 1 ){
        $query="INSERT INTO recptrans (recpno, cname, rdate, rtype, pmode, chqno, chqdet, bacc, rmks, amount, us, ts, revsd ) VALUES ('$newno','$cname','$rdate','$rtype','$pmode','$chqno','$chqdet','$bacc','$rmks','$amount','$user','$ts', 1 )"; 
        $sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );   
        $query="UPDATE recptrans SET revsd = 1 WHERE recpno = '$recpno'"; 
        $sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
		if($rtype=="TRACKER"){ $query="UPDATE trackinvoices SET recpno = 0 WHERE recpno = '$recpno'";  }
        elseif($rtype=="INSURANCE"){ $query="UPDATE insinvoices SET recpno = 0 WHERE recpno = '$recpno'";  }
		else{ $query="UPDATE repoinvoices SET recpno = 0 WHERE recpno = '$recpno'";  }
		$sql = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
        }
}            
?>
