Cleanup of white space and comment improvement
[timetracker.git] / invoice_add.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('ttTeamHelper');
32 import('ttInvoiceHelper');
33
34 // Access check.
35 if (!ttAccessCheck(right_manage_team)) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 if ($request->getMethod() == 'POST') {
41   $cl_date = $request->getParameter('date');
42   $cl_client = $request->getParameter('client');
43   $cl_project = $request->getParameter('project');
44   $cl_number = trim($request->getParameter('number'));
45   $cl_start = $request->getParameter('start');
46   $cl_finish = $request->getParameter('finish');
47 }
48
49 $form = new Form('invoiceForm');
50 $form->addInput(array('type'=>'datefield','name'=>'date','size'=>'20','value'=>$cl_date));
51
52 // Dropdown for clients if the clients plugin is enabled.
53 if (in_array('cl', explode(',', $user->plugins))) {
54   $clients = ttTeamHelper::getActiveClients($user->team_id);
55   $form->addInput(array('type'=>'combobox','name'=>'client','style'=>'width: 250px;','data'=>$clients,'datakeys'=>array('id','name'),'value'=>$cl_client,'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
56 }
57 // Dropdown for projects.
58 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
59   $projects = ttTeamHelper::getActiveProjects($user->team_id);
60   $form->addInput(array('type'=>'combobox','name'=>'project','style'=>'width: 250px;','data'=>$projects,'datakeys'=>array('id','name'),'value'=>$cl_project,'empty'=>array(''=>$i18n->getKey('dropdown.all'))));
61 }
62 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'number','style'=>'width: 250px;','value'=>$cl_number));
63 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start','value'=>$cl_start));
64 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'finish','value'=>$cl_finish));
65 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.add')));
66
67 if ($request->getMethod() == 'POST') {
68   // Validate user input.
69   if (!ttValidString($cl_number)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('form.invoice.number'));
70   if (!ttValidDate($cl_date)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.date'));
71   if (!$cl_client) $errors->add($i18n->getKey('error.client'));
72   if (!ttValidDate($cl_start)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.start_date'));
73   if (!ttValidDate($cl_finish)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.end_date'));
74
75   $fields = array('date'=>$cl_date,'name'=>$cl_number,'client_id'=>$cl_client,'project_id'=>$cl_project,'start_date'=>$cl_start,'end_date'=>$cl_finish);
76   if ($errors->isEmpty()) {
77     if (ttInvoiceHelper::getInvoiceByName($cl_number))
78       $errors->add($i18n->getKey('error.invoice_exists'));
79
80     if (!ttInvoiceHelper::invoiceableItemsExist($fields))
81       $errors->add($i18n->getKey('error.no_invoiceable_items'));
82   }
83
84   if ($errors->isEmpty()) {
85     // Now we can go ahead and create our invoice.
86     if (ttInvoiceHelper::createInvoice($fields)) {
87       header('Location: invoices.php');
88       exit();
89     }
90       $errors->add($i18n->getKey('error.db'));
91   }
92 } // POST
93
94 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
95 $smarty->assign('onload', 'onLoad="document.invoiceForm.number.focus()"');
96 $smarty->assign('title', $i18n->getKey('title.add_invoice'));
97 $smarty->assign('content_page_name', 'invoice_add.tpl');
98 $smarty->display('index.tpl');