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