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