Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1677

Přidáno uživatelem Ondřej Fibich před asi 12 roky(ů)

Branche pro 1.1 verzi. (viz. http://dev.freenetis.org/versions/2)

Zobrazit rozdíly:

freenetis/branches/1.1/application/controllers/transfers.php
<?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/
*
*/
/**
* Handles double-entry transfers and special actions
* with transfers like deducting fees etc.
*
* @author Jiri Svitak
* @package Controller
*/
class Transfers_Controller extends Controller
{
/**
* By default, it redirects user to day book - list of all double-entry transfers.
*
* @author Jiri Svitak
* @return unknown_type
*/
public function index()
{
url::redirect('transfers/show_all');
}
/**
* It shows all double-entry transfers. They are shown in day book.
*
* @author Jiri Svitak
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
*/
public function show_all(
$limit_results = 50, $order_by = 'datetime',
$order_by_direction = 'desc', $page_word = null, $page = 1)
{
if (!$this->acl_check_view('Accounts_Controller', 'transfers'))
Controller::error(ACCESS);
// get new selector
if (is_numeric($this->input->get('record_per_page')))
$limit_results = (int) $this->input->get('record_per_page');
// parameters control
$allowed_order_type = array
(
'id', 'oa_name', 'da_name', 'oa_attribute', 'da_attribute',
'datetime', 'text', 'amount'
);
if (!in_array(strtolower($order_by), $allowed_order_type))
$order_by = 'id';
if (strtolower($order_by_direction) != 'desc')
$order_by_direction = 'desc';
// there are two groups of transfers
$arr_groups[Transfer_Model::ALL_TRANSFERS] = __('All transfers');
$arr_groups[Transfer_Model::WITHOUT_INNER] = __('Without inner transfers');
// creates fields for filtering
$filter = new Table_Form(
url_lang::base() . "transfers/show_all", "get", array
(
new Table_Form_Item('text', 'oa_name', 'Origin account'),
new Table_Form_Item('text', 'datetime', 'Date and time'),
"tr",
new Table_Form_Item('text', 'da_name', 'Destination account'),
new Table_Form_Item('text', 'amount', 'Amount'),
"tr",
new Table_Form_Item('text', 'text', 'Text'),
new Table_Form_Item('select', 'group', 'Group', $arr_groups),
"tr",
"td", new Table_Form_Item('submit', 'submit', 'Filter')
));
$arr_gets = array();
foreach ($this->input->get() as $key => $value)
$arr_gets[] = $key . '=' . $value;
$query_string = '?' . implode('&', $arr_gets);
// model
$model_transfer = new Transfer_Model();
$total_transfers = $model_transfer->count_all_transfers($filter->values());
if (($sql_offset = ($page - 1) * $limit_results) > $total_transfers)
$sql_offset = 0;
$alltransfers = $model_transfer->get_all_transfers(
$sql_offset, (int) $limit_results, $order_by,
$order_by_direction, $filter->values()
);
$headline = __('Day book');
$grid = new Grid(url_lang::base() . 'transfers', null, array
(
'use_paginator' => true,
'use_selector' => true,
'current' => $limit_results,
'selector_increace' => 50,
'selector_min' => 50,
'selector_max_multiplier' => 10,
'base_url' => Config::get('lang') . '/transfers/show_all/'
. $limit_results . '/' . $order_by . '/' . $order_by_direction,
'uri_segment' => 'page',
'total_items' => $total_transfers,
'items_per_page' => $limit_results,
'style' => 'classic',
'order_by' => $order_by,
'order_by_direction' => $order_by_direction,
'limit_results' => $limit_results,
'query_string' => $query_string,
'filter' => $filter->view
));
if ($this->acl_check_edit('Accounts_Controller', 'transfers'))
{
$grid->add_new_button(
'transfers/add', __('Add new transfer'),
array(), help::hint('add_new_transfer')
);
$grid->add_new_button(
'transfers/deduct_fees', __('Deduction of member fees'),
array(), help::hint('deduct_member_fees')
);
$m = __('Are you sure you want to deduct all entrance fees');
$grid->add_new_button(
'transfers/deduct_entrance_fees',
__('Deduction of entrance fees'),
array('onclick' => 'return window.confirm(\'' . $m . '?\')'),
help::hint('deduct_entrance_fees')
);
$m = __('Are you sure you want to deduct all device repayments');
$grid->add_new_button(
'transfers/deduct_device_fees',
__('Deduction of device repayments'),
array('onclick' => 'return window.confirm(\'' . $m . '?\')'),
help::hint('deduct_device_repayments')
);
}
$grid->order_field('id')
->label('ID');
$grid->order_link_field('oa_id')
->link('transfers/show_by_account', 'oa_name')
->label('Origin account');
$grid->order_field('oa_attribute')
->label(__('Type'));
$grid->order_link_field('da_id')
->link('transfers/show_by_account', 'da_name')
->label('Origin account');
$grid->order_field('da_attribute')
->label(__('Type'));
$grid->order_field('datetime')
->label(__('Date and time'));
$grid->order_field('text')
->label(__('Text'));
$grid->order_callback_field('daybook_amount')
->label(__('Amount'))
->callback('callback::money');
if ($this->acl_check_view('Accounts_Controller', 'transfers'))
{
$grid->grouped_action_field()
->add_action('id')
->icon_action('show')
->url('transfers/show')
->label('Show transfer');
}
$grid->datasource($alltransfers);
$view = new View('main');
$view->title = $headline;
$view->breadcrumbs = $headline;
$view->content = new View('show_all');
$view->content->headline = $headline . '&nbsp;' . help::hint('day_book');
$view->content->table = $grid;
$view->render(TRUE);
}
/**
* It shows transfers of credit account.
*
* @author Jiri Svitak
* @param integer $account_id
* @return unknown_type
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
*/
public function show_by_account(
$account_id = NULL, $limit_results = 500, $order_by = 'datetime',
$order_by_direction = 'desc', $page_word = null, $page = 1)
{
if (!isset($account_id))
{
url::redirect('transfers/show_all');
}
$account = new Account_Model($account_id);
$variable_symbol_model = new Variable_Symbol_Model();
$variable_symbols = 0;
if ($account->member_id != 1)
{
$variable_symbols = $variable_symbol_model->find_account_variable_symbols($account->id);
}
if (!$account->id)
Controller::error(RECORD);
if (!(
$this->acl_check_view('Accounts_Controller', 'transfers', $account->member_id) ||
$this->acl_check_view('Members_Controller', 'currentcredit')
))
{
Controller::error(ACCESS);
}
// gets grid settings
if (is_numeric($this->input->get('record_per_page')))
$limit_results = (int) $this->input->get('record_per_page');
// allowed order type array
$allowed_order_type = array
(
'id', 'trans_type', 'name', 'datetime', 'amount', 'text'
);
if (!in_array(strtolower($order_by), $allowed_order_type))
$order_by = 'datetime';
if (strtolower($order_by_direction) != 'desc')
$order_by_direction = 'desc';
// creates fields for filtering
$arr_types[Transfer_Model::INBOUND_AND_OUTBOUND] = __('All transfers');
$arr_types[Transfer_Model::INBOUND] = __('Inbound');
$arr_types[Transfer_Model::OUTBOUND] = __('Outbound');
$filter = new Table_Form(
url_lang::base() . "transfers/show_by_account/$account_id", "get", array
(
new Table_Form_Item('text', 'name', 'Counteraccount'),
new Table_Form_Item('text', 'datetime', 'Date and time'),
"tr",
new Table_Form_Item('text', 'text', 'Text'),
new Table_Form_Item('text', 'amount', 'Amount'),
"tr",
new Table_Form_Item('select', 'type', 'Type', $arr_types),
"td", new Table_Form_Item('submit', 'submit', 'Filter')
));
$arr_gets = array();
foreach ($this->input->get() as $key => $value)
$arr_gets[] = $key . '=' . $value;
$query_string = '?' . implode('&', $arr_gets);
// all types of transfers are shown by default
$filter_values = $filter->values();
if (count($filter_values) == 0)
$filter_values['type'] = Transfer_Model::INBOUND_AND_OUTBOUND;
// transfers on account
$transfer_model = new Transfer_Model();
$total_transfers = $transfer_model->count_transfers($account_id, $filter_values);
if (($sql_offset = ($page - 1) * $limit_results) > $total_transfers)
$sql_offset = 0;
$transfers = $transfer_model->get_transfers(
$account_id, $sql_offset, (int) $limit_results, $order_by,
$order_by_direction, $filter_values
);
// total amount of inbound and outbound transfers and total balance
$balance = $inbound = $outbound = 0;
if (count($transfers) > 0)
{
// inbound and outbound amount of money are calculated from transfers of account
$inbound = $transfers->current()->inbound;
$outbound = $transfers->current()->outbound;
// balance is not calculated, fast redundant value from account itself is used
$balance = $account->balance;
}
// headline
$headline = __('Transfers of double-entry account');
// grid of transfers
$transfers_grid = new Grid('transfers', null, array
(
'current' => $limit_results,
'selector_increace' => 500,
'selector_min' => 500,
'selector_max_multiplier' => 10,
'base_url' => Config::get('lang') . '/transfers/show_by_account/'
. $account_id . '/' . $limit_results . '/'
. $order_by . '/' . $order_by_direction,
'uri_segment' => 'page',
'total_items' => $total_transfers,
'items_per_page' => $limit_results,
'style' => 'classic',
'order_by' => $order_by,
'order_by_direction' => $order_by_direction,
'limit_results' => $limit_results,
'variables' => $account_id . '/',
'url_array_ofset' => 1,
'query_string' => $query_string,
'filter' => $filter->view
));
// payment cash link
if ($this->acl_check_new('Accounts_Controller', 'transfers'))
{
$transfers_grid->add_new_button(
'transfers/add_member_fee_payment_by_cash/' .
$account->member->id, __('Payment by cash')
);
}
if ($this->acl_check_new('Accounts_Controller', 'transfers', $account->member_id))
{
$transfers_grid->add_new_button(
'transfers/add_from_account/' . $account_id,
__('Send money to other account'),
array(), help::hint('add_from_account')
);
$account = ORM::factory('account')->where('id', $account_id)->find();
if (Billing::instance()->has_driver() &&
Billing::instance()->get_account($account->member_id))
{
$transfers_grid->add_new_button(
'transfers/add_voip/' . $account_id,
__('Recharge VoIP credit')
);
}
}
if ($account->account_attribute_id == Account_attribute_Model::CREDIT)
{
if ($this->acl_check_edit('Accounts_Controller', 'transfers'))
{
$m = __('Are you sure you want to recount fees of this member');
$transfers_grid->add_new_button(
'transfers/recalculate_fees/' . $account->id,
__('Recount of member fees'),
array('onclick' => 'return window.confirm(\'' . $m . '?\')'),
help::hint('recalculate_fees')
);
}
if ($this->acl_check_edit('Accounts_Controller', 'transfers'))
{
$m = __('Are you sure you want to recount entrance fees of this member');
$transfers_grid->add_new_button(
'transfers/recalculate_entrance_fees/' .
$account->id, __('Recount of entrance fees'),
array('onclick' => 'return window.confirm(\'' . $m . '?\')'),
help::hint('recalculate_entrance_fees')
);
}
if ($this->acl_check_edit('Accounts_Controller', 'transfers'))
{
$m = __('Are you sure you want to recount device repayments of this member');
$transfers_grid->add_new_button(
'transfers/recalculate_device_fees/' .
$account->id, __('Recount of device repayments'),
array('onclick' => 'return window.confirm(\'' . $m . '?\')'),
help::hint('recalculate_device_fees')
);
}
}
$transfers_grid->order_field('id')
->label('ID');
$transfers_grid->order_field('name')
->label(__('Counteraccount'));
$transfers_grid->order_field('datetime')
->label(__('Date and time'));
$transfers_grid->order_callback_field('amount')
->label(__('Amount'))
->callback('callback::amount_field');
$transfers_grid->order_field('text')
->label(__('Text'));
$transfers_grid->order_field('variable_symbol')
->label(__('VS'));
if ($this->acl_check_view('Accounts_Controller', 'transfers', $account->member_id))
{
$transfers_grid->grouped_action_field()
->add_action('id')
->icon_action('show')
->url('transfers/show')
->label('Show transfer');
}
$transfers_grid->datasource($transfers);
if ($this->acl_check_view('Members_Controller', 'comment', $account->member_id))
{
$comment_model = new Comment_Model();
$comments = $comment_model->get_all_comments_by_comments_thread($account->comments_thread_id);
$comments_grid = new Grid(url_lang::base() . 'members', null, array
(
'separator' => '<br /><br />',
'use_paginator' => false,
'use_selector' => false,
));
$url = ($account->comments_thread_id) ?
'comments/add/' . $account->comments_thread_id :
'comments_threads/add/account/' . $account->id;
$comments_grid->add_new_button(
$url, __('Add comment to financial state of member')
);
$comments_grid->field('text');
$comments_grid->link_field('user_id')
->link('users/show', 'user_name')
->label('User');
$comments_grid->field('datetime')
->label(__('Time'));
$actions = $comments_grid->grouped_action_field();
$actions->add_conditional_action()
->icon_action('edit')
->url('comments/edit')
->condition('is_own');
$actions->add_conditional_action()
->icon_action('delete')
->url('comments/delete')
->condition('is_own')
->class('delete_link');
$comments_grid->datasource($comments);
}
$breadcrumbs = breadcrumbs::add()
->link('members/show_all', 'Members',
$this->acl_check_view('Members_Controller', 'members')
)->disable_translation()
->link('members/show/' . $account->member_id,
'ID ' . $account->member->id . ' - ' . $account->member->name,
$this->acl_check_view(
'Members_Controller', 'members',
$account->member_id
)
)->enable_translation()
->text('Transfers');
$view = new View('main');
$view->title = $headline;
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('transfers/show_by_account');
$view->content->headline = $headline;
$view->content->account = $account;
$view->content->balance = $balance;
$view->content->inbound = $inbound;
$view->content->outbound = $outbound;
$view->content->variable_symbols = $variable_symbols;
$view->content->expiration_date = Members_Controller::get_expiration_date($account);
$view->content->transfers_grid = $transfers_grid;
if ($this->acl_check_view('Members_Controller', 'comment', $account->member_id))
$view->content->comments_grid = $comments_grid;
$view->render(TRUE);
} // end of show_by_account function
/**
* Function shows information of transfer including previous transfer if exists.
*
* @author Jiri Svitak
* @param integer $transfer_id
*/
public function show($transfer_id = null)
{
if (!isset($transfer_id))
Controller::warning(PARAMETER);
if (!is_numeric($transfer_id))
Controller::error(RECORD);
$transfer_model = new Transfer_Model();
$transfer = $transfer_model->get_transfer($transfer_id);
if (!is_object($transfer))
Controller::error(RECORD);
$oa = new Account_Model($transfer->oa_id);
$da = new Account_Model($transfer->da_id);
if ($oa->member_id != 1)
$member_id = $oa->member_id;
elseif ($da->member_id != 1)
$member_id = $da->member_id;
else
$member_id = 1;
if (!$this->acl_check_view('Accounts_Controller', 'transfers', $member_id))
Controller::error(ACCESS);
// transfers dependent on this transfer, if this transfer is member fee payment
$dependent_transfers = $transfer_model->get_dependent_transfers($transfer->id);
// bank transfer is only assigned to transfer from member fees account to account of association
$member_fees = ORM::factory('account')->where(
'account_attribute_id', Account_attribute_Model::MEMBER_FEES
)->find();
// bt has to be first set to null, transfer need not to be of bank type
$bt = null;
$bt_model = ORM::factory('bank_transfer')->where(
'transfer_id', $transfer->id
)->find();
if ($bt_model->id)
$bt = $bt_model->get_bank_transfer($transfer_id);
$headline = __('Detail of transfer number') . ' ' . $transfer->id;
$view = new View('main');
$view->title = $headline;
$view->content = new View('transfers/show');
$view->content->headline = $headline;
$view->content->transfer = $transfer;
$view->content->dependent_transfers = $dependent_transfers;
$view->content->bt = $bt;
$view->render(TRUE);
}
/**
* Adds transfers from single origin account.
*
* @todo set of accounts choosen by account_attribute_id, must be done by ajax
* @author Jiri Svitak
* @param integer $origin_account_id
*/
public function add_from_account($origin_account_id = null)
{
if (!isset($origin_account_id) || !is_numeric($origin_account_id))
Controller::warning(PARAMETER);
// save for callback function valid_amount_to_send
$this->origin = $origin_account_id;
$origin_account = new Account_Model($origin_account_id);
if ($origin_account->id == 0)
Controller::error(RECORD);
if (!$this->acl_check_new('Accounts_Controller', 'transfers', $origin_account->member_id))
Controller::error(ACCESS);
// destination account, instead of origin one
$dst_account_model = new Account_Model();
$dst_accounts = $dst_account_model->get_some_doubleentry_account_names($origin_account_id);
foreach ($dst_accounts as $dst_account)
{ // convert the object into array (used for HTML select list)
$arr_dst_accounts[$dst_account->id] = $dst_account->name . ' '
. $dst_account->id . ' (' . $dst_account->addr . ')';
}
asort($arr_dst_accounts, SORT_LOCALE_STRING);
// default destination account
$operating = ORM::factory('account')->where(
'account_attribute_id', Account_attribute_Model::OPERATING
)->find();
// array with only one origin account
$arr_orig_accounts[$origin_account->id] = $origin_account->name . ' ' . $origin_account->id;
// account attributes for types of accounts
$aa_model = new Account_attribute_Model();
$account_attributes = $aa_model->get_account_attributes();
foreach ($account_attributes as $account_attribute)
{
$arr_attributes[$account_attribute->id] = $account_attribute->id . ' ' . $account_attribute->name;
}
$arr_attributes = arr::merge(array
(
NULL => '----- ' . __('Select account type') . ' -----'), $arr_attributes
);
// form
$form = new Forge('transfers/add_from_account/' . $origin_account_id);
// origin account
$form->group('Origin account');
$form->dropdown('oname')
->label('Origin account (account name, account ID)')
->options($arr_orig_accounts)
->rules('required')
->style('width:600px');
// destination account
$form->group('Destination account');
$form->dropdown('account_type')
->options($arr_attributes)
->style('width:600px');
$form->dropdown('aname')
->label('Destination account (account name, account ID)')
->options($arr_dst_accounts)
->rules('required')
->selected($operating->id)
->style('width:600px');
// other information
$form->group('Transfer');
$form->date('datetime')
->label('Date and time')
->years(date('Y') - 20, date('Y'))
->rules('required');
$form->input('amount')
->rules('required|valid_numeric')
->callback(array($this, 'valid_amount_to_send'));
$form->input('text')
->rules('required');
$form->submit('Send');
if ($form->validate())
{
$form_data = $form->as_array();
try
{
$db = new Transfer_Model();
$db->transaction_start();
Transfer_Model::insert_transfer(
$form_data['oname'], $form_data['aname'], null,
null, $this->session->get('user_id'),
null, date('Y-m-d', $form_data['datetime']),
date('Y-m-d H:i:s'), $form_data['text'],
$form_data['amount']
);
$member_model = new Member_Model();
$dst_account = new Account_Model($form_data['aname']);
$member_model->reactivate_messages($origin_account->member_id);
$member_model->reactivate_messages($dst_account->member_id);
$db->transaction_commit();
status::success('Transfer has been successfully added.');
}
catch (Exception $e)
{
$db->transaction_rollback();
Log::add_exception($e);
status::error('Error - cannot add new transfer.');
}
url::redirect('transfers/show_by_account/' . $origin_account_id);
}
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('members/show_all', 'Members',
$this->acl_check_view('Members_Controller', 'members')
)->disable_translation()
->link('members/show/' . $origin_account->member_id,
'ID ' . $origin_account->member->id . ' - ' . $origin_account->member->name,
$this->acl_check_view(
'Members_Controller', 'members',
$origin_account->member_id
)
)->enable_translation()
->link('transfers/show_by_account/' . $origin_account->id, 'Transfers')
->text('Add new transfer');
$headline = __('Add new transfer');
$view = new View('main');
$view->breadcrumbs = $breadcrumbs->html();
$view->title = $headline;
$view->content = new View('form');
$view->content->headline = $headline;
$view->content->form = $form->html();
$view->content->link_back = '';
$view->render(TRUE);
}
/**
* Function adds transfers from one arbitrary account to another arbitrary account.
*
* @todo set of accounts choosen by account_attribute_id, must be done by ajax
* @author Jiri Svitak, Tomas Dulik
* @param integer $origin_account
*/
public function add()
{
if (!$this->acl_check_new('Accounts_Controller', 'transfers'))
Controller::error(ACCESS);
$account_model = new Account_Model();
$accounts = $account_model->get_some_doubleentry_account_names();
$origin_accounts = $accounts;
$dst_accounts = $accounts;
foreach ($dst_accounts as $dst_account)
{
$arr_dst_accounts[$dst_account->id] = $dst_account->name . ' '
. $dst_account->id . ' (' . $dst_account->addr . ')';
}
asort($arr_dst_accounts);
// array origin accounts for dropdown
$arr_orig_accounts = $arr_dst_accounts;
// default destination account
$operating = ORM::factory('account')->where(
'account_attribute_id', Account_attribute_Model::OPERATING
)->find();
// account attributes for types of accounts
$aa_model = new Account_attribute_Model();
$account_attributes = $aa_model->get_account_attributes();
foreach ($account_attributes as $account_attribute)
{
$arr_attributes[$account_attribute->id] = $dst_account->name . ' ' . $dst_account->id;
}
// form
$form = new Forge('transfers/add');
// origin account
$form->group('Origin account');
$form->dropdown('oname')
->label(__('Origin account (account name, account ID)') . ':')
->options($arr_orig_accounts)
->rules('required');
// destination account
$form->group('Destination account');
$form->dropdown('aname')
->label(__('Destination account (account name, account ID)') . ':')
->options($arr_dst_accounts)
->rules('required')
->selected($operating->id);
// other information
$form->group('Transfer');
$form->date('datetime')
->label(__('Date and time') . ':')
->years(date('Y') - 20, date('Y'))
->rules('required');
// no amount on origin account is required, this arbitrary transfers
// should only admin or accountant of association who knows what is he doing
$form->input('amount')
->label(__('Amount') . ':')
->rules('required|valid_numeric')
->callback(array($this, 'valid_amount'));
$form->input('text')
->label(__('Text') . ':')
->rules('required');
$form->submit('Send');
if ($form->validate())
{
$form_data = $form->as_array();
try
{
$db = new Transfer_Model();
$db->transaction_start();
Transfer_Model::insert_transfer(
$form_data['oname'], $form_data['aname'], null, null,
$this->session->get('user_id'), null,
date('Y-m-d', $form_data['datetime']), date('Y-m-d H:i:s'),
$form_data['text'], $form_data['amount']
);
$db->transaction_commit();
status::success('Transfer has been successfully added.');
}
catch (Exception $e)
{
$db->transaction_rollback();
Log::add_exception($e);
status::success('Transfer hasnot been successfully added');
}
url::redirect('transfers/show_all');
}
$breadcrumbs = breadcrumbs::add()
->link('transfers/show_all', 'Day book')
->text('Add new transfer');
$headline = __('Add new transfer');
$view = new View('main');
$view->title = $headline;
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('form');
$view->content->headline = $headline;
$view->content->form = $form->html();
$view->render(TRUE);
}
/**
* Function adds VoIP transfers.
*
* @author Roman Sevcik
* @param integer $origin_account
*/
public function add_voip($origin_account = NULL)
{
if (!isset($origin_account))
Controller::warning(PARAMETER);
$account = ORM::factory('account')->where('id', $origin_account)->find();
if (!Billing::instance()->has_driver() ||
!Billing::instance()->get_account($account->member_id))
{
Controller::error(RECORD);
}
if (isset($origin_account))
{ // transfer from specific account?
// save for callback function valid_amount_to_send
$this->origin = $origin_account;
$origin_acc = new Account_Model($origin_account);
if ($origin_acc->id == 0)
{
Controller::error(RECORD);
}
if (!$this->acl_check_new('Accounts_Controller', 'transfers', $origin_acc->member_id))
{ // does the user have rights for this?
Controller::error(ACCESS);
}
// yes = create object of all accounts except the origin one
$dst_account_model = new Account_Model();
$dst_accounts = $dst_account_model->get_some_doubleentry_account_names($origin_account);
}
else
{ // transfer from any (arbitrary) account
if (!$this->acl_check_new('Accounts_Controller', 'transfers'))
{ // Does the user have the rights for this?
Controller::error(ACCESS);
}
// yes = create object of all accounts
$origin_acc = new Account_Model();
$dst_accounts = $origin_accounts = $origin_acc->get_some_doubleentry_account_names();
}
$arr_orig_accounts[$origin_acc->id] =
"$origin_acc->name - " . __('Account ID') . " $origin_acc->name - "
. __('Member ID') . " $origin_acc->member_id";
// form
$form = new Forge('transfers/add_voip/' . $origin_account);
$form->group('Transfer');
$form->dropdown('oname')
->label(__('Origin account'))
->options($arr_orig_accounts);
$form->date('datetime')
->label(__('Date and time') . ':')
->years(date('Y') - 20, date('Y'))
->rules('required');
$form->input('amount')
->label(__('Amount') . ':')
->rules('required|valid_numeric')
->callback(array($this, 'valid_amount_to_send'));
$form->submit('Send');
if ($form->validate())
{
// default destination account
$operating = ORM::factory('account')->where(
'account_attribute_id', Account_attribute_Model::OPERATING
)->find();
$text = __('Recharging of VoIP credit');
$form_data = $form->as_array();
try
{
$db = new Transfer_Model();
$db->transaction_start();
Transfer_Model::insert_transfer(
$form_data['oname'], $operating->id, null, null,
$this->session->get('user_id'),
Transfer_Model::DEDUCT_VOIP_UNNACCOUNTED_FEE,
date('Y-m-d', $form_data['datetime']), date('Y-m-d H:i:s'),
$text, $form_data['amount']
);
$db->transaction_commit();
status::success('Transfer has been successfully added.');
}
catch (Exception $e)
{
$db->transaction_rollback();
Log::add_exception($e);
status::success('Transfer has not been successfully added');
}
url::redirect('transfers/show_by_account/' . $origin_account);
} if ($this->acl_check_view('Members_Controller', 'members', $account->member_id))
{
$links[] = html::anchor(
'members/show/' . $account->member_id, __('Back to the member')
);
}
$links[] = html::anchor(
'transfers/show_by_account/' . $origin_account,
__('Back to transfers of account')
);
$headline = __('Add new VoIP transfer');
$info[] = __('Information') . ' : ' . __('Transfer will be effected within 15 minutes.');
$view = new View('main');
$view->title = $headline;
$view->content = new View('form');
$view->content->headline = $headline;
$view->content->form = $form->html();
$view->content->link_back = implode(' | ', $links);
$view->content->aditional_info = $info;
$view->render(TRUE);
}
/**
* Function edits double-entry transfers. They should not be edited.
* Wrong transfer should be solved by new transfer.
*
* @author Jiri Svitak
* @param integer $transfer_id
*/
public function edit($transfer_id = NULL)
{
if (!isset($transfer_id))
Controller::warning(PARAMETER);
// access rights
if (!$this->acl_check_edit('Accounts_Controller', 'transfers'))
Controller::error(ACCESS);
$transfer = new Transfer_Model($transfer_id);
$form = new Forge('transfers/edit/' . $transfer_id);
$form->group('Basic information');
$form->input('text')
->rules('required|length[3,50]')
->value($transfer->text);
$form->input('amount')
->rules('required')
->value($transfer->amount)
->callback(array($this, 'valid_amount'));
$form->submit('Edit');
if ($form->validate())
{
$form_data = $form->as_array();
try
{
$transfer->transaction_start();
Transfer_Model::edit_transfer(
$transfer->id, $form_data['text'], $form_data['amount']
);
$transfer->transaction_commit();
status::success('Transfer has been successfully updated.');
url::redirect('transfers/show/' . $transfer_id);
}
catch (Exception $e)
{
$transfer->transaction_rollback();
Log::add_exception($e);
status::error('Error - cant update transfer.');
url::redirect('transfers/show/' . $transfer_id);
}
}
else
{
$headline = __('Editing of transfer');
$view = new View('main');
$view->title = $headline;
$view->content = new View('form');
$view->content->headline = $headline;
$view->content->form = $form->html();
$view->render(TRUE);
}
}
/**
* Deducts fees of all members in one month. If deduct transfer for one month and
* account is found, then it is ignored and skipped.
*
* @todo rewrite SQL to models
* @author Jiri Svitak
* @return unknown_type
*/
public function deduct_fees()
{
// access rights
if (!$this->acl_check_new('Accounts_Controller', 'transfers'))
Controller::error(ACCESS);
// content of dropdown for months
for ($i = 1; $i <= 12; $i++)
$arr_months[$i] = $i;
$current_month = (int) date('n');
// content of dropdown for years
$year_from = date('Y') - 20;
for ($i = 1; $i <= 20; $i++)
$arr_years[$year_from + $i] = $year_from + $i;
$form = new Forge('transfers/deduct_fees');
$form->dropdown('year')
->rules('required')
->options($arr_years)
->style('width:200px')
->selected($year_from + 20);
$form->dropdown('month')
->rules('required')
->options($arr_months)
->selected($current_month)
->style('width:200px')
->callback(array($this, 'valid_default_fee'));
$form->submit('Deduct');
// form validation
if ($form->validate())
{
// access rights
if (!$this->acl_check_new('Accounts_Controller', 'transfers'))
Controller::error(ACCESS);
$form_data = $form->as_array();
// preparation
$created_transfers_count = 0;
$total_amount = 0;
$date = date('Y-m-d', mktime(
0, 0, 0, $form_data['month'], 15, $arr_years[$form_data['year']]
));
$creation_datetime = date('Y-m-d H:i:s');
$user_id = $this->session->get('user_id');
// finds default fee
$fee_model = new Fee_Model();
$fee = $fee_model->get_default_fee_by_date_type($date, 'regular member fee');
if ($fee && $fee->id)
{
$default_fee = $fee->fee;
}
else
{
status::error('Fees have not been set!');
url::redirect('transfers/show_all');
}
$operating = ORM::factory('account')->where(
'account_attribute_id', Account_attribute_Model::OPERATING
)->find();
$database = Database::instance();
$account_model = new Account_Model();
$accounts = $account_model->get_accounts_to_deduct($date);
try
{
$db = new Transfer_Model();
$db->transaction_start();
// first sql for inserting transfers
$sql_insert = "INSERT INTO transfers (origin_id, destination_id, ".
"previous_transfer_id, member_id, user_id, type, datetime, ".
"creation_datetime, text, amount) VALUES ";
$values = array();
// second sql for updating accounts
$sql_update = "UPDATE accounts SET balance = CASE id ";
$ids = array();
// main cycle
foreach ($accounts as $account)
{
// no deduct transfer for this date and account generated? then create one
if ($account->transfer_id == 0)
{
$text = __('Deduction of member fee');
if ($account->fee_is_set)
{
$amount = $account->fee;
$text = ($account->fee_readonly) ? $text . ' - ' .
__('' . $account->fee_name) : $text . ' - ' .
$account->fee_name;
}
else
{
$amount = $default_fee;
}
// is amount bigger than zero?
if ($amount > 0)
{
// insert
$values[] = "($account->id, $operating->id, NULL, NULL, " .
"$user_id, " . Transfer_Model::DEDUCT_MEMBER_FEE .
", '$date', '$creation_datetime', '$text', $amount)";
// update
$sql_update .= "WHEN $account->id THEN " . num::decimal_point($account->balance - $amount) . " ";
$ids[] = $account->id;
$total_amount += $amount;
$created_transfers_count++;
}
}
}
if ($created_transfers_count > 0)
{
// single query for inserting transfers
$sql_insert .= implode(",", $values);
if (!$database->query($sql_insert))
throw new Exception();
// single query for updating credit account balances
$ids_with_commas = implode(",", $ids);
$sql_update .= "END WHERE id IN ($ids_with_commas)";
if (!$database->query($sql_update))
throw new Exception();
// update also balance of operating account
if (!$account_model->recalculate_account_balance_of_account($operating->id))
throw new Exception();
}
$db->transaction_commit();
status::success(
'Fees have been successfully deducted, %d new transfers created.',
TRUE, array($created_transfers_count)
);
}
catch (Exception $e)
{
$db->transaction_rollback();
Log::add_exception($e);
status::error('Error - some fees have not been deducted.');
}
url::redirect('transfers/show_all');
}
$breadcrumbs = breadcrumbs::add()
->link('transfers/show_all', 'Day book')
->text('Deduction of member fees');
$view = new View('main');
$view->title = __('Deduction of member fees');
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('form');
$view->content->headline = __('Deduction of member fees');
$view->content->form = $form->html();
$view->render(TRUE);
}
/**
* Function recalculates wrong deducted fees of given account of member.
*
* @author Jiri Svitak
*/
public function recalculate_fees($account_id = 0)
{
// access control
if (!$this->acl_check_new('Accounts_Controller', 'transfers'))
Controller::error(ACCESS);
$account = new Account_Model($account_id);
// account doesn't exist
if (!$account->id)
Controller::error(RECORD);
// finds default fee
$fee_model = new Fee_Model();
// finds member's entrance date
$entrance_date = date_parse($account->member->entrance_date);
$year = $entrance_date['year'];
$month = $entrance_date['month'];
$day = $entrance_date['day'];
// round entrance date
if ($day > 15)
{
$month++;
if ($month > 12)
{
$year++;
$month = 1;
}
}
// finds member's leaving date
if ($account->member->leaving_date != '0000-00-00')
{
$max_date = date_parse($account->member->leaving_date);
}
else
{
// find datetime of last deduct fee
$transfer_model = new Transfer_Model();
$last_datetime = $transfer_model->find_last_transfer_datetime_by_type(
Transfer_Model::DEDUCT_MEMBER_FEE
);
echo $last_datetime;
if ($last_datetime)
$max_date = date_parse($last_datetime);
else
$max_date = date_parse(date('Y-m-d'));
}
$max_day = $max_date['day'];
$max_month = $max_date['month'];
$max_year = $max_date['year'];
// round max date
if ($max_day < 15)
{
$max_month--;
if ($max_month == 0)
{
$max_year--;
$max_month = 12;
}
}
// finds operating account
$operating = ORM::factory('account')->where(
'account_attribute_id', Account_attribute_Model::OPERATING
)->find();
// creation datetime of transfers
$creation_datetime = date('Y-m-d H:i:s');
try
{
$db = new Transfer_Model();
$db->transaction_start();
// deleting old deduct transfer of member
$deleted_transfers_count = $account->delete_deduct_transfers_of_account($account_id);
$created_transfers_count = 0;
$max_date = date('Y-m-d', mktime(0, 0, 0, $max_month, 15, $max_year));
while (($date = date('Y-m-d', mktime(0, 0, 0, $month, 15, $year))) <= $max_date)
{
$text = __('Deduction of member fee');
// finds default regular member fee for this month
$fee = $fee_model->get_default_fee_by_date_type($date, 'regular member fee');
if ($fee && $fee->id)
$default_fee = $fee->fee;
else
throw new Exception(__('Fatal error'), __('Fees have not been set!'));
// finds regular member fee for this member and this month
$fee = $fee_model->get_fee_by_member_date_type(
$account->member_id, $date, 'regular member fee'
);
// it exists
if ($fee && $fee->id)
{
$amount = $fee->fee;
// translate only read-only fee names
$text = ($fee->readonly) ? $text . ' - ' . __(''
. $fee->name) : $text . ' - ' . $fee->name;
}
// it doesn't exist - uses default fee
else
{
$amount = $default_fee;
}
if ($amount > 0)
{
$created_transfers_count++;
Transfer_Model::insert_transfer(
$account->id, $operating->id, null, null,
$this->session->get('user_id'),
Transfer_Model::DEDUCT_MEMBER_FEE, $date,
$creation_datetime, $text, $amount
);
}
// iterate to next month
$month++;
if ($month == 13)
{
$month = 1;
$year++;
}
}
$db->transaction_commit();
status::success(
'Fees have been successfully recalculated, %d deleted '.
'transfers, %d created new transfers.',
TRUE, array
(
0 => $deleted_transfers_count,
1 => $created_transfers_count
)
);
}
catch (Exception $e)
{
$db->transaction_rollback();
Log::add_exception($e);
status::error('Error - some fees have not been recounted.');
}
url::redirect('transfers/show_by_account/' . $account_id);
}
/**
* Function deducts entrance fees. This fee is deducted only one once to each member.
* It checks each member if his fee was deducted, and if it is not, then
* the fee is deducted.
*
* @author Jiri Svitak
*/
public function deduct_entrance_fees()
{
// access rights
if (!$this->acl_check_new('Accounts_Controller', 'transfers'))
Controller::error(ACCESS);
// preparation
$created_transfers_count = 0;
$creation_datetime = date('Y-m-d H:i:s');
//$current_date = date::get_middle_of_month(date('Y-m-d'));
$current_date = date('Y-m-d');
$infrastructure = ORM::factory('account')->where(
'account_attribute_id', Account_attribute_Model::INFRASTRUCTURE
)->find();
$account_model = new Account_Model();
$transfer_model = new Transfer_Model();
// gets all credit accounts, tries to find also existing transfer
$credit_accounts = $account_model->get_accounts_to_deduct_entrance_fees();
try
{
$db = new Transfer_Model();
$db->transaction_start();
foreach ($credit_accounts as $ca)
{
// if member's entrance fee is 0, then no transfer is generated
if ($ca->entrance_fee == 0)
continue;
// are there some deduct transfers?
if (empty($ca->transfer_id))
{
// no deduct entrance fee transfer means that nothing is already paid
$already_paid = 0;
$latest_date = '0001-01-01';
}
else
{
// some transfers already exist, then their total amount is calculated
$eftransfers = $transfer_model->get_entrance_fee_transfers_of_account($ca->id);
$already_paid = $eftransfers->current()->total_amount;
$latest_date = $eftransfers->current()->datetime;
$latest_date = substr($latest_date, 0, strpos($latest_date, ' '));
}
// is already whole entrance fee paid?
if ($already_paid >= $ca->entrance_fee)
continue;
// entrance fee is not wholy paid, calculate debt
$debt = $ca->entrance_fee - $already_paid;
// entrance date of current member
$entrance = date_parse($ca->entrance_date);
$eyear = $entrance['year'];
$emonth = $entrance['month'];
$eday = $entrance['day'];
if ($eday > 15)
$emonth++;
if ($emonth == 13)
{
$emonth = 1;
$eyear++;
}
// getting boundary date - 15th day in the month
$date = date('Y-m-d', mktime(0, 0, 0, $emonth, 15, $eyear));
// while debt is greater than zero and date of instalment is lower than current date
while ($debt > 0 && $date < $current_date)
{
// is debt still greater than one monthly instalment?
if ($debt > $ca->debt_payment_rate)
{
// one monthly instalment is deducted
$amount = $ca->debt_payment_rate;
// if debt pay rate is zero, then whole amount of debt is deducted
if ($amount <= 0)
$amount = $debt;
}
else
{
// rest of the debt is deducted
$amount = $debt;
}
// only one transfer per month - older transfers cannot be duplicated
if ($date > $latest_date)
{
// decrease amount of debt due to size of one instalment
$debt -= $amount;
// create new transfer
Transfer_Model::insert_transfer(
$ca->id, $infrastructure->id, null, null,
$this->session->get('user_id'),
Transfer_Model::DEDUCT_ENTRANCE_FEE, $date,
$creation_datetime, __('Entrance fee'), $amount
);
$created_transfers_count++;
}
// iterate to next month
$emonth++;
if ($emonth == 13)
{
$emonth = 1;
$eyear++;
}
// getting boundary date - 15th day in the month
$date = date('Y-m-d', mktime(0, 0, 0, $emonth, 15, $eyear));
}
}
$db->transaction_commit();
status::success(
'Entrance fees have been successfully deducted, '.
'%d new transfers created.', TRUE, $created_transfers_count
);
}
catch (Exception $e)
{
$db->transaction_rollback();
Log::add_exception($e);
status::error('Error - cant deduct entrance fee.');
}
url::redirect('transfers/show_all');
}
/**
* Recounts transfer of one member credit account.
* Used only in special cases, like changing entrance date.
... Rozdílový soubor je zkrácen, protože jeho délka přesahuje max. limit.

Také k dispozici: Unified diff