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