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