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