Refactoring, moving plugin config options into config field.
[timetracker.git] / WEB-INF / templates / expense_edit.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 {* Conditional include of confirmSave handler. *}
38 {if $confirm_save}
39 var original_date = "{$entry_date}";
40
41 function confirmSave() {
42   var date_on_save = document.getElementById("date").value;
43   if (original_date != date_on_save) {
44     return confirm("{$i18n.warn.confirm_save}");
45   }
46 }
47 {/if}
48
49 // The fillProjectDropdown function populates the project combo box with
50 // projects associated with a selected client (client id is passed here as id).
51 function fillProjectDropdown(id) {
52   var str_ids = project_ids[id];
53   var dropdown = document.getElementById("project");
54   // Determine previously selected item.
55   var selected_item = dropdown.options[dropdown.selectedIndex].value;
56
57   // Remove existing content.
58   dropdown.length = 0;
59   // Add mandatory top option.
60   dropdown.options[0] = new Option(empty_label_project, '', true);
61
62   // Populate project dropdown.
63   if (!id) {
64     // If we are here, client is not selected.
65     var len = projects.length;
66     for (var i = 0; i < len; i++) {
67       dropdown.options[i+1] = new Option(projects[i][1], projects[i][0]);
68       if (dropdown.options[i+1].value == selected_item)
69         dropdown.options[i+1].selected = true;
70     }
71   } else if (str_ids) {
72     var ids = new Array();
73     ids = str_ids.split(",");
74     var len = ids.length;
75
76     for (var i = 0; i < len; i++) {
77       var p_id = ids[i];
78       dropdown.options[i+1] = new Option(project_names[p_id], p_id);
79       if (dropdown.options[i+1].value == selected_item)
80         dropdown.options[i+1].selected = true;
81     }
82   }
83 }
84
85 function get_date() {
86   var date = new Date();
87   return date.strftime("%Y-%m-%d");
88 }
89
90 // The recalculateCost function recalculates cost based on the current selection
91 // of predefined expense and quantity and also changes the comment accordingly.
92 function recalculateCost() {
93   var quantity_control = document.getElementById("quantity");
94   // Set quantity to 1 if it is not set already.
95   if (!quantity_control.value) {
96      quantity_control.value = "1";
97   }
98
99   var comment_control = document.getElementById("item_name");
100   var cost_control = document.getElementById("cost");
101   var replaceDecimalMark = ("." != "{$user->getDecimalMark()}");
102
103   // Calculate cost.
104   var dropdown = document.getElementById("predefined_expense");
105   if (dropdown.selectedIndex == 0) {
106     quantity_control.value = "";
107     comment_control.value = "";
108     cost_control.value = "";
109   } else {
110     comment_control.value = defined_expenses[dropdown.selectedIndex - 1][1] + " - " + quantity_control.value;
111     var quantity = quantity_control.value;
112     if (isNaN(quantity))
113       cost_control.value = "";
114     else {
115       var expenseCost = defined_expenses[dropdown.selectedIndex - 1][2];
116       if (replaceDecimalMark)
117         expenseCost = expenseCost.replace("{$user->getDecimalMark()}", ".");
118       var newCost = (quantity_control.value * expenseCost).toFixed(2);
119       if (replaceDecimalMark)
120         newCost = newCost.replace(".", "{$user->getDecimalMark()}");
121       cost_control.value = newCost;
122     }
123   }
124 }
125 </script>
126
127 {$forms.expenseItemForm.open}
128 <table cellspacing="4" cellpadding="7" border="0">
129 <tr>
130   <td>
131   <table width = "100%">
132   <tr>
133     <td valign="top">
134     <table border="0">
135 {if $user->isPluginEnabled('cl')}
136     <tr>
137       <td align="right">{$i18n.label.client} {if $user->isOptionEnabled('client_required')}(*){/if}:</td>
138       <td>{$forms.expenseItemForm.client.control}</td>
139     </tr>
140 {/if}
141 {if $show_project}
142     <tr>
143       <td align="right">{$i18n.label.project} (*):</td>
144       <td>{$forms.expenseItemForm.project.control}</td>
145     </tr>
146 {/if}
147 {if $predefined_expenses}
148     <tr>
149       <td align="right">{$i18n.label.expense}:</td>
150       <td>{$forms.expenseItemForm.predefined_expense.control}</td>
151     </tr>
152     <tr>
153       <td align="right">{$i18n.label.quantity}:</td>
154       <td>{$forms.expenseItemForm.quantity.control}</td>
155     </tr>
156 {/if}
157     <tr>
158       <td align="right">{$i18n.label.comment}:</td>
159       <td>{$forms.expenseItemForm.item_name.control}</td>
160     </tr>
161     <tr>
162       <td align="right">{$i18n.label.cost}:</td>
163       <td>{$forms.expenseItemForm.cost.control} {$user->getCurrency()|escape}</td>
164     </tr>
165 {if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))}
166     <tr>
167       <td align="right">&nbsp;</td>
168       <td><label>{$forms.expenseItemForm.paid.control}{$i18n.label.paid}</label></td>
169     </tr>
170 {/if}
171     <tr>
172       <td align="right">{$i18n.label.date}:</td>
173       <td>{$forms.expenseItemForm.date.control}</td>
174     </tr>
175     <tr>
176       <td colspan="2">&nbsp;</td>
177     </tr>
178     <tr>
179       <td></td>
180       <td align="left">{$forms.expenseItemForm.btn_save.control} {$forms.expenseItemForm.btn_copy.control} {$forms.expenseItemForm.btn_delete.control}</td>
181     </tr>
182     </table>
183     </td>
184     </tr>
185   </table>
186   </td>
187   </tr>
188 </table>
189 {$forms.expenseItemForm.close}