<?php
ob_start();
include('includes/session.inc');
$Title = _('Create Invoice');
include('includes/SQL_CommonFunctions.inc');


	
//collect reasons for cancelation

	
if (isset($_GET['id'])) {
		$_SESSION['id'] = stripslashes($_GET['id']);
	}
	$id= $_SESSION['id'];
	
	
if (isset($_GET['amt'])) {
		$_SESSION['amt'] = stripslashes($_GET['amt']);
	}
	$amtallocated= $_SESSION['amt'];

if (isset($_GET['receipt_number'])) {
		$_SESSION['receipt_number'] = stripslashes($_GET['receipt_number']);
	}
	$receipt_number = $_SESSION['receipt_number'];
if (isset($_GET['invoice_number'])) {
		$_SESSION['invoice_number'] = stripslashes($_GET['invoice_number']);
	}
	$invoice_number = $_SESSION['invoice_number'];

//unallocate invoice number

$SQL = "SELECT alloc
			from debtortrans
			WHERE transno='" . $invoice_number."'";

	$ErrMsg =_('The bank account name cannot be retrieved because');
	$result= DB_query($SQL,$ErrMsg);
$myrow = DB_fetch_array($result);

$allocatedbalanceinvoice = $myrow['alloc'] - $amtallocated;



	$sql41 =  "UPDATE debtortrans set alloc = '" . $allocatedbalanceinvoice. "', settled = '0'
				WHERE transno = '" . $invoice_number. "'";

	$ErrMsg = _('This customer could not be added because');
			$result41 = DB_query($sql41,$ErrMsg);


//now receipt number
		
$SQL1 = "SELECT alloc
			from debtortrans
			WHERE transno='" . $receipt_number."'";

	$ErrMsg =_('The bank account name cannot be retrieved because');
	$result1= DB_query($SQL1,$ErrMsg);
$myrow1 = DB_fetch_array($result1);


$allocatedbalancereceipt = $myrow1['alloc'] + $amtallocated;


	$sql411 =  "UPDATE debtortrans set alloc = '" . $allocatedbalancereceipt. "', settled = '0'
				WHERE transno = '" . $receipt_number. "'";

	$ErrMsg = _('This customer could not be added because');
			$result411 = DB_query($sql411,$ErrMsg);

// now delete customer allocatiosn table

	$sql4111 =  "DELETE FROM custallocns 
				WHERE id = '" . $id. "'";

	$ErrMsg = _('This customer could not be added because');
			$result4111 = DB_query($sql4111,$ErrMsg);







$message = "This invoice has been unallocated!";
	echo "<script type='text/javascript'>alert('$message');</script>";
		echo "<script type='text/javascript'>javascript:history.go(-1)</script>"; 
	exit();


			
		

		
			
		

		
		
			    
		
	


//include('includes/footer.inc'); 

?>
