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