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