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