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