<?php

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


	


//first check whether this costing is approved or not
$SQLmain = "SELECT *
	               
			FROM debtortrans
			WHERE debtorno ='243'
			AND department = '18'
			";
	
			$resultmain = DB_query($SQLmain);
	
			while ($myrowmain = DB_fetch_array($resultmain) ) {

				$billingid = $myrowmain['billing_id'];
	
$SQL = "SELECT customer,branchcode,account_number,month,year,department,invoice_date,comments,second_option,status,modified_by
	               
			FROM cit_billing_profile
			WHERE billing_id='" . $myrowmain['billing_id']. "'
			";
	
			$result = DB_query($SQL);
	
			$myrow = DB_fetch_array($result) ;


	 if ($myrow['second_option'] =='1') {
	 //include('includes/PDFInvoiceHeaderCITANZ.inc');

			$sql8 =  "SELECT *
			FROM cit_bill_items
			
			WHERE billing_id = '" . $billingid. "'
			";
	$ErrMsg = _('This customer could not be added because');
			$result8 = DB_query($sql8,$ErrMsg);
	
		
 $totalvep = 0;
	 
	 
	 $Grandtotalvep = 0;
	  $Grandtotalvat = 0;
	  $Grandtotalvip = 0;
			while ($myrow1 = DB_fetch_array($result8)) { 
		
		
		
			 $Grandtotalvep += $myrow1['vep'];
				$Grandtotalvat += $myrow1['vat'];
				$Grandtotalvip += $myrow1['vip'];
				
		}	
		

		
			
		
		
		$FontSize = 10;
		


	       //2 decimal point outpu
	
	$vatfinal = round($Grandtotalvat,2);
				$displaytotalfinal= round($Grandtotalvip,2);
	
	$totalvepdec = round($Grandtotalvep,2);
		
			

//debtor trans and debtor vat
		//	echo $myrow['customer']." - ".$totalvepdec." - ".$myrowmain['id'];
		//	echo "<br>";
    
			//debtortranstax
				
	 
$SQLdebtor = "UPDATE debtortrans set ovamount = '" . $Grandtotalvep . "',
			                                     ovgst = '" . $Grandtotalvat . "'
												 where id = '" . $myrowmain['id']. "'
												 ";
		$ErrMsg = _('Cannot insert a GL entry for the journal line because');
		$DbgMsg = _('The SQL that failed to insert the GL Trans record was');
		$result = DB_query($SQLdebtor,$ErrMsg,$DbgMsg,true);
		
	 
	 
 
	 } else{
	     
	    
	    	$sql8 =  "SELECT *
			FROM cit_bill_items
			
			WHERE billing_id = '" . $billingid. "'
			";
	$ErrMsg = _('This customer could not be added because');
			$result8 = DB_query($sql8,$ErrMsg);
	
		
 $totalvep = 0;
	 
	 
	 $Grandtotalvep = 0;
	  $Grandtotalvat = 0;
	  $Grandtotalvip = 0;
			while ($myrow1 = DB_fetch_array($result8)) { 
		
		
		
			 $Grandtotalvep += $myrow1['total'];
			
				
		}	
		

		
		$Grandtotalvat =  $Grandtotalvep * 0.15;
		
		$Grandtotalvip = $Grandtotalvep * 1.15;
		
		
		$FontSize = 10;
		


	       //2 decimal point outpu
	
	$vatfinal = round($Grandtotalvat,2);
				$displaytotalfinal= round($Grandtotalvip,2);
	
	$totalvepdec = round($Grandtotalvep,2);
		
			

//debtor trans and debtor vat
		//	echo $myrow['customer']." - ".$totalvepdec." - ".$myrowmain['id'];
		//	echo "<br>";
    
			//debtortranstax
				
	 
$SQLdebtor = "UPDATE debtortrans set ovamount = '" . $Grandtotalvep . "',
			                                     ovgst = '" . $Grandtotalvat . "'
												 where id = '" . $myrowmain['id']. "'
												 ";
		$ErrMsg = _('Cannot insert a GL entry for the journal line because');
		$DbgMsg = _('The SQL that failed to insert the GL Trans record was');
		$result = DB_query($SQLdebtor,$ErrMsg,$DbgMsg,true);
		
	     
	     
	 }
				

			}
			
		
	
		
			
		

		
		
			    
		
	


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

?>
