<?php
ob_start();
include ('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');

if (isset($_GET['ref'])){
	$ref = $_GET['ref'];
}
if (isset($_GET['date'])){
	$date = $_GET['date'];
}

	
//collect reasons for cancelation


					$userid = $_SESSION['UserID'];

include('includes/newheader.php');

$sqlbalances="SELECT *
					FROM banktrans 
					WHERE ref = '" . $ref. "'
					Or banktranstype = 'Cash' 
					Or banktranstype = 'Cheque'
					
					Order by banktransid desc";
		$resultfromdebtoraccount =DB_query($sqlbalances);
$myrowbeg =  DB_fetch_array($resultfromdebtoraccount);

	$SQL= "SELECT bank_name,
	           bankaccountname,
				bankaccountnumber
			FROM bankaccounts 
			WHERE accountcode='" . $myrowbeg['bankact']. "'
			";

	$ErrMsg = _('An error occurred getting the header information about the receipt batch number') ;
	$DbgMsg = _('The SQL used to get the receipt header information that failed was');
	$Result=DB_query($SQL,$ErrMsg,$DbgMsg);


	/* OK get the row of receipt batch header info from the BankTrans table */
	$myrow = DB_fetch_array($Result);
	
	$bankname = $myrow['bank_name'];
	$bankaccountname = $myrow['bankaccountname'];
	$bankaccountnumber= $myrow['bankaccountnumber'];

$sqldate = $date;
$reasons = $ref;

		include('includes/PDFStarter.php');

	/*PDFStarter.php has all the variables for page size and width set up depending on the users default preferences for paper size */

	$pdf->addInfo('Title',_('Banking Summary'));
	$pdf->addInfo('Subject',_('Banking Summary Number') . ' ' . $_POST['BatchNo']);
	$line_height=12;
	$PageNumber = 0;
	
	$cashdeposit = 0;
	$cheqdeposit = 0;
	$TotalBanked = 0;
 $subtotalonchqaccount=0;
	include ('includes/PDFBankingSummaryPageHeader.inc');
	

$sqlbalances1= "SELECT 
					banktrans.transno,
					banktrans.client_account_number,
					banktrans.banktranstype,
					banktrans.amount,
					banktrans.drawer_bank,
					banktrans.drawer_accountnumber,
					banktrans.drawer_chqnumber,
					debtoraccount.billing_name
				FROM banktrans INNER JOIN debtoraccount
				ON banktrans.client_account_number=debtoraccount.account_number
				WHERE banktrans.ref ='" . $ref. "'
				AND banktrans.type = 12
				
				
				";
		$resultfromdebtoraccount1 =DB_query($sqlbalances1);

	while ($myrowdetail =  DB_fetch_array($resultfromdebtoraccount1)) {
	//before canceling this invoice unallocate invoices if there are any allocations
		 
		 
	
			 
			 
			
			 
			 
		 //update database

		
		 if ($myrowdetail["banktranstype"] == "Cheque"){
			 
			 
			 
										
			 	  	 //display pdf 
			 
			 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,60,$FontSize,(locale_number_format($myrowdetail["amount"],2)), 'right');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+65,$YPos,250,$FontSize,$myrowdetail['billing_name']. ' A/C: '.$myrowdetail['client_account_number']. ' Type: Cheque' , 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+355,$YPos,150,$FontSize,$myrowdetail['drawer_bank']._('-') .$myrowdetail["drawer_accountnumber"], 'left');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+440,$YPos,150,$FontSize,'Chq No: '.$myrowdetail["drawer_chqnumber"], 'left');
	
			 
		
			 
			 
			 $cheqdeposit += $myrowdetail["amount"];
			 
			
			 
			

		 }
		  	 if ($myrowdetail["banktranstype"] == "Cash"){
				 
				 
				 	  	 //display pdf 
			 
			 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,60,$FontSize,(locale_number_format($myrowdetail["amount"],2)), 'right');
		$LeftOvers = $pdf->addTextWrap($Left_Margin+65,$YPos,250,$FontSize,$myrowdetail['billing_name']. ' A/C: '.$myrowdetail['client_account_number']. ' Type: Cash ' , 'left');
	//	$LeftOvers = $pdf->addTextWrap($Left_Margin+215,$YPos,300,$FontSize,_('Bank: ').trim($_POST["drawer_bank"][$count])._('-') .trim($_POST["drawer_accountnumber"][$count]), 'left');
		//$LeftOvers = $pdf->addTextWrap($Left_Margin+400,$YPos,200,$FontSize,'Chq No: '.trim($_POST["drawer_chqnumber"][$count]), 'left');
				 
		
				 
				  $cashdeposit += $myrowdetail["amount"];

		 }
		

		
		 
		 
     //  $ListCount ++;

				



		$YPos -= $line_height;// Increment a line down for the next line item.

		 
		if ($YPos - (2 *$line_height) < $Bottom_Margin){
			/*Then set up a new page */
			include ('includes/PDFBankingSummaryPageHeader.inc');
		} /*end of new page header  */
		 

	 }
			
		 $TotalBanked = $cheqdeposit + $cashdeposit;
	
	
	$YPos-=$line_height;
	/*draw a line */
$pdf->line($XPos, $YPos,$Page_Width-$Right_Margin, $YPos);
	//$LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,60,$FontSize,_('---------------------'), 'right');
		$YPos-=$line_height;
	$LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,60,$FontSize,_('$').$TotalBanked, 'right');
	$LeftOvers = $pdf->addTextWrap($Left_Margin+65,$YPos,300,$FontSize,_('TOTAL Banked (cash:')._('$').$cashdeposit.', Cheque:'._('$').$cheqdeposit . ')', 'left');
	
	//$pdf->line($XPos, $Bottom_Margin,$XPos, $YPos);
	
$pdf->addTextWrap($Left_Margin+230,$Bottom_Margin-10,300,$FontSize,_('Powered by E-Business ERP 2.0'), 'left');
	//$pdf->OutputD($_SESSION['DatabaseName'] . '_BankingSummary_' . date('Y-m-d').'.pdf');
	
ob_end_clean();
		$pdf->OutputD('RePrint_Banking_summary_wormald_' . $bankname . date('d-m-Y').'.pdf');
	$pdf->__destruct();
	

	



		
			    
		
	


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

?>

  <script>


  $( function() {

    $( "#datepicker" ).datepicker({

    dateFormat: 'dd/mm/yy',//check change

    changeMonth: true,

    changeYear: true

});

  } );

	  

 
 

	  jQuery(document).ready(function($){
		  
		  
		  
		  
		  $(document).on('click', '.remove_row', function(){

          var row_id = $(this).attr("id");

        
          $('#row_id_'+row_id).remove();
			  
			  var counttotal =  $('#total_item').val();

         counttotal --;

          $('#total_item').val(counttotal);
			  
			  
			  
        });

		   $('#process').click(function(){

          


			

			$('#process').hide();
			   
			  



        });
		  
		  var table = $('#invoice-item-table1').DataTable( {
        dom: 'Bfrtip',
	    order: [[ 3, 'desc' ]],
        buttons: [
           
		{
                extend: 'pdfHtml5',
                orientation: 'landscape',
			pageSize: 'A4',
			
			
                customize: function(doc) {
      doc.defaultStyle.fontSize = 12; //<-- set fontsize to 16 instead of 10 
					  
   }  
            },
			{
                extend: 'excelHtml5'},
			
			{
                extend: 'copyHtml5'}
			
			
		],
	   
	   
	   
	   
    } );
		
		
		  
		  
		  
		  	 });
	  
	  

  </script>	
