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