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(name) {
185 var name = eval('(' + name + ')' );
187 customer.disabled = false;
188 customer.style.backgroundColor = '#ffffff';
190 if ( name.length > 0 ) {
191 customer.value = name;
192 customer.setAttribute('magic', 'nosearch');
193 % if ( $opt{custnum_update_callback} ) {
194 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
197 customer.value = 'Not found';
198 customer.style.color = '#ff0000';
199 custnum_obj.style.color = '#ff0000';
205 custnum_search( custnum, search_custnum_update );
209 function <% $opt{prefix} %>search_customer() {
211 var customer_obj = this;
212 var searchrow = this.getAttribute('rownum');
213 var customer = this.value;
215 if ( customer == 'searching...' || customer == 'Not found' || customer == '' )
218 if ( this.getAttribute('magic') == 'nosearch' ) {
219 this.setAttribute('magic', '');
223 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
224 <% $opt{prefix} %>addRow();
227 var invnum = document.getElementById('invnum'+searchrow);
230 var custnum_obj = document.getElementById('custnum'+searchrow);
231 custnum_obj.value = 'searching...';
232 custnum_obj.disabled = true;
233 custnum_obj.style.color = '#000000';
234 custnum_obj.style.backgroundColor = '#dddddd';
236 var customer_select = document.getElementById('cust_select'+searchrow);
238 var balance = document.getElementById('balance'+searchrow);
241 function search_customer_update(customers) {
243 var customerArray = eval('(' + customers + ')');
245 custnum_obj.disabled = false;
246 custnum_obj.style.backgroundColor = '#ffffff';
248 if ( customerArray.length == 0 ) {
250 custnum_obj.value = 'Not found';
251 custnum_obj.style.color = '#ff0000';
252 customer_obj.style.color = '#ff0000';
254 customer_obj.style.display = '';
255 customer_select.style.display = 'none';
257 } else if ( customerArray.length == 1 ) {
259 custnum_obj.value = customerArray[0][0];
260 customer_obj.value = customerArray[0][1];
261 balance.value = customerArray[0][2];
263 customer_obj.style.display = '';
264 customer_select.style.display = 'none';
266 % if ( $opt{custnum_update_callback} ) {
267 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
272 custnum_obj.value = 'Multiple'; // or something
273 custnum_obj.style.color = '#ff0000';
275 //blank the current list
276 for ( var i = customer_select.length; i >= 0; i-- )
277 customer_select.options[i] = null;
279 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
281 //add the multiple customers
282 for ( var s = 0; s < customerArray.length; s++ )
283 opt(customer_select, customerArray[s][0], customerArray[s][1], '#000000');
285 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
287 customer_obj.style.display = 'none';
289 customer_select.style.display = '';
295 smart_search( customer, search_customer_update );
299 function select_customer() {
301 var custnum = this.options[this.selectedIndex].value;
302 var customer = this.options[this.selectedIndex].text;
304 var searchrow = this.getAttribute('rownum');
305 var custnum_obj = document.getElementById('custnum'+searchrow);
306 var customer_obj = document.getElementById('customer'+searchrow);
308 if ( custnum == '' ) {
310 } else if ( custnum == 'cancel' ) {
312 custnum_obj.value = '';
313 custnum_obj.style.color = '#000000';
315 this.style.display = 'none';
316 customer_obj.style.display = '';
317 customer_obj.focus();
321 custnum_obj.value = custnum;
322 custnum_obj.style.color = '#000000';
324 customer_obj.value = customer;
325 customer_obj.style.color = '#000000';
327 this.style.display = 'none';
328 customer_obj.style.display = '';
330 % if ( $opt{custnum_update_callback} ) {
331 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
338 function opt(what,value,text,color) {
339 var optionName = new Option(text, value, false, false);
340 optionName.style.color = color;
341 var length = what.length;
342 what.options[length] = optionName;
347 <TABLE ID="<% $opt{prefix} %>OneTrueTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
353 % foreach my $header ( @{$opt{header}} ) {
354 <TH><% $header %></TH>
359 % for ( $row = 0; exists($param->{"custnum$row"}); $row++ ) {
364 NAME = "invnum<% $row %>"
365 ID = "invnum<% $row %>"
368 STYLE = "text-align:right;"
369 VALUE = "<% $param->{"invnum$row"} %>"
370 rownum = "<% $row %>"
372 <SCRIPT TYPE="text/javascript">
373 var invnum_input<% $row %> = document.getElementById("invnum<% $row %>");
374 invnum_input<% $row %>.onfocus = clearhint_invnum;
375 invnum_input<% $row %>.onchange = <% $opt{prefix} %>search_invnum;
381 NAME = "custnum<% $row %>"
382 ID = "custnum<% $row %>"
385 STYLE = "text-align:right;"
386 VALUE = "<% $param->{"custnum$row"} %>"
387 rownum = "<% $row %>"
389 <SCRIPT TYPE="text/javascript">
390 var custnum_input<% $row %> = document.getElementById("custnum<% $row %>");
391 custnum_input<% $row %>.onfocus = clearhint_custnum;
392 custnum_input<% $row %>.onchange = <% $opt{prefix} %>search_custnum;
397 <INPUT TYPE="text" NAME="customer<% $row %>" ID="customer<% $row %>" SIZE=64 VALUE="<% $param->{"customer$row"} %>" rownum="<% $row %>">
398 <SCRIPT TYPE="text/javascript">
399 var customer_input<% $row %> = document.getElementById("customer<% $row %>");
400 customer_input<% $row %>.onfocus = clearhint_customer;
401 customer_input<% $row %>.onclick = clearhint_customer;
402 customer_input<% $row %>.onchange = <% $opt{prefix} %>search_customer;
404 <SELECT NAME="cust_select<% $row %>" ID="cust_select<% $row %>" rownum="<% $row %>" STYLE="color:#ff0000; display:none">
406 <SCRIPT TYPE="text/javascript">
407 var customer_select<% $row %> = document.getElementById("cust_select<% $row %>");
408 customer_select<% $row %>.onchange = select_customer;
413 % foreach my $field ( @{$opt{fields}} ) {
415 % if ( ref($field) eq 'CODE' ) {
416 % $value = &{$field}($row,$param);
418 % $value = $param->{"$field$row"};
420 % my $name = (ref($field) eq 'CODE') ? "column${col}_$row" : "$field$row";
421 % my $align = $align{ $opt{align}->[$col] || 'l' };
422 % my $size = $sizes->[$col] || 10;
423 % my $color = $opt{color}->[$col];
424 % my $font = $color ? qq(<FONT COLOR="$color">) : '';
426 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
427 % $total[$col] += $value;
428 % $onchange = $opt{prefix}. "calc_total$col();";
429 % $onchange = qq(onchange="$onchange" onkeyup="$onchange");
431 <TD ALIGN="<% $align %>">
432 % if (! $types->[$col] || $types->[$col] eq 'text') {
437 STYLE = "text-align: <% $align %>;"
438 VALUE = "<% $value %>"
441 % } elsif ($types->[$col] eq 'immutable') {
442 <% $font %><% $value %><% $font ? '</FONT>' : '' %>
443 <INPUT TYPE="hidden" ID="<% $name %>" NAME="<% $name %>" VALUE="<% $value %>" >
445 Cannot represent unknown type: <% $types->[$col] %>
452 NAME = "balance<% $row %>"
453 ID = "balance<% $row %>"
456 STYLE = "text-align:right;"
457 VALUE = "<% $param->{"balance$row"} %>"
458 rownum = "<% $row %>"
465 <TH COLSPAN=3 ID="<% $opt{'prefix'} %>_TOTAL_TOTAL">
466 Total <% $row ? $row-1 : 0 %>
467 <% PL($opt{name_singular} || 'customer', ( $row ? $row-1 : 0 ) ) %>
470 % foreach my $footer ( @{$opt{footer}} ) {
471 % my $align = $align{ $opt{'footer_align'}->[$col] || 'c' };
472 % if ($footer eq '_TOTAL' ) {
473 % my $id = $opt{'fields'}->[$col];
474 % $id = ref($id) ? "column${col}_TOTAL" : "${id}_TOTAL";
475 <TH ALIGN="<% $align %>" ID="<% $id %>"> <% sprintf('%.2f', $total[$col] ) %></TH>
477 <TH ALIGN="<% $align %>"><% $footer %></TH>
485 <SCRIPT TYPE="text/javascript">
487 % foreach my $footer ( @{$opt{footer}} ) {
488 % if ($footer eq '_TOTAL' ) {
489 % my $name = $opt{fields}->[$col];
490 % $name = ref($name) ? "column$col" : $name;
491 var <% $opt{prefix}.$name %>_CACHE = new Array ();
492 var <% $opt{prefix} %>th_el = document.getElementById("<%$name%>_TOTAL");
493 function <% $opt{prefix} %>calc_total<% $col %>() {
498 ( <% $opt{prefix}.$name%>_CACHE[row] =
499 <% $opt{prefix}.$name%>_CACHE[row]
500 || document.getElementById("<%$name%>"+row)
506 var value = <%$name%>_CACHE[row].value;
507 value = parseFloat(value);
508 if ( ! isNaN(value) ) {
509 total = total + value;
512 <% $opt{prefix} %>th_el.innerHTML = ' ' + total.toFixed(2);
520 <% include('/elements/xmlhttp.html',
521 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
522 'subs' => [qw( custnum_search smart_search invnum_search )],
526 <SCRIPT TYPE="text/javascript">
528 var <% $opt{prefix} %>total_el =
529 document.getElementById("<% $opt{'prefix'} %>_TOTAL_TOTAL");
531 var <% $opt{prefix} %>rownum = <% $row %>;
533 function <% $opt{prefix} %>addRow() {
535 var table = document.getElementById('<% $opt{prefix} %>OneTrueTable');
536 var tablebody = table.getElementsByTagName('tbody').item(0);
538 var row = table.insertRow(rownum+1);
540 var invnum_cell = document.createElement('TD');
542 var invnum_input = document.createElement('INPUT');
543 invnum_input.setAttribute('name', 'invnum'+<% $opt{prefix} %>rownum);
544 invnum_input.setAttribute('id', 'invnum'+<% $opt{prefix} %>rownum);
545 invnum_input.style.textAlign = 'right';
546 invnum_input.setAttribute('size', 8);
547 invnum_input.setAttribute('maxlength', 12);
548 invnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
549 invnum_input.onfocus = clearhint_invnum;
550 invnum_input.onchange = <% $opt{prefix} %>search_invnum;
551 invnum_cell.appendChild(invnum_input);
553 row.appendChild(invnum_cell);
555 var custnum_cell = document.createElement('TD');
557 var custnum_input = document.createElement('INPUT');
558 custnum_input.setAttribute('name', 'custnum'+<% $opt{prefix} %>rownum);
559 custnum_input.setAttribute('id', 'custnum'+<% $opt{prefix} %>rownum);
560 custnum_input.style.textAlign = 'right';
561 custnum_input.setAttribute('size', 8);
562 custnum_input.setAttribute('maxlength', 12);
563 custnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
564 custnum_input.onfocus = clearhint_custnum;
565 custnum_input.onchange = <% $opt{prefix} %>search_custnum;
566 custnum_cell.appendChild(custnum_input);
568 row.appendChild(custnum_cell);
570 var customer_cell = document.createElement('TD');
572 var customer_input = document.createElement('INPUT');
573 customer_input.setAttribute('name', 'customer'+<% $opt{prefix} %>rownum);
574 customer_input.setAttribute('id', 'customer'+<% $opt{prefix} %>rownum);
575 customer_input.setAttribute('size', 64);
576 customer_input.setAttribute('value', '(last name or company)' );
577 customer_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
578 customer_input.onfocus = clearhint_customer;
579 customer_input.onclick = clearhint_customer;
580 customer_input.onchange = <% $opt{prefix} %>search_customer;
581 customer_cell.appendChild(customer_input);
583 var customer_select = document.createElement('SELECT');
584 customer_select.setAttribute('name', 'cust_select'+<% $opt{prefix} %>rownum);
585 customer_select.setAttribute('id', 'cust_select'+<% $opt{prefix} %>rownum);
586 customer_select.setAttribute('rownum', <% $opt{prefix} %>rownum);
587 customer_select.style.color = '#ff0000';
588 customer_select.style.display = 'none';
589 customer_select.onchange = select_customer;
590 customer_cell.appendChild(customer_select);
592 row.appendChild(customer_cell);
595 % foreach my $field ( @{$opt{fields}} ) {
597 var my_cell = document.createElement('TD');
598 my_cell.setAttribute('align', '<% $align{ $opt{align}->[$col] || 'l' } %>');
600 % if ($types->[$col] eq 'immutable') {
602 % if ( ref($field) eq 'CODE' ) {
603 % $value = &{$field}($row,$param);
605 % $value = $param->{"$field$row"};
607 var my_text = document.createTextNode('<% $value %>');
608 my_cell.appendChild(my_text);
611 var my_input = document.createElement('INPUT');
612 my_input.setAttribute('name', '<% $field %>'+<% $opt{prefix} %>rownum);
613 my_input.setAttribute('id', '<% $field %>'+<% $opt{prefix} %>rownum);
614 my_input.style.textAlign = '<% $align{ $opt{align}->[$col] || 'l' } %>';
615 my_input.setAttribute('size', <% $sizes->[$col] || 10 %>);
616 % if ($types->[$col] eq 'immutable') {
617 my_input.setAttribute('type', 'hidden');
619 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
620 my_input.onchange = <% $opt{prefix} %>calc_total<%$col%>;
621 my_input.onkeyup = <% $opt{prefix} %>calc_total<%$col%>;
623 my_cell.appendChild(my_input);
625 row.appendChild(my_cell);
630 var balance_cell = document.createElement('TD');
632 var balance_input = document.createElement('INPUT');
633 balance_input.setAttribute('name', 'balance'+<% $opt{prefix} %>rownum);
634 balance_input.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum);
635 balance_input.style.textAlign = 'right';
636 balance_input.setAttribute('size', 8);
637 balance_input.setAttribute('maxlength', 12);
638 balance_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
639 balance_input.disabled = true;
640 balance_input.style.backgroundColor = '#dddddd';
641 balance_cell.appendChild(balance_input);
643 row.appendChild(balance_cell);
645 //update the total # of rows display
646 if ( <% $opt{prefix} %>rownum == 1 ) {
647 <% $opt{prefix} %>total_el.innerHTML =
649 + <% $opt{prefix} %>rownum
650 + ' <% $opt{name_singular} || 'customer' %>';
652 <% $opt{prefix} %>total_el.innerHTML =
654 + <% $opt{prefix} %>rownum
655 + ' <% PL($opt{name_singular} || 'customer') %>';
658 <% $opt{prefix} %>rownum++;
662 % unless ($cgi->param('error')) {
663 <% $opt{prefix} %>addRow();
671 $opt{prefix} = '' unless defined $opt{prefix};
672 $opt{prefix} .= '_' if $opt{prefix};
674 my $types = $opt{'types'} ? [ @{$opt{'types'}} ] : [];
675 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
677 my $param = $opt{param};
678 $param = $cgi->Vars if $cgi->param('error');
680 $opt{$_} ||= [] foreach qw(align color footer footer_align);
682 my @total = map 0, @{$opt{footer}};