Changed so you can choose all the tax types in Quick Entries you want in a single row
[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 = 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 ($_POST['tax'] == 't')
114         {
115                 $res = get_all_tax_types_simple();
116                 $j = 1;
117                 $i = 0;
118                 while ($tt = db_fetch($res))
119                 {
120                         if (check_value('dest_id'.$tt['id']))
121                         {
122                                 $i |= $j;
123                                 unset($_POST['dest_id'.$tt['id']]);
124                         }       
125                         $j <<= 1;
126                 }
127                 $_POST['dest_id'] = $i;
128                 unset($_POST['tax']);
129         }
130         if ($selected_id2 != -1) 
131         {
132                 update_quick_entry_line($selected_id2, $selected_id, $_POST['actn'], $_POST['dest_id'], input_num('amount', 0), 
133                         $_POST['dimension_id'], $_POST['dimension2_id']);
134                 display_notification(_('Selected quick entry line has been updated'));
135         } 
136         else 
137         {
138                 add_quick_entry_line($selected_id, $_POST['actn'], $_POST['dest_id'], input_num('amount', 0), 
139                         $_POST['dimension_id'], $_POST['dimension2_id']);
140                 display_notification(_('New quick entry line has been added'));
141         }
142         $Mode2 = 'RESET2';
143 }
144
145 //-----------------------------------------------------------------------------------
146
147 if ($Mode == 'Delete')
148 {
149         if (!has_quick_entry_lines($selected_id))
150         {
151                 delete_quick_entry($selected_id);
152                 display_notification(_('Selected quick entry has been deleted'));
153                 $Mode = 'RESET';
154         }
155         else
156         {
157                 display_error( _("The Quick Entry has Quick Entry Lines. Cannot be deleted."));
158                 set_focus('description');
159         }
160 }
161
162 if ($Mode2 == 'BDel')
163 {
164         delete_quick_entry_line($selected_id2);
165         display_notification(_('Selected quick entry line has been deleted'));
166         $Mode2 = 'RESET2';
167 }
168 //-----------------------------------------------------------------------------------
169 if ($Mode == 'RESET')
170 {
171         $selected_id = -1;
172         $_POST['description'] = $_POST['type'] = '';
173         $_POST['base_desc']= _('Base Amount');
174         $_POST['base_amount'] = price_format(0);
175 }
176 if ($Mode2 == 'RESET2')
177 {
178         $selected_id2 = -1;
179         $_POST['actn'] = $_POST['dest_id'] = $_POST['amount'] = 
180                 $_POST['dimension_id'] = $_POST['dimension2_id'] = '';
181 }
182 //-----------------------------------------------------------------------------------
183
184 $result = get_quick_entries();
185 start_form();
186 start_table($table_style);
187 $th = array(_("Description"), _("Type"), "", "");
188 table_header($th);
189
190 $k = 0;
191 while ($myrow = db_fetch($result)) 
192 {
193         alt_table_row_color($k);
194         $type_text = $quick_entry_types[$myrow["type"]];
195         label_cell($myrow['description']);
196         label_cell($type_text);
197         edit_button_cell("Edit".$myrow["id"], _("Edit"));
198         delete_button_cell("Delete".$myrow["id"], _("Delete"));
199         end_row();
200 }
201
202 end_table(1);
203 end_form();
204 //-----------------------------------------------------------------------------------
205
206 start_form();
207
208 start_table($table_style2);
209
210 if ($selected_id != -1) 
211 {
212         //if ($Mode == 'Edit') 
213         //{
214                 //editing an existing status code
215                 $myrow = get_quick_entry($selected_id);
216
217                 $_POST['id']  = $myrow["id"];
218                 $_POST['description']  = $myrow["description"];
219                 $_POST['type']  = $myrow["type"];
220                 $_POST['base_desc']  = $myrow["base_desc"];
221                 $_POST['base_amount']  = price_format($myrow["base_amount"]);
222                 hidden('selected_id', $selected_id);
223         //}
224
225
226 text_row_ex(_("Description").':', 'description', 50, 60);
227
228 quick_entry_types_list_row(_("Entry Type").':', 'type');
229
230 text_row_ex(_("Base Amount Description").':', 'base_desc', 50, 60, '',_('Base Amount'));
231
232 amount_row(_("Default Base Amount").':', 'base_amount', price_format(0));
233
234 end_table(1);
235
236 submit_add_or_update_center($selected_id == -1, '', true);
237
238 end_form();
239
240
241 if ($selected_id != -1)
242 {
243         display_heading(_("Quick Entry Lines") . " - " . $_POST['description']);
244         $result = get_quick_entry_lines($selected_id);
245         start_form();
246         start_table($table_style2);
247         $dim = get_company_pref('use_dimension');
248         if ($dim == 2)
249                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), _("Dimension"), _("Dimension")." 2", "", "");
250         else if ($dim == 1)     
251                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), _("Dimension"), "", "");
252         else    
253                 $th = array(_("Post"), _("Account/Tax Type"), _("Amount"), "", "");
254
255         table_header($th);
256         $k = 0;
257         while ($myrow = db_fetch($result)) 
258         {
259                 alt_table_row_color($k);
260                 
261                 label_cell($quick_actions[$myrow['action']]);
262
263                 $act_type = strtolower(substr($myrow['action'], 0, 1));
264
265                 if ($act_type == 't') 
266                 {
267                         //label_cells($myrow['tax_name'], '');
268                         $res = get_all_tax_types_simple();
269                         $i = 1;
270                         $str = "";
271                         $first = true;
272                         while ($tt = db_fetch($res))
273                         {
274                                 if ($myrow['dest_id'] & $i)
275                                 {
276                                         if (!$first)
277                                                 $str .= ", ";
278                                         $str .= $tt['name'] . " " . $tt['rate'] . "%";
279                                         $first = false;
280                                 }       
281                                 $i <<= 1;
282                         }
283                         label_cells($str, '');
284                 } 
285                 else 
286                 {
287                         label_cell($myrow['dest_id'].' '.$myrow['account_name']);
288                         if ($act_type == '=') 
289                                 label_cell('');
290                         elseif ($act_type == '%') 
291                                 label_cell(number_format2($myrow['amount'], user_exrate_dec()), "nowrap align=right ");
292                         else
293                                 amount_cell($myrow['amount']);
294                 }               
295                 if ($dim >= 1)
296                         label_cell(get_dimension_string($myrow['dimension_id'], true));
297                 if ($dim > 1)
298                         label_cell(get_dimension_string($myrow['dimension2_id'], true));
299                 edit_button_cell("BEd".$myrow["id"], _("Edit"));
300                 delete_button_cell("BDel".$myrow["id"], _("Delete"));
301                 end_row();
302         }
303         end_table(1);
304         hidden('selected_id', $selected_id);
305         hidden('selected_id2', $selected_id2);
306         hidden('description', $_POST['description']);
307         hidden('type', $_POST['type']);
308         end_form();
309         start_form();
310
311         div_start('edit_line');
312         start_table($table_style2);
313
314         if ($selected_id2 != -1) 
315         {
316                 if ($Mode2 == 'BEd') 
317                 {
318                         //editing an existing status code
319                         $myrow = get_quick_entry_line($selected_id2);
320
321                         $_POST['id']  = $myrow["id"];
322                         $_POST['dest_id']  = $myrow["dest_id"];
323                         $_POST['actn']  = $myrow["action"];
324                         $_POST['amount']  = $myrow["amount"];
325                         $_POST['dimension_id']  = $myrow["dimension_id"];
326                         $_POST['dimension2_id']  = $myrow["dimension2_id"];
327                 }
328         } 
329
330         quick_actions_list_row(_("Posted").":",'actn', null, true);
331         if (list_updated('actn'))
332         {
333                 $Ajax->activate('edit_line');
334                 if (strtolower(substr($_POST['actn'],0,1)) == 't')
335                         $_POST['dest_id'] = "";
336         }       
337
338         $actn = strtolower(substr($_POST['actn'],0,1));
339
340         if ($actn == 't') 
341         {
342                 $res = get_all_tax_types_simple();
343                 $i = 1;
344                 if ($_POST['dest_id'] == '')
345                         $_POST['dest_id'] = 1;
346                 label_row(" ", "<b>" . _("Tax Types")."</b>");
347                 while ($tt = db_fetch($res))
348                 {
349                         $str = $tt['name'] . " " . $tt['rate'] . "% ";
350                         if ($_POST['dest_id'] & $i)
351                                 $_POST['dest_id'.$tt['id']] = 1;
352                         check_row($str, 'dest_id'.$tt['id'], null);
353                         $i <<= 1;
354                 }
355                 //label_cell($str);
356                 
357         } 
358         else 
359         {
360                 gl_all_accounts_list_row(_("Account").":", 'dest_id', null);
361                 if ($actn != '=') 
362                 {
363                         if ($actn == '%') 
364                                 small_amount_row(_("Part").":", 'amount', price_format(0), null, "%", user_exrate_dec());
365                         else
366                                 amount_row(_("Amount").":", 'amount', price_format(0));
367                 }
368         }
369         if ($dim >= 1) 
370                 dimensions_list_row(_("Dimension").":", 'dimension_id', null, true, " ", false, 1);
371         if ($dim > 1) 
372                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
373         
374         end_table(1);
375         if ($dim < 2)
376                 hidden('dimension2_id', 0);
377         if ($dim < 1)
378                 hidden('dimension_id', 0);
379         hidden('tax', $actn);
380         div_end();
381
382         hidden('selected_id', $selected_id);
383         hidden('selected_id2', $selected_id2);
384         hidden('description', $_POST['description']);
385         hidden('type', $_POST['type']);
386
387         submit_add_or_update_center2($selected_id2 == -1, '', true);
388
389         end_form();
390 }               
391 //------------------------------------------------------------------------------------
392
393 end_page();
394
395 ?>