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