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