<?php
ob_start();

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



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

$reasons = date('Y-m-d');

$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.'       '.'EVERGREEN INTERNATIO' ."\r\n";
		fwrite($file, $header);
		
	//trans

				$SQL = "SELECT  payrun_details.payid,
								payrun_details.employeeid,
								payrun_details.net_pay,
								employeemaster.first_name,
								employeemaster.last_name,
								employeemaster.bank_name,
								employeemaster.account_number,
								employeemaster.account_name,
								employeemaster.second_bankname,
								employeemaster.second_accountnumber,
								employeemaster.second_accountname,
								employeemaster.second_amount

													
							    
								
								
									
		                            FROM payrun_details 
									INNER JOIN employeemaster on employeemaster.employeeid = payrun_details.employeeid
									
		                            WHERE payrun_details .id = '" . $id."'
									AND payrun_details.method_of_payment = 'Transfer to Bank'
									
									";

$result=DB_query($SQL);








	while ($myrow50=DB_fetch_array($result)){
		
		//get name
		
		//$SQL1 = "SELECT first_name,last_name
									
		                    //        FROM employeemaster
									
		                        //    WHERE employeeid = '" . $myrow50['employeeid']."'
								//	";

//$result1=DB_query($SQL1);

		//$myrow501=DB_fetch_array($result1);
		
$employeename = $myrow50['first_name'].' '.$myrow50['last_name'];



		//print the transactions


//check if another bank account split payment
if (isset($myrow50['second_accountnumber']) && ($myrow50['second_amount'] > 0 ) ) {
	//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['account_number'])==12 ){
			
			$accountnumber = $myrow50['account_number'];
			$leadingzeros ='';
		}
		if (strlen($myrow50['account_number']) < 12 ){
			
			$count = strlen($myrow50['account_number']);
			
			$numberofzeros = 12-$count;
			
			$accountnumber = $myrow50['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 (isset($myrow50['second_accountnumber']) && ($myrow50['second_amount'] > 0 ) ) {

					$secondamount = $myrow50['second_amount'];
					$firstamount = $myrow50['net_pay'] - $myrow50['second_amount'];

			$netpay = $firstamount;

		} else{

			$netpay = $myrow50['net_pay'];

		}
		//$netpay = $myrow50['net_pay'];

		


		$finalpay1 = str_replace(".", "", $netpay);
		




		if (strlen($finalpay1 )==10 ){
			
			//$netpay = $myrow50['net_pay'];
			$finalpay = str_replace(".", "", $netpay);
		}
		if (strlen($finalpay1 ) < 10 ){
			
			$countzero = strlen($finalpay1);
			
			$numberofzerosnp = 10-$countzero;
			
			$netpay = $netpay;
			
			$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($employeename)==20 ){
			
			$payeename = $employeename;
		}
		if (strlen($employeename) < 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			
			$payeename = $employeename;
			
			
			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 ($char == 0){
				
				$payespace = '';
			}
		}
	
			if (strlen($employeename) >= 20 ){
			
		
			
			$payeename = substr($employeename,0,20);
			
		}
		
		if (strlen($employeename) < 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			$spacesneeded = 36+$char;
		}
		if (strlen($employeename) >= 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			$spacesneeded = 36;
		}
		
		$spacebeforereference = '                                    ';
		
   $trans = '13'.$bankcode .'9'.$branchcode.$leadingzeros.$accountnumber.'053'.$leadingzerosnp.$finalpay.$payeename. "\r\n";
		//find code for the payees branch and bank codes
		if ($myrow50['second_bankname'] == 'Westpac'){
			
			$bankcode = '03';
			$branchcode = '001';
			
		}
		if ($myrow50['second_bankname'] == 'ANZ'){
			$bankcode = '09';
			$branchcode = '015';
		}
		if ($myrow50['second_bankname'] == 'BSP'){
			
			$bankcode = '07';
			$branchcode = '001';
		}
		if ($myrow50['second_bankname'] == 'BRED'){
			$bankcode = '11';
			$branchcode = '001';
			
		}
		if ($myrow50['second_bankname'] == 'HFC'){
			$bankcode = '12';
			$branchcode = '001';
			
		}
		if ($myrow50['second_bankname'] == 'BOB'){
			$bankcode = '05';
			$branchcode = '001';
		}
		
		
		//determine the lenght of account numbers
		
		if (strlen($myrow50['second_accountnumber'])==12 ){
			
			$accountnumber = $myrow50['second_accountnumber'];
			$leadingzeros ='';
		}
		if (strlen($myrow50['second_accountnumber']) < 12 ){
			
			$count = strlen($myrow50['second_accountnumber']);
			
			$numberofzeros = 12-$count;
			
			$accountnumber = $myrow50['second_accountnumber'];
			
			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';
			}
			
		}

     


$netpay = $myrow50['second_amount'];


		


		$finalpay1 = str_replace(".", "", $netpay);
		




		if (strlen($finalpay1 )==10 ){
			
			$netpay = $myrow50['second_amount'];

			$finalpay = str_replace(".", "", $netpay);
		}
		if (strlen($finalpay1 ) < 10 ){
			
			$countzero = strlen($finalpay1);
			
			$numberofzerosnp = 10-$countzero;
			
			$netpay = $myrow50['second_amount'];
			
			$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($employeename)==20 ){
			
			$payeename = $employeename;
		}
		if (strlen($employeename) < 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			
			$payeename = $employeename;
			
			
			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 ($char == 0){
				
				$payespace = '';
			}
		}
	
			if (strlen($employeename) >= 20 ){
			
		
			
			$payeename = substr($employeename,0,20);
			
		}
		
		if (strlen($employeename) < 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			$spacesneeded = 36+$char;
		}
		if (strlen($employeename) >= 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			$spacesneeded = 36;
		}
		
		$spacebeforereference = '                                    ';
		
   $trans2 = '13'.$bankcode .'9'.$branchcode.$leadingzeros.$accountnumber.'053'.$leadingzerosnp.$finalpay.$payeename. "\r\n";

fwrite($file, $trans);
fwrite($file, $trans2);
}else{
	
		//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['account_number'])==12 ){
			
			$accountnumber = $myrow50['account_number'];
			$leadingzeros ='';
		}
		if (strlen($myrow50['account_number']) < 12 ){
			
			$count = strlen($myrow50['account_number']);
			
			$numberofzeros = 12-$count;
			
			$accountnumber = $myrow50['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 (isset($myrow50['second_accountnumber']) && ($myrow50['second_amount'] > 0 ) ) {

					$secondamount = $myrow50['second_amount'];
					$firstamount = $myrow50['net_pay'] - $myrow50['second_amount'];

			$netpay = $firstamount;

		} else{

			$netpay = $myrow50['net_pay'];

		}
		//$netpay = $myrow50['net_pay'];

		


		$finalpay1 = str_replace(".", "", $netpay);
		




		if (strlen($finalpay1 )==10 ){
			
			//$netpay = $myrow50['net_pay'];
			$finalpay = str_replace(".", "", $netpay);
		}
		if (strlen($finalpay1 ) < 10 ){
			
			$countzero = strlen($finalpay1);
			
			$numberofzerosnp = 10-$countzero;
			
			$netpay = $netpay;
			
			$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($employeename)==20 ){
			
			$payeename = $employeename;
		}
		if (strlen($employeename) < 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			
			$payeename = $employeename;
			
			
			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 ($char == 0){
				
				$payespace = '';
			}
		}
	
			if (strlen($employeename) >= 20 ){
			
		
			
			$payeename = substr($employeename,0,20);
			
		}
		
		if (strlen($employeename) < 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			$spacesneeded = 36+$char;
		}
		if (strlen($employeename) >= 20 ){
			
			$count1 = strlen($employeename);
			
			$char = 20-$count1;
			$spacesneeded = 36;
		}
		
		$spacebeforereference = '                                    ';
		
   $trans = '13'.$bankcode .'9'.$branchcode.$leadingzeros.$accountnumber.'053'.$leadingzerosnp.$finalpay.$payeename. "\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'];
		
			$zeros  = '';

		
		}
	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';
			}
	}
	$total = $myrow505['totalpay'];
	$finaltotalpay1 = str_replace(".", "", $total);
	if (strlen($finaltotalpay1) == 10 ){
			
		$zeros1 ='';
			
			$total = $myrow505['totalpay'];
		
			$finaltotalpay = str_replace(".", "", $total);
		}
	if (strlen($finaltotalpay1)< 10 ){
			
		
			
	$total = $myrow505['totalpay'];
		
			$finaltotalpay = str_replace(".", "", $total);
		
			
		
      $count7 = strlen($finaltotalpay1);
			
			$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';
			}
		if ($numberofzerosnp11 == 0 ){
				
				$zeros1 = '';
			}
	}
	

 $trailer= '139900000000000201   '.$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');
	

	

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




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

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

?>

