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