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