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