Optimized editkey graphics by using JS callEditor instead.
[fa-stable.git] / includes / ui / ui_lists.inc
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 include_once($path_to_root . "/includes/banking.inc");
13 include_once($path_to_root . "/includes/types.inc");
14 include_once($path_to_root . "/includes/current_user.inc");
15
16 $_search_button = "<input %s type='submit' class='combo_submit' style='border:0;background:url($path_to_root/themes/"
17         ."%s/images/locate.png) no-repeat;%s' aspect='fallback' name='%s' value=' ' title='"._("Set filter")."'> ";
18
19 $_select_button = "<input %s type='submit' class='combo_select' style='border:0;background:url($path_to_root/themes/"
20         ."%s/images/button_ok.png) no-repeat;%s' aspect='fallback' name='%s' value=' ' title='"._("Select")."'> ";
21
22 $all_items = ALL_TEXT;
23
24 //----------------------------------------------------------------------------
25 //      Universal sql combo generator
26 //      $sql must return selector values and selector texts in columns 0 & 1
27 //      Options are merged with defaults.
28
29 function combo_input($name, $selected_id, $sql, $valfield, $namefield,
30         $options=null)
31 {
32 global $Ajax;
33
34 $opts = array(          // default options
35         'where'=> array(),              // additional constraints
36         'order' => $namefield,  // list sort order
37                 // special option parameters
38         'spec_option'=>false,   // option text or false
39         'spec_id' => 0,         // option id
40                 // submit on select parameters
41         'default' => '', // default value when $_POST is not set
42         'multi' => false,       // multiple select
43         'select_submit' => false, //submit on select: true/false
44         'async' => true,        // select update via ajax (true) vs _page_body reload
45                 // search box parameters
46         'sel_hint' => null,
47         'search_box' => false,  // name or true/false
48         'type' => 0,    // type of extended selector:
49                 // 0 - with (optional) visible search box, search by fragment inside id
50                 // 1 - with hidden search box, search by option text
51                 // 2 - with (optional) visible search box, search by fragment at the start of id
52                 // 3 - TODO reverse: box with hidden selector available via enter; this
53                 // would be convenient for optional ad hoc adding of new item
54         'search_submit' => true, //search submit button: true/false
55         'size' => 8,    // size and max of box tag
56         'max' => 50,
57         'height' => false,      // number of lines in select box
58         'cells' => false,       // combo displayed as 2 <td></td> cells
59         'search' => array(), // sql field names to search
60         'format' => null,        // format functions for regular options
61         'disabled' => false,
62         'box_hint' => null, // box/selectors hints; null = std see below
63         'category' => false, // category column name or false
64         'show_inactive' => false, // show inactive records. 
65         'editable' => false // false, or length of editable entry field
66 );
67 // ------ merge options with defaults ----------
68         if($options != null)
69                 $opts = array_merge($opts, $options);
70         if (!is_array($opts['where']))  $opts['where'] = array($opts['where']);
71
72         $search_box = $opts['search_box']===true ? '_'.$name.'_edit' : $opts['search_box'];
73         // select content filtered by search field:
74         $search_submit = $opts['search_submit']===true ? '_'.$name.'_button' : $opts['search_submit'];
75         // select set by select content field
76         $search_button = $opts['editable'] ? '_'.$name.'_button' : ($search_box ? $search_submit : false);
77
78         $select_submit =  $opts['select_submit'];
79         $spec_id = $opts['spec_id'];
80         $spec_option = $opts['spec_option'];
81         if ($opts['type'] == 0) {
82                 $by_id = true;
83                 $class = 'combo';
84         } elseif($opts['type'] == 1) {
85                 $by_id = false;
86                 $class = 'combo2';
87         } else {
88                 $by_id = true;
89                 $class = 'combo3';
90         }
91
92         $disabled = $opts['disabled'] ? "disabled" : '';
93         $multi = $opts['multi'];
94         
95         if(!count($opts['search'])) {
96                 $opts['search'] = array($by_id ? $valfield : $namefield);
97         }
98         if ($opts['sel_hint'] === null) 
99                 $opts['sel_hint'] = $by_id || $search_box==false ?
100                         '' : _('Press Space tab for search pattern entry');
101
102         if ($opts['box_hint'] === null)
103                 $opts['box_hint'] = $search_box && $search_submit != false ?
104                         ($by_id ? _('Enter code fragment to search or * for all')
105                         : _('Enter description fragment to search or * for all')) :'';
106
107         if ($selected_id == null) {
108                 $selected_id = get_post($name, (string)$opts['default']);
109         }
110         if(!is_array($selected_id))
111                 $selected_id = array((string)$selected_id); // code is generalized for multiple selection support
112
113         $txt = get_post($search_box);
114         $rel = '';
115         $limit = '';
116         if (isset($_POST['_'.$name.'_update'])) { // select list or search box change
117                 if ($by_id) $txt = $_POST[$name];
118
119                 if (!$opts['async'])
120                         $Ajax->activate('_page_body');
121                 else
122                         $Ajax->activate($name);
123         }
124         if (isset($_POST[$search_button])) {
125                 if (!$opts['async'])
126                         $Ajax->activate('_page_body');
127                 else
128                         $Ajax->activate($name);
129         }
130         if ($search_box) {
131                 // search related sql modifications
132
133                 $rel = "rel='$search_box'"; // set relation to list
134                 if ($opts['search_submit']) {
135                         if (isset($_POST[$search_button])) {
136                                 $selected_id = array(); // ignore selected_id while search
137                                 if (!$opts['async'])
138                                         $Ajax->activate('_page_body');
139                                 else
140                                         $Ajax->activate($name);
141                         }
142                         if ($txt == '') {
143                                 if ($spec_option === false && $selected_id == array())
144                                         $limit = ' LIMIT 1';
145                                 else
146                                         $opts['where'][] = $valfield . "=". db_escape(get_post($name, $spec_id));
147                         }
148                         else
149                                 if ($txt != '*') {
150
151                                         foreach($opts['search'] as $i=> $s)
152                                                 $opts['search'][$i] = $s . " LIKE "
153                                                         .db_escape(($class=='combo3' ? '' : '%').$txt.'%');
154                                         $opts['where'][] = '('. implode($opts['search'], ' OR ') . ')';
155                                 }
156                 }
157         }
158
159         // sql completion
160         if (count($opts['where'])) {
161                 $where = strpos($sql, 'WHERE')==false ? ' WHERE ':' AND ';
162                 $where .= '('. implode($opts['where'], ' AND ') . ')';
163                 $group_pos = strpos($sql, 'GROUP BY');
164                 if ($group_pos) {
165                         $group = substr($sql, $group_pos);
166                         $sql = substr($sql, 0, $group_pos) . $where.' '.$group;
167                 } else {
168                         $sql .= $where;
169                 }
170         }
171         if ($opts['order'] != false) {
172                 if (!is_array($opts['order']))
173                         $opts['order'] = array($opts['order']);
174                 $sql .= ' ORDER BY '.implode(',',$opts['order']);
175         }
176
177         $sql .= $limit;
178         // ------ make selector ----------
179         $selector = $first_opt = '';
180         $first_id = false;
181         $found = false;
182         $lastcat = null;
183         $edit = false;
184 //if($name=='stock_id') display_notification('<pre>'.print_r($_POST, true).'</pre>');
185 //if($name=='curr_default') display_notification($opts['search_submit']);
186         if($result = db_query($sql)) {
187                 while ($contact_row = db_fetch($result)) {
188                         $value = $contact_row[0];
189                         $descr = $opts['format']==null ?  $contact_row[1] :
190                                 call_user_func($opts['format'], $contact_row);
191                         $sel = '';
192                         if (get_post($search_button) && ($txt == $value)) {
193                                 $selected_id[] = $value;
194                         }
195
196                         if (in_array((string)$value, $selected_id, true)) {
197                                 $sel = 'selected';
198                                 $found = $value;
199                                 $edit = $opts['editable'] && $contact_row['editable'] 
200                                         && (@$_POST[$search_box] == $value)
201                                         ? $contact_row[1] : false; // get non-formatted description
202                                 if ($edit)
203                                         break;  // selected field is editable - abandon list construction
204                         }
205                         // show selected option even if inactive 
206                         if (!$opts['show_inactive'] && @$contact_row['inactive'] && $sel==='') {
207                                 continue;
208                         } else 
209                                 $optclass = @$contact_row['inactive'] ? "class='inactive'" : '';
210
211                         if ($first_id === false) {
212                                 $first_id = $value;
213                                 $first_opt = $descr;
214                         }
215                         $cat = $contact_row[$opts['category']];
216                         if ($opts['category'] !== false && $cat != $lastcat){
217                                 $selector .= "<optgroup label='".$cat."'>\n";
218                                 $lastcat = $cat;
219                         }
220                         $selector .= "<option $sel $optclass value='$value'>$descr</option>\n";
221                 }
222                 db_free_result($result);
223         }
224
225         // Prepend special option.
226         if ($spec_option !== false) { // if special option used - add it
227                 $first_id = $spec_id;
228                 $first_opt = $spec_option;
229 //      }
230 //      if($first_id !== false) {
231                 $sel = $found===false ? 'selected' : '';
232                 $optclass = @$contact_row['inactive'] ? "class='inactive'" : '';
233                 $selector = "<option $sel value='$first_id'>$first_opt</option>\n"
234                         . $selector;
235         }
236
237         if ($found===false) {
238                 $selected_id = array($first_id);
239         }
240         
241         $_POST[$name] = $multi ? $selected_id : $selected_id[0];
242
243         $selector = "<select autocomplete='off' ".($multi ? "multiple" : '')
244                 . ($opts['height']!==false ? ' size="'.$opts['height'].'"' : '')
245                 . "$disabled name='$name".($multi ? '[]':'')."' class='$class' title='"
246                 . $opts['sel_hint']."' $rel>".$selector."</select>\n";
247
248         if ($by_id && ($search_box != false || $opts['editable']) ) {
249                 // on first display show selector list
250                 if (isset($_POST[$search_box]) && $opts['editable'] && $edit) {
251                         $selector = "<input type='hidden' name='$name' value='".$_POST[$name]."'>"
252                         ."<input type='text' $disabled name='{$name}_text' id='{$name}_text' size='".
253                                 $opts['editable']."' maxlength='".$opts['max']."' $rel value='$edit'>\n";
254                                 set_focus($name.'_text'); // prevent lost focus
255                 } else if (get_post($search_submit ? $search_submit : "_{$name}_button"))
256                         set_focus($name); // prevent lost focus
257                 if (!$opts['editable'])
258                         $txt = $found;
259                 $Ajax->addUpdate($name, $search_box, $txt ? $txt : '');
260         }
261
262         $Ajax->addUpdate($name, "_{$name}_sel", $selector);
263
264         // span for select list/input field update
265         $selector = "<span id='_{$name}_sel'>".$selector."</span>\n";
266
267          // if selectable or editable list is used - add select button
268         if ($select_submit != false || $search_button) {
269                 global $_select_button;
270         // button class selects form reload/ajax selector update
271                 $selector .= sprintf($_select_button, $disabled, user_theme(),
272                         (fallback_mode() ? '' : 'display:none;'),
273                          '_'.$name.'_update')."\n";
274         }
275 // ------ make combo ----------
276         $edit_entry = '';
277         if ($search_box != false) {
278                 $edit_entry = "<input $disabled type='text' name='$search_box' id='$search_box' size='".
279                         $opts['size']."' maxlength='".$opts['max'].
280                         "' value='$txt' class='$class' rel='$name' autocomplete='off' title='"
281                         .$opts['box_hint']."'"
282                         .(!fallback_mode() && !$by_id ? " style=display:none;":'')
283                         .">\n";
284                 if ($search_submit != false || $opts['editable']) {
285                         global $_search_button;
286                         $edit_entry .= sprintf($_search_button, $disabled, user_theme(),
287                                 (fallback_mode() ? '' : 'display:none;'),
288                                 $search_submit ? $search_submit : "_{$name}_button")."\n";
289                 }
290         }
291         default_focus(($search_box && $by_id) ? $search_box : $name);
292
293         if ($search_box && $opts['cells'])
294                 $str = ($edit_entry!='' ? "<td>$edit_entry</td>" : '')."<td>$selector</td>";
295         else
296                 $str = $edit_entry.$selector;
297         return $str;
298 }
299
300 /*
301         Helper function.
302         Returns true if selector $name is subject to update.
303 */
304 function list_updated($name)
305 {
306         return isset($_POST['_'.$name.'_update']) || isset($_POST['_'.$name.'_button']);
307 }
308 //----------------------------------------------------------------------------------------------
309 //      Universal array combo generator
310 //      $items is array of options 'value' => 'description'
311 //      Options is reduced set of combo_selector options and is merged with defaults.
312
313 function array_selector($name, $selected_id, $items, $options=null)
314 {
315         global $Ajax;
316
317 $opts = array(          // default options
318         'spec_option'=>false,   // option text or false
319         'spec_id' => 0,         // option id
320         'select_submit' => false, //submit on select: true/false
321         'async' => true,        // select update via ajax (true) vs _page_body reload
322         'default' => '', // default value when $_POST is not set
323         'multi'=>false, // multiple select
324                 // search box parameters
325         'height' => false,      // number of lines in select box
326         'sel_hint' => null,
327         'disabled' => false
328 );
329 // ------ merge options with defaults ----------
330         if($options != null)
331                 $opts = array_merge($opts, $options);
332         $select_submit =  $opts['select_submit'];
333         $spec_id = $opts['spec_id'];
334         $spec_option = $opts['spec_option'];
335         $disabled = $opts['disabled'] ? "disabled" : '';
336         $multi = $opts['multi'];
337
338         if ($selected_id == null) {
339                 $selected_id = get_post($name, $opts['default']);
340         }
341         if(!is_array($selected_id))
342                 $selected_id = array((string)$selected_id); // code is generalized for multiple selection support
343
344         if (isset($_POST[ '_'.$name.'_update'])) {
345                 if (!$opts['async'])
346                         $Ajax->activate('_page_body');
347                 else
348                         $Ajax->activate($name);
349         }
350
351         // ------ make selector ----------
352         $selector = $first_opt = '';
353         $first_id = false;
354         $found = false;
355 //if($name=='SelectStockFromList') display_error($sql);
356                 foreach($items as $value=>$descr) {
357                         $sel = '';
358                         if (in_array((string)$value, $selected_id, true)) {
359                                 $sel = 'selected';
360                                 $found = $value;
361                         }
362                         if ($first_id === false) {
363                                 $first_id = $value;
364                                 $first_opt = $descr;
365                         }
366                         $selector .= "<option $sel value='$value'>$descr</option>\n";
367                 }
368
369         if ($first_id!==false) {
370                 $sel = ($found===$first_id) || ($found===false && ($spec_option===false)) ? "selected='selected'" : '';
371                 $selector = sprintf($first_opt, $sel).$selector;
372         }
373         // Prepend special option.
374         if ($spec_option !== false) { // if special option used - add it
375                 $first_id = $spec_id;
376                 $first_opt = $spec_option;
377                 $sel = $found===false ? 'selected' : '';
378                 $selector = "<option $sel value='$spec_id'>$spec_option</option>\n"
379                         . $selector;
380         }
381
382         if ($found===false) {
383                 $selected_id = array($first_id);
384         }
385         $_POST[$name] = $multi ? $selected_id : $selected_id[0];
386
387         $selector = "<select autocomplete='off' ".($multi  ? "multiple" : '')
388                 . ($opts['height']!==false ? ' size="'.$opts['height'].'"' : '')
389                 . "$disabled name='$name".($multi ? '[]' : '')."' class='combo' title='"
390                 . $opts['sel_hint']."'>".$selector."</select>\n";
391
392         $Ajax->addUpdate($name, "_{$name}_sel", $selector);
393
394         $selector = "<span id='_{$name}_sel'>".$selector."</span>\n";
395
396         if ($select_submit != false) { // if submit on change is used - add select button
397                 global $_select_button;
398                 $selector .= sprintf($_select_button, $disabled, user_theme(),
399                         (fallback_mode() ? '' : 'display:none;'),
400                          '_'.$name.'_update')."\n";
401         }
402         default_focus($name);
403
404         return $selector;
405 }
406 //----------------------------------------------------------------------------------------------
407
408 function _format_add_curr($row)
409 {
410         static $company_currency;
411
412         if ($company_currency == null)
413         {
414                 $company_currency = get_company_currency();
415         }
416         return $row[1] . ($row[2] == $company_currency ?
417                 '' : ("&nbsp;-&nbsp;" . $row[2]));
418 }
419
420 function add_edit_combo($type)
421 {
422         global $path_to_root, $popup_editors;
423
424         // Derive theme path
425         $theme_path = $path_to_root . '/themes/' . user_theme();
426
427         $key = $popup_editors[$type][1];
428         $onclick = "onclick=\"javascript:callEditor($key); return false;\"";
429         $img = "<img width='12' height='12' border='0' alt='Add/Edit' title='Add/Edit' src='$theme_path/images/".ICON_EDIT."'>";
430         return "<a target = '_blank' href='#' $onclick tabindex='-1'>$img</a>"; 
431 }
432
433 function supplier_list($name, $selected_id=null, $spec_option=false, $submit_on_change=false,
434         $all=false, $editkey = false)
435 {
436         global $all_items;
437
438         $sql = "SELECT supplier_id, supp_ref, curr_code, inactive FROM ".TB_PREF."suppliers ";
439
440         $mode = get_company_pref('no_supplier_list');
441
442         if ($editkey)
443                 set_editor('supplier', $name, $editkey);
444                 
445         $ret = combo_input($name, $selected_id, $sql, 'supplier_id', 'supp_name',
446         array(
447                 'format' => '_format_add_curr',
448             'order' => array('supp_ref'),
449                 'search_box' => $mode!=0,
450                 'type' => 1,
451                 'spec_option' => $spec_option === true ? _("All Suppliers") : $spec_option,
452                 'spec_id' => $all_items,
453                 'select_submit'=> $submit_on_change,
454                 'async' => false,
455                 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment') :
456                 _('Select supplier'),
457                 'show_inactive'=>$all
458                 ));
459         if ($editkey)
460                 $ret .= add_edit_combo('supplier');             
461         return $ret;
462 }
463
464 function supplier_list_cells($label, $name, $selected_id=null, $all_option=false, 
465         $submit_on_change=false, $all=false, $editkey = false)
466 {
467         if ($label != null)
468                 echo "<td>$label</td><td>\n";
469                 echo supplier_list($name, $selected_id, $all_option, $submit_on_change, 
470                 $all, $editkey);
471                 echo "</td>\n";
472 }
473
474 function supplier_list_row($label, $name, $selected_id=null, $all_option = false, 
475         $submit_on_change=false, $all=false, $editkey = false)
476 {
477         echo "<tr><td class='label'>$label</td><td>";
478         echo supplier_list($name, $selected_id, $all_option, $submit_on_change,
479                 $all, $editkey);
480         echo "</td></tr>\n";
481 }
482 //----------------------------------------------------------------------------------------------
483
484 function customer_list($name, $selected_id=null, $spec_option=false, $submit_on_change=false, 
485         $show_inactive=false, $editkey = false)
486 {
487         global $all_items;
488
489         $sql = "SELECT debtor_no, debtor_ref, curr_code, inactive FROM ".TB_PREF."debtors_master ";
490
491         $mode = get_company_pref('no_customer_list');
492
493         if ($editkey)
494                 set_editor('customer', $name, $editkey);
495
496         $ret = combo_input($name, $selected_id, $sql, 'debtor_no', 'debtor_ref',
497         array(
498             'format' => '_format_add_curr',
499             'order' => array('debtor_ref'),
500                 'search_box' => $mode!=0,
501                 'type' => 1,
502                 'size' => 20,
503                 'spec_option' => $spec_option === true ? _("All Customers") : $spec_option,
504                 'spec_id' => $all_items,
505                 'select_submit'=> $submit_on_change,
506                 'async' => false,
507                 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment; F2 - entry new customer') :
508                 _('Select customer'),
509                 'show_inactive' => $show_inactive
510         ) );
511         if ($editkey)
512                 $ret .= add_edit_combo('customer');
513         return $ret;
514 }
515
516 function customer_list_cells($label, $name, $selected_id=null, $all_option=false, 
517         $submit_on_change=false, $show_inactive=false, $editkey = false)
518 {
519         if ($label != null)
520                 echo "<td>$label</td>\n";
521         echo "<td nowrap>";
522         echo customer_list($name, $selected_id, $all_option, $submit_on_change,
523                 $show_inactive, $editkey);
524         echo "</td>\n";
525 }
526
527 function customer_list_row($label, $name, $selected_id=null, $all_option = false, 
528         $submit_on_change=false, $show_inactive=false, $editkey = false)
529 {
530         global $path_to_root;
531
532         echo "<tr><td class='label'>$label</td><td nowrap>";
533         echo customer_list($name, $selected_id, $all_option, $submit_on_change,
534                 $show_inactive, $editkey);
535         echo "</td>\n</tr>\n";
536 }
537
538 //------------------------------------------------------------------------------------------------
539
540 function customer_branches_list($customer_id, $name, $selected_id=null,
541         $spec_option = true, $enabled=true, $submit_on_change=false, $editkey = false)
542 {
543         global $all_items;
544
545         $sql = "SELECT branch_code, branch_ref FROM ".TB_PREF."cust_branch
546                 WHERE debtor_no=" . db_escape($customer_id)." ";
547
548         if ($editkey)
549                 set_editor('branch', $name, $editkey);
550
551         $where = $enabled ? array("disable_trans = 0") : array();
552         $ret = combo_input($name, $selected_id, $sql, 'branch_code', 'branch_ref',
553         array(
554                 'where' => $where,
555                 'order' => array('branch_ref'),
556                 'spec_option' => $spec_option === true ? _('All branches') : $spec_option,
557                 'spec_id' => $all_items,
558                 'select_submit'=> $submit_on_change,
559                 'sel_hint' => _('Select customer branch')
560         ) );
561         if ($editkey)
562         {
563                 $ret .= add_edit_combo('branch');
564         }       
565         return $ret;
566 }
567 //------------------------------------------------------------------------------------------------
568
569 function customer_branches_list_cells($label,$customer_id, $name, $selected_id=null, 
570         $all_option = true, $enabled=true, $submit_on_change=false, $editkey = false)
571 {
572         if ($label != null)
573                 echo "<td>$label</td>\n";
574         echo "<td>";
575         echo customer_branches_list($customer_id, $name, $selected_id, $all_option, $enabled, 
576                 $submit_on_change, $editkey);
577         echo "</td>\n";
578 }
579
580 function customer_branches_list_row($label, $customer_id, $name, $selected_id=null, 
581         $all_option = true, $enabled=true, $submit_on_change=false, $editkey = false)
582 {
583         echo "<tr><td class='label'>$label</td>";
584         customer_branches_list_cells(null, $customer_id, $name, $selected_id, 
585                 $all_option, $enabled, $submit_on_change, $editkey);
586         echo "</tr>";
587 }
588
589 //------------------------------------------------------------------------------------------------
590
591 function locations_list($name, $selected_id=null, $all_option=false, $submit_on_change=false)
592 {
593         global $all_items;
594
595         $sql = "SELECT loc_code, location_name, inactive FROM ".TB_PREF."locations";
596
597 return combo_input($name, $selected_id, $sql, 'loc_code', 'location_name',
598         array(
599                 'spec_option' => $all_option === true ? _("All Locations") : $all_option,
600                 'spec_id' => $all_items,
601                 'select_submit'=> $submit_on_change
602         ) );
603 }
604
605 function locations_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
606 {
607         if ($label != null)
608                 echo "<td>$label</td>\n";
609         echo "<td>";
610         echo locations_list($name, $selected_id, $all_option, $submit_on_change);
611         echo "</td>\n";
612 }
613
614 function locations_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
615 {
616         echo "<tr><td class='label'>$label</td>";
617         locations_list_cells(null, $name, $selected_id, $all_option, $submit_on_change);
618         echo "</tr>\n";
619 }
620
621 //-----------------------------------------------------------------------------------------------
622
623 function currencies_list($name, $selected_id=null, $submit_on_change=false)
624 {
625         $sql = "SELECT curr_abrev, currency, inactive FROM ".TB_PREF."currencies";
626
627 // default to the company currency
628 return combo_input($name, $selected_id, $sql, 'curr_abrev', 'currency',
629         array(
630                 'select_submit'=> $submit_on_change,
631                 'default' => get_company_currency(),
632                 'async' => false                
633         ) );
634 }
635
636 function currencies_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
637 {
638         if ($label != null)
639                 echo "<td>$label</td>\n";
640         echo "<td>";
641         echo currencies_list($name, $selected_id, $submit_on_change);
642         echo "</td>\n";
643 }
644
645 function currencies_list_row($label, $name, $selected_id=null, $submit_on_change=false)
646 {
647         echo "<tr><td class='label'>$label</td>";
648         currencies_list_cells(null, $name, $selected_id, $submit_on_change);
649         echo "</tr>\n";
650 }
651
652 //---------------------------------------------------------------------------------------------------
653
654 function fiscalyears_list($name, $selected_id=null, $submit_on_change=false)
655 {
656
657         $sql = "SELECT * FROM ".TB_PREF."fiscal_year";
658
659 // default to the company current fiscal year
660
661 return combo_input($name, $selected_id, $sql, 'id', '',
662         array(
663                 'order' => 'begin',
664                 'default' => get_company_pref('f_year'),
665                 'format' => '_format_fiscalyears',
666                 'select_submit'=> $submit_on_change,
667                 'async' => false
668         ) );
669 }
670
671 function _format_fiscalyears($row)
672 {
673         return sql2date($row[1]) . "&nbsp;-&nbsp;" . sql2date($row[2])
674         . "&nbsp;&nbsp;" . ($row[3] ? _('Closed') : _('Active')) . "</option>\n";
675 }
676
677 function fiscalyears_list_cells($label, $name, $selected_id=null)
678 {
679         if ($label != null)
680                 echo "<td>$label</td>\n";
681         echo "<td>";
682         echo fiscalyears_list($name, $selected_id);
683         echo "</td>\n";
684 }
685
686 function fiscalyears_list_row($label, $name, $selected_id=null)
687 {
688         echo "<tr><td class='label'>$label</td>";
689         fiscalyears_list_cells(null, $name, $selected_id);
690         echo "</tr>\n";
691 }
692 //------------------------------------------------------------------------------------
693
694 function dimensions_list($name, $selected_id=null, $no_option=false, $showname=' ',
695         $submit_on_change=false, $showclosed=false, $showtype=1)
696 {
697 $sql = "SELECT id, CONCAT(reference,'  ',name) as ref FROM ".TB_PREF."dimensions";
698
699 $options = array(
700         'order' => 'reference',
701         'spec_option'=>$no_option ? $showname : false,
702         'spec_id' => 0,
703         'select_submit'=> $submit_on_change,
704         'async' => false,
705         );
706
707         if (!$showclosed)
708                 $options['where'][] = "closed=0";
709         if($showtype)
710                 $options['where'][] = "type_=$showtype";
711
712         return combo_input($name, $selected_id, $sql, 'id', 'ref', $options);
713 }
714
715 function dimensions_list_cells($label, $name, $selected_id=null, $no_option=false, $showname=null,
716         $showclosed=false, $showtype=0, $submit_on_change=false)
717 {
718         if ($label != null)
719                 echo "<td>$label</td>\n";
720         echo "<td>";
721         echo dimensions_list($name, $selected_id, $no_option, $showname, $submit_on_change, $showclosed, $showtype);
722         echo "</td>\n";
723 }
724
725 function dimensions_list_row($label, $name, $selected_id=null, $no_option=false, $showname=null,
726         $showclosed=false, $showtype=0, $submit_on_change=false)
727 {
728         echo "<tr><td class='label'>$label</td>";
729         dimensions_list_cells(null, $name, $selected_id, $no_option, $showname,
730                 $showclosed, $showtype, $submit_on_change);
731         echo "</tr>\n";
732 }
733
734 //---------------------------------------------------------------------------------------------------
735
736 function stock_items_list($name, $selected_id=null, $all_option=false, 
737         $submit_on_change=false, $opts=array(), $editkey = false)
738 {
739         global $all_items;
740
741         $sql = "SELECT stock_id, s.description, c.description, s.inactive, s.editable
742                         FROM ".TB_PREF."stock_master s,".TB_PREF."stock_category c WHERE s.category_id=c.category_id";
743
744         if ($editkey)
745                 set_editor('item', $name, $editkey);
746
747         $ret = combo_input($name, $selected_id, $sql, 'stock_id', 's.description',
748         array_merge(
749           array(
750                 'format' => '_format_stock_items',
751                 'spec_option' => $all_option===true ?  _("All Items") : $all_option,
752                 'spec_id' => $all_items,
753                 'search_box' => true,
754                 'search' => array("stock_id", "c.description","s.description"),
755                 'search_submit' => get_company_pref('no_item_list')!=0,
756                 'size'=>10,
757                 'select_submit'=> $submit_on_change,
758                 'category' => 2,
759                 'order' => array('c.description','stock_id')
760           ), $opts) );
761         if ($editkey)
762                 $ret .= add_edit_combo('item');         
763         return $ret;
764           
765 }
766
767 function _format_stock_items($row)
768 {
769         return (user_show_codes() ?  ($row[0] . "&nbsp;-&nbsp;") : "") . $row[1];
770 }
771
772 function stock_items_list_cells($label, $name, $selected_id=null, $all_option=false, 
773         $submit_on_change=false, $all=false, $editkey = false)
774 {
775         if ($label != null)
776                 echo "<td>$label</td>\n";
777         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
778                 array('cells'=>true, 'show_inactive'=>$all), $editkey);
779 }
780 /*
781 function stock_items_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
782 {
783         echo "<tr>\n";
784         stock_items_list_cells($label, $name, $selected_id, $all_option, $submit_on_change);
785         echo "</tr>\n";
786 }
787 */
788 //---------------------------------------------------------------------------------------------------
789 //
790 // Select item via foreign code.
791 //
792 function sales_items_list($name, $selected_id=null, $all_option=false, 
793         $submit_on_change=false, $type='', $opts=array())
794 {
795         global $all_items;
796         // all sales codes
797         $sql = "SELECT i.item_code, i.description, c.description, count(*)>1 as kit,
798                          i.inactive, if(count(*)>1, '0', s.editable) as editable
799                         FROM
800                         ".TB_PREF."stock_master s,
801                         ".TB_PREF."item_codes i
802                         LEFT JOIN
803                         ".TB_PREF."stock_category c
804                         ON i.category_id=c.category_id
805                         WHERE i.stock_id=s.stock_id";
806
807         
808         if ($type == 'local')   { // exclude foreign codes
809                 $sql .= " AND !i.is_foreign"; 
810         } elseif ($type == 'kits') { // sales kits
811                 $sql .= " AND !i.is_foreign AND i.item_code!=i.stock_id";
812         }
813         $sql .= " AND !i.inactive AND !s.inactive AND !s.no_sale";
814         $sql .= " GROUP BY i.item_code";
815
816         return combo_input($name, $selected_id, $sql, 'i.item_code', 'c.description',
817         array_merge(
818           array(
819                 'format' => '_format_stock_items',
820                 'spec_option' => $all_option===true ?  _("All Items") : $all_option,
821                 'spec_id' => $all_items,
822                 'search_box' => true,
823                 'search' => array("i.item_code", "c.description", "i.description"),
824                 'search_submit' => get_company_pref('no_item_list')!=0,
825                 'size'=>15,
826                 'select_submit'=> $submit_on_change,
827                 'category' => 2,
828                 'order' => array('c.description','i.item_code'),
829                 'editable' => 30,
830                 'max' => 255
831           ), $opts) );
832 }
833
834 function sales_items_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
835 {
836         if ($label != null)
837                 echo "<td>$label</td>\n";
838         echo sales_items_list($name, $selected_id, $all_option, $submit_on_change,
839                 '', array('cells'=>true));
840 }
841
842 function sales_kits_list($name, $selected_id=null, $all_option=false, $submit_on_change=false)
843 {
844         return sales_items_list($name, $selected_id, $all_option, $submit_on_change,
845                 'kits', array('cells'=>false, 'editable' => false));
846 }
847
848 function sales_local_items_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
849 {
850         echo "<tr>";
851         if ($label != null)
852                 echo "<td class='label'>$label</td>\n";
853         echo "<td>";
854         echo sales_items_list($name, $selected_id, $all_option, $submit_on_change,
855                 'local', array('cells'=>false, 'editable' => false));
856         echo "</td></tr>";
857 }
858 //------------------------------------------------------------------------------------
859
860 function stock_manufactured_items_list($name, $selected_id=null,
861         $all_option=false, $submit_on_change=false)
862 {
863         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
864                 array('where'=>array("mb_flag= 'M'")));
865 }
866
867 function stock_manufactured_items_list_cells($label, $name, $selected_id=null,
868                                 $all_option=false, $submit_on_change=false)
869 {
870         if ($label != null)
871                 echo "<td>$label</td>\n";
872         echo "<td>";
873         echo stock_manufactured_items_list($name, $selected_id, $all_option, $submit_on_change);
874         echo "</td>\n";
875 }
876
877 function stock_manufactured_items_list_row($label, $name, $selected_id=null,
878                 $all_option=false, $submit_on_change=false)
879 {
880         echo "<tr><td class='label'>$label</td>";
881         stock_manufactured_items_list_cells(null, $name, $selected_id, $all_option, $submit_on_change);
882         echo "</tr>\n";
883 }
884 //------------------------------------------------------------------------------------
885
886 function stock_component_items_list($name, $parent_stock_id, $selected_id=null,
887         $all_option=false, $submit_on_change=false, $editkey = false)
888 {
889         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
890                 array('where'=>array("stock_id != '$parent_stock_id'")), $editkey);
891 }
892
893 function stock_component_items_list_cells($label, $name, $parent_stock_id, 
894         $selected_id=null, $all_option=false, $submit_on_change=false, $editkey = false)
895 {
896         if ($label != null)
897                 echo "<td>$label</td>\n";
898         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
899                 array('where'=>array("stock_id != '$parent_stock_id'"), 'cells'=>true),
900                 $editkey);
901 }
902 //------------------------------------------------------------------------------------
903
904 function stock_costable_items_list($name, $selected_id=null,
905         $all_option=false, $submit_on_change=false)
906 {
907         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
908                 array('where'=>array("mb_flag!='D'")));
909 }
910
911 function stock_costable_items_list_cells($label, $name, $selected_id=null, 
912         $all_option=false, $submit_on_change=false)
913 {
914         if ($label != null)
915                 echo "<td>$label</td>\n";
916         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
917                 array('where'=>array("mb_flag!='D'"), 'cells'=>true));
918 }
919
920 //------------------------------------------------------------------------------------
921 function stock_purchasable_items_list($name, $selected_id=null, 
922         $all_option=false, $submit_on_change=false, $all=false, $editkey=false)
923 {
924         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
925                 array('where'=>array("mb_flag!= 'M'"), 
926                         'show_inactive'=>$all), $editkey);
927 }
928 //
929 //      This helper is used in PO/GRN/PI entry and supports editable descriptions.
930 //
931 function stock_purchasable_items_list_cells($label, $name, $selected_id=null,
932                         $all_option=false, $submit_on_change=false, $editkey=false)
933 {
934         if ($label != null)
935                 echo "<td>$label</td>\n";
936         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
937                 array('where'=>array("mb_flag!= 'M'"), 
938                          'editable' => 30,
939                          'cells'=>true), $editkey);
940 }
941 /*
942 function stock_purchasable_items_list_row($label, $name, $selected_id=null,
943                         $all_option=false, $submit_on_change=false, $editkey=false)
944 {
945         echo "<tr><td class='label'>$label</td>";
946         stock_purchasable_items_list_cells(null, $name, $selected_id=null,
947                 $all_option, $submit_on_change, $editkey);
948         echo "</tr>\n";
949 }
950 */
951 //------------------------------------------------------------------------------------
952
953 function stock_item_types_list_row($label, $name, $selected_id=null, $enabled=true)
954 {
955         global $stock_types;
956
957         echo "<tr>";
958         if ($label != null)
959                 echo "<td class='label'>$label</td>\n";
960         echo "<td>";
961
962         echo array_selector($name, $selected_id, $stock_types, 
963                 array( 
964                         'select_submit'=> true, 
965                         'disabled' => !$enabled) );
966         echo "</td></tr>\n";
967 }
968
969 function stock_units_list_row($label, $name, $value=null, $enabled=true)
970 {
971         $result = get_all_item_units();
972         echo "<tr>";
973         if ($label != null)
974                 echo "<td class='label'>$label</td>\n";
975         echo "<td>";
976
977         while($unit = db_fetch($result))
978                 $units[$unit['abbr']] = $unit['name'];
979
980         echo array_selector($name, $value, $units, array( 'disabled' => !$enabled) );
981
982         echo "</td></tr>\n";
983 }
984
985 //------------------------------------------------------------------------------------
986
987 function tax_types_list($name, $selected_id=null, $none_option=false, $submit_on_change=false)
988 {
989         $sql = "SELECT id, CONCAT(name, ' (',rate,'%)') as name FROM ".TB_PREF."tax_types";
990
991         return combo_input($name, $selected_id, $sql, 'id', 'name',
992         array(
993                 'spec_option' => $none_option,
994                 'spec_id' => ALL_NUMERIC,
995                 'select_submit'=> $submit_on_change,
996                 'async' => false,
997         ) );
998 }
999
1000 function tax_types_list_cells($label, $name, $selected_id=null, $none_option=false,
1001         $submit_on_change=false)
1002 {
1003         if ($label != null)
1004                 echo "<td>$label</td>\n";
1005         echo "<td>";
1006         echo tax_types_list($name, $selected_id, $none_option, $submit_on_change);
1007         echo "</td>\n";
1008 }
1009
1010 function tax_types_list_row($label, $name, $selected_id=null, $none_option=false,
1011         $submit_on_change=false)
1012 {
1013         echo "<tr><td class='label'>$label</td>";
1014         tax_types_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1015         echo "</tr>\n";
1016 }
1017
1018 //------------------------------------------------------------------------------------
1019
1020 function tax_groups_list($name, $selected_id=null,
1021         $none_option=false, $submit_on_change=false)
1022 {
1023         $sql = "SELECT id, name FROM ".TB_PREF."tax_groups";
1024
1025         return combo_input($name, $selected_id, $sql, 'id', 'name',
1026         array(
1027                 'order' => 'id',
1028                 'spec_option' => $none_option,
1029                 'spec_id' => ALL_NUMERIC,
1030                 'select_submit'=> $submit_on_change,
1031                 'async' => false,
1032         ) );
1033 }
1034
1035 function tax_groups_list_cells($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1036 {
1037         if ($label != null)
1038                 echo "<td>$label</td>\n";
1039         echo "<td>";
1040         echo tax_groups_list($name, $selected_id, $none_option, $submit_on_change);
1041         echo "</td>\n";
1042 }
1043
1044 function tax_groups_list_row($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1045 {
1046         echo "<tr><td class='label'>$label</td>";
1047         tax_groups_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1048         echo "</tr>\n";
1049 }
1050
1051 //------------------------------------------------------------------------------------
1052
1053 function item_tax_types_list($name, $selected_id=null)
1054 {
1055         $sql ="SELECT id, name FROM ".TB_PREF."item_tax_types";
1056         return combo_input($name, $selected_id, $sql, 'id', 'name', array('order' => 'id') );
1057 }
1058
1059 function item_tax_types_list_cells($label, $name, $selected_id=null)
1060 {
1061         if ($label != null)
1062                 echo "<td>$label</td>\n";
1063         echo "<td>";
1064         echo item_tax_types_list($name, $selected_id);
1065         echo "</td>\n";
1066 }
1067
1068 function item_tax_types_list_row($label, $name, $selected_id=null)
1069 {
1070         echo "<tr><td class='label'>$label</td>";
1071         item_tax_types_list_cells(null, $name, $selected_id);
1072         echo "</tr>\n";
1073 }
1074
1075 //------------------------------------------------------------------------------------
1076
1077 function shippers_list($name, $selected_id=null)
1078 {
1079         $sql = "SELECT shipper_id, shipper_name, inactive FROM ".TB_PREF."shippers";
1080         return combo_input($name, $selected_id, $sql, 'shipper_id', 'shipper_name', 
1081                 array('order'=>array('shipper_name')));
1082 }
1083
1084 function shippers_list_cells($label, $name, $selected_id=null)
1085 {
1086         if ($label != null)
1087                 echo "<td>$label</td>\n";
1088         echo "<td>";
1089         echo shippers_list($name, $selected_id);
1090         echo "</td>\n";
1091 }
1092
1093 function shippers_list_row($label, $name, $selected_id=null)
1094 {
1095         echo "<tr><td class='label'>$label</td>";
1096         shippers_list_cells(null, $name, $selected_id);
1097         echo "</tr>\n";
1098 }
1099
1100 //-------------------------------------------------------------------------------------
1101
1102 function sales_persons_list($name, $selected_id=null, $spec_opt=false)
1103 {
1104         $sql = "SELECT salesman_code, salesman_name, inactive FROM ".TB_PREF."salesman";
1105         return combo_input($name, $selected_id, $sql, 'salesman_code', 'salesman_name', 
1106                 array('order'=>array('salesman_name'),
1107                         'spec_option' => $spec_opt,
1108                         'spec_id' => ALL_NUMERIC));
1109 }
1110
1111 function sales_persons_list_cells($label, $name, $selected_id=null, $spec_opt=false)
1112 {
1113         if ($label != null)
1114                 echo "<td>$label</td>\n";
1115         echo "<td>\n";
1116         echo sales_persons_list($name, $selected_id, $spec_opt);
1117         echo "</td>\n";
1118 }
1119
1120 function sales_persons_list_row($label, $name, $selected_id=null, $spec_opt=false)
1121 {
1122         echo "<tr><td class='label'>$label</td>";
1123         sales_persons_list_cells(null, $name, $selected_id, $spec_opt);
1124         echo "</tr>\n";
1125 }
1126
1127 //------------------------------------------------------------------------------------
1128
1129 function sales_areas_list($name, $selected_id=null)
1130 {
1131         $sql = "SELECT area_code, description, inactive FROM ".TB_PREF."areas";
1132         return combo_input($name, $selected_id, $sql, 'area_code', 'description', array());
1133 }
1134
1135 function sales_areas_list_cells($label, $name, $selected_id=null)
1136 {
1137         if ($label != null)
1138                 echo "<td>$label</td>\n";
1139         echo "<td>";
1140         echo sales_areas_list($name, $selected_id);
1141         echo "</td>\n";
1142 }
1143
1144 function sales_areas_list_row($label, $name, $selected_id=null)
1145 {
1146         echo "<tr><td class='label'>$label</td>";
1147         sales_areas_list_cells(null, $name, $selected_id);
1148         echo "</tr>\n";
1149 }
1150
1151 //------------------------------------------------------------------------------------
1152
1153 function sales_groups_list($name, $selected_id=null, $special_option=false)
1154 {
1155         $sql = "SELECT id, description, inactive FROM ".TB_PREF."groups";
1156         return combo_input($name, $selected_id, $sql, 'id', 'description', array(
1157                 'spec_option' => $special_option===true ? ' ' : $special_option,
1158                 'order' => 'description', 'spec_id' => 0,
1159         ));
1160 }
1161
1162 function sales_groups_list_cells($label, $name, $selected_id=null, $special_option=false)
1163 {
1164         if ($label != null)
1165                 echo "<td>$label</td>\n";
1166         echo "<td>";
1167         echo sales_groups_list($name, $selected_id, $special_option);
1168         echo "</td>\n";
1169 }
1170
1171 function sales_groups_list_row($label, $name, $selected_id=null, $special_option=false)
1172 {
1173         echo "<tr><td class='label'>$label</td>";
1174         sales_groups_list_cells(null, $name, $selected_id, $special_option);
1175         echo "</tr>\n";
1176 }
1177
1178 //------------------------------------------------------------------------------------
1179
1180 function _format_template_items($row)
1181 {
1182         return ($row[0] . "&nbsp;- &nbsp;" . _("Amount") . "&nbsp;".$row[1]);
1183 }
1184
1185 function templates_list($name, $selected_id=null, $special_option=false)
1186 {
1187         $sql = "SELECT sorder.order_no, Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue
1188                 FROM ".TB_PREF."sales_orders as sorder, ".TB_PREF."sales_order_details as line
1189                 WHERE sorder.order_no = line.order_no AND sorder.type = 1 GROUP BY line.order_no";
1190         return combo_input($name, $selected_id, $sql, 'order_no', 'OrderValue', array(
1191                 'format' => '_format_template_items',
1192                 'spec_option' => $special_option===true ? ' ' : $special_option,
1193                 'order' => 'order_no', 'spec_id' => 0,
1194         ));
1195 }
1196
1197 function templates_list_cells($label, $name, $selected_id=null, $special_option=false)
1198 {
1199         if ($label != null)
1200                 echo "<td>$label</td>\n";
1201         echo "<td>";
1202         echo templates_list($name, $selected_id, $special_option);
1203         echo "</td>\n";
1204 }
1205
1206 function templates_list_row($label, $name, $selected_id=null, $special_option=false)
1207 {
1208         echo "<tr><td class='label'>$label</td>";
1209         templates_list_cells(null, $name, $selected_id, $special_option);
1210         echo "</tr>\n";
1211 }
1212
1213 //------------------------------------------------------------------------------------
1214
1215 function workorders_list($name, $selected_id=null)
1216 {
1217         $sql = "SELECT id, wo_ref FROM ".TB_PREF."workorders WHERE closed=0";
1218         return combo_input($name, $selected_id, $sql, 'id', 'wo_ref', array());
1219 }
1220
1221 function workorders_list_cells($label, $name, $selected_id=null)
1222 {
1223         if ($label != null)
1224                 echo "<td>$label</td>\n";
1225         echo "<td>";
1226         echo workorders_list($name, $selected_id);
1227         echo "</td>\n";
1228 }
1229
1230 function workorders_list_row($label, $name, $selected_id=null)
1231 {
1232         echo "<tr><td class='label'>$label</td>";
1233         workorders_list_cells(null, $name, $selected_id);
1234         echo "</tr>\n";
1235 }
1236
1237 //------------------------------------------------------------------------------------
1238
1239 function payment_terms_list($name, $selected_id=null)
1240 {
1241         $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
1242         return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms', array());
1243 }
1244
1245 function payment_terms_list_cells($label, $name, $selected_id=null)
1246 {
1247         if ($label != null)
1248                 echo "<td>$label</td>\n";
1249         echo "<td>";
1250         echo payment_terms_list($name, $selected_id);
1251         echo "</td>\n";
1252 }
1253
1254 function payment_terms_list_row($label, $name, $selected_id=null)
1255 {
1256         echo "<tr><td class='label'>$label</td>";
1257         payment_terms_list_cells(null, $name, $selected_id);
1258         echo "</tr>\n";
1259 }
1260
1261 //------------------------------------------------------------------------------------
1262
1263 function credit_status_list($name, $selected_id=null)
1264 {
1265         $sql ="SELECT id, reason_description, inactive FROM ".TB_PREF."credit_status";
1266         return combo_input($name, $selected_id, $sql, 'id', 'reason_description', array());
1267 }
1268
1269 function credit_status_list_cells($label, $name, $selected_id=null)
1270 {
1271         if ($label != null)
1272                 echo "<td>$label</td>\n";
1273         echo "<td>";
1274         echo credit_status_list($name, $selected_id);
1275         echo "</td>\n";
1276 }
1277
1278 function credit_status_list_row($label, $name, $selected_id=null)
1279 {
1280         echo "<tr><td class='label'>$label</td>";
1281         credit_status_list_cells(null, $name, $selected_id);
1282         echo "</tr>\n";
1283 }
1284
1285 //-----------------------------------------------------------------------------------------------
1286
1287 function sales_types_list($name, $selected_id=null, $submit_on_change=false, $special_option=false)
1288 {
1289         $sql = "SELECT id, sales_type, inactive FROM ".TB_PREF."sales_types";
1290
1291         return combo_input($name, $selected_id, $sql, 'id', 'sales_type',
1292         array(
1293                 'spec_option' => $special_option===true ? _("All Sales Types") : $special_option,
1294                 'spec_id' => 0,
1295                 'select_submit'=> $submit_on_change,
1296         //        'async' => false,
1297         ) );
1298 }
1299
1300 function sales_types_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1301 {
1302         if ($label != null)
1303                 echo "<td>$label</td>\n";
1304         echo "<td>";
1305         echo sales_types_list($name, $selected_id, $submit_on_change, $special_option);
1306         echo "</td>\n";
1307 }
1308
1309 function sales_types_list_row($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1310 {
1311         echo "<tr><td class='label'>$label</td>";
1312         sales_types_list_cells(null, $name, $selected_id, $submit_on_change, $special_option);
1313         echo "</tr>\n";
1314 }
1315
1316 //-----------------------------------------------------------------------------------------------
1317
1318 function movement_types_list($name, $selected_id=null)
1319 {
1320         $sql = "SELECT id, name FROM ".TB_PREF."movement_types";
1321         return combo_input($name, $selected_id, $sql, 'id', 'name', array());
1322 }
1323
1324 function movement_types_list_cells($label, $name, $selected_id=null)
1325 {
1326         if ($label != null)
1327                 echo "<td>$label</td>\n";
1328         echo "<td>";
1329         echo movement_types_list($name, $selected_id);
1330         echo "</td>\n";
1331 }
1332
1333 function movement_types_list_row($label, $name, $selected_id=null)
1334 {
1335         echo "<tr><td class='label'>$label</td>";
1336         movement_types_list_cells(null, $name, $selected_id);
1337         echo "</tr>\n";
1338 }
1339
1340 //-----------------------------------------------------------------------------------------------
1341 function _format_date($row)
1342 {
1343         return sql2date($row['reconciled']);
1344 }
1345
1346 function bank_reconciliation_list($account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1347 {
1348         $sql = "SELECT reconciled, reconciled FROM ".TB_PREF."bank_trans
1349                 WHERE bank_act=".db_escape($account)." AND reconciled IS NOT NULL
1350                 GROUP BY reconciled";
1351         return combo_input($name, $selected_id, $sql, 'id', 'reconciled',
1352         array(
1353                 'spec_option' => $special_option,
1354                 'format' => '_format_date',
1355                 'spec_id' => '',
1356                 'select_submit'=> $submit_on_change
1357         ) );
1358 }
1359
1360 function bank_reconciliation_list_cells($label,$account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1361 {
1362         if ($label != null)
1363                 echo "<td>$label</td>\n";
1364         echo "<td>";
1365         echo bank_reconciliation_list($account, $name, $selected_id, $submit_on_change, $special_option);
1366         echo "</td>\n";
1367 }
1368 /*
1369 function bank_reconciliation_list_row($label, $account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1370 {
1371         echo "<tr>\n";
1372         bank_reconciliation_list_cells($label, $account, $name, $selected_id, $submit_on_change, $special_option);
1373         echo "</tr>\n";
1374 }
1375 */
1376 //-----------------------------------------------------------------------------------------------
1377
1378 function workcenter_list($name, $selected_id=null, $all_option=false)
1379 {
1380         global $all_items;
1381
1382         $sql = "SELECT id, name, inactive FROM ".TB_PREF."workcentres";
1383
1384         return combo_input($name, $selected_id, $sql, 'id', 'name',
1385         array(
1386                 'spec_option' =>$all_option===true ? _("All Suppliers") : $all_option,
1387                 'spec_id' => $all_items,
1388         ) );
1389 }
1390
1391 function workcenter_list_cells($label, $name, $selected_id=null, $all_option=false)
1392 {
1393         default_focus($name);
1394         if ($label != null)
1395                 echo "<td>$label</td>\n";
1396         echo "<td>";
1397         echo workcenter_list($name, $selected_id, $all_option);
1398         echo "</td>\n";
1399 }
1400
1401 function workcenter_list_row($label, $name, $selected_id=null, $all_option=false)
1402 {
1403         echo "<tr><td class='label'>$label</td>";
1404         workcenter_list_cells(null, $name, $selected_id, $all_option);
1405         echo "</tr>\n";
1406 }
1407
1408 //-----------------------------------------------------------------------------------------------
1409
1410 function bank_accounts_list($name, $selected_id=null, $submit_on_change=false)
1411 {
1412         $sql = "SELECT ".TB_PREF."bank_accounts.id, bank_account_name, bank_curr_code, inactive
1413                 FROM ".TB_PREF."bank_accounts";
1414
1415         return combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1416         array(
1417                 'format' => '_format_add_curr',
1418                 'select_submit'=> $submit_on_change,
1419                 'async' => false
1420         ) );
1421 }
1422
1423 function bank_accounts_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1424 {
1425         if ($label != null)
1426                 echo "<td>$label</td>\n";
1427         echo "<td>";
1428         echo bank_accounts_list($name, $selected_id, $submit_on_change);
1429         echo "</td>\n";
1430 }
1431
1432 function bank_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1433 {
1434         echo "<tr><td class='label'>$label</td>";
1435         bank_accounts_list_cells(null, $name, $selected_id, $submit_on_change);
1436         echo "</tr>\n";
1437 }
1438 //-----------------------------------------------------------------------------------------------
1439
1440 function cash_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1441 {
1442         $sql = "SELECT ".TB_PREF."bank_accounts.id, bank_account_name, bank_curr_code, inactive
1443                 FROM ".TB_PREF."bank_accounts
1444                 WHERE ".TB_PREF."bank_accounts.account_type=".BT_CASH;
1445
1446         if ($label != null)
1447                 echo "<tr><td class='label'>$label</td>\n";
1448         echo "<td>";
1449         echo combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1450         array(
1451                 'format' => '_format_add_curr',
1452                 'select_submit'=> $submit_on_change,
1453                 'async' => true
1454         ) );
1455         echo "</td></tr>\n";
1456 }
1457 //-----------------------------------------------------------------------------------------------
1458
1459 function pos_list_row($label, $name, $selected_id=null, $spec_option=false, $submit_on_change=false)
1460 {
1461         $sql = "SELECT id, pos_name, inactive FROM ".TB_PREF."sales_pos";
1462
1463         default_focus($name);
1464         echo '<tr>';
1465         if ($label != null)
1466                 echo "<td class='label'>$label</td>\n";
1467         echo "<td>";
1468
1469         echo combo_input($name, $selected_id, $sql, 'id', 'pos_name',
1470         array(
1471                 'select_submit'=> $submit_on_change,
1472                 'async' => true,
1473                 'spec_option' =>$spec_option,
1474                 'spec_id' => -1,
1475                 'order'=> array('pos_name')
1476         ) );
1477         echo "</td></tr>\n";
1478
1479 }
1480 //-----------------------------------------------------------------------------------------------
1481 // Payment type selector for current user.
1482 //
1483 function sale_payment_list($name, $category, $selected_id=null, $submit_on_change=true)
1484 {
1485         $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
1486         
1487         if ($category == PM_CASH) // only cash
1488                         $sql .= " WHERE days_before_due=0 AND day_in_following_month=0";
1489         if ($category == PM_CREDIT) // only delayed payments
1490                         $sql .= " WHERE days_before_due!=0 OR day_in_following_month!=0";
1491
1492         return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms',
1493         array(
1494                 'select_submit'=> $submit_on_change,
1495                 'async' => true
1496         ) );
1497
1498 }
1499
1500 function sale_payment_list_cells($label, $name, $category, $selected_id=null, $submit_on_change=true)
1501 {
1502         if ($label != null)
1503                 echo "<td class='label'>$label</td>\n";
1504         echo "<td>";
1505
1506         echo sale_payment_list($name, $category, $selected_id, $submit_on_change);
1507
1508         echo "</td>\n";
1509 }
1510 //-----------------------------------------------------------------------------------------------
1511
1512 function class_list($name, $selected_id=null, $submit_on_change=false)
1513 {
1514         $sql = "SELECT cid, class_name FROM ".TB_PREF."chart_class";
1515
1516         return combo_input($name, $selected_id, $sql, 'cid', 'class_name',
1517         array(
1518                 'select_submit'=> $submit_on_change,
1519                 'async' => false
1520         ) );
1521
1522 }
1523
1524 function class_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1525 {
1526         if ($label != null)
1527                 echo "<td>$label</td>\n";
1528         echo "<td>";
1529         echo class_list($name, $selected_id, $submit_on_change);
1530         echo "</td>\n";
1531 }
1532
1533 function class_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1534 {
1535         echo "<tr><td class='label'>$label</td>";
1536         class_list_cells(null, $name, $selected_id, $submit_on_change);
1537         echo "</tr>\n";
1538 }
1539
1540 //-----------------------------------------------------------------------------------------------
1541 function stock_categories_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1542 {
1543         $sql = "SELECT category_id, description, inactive FROM ".TB_PREF."stock_category";
1544         return combo_input($name, $selected_id, $sql, 'category_id', 'description',
1545         array('order'=>'category_id',
1546                 'spec_option' => $spec_opt,
1547                 'spec_id' => -1,
1548                 'select_submit'=> $submit_on_change,
1549                 'async' => true
1550         ));
1551 }
1552
1553 function stock_categories_list_cells($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1554 {
1555         if ($label != null)
1556                 echo "<td>$label</td>\n";
1557         echo "<td>";
1558         echo stock_categories_list($name, $selected_id, $spec_opt, $submit_on_change);
1559         echo "</td>\n";
1560 }
1561
1562 function stock_categories_list_row($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1563 {
1564         echo "<tr><td class='label'>$label</td>";
1565         stock_categories_list_cells(null, $name, $selected_id, $spec_opt, $submit_on_change);
1566         echo "</tr>\n";
1567 }
1568
1569 //-----------------------------------------------------------------------------------------------
1570
1571 function gl_account_types_list($name, $selected_id=null, $all_option=false, $all=true)
1572 {
1573         global $all_items;
1574
1575         $sql = "SELECT id, name FROM ".TB_PREF."chart_types";
1576
1577         return combo_input($name, $selected_id, $sql, 'id', 'name',
1578         array(
1579                 'format' => '_format_account',
1580                 'order' => array('class_id', 'id', 'parent'),
1581                 'spec_option' =>$all_option,
1582                 'spec_id' => $all_items
1583         ) );
1584 }
1585
1586 function gl_account_types_list_cells($label, $name, $selected_id=null, $all_option=false, $all=false)
1587 {
1588         if ($label != null)
1589                 echo "<td>$label</td>\n";
1590         echo "<td>";
1591         echo gl_account_types_list($name, $selected_id, $all_option, $all);
1592         echo "</td>\n";
1593 }
1594
1595 function gl_account_types_list_row($label, $name, $selected_id=null, $all_option=false, $all=false)
1596 {
1597         echo "<tr><td class='label'>$label</td>";
1598         gl_account_types_list_cells(null, $name, $selected_id, $all_option,
1599                 $all);
1600         echo "</tr>\n";
1601 }
1602
1603 //-----------------------------------------------------------------------------------------------
1604 function gl_all_accounts_list($name, $selected_id=null, $skip_bank_accounts=false,
1605         $cells=false, $all_option=false, $submit_on_change=false, $all=false)
1606 {
1607         if ($skip_bank_accounts)
1608                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
1609                         FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
1610                         ."LEFT JOIN ".TB_PREF."bank_accounts acc "
1611                         ."ON chart.account_code=acc.account_code
1612                                 WHERE acc.account_code  IS NULL
1613                         AND chart.account_type=type.id";
1614         else
1615                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
1616                         FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type
1617                         WHERE chart.account_type=type.id";
1618
1619         return combo_input($name, $selected_id, $sql, 'chart.account_code', 'chart.account_name',
1620         array(
1621                 'format' => '_format_account',
1622                 'spec_option' => $all_option===true ?  _("Use Item Sales Accounts") : $all_option,
1623                 'spec_id' => '',
1624                 'type' => 2,
1625                 'order' => array('type.class_id','type.id','account_code'),
1626                 'search_box' => $cells,
1627                         'search_submit' => false,
1628                         'size' => 12,
1629                         'max' => 10,
1630                         'cells' => true,
1631                 'select_submit'=> $submit_on_change,
1632                 'async' => false,
1633                 'category' => 2,
1634                 'show_inactive' => $all
1635         ) );
1636
1637 }
1638
1639 function _format_account($row)
1640 {
1641                 return $row[0] .  "&nbsp;&nbsp;&nbsp;&nbsp;" . $row[1];
1642 }
1643
1644 function gl_all_accounts_list_cells($label, $name, $selected_id=null, 
1645         $skip_bank_accounts=false, $cells=false, $all_option=false, 
1646         $submit_on_change=false, $all=false)
1647 {
1648         if ($label != null)
1649                 echo "<td>$label</td>\n";
1650         echo "<td>";
1651         echo gl_all_accounts_list($name, $selected_id, 
1652                 $skip_bank_accounts, $cells, $all_option, $submit_on_change, $all);
1653         echo "</td>\n";
1654 }
1655
1656 function gl_all_accounts_list_row($label, $name, $selected_id=null, 
1657         $skip_bank_accounts=false, $cells=false, $all_option=false)
1658 {
1659         echo "<tr><td class='label'>$label</td>";
1660         gl_all_accounts_list_cells(null, $name, $selected_id, 
1661                 $skip_bank_accounts, $cells, $all_option);
1662         echo "</tr>\n";
1663 }
1664
1665 function yesno_list($name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1666 {
1667         $items = array();
1668         $items['0'] = strlen($name_no) ? $name_no : _("No");
1669         $items['1'] = strlen($name_yes) ? $name_yes : _("Yes");
1670
1671         return array_selector($name, $selected_id, $items, 
1672                 array( 
1673                         'select_submit'=> $submit_on_change,
1674                         'async' => false ) ); // FIX?
1675 }
1676
1677 function yesno_list_cells($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1678 {
1679         if ($label != null)
1680                 echo "<td>$label</td>\n";
1681         echo "<td>";
1682         echo yesno_list($name, $selected_id, $name_yes, $name_no, $submit_on_change);
1683         echo "</td>\n";
1684 }
1685
1686 function yesno_list_row($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1687 {
1688         echo "<tr><td class='label'>$label</td>";
1689         yesno_list_cells(null, $name, $selected_id, $name_yes, $name_no, $submit_on_change);
1690         echo "</tr>\n";
1691 }
1692
1693 //------------------------------------------------------------------------------------------------
1694
1695 function languages_list($name, $selected_id=null, $all_option=false)
1696 {
1697         global $installed_languages;
1698
1699         $items = array();
1700         if ($all_option)
1701                 $items[''] = $all_option;
1702         foreach ($installed_languages as $lang)
1703                         $items[$lang['code']] = $lang['name'];
1704         return array_selector($name, $selected_id, $items);
1705 }
1706
1707 function languages_list_cells($label, $name, $selected_id=null, $all_option=false)
1708 {
1709         if ($label != null)
1710                 echo "<td>$label</td>\n";
1711         echo "<td>";
1712         echo languages_list($name, $selected_id, $all_option);
1713         echo "</td>\n";
1714 }
1715
1716 function languages_list_row($label, $name, $selected_id=null, $all_option=false)
1717 {
1718         echo "<tr><td class='label'>$label</td>";
1719         languages_list_cells(null, $name, $selected_id, $all_option);
1720         echo "</tr>\n";
1721 }
1722
1723 //------------------------------------------------------------------------------------------------
1724
1725 function bank_account_types_list($name, $selected_id=null)
1726 {
1727         global $bank_account_types;
1728
1729         return array_selector($name, $selected_id, $bank_account_types);
1730 }
1731
1732 function bank_account_types_list_cells($label, $name, $selected_id=null)
1733 {
1734         if ($label != null)
1735                 echo "<td>$label</td>\n";
1736         echo "<td>";
1737         echo bank_account_types_list($name, $selected_id);
1738         echo "</td>\n";
1739 }
1740
1741 function bank_account_types_list_row($label, $name, $selected_id=null)
1742 {
1743         echo "<tr><td class='label'>$label</td>";
1744         bank_account_types_list_cells(null, $name, $selected_id);
1745         echo "</tr>\n";
1746 }
1747
1748 //------------------------------------------------------------------------------------------------
1749 function payment_person_types_list($name, $selected_id=null, $submit_on_change=false)
1750 {
1751         global $payment_person_types;
1752
1753         $items = array();
1754         foreach ($payment_person_types as $key=>$type)
1755         {
1756                 if ($key != PT_WORKORDER)
1757                         $items[$key] = $type;
1758         }               
1759         return array_selector($name, $selected_id, $items, 
1760                 array( 'select_submit'=> $submit_on_change ) );
1761 }
1762
1763 function payment_person_types_list_cells($label, $name, $selected_id=null, $related=null)
1764 {
1765         if ($label != null)
1766                 echo "<td>$label</td>\n";
1767         echo "<td>";
1768         echo payment_person_types_list($name, $selected_id, $related);
1769         echo "</td>\n";
1770 }
1771
1772 function payment_person_types_list_row($label, $name, $selected_id=null, $related=null)
1773 {
1774         echo "<tr><td class='label'>$label</td>";
1775         payment_person_types_list_cells(null, $name, $selected_id, $related);
1776         echo "</tr>\n";
1777 }
1778
1779 //------------------------------------------------------------------------------------------------
1780
1781 function wo_types_list($name, $selected_id=null)
1782 {
1783         global $wo_types_array;
1784         
1785         return array_selector($name, $selected_id, $wo_types_array, 
1786                 array( 'select_submit'=> true, 'async' => true ) );
1787 }
1788
1789 function wo_types_list_row($label, $name, $selected_id=null)
1790 {
1791         echo "<tr><td class='label'>$label</td><td>\n";
1792         echo wo_types_list($name, $selected_id);
1793         echo "</td></tr>\n";
1794 }
1795
1796 //------------------------------------------------------------------------------------------------
1797
1798 function dateformats_list_row($label, $name, $value=null)
1799 {
1800         global $dateformats;
1801
1802         echo "<tr><td class='label'>$label</td>\n<td>";
1803         echo array_selector( $name, $value, $dateformats );
1804         echo "</td></tr>\n";
1805 }
1806
1807 function dateseps_list_row($label, $name, $value=null)
1808 {
1809         global $dateseps;
1810
1811         echo "<tr><td class='label'>$label</td>\n<td>";
1812         echo array_selector( $name, $value, $dateseps );
1813         echo "</td></tr>\n";
1814 }
1815
1816 function thoseps_list_row($label, $name, $value=null)
1817 {
1818         global $thoseps;
1819
1820         echo "<tr><td class='label'>$label</td>\n<td>";
1821         echo array_selector( $name, $value, $thoseps );
1822         echo "</td></tr>\n";
1823 }
1824
1825 function decseps_list_row($label, $name, $value=null)
1826 {
1827         global $decseps;
1828
1829         echo "<tr><td class='label'>$label</td>\n<td>";
1830         echo array_selector( $name, $value, $decseps );
1831         echo "</td></tr>\n";
1832 }
1833
1834 function themes_list_row($label, $name, $value=null)
1835 {
1836         global $path_to_root;
1837
1838         $path = $path_to_root.'/themes/';
1839         $themes = array();
1840         $themedir = opendir($path);
1841         while(false !== ($fname = readdir($themedir)))
1842         {
1843                 if($fname!='.' && $fname!='..' && $fname!='CVS' && is_dir($path.$fname))
1844                 {
1845                         $themes[$fname] =  $fname;
1846                 }
1847         }
1848         ksort($themes);
1849
1850         echo "<tr><td class='label'>$label</td>\n<td>";
1851         echo array_selector( $name, $value, $themes );
1852         echo "</td></tr>\n";
1853 }
1854
1855 function pagesizes_list_row($label, $name, $value=null)
1856 {
1857         global $pagesizes;
1858
1859         $items = array();
1860         foreach ($pagesizes as $pz)
1861                 $items[$pz] = $pz;
1862
1863         echo "<tr><td class='label'>$label</td>\n<td>";
1864         echo array_selector( $name, $value, $items );
1865         echo "</td></tr>\n";
1866 }
1867
1868 function systypes_list($name, $value=null, $spec_opt=false, $submit_on_change=false, $exclude=array())
1869 {
1870         global $systypes_array;
1871
1872         // emove non-voidable transactions if needed
1873         $systypes = array_diff_key($systypes_array, array_flip($exclude));
1874         return array_selector($name, $value, $systypes, 
1875                 array( 
1876                         'spec_option'=> $spec_opt,
1877                         'spec_id' => ALL_NUMERIC,
1878                         'select_submit'=> $submit_on_change,
1879                         'async' => false,
1880                         )
1881         );
1882 }
1883
1884 function systypes_list_cells($label, $name, $value=null, $submit_on_change=false, $exclude=array())
1885 {
1886         if ($label != null)
1887                 echo "<td>$label</td>\n";
1888         echo "<td>";
1889         echo systypes_list($name, $value, false, $submit_on_change, $exclude);
1890         echo "</td>\n";
1891 }
1892
1893 function systypes_list_row($label, $name, $value=null, $submit_on_change=false, $exclude=array())
1894 {
1895         echo "<tr><td class='label'>$label</td>";
1896         systypes_list_cells(null, $name, $value, $submit_on_change, $exclude);
1897         echo "</tr>\n";
1898 }
1899
1900 function journal_types_list_cells($label, $name, $value=null, $submit_on_change=false)
1901 {
1902         global $systypes_array;
1903
1904         if ($label != null)
1905                 echo "<td>$label</td>\n";
1906         echo "<td>";
1907
1908         $items = $systypes_array;
1909
1910         // exclude quotes, orders and dimensions
1911         foreach (array(ST_PURCHORDER, ST_WORKORDER, ST_SALESORDER, ST_DIMENSION, 
1912                                 ST_SALESQUOTE) as $excl)
1913                         unset($items[$excl]);
1914         
1915         echo array_selector($name, $value, $items, 
1916                 array( 
1917                         'spec_option'=> _("All"),
1918                         'spec_id' => -1,
1919                         'select_submit'=> $submit_on_change,
1920                         'async' => false
1921                         )
1922         );
1923         echo "</td>\n";
1924 }
1925
1926 function cust_allocations_list_cells($label, $name, $selected=null)
1927 {
1928         global $all_items;
1929
1930         if ($label != null)
1931                 label_cell($label);
1932         echo "<td>\n";
1933         $allocs = array( 
1934                 $all_items=>_("All Types"),
1935                 '1'=> _("Sales Invoices"),
1936                 '2'=> _("Overdue Invoices"),
1937                 '3' => _("Payments"),
1938                 '4' => _("Credit Notes"),
1939                 '5' => _("Delivery Notes")
1940         );
1941         echo array_selector($name, $selected, $allocs);
1942         echo "</td>\n";
1943 }
1944
1945 function supp_allocations_list_cell($name, $selected=null)
1946 {
1947         global $all_items;
1948
1949         echo "<td>\n";
1950         $allocs = array( 
1951                 $all_items=>_("All Types"),
1952                 '1'=> _("Invoices"),
1953                 '2'=> _("Overdue Invoices"),
1954                 '3' => _("Payments"),
1955                 '4' => _("Credit Notes"),
1956                 '5' => _("Overdue Credit Notes")
1957         );
1958         echo array_selector($name, $selected, $allocs);
1959         echo "</td>\n";
1960 }
1961
1962 function supp_transactions_list_cell($name, $selected=null)
1963 {
1964         global $all_items;
1965
1966         echo "<td>\n";
1967         $allocs = array( 
1968                 $all_items=>_("All Types"),
1969                 '6'=>_("GRNs"),
1970                 '1'=> _("Invoices"),
1971                 '2'=> _("Overdue Invoices"),
1972                 '3' => _("Payments"),
1973                 '4' => _("Credit Notes"),
1974                 '5' => _("Overdue Credit Notes")
1975         );
1976
1977         echo array_selector($name, $selected, $allocs);
1978         echo "</td>\n";
1979 }
1980
1981 function policy_list_cells($label, $name, $selected=null)
1982 {
1983         if ($label != null)
1984                 label_cell($label);
1985         echo "<td>\n";
1986         echo array_selector($name, $selected, 
1987                                 array( '' => _("Automatically put balance on back order"),
1988                                         'CAN' => _("Cancel any quantites not delivered")) );
1989         echo "</td>\n";
1990 }
1991
1992 function policy_list_row($label, $name, $selected=null)
1993 {
1994         echo "<tr><td class='label'>$label</td>";
1995         policy_list_cells(null, $name, $selected);
1996         echo "</tr>\n";
1997 }
1998
1999 function credit_type_list_cells($label, $name, $selected=null, $submit_on_change=false)
2000 {
2001         if ($label != null)
2002                 label_cell($label);
2003         echo "<td>\n";
2004         echo array_selector($name, $selected, 
2005                                 array( 'Return' => _("Items Returned to Inventory Location"),
2006                                         'WriteOff' => _("Items Written Off")),
2007                                 array( 'select_submit'=> $submit_on_change ) );
2008         echo "</td>\n";
2009 }
2010
2011 function credit_type_list_row($label, $name, $selected=null, $submit_on_change=false)
2012 {
2013         echo "<tr><td class='label'>$label</td>";
2014         credit_type_list_cells(null, $name, $selected, $submit_on_change);
2015         echo "</tr>\n";
2016 }
2017
2018 function number_list($name, $selected, $from, $to, $no_option=false)
2019 {
2020         $items = array();
2021         for ($i = $from; $i <= $to; $i++)
2022                 $items[$i] = "$i";
2023
2024         return array_selector($name, $selected, $items,
2025                                 array(  'spec_option' => $no_option,
2026                                                 'spec_id' => ALL_NUMERIC) );
2027 }
2028
2029 function number_list_cells($label, $name, $selected, $from, $to, $no_option=false)
2030 {
2031         if ($label != null)
2032                 label_cell($label);
2033         echo "<td>\n";
2034         echo number_list($name, $selected, $from, $to, $no_option);
2035         echo "</td>\n";
2036 }
2037
2038 function number_list_row($label, $name, $selected, $from, $to, $no_option=false)
2039 {
2040         echo "<tr><td class='label'>$label</td>";
2041         echo number_list_cells(null, $name, $selected, $from, $to, $no_option);
2042         echo "</tr>\n";
2043 }
2044
2045 function print_profiles_list_row($label, $name, $selected_id=null, $spec_opt=false,
2046         $submit_on_change=true)
2047 {
2048         $sql = "SELECT profile FROM ".TB_PREF."print_profiles"
2049                 ." GROUP BY profile";
2050         $result = db_query($sql, 'cannot get all profile names');
2051         $profiles = array();
2052         while($myrow=db_fetch($result)) {
2053                 $profiles[$myrow['profile']] = $myrow['profile'];
2054         }
2055
2056         echo "<tr>";
2057         if ($label != null)
2058                 echo "<td class='label'>$label</td>\n";
2059         echo "<td>";
2060
2061         echo array_selector($name, $selected_id, $profiles, 
2062                 array( 'select_submit'=> $submit_on_change,
2063                         'spec_option'=>$spec_opt,
2064                         'spec_id' => ''
2065                  ));
2066
2067         echo "</td></tr>\n";
2068 }
2069
2070 function printers_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
2071 {
2072         static $printers; // query only once for page display
2073
2074         if (!$printers) {
2075                 $sql = "SELECT id, name, description FROM ".TB_PREF."printers"; 
2076                 $result = db_query($sql, 'cannot get all printers');
2077                 $printers = array();
2078                 while($myrow=db_fetch($result)) {
2079                         $printers[$myrow['id']] = $myrow['name'].'&nbsp;-&nbsp;'.$myrow['description'];
2080                 }
2081         }
2082         return array_selector($name, $selected_id, $printers, 
2083                 array( 'select_submit'=> $submit_on_change,
2084                         'spec_option'=>$spec_opt,
2085                         'spec_id' => ''
2086                  ));
2087 }
2088
2089 //------------------------------------------------------------------------------------------------
2090
2091 function quick_entries_list($name, $selected_id=null, $type=null, $submit_on_change=false)
2092 {
2093         $where = false;
2094         $sql = "SELECT id, description FROM ".TB_PREF."quick_entries";
2095         if ($type != null)
2096                 $sql .= " WHERE type=$type";
2097
2098         return combo_input($name, $selected_id, $sql, 'id', 'description',
2099                 array(
2100                         'spec_id' => '',
2101                         'order' => 'description',
2102                         'select_submit'=> $submit_on_change,
2103                         'async' => false
2104                 ) );
2105
2106 }
2107
2108 function quick_entries_list_cells($label, $name, $selected_id=null, $type, $submit_on_change=false)
2109 {
2110         if ($label != null)
2111                 echo "<td>$label</td>\n";
2112         echo "<td>";    
2113         echo quick_entries_list($name, $selected_id, $type, $submit_on_change);
2114         echo "</td>";
2115 }
2116
2117 function quick_entries_list_row($label, $name, $selected_id=null, $type, $submit_on_change=false)
2118 {
2119         echo "<tr><td class='label'>$label</td>";
2120         quick_entries_list_cells(null, $name, $selected_id, $type, $submit_on_change);
2121         echo "</tr>\n";
2122 }
2123
2124
2125 function quick_actions_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2126 {
2127         global $quick_actions;
2128         
2129         echo "<tr><td class='label'>$label</td><td>";
2130         echo array_selector($name, $selected_id, $quick_actions, 
2131                 array( 
2132                         'select_submit'=> $submit_on_change
2133                 ) );
2134         echo "</td></tr>\n";
2135 }
2136
2137 function quick_entry_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2138 {
2139         global $quick_entry_types;
2140                 
2141         echo "<tr><td class='label'>$label</td><td>";
2142         echo array_selector($name, $selected_id, $quick_entry_types, 
2143                 array( 
2144                         'select_submit'=> $submit_on_change
2145                         ) );
2146         echo "</td></tr>\n";
2147 }
2148
2149 function record_status_list_row($label, $name) {
2150         return yesno_list_row($label, $name, null,      _('Inactive'), _('Active'));
2151 }
2152
2153 function class_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2154 {
2155         global $class_types;
2156         
2157         echo "<tr><td class='label'>$label</td><td>";
2158         echo array_selector($name, $selected_id, $class_types, 
2159                 array( 
2160                         'select_submit'=> $submit_on_change
2161                 ) );
2162         echo "</td></tr>\n";
2163 }
2164
2165 //------------------------------------------------------------------------------------------------
2166
2167 function security_roles_list($name, $selected_id=null, $new_item=false, $submit_on_change=false,
2168         $show_inactive = false)
2169 {
2170         global $all_items;
2171
2172         $sql = "SELECT id, role, inactive FROM ".TB_PREF."security_roles";
2173
2174 return combo_input($name, $selected_id, $sql, 'id', 'description',
2175         array(
2176                 'spec_option'=>$new_item ? _("New role") : false,
2177                 'spec_id' => '',
2178                 'select_submit'=> $submit_on_change,
2179                 'show_inactive' => $show_inactive
2180         ) );
2181 }
2182
2183 function security_roles_list_cells($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2184         $show_inactive = false)
2185 {
2186         if ($label != null)
2187                 echo "<td>$label</td>\n";
2188         echo "<td>";
2189         echo security_roles_list($name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2190         echo "</td>\n";
2191 }
2192
2193 function security_roles_list_row($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2194         $show_inactive = false)
2195 {
2196         echo "<tr><td class='label'>$label</td>";
2197         security_roles_list_cells(null, $name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2198         echo "</tr>\n";
2199 }
2200
2201 function tab_list_row($label, $name, $selected_id=null, $all = false)
2202 {
2203         global $installed_extensions;
2204         
2205         $tabs = array();
2206         foreach ($_SESSION['App']->applications as $app) {
2207                 $tabs[$app->id] = access_string($app->name, true);
2208         }
2209         if ($all) {     // add also not active ext. modules
2210                 foreach ($installed_extensions as $ext) {
2211                         if ($ext['type'] == 'module' && !$ext['active'])
2212                                 $tabs[$ext['tab']] = access_string($ext['title'], true);
2213                 }
2214         }
2215         echo "<tr>\n";
2216         echo "<td class='label'>$label</td><td>\n";
2217         echo array_selector($name, $selected_id, $tabs);
2218         echo "</td></tr>\n";
2219 }
2220
2221 //-----------------------------------------------------------------------------------------------
2222
2223 function tag_list($name, $height, $type, $multi=false, $all=false, $spec_opt = false)
2224 {
2225         // Get tags
2226         global $path_to_root;
2227         include_once($path_to_root . "/admin/db/tags_db.inc");
2228         $results = get_tags($type, $all);
2229
2230         while ($tag = db_fetch($results))
2231                 $tags[$tag['id']] = $tag['name'];
2232         
2233         if (!isset($tags)) {
2234                 $tags[''] = $all ? _("No tags defined.") : _("No active tags defined.");
2235                 $spec_opt = false;
2236         }
2237         return array_selector($name, null, $tags,
2238                 array(
2239                         'multi' => $multi,
2240                         'height' => $height,
2241                         'spec_option'=> $spec_opt,
2242                         'spec_id' => -1,
2243                 ) );
2244 }
2245
2246 function tag_list_cells($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2247 {
2248         if ($label != null)
2249                 echo "<td>$label</td>\n";
2250         echo "<td>\n";
2251         echo tag_list($name, $height, $type, $mult, $all, $spec_opt);
2252         echo "</td>\n";
2253         
2254 }
2255
2256 function tag_list_row($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2257 {
2258         echo "<tr><td class='label'>$label</td>";
2259         tag_list_cells(null, $name, $height, $type, $mult, $all, $spec_opt);
2260         echo "</tr>\n"; 
2261 }
2262
2263 //---------------------------------------------------------------------------------------------
2264 //      List of sets of active extensions 
2265 //
2266 function extset_list($name, $value=null, $submit_on_change=false)
2267 {
2268         global $db_connections;
2269
2270         $items = array();
2271         foreach ($db_connections as $comp)
2272                 $items[] = sprintf(_("Activated for '%s'"), $comp['name']);
2273         return array_selector( $name, $value, $items,
2274                 array(
2275                         'spec_option'=> _("Available and/or installed"),
2276                         'spec_id' => -1,
2277                         'select_submit'=> $submit_on_change,
2278                         'async' => true
2279                 ));
2280 }
2281
2282 function crm_category_types_list($name, $selected_id=null, $filter=array(), $submit_on_change=true)
2283 {
2284
2285         $sql = "SELECT id, name, type, inactive FROM ".TB_PREF."crm_categories";
2286
2287         $multi = false;
2288         $groups = false;
2289         $where = array();
2290         if (@$filter['class']) {
2291                 $where[] = 'type='.db_escape($filter['class']);
2292         } else
2293                 $groups = 'type';
2294         if (@$filter['subclass']) $where[] = 'action='.db_escape($filter['subclass']);
2295         if (@$filter['entity']) $where[] = 'entity_id='.db_escape($filter['entity']);
2296         if (@$filter['multi']) { // contact category selector for person
2297                 $multi = true;
2298         }
2299
2300         return combo_input($name, $selected_id, $sql, 'id', 'name',
2301                 array(
2302                         'multi' => $multi,
2303                         'height' => $multi ? 5:1,
2304                         'category' => $groups,
2305                         'select_submit'=> $submit_on_change,
2306                         'async' => true,
2307                         'where' => $where
2308                 ));
2309 }
2310
2311 function crm_category_types_list_row($label, $name, $selected_id=null, $filter=array(), $submit_on_change=true)
2312 {
2313         echo "<tr><td class='label'>$label</td><td>";
2314         echo crm_category_types_list($name, $selected_id, $filter, $submit_on_change);
2315         echo "</td></tr>\n";
2316 }
2317
2318 function payment_type_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2319 {
2320         global $pterm_types;
2321         
2322         echo "<tr><td class='label'>$label</td><td>";
2323         echo array_selector($name, $selected_id, $pterm_types, 
2324                 array( 
2325                         'select_submit'=> $submit_on_change
2326                 ) );
2327         echo "</td></tr>\n";
2328 }
2329
2330 function coa_list_row($label, $name, $value=null)
2331 {
2332         global $path_to_root, $installed_extensions;
2333
2334         $path = $path_to_root.'/sql/';
2335         $coas = array();
2336         $sqldir = opendir($path);
2337         while (false !== ($fname = readdir($sqldir)))
2338         {
2339                 if (is_file($path.$fname) && substr($fname,-4)=='.sql' && @($fname[2] == '_'))
2340                 {
2341                         $ext = array_search_value($fname, $installed_extensions, 'sql');
2342                         if ($ext!=null) {
2343                                 $descr = $ext['name'];
2344                         } elseif ($fname == 'en_US-new.sql') { // two standard COAs
2345                                 $descr = _("Standard new company American COA (4 digit)");
2346                         } elseif ($fname == 'en_US-demo.sql') {
2347                                 $descr = _("Standard American COA (4 digit) with demo data");
2348                         } else
2349                                 $descr = $fname;
2350
2351                         $coas[$fname] =  $descr;
2352                 }
2353         }
2354         ksort($coas);
2355
2356         echo "<tr><td class='label'>$label</td>\n<td>";
2357         echo array_selector( $name, $value, $coas );
2358         echo "</td></tr>\n";
2359 }
2360
2361 function payment_services($name)
2362 {
2363         global $payment_services;
2364
2365         $services = array_combine(array_keys($payment_services), array_keys($payment_services));
2366
2367         return array_selector($name, null, $services, array(
2368                         'spec_option'=> _("No payment Link"),
2369                         'spec_id' => '',
2370                 ));
2371 }
2372
2373 ?>