Projekt

Obecné

Profil

1107 dzolo
<?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/
*
*/
258 michalklim
1107 dzolo
/**
1223 dzolo
* Controller handles fees in system.
* Fee is payment, which can be single-shot (payed just once) or regular (payed
* after some time interval periodicly).
1107 dzolo
*
* @package Controller
*/
class Fees_Controller extends Controller
{
265 michalklim
1107 dzolo
private $fee_id = NULL;
258 michalklim
1107 dzolo
/**
* Default function for Fees.
*
* @author Michal Kliment
*/
public function index()
{
url::redirect(url_lang::base() . 'fees/show_all');
}
258 michalklim
1107 dzolo
/**
* Shows all fees table.
*
* @author Michal Kliment
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
*/
public function show_all(
$limit_results = 200, $order_by = 'id', $order_by_direction = 'ASC')
{
// check if logged user have access right to view all fees
if (!$this->acl_check_view('Settings_Controller', 'fees'))
735 jsvitak
Controller::Error(ACCESS);
258 michalklim
735 jsvitak
// to-do - pagination
// get new selector
258 michalklim
if (is_numeric($this->input->get('record_per_page')))
$limit_results = (int) $this->input->get('record_per_page');

735 jsvitak
$allowed_order_type = array('id', 'type', 'fee', 'from', 'to');
1107 dzolo
if (!in_array(strtolower($order_by), $allowed_order_type))
258 michalklim
$order_by = 'id';
1107 dzolo
if (strtolower($order_by_direction) != 'desc')
258 michalklim
$order_by_direction = 'asc';

735 jsvitak
$fee_model = new Fee_Model();
1107 dzolo
$fees = $fee_model->get_all_fees($order_by, $order_by_direction);
1220 dzolo
$total_fees = $fee_model->count_all();
258 michalklim
735 jsvitak
// create grid
1107 dzolo
$grid = new Grid(url_lang::base() . 'fees', '', array
(
'use_paginator' => true,
'use_selector' => true,
'current' => $limit_results,
'selector_increace' => 200,
'selector_min' => 200,
'selector_max_multiplier' => 10,
'base_url' => Config::get('lang') . '/fees/show_all/'
. $limit_results . '/' . $order_by . '/' . $order_by_direction,
'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
258 michalklim
));

735 jsvitak
// add button for new translation
258 michalklim
// check if logged user have access right to add new translation
1107 dzolo
if ($this->acl_check_new('Settings_Controller', 'fees'))
{
$grid->add_new_button(
url_lang::base() . 'fees/add',
1137 dzolo
__('Add new fee')
1107 dzolo
);
}
258 michalklim
735 jsvitak
// set grid fields
1107 dzolo
$grid->order_field('id')
1137 dzolo
->label(__('Id'));
1107 dzolo
$grid->order_field('type')
1137 dzolo
->label(__('Type'));
1107 dzolo
$grid->order_callback_field('name')
1137 dzolo
->label(__('Name'))
1107 dzolo
->callback('Fees_Controller::name_field');

$grid->order_field('fee')
1137 dzolo
->label(__('Fee'));
1107 dzolo
$grid->order_field('from')
1137 dzolo
->label(__('Date from'));
1107 dzolo
$grid->order_field('to')
1137 dzolo
->label(__('Date to'));
1235 dzolo
$actions = $grid->grouped_action_field();
258 michalklim
735 jsvitak
// check if logged user have access right to edit this enum types
1107 dzolo
if ($this->acl_check_edit('Settings_Controller', 'fees'))
{
1235 dzolo
$actions->add_conditional_action()
->icon_action('edit')
->condition('is_not_readonly')
->url('fees/edit');
1107 dzolo
}
258 michalklim
// check if logged user have access right to delete this enum_types
1107 dzolo
if ($this->acl_check_delete('Settings_Controller', 'fees'))
{
1235 dzolo
$actions->add_conditional_action()
->icon_action('delete')
->condition('is_not_readonly')
->url('fees/delete')
->class('delete_link');
1107 dzolo
}
258 michalklim
735 jsvitak
$grid->datasource($fees);
258 michalklim
735 jsvitak
// create view for this template
461 jsvitak
$view = new View('main');
1137 dzolo
$view->title = __('Fees');
$view->breadcrumbs = __('Fees');
258 michalklim
$view->content = new View('show_all');
$view->content->table = $grid;
1137 dzolo
$view->content->headline = __('Fees');
258 michalklim
$view->render(TRUE);
735 jsvitak
}
258 michalklim
735 jsvitak
/**
1107 dzolo
* Adds new enum type
*
735 jsvitak
* @author Michal Kliment
*/
1107 dzolo
public function add($fee_type_id = NULL)
735 jsvitak
{
// access control
1107 dzolo
if (!$this->acl_check_new('Settings_Controller', 'fees'))
735 jsvitak
Controller::error(ACCESS);
258 michalklim
791 michalklim
if ($fee_type_id && is_numeric($fee_type_id))
{
$enum_type_model = new Enum_type_Model();
1107 dzolo
$fee_type = $enum_type_model->where(array
(
'id' => $fee_type_id,
'type_id' => Enum_type_Model::$fee_type_id)
)->find();
258 michalklim
791 michalklim
if (!$fee_type || !$fee_type->id)
Controller::error(RECORD);
258 michalklim
791 michalklim
$enum_types = array($fee_type->id => $enum_type_model->get_value($fee_type->id));
}
else
{
$enum_type_name_model = new Enum_type_name_Model();
1107 dzolo
$enum_type_name = $enum_type_name_model->where('type_name', 'Fees types')->find();
791 michalklim
$enum_type_model = new Enum_type_Model();
$enum_types = $enum_type_model->get_values($enum_type_name->id);
}

735 jsvitak
$form = array
(
'name' => '',
'fee' => 0,
1107 dzolo
'from' => array
(
'day' => date('j'),
'month' => date('n'),
'year' => date('Y')
),
'to' => array
(
'day' => date('j'),
'month' => date('n'),
'year' => date('Y')
),
735 jsvitak
'type_id' => 1
);
265 michalklim
735 jsvitak
$errors = array
1107 dzolo
(
735 jsvitak
'fee' => '',
'from' => '',
'to' => '',
'type_id' => ''
);
265 michalklim
791 michalklim
735 jsvitak
$days = array();
1107 dzolo
for ($i = 1; $i <= 31; $i++)
$days[$i] = $i;
265 michalklim
735 jsvitak
$months = array();
1107 dzolo
for ($i = 1; $i <= 12; $i++)
$months[$i] = $i;
265 michalklim
735 jsvitak
$years = array();
1107 dzolo
for ($i = date('Y') - 100; $i <= date('Y') + 100; $i++)
$years[$i] = $i;
265 michalklim

735 jsvitak
if ($_POST)
{
$post = new Validation($_POST);
1107 dzolo
$post->add_rules('fee', 'required', 'numeric');
258 michalklim
1107 dzolo
$post->add_rules('from', array('valid', 'date'));
$post->add_rules('to', array('valid', 'date'));
258 michalklim
735 jsvitak
// new system is not restricted on overlapping intervals in global scale
791 michalklim
// overlapping will be checked individually with each member
1107 dzolo
$post->add_callbacks('type_id', array($this, 'valid_interval'));
258 michalklim
735 jsvitak
if ($post->validate())
{
$fee = new Fee_Model();
$fee->type_id = $post->type_id;
$fee->name = $post->name;
$fee->fee = $post->fee;
1107 dzolo
$fee->from = date::round_month(
$post->from['day'],
$post->from['month'],
$post->from['year']
);
$fee->to = date::round_month(
$post->to['day'],
$post->to['month'],
$post->to['year'], TRUE
);
735 jsvitak
// clears form content
unset($form_data);
791 michalklim
// for popup adding
if ($this->popup)
735 jsvitak
{
791 michalklim
// save fee
$fee->save();

1196 dzolo
$fee_name = ($fee->name != '') ? "$fee->name - " : "";
1107 dzolo
$from = str_replace('-', '/', $fee->from);
$to = str_replace('-', '/', $fee->to);
1196 dzolo
$fee_name = "$fee_name$fee->fee " . $this->settings->get('currency') . " ($from-$to)";
735 jsvitak
}
else
{
791 michalklim
// has fee been successfully saved?
if ($fee->save())
1107 dzolo
{
1195 dzolo
status::success('Fee has been successfully added');
1107 dzolo
}
791 michalklim
else
1107 dzolo
{
1195 dzolo
status::error('Error - can\'t add new fee.');
1107 dzolo
}
791 michalklim
// classic adding
1107 dzolo
url::redirect(url_lang::base() . 'fees/show_all');
735 jsvitak
}
}
else
{
$form = arr::overwrite($form, $post->as_array());
$errors = arr::overwrite($errors, $post->errors('errors'));
}
}
1107 dzolo
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('fees/show_all', 'Fees',
$this->acl_check_view('Settings_Controller', 'fees'))
->text('Add new fee');
265 michalklim
735 jsvitak
// view for adding translation
461 jsvitak
$view = new View('main');
1137 dzolo
$view->title = __('Add new fee');
1107 dzolo
$view->breadcrumbs = $breadcrumbs->html();
265 michalklim
$view->content = new View('fees/add');
735 jsvitak
$view->content->name = $form['name'];
$view->content->fee = $form['fee'];
$view->content->from = $form['from'];
$view->content->to = $form['to'];
$view->content->type_id = $form['type_id'];
$view->content->errors = $errors;
265 michalklim
$view->content->days = $days;
735 jsvitak
$view->content->months = $months;
$view->content->years = $years;
$view->content->types = $enum_types;
1196 dzolo
$view->content->fee_model = isset($fee) && $fee->id ? $fee : NULL;
$view->content->fee_name = isset($fee_name) ? $fee_name : NULL;
265 michalklim
$view->render(TRUE);
735 jsvitak
}
258 michalklim
735 jsvitak
/**
1107 dzolo
* Edits fee
*
735 jsvitak
* @author Michal Kliment
1107 dzolo
* @param integer $fee_id
735 jsvitak
*/
1107 dzolo
public function edit($fee_id = NULL)
735 jsvitak
{
if ($fee_id)
{
1107 dzolo
// access control
if (!$this->acl_check_edit('Settings_Controller', 'fees'))
Controller::error(1);
258 michalklim
1107 dzolo
$fee = new Fee_Model($fee_id);
258 michalklim
1107 dzolo
// item is read only
if ($fee->readonly)
Controller::error(READONLY);
258 michalklim
1107 dzolo
$enum_type_name_model = new Enum_type_name_Model();
$enum_type_name = $enum_type_name_model->where('type_name', 'Fees types')->find();
791 michalklim
1107 dzolo
$enum_type_model = new Enum_type_Model();
$enum_types = $enum_type_model->get_values($enum_type_name->id);
791 michalklim
1107 dzolo
$this->fee_id = $fee->id;
258 michalklim
1107 dzolo
$form = array
(
'name' => $fee->name,
'fee' => $fee->fee,
'from' => array
(
'day' => (int) substr($fee->from, 8, 2),
'month' => (int) substr($fee->from, 5, 2),
'year' => (int) substr($fee->from, 0, 4)
),
'to' => array
(
'day' => (int) substr($fee->to, 8, 2),
'month' => (int) substr($fee->to, 5, 2),
'year' => (int) substr($fee->to, 0, 4)
),
'type_id' => $fee->type_id
);
258 michalklim
1107 dzolo
$errors = array
(
'fee' => '',
'from' => '',
'to' => '',
'type_id' => ''
);
258 michalklim

1107 dzolo
$days = array();
for ($i = 1; $i <= 31; $i++)
$days[$i] = $i;
258 michalklim
1107 dzolo
$months = array();
for ($i = 1; $i <= 12; $i++)
$months[$i] = $i;
265 michalklim
1107 dzolo
$years = array();
for ($i = date('Y') - 100; $i <= date('Y') + 100; $i++)
$years[$i] = $i;
265 michalklim

1107 dzolo
if ($_POST)
{
$post = new Validation($_POST);
$post->add_rules('fee', 'required', 'numeric');
265 michalklim
1107 dzolo
$post->add_rules('from', array('valid', 'date'));
$post->add_rules('to', array('valid', 'date'));
265 michalklim
1107 dzolo
// new system is not restricted on overlapping intervals in global scale
// overlapping will be checked individually with each member
$post->add_callbacks('type_id', array($this, 'valid_interval'));
265 michalklim
1107 dzolo
if ($post->validate())
735 jsvitak
{
1107 dzolo
$fee = new Fee_Model($fee_id);
$fee->type_id = $post->type_id;
$fee->name = $post->name;
$fee->fee = $post->fee;
$fee->from = date::round_month(
$post->from['day'],
$post->from['month'],
$post->from['year']
);
$fee->to = date::round_month(
$post->to['day'],
$post->to['month'],
$post->to['year']
);
// clears form content
unset($form_data);
// has fee been successfully saved?
if ($fee->save())
{
1195 dzolo
status::success('Fee has been successfully updated');
1107 dzolo
url::redirect(url_lang::base() . 'fees/show_all');
}
else
{
1195 dzolo
status::error('Error - cant edit fee.');
1107 dzolo
}
735 jsvitak
}
else
{
1107 dzolo
$form = arr::overwrite($form, $post->as_array());
$errors = arr::overwrite($errors, $post->errors('errors'));
735 jsvitak
}
}
791 michalklim
1107 dzolo
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('fees/show_all', 'Fees',
$this->acl_check_view('Settings_Controller', 'fees'))
->text($fee->name . ' (' . $fee->id . ')')
->text('Edit fee');

// view for adding translation
$view = new View('main');
1137 dzolo
$view->title = __('Edit fee');
1107 dzolo
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('fees/edit');
$view->content->fee_id = $fee_id;
$view->content->name = $form['name'];
$view->content->fee = $form['fee'];
$view->content->from = $form['from'];
$view->content->to = $form['to'];
$view->content->type_id = $form['type_id'];
$view->content->errors = $errors;
$view->content->days = $days;
$view->content->months = $months;
$view->content->years = $years;
$view->content->types = $enum_types;
$view->render(TRUE);
735 jsvitak
}
1107 dzolo
else
{
Controller::warning(1);
}
735 jsvitak
}
791 michalklim
735 jsvitak
/**
1107 dzolo
* Deletes fee
*
735 jsvitak
* @author Michal Kliment
1107 dzolo
* @param ineteger $fee_id
735 jsvitak
*/
1107 dzolo
public function delete($fee_id = NULL)
735 jsvitak
{
791 michalklim
// wrong parameter
if (!$fee_id || !is_numeric($fee_id))
Controller::warning(PARAMETER);
258 michalklim
791 michalklim
// access control
1107 dzolo
if (!$this->acl_check_delete('Settings_Controller', 'fees'))
791 michalklim
Controller::error(ACCESS);
258 michalklim
791 michalklim
$fee = new Fee_Model($fee_id);
258 michalklim
791 michalklim
// 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())
735 jsvitak
{
1195 dzolo
status::warning('Fee is used in some tariffs');
1107 dzolo
url::redirect(url_lang::base() . 'fees/show_all');
735 jsvitak
}
791 michalklim
// success
if ($fee->delete())
1107 dzolo
{
1195 dzolo
status::success('Fee has been successfully deleted');
1107 dzolo
}
791 michalklim
1107 dzolo
url::redirect(url_lang::base() . 'fees/show_all');
735 jsvitak
}
258 michalklim
735 jsvitak
/**
* Checks overlapping of fee validity intervals.
1107 dzolo
*
* @param Validation $post
735 jsvitak
*/
1107 dzolo
public function valid_interval(Validation $post)
735 jsvitak
{
265 michalklim
1107 dzolo
$from_date = date::round_month(
$post->from['day'], $post->from['month'], $post->from['year']
);

$to_date = date::round_month(
$post->to['day'], $post->to['month'], $post->to['year'], TRUE
);
265 michalklim
1107 dzolo
$diff = date::diff_month($to_date, $from_date);

735 jsvitak
if ($diff < -1)
{
$post->add_error('from', 'bigger');
return;
}
265 michalklim
735 jsvitak
if ($diff < 0)
{
$post->add_error('to', 'minimal');
return;
}
}
265 michalklim
791 michalklim
/**
* Callback function to show name of fee, in read-only fee show translated name
*
* @author Michal Kliment
1107 dzolo
* @param object $item
* @param string $name
791 michalklim
*/
public static function name_field($item, $name)
{
// fee is read-only
if ($item->readonly)
1107 dzolo
// show trasnslated name
1137 dzolo
echo __('' . $item->name);
791 michalklim
else
1107 dzolo
// show name
791 michalklim
echo $item->name;
}

258 michalklim
}