b803ac3215b40c4f9231d2ab5742c5e96983b142
[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 nowrap>$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, $submit_on_change=false)
765 {
766         if ($label != null)
767                 echo "<td>$label</td>\n";
768         echo "<td>";
769         echo fiscalyears_list($name, $selected_id, $submit_on_change);
770         echo "</td>\n";
771 }
772
773 function fiscalyears_list_row($label, $name, $selected_id=null, $submit_on_change=false)
774 {
775         echo "<tr><td class='label'>$label</td>";
776         fiscalyears_list_cells(null, $name, $selected_id, $submit_on_change);
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, shipper.shipper_name, s.inactive, s.editable
827                         FROM ".TB_PREF."stock_master s 
828                                 LEFT JOIN ".TB_PREF."shippers shipper ON shipper.shipper_id=s.shipper_id
829                                 LEFT JOIN ".TB_PREF."stock_category c ON s.category_id=c.category_id";
830
831         if (isset($opts['shipping']) && $opts['shipping'])
832                 $where[] = "mb_flag='T'";
833         elseif (isset($opts['fixed_asset']) && $opts['fixed_asset'])
834                 $where[] = "mb_flag='F'";
835         else
836                 $where[] = "NOT mb_flag IN('F','T')";
837
838         if ($editkey)
839                 set_editor('item', $name, $editkey);
840
841         $ret = combo_input($name, $selected_id, $sql, 'stock_id', 's.description',
842                 array_merge(
843                   array(
844                         'format' => '_format_stock_items',
845                         'spec_option' => $all_option===true ?  _("All Items") : $all_option,
846                         'spec_id' => ALL_TEXT,
847                         'search_box' => true,
848                         'search' => array("stock_id", "c.description","s.description"),
849                         'search_submit' => get_company_pref('no_item_list') != 0 && (!isset($opts['fixed_asset']) || !$opts['fixed_asset']),
850                         'size'=>10,
851                         'select_submit'=> $submit_on_change,
852                         'category' => 2,
853                         'order' => (isset($opts['shipping']) && $opts['shipping']) ? array('shipper.shipper_name','stock_id'): array('c.description','stock_id'),
854                         'editlink' => $editkey ? add_edit_combo('item') : false,
855                         'editable' => false,
856                         'max' => 255,
857                         'where' => $where
858                   ), $opts), $type );
859         return $ret;
860 }
861
862 function _format_stock_items($row)
863 {
864         return (user_show_codes() ?  ($row[0] . "&nbsp;-&nbsp;") : "") . $row[1];
865 }
866
867 function stock_items_list_cells($label, $name, $selected_id=null, $all_option=false, 
868         $submit_on_change=false, $all=false, $editkey = false, $opts= array())
869 {
870         if (isset($opts['fixed_asset']) && $opts['fixed_asset'])
871                 $editor_item = 'fa_item';
872         else
873                 $editor_item = 'item';
874
875 //      if ($editkey) ??
876 //              set_editor($editor_item, $name, $editkey);
877
878         if ($label != null)
879                 echo "<td>$label</td>\n";
880
881 // ??
882 //  $opts = array_merge($options, array('cells'=>true, 'show_inactive'=>$all, 'new_icon' => $editkey ? 'item' : false));
883 //
884 //      echo stock_items_list($name, $selected_id, $all_option, $submit_on_change, $opts);
885
886         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
887                 array_merge(array('cells'=>true, 'show_inactive'=>$all), $opts), $editkey);
888
889 }
890 /*
891 function stock_items_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
892 {
893         echo "<tr>\n";
894         stock_items_list_cells($label, $name, $selected_id, $all_option, $submit_on_change);
895         echo "</tr>\n";
896 }
897 */
898 //---------------------------------------------------------------------------------------------------
899 //
900 // Select item via foreign code.
901 //
902 function sales_items_list($name, $selected_id=null, $all_option=false, 
903         $submit_on_change=false, $type='', $opts=array())
904 {
905         // all sales codes
906         $sql = "SELECT i.item_code, i.description, c.description, count(*)>1 as kit,
907                          i.inactive, if(count(*)>1, '0', s.editable) as editable
908                         FROM
909                         ".TB_PREF."stock_master s,
910                         ".TB_PREF."item_codes i
911                         LEFT JOIN
912                         ".TB_PREF."stock_category c
913                         ON i.category_id=c.category_id
914                         WHERE i.stock_id=s.stock_id
915       AND NOT mb_flag IN('F','T')";
916
917         
918         if ($type == 'local')   { // exclude foreign codes
919                 $sql .= " AND !i.is_foreign"; 
920         } elseif ($type == 'kits') { // sales kits
921                 $sql .= " AND !i.is_foreign AND i.item_code!=i.stock_id";
922         }
923         $sql .= " AND !i.inactive AND !s.inactive AND !s.no_sale";
924         $sql .= " GROUP BY i.item_code";
925
926         return combo_input($name, $selected_id, $sql, 'i.item_code', 'c.description',
927                 array_merge(
928                   array(
929                         'format' => '_format_stock_items',
930                         'spec_option' => $all_option===true ?  _("All Items") : $all_option,
931                         'spec_id' => ALL_TEXT,
932                         'search_box' => true,
933                         'search' => array("i.item_code", "c.description", "i.description"),
934                         'search_submit' => get_company_pref('no_item_list')!=0,
935                         'size'=>15,
936                         'select_submit'=> $submit_on_change,
937                         'category' => 2,
938                         'order' => array('c.description','i.item_code'),
939                         'editable' => 30,
940                         'max' => 255
941                   ), $opts), $type == 'kits' ? $type : "stock_sales" );
942 }
943
944 function sales_items_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false, $editkey=false)
945 {
946         if ($editkey)
947                 set_editor('item', $name, $editkey);
948
949         if ($label != null)
950                 echo "<td>$label</td>\n";
951         echo sales_items_list($name, $selected_id, $all_option, $submit_on_change,
952                 '', array('cells'=>true, 'max'=>50)); // maximum is set to 50 and not default 255.
953 }
954
955 function sales_kits_list($name, $selected_id=null, $all_option=false, $submit_on_change=false)
956 {
957         return sales_items_list($name, $selected_id, $all_option, $submit_on_change,
958                 'kits', array('cells'=>false, 'editable' => false));
959 }
960
961 function sales_local_items_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
962 {
963         echo "<tr>";
964         if ($label != null)
965                 echo "<td class='label'>$label</td>\n";
966         echo "<td>";
967         echo sales_items_list($name, $selected_id, $all_option, $submit_on_change,
968                 'local', array('cells'=>false, 'editable' => false));
969         echo "</td></tr>";
970 }
971 //------------------------------------------------------------------------------------
972
973 function stock_manufactured_items_list($name, $selected_id=null,
974         $all_option=false, $submit_on_change=false)
975 {
976         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
977                 array('where'=>array("mb_flag= 'M'")), false, "stock_manufactured");
978 }
979
980 function stock_manufactured_items_list_cells($label, $name, $selected_id=null,
981                                 $all_option=false, $submit_on_change=false)
982 {
983         if ($label != null)
984                 echo "<td>$label</td>\n";
985         echo "<td>";
986         echo stock_manufactured_items_list($name, $selected_id, $all_option, $submit_on_change);
987         echo "</td>\n";
988 }
989
990 function stock_manufactured_items_list_row($label, $name, $selected_id=null,
991                 $all_option=false, $submit_on_change=false)
992 {
993         echo "<tr><td class='label'>$label</td>";
994         stock_manufactured_items_list_cells(null, $name, $selected_id, $all_option, $submit_on_change);
995         echo "</tr>\n";
996 }
997 //------------------------------------------------------------------------------------
998
999 function stock_component_items_list($name, $parent_stock_id, $selected_id=null,
1000         $all_option=false, $submit_on_change=false, $editkey = false)
1001 {
1002         $parent = db_escape($parent_stock_id);
1003         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1004                 array('where'=>array("stock_id != $parent"), 'parent'=> $parent_stock_id), $editkey, "component");
1005 }
1006
1007
1008 function stock_component_items_list_cells($label, $name, $parent_stock_id, 
1009         $selected_id=null, $all_option=false, $submit_on_change=false, $editkey = false)
1010 {
1011         if ($label != null)
1012                 echo "<td>$label</td>\n";
1013         $parent = db_escape($parent_stock_id);
1014         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1015                 array('where'=>array("stock_id != '$parent_stock_id'"), 'cells'=>true, 'parent'=> $parent_stock_id), $editkey, "component");
1016 }
1017 //------------------------------------------------------------------------------------
1018
1019 function stock_costable_items_list($name, $selected_id=null,
1020         $all_option=false, $submit_on_change=false)
1021 {
1022         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1023                 array('where'=>array("mb_flag!='D'")), false, "stock_costable");
1024 }
1025
1026 function stock_costable_items_list_cells($label, $name, $selected_id=null, 
1027         $all_option=false, $submit_on_change=false)
1028 {
1029         if ($label != null)
1030                 echo "<td>$label</td>\n";
1031         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1032                 array('where'=>array("mb_flag!='D'"), 'cells'=>true), false, "stock_costable");
1033 }
1034
1035 //------------------------------------------------------------------------------------
1036 function stock_purchasable_items_list($name, $selected_id=null, 
1037         $all_option=false, $submit_on_change=false, $all=false, $editkey=false)
1038 {
1039         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1040                 array('where'=>array("NOT no_purchase", "mb_flag!='F'"), 
1041                         'show_inactive'=>$all), $editkey, "stock_purchased");
1042 }
1043 //
1044 //      This helper is used in PO/GRN/PI entry and supports editable descriptions.
1045 //
1046 function stock_purchasable_items_list_cells($label, $name, $selected_id=null,
1047                         $all_option=false, $submit_on_change=false, $editkey=false)
1048 {
1049         if ($label != null)
1050                 echo "<td>$label</td>\n";
1051         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1052                 array('where'=>array("NOT no_purchase"), 
1053                          'editable' => 30,
1054                          'cells'=>true), $editkey);
1055 }
1056
1057 //------------------------------------------------------------------------------------
1058
1059 function shipping_methods_list($name, $selected_id=null, $all_option=false, $submit_on_change=false)
1060 {
1061         return stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1062                 array('type'=>1, 'shipping'=>true, 'category' => false, 'search_box'=>false, 'search_sumbit' => false),
1063                         false, false);//, "shipping_methods");
1064 }
1065
1066 function shipping_methods_list_cells($label, $name, $selected_id=null,
1067                                 $all_option=false, $submit_on_change=false)
1068 {
1069         if ($label != null)
1070                 echo "<td>$label</td>\n";
1071         echo "<td>";
1072         echo shipping_methods_list($name, $selected_id, $all_option, $submit_on_change);
1073         echo "</td>\n";
1074 }
1075
1076 function shipping_methods_list_row($label, $name, $selected_id=null,
1077                 $all_option=false, $submit_on_change=false)
1078 {
1079         echo "<tr><td class='label'>$label</td>";
1080         shipping_methods_list_cells(null, $name, $selected_id, $all_option, $submit_on_change);
1081         echo "</tr>\n";
1082 }
1083
1084 //------------------------------------------------------------------------------------
1085
1086 function stock_item_types_list_row($label, $name, $selected_id=null, $enabled=true, $all=true)
1087 {
1088         global $stock_types;
1089
1090         $options = $stock_types;
1091         if (!$all)
1092         {
1093                 unset($options['T']);
1094                 unset($options['F']);
1095         }
1096         echo "<tr>";
1097         if ($label != null)
1098                 echo "<td class='label'>$label</td>\n";
1099         echo "<td>";
1100                 
1101         echo array_selector($name, $selected_id, $options, 
1102                 array( 
1103                         'select_submit'=> true, 
1104                         'disabled' => !$enabled)
1105                 );
1106         echo "</td></tr>\n";
1107 }
1108
1109 function stock_units_list_row($label, $name, $value=null, $enabled=true)
1110 {
1111         $result = get_all_item_units();
1112         echo "<tr>";
1113         if ($label != null)
1114                 echo "<td class='label'>$label</td>\n";
1115         echo "<td>";
1116
1117         while($unit = db_fetch($result))
1118                 $units[$unit['abbr']] = $unit['name'];
1119
1120         echo array_selector($name, $value, $units, array( 'disabled' => !$enabled) );
1121
1122         echo "</td></tr>\n";
1123 }
1124
1125 //------------------------------------------------------------------------------------
1126
1127 function stock_purchasable_fa_list_cells($label, $name, $selected_id=null, $all_option=false,
1128         $submit_on_change=false, $all=false, $editkey = false, $exclude_items = array())
1129 {
1130         // Check if a fixed asset has been bought.
1131         $where_opts[] = "stock_id NOT IN
1132         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )";
1133
1134         // exclude items currently on the order.
1135         foreach($exclude_items as $item) {
1136         $where_opts[] = "stock_id != ".db_escape($item->stock_id);
1137         }
1138         $where_opts[] = "mb_flag='F'";
1139
1140         echo stock_items_list_cells($label, $name, $selected_id, $all_option, $submit_on_change, $all, $editkey, 
1141     array('fixed_asset' => true, 'where' => $where_opts));
1142 }
1143
1144 function stock_disposable_fa_list($name, $selected_id=null,
1145         $all_option=false, $submit_on_change=false)
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         $where_opts[] = "mb_flag='F'";
1155
1156         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1157     array('fixed_asset' => true, 'where' => $where_opts));
1158 }
1159
1160 function stock_disposable_fa_list_cells($label, $name, $selected_id=null,
1161         $all_option=false, $submit_on_change=false, $exclude_items = array())
1162 {
1163         // Check if a fixed asset has been bought....
1164         $where_opts[] = "stock_id IN
1165         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )";
1166         // ...but has not been disposed or sold already.
1167         $where_opts[] = "stock_id NOT IN
1168         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )";
1169
1170         $where_opts[] = "mb_flag='F'";
1171
1172         foreach($exclude_items as $item) {
1173         $where_opts[] = "stock_id != ".db_escape($item->stock_id);
1174         }
1175
1176         if ($label != null)
1177                 echo "<td>$label</td>\n";
1178         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change, 
1179     array('fixed_asset' => true, 'cells'=>true, 'where' => $where_opts));
1180 }
1181
1182 function stock_depreciable_fa_list_cells($label, $name, $selected_id=null,
1183         $all_option=false, $submit_on_change=false)
1184 {
1185
1186         // Check if a fixed asset has been bought....
1187         $where_opts[] = "stock_id IN
1188         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE type=".ST_SUPPRECEIVE." AND qty!=0 )";
1189         // ...but has not been disposed or sold already.
1190         $where_opts[] = "stock_id NOT IN
1191         ( SELECT stock_id FROM ".TB_PREF."stock_moves WHERE (type=".ST_CUSTDELIVERY." OR type=".ST_INVADJUST.") AND qty!=0 )";
1192
1193         $year = get_current_fiscalyear();
1194         $begin = date2sql(add_months(sql2date($year['begin']), -1));
1195         $end = date2sql(add_months(sql2date($year['end']), -1));
1196
1197         // check if current fiscal year
1198         $where_opts[] = "depreciation_date <= '".$end."'";
1199         $where_opts[] = "depreciation_date >= '".$begin."'";
1200
1201         $where_opts[] = "material_cost > 0";
1202         $where_opts[] = "mb_flag='F'";
1203
1204         if ($label != null)
1205                 echo "<td>$label</td>\n";
1206         echo stock_items_list($name, $selected_id, $all_option, $submit_on_change,
1207                  array('fixed_asset' => true, 'where' => $where_opts, 'cells'=>true));
1208 }
1209
1210 //------------------------------------------------------------------------------------
1211
1212 function tax_types_list($name, $selected_id=null, $none_option=false, $submit_on_change=false)
1213 {
1214         $sql = "SELECT id, CONCAT(name, ' (',rate,'%)') as name FROM ".TB_PREF."tax_types";
1215
1216         return combo_input($name, $selected_id, $sql, 'id', 'name',
1217                 array(
1218                         'spec_option' => $none_option,
1219                         'spec_id' => ALL_NUMERIC,
1220                         'select_submit'=> $submit_on_change,
1221                         'async' => false,
1222                 ) );
1223 }
1224
1225 function tax_types_list_cells($label, $name, $selected_id=null, $none_option=false,
1226         $submit_on_change=false)
1227 {
1228         if ($label != null)
1229                 echo "<td>$label</td>\n";
1230         echo "<td>";
1231         echo tax_types_list($name, $selected_id, $none_option, $submit_on_change);
1232         echo "</td>\n";
1233 }
1234
1235 function tax_types_list_row($label, $name, $selected_id=null, $none_option=false,
1236         $submit_on_change=false)
1237 {
1238         echo "<tr><td class='label'>$label</td>";
1239         tax_types_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1240         echo "</tr>\n";
1241 }
1242
1243 //------------------------------------------------------------------------------------
1244
1245 function tax_groups_list($name, $selected_id=null,
1246         $none_option=false, $submit_on_change=false)
1247 {
1248         $sql = "SELECT id, name, inactive FROM ".TB_PREF."tax_groups";
1249
1250         return combo_input($name, $selected_id, $sql, 'id', 'name',
1251                 array(
1252                         'order' => 'id',
1253                         'spec_option' => $none_option,
1254                         'spec_id' => ALL_NUMERIC,
1255                         'select_submit'=> $submit_on_change,
1256                         'async' => false,
1257                 ) );
1258 }
1259
1260 function tax_groups_list_cells($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1261 {
1262         if ($label != null)
1263                 echo "<td>$label</td>\n";
1264         echo "<td>";
1265         echo tax_groups_list($name, $selected_id, $none_option, $submit_on_change);
1266         echo "</td>\n";
1267 }
1268
1269 function tax_groups_list_row($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1270 {
1271         echo "<tr><td class='label'>$label</td>";
1272         tax_groups_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1273         echo "</tr>\n";
1274 }
1275
1276 //------------------------------------------------------------------------------------
1277
1278 function item_tax_types_list($name, $selected_id=null, $show_inactive = false)
1279 {
1280         $sql ="SELECT id, name FROM ".TB_PREF."item_tax_types";
1281         if (!$show_inactive)
1282                 $sql .= " WHERE !inactive";
1283
1284         return combo_input($name, $selected_id, $sql, 'id', 'name', array('order' => 'id') );
1285 }
1286
1287 function item_tax_types_list_cells($label, $name, $selected_id=null, $show_inactive = false)
1288 {
1289         if ($label != null)
1290                 echo "<td>$label</td>\n";
1291         echo "<td>";
1292         echo item_tax_types_list($name, $selected_id, $show_inactive);
1293         echo "</td>\n";
1294 }
1295
1296 function item_tax_types_list_row($label, $name, $selected_id=null, $show_inactive = false)
1297 {
1298         echo "<tr><td class='label'>$label</td>";
1299         item_tax_types_list_cells(null, $name, $selected_id, $show_inactive);
1300         echo "</tr>\n";
1301 }
1302
1303 //------------------------------------------------------------------------------------
1304
1305 function shippers_list($name, $selected_id=null, $none_option=false, $submit_on_change=false)
1306 {
1307         $sql = "SELECT shipper_id, shipper_name, inactive FROM ".TB_PREF."shippers";
1308         return combo_input($name, $selected_id, $sql, 'shipper_id', 'shipper_name', 
1309                 array('order'=>array('shipper_name'),
1310                         'spec_option' => $none_option,
1311                         'spec_id' => ALL_NUMERIC,
1312                         'select_submit'=> $submit_on_change,
1313                         'async' => false,
1314 ));
1315 }
1316
1317 function shippers_list_cells($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1318 {
1319         if ($label != null)
1320                 echo "<td>$label</td>\n";
1321         echo "<td>";
1322         echo shippers_list($name, $selected_id, $none_option, $submit_on_change);
1323         echo "</td>\n";
1324 }
1325
1326 function shippers_list_row($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
1327 {
1328         echo "<tr><td class='label'>$label</td>";
1329         shippers_list_cells(null, $name, $selected_id, $none_option, $submit_on_change);
1330         echo "</tr>\n";
1331 }
1332
1333 //-------------------------------------------------------------------------------------
1334
1335 function sales_persons_list($name, $selected_id=null, $spec_opt=false)
1336 {
1337         $sql = "SELECT salesman_code, salesman_name, inactive FROM ".TB_PREF."salesman";
1338         return combo_input($name, $selected_id, $sql, 'salesman_code', 'salesman_name', 
1339                 array('order'=>array('salesman_name'),
1340                         'spec_option' => $spec_opt,
1341                         'spec_id' => ALL_NUMERIC));
1342 }
1343
1344 function sales_persons_list_cells($label, $name, $selected_id=null, $spec_opt=false)
1345 {
1346         if ($label != null)
1347                 echo "<td>$label</td>\n";
1348         echo "<td>\n";
1349         echo sales_persons_list($name, $selected_id, $spec_opt);
1350         echo "</td>\n";
1351 }
1352
1353 function sales_persons_list_row($label, $name, $selected_id=null, $spec_opt=false)
1354 {
1355         echo "<tr><td class='label'>$label</td>";
1356         sales_persons_list_cells(null, $name, $selected_id, $spec_opt);
1357         echo "</tr>\n";
1358 }
1359
1360 //------------------------------------------------------------------------------------
1361
1362 function sales_areas_list($name, $selected_id=null, $special_option=false)
1363 {
1364         $sql = "SELECT area_code, description, inactive FROM ".TB_PREF."areas";
1365         return combo_input($name, $selected_id, $sql, 'area_code', 'description', 
1366                 array('spec_option' => $special_option===true ? ' ' : $special_option,
1367                 'order' => 'description', 'spec_id' => 0));
1368 }
1369
1370 function sales_areas_list_cells($label, $name, $selected_id=null, $special_option=false)
1371 {
1372         if ($label != null)
1373                 echo "<td>$label</td>\n";
1374         echo "<td>";
1375         echo sales_areas_list($name, $selected_id, $special_option);
1376         echo "</td>\n";
1377 }
1378
1379 function sales_areas_list_row($label, $name, $selected_id=null, $special_option=false)
1380 {
1381         echo "<tr><td class='label'>$label</td>";
1382         sales_areas_list_cells(null, $name, $selected_id, $special_option);
1383         echo "</tr>\n";
1384 }
1385
1386 //------------------------------------------------------------------------------------
1387
1388 function sales_groups_list($name, $selected_id=null, $special_option=false)
1389 {
1390         $sql = "SELECT id, description, inactive FROM ".TB_PREF."groups";
1391         return combo_input($name, $selected_id, $sql, 'id', 'description', array(
1392                 'spec_option' => $special_option===true ? ' ' : $special_option,
1393                 'order' => 'description', 'spec_id' => 0,
1394         ));
1395 }
1396
1397 function sales_groups_list_cells($label, $name, $selected_id=null, $special_option=false)
1398 {
1399         if ($label != null)
1400                 echo "<td>$label</td>\n";
1401         echo "<td>";
1402         echo sales_groups_list($name, $selected_id, $special_option);
1403         echo "</td>\n";
1404 }
1405
1406 function sales_groups_list_row($label, $name, $selected_id=null, $special_option=false)
1407 {
1408         echo "<tr><td class='label'>$label</td>";
1409         sales_groups_list_cells(null, $name, $selected_id, $special_option);
1410         echo "</tr>\n";
1411 }
1412
1413 //------------------------------------------------------------------------------------
1414
1415 function _format_template_items($row)
1416 {
1417         return ($row[0] . "&nbsp;- &nbsp;" . _("Amount") . "&nbsp;".$row[1]);
1418 }
1419
1420 function templates_list($name, $selected_id=null, $special_option=false)
1421 {
1422         $sql = "SELECT sorder.order_no, Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue
1423                 FROM ".TB_PREF."sales_orders as sorder, ".TB_PREF."sales_order_details as line
1424                 WHERE sorder.order_no = line.order_no AND sorder.type = 1 GROUP BY line.order_no";
1425         return combo_input($name, $selected_id, $sql, 'order_no', 'OrderValue', array(
1426                 'format' => '_format_template_items',
1427                 'spec_option' => $special_option===true ? ' ' : $special_option,
1428                 'order' => 'order_no', 'spec_id' => 0,
1429         ));
1430 }
1431
1432 function templates_list_cells($label, $name, $selected_id=null, $special_option=false)
1433 {
1434         if ($label != null)
1435                 echo "<td>$label</td>\n";
1436         echo "<td>";
1437         echo templates_list($name, $selected_id, $special_option);
1438         echo "</td>\n";
1439 }
1440
1441 function templates_list_row($label, $name, $selected_id=null, $special_option=false)
1442 {
1443         echo "<tr><td class='label'>$label</td>";
1444         templates_list_cells(null, $name, $selected_id, $special_option);
1445         echo "</tr>\n";
1446 }
1447
1448 //------------------------------------------------------------------------------------
1449
1450 function workorders_list($name, $selected_id=null)
1451 {
1452         $sql = "SELECT id, wo_ref FROM ".TB_PREF."workorders WHERE closed=0";
1453         return combo_input($name, $selected_id, $sql, 'id', 'wo_ref', array());
1454 }
1455
1456 function workorders_list_cells($label, $name, $selected_id=null)
1457 {
1458         if ($label != null)
1459                 echo "<td>$label</td>\n";
1460         echo "<td>";
1461         echo workorders_list($name, $selected_id);
1462         echo "</td>\n";
1463 }
1464
1465 function workorders_list_row($label, $name, $selected_id=null)
1466 {
1467         echo "<tr><td class='label'>$label</td>";
1468         workorders_list_cells(null, $name, $selected_id);
1469         echo "</tr>\n";
1470 }
1471
1472 //------------------------------------------------------------------------------------
1473
1474 function payment_terms_list($name, $selected_id=null)
1475 {
1476         $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
1477         return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms', array());
1478 }
1479
1480 function payment_terms_list_cells($label, $name, $selected_id=null)
1481 {
1482         if ($label != null)
1483                 echo "<td>$label</td>\n";
1484         echo "<td>";
1485         echo payment_terms_list($name, $selected_id);
1486         echo "</td>\n";
1487 }
1488
1489 function payment_terms_list_row($label, $name, $selected_id=null)
1490 {
1491         echo "<tr><td class='label'>$label</td>";
1492         payment_terms_list_cells(null, $name, $selected_id);
1493         echo "</tr>\n";
1494 }
1495
1496 //------------------------------------------------------------------------------------
1497
1498 function credit_status_list($name, $selected_id=null)
1499 {
1500         $sql ="SELECT id, reason_description, inactive FROM ".TB_PREF."credit_status";
1501         return combo_input($name, $selected_id, $sql, 'id', 'reason_description', array());
1502 }
1503
1504 function credit_status_list_cells($label, $name, $selected_id=null)
1505 {
1506         if ($label != null)
1507                 echo "<td>$label</td>\n";
1508         echo "<td>";
1509         echo credit_status_list($name, $selected_id);
1510         echo "</td>\n";
1511 }
1512
1513 function credit_status_list_row($label, $name, $selected_id=null)
1514 {
1515         echo "<tr><td class='label'>$label</td>";
1516         credit_status_list_cells(null, $name, $selected_id);
1517         echo "</tr>\n";
1518 }
1519
1520 //-----------------------------------------------------------------------------------------------
1521
1522 function sales_types_list($name, $selected_id=null, $submit_on_change=false, $special_option=false)
1523 {
1524         $sql = "SELECT id, sales_type, inactive FROM ".TB_PREF."sales_types";
1525
1526         return combo_input($name, $selected_id, $sql, 'id', 'sales_type',
1527         array(
1528                 'spec_option' => $special_option===true ? _("All Sales Types") : $special_option,
1529                 'spec_id' => 0,
1530                 'select_submit'=> $submit_on_change,
1531         ) );
1532 }
1533
1534 function sales_types_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1535 {
1536         if ($label != null)
1537                 echo "<td>$label</td>\n";
1538         echo "<td>";
1539         echo sales_types_list($name, $selected_id, $submit_on_change, $special_option);
1540         echo "</td>\n";
1541 }
1542
1543 function sales_types_list_row($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1544 {
1545         echo "<tr><td class='label'>$label</td>";
1546         sales_types_list_cells(null, $name, $selected_id, $submit_on_change, $special_option);
1547         echo "</tr>\n";
1548 }
1549
1550 //-----------------------------------------------------------------------------------------------
1551
1552 function _format_date($row)
1553 {
1554         return sql2date($row['reconciled']);
1555 }
1556
1557 function bank_reconciliation_list($account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1558 {
1559         $sql = "SELECT reconciled, reconciled FROM ".TB_PREF."bank_trans
1560                 WHERE bank_act=".db_escape($account)." AND reconciled IS NOT NULL
1561                 GROUP BY reconciled";
1562         return combo_input($name, $selected_id, $sql, 'id', 'reconciled',
1563                 array(
1564                         'spec_option' => $special_option,
1565                         'format' => '_format_date',
1566                         'spec_id' => '',
1567                         'select_submit'=> $submit_on_change
1568                 ) );
1569 }
1570
1571 function bank_reconciliation_list_cells($label,$account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1572 {
1573         if ($label != null)
1574                 echo "<td>$label</td>\n";
1575         echo "<td>";
1576         echo bank_reconciliation_list($account, $name, $selected_id, $submit_on_change, $special_option);
1577         echo "</td>\n";
1578 }
1579 /*
1580 function bank_reconciliation_list_row($label, $account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1581 {
1582         echo "<tr>\n";
1583         bank_reconciliation_list_cells($label, $account, $name, $selected_id, $submit_on_change, $special_option);
1584         echo "</tr>\n";
1585 }
1586 */
1587 //-----------------------------------------------------------------------------------------------
1588
1589 function workcenter_list($name, $selected_id=null, $all_option=false)
1590 {
1591
1592         $sql = "SELECT id, name, inactive FROM ".TB_PREF."workcentres";
1593
1594         return combo_input($name, $selected_id, $sql, 'id', 'name',
1595                 array(
1596                         'spec_option' =>$all_option===true ? _("All Suppliers") : $all_option,
1597                         'spec_id' => ALL_TEXT,
1598                 ) );
1599 }
1600
1601 function workcenter_list_cells($label, $name, $selected_id=null, $all_option=false)
1602 {
1603         default_focus($name);
1604         if ($label != null)
1605                 echo "<td>$label</td>\n";
1606         echo "<td>";
1607         echo workcenter_list($name, $selected_id, $all_option);
1608         echo "</td>\n";
1609 }
1610
1611 function workcenter_list_row($label, $name, $selected_id=null, $all_option=false)
1612 {
1613         echo "<tr><td class='label'>$label</td>";
1614         workcenter_list_cells(null, $name, $selected_id, $all_option);
1615         echo "</tr>\n";
1616 }
1617
1618 //-----------------------------------------------------------------------------------------------
1619
1620 function bank_accounts_list($name, $selected_id=null, $submit_on_change=false, $spec_option=false)
1621 {
1622         $sql = "SELECT id, bank_account_name, bank_curr_code, inactive
1623                 FROM ".TB_PREF."bank_accounts";
1624
1625         return combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1626                 array(
1627                         'format' => '_format_add_curr',
1628                         'select_submit'=> $submit_on_change,
1629                         'spec_option' => $spec_option,
1630                         'spec_id' => '',
1631                         'async' => false
1632                 ) );
1633 }
1634
1635 function bank_accounts_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1636 {
1637         if ($label != null)
1638                 echo "<td>$label</td>\n";
1639         echo "<td>";
1640         echo bank_accounts_list($name, $selected_id, $submit_on_change);
1641         echo "</td>\n";
1642 }
1643
1644 function bank_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1645 {
1646         echo "<tr><td class='label'>$label</td>";
1647         bank_accounts_list_cells(null, $name, $selected_id, $submit_on_change);
1648         echo "</tr>\n";
1649 }
1650 //-----------------------------------------------------------------------------------------------
1651
1652 function cash_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false, $all_option=false)
1653 {
1654
1655         $sql = "SELECT id, bank_account_name, bank_curr_code, inactive
1656                 FROM ".TB_PREF."bank_accounts
1657                 WHERE account_type=".BT_CASH;
1658
1659         if ($label != null)
1660                 echo "<tr><td class='label'>$label</td>\n";
1661         echo "<td>";
1662         echo combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1663                 array(
1664                         'spec_option' => $all_option,
1665                         'spec_id' => ALL_TEXT,
1666                         'format' => '_format_add_curr',
1667                         'select_submit'=> $submit_on_change,
1668                         'async' => true
1669                 ) );
1670         echo "</td></tr>\n";
1671 }
1672 //-----------------------------------------------------------------------------------------------
1673
1674 function pos_list_row($label, $name, $selected_id=null, $spec_option=false, $submit_on_change=false)
1675 {
1676         $sql = "SELECT id, pos_name, inactive FROM ".TB_PREF."sales_pos";
1677
1678         default_focus($name);
1679         echo '<tr>';
1680         if ($label != null)
1681                 echo "<td class='label'>$label</td>\n";
1682         echo "<td>";
1683
1684         echo combo_input($name, $selected_id, $sql, 'id', 'pos_name',
1685                 array(
1686                         'select_submit'=> $submit_on_change,
1687                         'async' => true,
1688                         'spec_option' =>$spec_option,
1689                         'spec_id' => -1,
1690                         'order'=> array('pos_name')
1691                 ) );
1692         echo "</td></tr>\n";
1693
1694 }
1695 //-----------------------------------------------------------------------------------------------
1696 // Payment type selector for current user.
1697 //
1698 function sale_payment_list($name, $category, $selected_id=null, $submit_on_change=true, $prepayments=true)
1699 {
1700         $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
1701
1702         if ($category == PM_CASH) // only cash
1703                         $sql .= " WHERE days_before_due=0 AND day_in_following_month=0";
1704         elseif ($category == PM_CREDIT) // only delayed payments
1705                         $sql .= " WHERE days_before_due".($prepayments ? '!=': '>')."0 OR day_in_following_month!=0";
1706         elseif (!$prepayments)
1707                         $sql .= " WHERE days_before_due>=0";
1708
1709         return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms',
1710                 array(
1711                         'select_submit'=> $submit_on_change,
1712                         'async' => true
1713                 ) );
1714
1715 }
1716
1717 function sale_payment_list_cells($label, $name, $category, $selected_id=null, $submit_on_change=true, $prepayments=true)
1718 {
1719         if ($label != null)
1720                 echo "<td class='label'>$label</td>\n";
1721         echo "<td>";
1722
1723         echo sale_payment_list($name, $category, $selected_id, $submit_on_change, $prepayments);
1724
1725         echo "</td>\n";
1726 }
1727 //-----------------------------------------------------------------------------------------------
1728
1729 function class_list($name, $selected_id=null, $submit_on_change=false)
1730 {
1731         $sql = "SELECT cid, class_name FROM ".TB_PREF."chart_class";
1732
1733         return combo_input($name, $selected_id, $sql, 'cid', 'class_name',
1734                 array(
1735                         'select_submit'=> $submit_on_change,
1736                         'async' => false
1737                 ) );
1738
1739 }
1740
1741 function class_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1742 {
1743         if ($label != null)
1744                 echo "<td>$label</td>\n";
1745         echo "<td>";
1746         echo class_list($name, $selected_id, $submit_on_change);
1747         echo "</td>\n";
1748 }
1749
1750 function class_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1751 {
1752         echo "<tr><td class='label'>$label</td>";
1753         class_list_cells(null, $name, $selected_id, $submit_on_change);
1754         echo "</tr>\n";
1755 }
1756
1757 //-----------------------------------------------------------------------------------------------
1758 function stock_categories_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false, $type=false)
1759 {
1760         $where_opts = array();
1761         if (in_array($type, array('F', 'T')))
1762                 $where_opts[] = "dflt_mb_flag=".db_escape($type);
1763         else
1764                 $where_opts[] = "NOT dflt_mb_flag IN('F','T')";
1765
1766         $sql = "SELECT category_id, description, inactive FROM ".TB_PREF."stock_category";
1767         return combo_input($name, $selected_id, $sql, 'category_id', 'description',
1768                 array('order'=>'category_id',
1769                         'spec_option' => $spec_opt,
1770                         'spec_id' => -1,
1771                         'select_submit'=> $submit_on_change,
1772                         'async' => true,
1773                         'where' => $where_opts,
1774                 ));
1775 }
1776
1777 function stock_categories_list_cells($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false, $type=false)
1778 {
1779         if ($label != null)
1780                 echo "<td>$label</td>\n";
1781         echo "<td>";
1782         echo stock_categories_list($name, $selected_id, $spec_opt, $submit_on_change, $type);
1783         echo "</td>\n";
1784 }
1785
1786 function stock_categories_list_row($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false, $type=false)
1787 {
1788         echo "<tr><td class='label'>$label</td>";
1789         stock_categories_list_cells(null, $name, $selected_id, $spec_opt, $submit_on_change, $type);
1790         echo "</tr>\n";
1791 }
1792
1793 //-----------------------------------------------------------------------------------------------
1794 function fixed_asset_classes_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1795 {
1796         $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 "
1797                 .TB_PREF."stock_fa_class c LEFT JOIN ".TB_PREF."stock_fa_class p ON c.parent_id=p.fa_class_id";
1798
1799         return combo_input($name, $selected_id, $sql, 'c.fa_class_id', 'desc',
1800                 array('order'=>'c.fa_class_id',
1801                         'spec_option' => $spec_opt,
1802                         'spec_id' => '-1',
1803                         'select_submit'=> $submit_on_change,
1804                         'async' => true,
1805                         'search_box' => true,
1806                         'search' => array("c.fa_class_id"),
1807                     'search_submit' => false,
1808                     'spec_id' => '',
1809                     'size' => 3,
1810                     'max' => 3,
1811                         'category' => 'class',
1812                 ));
1813 }
1814
1815 function fixed_asset_classes_list_row($label, $name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1816 {
1817         echo "<tr><td class='label'>$label</td>";
1818         echo "<td>";
1819         echo fixed_asset_classes_list($name, $selected_id, $spec_opt, $submit_on_change);
1820         echo "</td></tr>\n";
1821 }
1822
1823 //-----------------------------------------------------------------------------------------------
1824
1825 function gl_account_types_list($name, $selected_id=null, $all_option=false, $all=true)
1826 {
1827
1828         $sql = "SELECT id, name FROM ".TB_PREF."chart_types";
1829
1830         return combo_input($name, $selected_id, $sql, 'id', 'name',
1831                 array(
1832                         'format' => '_format_account',
1833                         'order' => array('class_id', 'id', 'parent'),
1834                         'spec_option' =>$all_option,
1835                         'spec_id' => ALL_TEXT
1836                 ) );
1837 }
1838
1839 function gl_account_types_list_cells($label, $name, $selected_id=null, $all_option=false, $all=false)
1840 {
1841         if ($label != null)
1842                 echo "<td>$label</td>\n";
1843         echo "<td>";
1844         echo gl_account_types_list($name, $selected_id, $all_option, $all);
1845         echo "</td>\n";
1846 }
1847
1848 function gl_account_types_list_row($label, $name, $selected_id=null, $all_option=false, $all=false)
1849 {
1850         echo "<tr><td class='label'>$label</td>";
1851         gl_account_types_list_cells(null, $name, $selected_id, $all_option,
1852                 $all);
1853         echo "</tr>\n";
1854 }
1855
1856 //-----------------------------------------------------------------------------------------------
1857 function gl_all_accounts_list($name, $selected_id=null, $skip_bank_accounts=false,
1858         $cells=false, $all_option=false, $submit_on_change=false, $all=false, $type_id=false)
1859 {
1860         if ($skip_bank_accounts)
1861                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
1862                         FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
1863                         ."LEFT JOIN ".TB_PREF."bank_accounts acc "
1864                         ."ON chart.account_code=acc.account_code
1865                                 WHERE acc.account_code  IS NULL
1866                         AND chart.account_type=type.id";
1867         else
1868                 $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
1869                         FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type
1870                         WHERE chart.account_type=type.id";
1871         if ($type_id ) $sql .= " AND chart.account_type=".db_escape($type_id);
1872         $account = ($skip_bank_accounts ? "account2" : "account");
1873         return combo_input($name, $selected_id, $sql, 'chart.account_code', 'chart.account_name',
1874                 array(
1875                         'format' => '_format_account',
1876                         'spec_option' => $all_option===true ?  _("Use Item Sales Accounts") : $all_option,
1877                         'spec_id' => '',
1878                         'type' => 2,
1879                         'order' => array('type.class_id','type.id','account_code'),
1880                         'search_box' => $cells,
1881                         'search_submit' => false,
1882                         'size' => 12,
1883                         'max' => 10,
1884                         'cells' => true,
1885                         'select_submit'=> $submit_on_change,
1886                         'async' => false,
1887                         'category' => 2,
1888                         'show_inactive' => $all
1889                 ), $account );
1890 }
1891
1892 function _format_account($row)
1893 {
1894         return $row[0] .  "&nbsp;&nbsp;&nbsp;&nbsp;" . $row[1];
1895 }
1896
1897 function gl_all_accounts_list_cells($label, $name, $selected_id=null, 
1898         $skip_bank_accounts=false, $cells=false, $all_option=false, 
1899         $submit_on_change=false, $all=false, $type_id=false)
1900 {
1901         if ($label != null)
1902                 echo "<td>$label</td>\n";
1903         echo "<td>";
1904         echo gl_all_accounts_list($name, $selected_id, 
1905                 $skip_bank_accounts, $cells, $all_option, $submit_on_change, $all, $type_id);
1906         echo "</td>\n";
1907 }
1908
1909 function gl_all_accounts_list_row($label, $name, $selected_id=null, 
1910         $skip_bank_accounts=false, $cells=false, $all_option=false, $submit_on_change=false, $all=false, $type_id=false)
1911 {
1912         echo "<tr><td class='label'>$label</td>";
1913         gl_all_accounts_list_cells(null, $name, $selected_id, 
1914                 $skip_bank_accounts, $cells, $all_option, $submit_on_change, $all, $type_id);
1915         echo "</tr>\n";
1916 }
1917
1918 function yesno_list($name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1919 {
1920         $items = array();
1921         $items['0'] = strlen($name_no) ? $name_no : _("No");
1922         $items['1'] = strlen($name_yes) ? $name_yes : _("Yes");
1923
1924         return array_selector($name, $selected_id, $items, 
1925                 array( 
1926                         'select_submit'=> $submit_on_change,
1927                         'async' => false ) ); // FIX?
1928 }
1929
1930 function yesno_list_cells($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1931 {
1932         if ($label != null)
1933                 echo "<td>$label</td>\n";
1934         echo "<td>";
1935         echo yesno_list($name, $selected_id, $name_yes, $name_no, $submit_on_change);
1936         echo "</td>\n";
1937 }
1938
1939 function yesno_list_row($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1940 {
1941         echo "<tr><td class='label'>$label</td>";
1942         yesno_list_cells(null, $name, $selected_id, $name_yes, $name_no, $submit_on_change);
1943         echo "</tr>\n";
1944 }
1945
1946 //------------------------------------------------------------------------------------------------
1947
1948 function languages_list($name, $selected_id=null, $all_option=false)
1949 {
1950         global $installed_languages;
1951
1952         $items = array();
1953         if ($all_option)
1954                 $items[''] = $all_option;
1955         foreach ($installed_languages as $lang)
1956                         $items[$lang['code']] = $lang['name'];
1957         return array_selector($name, $selected_id, $items);
1958 }
1959
1960 function languages_list_cells($label, $name, $selected_id=null, $all_option=false)
1961 {
1962         if ($label != null)
1963                 echo "<td>$label</td>\n";
1964         echo "<td>";
1965         echo languages_list($name, $selected_id, $all_option);
1966         echo "</td>\n";
1967 }
1968
1969 function languages_list_row($label, $name, $selected_id=null, $all_option=false)
1970 {
1971         echo "<tr><td class='label'>$label</td>";
1972         languages_list_cells(null, $name, $selected_id, $all_option);
1973         echo "</tr>\n";
1974 }
1975
1976 //------------------------------------------------------------------------------------------------
1977
1978 function bank_account_types_list($name, $selected_id=null)
1979 {
1980         global $bank_account_types;
1981
1982         return array_selector($name, $selected_id, $bank_account_types);
1983 }
1984
1985 function bank_account_types_list_cells($label, $name, $selected_id=null)
1986 {
1987         if ($label != null)
1988                 echo "<td>$label</td>\n";
1989         echo "<td>";
1990         echo bank_account_types_list($name, $selected_id);
1991         echo "</td>\n";
1992 }
1993
1994 function bank_account_types_list_row($label, $name, $selected_id=null)
1995 {
1996         echo "<tr><td class='label'>$label</td>";
1997         bank_account_types_list_cells(null, $name, $selected_id);
1998         echo "</tr>\n";
1999 }
2000
2001 //------------------------------------------------------------------------------------------------
2002 function payment_person_types_list($name, $selected_id=null, $submit_on_change=false)
2003 {
2004         global $payment_person_types;
2005
2006         $items = array();
2007         foreach ($payment_person_types as $key=>$type)
2008         {
2009                 if ($key != PT_WORKORDER)
2010                         $items[$key] = $type;
2011         }               
2012         return array_selector($name, $selected_id, $items, 
2013                 array( 'select_submit'=> $submit_on_change ) );
2014 }
2015
2016 function payment_person_types_list_cells($label, $name, $selected_id=null, $related=null)
2017 {
2018         if ($label != null)
2019                 echo "<td>$label</td>\n";
2020         echo "<td>";
2021         echo payment_person_types_list($name, $selected_id, $related);
2022         echo "</td>\n";
2023 }
2024
2025 function payment_person_types_list_row($label, $name, $selected_id=null, $related=null)
2026 {
2027         echo "<tr><td class='label'>$label</td>";
2028         payment_person_types_list_cells(null, $name, $selected_id, $related);
2029         echo "</tr>\n";
2030 }
2031
2032 //------------------------------------------------------------------------------------------------
2033
2034 function wo_types_list($name, $selected_id=null)
2035 {
2036         global $wo_types_array;
2037         
2038         return array_selector($name, $selected_id, $wo_types_array, 
2039                 array( 'select_submit'=> true, 'async' => true ) );
2040 }
2041
2042 function wo_types_list_row($label, $name, $selected_id=null)
2043 {
2044         echo "<tr><td class='label'>$label</td><td>\n";
2045         echo wo_types_list($name, $selected_id);
2046         echo "</td></tr>\n";
2047 }
2048
2049 //------------------------------------------------------------------------------------------------
2050
2051 function dateformats_list_row($label, $name, $value=null)
2052 {
2053         global $SysPrefs;
2054
2055         echo "<tr><td class='label'>$label</td>\n<td>";
2056         echo array_selector( $name, $value, $SysPrefs->dateformats );
2057         echo "</td></tr>\n";
2058 }
2059
2060 function dateseps_list_row($label, $name, $value=null)
2061 {
2062         global $SysPrefs;
2063
2064         echo "<tr><td class='label'>$label</td>\n<td>";
2065         echo array_selector( $name, $value, $SysPrefs->dateseps );
2066         echo "</td></tr>\n";
2067 }
2068
2069 function thoseps_list_row($label, $name, $value=null)
2070 {
2071         global $SysPrefs;
2072
2073         echo "<tr><td class='label'>$label</td>\n<td>";
2074         echo array_selector( $name, $value, $SysPrefs->thoseps );
2075         echo "</td></tr>\n";
2076 }
2077
2078 function decseps_list_row($label, $name, $value=null)
2079 {
2080         global $SysPrefs;
2081
2082         echo "<tr><td class='label'>$label</td>\n<td>";
2083         echo array_selector( $name, $value, $SysPrefs->decseps );
2084         echo "</td></tr>\n";
2085 }
2086
2087 function themes_list_row($label, $name, $value=null)
2088 {
2089         global $path_to_root;
2090
2091         $path = $path_to_root.'/themes/';
2092         $themes = array();
2093         $themedir = opendir($path);
2094         while(false !== ($fname = readdir($themedir)))
2095         {
2096                 if($fname!='.' && $fname!='..' && $fname!='CVS' && is_dir($path.$fname))
2097                 {
2098                         $themes[$fname] =  $fname;
2099                 }
2100         }
2101         ksort($themes);
2102
2103         echo "<tr><td class='label'>$label</td>\n<td>";
2104         echo array_selector( $name, $value, $themes );
2105         echo "</td></tr>\n";
2106 }
2107
2108 function pagesizes_list_row($label, $name, $value=null)
2109 {
2110         global $SysPrefs;
2111
2112         $items = array();
2113         foreach ($SysPrefs->pagesizes as $pz)
2114                 $items[$pz] = $pz;
2115
2116         echo "<tr><td class='label'>$label</td>\n<td>";
2117         echo array_selector( $name, $value, $items );
2118         echo "</td></tr>\n";
2119 }
2120
2121 function systypes_list($name, $value=null, $spec_opt=false, $submit_on_change=false, $exclude=array())
2122 {
2123         global $systypes_array;
2124
2125         // emove non-voidable transactions if needed
2126         $systypes = array_diff_key($systypes_array, array_flip($exclude));
2127         return array_selector($name, $value, $systypes, 
2128                 array( 
2129                         'spec_option'=> $spec_opt,
2130                         'spec_id' => ALL_NUMERIC,
2131                         'select_submit'=> $submit_on_change,
2132                         'async' => false,
2133                         )
2134         );
2135 }
2136
2137 function systypes_list_cells($label, $name, $value=null, $submit_on_change=false, $exclude=array())
2138 {
2139         if ($label != null)
2140                 echo "<td>$label</td>\n";
2141         echo "<td>";
2142         echo systypes_list($name, $value, false, $submit_on_change, $exclude);
2143         echo "</td>\n";
2144 }
2145
2146 function systypes_list_row($label, $name, $value=null, $submit_on_change=false, $exclude=array())
2147 {
2148         echo "<tr><td class='label'>$label</td>";
2149         systypes_list_cells(null, $name, $value, $submit_on_change, $exclude);
2150         echo "</tr>\n";
2151 }
2152
2153 function journal_types_list_cells($label, $name, $value=null, $submit_on_change=false)
2154 {
2155         global $systypes_array;
2156
2157         if ($label != null)
2158                 echo "<td>$label</td>\n";
2159         echo "<td>";
2160
2161         $items = $systypes_array;
2162
2163         // exclude quotes, orders and dimensions
2164         foreach (array(ST_PURCHORDER, ST_SALESORDER, ST_DIMENSION, ST_SALESQUOTE, ST_LOCTRANSFER) as $excl)
2165                         unset($items[$excl]);
2166         
2167         echo array_selector($name, $value, $items, 
2168                 array( 
2169                         'spec_option'=> _("All"),
2170                         'spec_id' => -1,
2171                         'select_submit'=> $submit_on_change,
2172                         'async' => false
2173                         )
2174         );
2175         echo "</td>\n";
2176 }
2177
2178 function cust_allocations_list_cells($label, $name, $selected=null, $submit_on_change=false)
2179 {
2180
2181         if ($label != null)
2182                 label_cell($label);
2183         echo "<td>\n";
2184         $allocs = array( 
2185                 ALL_TEXT=>_("All Types"),
2186                 '1'=> _("Sales Invoices"),
2187                 '2'=> _("Unsettled transactions"),
2188                 '3' => _("Payments"),
2189                 '4' => _("Credit Notes"),
2190                 '5' => _("Delivery Notes")
2191         );
2192         echo array_selector($name, $selected, $allocs, array('select_submit'=> $submit_on_change));
2193         echo "</td>\n";
2194 }
2195
2196 function supp_allocations_list_cell($name, $selected=null)
2197 {
2198
2199         echo "<td>\n";
2200         $allocs = array( 
2201                 ALL_TEXT=>_("All Types"),
2202                 '1'=> _("Invoices"),
2203                 '2'=> _("Overdue Invoices"),
2204                 '3' => _("Payments"),
2205                 '4' => _("Credit Notes"),
2206                 '5' => _("Overdue Credit Notes")
2207         );
2208         echo array_selector($name, $selected, $allocs);
2209         echo "</td>\n";
2210 }
2211
2212 function supp_transactions_list_cell($name, $selected=null, $submit_on_change=false)
2213 {
2214
2215         echo "<td>\n";
2216         $allocs = array( 
2217                 ALL_TEXT=>_("All Types"),
2218                 '6'=>_("GRNs"),
2219                 '1'=> _("Invoices"),
2220                 '2'=> _("Unsettled transactions"),
2221                 '3' => _("Payments"),
2222                 '4' => _("Credit Notes"),
2223                 '5' => _("Overdue Credit Notes")
2224         );
2225
2226         echo array_selector($name, $selected, $allocs, array('select_submit'=> $submit_on_change));
2227         echo "</td>\n";
2228 }
2229
2230 function policy_list_cells($label, $name, $selected=null)
2231 {
2232         if ($label != null)
2233                 label_cell($label);
2234         echo "<td>\n";
2235         echo array_selector($name, $selected, 
2236                                 array( '' => _("Automatically put balance on back order"),
2237                                         'CAN' => _("Cancel any quantites not delivered")) );
2238         echo "</td>\n";
2239 }
2240
2241 function policy_list_row($label, $name, $selected=null)
2242 {
2243         echo "<tr><td class='label'>$label</td>";
2244         policy_list_cells(null, $name, $selected);
2245         echo "</tr>\n";
2246 }
2247
2248 function credit_type_list_cells($label, $name, $selected=null, $submit_on_change=false)
2249 {
2250         if ($label != null)
2251                 label_cell($label);
2252         echo "<td>\n";
2253         echo array_selector($name, $selected, 
2254                         array( 'Return' => _("Items Returned to Inventory Location"),
2255                                 'WriteOff' => _("Items Written Off")),
2256                         array( 'select_submit'=> $submit_on_change ) );
2257         echo "</td>\n";
2258 }
2259
2260 function credit_type_list_row($label, $name, $selected=null, $submit_on_change=false)
2261 {
2262         echo "<tr><td class='label'>$label</td>";
2263         credit_type_list_cells(null, $name, $selected, $submit_on_change);
2264         echo "</tr>\n";
2265 }
2266
2267 function number_list($name, $selected, $from, $to, $no_option=false)
2268 {
2269         $items = array();
2270         for ($i = $from; $i <= $to; $i++)
2271                 $items[$i] = "$i";
2272
2273         return array_selector($name, $selected, $items,
2274                         array(  'spec_option' => $no_option,
2275                                         'spec_id' => ALL_NUMERIC) );
2276 }
2277
2278 function number_list_cells($label, $name, $selected, $from, $to, $no_option=false)
2279 {
2280         if ($label != null)
2281                 label_cell($label);
2282         echo "<td>\n";
2283         echo number_list($name, $selected, $from, $to, $no_option);
2284         echo "</td>\n";
2285 }
2286
2287 function number_list_row($label, $name, $selected, $from, $to, $no_option=false)
2288 {
2289         echo "<tr><td class='label'>$label</td>";
2290         echo number_list_cells(null, $name, $selected, $from, $to, $no_option);
2291         echo "</tr>\n";
2292 }
2293
2294 function print_profiles_list_row($label, $name, $selected_id=null, $spec_opt=false,
2295         $submit_on_change=true)
2296 {
2297         $sql = "SELECT profile FROM ".TB_PREF."print_profiles"
2298                 ." GROUP BY profile";
2299         $result = db_query($sql, 'cannot get all profile names');
2300         $profiles = array();
2301         while($myrow=db_fetch($result)) {
2302                 $profiles[$myrow['profile']] = $myrow['profile'];
2303         }
2304
2305         echo "<tr>";
2306         if ($label != null)
2307                 echo "<td class='label'>$label</td>\n";
2308         echo "<td>";
2309
2310         echo array_selector($name, $selected_id, $profiles, 
2311                 array( 'select_submit'=> $submit_on_change,
2312                         'spec_option'=>$spec_opt,
2313                         'spec_id' => ''
2314                  ));
2315
2316         echo "</td></tr>\n";
2317 }
2318
2319 function printers_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
2320 {
2321         static $printers; // query only once for page display
2322
2323         if (!$printers) {
2324                 $sql = "SELECT id, name, description FROM ".TB_PREF."printers"; 
2325                 $result = db_query($sql, 'cannot get all printers');
2326                 $printers = array();
2327                 while($myrow=db_fetch($result)) {
2328                         $printers[$myrow['id']] = $myrow['name'].'&nbsp;-&nbsp;'.$myrow['description'];
2329                 }
2330         }
2331         return array_selector($name, $selected_id, $printers, 
2332                 array( 'select_submit'=> $submit_on_change,
2333                         'spec_option'=>$spec_opt,
2334                         'spec_id' => ''
2335                  ));
2336 }
2337
2338 //------------------------------------------------------------------------------------------------
2339
2340 function quick_entries_list($name, $selected_id=null, $type=null, $submit_on_change=false)
2341 {
2342         $where = false;
2343         $sql = "SELECT id, description FROM ".TB_PREF."quick_entries";
2344         if ($type != null)
2345                 $sql .= " WHERE type=$type";
2346
2347         return combo_input($name, $selected_id, $sql, 'id', 'description',
2348                 array(
2349                         'spec_id' => '',
2350                         'order' => 'description',
2351                         'select_submit'=> $submit_on_change,
2352                         'async' => false
2353                 ) );
2354
2355 }
2356
2357 function quick_entries_list_cells($label, $name, $selected_id=null, $type, $submit_on_change=false)
2358 {
2359         if ($label != null)
2360                 echo "<td>$label</td>\n";
2361         echo "<td>";    
2362         echo quick_entries_list($name, $selected_id, $type, $submit_on_change);
2363         echo "</td>";
2364 }
2365
2366 function quick_entries_list_row($label, $name, $selected_id=null, $type, $submit_on_change=false)
2367 {
2368         echo "<tr><td class='label'>$label</td>";
2369         quick_entries_list_cells(null, $name, $selected_id, $type, $submit_on_change);
2370         echo "</tr>\n";
2371 }
2372
2373
2374 function quick_actions_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2375 {
2376         global $quick_actions;
2377         
2378         echo "<tr><td class='label'>$label</td><td>";
2379         echo array_selector($name, $selected_id, $quick_actions, 
2380                 array( 
2381                         'select_submit'=> $submit_on_change
2382                 ) );
2383         echo "</td></tr>\n";
2384 }
2385
2386 function quick_entry_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2387 {
2388         global $quick_entry_types;
2389                 
2390         echo "<tr><td class='label'>$label</td><td>";
2391         echo array_selector($name, $selected_id, $quick_entry_types, 
2392                 array( 
2393                         'select_submit'=> $submit_on_change
2394                         ) );
2395         echo "</td></tr>\n";
2396 }
2397
2398 function record_status_list_row($label, $name) {
2399         return yesno_list_row($label, $name, null,      _('Inactive'), _('Active'));
2400 }
2401
2402 function class_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2403 {
2404         global $class_types;
2405         
2406         echo "<tr><td class='label'>$label</td><td>";
2407         echo array_selector($name, $selected_id, $class_types, 
2408                 array( 
2409                         'select_submit'=> $submit_on_change
2410                 ) );
2411         echo "</td></tr>\n";
2412 }
2413
2414 //------------------------------------------------------------------------------------------------
2415
2416 function security_roles_list($name, $selected_id=null, $new_item=false, $submit_on_change=false,
2417         $show_inactive = false)
2418 {
2419
2420         $sql = "SELECT id, role, inactive FROM ".TB_PREF."security_roles";
2421
2422         return combo_input($name, $selected_id, $sql, 'id', 'description',
2423                 array(
2424                         'spec_option'=>$new_item ? _("New role") : false,
2425                         'spec_id' => '',
2426                         'select_submit'=> $submit_on_change,
2427                         'show_inactive' => $show_inactive
2428                 ) );
2429 }
2430
2431 function security_roles_list_cells($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2432         $show_inactive = false)
2433 {
2434         if ($label != null)
2435                 echo "<td>$label</td>\n";
2436         echo "<td>";
2437         echo security_roles_list($name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2438         echo "</td>\n";
2439 }
2440
2441 function security_roles_list_row($label, $name, $selected_id=null, $new_item=false, $submit_on_change=false,
2442         $show_inactive = false)
2443 {
2444         echo "<tr><td class='label'>$label</td>";
2445         security_roles_list_cells(null, $name, $selected_id, $new_item, $submit_on_change, $show_inactive);
2446         echo "</tr>\n";
2447 }
2448
2449 function tab_list_row($label, $name, $selected_id=null)
2450 {
2451         global $installed_extensions;
2452         
2453         $tabs = array();
2454         foreach ($_SESSION['App']->applications as $app) {
2455                 $tabs[$app->id] = access_string($app->name, true);
2456         }
2457         echo "<tr>\n";
2458         echo "<td class='label'>$label</td><td>\n";
2459         echo array_selector($name, $selected_id, $tabs);
2460         echo "</td></tr>\n";
2461 }
2462
2463 //-----------------------------------------------------------------------------------------------
2464
2465 function tag_list($name, $height, $type, $multi=false, $all=false, $spec_opt = false)
2466 {
2467         // Get tags
2468         global $path_to_root;
2469         include_once($path_to_root . "/admin/db/tags_db.inc");
2470         $results = get_tags($type, $all);
2471
2472         while ($tag = db_fetch($results))
2473                 $tags[$tag['id']] = $tag['name'];
2474         
2475         if (!isset($tags)) {
2476                 $tags[''] = $all ? _("No tags defined.") : _("No active tags defined.");
2477                 $spec_opt = false;
2478         }
2479         return array_selector($name, null, $tags,
2480                 array(
2481                         'multi' => $multi,
2482                         'height' => $height,
2483                         'spec_option'=> $spec_opt,
2484                         'spec_id' => -1,
2485                 ) );
2486 }
2487
2488 function tag_list_cells($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2489 {
2490         if ($label != null)
2491                 echo "<td>$label</td>\n";
2492         echo "<td>\n";
2493         echo tag_list($name, $height, $type, $mult, $all, $spec_opt);
2494         echo "</td>\n";
2495         
2496 }
2497
2498 function tag_list_row($label, $name, $height, $type, $mult=false, $all=false, $spec_opt = false)
2499 {
2500         echo "<tr><td class='label'>$label</td>";
2501         tag_list_cells(null, $name, $height, $type, $mult, $all, $spec_opt);
2502         echo "</tr>\n"; 
2503 }
2504
2505 //---------------------------------------------------------------------------------------------
2506 //      List of sets of active extensions 
2507 //
2508 function extset_list($name, $value=null, $submit_on_change=false)
2509 {
2510         global $db_connections;
2511
2512         $items = array();
2513         foreach ($db_connections as $comp)
2514                 $items[] = sprintf(_("Activated for '%s'"), $comp['name']);
2515         return array_selector( $name, $value, $items,
2516                 array(
2517                         'spec_option'=> _("Available and/or installed"),
2518                         'spec_id' => -1,
2519                         'select_submit'=> $submit_on_change,
2520                         'async' => true
2521                 ));
2522 }
2523
2524 function crm_category_types_list($name, $selected_id=null, $filter=array(), $submit_on_change=true)
2525 {
2526
2527         $sql = "SELECT id, name, type, inactive FROM ".TB_PREF."crm_categories";
2528
2529         $multi = false;
2530         $groups = false;
2531         $where = array();
2532         if (@$filter['class']) {
2533                 $where[] = 'type='.db_escape($filter['class']);
2534         } else
2535                 $groups = 'type';
2536         if (@$filter['subclass']) $where[] = 'action='.db_escape($filter['subclass']);
2537         if (@$filter['entity']) $where[] = 'entity_id='.db_escape($filter['entity']);
2538         if (@$filter['multi']) { // contact category selector for person
2539                 $multi = true;
2540         }
2541
2542         return combo_input($name, $selected_id, $sql, 'id', 'name',
2543                 array(
2544                         'multi' => $multi,
2545                         'height' => $multi ? 5:1,
2546                         'category' => $groups,
2547                         'select_submit'=> $submit_on_change,
2548                         'async' => true,
2549                         'where' => $where
2550                 ));
2551 }
2552
2553 function crm_category_types_list_row($label, $name, $selected_id=null, $filter=array(), $submit_on_change=true)
2554 {
2555         echo "<tr><td class='label'>$label</td><td>";
2556         echo crm_category_types_list($name, $selected_id, $filter, $submit_on_change);
2557         echo "</td></tr>\n";
2558 }
2559
2560 function payment_type_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2561 {
2562         global $pterm_types;
2563         
2564         echo "<tr><td class='label'>$label</td><td>";
2565         echo array_selector($name, $selected_id, $pterm_types, 
2566                 array( 
2567                         'select_submit'=> $submit_on_change
2568                 ) );
2569         echo "</td></tr>\n";
2570 }
2571
2572 function coa_list_row($label, $name, $value=null)
2573 {
2574         global $path_to_root, $installed_extensions;
2575
2576         $path = $path_to_root.'/sql/';
2577         $coas = array();
2578         $sqldir = opendir($path);
2579         while (false !== ($fname = readdir($sqldir)))
2580         {
2581                 if (is_file($path.$fname) && substr($fname,-4)=='.sql' && @($fname[2] == '_'))
2582                 {
2583                         $ext = array_search_value($fname, $installed_extensions, 'sql');
2584                         if ($ext!=null) {
2585                                 $descr = $ext['name'];
2586                         } elseif ($fname == 'en_US-new.sql') { // two standard COAs
2587                                 $descr = _("Standard new company American COA (4 digit)");
2588                         } elseif ($fname == 'en_US-demo.sql') {
2589                                 $descr = _("Standard American COA (4 digit) with demo data");
2590                         } else
2591                                 $descr = $fname;
2592
2593                         $coas[$fname] =  $descr;
2594                 }
2595         }
2596         ksort($coas);
2597
2598         echo "<tr><td class='label'>$label</td>\n<td>";
2599         echo array_selector( $name, $value, $coas );
2600         echo "</td></tr>\n";
2601 }
2602
2603 function payment_services($name)
2604 {
2605         global $payment_services;
2606
2607         $services = (object)array_combine(array_keys($payment_services), array_keys($payment_services));
2608
2609         return array_selector($name, null, $services, array(
2610                         'spec_option'=> _("No payment Link"),
2611                         'spec_id' => '',
2612                 ));
2613 }
2614
2615 function vat_category_list_row($label, $name, $selected_id=null, $enabled = true, $submit_on_change=false, $show_obsolete=false)
2616 {
2617         global $vat_categories;
2618
2619         echo "<tr>";
2620         if ($label != null)
2621                 echo "<td class='label'>$label</td>\n";
2622         echo "<td>";
2623         
2624         $categories = $vat_categories;
2625         if (!$show_obsolete)
2626           unset($categories[VC_MEDIA]); // obsolete category removed to avoid mistake usage
2627
2628         echo array_selector($name, $selected_id, $categories,
2629                 array( 'select_submit'=> $submit_on_change, 
2630                         'disabled' => !$enabled));
2631         echo "</td></tr>\n";
2632
2633 }
2634
2635 function tax_algorithm_list($name, $value=null, $submit_on_change = false)
2636 {
2637         global $tax_algorithms;
2638         
2639         return array_selector($name, $value, $tax_algorithms, 
2640                 array( 
2641                         'select_submit'=> $submit_on_change,
2642                         'async' => true,
2643                         )
2644         );
2645 }
2646
2647 function tax_algorithm_list_cells($label, $name, $value=null, $submit_on_change=false)
2648 {
2649         if ($label != null)
2650                 echo "<td>$label</td>\n";
2651         echo "<td>";
2652         echo tax_algorithm_list($name, $value, $submit_on_change);
2653         echo "</td>\n";
2654 }
2655
2656 function tax_algorithm_list_row($label, $name, $value=null, $submit_on_change=false)
2657 {
2658         echo "<tr><td class='label'>$label</td>";
2659         tax_algorithm_list_cells(null, $name, $value, $submit_on_change);
2660         echo "</tr>\n";
2661 }
2662
2663 function refline_list($name, $type, $value=null, $spec_option=false)
2664 {
2665         $sql = "SELECT id, prefix, inactive FROM ".TB_PREF."reflines";
2666
2667         $where = array();
2668
2669         if (isset($type))
2670                 $where = array('`trans_type`='.db_escape($type));
2671
2672         return combo_input($name, $value, $sql, 'id', 'prefix',
2673                 array(
2674                         'order'=>array('prefix'),
2675                         'spec_option' => $spec_option,
2676                         'spec_id' => '',
2677                         'type' => 2,
2678                         'where' => $where,
2679                         'select_submit' => true,
2680                         )
2681                 );
2682 }
2683
2684 function refline_list_row($label, $name, $type, $selected_id=null, $spec_option=false)
2685 {
2686         echo "<tr>";
2687         if ($label != null)
2688                 echo "<td class='label'>$label</td>\n";
2689         echo "<td>";
2690
2691         echo refline_list($name, $type, $selected_id, $spec_option);
2692         echo "</td></tr>\n";
2693 }
2694
2695
2696 //----------------------------------------------------------------------------------------------
2697
2698 function subledger_list($name, $account, $selected_id=null)
2699 {
2700
2701         $type = is_subledger_account($account);
2702         if (!$type)
2703                 return '';
2704
2705         if($type > 0)
2706                 $sql = "SELECT DISTINCT d.debtor_no as id, debtor_ref as name 
2707                 FROM "
2708                 .TB_PREF."debtors_master d,"
2709                 .TB_PREF."cust_branch c
2710                 WHERE d.debtor_no=c.debtor_no AND NOT d.inactive AND c.receivables_account=".db_escape($account);
2711         else
2712                 $sql = "SELECT supplier_id as id, supp_ref as name 
2713                 FROM "
2714                 .TB_PREF."suppliers s
2715                 WHERE NOT s.inactive AND s.payable_account=".db_escape($account);
2716
2717         $mode = get_company_pref('no_customer_list');
2718
2719         return combo_input($name, $selected_id, $sql, 'id', 'name',
2720         array(
2721                 'type' => 1,
2722                 'size' => 20,
2723                 'async' => false,
2724         ) );
2725 }
2726
2727 function subledger_list_cells($label, $name, $account, $selected_id=null)
2728 {
2729         if ($label != null)
2730                 echo "<td>$label</td>\n";
2731         echo "<td nowrap>";
2732         echo subledger_list($name, $account, $selected_id);
2733         echo "</td>\n";
2734 }
2735
2736 function subledger_list_row($label, $name, $selected_id=null, $all_option = false, 
2737         $submit_on_change=false, $show_inactive=false, $editkey = false)
2738 {
2739         echo "<tr><td class='label'>$label</td><td nowrap>";
2740         echo subledger_list($name, $account, $selected_id);
2741         echo "</td>\n</tr>\n";
2742 }
2743
2744 function accounts_type_list_row($label, $name, $selected_id=null)
2745 {
2746         echo "<tr>";
2747         if ($label != null)
2748                 echo "<td class='label'>$label</td>\n";
2749         echo "<td>";
2750         $sel = array(_("Numeric"), _("Alpha Numeric"), _("ALPHA NUMERIC"));
2751         echo array_selector($name, $selected_id, $sel); 
2752         echo "</td></tr>\n";
2753 }
2754
2755 function users_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $spec_opt=true)
2756 {
2757     $where = false;
2758         $sql = " SELECT user_id, IF(real_name = '', user_id, real_name) FROM ".TB_PREF."users";
2759
2760     if ($label != null)
2761         echo "<td>$label</td>\n";
2762     echo "<td>";
2763
2764     echo combo_input($name, $selected_id, $sql, 'user_id', 'real_name',
2765         array(
2766             'spec_option' => $spec_opt===true ?_("All users") : $spec_opt,
2767             'spec_id' => '',
2768             'order' => 'real_name',
2769             'select_submit'=> $submit_on_change,
2770             'async' => false
2771         ) );
2772     echo "</td>";
2773
2774 }
2775
2776 function collations_list_row($label, $name, $selected_id=null)
2777 {
2778         global $supported_collations;
2779
2780         echo "<tr>";
2781         if ($label != null)
2782                 echo "<td class='label'>$label</td>\n";
2783         echo "<td>";
2784
2785         echo array_selector($name, $selected_id, $supported_collations, 
2786                 array('select_submit'=> false) );
2787         echo "</td></tr>\n";
2788 }
2789
2790 function vat_areas_list_row($label, $name, $selected_id=null, $enabled=true)
2791 {
2792         global $tax_area_types;
2793
2794         echo "<tr>";
2795         if ($label != null)
2796                 echo "<td class='label'>$label</td>\n";
2797         echo "<td>";
2798
2799         echo array_selector($name, $selected_id, $tax_area_types,
2800                 array(
2801                         'select_submit'=> true,
2802                         'disabled' => !$enabled) );
2803         echo "</td></tr>\n";
2804 }
2805