<?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']);
	}
	$orderno= $_SESSION['id'];

if (isset($_GET['transno'])) {
		$_SESSION['transno'] = stripslashes($_GET['transno']);
	}
	$InvoiceNo = $_SESSION['transno'];

//first check whether this costing is approved or not
	$quotedated = date('Y-m-d');
		$quotedate = date('d-m-Y');
$yeardate = date('Y');
$monthdate =date('m');

					$userid = $_SESSION['UserID'];

include('includes/newheader.php');
echo '<p> </p> <p></p><p class="page_title_text"><img alt="" src="'.$RootPath.'/css/'.$Theme.
		'/images/printer.png" title="' .// Icon image.
		_('Reasons for Cancellation .  ') . '" /> ' .// Icon title.
		_('Please this process needs to be done by your manager') . '</p>';// Page title.
echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
	echo '<div class="col-sm-12">';// div class=?
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table class="table">';



echo '<tr>
			<td>' ._('Enter Reasons'). ':</td>
			<td><input tabindex="'.$j.'" type="text" maxlength="10" size="15" required="required" name="reason"  /></td>
		</tr>';

echo '</td>';

echo '<input type="submit" name="reversal" value="' ._('Process Reversal') . '" />
		</div>
		';



echo '</div>';

echo '</div>';

$reasons = $_POST['reason'];

if (isset($_POST['reversal']) && ($_POST['reason'] == $reasons )){
	
	
	//before canceling this invoice unallocate invoices if there are any allocations
	
			
$SQL1 = "SELECT receipt_amount,amt,invoice_number
			from custallocns
			WHERE receipt_number='" . $InvoiceNo."'";

	$ErrMsg =_('The bank account name cannot be retrieved because');
	$result1= DB_query($SQL1,$ErrMsg);
	
while ($myrow1 = DB_fetch_array($result1)){
	
	
	$SQL11 = "SELECT alloc
			from debtortrans
			WHERE transno='" . $myrow1['invoice_number']."'";

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


$allocatedbalance = $myrow11['alloc'] - $myrow1['amt'];
	
	$sql411 =  "UPDATE debtortrans set alloc = '" . $allocatedbalance. "', settled = '0'
				WHERE transno = '" .$myrow1['invoice_number']. "'";

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

	

	
}

	
	//unallocate receipt amount
	$SQL12 = "SELECT alloc
			from debtortrans
			WHERE transno='" . $InvoiceNo."'";

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


$allocatedbalancereceipt = $myrow12['alloc'] + -$myrow1['receipt_amount'];
	
	$sql4012 =  "UPDATE debtortrans set alloc = '" . $allocatedbalancereceipt. "', settled = '0'
				WHERE transno = '" . $InvoiceNo. "'";

	$ErrMsg = _('This customer could not be added because');
			$result4012 = DB_query($sql4012,$ErrMsg);
	
	
	
$sql40 =  "UPDATE debtortrans set cancelled ='1', cancelled_by = '" . $userid. "',cancel_reason = '" . $reasons. "'
				WHERE transno = '" . $InvoiceNo. "'";

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

	//update in bank transactions table
	$sql401 =  "UPDATE banktrans set cancelled ='1', cancelled_by = '" . $userid. "',cancel_reason = '" . $reasons. "'
				WHERE transno = '" . $InvoiceNo. "'";

	$ErrMsg = _('This customer could not be added because');
			$result401 = DB_query($sql401,$ErrMsg);
	
		$sql4111 =  "DELETE FROM custallocns 
				WHERE receipt_number = '" . $InvoiceNo. "'";

	$ErrMsg = _('This customer could not be added because');
			$result4111 = DB_query($sql4111,$ErrMsg);
//reverse gl entries
	//decrease in bank
	
	//first get gl account number for bank 
	
			
$SQL1 = "SELECT bankact,amount
			from banktrans
			WHERE transno='" . $InvoiceNo."'";

	$ErrMsg =_('The bank account name cannot be retrieved because');
	$result1= DB_query($SQL1,$ErrMsg);
$myrow1 = DB_fetch_array($result1);
$bankaccount =$myrow1['bankact'];
	$amount=$myrow1['amount'];
	//period
	
	
	    $query2 = "SELECT 
						periodno,
		             lastdate_in_period
		                 
					  
		          FROM periods
				    WHERE YEAR(lastdate_in_period) = '" . $yeardate . "'
					AND MONTH(lastdate_in_period)  = '" . $monthdate. "'";
				
        $result2=DB_query($query2);
        $myrow2=DB_fetch_array($result2);
      
				$PeriodNo = $myrow2['periodno'];
	


$message = "This receipt has been cancelled! The GL has been reversed for bank and trade debtors";
	echo "<script type='text/javascript'>alert('$message');</script>";
		echo "<script type='text/javascript'>javascript:history.go(-2)</script>"; 
	exit();


			
		
}
		
			
		

		
		
			    
		
	


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

?>
