5 include( '/elements/customer-table.html',
12 'header' => [ '#', 'Item' ],
15 sub { my ($row,$param) = @_;
16 $param->{"column$row"};
24 'name_singular' => 'customer', #label
25 'custnum_update_callback' => 'name_of_js_callback' #passed a rownum
28 'types' => ['immutable', ''], # immutable or ''/text
29 'align' => [ 'c', 'l', 'r', '' ],
30 'size' => [], # sizes ignored for immutable
32 'footer' => ['string', '_TOTAL'], # strings or the special
34 'footer_align' => [ 'c', 'l', 'r', '' ],
36 'param' => { column0 => 1 }, # preset column of row 0 to 1
42 <SCRIPT TYPE="text/javascript">
44 function clearhint_invnum() {
46 if ( this.value == 'Not found' || this.value == 'Multiple' ) {
48 this.style.color = '#000000';
53 function clearhint_custnum() {
55 if ( this.value == 'Not found' || this.value == 'Multiple' ) {
57 this.style.color = '#000000';
62 function clearhint_customer() {
64 this.style.color = '#000000';
66 if ( this.value == '(last name or company)' || this.value == 'Not found' )
71 function <% $opt{prefix} %>search_invnum() {
73 this.style.color = '#000000'
75 var invnum_obj = this;
76 var searchrow = this.getAttribute('rownum');
77 var invnum = this.value;
79 if ( invnum == 'searching...' || invnum == 'Not found' || invnum == '' )
82 if ( this.getAttribute('magic') == 'nosearch' ) {
83 this.setAttribute('magic', '');
87 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
88 <% $opt{prefix} %>addRow();
90 var customer = document.getElementById('customer'+searchrow);
91 customer.value = 'searching...';
92 customer.disabled = true;
93 customer.style.color = '#000000';
94 customer.style.backgroundColor = '#dddddd';
96 var customer_select = document.getElementById('cust_select'+searchrow);
98 customer.style.display = '';
99 customer_select.style.display = 'none';
101 var custnum_obj = document.getElementById('custnum'+searchrow);
102 var balance = document.getElementById('balance'+searchrow);
105 function search_invnum_update(customers) {
107 var customerArray = eval('(' + customers + ')');
109 custnum_obj.disabled = false;
110 custnum_obj.style.backgroundColor = '#ffffff';
111 customer.disabled = false;
112 customer.style.backgroundColor = '#ffffff';
114 if ( customerArray.length == 0 ) {
116 custnum_obj.value = 'Not found';
117 customer.value = 'Not found';
118 custnum_obj.style.color = '#ff0000';
119 customer.style.color = '#ff0000';
121 customer.style.display = '';
122 customer_select.style.display = 'none';
124 } else if ( customerArray.length == 3 ) {
126 custnum_obj.value = customerArray[0];
127 custnum_obj.style.color = '#000000';
128 customer.value = customerArray[1];
129 balance.value = customerArray[2];
131 customer.style.display = '';
132 customer_select.style.display = 'none';
134 % if ( $opt{invnum_update_callback} ) {
135 <% $opt{invnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
142 invnum_search( invnum, search_invnum_update );
147 function <% $opt{prefix} %>search_custnum() {
149 this.style.color = '#000000'
151 var custnum_obj = this;
152 var searchrow = this.getAttribute('rownum');
153 var custnum = this.value;
155 if ( custnum == 'searching...' || custnum == 'Not found' || custnum == '' )
158 if ( this.getAttribute('magic') == 'nosearch' ) {
159 this.setAttribute('magic', '');
163 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
164 <% $opt{prefix} %>addRow();
166 var customer = document.getElementById('customer'+searchrow);
167 customer.value = 'searching...';
168 customer.disabled = true;
169 customer.style.color = '#000000';
170 customer.style.backgroundColor = '#dddddd';
172 var customer_select = document.getElementById('cust_select'+searchrow);
174 customer.style.display = '';
175 customer_select.style.display = 'none';
177 var invnum = document.getElementById('invnum'+searchrow);
180 var balance = document.getElementById('balance'+searchrow);
183 function search_custnum_update(customers) {
185 var customerArray = eval('(' + customers + ')');
187 customer.disabled = false;
188 customer.style.backgroundColor = '#ffffff';
190 if ( customerArray.length == 0 ) {
192 customer.value = 'Not found';
193 customer.style.color = '#ff0000';
194 custnum_obj.style.color = '#ff0000';
196 } else if ( customerArray.length == 3 ) {
198 custnum_obj.value = customerArray[0];
199 custnum_obj.style.color = '#000000';
200 customer.value = customerArray[1];
201 balance.value = customerArray[2];
203 customer.style.display = '';
204 customer_select.style.display = 'none';
206 % if ( $opt{custnum_update_callback} ) {
207 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
212 custnum_search(custnum, search_custnum_update );
216 function <% $opt{prefix} %>search_customer() {
218 var customer_obj = this;
219 var searchrow = this.getAttribute('rownum');
220 var customer = this.value;
222 if ( customer == 'searching...' || customer == 'Not found' || customer == '' )
225 if ( this.getAttribute('magic') == 'nosearch' ) {
226 this.setAttribute('magic', '');
230 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
231 <% $opt{prefix} %>addRow();
234 var invnum = document.getElementById('invnum'+searchrow);
237 var custnum_obj = document.getElementById('custnum'+searchrow);
238 custnum_obj.value = 'searching...';
239 custnum_obj.disabled = true;
240 custnum_obj.style.color = '#000000';
241 custnum_obj.style.backgroundColor = '#dddddd';
243 var customer_select = document.getElementById('cust_select'+searchrow);
245 var balance = document.getElementById('balance'+searchrow);
248 function search_customer_update(customers) {
250 var customerArray = eval('(' + customers + ')');
252 custnum_obj.disabled = false;
253 custnum_obj.style.backgroundColor = '#ffffff';
255 if ( customerArray.length == 0 ) {
257 custnum_obj.value = 'Not found';
258 custnum_obj.style.color = '#ff0000';
259 customer_obj.style.color = '#ff0000';
261 customer_obj.style.display = '';
262 customer_select.style.display = 'none';
264 } else if ( customerArray.length == 1 ) {
266 custnum_obj.value = customerArray[0][0];
267 customer_obj.value = customerArray[0][1];
268 balance.value = customerArray[0][2];
270 customer_obj.style.display = '';
271 customer_select.style.display = 'none';
273 % if ( $opt{custnum_update_callback} ) {
274 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
279 custnum_obj.value = 'Multiple'; // or something
280 custnum_obj.style.color = '#ff0000';
282 //blank the current list
283 for ( var i = customer_select.length; i >= 0; i-- )
284 customer_select.options[i] = null;
286 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
288 //add the multiple customers
289 for ( var s = 0; s < customerArray.length; s++ )
290 opt(customer_select, customerArray[s][0] + '_' + customerArray[s][2], customerArray[s][1], '#000000');
292 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
294 customer_obj.style.display = 'none';
296 customer_select.style.display = '';
302 smart_search( customer, search_customer_update );
306 function select_customer() {
308 var custnum_balance = this.options[this.selectedIndex].value;
309 var customer = this.options[this.selectedIndex].text;
311 var searchrow = this.getAttribute('rownum');
312 var custnum_obj = document.getElementById('custnum'+searchrow);
313 var customer_obj = document.getElementById('customer'+searchrow);
314 var balance_obj = document.getElementById('balance'+searchrow);
316 if ( custnum_balance == '' ) {
318 } else if ( custnum_balance == 'cancel' ) {
320 custnum_obj.value = '';
321 custnum_obj.style.color = '#000000';
323 this.style.display = 'none';
324 customer_obj.style.display = '';
325 customer_obj.focus();
329 var custnum = custnum_balance.substring(0,custnum_balance.indexOf('_'));
330 var balance = custnum_balance.substring(custnum_balance.indexOf('_')+1);
332 custnum_obj.value = custnum;
333 custnum_obj.style.color = '#000000';
335 customer_obj.value = customer;
336 customer_obj.style.color = '#000000';
338 balance_obj.value = balance;
340 this.style.display = 'none';
341 customer_obj.style.display = '';
343 % if ( $opt{custnum_update_callback} ) {
344 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
351 function opt(what,value,text,color) {
352 var optionName = new Option(text, value, false, false);
353 optionName.style.color = color;
354 var length = what.length;
355 what.options[length] = optionName;
360 <TABLE ID="<% $opt{prefix} %>OneTrueTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
366 % foreach my $header ( @{$opt{header}} ) {
367 <TH><% $header %></TH>
372 % for ( $row = 0; exists($param->{"custnum$row"}); $row++ ) {
377 NAME = "invnum<% $row %>"
378 ID = "invnum<% $row %>"
381 STYLE = "text-align:right;"
382 VALUE = "<% $param->{"invnum$row"} %>"
383 rownum = "<% $row %>"
385 <SCRIPT TYPE="text/javascript">
386 var invnum_input<% $row %> = document.getElementById("invnum<% $row %>");
387 invnum_input<% $row %>.onfocus = clearhint_invnum;
388 invnum_input<% $row %>.onchange = <% $opt{prefix} %>search_invnum;
394 NAME = "custnum<% $row %>"
395 ID = "custnum<% $row %>"
398 STYLE = "text-align:right;"
399 VALUE = "<% $param->{"custnum$row"} %>"
400 rownum = "<% $row %>"
402 <SCRIPT TYPE="text/javascript">
403 var custnum_input<% $row %> = document.getElementById("custnum<% $row %>");
404 custnum_input<% $row %>.onfocus = clearhint_custnum;
405 custnum_input<% $row %>.onchange = <% $opt{prefix} %>search_custnum;
410 <INPUT TYPE="text" NAME="customer<% $row %>" ID="customer<% $row %>" SIZE=64 VALUE="<% $param->{"customer$row"} %>" rownum="<% $row %>">
411 <SCRIPT TYPE="text/javascript">
412 var customer_input<% $row %> = document.getElementById("customer<% $row %>");
413 customer_input<% $row %>.onfocus = clearhint_customer;
414 customer_input<% $row %>.onclick = clearhint_customer;
415 customer_input<% $row %>.onchange = <% $opt{prefix} %>search_customer;
417 <SELECT NAME="cust_select<% $row %>" ID="cust_select<% $row %>" rownum="<% $row %>" STYLE="color:#ff0000; display:none">
419 <SCRIPT TYPE="text/javascript">
420 var customer_select<% $row %> = document.getElementById("cust_select<% $row %>");
421 customer_select<% $row %>.onchange = select_customer;
426 % foreach my $field ( @{$opt{fields}} ) {
428 % if ( ref($field) eq 'CODE' ) {
429 % $value = &{$field}($row,$param);
431 % $value = $param->{"$field$row"};
433 % my $name = (ref($field) eq 'CODE') ? "column${col}_$row" : "$field$row";
434 % my $align = $align{ $opt{align}->[$col] || 'l' };
435 % my $size = $sizes->[$col] || 10;
436 % my $color = $opt{color}->[$col];
437 % my $font = $color ? qq(<FONT COLOR="$color">) : '';
439 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
440 % $total[$col] += $value;
441 % $onchange = $opt{prefix}. "calc_total$col();";
442 % $onchange = qq(onchange="$onchange" onkeyup="$onchange");
444 <TD ALIGN="<% $align %>">
445 % if (! $types->[$col] || $types->[$col] eq 'text') {
450 STYLE = "text-align: <% $align %>;"
451 VALUE = "<% $value %>"
454 % } elsif ($types->[$col] eq 'immutable') {
455 <% $font %><% $value %><% $font ? '</FONT>' : '' %>
456 <INPUT TYPE="hidden" ID="<% $name %>" NAME="<% $name %>" VALUE="<% $value %>" >
458 Cannot represent unknown type: <% $types->[$col] %>
465 NAME = "balance<% $row %>"
466 ID = "balance<% $row %>"
469 STYLE = "text-align:right;"
470 VALUE = "<% $param->{"balance$row"} %>"
471 rownum = "<% $row %>"
478 <TH COLSPAN=3 ID="<% $opt{'prefix'} %>_TOTAL_TOTAL">
479 Total <% $row ? $row-1 : 0 %>
480 <% PL($opt{name_singular} || 'customer', ( $row ? $row-1 : 0 ) ) %>
483 % foreach my $footer ( @{$opt{footer}} ) {
484 % my $align = $align{ $opt{'footer_align'}->[$col] || 'c' };
485 % if ($footer eq '_TOTAL' ) {
486 % my $id = $opt{'fields'}->[$col];
487 % $id = ref($id) ? "column${col}_TOTAL" : "${id}_TOTAL";
488 <TH ALIGN="<% $align %>" ID="<% $id %>"> <% sprintf('%.2f', $total[$col] ) %></TH>
490 <TH ALIGN="<% $align %>"><% $footer %></TH>
498 <SCRIPT TYPE="text/javascript">
500 % foreach my $footer ( @{$opt{footer}} ) {
501 % if ($footer eq '_TOTAL' ) {
502 % my $name = $opt{fields}->[$col];
503 % $name = ref($name) ? "column$col" : $name;
504 var <% $opt{prefix}.$name %>_CACHE = new Array ();
505 var <% $opt{prefix} %>th_el = document.getElementById("<%$name%>_TOTAL");
506 function <% $opt{prefix} %>calc_total<% $col %>() {
511 ( <% $opt{prefix}.$name%>_CACHE[row] =
512 <% $opt{prefix}.$name%>_CACHE[row]
513 || document.getElementById("<%$name%>"+row)
519 var value = <%$name%>_CACHE[row].value;
520 value = parseFloat(value);
521 if ( ! isNaN(value) ) {
522 total = total + value;
525 <% $opt{prefix} %>th_el.innerHTML = ' ' + total.toFixed(2);
533 <% include('/elements/xmlhttp.html',
534 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
535 'subs' => [qw( custnum_search smart_search invnum_search )],
539 <SCRIPT TYPE="text/javascript">
541 var <% $opt{prefix} %>total_el =
542 document.getElementById("<% $opt{'prefix'} %>_TOTAL_TOTAL");
544 var <% $opt{prefix} %>rownum = <% $row %>;
546 function <% $opt{prefix} %>addRow() {
548 var table = document.getElementById('<% $opt{prefix} %>OneTrueTable');
549 var tablebody = table.getElementsByTagName('tbody').item(0);
551 var row = table.insertRow(rownum+1);
553 var invnum_cell = document.createElement('TD');
555 var invnum_input = document.createElement('INPUT');
556 invnum_input.setAttribute('name', 'invnum'+<% $opt{prefix} %>rownum);
557 invnum_input.setAttribute('id', 'invnum'+<% $opt{prefix} %>rownum);
558 invnum_input.style.textAlign = 'right';
559 invnum_input.setAttribute('size', 8);
560 invnum_input.setAttribute('maxlength', 12);
561 invnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
562 invnum_input.onfocus = clearhint_invnum;
563 invnum_input.onchange = <% $opt{prefix} %>search_invnum;
564 invnum_cell.appendChild(invnum_input);
566 row.appendChild(invnum_cell);
568 var custnum_cell = document.createElement('TD');
570 var custnum_input = document.createElement('INPUT');
571 custnum_input.setAttribute('name', 'custnum'+<% $opt{prefix} %>rownum);
572 custnum_input.setAttribute('id', 'custnum'+<% $opt{prefix} %>rownum);
573 custnum_input.style.textAlign = 'right';
574 custnum_input.setAttribute('size', 8);
575 custnum_input.setAttribute('maxlength', 12);
576 custnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
577 custnum_input.onfocus = clearhint_custnum;
578 custnum_input.onchange = <% $opt{prefix} %>search_custnum;
579 custnum_cell.appendChild(custnum_input);
581 row.appendChild(custnum_cell);
583 var customer_cell = document.createElement('TD');
585 var customer_input = document.createElement('INPUT');
586 customer_input.setAttribute('name', 'customer'+<% $opt{prefix} %>rownum);
587 customer_input.setAttribute('id', 'customer'+<% $opt{prefix} %>rownum);
588 customer_input.setAttribute('size', 64);
589 customer_input.setAttribute('value', '(last name or company)' );
590 customer_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
591 customer_input.onfocus = clearhint_customer;
592 customer_input.onclick = clearhint_customer;
593 customer_input.onchange = <% $opt{prefix} %>search_customer;
594 customer_cell.appendChild(customer_input);
596 var customer_select = document.createElement('SELECT');
597 customer_select.setAttribute('name', 'cust_select'+<% $opt{prefix} %>rownum);
598 customer_select.setAttribute('id', 'cust_select'+<% $opt{prefix} %>rownum);
599 customer_select.setAttribute('rownum', <% $opt{prefix} %>rownum);
600 customer_select.style.color = '#ff0000';
601 customer_select.style.display = 'none';
602 customer_select.onchange = select_customer;
603 customer_cell.appendChild(customer_select);
605 row.appendChild(customer_cell);
608 % foreach my $field ( @{$opt{fields}} ) {
610 var my_cell = document.createElement('TD');
611 my_cell.setAttribute('align', '<% $align{ $opt{align}->[$col] || 'l' } %>');
613 % if ($types->[$col] eq 'immutable') {
615 % if ( ref($field) eq 'CODE' ) {
616 % $value = &{$field}($row,$param);
618 % $value = $param->{"$field$row"};
620 var my_text = document.createTextNode('<% $value %>');
621 my_cell.appendChild(my_text);
624 var my_input = document.createElement('INPUT');
625 my_input.setAttribute('name', '<% $field %>'+<% $opt{prefix} %>rownum);
626 my_input.setAttribute('id', '<% $field %>'+<% $opt{prefix} %>rownum);
627 my_input.style.textAlign = '<% $align{ $opt{align}->[$col] || 'l' } %>';
628 my_input.setAttribute('size', <% $sizes->[$col] || 10 %>);
629 % if ($types->[$col] eq 'immutable') {
630 my_input.setAttribute('type', 'hidden');
632 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
633 my_input.onchange = <% $opt{prefix} %>calc_total<%$col%>;
634 my_input.onkeyup = <% $opt{prefix} %>calc_total<%$col%>;
636 my_cell.appendChild(my_input);
638 row.appendChild(my_cell);
643 var balance_cell = document.createElement('TD');
645 var balance_input = document.createElement('INPUT');
646 balance_input.setAttribute('name', 'balance'+<% $opt{prefix} %>rownum);
647 balance_input.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum);
648 balance_input.style.textAlign = 'right';
649 balance_input.setAttribute('size', 8);
650 balance_input.setAttribute('maxlength', 12);
651 balance_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
652 balance_input.disabled = true;
653 balance_input.style.backgroundColor = '#dddddd';
654 balance_cell.appendChild(balance_input);
656 row.appendChild(balance_cell);
658 //update the total # of rows display
659 if ( <% $opt{prefix} %>rownum == 1 ) {
660 <% $opt{prefix} %>total_el.innerHTML =
662 + <% $opt{prefix} %>rownum
663 + ' <% $opt{name_singular} || 'customer' %>';
665 <% $opt{prefix} %>total_el.innerHTML =
667 + <% $opt{prefix} %>rownum
668 + ' <% PL($opt{name_singular} || 'customer') %>';
671 <% $opt{prefix} %>rownum++;
675 % unless ($cgi->param('error')) {
676 <% $opt{prefix} %>addRow();
684 $opt{prefix} = '' unless defined $opt{prefix};
685 $opt{prefix} .= '_' if $opt{prefix};
687 my $types = $opt{'types'} ? [ @{$opt{'types'}} ] : [];
688 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
690 my $param = $opt{param};
691 $param = $cgi->Vars if $cgi->param('error');
693 $opt{$_} ||= [] foreach qw(align color footer footer_align);
695 my @total = map 0, @{$opt{footer}};