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