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