Projekt

Obecné

Profil

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

/**
* Controller handles fees in system.
* Fee is payment, which can be single-shot (payed just once) or regular (payed
* after some time interval periodicly).
*
* @package Controller
*/
class Fees_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
private $fee_id = NULL;

/**
* Default function for Fees.
*
* @author Michal Kliment
*/
public function index()
{
url::redirect(url_lang::base() . 'fees/show_all');
}

/**
* Shows all fees table.
*
* @author Michal Kliment
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
*/
public function show_all(
c1bdc1c4 Michal Kliment
$limit_results = 200, $order_by = 'from',
$order_by_direction = 'ASC', $page_word = 'page', $page = 1)
8baed187 Michal Kliment
{
// check if logged user have access right to view all fees
c1bdc1c4 Michal Kliment
if (!$this->acl_check_view('Fees_Controller', 'fees'))
8baed187 Michal Kliment
Controller::Error(ACCESS);

// to-do - pagination
// get new selector
c1bdc1c4 Michal Kliment
if (is_numeric($this->input->post('record_per_page')))
$limit_results = (int) $this->input->post('record_per_page');
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
$allowed_order_type = array('id', 'type', 'name', 'fee', 'from', 'to');
8baed187 Michal Kliment
if (!in_array(strtolower($order_by), $allowed_order_type))
c1bdc1c4 Michal Kliment
$order_by = 'from';
8baed187 Michal Kliment
if (strtolower($order_by_direction) != 'desc')
$order_by_direction = 'asc';

$fee_model = new Fee_Model();
c1bdc1c4 Michal Kliment
8baed187 Michal Kliment
$total_fees = $fee_model->count_all();
c1bdc1c4 Michal Kliment
// limit check
if (($sql_offset = ($page - 1) * $limit_results) > $total_fees)
$sql_offset = 0;
$fees = $fee_model->get_all_fees(
$sql_offset, $limit_results, $order_by, $order_by_direction
);
// path to form
$path = Config::get('lang') . '/fees/show_all/' . $limit_results . '/'
. $order_by . '/' . $order_by_direction.'/'.$page_word.'/'
. $page;
8baed187 Michal Kliment
// create grid
$grid = new Grid('fees', '', array
(
'use_paginator' => true,
'use_selector' => true,
'current' => $limit_results,
'selector_increace' => 200,
'selector_min' => 200,
'selector_max_multiplier' => 10,
c1bdc1c4 Michal Kliment
'base_url' => $path,
8baed187 Michal Kliment
'uri_segment' => 'page',
'total_items' => $total_fees,
'items_per_page' => $limit_results,
'style' => 'classic',
'order_by' => $order_by,
'order_by_direction' => $order_by_direction,
'limit_results' => $limit_results
));

// add button for new translation
// check if logged user have access right to add new translation
c1bdc1c4 Michal Kliment
if ($this->acl_check_new('Fees_Controller', 'fees'))
8baed187 Michal Kliment
{
$grid->add_new_button('fees/add', __('Add new fee'));
}

// set grid fields
c1bdc1c4 Michal Kliment
$grid->order_field('id')
->label('ID');
8baed187 Michal Kliment
$grid->order_field('type');
$grid->order_callback_field('name')
->callback(array($this, 'name_field'));
74a7dbca Michal Kliment
$grid->order_callback_field('fee')
->callback('callback::money', 'print_zero');
8baed187 Michal Kliment
$grid->order_field('from')
->label(__('Date from'));
$grid->order_field('to')
->label(__('Date to'));
$actions = $grid->grouped_action_field();

// check if logged user have access right to edit this enum types
c1bdc1c4 Michal Kliment
if ($this->acl_check_edit('Fees_Controller', 'fees'))
8baed187 Michal Kliment
{
$actions->add_conditional_action()
->icon_action('edit')
->condition('is_not_readonly')
->url('fees/edit');
}

// check if logged user have access right to delete this enum_types
c1bdc1c4 Michal Kliment
if ($this->acl_check_delete('Fees_Controller', 'fees'))
8baed187 Michal Kliment
{
$actions->add_conditional_action()
->icon_action('delete')
->condition('is_not_readonly')
->url('fees/delete')
->class('delete_link');
}

$grid->datasource($fees);

// create view for this template
$view = new View('main');
$view->title = __('Fees');
$view->breadcrumbs = __('Fees');
$view->content = new View('show_all');
$view->content->table = $grid;
$view->content->headline = __('Fees');
$view->render(TRUE);
}

/**
c1bdc1c4 Michal Kliment
* Adds new fee
8baed187 Michal Kliment
*
c1bdc1c4 Michal Kliment
* @author David Raška
8baed187 Michal Kliment
*/
public function add($fee_type_id = NULL)
{
// access control
c1bdc1c4 Michal Kliment
if (!$this->acl_check_new('Fees_Controller', 'fees'))
8baed187 Michal Kliment
Controller::error(ACCESS);
c1bdc1c4 Michal Kliment
8baed187 Michal Kliment
if ($fee_type_id && is_numeric($fee_type_id))
{
$enum_type_model = new Enum_type_Model();
$fee_type = $enum_type_model->where(array
(
'id' => $fee_type_id,
'type_id' => Enum_type_Model::FEE_TYPE_ID)
)->find();

if (!$fee_type || !$fee_type->id)
Controller::error(RECORD);

$enum_types = array($fee_type->id => $enum_type_model->get_value($fee_type->id));
}
else
{
$enum_type_name_model = new Enum_type_name_Model();
$enum_type_name = $enum_type_name_model->where('type_name', 'Fees types')->find();

$enum_type_model = new Enum_type_Model();
$enum_types = $enum_type_model->get_values($enum_type_name->id);
}
c1bdc1c4 Michal Kliment
$form = new Forge();
$form->dropdown('type_id')
->label('Type')
->options($enum_types);
$form->input('name')
->label('Tariff name');
$form->input('fee')
->rules('valid_numeric')
->value(0);
$form->date('from')
->rules('required')
->callback(array($this, 'valid_interval'))
->label('Date from');
$form->date('to')
->rules('required')
->label('Date to');
$form->submit('Add');
if ($form->validate())
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$form_data = $form->as_array();
$fee = new Fee_Model();
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
try
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$fee->transaction_start();
$fee->type_id = $form_data['type_id'];
$fee->name = $form_data['name'];
$fee->fee = $form_data['fee'];
$fee->from = date("Y-m-d", $form_data['from']);
$fee->to = date("Y-m-d", $form_data['to']);
$fee->save_throwable();
$fee->transaction_commit();
8baed187 Michal Kliment
// for popup adding
if ($this->popup)
{
c1bdc1c4 Michal Kliment
$this->redirect('fees/show_all', $fee->id);
8baed187 Michal Kliment
}
else
{
c1bdc1c4 Michal Kliment
status::success('Fee has been successfully added');
$this->redirect('fees/show_all');
8baed187 Michal Kliment
}
}
c1bdc1c4 Michal Kliment
catch (Exception $e)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$fee->transaction_rollback();
status::error('Error - can\'t add new fee.');
Log::add_exception($e);
$this->redirect('fees/show_all');
8baed187 Michal Kliment
}
}
c1bdc1c4 Michal Kliment
$headline = __('Add new fee');
8baed187 Michal Kliment
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('fees/show_all', 'Fees',
c1bdc1c4 Michal Kliment
$this->acl_check_view('Fees_Controller', 'fees'))
8baed187 Michal Kliment
->text('Add new fee');

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

/**
* Edits fee
*
c1bdc1c4 Michal Kliment
* @author David Raška
8baed187 Michal Kliment
* @param integer $fee_id
*/
public function edit($fee_id = NULL)
{
if (!$fee_id)
Controller::warning(PARAMETER);
// access control
c1bdc1c4 Michal Kliment
if (!$this->acl_check_edit('Fees_Controller', 'fees'))
8baed187 Michal Kliment
Controller::error(ACCESS);

$fee = new Fee_Model($fee_id);

// item is read only
if ($fee->readonly)
Controller::error(READONLY);

$enum_type_name_model = new Enum_type_name_Model();
$enum_type_name = $enum_type_name_model->where('type_name', 'Fees types')->find();

$enum_type_model = new Enum_type_Model();
$enum_types = $enum_type_model->get_values($enum_type_name->id);

c1bdc1c4 Michal Kliment
$form = new Forge();
$form->dropdown('type_id')
->label('Type')
->options($enum_types)
->selected($fee->type_id);
$form->input('name')
->label('Tariff name')
->value($fee->name);
$form->input('fee')
->rules('valid_numeric')
->value($fee->fee);
$form->date('from')
->rules('required')
->callback(array($this, 'valid_interval'))
->label('Date from')
->value(strtotime($fee->from));
$form->date('to')
->rules('required')
->label('Date to')
->value(strtotime($fee->to));
$form->submit('Edit');
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
if ($form->validate())
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$form_data = $form->as_array();
$fee = new Fee_Model($fee_id);
try
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$fee->transaction_start();
$fee->type_id = $form_data['type_id'];
$fee->name = $form_data['name'];
$fee->fee = $form_data['fee'];
$fee->from = date("Y-m-d", $form_data['from']);
$fee->to = date("Y-m-d", $form_data['to']);
$fee->save_throwable();
$fee->transaction_commit();
status::success('Fee has been successfully updated.');

$this->redirect('fees/show_all');
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
catch (Exception $e)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$fee->transaction_rollback();
status::error('Error - cant edit fee.');
Log::add_exception($e);
if (!$this->popup)
{
$this->redirect('fees/show_all');
}
8baed187 Michal Kliment
}
}
c1bdc1c4 Michal Kliment
$headline = __('Edit fee');
8baed187 Michal Kliment
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('fees/show_all', 'Fees',
c1bdc1c4 Michal Kliment
$this->acl_check_view('Fees_Controller', 'fees'))
8baed187 Michal Kliment
->text($fee->name . ' (' . $fee->id . ')')
->text('Edit fee');

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

/**
* Deletes fee
*
* @author Michal Kliment
* @param ineteger $fee_id
*/
public function delete($fee_id = NULL)
{
// wrong parameter
if (!$fee_id || !is_numeric($fee_id))
Controller::warning(PARAMETER);

// access control
c1bdc1c4 Michal Kliment
if (!$this->acl_check_delete('Fees_Controller', 'fees'))
8baed187 Michal Kliment
Controller::error(ACCESS);

$fee = new Fee_Model($fee_id);

// fee doesn't exist
if (!$fee->id)
Controller::error(RECORD);

// item is read only
if ($fee->readonly)
Controller::error(READONLY);

// fee is used on some tariffs
if ($fee->members_fees->count())
{
status::warning('Fee is used in some tariffs');
url::redirect(url_lang::base() . 'fees/show_all');
}

// success
if ($fee->delete())
{
status::success('Fee has been successfully deleted');
}

url::redirect(url_lang::base() . 'fees/show_all');
}

/**
* Checks overlapping of fee validity intervals.
*
c1bdc1c4 Michal Kliment
* @param Input $input
8baed187 Michal Kliment
*/
c1bdc1c4 Michal Kliment
public function valid_interval($input = NULL)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
// validators cannot be accessed
if (empty($input) || !is_object($input))
8baed187 Michal Kliment
{
self::error(PAGE);
}
c1bdc1c4 Michal Kliment
$arr_from = date_parse_from_format(DateTime::ISO8601, $this->input->post('from'));
$arr_to = date_parse_from_format(DateTime::ISO8601, $this->input->post('to'));
1e8c793d Michal Kliment
c1bdc1c4 Michal Kliment
$date_from = date::round_month($arr_from['day'], $arr_from['month'], $arr_from['year']);
1e8c793d Michal Kliment
// handle 9999-12-31 (#956)
if ($arr_to['year'] == 9999 && $arr_to['month'] == 12 && $arr_to['day'] == 31)
{
$date_to = '9999-12-31';
}
else
{
$date_to = date::round_month($arr_to['day'], $arr_to['month'], $arr_to['year']);
}

c1bdc1c4 Michal Kliment
$diff = date::diff_month($date_to, $date_from);
if ($diff < 0)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$input->add_error('required', __('Date from must be smaller then date to.'));
8baed187 Michal Kliment
return;
}
c1bdc1c4 Michal Kliment
if ($diff == 0)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
$input->add_error('required', __('Minimal duration is one month.'));
8baed187 Michal Kliment
return;
}
}

/**
* Callback function to show name of fee, in read-only fee show translated name
*
* @author Michal Kliment
* @param object $item
* @param string $name
*/
protected static function name_field($item, $name)
{
// fee is read-only
if ($item->readonly)
// show trasnslated name
echo __('' . $item->name);
else
// show name
echo $item->name;
}

}