5 include( '/elements/customer-table.html',
12 'header' => [ '#', 'Item' ],
15 sub { my ($row,$param) = @_;
16 $param->{"column$row"};
24 'name_singular' => 'customer', #label
27 'types' => ['immutable', ''], # immutable or ''/text
28 'align' => [ 'c', 'l', 'r', '' ],
29 'size' => [], # sizes ignored for immutable
31 'footer' => ['string', '_TOTAL'], # strings or the special
33 'footer_align' => [ 'c', 'l', 'r', '' ],
35 'param' => { column0 => 1 }, # preset column of row 0 to 1
41 <SCRIPT TYPE="text/javascript">
43 function clearhint_custnum() {
45 if ( this.value == 'Not found' || this.value == 'Multiple' ) {
47 this.style.color = '#000000';
52 function clearhint_customer() {
54 this.style.color = '#000000';
56 if ( this.value == '(last name or company)' || this.value == 'Not found' )
61 function <% $opt{prefix} %>search_custnum() {
63 this.style.color = '#000000'
65 var custnum_obj = this;
66 var searchrow = this.getAttribute('rownum');
67 var custnum = this.value;
69 if ( custnum == 'searching...' || custnum == 'Not found' || custnum == '' )
72 if ( this.getAttribute('magic') == 'nosearch' ) {
73 this.setAttribute('magic', '');
77 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
78 <% $opt{prefix} %>addRow();
80 var customer = document.getElementById('customer'+searchrow);
81 customer.value = 'searching...';
82 customer.disabled = true;
83 customer.style.color = '#000000';
84 customer.style.backgroundColor = '#dddddd';
86 var customer_select = document.getElementById('cust_select'+searchrow);
88 customer.style.display = '';
89 customer_select.style.display = 'none';
91 function search_custnum_update(name) {
93 var name = eval('(' + name + ')' );
95 customer.disabled = false;
96 customer.style.backgroundColor = '#ffffff';
98 if ( name.length > 0 ) {
99 customer.value = name;
100 customer.setAttribute('magic', 'nosearch');
102 customer.value = 'Not found';
103 customer.style.color = '#ff0000';
104 custnum_obj.style.color = '#ff0000';
110 custnum_search( custnum, search_custnum_update );
114 function <% $opt{prefix} %>search_customer() {
116 var customer_obj = this;
117 var searchrow = this.getAttribute('rownum');
118 var customer = this.value;
120 if ( customer == 'searching...' || customer == 'Not found' || customer == '' )
123 if ( this.getAttribute('magic') == 'nosearch' ) {
124 this.setAttribute('magic', '');
128 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
129 <% $opt{prefix} %>addRow();
132 var custnum_obj = document.getElementById('custnum'+searchrow);
133 custnum_obj.value = 'searching...';
134 custnum_obj.disabled = true;
135 custnum_obj.style.color = '#000000';
136 custnum_obj.style.backgroundColor = '#dddddd';
138 var customer_select = document.getElementById('cust_select'+searchrow);
140 function search_customer_update(customers) {
142 var customerArray = eval('(' + customers + ')');
144 custnum_obj.disabled = false;
145 custnum_obj.style.backgroundColor = '#ffffff';
147 if ( customerArray.length == 0 ) {
149 custnum_obj.value = 'Not found';
150 custnum_obj.style.color = '#ff0000';
151 customer_obj.style.color = '#ff0000';
153 customer_obj.style.display = '';
154 customer_select.style.display = 'none';
157 } else if ( customerArray.length == 1 ) {
159 custnum_obj.value = customerArray[0][0];
160 customer_obj.value = customerArray[0][1];
162 customer_obj.style.display = '';
163 customer_select.style.display = 'none';
168 custnum_obj.value = 'Multiple'; // or something
169 custnum_obj.style.color = '#ff0000';
171 //blank the current list
172 for ( var i = customer_select.length; i >= 0; i-- )
173 customer_select.options[i] = null;
175 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
177 //add the multiple customers
178 for ( var s = 0; s < customerArray.length; s++ )
179 opt(customer_select, customerArray[s][0], customerArray[s][1], '#000000');
181 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
183 customer_obj.style.display = 'none';
185 customer_select.style.display = '';
191 smart_search( customer, search_customer_update );
195 function select_customer() {
197 var custnum = this.options[this.selectedIndex].value;
198 var customer = this.options[this.selectedIndex].text;
200 var searchrow = this.getAttribute('rownum');
201 var custnum_obj = document.getElementById('custnum'+searchrow);
202 var customer_obj = document.getElementById('customer'+searchrow);
204 if ( custnum == '' ) {
206 } else if ( custnum == 'cancel' ) {
208 custnum_obj.value = '';
209 custnum_obj.style.color = '#000000';
211 this.style.display = 'none';
212 customer_obj.style.display = '';
213 customer_obj.focus();
217 custnum_obj.value = custnum;
218 custnum_obj.style.color = '#000000';
220 customer_obj.value = customer;
221 customer_obj.style.color = '#000000';
223 this.style.display = 'none';
224 customer_obj.style.display = '';
230 function opt(what,value,text,color) {
231 var optionName = new Option(text, value, false, false);
232 optionName.style.color = color;
233 var length = what.length;
234 what.options[length] = optionName;
239 <TABLE ID="<% $opt{prefix} %>OneTrueTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
244 % foreach my $header ( @{$opt{header}} ) {
245 <TH><% $header %></TH>
249 % for ( $row = 0; exists($param->{"custnum$row"}); $row++ ) {
255 NAME = "custnum<% $row %>"
256 ID = "custnum<% $row %>"
259 STYLE = "text-align:right;"
260 VALUE = "<% $param->{"custnum$row"} %>"
261 rownum = "<% $row %>"
263 <SCRIPT TYPE="text/javascript">
264 var custnum_input<% $row %> = document.getElementById("custnum<% $row %>");
265 custnum_input<% $row %>.onfocus = clearhint_custnum;
266 custnum_input<% $row %>.onchange = <% $opt{prefix} %>search_custnum;
271 <INPUT TYPE="text" NAME="customer<% $row %>" ID="customer<% $row %>" SIZE=64 VALUE="<% $param->{"customer$row"} %>" rownum="<% $row %>">
272 <SCRIPT TYPE="text/javascript">
273 var customer_input<% $row %> = document.getElementById("customer<% $row %>");
274 customer_input<% $row %>.onfocus = clearhint_customer;
275 customer_input<% $row %>.onclick = clearhint_customer;
276 customer_input<% $row %>.onchange = <% $opt{prefix} %>search_customer;
278 <SELECT NAME="cust_select<% $row %>" ID="cust_select<% $row %>" rownum="<% $row %>" STYLE="color:#ff0000; display:none">
280 <SCRIPT TYPE="text/javascript">
281 var customer_select<% $row %> = document.getElementById("cust_select<% $row %>");
282 customer_select<% $row %>.onchange = select_customer;
287 % foreach my $field ( @{$opt{fields}} ) {
289 % if ( ref($field) eq 'CODE' ) {
290 % $value = &{$field}($row,$param);
292 % $value = $param->{"$field$row"};
294 % my $name = (ref($field) eq 'CODE') ? "column${col}_$row" : "$field$row";
295 % my $align = $align{ $opt{align}->[$col] || 'l' };
296 % my $size = $sizes->[$col] || 10;
297 % my $color = $opt{color}->[$col];
298 % my $font = $color ? qq(<FONT COLOR="$color">) : '';
300 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
301 % $total[$col] += $value;
302 % $onchange = $opt{prefix}. "calc_total$col();";
303 % $onchange = qq(onchange="$onchange" onkeyup="$onchange");
305 <TD ALIGN="<% $align %>">
306 % if (! $types->[$col] || $types->[$col] eq 'text') {
311 STYLE = "text-align: <% $align %>;"
312 VALUE = "<% $value %>"
315 % } elsif ($types->[$col] eq 'immutable') {
316 <% $font %><% $value %><% $font ? '</FONT>' : '' %>
317 <INPUT TYPE="hidden" NAME="<% $name %>" VALUE="<% $value %>" >
319 Cannot represent unknown type: <% $types->[$col] %>
329 <TH COLSPAN=2 ID="<% $opt{'prefix'} %>_TOTAL_TOTAL">
330 Total <% $row ? $row-1 : 0 %>
331 <% PL($opt{name_singular} || 'customer', ( $row ? $row-1 : 0 ) ) %>
334 % foreach my $footer ( @{$opt{footer}} ) {
335 % my $align = $align{ $opt{'footer_align'}->[$col] || 'c' };
336 % if ($footer eq '_TOTAL' ) {
337 % my $id = $opt{'fields'}->[$col];
338 % $id = ref($id) ? "column${col}_TOTAL" : "${id}_TOTAL";
339 <TH ALIGN="<% $align %>" ID="<% $id %>"> <% sprintf('%.2f', $total[$col] ) %></TH>
341 <TH ALIGN="<% $align %>"><% $footer %></TH>
349 <SCRIPT TYPE="text/javascript">
351 % foreach my $footer ( @{$opt{footer}} ) {
352 % if ($footer eq '_TOTAL' ) {
353 % my $name = $opt{fields}->[$col];
354 % $name = ref($name) ? "column$col" : $name;
355 var <% $opt{prefix}.$name %>_CACHE = new Array ();
356 var <% $opt{prefix} %>th_el = document.getElementById("<%$name%>_TOTAL");
357 function <% $opt{prefix} %>calc_total<% $col %>() {
362 ( <% $opt{prefix}.$name%>_CACHE[row] =
363 <% $opt{prefix}.$name%>_CACHE[row]
364 || document.getElementById("<%$name%>"+row)
370 var value = <%$name%>_CACHE[row].value;
371 value = parseFloat(value);
372 if ( ! isNaN(value) ) {
373 total = total + value;
376 <% $opt{prefix} %>th_el.innerHTML = ' ' + total.toFixed(2);
384 <% include('/elements/xmlhttp.html',
385 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
386 'subs' => [qw( custnum_search smart_search )],
390 <SCRIPT TYPE="text/javascript">
392 var <% $opt{prefix} %>total_el =
393 document.getElementById("<% $opt{'prefix'} %>_TOTAL_TOTAL");
395 var <% $opt{prefix} %>rownum = <% $row %>;
397 function <% $opt{prefix} %>addRow() {
399 var table = document.getElementById('<% $opt{prefix} %>OneTrueTable');
400 var tablebody = table.getElementsByTagName('tbody').item(0);
402 var row = table.insertRow(rownum+1);
404 var custnum_cell = document.createElement('TD');
406 var custnum_input = document.createElement('INPUT');
407 custnum_input.setAttribute('name', 'custnum'+<% $opt{prefix} %>rownum);
408 custnum_input.setAttribute('id', 'custnum'+<% $opt{prefix} %>rownum);
409 custnum_input.style.textAlign = 'right';
410 custnum_input.setAttribute('size', 8);
411 custnum_input.setAttribute('maxlength', 12);
412 custnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
413 custnum_input.onfocus = clearhint_custnum;
414 custnum_input.onchange = <% $opt{prefix} %>search_custnum;
415 custnum_cell.appendChild(custnum_input);
417 row.appendChild(custnum_cell);
419 var customer_cell = document.createElement('TD');
421 var customer_input = document.createElement('INPUT');
422 customer_input.setAttribute('name', 'customer'+<% $opt{prefix} %>rownum);
423 customer_input.setAttribute('id', 'customer'+<% $opt{prefix} %>rownum);
424 customer_input.setAttribute('size', 64);
425 customer_input.setAttribute('value', '(last name or company)' );
426 customer_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
427 customer_input.onfocus = clearhint_customer;
428 customer_input.onclick = clearhint_customer;
429 customer_input.onchange = <% $opt{prefix} %>search_customer;
430 customer_cell.appendChild(customer_input);
432 var customer_select = document.createElement('SELECT');
433 customer_select.setAttribute('name', 'cust_select'+<% $opt{prefix} %>rownum);
434 customer_select.setAttribute('id', 'cust_select'+<% $opt{prefix} %>rownum);
435 customer_select.setAttribute('rownum', <% $opt{prefix} %>rownum);
436 customer_select.style.color = '#ff0000';
437 customer_select.style.display = 'none';
438 customer_select.onchange = select_customer;
439 customer_cell.appendChild(customer_select);
441 row.appendChild(customer_cell);
444 % foreach my $field ( @{$opt{fields}} ) {
446 var my_cell = document.createElement('TD');
447 my_cell.setAttribute('align', '<% $align{ $opt{align}->[$col] || 'l' } %>');
449 % if ($types->[$col] eq 'immutable') {
451 % if ( ref($field) eq 'CODE' ) {
452 % $value = &{$field}($row,$param);
454 % $value = $param->{"$field$row"};
456 var my_text = document.createTextNode('<% $value %>');
457 my_cell.appendChild(my_text);
460 var my_input = document.createElement('INPUT');
461 my_input.setAttribute('name', '<% $field %>'+<% $opt{prefix} %>rownum);
462 my_input.setAttribute('id', '<% $field %>'+<% $opt{prefix} %>rownum);
463 my_input.style.textAlign = '<% $align{ $opt{align}->[$col] || 'l' } %>';
464 my_input.setAttribute('size', <% $sizes->[$col] || 10 %>);
465 % if ($types->[$col] eq 'immutable') {
466 my_input.setAttribute('type', 'hidden');
468 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
469 my_input.onchange = <% $opt{prefix} %>calc_total<%$col%>;
470 my_input.onkeyup = <% $opt{prefix} %>calc_total<%$col%>;
472 my_cell.appendChild(my_input);
474 row.appendChild(my_cell);
479 //update the total # of rows display
480 if ( <% $opt{prefix} %>rownum == 1 ) {
481 <% $opt{prefix} %>total_el.innerHTML =
483 + <% $opt{prefix} %>rownum
484 + ' <% $opt{name_singular} || 'customer' %>';
486 <% $opt{prefix} %>total_el.innerHTML =
488 + <% $opt{prefix} %>rownum
489 + ' <% PL($opt{name_singular} || 'customer') %>';
492 <% $opt{prefix} %>rownum++;
496 % unless ($cgi->param('error')) {
497 <% $opt{prefix} %>addRow();
505 $opt{prefix} = '' unless defined $opt{prefix};
506 $opt{prefix} .= '_' if $opt{prefix};
508 my $types = $opt{'types'} ? [ @{$opt{'types'}} ] : [];
509 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
511 my $param = $opt{param};
512 $param = $cgi->Vars if $cgi->param('error');
514 $opt{$_} ||= [] foreach qw(align color footer footer_align);
516 my @total = map 0, @{$opt{footer}};