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