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