freenetis-github/application/libraries/importers/Csv_Fio_Bank_Statement_File_Importer.php @ 170ebe5e
18ac9009 | Ondřej Fibich | <?php
|
|
31ca0a32 | Michal Kliment | /*
|
|
* 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 | */
|
|
18ac9009 | Ondřej Fibich | require_once __DIR__ . '/Fio_Bank_Statement_File_Importer.php';
|
|
require_once __DIR__ . '/Fio/FioCsvStatement.php';
|
|||
require_once __DIR__ . '/Fio/FioCsvParser.php';
|
|||
require_once __DIR__ . '/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
|
|||
{
|
|||
18ac9009 | Ondřej Fibich | /**
|
|
* Required currency.
|
|||
*
|
|||
* @todo we want to handle more currency, maybe it will be good to have
|
|||
* currency of each association bank account set by settings
|
|||
*/
|
|||
const CURRENCY = 'CZK';
|
|||
a29b0d63 | Michal Kliment | ||
31ca0a32 | Michal Kliment | /**
|
|
* Data reprezentation of import.
|
|||
*
|
|||
18ac9009 | Ondřej Fibich | * @var FioCsvStatement
|
|
31ca0a32 | Michal Kliment | */
|
|
private $data = NULL;
|
|||
18ac9009 | Ondřej Fibich | ||
a29b0d63 | Michal Kliment | /**
|
|
* Indicates whether header is available or not.
|
|||
*
|
|||
* @var boolean
|
|||
*/
|
|||
private $header_available = TRUE;
|
|||
18ac9009 | Ondřej Fibich | /*
|
|
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
|
|||
{
|
|||
18ac9009 | Ondřej Fibich | $file_data = $this->get_file_data();
|
|
// new parser
|
|||
$parserNew = new NewFioCsvParser;
|
|||
if ($parserNew->accept_file($file_data))
|
|||
a29b0d63 | Michal Kliment | {
|
|
$this->header_available = FALSE;
|
|||
18ac9009 | Ondřej Fibich | $this->data = $parserNew->parse($file_data);
|
|
a29b0d63 | Michal Kliment | }
|
|
// old parser
|
|||
else
|
|||
{
|
|||
18ac9009 | Ondřej Fibich | $parserOld = new FioCsvParser;
|
|
$this->data = $parserOld->parse($file_data, 'cp1250');
|
|||
a29b0d63 | Michal Kliment | }
|
|
18ac9009 | Ondřej Fibich | // correct each data row
|
|
for ($i = 0; $i < count($this->data->items); $i++)
|
|||
{
|
|||
$this->correct_listing_row($this->data->items[$i]);
|
|||
}
|
|||
31ca0a32 | Michal Kliment | // ok
|
|
return TRUE;
|
|||
}
|
|||
catch (Exception $e)
|
|||
{
|
|||
$this->data = NULL;
|
|||
$this->add_exception_error($e, FALSE);
|
|||
return FALSE;
|
|||
}
|
|||
}
|
|||
a29b0d63 | Michal Kliment | /**
|
|
18ac9009 | Ondřej Fibich | * Correct passed listing row for application needs.
|
|
a29b0d63 | Michal Kliment | *
|
|
* @param array $row listing row passed by reference
|
|||
* @throws Exception on error in data
|
|||
*/
|
|||
18ac9009 | Ondřej Fibich | private function correct_listing_row(&$row)
|
|
a29b0d63 | Michal Kliment | {
|
|
18ac9009 | Ondřej Fibich | if ($row['mena'] != self::CURRENCY)
|
|
a29b0d63 | Michal Kliment | {
|
|
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'];
|
|||
}
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | /*
|
|
31ca0a32 | Michal Kliment | * @Override
|
|
*/
|
|||
protected function get_header_data()
|
|||
{
|
|||
a29b0d63 | Michal Kliment | if (!$this->header_available)
|
|
{
|
|||
return NULL;
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
if (empty($this->data))
|
|||
{
|
|||
throw new InvalidArgumentException('Check CSV first');
|
|||
}
|
|||
a29b0d63 | Michal Kliment | ||
18ac9009 | Ondřej Fibich | $hd = new Header_Data($this->data->account_nr, $this->data->bank_nr);
|
|
a29b0d63 | Michal Kliment | ||
18ac9009 | Ondřej Fibich | $hd->currency = self::CURRENCY;
|
|
$hd->openingBalance = $this->data->opening_balance;
|
|||
$hd->closingBalance = $this->data->closing_balance;
|
|||
$hd->dateStart = $this->data->from;
|
|||
$hd->dateEnd = $this->data->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()
|
|||
{
|
|||
18ac9009 | Ondřej Fibich | return $this->data->items;
|
|
31ca0a32 | Michal Kliment | }
|
|
a29b0d63 | Michal Kliment | ||
31ca0a32 | Michal Kliment | }
|