Recipes
The recipes parse the sample statement file: three statements of two accounts.
Filtering operations
Section titled “Filtering operations”Operations above 100 in absolute value, debits and credits alike:
<?php
use Silarhi\Cfonb\Banking\Operation;use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse(file_get_contents('statements.txt'));
foreach ($statements as $statement) { $largeOperations = array_filter( $statement->getOperations(), fn (Operation $operation) => abs($operation->getAmount()) > 100 );
foreach ($largeOperations as $operation) { echo sprintf("Large operation: %.2f - %s\n", $operation->getAmount(), $operation->getLabel()); }}Large operation: 5000.00 - VIR JOHNDOE / FOOBARBuilding a transaction summary
Section titled “Building a transaction summary”<?php
use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse(file_get_contents('statements.txt'));
$summary = ['total_credit' => 0.0, 'total_debit' => 0.0, 'operation_count' => 0];
foreach ($statements as $statement) { foreach ($statement->getOperations() as $operation) { $amount = $operation->getAmount(); if ($amount > 0) { $summary['total_credit'] += $amount; } else { $summary['total_debit'] += abs($amount); } ++$summary['operation_count']; }}
echo sprintf("Total credits: %.2f\n", $summary['total_credit']);echo sprintf("Total debits: %.2f\n", $summary['total_debit']);echo sprintf("Operations: %d\n", $summary['operation_count']);Total credits: 5000.00Total debits: 63.01Operations: 4Amounts are floats: round them, or add cents ((int) round($amount * 100)), when the result must be exact.
Converting to an array or JSON
Section titled “Converting to an array or JSON”For an API response, or to store the statements:
<?php
use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse(file_get_contents('statements.txt'));
$data = [];foreach ($statements as $statement) { $statementData = [ 'account_number' => $statement->getOldBalance()->getAccountNumber(), 'date' => $statement->getNewBalance()->getDate()->format('Y-m-d'), 'opening_balance' => $statement->getOldBalance()->getAmount(), 'closing_balance' => $statement->getNewBalance()->getAmount(), 'operations' => [], ];
foreach ($statement->getOperations() as $operation) { $statementData['operations'][] = [ 'date' => $operation->getDate()->format('Y-m-d'), 'value_date' => $operation->getValueDate()->format('Y-m-d'), 'amount' => $operation->getAmount(), 'label' => $operation->getLabel(), 'reference' => $operation->getReference(), 'bank_code' => $operation->getBankCode(), 'account_number' => $operation->getAccountNumber(), ]; }
$data[] = $statementData;}
echo json_encode($data, JSON_PRETTY_PRINT), "\n";[ { "account_number": "00012345603", "date": "2020-04-07", "opening_balance": 16695.65, "closing_balance": 16672.86, "operations": [ { "date": "2020-04-07", "value_date": "2020-04-07", "amount": -22.79, "label": "PRLV SEPA ONLINE SAS", "reference": "0000000", "bank_code": "10556", "account_number": "00012345603" } ] }, { "account_number": "00020427603", "date": "2020-04-08", "opening_balance": 16672.86, "closing_balance": 21632.64, "operations": [ { "date": "2020-04-08", "value_date": "2020-04-08", "amount": -20.11, "label": "PRLV SEPA FREE MOBILE", "reference": "0000000", "bank_code": "10556", "account_number": "00020427603" }, { "date": "2020-04-08", "value_date": "2020-04-08", "amount": 5000, "label": "VIR JOHNDOE \/ FOOBAR", "reference": "0000000", "bank_code": "10278", "account_number": "00020427603" }, { "date": "2020-04-08", "value_date": "2020-04-08", "amount": -20.11, "label": "VIR BOR TEST \/ FOOBAR", "reference": "0000000", "bank_code": "10278", "account_number": "00020427603" } ] }, { "account_number": "00020427603", "date": "2020-04-09", "opening_balance": 21632.64, "closing_balance": 21632.64, "operations": [] }]Handling multiple account statements
Section titled “Handling multiple account statements”A file can hold the statements of several accounts. Group them with the account number of their balance:
<?php
use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse(file_get_contents('statements.txt'));
// Group the statements by account$accountStatements = [];foreach ($statements as $statement) { $accountNumber = $statement->getOldBalance()->getAccountNumber(); $accountStatements[$accountNumber][] = $statement;}
foreach ($accountStatements as $accountNumber => $statementsOfAccount) { // The statements follow the order of the file: the last one holds the latest balance $last = $statementsOfAccount[array_key_last($statementsOfAccount)];
echo sprintf("Account %s\n", $accountNumber); echo sprintf(" Statements: %d\n", count($statementsOfAccount)); echo sprintf(" Final balance: %.2f\n", $last->getNewBalance()->getAmount());}Account 00012345603 Statements: 1 Final balance: 16672.86Account 00020427603 Statements: 2 Final balance: 21632.64Labeling operation codes
Section titled “Labeling operation codes”CfonbCodes::$codes gives the label of an operation code, its direction and its SWIFT equivalent:
<?php
use Silarhi\Cfonb\Cfonb120Reader;use Silarhi\Cfonb\CfonbCodes;
$reader = new Cfonb120Reader();
foreach ($reader->parse(file_get_contents('statements.txt')) as $statement) { foreach ($statement->getOperations() as $operation) { $code = CfonbCodes::$codes[$operation->getCode()] ?? null;
echo sprintf( "%-22s %s %s (%s, %s)\n", $operation->getLabel(), $operation->getCode(), $code['label'] ?? 'Unknown code', $code['swift_code'] ?? '-', $code['way'] ?? '-' ); }}PRLV SEPA ONLINE SAS B1 Prélèvements SEPA domiciliés (DDT, D)PRLV SEPA FREE MOBILE B1 Prélèvements SEPA domiciliés (DDT, D)VIR JOHNDOE / FOOBAR 05 Virements reçus (TRF, C)VIR BOR TEST / FOOBAR 06 Virements émis (TRF, D)The whole list: operation codes.
