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