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