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