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