Project Website | Online Demo | Forum |
It is currently Fri Oct 30, 2020 6:03 pm

All times are UTC + 1 hour [ DST ]





Post new topic Reply to topic  [ 1 post ] 
  Print view Previous topic | Next topic 
Author Message
 Post subject: Parser for First National Bank in South Africa
PostPosted: Sat Feb 18, 2012 11:34 am 
Offline
Site Admin
User avatar

Joined: Tue Mar 07, 2006 7:34 pm
Posts: 331
User ScorpKing provided the following parser for FNB (South Africa).

Thanks a lot ScorpKing!

Code:
<?php
/*
* ____          _____   _____ ______ _____
*|  _ \   /\   |  __ \ / ____|  ____|  __ \
*| |_) | /  \  | |  | | |  __| |__  | |__) |
*|  _ < / /\ \ | |  | | | |_ |  __| |  _  /
*| |_) / ____ \| |__| | |__| | |____| | \ \
*|____/_/    \_\_____/ \_____|______|_|  \_\
* Open Source Finance Management
* Visit http://www.badger-finance.org
*
* Parse .csv detailed transaction history downloaded files from FNB South Africa . Tested with examplefile from 2012-02-16 by ScorpKing
**/
// The next line determines the displayed name of this parser.
// BADGER_REAL_PARSER_NAME FNB South Africa CSV
define ('HEADER_END_MARKER', 'Date');

/**
* transform csv to array
*
* File to store: ..\modules\csvImport\parser\republic_bank_csv.php
*
* @param $fp filepointer, $accountId
* @return array (categoryId, accountId, title, description, valutaDate, amount, transactionPartner)
*/

function parseToArray($fp, $accountId){
  /**
   * count Rows of csv
   *
   * @var int
   */
  $csvRow = 0;
  /**
   * is set true, a line contains "\t" (tabs), but not the correct number for this parser (5)
   *
   * @var boolean
   */
  $noValidFile = NULL;
  /**
   * is set true, after the header was ignored
   *
   * @var boolean
   */
  $headerIgnored = FALSE;
  function avoid_bad_sign($strings) {
     $strings = str_replace("\"", "", $strings);
     $strings = str_replace("\\", "", $strings);
     $strings = str_replace("\n", " ",$strings);
     $strings = str_replace("\r", " ",$strings);
     return $strings;
  }
  // check the valueDate, if day | month have only 1 digit and adds an leading zero
  // those numbers could cause problems
  function strlen2($string) {
     if (strlen(trim($string)) == 1) {
        $string = "0".trim($string);
     }
     return $string;
  }
  // check the valueDate, if year have only 2 digits.
  // If the number of the year is between 50 and 99 it will add a leading 19 else a leading 20
  // those numbers could cause problems
  function strlen4($string) {
     if (strlen(trim($string)) == 2) {
        if ($string >= 50 && $string <= 99) {
           $string = "19".trim($string);
        } else {
           $string = "20".trim($string);
        }
     }
     return trim($string);
  }

/**************************************** E X A M P L E *************************************
* 0_Date        1_Amount    2_Balance    3_Description
* 2012/02/16    -100        1596.11         CASH WITHDRAWAL
* 2012/02/15    -1            1696.11         BANK FEE
* 2012/02/15    114            1697.11         CASH DEPOSIT
* 2012/02/14    -159        1583.11         CASH WITHDRAWAL
********************************************************************************************/
  while (!feof($fp)) {

  $rowArray = NULL;

  //read one line
  $line = fgets($fp, 1024);
        //ignore header (first 6 lines)
        if (!$headerIgnored){
           for ($headerLine = 0; $headerLine < 6; $headerLine++) {
              $garbage = fgets($fp, 1024);
              //to ignore this code on the next loop run
              $headerIgnored = true;
           }
        }
     //if array is not empty or is no header
     while ($transactionArray = fgetcsv ($fp, 1024, ","))   {
        if (!empty ($transactionArray[0]) ) {
           //if array contains exactly 4 (sometimes descriptions have a separator in them so ignore it), to ensure it is a valid csv file

           if ( count ($transactionArray) == 4) {
              //format date YY-MM-DD or YYYY-MM-DD
              $transactionArray[0] = avoid_bad_sign($transactionArray[0]);
              $valutaDate = explode("/", $transactionArray[0]); //Valuta Date
              $valutaDate[0] = strlen4($valutaDate[0]);
              $valutaDate[1] = strlen2($valutaDate[1]);
              $valutaDate[2] = strlen2($valutaDate[2]);
              $valutaDate[4] = $valutaDate[0] . "-" . $valutaDate[1] . "-" . $valutaDate[2];
              $valutaDate1 = new Date($valutaDate[4]);

              $transactionArray[3] = avoid_bad_sign($transactionArray[3]);
              $amount = new Amount($transactionArray[1]);

           /**
            * transaction array
            *
            * @var array
            */
           $rowArray = array (
              "categoryId" => "",
              "accountId" => $accountId,
              "title" => substr($transactionArray[3],0,99), // cut title with more than 100 chars
              "description" => $transactionArray[3],
              "valutaDate" => $valutaDate1,
              "amount" => $amount,
              "transactionPartner" => ""
              );

           } else{
              $noValidFile = 'true';
           }
        }

        // if a row contains valid data
        if ($rowArray){
        /**
         * array of all transaction arrays
         *
         * @var array
         */
        $importedTransactions[$csvRow] = $rowArray;
        $csvRow++;
        }
     }
  }
  if ($noValidFile) {
     throw new badgerException('importCsv', 'wrongSeperatorNumber');
     //close file
     fclose ($fp);
  } else {
     if ($csvRow == 0){
        throw new badgerException('importCsv', 'noSeperator');
        //close file
        fclose ($fp);
     } else{
        //close file
        fclose ($fp);
        return $importedTransactions;
     }
  }
}
?>

_________________
BADGER finance lead backend developer and site admin


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron




Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Style supported by CodeMiles Team.