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