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