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