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