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


	

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

	
if (isset($_GET['invoice'])) {
		$_SESSION['invoice'] = stripslashes($_GET['invoice']);
	}
	$invoice= $_SESSION['invoice'];
if (isset($_GET['ptax'])) {
		$_SESSION['ptax'] = stripslashes($_GET['ptax']);
	}
	$ptax= $_SESSION['ptax'];
if (isset($_GET['date'])) {
		$_SESSION['date'] = stripslashes($_GET['date']);
	}
	$date= $_SESSION['date'];
//first check whether this costing is approved or not


//first check whether this costing is approved or not

	
$SQL = "UPDATE debtortrans
		               
					  
		         set  ptax = '0'
				 
				 WHERE transno = '" . $invoice . "'
			";
	
			$result = DB_query($SQL);
	
		

		
		
			$monthdate = date('m',strtotime($date));
	$transdate =  date('Y-m-d h:i:s',strtotime($date));
	
	    $query = "SELECT 
						periodno
		                 
					  
		          FROM periods
				    WHERE YEAR(lastdate_in_period) = '" . $yeardate . "'
					AND MONTH(lastdate_in_period)  = '" . $monthdate. "'";
        
      	$result = DB_query($query);
	
			$myrow = DB_fetch_array($result) ;

			$PeriodNo=$myrow['periodno'];
		
			 $query1 = "select transno from debtortrans
					order by transno desc
						limit 1";
        
        $result1 = DB_query($query1);
			$myrow1 = DB_fetch_array($result1);

			$lasttransno=$myrow1['transno'];
			
        
	
	$TransNo = $lasttransno + 1;
	
	//debtortrans


	    $query3 = "SELECT 
		            department
		               
					  
		          FROM debtortrans
				    
				   WHERE transno = '" . $invoice . "'";
        
        $result3 = DB_query($query3);
			$myrow3 = DB_fetch_array($result3);
        
			
			$department=$myrow3['department'];
			
 
	
$query2 = "INSERT INTO debtortrans (transno,
									type,
									debtorno,
									branchcode,
									trandate,
									inputdate,
									prd,
									settled,
									reference,
									tpe,
									order_,
									rate,
									ovamount,
								
							
									
									department,
									entered_by)
				VALUES (
					'" . $TransNo . "',
					'54',
					'" . $name . "',
					'" . $name . "',
					'" . $date. "',
					'" . $transdate . "',
					
					'" . $PeriodNo . "',
					'1',
					'Reversal-ptax',
					'DE',
					'" . $TransNo . "',
					'1',
					'" . $ptax. "',
					
								
					'" . $department . "',
					'admin'
					)";
        
       $result2 = DB_query($query2);
	
	
	
	//first decrease ptax 
	
	    $query4 = "INSERT INTO gltrans (type,
									typeno,
									trandate,
									periodno,
									account,
									narrative,
									amount,
									posted,
									tag,
									departmentid)
				VALUES ('0',
					'" . $TransNo . "',
					'" . $date . "',
					'" . $PeriodNo . "',
					'12000022',
					'Reversal',
					'" . -$ptax. "',
					'1',
					'" . $department. "',
					'" . $department. "'
					)";
        
         $result4 = DB_query($query4);
	//increase in AR 
	
	
		
	    $query5 = "INSERT INTO gltrans (type,
									typeno,
									trandate,
									periodno,
									account,
									narrative,
									amount,
									posted,
									tag,
									departmentid)
				VALUES ('0',
					'" . $TransNo . "',
					'" . $date . "',
					'" . $PeriodNo . "',
					'1200002',
					'Reversal',
					'" . $ptax. "',
					'1',
					'" . $department. "',
					'" . $department. "'
					)";
        
           $result5 = DB_query($query5);
	
	
		
			$message = "This Reversal Done.";
	echo "<script type='text/javascript'>alert('$message');</script>";
		echo "<script type='text/javascript'>javascript:history.go(-1)</script>"; 
	exit();
		

		
		
			    
		
	


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

?>
