Account Class/Group drill down added according to Apmuthu.
[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, $type_id=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         if ($type_id ) $sql .= " AND chart.account_type=".db_escape($type_id);
1822
1823         return combo_input($name, $selected_id, $sql, 'chart.account_code', 'chart.account_name',
1824                 array(
1825                         'format' => '_format_account',
1826                         'spec_option' => $all_option===true ?  _("Use Item Sales Accounts") : $all_option,
1827                         'spec_id' => '',
1828                         'type' => 2,
1829                         'order' => array('type.class_id','type.id','account_code'),
1830                         'search_box' => $cells,
1831                         'search_submit' => false,
1832                         'size' => 12,
1833                         'max' => 10,
1834                         'cells' => true,
1835                         'select_submit'=> $submit_on_change,
1836                         'async' => false,
1837                         'category' => 2,
1838                         'show_inactive' => $all
1839                 ), "account" );
1840 }
1841
1842 function _format_account($row)
1843 {
1844         return $row[0] .  "&nbsp;&nbsp;&nbsp;&nbsp;" . $row[1];
1845 }
1846
1847 function gl_all_accounts_list_cells($label, $name, $selected_id=null, 
1848         $skip_bank_accounts=false, $cells=false, $all_option=false, 
1849         $submit_on_change=false, $all=false, $type_id=false)
1850 {
1851         if ($label != null)
1852                 echo "<td>$label</td>\n";
1853         echo "<td>";
1854         echo gl_all_accounts_list($name, $selected_id, 
1855                 $skip_bank_accounts, $cells, $all_option, $submit_on_change, $all, $type_id);
1856         echo "</td>\n";
1857 }
1858
1859 function gl_all_accounts_list_row($label, $name, $selected_id=null, 
1860         $skip_bank_accounts=false, $cells=false, $all_option=false, $type_id=false)
1861 {
1862         echo "<tr><td class='label'>$label</td>";
1863         gl_all_accounts_list_cells(null, $name, $selected_id, 
1864                 $skip_bank_accounts, $cells, $all_option, $type_id);
1865         echo "</tr>\n";
1866 }
1867
1868 function yesno_list($name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1869 {
1870         $items = array();
1871         $items['0'] = strlen($name_no) ? $name_no : _("No");
1872         $items['1'] = strlen($name_yes) ? $name_yes : _("Yes");
1873
1874         return array_selector($name, $selected_id, $items, 
1875                 array( 
1876                         'select_submit'=> $submit_on_change,
1877                         'async' => false ) ); // FIX?
1878 }
1879
1880 function yesno_list_cells($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1881 {
1882         if ($label != null)
1883                 echo "<td>$label</td>\n";
1884         echo "<td>";
1885         echo yesno_list($name, $selected_id, $name_yes, $name_no, $submit_on_change);
1886         echo "</td>\n";
1887 }
1888
1889 function yesno_list_row($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1890 {
1891         echo "<tr><td class='label'>$label</td>";
1892         yesno_list_cells(null, $name, $selected_id, $name_yes, $name_no, $submit_on_change);
1893         echo "</tr>\n";
1894 }
1895
1896 //------------------------------------------------------------------------------------------------
1897
1898 function languages_list($name, $selected_id=null, $all_option=false)
1899 {
1900         global $installed_languages;
1901
1902         $items = array();
1903         if ($all_option)
1904                 $items[''] = $all_option;
1905         foreach ($installed_languages as $lang)
1906                         $items[$lang['code']] = $lang['name'];
1907         return array_selector($name, $selected_id, $items);
1908 }
1909
1910 function languages_list_cells($label, $name, $selected_id=null, $all_option=false)
1911 {
1912         if ($label != null)
1913                 echo "<td>$label</td>\n";
1914         echo "<td>";
1915         echo languages_list($name, $selected_id, $all_option);
1916         echo "</td>\n";
1917 }
1918
1919 function languages_list_row($label, $name, $selected_id=null, $all_option=false)
1920 {
1921         echo "<tr><td class='label'>$label</td>";
1922         languages_list_cells(null, $name, $selected_id, $all_option);
1923         echo "</tr>\n";
1924 }
1925
1926 //------------------------------------------------------------------------------------------------
1927
1928 function bank_account_types_list($name, $selected_id=null)
1929 {
1930         global $bank_account_types;
1931
1932         return array_selector($name, $selected_id, $bank_account_types);
1933 }
1934
1935 function bank_account_types_list_cells($label, $name, $selected_id=null)
1936 {
1937         if ($label != null)
1938                 echo "<td>$label</td>\n";
1939         echo "<td>";
1940         echo bank_account_types_list($name, $selected_id);
1941         echo "</td>\n";
1942 }
1943
1944 function bank_account_types_list_row($label, $name, $selected_id=null)
1945 {
1946         echo "<tr><td class='label'>$label</td>";
1947         bank_account_types_list_cells(null, $name, $selected_id);
1948         echo "</tr>\n";
1949 }
1950
1951 //------------------------------------------------------------------------------------------------
1952 function payment_person_types_list($name, $selected_id=null, $submit_on_change=false)
1953 {
1954         global $payment_person_types;
1955
1956         $items = array();
1957         foreach ($payment_person_types as $key=>$type)
1958         {
1959                 if ($key != PT_WORKORDER)
1960                         $items[$key] = $type;
1961         }               
1962         return array_selector($name, $selected_id, $items, 
1963                 array( 'select_submit'=> $submit_on_change ) );
1964 }
1965
1966 function payment_person_types_list_cells($label, $name, $selected_id=null, $related=null)
1967 {
1968         if ($label != null)
1969                 echo "<td>$label</td>\n";
1970         echo "<td>";
1971         echo payment_person_types_list($name, $selected_id, $related);
1972         echo "</td>\n";
1973 }
1974
1975 function payment_person_types_list_row($label, $name, $selected_id=null, $related=null)
1976 {
1977         echo "<tr><td class='label'>$label</td>";
1978         payment_person_types_list_cells(null, $name, $selected_id, $related);
1979         echo "</tr>\n";
1980 }
1981
1982 //------------------------------------------------------------------------------------------------
1983
1984 function wo_types_list($name, $selected_id=null)
1985 {
1986         global $wo_types_array;
1987         
1988         return array_selector($name, $selected_id, $wo_types_array, 
1989                 array( 'select_submit'=> true, 'async' => true ) );
1990 }
1991
1992 function wo_types_list_row($label, $name, $selected_id=null)
1993 {
1994         echo "<tr><td class='label'>$label</td><td>\n";
1995         echo wo_types_list($name, $selected_id);
1996         echo "</td></tr>\n";
1997 }
1998
1999 //------------------------------------------------------------------------------------------------
2000
2001 function dateformats_list_row($label, $name, $value=null)
2002 {
2003         global $SysPrefs;
2004
2005         echo "<tr><td class='label'>$label</td>\n<td>";
2006         echo array_selector( $name, $value, $SysPrefs->dateformats );
2007         echo "</td></tr>\n";
2008 }
2009
2010 function dateseps_list_row($label, $name, $value=null)
2011 {
2012         global $SysPrefs;
2013
2014         echo "<tr><td class='label'>$label</td>\n<td>";
2015         echo array_selector( $name, $value, $SysPrefs->dateseps );
2016         echo "</td></tr>\n";
2017 }
2018
2019 function thoseps_list_row($label, $name, $value=null)
2020 {
2021         global $SysPrefs;
2022
2023         echo "<tr><td class='label'>$label</td>\n<td>";
2024         echo array_selector( $name, $value, $SysPrefs->thoseps );
2025         echo "</td></tr>\n";
2026 }
2027
2028 function decseps_list_row($label, $name, $value=null)
2029 {
2030         global $SysPrefs;
2031
2032         echo "<tr><td class='label'>$label</td>\n<td>";
2033         echo array_selector( $name, $value, $SysPrefs->decseps );
2034         echo "</td></tr>\n";
2035 }
2036
2037 function themes_list_row($label, $name, $value=null)
2038 {
2039         global $path_to_root;
2040
2041         $path = $path_to_root.'/themes/';
2042         $themes = array();
2043         $themedir = opendir($path);
2044         while(false !== ($fname = readdir($themedir)))
2045         {
2046                 if($fname!='.' && $fname!='..' && $fname!='CVS' && is_dir($path.$fname))
2047                 {
2048                         $themes[$fname] =  $fname;
2049                 }
2050         }
2051         ksort($themes);
2052
2053         echo "<tr><td class='label'>$label</td>\n<td>";
2054         echo array_selector( $name, $value, $themes );
2055         echo "</td></tr>\n";
2056 }
2057
2058 function pagesizes_list_row($label, $name, $value=null)
2059 {
2060         global $SysPrefs;
2061
2062         $items = array();
2063         foreach ($SysPrefs->pagesizes as $pz)
2064                 $items[$pz] = $pz;
2065
2066         echo "<tr><td class='label'>$label</td>\n<td>";
2067         echo array_selector( $name, $value, $items );
2068         echo "</td></tr>\n";
2069 }
2070
2071 function systypes_list($name, $value=null, $spec_opt=false, $submit_on_change=false, $exclude=array())
2072 {
2073         global $systypes_array;
2074
2075         // emove non-voidable transactions if needed
2076         $systypes = array_diff_key($systypes_array, array_flip($exclude));
2077         return array_selector($name, $value, $systypes, 
2078                 array( 
2079                         'spec_option'=> $spec_opt,
2080                         'spec_id' => ALL_NUMERIC,
2081                         'select_submit'=> $submit_on_change,
2082                         'async' => false,
2083                         )
2084         );
2085 }
2086
2087 function systypes_list_cells($label, $name, $value=null, $submit_on_change=false, $exclude=array())
2088 {
2089         if ($label != null)
2090                 echo "<td>$label</td>\n";
2091         echo "<td>";
2092         echo systypes_list($name, $value, false, $submit_on_change, $exclude);
2093         echo "</td>\n";
2094 }
2095
2096 function systypes_list_row($label, $name, $value=null, $submit_on_change=false, $exclude=array())
2097 {
2098         echo "<tr><td class='label'>$label</td>";
2099         systypes_list_cells(null, $name, $value, $submit_on_change, $exclude);
2100         echo "</tr>\n";
2101 }
2102
2103 function journal_types_list_cells($label, $name, $value=null, $submit_on_change=false)
2104 {
2105         global $systypes_array;
2106
2107         if ($label != null)
2108                 echo "<td>$label</td>\n";
2109         echo "<td>";
2110
2111         $items = $systypes_array;
2112
2113         // exclude quotes, orders and dimensions
2114         foreach (array(ST_PURCHORDER, ST_SALESORDER, ST_DIMENSION, ST_SALESQUOTE, ST_LOCTRANSFER) as $excl)
2115                         unset($items[$excl]);
2116         
2117         echo array_selector($name, $value, $items, 
2118                 array( 
2119                         'spec_option'=> _("All"),
2120                         'spec_id' => -1,
2121                         'select_submit'=> $submit_on_change,
2122                         'async' => false
2123                         )
2124         );
2125         echo "</td>\n";
2126 }
2127
2128 function cust_allocations_list_cells($label, $name, $selected=null, $submit_on_change=false)
2129 {
2130
2131         if ($label != null)
2132                 label_cell($label);
2133         echo "<td>\n";
2134         $allocs = array( 
2135                 ALL_TEXT=>_("All Types"),
2136                 '1'=> _("Sales Invoices"),
2137                 '2'=> _("Unsettled transactions"),
2138                 '3' => _("Payments"),
2139                 '4' => _("Credit Notes"),
2140                 '5' => _("Delivery Notes")
2141         );
2142         echo array_selector($name, $selected, $allocs, array('select_submit'=> $submit_on_change));
2143         echo "</td>\n";
2144 }
2145
2146 function supp_allocations_list_cell($name, $selected=null)
2147 {
2148
2149         echo "<td>\n";
2150         $allocs = array( 
2151                 ALL_TEXT=>_("All Types"),
2152                 '1'=> _("Invoices"),
2153                 '2'=> _("Overdue Invoices"),
2154                 '3' => _("Payments"),
2155                 '4' => _("Credit Notes"),
2156                 '5' => _("Overdue Credit Notes")
2157         );
2158         echo array_selector($name, $selected, $allocs);
2159         echo "</td>\n";
2160 }
2161
2162 function supp_transactions_list_cell($name, $selected=null, $submit_on_change=false)
2163 {
2164
2165         echo "<td>\n";
2166         $allocs = array( 
2167                 ALL_TEXT=>_("All Types"),
2168                 '6'=>_("GRNs"),
2169                 '1'=> _("Invoices"),
2170                 '2'=> _("Unsettled transactions"),
2171                 '3' => _("Payments"),
2172                 '4' => _("Credit Notes"),
2173                 '5' => _("Overdue Credit Notes")
2174         );
2175
2176         echo array_selector($name, $selected, $allocs, array('select_submit'=> $submit_on_change));
2177         echo "</td>\n";
2178 }
2179
2180 function policy_list_cells($label, $name, $selected=null)
2181 {
2182         if ($label != null)
2183                 label_cell($label);
2184         echo "<td>\n";
2185         echo array_selector($name, $selected, 
2186                                 array( '' => _("Automatically put balance on back order"),
2187                                         'CAN' => _("Cancel any quantites not delivered")) );
2188         echo "</td>\n";
2189 }
2190
2191 function policy_list_row($label, $name, $selected=null)
2192 {
2193         echo "<tr><td class='label'>$label</td>";
2194         policy_list_cells(null, $name, $selected);
2195         echo "</tr>\n";
2196 }
2197
2198 function credit_type_list_cells($label, $name, $selected=null, $submit_on_change=false)
2199 {
2200         if ($label != null)
2201                 label_cell($label);
2202         echo "<td>\n";
2203         echo array_selector($name, $selected, 
2204                         array( 'Return' => _("Items Returned to Inventory Location"),
2205                                 'WriteOff' => _("Items Written Off")),
2206                         array( 'select_submit'=> $submit_on_change ) );
2207         echo "</td>\n";
2208 }
2209
2210 function credit_type_list_row($label, $name, $selected=null, $submit_on_change=false)
2211 {
2212         echo "<tr><td class='label'>$label</td>";
2213         credit_type_list_cells(null, $name, $selected, $submit_on_change);
2214         echo "</tr>\n";
2215 }
2216
2217 function number_list($name, $selected, $from, $to, $no_option=false)
2218 {
2219         $items = array();
2220         for ($i = $from; $i <= $to; $i++)
2221                 $items[$i] = "$i";
2222
2223         return array_selector($name, $selected, $items,
2224                         array(  'spec_option' => $no_option,
2225                                         'spec_id' => ALL_NUMERIC) );
2226 }
2227
2228 function number_list_cells($label, $name, $selected, $from, $to, $no_option=false)
2229 {
2230         if ($label != null)
2231                 label_cell($label);
2232         echo "<td>\n";
2233         echo number_list($name, $selected, $from, $to, $no_option);
2234         echo "</td>\n";
2235 }
2236
2237 function number_list_row($label, $name, $selected, $from, $to, $no_option=false)
2238 {
2239         echo "<tr><td class='label'>$label</td>";
2240         echo number_list_cells(null, $name, $selected, $from, $to, $no_option);
2241         echo "</tr>\n";
2242 }
2243
2244 function print_profiles_list_row($label, $name, $selected_id=null, $spec_opt=false,
2245         $submit_on_change=true)
2246 {
2247         $sql = "SELECT profile FROM ".TB_PREF."print_profiles"
2248                 ." GROUP BY profile";
2249         $result = db_query($sql, 'cannot get all profile names');
2250         $profiles = array();
2251         while($myrow=db_fetch($result)) {
2252                 $profiles[$myrow['profile']] = $myrow['profile'];
2253         }
2254
2255         echo "<tr>";
2256         if ($label != null)
2257                 echo "<td class='label'>$label</td>\n";
2258         echo "<td>";
2259
2260         echo array_selector($name, $selected_id, $profiles, 
2261                 array( 'select_submit'=> $submit_on_change,
2262                         'spec_option'=>$spec_opt,
2263                         'spec_id' => ''
2264                  ));
2265
2266         echo "</td></tr>\n";
2267 }
2268
2269 function printers_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
2270 {
2271         static $printers; // query only once for page display
2272
2273         if (!$printers) {
2274                 $sql = "SELECT id, name, description FROM ".TB_PREF."printers"; 
2275                 $result = db_query($sql, 'cannot get all printers');
2276                 $printers = array();
2277                 while($myrow=db_fetch($result)) {
2278                         $printers[$myrow['id']] = $myrow['name'].'&nbsp;-&nbsp;'.$myrow['description'];
2279                 }
2280         }
2281         return array_selector($name, $selected_id, $printers, 
2282                 array( 'select_submit'=> $submit_on_change,
2283                         'spec_option'=>$spec_opt,
2284                         'spec_id' => ''
2285                  ));
2286 }
2287
2288 //------------------------------------------------------------------------------------------------
2289
2290 function quick_entries_list($name, $selected_id=null, $type=null, $submit_on_change=false)
2291 {
2292         $where = false;
2293         $sql = "SELECT id, description FROM ".TB_PREF."quick_entries";
2294         if ($type != null)
2295                 $sql .= " WHERE type=$type";
2296
2297         return combo_input($name, $selected_id, $sql, 'id', 'description',
2298                 array(
2299                         'spec_id' => '',
2300                         'order' => 'description',
2301                         'select_submit'=> $submit_on_change,
2302                         'async' => false
2303                 ) );
2304
2305 }
2306
2307 function quick_entries_list_cells($label, $name, $selected_id=null, $type, $submit_on_change=false)
2308 {
2309         if ($label != null)
2310                 echo "<td>$label</td>\n";
2311         echo "<td>";    
2312         echo quick_entries_list($name, $selected_id, $type, $submit_on_change);
2313         echo "</td>";
2314 }
2315
2316 function quick_entries_list_row($label, $name, $selected_id=null, $type, $submit_on_change=false)
2317 {
2318         echo "<tr><td class='label'>$label</td>";
2319         quick_entries_list_cells(null, $name, $selected_id, $type, $submit_on_change);
2320         echo "</tr>\n";
2321 }
2322
2323
2324 function quick_actions_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2325 {
2326         global $quick_actions;
2327         
2328         echo "<tr><td class='label'>$label</td><td>";
2329         echo array_selector($name, $selected_id, $quick_actions, 
2330                 array( 
2331                         'select_submit'=> $submit_on_change
2332                 ) );
2333         echo "</td></tr>\n";
2334 }
2335
2336 function quick_entry_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2337 {
2338         global $quick_entry_types;
2339                 
2340         echo "<tr><td class='label'>$label</td><td>";
2341         echo array_selector($name, $selected_id, $quick_entry_types, 
2342                 array( 
2343                         'select_submit'=> $submit_on_change
2344                         ) );
2345         echo "</td></tr>\n";
2346 }
2347
2348 function record_status_list_row($label, $name) {
2349         return yesno_list_row($label, $name, null,      _('Inactive'), _('Active'));
2350 }
2351
2352 function class_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2353 {
2354         global $class_types;
2355         
2356         echo "<tr><td class='label'>$label</td><td>";
2357         echo array_selector($name, $selected_id, $class_types, 
2358                 array( 
2359                         'select_submit'=> $submit_on_change
2360                 ) );
2361         echo "</td></tr>\n";
2362 }
2363
2364 //------------------------------------------------------------------------------------------------
2365
2366 function security_roles_list($name, $selected_id=null, $new_item=false, $submit_on_change=false,
2367         $show_inactive = false)
2368 {
2369
2370         $sql = "SELECT id, role, inactive FROM ".TB_PREF."security_roles";
2371
2372         return combo_input($name, $selected_id, $sql, 'id', 'description',
2373                 array(
2374                         'spec_option'=>$new_item ? _("New role") : false,
2375                         'spec_id' => '',
2376                         'select_submit'=> $submit_on_change,
2377                         'show_inactive' => $show_inactive
2378                 ) );
2379 }
2380
2381 function security_roles_list_cells($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2382         $show_inactive = false)
2383 {
2384         if ($label != null)
2385                 echo "<td>$label</td>\n";
2386         echo "<td>";
2387         echo security_roles_list($name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2388         echo "</td>\n";
2389 }
2390
2391 function security_roles_list_row($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2392         $show_inactive = false)
2393 {
2394         echo "<tr><td class='label'>$label</td>";
2395         security_roles_list_cells(null, $name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2396         echo "</tr>\n";
2397 }
2398
2399 function tab_list_row($label, $name, $selected_id=null)
2400 {
2401         global $installed_extensions;
2402         
2403         $tabs = array();
2404         foreach ($_SESSION['App']->applications as $app) {
2405                 $tabs[$app->id] = access_string($app->name, true);
2406         }
2407         echo "<tr>\n";
2408         echo "<td class='label'>$label</td><td>\n";
2409         echo array_selector($name, $selected_id, $tabs);
2410         echo "</td></tr>\n";
2411 }
2412
2413 //-----------------------------------------------------------------------------------------------
2414
2415 function tag_list($name, $height, $type, $multi=false, $all=false, $spec_opt = false)
2416 {
2417         // Get tags
2418         global $path_to_root;
2419         include_once($path_to_root . "/admin/db/tags_db.inc");
2420         $results = get_tags($type, $all);
2421
2422         while ($tag = db_fetch($results))
2423                 $tags[$tag['id']] = $tag['name'];
2424         
2425         if (!isset($tags)) {
2426                 $tags[''] = $all ? _("No tags defined.") : _("No active tags defined.");
2427                 $spec_opt = false;
2428         }
2429         return array_selector($name, null, $tags,
2430                 array(
2431                         'multi' => $multi,
2432                         'height' => $height,
2433                         'spec_option'=> $spec_opt,
2434                         'spec_id' => -1,
2435                 ) );
2436 }
2437
2438 function tag_list_cells($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2439 {
2440         if ($label != null)
2441                 echo "<td>$label</td>\n";
2442         echo "<td>\n";
2443         echo tag_list($name, $height, $type, $mult, $all, $spec_opt);
2444         echo "</td>\n";
2445         
2446 }
2447
2448 function tag_list_row($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2449 {
2450         echo "<tr><td class='label'>$label</td>";
2451         tag_list_cells(null, $name, $height, $type, $mult, $all, $spec_opt);
2452         echo "</tr>\n"; 
2453 }
2454
2455 //---------------------------------------------------------------------------------------------
2456 //      List of sets of active extensions 
2457 //
2458 function extset_list($name, $value=null, $submit_on_change=false)
2459 {
2460         global $db_connections;
2461
2462         $items = array();
2463         foreach ($db_connections as $comp)
2464                 $items[] = sprintf(_("Activated for '%s'"), $comp['name']);
2465         return array_selector( $name, $value, $items,
2466                 array(
2467                         'spec_option'=> _("Available and/or installed"),
2468                         'spec_id' => -1,
2469                         'select_submit'=> $submit_on_change,
2470                         'async' => true
2471                 ));
2472 }
2473
2474 function crm_category_types_list($name, $selected_id=null, $filter=array(), $submit_on_change=true)
2475 {
2476
2477         $sql = "SELECT id, name, type, inactive FROM ".TB_PREF."crm_categories";
2478
2479         $multi = false;
2480         $groups = false;
2481         $where = array();
2482         if (@$filter['class']) {
2483                 $where[] = 'type='.db_escape($filter['class']);
2484         } else
2485                 $groups = 'type';
2486         if (@$filter['subclass']) $where[] = 'action='.db_escape($filter['subclass']);
2487         if (@$filter['entity']) $where[] = 'entity_id='.db_escape($filter['entity']);
2488         if (@$filter['multi']) { // contact category selector for person
2489                 $multi = true;
2490         }
2491
2492         return combo_input($name, $selected_id, $sql, 'id', 'name',
2493                 array(
2494                         'multi' => $multi,
2495                         'height' => $multi ? 5:1,
2496                         'category' => $groups,
2497                         'select_submit'=> $submit_on_change,
2498                         'async' => true,
2499                         'where' => $where
2500                 ));
2501 }
2502
2503 function crm_category_types_list_row($label, $name, $selected_id=null, $filter=array(), $submit_on_change=true)
2504 {
2505         echo "<tr><td class='label'>$label</td><td>";
2506         echo crm_category_types_list($name, $selected_id, $filter, $submit_on_change);
2507         echo "</td></tr>\n";
2508 }
2509
2510 function payment_type_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2511 {
2512         global $pterm_types;
2513         
2514         echo "<tr><td class='label'>$label</td><td>";
2515         echo array_selector($name, $selected_id, $pterm_types, 
2516                 array( 
2517                         'select_submit'=> $submit_on_change
2518                 ) );
2519         echo "</td></tr>\n";
2520 }
2521
2522 function coa_list_row($label, $name, $value=null)
2523 {
2524         global $path_to_root, $installed_extensions;
2525
2526         $path = $path_to_root.'/sql/';
2527         $coas = array();
2528         $sqldir = opendir($path);
2529         while (false !== ($fname = readdir($sqldir)))
2530         {
2531                 if (is_file($path.$fname) && substr($fname,-4)=='.sql' && @($fname[2] == '_'))
2532                 {
2533                         $ext = array_search_value($fname, $installed_extensions, 'sql');
2534                         if ($ext!=null) {
2535                                 $descr = $ext['name'];
2536                         } elseif ($fname == 'en_US-new.sql') { // two standard COAs
2537                                 $descr = _("Standard new company American COA (4 digit)");
2538                         } elseif ($fname == 'en_US-demo.sql') {
2539                                 $descr = _("Standard American COA (4 digit) with demo data");
2540                         } else
2541                                 $descr = $fname;
2542
2543                         $coas[$fname] =  $descr;
2544                 }
2545         }
2546         ksort($coas);
2547
2548         echo "<tr><td class='label'>$label</td>\n<td>";
2549         echo array_selector( $name, $value, $coas );
2550         echo "</td></tr>\n";
2551 }
2552
2553 function payment_services($name)
2554 {
2555         global $payment_services;
2556
2557         $services = (object)array_combine(array_keys($payment_services), array_keys($payment_services));
2558
2559         return array_selector($name, null, $services, array(
2560                         'spec_option'=> _("No payment Link"),
2561                         'spec_id' => '',
2562                 ));
2563 }
2564
2565 function tax_algorithm_list($name, $value=null, $submit_on_change = false)
2566 {
2567         global $tax_algorithms;
2568         
2569         return array_selector($name, $value, $tax_algorithms, 
2570                 array( 
2571                         'select_submit'=> $submit_on_change,
2572                         'async' => true,
2573                         )
2574         );
2575 }
2576
2577 function tax_algorithm_list_cells($label, $name, $value=null, $submit_on_change=false)
2578 {
2579         if ($label != null)
2580                 echo "<td>$label</td>\n";
2581         echo "<td>";
2582         echo tax_algorithm_list($name, $value, $submit_on_change);
2583         echo "</td>\n";
2584 }
2585
2586 function tax_algorithm_list_row($label, $name, $value=null, $submit_on_change=false)
2587 {
2588         echo "<tr><td class='label'>$label</td>";
2589         tax_algorithm_list_cells(null, $name, $value, $submit_on_change);
2590         echo "</tr>\n";
2591 }
2592
2593 function refline_list($name, $type, $value=null, $spec_option=false)
2594 {
2595         $sql = "SELECT id, prefix, inactive FROM ".TB_PREF."reflines";
2596
2597         $where = array();
2598
2599         if (isset($type))
2600                 $where = array('`trans_type`='.db_escape($type));
2601
2602         return combo_input($name, $value, $sql, 'id', 'prefix',
2603                 array(
2604                         'order'=>array('prefix'),
2605                         'spec_option' => $spec_option,
2606                         'spec_id' => '',
2607                         'type' => 2,
2608                         'where' => $where,
2609                         'select_submit' => true,
2610                         )
2611                 );
2612 }
2613
2614 function refline_list_row($label, $name, $type, $selected_id=null, $spec_option=false)
2615 {
2616         echo "<tr>";
2617         if ($label != null)
2618                 echo "<td class='label'>$label</td>\n";
2619         echo "<td>";
2620
2621         echo refline_list($name, $type, $selected_id, $spec_option);
2622         echo "</td></tr>\n";
2623 }
2624
2625
2626 //----------------------------------------------------------------------------------------------
2627
2628 function subledger_list($name, $account, $selected_id=null)
2629 {
2630
2631         $type = is_subledger_account($account);
2632         if (!$type)
2633                 return '';
2634
2635         if($type > 0)
2636                 $sql = "SELECT DISTINCT d.debtor_no as id, debtor_ref as name 
2637                 FROM "
2638                 .TB_PREF."debtors_master d,"
2639                 .TB_PREF."cust_branch c
2640                 WHERE d.debtor_no=c.debtor_no AND c.receivables_account=".db_escape($account);
2641         else
2642                 $sql = "SELECT supplier_id as id, supp_ref as name 
2643                 FROM "
2644                 .TB_PREF."suppliers s
2645                 WHERE s.payable_account=".db_escape($account);
2646
2647         $mode = get_company_pref('no_customer_list');
2648
2649         return combo_input($name, $selected_id, $sql, 'id', 'name',
2650         array(
2651                 'type' => 1,
2652                 'size' => 20,
2653                 'async' => false,
2654         ) );
2655 }
2656
2657 function subledger_list_cells($label, $name, $account, $selected_id=null)
2658 {
2659         if ($label != null)
2660                 echo "<td>$label</td>\n";
2661         echo "<td nowrap>";
2662         echo subledger_list($name, $account, $selected_id);
2663         echo "</td>\n";
2664 }
2665
2666 function subledger_list_row($label, $name, $selected_id=null, $all_option = false, 
2667         $submit_on_change=false, $show_inactive=false, $editkey = false)
2668 {
2669         echo "<tr><td class='label'>$label</td><td nowrap>";
2670         echo subledger_list($name, $account, $selected_id);
2671         echo "</td>\n</tr>\n";
2672 }
2673
2674 function accounts_type_list_row($label, $name, $selected_id=null)
2675 {
2676         echo "<tr>";
2677         if ($label != null)
2678                 echo "<td class='label'>$label</td>\n";
2679         echo "<td>";
2680         $sel = array(_("Numeric"), _("Alpha Numeric"), _("ALPHA NUMERIC"));
2681         echo array_selector($name, $selected_id, $sel); 
2682         echo "</td></tr>\n";
2683 }
2684
2685 function users_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $spec_opt=true)
2686 {
2687     $where = false;
2688     $sql = " SELECT user_id, real_name FROM ".TB_PREF."users";
2689
2690     if ($label != null)
2691         echo "<td>$label</td>\n";
2692     echo "<td>";
2693
2694     echo combo_input($name, $selected_id, $sql, 'user_id', 'real_name',
2695         array(
2696             'spec_option' => $spec_opt===true ?_("All users") : $spec_opt,
2697             'spec_id' => '',
2698             'order' => 'real_name',
2699             'select_submit'=> $submit_on_change,
2700             'async' => false
2701         ) );
2702     echo "</td>";
2703
2704 }
2705
2706 function collations_list_row($label, $name, $selected_id=null)
2707 {
2708         global $supported_collations;
2709
2710         echo "<tr>";
2711         if ($label != null)
2712                 echo "<td class='label'>$label</td>\n";
2713         echo "<td>";
2714
2715         echo array_selector($name, $selected_id, $supported_collations, 
2716                 array('select_submit'=> false) );
2717         echo "</td></tr>\n";
2718 }