Cleanup - removed ancient references to pre-2.2 modules/plugins.
[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                 'spec_option' => $spec_option === true ? _("All Suppliers") : $spec_option,
454                 'spec_id' => $all_items,
455                 'select_submit'=> $submit_on_change,
456                 'async' => false,
457                 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment') :
458                 _('Select supplier'),
459                 'show_inactive'=>$all
460                 ));
461         if ($editkey)
462                 $ret .= add_edit_combo('supplier');             
463         return $ret;
464 }
465
466 function supplier_list_cells($label, $name, $selected_id=null, $all_option=false, 
467         $submit_on_change=false, $all=false, $editkey = false)
468 {
469         if ($label != null)
470                 echo "<td>$label</td><td>\n";
471                 echo supplier_list($name, $selected_id, $all_option, $submit_on_change, 
472                 $all, $editkey);
473                 echo "</td>\n";
474 }
475
476 function supplier_list_row($label, $name, $selected_id=null, $all_option = false, 
477         $submit_on_change=false, $all=false, $editkey = false)
478 {
479         echo "<tr><td class='label'>$label</td><td>";
480         echo supplier_list($name, $selected_id, $all_option, $submit_on_change,
481                 $all, $editkey);
482         echo "</td></tr>\n";
483 }
484 //----------------------------------------------------------------------------------------------
485
486 function customer_list($name, $selected_id=null, $spec_option=false, $submit_on_change=false, 
487         $show_inactive=false, $editkey = false)
488 {
489         global $all_items;
490
491         $sql = "SELECT debtor_no, debtor_ref, curr_code, inactive FROM ".TB_PREF."debtors_master ";
492
493         $mode = get_company_pref('no_customer_list');
494
495         if ($editkey)
496                 set_editor('customer', $name, $editkey);
497
498         $ret = combo_input($name, $selected_id, $sql, 'debtor_no', 'debtor_ref',
499         array(
500             'format' => '_format_add_curr',
501             'order' => array('debtor_ref'),
502                 'search_box' => $mode!=0,
503                 'type' => 1,
504                 'size' => 20,
505                 'spec_option' => $spec_option === true ? _("All Customers") : $spec_option,
506                 'spec_id' => $all_items,
507                 'select_submit'=> $submit_on_change,
508                 'async' => false,
509                 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment; F2 - entry new customer') :
510                 _('Select customer'),
511                 'show_inactive' => $show_inactive
512         ) );
513         if ($editkey)
514                 $ret .= add_edit_combo('customer');
515         return $ret;
516 }
517
518 function customer_list_cells($label, $name, $selected_id=null, $all_option=false, 
519         $submit_on_change=false, $show_inactive=false, $editkey = false)
520 {
521         if ($label != null)
522                 echo "<td>$label</td>\n";
523         echo "<td nowrap>";
524         echo customer_list($name, $selected_id, $all_option, $submit_on_change,
525                 $show_inactive, $editkey);
526         echo "</td>\n";
527 }
528
529 function customer_list_row($label, $name, $selected_id=null, $all_option = false, 
530         $submit_on_change=false, $show_inactive=false, $editkey = false)
531 {
532         global $path_to_root;
533
534         echo "<tr><td class='label'>$label</td><td nowrap>";
535         echo customer_list($name, $selected_id, $all_option, $submit_on_change,
536                 $show_inactive, $editkey);
537         echo "</td>\n</tr>\n";
538 }
539
540 //------------------------------------------------------------------------------------------------
541
542 function customer_branches_list($customer_id, $name, $selected_id=null,
543         $spec_option = true, $enabled=true, $submit_on_change=false, $editkey = false)
544 {
545         global $all_items;
546
547         $sql = "SELECT branch_code, branch_ref FROM ".TB_PREF."cust_branch
548                 WHERE debtor_no=" . db_escape($customer_id)." ";
549
550         if ($editkey)
551                 set_editor('branch', $name, $editkey);
552
553         $where = $enabled ? array("disable_trans = 0") : array();
554         $ret = combo_input($name, $selected_id, $sql, 'branch_code', 'branch_ref',
555         array(
556                 'where' => $where,
557                 'order' => array('branch_ref'),
558                 'spec_option' => $spec_option === true ? _('All branches') : $spec_option,
559                 'spec_id' => $all_items,
560                 'select_submit'=> $submit_on_change,
561                 'sel_hint' => _('Select customer branch')
562         ) );
563         if ($editkey)
564         {
565                 $ret .= add_edit_combo('branch');
566         }       
567         return $ret;
568 }
569 //------------------------------------------------------------------------------------------------
570
571 function customer_branches_list_cells($label,$customer_id, $name, $selected_id=null, 
572         $all_option = true, $enabled=true, $submit_on_change=false, $editkey = false)
573 {
574         if ($label != null)
575                 echo "<td>$label</td>\n";
576         echo "<td>";
577         echo customer_branches_list($customer_id, $name, $selected_id, $all_option, $enabled, 
578                 $submit_on_change, $editkey);
579         echo "</td>\n";
580 }
581
582 function customer_branches_list_row($label, $customer_id, $name, $selected_id=null, 
583         $all_option = true, $enabled=true, $submit_on_change=false, $editkey = false)
584 {
585         echo "<tr><td class='label'>$label</td>";
586         customer_branches_list_cells(null, $customer_id, $name, $selected_id, 
587                 $all_option, $enabled, $submit_on_change, $editkey);
588         echo "</tr>";
589 }
590
591 //------------------------------------------------------------------------------------------------
592
593 function locations_list($name, $selected_id=null, $all_option=false, $submit_on_change=false)
594 {
595         global $all_items;
596
597         $sql = "SELECT loc_code, location_name, inactive FROM ".TB_PREF."locations";
598
599 return combo_input($name, $selected_id, $sql, 'loc_code', 'location_name',
600         array(
601                 'spec_option' => $all_option === true ? _("All Locations") : $all_option,
602                 'spec_id' => $all_items,
603                 'select_submit'=> $submit_on_change
604         ) );
605 }
606
607 function locations_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
608 {
609         if ($label != null)
610                 echo "<td>$label</td>\n";
611         echo "<td>";
612         echo locations_list($name, $selected_id, $all_option, $submit_on_change);
613         echo "</td>\n";
614 }
615
616 function locations_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
617 {
618         echo "<tr><td class='label'>$label</td>";
619         locations_list_cells(null, $name, $selected_id, $all_option, $submit_on_change);
620         echo "</tr>\n";
621 }
622
623 //-----------------------------------------------------------------------------------------------
624
625 function currencies_list($name, $selected_id=null, $submit_on_change=false)
626 {
627         $sql = "SELECT curr_abrev, currency, inactive FROM ".TB_PREF."currencies";
628
629 // default to the company currency
630 return combo_input($name, $selected_id, $sql, 'curr_abrev', 'currency',
631         array(
632                 'select_submit'=> $submit_on_change,
633                 'default' => get_company_currency(),
634                 'async' => false                
635         ) );
636 }
637
638 function currencies_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
639 {
640         if ($label != null)
641                 echo "<td>$label</td>\n";
642         echo "<td>";
643         echo currencies_list($name, $selected_id, $submit_on_change);
644         echo "</td>\n";
645 }
646
647 function currencies_list_row($label, $name, $selected_id=null, $submit_on_change=false)
648 {
649         echo "<tr><td class='label'>$label</td>";
650         currencies_list_cells(null, $name, $selected_id, $submit_on_change);
651         echo "</tr>\n";
652 }
653
654 //---------------------------------------------------------------------------------------------------
655
656 function fiscalyears_list($name, $selected_id=null, $submit_on_change=false)
657 {
658
659         $sql = "SELECT * FROM ".TB_PREF."fiscal_year";
660
661 // default to the company current fiscal year
662
663 return combo_input($name, $selected_id, $sql, 'id', '',
664         array(
665                 'order' => 'begin',
666                 'default' => get_company_pref('f_year'),
667                 'format' => '_format_fiscalyears',
668                 'select_submit'=> $submit_on_change,
669                 'async' => false
670         ) );
671 }
672
673 function _format_fiscalyears($row)
674 {
675         return sql2date($row[1]) . "&nbsp;-&nbsp;" . sql2date($row[2])
676         . "&nbsp;&nbsp;" . ($row[3] ? _('Closed') : _('Active')) . "</option>\n";
677 }
678
679 function fiscalyears_list_cells($label, $name, $selected_id=null)
680 {
681         if ($label != null)
682                 echo "<td>$label</td>\n";
683         echo "<td>";
684         echo fiscalyears_list($name, $selected_id);
685         echo "</td>\n";
686 }
687
688 function fiscalyears_list_row($label, $name, $selected_id=null)
689 {
690         echo "<tr><td class='label'>$label</td>";
691         fiscalyears_list_cells(null, $name, $selected_id);
692         echo "</tr>\n";
693 }
694 //------------------------------------------------------------------------------------
695
696 function dimensions_list($name, $selected_id=null, $no_option=false, $showname=' ',
697         $submit_on_change=false, $showclosed=false, $showtype=1)
698 {
699 $sql = "SELECT id, CONCAT(reference,'  ',name) as ref FROM ".TB_PREF."dimensions";
700
701 $options = array(
702         'order' => 'reference',
703         'spec_option'=>$no_option ? $showname : false,
704         'spec_id' => 0,
705         'select_submit'=> $submit_on_change,
706         'async' => false,
707         );
708
709         if (!$showclosed)
710                 $options['where'][] = "closed=0";
711         if($showtype)
712                 $options['where'][] = "type_=".db_escape($showtype);
713
714         return combo_input($name, $selected_id, $sql, 'id', 'ref', $options);
715 }
716
717 function dimensions_list_cells($label, $name, $selected_id=null, $no_option=false, $showname=null,
718         $showclosed=false, $showtype=0, $submit_on_change=false)
719 {
720         if ($label != null)
721                 echo "<td>$label</td>\n";
722         echo "<td>";
723         echo dimensions_list($name, $selected_id, $no_option, $showname, $submit_on_change, $showclosed, $showtype);
724         echo "</td>\n";
725 }
726
727 function dimensions_list_row($label, $name, $selected_id=null, $no_option=false, $showname=null,
728         $showclosed=false, $showtype=0, $submit_on_change=false)
729 {
730         echo "<tr><td class='label'>$label</td>";
731         dimensions_list_cells(null, $name, $selected_id, $no_option, $showname,
732                 $showclosed, $showtype, $submit_on_change);
733         echo "</tr>\n";
734 }
735
736 //---------------------------------------------------------------------------------------------------
737
738 function stock_items_list($name, $selected_id=null, $all_option=false, 
739         $submit_on_change=false, $opts=array(), $editkey = false)
740 {
741         global $all_items;
742
743         $sql = "SELECT stock_id, s.description, c.description, s.inactive, s.editable
744                         FROM ".TB_PREF."stock_master s,".TB_PREF."stock_category c WHERE s.category_id=c.category_id";
745
746         if ($editkey)
747                 set_editor('item', $name, $editkey);
748
749         $ret = combo_input($name, $selected_id, $sql, 'stock_id', 's.description',
750         array_merge(
751           array(
752                 'format' => '_format_stock_items',
753                 'spec_option' => $all_option===true ?  _("All Items") : $all_option,
754                 'spec_id' => $all_items,
755                 'search_box' => true,
756                 'search' => array("stock_id", "c.description","s.description"),
757                 'search_submit' => get_company_pref('no_item_list')!=0,
758                 'size'=>10,
759                 'select_submit'=> $submit_on_change,
760                 'category' => 2,
761                 'order' => array('c.description','stock_id')
762           ), $opts) );
763         if ($editkey)
764                 $ret .= add_edit_combo('item');         
765         return $ret;
766           
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)
837 {
838         if ($label != null)
839                 echo "<td>$label</td>\n";
840         echo sales_items_list($name, $selected_id, $all_option, $submit_on_change,
841                 '', array('cells'=>true));
842 }
843
844 function sales_kits_list($name, $selected_id=null, $all_option=false, $submit_on_change=false)
845 {
846         return sales_items_list($name, $selected_id, $all_option, $submit_on_change,
847                 'kits', array('cells'=>false, 'editable' => false));
848 }
849
850 function sales_local_items_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
851 {
852         echo "<tr>";
853         if ($label != null)
854                 echo "<td class='label'>$label</td>\n";
855         echo "<td>";
856         echo sales_items_list($name, $selected_id, $all_option, $submit_on_change,
857                 'local', array('cells'=>false, 'editable' => false));
858         echo "</td></tr>";
859 }
860 //------------------------------------------------------------------------------------
861
862 function stock_manufactured_items_list($name, $selected_id=null,
863         $all_option=false, $submit_on_change=false)
864 {
865         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
866                 array('where'=>array("mb_flag= 'M'")));
867 }
868
869 function stock_manufactured_items_list_cells($label, $name, $selected_id=null,
870                                 $all_option=false, $submit_on_change=false)
871 {
872         if ($label != null)
873                 echo "<td>$label</td>\n";
874         echo "<td>";
875         echo stock_manufactured_items_list($name, $selected_id, $all_option, $submit_on_change);
876         echo "</td>\n";
877 }
878
879 function stock_manufactured_items_list_row($label, $name, $selected_id=null,
880                 $all_option=false, $submit_on_change=false)
881 {
882         echo "<tr><td class='label'>$label</td>";
883         stock_manufactured_items_list_cells(null, $name, $selected_id, $all_option, $submit_on_change);
884         echo "</tr>\n";
885 }
886 //------------------------------------------------------------------------------------
887
888 function stock_component_items_list($name, $parent_stock_id, $selected_id=null,
889         $all_option=false, $submit_on_change=false, $editkey = false)
890 {
891         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
892                 array('where'=>array("stock_id != ".db_escape($parent_stock_id))), $editkey);
893 }
894
895 function stock_component_items_list_cells($label, $name, $parent_stock_id, 
896         $selected_id=null, $all_option=false, $submit_on_change=false, $editkey = false)
897 {
898         if ($label != null)
899                 echo "<td>$label</td>\n";
900         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
901                 array('where'=>array("stock_id != ".db_escape($parent_stock_id)), 'cells'=>true),
902                 $editkey);
903 }
904 //------------------------------------------------------------------------------------
905
906 function stock_costable_items_list($name, $selected_id=null,
907         $all_option=false, $submit_on_change=false)
908 {
909         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
910                 array('where'=>array("mb_flag!='D'")));
911 }
912
913 function stock_costable_items_list_cells($label, $name, $selected_id=null, 
914         $all_option=false, $submit_on_change=false)
915 {
916         if ($label != null)
917                 echo "<td>$label</td>\n";
918         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
919                 array('where'=>array("mb_flag!='D'"), 'cells'=>true));
920 }
921
922 //------------------------------------------------------------------------------------
923 function stock_purchasable_items_list($name, $selected_id=null, 
924         $all_option=false, $submit_on_change=false, $all=false, $editkey=false)
925 {
926         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
927                 array('where'=>array("mb_flag!= 'M'"), 
928                         'show_inactive'=>$all), $editkey);
929 }
930 //
931 //      This helper is used in PO/GRN/PI entry and supports editable descriptions.
932 //
933 function stock_purchasable_items_list_cells($label, $name, $selected_id=null,
934                         $all_option=false, $submit_on_change=false, $editkey=false)
935 {
936         if ($label != null)
937                 echo "<td>$label</td>\n";
938         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
939                 array('where'=>array("mb_flag!= 'M'"), 
940                          'editable' => 30,
941                          'cells'=>true), $editkey);
942 }
943 /*
944 function stock_purchasable_items_list_row($label, $name, $selected_id=null,
945                         $all_option=false, $submit_on_change=false, $editkey=false)
946 {
947         echo "<tr><td class='label'>$label</td>";
948         stock_purchasable_items_list_cells(null, $name, $selected_id=null,
949                 $all_option, $submit_on_change, $editkey);
950         echo "</tr>\n";
951 }
952 */
953 //------------------------------------------------------------------------------------
954
955 function stock_item_types_list_row($label, $name, $selected_id=null, $enabled=true)
956 {
957         global $stock_types;
958
959         echo "<tr>";
960         if ($label != null)
961                 echo "<td class='label'>$label</td>\n";
962         echo "<td>";
963
964         echo array_selector($name, $selected_id, $stock_types, 
965                 array( 
966                         'select_submit'=> true, 
967                         'disabled' => !$enabled) );
968         echo "</td></tr>\n";
969 }
970
971 function stock_units_list_row($label, $name, $value=null, $enabled=true)
972 {
973         $result = get_all_item_units();
974         echo "<tr>";
975         if ($label != null)
976                 echo "<td class='label'>$label</td>\n";
977         echo "<td>";
978
979         while($unit = db_fetch($result))
980                 $units[$unit['abbr']] = $unit['name'];
981
982         echo array_selector($name, $value, $units, array( 'disabled' => !$enabled) );
983
984         echo "</td></tr>\n";
985 }
986
987 //------------------------------------------------------------------------------------
988
989 function tax_types_list($name, $selected_id=null, $none_option=false, $submit_on_change=false)
990 {
991         $sql = "SELECT id, CONCAT(name, ' (',rate,'%)') as name FROM ".TB_PREF."tax_types";
992
993         return combo_input($name, $selected_id, $sql, 'id', 'name',
994         array(
995                 'spec_option' => $none_option,
996                 'spec_id' => ALL_NUMERIC,
997                 'select_submit'=> $submit_on_change,
998                 'async' => false,
999         ) );
1000 }
1001
1002 function tax_types_list_cells($label, $name, $selected_id=null, $none_option=false,
1003         $submit_on_change=false)
1004 {
1005         if ($label != null)
1006                 echo "<td>$label</td>\n";
1007         echo "<td>";
1008         echo tax_types_list($name, $selected_id, $none_option, $submit_on_change);
1009         echo "</td>\n";
1010 }
1011
1012 function tax_types_list_row($label, $name, $selected_id=null, $none_option=false,
1013         $submit_on_change=false)
1014 {
1015         echo "<tr><td class='label'>$label</td>";
1016         tax_types_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1017         echo "</tr>\n";
1018 }
1019
1020 //------------------------------------------------------------------------------------
1021
1022 function tax_groups_list($name, $selected_id=null,
1023         $none_option=false, $submit_on_change=false)
1024 {
1025         $sql = "SELECT id, name FROM ".TB_PREF."tax_groups";
1026
1027         return combo_input($name, $selected_id, $sql, 'id', 'name',
1028         array(
1029                 'order' => 'id',
1030                 'spec_option' => $none_option,
1031                 'spec_id' => ALL_NUMERIC,
1032                 'select_submit'=> $submit_on_change,
1033                 'async' => false,
1034         ) );
1035 }
1036
1037 function tax_groups_list_cells($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1038 {
1039         if ($label != null)
1040                 echo "<td>$label</td>\n";
1041         echo "<td>";
1042         echo tax_groups_list($name, $selected_id, $none_option, $submit_on_change);
1043         echo "</td>\n";
1044 }
1045
1046 function tax_groups_list_row($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1047 {
1048         echo "<tr><td class='label'>$label</td>";
1049         tax_groups_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1050         echo "</tr>\n";
1051 }
1052
1053 //------------------------------------------------------------------------------------
1054
1055 function item_tax_types_list($name, $selected_id=null)
1056 {
1057         $sql ="SELECT id, name FROM ".TB_PREF."item_tax_types";
1058         return combo_input($name, $selected_id, $sql, 'id', 'name', array('order' => 'id') );
1059 }
1060
1061 function item_tax_types_list_cells($label, $name, $selected_id=null)
1062 {
1063         if ($label != null)
1064                 echo "<td>$label</td>\n";
1065         echo "<td>";
1066         echo item_tax_types_list($name, $selected_id);
1067         echo "</td>\n";
1068 }
1069
1070 function item_tax_types_list_row($label, $name, $selected_id=null)
1071 {
1072         echo "<tr><td class='label'>$label</td>";
1073         item_tax_types_list_cells(null, $name, $selected_id);
1074         echo "</tr>\n";
1075 }
1076
1077 //------------------------------------------------------------------------------------
1078
1079 function shippers_list($name, $selected_id=null)
1080 {
1081         $sql = "SELECT shipper_id, shipper_name, inactive FROM ".TB_PREF."shippers";
1082         return combo_input($name, $selected_id, $sql, 'shipper_id', 'shipper_name', 
1083                 array('order'=>array('shipper_name')));
1084 }
1085
1086 function shippers_list_cells($label, $name, $selected_id=null)
1087 {
1088         if ($label != null)
1089                 echo "<td>$label</td>\n";
1090         echo "<td>";
1091         echo shippers_list($name, $selected_id);
1092         echo "</td>\n";
1093 }
1094
1095 function shippers_list_row($label, $name, $selected_id=null)
1096 {
1097         echo "<tr><td class='label'>$label</td>";
1098         shippers_list_cells(null, $name, $selected_id);
1099         echo "</tr>\n";
1100 }
1101
1102 //-------------------------------------------------------------------------------------
1103
1104 function sales_persons_list($name, $selected_id=null, $spec_opt=false)
1105 {
1106         $sql = "SELECT salesman_code, salesman_name, inactive FROM ".TB_PREF."salesman";
1107         return combo_input($name, $selected_id, $sql, 'salesman_code', 'salesman_name', 
1108                 array('order'=>array('salesman_name'),
1109                         'spec_option' => $spec_opt,
1110                         'spec_id' => ALL_NUMERIC));
1111 }
1112
1113 function sales_persons_list_cells($label, $name, $selected_id=null, $spec_opt=false)
1114 {
1115         if ($label != null)
1116                 echo "<td>$label</td>\n";
1117         echo "<td>\n";
1118         echo sales_persons_list($name, $selected_id, $spec_opt);
1119         echo "</td>\n";
1120 }
1121
1122 function sales_persons_list_row($label, $name, $selected_id=null, $spec_opt=false)
1123 {
1124         echo "<tr><td class='label'>$label</td>";
1125         sales_persons_list_cells(null, $name, $selected_id, $spec_opt);
1126         echo "</tr>\n";
1127 }
1128
1129 //------------------------------------------------------------------------------------
1130
1131 function sales_areas_list($name, $selected_id=null)
1132 {
1133         $sql = "SELECT area_code, description, inactive FROM ".TB_PREF."areas";
1134         return combo_input($name, $selected_id, $sql, 'area_code', 'description', array());
1135 }
1136
1137 function sales_areas_list_cells($label, $name, $selected_id=null)
1138 {
1139         if ($label != null)
1140                 echo "<td>$label</td>\n";
1141         echo "<td>";
1142         echo sales_areas_list($name, $selected_id);
1143         echo "</td>\n";
1144 }
1145
1146 function sales_areas_list_row($label, $name, $selected_id=null)
1147 {
1148         echo "<tr><td class='label'>$label</td>";
1149         sales_areas_list_cells(null, $name, $selected_id);
1150         echo "</tr>\n";
1151 }
1152
1153 //------------------------------------------------------------------------------------
1154
1155 function sales_groups_list($name, $selected_id=null, $special_option=false)
1156 {
1157         $sql = "SELECT id, description, inactive FROM ".TB_PREF."groups";
1158         return combo_input($name, $selected_id, $sql, 'id', 'description', array(
1159                 'spec_option' => $special_option===true ? ' ' : $special_option,
1160                 'order' => 'description', 'spec_id' => 0,
1161         ));
1162 }
1163
1164 function sales_groups_list_cells($label, $name, $selected_id=null, $special_option=false)
1165 {
1166         if ($label != null)
1167                 echo "<td>$label</td>\n";
1168         echo "<td>";
1169         echo sales_groups_list($name, $selected_id, $special_option);
1170         echo "</td>\n";
1171 }
1172
1173 function sales_groups_list_row($label, $name, $selected_id=null, $special_option=false)
1174 {
1175         echo "<tr><td class='label'>$label</td>";
1176         sales_groups_list_cells(null, $name, $selected_id, $special_option);
1177         echo "</tr>\n";
1178 }
1179
1180 //------------------------------------------------------------------------------------
1181
1182 function _format_template_items($row)
1183 {
1184         return ($row[0] . "&nbsp;- &nbsp;" . _("Amount") . "&nbsp;".$row[1]);
1185 }
1186
1187 function templates_list($name, $selected_id=null, $special_option=false)
1188 {
1189         $sql = "SELECT sorder.order_no, Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue
1190                 FROM ".TB_PREF."sales_orders as sorder, ".TB_PREF."sales_order_details as line
1191                 WHERE sorder.order_no = line.order_no AND sorder.type = 1 GROUP BY line.order_no";
1192         return combo_input($name, $selected_id, $sql, 'order_no', 'OrderValue', array(
1193                 'format' => '_format_template_items',
1194                 'spec_option' => $special_option===true ? ' ' : $special_option,
1195                 'order' => 'order_no', 'spec_id' => 0,
1196         ));
1197 }
1198
1199 function templates_list_cells($label, $name, $selected_id=null, $special_option=false)
1200 {
1201         if ($label != null)
1202                 echo "<td>$label</td>\n";
1203         echo "<td>";
1204         echo templates_list($name, $selected_id, $special_option);
1205         echo "</td>\n";
1206 }
1207
1208 function templates_list_row($label, $name, $selected_id=null, $special_option=false)
1209 {
1210         echo "<tr><td class='label'>$label</td>";
1211         templates_list_cells(null, $name, $selected_id, $special_option);
1212         echo "</tr>\n";
1213 }
1214
1215 //------------------------------------------------------------------------------------
1216
1217 function workorders_list($name, $selected_id=null)
1218 {
1219         $sql = "SELECT id, wo_ref FROM ".TB_PREF."workorders WHERE closed=0";
1220         return combo_input($name, $selected_id, $sql, 'id', 'wo_ref', array());
1221 }
1222
1223 function workorders_list_cells($label, $name, $selected_id=null)
1224 {
1225         if ($label != null)
1226                 echo "<td>$label</td>\n";
1227         echo "<td>";
1228         echo workorders_list($name, $selected_id);
1229         echo "</td>\n";
1230 }
1231
1232 function workorders_list_row($label, $name, $selected_id=null)
1233 {
1234         echo "<tr><td class='label'>$label</td>";
1235         workorders_list_cells(null, $name, $selected_id);
1236         echo "</tr>\n";
1237 }
1238
1239 //------------------------------------------------------------------------------------
1240
1241 function payment_terms_list($name, $selected_id=null)
1242 {
1243         $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
1244         return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms', array());
1245 }
1246
1247 function payment_terms_list_cells($label, $name, $selected_id=null)
1248 {
1249         if ($label != null)
1250                 echo "<td>$label</td>\n";
1251         echo "<td>";
1252         echo payment_terms_list($name, $selected_id);
1253         echo "</td>\n";
1254 }
1255
1256 function payment_terms_list_row($label, $name, $selected_id=null)
1257 {
1258         echo "<tr><td class='label'>$label</td>";
1259         payment_terms_list_cells(null, $name, $selected_id);
1260         echo "</tr>\n";
1261 }
1262
1263 //------------------------------------------------------------------------------------
1264
1265 function credit_status_list($name, $selected_id=null)
1266 {
1267         $sql ="SELECT id, reason_description, inactive FROM ".TB_PREF."credit_status";
1268         return combo_input($name, $selected_id, $sql, 'id', 'reason_description', array());
1269 }
1270
1271 function credit_status_list_cells($label, $name, $selected_id=null)
1272 {
1273         if ($label != null)
1274                 echo "<td>$label</td>\n";
1275         echo "<td>";
1276         echo credit_status_list($name, $selected_id);
1277         echo "</td>\n";
1278 }
1279
1280 function credit_status_list_row($label, $name, $selected_id=null)
1281 {
1282         echo "<tr><td class='label'>$label</td>";
1283         credit_status_list_cells(null, $name, $selected_id);
1284         echo "</tr>\n";
1285 }
1286
1287 //-----------------------------------------------------------------------------------------------
1288
1289 function sales_types_list($name, $selected_id=null, $submit_on_change=false, $special_option=false)
1290 {
1291         $sql = "SELECT id, sales_type, inactive FROM ".TB_PREF."sales_types";
1292
1293         return combo_input($name, $selected_id, $sql, 'id', 'sales_type',
1294         array(
1295                 'spec_option' => $special_option===true ? _("All Sales Types") : $special_option,
1296                 'spec_id' => 0,
1297                 'select_submit'=> $submit_on_change,
1298         //        'async' => false,
1299         ) );
1300 }
1301
1302 function sales_types_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1303 {
1304         if ($label != null)
1305                 echo "<td>$label</td>\n";
1306         echo "<td>";
1307         echo sales_types_list($name, $selected_id, $submit_on_change, $special_option);
1308         echo "</td>\n";
1309 }
1310
1311 function sales_types_list_row($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1312 {
1313         echo "<tr><td class='label'>$label</td>";
1314         sales_types_list_cells(null, $name, $selected_id, $submit_on_change, $special_option);
1315         echo "</tr>\n";
1316 }
1317
1318 //-----------------------------------------------------------------------------------------------
1319
1320 function movement_types_list($name, $selected_id=null)
1321 {
1322         $sql = "SELECT id, name FROM ".TB_PREF."movement_types";
1323         return combo_input($name, $selected_id, $sql, 'id', 'name', array());
1324 }
1325
1326 function movement_types_list_cells($label, $name, $selected_id=null)
1327 {
1328         if ($label != null)
1329                 echo "<td>$label</td>\n";
1330         echo "<td>";
1331         echo movement_types_list($name, $selected_id);
1332         echo "</td>\n";
1333 }
1334
1335 function movement_types_list_row($label, $name, $selected_id=null)
1336 {
1337         echo "<tr><td class='label'>$label</td>";
1338         movement_types_list_cells(null, $name, $selected_id);
1339         echo "</tr>\n";
1340 }
1341
1342 //-----------------------------------------------------------------------------------------------
1343 function _format_date($row)
1344 {
1345         return sql2date($row['reconciled']);
1346 }
1347
1348 function bank_reconciliation_list($account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1349 {
1350         $sql = "SELECT reconciled, reconciled FROM ".TB_PREF."bank_trans
1351                 WHERE bank_act=".db_escape($account)." AND reconciled IS NOT NULL
1352                 GROUP BY reconciled";
1353         return combo_input($name, $selected_id, $sql, 'id', 'reconciled',
1354         array(
1355                 'spec_option' => $special_option,
1356                 'format' => '_format_date',
1357                 'spec_id' => '',
1358                 'select_submit'=> $submit_on_change
1359         ) );
1360 }
1361
1362 function bank_reconciliation_list_cells($label,$account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1363 {
1364         if ($label != null)
1365                 echo "<td>$label</td>\n";
1366         echo "<td>";
1367         echo bank_reconciliation_list($account, $name, $selected_id, $submit_on_change, $special_option);
1368         echo "</td>\n";
1369 }
1370 /*
1371 function bank_reconciliation_list_row($label, $account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1372 {
1373         echo "<tr>\n";
1374         bank_reconciliation_list_cells($label, $account, $name, $selected_id, $submit_on_change, $special_option);
1375         echo "</tr>\n";
1376 }
1377 */
1378 //-----------------------------------------------------------------------------------------------
1379
1380 function workcenter_list($name, $selected_id=null, $all_option=false)
1381 {
1382         global $all_items;
1383
1384         $sql = "SELECT id, name, inactive FROM ".TB_PREF."workcentres";
1385
1386         return combo_input($name, $selected_id, $sql, 'id', 'name',
1387         array(
1388                 'spec_option' =>$all_option===true ? _("All Suppliers") : $all_option,
1389                 'spec_id' => $all_items,
1390         ) );
1391 }
1392
1393 function workcenter_list_cells($label, $name, $selected_id=null, $all_option=false)
1394 {
1395         default_focus($name);
1396         if ($label != null)
1397                 echo "<td>$label</td>\n";
1398         echo "<td>";
1399         echo workcenter_list($name, $selected_id, $all_option);
1400         echo "</td>\n";
1401 }
1402
1403 function workcenter_list_row($label, $name, $selected_id=null, $all_option=false)
1404 {
1405         echo "<tr><td class='label'>$label</td>";
1406         workcenter_list_cells(null, $name, $selected_id, $all_option);
1407         echo "</tr>\n";
1408 }
1409
1410 //-----------------------------------------------------------------------------------------------
1411
1412 function bank_accounts_list($name, $selected_id=null, $submit_on_change=false)
1413 {
1414         $sql = "SELECT ".TB_PREF."bank_accounts.id, bank_account_name, bank_curr_code, inactive
1415                 FROM ".TB_PREF."bank_accounts";
1416
1417         return combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1418         array(
1419                 'format' => '_format_add_curr',
1420                 'select_submit'=> $submit_on_change,
1421                 'async' => false
1422         ) );
1423 }
1424
1425 function bank_accounts_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1426 {
1427         if ($label != null)
1428                 echo "<td>$label</td>\n";
1429         echo "<td>";
1430         echo bank_accounts_list($name, $selected_id, $submit_on_change);
1431         echo "</td>\n";
1432 }
1433
1434 function bank_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1435 {
1436         echo "<tr><td class='label'>$label</td>";
1437         bank_accounts_list_cells(null, $name, $selected_id, $submit_on_change);
1438         echo "</tr>\n";
1439 }
1440 //-----------------------------------------------------------------------------------------------
1441
1442 function cash_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1443 {
1444         $sql = "SELECT ".TB_PREF."bank_accounts.id, bank_account_name, bank_curr_code, inactive
1445                 FROM ".TB_PREF."bank_accounts
1446                 WHERE ".TB_PREF."bank_accounts.account_type=".BT_CASH;
1447
1448         if ($label != null)
1449                 echo "<tr><td class='label'>$label</td>\n";
1450         echo "<td>";
1451         echo combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1452         array(
1453                 'format' => '_format_add_curr',
1454                 'select_submit'=> $submit_on_change,
1455                 'async' => true
1456         ) );
1457         echo "</td></tr>\n";
1458 }
1459 //-----------------------------------------------------------------------------------------------
1460
1461 function pos_list_row($label, $name, $selected_id=null, $spec_option=false, $submit_on_change=false)
1462 {
1463         $sql = "SELECT id, pos_name, inactive FROM ".TB_PREF."sales_pos";
1464
1465         default_focus($name);
1466         echo '<tr>';
1467         if ($label != null)
1468                 echo "<td class='label'>$label</td>\n";
1469         echo "<td>";
1470
1471         echo combo_input($name, $selected_id, $sql, 'id', 'pos_name',
1472         array(
1473                 'select_submit'=> $submit_on_change,
1474                 'async' => true,
1475                 'spec_option' =>$spec_option,
1476                 'spec_id' => -1,
1477                 'order'=> array('pos_name')
1478         ) );
1479         echo "</td></tr>\n";
1480
1481 }
1482 //-----------------------------------------------------------------------------------------------
1483 // Payment type selector for current user.
1484 //
1485 function sale_payment_list($name, $category, $selected_id=null, $submit_on_change=true)
1486 {
1487         $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
1488         
1489         if ($category == PM_CASH) // only cash
1490                         $sql .= " WHERE days_before_due=0 AND day_in_following_month=0";
1491         if ($category == PM_CREDIT) // only delayed payments
1492                         $sql .= " WHERE days_before_due!=0 OR day_in_following_month!=0";
1493
1494         return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms',
1495         array(
1496                 'select_submit'=> $submit_on_change,
1497                 'async' => true
1498         ) );
1499
1500 }
1501
1502 function sale_payment_list_cells($label, $name, $category, $selected_id=null, $submit_on_change=true)
1503 {
1504         if ($label != null)
1505                 echo "<td class='label'>$label</td>\n";
1506         echo "<td>";
1507
1508         echo sale_payment_list($name, $category, $selected_id, $submit_on_change);
1509
1510         echo "</td>\n";
1511 }
1512 //-----------------------------------------------------------------------------------------------
1513
1514 function class_list($name, $selected_id=null, $submit_on_change=false)
1515 {
1516         $sql = "SELECT cid, class_name FROM ".TB_PREF."chart_class";
1517
1518         return combo_input($name, $selected_id, $sql, 'cid', 'class_name',
1519         array(
1520                 'select_submit'=> $submit_on_change,
1521                 'async' => false
1522         ) );
1523
1524 }
1525
1526 function class_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1527 {
1528         if ($label != null)
1529                 echo "<td>$label</td>\n";
1530         echo "<td>";
1531         echo class_list($name, $selected_id, $submit_on_change);
1532         echo "</td>\n";
1533 }
1534
1535 function class_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1536 {
1537         echo "<tr><td class='label'>$label</td>";
1538         class_list_cells(null, $name, $selected_id, $submit_on_change);
1539         echo "</tr>\n";
1540 }
1541
1542 //-----------------------------------------------------------------------------------------------
1543 function stock_categories_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1544 {
1545         $sql = "SELECT category_id, description, inactive FROM ".TB_PREF."stock_category";
1546         return combo_input($name, $selected_id, $sql, 'category_id', 'description',
1547         array('order'=>'category_id',
1548                 'spec_option' => $spec_opt,
1549                 'spec_id' => -1,
1550                 'select_submit'=> $submit_on_change,
1551                 'async' => true
1552         ));
1553 }
1554
1555 function stock_categories_list_cells($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1556 {
1557         if ($label != null)
1558                 echo "<td>$label</td>\n";
1559         echo "<td>";
1560         echo stock_categories_list($name, $selected_id, $spec_opt, $submit_on_change);
1561         echo "</td>\n";
1562 }
1563
1564 function stock_categories_list_row($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1565 {
1566         echo "<tr><td class='label'>$label</td>";
1567         stock_categories_list_cells(null, $name, $selected_id, $spec_opt, $submit_on_change);
1568         echo "</tr>\n";
1569 }
1570
1571 //-----------------------------------------------------------------------------------------------
1572
1573 function gl_account_types_list($name, $selected_id=null, $all_option=false, $all=true)
1574 {
1575         global $all_items;
1576
1577         $sql = "SELECT id, name FROM ".TB_PREF."chart_types";
1578
1579         return combo_input($name, $selected_id, $sql, 'id', 'name',
1580         array(
1581                 'format' => '_format_account',
1582                 'order' => array('class_id', 'id', 'parent'),
1583                 'spec_option' =>$all_option,
1584                 'spec_id' => $all_items
1585         ) );
1586 }
1587
1588 function gl_account_types_list_cells($label, $name, $selected_id=null, $all_option=false, $all=false)
1589 {
1590         if ($label != null)
1591                 echo "<td>$label</td>\n";
1592         echo "<td>";
1593         echo gl_account_types_list($name, $selected_id, $all_option, $all);
1594         echo "</td>\n";
1595 }
1596
1597 function gl_account_types_list_row($label, $name, $selected_id=null, $all_option=false, $all=false)
1598 {
1599         echo "<tr><td class='label'>$label</td>";
1600         gl_account_types_list_cells(null, $name, $selected_id, $all_option,
1601                 $all);
1602         echo "</tr>\n";
1603 }
1604
1605 //-----------------------------------------------------------------------------------------------
1606 function gl_all_accounts_list($name, $selected_id=null, $skip_bank_accounts=false,
1607         $cells=false, $all_option=false, $submit_on_change=false, $all=false)
1608 {
1609         if ($skip_bank_accounts)
1610                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
1611                         FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
1612                         ."LEFT JOIN ".TB_PREF."bank_accounts acc "
1613                         ."ON chart.account_code=acc.account_code
1614                                 WHERE acc.account_code  IS NULL
1615                         AND chart.account_type=type.id";
1616         else
1617                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
1618                         FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type
1619                         WHERE chart.account_type=type.id";
1620
1621         return combo_input($name, $selected_id, $sql, 'chart.account_code', 'chart.account_name',
1622         array(
1623                 'format' => '_format_account',
1624                 'spec_option' => $all_option===true ?  _("Use Item Sales Accounts") : $all_option,
1625                 'spec_id' => '',
1626                 'type' => 2,
1627                 'order' => array('type.class_id','type.id','account_code'),
1628                 'search_box' => $cells,
1629                         'search_submit' => false,
1630                         'size' => 12,
1631                         'max' => 10,
1632                         'cells' => true,
1633                 'select_submit'=> $submit_on_change,
1634                 'async' => false,
1635                 'category' => 2,
1636                 'show_inactive' => $all
1637         ) );
1638
1639 }
1640
1641 function _format_account($row)
1642 {
1643                 return $row[0] .  "&nbsp;&nbsp;&nbsp;&nbsp;" . $row[1];
1644 }
1645
1646 function gl_all_accounts_list_cells($label, $name, $selected_id=null, 
1647         $skip_bank_accounts=false, $cells=false, $all_option=false, 
1648         $submit_on_change=false, $all=false)
1649 {
1650         if ($label != null)
1651                 echo "<td>$label</td>\n";
1652         echo "<td>";
1653         echo gl_all_accounts_list($name, $selected_id, 
1654                 $skip_bank_accounts, $cells, $all_option, $submit_on_change, $all);
1655         echo "</td>\n";
1656 }
1657
1658 function gl_all_accounts_list_row($label, $name, $selected_id=null, 
1659         $skip_bank_accounts=false, $cells=false, $all_option=false)
1660 {
1661         echo "<tr><td class='label'>$label</td>";
1662         gl_all_accounts_list_cells(null, $name, $selected_id, 
1663                 $skip_bank_accounts, $cells, $all_option);
1664         echo "</tr>\n";
1665 }
1666
1667 function yesno_list($name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1668 {
1669         $items = array();
1670         $items['0'] = strlen($name_no) ? $name_no : _("No");
1671         $items['1'] = strlen($name_yes) ? $name_yes : _("Yes");
1672
1673         return array_selector($name, $selected_id, $items, 
1674                 array( 
1675                         'select_submit'=> $submit_on_change,
1676                         'async' => false ) ); // FIX?
1677 }
1678
1679 function yesno_list_cells($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1680 {
1681         if ($label != null)
1682                 echo "<td>$label</td>\n";
1683         echo "<td>";
1684         echo yesno_list($name, $selected_id, $name_yes, $name_no, $submit_on_change);
1685         echo "</td>\n";
1686 }
1687
1688 function yesno_list_row($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1689 {
1690         echo "<tr><td class='label'>$label</td>";
1691         yesno_list_cells(null, $name, $selected_id, $name_yes, $name_no, $submit_on_change);
1692         echo "</tr>\n";
1693 }
1694
1695 //------------------------------------------------------------------------------------------------
1696
1697 function languages_list($name, $selected_id=null, $all_option=false)
1698 {
1699         global $installed_languages;
1700
1701         $items = array();
1702         if ($all_option)
1703                 $items[''] = $all_option;
1704         foreach ($installed_languages as $lang)
1705                         $items[$lang['code']] = $lang['name'];
1706         return array_selector($name, $selected_id, $items);
1707 }
1708
1709 function languages_list_cells($label, $name, $selected_id=null, $all_option=false)
1710 {
1711         if ($label != null)
1712                 echo "<td>$label</td>\n";
1713         echo "<td>";
1714         echo languages_list($name, $selected_id, $all_option);
1715         echo "</td>\n";
1716 }
1717
1718 function languages_list_row($label, $name, $selected_id=null, $all_option=false)
1719 {
1720         echo "<tr><td class='label'>$label</td>";
1721         languages_list_cells(null, $name, $selected_id, $all_option);
1722         echo "</tr>\n";
1723 }
1724
1725 //------------------------------------------------------------------------------------------------
1726
1727 function bank_account_types_list($name, $selected_id=null)
1728 {
1729         global $bank_account_types;
1730
1731         return array_selector($name, $selected_id, $bank_account_types);
1732 }
1733
1734 function bank_account_types_list_cells($label, $name, $selected_id=null)
1735 {
1736         if ($label != null)
1737                 echo "<td>$label</td>\n";
1738         echo "<td>";
1739         echo bank_account_types_list($name, $selected_id);
1740         echo "</td>\n";
1741 }
1742
1743 function bank_account_types_list_row($label, $name, $selected_id=null)
1744 {
1745         echo "<tr><td class='label'>$label</td>";
1746         bank_account_types_list_cells(null, $name, $selected_id);
1747         echo "</tr>\n";
1748 }
1749
1750 //------------------------------------------------------------------------------------------------
1751 function payment_person_types_list($name, $selected_id=null, $submit_on_change=false)
1752 {
1753         global $payment_person_types;
1754
1755         $items = array();
1756         foreach ($payment_person_types as $key=>$type)
1757         {
1758                 if ($key != PT_WORKORDER)
1759                         $items[$key] = $type;
1760         }               
1761         return array_selector($name, $selected_id, $items, 
1762                 array( 'select_submit'=> $submit_on_change ) );
1763 }
1764
1765 function payment_person_types_list_cells($label, $name, $selected_id=null, $related=null)
1766 {
1767         if ($label != null)
1768                 echo "<td>$label</td>\n";
1769         echo "<td>";
1770         echo payment_person_types_list($name, $selected_id, $related);
1771         echo "</td>\n";
1772 }
1773
1774 function payment_person_types_list_row($label, $name, $selected_id=null, $related=null)
1775 {
1776         echo "<tr><td class='label'>$label</td>";
1777         payment_person_types_list_cells(null, $name, $selected_id, $related);
1778         echo "</tr>\n";
1779 }
1780
1781 //------------------------------------------------------------------------------------------------
1782
1783 function wo_types_list($name, $selected_id=null)
1784 {
1785         global $wo_types_array;
1786         
1787         return array_selector($name, $selected_id, $wo_types_array, 
1788                 array( 'select_submit'=> true, 'async' => true ) );
1789 }
1790
1791 function wo_types_list_row($label, $name, $selected_id=null)
1792 {
1793         echo "<tr><td class='label'>$label</td><td>\n";
1794         echo wo_types_list($name, $selected_id);
1795         echo "</td></tr>\n";
1796 }
1797
1798 //------------------------------------------------------------------------------------------------
1799
1800 function dateformats_list_row($label, $name, $value=null)
1801 {
1802         global $dateformats;
1803
1804         echo "<tr><td class='label'>$label</td>\n<td>";
1805         echo array_selector( $name, $value, $dateformats );
1806         echo "</td></tr>\n";
1807 }
1808
1809 function dateseps_list_row($label, $name, $value=null)
1810 {
1811         global $dateseps;
1812
1813         echo "<tr><td class='label'>$label</td>\n<td>";
1814         echo array_selector( $name, $value, $dateseps );
1815         echo "</td></tr>\n";
1816 }
1817
1818 function thoseps_list_row($label, $name, $value=null)
1819 {
1820         global $thoseps;
1821
1822         echo "<tr><td class='label'>$label</td>\n<td>";
1823         echo array_selector( $name, $value, $thoseps );
1824         echo "</td></tr>\n";
1825 }
1826
1827 function decseps_list_row($label, $name, $value=null)
1828 {
1829         global $decseps;
1830
1831         echo "<tr><td class='label'>$label</td>\n<td>";
1832         echo array_selector( $name, $value, $decseps );
1833         echo "</td></tr>\n";
1834 }
1835
1836 function themes_list_row($label, $name, $value=null)
1837 {
1838         global $path_to_root;
1839
1840         $path = $path_to_root.'/themes/';
1841         $themes = array();
1842         $themedir = opendir($path);
1843         while(false !== ($fname = readdir($themedir)))
1844         {
1845                 if($fname!='.' && $fname!='..' && $fname!='CVS' && is_dir($path.$fname))
1846                 {
1847                         $themes[$fname] =  $fname;
1848                 }
1849         }
1850         ksort($themes);
1851
1852         echo "<tr><td class='label'>$label</td>\n<td>";
1853         echo array_selector( $name, $value, $themes );
1854         echo "</td></tr>\n";
1855 }
1856
1857 function pagesizes_list_row($label, $name, $value=null)
1858 {
1859         global $pagesizes;
1860
1861         $items = array();
1862         foreach ($pagesizes as $pz)
1863                 $items[$pz] = $pz;
1864
1865         echo "<tr><td class='label'>$label</td>\n<td>";
1866         echo array_selector( $name, $value, $items );
1867         echo "</td></tr>\n";
1868 }
1869
1870 function systypes_list($name, $value=null, $spec_opt=false, $submit_on_change=false, $exclude=array())
1871 {
1872         global $systypes_array;
1873
1874         // emove non-voidable transactions if needed
1875         $systypes = array_diff_key($systypes_array, array_flip($exclude));
1876         return array_selector($name, $value, $systypes, 
1877                 array( 
1878                         'spec_option'=> $spec_opt,
1879                         'spec_id' => ALL_NUMERIC,
1880                         'select_submit'=> $submit_on_change,
1881                         'async' => false,
1882                         )
1883         );
1884 }
1885
1886 function systypes_list_cells($label, $name, $value=null, $submit_on_change=false, $exclude=array())
1887 {
1888         if ($label != null)
1889                 echo "<td>$label</td>\n";
1890         echo "<td>";
1891         echo systypes_list($name, $value, false, $submit_on_change, $exclude);
1892         echo "</td>\n";
1893 }
1894
1895 function systypes_list_row($label, $name, $value=null, $submit_on_change=false, $exclude=array())
1896 {
1897         echo "<tr><td class='label'>$label</td>";
1898         systypes_list_cells(null, $name, $value, $submit_on_change, $exclude);
1899         echo "</tr>\n";
1900 }
1901
1902 function journal_types_list_cells($label, $name, $value=null, $submit_on_change=false)
1903 {
1904         global $systypes_array;
1905
1906         if ($label != null)
1907                 echo "<td>$label</td>\n";
1908         echo "<td>";
1909
1910         $items = $systypes_array;
1911
1912         // exclude quotes, orders and dimensions
1913         foreach (array(ST_PURCHORDER, ST_WORKORDER, ST_SALESORDER, ST_DIMENSION, 
1914                                 ST_SALESQUOTE, ST_LOCTRANSFER) as $excl)
1915                         unset($items[$excl]);
1916         
1917         echo array_selector($name, $value, $items, 
1918                 array( 
1919                         'spec_option'=> _("All"),
1920                         'spec_id' => -1,
1921                         'select_submit'=> $submit_on_change,
1922                         'async' => false
1923                         )
1924         );
1925         echo "</td>\n";
1926 }
1927
1928 function cust_allocations_list_cells($label, $name, $selected=null)
1929 {
1930         global $all_items;
1931
1932         if ($label != null)
1933                 label_cell($label);
1934         echo "<td>\n";
1935         $allocs = array( 
1936                 $all_items=>_("All Types"),
1937                 '1'=> _("Sales Invoices"),
1938                 '2'=> _("Overdue Invoices"),
1939                 '3' => _("Payments"),
1940                 '4' => _("Credit Notes"),
1941                 '5' => _("Delivery Notes")
1942         );
1943         echo array_selector($name, $selected, $allocs);
1944         echo "</td>\n";
1945 }
1946
1947 function supp_allocations_list_cell($name, $selected=null)
1948 {
1949         global $all_items;
1950
1951         echo "<td>\n";
1952         $allocs = array( 
1953                 $all_items=>_("All Types"),
1954                 '1'=> _("Invoices"),
1955                 '2'=> _("Overdue Invoices"),
1956                 '3' => _("Payments"),
1957                 '4' => _("Credit Notes"),
1958                 '5' => _("Overdue Credit Notes")
1959         );
1960         echo array_selector($name, $selected, $allocs);
1961         echo "</td>\n";
1962 }
1963
1964 function supp_transactions_list_cell($name, $selected=null)
1965 {
1966         global $all_items;
1967
1968         echo "<td>\n";
1969         $allocs = array( 
1970                 $all_items=>_("All Types"),
1971                 '6'=>_("GRNs"),
1972                 '1'=> _("Invoices"),
1973                 '2'=> _("Overdue Invoices"),
1974                 '3' => _("Payments"),
1975                 '4' => _("Credit Notes"),
1976                 '5' => _("Overdue Credit Notes")
1977         );
1978
1979         echo array_selector($name, $selected, $allocs);
1980         echo "</td>\n";
1981 }
1982
1983 function policy_list_cells($label, $name, $selected=null)
1984 {
1985         if ($label != null)
1986                 label_cell($label);
1987         echo "<td>\n";
1988         echo array_selector($name, $selected, 
1989                                 array( '' => _("Automatically put balance on back order"),
1990                                         'CAN' => _("Cancel any quantites not delivered")) );
1991         echo "</td>\n";
1992 }
1993
1994 function policy_list_row($label, $name, $selected=null)
1995 {
1996         echo "<tr><td class='label'>$label</td>";
1997         policy_list_cells(null, $name, $selected);
1998         echo "</tr>\n";
1999 }
2000
2001 function credit_type_list_cells($label, $name, $selected=null, $submit_on_change=false)
2002 {
2003         if ($label != null)
2004                 label_cell($label);
2005         echo "<td>\n";
2006         echo array_selector($name, $selected, 
2007                                 array( 'Return' => _("Items Returned to Inventory Location"),
2008                                         'WriteOff' => _("Items Written Off")),
2009                                 array( 'select_submit'=> $submit_on_change ) );
2010         echo "</td>\n";
2011 }
2012
2013 function credit_type_list_row($label, $name, $selected=null, $submit_on_change=false)
2014 {
2015         echo "<tr><td class='label'>$label</td>";
2016         credit_type_list_cells(null, $name, $selected, $submit_on_change);
2017         echo "</tr>\n";
2018 }
2019
2020 function number_list($name, $selected, $from, $to, $no_option=false)
2021 {
2022         $items = array();
2023         for ($i = $from; $i <= $to; $i++)
2024                 $items[$i] = "$i";
2025
2026         return array_selector($name, $selected, $items,
2027                                 array(  'spec_option' => $no_option,
2028                                                 'spec_id' => ALL_NUMERIC) );
2029 }
2030
2031 function number_list_cells($label, $name, $selected, $from, $to, $no_option=false)
2032 {
2033         if ($label != null)
2034                 label_cell($label);
2035         echo "<td>\n";
2036         echo number_list($name, $selected, $from, $to, $no_option);
2037         echo "</td>\n";
2038 }
2039
2040 function number_list_row($label, $name, $selected, $from, $to, $no_option=false)
2041 {
2042         echo "<tr><td class='label'>$label</td>";
2043         echo number_list_cells(null, $name, $selected, $from, $to, $no_option);
2044         echo "</tr>\n";
2045 }
2046
2047 function print_profiles_list_row($label, $name, $selected_id=null, $spec_opt=false,
2048         $submit_on_change=true)
2049 {
2050         $sql = "SELECT profile FROM ".TB_PREF."print_profiles"
2051                 ." GROUP BY profile";
2052         $result = db_query($sql, 'cannot get all profile names');
2053         $profiles = array();
2054         while($myrow=db_fetch($result)) {
2055                 $profiles[$myrow['profile']] = $myrow['profile'];
2056         }
2057
2058         echo "<tr>";
2059         if ($label != null)
2060                 echo "<td class='label'>$label</td>\n";
2061         echo "<td>";
2062
2063         echo array_selector($name, $selected_id, $profiles, 
2064                 array( 'select_submit'=> $submit_on_change,
2065                         'spec_option'=>$spec_opt,
2066                         'spec_id' => ''
2067                  ));
2068
2069         echo "</td></tr>\n";
2070 }
2071
2072 function printers_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
2073 {
2074         static $printers; // query only once for page display
2075
2076         if (!$printers) {
2077                 $sql = "SELECT id, name, description FROM ".TB_PREF."printers"; 
2078                 $result = db_query($sql, 'cannot get all printers');
2079                 $printers = array();
2080                 while($myrow=db_fetch($result)) {
2081                         $printers[$myrow['id']] = $myrow['name'].'&nbsp;-&nbsp;'.$myrow['description'];
2082                 }
2083         }
2084         return array_selector($name, $selected_id, $printers, 
2085                 array( 'select_submit'=> $submit_on_change,
2086                         'spec_option'=>$spec_opt,
2087                         'spec_id' => ''
2088                  ));
2089 }
2090
2091 //------------------------------------------------------------------------------------------------
2092
2093 function quick_entries_list($name, $selected_id=null, $type=null, $submit_on_change=false)
2094 {
2095         $where = false;
2096         $sql = "SELECT id, description FROM ".TB_PREF."quick_entries";
2097         if ($type != null)
2098                 $sql .= " WHERE type=$type";
2099
2100         return combo_input($name, $selected_id, $sql, 'id', 'description',
2101                 array(
2102                         'spec_id' => '',
2103                         'order' => 'description',
2104                         'select_submit'=> $submit_on_change,
2105                         'async' => false
2106                 ) );
2107
2108 }
2109
2110 function quick_entries_list_cells($label, $name, $selected_id=null, $type, $submit_on_change=false)
2111 {
2112         if ($label != null)
2113                 echo "<td>$label</td>\n";
2114         echo "<td>";    
2115         echo quick_entries_list($name, $selected_id, $type, $submit_on_change);
2116         echo "</td>";
2117 }
2118
2119 function quick_entries_list_row($label, $name, $selected_id=null, $type, $submit_on_change=false)
2120 {
2121         echo "<tr><td class='label'>$label</td>";
2122         quick_entries_list_cells(null, $name, $selected_id, $type, $submit_on_change);
2123         echo "</tr>\n";
2124 }
2125
2126
2127 function quick_actions_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2128 {
2129         global $quick_actions;
2130         
2131         echo "<tr><td class='label'>$label</td><td>";
2132         echo array_selector($name, $selected_id, $quick_actions, 
2133                 array( 
2134                         'select_submit'=> $submit_on_change
2135                 ) );
2136         echo "</td></tr>\n";
2137 }
2138
2139 function quick_entry_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2140 {
2141         global $quick_entry_types;
2142                 
2143         echo "<tr><td class='label'>$label</td><td>";
2144         echo array_selector($name, $selected_id, $quick_entry_types, 
2145                 array( 
2146                         'select_submit'=> $submit_on_change
2147                         ) );
2148         echo "</td></tr>\n";
2149 }
2150
2151 function record_status_list_row($label, $name) {
2152         return yesno_list_row($label, $name, null,      _('Inactive'), _('Active'));
2153 }
2154
2155 function class_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2156 {
2157         global $class_types;
2158         
2159         echo "<tr><td class='label'>$label</td><td>";
2160         echo array_selector($name, $selected_id, $class_types, 
2161                 array( 
2162                         'select_submit'=> $submit_on_change
2163                 ) );
2164         echo "</td></tr>\n";
2165 }
2166
2167 //------------------------------------------------------------------------------------------------
2168
2169 function security_roles_list($name, $selected_id=null, $new_item=false, $submit_on_change=false,
2170         $show_inactive = false)
2171 {
2172         global $all_items;
2173
2174         $sql = "SELECT id, role, inactive FROM ".TB_PREF."security_roles";
2175
2176 return combo_input($name, $selected_id, $sql, 'id', 'description',
2177         array(
2178                 'spec_option'=>$new_item ? _("New role") : false,
2179                 'spec_id' => '',
2180                 'select_submit'=> $submit_on_change,
2181                 'show_inactive' => $show_inactive
2182         ) );
2183 }
2184
2185 function security_roles_list_cells($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2186         $show_inactive = false)
2187 {
2188         if ($label != null)
2189                 echo "<td>$label</td>\n";
2190         echo "<td>";
2191         echo security_roles_list($name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2192         echo "</td>\n";
2193 }
2194
2195 function security_roles_list_row($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2196         $show_inactive = false)
2197 {
2198         echo "<tr><td class='label'>$label</td>";
2199         security_roles_list_cells(null, $name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2200         echo "</tr>\n";
2201 }
2202
2203 function tab_list_row($label, $name, $selected_id=null)
2204 {
2205         global $installed_extensions;
2206         
2207         $tabs = array();
2208         foreach ($_SESSION['App']->applications as $app) {
2209                 $tabs[$app->id] = access_string($app->name, true);
2210         }
2211         echo "<tr>\n";
2212         echo "<td class='label'>$label</td><td>\n";
2213         echo array_selector($name, $selected_id, $tabs);
2214         echo "</td></tr>\n";
2215 }
2216
2217 //-----------------------------------------------------------------------------------------------
2218
2219 function tag_list($name, $height, $type, $multi=false, $all=false, $spec_opt = false)
2220 {
2221         // Get tags
2222         global $path_to_root;
2223         include_once($path_to_root . "/admin/db/tags_db.inc");
2224         $results = get_tags($type, $all);
2225
2226         while ($tag = db_fetch($results))
2227                 $tags[$tag['id']] = $tag['name'];
2228         
2229         if (!isset($tags)) {
2230                 $tags[''] = $all ? _("No tags defined.") : _("No active tags defined.");
2231                 $spec_opt = false;
2232         }
2233         return array_selector($name, null, $tags,
2234                 array(
2235                         'multi' => $multi,
2236                         'height' => $height,
2237                         'spec_option'=> $spec_opt,
2238                         'spec_id' => -1,
2239                 ) );
2240 }
2241
2242 function tag_list_cells($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2243 {
2244         if ($label != null)
2245                 echo "<td>$label</td>\n";
2246         echo "<td>\n";
2247         echo tag_list($name, $height, $type, $mult, $all, $spec_opt);
2248         echo "</td>\n";
2249         
2250 }
2251
2252 function tag_list_row($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2253 {
2254         echo "<tr><td class='label'>$label</td>";
2255         tag_list_cells(null, $name, $height, $type, $mult, $all, $spec_opt);
2256         echo "</tr>\n"; 
2257 }
2258
2259 //---------------------------------------------------------------------------------------------
2260 //      List of sets of active extensions 
2261 //
2262 function extset_list($name, $value=null, $submit_on_change=false)
2263 {
2264         global $db_connections;
2265
2266         $items = array();
2267         foreach ($db_connections as $comp)
2268                 $items[] = sprintf(_("Activated for '%s'"), $comp['name']);
2269         return array_selector( $name, $value, $items,
2270                 array(
2271                         'spec_option'=> _("Available and/or installed"),
2272                         'spec_id' => -1,
2273                         'select_submit'=> $submit_on_change,
2274                         'async' => true
2275                 ));
2276 }
2277
2278 function crm_category_types_list($name, $selected_id=null, $filter=array(), $submit_on_change=true)
2279 {
2280
2281         $sql = "SELECT id, name, type, inactive FROM ".TB_PREF."crm_categories";
2282
2283         $multi = false;
2284         $groups = false;
2285         $where = array();
2286         if (@$filter['class']) {
2287                 $where[] = 'type='.db_escape($filter['class']);
2288         } else
2289                 $groups = 'type';
2290         if (@$filter['subclass']) $where[] = 'action='.db_escape($filter['subclass']);
2291         if (@$filter['entity']) $where[] = 'entity_id='.db_escape($filter['entity']);
2292         if (@$filter['multi']) { // contact category selector for person
2293                 $multi = true;
2294         }
2295
2296         return combo_input($name, $selected_id, $sql, 'id', 'name',
2297                 array(
2298                         'multi' => $multi,
2299                         'height' => $multi ? 5:1,
2300                         'category' => $groups,
2301                         'select_submit'=> $submit_on_change,
2302                         'async' => true,
2303                         'where' => $where
2304                 ));
2305 }
2306
2307 function crm_category_types_list_row($label, $name, $selected_id=null, $filter=array(), $submit_on_change=true)
2308 {
2309         echo "<tr><td class='label'>$label</td><td>";
2310         echo crm_category_types_list($name, $selected_id, $filter, $submit_on_change);
2311         echo "</td></tr>\n";
2312 }
2313
2314 function payment_type_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2315 {
2316         global $pterm_types;
2317         
2318         echo "<tr><td class='label'>$label</td><td>";
2319         echo array_selector($name, $selected_id, $pterm_types, 
2320                 array( 
2321                         'select_submit'=> $submit_on_change
2322                 ) );
2323         echo "</td></tr>\n";
2324 }
2325
2326 function coa_list_row($label, $name, $value=null)
2327 {
2328         global $path_to_root, $installed_extensions;
2329
2330         $path = $path_to_root.'/sql/';
2331         $coas = array();
2332         $sqldir = opendir($path);
2333         while (false !== ($fname = readdir($sqldir)))
2334         {
2335                 if (is_file($path.$fname) && substr($fname,-4)=='.sql' && @($fname[2] == '_'))
2336                 {
2337                         $ext = array_search_value($fname, $installed_extensions, 'sql');
2338                         if ($ext!=null) {
2339                                 $descr = $ext['name'];
2340                         } elseif ($fname == 'en_US-new.sql') { // two standard COAs
2341                                 $descr = _("Standard new company American COA (4 digit)");
2342                         } elseif ($fname == 'en_US-demo.sql') {
2343                                 $descr = _("Standard American COA (4 digit) with demo data");
2344                         } else
2345                                 $descr = $fname;
2346
2347                         $coas[$fname] =  $descr;
2348                 }
2349         }
2350         ksort($coas);
2351
2352         echo "<tr><td class='label'>$label</td>\n<td>";
2353         echo array_selector( $name, $value, $coas );
2354         echo "</td></tr>\n";
2355 }
2356
2357 function payment_services($name)
2358 {
2359         global $payment_services;
2360
2361         $services = array_combine(array_keys($payment_services), array_keys($payment_services));
2362
2363         return array_selector($name, null, $services, array(
2364                         'spec_option'=> _("No payment Link"),
2365                         'spec_id' => '',
2366                 ));
2367 }
2368
2369 ?>