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