Project Website | Online Demo | Forum |
It is currently Sun Apr 22, 2018 12:54 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: CSV Parser for DKB (Deutsche Kreditbank)
PostPosted: Sun Jan 03, 2010 1:08 pm 
Offline

Joined: Fri Mar 17, 2006 6:53 pm
Posts: 201
Location: Germany: Mannheim, Kassel
Just added a new parser for DKB Cash (Deutsche Kreditbank) http://www.dkb.de

Link to SVN:
http://svn.badger-finance.org/filedetai ... tKonto.php

Code:
<?php
/*
* ____          _____   _____ ______ _____ 
*|  _ \   /\   |  __ \ / ____|  ____|  __ \
*| |_) | /  \  | |  | | |  __| |__  | |__) |
*|  _ < / /\ \ | |  | | | |_ |  __| |  _  /
*| |_) / ____ \| |__| | |__| | |____| | \ \
*|____/_/    \_\_____/ \_____|______|_|  \_\
* Open Source Finance Management
* Visit http://www.badger-finance.org
*
* Parse .csv files from DKB Deutsche Kredit Bank (Germany) DKB-Cash
*
* created by Sepp (03.01.2010)
**/
/* AUFBAU:
* 1: Buchungsdatum                   
* 2: Valuta     
* 3: Buchungstext             
* 4: Auftraggeber/Empfänger
* 5: Verwendungszweck
* 6: Kontonummner
* 7: BLZ
* 8: Betrag in EUR

*/
// The next line determines the displayed name of this parser.
// BADGER_REAL_PARSER_NAME DKB Cash Konto - Deutsche Kreditbank
/**
* transform csv to array
*
* @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 = NULL;
        //for every line
        while (!feof($fp)) {
            //read one line
            $rowArray = NULL;
            //ignore header (first 8 lines)
            if (!$headerIgnored){
                for ($headerLine = 0; $headerLine < 7; $headerLine++) {
                    $garbage = fgets($fp, 1024);
                    //to ignore this code on the next loop run
                    $headerIgnored = true;
                }
            }
            //read one line
            $line = fgets($fp, 1024);
            //if line is not empty or is no header
            if (strstr($line, ";")) { //
           
                //if line contains excactly 8 ;, to ensure it is a valid csv file
                if (substr_count ($line, ";")==8){
                    // divide String to an array
                    $transactionArray = explode(";", $line);
                   
                    // 0. Booking date (Buchungsdatum)
                    //$transactionArray[0] =   UNUSED
                   
                    // 1. Valuta Date (Wertstellung)                   
                    $transValuta = str_replace("\"","",$transactionArray[1]);
                    $transValuta = explode(".", $transValuta); //Valuta Date
                    $transValuta[4] = $transValuta[2] . "-" . $transValuta[1] . "-" . $transValuta[0];
                    $valutaDateObj = new Date($transValuta[4]);
                   
                    // 2. Description (Buchungstext, eg. Gutschrift)
                    $transDescription = $transactionArray[2];
                    $transDescription = str_replace("\"","",$transDescription);
                    $transDescription = str_replace("\\","",$transDescription);
                    $transDescription = trim($transDescription);
                   
                    // 3. Transaction Partner (Auftraggeber/Empfänger)
                    $transactionPartner = $transactionArray[3];
                    $transactionPartner = str_replace("\"","",$transactionPartner);
                    $transactionPartner = str_replace("\\","",$transactionPartner);
                    $transactionPartner = trim($transactionPartner);
                   
                    // 4. Title (Verwendungszweck)
                    $transTitle = $transactionArray[4];
                    $transTitle = str_replace("\"","",$transTitle);
                    $transTitle = str_replace("\\","",$transTitle);
                    $transTitle = trim($transTitle);
                   
                    // 5. Kontonummer
                   
                    // 6. BLZ
                               
                    // 7. Amount (Betrag)
                    $transAmount = $transactionArray[7];
                    $transAmount = str_replace("\"","", $transAmount);
                    $transAmount = str_replace("\\","", $transAmount);
                    //format amount to usersettings
                    $transAmount = str_replace(".","", $transAmount);
                    $transAmount = str_replace(",",".",$transAmount);
                    $transAmountObj = new Amount($transAmount);
                   
                   
                    /**
                     * transaction array
                     *
                     * @var array
                     */
                    $rowArray = array (
                       "categoryId" => "",
                       "accountId" => $accountId,
                       "title" => substr($transTitle, 0,99),// cut title with more than 100 chars
                       "description" => $transDescription,
                       "valutaDate" => $valutaDateObj,
                       "amount" => $transAmountObj,
                       "transactionPartner" => $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;
            }
        }
}

?>


Edit: small bug with amount format solved

_________________
BADGER finance lead frontend developer


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 2 guests


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.