2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('initialize.php');
31 import('ttTeamHelper');
32 import('ttGroupHelper');
33 import('ttInvoiceHelper');
36 if (!ttAccessAllowed('manage_invoices')) {
37 header('Location: access_denied.php');
40 if (!$user->isPluginEnabled('iv')) {
41 header('Location: feature_disabled.php');
44 // End of access checks.
46 if ($request->isPost()) {
47 $cl_date = $request->getParameter('date');
48 $cl_client = $request->getParameter('client');
49 $cl_project = $request->getParameter('project');
50 $cl_number = trim($request->getParameter('number'));
51 $cl_start = $request->getParameter('start');
52 $cl_finish = $request->getParameter('finish');
55 $form = new Form('invoiceForm');
56 $form->addInput(array('type'=>'datefield','name'=>'date','size'=>'20','value'=>$cl_date));
58 // Dropdown for clients if the clients plugin is enabled.
59 if ($user->isPluginEnabled('cl')) {
60 $clients = ttGroupHelper::getActiveClients();
61 $form->addInput(array('type'=>'combobox','name'=>'client','style'=>'width: 250px;','data'=>$clients,'datakeys'=>array('id','name'),'value'=>$cl_client,'empty'=>array(''=>$i18n->get('dropdown.select'))));
63 // Dropdown for projects.
64 $show_project = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
66 $projects = ttGroupHelper::getActiveProjects();
67 $form->addInput(array('type'=>'combobox','name'=>'project','style'=>'width: 250px;','data'=>$projects,'datakeys'=>array('id','name'),'value'=>$cl_project,'empty'=>array(''=>$i18n->get('dropdown.all'))));
69 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'number','style'=>'width: 250px;','value'=>$cl_number));
70 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start','value'=>$cl_start));
71 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'finish','value'=>$cl_finish));
72 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.add')));
74 if ($request->isPost()) {
75 // Validate user input.
76 if (!ttValidString($cl_number)) $err->add($i18n->get('error.field'), $i18n->get('form.invoice.number'));
77 if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
78 if (!$cl_client) $err->add($i18n->get('error.client'));
79 if (!ttValidDate($cl_start)) $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
80 if (!ttValidDate($cl_finish)) $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
82 $fields = array('date'=>$cl_date,'name'=>$cl_number,'client_id'=>$cl_client,'project_id'=>$cl_project,'start_date'=>$cl_start,'end_date'=>$cl_finish);
84 if (ttInvoiceHelper::getInvoiceByName($cl_number))
85 $err->add($i18n->get('error.invoice_exists'));
87 if (!ttInvoiceHelper::invoiceableItemsExist($fields))
88 $err->add($i18n->get('error.no_invoiceable_items'));
92 // Now we can go ahead and create our invoice.
93 if (ttInvoiceHelper::createInvoice($fields)) {
94 header('Location: invoices.php');
97 $err->add($i18n->get('error.db'));
102 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
103 $smarty->assign('onload', 'onLoad="document.invoiceForm.number.focus()"');
104 $smarty->assign('show_project', $show_project);
105 $smarty->assign('title', $i18n->get('title.add_invoice'));
106 $smarty->assign('content_page_name', 'invoice_add.tpl');
107 $smarty->display('index.tpl');