<?php
ob_start();

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
include('includes/session.inc');
$Title = _('FRCS EMS File');
include('includes/SQL_CommonFunctions.inc');
include('includes/newheader.php');

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


$printdate = date("d-m-Y");




$reasons = $_POST['reason'];

if (isset($_POST['reversal']) && ($_POST['reason'] == $reasons )){

//print the header
	
	



$monthdate =date('m',strtotime($reasons));
	$daydate =date('d',strtotime($reasons));
	$yeardate =date('Y',strtotime($reasons));
	 $file= fopen("westpac/payrol.txt","w");
		
   $header = '12039030009804778075201 '.$daydate.$monthdate.$yeardate.'EDS0000'.'Evergreen' ."\r\n";
		fwrite($file, $header);
		
	//trans

				$SQL = "SELECT payid,
								net_pay,
								bank_name,
								branch,
								bank_account_name,
								bank_account_number
								
							    
								
								
									
		                            FROM payrun_details 
									
		                            WHERE id = '" . $id."'
									AND method_of_payment = 'Transfer to Bank'
									
									";

$result=DB_query($SQL);








	while ($myrow50=DB_fetch_array($result)){
		
		//print the transactions

		//find code for the payees branch and bank codes
		if ($myrow50['bank_name'] == 'Westpac'){
			
			$bankcode = '03';
			$branchcode = '001';
			
		}
		if ($myrow50['bank_name'] == 'ANZ'){
			$bankcode = '09';
			$branchcode = '015';
		}
		if ($myrow50['bank_name'] == 'BSP'){
			
			$bankcode = '07';
			$branchcode = '001';
		}
		if ($myrow50['bank_name'] == 'BRED'){
			$bankcode = '11';
			$branchcode = '001';
			
		}
		if ($myrow50['bank_name'] == 'HFC'){
			$bankcode = '12';
			$branchcode = '001';
			
		}
		if ($myrow50['bank_name'] == 'BOB'){
			$bankcode = '05';
			$branchcode = '001';
		}
		
		
		//determine the lenght of account numbers
		
		if (strlen($myrow50['bank_account_number'])==12 ){
			
			$accountnumber = $myrow50['bank_account_number'];
		}
		if (strlen($myrow50['bank_account_number']) < 12 ){
			
			$count = strlen($myrow50['bank_account_number']);
			
			$numberofzeros = 12-$count;
			
			$accountnumber = $myrow50['bank_account_number'];
			
			if ($numberofzeros == 8 ){
				
				$leadingzeros = '00000000';
			}
			if ($numberofzeros == 7 ){
				
				$leadingzeros = '0000000';
			}
			if ($numberofzeros == 6 ){
				
				$leadingzeros = '000000';
			}
			if ($numberofzeros == 5 ){
				
				$leadingzeros = '00000';
			}
			if ($numberofzeros == 4 ){
				
				$leadingzeros = '0000';
			}
			if ($numberofzeros == 3 ){
				
				$leadingzeros = '000';
			}
			if ($numberofzeros == 2 ){
				
				$leadingzeros = '00';
			}
			if ($numberofzeros == 1 ){
				
				$leadingzeros = '0';
			}
			
		}
		
		
		
		if (strlen($myrow50['net_pay'])==10 ){
			
			$netpay = $myrow50['net_pay'];
			$finalpay = str_replace(".", "", $netpay);
		}
		if (strlen($myrow50['net_pay']) < 10 ){
			
			$countzero = strlen($myrow50['net_pay']);
			
			$numberofzerosnp = 10-$countzero;
			
			$netpay = $myrow50['net_pay'];
			
			$finalpay = str_replace(".", "", $netpay);
			if ($numberofzerosnp == 9 ){
				
				$leadingzerosnp = '000000000';
			}
			
			if ($numberofzerosnp == 8 ){
				
				$leadingzerosnp = '00000000';
			}
			if ($numberofzerosnp == 7 ){
				
				$leadingzerosnp = '0000000';
			}
			if ($numberofzerosnp == 6 ){
				
				$leadingzerosnp = '000000';
			}
			if ($numberofzerosnp == 5 ){
				
				$leadingzerosnp = '00000';
			}
			if ($numberofzerosnp == 4 ){
				
				$leadingzerosnp = '0000';
			}
			if ($numberofzerosnp == 3 ){
				
				$leadingzerosnp = '000';
			}
			if ($numberofzerosnp == 2 ){
				
				$leadingzerosnp = '00';
			}
			if ($numberofzerosnp == 1 ){
				
				$leadingzerosnp = '0';
			}
			
		}
		
			if (strlen($myrow50['bank_account_name'])==20 ){
			
			$payeename = $myrow50['bank_account_name'];
		}
		if (strlen($myrow50['bank_account_name']) < 20 ){
			
			$count1 = strlen($myrow50['bank_account_name']);
			
			$char = 20-$count1;
			
			$payeename = $myrow50['bank_account_name'];
			
			
			if ($char == 15 ){
				
				$payespace  = '               ';
			}
			
			if ($char == 14 ){
				
					$payespace  = '              ';
			}
			if ($char == 13 ){
				
					$payespace  = '             ';
			}
			if ($char == 12 ){
				
				$payespace = '            ';
			}
			if ($char == 11 ){
				
				$payespace = '           ';
			}
			if ($char == 10 ){
				
				$payespace = '          ';
			}
		
			if ($char == 9 ){
				
				$payespace = '         ';
			}
			if ($char == 8 ){
				
				$payespace = '        ';
			}
			if ($char == 7 ){
				
				$payespace = '       ';
			}
			if ($char == 6 ){
				
				$payespace = '      ';
			}
			if ($char == 5 ){
				
				$payespace = '     ';
			}
			if ($char == 4 ){
				
				$payespace = '    ';
			}
			if ($char == 3 ){
				
				$payespace = '   ';
			}
			if ($char == 2 ){
				
				$payespace = '  ';
			}
			if ($char == 1 ){
				
				$payespace = ' ';
			}
		}
	
			if (strlen($myrow50['bank_account_name']) > 20 ){
			
		
			
			$payeename = substr($myrow50['bank_account_name'],0,20);
			
		}
		
		
   $trans = '13'.$bankcode .'9'.$branchcode.$leadingzeros.$accountnumber.'000'.$leadingzerosnp.$finalpay.$payeename.$payespace.'000000000000'.'                                     Evergreen                      EDS0000                     '. "\r\n";
		fwrite($file, $trans);
	}


//print footer 
	
	$SQL5 = "SELECT 
								count(payid) as harshtotal,
								SUM(net_pay) as totalpay
								
							    
								
								
									
		                            FROM payrun_details 
									
		                            WHERE id = '" . $id."'
									AND method_of_payment = 'Transfer to Bank'
									
									";

$result5=DB_query($SQL5);








$myrow505=DB_fetch_array($result5);
	
	
	if (strlen($myrow505['harshtotal'])== 11 ){
			
		
			
			$hashtotal = $myrow505['harshtotal'];
		
			
		}
	if (strlen($myrow505['harshtotal'])< 11 ){
			
		
			
	$hashtotal = $myrow505['harshtotal'];
		
			
		
      $count9 = strlen($myrow505['harshtotal']);
			
			$numberofzerosnp1 = 11-$count9;
			
		
			
			
			if ($numberofzerosnp1 == 10 ){
				
				$zeros  = '0000000000';
			}
		
			if ($numberofzerosnp1 == 9 ){
				
				$zeros = '000000000';
			}
			
			if ($numberofzerosnp1 == 8 ){
				
				$zeros = '00000000';
			}
			if ($numberofzerosnp1 == 7 ){
				
				$zeros = '0000000';
			}
			if ($numberofzerosnp1 == 6 ){
				
				$zeros = '000000';
			}
			if ($numberofzerosnp1 == 5 ){
				
				$zeros = '00000';
			}
			if ($numberofzerosnp1 == 4 ){
				
				$zeros = '0000';
			}
			if ($numberofzerosnp1 == 3 ){
				
				$zeros = '000';
			}
			if ($numberofzerosnp1 == 2 ){
				
				$zeros = '00';
			}
			if ($numberofzerosnp1 == 1 ){
				
				$zeros = '0';
			}
	}
	
	
	if (strlen($myrow505['totalpay']) == 10 ){
			
		
			
			$total = $myrow505['totalpay'];
		
			$finaltotalpay = str_replace(".", "", $total);
		}
	if (strlen($myrow505['totalpay'])< 10 ){
			
		
			
	$total = $myrow505['totalpay'];
		
			$finaltotalpay = str_replace(".", "", $total);
		
			
		
      $count7 = strlen($myrow505['totalpay']);
			
			$numberofzerosnp11 = 10-$count7;
			
		
			
			
				
			if ($numberofzerosnp11 == 9 ){
				
				$zeros1 = '000000000';
			}
			
			if ($numberofzerosnp11 == 8 ){
				
				$zeros1 = '00000000';
			}
			if ($numberofzerosnp1 == 7 ){
				
				$zeros = '0000000';
			}
			if ($numberofzerosnp11 == 6 ){
				
				$zeros1 = '000000';
			}
			if ($numberofzerosnp11 == 5 ){
				
				$zeros1 = '00000';
			}
			if ($numberofzerosnp1 == 4 ){
				
				$zeros = '0000';
			}
			if ($numberofzerosnp11 == 3 ){
				
				$zeros1 = '000';
			}
			if ($numberofzerosnp11 == 2 ){
				
				$zeros1 = '00';
			}
			if ($numberofzerosnp11 == 1 ){
				
				$zeros1 = '0';
			}
	}
	
 $trailer= '1399'.$zero.$hashtotal.'201   '.$zeros1.$finaltotalpay.'                                                                                                                              '. "\r\n";
		fwrite($file, $trailer);


	fclose($file);


	    
		echo "<br>";
	
     echo '<p class="page_title_text"><a href=westpac/payrol.txt download="DISDATA.PC1" >' . _('Successfully created the file. click here to download it');
	
}
else 
{
	
	
echo '<p> </p> <p></p><p class="page_title_text"><img alt="" src="'.$RootPath.'/css/'.$Theme.
		'/images/printer.png" title="' .// Icon image.
		_('Due date.  ') . '" /> ' .// 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 Due Date'). ':</td>
			<td><input tabindex="'.$j.'" type="date" maxlength="10" size="15" required="required" name="reason"  /></td>
		</tr>';

echo '</td>';

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



echo '</div>';

echo '</div>';
}
				
		
	

	
//echo 'Quotation Created Successfully'. $jobno.'!';




//echo 'Quotation Created Successfully'. $jobno.'!';
echo '</div>';
echo '</div>';

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

?>

