Skip to content

Instantly share code, notes, and snippets.

@bendechrai
Created October 28, 2014 11:17
Show Gist options
  • Save bendechrai/c87d639c1f7679d7c560 to your computer and use it in GitHub Desktop.
Save bendechrai/c87d639c1f7679d7c560 to your computer and use it in GitHub Desktop.
Simple parser of Westpac statements
#!/usr/bin/php
<?php
/**
* Simple parser of Westpac statements
* @author Ben Dechrai <ben@dechrai.com>
* @license http://www.json.org/license.html
*
* Run ./parse-westpac input.txt output.csv
*
* I make no apologies for the lack of professional consideration in the creation of this
* script. It was hacked together. I hope it brings you joy. Back up your files first!
*
* This script will read a file with a similar format to:
*
* DATE DESCRIPTION OF DEBIT CREDIT BALANCE
* TRANSACTION
* ________________________________________________________________
* 2007
* STATEMENT OPENING BALANCE 4593.68
* 23OCT DEPOSIT SOME CO NAME PTY
* PAYMENT 1234 1818.00 6411.68
* 01NOV WITHDRAWAL - INTERNET
* ONLINE BANKING 1557288
* XFER 01-NOV 1818.00 4593.68
* 02NOV INTERNET ONLINE BANKING
* TRANSACTION FEE 0.50 4593.18
* 12NOV CLOSING BALANCE 4593.18
*
*
* And produce an output like:
*
* 1193061600,"DEPOSIT SOME CO NAME PTY PAYMENT 1234",,1818,6411.68
* 1193835600,"WITHDRAWAL - INTERNET ONLINE BANKING 1557288 XFER 01-NOV",1818,,4593.68
* 1193922000,"INTERNET ONLINE BANKING TRANSACTION FEE",0.5,,4593.18
*
*/
$inputfile = null;
$outputfile = null;
if(count($_SERVER['argv'])>2) list($script, $inputfile, $outputfile) = @$_SERVER['argv'];
if(is_null($inputfile)) {
echo "NOTICE: Please provide the input and output filenames as parameters to this script\n";
exit;
}
$transactions = array();
$transaction = newTransaction();
$balance = null;
$year = null;
$fhin = fopen($inputfile, 'r');
$fhout = fopen($outputfile, 'w');
while($line=fgets($fhin)) {
$line = trim($line, "\n");
if($line=='') continue;
// Ignore separator lines
if(preg_match('#^_+$#', $line)) continue;
// Detect year markers
if(preg_match('#^[0-9]{4}$#', $line)) {
$year = intval($line);
continue;
}
// Processing assumes a minimum length string
$line = str_pad($line, 70);
// Split string in to columns
preg_match('#^(..)(...).(...........................)(.........)(.........)(.............)#', $line, $columns);
list($original, $day, $month, $description, $debit, $credit, $balance) = $columns;
// If there's a valid date, convert it
if(preg_match('#^[0-9]{2}$#', $day)) $transaction['date'] = strtotime("$day $month $year");
// If this transaction has a date
if(!is_null($transaction['date'])) {
// Collect the description
$transaction['description'] = trim($transaction['description'] . ' ' . trim($description));
// If there's a balance, store finances and process line
if((float)$balance > 0) {
if((float)$debit > 0) $transaction['debit'] = (float)$debit;
if((float)$credit > 0) $transaction['credit'] = (float)$credit;
if((float)$balance > 0) $transaction['balance'] = (float)$balance;
// Ignore closing balances
if($transaction['description']!='CLOSING BALANCE') {
fputcsv($fhout, $transaction);
}
// End of transactions, start anew
$transaction = newTransaction();
}
}
}
function newTransaction() {
return array(
'date' => null,
'description' => '',
'debit' => null,
'credit' => null,
'balance' => null
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment