Revize 2398
Přidáno uživatelem Ondřej Fibich před více než 9 roky(ů)
freenetis/branches/1.2/application/libraries/importers/Fio/FioCsvParser.php | ||
---|---|---|
*/
|
||
const DEFAULT_CHARSET = 'UTF-8';
|
||
|
||
/**
|
||
* All fields available must be used and sorted alphabetically
|
||
*
|
||
* @var array[string]
|
||
*/
|
||
private static $fields = array
|
||
(
|
||
'datum' => 'Datum',
|
||
'id_pohybu' => 'ID pohybu',
|
||
'id_pokynu' => 'ID pokynu',
|
||
'kod_banky' => 'Kód banky',
|
||
'ks' => 'KS',
|
||
'mena' => 'Měna',
|
||
'nazev_banky' => 'Název banky',
|
||
'nazev_protiuctu' => 'Název protiúčtu',
|
||
'castka' => 'Objem',
|
||
'protiucet' => 'Protiúčet',
|
||
'provedl' => 'Provedl',
|
||
'prevod' => 'Převod',
|
||
'ss' => 'SS',
|
||
'typ' => 'Typ',
|
||
'upresneni' => 'Upřesnění',
|
||
'identifikace' => 'Uživatelská identifikace',
|
||
'vs' => 'VS',
|
||
'zprava' => 'Zpráva pro příjemce',
|
||
);
|
||
/**
|
||
* All fields available must be used and sorted alphabetically
|
||
*
|
||
* @var array[string]
|
||
*/
|
||
private static $fields = array
|
||
(
|
||
'datum' => 'Datum',
|
||
'id_pohybu' => 'ID pohybu',
|
||
'id_pokynu' => 'ID pokynu',
|
||
'kod_banky' => 'Kód banky',
|
||
'ks' => 'KS',
|
||
'mena' => 'Měna',
|
||
'nazev_banky' => 'Název banky',
|
||
'nazev_protiuctu' => 'Název protiúčtu',
|
||
'castka' => 'Objem',
|
||
'protiucet' => 'Protiúčet',
|
||
'provedl' => 'Provedl',
|
||
'prevod' => 'Převod',
|
||
'ss' => 'SS',
|
||
'typ' => 'Typ',
|
||
'upresneni' => 'Upřesnění',
|
||
'identifikace' => 'Uživatelská identifikace',
|
||
'vs' => 'VS',
|
||
'zprava' => 'Zpráva pro příjemce',
|
||
);
|
||
|
||
/**
|
||
* FIO statement columns fields names.
|
||
... | ... | |
}
|
||
|
||
/**
|
||
* Parse bank statement in CSV format that is passed as string.
|
||
*
|
||
* @param string $csv string containing the original csv file.
|
||
* Parse bank statement in CSV format that is passed as string.
|
||
*
|
||
* @param string $csv string containing the original csv file.
|
||
* @param string $charset optional charset name of file, default is UTF-8
|
||
* @return FioCsvStatement result
|
||
* @throws Exception on parse error
|
||
*/
|
||
public function parse($csv, $charset = self::DEFAULT_CHARSET)
|
||
{
|
||
* @return FioCsvStatement result
|
||
* @throws Exception on parse error
|
||
*/
|
||
public function parse($csv, $charset = self::DEFAULT_CHARSET)
|
||
{
|
||
$result = new FioCsvStatement();
|
||
$sum = 0;
|
||
$keys = array_keys(self::$fields);
|
||
$sum = 0;
|
||
$keys = array_keys(self::$fields);
|
||
$lines = $this->transformFileToLineArray($csv, $charset);
|
||
// check each line of CSV
|
||
for ($i = 0; $i < count($lines); $i++)
|
||
{
|
||
$line = trim($lines[$i]);
|
||
{
|
||
$line = trim($lines[$i]);
|
||
// header
|
||
if ($i < 10)
|
||
{
|
||
$this->parseHeaderLine($result, $line, $i);
|
||
continue;
|
||
}
|
||
// data lines
|
||
// data lines
|
||
$cols = $this->parseLine($line, $keys);
|
||
// check last sum line, if last line encountered, then we are done
|
||
if ($cols['datum'] == self::LAST_LINE_DATE_VALUE)
|
||
... | ... | |
}
|
||
// add data row
|
||
$sum += $cols['castka'];
|
||
$result->items[] = $cols;
|
||
}
|
||
throw new Exception('CSV soubor není kompletní.');
|
||
}
|
||
$result->items[] = $cols;
|
||
}
|
||
throw new Exception('CSV soubor není kompletní.');
|
||
}
|
||
|
||
/**
|
||
* Parse given line of statement header.
|
||
... | ... | |
return self::parseAmount($amount);
|
||
}
|
||
|
||
/**
|
||
* Checks headers that start data part of statement.
|
||
*
|
||
* @param integer $header_line header line
|
||
* @throws Exception
|
||
*/
|
||
private function checkHeaders($header_line)
|
||
{
|
||
/**
|
||
* Checks headers that start data part of statement.
|
||
*
|
||
* @param integer $header_line header line
|
||
* @throws Exception
|
||
*/
|
||
private function checkHeaders($header_line)
|
||
{
|
||
// reconstruct valid header
|
||
$expected = implode(self::CSV_COL_DELIM, self::$fields)
|
||
$expected = implode(self::CSV_COL_DELIM, self::$fields)
|
||
. self::CSV_COL_DELIM;
|
||
// compare with passed
|
||
if ($header_line != $expected)
|
||
if ($header_line != $expected)
|
||
{
|
||
throw new Exception(__("Nelze parsovat hlavičku Fio výpisu. "
|
||
. "Ujistěte se, že jste zvolili všech " . count(self::$fields)
|
||
... | ... | |
}
|
||
}
|
||
|
||
/**
|
||
* Normalize string amount to double value.
|
||
*
|
||
/**
|
||
* Normalize string amount to double value.
|
||
*
|
||
* @example " 1000 278,40 " -> "1000278.40"
|
||
* @param string $amount
|
||
* @return double
|
||
* @throws InvalidArgumentException on invalid passed amount
|
||
*/
|
||
public static function parseAmount($amount)
|
||
{
|
||
* @param string $amount
|
||
* @return double
|
||
* @throws InvalidArgumentException on invalid passed amount
|
||
*/
|
||
public static function parseAmount($amount)
|
||
{
|
||
$norm_amount = str_replace(array(' ', ','), array('', '.'), $amount);
|
||
if (!is_numeric($norm_amount))
|
||
{
|
||
throw new InvalidArgumentException('Chybný formát částky převodu.');
|
||
}
|
||
return doubleval($norm_amount);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Parse date from format DD.MM.YYYY into YYYY-MM-DD.
|
||
... | ... | |
return date("Y-m-d", $timestamp);
|
||
}
|
||
|
||
/**
|
||
* Parse line of dump
|
||
*
|
||
* @param string $line
|
||
* @param array $keys
|
||
* @return array
|
||
* @throws Exception
|
||
*/
|
||
private function parseLine($line, $keys)
|
||
{
|
||
$cols = explode(self::CSV_COL_DELIM, $line);
|
||
/**
|
||
* Parse line of dump
|
||
*
|
||
* @param string $line
|
||
* @param array $keys
|
||
* @return array
|
||
* @throws Exception
|
||
*/
|
||
private function parseLine($line, $keys)
|
||
{
|
||
$cols = explode(self::CSV_COL_DELIM, $line);
|
||
|
||
if (count($cols) != count(self::$fields) + 1)
|
||
if (count($cols) != count(self::$fields) + 1)
|
||
{
|
||
throw new Exception('Chybný počet políček v položce výpisu.');
|
||
}
|
||
|
||
array_pop($cols);
|
||
|
||
// Convert to associative array
|
||
$assoc_cols = array_combine($keys, $cols);
|
||
// Convert to associative array
|
||
$assoc_cols = array_combine($keys, $cols);
|
||
|
||
// Convert date
|
||
if ($assoc_cols['datum'] != self::LAST_LINE_DATE_VALUE)
|
||
... | ... | |
$assoc_cols['datum'] = self::parseDate($assoc_cols['datum']);
|
||
}
|
||
|
||
// Amount has to be converted
|
||
$assoc_cols['castka'] = self::parseAmount($assoc_cols['castka']);
|
||
// Amount has to be converted
|
||
$assoc_cols['castka'] = self::parseAmount($assoc_cols['castka']);
|
||
|
||
// Trim leading zeros from VS
|
||
$assoc_cols['vs'] = ltrim($assoc_cols['vs'], '0');
|
||
// Trim leading zeros from VS
|
||
$assoc_cols['vs'] = ltrim($assoc_cols['vs'], '0');
|
||
|
||
return $assoc_cols;
|
||
}
|
||
return $assoc_cols;
|
||
}
|
||
|
||
/**
|
||
* Checks parsed money amount agains data from integrity line.
|
||
*
|
||
* @param array $integrity_cols
|
||
* @param integer $calculated_sum total counted sum
|
||
* @throws Exception on integrity error
|
||
*/
|
||
private function checkIntegrity($integrity_cols, $calculated_sum)
|
||
{
|
||
$amount = $integrity_cols['castka'];
|
||
/**
|
||
* Checks parsed money amount agains data from integrity line.
|
||
*
|
||
* @param array $integrity_cols
|
||
* @param integer $calculated_sum total counted sum
|
||
* @throws Exception on integrity error
|
||
*/
|
||
private function checkIntegrity($integrity_cols, $calculated_sum)
|
||
{
|
||
$amount = $integrity_cols['castka'];
|
||
|
||
if (abs($calculated_sum - $amount) > 0.0001)
|
||
if (abs($calculated_sum - $amount) > 0.0001)
|
||
{
|
||
throw new Exception("Chybný kontrolní součet částky "
|
||
. "('$calculated_sum' != '$amount').");
|
||
... | ... | |
class FioCsvStatement
|
||
{
|
||
/**
|
||
* Account number
|
||
*
|
||
* @var integer
|
||
*/
|
||
public $account_nr = 0;
|
||
* Account number
|
||
*
|
||
* @var integer
|
||
*/
|
||
public $account_nr = 0;
|
||
|
||
/**
|
||
* Bank number
|
||
*
|
||
* @var integer
|
||
*/
|
||
public $bank_nr;
|
||
/**
|
||
* Bank number
|
||
*
|
||
* @var integer
|
||
*/
|
||
public $bank_nr;
|
||
|
||
/**
|
||
* Statement period from date in format YYYY-MM-DD
|
||
*
|
||
* @var string
|
||
*/
|
||
public $from;
|
||
/**
|
||
* Statement period from date in format YYYY-MM-DD
|
||
*
|
||
* @var string
|
||
*/
|
||
public $from;
|
||
|
||
/**
|
||
* Statement period to date in format YYYY-MM-DD
|
||
*
|
||
* @var string
|
||
*/
|
||
public $to;
|
||
/**
|
||
* Statement period to date in format YYYY-MM-DD
|
||
*
|
||
* @var string
|
||
*/
|
||
public $to;
|
||
|
||
/**
|
||
* Openning balance
|
||
*
|
||
* @var double
|
||
*/
|
||
public $opening_balance;
|
||
/**
|
||
* Openning balance
|
||
*
|
||
* @var double
|
||
*/
|
||
public $opening_balance;
|
||
|
||
/**
|
||
* Closing balance
|
||
*
|
||
* @var double
|
||
*/
|
||
public $closing_balance;
|
||
/**
|
||
* Closing balance
|
||
*
|
||
* @var double
|
||
*/
|
||
public $closing_balance;
|
||
|
||
/**
|
||
* Items of statement that contain array of associative array where
|
||
... | ... | |
public $items;
|
||
|
||
/**
|
||
* Returns account number as array
|
||
*
|
||
* @return associative array("account_nr"=>"XXXXXXX", "bank_nr" => "YYYY"
|
||
*/
|
||
public function getAccountNumberAsArray()
|
||
{
|
||
* Returns account number as array
|
||
*
|
||
* @return associative array("account_nr"=>"XXXXXXX", "bank_nr" => "YYYY"
|
||
*/
|
||
public function getAccountNumberAsArray()
|
||
{
|
||
return array
|
||
(
|
||
"account_nr" => $this->account_nr,
|
||
"bank_nr" => $this->bank_nr
|
||
);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Returns associative array containing important listing header information.
|
||
* Must be called after parsing.
|
||
*
|
||
* @author Jiri Svitak
|
||
* @return header information
|
||
*/
|
||
public function getListingHeader()
|
||
{
|
||
// account number
|
||
$header = $this->getAccountNumberAsArray();
|
||
// date from to
|
||
$header["from"] = $this->from;
|
||
$header["to"] = $this->to;
|
||
// opening and closing balance
|
||
$header["opening_balance"] = $this->opening_balance;
|
||
$header["closing_balance"] = $this->closing_balance;
|
||
/**
|
||
* Returns associative array containing important listing header information.
|
||
* Must be called after parsing.
|
||
*
|
||
* @author Jiri Svitak
|
||
* @return header information
|
||
*/
|
||
public function getListingHeader()
|
||
{
|
||
// account number
|
||
$header = $this->getAccountNumberAsArray();
|
||
// date from to
|
||
$header["from"] = $this->from;
|
||
$header["to"] = $this->to;
|
||
// opening and closing balance
|
||
$header["opening_balance"] = $this->opening_balance;
|
||
$header["closing_balance"] = $this->closing_balance;
|
||
|
||
return $header;
|
||
}
|
||
return $header;
|
||
}
|
||
|
||
}
|
Také k dispozici: Unified diff
Opravy:
- odstraneni tabulatoru