df0eb4d124652ff96571699671411d15c9e854e2
[timetracker.git] / WEB-INF / templates / expenses.tpl
1 <script>
2 // We need a few arrays to populate project dropdown.
3 // When client selection changes, the project dropdown must be re-populated with only relevant projects.
4 // Format:
5 // project_ids[143] = "325,370,390,400";  // Comma-separated list of project ids for client.
6 // project_names[325] = "Time Tracker";   // Project name.
7
8 // Prepare an array of project ids for clients.
9 var project_ids = new Array();
10 {foreach $client_list as $client}
11   project_ids[{$client.id}] = "{$client.projects}";
12 {/foreach}
13 // Prepare an array of project names.
14 var project_names = new Array();
15 {foreach $project_list as $project}
16   project_names[{$project.id}] = "{$project.name|escape:'javascript'}";
17 {/foreach}
18 // We'll use this array to populate project dropdown when client is not selected.
19 var idx = 0;
20 var projects = new Array();
21 {foreach $project_list as $project}
22   projects[idx] = new Array("{$project.id}", "{$project.name|escape:'javascript'}");
23   idx++;
24 {/foreach}
25
26 // Mandatory top option for project dropdown.
27 var empty_label_project = "{$i18n.dropdown.select|escape:'javascript'}";
28
29 // Prepare an array of predefined expenses.
30 idx = 0;
31 var defined_expenses = new Array();
32 {foreach $predefined_expenses as $predefined_expense}
33   defined_expenses[idx] = new Array("{$predefined_expense.id}", "{$predefined_expense.name|escape:'javascript'}", "{$predefined_expense.cost}");
34   idx++;
35 {/foreach}
36
37 // The fillProjectDropdown function populates the project combo box with
38 // projects associated with a selected client (client id is passed here as id).
39 function fillProjectDropdown(id) {
40   var str_ids = project_ids[id];
41   var dropdown = document.getElementById("project");
42   // Determine previously selected item.
43   var selected_item = dropdown.options[dropdown.selectedIndex].value;
44
45   // Remove existing content.
46   dropdown.length = 0;
47   // Add mandatory top option.
48   dropdown.options[0] = new Option(empty_label_project, '', true);
49
50   // Populate project dropdown.
51   if (!id) {
52     // If we are here, client is not selected.
53     var len = projects.length;
54     for (var i = 0; i < len; i++) {
55       dropdown.options[i+1] = new Option(projects[i][1], projects[i][0]);
56       if (dropdown.options[i+1].value == selected_item)
57         dropdown.options[i+1].selected = true;
58     }
59   } else if (str_ids) {
60     var ids = new Array();
61     ids = str_ids.split(",");
62     var len = ids.length;
63
64     for (var i = 0; i < len; i++) {
65       var p_id = ids[i];
66       dropdown.options[i+1] = new Option(project_names[p_id], p_id);
67       if (dropdown.options[i+1].value == selected_item)
68         dropdown.options[i+1].selected = true;
69     }
70   }
71 }
72
73 function get_date() {
74   var date = new Date();
75   return date.strftime("%Y-%m-%d");
76 }
77
78 // The recalculateCost function recalculates cost based on the current selection
79 // of predefined expense and quantity and also changes the comment accordingly.
80 function recalculateCost() {
81   var quantity_control = document.getElementById("quantity");
82   // Set quantity to 1 if it is not set already.
83   if (!quantity_control.value) {
84      quantity_control.value = "1";
85   }
86
87   var comment_control = document.getElementById("item_name");
88   var cost_control = document.getElementById("cost");
89   var replaceDecimalMark = ("." != "{$user->getDecimalMark()}");
90
91   // Calculate cost.
92   var dropdown = document.getElementById("predefined_expense");
93   if (dropdown.selectedIndex == 0) {
94     quantity_control.value = "";
95     comment_control.value = "";
96     cost_control.value = "";
97   } else {
98     comment_control.value = defined_expenses[dropdown.selectedIndex - 1][1] + " - " + quantity_control.value;
99     var quantity = quantity_control.value;
100     if (isNaN(quantity))
101       cost_control.value = "";
102     else {
103       var expenseCost = defined_expenses[dropdown.selectedIndex - 1][2];
104       if (replaceDecimalMark)
105         expenseCost = expenseCost.replace("{$user->getDecimalMark()}", ".");
106       var newCost = (quantity_control.value * expenseCost).toFixed(2);
107       if (replaceDecimalMark)
108         newCost = newCost.replace(".", "{$user->getDecimalMark()}");
109       cost_control.value = newCost;
110     }
111   }
112 }
113 </script>
114
115 {$forms.expensesForm.open}
116 <table cellspacing="4" cellpadding="0" border="0">
117   <tr>
118     <td valign="top">
119       <table>
120 {if $user_dropdown}
121         <tr>
122           <td align="right">{$i18n.label.user}:</td>
123           <td>{$forms.expensesForm.user.control}</td>
124         </tr>
125 {/if}
126 {if $user->isPluginEnabled('cl')}
127         <tr>
128           <td align="right">{$i18n.label.client}{if $user->isPluginEnabled('cm')} (*){/if}:</td>
129           <td>{$forms.expensesForm.client.control}</td>
130         </tr>
131 {/if}
132 {if $show_project}
133         <tr>
134           <td align="right">{$i18n.label.project} (*):</td>
135           <td>{$forms.expensesForm.project.control}</td>
136         </tr>
137 {/if}
138 {if $predefined_expenses}
139         <tr>
140           <td align="right">{$i18n.label.expense}:</td>
141           <td>{$forms.expensesForm.predefined_expense.control}</td>
142         </tr>
143         <tr>
144           <td align="right">{$i18n.label.quantity}:</td>
145           <td>{$forms.expensesForm.quantity.control}</td>
146         </tr>
147 {/if}
148         <tr>
149           <td align="right">{$i18n.label.comment} (*):</td>
150           <td>{$forms.expensesForm.item_name.control}</td>
151         </tr>
152         <tr>
153           <td align="right">{$i18n.label.cost} (*):</td>
154           <td>{$forms.expensesForm.cost.control} {$user->getCurrency()|escape}</td>
155         </tr>
156       </table>
157     </td>
158     <td valign="top">
159       <table>
160         <tr><td>{$forms.expensesForm.date.control}</td></tr>
161       </table>
162     </td>
163   </tr>
164 </table>
165
166 <table>
167   <tr>
168     <td align="center" colspan="2">{$forms.expensesForm.btn_submit.control}</td>
169   </tr>
170 </table>
171
172 <table width="720">
173 <tr>
174   <td valign="top">
175 {if $expense_items}
176       <table border="0" cellpadding="3" cellspacing="1" width="100%">
177       <tr>
178   {if $user->isPluginEnabled('cl')}
179         <td class="tableHeader">{$i18n.label.client}</td>
180   {/if}
181   {if $show_project}
182         <td class="tableHeader">{$i18n.label.project}</td>
183   {/if}
184         <td class="tableHeader">{$i18n.label.item}</td>
185         <td width="5%" class="tableHeaderCentered">{$i18n.label.cost}</td>
186         <td></td>
187         <td></td>
188       </tr>
189   {foreach $expense_items as $item}
190       <tr bgcolor="{cycle values="#f5f5f5,#ffffff"}">
191     {if $user->isPluginEnabled('cl')}
192         <td valign="top">{$item.client|escape}</td>
193     {/if}
194     {if $show_project}
195         <td valign="top">{$item.project|escape}</td>
196     {/if}
197         <td valign="top">{$item.item|escape}</td>
198         <td valign="top" align="right">{$item.cost}</td>
199         <td valign="top" align="center">
200     {if $item.approved || $item.invoice_id}
201           &nbsp;
202     {else}
203           <a href='expense_edit.php?id={$item.id}'><img class="table_icon" alt="{$i18n.label.edit}" src="images/icon_edit.png"></a>
204     {/if}
205         </td>
206         <td valign="top" align="center">
207     {if $item.approved || $item.invoice_id}
208           &nbsp;
209     {else}
210           <a href='expense_delete.php?id={$item.id}'><img class="table_icon" alt="{$i18n.label.delete}" src="images/icon_delete.png"></a>
211     {/if}
212         </td>
213       </tr>
214   {/foreach}
215     </table>
216     <table border="0" cellpadding="3" cellspacing="1" width="100%">
217       <tr>
218         <td nowrap align="right">{$i18n.label.day_total}: {$user->getCurrency()|escape} {$day_total}</td>
219       </tr>
220     </table>
221 {/if}
222   </td>
223 </tr>
224 </table>
225 {$forms.expensesForm.close}