Projekt

Obecné

Profil

<?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.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/

require_once APPPATH."libraries/importers/Raiffeisenbank/RB_Importer.php";
require_once APPPATH."libraries/importers/Raiffeisenbank/Parser_Ebanka.php";
require_once APPPATH."libraries/importers/Fio/FioImport.php";
require_once APPPATH."libraries/importers/Fio/FioSaver.php";


/**
* Handles importing of all types of bank listings into the database.
*
* @author Tomas Dulik, Jiri Svitak
* @package Controller
*/
class Import_Controller extends Controller
{
// static constants of supported bank listing types
const HTML_RAIFFEISENBANK = 1;
const CSV_FIO = 2;

private static $types = array();

/**
* Contruct - check if upload dir is writable
*/
public function __construct()
{
parent::__construct();

// supported bank listings
self::$types = array();
self::$types[self::HTML_RAIFFEISENBANK] = 'HTML Raiffeisenbank';
self::$types[self::CSV_FIO] = 'CSV Fio';

if (!is_writable('upload'))
{
Controller::error(WRITABLE, __(
'Directory "upload" is not writable, change access rights.'
));
}

}

/**
* Automatic redirect
*/
public function index()
{
url::redirect(url::base());
}

/**
* Uploads bank files.
*
* @author Jiri Svitak
* @param integer $id
*/
public function upload_bank_file($id = null)
{
if (!isset($id))
Controller::warning(PARAMETER);

if (!$this->acl_check_new('Accounts_Controller', 'bank_transfers'))
Controller::error(ACCESS);

$bank_acc_model = new Bank_account_Model($id);

if ($bank_acc_model->id == 0)
Controller::error(RECORD);

// form
$form = new Forge(
url_lang::base() . "import/upload_bank_file/$id", '',
'POST', array('id' => 'article_form')
);

$form->set_attr('class', 'form_class')
->set_attr('method', 'post');

$form->group('')
->label(__('File type'));

$form->dropdown('type')
->label(__('File type') . ':')
->options(self::$types)
->rules('required');

$form->upload('listing', TRUE)
->label(__('File with bank transfer listing') . ':')
->rules('required');
$form->submit(__('Submit'));

special::required_forge_style($form, ' *', 'required');

// validation
if ($form->validate())
{
$form_data = $form->as_array();
foreach ($form_data as $key => $value)
{
$form_data[$key] = htmlspecialchars($value);
}

switch ($form_data['type'])
{
case self::HTML_RAIFFEISENBANK:
$this->parse_ebank_account($id, $form->listing->value);
break;
case self::CSV_FIO:
$this->import_fio($id, $form->listing->value);
break;
default:
break;
}
}
else
{
// breadcrubs
$breadcrumbs = breadcrumbs::add()
->link('members/show/1', 'Profile of association',
$this->acl_check_view('Members_Controller', 'members'))
->link('bank_accounts/show_all', 'Bank accounts',
$this->acl_check_view('Accounts_Controller', 'bank_accounts'))
->text($bank_acc_model->name)
->text('Upload bank transfers listing')
->html();
// view
$title = __('Upload bank transfers listing');
$view = new View('main');
$view->title = $title;
$view->breadcrumbs = $breadcrumbs;
$view->content = new View('form');
$view->content->form = $form->html();
$view->content->headline = $title;
$view->render(TRUE);
}
}

/**
* Imports fio bank listing items from specified file.
*
* @author Jiri Svitak
* @param string $file_url
*/
private function import_fio($bank_account_id, $file_url)
{
try
{
$db = new Transfer_Model();
$db->transaction_start();

$ba = new Bank_account_Model($bank_account_id);
// parse bank listing items
$data = FioImport::getDataFromFile($file_url);
// check validity of bank listing
$header = FioImport::getListingHeader();
$ba = new Bank_account_Model($bank_account_id);
if ($ba->account_nr != $header["account_nr"] &&
$ba->bank_nr != $header["bank_nr"])
{
$ba_nr = $ba->account_nr."/".$ba->bank_nr;
$listing_ba_nr = $header["account_nr"]."/".$header["bank_nr"];
throw new FioException(__(
"Bank account number in listing (%s) header does not match " .
"bank account %s in database!", array($listing_ba_nr, $ba_nr)
));
}

// save bank statement
$statement = new Bank_statement_Model();
$statement->set_logger(FALSE);
$statement->bank_account_id = $bank_account_id;
$statement->user_id = $this->session->get('user_id');
$statement->type = self::$types[self::CSV_FIO];
$statement->from = $header["from"];
$statement->to = $header["to"];
$statement->opening_balance = $header["opening_balance"];
$statement->closing_balance = $header['closing_balance'];
$statement->save_throwable();

// save bank listing items
$stats = FioSaver::save(
$data, $bank_account_id, $statement->id,
$this->session->get('user_id')
);

// clean temporary whitelist, members should have payed, now
// they are no longer protected from redirection messages by whitelisting
$ip_model = new Ip_address_Model();
$ip_model->clean_temporary_whitelist();
$users_contacts_model = new Users_contacts_Model();
$users_contacts_model->clean_temporary_whitelist();

$db->transaction_commit();
}
catch (FioException $e)
{
$db->transaction_rollback();
status::error(__('Import has failed.') . ' ' .
$e->getMessage(),
FALSE
);
url::redirect(url_lang::base() . 'bank_accounts/show_all');
}
catch (Duplicity_Exception $e)
{
$db->transaction_rollback();
status::error(
__('Import has failed.') . ' ' .
__('Bank statement contains items that were already imported.') . ' ' .
$e->getMessage(),
FALSE
);
url::redirect(url_lang::base() . 'bank_accounts/show_all');
}
catch (Exception $e)
{
$db->transaction_rollback();
status::error(
__('Import has failed') . '.<br>' . $e->getMessage(),
FALSE
);
url::redirect(url_lang::base() . 'bank_accounts/show_all');
}

url::redirect(url_lang::base().'bank_transfers/show_by_bank_statement/'.$statement->id);
}

/**
* Parse ebank account
*
* @author Jiri Svitak
* @param integer $account_id ID of the account whose data will be parsed.
* @param string $url URL containing the file to parse
*/
private function parse_ebank_account($bank_account_id, $url)
{
try
{
$db = new Transfer_Model();
$db->transaction_start();

$parser = new Parser_Ebanka();
if (isset($bank_account_id))
{
$parsed_bank_acc = new Bank_account_Model($bank_account_id);
RB_Importer::$parsed_bank_acc = $parsed_bank_acc;
}

$statement = new Bank_statement_Model();
$statement->set_logger(FALSE);
$statement->bank_account_id = $bank_account_id;
$statement->user_id = $this->session->get('user_id');
$statement->type = self::$types[self::HTML_RAIFFEISENBANK];
$statement->save_throwable();

RB_Importer::$bank_statement_id = $statement->id;
RB_Importer::$user_id = $this->session->get('user_id');
RB_IMporter::$time_now = date("Y-m-d H:i:s");

// safe import is done by transaction processing started in method which called this one
$parser->parse($url);

// save statement's from and to
$statement->from = $parser->from;
$statement->to = $parser->to;
$statement->save_throwable();

// clean temporary whitelist, members should have payed, now
// they are no longer protected from redirection messages by whitelisting
$ip_model = new Ip_address_Model();
$ip_model->clean_temporary_whitelist();
$users_contacts_model = new Users_contacts_Model();
$users_contacts_model->clean_temporary_whitelist();

$db->transaction_commit();
}
catch (RB_Exception $e)
{
$db->transaction_rollback();
status::error(__('Import has failed.') . ' ' .
$e->getMessage(),
FALSE
);
url::redirect(url_lang::base() . 'bank_accounts/show_all');
}
catch (Duplicity_Exception $e)
{
$db->transaction_rollback();
status::error(
__('Import has failed.') . ' ' .
__('Bank statement contains items that were already imported.') . ' ' .
$e->getMessage(),
FALSE
);
url::redirect(url_lang::base() . 'bank_accounts/show_all');
}
catch (Exception $e)
{
$db->transaction_rollback();
status::error(
__('Import has failed') . '.<br>' . $e->getMessage() . ' '.
$e->getMessage(),
FALSE
);
url::redirect(url_lang::base() . 'bank_accounts/show_all');
}

url::redirect(url_lang::base().'bank_transfers/show_by_bank_statement/'.$statement->id);

}

}
(27-27/75)