Improved quick entries.
[fa-stable.git] / gl / manage / gl_quick_entries.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 3;
13 $path_to_root="../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("Quick Entries"));
17
18 include($path_to_root . "/gl/includes/gl_db.inc");
19
20 include($path_to_root . "/includes/ui.inc");
21
22 simple_page_mode(true);
23 simple_page_mode2(true);
24
25 function simple_page_mode2($numeric_id = true)
26 {
27         global $Ajax, $Mode2, $selected_id2;
28
29         $default = $numeric_id ? -1 : '';
30         $selected_id2 = get_post('selected_id2', $default);
31         foreach (array('ADD_ITEM2', 'UPDATE_ITEM2', 'RESET2') as $m) {
32                 if (isset($_POST[$m])) {
33                         $Ajax->activate('_page_body');
34                         if ($m == 'RESET2') 
35                                 $selected_id2 = $default;
36                         $Mode2 = $m; return;
37                 }
38         }
39         foreach (array('BEd', 'BDel') as $m) {
40                 foreach ($_POST as $p => $pvar) {
41                         if (strpos($p, $m) === 0) {
42 //                              $selected_id2 = strtr(substr($p, strlen($m)), array('%2E'=>'.'));
43                                 unset($_POST['_focus']); // focus on first form entry
44                                 $selected_id2 = quoted_printable_decode(substr($p, strlen($m)));
45                                 $Ajax->activate('_page_body');
46                                 $Mode2 = $m;
47                                 return;
48                         }
49                 }
50         }
51         $Mode2 = '';
52 }
53
54 function submit_add_or_update_center2($add=true, $title=false, $async=false)
55 {
56         echo "<center>";
57         if ($add)
58                 submit('ADD_ITEM2', _("Add new"), true, $title, $async);
59         else {
60                 submit('UPDATE_ITEM2', _("Update"), true, $title, $async);
61                 submit('RESET2', _("Cancel"), true, $title, $async);
62         }
63         echo "</center>";
64 }
65
66 //-----------------------------------------------------------------------------------
67
68 function can_process() 
69 {
70
71         if (strlen($_POST['description']) == 0) 
72         {
73                 display_error( _("The Quick Entry description cannot be empty."));
74                 set_focus('description');
75                 return false;
76         }
77         if (strlen($_POST['base_desc']) == 0) 
78         {
79                 display_error( _("The base amount description cannot be empty."));
80                 set_focus('base_desc');
81                 return false;
82         }
83
84         return true;
85 }
86
87 //-----------------------------------------------------------------------------------
88
89 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
90 {
91
92         if (can_process())      
93         {       
94
95                 if ($selected_id != -1) 
96                 {
97                         update_quick_entry($selected_id, $_POST['description'], $_POST['type'],
98                                  input_num('base_amount'), $_POST['base_desc']);
99                         display_notification(_('Selected quick entry has been updated'));
100                 } 
101                 else 
102                 {
103                         add_quick_entry($_POST['description'], $_POST['type'], 
104                                 input_num('base_amount'), $_POST['base_desc']);
105                         display_notification(_('New quick entry has been added'));
106                 }
107                 $Mode = 'RESET';
108         }
109 }
110
111 if ($Mode2=='ADD_ITEM2' || $Mode2=='UPDATE_ITEM2') 
112 {
113         if ($selected_id2 != -1) 
114         {
115                 update_quick_entry_line($selected_id2, $selected_id, $_POST['actn'], $_POST['dest_id'], input_num('amount', 0), 
116                         $_POST['dimension_id'], $_POST['dimension2_id']);
117                 display_notification(_('Selected quick entry line has been updated'));
118         } 
119         else 
120         {
121                 add_quick_entry_line($selected_id, $_POST['actn'], $_POST['dest_id'], input_num('amount', 0), 
122                         $_POST['dimension_id'], $_POST['dimension2_id']);
123                 display_notification(_('New quick entry line has been added'));
124         }
125         $Mode2 = 'RESET2';
126 }
127
128 //-----------------------------------------------------------------------------------
129
130 if ($Mode == 'Delete')
131 {
132         if (!has_quick_entry_lines($selected_id))
133         {
134                 delete_quick_entry($selected_id);
135                 display_notification(_('Selected quick entry has been deleted'));
136                 $Mode = 'RESET';
137         }
138         else
139         {
140                 display_error( _("The Quick Entry has Quick Entry Lines. Cannot be deleted."));
141                 set_focus('description');
142         }
143 }
144
145 if ($Mode2 == 'BDel')
146 {
147         delete_quick_entry_line($selected_id2);
148         display_notification(_('Selected quick entry line has been deleted'));
149         $Mode2 = 'RESET2';
150 }
151 //-----------------------------------------------------------------------------------
152 if ($Mode == 'RESET')
153 {
154         $selected_id = -1;
155         $_POST['description'] = $_POST['type'] = '';
156         $_POST['base_desc']= _('Base Amount');
157         $_POST['base_amount'] = price_format(0);
158 }
159 if ($Mode2 == 'RESET2')
160 {
161         $selected_id2 = -1;
162         $_POST['actn'] = $_POST['dest_id'] = $_POST['amount'] = 
163                 $_POST['dimension_id'] = $_POST['dimension2_id'] = '';
164 }
165 //-----------------------------------------------------------------------------------
166
167 $result = get_quick_entries();
168 start_form();
169 start_table($table_style);
170 $th = array(_("Description"), _("Type"), "", "");
171 table_header($th);
172
173 $k = 0;
174 while ($myrow = db_fetch($result)) 
175 {
176         alt_table_row_color($k);
177         $type_text = $quick_entry_types[$myrow["type"]];
178         label_cell($myrow['description']);
179         label_cell($type_text);
180         edit_button_cell("Edit".$myrow["id"], _("Edit"));
181         delete_button_cell("Delete".$myrow["id"], _("Delete"));
182         end_row();
183 }
184
185 end_table(1);
186 end_form();
187 //-----------------------------------------------------------------------------------
188
189 start_form();
190
191 start_table($table_style2);
192
193 if ($selected_id != -1) 
194 {
195         //if ($Mode == 'Edit') 
196         //{
197                 //editing an existing status code
198                 $myrow = get_quick_entry($selected_id);
199
200                 $_POST['id']  = $myrow["id"];
201                 $_POST['description']  = $myrow["description"];
202                 $_POST['deposit']  = $myrow["type"];
203                 $_POST['base_desc']  = $myrow["base_desc"];
204                 $_POST['base_amount']  = price_format($myrow["base_amount"]);
205                 hidden('selected_id', $selected_id);
206         //}
207
208
209 text_row_ex(_("Description").':', 'description', 50, 60);
210
211 quick_entry_types_list_row(_("Entry Type").':', 'type');
212
213 text_row_ex(_("Base Amount Description").':', 'base_desc', 50, 60, '',_('Base Amount'));
214
215 amount_row(_("Default Base Amount").':', 'base_amount', price_format(0));
216
217 end_table(1);
218
219 submit_add_or_update_center($selected_id == -1, '', true);
220
221 end_form();
222
223
224 if ($selected_id != -1)
225 {
226         display_heading(_("Quick Entry Lines") . " - " . $_POST['description']);
227         $result = get_quick_entry_lines($selected_id);
228         start_form();
229         start_table($table_style2);
230         $dim = get_company_pref('use_dimension');
231         if ($dim == 2)
232                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), _("Dimension"), _("Dimension")." 2", "", "");
233         else if ($dim == 1)     
234                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), _("Dimension"), "", "");
235         else    
236                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), "", "");
237
238         table_header($th);
239         $k = 0;
240         while ($myrow = db_fetch($result)) 
241         {
242                 alt_table_row_color($k);
243                 
244                 label_cell($quick_actions[$myrow['action']]);
245
246                 $act_type = strtolower(substr($myrow['action'], 0, 1));
247
248                 if ($act_type == 't') {
249                         label_cells($myrow['tax_name'], '');
250                 } else {
251                         label_cell($myrow['dest_id'].' '.$myrow['account_name']);
252                         if ($act_type == '=') 
253                                 label_cell('');
254                         else
255                         if ($act_type == '%') 
256                                 percent_cell($myrow['amount']);
257                         else
258                                 amount_cell($myrow['amount']);
259                 }               
260                 if ($dim >= 1)
261                         label_cell(get_dimension_string($myrow['dimension_id'], true));
262                 if ($dim > 1)
263                         label_cell(get_dimension_string($myrow['dimension2_id'], true));
264                 edit_button_cell("BEd".$myrow["id"], _("Edit"));
265                 delete_button_cell("BDel".$myrow["id"], _("Delete"));
266                 end_row();
267         }
268         end_table(1);
269         hidden('selected_id', $selected_id);
270         hidden('selected_id2', $selected_id2);
271         hidden('description', $_POST['description']);
272         hidden('type', $_POST['type']);
273         end_form();
274         start_form();
275
276         div_start('edit_line');
277         start_table($table_style2);
278
279         if ($selected_id2 != -1) 
280         {
281                 if ($Mode2 == 'BEd') 
282                 {
283                         //editing an existing status code
284                         $myrow = get_quick_entry_line($selected_id2);
285
286                         $_POST['id']  = $myrow["id"];
287                         $_POST['dest_id']  = $myrow["dest_id"];
288                         $_POST['actn']  = $myrow["action"];
289                         $_POST['amount']  = $myrow["amount"];
290                         $_POST['dimension_id']  = $myrow["dimension_id"];
291                         $_POST['dimension2_id']  = $myrow["dimension2_id"];
292                 }
293         } 
294
295         quick_actions_list_row(_("Posted").":",'actn', null, true);
296         if (list_updated('actn'))
297                 $Ajax->activate('edit_line');
298
299         $actn = strtolower(substr($_POST['actn'],0,1));
300
301         if ($actn == 't') {
302                 item_tax_types_list_row(_("Item Tax Type").":",'dest_id', null);
303         } else {
304                 gl_all_accounts_list_row(_("Account").":", 'dest_id', null);
305                 if ($actn != '=') {
306                         if ($actn = '%') 
307                                 percent_row(_("Part").":", 'amount', percent_format(0));
308                         else
309                                 amount_row(_("Amount").":", 'amount', price_format(0));
310                 }
311         }
312         if ($dim >= 1) 
313                 dimensions_list_row(_("Dimension").":", 'dimension_id', null, true, " ", false, 1);
314         if ($dim > 1) 
315                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
316         
317         end_table(1);
318         if ($dim < 2)
319                 hidden('dimension2_id', 0);
320         if ($dim < 1)
321                 hidden('dimension_id', 0);
322         div_end();
323
324         hidden('selected_id', $selected_id);
325         hidden('selected_id2', $selected_id2);
326         hidden('description', $_POST['description']);
327         hidden('type', $_POST['type']);
328
329         submit_add_or_update_center2($selected_id2 == -1, '', true);
330
331         end_form();
332 }               
333 //------------------------------------------------------------------------------------
334
335 end_page();
336
337 ?>