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