freenetis-github/application/libraries/importers/Csv_Fio_Bank_Statement_File_Importer.php @ 761ccd9a
31ca0a32 | Michal Kliment | <?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|||
* This file is part of open source system FreenetIS
|
|||
* and it is released under GPLv3 licence.
|
|||
a29b0d63 | Michal Kliment | *
|
|
31ca0a32 | Michal Kliment | * More info about licence can be found:
|
|
* http://www.gnu.org/licenses/gpl-3.0.html
|
|||
a29b0d63 | Michal Kliment | *
|
|
31ca0a32 | Michal Kliment | * More info about project can be found:
|
|
* http://www.freenetis.org/
|
|||
a29b0d63 | Michal Kliment | *
|
|
31ca0a32 | Michal Kliment | */
|
|
require dirname(__FILE__) . '/Fio_Bank_Statement_File_Importer.php';
|
|||
require dirname(__FILE__) . '/Fio/FioParser.php';
|
|||
a29b0d63 | Michal Kliment | require dirname(__FILE__) . '/Fio/NewFioCsvParser.php';
|
|
31ca0a32 | Michal Kliment | ||
/**
|
|||
* FIO importer for statements in CSV format that are obtained from the FIO
|
|||
* e-banking portal.
|
|||
a29b0d63 | Michal Kliment | *
|
|
31ca0a32 | Michal Kliment | * It uses old version of driver for parsing.
|
|
a29b0d63 | Michal Kliment | *
|
|
31ca0a32 | Michal Kliment | * @author Ondrej Fibich
|
|
* @since 1.1
|
|||
*/
|
|||
class Csv_Fio_Bank_Statement_File_Importer extends Fio_Bank_Statement_File_Importer
|
|||
{
|
|||
a29b0d63 | Michal Kliment | ||
31ca0a32 | Michal Kliment | /**
|
|
* Data reprezentation of import.
|
|||
*
|
|||
* @var array
|
|||
*/
|
|||
private $data = NULL;
|
|||
a29b0d63 | Michal Kliment | ||
/**
|
|||
* Indicates whether header is available or not.
|
|||
*
|
|||
* @var boolean
|
|||
*/
|
|||
private $header_available = TRUE;
|
|||
31ca0a32 | Michal Kliment | /*
|
|
* @Override
|
|||
*/
|
|||
protected function check_file_data_format()
|
|||
{
|
|||
// reset
|
|||
$this->data = NULL;
|
|||
a29b0d63 | Michal Kliment | $this->header_available = TRUE;
|
|
31ca0a32 | Michal Kliment | // parse (we have no function for checking)
|
|
try
|
|||
{
|
|||
a29b0d63 | Michal Kliment | $parser_new = new NewFioCsvParser;
|
|
if ($parser_new->accept_file($this->get_file_data()))
|
|||
{
|
|||
$this->header_available = FALSE;
|
|||
$this->data = $parser_new->parse($this->get_file_data());
|
|||
// correct data
|
|||
foreach ($this->data as &$row)
|
|||
{
|
|||
$this->correct_new_listing_row($row);
|
|||
}
|
|||
}
|
|||
// old parser
|
|||
else
|
|||
{
|
|||
$this->set_file_data(iconv('cp1250', 'UTF-8', $this->get_file_data()));
|
|||
$this->data = FioParser::parseCSV($this->get_file_data());
|
|||
// correct data
|
|||
foreach ($this->data as &$row)
|
|||
{
|
|||
$this->correct_old_listing_row($row);
|
|||
}
|
|||
}
|
|||
31ca0a32 | Michal Kliment | // ok
|
|
return TRUE;
|
|||
}
|
|||
catch (Exception $e)
|
|||
{
|
|||
$this->data = NULL;
|
|||
$this->add_exception_error($e, FALSE);
|
|||
return FALSE;
|
|||
}
|
|||
}
|
|||
a29b0d63 | Michal Kliment | /**
|
|
* Correct passed listing row from old parser for application needs.
|
|||
*
|
|||
* @param array $row listing row passed by reference
|
|||
* @throws Exception on error in data
|
|||
*/
|
|||
private function correct_new_listing_row(&$row)
|
|||
{
|
|||
if ($row['mena'] != 'CZK')
|
|||
{
|
|||
throw new Exception(__('Unknown currency %s!', $row['mena']));
|
|||
}
|
|||
// only transfer from Fio to Fio have 'nazev_protiuctu'
|
|||
// for accounts in other banks we have to derive account name
|
|||
if (!$row['nazev_protiuctu'] && $row['identifikace'])
|
|||
{
|
|||
$row['nazev_protiuctu'] = $row['identifikace'];
|
|||
}
|
|||
}
|
|||
/**
|
|||
* Correct passed listing row from old parser for application needs.
|
|||
*
|
|||
* @param array $row listing row passed by reference
|
|||
* @throws Exception on error in data
|
|||
*/
|
|||
private function correct_old_listing_row(&$row)
|
|||
{
|
|||
if ($row['mena'] != 'CZK')
|
|||
{
|
|||
throw new Exception(__('Unknown currency %s!', $row['mena']));
|
|||
}
|
|||
// convert date
|
|||
if (preg_match('/^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}\.$/', $row['datum']) !== FALSE)
|
|||
{
|
|||
$date_arr = explode('.', $row['datum']);
|
|||
$timestamp = mktime(0, 0, 0, $date_arr[1], $date_arr[0], $date_arr[2]);
|
|||
$row['datum'] = date('Y-m-d', $timestamp);
|
|||
}
|
|||
// only transfer from Fio to Fio have 'nazev_protiuctu'
|
|||
// for accounts in other banks we have to derive account name
|
|||
if (!$row['nazev_protiuctu'] && $row['identifikace'])
|
|||
{
|
|||
$row['nazev_protiuctu'] = $row['identifikace'];
|
|||
}
|
|||
// convert from cents
|
|||
$row['castka'] /= 100;
|
|||
}
|
|||
31ca0a32 | Michal Kliment | /*
|
|
* @Override
|
|||
*/
|
|||
protected function get_header_data()
|
|||
{
|
|||
if (empty($this->data))
|
|||
throw new InvalidArgumentException('Check CSV first');
|
|||
a29b0d63 | Michal Kliment | ||
if (!$this->header_available)
|
|||
{
|
|||
return NULL;
|
|||
}
|
|||
31ca0a32 | Michal Kliment | $fio_ph = FioParser::getListingHeader();
|
|
a29b0d63 | Michal Kliment | ||
31ca0a32 | Michal Kliment | $hd = new Header_Data($fio_ph['account_nr'], $fio_ph['bank_nr']);
|
|
a29b0d63 | Michal Kliment | ||
31ca0a32 | Michal Kliment | $hd->currency = 'CZK';
|
|
$hd->openingBalance = $fio_ph['opening_balance'];
|
|||
$hd->closingBalance = $fio_ph['closing_balance'];
|
|||
$hd->dateStart = $fio_ph['from'];
|
|||
$hd->dateEnd = $fio_ph['to'];
|
|||
a29b0d63 | Michal Kliment | ||
31ca0a32 | Michal Kliment | return $hd;
|
|
}
|
|||
/*
|
|||
* @Override
|
|||
*/
|
|||
protected function parse_file_data()
|
|||
{
|
|||
// already parsed
|
|||
return !empty($this->data);
|
|||
}
|
|||
/*
|
|||
* @Override
|
|||
*/
|
|||
protected function get_parsed_transactions()
|
|||
{
|
|||
return $this->data;
|
|||
}
|
|||
a29b0d63 | Michal Kliment | ||
31ca0a32 | Michal Kliment | }
|