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