Projekt

Obecné

Profil

Stáhnout (17.7 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 release 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/
*
*/

/**
* Handles bank accounts of members.
*
* @package Controller
*/
class Bank_accounts_Controller extends Controller
{
c1bdc1c4 Michal Kliment
/**
* Constructor, only test if finance is enabled
*/
public function __construct()
{
parent::__construct();
if (!Settings::get('finance_enabled'))
Controller::error (ACCESS);
}
8baed187 Michal Kliment
/**
* Index redirects to show all
*/
public function index()
{
url::redirect('bank_accounts/show_all');
}
/**
* It shows bank accounts of association.
*
* @author Jiri Svitak
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
*/
public function show_all(
$limit_results = 500, $order_by = 'id', $order_by_direction = 'ASC',
$page_word = null, $page = 1)
{
// access
if (!$this->acl_check_view('Accounts_Controller', 'bank_accounts'))
{
Controller::error(ACCESS);
}
// it gets only bank accounts of association
$bank_account_model = new Bank_account_Model();
$baa = $bank_account_model->get_assoc_bank_accounts();
$baa_headline = __('Bank accounts of association').'&nbsp;'
. help::hint('bank_accounts_of_association');
$baa_grid = new Grid('bank_accounts', null, array
(
'use_paginator' => false,
'use_selector' => false
));
// adding bank account
if ($this->acl_check_new('Accounts_Controller', 'bank_accounts'))
{
$baa_grid->add_new_button(
c1bdc1c4 Michal Kliment
'bank_accounts/add/1', 'Add new bank account of association'
8baed187 Michal Kliment
);
}
$baa_grid->field('id')
c1bdc1c4 Michal Kliment
->label('ID');
8baed187 Michal Kliment
$baa_grid->field('baname')
c1bdc1c4 Michal Kliment
->label('Account name');
$baa_grid->callback_field('type')
->callback('callback::bank_account_type');
8baed187 Michal Kliment
$baa_grid->field('account_number');
$baa_grid->field('mname')
c1bdc1c4 Michal Kliment
->label('Member name');
8baed187 Michal Kliment
$actions = $baa_grid->grouped_action_field();
if ($this->acl_check_view('Accounts_Controller', 'bank_transfers'))
{
$actions->add_action('id')
->icon_action('transfer')
->url('bank_transfers/show_by_bank_account')
->label('Show transfers');
}
if ($this->acl_check_view('Accounts_Controller', 'bank_statements'))
{
$actions->add_action('id')
->icon_action('dumps')
->url('bank_statements/show_by_bank_account')
->label('Show statements');
}
if ($this->acl_check_new('Accounts_Controller', 'bank_transfers'))
{
c1bdc1c4 Michal Kliment
$actions->add_conditional_action('id')
->condition('is_import_of_statement_available')
8baed187 Michal Kliment
->icon_action('import')
->url('import/upload_bank_file')
->label('Import');
}
c1bdc1c4 Michal Kliment
if ($this->acl_check_view('Accounts_Controller', 'bank_account_auto_down_config'))
{
$actions->add_conditional_action('id')
->condition('is_automatical_down_of_statement_available')
->icon_action('settings_auto')
->url('bank_accounts_auto_down_settings/show')
->label('Setup automatical downloading of statements');
}
if ($this->acl_check_edit('Accounts_Controller', 'bank_accounts'))
{
$actions->add_conditional_action('id')
->icon_action('edit')
->url('bank_accounts/edit');
}
8baed187 Michal Kliment
$baa_grid->datasource($baa);

// bank accounts except association's
if ($this->acl_check_view('Accounts_Controller', 'bank_accounts'))
{
// get new selector
c1bdc1c4 Michal Kliment
if (is_numeric($this->input->post('record_per_page')))
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$limit_results = (int) $this->input->post('record_per_page');
8baed187 Michal Kliment
}
// parameters control
$allowed_order_type = array('id', 'baname', 'account_number', 'mname');
if (!in_array(strtolower($order_by),$allowed_order_type))
{
$order_by = 'id';
}
if (strtolower($order_by_direction) != 'desc')
{
$order_by_direction = 'asc';
}
c1bdc1c4 Michal Kliment
// Create filter form
$filter_form = new Filter_form();

7dafd607 Michal Kliment
$filter_form->add('baname')
c1bdc1c4 Michal Kliment
->label('Account name')
->callback('json/bank_account_name');

$filter_form->add('account_nr')
->label('Account number');

$filter_form->add('bank_nr')
7dafd607 Michal Kliment
->label('Bank code')
->type('number');
c1bdc1c4 Michal Kliment
7dafd607 Michal Kliment
$filter_form->add('member_name')
->label('Owner')
->callback('json/member_name');

$filter_form->add('member_id')
->label('Owner ID')
->type('number');

// bank accounts
c1bdc1c4 Michal Kliment
$total_baccounts = $bank_account_model->count_bank_accounts($filter_form->as_sql());
8baed187 Michal Kliment
if (($sql_offset = ($page - 1) * $limit_results) > $total_baccounts)
$sql_offset = 0;
$ba = $bank_account_model->get_bank_accounts(
$sql_offset, (int)$limit_results, $order_by,
c1bdc1c4 Michal Kliment
$order_by_direction, $filter_form->as_sql()
8baed187 Michal Kliment
);
$title = __('Bank accounts').'&nbsp;'.help::hint('bank_accounts');
$grid = new Grid('bank_accounts', $title, array
(
'separator' => '<br /><br />',
'current' => $limit_results,
'selector_increace' => 500,
'selector_min' => 500,
'selector_max_multiplier' => 10,
'base_url' => Config::get('lang').'/bank_accounts/show_all/'
. $limit_results.'/'.$order_by.'/'.$order_by_direction,
'uri_segment' => 'page',
'total_items' => $total_baccounts,
'items_per_page' => $limit_results,
'style' => 'classic',
'order_by' => $order_by,
'order_by_direction' => $order_by_direction,
'limit_results' => $limit_results,
c1bdc1c4 Michal Kliment
'filter' => $filter_form,
8baed187 Michal Kliment
));
// adding bank account
if ($this->acl_check_new('Accounts_Controller', 'bank_accounts'))
{
c1bdc1c4 Michal Kliment
$grid->add_new_button('bank_accounts/add', 'Add new bank account');
8baed187 Michal Kliment
}
$grid->order_field('id')
->label('ID');
$grid->order_field('baname')
c1bdc1c4 Michal Kliment
->label('Account name');
8baed187 Michal Kliment
$grid->order_field('account_nr')
c1bdc1c4 Michal Kliment
->label('Account number');
8baed187 Michal Kliment
$grid->order_field('bank_nr')
c1bdc1c4 Michal Kliment
->label('Bank code');
8baed187 Michal Kliment
$grid->order_link_field('member_id')
->link('members/show', 'member_name')
c1bdc1c4 Michal Kliment
->label('Member name');
8baed187 Michal Kliment
$actions = $grid->grouped_action_field();
if ($this->acl_check_view('Accounts_Controller', 'bank_transfers'))
{
$actions->add_action('id')
->icon_action('transfer')
->url('bank_transfers/show_by_bank_account')
->label('Show transfers');
}
2a205303 Michal Kliment
if ($this->acl_check_edit('Accounts_Controller', 'bank_accounts'))
{
$actions->add_conditional_action('id')
->icon_action('edit')
->url('bank_accounts/edit')
->class('popup_link');
}
8baed187 Michal Kliment
$grid->datasource($ba);
}
else
{
$grid = '';
}
// breadcrubs
$breadcrumbs = breadcrumbs::add()
->link('members/show/1', 'Profile of association',
$this->acl_check_view('Members_Controller', 'members'))
->text('Bank accounts')
->html();
// view
$view = new View('main');
$view->title = __('Bank accounts');
$view->breadcrumbs = $breadcrumbs;
$view->content = new View('bank_accounts/show_all');
$view->content->baa_headline = $baa_headline;
$view->content->baa_grid = $baa_grid;
$view->content->grid = $grid;
$view->render(TRUE);
}
/**
* Function adds bank account.
* If member id 1 is specified, then it is new bank account of association.
*/
c1bdc1c4 Michal Kliment
public function add($member_id = NULL)
8baed187 Michal Kliment
{
// access
if (!$this->acl_check_new('Accounts_Controller', 'bank_accounts'))
{
Controller::error(ACCESS);
}
// form
c1bdc1c4 Michal Kliment
if (!isset($member_id) || $member_id != Member_Model::ASSOCIATION)
8baed187 Michal Kliment
{
// members list
$arr_members = ORM::factory('member')->select_list();
if (isset($arr_members[1]))
{
unset($arr_members[1]);
}
$form = new Forge('bank_accounts/add/');
$form->dropdown('member_id')
->label('Member name')
->options($arr_members)
->selected($this->session->get('member_id'))
->rules('required')
->style('width:200px');
}
else
{
c1bdc1c4 Michal Kliment
$form = new Forge('bank_accounts/add/' . $member_id);
$form->dropdown('type')
->options(Bank_account_Model::get_type_names())
->style('width:200px');
8baed187 Michal Kliment
}
$form->input('account_name')
->rules('required|length[3,50]');
$form->input('account_nr')
->label('Account number')
->rules('required|length[3,50]|valid_numeric');
$form->input('bank_nr')
->label('Bank code')
->rules('required|length[3,10]|valid_numeric');
$form->input('IBAN');
$form->input('SWIFT');
// submit button
$form->submit('Add');
// validation
if ($form->validate())
{
$form_data = $form->as_array();
// determining owner's id
if (!isset($member_id) || $member_id != 1)
{
$member_id = $form_data["member_id"];
}
c1bdc1c4 Michal Kliment
// determinig type
if ($member_id == Member_Model::ASSOCIATION)
{
$type = $form_data["type"];
}
else
{
$type = Bank_account_Model::TYPE_OTHER;
}
8baed187 Michal Kliment
// real bank account
$bank_account = new Bank_account_Model();
c1bdc1c4 Michal Kliment
$bank_account->name = $form_data['account_name'];
$bank_account->member_id = $member_id;
$bank_account->type = $type;
$bank_account->account_nr = $form_data['account_nr'];
$bank_account->bank_nr = $form_data['bank_nr'];
$bank_account->IBAN = $form_data['IBAN'];
$bank_account->SWIFT = $form_data['SWIFT'];
8baed187 Michal Kliment
$bank_account->save();
// only member 1 - association itself - has related double-entry accounts to added bank account
c1bdc1c4 Michal Kliment
if ($member_id == Member_Model::ASSOCIATION)
8baed187 Michal Kliment
{
// these three double-entry accounts are related to one bank account through relation table
// double-entry bank account
$doubleentry_bank_account = new Account_Model();
$doubleentry_bank_account->member_id = $member_id;
c1bdc1c4 Michal Kliment
$doubleentry_bank_account->name = $form_data['account_name'];
8baed187 Michal Kliment
$doubleentry_bank_account->account_attribute_id = Account_attribute_Model::BANK;
$doubleentry_bank_account->comment = __('Bank accounts');
$doubleentry_bank_account->add($bank_account);
$doubleentry_bank_account->save();
// double-entry account of bank fees
$bank_fees_account = new Account_Model();
$bank_fees_account->member_id = $member_id;
c1bdc1c4 Michal Kliment
$bank_fees_account->name = $form_data['account_name'].' - '.__('Bank fees');
8baed187 Michal Kliment
$bank_fees_account->account_attribute_id = Account_attribute_Model::BANK_FEES;
$bank_fees_account->comment = __('Bank fees');
$bank_fees_account->add($bank_account);
$bank_fees_account->save();
// double-entry account of bank interests
$bank_interests_account = new Account_Model();
$bank_interests_account->member_id = $member_id;
c1bdc1c4 Michal Kliment
$bank_interests_account->name = $form_data['account_name'].' - '.__('Bank interests');
8baed187 Michal Kliment
$bank_interests_account->account_attribute_id = Account_attribute_Model::BANK_INTERESTS;
$bank_interests_account->comment = __('Bank interests');
$bank_interests_account->add($bank_account);
$bank_interests_account->save();
}
// redirection
url::redirect('bank_accounts/show_all');
}
if ($member_id == 1)
$headline = __('Add new bank account of association');
else
$headline = __('Add new bank account');
// 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')
->disable_translation()
->text($headline)
->html();
// view
$view = new View('main');
$view->title = $headline;
$view->breadcrumbs = $breadcrumbs;
$view->content = new View('form');
$view->content->headline = $headline;
$view->content->form = $form->html();
c1bdc1c4 Michal Kliment
$view->render(TRUE);
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
/**
* Enables to edit bank account of association.
*
* @param integer $bank_account_id
8baed187 Michal Kliment
*/
c1bdc1c4 Michal Kliment
public function edit($bank_account_id = NULL)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
// param
if (!intval($bank_account_id))
{
self::warning(PARAMETER);
}
// access
if (!$this->acl_check_edit('Accounts_Controller', 'bank_accounts'))
{
self::error(ACCESS);
}
$bank_account = new Bank_account_Model($bank_account_id);
// exists?
2a205303 Michal Kliment
if (!$bank_account || !$bank_account->id)
c1bdc1c4 Michal Kliment
{
self::error(RECORD);
}
2a205303 Michal Kliment
// different logic for bank account of association
if ($bank_account->member_id == Member_Model::ASSOCIATION)
{
$this->edit_association($bank_account);
}
else
{
$this->edit_non_association($bank_account);
}
}

/**
* Edit form for bank account of association.
*
* @param Bank_account_Model $bank_account
*/
private function edit_association(Bank_account_Model $bank_account)
{
c1bdc1c4 Michal Kliment
try
{
$ba_driver = Bank_Account_Settings::factory($bank_account->type);
$ba_driver->load_column_data($bank_account->settings);
}
catch (InvalidArgumentException $e)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$ba_driver = NULL;
}
// form
$form = new Forge();
$form->group('Basic information');
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
$form->dropdown('type')
->options(Bank_account_Model::get_type_names())
->selected($bank_account->type)
->style('width:200px');
$form->input('IBAN')
->value($bank_account->IBAN);
$form->input('SWIFT')
->value($bank_account->SWIFT);
// bank account settings
if ($ba_driver && count($ba_driver->get_column_fields()))
{
$form->group('Settings');
$columns = $ba_driver->get_column_fields();
foreach ($columns as $column => $info)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
switch ($info['type'])
{
case Bank_Account_Settings::FIELD_TYPE_BOOL:
$input = $form->checkbox($column)->checked($ba_driver->$column);
break;
139add4b jeffraska
case Bank_Account_Settings::FIELD_TYPE_DROPDOWN:
$input = $form->dropdown($column)->selected($ba_driver->$column)->options(array());
break;
c1bdc1c4 Michal Kliment
default:
$input = $form->input($column)->value($ba_driver->$column);
break;
}
if (isset($info['name']) && !empty($info['name']))
$input->label($info['name']);
if (isset($info['help']) && !empty($info['help']))
$input->help(help::hint($info['help']));
if (isset($info['rules']) && !empty($info['rules']))
$input->rules($info['rules']);
139add4b jeffraska
if (isset($info['options']) && !empty($info['options']))
$input->options($info['options']);
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
}
// submit button
$form->submit('Edit');
// validation
if ($form->validate())
{
$form_data = $form->as_array();
// real bank account
$bank_account->type = $form_data['type'];
$bank_account->IBAN = $form_data['IBAN'];
$bank_account->SWIFT = $form_data['SWIFT'];
if ($ba_driver && count($ba_driver->get_column_fields()))
{
unset($form_data['type']);
unset($form_data['IBAN']);
unset($form_data['SWIFT']);
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
foreach ($form_data as $key => $value)
{
$ba_driver->$key = $value;
}
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
$bank_account->settings = $ba_driver->get_column_data();
}
2bea9538 Michal Kliment
// empty settings (#877)
else
{
$bank_account->settings = NULL;
}
c1bdc1c4 Michal Kliment
$bank_account->save();
// redirection
url::redirect('bank_accounts/show_all');
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
$headline = __('Edit bank account');
// 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')
->disable_translation()
->text($bank_account->account_nr . '/' . $bank_account->bank_nr)
->text($headline)
->html();
// view
8baed187 Michal Kliment
$view = new View('main');
c1bdc1c4 Michal Kliment
$view->title = $headline;
$view->breadcrumbs = $breadcrumbs;
8baed187 Michal Kliment
$view->content = new View('form');
c1bdc1c4 Michal Kliment
$view->content->headline = $headline;
$view->content->form = $form->html();
$view->render(TRUE);
}
2a205303 Michal Kliment
/**
* Edit form for bank account that do not belongs to association.
*
* @param Bank_account_Model $bank_account
*/
private function edit_non_association(Bank_account_Model $bank_account)
{
$members = array
(
NULL => '--- ' . __('Without owner') . ' ---'
) + ORM::factory('member')->select_list_grouped(FALSE);
unset($members[Member_Model::ASSOCIATION]); // remove association

// form
$form = new Forge();

$form->group('Basic information');

$form->input('name')
->rules('required')
->value($bank_account->name)
->style('width:200px');

$form->dropdown('member_id')
->label('Owner')
->options($members)
->selected($bank_account->member_id)
->style('width:200px');

// submit button
$form->submit('Edit');

// validation
if ($form->validate())
{
$form_data = $form->as_array();

// real bank account
$bank_account->name = $form_data['name'];
$bank_account->member_id = $form_data['member_id'];

$bank_account->save();

// redirection
$this->redirect('bank_accounts/show_all');
}
else
{
$headline = __('Edit bank account');

// 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')
->disable_translation()
->text($bank_account->account_nr . '/' . $bank_account->bank_nr)
->text($headline)
->html();

// view
$view = new View('main');
$view->title = $headline;
$view->breadcrumbs = $breadcrumbs;
$view->content = new View('form');
$view->content->headline = $headline;
$view->content->form = $form->html();
$view->render(TRUE);
}
}
c1bdc1c4 Michal Kliment
8baed187 Michal Kliment
}