Exchange Rates: removed sparse home currency from selector.
[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         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
991                 array('where'=>array("stock_id != $parent"), 'cells'=>true, 'parent'=> $parent_stock_id), $editkey, "component");
992 }
993 //------------------------------------------------------------------------------------
994
995 function stock_costable_items_list($name, $selected_id=null,
996         $all_option=false, $submit_on_change=false)
997 {
998         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
999                 array('where'=>array("mb_flag!='D'")), false, "stock_costable");
1000 }
1001
1002 function stock_costable_items_list_cells($label, $name, $selected_id=null, 
1003         $all_option=false, $submit_on_change=false)
1004 {
1005         if ($label != null)
1006                 echo "<td>$label</td>\n";
1007         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1008                 array('where'=>array("mb_flag!='D'"), 'cells'=>true), false, "stock_costable");
1009 }
1010
1011 //------------------------------------------------------------------------------------
1012 function stock_purchasable_items_list($name, $selected_id=null, 
1013         $all_option=false, $submit_on_change=false, $all=false, $editkey=false)
1014 {
1015         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1016                 array('where'=>array("NOT no_purchase"), 
1017                         'show_inactive'=>$all), $editkey, "stock_purchased");
1018 }
1019 //
1020 //      This helper is used in PO/GRN/PI entry and supports editable descriptions.
1021 //
1022 function stock_purchasable_items_list_cells($label, $name, $selected_id=null,
1023                         $all_option=false, $submit_on_change=false, $editkey=false)
1024 {
1025         if ($label != null)
1026                 echo "<td>$label</td>\n";
1027         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1028                 array('where'=>array("NOT no_purchase"), 
1029                          'editable' => 30,
1030                          'cells'=>true), $editkey);
1031 }
1032
1033 //------------------------------------------------------------------------------------
1034
1035 function stock_item_types_list_row($label, $name, $selected_id=null, $enabled=true)
1036 {
1037         global $stock_types;
1038
1039         echo "<tr>";
1040         if ($label != null)
1041                 echo "<td class='label'>$label</td>\n";
1042         echo "<td>";
1043
1044         echo array_selector($name, $selected_id, $stock_types, 
1045                 array( 
1046                         'select_submit'=> true, 
1047                         'disabled' => !$enabled) );
1048         echo "</td></tr>\n";
1049 }
1050
1051 function stock_units_list_row($label, $name, $value=null, $enabled=true)
1052 {
1053         $result = get_all_item_units();
1054         echo "<tr>";
1055         if ($label != null)
1056                 echo "<td class='label'>$label</td>\n";
1057         echo "<td>";
1058
1059         while($unit = db_fetch($result))
1060                 $units[$unit['abbr']] = $unit['name'];
1061
1062         echo array_selector($name, $value, $units, array( 'disabled' => !$enabled) );
1063
1064         echo "</td></tr>\n";
1065 }
1066
1067 //------------------------------------------------------------------------------------
1068
1069 function stock_purchasable_fa_list_cells($label, $name, $selected_id=null, $all_option=false,
1070         $submit_on_change=false, $all=false, $editkey = false, $exclude_items = array())
1071 {
1072         // Check if a fixed asset has been bought.
1073         $where_opts[] = "stock_id NOT IN
1074         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )";
1075
1076         // exclude items currently on the order.
1077         foreach($exclude_items as $item) {
1078         $where_opts[] = "stock_id != ".db_escape($item->stock_id);
1079         }
1080         $where_opts[] = "mb_flag='F'";
1081
1082         echo stock_items_list_cells($label, $name, $selected_id, $all_option, $submit_on_change, $all, $editkey, 
1083     array('fixed_asset' => true, 'where' => $where_opts));
1084 }
1085
1086 function stock_disposable_fa_list($name, $selected_id=null,
1087         $all_option=false, $submit_on_change=false)
1088 {
1089         // Check if a fixed asset has been bought....
1090         $where_opts[] = "stock_id IN
1091         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )";
1092         // ...but has not been disposed or sold already.
1093         $where_opts[] = "stock_id NOT IN
1094                 ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )";
1095
1096         $where_opts[] = "mb_flag='F'";
1097
1098         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1099     array('fixed_asset' => true, 'where' => $where_opts));
1100 }
1101
1102 function stock_disposable_fa_list_cells($label, $name, $selected_id=null,
1103         $all_option=false, $submit_on_change=false, $exclude_items = array())
1104 {
1105         // Check if a fixed asset has been bought....
1106         $where_opts[] = "stock_id IN
1107         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )";
1108         // ...but has not been disposed or sold already.
1109         $where_opts[] = "stock_id NOT IN
1110         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )";
1111
1112         $where_opts[] = "mb_flag='F'";
1113
1114         foreach($exclude_items as $item) {
1115         $where_opts[] = "stock_id != ".db_escape($item->stock_id);
1116         }
1117
1118         if ($label != null)
1119                 echo "<td>$label</td>\n";
1120         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change, 
1121     array('fixed_asset' => true, 'cells'=>true, 'where' => $where_opts));
1122 }
1123
1124 function stock_depreciable_fa_list_cells($label, $name, $selected_id=null,
1125         $all_option=false, $submit_on_change=false)
1126 {
1127
1128         // Check if a fixed asset has been bought....
1129         $where_opts[] = "stock_id IN
1130         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )";
1131         // ...but has not been disposed or sold already.
1132         $where_opts[] = "stock_id NOT IN
1133         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )";
1134
1135         $year = get_current_fiscalyear();
1136         $y = date('Y', strtotime($year['end']));
1137
1138         // check if current fiscal year
1139         $where_opts[] = "depreciation_date < '".$y."-12-01'";
1140         $where_opts[] = "depreciation_date >= '".($y-1)."-12-01'";
1141
1142         $where_opts[] = "material_cost > 0";
1143         $where_opts[] = "mb_flag='F'";
1144
1145         if ($label != null)
1146                 echo "<td>$label</td>\n";
1147         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1148                  array('fixed_asset' => true, 'where' => $where_opts, 'cells'=>true));
1149 }
1150
1151 //------------------------------------------------------------------------------------
1152
1153 function tax_types_list($name, $selected_id=null, $none_option=false, $submit_on_change=false)
1154 {
1155         $sql = "SELECT id, CONCAT(name, ' (',rate,'%)') as name FROM ".TB_PREF."tax_types";
1156
1157         return combo_input($name, $selected_id, $sql, 'id', 'name',
1158                 array(
1159                         'spec_option' => $none_option,
1160                         'spec_id' => ALL_NUMERIC,
1161                         'select_submit'=> $submit_on_change,
1162                         'async' => false,
1163                 ) );
1164 }
1165
1166 function tax_types_list_cells($label, $name, $selected_id=null, $none_option=false,
1167         $submit_on_change=false)
1168 {
1169         if ($label != null)
1170                 echo "<td>$label</td>\n";
1171         echo "<td>";
1172         echo tax_types_list($name, $selected_id, $none_option, $submit_on_change);
1173         echo "</td>\n";
1174 }
1175
1176 function tax_types_list_row($label, $name, $selected_id=null, $none_option=false,
1177         $submit_on_change=false)
1178 {
1179         echo "<tr><td class='label'>$label</td>";
1180         tax_types_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1181         echo "</tr>\n";
1182 }
1183
1184 //------------------------------------------------------------------------------------
1185
1186 function tax_groups_list($name, $selected_id=null,
1187         $none_option=false, $submit_on_change=false)
1188 {
1189         $sql = "SELECT id, name FROM ".TB_PREF."tax_groups";
1190
1191         return combo_input($name, $selected_id, $sql, 'id', 'name',
1192                 array(
1193                         'order' => 'id',
1194                         'spec_option' => $none_option,
1195                         'spec_id' => ALL_NUMERIC,
1196                         'select_submit'=> $submit_on_change,
1197                         'async' => false,
1198                 ) );
1199 }
1200
1201 function tax_groups_list_cells($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1202 {
1203         if ($label != null)
1204                 echo "<td>$label</td>\n";
1205         echo "<td>";
1206         echo tax_groups_list($name, $selected_id, $none_option, $submit_on_change);
1207         echo "</td>\n";
1208 }
1209
1210 function tax_groups_list_row($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1211 {
1212         echo "<tr><td class='label'>$label</td>";
1213         tax_groups_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1214         echo "</tr>\n";
1215 }
1216
1217 //------------------------------------------------------------------------------------
1218
1219 function item_tax_types_list($name, $selected_id=null)
1220 {
1221         $sql ="SELECT id, name FROM ".TB_PREF."item_tax_types";
1222         return combo_input($name, $selected_id, $sql, 'id', 'name', array('order' => 'id') );
1223 }
1224
1225 function item_tax_types_list_cells($label, $name, $selected_id=null)
1226 {
1227         if ($label != null)
1228                 echo "<td>$label</td>\n";
1229         echo "<td>";
1230         echo item_tax_types_list($name, $selected_id);
1231         echo "</td>\n";
1232 }
1233
1234 function item_tax_types_list_row($label, $name, $selected_id=null)
1235 {
1236         echo "<tr><td class='label'>$label</td>";
1237         item_tax_types_list_cells(null, $name, $selected_id);
1238         echo "</tr>\n";
1239 }
1240
1241 //------------------------------------------------------------------------------------
1242
1243 function shippers_list($name, $selected_id=null)
1244 {
1245         $sql = "SELECT shipper_id, shipper_name, inactive FROM ".TB_PREF."shippers";
1246         return combo_input($name, $selected_id, $sql, 'shipper_id', 'shipper_name', 
1247                 array('order'=>array('shipper_name')));
1248 }
1249
1250 function shippers_list_cells($label, $name, $selected_id=null)
1251 {
1252         if ($label != null)
1253                 echo "<td>$label</td>\n";
1254         echo "<td>";
1255         echo shippers_list($name, $selected_id);
1256         echo "</td>\n";
1257 }
1258
1259 function shippers_list_row($label, $name, $selected_id=null)
1260 {
1261         echo "<tr><td class='label'>$label</td>";
1262         shippers_list_cells(null, $name, $selected_id);
1263         echo "</tr>\n";
1264 }
1265
1266 //-------------------------------------------------------------------------------------
1267
1268 function sales_persons_list($name, $selected_id=null, $spec_opt=false)
1269 {
1270         $sql = "SELECT salesman_code, salesman_name, inactive FROM ".TB_PREF."salesman";
1271         return combo_input($name, $selected_id, $sql, 'salesman_code', 'salesman_name', 
1272                 array('order'=>array('salesman_name'),
1273                         'spec_option' => $spec_opt,
1274                         'spec_id' => ALL_NUMERIC));
1275 }
1276
1277 function sales_persons_list_cells($label, $name, $selected_id=null, $spec_opt=false)
1278 {
1279         if ($label != null)
1280                 echo "<td>$label</td>\n";
1281         echo "<td>\n";
1282         echo sales_persons_list($name, $selected_id, $spec_opt);
1283         echo "</td>\n";
1284 }
1285
1286 function sales_persons_list_row($label, $name, $selected_id=null, $spec_opt=false)
1287 {
1288         echo "<tr><td class='label'>$label</td>";
1289         sales_persons_list_cells(null, $name, $selected_id, $spec_opt);
1290         echo "</tr>\n";
1291 }
1292
1293 //------------------------------------------------------------------------------------
1294
1295 function sales_areas_list($name, $selected_id=null)
1296 {
1297         $sql = "SELECT area_code, description, inactive FROM ".TB_PREF."areas";
1298         return combo_input($name, $selected_id, $sql, 'area_code', 'description', array());
1299 }
1300
1301 function sales_areas_list_cells($label, $name, $selected_id=null)
1302 {
1303         if ($label != null)
1304                 echo "<td>$label</td>\n";
1305         echo "<td>";
1306         echo sales_areas_list($name, $selected_id);
1307         echo "</td>\n";
1308 }
1309
1310 function sales_areas_list_row($label, $name, $selected_id=null)
1311 {
1312         echo "<tr><td class='label'>$label</td>";
1313         sales_areas_list_cells(null, $name, $selected_id);
1314         echo "</tr>\n";
1315 }
1316
1317 //------------------------------------------------------------------------------------
1318
1319 function sales_groups_list($name, $selected_id=null, $special_option=false)
1320 {
1321         $sql = "SELECT id, description, inactive FROM ".TB_PREF."groups";
1322         return combo_input($name, $selected_id, $sql, 'id', 'description', array(
1323                 'spec_option' => $special_option===true ? ' ' : $special_option,
1324                 'order' => 'description', 'spec_id' => 0,
1325         ));
1326 }
1327
1328 function sales_groups_list_cells($label, $name, $selected_id=null, $special_option=false)
1329 {
1330         if ($label != null)
1331                 echo "<td>$label</td>\n";
1332         echo "<td>";
1333         echo sales_groups_list($name, $selected_id, $special_option);
1334         echo "</td>\n";
1335 }
1336
1337 function sales_groups_list_row($label, $name, $selected_id=null, $special_option=false)
1338 {
1339         echo "<tr><td class='label'>$label</td>";
1340         sales_groups_list_cells(null, $name, $selected_id, $special_option);
1341         echo "</tr>\n";
1342 }
1343
1344 //------------------------------------------------------------------------------------
1345
1346 function _format_template_items($row)
1347 {
1348         return ($row[0] . "&nbsp;- &nbsp;" . _("Amount") . "&nbsp;".$row[1]);
1349 }
1350
1351 function templates_list($name, $selected_id=null, $special_option=false)
1352 {
1353         $sql = "SELECT sorder.order_no, Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue
1354                 FROM ".TB_PREF."sales_orders as sorder, ".TB_PREF."sales_order_details as line
1355                 WHERE sorder.order_no = line.order_no AND sorder.type = 1 GROUP BY line.order_no";
1356         return combo_input($name, $selected_id, $sql, 'order_no', 'OrderValue', array(
1357                 'format' => '_format_template_items',
1358                 'spec_option' => $special_option===true ? ' ' : $special_option,
1359                 'order' => 'order_no', 'spec_id' => 0,
1360         ));
1361 }
1362
1363 function templates_list_cells($label, $name, $selected_id=null, $special_option=false)
1364 {
1365         if ($label != null)
1366                 echo "<td>$label</td>\n";
1367         echo "<td>";
1368         echo templates_list($name, $selected_id, $special_option);
1369         echo "</td>\n";
1370 }
1371
1372 function templates_list_row($label, $name, $selected_id=null, $special_option=false)
1373 {
1374         echo "<tr><td class='label'>$label</td>";
1375         templates_list_cells(null, $name, $selected_id, $special_option);
1376         echo "</tr>\n";
1377 }
1378
1379 //------------------------------------------------------------------------------------
1380
1381 function workorders_list($name, $selected_id=null)
1382 {
1383         $sql = "SELECT id, wo_ref FROM ".TB_PREF."workorders WHERE closed=0";
1384         return combo_input($name, $selected_id, $sql, 'id', 'wo_ref', array());
1385 }
1386
1387 function workorders_list_cells($label, $name, $selected_id=null)
1388 {
1389         if ($label != null)
1390                 echo "<td>$label</td>\n";
1391         echo "<td>";
1392         echo workorders_list($name, $selected_id);
1393         echo "</td>\n";
1394 }
1395
1396 function workorders_list_row($label, $name, $selected_id=null)
1397 {
1398         echo "<tr><td class='label'>$label</td>";
1399         workorders_list_cells(null, $name, $selected_id);
1400         echo "</tr>\n";
1401 }
1402
1403 //------------------------------------------------------------------------------------
1404
1405 function payment_terms_list($name, $selected_id=null)
1406 {
1407         $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
1408         return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms', array());
1409 }
1410
1411 function payment_terms_list_cells($label, $name, $selected_id=null)
1412 {
1413         if ($label != null)
1414                 echo "<td>$label</td>\n";
1415         echo "<td>";
1416         echo payment_terms_list($name, $selected_id);
1417         echo "</td>\n";
1418 }
1419
1420 function payment_terms_list_row($label, $name, $selected_id=null)
1421 {
1422         echo "<tr><td class='label'>$label</td>";
1423         payment_terms_list_cells(null, $name, $selected_id);
1424         echo "</tr>\n";
1425 }
1426
1427 //------------------------------------------------------------------------------------
1428
1429 function credit_status_list($name, $selected_id=null)
1430 {
1431         $sql ="SELECT id, reason_description, inactive FROM ".TB_PREF."credit_status";
1432         return combo_input($name, $selected_id, $sql, 'id', 'reason_description', array());
1433 }
1434
1435 function credit_status_list_cells($label, $name, $selected_id=null)
1436 {
1437         if ($label != null)
1438                 echo "<td>$label</td>\n";
1439         echo "<td>";
1440         echo credit_status_list($name, $selected_id);
1441         echo "</td>\n";
1442 }
1443
1444 function credit_status_list_row($label, $name, $selected_id=null)
1445 {
1446         echo "<tr><td class='label'>$label</td>";
1447         credit_status_list_cells(null, $name, $selected_id);
1448         echo "</tr>\n";
1449 }
1450
1451 //-----------------------------------------------------------------------------------------------
1452
1453 function sales_types_list($name, $selected_id=null, $submit_on_change=false, $special_option=false)
1454 {
1455         $sql = "SELECT id, sales_type, inactive FROM ".TB_PREF."sales_types";
1456
1457         return combo_input($name, $selected_id, $sql, 'id', 'sales_type',
1458         array(
1459                 'spec_option' => $special_option===true ? _("All Sales Types") : $special_option,
1460                 'spec_id' => 0,
1461                 'select_submit'=> $submit_on_change,
1462         ) );
1463 }
1464
1465 function sales_types_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1466 {
1467         if ($label != null)
1468                 echo "<td>$label</td>\n";
1469         echo "<td>";
1470         echo sales_types_list($name, $selected_id, $submit_on_change, $special_option);
1471         echo "</td>\n";
1472 }
1473
1474 function sales_types_list_row($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1475 {
1476         echo "<tr><td class='label'>$label</td>";
1477         sales_types_list_cells(null, $name, $selected_id, $submit_on_change, $special_option);
1478         echo "</tr>\n";
1479 }
1480
1481 //-----------------------------------------------------------------------------------------------
1482
1483 function _format_date($row)
1484 {
1485         return sql2date($row['reconciled']);
1486 }
1487
1488 function bank_reconciliation_list($account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1489 {
1490         $sql = "SELECT reconciled, reconciled FROM ".TB_PREF."bank_trans
1491                 WHERE bank_act=".db_escape($account)." AND reconciled IS NOT NULL
1492                 GROUP BY reconciled";
1493         return combo_input($name, $selected_id, $sql, 'id', 'reconciled',
1494                 array(
1495                         'spec_option' => $special_option,
1496                         'format' => '_format_date',
1497                         'spec_id' => '',
1498                         'select_submit'=> $submit_on_change
1499                 ) );
1500 }
1501
1502 function bank_reconciliation_list_cells($label,$account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1503 {
1504         if ($label != null)
1505                 echo "<td>$label</td>\n";
1506         echo "<td>";
1507         echo bank_reconciliation_list($account, $name, $selected_id, $submit_on_change, $special_option);
1508         echo "</td>\n";
1509 }
1510 /*
1511 function bank_reconciliation_list_row($label, $account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1512 {
1513         echo "<tr>\n";
1514         bank_reconciliation_list_cells($label, $account, $name, $selected_id, $submit_on_change, $special_option);
1515         echo "</tr>\n";
1516 }
1517 */
1518 //-----------------------------------------------------------------------------------------------
1519
1520 function workcenter_list($name, $selected_id=null, $all_option=false)
1521 {
1522
1523         $sql = "SELECT id, name, inactive FROM ".TB_PREF."workcentres";
1524
1525         return combo_input($name, $selected_id, $sql, 'id', 'name',
1526                 array(
1527                         'spec_option' =>$all_option===true ? _("All Suppliers") : $all_option,
1528                         'spec_id' => ALL_TEXT,
1529                 ) );
1530 }
1531
1532 function workcenter_list_cells($label, $name, $selected_id=null, $all_option=false)
1533 {
1534         default_focus($name);
1535         if ($label != null)
1536                 echo "<td>$label</td>\n";
1537         echo "<td>";
1538         echo workcenter_list($name, $selected_id, $all_option);
1539         echo "</td>\n";
1540 }
1541
1542 function workcenter_list_row($label, $name, $selected_id=null, $all_option=false)
1543 {
1544         echo "<tr><td class='label'>$label</td>";
1545         workcenter_list_cells(null, $name, $selected_id, $all_option);
1546         echo "</tr>\n";
1547 }
1548
1549 //-----------------------------------------------------------------------------------------------
1550
1551 function bank_accounts_list($name, $selected_id=null, $submit_on_change=false, $spec_option=false)
1552 {
1553         $sql = "SELECT id, bank_account_name, bank_curr_code, inactive
1554                 FROM ".TB_PREF."bank_accounts";
1555
1556         return combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1557                 array(
1558                         'format' => '_format_add_curr',
1559                         'select_submit'=> $submit_on_change,
1560                         'spec_option' => $spec_option,
1561                         'spec_id' => '',
1562                         'async' => false
1563                 ) );
1564 }
1565
1566 function bank_accounts_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1567 {
1568         if ($label != null)
1569                 echo "<td>$label</td>\n";
1570         echo "<td>";
1571         echo bank_accounts_list($name, $selected_id, $submit_on_change);
1572         echo "</td>\n";
1573 }
1574
1575 function bank_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1576 {
1577         echo "<tr><td class='label'>$label</td>";
1578         bank_accounts_list_cells(null, $name, $selected_id, $submit_on_change);
1579         echo "</tr>\n";
1580 }
1581 //-----------------------------------------------------------------------------------------------
1582
1583 function cash_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false, $all_option=false)
1584 {
1585
1586         $sql = "SELECT id, bank_account_name, bank_curr_code, inactive
1587                 FROM ".TB_PREF."bank_accounts
1588                 WHERE account_type=".BT_CASH;
1589
1590         if ($label != null)
1591                 echo "<tr><td class='label'>$label</td>\n";
1592         echo "<td>";
1593         echo combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1594                 array(
1595                         'spec_option' => $all_option,
1596                         'spec_id' => ALL_TEXT,
1597                         'format' => '_format_add_curr',
1598                         'select_submit'=> $submit_on_change,
1599                         'async' => true
1600                 ) );
1601         echo "</td></tr>\n";
1602 }
1603 //-----------------------------------------------------------------------------------------------
1604
1605 function pos_list_row($label, $name, $selected_id=null, $spec_option=false, $submit_on_change=false)
1606 {
1607         $sql = "SELECT id, pos_name, inactive FROM ".TB_PREF."sales_pos";
1608
1609         default_focus($name);
1610         echo '<tr>';
1611         if ($label != null)
1612                 echo "<td class='label'>$label</td>\n";
1613         echo "<td>";
1614
1615         echo combo_input($name, $selected_id, $sql, 'id', 'pos_name',
1616                 array(
1617                         'select_submit'=> $submit_on_change,
1618                         'async' => true,
1619                         'spec_option' =>$spec_option,
1620                         'spec_id' => -1,
1621                         'order'=> array('pos_name')
1622                 ) );
1623         echo "</td></tr>\n";
1624
1625 }
1626 //-----------------------------------------------------------------------------------------------
1627 // Payment type selector for current user.
1628 //
1629 function sale_payment_list($name, $category, $selected_id=null, $submit_on_change=true, $prepayments=true)
1630 {
1631         $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
1632
1633         if ($category == PM_CASH) // only cash
1634                         $sql .= " WHERE days_before_due=0 AND day_in_following_month=0";
1635         elseif ($category == PM_CREDIT) // only delayed payments
1636                         $sql .= " WHERE days_before_due".($prepayments ? '!=': '>')."0 OR day_in_following_month!=0";
1637         elseif (!$prepayments)
1638                         $sql .= " WHERE days_before_due>=0";
1639
1640         return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms',
1641                 array(
1642                         'select_submit'=> $submit_on_change,
1643                         'async' => true
1644                 ) );
1645
1646 }
1647
1648 function sale_payment_list_cells($label, $name, $category, $selected_id=null, $submit_on_change=true, $prepayments=true)
1649 {
1650         if ($label != null)
1651                 echo "<td class='label'>$label</td>\n";
1652         echo "<td>";
1653
1654         echo sale_payment_list($name, $category, $selected_id, $submit_on_change, $prepayments);
1655
1656         echo "</td>\n";
1657 }
1658 //-----------------------------------------------------------------------------------------------
1659
1660 function class_list($name, $selected_id=null, $submit_on_change=false)
1661 {
1662         $sql = "SELECT cid, class_name FROM ".TB_PREF."chart_class";
1663
1664         return combo_input($name, $selected_id, $sql, 'cid', 'class_name',
1665                 array(
1666                         'select_submit'=> $submit_on_change,
1667                         'async' => false
1668                 ) );
1669
1670 }
1671
1672 function class_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1673 {
1674         if ($label != null)
1675                 echo "<td>$label</td>\n";
1676         echo "<td>";
1677         echo class_list($name, $selected_id, $submit_on_change);
1678         echo "</td>\n";
1679 }
1680
1681 function class_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1682 {
1683         echo "<tr><td class='label'>$label</td>";
1684         class_list_cells(null, $name, $selected_id, $submit_on_change);
1685         echo "</tr>\n";
1686 }
1687
1688 //-----------------------------------------------------------------------------------------------
1689 function stock_categories_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false, $fixed_asset=false)
1690 {
1691         $where_opts = array();
1692         if ($fixed_asset)
1693                 $where_opts[0] = "dflt_mb_flag='F'";
1694         else
1695                 $where_opts[0] = "dflt_mb_flag!='F'";
1696
1697         $sql = "SELECT category_id, description, inactive FROM ".TB_PREF."stock_category";
1698         return combo_input($name, $selected_id, $sql, 'category_id', 'description',
1699                 array('order'=>'category_id',
1700                         'spec_option' => $spec_opt,
1701                         'spec_id' => -1,
1702                         'select_submit'=> $submit_on_change,
1703                         'async' => true,
1704                         'where' => $where_opts,
1705                 ));
1706 }
1707
1708 function stock_categories_list_cells($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false, $fixed_asset=false)
1709 {
1710         if ($label != null)
1711                 echo "<td>$label</td>\n";
1712         echo "<td>";
1713         echo stock_categories_list($name, $selected_id, $spec_opt, $submit_on_change, $fixed_asset);
1714         echo "</td>\n";
1715 }
1716
1717 function stock_categories_list_row($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false, $fixed_asset=false)
1718 {
1719         echo "<tr><td class='label'>$label</td>";
1720         stock_categories_list_cells(null, $name, $selected_id, $spec_opt, $submit_on_change, $fixed_asset);
1721         echo "</tr>\n";
1722 }
1723
1724 //-----------------------------------------------------------------------------------------------
1725 function fixed_asset_classes_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1726 {
1727         $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 "
1728                 .TB_PREF."stock_fa_class c LEFT JOIN ".TB_PREF."stock_fa_class p ON c.parent_id=p.fa_class_id";
1729
1730         return combo_input($name, $selected_id, $sql, 'c.fa_class_id', 'desc',
1731                 array('order'=>'c.fa_class_id',
1732                         'spec_option' => $spec_opt,
1733                         'spec_id' => '-1',
1734                         'select_submit'=> $submit_on_change,
1735                         'async' => true,
1736                         'search_box' => true,
1737                         'search' => array("c.fa_class_id"),
1738                     'search_submit' => false,
1739                     'spec_id' => '',
1740                     'size' => 3,
1741                     'max' => 3,
1742                         'category' => 'class',
1743                 ));
1744 }
1745
1746 function fixed_asset_classes_list_row($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1747 {
1748         echo "<tr><td class='label'>$label</td>";
1749         echo "<td>";
1750         echo fixed_asset_classes_list($name, $selected_id, $spec_opt, $submit_on_change);
1751         echo "</td></tr>\n";
1752 }
1753
1754 //-----------------------------------------------------------------------------------------------
1755
1756 function gl_account_types_list($name, $selected_id=null, $all_option=false, $all=true)
1757 {
1758
1759         $sql = "SELECT id, name FROM ".TB_PREF."chart_types";
1760
1761         return combo_input($name, $selected_id, $sql, 'id', 'name',
1762                 array(
1763                         'format' => '_format_account',
1764                         'order' => array('class_id', 'id', 'parent'),
1765                         'spec_option' =>$all_option,
1766                         'spec_id' => ALL_TEXT
1767                 ) );
1768 }
1769
1770 function gl_account_types_list_cells($label, $name, $selected_id=null, $all_option=false, $all=false)
1771 {
1772         if ($label != null)
1773                 echo "<td>$label</td>\n";
1774         echo "<td>";
1775         echo gl_account_types_list($name, $selected_id, $all_option, $all);
1776         echo "</td>\n";
1777 }
1778
1779 function gl_account_types_list_row($label, $name, $selected_id=null, $all_option=false, $all=false)
1780 {
1781         echo "<tr><td class='label'>$label</td>";
1782         gl_account_types_list_cells(null, $name, $selected_id, $all_option,
1783                 $all);
1784         echo "</tr>\n";
1785 }
1786
1787 //-----------------------------------------------------------------------------------------------
1788 function gl_all_accounts_list($name, $selected_id=null, $skip_bank_accounts=false,
1789         $cells=false, $all_option=false, $submit_on_change=false, $all=false)
1790 {
1791         if ($skip_bank_accounts)
1792                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
1793                         FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
1794                         ."LEFT JOIN ".TB_PREF."bank_accounts acc "
1795                         ."ON chart.account_code=acc.account_code
1796                                 WHERE acc.account_code  IS NULL
1797                         AND chart.account_type=type.id";
1798         else
1799                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
1800                         FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type
1801                         WHERE chart.account_type=type.id";
1802
1803         return combo_input($name, $selected_id, $sql, 'chart.account_code', 'chart.account_name',
1804                 array(
1805                         'format' => '_format_account',
1806                         'spec_option' => $all_option===true ?  _("Use Item Sales Accounts") : $all_option,
1807                         'spec_id' => '',
1808                         'type' => 2,
1809                         'order' => array('type.class_id','type.id','account_code'),
1810                         'search_box' => $cells,
1811                         'search_submit' => false,
1812                         'size' => 12,
1813                         'max' => 10,
1814                         'cells' => true,
1815                         'select_submit'=> $submit_on_change,
1816                         'async' => false,
1817                         'category' => 2,
1818                         'show_inactive' => $all
1819                 ), "account" );
1820 }
1821
1822 function _format_account($row)
1823 {
1824         return $row[0] .  "&nbsp;&nbsp;&nbsp;&nbsp;" . $row[1];
1825 }
1826
1827 function gl_all_accounts_list_cells($label, $name, $selected_id=null, 
1828         $skip_bank_accounts=false, $cells=false, $all_option=false, 
1829         $submit_on_change=false, $all=false)
1830 {
1831         if ($label != null)
1832                 echo "<td>$label</td>\n";
1833         echo "<td>";
1834         echo gl_all_accounts_list($name, $selected_id, 
1835                 $skip_bank_accounts, $cells, $all_option, $submit_on_change, $all);
1836         echo "</td>\n";
1837 }
1838
1839 function gl_all_accounts_list_row($label, $name, $selected_id=null, 
1840         $skip_bank_accounts=false, $cells=false, $all_option=false)
1841 {
1842         echo "<tr><td class='label'>$label</td>";
1843         gl_all_accounts_list_cells(null, $name, $selected_id, 
1844                 $skip_bank_accounts, $cells, $all_option);
1845         echo "</tr>\n";
1846 }
1847
1848 function yesno_list($name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1849 {
1850         $items = array();
1851         $items['0'] = strlen($name_no) ? $name_no : _("No");
1852         $items['1'] = strlen($name_yes) ? $name_yes : _("Yes");
1853
1854         return array_selector($name, $selected_id, $items, 
1855                 array( 
1856                         'select_submit'=> $submit_on_change,
1857                         'async' => false ) ); // FIX?
1858 }
1859
1860 function yesno_list_cells($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1861 {
1862         if ($label != null)
1863                 echo "<td>$label</td>\n";
1864         echo "<td>";
1865         echo yesno_list($name, $selected_id, $name_yes, $name_no, $submit_on_change);
1866         echo "</td>\n";
1867 }
1868
1869 function yesno_list_row($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1870 {
1871         echo "<tr><td class='label'>$label</td>";
1872         yesno_list_cells(null, $name, $selected_id, $name_yes, $name_no, $submit_on_change);
1873         echo "</tr>\n";
1874 }
1875
1876 //------------------------------------------------------------------------------------------------
1877
1878 function languages_list($name, $selected_id=null, $all_option=false)
1879 {
1880         global $installed_languages;
1881
1882         $items = array();
1883         if ($all_option)
1884                 $items[''] = $all_option;
1885         foreach ($installed_languages as $lang)
1886                         $items[$lang['code']] = $lang['name'];
1887         return array_selector($name, $selected_id, $items);
1888 }
1889
1890 function languages_list_cells($label, $name, $selected_id=null, $all_option=false)
1891 {
1892         if ($label != null)
1893                 echo "<td>$label</td>\n";
1894         echo "<td>";
1895         echo languages_list($name, $selected_id, $all_option);
1896         echo "</td>\n";
1897 }
1898
1899 function languages_list_row($label, $name, $selected_id=null, $all_option=false)
1900 {
1901         echo "<tr><td class='label'>$label</td>";
1902         languages_list_cells(null, $name, $selected_id, $all_option);
1903         echo "</tr>\n";
1904 }
1905
1906 //------------------------------------------------------------------------------------------------
1907
1908 function bank_account_types_list($name, $selected_id=null)
1909 {
1910         global $bank_account_types;
1911
1912         return array_selector($name, $selected_id, $bank_account_types);
1913 }
1914
1915 function bank_account_types_list_cells($label, $name, $selected_id=null)
1916 {
1917         if ($label != null)
1918                 echo "<td>$label</td>\n";
1919         echo "<td>";
1920         echo bank_account_types_list($name, $selected_id);
1921         echo "</td>\n";
1922 }
1923
1924 function bank_account_types_list_row($label, $name, $selected_id=null)
1925 {
1926         echo "<tr><td class='label'>$label</td>";
1927         bank_account_types_list_cells(null, $name, $selected_id);
1928         echo "</tr>\n";
1929 }
1930
1931 //------------------------------------------------------------------------------------------------
1932 function payment_person_types_list($name, $selected_id=null, $submit_on_change=false)
1933 {
1934         global $payment_person_types;
1935
1936         $items = array();
1937         foreach ($payment_person_types as $key=>$type)
1938         {
1939                 if ($key != PT_WORKORDER)
1940                         $items[$key] = $type;
1941         }               
1942         return array_selector($name, $selected_id, $items, 
1943                 array( 'select_submit'=> $submit_on_change ) );
1944 }
1945
1946 function payment_person_types_list_cells($label, $name, $selected_id=null, $related=null)
1947 {
1948         if ($label != null)
1949                 echo "<td>$label</td>\n";
1950         echo "<td>";
1951         echo payment_person_types_list($name, $selected_id, $related);
1952         echo "</td>\n";
1953 }
1954
1955 function payment_person_types_list_row($label, $name, $selected_id=null, $related=null)
1956 {
1957         echo "<tr><td class='label'>$label</td>";
1958         payment_person_types_list_cells(null, $name, $selected_id, $related);
1959         echo "</tr>\n";
1960 }
1961
1962 //------------------------------------------------------------------------------------------------
1963
1964 function wo_types_list($name, $selected_id=null)
1965 {
1966         global $wo_types_array;
1967         
1968         return array_selector($name, $selected_id, $wo_types_array, 
1969                 array( 'select_submit'=> true, 'async' => true ) );
1970 }
1971
1972 function wo_types_list_row($label, $name, $selected_id=null)
1973 {
1974         echo "<tr><td class='label'>$label</td><td>\n";
1975         echo wo_types_list($name, $selected_id);
1976         echo "</td></tr>\n";
1977 }
1978
1979 //------------------------------------------------------------------------------------------------
1980
1981 function dateformats_list_row($label, $name, $value=null)
1982 {
1983         global $SysPrefs;
1984
1985         echo "<tr><td class='label'>$label</td>\n<td>";
1986         echo array_selector( $name, $value, $SysPrefs->dateformats );
1987         echo "</td></tr>\n";
1988 }
1989
1990 function dateseps_list_row($label, $name, $value=null)
1991 {
1992         global $SysPrefs;
1993
1994         echo "<tr><td class='label'>$label</td>\n<td>";
1995         echo array_selector( $name, $value, $SysPrefs->dateseps );
1996         echo "</td></tr>\n";
1997 }
1998
1999 function thoseps_list_row($label, $name, $value=null)
2000 {
2001         global $SysPrefs;
2002
2003         echo "<tr><td class='label'>$label</td>\n<td>";
2004         echo array_selector( $name, $value, $SysPrefs->thoseps );
2005         echo "</td></tr>\n";
2006 }
2007
2008 function decseps_list_row($label, $name, $value=null)
2009 {
2010         global $SysPrefs;
2011
2012         echo "<tr><td class='label'>$label</td>\n<td>";
2013         echo array_selector( $name, $value, $SysPrefs->decseps );
2014         echo "</td></tr>\n";
2015 }
2016
2017 function themes_list_row($label, $name, $value=null)
2018 {
2019         global $path_to_root;
2020
2021         $path = $path_to_root.'/themes/';
2022         $themes = array();
2023         $themedir = opendir($path);
2024         while(false !== ($fname = readdir($themedir)))
2025         {
2026                 if($fname!='.' && $fname!='..' && $fname!='CVS' && is_dir($path.$fname))
2027                 {
2028                         $themes[$fname] =  $fname;
2029                 }
2030         }
2031         ksort($themes);
2032
2033         echo "<tr><td class='label'>$label</td>\n<td>";
2034         echo array_selector( $name, $value, $themes );
2035         echo "</td></tr>\n";
2036 }
2037
2038 function pagesizes_list_row($label, $name, $value=null)
2039 {
2040         global $SysPrefs;
2041
2042         $items = array();
2043         foreach ($SysPrefs->pagesizes as $pz)
2044                 $items[$pz] = $pz;
2045
2046         echo "<tr><td class='label'>$label</td>\n<td>";
2047         echo array_selector( $name, $value, $items );
2048         echo "</td></tr>\n";
2049 }
2050
2051 function systypes_list($name, $value=null, $spec_opt=false, $submit_on_change=false, $exclude=array())
2052 {
2053         global $systypes_array;
2054
2055         // emove non-voidable transactions if needed
2056         $systypes = array_diff_key($systypes_array, array_flip($exclude));
2057         return array_selector($name, $value, $systypes, 
2058                 array( 
2059                         'spec_option'=> $spec_opt,
2060                         'spec_id' => ALL_NUMERIC,
2061                         'select_submit'=> $submit_on_change,
2062                         'async' => false,
2063                         )
2064         );
2065 }
2066
2067 function systypes_list_cells($label, $name, $value=null, $submit_on_change=false, $exclude=array())
2068 {
2069         if ($label != null)
2070                 echo "<td>$label</td>\n";
2071         echo "<td>";
2072         echo systypes_list($name, $value, false, $submit_on_change, $exclude);
2073         echo "</td>\n";
2074 }
2075
2076 function systypes_list_row($label, $name, $value=null, $submit_on_change=false, $exclude=array())
2077 {
2078         echo "<tr><td class='label'>$label</td>";
2079         systypes_list_cells(null, $name, $value, $submit_on_change, $exclude);
2080         echo "</tr>\n";
2081 }
2082
2083 function journal_types_list_cells($label, $name, $value=null, $submit_on_change=false)
2084 {
2085         global $systypes_array;
2086
2087         if ($label != null)
2088                 echo "<td>$label</td>\n";
2089         echo "<td>";
2090
2091         $items = $systypes_array;
2092
2093         // exclude quotes, orders and dimensions
2094         foreach (array(ST_PURCHORDER, ST_SALESORDER, ST_DIMENSION, ST_SALESQUOTE, ST_LOCTRANSFER) as $excl)
2095                         unset($items[$excl]);
2096         
2097         echo array_selector($name, $value, $items, 
2098                 array( 
2099                         'spec_option'=> _("All"),
2100                         'spec_id' => -1,
2101                         'select_submit'=> $submit_on_change,
2102                         'async' => false
2103                         )
2104         );
2105         echo "</td>\n";
2106 }
2107
2108 function cust_allocations_list_cells($label, $name, $selected=null)
2109 {
2110
2111         if ($label != null)
2112                 label_cell($label);
2113         echo "<td>\n";
2114         $allocs = array( 
2115                 ALL_TEXT=>_("All Types"),
2116                 '1'=> _("Sales Invoices"),
2117                 '2'=> _("Overdue Invoices"),
2118                 '3' => _("Payments"),
2119                 '4' => _("Credit Notes"),
2120                 '5' => _("Delivery Notes")
2121         );
2122         echo array_selector($name, $selected, $allocs);
2123         echo "</td>\n";
2124 }
2125
2126 function supp_allocations_list_cell($name, $selected=null)
2127 {
2128
2129         echo "<td>\n";
2130         $allocs = array( 
2131                 ALL_TEXT=>_("All Types"),
2132                 '1'=> _("Invoices"),
2133                 '2'=> _("Overdue Invoices"),
2134                 '3' => _("Payments"),
2135                 '4' => _("Credit Notes"),
2136                 '5' => _("Overdue Credit Notes")
2137         );
2138         echo array_selector($name, $selected, $allocs);
2139         echo "</td>\n";
2140 }
2141
2142 function supp_transactions_list_cell($name, $selected=null)
2143 {
2144
2145         echo "<td>\n";
2146         $allocs = array( 
2147                 ALL_TEXT=>_("All Types"),
2148                 '6'=>_("GRNs"),
2149                 '1'=> _("Invoices"),
2150                 '2'=> _("Overdue Invoices"),
2151                 '3' => _("Payments"),
2152                 '4' => _("Credit Notes"),
2153                 '5' => _("Overdue Credit Notes")
2154         );
2155
2156         echo array_selector($name, $selected, $allocs);
2157         echo "</td>\n";
2158 }
2159
2160 function policy_list_cells($label, $name, $selected=null)
2161 {
2162         if ($label != null)
2163                 label_cell($label);
2164         echo "<td>\n";
2165         echo array_selector($name, $selected, 
2166                                 array( '' => _("Automatically put balance on back order"),
2167                                         'CAN' => _("Cancel any quantites not delivered")) );
2168         echo "</td>\n";
2169 }
2170
2171 function policy_list_row($label, $name, $selected=null)
2172 {
2173         echo "<tr><td class='label'>$label</td>";
2174         policy_list_cells(null, $name, $selected);
2175         echo "</tr>\n";
2176 }
2177
2178 function credit_type_list_cells($label, $name, $selected=null, $submit_on_change=false)
2179 {
2180         if ($label != null)
2181                 label_cell($label);
2182         echo "<td>\n";
2183         echo array_selector($name, $selected, 
2184                         array( 'Return' => _("Items Returned to Inventory Location"),
2185                                 'WriteOff' => _("Items Written Off")),
2186                         array( 'select_submit'=> $submit_on_change ) );
2187         echo "</td>\n";
2188 }
2189
2190 function credit_type_list_row($label, $name, $selected=null, $submit_on_change=false)
2191 {
2192         echo "<tr><td class='label'>$label</td>";
2193         credit_type_list_cells(null, $name, $selected, $submit_on_change);
2194         echo "</tr>\n";
2195 }
2196
2197 function number_list($name, $selected, $from, $to, $no_option=false)
2198 {
2199         $items = array();
2200         for ($i = $from; $i <= $to; $i++)
2201                 $items[$i] = "$i";
2202
2203         return array_selector($name, $selected, $items,
2204                         array(  'spec_option' => $no_option,
2205                                         'spec_id' => ALL_NUMERIC) );
2206 }
2207
2208 function number_list_cells($label, $name, $selected, $from, $to, $no_option=false)
2209 {
2210         if ($label != null)
2211                 label_cell($label);
2212         echo "<td>\n";
2213         echo number_list($name, $selected, $from, $to, $no_option);
2214         echo "</td>\n";
2215 }
2216
2217 function number_list_row($label, $name, $selected, $from, $to, $no_option=false)
2218 {
2219         echo "<tr><td class='label'>$label</td>";
2220         echo number_list_cells(null, $name, $selected, $from, $to, $no_option);
2221         echo "</tr>\n";
2222 }
2223
2224 function print_profiles_list_row($label, $name, $selected_id=null, $spec_opt=false,
2225         $submit_on_change=true)
2226 {
2227         $sql = "SELECT profile FROM ".TB_PREF."print_profiles"
2228                 ." GROUP BY profile";
2229         $result = db_query($sql, 'cannot get all profile names');
2230         $profiles = array();
2231         while($myrow=db_fetch($result)) {
2232                 $profiles[$myrow['profile']] = $myrow['profile'];
2233         }
2234
2235         echo "<tr>";
2236         if ($label != null)
2237                 echo "<td class='label'>$label</td>\n";
2238         echo "<td>";
2239
2240         echo array_selector($name, $selected_id, $profiles, 
2241                 array( 'select_submit'=> $submit_on_change,
2242                         'spec_option'=>$spec_opt,
2243                         'spec_id' => ''
2244                  ));
2245
2246         echo "</td></tr>\n";
2247 }
2248
2249 function printers_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
2250 {
2251         static $printers; // query only once for page display
2252
2253         if (!$printers) {
2254                 $sql = "SELECT id, name, description FROM ".TB_PREF."printers"; 
2255                 $result = db_query($sql, 'cannot get all printers');
2256                 $printers = array();
2257                 while($myrow=db_fetch($result)) {
2258                         $printers[$myrow['id']] = $myrow['name'].'&nbsp;-&nbsp;'.$myrow['description'];
2259                 }
2260         }
2261         return array_selector($name, $selected_id, $printers, 
2262                 array( 'select_submit'=> $submit_on_change,
2263                         'spec_option'=>$spec_opt,
2264                         'spec_id' => ''
2265                  ));
2266 }
2267
2268 //------------------------------------------------------------------------------------------------
2269
2270 function quick_entries_list($name, $selected_id=null, $type=null, $submit_on_change=false)
2271 {
2272         $where = false;
2273         $sql = "SELECT id, description FROM ".TB_PREF."quick_entries";
2274         if ($type != null)
2275                 $sql .= " WHERE type=$type";
2276
2277         return combo_input($name, $selected_id, $sql, 'id', 'description',
2278                 array(
2279                         'spec_id' => '',
2280                         'order' => 'description',
2281                         'select_submit'=> $submit_on_change,
2282                         'async' => false
2283                 ) );
2284
2285 }
2286
2287 function quick_entries_list_cells($label, $name, $selected_id=null, $type, $submit_on_change=false)
2288 {
2289         if ($label != null)
2290                 echo "<td>$label</td>\n";
2291         echo "<td>";    
2292         echo quick_entries_list($name, $selected_id, $type, $submit_on_change);
2293         echo "</td>";
2294 }
2295
2296 function quick_entries_list_row($label, $name, $selected_id=null, $type, $submit_on_change=false)
2297 {
2298         echo "<tr><td class='label'>$label</td>";
2299         quick_entries_list_cells(null, $name, $selected_id, $type, $submit_on_change);
2300         echo "</tr>\n";
2301 }
2302
2303
2304 function quick_actions_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2305 {
2306         global $quick_actions;
2307         
2308         echo "<tr><td class='label'>$label</td><td>";
2309         echo array_selector($name, $selected_id, $quick_actions, 
2310                 array( 
2311                         'select_submit'=> $submit_on_change
2312                 ) );
2313         echo "</td></tr>\n";
2314 }
2315
2316 function quick_entry_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2317 {
2318         global $quick_entry_types;
2319                 
2320         echo "<tr><td class='label'>$label</td><td>";
2321         echo array_selector($name, $selected_id, $quick_entry_types, 
2322                 array( 
2323                         'select_submit'=> $submit_on_change
2324                         ) );
2325         echo "</td></tr>\n";
2326 }
2327
2328 function record_status_list_row($label, $name) {
2329         return yesno_list_row($label, $name, null,      _('Inactive'), _('Active'));
2330 }
2331
2332 function class_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2333 {
2334         global $class_types;
2335         
2336         echo "<tr><td class='label'>$label</td><td>";
2337         echo array_selector($name, $selected_id, $class_types, 
2338                 array( 
2339                         'select_submit'=> $submit_on_change
2340                 ) );
2341         echo "</td></tr>\n";
2342 }
2343
2344 //------------------------------------------------------------------------------------------------
2345
2346 function security_roles_list($name, $selected_id=null, $new_item=false, $submit_on_change=false,
2347         $show_inactive = false)
2348 {
2349
2350         $sql = "SELECT id, role, inactive FROM ".TB_PREF."security_roles";
2351
2352         return combo_input($name, $selected_id, $sql, 'id', 'description',
2353                 array(
2354                         'spec_option'=>$new_item ? _("New role") : false,
2355                         'spec_id' => '',
2356                         'select_submit'=> $submit_on_change,
2357                         'show_inactive' => $show_inactive
2358                 ) );
2359 }
2360
2361 function security_roles_list_cells($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2362         $show_inactive = false)
2363 {
2364         if ($label != null)
2365                 echo "<td>$label</td>\n";
2366         echo "<td>";
2367         echo security_roles_list($name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2368         echo "</td>\n";
2369 }
2370
2371 function security_roles_list_row($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2372         $show_inactive = false)
2373 {
2374         echo "<tr><td class='label'>$label</td>";
2375         security_roles_list_cells(null, $name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2376         echo "</tr>\n";
2377 }
2378
2379 function tab_list_row($label, $name, $selected_id=null)
2380 {
2381         global $installed_extensions;
2382         
2383         $tabs = array();
2384         foreach ($_SESSION['App']->applications as $app) {
2385                 $tabs[$app->id] = access_string($app->name, true);
2386         }
2387         echo "<tr>\n";
2388         echo "<td class='label'>$label</td><td>\n";
2389         echo array_selector($name, $selected_id, $tabs);
2390         echo "</td></tr>\n";
2391 }
2392
2393 //-----------------------------------------------------------------------------------------------
2394
2395 function tag_list($name, $height, $type, $multi=false, $all=false, $spec_opt = false)
2396 {
2397         // Get tags
2398         global $path_to_root;
2399         include_once($path_to_root . "/admin/db/tags_db.inc");
2400         $results = get_tags($type, $all);
2401
2402         while ($tag = db_fetch($results))
2403                 $tags[$tag['id']] = $tag['name'];
2404         
2405         if (!isset($tags)) {
2406                 $tags[''] = $all ? _("No tags defined.") : _("No active tags defined.");
2407                 $spec_opt = false;
2408         }
2409         return array_selector($name, null, $tags,
2410                 array(
2411                         'multi' => $multi,
2412                         'height' => $height,
2413                         'spec_option'=> $spec_opt,
2414                         'spec_id' => -1,
2415                 ) );
2416 }
2417
2418 function tag_list_cells($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2419 {
2420         if ($label != null)
2421                 echo "<td>$label</td>\n";
2422         echo "<td>\n";
2423         echo tag_list($name, $height, $type, $mult, $all, $spec_opt);
2424         echo "</td>\n";
2425         
2426 }
2427
2428 function tag_list_row($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2429 {
2430         echo "<tr><td class='label'>$label</td>";
2431         tag_list_cells(null, $name, $height, $type, $mult, $all, $spec_opt);
2432         echo "</tr>\n"; 
2433 }
2434
2435 //---------------------------------------------------------------------------------------------
2436 //      List of sets of active extensions 
2437 //
2438 function extset_list($name, $value=null, $submit_on_change=false)
2439 {
2440         global $db_connections;
2441
2442         $items = array();
2443         foreach ($db_connections as $comp)
2444                 $items[] = sprintf(_("Activated for '%s'"), $comp['name']);
2445         return array_selector( $name, $value, $items,
2446                 array(
2447                         'spec_option'=> _("Available and/or installed"),
2448                         'spec_id' => -1,
2449                         'select_submit'=> $submit_on_change,
2450                         'async' => true
2451                 ));
2452 }
2453
2454 function crm_category_types_list($name, $selected_id=null, $filter=array(), $submit_on_change=true)
2455 {
2456
2457         $sql = "SELECT id, name, type, inactive FROM ".TB_PREF."crm_categories";
2458
2459         $multi = false;
2460         $groups = false;
2461         $where = array();
2462         if (@$filter['class']) {
2463                 $where[] = 'type='.db_escape($filter['class']);
2464         } else
2465                 $groups = 'type';
2466         if (@$filter['subclass']) $where[] = 'action='.db_escape($filter['subclass']);
2467         if (@$filter['entity']) $where[] = 'entity_id='.db_escape($filter['entity']);
2468         if (@$filter['multi']) { // contact category selector for person
2469                 $multi = true;
2470         }
2471
2472         return combo_input($name, $selected_id, $sql, 'id', 'name',
2473                 array(
2474                         'multi' => $multi,
2475                         'height' => $multi ? 5:1,
2476                         'category' => $groups,
2477                         'select_submit'=> $submit_on_change,
2478                         'async' => true,
2479                         'where' => $where
2480                 ));
2481 }
2482
2483 function crm_category_types_list_row($label, $name, $selected_id=null, $filter=array(), $submit_on_change=true)
2484 {
2485         echo "<tr><td class='label'>$label</td><td>";
2486         echo crm_category_types_list($name, $selected_id, $filter, $submit_on_change);
2487         echo "</td></tr>\n";
2488 }
2489
2490 function payment_type_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2491 {
2492         global $pterm_types;
2493         
2494         echo "<tr><td class='label'>$label</td><td>";
2495         echo array_selector($name, $selected_id, $pterm_types, 
2496                 array( 
2497                         'select_submit'=> $submit_on_change
2498                 ) );
2499         echo "</td></tr>\n";
2500 }
2501
2502 function coa_list_row($label, $name, $value=null)
2503 {
2504         global $path_to_root, $installed_extensions;
2505
2506         $path = $path_to_root.'/sql/';
2507         $coas = array();
2508         $sqldir = opendir($path);
2509         while (false !== ($fname = readdir($sqldir)))
2510         {
2511                 if (is_file($path.$fname) && substr($fname,-4)=='.sql' && @($fname[2] == '_'))
2512                 {
2513                         $ext = array_search_value($fname, $installed_extensions, 'sql');
2514                         if ($ext!=null) {
2515                                 $descr = $ext['name'];
2516                         } elseif ($fname == 'en_US-new.sql') { // two standard COAs
2517                                 $descr = _("Standard new company American COA (4 digit)");
2518                         } elseif ($fname == 'en_US-demo.sql') {
2519                                 $descr = _("Standard American COA (4 digit) with demo data");
2520                         } else
2521                                 $descr = $fname;
2522
2523                         $coas[$fname] =  $descr;
2524                 }
2525         }
2526         ksort($coas);
2527
2528         echo "<tr><td class='label'>$label</td>\n<td>";
2529         echo array_selector( $name, $value, $coas );
2530         echo "</td></tr>\n";
2531 }
2532
2533 function payment_services($name)
2534 {
2535         global $payment_services;
2536
2537         $services = array_combine(array_keys($payment_services), array_keys($payment_services));
2538
2539         return array_selector($name, null, $services, array(
2540                         'spec_option'=> _("No payment Link"),
2541                         'spec_id' => '',
2542                 ));
2543 }
2544
2545 function tax_algorithm_list($name, $value=null, $submit_on_change = false)
2546 {
2547         global $tax_algorithms;
2548         
2549         return array_selector($name, $value, $tax_algorithms, 
2550                 array( 
2551                         'select_submit'=> $submit_on_change,
2552                         'async' => true,
2553                         )
2554         );
2555 }
2556
2557 function tax_algorithm_list_cells($label, $name, $value=null, $submit_on_change=false)
2558 {
2559         if ($label != null)
2560                 echo "<td>$label</td>\n";
2561         echo "<td>";
2562         echo tax_algorithm_list($name, $value, $submit_on_change);
2563         echo "</td>\n";
2564 }
2565
2566 function tax_algorithm_list_row($label, $name, $value=null, $submit_on_change=false)
2567 {
2568         echo "<tr><td class='label'>$label</td>";
2569         tax_algorithm_list_cells(null, $name, $value, $submit_on_change);
2570         echo "</tr>\n";
2571 }
2572
2573 function refline_list($name, $type, $value=null, $spec_option=false)
2574 {
2575         $sql = "SELECT id, prefix, inactive FROM ".TB_PREF."reflines";
2576
2577         $where = array();
2578
2579         if (isset($type))
2580                 $where = array('`trans_type`='.db_escape($type));
2581
2582         return combo_input($name, $value, $sql, 'id', 'prefix',
2583                 array(
2584                         'order'=>array('prefix'),
2585                         'spec_option' => $spec_option,
2586                         'spec_id' => '',
2587                         'type' => 2,
2588                         'where' => $where,
2589                         'select_submit' => true,
2590                         )
2591                 );
2592 }
2593
2594 function refline_list_row($label, $name, $type, $selected_id=null, $spec_option=false)
2595 {
2596         echo "<tr>";
2597         if ($label != null)
2598                 echo "<td class='label'>$label</td>\n";
2599         echo "<td>";
2600
2601         echo refline_list($name, $type, $selected_id, $spec_option);
2602         echo "</td></tr>\n";
2603 }
2604
2605
2606 //----------------------------------------------------------------------------------------------
2607
2608 function subledger_list($name, $account, $selected_id=null)
2609 {
2610
2611         $type = is_subledger_account($account);
2612         if (!$type)
2613                 return '';
2614
2615         if($type > 0)
2616                 $sql = "SELECT DISTINCT d.debtor_no as id, debtor_ref as name 
2617                 FROM "
2618                 .TB_PREF."debtors_master d,"
2619                 .TB_PREF."cust_branch c
2620                 WHERE d.debtor_no=c.debtor_no AND c.receivables_account=".db_escape($account);
2621         else
2622                 $sql = "SELECT supplier_id as id, supp_ref as name 
2623                 FROM "
2624                 .TB_PREF."suppliers s
2625                 WHERE s.payable_account=".db_escape($account);
2626
2627         $mode = get_company_pref('no_customer_list');
2628
2629         return combo_input($name, $selected_id, $sql, 'id', 'name',
2630         array(
2631                 'type' => 1,
2632                 'size' => 20,
2633                 'async' => false,
2634         ) );
2635 }
2636
2637 function subledger_list_cells($label, $name, $account, $selected_id=null)
2638 {
2639         if ($label != null)
2640                 echo "<td>$label</td>\n";
2641         echo "<td nowrap>";
2642         echo subledger_list($name, $account, $selected_id);
2643         echo "</td>\n";
2644 }
2645
2646 function subledger_list_row($label, $name, $selected_id=null, $all_option = false, 
2647         $submit_on_change=false, $show_inactive=false, $editkey = false)
2648 {
2649         echo "<tr><td class='label'>$label</td><td nowrap>";
2650         echo subledger_list($name, $account, $selected_id);
2651         echo "</td>\n</tr>\n";
2652 }
2653
2654 function accounts_type_list_row($label, $name, $selected_id=null)
2655 {
2656         echo "<tr>";
2657         if ($label != null)
2658                 echo "<td class='label'>$label</td>\n";
2659         echo "<td>";
2660         $sel = array(_("Numeric"), _("Alpha Numeric"), _("ALPHA NUMERIC"));
2661         echo array_selector($name, $selected_id, $sel); 
2662         echo "</td></tr>\n";
2663 }
2664
2665 function users_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $spec_opt=true)
2666 {
2667     $where = false;
2668     $sql = " SELECT user_id, real_name FROM ".TB_PREF."users";
2669
2670     if ($label != null)
2671         echo "<td>$label</td>\n";
2672     echo "<td>";
2673
2674     echo combo_input($name, $selected_id, $sql, 'user_id', 'real_name',
2675         array(
2676             'spec_option' => $spec_opt===true ?_("All users") : $spec_opt,
2677             'spec_id' => '',
2678             'order' => 'real_name',
2679             'select_submit'=> $submit_on_change,
2680             'async' => false
2681         ) );
2682     echo "</td>";
2683
2684 }
2685
2686 function collations_list_row($label, $name, $selected_id=null)
2687 {
2688         global $supported_collations;
2689
2690         echo "<tr>";
2691         if ($label != null)
2692                 echo "<td class='label'>$label</td>\n";
2693         echo "<td>";
2694
2695         echo array_selector($name, $selected_id, $supported_collations, 
2696                 array('select_submit'=> false) );
2697         echo "</td></tr>\n";
2698 }