Projekt

Obecné

Profil

Stáhnout (14.8 KB) Statistiky
| Větev: | Tag: | Revize:
8baed187 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.
*
* 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/
*
*/

c1bdc1c4 Michal Kliment
require_once APPPATH."libraries/importers/Unicredit/UnicreditImport.php";
require_once APPPATH."libraries/importers/Unicredit/UnicreditSaver.php";
5f005dca Jakub Juračka
require_once APPPATH."libraries/importers/Raiffeisenbank/Parser_RB.php";
require_once APPPATH."libraries/importers/Raiffeisenbank/RB_Saver.php";
8baed187 Michal Kliment
/**
* Handles importing of all types of bank listings into the database.
*
* @author Tomas Dulik, Jiri Svitak
* @package Controller
*/
class Import_Controller extends Controller
{
c1bdc1c4 Michal Kliment
/**
* Types of import documents (supported bank listings)
*
* @var array
*/
private static $file_types = array
(
5f005dca Jakub Juračka
Bank_account_Model::TYPE_RAIFFEISENBANK => 'XML Raiffeisenbank',
c1bdc1c4 Michal Kliment
Bank_account_Model::TYPE_FIO => 'Fio CSV',
Bank_account_Model::TYPE_UNICREDIT => 'Unicredit CSV'
);
8baed187 Michal Kliment
/**
c1bdc1c4 Michal Kliment
* Contruct - check if finance is enabled and upload dir is writable
8baed187 Michal Kliment
*/
public function __construct()
{
parent::__construct();
c1bdc1c4 Michal Kliment
if (!Settings::get('finance_enabled'))
Controller::error(ACCESS);
8baed187 Michal Kliment
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
*/
c1bdc1c4 Michal Kliment
public function upload_bank_file($id = NULL)
8baed187 Michal Kliment
{
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);

c1bdc1c4 Michal Kliment
if (!$bank_acc_model->id)
Controller::error(RECORD);
try
{
$ba_drive = Bank_Account_Settings::factory($bank_acc_model->type);
$ba_drive->load_column_data($bank_acc_model->settings);
}
catch (InvalidArgumentException $e)
{
$ba_drive = NULL;
}
if (!$ba_drive || !$ba_drive->can_import_statements())
8baed187 Michal Kliment
Controller::error(RECORD);

// form
$form = new Forge('import/upload_bank_file/' . $id);
c1bdc1c4 Michal Kliment
$form->group(__('File') . ' - ' . self::$file_types[$bank_acc_model->type]);
8baed187 Michal Kliment
$form->upload('listing', TRUE)
->label('File with bank transfer listing')
->rules('required');
c1bdc1c4 Michal Kliment
if (module::e('notification'))
{
if (Settings::get('email_enabled') || Settings::get('sms_enabled'))
{
$form->group('Notifications');
}

if (Settings::get('email_enabled'))
{
$form->checkbox('send_email_notice')
->value('1')
->label('Send e-mail notice about received payment to member')
->checked('checked');
}

if (Settings::get('sms_enabled'))
{
$form->checkbox('send_sms_notice')
->value('1')
->label('Send SMS notice about received payment to member');
}

if (module::e('redirection') &&
$this->acl_check_edit('Messages_Controller', 'message'))
{
$form->group('Redirection');

$form->checkbox('reactivate_debtor_redir')
->value('1')
->label('Reactivate debtor redirection after importing');

$form->checkbox('reactivate_payment_notice_redir')
->value('1')
->label('Reactivate payment notice redirection after importing');
}
}
8baed187 Michal Kliment
$form->submit('Submit');

// validation
if ($form->validate())
{
$form_data = $form->as_array();
c1bdc1c4 Michal Kliment
switch ($bank_acc_model->type)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
case Bank_account_Model::TYPE_RAIFFEISENBANK:
5f005dca Jakub Juračka
$this->import_raiffeisenbank(
c1bdc1c4 Michal Kliment
$id, $form->listing->value,
Settings::get('email_enabled') &&
@$form_data['send_email_notice'] == 1,
Settings::get('sms_enabled') &&
(@$form_data['send_sms_notice'] == 1),
@$form_data['reactivate_debtor_redir'] == 1,
@$form_data['reactivate_payment_notice_redir'] == 1
);
break;
case Bank_account_Model::TYPE_FIO:
$this->import_fio(
$id, $form->listing->value,
Settings::get('email_enabled') &&
@$form_data['send_email_notice'] == 1,
Settings::get('sms_enabled') &&
(@$form_data['send_sms_notice'] == 1),
@$form_data['reactivate_debtor_redir'] == 1,
@$form_data['reactivate_payment_notice_redir'] == 1
);
8baed187 Michal Kliment
break;
c1bdc1c4 Michal Kliment
case Bank_account_Model::TYPE_UNICREDIT:
$this->import_unicredit(
$id, $form->listing->value,
Settings::get('email_enabled') &&
@$form_data['send_email_notice'] == 1,
Settings::get('sms_enabled') &&
(@$form_data['send_sms_notice'] == 1),
@$form_data['reactivate_debtor_redir'] == 1,
@$form_data['reactivate_payment_notice_redir'] == 1
);
8baed187 Michal Kliment
break;
default:
break;
}
}
c1bdc1c4 Michal Kliment
// 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);
8baed187 Michal Kliment
}

/**
* Imports fio bank listing items from specified file.
*
* @author Jiri Svitak
c1bdc1c4 Michal Kliment
* @param integer $back_account_id
8baed187 Michal Kliment
* @param string $file_url
c1bdc1c4 Michal Kliment
* @param boolean $send_emails Send emails as payment accept notification?
* @param boolean $send_sms Send SMSs as payment accept notification?
* @param boolean $debtor_redir_react Reactivate debtor redirection?
* @param boolean $payment_notice_redir_react Reactivate payment notice redirection?
8baed187 Michal Kliment
*/
c1bdc1c4 Michal Kliment
private function import_fio($bank_account_id, $file_url, $send_emails, $send_sms,
$debtor_redir_react, $payment_notice_redir_react)
8baed187 Michal Kliment
{
try
{
c1bdc1c4 Michal Kliment
// bank account
$bank_account = new Bank_account_Model($bank_account_id);
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
// import
$statement = Bank_Statement_File_Importer::import(
$bank_account, $file_url, 'csv', $send_emails, $send_sms
);
// redirection reactivation
if ($debtor_redir_react)
8baed187 Michal Kliment
{
9c8950bf Ondřej Fibich
if (is_numeric(Settings::get('big_debtor_boundary')))
{
$this->reactivate_redir(Message_Model::BIG_DEBTOR_MESSAGE);
}
c1bdc1c4 Michal Kliment
$this->reactivate_redir(Message_Model::DEBTOR_MESSAGE);
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
if ($payment_notice_redir_react)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$this->reactivate_redir(Message_Model::PAYMENT_NOTICE_MESSAGE);
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
url::redirect('bank_transfers/show_by_bank_statement/'.$statement->id);
8baed187 Michal Kliment
}
catch (Duplicity_Exception $e)
{
c1bdc1c4 Michal Kliment
$m = __('Import has failed.') . ' '
. __('Bank statement contains items that were already imported.');
status::error($m, $e, FALSE);
8baed187 Michal Kliment
}
catch (Exception $e)
{
Log::add_exception($e);
c1bdc1c4 Michal Kliment
status::error('Import has failed.', $e);
8baed187 Michal Kliment
}
}

5f005dca Jakub Juračka
/**
* Parse and imports Raiffeisen bank account statement
* This part of code was created for parsing RB XML account statemenets.
* This is only a modified code of function below
* "import_unicredit(@params)" which was created earlier.
8baed187 Michal Kliment
*
5f005dca Jakub Juračka
* @author Jakub Juračka
*
* @param integer $bank_account_id
c1bdc1c4 Michal Kliment
* @param string $file_url
* @param boolean $send_emails Send emails as payment accept notification?
* @param boolean $send_sms Send SMSs as payment accept notification?
* @param boolean $debtor_redir_react Reactivate debtor redirection?
* @param boolean $payment_notice_redir_react Reactivate payment notice redirection?
8baed187 Michal Kliment
*/
5f005dca Jakub Juračka
private function import_raiffeisenbank($bank_account_id, $file_url,
$send_emails, $send_sms, $debtor_redir_react,
$payment_notice_redir_react)
8baed187 Michal Kliment
{
try
{
$db = new Transfer_Model();
$db->transaction_start();
5f005dca Jakub Juračka
$parser = new Parser_RB;
// parse bank listing items
$parser->parse($file_url);
// get data
$data = $parser->get_data();
8baed187 Michal Kliment
5f005dca Jakub Juračka
// get header
$header = $parser->get_header();
// does match bank account in system with bank account of statement?
$ba = new Bank_account_Model($bank_account_id);
if ($ba->account_nr != $header['account_nr'] ||
$ba->bank_nr != $header['bank_nr'])
8baed187 Michal Kliment
{
5f005dca Jakub Juračka
$ba_nr = $ba->account_nr.'/'.$ba->bank_nr;
$listing_ba_nr = $header['account_nr'].'/'.$header['bank_nr'];
throw new RB_Exception(__(
'Bank account number in listing (%s) header does not match ' .
'bank account %s in database!', array($listing_ba_nr, $ba_nr)
));
8baed187 Michal Kliment
}

5f005dca Jakub Juračka
// save bank statement
8baed187 Michal Kliment
$statement = new Bank_statement_Model();
$statement->set_logger(FALSE);
$statement->bank_account_id = $bank_account_id;
5f005dca Jakub Juračka
$statement->user_id = $this->user_id;
c1bdc1c4 Michal Kliment
$statement->type = self::$file_types[Bank_account_Model::TYPE_RAIFFEISENBANK];
5f005dca Jakub Juračka
$statement->from = $header['from'];
$statement->to = $header['to'];
$statement->opening_balance = $header['bal_start'];
$statement->closing_balance = $header['bal_end'];
8baed187 Michal Kliment
$statement->save_throwable();

5f005dca Jakub Juračka
// save bank listing items
$stats = RB_Saver::save(
$data, $bank_account_id, $statement->id,
$this->user_id, $send_emails, $send_sms
);
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
$db->transaction_commit();
// redirection reactivation
if ($debtor_redir_react)
8baed187 Michal Kliment
{
9c8950bf Ondřej Fibich
if (is_numeric(Settings::get('big_debtor_boundary')))
{
$this->reactivate_redir(Message_Model::BIG_DEBTOR_MESSAGE);
}
c1bdc1c4 Michal Kliment
$this->reactivate_redir(Message_Model::DEBTOR_MESSAGE);
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
if ($payment_notice_redir_react)
{
$this->reactivate_redir(Message_Model::PAYMENT_NOTICE_MESSAGE);
}
url::redirect('bank_transfers/show_by_bank_statement/'.$statement->id);
8baed187 Michal Kliment
}
catch (RB_Exception $e)
{
$db->transaction_rollback();
5f005dca Jakub Juračka
status::error(__('Import has failed.') . ' ' . $e->getMessage(), NULL, FALSE);
8baed187 Michal Kliment
}
catch (Duplicity_Exception $e)
{
$db->transaction_rollback();
status::error(
__('Import has failed.') . ' ' .
__('Bank statement contains items that were already imported.') . ' ' .
c1bdc1c4 Michal Kliment
$e->getMessage(), NULL, FALSE
8baed187 Michal Kliment
);
}
catch (Exception $e)
{
$db->transaction_rollback();
Log::add_exception($e);
status::error(
5f005dca Jakub Juračka
__('Import has failed') . '.<br>' . $e->getMessage(), NULL, FALSE
8baed187 Michal Kliment
);
}
c1bdc1c4 Michal Kliment
5f005dca Jakub Juračka
}
c1bdc1c4 Michal Kliment
/**
* Imports fio bank listing items from specified file.
*
* @author Ondrej Fibich
* @param integer $back_account_id
* @param string $file_url
* @param boolean $send_emails Send emails as payment accept notification?
* @param boolean $send_sms Send SMSs as payment accept notification?
* @param boolean $debtor_redir_react Reactivate debtor redirection?
* @param boolean $payment_notice_redir_react Reactivate payment notice redirection?
*/
private function import_unicredit($bank_account_id, $file_url, $send_emails, $send_sms,
$debtor_redir_react, $payment_notice_redir_react)
{
try
{
$db = new Transfer_Model();
$db->transaction_start();
// parse bank listing items
$data = UnicreditImport::getDataFromFile($file_url);
// get header
$header = UnicreditImport::getListingHeader();
// does match bank account in system with bank account of statement?
$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 UnicreditException(__(
'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->user_id;
$statement->type = self::$file_types[Bank_account_Model::TYPE_UNICREDIT];
$statement->from = $header['from'];
$statement->to = $header['to'];
$statement->save_throwable();
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
// save bank listing items
$stats = UnicreditSaver::save(
$data, $bank_account_id, $statement->id,
$this->user_id, $send_emails, $send_sms
);
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
$db->transaction_commit();
// redirection reactivation
if ($debtor_redir_react)
{
9c8950bf Ondřej Fibich
if (is_numeric(Settings::get('big_debtor_boundary')))
{
$this->reactivate_redir(Message_Model::BIG_DEBTOR_MESSAGE);
}
c1bdc1c4 Michal Kliment
$this->reactivate_redir(Message_Model::DEBTOR_MESSAGE);
}
if ($payment_notice_redir_react)
{
$this->reactivate_redir(Message_Model::PAYMENT_NOTICE_MESSAGE);
}
url::redirect('bank_transfers/show_by_bank_statement/'.$statement->id);
}
catch (UnicreditException $e)
{
$db->transaction_rollback();
status::error(__('Import has failed.') . ' ' . $e->getMessage(), NULL, FALSE);
}
catch (Duplicity_Exception $e)
{
$db->transaction_rollback();
status::error(
__('Import has failed.') . ' ' .
__('Bank statement contains items that were already imported.') . ' ' .
$e->getMessage(), NULL, FALSE
);
}
catch (Exception $e)
{
$db->transaction_rollback();
Log::add_exception($e);
status::error(
__('Import has failed') . '.<br>' . $e->getMessage(), NULL, FALSE
);
}

}
/**
* Reactivates redirection of a message given by type.
*
* @author Ondřej Fibich
* @param integer $type Message type
* @throws InvalidArgumentException On wrong type
*/
private function reactivate_redir($type)
{
// member model
$member_model = new Member_Model();
$message_model = new Message_Model();
// find message
$message = new Message_Model($message_model->get_message_id_by_type($type));
if (!$message || !$message->id)
{
throw new InvalidArgumentException('Invalid type');
}
// get all members for messages
$members = $member_model->get_members_to_messages($type);
// activate notification
try
{
// notify
$stats = Notifications_Controller::notify(
$message, $members, $this->user_id,
NULL, TRUE, FALSE, FALSE, TRUE
);
// info messages
$info_messages = notification::build_stats_string(
$stats, TRUE, FALSE, FALSE, TRUE
);
// log action
$name = ORM::factory('user', $this->user_id)->get_full_name();
$m = __('Redirection "%s" has been reactivated during importing of bank statement by "%s"',
array(__($message->name), $name));
Log_queue_Model::info($m, implode("\n", $info_messages));
// show status to user
status::info($m . '<br>' . implode('<br>', $info_messages));
}
catch (Exception $e)
{
self::log_error($e->getMessage(), $e, FALSE);
$m = __('Redirection "%s" has not been reactivated', $message->name);
status::warning($m, $e, FALSE);
}
8baed187 Michal Kliment
}

}