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