Quick Entries: fixed false warning.
[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 General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         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/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_QUICKENTRY';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "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         $bal_type = get_post('bal_type');
78         if ($bal_type == 1 && $_POST['type'] != QE_JOURNAL)
79         {
80                 display_error( _("You can only use Balance Based together with Journal Entries."));
81                 set_focus('base_desc');
82                 return false;
83         }
84         if (!$bal_type && strlen($_POST['base_desc']) == 0) 
85         {
86                 display_error( _("The base amount description cannot be empty."));
87                 set_focus('base_desc');
88                 return false;
89         }
90
91         return true;
92 }
93
94 //-----------------------------------------------------------------------------------
95
96 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
97 {
98
99         if (can_process())      
100         {       
101
102                 if ($selected_id != -1) 
103                 {
104                         update_quick_entry($selected_id, $_POST['description'], $_POST['type'],
105                                  input_num('base_amount'), $_POST['base_desc'], get_post('bal_type', 0), $_POST['usage']);
106                         display_notification(_('Selected quick entry has been updated'));
107                 } 
108                 else 
109                 {
110                         add_quick_entry($_POST['description'], $_POST['type'], 
111                                 input_num('base_amount'), $_POST['base_desc'], get_post('bal_type', 0), $_POST['usage']);
112                         display_notification(_('New quick entry has been added'));
113                 }
114                 $Mode = 'RESET';
115         }
116 }
117
118 if ($Mode2=='ADD_ITEM2' || $Mode2=='UPDATE_ITEM2') 
119 {
120         if (!get_post('dest_id')) {
121                 display_error(_("You must select GL account."));
122                 set_focus('dest_id');
123         }
124         elseif ($selected_id2 != -1) 
125         {
126                 update_quick_entry_line($selected_id2, $selected_id, $_POST['actn'], $_POST['dest_id'], input_num('amount', 0), 
127                         $_POST['dimension_id'], $_POST['dimension2_id'], get_post('memo'));
128                 display_notification(_('Selected quick entry line has been updated'));
129         } 
130         else 
131         {
132                 add_quick_entry_line($selected_id, $_POST['actn'], $_POST['dest_id'], input_num('amount', 0), 
133                         $_POST['dimension_id'], $_POST['dimension2_id'], get_post('memo'));
134                 display_notification(_('New quick entry line has been added'));
135         }
136         $Mode2 = 'RESET2';
137 }
138
139 //-----------------------------------------------------------------------------------
140
141 if ($Mode == 'Delete')
142 {
143         if (!has_quick_entry_lines($selected_id))
144         {
145                 delete_quick_entry($selected_id);
146                 display_notification(_('Selected quick entry has been deleted'));
147                 $Mode = 'RESET';
148         }
149         else
150         {
151                 display_error( _("The Quick Entry has Quick Entry Lines. Cannot be deleted."));
152                 set_focus('description');
153         }
154 }
155
156 if (find_submit('Edit') != -1) {
157         $Mode2 = 'RESET2';
158         set_focus('description');
159 }
160 if (find_submit('BEd') != -1 || get_post('ADD_ITEM2')) {
161         set_focus('actn');
162 }
163
164 if ($Mode2 == 'BDel')
165 {
166         delete_quick_entry_line($selected_id2);
167         display_notification(_('Selected quick entry line has been deleted'));
168         $Mode2 = 'RESET2';
169 }
170 //-----------------------------------------------------------------------------------
171 if ($Mode == 'RESET')
172 {
173         $selected_id = -1;
174         $_POST['description'] = $_POST['type'] = $_POST['usage'] = '';
175         $_POST['base_desc']= _('Base Amount');
176         $_POST['base_amount'] = price_format(0);
177         $_POST['bal_type'] = 0;
178 }
179 if ($Mode2 == 'RESET2')
180 {
181         $selected_id2 = -1;
182         $_POST['actn'] = $_POST['dest_id'] = $_POST['amount'] = 
183                 $_POST['dimension_id'] = $_POST['dimension2_id'] = '';
184 }
185 //-----------------------------------------------------------------------------------
186
187 $result = get_quick_entries();
188 start_form();
189 start_table(TABLESTYLE);
190 $th = array(_("Description"), _("Type"), _("Usage"),  "", "");
191 table_header($th);
192
193 $k = 0;
194 while ($myrow = db_fetch($result)) 
195 {
196         alt_table_row_color($k);
197         $type_text = $quick_entry_types[$myrow["type"]];
198         label_cell($myrow['description']);
199         label_cell($type_text);
200         label_cell($myrow['usage']);
201         edit_button_cell("Edit".$myrow["id"], _("Edit"));
202         delete_button_cell("Delete".$myrow["id"], _("Delete"));
203         end_row();
204 }
205
206 end_table(1);
207 //-----------------------------------------------------------------------------------
208
209 div_start('qe');
210 start_table(TABLESTYLE2);
211
212 if ($selected_id != -1) 
213 {
214         if ($Mode == 'Edit') // changed by Joe 2010-11-09
215         {
216                 $myrow = get_quick_entry($selected_id);
217
218                 $_POST['id']  = $myrow["id"];
219                 $_POST['description']  = $myrow["description"];
220                 $_POST['type']  = $myrow["type"];
221                 $_POST['base_desc']  = $myrow["base_desc"];
222                 $_POST['usage']  = $myrow["usage"];
223                 $_POST['bal_type']  = $myrow["bal_type"];
224                 $_POST['base_amount']  = $myrow["bal_type"] ?
225                         $myrow["base_amount"] : price_format($myrow["base_amount"]);
226         }       
227         hidden('selected_id', $selected_id);
228
229
230 text_row_ex(_("Description").':', 'description', 50, 60);
231 text_row_ex(_("Usage").':', 'usage', 80, 120);
232
233 quick_entry_types_list_row(_("Entry Type").':', 'type', null, true);
234
235 if (get_post('type') == QE_JOURNAL)
236 {
237         yesno_list_row(_("Balance Based"), 'bal_type', null, _("Yes"), _("No"), true);
238 }       
239
240 if (list_updated('bal_type') || list_updated('type'))
241 {
242         $Ajax->activate('qe');
243 }
244
245 if (get_post('type') == QE_JOURNAL && get_post('bal_type') == 1)
246 {
247         yesno_list_row(_("Period"), 'base_amount', null, _("Monthly"), _("Yearly"));
248         gl_all_accounts_list_row(_("Account"), 'base_desc', null, true);
249 }
250 else
251 {
252         text_row_ex(_("Base Amount Description").':', 'base_desc', 50, 60, '');
253         amount_row(_("Default Base Amount").':', 'base_amount', price_format(0));
254 }
255 end_table(1);
256 submit_add_or_update_center($selected_id == -1, '', 'both');
257 div_end();
258
259
260 if ($selected_id != -1)
261 {
262         display_heading(_("Quick Entry Lines") . " - " . $_POST['description']);
263         $result = get_quick_entry_lines($selected_id);
264
265         start_table(TABLESTYLE2);
266         $dim = get_company_pref('use_dimension');
267         if ($dim == 2)
268                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), _("Memo"), _("Dimension"), _("Dimension")." 2", "", "");
269         elseif ($dim == 1)      
270                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), _("Memo"), _("Dimension"), "", "");
271         else    
272                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), _("Memo"), "", "");
273
274         table_header($th);
275         $k = 0;
276         while ($myrow = db_fetch($result)) 
277         {
278                 alt_table_row_color($k);
279                 
280                 label_cell($quick_actions[$myrow['action']]);
281
282                 $act_type = strtolower(substr($myrow['action'], 0, 1));
283
284                 if ($act_type == 't') 
285                 {
286                         label_cells($myrow['tax_name'], '');
287                 } 
288                 else 
289                 {
290                         label_cell($myrow['dest_id'].' '.$myrow['account_name']);
291                         if ($act_type == '=') 
292                                 label_cell('');
293                         elseif ($act_type == '%') 
294                                 label_cell(number_format2($myrow['amount'], user_exrate_dec()), "nowrap align=right ");
295                         else
296                                 amount_cell($myrow['amount']);
297                 }
298                 label_cell($myrow['memo']);
299                 if ($dim >= 1)
300                         label_cell(get_dimension_string($myrow['dimension_id'], true));
301                 if ($dim > 1)
302                         label_cell(get_dimension_string($myrow['dimension2_id'], true));
303                 edit_button_cell("BEd".$myrow["id"], _("Edit"));
304                 delete_button_cell("BDel".$myrow["id"], _("Delete"));
305                 end_row();
306         }
307         end_table(1);
308
309         div_start('edit_line');
310         start_table(TABLESTYLE2);
311
312         if ($selected_id2 != -1) 
313         {
314                 if ($Mode2 == 'BEd') 
315                 {
316                         //editing an existing status code
317                         $myrow = get_quick_entry_line($selected_id2);
318
319                         $_POST['id']  = $myrow["id"];
320                         $_POST['dest_id']  = $myrow["dest_id"];
321                         $_POST['actn']  = $myrow["action"];
322                         $_POST['amount']  = $myrow["amount"];
323                         $_POST['memo']  = $myrow["memo"];
324                         $_POST['dimension_id']  = $myrow["dimension_id"];
325                         $_POST['dimension2_id']  = $myrow["dimension2_id"];
326                 }
327         } 
328
329         quick_actions_list_row(_("Posted").":",'actn', null, true);
330         if (list_updated('actn'))
331                 $Ajax->activate('edit_line');
332
333         $actn = strtolower(substr($_POST['actn'],0,1));
334
335         if ($actn == 't') 
336         {
337                 //item_tax_types_list_row(_("Item Tax Type").":",'dest_id', null);
338                 tax_types_list_row(_("Tax Type").":", 'dest_id', null);
339         } 
340         else 
341         {
342                 gl_all_accounts_list_row(_("Account").":", 'dest_id', null, $_POST['type'] == QE_DEPOSIT || $_POST['type'] == QE_PAYMENT);
343                 if ($actn != '=') 
344                 {
345                         if ($actn == '%') 
346                                 small_amount_row(_("Part").":", 'amount', price_format(0), null, "%", user_exrate_dec());
347                         else
348                                 amount_row(_("Amount").":", 'amount', price_format(0));
349                 }
350                 text_row_ex(_("Line memo").':', 'memo', 50, 256, '');
351         }
352         if ($dim >= 1) 
353                 dimensions_list_row(_("Dimension").":", 'dimension_id', null, true, " ", false, 1);
354         if ($dim > 1) 
355                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
356         
357         end_table(1);
358         if ($dim < 2)
359                 hidden('dimension2_id', 0);
360         if ($dim < 1)
361                 hidden('dimension_id', 0);
362         div_end();
363         
364         hidden('selected_id', $selected_id);
365         hidden('selected_id2', $selected_id2);
366
367         submit_add_or_update_center2($selected_id2 == -1, '', true);
368
369 }               
370 end_form();
371 //------------------------------------------------------------------------------------
372
373 end_page();
374