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