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