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