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