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