Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c1bdc1c4

Přidáno uživatelem Michal Kliment před více než 9 roky(ů)

Release 1.1.0

Zobrazit rozdíly:

application/controllers/invoice_items.php
*/
class Invoice_items_Controller extends Controller
{
/**
* Constructor, only test if finance is enabled
*/
public function __construct()
{
parent::__construct();
if (!Settings::get('finance_enabled'))
Controller::error (ACCESS);
}
/**
* Index redirects to invoices show all
*/
......
*
* @author Michal Kliment
* @param integer $invoice_id id of invoice which belongs to added items
* @param integer $item_count count of items to addd
*/
public function add($invoice_id = NULL, $item_count = 1)
public function add($invoice_id = NULL)
{
// access rights
if (!$this->acl_check_new('Accounts_Controller', 'invoices'))
Controller::Error(ACCESS);
$invoice_model = new Invoice_Model();
$invoices = $invoice_model->where('id', $invoice_id)->find_all();
// if invoice doesn't exist redirects to list of all invoices
if (!count($invoices))
url::redirect('invoices/show_all');
// transforms array of objects to classic array
$arr_invoices = arr::from_objects($invoices, 'invoice_nr');
$invoice_items = $this->session->get('ssInvoice_items');
if (!$invoice_id)
{
$invoice_model = new Invoice_Model();
$arr_invoices = $invoice_model->select_list('id', 'invoice_nr');
}
else
{
if (!is_numeric($invoice_id))
Controller::warning (PARAMETER);
$invoice = new Invoice_Model($invoice_id);
if (!$invoice->id)
Controller::error (RECORD);
$arr_invoices = array($invoice->id => $invoice->invoice_nr);
}
// creates form
$this->form = new Forge(
'invoice_items/add/' . $invoice_id . '/' . $item_count
);
$this->form = new Forge();
$this->form->group('Basic information');
......
->options($arr_invoices)
->rules('required')
->selected($invoice_id);
// creates first, only one required,
$this->form->group('')
->label(__('Item number') . ' 1');
$this->form->input('name_0')
->label(__('Name') . ':')
$this->form->input('name')
->rules('required|length[3,40]');
$this->form->input('code_0')
->label(__('Code') . ':')
$this->form->input('code')
->rules('required|length[3,40]');
$this->form->input('quantity_0')
->label(__('Quantity') . ':')
$this->form->input('quantity')
->rules('required|length[1,40]|valid_numeric');
$this->form->input('author_fee_0')
->label(__('Author fee') . ':')
$this->form->input('author_fee')
->rules('length[1,40]|valid_numeric');
$this->form->input('contractual_increase_0')
->label(__('Contractual increase') . ':')
$this->form->input('contractual_increase')
->rules('length[1,40]|valid_numeric');
$this->form->radio('service_0')
->label(__('Service') . ':')
$this->form->radio('service')
->options(arr::bool())
->default(0);
$this->form->input('price_0')
->label(__('Price') . ':')
->rules('length[1,40]|valid_numeric');
$this->form->input('price')
->rules('required|length[1,40]|valid_numeric');
$this->form->input('price_vat_0')
->label(__('Price vat') . ':')
->rules('length[1,40]|valid_numeric')
->callback(array($this, 'valid_prices'));
// next, only optional, with javascript func
for ($i = 1; $i < $item_count; $i++)
{
$this->form->group('')
->label(__('Item number') . ' ' .
($i + 1) . ' - ' . __('optional'));
$this->form->checkbox('ignore_' . $i . '')
->label(__('Ignore'))
->value('1')
->checked(FALSE)
->onclick('ignore = document.getElementById(\'ignore_' . $i .
'\').checked; document.getElementById(\'name_' . $i .
'\').disabled = ignore; document.getElementById(\'code_' . $i .
'\').disabled = ignore; document.getElementById(\'quantity_' . $i .
'\').disabled = ignore; document.getElementById(\'author_fee_' . $i .
'\').disabled = ignore; document.getElementById(\'contractual_increase_' . $i .
'\').disabled = ignore; document.getElementById(\'price_' . $i .
'\').disabled = ignore; document.getElementById(\'price_vat_' . $i .
'\').disabled = ignore;'
);
$this->form->input('name_' . $i . '')
->label(__('Name') . ':')
->rules('length[3,40]')
->callback(array($this, 'valid_name'))
->onKeyUp('document.getElementById(\'ignore_' . $i .
'\').checked = false;'
);
$this->form->input('code_' . $i . '')
->label(__('Code') . ':')
->rules('length[3,40]')
->callback(array($this, 'valid_code'))
->onKeyUp('document.getElementById(\'ignore_' . $i .
'\').checked = false;'
);
$this->form->input('quantity_' . $i . '')
->label(__('Quantity') . ':')
->rules('length[1,40]|valid_numeric')
->callback(array($this, 'valid_quantity'))
->onKeyUp('document.getElementById(\'ignore_' . $i .
'\').checked = false;'
);
$this->form->input('author_fee_' . $i . '')
->label(__('Author fee') . ':')
->rules('length[1,40]|valid_numeric')
->onKeyUp('document.getElementById(\'ignore_' . $i .
'\').checked = false;'
);
$this->form->input('contractual_increase_' . $i . '')
->label(__('Contractual increase') . ':')
->rules('length[1,40]|valid_numeric')
->onKeyUp('document.getElementById(\'ignore_' . $i .
'\').checked = false;'
);
$this->form->radio('service_' . $i . '')
->label(__('Service') . ':')
->options(arr::bool())
->default(0)
->onKeyUp('document.getElementById(\'ignore_' . $i .
'\').checked = false;'
);
$this->form->input('price_' . $i . '')
->label(__('Price') . ':')
->rules('length[1,40]|valid_numeric')
->onKeyUp('document.getElementById(\'ignore_' . $i .
'\').checked = false;'
);
$this->form->input('price_vat_' . $i . '')
->label(__('Price vat') . ':')
->rules('length[1,40]|valid_numeric')
->callback(array($this, 'valid_prices'))
->onKeyUp('document.getElementById(\'ignore_' . $i .
'\').checked = false;'
);
}
$this->form->input('vat')
->label('tax rate')
->rules('length[1,5]|valid_range|valid_numeric')
->value(0);
$this->form->submit('Add');
......
if ($this->form->validate())
{
$form_data = $this->form->as_array();
// counter of successfully saved items
$count_saved = 0;
for ($i = 0; $i < $item_count; $i++)
{
// if (ignore checkbox has been posted AND
// it has not been checked (optional items))
// OR inore checkbox has not been posted (first, required item)
if ((isset($form_data['ignore_' . $i]) &&
!$form_data['ignore_' . $i]) ||
!isset($form_data['ignore_' . $i]))
{
// creates new invoice item
$ii = new Invoice_item_Model();
$ii->invoice_id = $form_data['invoice_id'];
$ii->name = $form_data['name_' . $i];
$ii->code = $form_data['code_' . $i];
$ii->quantity = (double) $form_data['quantity_' . $i];
$ii->author_fee = (double) $form_data['author_fee_' . $i];
$ii->contractual_increase = (double) $form_data['contractual_increase_' . $i];
$ii->service = $form_data['service_' . $i];
$ii->price = (double) $form_data['price_' . $i];
$ii->price_vat = (double) $form_data['price_vat_' . $i];
// if price/price_vat is null, counts it from price_vat/price
if (!$ii->price || !$ii->price_vat)
{
$invoice = new Invoice_Model($invoice_id);
$vat = ($invoice->vat / 100) + 1;
if (!$ii->price_vat)
{
$ii->price_vat = round(
$ii->price * $vat, 2
);
}
else
{
$ii->price = round(
$ii->price_vat / $vat, 2
);
}
}
// success
if ($ii->save())
$count_saved++;
}
}
// succes
if ($count_saved)
// creates new invoice item
$invoice_item = new Invoice_item_Model();
$invoice_item->invoice_id = $form_data['invoice_id'];
$invoice_item->name = $form_data['name'];
$invoice_item->code = $form_data['code'];
$invoice_item->quantity = (double) $form_data['quantity'];
$invoice_item->author_fee = (double) $form_data['author_fee'];
$invoice_item->contractual_increase = (double) $form_data['contractual_increase'];
$invoice_item->service = $form_data['service'];
$invoice_item->price = (double) $form_data['price'];
$invoice_item->vat = (double) !empty($form_data['vat']) ? $form_data['vat'] / 100 : 0;
// success
if ($invoice_item->save())
{
status::success('Item(s) have been successfully added.');
status::success('Invoice item has been successfully added');
}
url::redirect('invoices/show/' . $invoice_id);
$this->redirect('invoices/show/' . $invoice_id);
} else {
// breadcrumbs
$breadcrumbs = breadcrumbs::add()
->link('invoices/show_all', 'Invoices',
$this->acl_check_view('Accounts_Controller', 'invoices'))
->link('invoices/show/' . $invoice_id,
__('Invoice') . ' (' . $invoice_id . ')',
$this->acl_check_view('Accounts_Controller', 'invoices'))
->text('Add new invoice item');
$view = new View('main');
$view->title = __('Add new invoice item');
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('form');
$view->content->headline = __('Add new invoice item');
$view->content->link_back = '';
$view->content->form = $this->form->html();
$view->render(TRUE);
}
// breadcrumbs
$breadcrumbs = breadcrumbs::add()
->link('invoices/show_all', 'Invoices',
$this->acl_check_view('Accounts_Controller', 'invoices'))
->link('invoices/show/' . $invoice_id,
__('Invoice') . ' (' . $invoice_id . ')',
$this->acl_check_view('Accounts_Controller', 'invoices'))
->text('Add new invoice item');
$view = new View('main');
$view->title = __('Add new invoice item');
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('form');
$view->content->headline = __('Add new invoice item');
$view->content->link_back = '';
$view->content->form = $this->form->html();
$view->render(TRUE);
}
/**
......
if (!$invoice_item_id || !$invoice_item->id)
url::redirect('invoices');
$invoice_model = new Invoice_Model($invoice_item->invoice_id);
$arr_invoices[] = $invoice_model->invoice_nr;
if ($this->session->get('ssInvoice_item_id'))
{
$link_back = html::anchor(
'invoice_items/show/' . $this->session->get('ssInvoice_item_id'),
__('Back to the invoice item')
);
}
else
{
$link_back = html::anchor(
'invoices/show/' . $invoice_item->invoice_id,
__('Back to the invoice')
);
}
$invoice_model = new Invoice_Model();
$arr_invoices = $invoice_model->select_list('id', 'invoice_nr');
// creates form
$this->form = new Forge('invoice_items/edit/' . $invoice_item_id);
$this->form = new Forge();
$this->form->group('Basic informations');
$this->form->group('Basic information');
$this->form->dropdown('invoice_id')
->label(__('Invoice number') . ':')
->options($arr_invoices)
->rules('required')
->options($arr_invoices)
->selected($invoice_item->invoice_id);
$this->form->input('name')
......
->default($invoice_item->service);
$this->form->input('price')
->rules('required|length[3,40]|valid_numeric')
->rules('required|length[1,40]|valid_numeric')
->value($invoice_item->price);
$this->form->input('price_vat')
->rules('required|length[3,40]|valid_numeric')
->value($invoice_item->price_vat);
$this->form->input('vat')
->label(__('Tax rate') . ':')
->rules('length[1,5]|valid_range|valid_numeric')
->value($invoice_item->vat * 100);
$this->form->submit('Edit');
......
$invoice_item->contractual_increase = (double) $form_data['contractual_increase'];
$invoice_item->service = $form_data['service'];
$invoice_item->price = (double) $form_data['price'];
$invoice_item->price_vat = (double) $form_data['price_vat'];
$invoice_item->vat = (double) !empty($form_data['vat']) ? $form_data['vat'] / 100 : 0;
// succes
if ($invoice_item->save())
......
}
}
/**
* Validator callback
*
* @param object $input
*/
public function valid_prices($input = NULL)
{
// validators cannot be accessed
if (empty($input) || !is_object($input))
{
self::error(PAGE);
}
$method = $input->method;
$ignore_name = str_replace('price_vat_', 'ignore_', $input->name);
$price_name = str_replace('price_vat_', 'price_', $input->name);
if (!$this->input->$method($ignore_name) &&
!$this->input->$method($price_name) &&
!$input->value)
{
$input->add_error('required', __('Fill in at least one from prices.'));
}
}
}

Také k dispozici: Unified diff