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