Removed unneeded submit_on_change in uom selector.
[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/reserved.inc");
14 include_once($path_to_root . "/includes/types.inc");
15 include_once($path_to_root . "/includes/current_user.inc");
16
17 $_search_button = "<input %s type='submit' class='combo_submit' style='border:0;background:url($path_to_root/themes/"
18         ."%s/images/locate.png) no-repeat;%s' aspect='fallback' name='%s' value=' ' title='"._("Set filter")."'> ";
19
20 $_select_button = "<input %s type='submit' class='combo_select' style='border:0;background:url($path_to_root/themes/"
21         ."%s/images/button_ok.png) no-repeat;%s' aspect='fallback' name='%s' value=' ' title='"._("Select")."'> ";
22
23 $all_items = reserved_words::get_all();
24
25 //----------------------------------------------------------------------------
26 //      Universal sql combo generator
27 //      $sql must return selector values and selector texts in columns 0 & 1
28 //      Options are merged with defaults.
29
30 function combo_input($name, $selected_id, $sql, $valfield, $namefield,
31         $options=null)
32 {
33 global $Ajax;
34
35 $opts = array(          // default options
36         'where'=> array(),              // additional constraints
37         'order' => $namefield,  // list sort order
38                 // special option parameters
39         'spec_option'=>false,   // option text or false
40         'spec_id' => 0,         // option id
41                 // submit on select parameters
42         'default' => '', // default value when $_POST is not set
43         'select_submit' => false, //submit on select: true/false
44         'edit_submit' => false, // call editor on F4
45         'async' => true,        // select update via ajax (true) vs _page_body reload
46                 // search box parameters
47         'sel_hint' => null,
48         'search_box' => false,  // name or true/false
49         'type' => 0,    // type of extended selector:
50                 // 0 - with (optional) visible search box, search by id
51                 // 1 - with hidden search box, search by option text
52                 // 2 - TODO reverse: box with hidden selector available via enter; this
53                 // would be convenient for optional ad hoc adding of new item
54         'search_submit' => true, //search submit button: true/false
55         'size' => 8,    // size and max of box tag
56         'max' => 50,
57         'cells' => false,       // combo displayed as 2 <td></td> cells
58         'search' => array(), // sql field names to search
59         'format' => null,        // format functions for regular options
60         'disabled' => false,
61         'box_hint' => null, // box/selectors hints; null = std see below
62         'category' => false     // category column name or false
63 );
64 // ------ merge options with defaults ----------
65         if($options != null)
66                 $opts = array_merge($opts, $options);
67         if (!is_array($opts['where']))  $opts['where'] = array($opts['where']);
68
69         $search_box = $opts['search_box']===true ? '_'.$name.'_edit' : $opts['search_box'];
70         $search_submit = $opts['search_submit']===true ? '_'.$name.'_button' : $opts['search_submit'];
71         $select_submit =  $opts['select_submit']===true ? '_'.$name.'_update' : $opts['select_submit'];
72         $spec_id = $opts['spec_id'];
73         $spec_option = $opts['spec_option'];
74         $by_id = ($opts['type'] == 0);
75         $class = $by_id ? 'combo':'combo2';
76         $disabled = $opts['disabled'] ? "disabled" : '';
77
78         if(!count($opts['search'])) {
79                 $opts['search'] = array($by_id ? $valfield : $namefield);
80         }
81         if ($opts['sel_hint'] === null) 
82                 $opts['sel_hint'] = $by_id || $search_box==false ?
83                         '' : _('Press Space tab for search pattern entry');
84
85         if ($opts['box_hint'] === null)
86                 $opts['box_hint'] = $search_box && $search_submit != false ?
87                         ($by_id ? _('Enter code fragment to search or * for all')
88                         : _('Enter description fragment to search or * for all')) :'';
89
90         if ($selected_id == null) {
91                 $selected_id = get_post($name, $opts['default']);
92         }
93         $txt = get_post($search_box);
94         $rel = '';
95         $limit = '';
96
97         if (isset($_POST[$select_submit])) {
98                 if ($by_id) $txt = $_POST[$name];
99
100                 if (!$opts['async'])
101                         $Ajax->activate('_page_body');
102                 else
103                         $Ajax->activate($name);
104         }
105         if ($search_box) {
106                 // search related sql modifications
107
108         $rel = "rel='$search_box'"; // set relation to list
109    if ($opts['search_submit']) {
110         if (isset($_POST[$search_submit])) {
111                 $selected_id = ''; // ignore selected_id while search
112                 if (!$opts['async'])
113                         $Ajax->activate('_page_body');
114                 else
115                         $Ajax->activate($name);
116         }
117         if ($txt == '') {
118                 if ($spec_option === false && $selected_id==null)
119                   $limit = ' LIMIT 1';
120                 else
121                   $opts['where'][] = $valfield . "='". get_post($name, $spec_id)."'";
122         }
123         else
124                 if ($txt != '*') {
125
126                 foreach($opts['search'] as $i=> $s)
127                         $opts['search'][$i] = $s . " LIKE '%{$txt}%'";
128                 $opts['where'][] = '('. implode($opts['search'], ' OR ') . ')';
129                 }
130         }
131    }
132         // sql completion
133         if (count($opts['where'])) {
134                 $where = strpos($sql, 'WHERE')==false ? ' WHERE ':' AND ';
135                 $where .= '('. implode($opts['where'], ' AND ') . ')';
136                 $group_pos = strpos($sql, 'GROUP BY');
137                 if ($group_pos) {
138                         $group = substr($sql, $group_pos);
139                         $sql = substr($sql, 0, $group_pos) . $where.' '.$group;
140                 } else {
141                         $sql .= $where;
142                 }
143         }
144         if ($opts['order'] != false) {
145                 if (!is_array($opts['order']))
146                         $opts['order'] = array($opts['order']);
147                 $sql .= ' ORDER BY '.implode(',',$opts['order']);
148         }
149
150         $sql .= $limit;
151         // ------ make selector ----------
152         $selector = $first_opt = '';
153         $first_id = false;
154         $found = false;
155         $lastcat = null;
156 //if($name=='stock_id') display_error($sql);
157         if($result = db_query($sql)) {
158                 while ($contact_row = db_fetch($result)) {
159                         $value = $contact_row[0];
160                         $descr = $opts['format']==null ?  $contact_row[1] :
161                         call_user_func($opts['format'], $contact_row);
162                         $sel = '';
163                         if (get_post($search_submit) && ($txt === $value)) {
164                                 $selected_id = $value;
165                         }
166                         if      ((string)($selected_id) === $value) {
167                                 $sel = 'selected';
168                                 $found = $value;
169                         }
170                         if ($first_id === false) {
171                                 $first_id = $value;
172                                 $first_opt = $descr;
173                         }
174                         $cat = $contact_row[$opts['category']];
175                         if ($opts['category'] !== false && $cat != $lastcat){
176                                 $selector .= "<optgroup label='".$cat."'>\n";
177                                 $lastcat = $cat;
178                         }
179                         $selector .= "<option $sel value='$value'>$descr</option>\n";
180                 }
181                 db_free_result($result);
182         }
183
184         // Prepend special option.
185         if ($spec_option !== false) { // if special option used - add it
186                 $first_id = $spec_id;
187                 $first_opt = $spec_option;
188                 $sel = $found===false ? 'selected' : '';
189                 $selector = "<option $sel value='$spec_id'>$spec_option</option>\n"
190                         . $selector;
191         }
192
193         if ($found === false) {
194                 $selected_id = $first_id;
195         }
196         $_POST[$name] = $selected_id;
197
198         if ($by_id && $search_box != false) {
199                 $txt = $found;
200                 $Ajax->addUpdate($name, $search_box, $txt ? $txt : '');
201         }
202         $aspect = $opts['edit_submit'] ? " aspect='editable'" : '';
203         $selector = "<select $disabled name='$name' class='$class' title='"
204                 . $opts['sel_hint']."'$aspect $rel>".$selector."</select>\n";
205
206         $Ajax->addUpdate($name, "_{$name}_sel", $selector);
207
208         // because of bug which M$ cannot fix since IE 5.0
209         // we must embed whole selector in span tags to enable proper ajax update
210         $selector = "<span id='_{$name}_sel'>".$selector."</span>\n";
211
212         if ($select_submit != false) { // if submit on change is used - add select button
213                 global $_select_button;
214         // button class selects form reload/ajax selector update
215                 $selector .= sprintf($_select_button, $disabled, user_theme(),
216                         (fallback_mode() ? '' : 'display:none;'),
217                         $select_submit)."\n";
218         }
219 // ------ make combo ----------
220
221         $edit_entry = '';
222         if ($search_box != false) {
223                 $edit_entry = "<input $disabled type='text' name='$search_box' id='$search_box' size='".
224                         $opts['size']."' maxlength='".$opts['max'].
225                         "' value='$txt' class='$class' rel='$name' autocomplete='off' title='"
226                         .$opts['box_hint']."'"
227                         .(!fallback_mode() && !$by_id ? " style=display:none;":'')
228                         .">\n";
229                 if ($search_submit != false) {
230                         global $_search_button;
231                         $edit_entry .= sprintf($_search_button, $disabled, user_theme(),
232                                 (fallback_mode() ? '' : 'display:none;'),
233                                 $search_submit)."\n";
234                 }
235         }
236         default_focus(($search_box && $by_id) ? $search_box : $name);
237         $str = $selector;
238         if ($search_box && $opts['cells'])
239                 echo ($edit_entry!='' ? "<td>$edit_entry</td>" : '')."<td>$selector</td>";
240         else
241                 echo $edit_entry.$selector;
242
243         return $str;
244 }
245
246 /*
247         Helper function.
248         Returns true if selector $name is subject to update.
249 */
250 function list_updated($name)
251 {
252         return isset($_POST['_'.$name.'_update']) || isset($_POST['_'.$name.'_button']);
253 }
254 //----------------------------------------------------------------------------------------------
255 //      Universal array combo generator
256 //      $items is array of options 'value' => 'description'
257 //      Options is reduced set of combo_selector options and is merged with defaults.
258
259 function array_selector($name, $selected_id, $items, $options=null)
260 {
261         global $Ajax;
262
263 $opts = array(          // default options
264         'spec_option'=>false,   // option text or false
265         'spec_id' => 0,         // option id
266         'select_submit' => false, //submit on select: true/false
267         'edit_submit' => false, // call editor on F4
268         'async' => true,        // select update via ajax (true) vs _page_body reload
269         'default' => '', // default value when $_POST is not set
270                 // search box parameters
271         'sel_hint' => null,
272         'disabled' => false
273 );
274 // ------ merge options with defaults ----------
275         if($options != null)
276                 $opts = array_merge($opts, $options);
277         $select_submit =  $opts['select_submit']===true ? '_'.$name.'_update' : $opts['select_submit'];
278         $spec_id = $opts['spec_id'];
279         $spec_option = $opts['spec_option'];
280         $disabled = $opts['disabled'] ? "disabled" : '';
281
282         if ($selected_id == null) {
283                 $selected_id = get_post($name, $opts['default']);
284         }
285
286         if (isset($_POST[$select_submit])) {
287                 if (!$opts['async'])
288                         $Ajax->activate('_page_body');
289                 else
290                         $Ajax->activate($name);
291         }
292
293         // ------ make selector ----------
294         $selector = $first_opt = '';
295         $first_id = false;
296         $found = false;
297 //if($name=='SelectStockFromList') display_error($sql);
298                 foreach($items as $value=>$descr) {
299                         $sel = '';
300                         if ((string)$selected_id === (string)$value) {
301                                 $sel = 'selected';
302                                 $found = $value;
303                         }
304                         if ($first_id === false) {
305                                 $first_id = $value;
306                                 $first_opt = $descr;
307                         }
308                         $selector .= "<option $sel value='$value'>$descr</option>\n";
309                 }
310
311         // Prepend special option.
312         if ($spec_option !== false) { // if special option used - add it
313                 $first_id = $spec_id;
314                 $first_opt = $spec_option;
315                 $sel = $found===false ? 'selected' : '';
316                 $selector = "<option $sel value='$spec_id'>$spec_option</option>\n"
317                         . $selector;
318         }
319
320         if ($found === false) {
321                 $selected_id = $first_id;
322         }
323         $_POST[$name] = $selected_id;
324
325         $aspect = $opts['edit_submit'] ? " aspect='editable'" : '';
326         $selector = "<select $disabled name='$name' class='combo' title='"
327                 . $opts['sel_hint']."'$aspect >".$selector."</select>\n";
328
329         $Ajax->addUpdate($name, "_{$name}_sel", $selector);
330
331         $selector = "<span id='_{$name}_sel'>".$selector."</span>\n";
332
333         if ($select_submit != false) { // if submit on change is used - add select button
334                 global $_select_button;
335                 $selector .= sprintf($_select_button, $disabled, user_theme(),
336                         (fallback_mode() ? '' : 'display:none;'),
337                         $select_submit)."\n";
338         }
339         default_focus($name);
340         echo $selector;
341
342         return $selector;
343 }
344 //----------------------------------------------------------------------------------------------
345
346 function _format_add_curr($row)
347 {
348         static $company_currency;
349
350         if ($company_currency == null)
351         {
352                 $company_currency = get_company_currency();
353         }
354         return $row[1] . ($row[2] == $company_currency ?
355                 '' : ("&nbsp;-&nbsp;" . $row[2]));
356 }
357
358 function supplier_list($name, $selected_id=null, $spec_option=false, $submit_on_change=false)
359 {
360         global $all_items;
361
362         $sql = "SELECT supplier_id, supp_name, curr_code FROM ".TB_PREF."suppliers ";
363
364         $mode = get_company_pref('no_supplier_list');
365
366         return combo_input($name, $selected_id, $sql, 'supplier_id', 'supp_name',
367         array(
368                 'format' => '_format_add_curr',
369                 'search_box' => $mode!=0,
370                 'edit_submit' => true, 
371                 'type' => 1,
372                 'spec_option' => $spec_option === true ? _("All Suppliers") : $spec_option,
373                 'spec_id' => $all_items,
374                 'select_submit'=> $submit_on_change,
375                 'async' => false,
376                 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment') :
377                 _('Select supplier')
378                 ));
379 }
380
381 function supplier_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
382 {
383         if ($label != null)
384                 echo "<td>$label</td><td>\n";
385         $str = supplier_list($name, $selected_id, $all_option, $submit_on_change);
386                 echo "</td>\n";
387         return $str;
388 }
389
390 function supplier_list_row($label, $name, $selected_id=null, $all_option = false, $submit_on_change=false)
391 {
392 echo "<tr><td>$label</td><td>";
393         $str = supplier_list($name, $selected_id, $all_option, $submit_on_change );
394 echo "</td></tr>\n";
395 return $str;
396 }
397 //----------------------------------------------------------------------------------------------
398
399 function customer_list($name, $selected_id=null, $spec_option=false, $submit_on_change=false)
400 {
401         global $all_items;
402
403         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
404
405         $mode = get_company_pref('no_customer_list');
406
407 return combo_input($name, $selected_id, $sql, 'debtor_no', 'name',
408         array(
409             'format' => '_format_add_curr',
410                 'search_box' => $mode!=0,
411                 'type' => 1,
412                 'size' => 20,
413                 'spec_option' => $spec_option === true ? _("All Customers") : $spec_option,
414                 'spec_id' => $all_items,
415                 'select_submit'=> $submit_on_change,
416                 'edit_submit' => true, // call editor on F4
417                 'async' => false,
418                 'sel_hint' => $mode ? _('Press Space tab to filter by name fragment; F4 - entry new customer') :
419                 _('Select customer')
420         ) );
421 }
422
423 function customer_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
424 {
425         if ($label != null)
426                 echo "<td>$label</td>\n";
427         echo "<td nowrap>";
428         $str = customer_list($name, $selected_id, $all_option, $submit_on_change);
429         echo "</td>\n";
430         return $str;
431 }
432
433 function customer_list_row($label, $name, $selected_id=null, $all_option = false, $submit_on_change=false)
434 {
435         echo "<tr><td>$label</td><td nowrap>";
436         $str = customer_list($name, $selected_id, $all_option, $submit_on_change);
437         echo "</td>\n</tr>\n";
438         return $str;
439 }
440
441 //------------------------------------------------------------------------------------------------
442
443 function customer_branches_list($customer_id, $name, $selected_id=null,
444         $spec_option = true, $enabled=true, $submit_on_change=false)
445 {
446         global $all_items;
447
448         $sql = "SELECT branch_code, br_name FROM ".TB_PREF."cust_branch
449                 WHERE debtor_no='" . $customer_id . "' ";
450
451         $where = $enabled ? array("disable_trans = 0") : array();
452 return  combo_input($name, $selected_id, $sql, 'branch_code', 'br_name',
453         array(
454                 'where' => $where,
455                 'spec_option' => $spec_option === true ? _('All branches') : $spec_option,
456                 'spec_id' => $all_items,
457                 'select_submit'=> $submit_on_change,
458                 'sel_hint' => _('Select customer branch')
459         ) );
460 }
461 //------------------------------------------------------------------------------------------------
462
463 function customer_branches_list_cells($label,$customer_id, $name, $selected_id=null, $all_option = true, $enabled=true, $submit_on_change=false)
464 {
465         if ($label != null)
466                 echo "<td>$label</td>\n";
467         echo "<td>";
468         $ret = customer_branches_list($customer_id, $name, $selected_id, $all_option, $enabled, $submit_on_change);
469         echo "</td>\n";
470         return $ret;
471 }
472
473 function customer_branches_list_row($label,$customer_id, $name, $selected_id=null, $all_option = true, $enabled=true, $submit_on_change=false)
474 {
475         echo "<tr>";
476         $ret = customer_branches_list_cells($label, $customer_id, $name, $selected_id, $all_option, $enabled, $submit_on_change);
477         echo "</tr>";
478         return $ret;
479 }
480
481 //------------------------------------------------------------------------------------------------
482
483 function locations_list($name, $selected_id=null, $all_option=false, $submit_on_change=false)
484 {
485         global $all_items;
486
487         $sql = "SELECT loc_code, location_name FROM ".TB_PREF."locations";
488
489 return combo_input($name, $selected_id, $sql, 'loc_code', 'location_name',
490         array(
491                 'spec_option' => $all_option === true ? _("All Locations") : $all_option,
492                 'spec_id' => $all_items,
493                 'select_submit'=> $submit_on_change
494         ) );
495 }
496
497 function locations_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
498 {
499         if ($label != null)
500                 echo "<td>$label</td>\n";
501         echo "<td>";
502         $str = locations_list($name, $selected_id, $all_option, $submit_on_change);
503         echo "</td>\n";
504         return $str;
505 }
506
507 function locations_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
508 {
509         echo "<tr>";
510         $str = locations_list_cells($label, $name, $selected_id, $all_option, $submit_on_change);
511         echo "</tr>\n";
512         return $str;
513 }
514
515 //-----------------------------------------------------------------------------------------------
516
517 function currencies_list($name, $selected_id=null, $submit_on_change=false)
518 {
519         $sql = "SELECT curr_abrev, currency FROM ".TB_PREF."currencies";
520
521 // default to the company currency
522
523 return combo_input($name, $selected_id, $sql, 'curr_abrev', 'currency',
524         array(
525                 'select_submit'=> $submit_on_change,
526                 'default' => get_company_currency(),
527                 'async' => false                
528         ) );
529 }
530
531 function currencies_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
532 {
533         if ($label != null)
534                 echo "<td>$label</td>\n";
535         echo "<td>";
536         $str = currencies_list($name, $selected_id, $submit_on_change);
537         echo "</td>\n";
538         return $str;
539 }
540
541 function currencies_list_row($label, $name, $selected_id=null, $submit_on_change=false)
542 {
543         echo "<tr>\n";
544         $str = currencies_list_cells($label, $name, $selected_id, $submit_on_change);
545         echo "</tr>\n";
546         return $str;
547 }
548
549 //---------------------------------------------------------------------------------------------------
550
551 function fiscalyears_list($name, $selected_id=null, $submit_on_change=false)
552 {
553
554         $sql = "SELECT * FROM ".TB_PREF."fiscal_year";
555
556 // default to the company current fiscal year
557
558 return combo_input($name, $selected_id, $sql, 'id', '',
559         array(
560                 'order' => 'begin',
561                 'default' => get_company_pref('f_year'),
562                 'format' => '_format_fiscalyears',
563                 'select_submit'=> $submit_on_change,
564                 'async' => false
565         ) );
566 }
567
568 function _format_fiscalyears($row)
569 {
570         return sql2date($row[1]) . "&nbsp;-&nbsp;" . sql2date($row[2])
571         . "&nbsp;&nbsp;" . ($row[3] ? _('Closed') : _('Active')) . "</option>\n";
572 }
573
574 function fiscalyears_list_cells($label, $name, $selected_id=null)
575 {
576         if ($label != null)
577                 echo "<td>$label</td>\n";
578         echo "<td>";
579         $str = fiscalyears_list($name, $selected_id);
580         echo "</td>\n";
581         return $str;
582 }
583
584 function fiscalyears_list_row($label, $name, $selected_id=null)
585 {
586         echo "<tr>\n";
587         $str = fiscalyears_list_cells($label, $name, $selected_id);
588         echo "</tr>\n";
589         return $str;
590 }
591 //------------------------------------------------------------------------------------
592
593 function dimensions_list($name, $selected_id=null, $no_option=false, $showname=' ',
594         $submit_on_change=false, $showclosed=false, $showtype=1)
595 {
596 $sql = "SELECT id, CONCAT(reference,'  ',name) as ref FROM ".TB_PREF."dimensions";
597
598 $options = array(
599         'order' => 'reference',
600         'spec_option'=>$no_option ? $showname : false,
601         'spec_id' => 0,
602         'select_submit'=> $submit_on_change,
603         'async' => false,
604         );
605
606         if (!$showclosed)
607                 $options['where'][] = "closed=0";
608         if($showtype)
609                 $options['where'][] = "type_=$showtype";
610
611         return combo_input($name, $selected_id, $sql, 'id', 'ref', $options);
612 }
613
614 function dimensions_list_cells($label, $name, $selected_id=null, $no_option=false, $showname=null,
615         $showclosed=false, $showtype=0, $submit_on_change=false)
616 {
617         if ($label != null)
618                 echo "<td>$label</td>\n";
619         echo "<td>";
620         $str = dimensions_list($name, $selected_id, $no_option, $showname, $submit_on_change, $showclosed, $showtype);
621         echo "</td>\n";
622         return $str;
623 }
624
625 function dimensions_list_row($label, $name, $selected_id=null, $no_option=false, $showname=null,
626         $showclosed=false, $showtype=0, $submit_on_change=false)
627 {
628         echo "<tr>\n";
629         $str = dimensions_list_cells($label, $name, $selected_id, $no_option, $showname,
630                 $showclosed, $showtype, $submit_on_change);
631         echo "</tr>\n";
632         return $str;
633 }
634
635 //---------------------------------------------------------------------------------------------------
636
637 function stock_items_list($name, $selected_id=null, $all_option=false, $submit_on_change=false, $opts=array())
638 {
639         global $all_items;
640
641         $sql = "SELECT stock_id, s.description, c.description
642                         FROM ".TB_PREF."stock_master s,".TB_PREF."stock_category c WHERE s.category_id=c.category_id";
643
644         return combo_input($name, $selected_id, $sql, 'stock_id', 's.description',
645         array_merge(
646           array(
647                 'format' => '_format_stock_items',
648                 'spec_option' => $all_option===true ?  _("All Items") : $all_option,
649                 'spec_id' => $all_items,
650                 'search_box' => true,
651                 'search' => array("stock_id", "c.description","s.description"),
652                 'search_submit' => get_company_pref('no_item_list')!=0,
653                 'size'=>10,
654                 'select_submit'=> $submit_on_change,
655                 'category' => 2,
656                 'order' => array('c.description','stock_id')
657           ), $opts) );
658 }
659
660 function _format_stock_items($row)
661 {
662         return (user_show_codes() ?  ($row[0] . "&nbsp;-&nbsp;") : "") . $row[1];
663 }
664
665 function stock_items_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
666 {
667         if ($label != null)
668                 echo "<td>$label</td>\n";
669         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
670                 array('cells'=>true));
671         return $str;
672 }
673 /*
674 function stock_items_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
675 {
676         echo "<tr>\n";
677         $str = stock_items_list_cells($label, $name, $selected_id, $all_option, $submit_on_change);
678         echo "</tr>\n";
679         return $str;
680 }
681 */
682 //---------------------------------------------------------------------------------------------------
683 //
684 // Select item via foreign code.
685 //
686 function sales_items_list($name, $selected_id=null, $all_option=false, 
687         $submit_on_change=false, $type='', $opts=array())
688 {
689         global $all_items;
690         // all sales codes
691         $sql = "SELECT i.item_code, i.description, c.description, count(*)>1 as kit
692                         FROM
693                         ".TB_PREF."item_codes i
694                         LEFT JOIN
695                         ".TB_PREF."stock_category c
696                         ON i.category_id=c.category_id";
697
698         if ($type == 'local')   { // exclude foreign codes
699                 $sql .= " WHERE !i.is_foreign"; 
700         } elseif ($type == 'kits') { // sales kits
701                 $sql .= " WHERE !i.is_foreign AND i.item_code!=i.stock_id";
702         }
703                                 
704         $sql .= " GROUP BY i.item_code";
705
706         return combo_input($name, $selected_id, $sql, 'i.item_code', 'c.description',
707         array_merge(
708           array(
709                 'format' => '_format_stock_items',
710                 'spec_option' => $all_option===true ?  _("All Items") : $all_option,
711                 'spec_id' => $all_items,
712                 'search_box' => true,
713                 'search' => array("i.item_code", "i.description"),
714                 'search_submit' => get_company_pref('no_item_list')!=0,
715                 'size'=>15,
716                 'select_submit'=> $submit_on_change,
717                 'category' => 2,
718                 'order' => array('c.description','i.item_code')
719           ), $opts) );
720 }
721
722 function sales_items_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
723 {
724         if ($label != null)
725                 echo "<td>$label</td>\n";
726         $str = sales_items_list($name, $selected_id, $all_option, $submit_on_change,
727                 '', array('cells'=>true));
728         return $str;
729 }
730
731 function sales_kits_list($name, $selected_id=null, $all_option=false, $submit_on_change=false)
732 {
733         $str = sales_items_list($name, $selected_id, $all_option, $submit_on_change,
734                 'kits', array('cells'=>false));
735         return $str;
736 }
737
738 function sales_local_items_list_row($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false)
739 {
740         echo "<tr>";
741         if ($label != null)
742                 echo "<td>$label</td>\n";
743         echo "<td>";
744         $str = sales_items_list($name, $selected_id, $all_option, $submit_on_change,
745                 'local', array('cells'=>false));
746         echo "</td></tr>";
747         return $str;
748 }
749 //------------------------------------------------------------------------------------
750
751 function stock_manufactured_items_list($name, $selected_id=null,
752         $all_option=false, $submit_on_change=false)
753 {
754         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
755                 array('where'=>array("mb_flag= 'M'"), 
756                         'edit_submit' => true));
757         return $str;            
758 }
759
760 function stock_manufactured_items_list_cells($label, $name, $selected_id=null,
761                                 $all_option=false, $submit_on_change=false)
762 {
763         if ($label != null)
764                 echo "<td>$label</td>\n";
765         echo "<td>";
766         $str = stock_manufactured_items_list($name, $selected_id, $all_option, $submit_on_change);
767         echo "</td>\n";
768         return $str;
769 }
770
771 function stock_manufactured_items_list_row($label, $name, $selected_id=null,
772                 $all_option=false, $submit_on_change=false)
773 {
774         echo "<tr>\n";
775         $str = stock_manufactured_items_list_cells($label, $name, $selected_id, $all_option, $submit_on_change);
776         echo "</tr>\n";
777         return $str;
778 }
779 //------------------------------------------------------------------------------------
780
781 function stock_component_items_list($name, $parent_stock_id, $selected_id=null,
782         $all_option=false, $submit_on_change=false)
783 {
784         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
785                 array('where'=>array("stock_id != '$parent_stock_id'")));
786         return $str;
787 }
788
789 function stock_component_items_list_cells($label, $name, $parent_stock_id, 
790         $selected_id=null, $all_option=false, $submit_on_change=false)
791 {
792         if ($label != null)
793                 echo "<td>$label</td>\n";
794         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
795                 array('where'=>array("stock_id != '$parent_stock_id'"), 'cells'=>true));
796         return $str;
797 }
798 //------------------------------------------------------------------------------------
799
800 function stock_costable_items_list($name, $selected_id=null,
801         $all_option=false, $submit_on_change=false)
802 {
803         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
804                 array('where'=>array("mb_flag!='D'")));
805         return $str;
806 }
807
808 function stock_costable_items_list_cells($label, $name, $selected_id=null, 
809         $all_option=false, $submit_on_change=false)
810 {
811         if ($label != null)
812                 echo "<td>$label</td>\n";
813         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
814                 array('where'=>array("mb_flag!='D'"), 'cells'=>true));
815         return $str;
816 }
817
818 //------------------------------------------------------------------------------------
819 function stock_purchasable_items_list($name, $selected_id=null, 
820         $all_option=false, $submit_on_change=false)
821 {
822         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
823                 array('where'=>array("mb_flag!= 'M'"), 
824                         'edit_submit' => true));
825         return $str;
826 }
827
828 function stock_purchasable_items_list_cells($label, $name, $selected_id=null,
829                         $all_option=false, $submit_on_change=false)
830 {
831         if ($label != null)
832                 echo "<td>$label</td>\n";
833         $str = stock_items_list($name, $selected_id, $all_option, $submit_on_change,
834                 array('where'=>array("mb_flag!= 'M'"), 
835                          'edit_submit' => true,
836                          'cells'=>true));
837         return $str;
838 }
839
840 function stock_purchasable_items_list_row($label, $name, $selected_id=null,
841                         $all_option=false, $submit_on_change=false)
842 {
843         echo "<tr>\n";
844         $ret = stock_purchasable_items_list_cells($label, $name, $selected_id=null,
845                 $all_option, $submit_on_change);
846         echo "</tr>\n";
847         return $ret;
848 }
849
850 //------------------------------------------------------------------------------------
851
852 function stock_item_types_list_row($label, $name, $selected_id=null, $enabled=true)
853 {
854         $types = array(
855                 'M' => _("Manufactured"),
856                 'B' => _("Purchased"),
857                 'D' => _("Service")
858         );
859
860         echo "<tr>";
861         if ($label != null)
862                 echo "<td>$label</td>\n";
863         echo "<td>";
864
865         array_selector($name, $selected_id, $types, 
866                 array( 
867                         'select_submit'=> true, 
868                         'disabled' => !$enabled) );
869         echo "</td></tr>\n";
870 }
871
872 function stock_units_list_row($label, $name, $value=null, $enabled=true)
873 {
874         $result = get_all_item_units();
875         echo "<tr>";
876         if ($label != null)
877                 echo "<td>$label</td>\n";
878         echo "<td>";
879
880         while($unit = db_fetch($result))
881                 $units[$unit['abbr']] = $unit['name'];
882
883         array_selector($name, $value, $units, array( 'disabled' => !$enabled) );
884
885         echo "</td></tr>\n";
886 }
887
888 //------------------------------------------------------------------------------------
889
890 function tax_types_list($name, $selected_id=null, $none_option=false, $submit_on_change=false)
891 {
892         $sql = "SELECT id, CONCAT(name, ' (',rate,'%)') as name FROM ".TB_PREF."tax_types";
893
894         return combo_input($name, $selected_id, $sql, 'id', 'name',
895         array(
896                 'spec_option' => $none_option,
897                 'spec_id' => reserved_words::get_all_numeric(),
898                 'select_submit'=> $submit_on_change,
899                 'async' => false,
900         ) );
901 }
902
903 function tax_types_list_cells($label, $name, $selected_id=null, $none_option=false,
904         $submit_on_change=false)
905 {
906         if ($label != null)
907                 echo "<td>$label</td>\n";
908         echo "<td>";
909         $str = tax_types_list($name, $selected_id, $none_option, $submit_on_change);
910         echo "</td>\n";
911         return $str;
912 }
913
914 function tax_types_list_row($label, $name, $selected_id=null, $none_option=false,
915         $submit_on_change=false)
916 {
917         echo "<tr>\n";
918         $str = tax_types_list_cells($label, $name, $selected_id, $none_option, $submit_on_change);
919         echo "</tr>\n";
920         return $str;
921 }
922
923 //------------------------------------------------------------------------------------
924
925 function tax_groups_list($name, $selected_id=null,
926         $none_option=false, $submit_on_change=false)
927 {
928         $sql = "SELECT id, name FROM ".TB_PREF."tax_groups";
929
930         return combo_input($name, $selected_id, $sql, 'id', 'name',
931         array(
932                 'order' => 'id',
933                 'spec_option' => $none_option,
934                 'spec_id' => reserved_words::get_all_numeric(),
935                 'select_submit'=> $submit_on_change,
936                 'async' => false,
937         ) );
938 }
939
940 function tax_groups_list_cells($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
941 {
942         if ($label != null)
943                 echo "<td>$label</td>\n";
944         echo "<td>";
945         $str = tax_groups_list($name, $selected_id, $none_option, $submit_on_change);
946         echo "</td>\n";
947         return $str;
948 }
949
950 function tax_groups_list_row($label, $name, $selected_id=null, $none_option=false, $submit_on_change=false)
951 {
952         echo "<tr>\n";
953         $str = tax_groups_list_cells($label, $name, $selected_id, $none_option, $submit_on_change);
954         echo "</tr>\n";
955         return $str;
956 }
957
958 //------------------------------------------------------------------------------------
959
960 function item_tax_types_list($name, $selected_id=null)
961 {
962         $sql ="SELECT id, name FROM ".TB_PREF."item_tax_types";
963         combo_input($name, $selected_id, $sql, 'id', 'name', array('order' => 'id') );
964 }
965
966 function item_tax_types_list_cells($label, $name, $selected_id=null)
967 {
968         if ($label != null)
969                 echo "<td>$label</td>\n";
970         echo "<td>";
971         item_tax_types_list($name, $selected_id);
972         echo "</td>\n";
973 }
974
975 function item_tax_types_list_row($label, $name, $selected_id=null)
976 {
977         echo "<tr>\n";
978         item_tax_types_list_cells($label, $name, $selected_id);
979         echo "</tr>\n";
980 }
981
982 //------------------------------------------------------------------------------------
983
984 function shippers_list($name, $selected_id=null)
985 {
986         $sql = "SELECT shipper_id, shipper_name FROM ".TB_PREF."shippers";
987         combo_input($name, $selected_id, $sql, 'shipper_id', 'shipper_name', array());
988 }
989
990 function shippers_list_cells($label, $name, $selected_id=null)
991 {
992         if ($label != null)
993                 echo "<td>$label</td>\n";
994         echo "<td>";
995         shippers_list($name, $selected_id);
996         echo "</td>\n";
997 }
998
999 function shippers_list_row($label, $name, $selected_id=null)
1000 {
1001         echo "<tr>\n";
1002         shippers_list_cells($label, $name, $selected_id);
1003         echo "</tr>\n";
1004 }
1005
1006 //-------------------------------------------------------------------------------------
1007
1008 function sales_persons_list($name, $selected_id=null)
1009 {
1010         $sql = "SELECT salesman_code, salesman_name FROM ".TB_PREF."salesman";
1011         combo_input($name, $selected_id, $sql, 'salesman_code', 'salesman_name', array());
1012 }
1013
1014 function sales_persons_list_cells($label, $name, $selected_id=null)
1015 {
1016         if ($label != null)
1017                 echo "<td>$label</td>\n";
1018         echo "<td>\n";
1019         sales_persons_list($name, $selected_id);
1020         echo "</td>\n";
1021 }
1022
1023 function sales_persons_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1024 {
1025         echo "<tr>\n";
1026         sales_persons_list_cells($label, $name, $selected_id, $submit_on_change=false);
1027         echo "</tr>\n";
1028 }
1029
1030 //------------------------------------------------------------------------------------
1031
1032 function sales_areas_list($name, $selected_id=null)
1033 {
1034         $sql = "SELECT area_code, description FROM ".TB_PREF."areas";
1035         combo_input($name, $selected_id, $sql, 'area_code', 'description', array());
1036 }
1037
1038 function sales_areas_list_cells($label, $name, $selected_id=null)
1039 {
1040         if ($label != null)
1041                 echo "<td>$label</td>\n";
1042         echo "<td>";
1043         sales_areas_list($name, $selected_id);
1044         echo "</td>\n";
1045 }
1046
1047 function sales_areas_list_row($label, $name, $selected_id=null)
1048 {
1049         echo "<tr>\n";
1050         sales_areas_list_cells($label, $name, $selected_id);
1051         echo "</tr>\n";
1052 }
1053
1054 //------------------------------------------------------------------------------------
1055
1056 function sales_groups_list($name, $selected_id=null, $special_option=false)
1057 {
1058         $sql = "SELECT id, description FROM ".TB_PREF."groups";
1059         combo_input($name, $selected_id, $sql, 'id', 'description', array(
1060                 'spec_option' => $special_option===true ? ' ' : $special_option,
1061                 'order' => 'description', 'spec_id' => 0,
1062         ));
1063 }
1064
1065 function sales_groups_list_cells($label, $name, $selected_id=null, $special_option=false)
1066 {
1067         if ($label != null)
1068                 echo "<td>$label</td>\n";
1069         echo "<td>";
1070         sales_groups_list($name, $selected_id, $special_option);
1071         echo "</td>\n";
1072 }
1073
1074 function sales_groups_list_row($label, $name, $selected_id=null, $special_option=false)
1075 {
1076         echo "<tr>\n";
1077         sales_groups_list_cells($label, $name, $selected_id, $special_option);
1078         echo "</tr>\n";
1079 }
1080
1081 //------------------------------------------------------------------------------------
1082
1083 function _format_template_items($row)
1084 {
1085         return ($row[0] . "&nbsp;- &nbsp;" . _("Amount") . "&nbsp;".$row[1]);
1086 }
1087
1088 function templates_list($name, $selected_id=null, $special_option=false)
1089 {
1090         $sql = "SELECT sorder.order_no, Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue
1091                 FROM ".TB_PREF."sales_orders as sorder, ".TB_PREF."sales_order_details as line
1092                 WHERE sorder.order_no = line.order_no AND sorder.type = 1 GROUP BY line.order_no";
1093         combo_input($name, $selected_id, $sql, 'order_no', 'OrderValue', array(
1094                 'format' => '_format_template_items',
1095                 'spec_option' => $special_option===true ? ' ' : $special_option,
1096                 'order' => 'order_no', 'spec_id' => 0,
1097         ));
1098 }
1099
1100 function templates_list_cells($label, $name, $selected_id=null, $special_option=false)
1101 {
1102         if ($label != null)
1103                 echo "<td>$label</td>\n";
1104         echo "<td>";
1105         templates_list($name, $selected_id, $special_option);
1106         echo "</td>\n";
1107 }
1108
1109 function templates_list_row($label, $name, $selected_id=null, $special_option=false)
1110 {
1111         echo "<tr>\n";
1112         templates_list_cells($label, $name, $selected_id, $special_option);
1113         echo "</tr>\n";
1114 }
1115
1116 //------------------------------------------------------------------------------------
1117
1118 function workorders_list($name, $selected_id=null)
1119 {
1120         $sql = "SELECT id, wo_ref FROM ".TB_PREF."workorders WHERE closed=0";
1121         combo_input($name, $selected_id, $sql, 'id', 'wo_ref', array());
1122 }
1123
1124 function workorders_list_cells($label, $name, $selected_id=null)
1125 {
1126         if ($label != null)
1127                 echo "<td>$label</td>\n";
1128         echo "<td>";
1129         workorders_list($name, $selected_id);
1130         echo "</td>\n";
1131 }
1132
1133 function workorders_list_row($label, $name, $selected_id=null)
1134 {
1135         echo "<tr>\n";
1136         workorders_list_cells($label, $name, $selected_id);
1137         echo "</tr>\n";
1138 }
1139
1140 //------------------------------------------------------------------------------------
1141
1142 function payment_terms_list($name, $selected_id=null)
1143 {
1144         $sql = "SELECT terms_indicator, terms FROM ".TB_PREF."payment_terms";
1145         combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms', array());
1146 }
1147
1148 function payment_terms_list_cells($label, $name, $selected_id=null)
1149 {
1150         if ($label != null)
1151                 echo "<td>$label</td>\n";
1152         echo "<td>";
1153         payment_terms_list($name, $selected_id);
1154         echo "</td>\n";
1155 }
1156
1157 function payment_terms_list_row($label, $name, $selected_id=null)
1158 {
1159         echo "<tr>\n";
1160         payment_terms_list_cells($label, $name, $selected_id);
1161         echo "</tr>\n";
1162 }
1163
1164 //------------------------------------------------------------------------------------
1165
1166 function credit_status_list($name, $selected_id=null)
1167 {
1168         $sql ="SELECT id, reason_description FROM ".TB_PREF."credit_status";
1169         combo_input($name, $selected_id, $sql, 'id', 'reason_description', array());
1170 }
1171
1172 function credit_status_list_cells($label, $name, $selected_id=null)
1173 {
1174         if ($label != null)
1175                 echo "<td>$label</td>\n";
1176         echo "<td>";
1177         credit_status_list($name, $selected_id);
1178         echo "</td>\n";
1179 }
1180
1181 function credit_status_list_row($label, $name, $selected_id=null)
1182 {
1183         echo "<tr>\n";
1184         credit_status_list_cells($label, $name, $selected_id);
1185         echo "</tr>\n";
1186 }
1187
1188 //-----------------------------------------------------------------------------------------------
1189
1190 function sales_types_list($name, $selected_id=null, $submit_on_change=false, $special_option=false)
1191 {
1192         $sql = "SELECT id, sales_type FROM ".TB_PREF."sales_types";
1193
1194         return combo_input($name, $selected_id, $sql, 'id', 'sales_type',
1195         array(
1196                 'spec_option' => $special_option===true ? _("All Sales Types") : $special_option,
1197                 'spec_id' => 0,
1198                 'select_submit'=> $submit_on_change,
1199         //        'async' => false,
1200         ) );
1201 }
1202
1203 function sales_types_list_cells($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1204 {
1205         if ($label != null)
1206                 echo "<td>$label</td>\n";
1207         echo "<td>";
1208         $str = sales_types_list($name, $selected_id, $submit_on_change, $special_option);
1209         echo "</td>\n";
1210         return $str;
1211 }
1212
1213 function sales_types_list_row($label, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1214 {
1215         echo "<tr>\n";
1216         $str = sales_types_list_cells($label, $name, $selected_id, $submit_on_change, $special_option);
1217         echo "</tr>\n";
1218         return $str;
1219 }
1220
1221 //-----------------------------------------------------------------------------------------------
1222
1223 function movement_types_list($name, $selected_id=null)
1224 {
1225         $sql = "SELECT id, name FROM ".TB_PREF."movement_types";
1226         combo_input($name, $selected_id, $sql, 'id', 'name', array());
1227 }
1228
1229 function movement_types_list_cells($label, $name, $selected_id=null)
1230 {
1231         if ($label != null)
1232                 echo "<td>$label</td>\n";
1233         echo "<td>";
1234         movement_types_list($name, $selected_id);
1235         echo "</td>\n";
1236 }
1237
1238 function movement_types_list_row($label, $name, $selected_id=null)
1239 {
1240         echo "<tr>\n";
1241         movement_types_list_cells($label, $name, $selected_id);
1242         echo "</tr>\n";
1243 }
1244
1245 //-----------------------------------------------------------------------------------------------
1246 function _format_date($row)
1247 {
1248         return sql2date($row['reconciled']);
1249 }
1250
1251 function bank_reconciliation_list($account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1252 {
1253         $sql = "SELECT reconciled, reconciled FROM ".TB_PREF."bank_trans
1254                 WHERE bank_act=".db_escape($account)." AND reconciled IS NOT NULL
1255                 GROUP BY reconciled";
1256         combo_input($name, $selected_id, $sql, 'id', 'reconciled',
1257         array(
1258                 'spec_option' => $special_option,
1259                 'format' => '_format_date',
1260                 'spec_id' => '',
1261                 'select_submit'=> $submit_on_change
1262         ) );
1263 }
1264
1265 function bank_reconciliation_list_cells($label,$account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1266 {
1267         if ($label != null)
1268                 echo "<td>$label</td>\n";
1269         echo "<td>";
1270         bank_reconciliation_list($account, $name, $selected_id, $submit_on_change, $special_option);
1271         echo "</td>\n";
1272 }
1273 /*
1274 function bank_reconciliation_list_row($label, $account, $name, $selected_id=null, $submit_on_change=false, $special_option=false)
1275 {
1276         echo "<tr>\n";
1277         bank_reconciliation_list_cells($label, $account, $name, $selected_id, $submit_on_change, $special_option);
1278         echo "</tr>\n";
1279 }
1280 */
1281 //-----------------------------------------------------------------------------------------------
1282
1283 function workcenter_list($name, $selected_id=null, $all_option=false)
1284 {
1285         global $all_items;
1286
1287         $sql = "SELECT id, name FROM ".TB_PREF."workcentres";
1288
1289         return combo_input($name, $selected_id, $sql, 'id', 'name',
1290         array(
1291                 'spec_option' =>$all_option===true ? _("All Suppliers") : $all_option,
1292                 'spec_id' => $all_items,
1293         ) );
1294 }
1295
1296 function workcenter_list_cells($label, $name, $selected_id=null, $all_option=false)
1297 {
1298         default_focus($name);
1299         if ($label != null)
1300                 echo "<td>$label</td>\n";
1301         echo "<td>";
1302         workcenter_list($name, $selected_id, $all_option);
1303         echo "</td>\n";
1304 }
1305
1306 function workcenter_list_row($label, $name, $selected_id=null, $all_option=false)
1307 {
1308         echo "<tr>\n";
1309         workcenter_list_cells($label, $name, $selected_id, $all_option);
1310         echo "</tr>\n";
1311 }
1312
1313 //-----------------------------------------------------------------------------------------------
1314
1315 function bank_accounts_list($name, $selected_id=null, $submit_on_change=false)
1316 {
1317         $sql = "SELECT ".TB_PREF."bank_accounts.id, bank_account_name, bank_curr_code
1318                 FROM ".TB_PREF."bank_accounts";
1319 //              , ".TB_PREF."chart_master
1320 //              WHERE ".TB_PREF."bank_accounts.account_code=".TB_PREF."chart_master.account_code";
1321
1322         return combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1323         array(
1324                 'format' => '_format_add_curr',
1325                 'select_submit'=> $submit_on_change,
1326                 'async' => false
1327         ) );
1328 }
1329
1330 function bank_accounts_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1331 {
1332         if ($label != null)
1333                 echo "<td>$label</td>\n";
1334         echo "<td>";
1335         $str = bank_accounts_list($name, $selected_id, $submit_on_change);
1336         echo "</td>\n";
1337         return $str;
1338 }
1339
1340 function bank_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1341 {
1342         echo "<tr>\n";
1343         $str = bank_accounts_list_cells($label, $name, $selected_id, $submit_on_change);
1344         echo "</tr>\n";
1345         return $str;
1346 }
1347 //-----------------------------------------------------------------------------------------------
1348
1349 function cash_accounts_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1350 {
1351         $sql = "SELECT ".TB_PREF."bank_accounts.id, bank_account_name, bank_curr_code
1352                 FROM ".TB_PREF."bank_accounts
1353                 WHERE ".TB_PREF."bank_accounts.account_type=3";
1354
1355         if ($label != null)
1356                 echo "<tr><td>$label</td>\n";
1357         echo "<td>";
1358         $str = combo_input($name, $selected_id, $sql, 'id', 'bank_account_name',
1359         array(
1360                 'format' => '_format_add_curr',
1361                 'select_submit'=> $submit_on_change,
1362                 'async' => true
1363         ) );
1364         echo "</td></tr>\n";
1365         return $str;
1366 }
1367 //-----------------------------------------------------------------------------------------------
1368
1369 function pos_list_row($label, $name, $selected_id=null, $spec_option=false, $submit_on_change=false)
1370 {
1371         $sql = "SELECT id, pos_name FROM ".TB_PREF."sales_pos";
1372
1373         default_focus($name);
1374         echo '<tr>';
1375         if ($label != null)
1376                 echo "<td>$label</td>\n";
1377         echo "<td>";
1378
1379         $str = combo_input($name, $selected_id, $sql, 'id', 'pos_name',
1380         array(
1381                 'select_submit'=> $submit_on_change,
1382                 'async' => true,
1383                 'spec_option' =>$spec_option,
1384                 'spec_id' => -1,
1385         ) );
1386         echo "</td></tr>\n";
1387
1388         return $str;    
1389 }
1390 //-----------------------------------------------------------------------------------------------
1391
1392 function sale_payment_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1393 {
1394         if ($label != null)
1395                 echo "<td>$label</td>\n";
1396         echo "<td>";
1397         $str = yesno_list($name, $selected_id, _('Cash'), _('Delayed'), $submit_on_change);
1398         echo "</td>\n";
1399         return $str;
1400 }
1401 //-----------------------------------------------------------------------------------------------
1402
1403 function class_list($name, $selected_id=null, $submit_on_change=false)
1404 {
1405         $sql = "SELECT cid, class_name FROM ".TB_PREF."chart_class";
1406
1407         return combo_input($name, $selected_id, $sql, 'cid', 'class_name',
1408         array(
1409                 'select_submit'=> $submit_on_change,
1410                 'async' => false
1411         ) );
1412
1413 }
1414
1415 function class_list_cells($label, $name, $selected_id=null, $submit_on_change=false)
1416 {
1417         if ($label != null)
1418                 echo "<td>$label</td>\n";
1419         echo "<td>";
1420         $str = class_list($name, $selected_id, $submit_on_change);
1421         echo "</td>\n";
1422         return $str;
1423 }
1424
1425 function class_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1426 {
1427         echo "<tr>\n";
1428         $str = class_list_cells($label, $name, $selected_id, $submit_on_change);
1429         echo "</tr>\n";
1430         return $str;
1431 }
1432
1433 //-----------------------------------------------------------------------------------------------
1434
1435 function stock_categories_list($name, $selected_id=null)
1436 {
1437         $sql = "SELECT category_id, description FROM ".TB_PREF."stock_category";
1438         combo_input($name, $selected_id, $sql, 'category_id', 'description',
1439         array('order'=>'category_id'));
1440 }
1441
1442 function stock_categories_list_cells($label, $name, $selected_id=null)
1443 {
1444         if ($label != null)
1445                 echo "<td>$label</td>\n";
1446         echo "<td>";
1447         stock_categories_list($name, $selected_id);
1448         echo "</td>\n";
1449 }
1450
1451 function stock_categories_list_row($label, $name, $selected_id=null)
1452 {
1453         echo "<tr>\n";
1454         stock_categories_list_cells($label, $name, $selected_id);
1455         echo "</tr>\n";
1456 }
1457
1458 //-----------------------------------------------------------------------------------------------
1459
1460 function gl_account_types_list($name, $selected_id=null, $all_option, $all_option_numeric)
1461 {
1462         global $all_items;
1463
1464         $sql = "SELECT id, name FROM ".TB_PREF."chart_types";
1465
1466         combo_input($name, $selected_id, $sql, 'id', 'name',
1467         array(
1468                 'order' => 'id',
1469                 'spec_option' =>$all_option,
1470                 'spec_id' => $all_option_numeric ? 0 : $all_items
1471         ) );
1472 }
1473
1474 function gl_account_types_list_cells($label, $name, $selected_id=null, $all_option=false,
1475         $all_option_numeric=false)
1476 {
1477         if ($label != null)
1478                 echo "<td>$label</td>\n";
1479         echo "<td>";
1480         gl_account_types_list($name, $selected_id, $all_option, $all_option_numeric);
1481         echo "</td>\n";
1482 }
1483
1484 function gl_account_types_list_row($label, $name, $selected_id=null, $all_option=false,
1485         $all_option_numeric=false)
1486 {
1487         echo "<tr>\n";
1488         gl_account_types_list_cells($label, $name, $selected_id, $all_option,
1489                 $all_option_numeric);
1490         echo "</tr>\n";
1491 }
1492
1493 //-----------------------------------------------------------------------------------------------
1494 function gl_all_accounts_list($name, $selected_id=null, $skip_bank_accounts=false,
1495         $cells=false, $all_option=false, $submit_on_change=false)
1496 {
1497         if ($skip_bank_accounts)
1498                 $sql = "SELECT chart.account_code, chart.account_name, type.name
1499                         FROM (".TB_PREF."chart_master chart,".TB_PREF."chart_types type) "
1500                         ."LEFT JOIN ".TB_PREF."bank_accounts acc "
1501                         ."ON chart.account_code=acc.account_code
1502                                 WHERE acc.account_code  IS NULL
1503                         AND chart.account_type=type.id";
1504         else
1505                 $sql = "SELECT chart.account_code, chart.account_name, type.name
1506                         FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type
1507                         WHERE chart.account_type=type.id";
1508
1509         combo_input($name, $selected_id, $sql, 'chart.account_code', 'chart.account_name',
1510         array(
1511                 'format' => '_format_account',
1512                 'spec_option' => $all_option===true ?  _("Use Item Sales Accounts") : $all_option,
1513                 'spec_id' => '',
1514                 'order' => array('name','account_code'),
1515                 'search_box' => $cells,
1516                         'search_submit' => false,
1517                         'size' => 12,
1518                         'max' => 10,
1519                         'cells' => true,
1520                 'select_submit'=> $submit_on_change,
1521                 'async' => false,
1522                 'category' => 2
1523         ) );
1524
1525 }
1526
1527 function _format_account($row)
1528 {
1529                 return $row[0] .  "&nbsp;&nbsp;&nbsp;&nbsp;" . $row[1];
1530 }
1531
1532 function gl_all_accounts_list_cells($label, $name, $selected_id=null, 
1533         $skip_bank_accounts=false, $cells=false, $all_option=false)
1534 {
1535         if ($label != null)
1536                 echo "<td>$label</td>\n";
1537         echo "<td>";
1538         gl_all_accounts_list($name, $selected_id, 
1539                 $skip_bank_accounts, $cells, $all_option);
1540         echo "</td>\n";
1541 }
1542
1543 function gl_all_accounts_list_row($label, $name, $selected_id=null, 
1544         $skip_bank_accounts=false, $cells=false, $all_option=false)
1545 {
1546         echo "<tr>\n";
1547         gl_all_accounts_list_cells($label, $name, $selected_id, 
1548                 $skip_bank_accounts, $cells, $all_option);
1549         echo "</tr>\n";
1550 }
1551
1552 function yesno_list($name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1553 {
1554         $items = array();
1555         $items['0'] = strlen($name_no) ? $name_no : _("No");
1556         $items['1'] = strlen($name_yes) ? $name_yes : _("Yes");
1557
1558         return array_selector($name, $selected_id, $items, 
1559                 array( 
1560                         'select_submit'=> $submit_on_change,
1561                         'async' => false ) ); // FIX?
1562 }
1563
1564 function yesno_list_cells($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1565 {
1566         if ($label != null)
1567                 echo "<td>$label</td>\n";
1568         echo "<td>";
1569         $str = yesno_list($name, $selected_id, $name_yes, $name_no, $submit_on_change);
1570         echo "</td>\n";
1571         return $str;
1572 }
1573
1574 function yesno_list_row($label, $name, $selected_id=null, $name_yes="", $name_no="", $submit_on_change=false)
1575 {
1576         echo "<tr>\n";
1577         $str = yesno_list_cells($label, $name, $selected_id, $name_yes, $name_no, $submit_on_change);
1578         echo "</tr>\n";
1579         return $str;
1580 }
1581
1582 //------------------------------------------------------------------------------------------------
1583
1584 function languages_list($name, $selected_id=null)
1585 {
1586         global $installed_languages;
1587
1588         $items = array();
1589         foreach ($installed_languages as $lang)
1590                         $items[$lang['code']] = $lang['name'];
1591         
1592         return array_selector($name, $selected_id, $items );
1593 }
1594
1595 function languages_list_cells($label, $name, $selected_id=null)
1596 {
1597         if ($label != null)
1598                 echo "<td>$label</td>\n";
1599         echo "<td>";
1600         languages_list($name, $selected_id);
1601         echo "</td>\n";
1602 }
1603
1604 function languages_list_row($label, $name, $selected_id=null)
1605 {
1606         echo "<tr>\n";
1607         languages_list_cells($label, $name, $selected_id);
1608         echo "</tr>\n";
1609 }
1610
1611 //------------------------------------------------------------------------------------------------
1612
1613 function bank_account_types_list($name, $selected_id=null)
1614 {
1615         $types = bank_account_types::get_all();
1616
1617         $items = array();
1618         foreach ($types as $type)
1619         {
1620                         $items[$type['id']] = $type['name'];
1621         }
1622         
1623         return array_selector($name, $selected_id, $items );
1624 }
1625
1626 function bank_account_types_list_cells($label, $name, $selected_id=null)
1627 {
1628         if ($label != null)
1629                 echo "<td>$label</td>\n";
1630         echo "<td>";
1631         bank_account_types_list($name, $selected_id);
1632         echo "</td>\n";
1633 }
1634
1635 function bank_account_types_list_row($label, $name, $selected_id=null)
1636 {
1637         echo "<tr>\n";
1638         bank_account_types_list_cells($label, $name, $selected_id);
1639         echo "</tr>\n";
1640 }
1641
1642 //------------------------------------------------------------------------------------------------
1643 function payment_person_types_list($name, $selected_id=null, $submit_on_change=false)
1644 {
1645         $types = payment_person_types::get_all();
1646
1647         $items = array();
1648         foreach ($types as $type)
1649         {
1650                 if (payment_person_types::has_items($type['id']))
1651                 {
1652                         if ($type['id'] != payment_person_types::WorkOrder())
1653                                 $items[$type['id']] = $type['name'];
1654                 }
1655         }
1656         
1657         return array_selector($name, $selected_id, $items, 
1658                 array( 'select_submit'=> $submit_on_change ) );
1659 }
1660
1661 function payment_person_types_list_cells($label, $name, $selected_id=null, $related=null)
1662 {
1663         if ($label != null)
1664                 echo "<td>$label</td>\n";
1665         echo "<td>";
1666         $str = payment_person_types_list($name, $selected_id, $related);
1667         echo "</td>\n";
1668         return $str;
1669 }
1670
1671 function payment_person_types_list_row($label, $name, $selected_id=null, $related=null)
1672 {
1673         echo "<tr>\n";
1674         $str = payment_person_types_list_cells($label, $name, $selected_id, $related);
1675         echo "</tr>\n";
1676         return $str;
1677 }
1678
1679 //------------------------------------------------------------------------------------------------
1680
1681 function wo_types_list($name, $selected_id=null)
1682 {
1683         $types = wo_types::get_all();
1684
1685         $items = array();
1686         foreach ($types as $type)
1687                 $items[$type['id']] = $type['name'];
1688         
1689         return array_selector($name, $selected_id, $items, 
1690                 array( 'select_submit'=> true, 'async' => true ) );
1691 }
1692
1693 function wo_types_list_row($label, $name, $selected_id=null)
1694 {
1695         echo "<tr><td>$label</td><td>\n";
1696         $str = wo_types_list($name, $selected_id);
1697         echo "</td></tr>\n";
1698         return $str;
1699 }
1700
1701 //------------------------------------------------------------------------------------------------
1702
1703 function dateformats_list_row($label, $name, $value=null)
1704 {
1705         global $dateformats;
1706
1707         echo "<tr><td>$label</td>\n<td>";
1708         array_selector( $name, $value, $dateformats );
1709         echo "</td></tr>\n";
1710 }
1711
1712 function dateseps_list_row($label, $name, $value=null)
1713 {
1714         global $dateseps;
1715
1716         echo "<tr><td>$label</td>\n<td>";
1717         array_selector( $name, $value, $dateseps );
1718         echo "</td></tr>\n";
1719 }
1720
1721 function thoseps_list_row($label, $name, $value=null)
1722 {
1723         global $thoseps;
1724
1725         echo "<tr><td>$label</td>\n<td>";
1726         array_selector( $name, $value, $thoseps );
1727         echo "</td></tr>\n";
1728 }
1729
1730 function decseps_list_row($label, $name, $value=null)
1731 {
1732         global $decseps;
1733
1734         echo "<tr><td>$label</td>\n<td>";
1735         array_selector( $name, $value, $decseps );
1736         echo "</td></tr>\n";
1737 }
1738
1739 function themes_list_row($label, $name, $value=null)
1740 {
1741         global $path_to_root;
1742
1743         $path = $path_to_root.'/themes/';
1744         $themes = array();
1745         $themedir = opendir($path);
1746         while(false !== ($fname = readdir($themedir)))
1747         {
1748                 if($fname!='.' && $fname!='..' && $fname!='CVS' && is_dir($path.$fname))
1749                 {
1750                         $themes[$fname] =  $fname;
1751                 }
1752         }
1753         ksort($themes);
1754
1755         echo "<tr><td>$label</td>\n<td>";
1756         array_selector( $name, $value, $themes );
1757         echo "</td></tr>\n";
1758 }
1759
1760 function pagesizes_list_row($label, $name, $value=null)
1761 {
1762         global $pagesizes;
1763
1764         $items = array();
1765         foreach ($pagesizes as $pz)
1766                 $items[$pz] = $pz;
1767
1768         echo "<tr><td>$label</td>\n<td>";
1769         array_selector( $name, $value, $items );
1770         echo "</td></tr>\n";
1771 }
1772
1773 function security_headings_list_row($label, $name, $value=null)
1774 {
1775         global $security_headings;
1776
1777         echo "<tr><td>$label</td>\n<td>";
1778         array_selector( $name, $value, $security_headings );
1779         echo "</td></tr>\n";
1780 }
1781
1782 function systypes_list_cells($label, $name, $value=null, $submit_on_change=false)
1783 {
1784         global $systypes_array;
1785
1786         if ($label != null)
1787                 echo "<td>$label</td>\n";
1788         echo "<td>";
1789
1790         $items = array();
1791         foreach ($systypes_array as $key=>$type)
1792                         $items[$key] = $type['name'];
1793         
1794         $str = array_selector($name, $value, $items, 
1795                 array( 
1796                         'select_submit'=> $submit_on_change,
1797                         'async' => false
1798                         )
1799         );
1800         echo "</td>\n";
1801         return $str;
1802 }
1803
1804 function systypes_list_row($label, $name, $value=null, $submit_on_change=false)
1805 {
1806         echo "<tr>\n";
1807         $str = systypes_list_cells($label, $name, $value, $submit_on_change);
1808         echo "</tr>\n";
1809         return $str;
1810 }
1811
1812 function cust_allocations_list_cells($label, $name, $selected=null)
1813 {
1814         global $all_items;
1815
1816         if ($label != null)
1817                 label_cell($label);
1818         echo "<td>\n";
1819         $allocs = array( 
1820                 $all_items=>_("All Types"),
1821                 '1'=> _("Sales Invoices"),
1822                 '2'=> _("Overdue Invoices"),
1823                 '3' => _("Payments"),
1824                 '4' => _("Credit Notes"),
1825                 '5' => _("Delivery Notes")
1826         );
1827         $str = array_selector($name, $selected, $allocs);
1828         echo "</td>\n";
1829         return $str;
1830 }
1831
1832 function supp_allocations_list_cell($name, $selected=null)
1833 {
1834         global $all_items;
1835
1836         echo "<td>\n";
1837         $allocs = array( 
1838                 $all_items=>_("All Types"),
1839                 '1'=> _("Invoices"),
1840                 '2'=> _("Overdue Invoices"),
1841                 '3' => _("Payments"),
1842                 '4' => _("Credit Notes"),
1843                 '5' => _("Overdue Credit Notes")
1844         );
1845         $str = array_selector($name, $selected, $allocs);
1846         echo "</td>\n";
1847         return $str;
1848 }
1849
1850 function policy_list_cells($label, $name, $selected=null)
1851 {
1852         if ($label != null)
1853                 label_cell($label);
1854         echo "<td>\n";
1855         $str = array_selector($name, $selected, 
1856                                 array( '' => _("Automatically put balance on back order"),
1857                                         'CAN' => _("Cancel any quantites not delivered")) );
1858         echo "</td>\n";
1859         return $str;
1860 }
1861
1862 function policy_list_row($label, $name, $selected=null)
1863 {
1864         echo "<tr>\n";
1865         policy_list_cells($label, $name, $selected);
1866         echo "</tr>\n";
1867 }
1868
1869 function credit_type_list_cells($label, $name, $selected=null, $submit_on_change=false)
1870 {
1871         if ($label != null)
1872                 label_cell($label);
1873         echo "<td>\n";
1874         $str = array_selector($name, $selected, 
1875                                 array( 'Return' => _("Items Returned to Inventory Location"),
1876                                         'WriteOff' => _("Items Written Off")),
1877                                 array( 'select_submit'=> $submit_on_change ) );
1878         echo "</td>\n";
1879         return $str;
1880 }
1881
1882 function credit_type_list_row($label, $name, $selected=null, $submit_on_change=false)
1883 {
1884         echo "<tr>\n";
1885         $str = credit_type_list_cells($label, $name, $selected, $submit_on_change);
1886         echo "</tr>\n";
1887         return $str;
1888 }
1889
1890 function number_list($name, $selected, $from, $to, $no_option=false)
1891 {
1892         $items = array();
1893         for ($i = $from; $i <= $to; $i++)
1894                 $items[$i] = "$i";
1895
1896         return array_selector($name, $selected, $items,
1897                                 array(  'spec_option' => $no_option,
1898                                                 'spec_id' => reserved_words::get_all_numeric()) );
1899 }
1900
1901 function number_list_cells($label, $name, $selected, $from, $to, $no_option=false)
1902 {
1903         if ($label != null)
1904                 label_cell($label);
1905         echo "<td>\n";
1906         number_list($name, $selected, $from, $to, $no_option);
1907         echo "</td>\n";
1908 }
1909
1910 function number_list_row($label, $name, $selected, $from, $to, $no_option=false)
1911 {
1912         echo "<tr>\n";
1913         number_list_cells($label, $name, $selected, $from, $to, $no_option);
1914         echo "</tr>\n";
1915 }
1916
1917 function print_profiles_list_row($label, $name, $selected_id=null, $spec_opt=false,
1918         $submit_on_change=true)
1919 {
1920         $sql = "SELECT profile FROM ".TB_PREF."print_profiles"
1921                 ." GROUP BY profile";
1922         $result = db_query($sql, 'cannot get all profile names');
1923         $profiles = array();
1924         while($myrow=db_fetch($result)) {
1925                 $profiles[$myrow['profile']] = $myrow['profile'];
1926         }
1927
1928         echo "<tr>";
1929         if ($label != null)
1930                 echo "<td>$label</td>\n";
1931         echo "<td>";
1932
1933         array_selector($name, $selected_id, $profiles, 
1934                 array( 'select_submit'=> $submit_on_change,
1935                         'spec_option'=>$spec_opt,
1936                         'spec_id' => ''
1937                  ));
1938
1939         echo "</td></tr>\n";
1940 }
1941
1942 function printers_list($name, $selected_id=null, $spec_opt=false, $submit_on_change=false)
1943 {
1944         static $printers; // query only once for page display
1945
1946         if (!$printers) {
1947                 $sql = "SELECT id, name, description FROM ".TB_PREF."printers"; 
1948                 $result = db_query($sql, 'cannot get all printers');
1949                 $printers = array();
1950                 while($myrow=db_fetch($result)) {
1951                         $printers[$myrow['id']] = $myrow['name'].'&nbsp;-&nbsp;'.$myrow['description'];
1952                 }
1953         }
1954         array_selector($name, $selected_id, $printers, 
1955                 array( 'select_submit'=> $submit_on_change,
1956                         'spec_option'=>$spec_opt,
1957                         'spec_id' => ''
1958                  ));
1959 }
1960
1961 //------------------------------------------------------------------------------------------------
1962
1963 function quick_entries_list($name, $selected_id=null, $type=null, $submit_on_change=false)
1964 {
1965         $where = false;
1966         $sql = "SELECT id, description FROM ".TB_PREF."quick_entries";
1967         if ($type != null)
1968                 $sql .= " WHERE type=$type";
1969
1970         combo_input($name, $selected_id, $sql, 'id', 'description',
1971                 array(
1972                         'spec_id' => '',
1973                         'order' => 'description',
1974                         'select_submit'=> $submit_on_change,
1975                         'async' => false
1976                 ) );
1977
1978 }
1979
1980 function quick_entries_list_cells($label, $name, $selected_id=null, $type, $submit_on_change=false)
1981 {
1982         echo "<td>$label</td><td>\n";
1983         quick_entries_list($name, $selected_id, $type, $submit_on_change);
1984         echo "</td>";
1985 }
1986
1987 function quick_entries_list_row($label, $name, $selected_id=null, $type, $submit_on_change=false)
1988 {
1989         echo "<tr>\n";
1990         quick_entries_list_cells($label, $name, $selected_id, $type, $submit_on_change);
1991         echo "</tr>\n";
1992 }
1993
1994
1995 function quick_actions_list_row($label, $name, $selected_id=null, $submit_on_change=false)
1996 {
1997         global $quick_actions;
1998         
1999         echo "<tr><td>$label</td><td>";
2000         array_selector($name, $selected_id, $quick_actions, 
2001                 array( 
2002                         'select_submit'=> $submit_on_change
2003                 ) );
2004         echo "</td></tr>\n";
2005 }
2006
2007 function quick_entry_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2008 {
2009         global $quick_entry_types;
2010                 
2011         echo "<tr><td>$label</td><td>";
2012         array_selector($name, $selected_id, $quick_entry_types, 
2013                 array( 
2014                         'select_submit'=> $submit_on_change
2015                         ) );
2016         echo "</td></tr>\n";
2017 }
2018
2019
2020 function class_types_list_row($label, $name, $selected_id=null, $submit_on_change=false)
2021 {
2022         global $class_types;
2023         
2024         echo "<tr><td>$label</td><td>";
2025         array_selector($name, $selected_id, $class_types, 
2026                 array( 
2027                         'select_submit'=> $submit_on_change
2028                 ) );
2029         echo "</td></tr>\n";
2030 }
2031
2032
2033 ?>