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 update_customer(searchrow, customerArray) {
73 var custnum_obj = document.getElementById('custnum'+searchrow);
74 var customer = document.getElementById('customer'+searchrow);
75 var customer_select = document.getElementById('cust_select'+searchrow);
77 custnum_obj.disabled = false;
78 custnum_obj.style.backgroundColor = '#ffffff';
79 customer.disabled = false;
80 customer.style.backgroundColor = '#ffffff';
82 if ( customerArray.length == 0 ) {
84 custnum_obj.value = 'Not found';
85 customer.value = 'Not found';
86 custnum_obj.style.color = '#ff0000';
87 customer.style.color = '#ff0000';
89 customer.style.display = '';
90 customer_select.style.display = 'none';
93 } else if ( customerArray.length == 5 ) {
95 custnum_obj.value = customerArray[0];
96 custnum_obj.style.color = '#000000';
97 customer.value = customerArray[1];
99 update_balance_text(searchrow, customerArray[2]);
100 update_status_text( searchrow, customerArray[3]);
101 update_status_color(searchrow, '#'+customerArray[4]);
103 customer.style.display = '';
104 customer_select.style.display = 'none';
109 function <% $opt{prefix} %>search_invnum() {
111 this.style.color = '#000000'
113 var invnum_obj = this;
114 var searchrow = this.getAttribute('rownum');
115 var invnum = this.value;
117 if ( invnum == 'searching...' || invnum == 'Not found' || invnum == '' )
120 if ( this.getAttribute('magic') == 'nosearch' ) {
121 this.setAttribute('magic', '');
125 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
126 <% $opt{prefix} %>addRow();
128 var customer = document.getElementById('customer'+searchrow);
129 customer.value = 'searching...';
130 customer.disabled = true;
131 customer.style.color = '#000000';
132 customer.style.backgroundColor = '#dddddd';
134 var customer_select = document.getElementById('cust_select'+searchrow);
136 customer.style.display = '';
137 customer_select.style.display = 'none';
139 var custnum_obj = document.getElementById('custnum'+searchrow);
140 update_balance_text(searchrow, '');
141 update_status_text(searchrow, '');
142 update_status_color(searchrow, '#000000');
144 function search_invnum_update(customers) {
146 var customerArray = eval('(' + customers + ')');
147 update_customer(searchrow, customerArray);
149 % if ( $opt{invnum_update_callback} ) {
150 <% $opt{invnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
155 invnum_search( invnum, search_invnum_update );
159 function <% $opt{prefix} %>search_custnum() {
161 this.style.color = '#000000'
163 var custnum_obj = this;
164 var searchrow = this.getAttribute('rownum');
165 var custnum = this.value;
167 if ( custnum == 'searching...' || custnum == 'Not found' || custnum == '' )
170 if ( this.getAttribute('magic') == 'nosearch' ) {
171 this.setAttribute('magic', '');
175 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
176 <% $opt{prefix} %>addRow();
178 var customer = document.getElementById('customer'+searchrow);
179 customer.value = 'searching...';
180 customer.disabled = true;
181 customer.style.color = '#000000';
182 customer.style.backgroundColor = '#dddddd';
184 var customer_select = document.getElementById('cust_select'+searchrow);
186 customer.style.display = '';
187 customer_select.style.display = 'none';
189 var invnum = document.getElementById('invnum'+searchrow);
192 update_balance_text(searchrow, '');
193 update_status_text( searchrow, '');
194 update_status_color(searchrow, '#000000');
196 function search_custnum_update(customers) {
198 var customerArray = eval('(' + customers + ')') || [];
199 update_customer(searchrow, customerArray);
201 % if ( $opt{custnum_update_callback} ) {
202 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
206 custnum_search(custnum, search_custnum_update );
210 function <% $opt{prefix} %>search_customer() {
212 var customer_obj = this;
213 var searchrow = this.getAttribute('rownum');
214 var customer = this.value;
216 if ( customer == 'searching...' || customer == 'Not found' || customer == '' )
219 if ( this.getAttribute('magic') == 'nosearch' ) {
220 this.setAttribute('magic', '');
224 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
225 <% $opt{prefix} %>addRow();
228 var invnum = document.getElementById('invnum'+searchrow);
231 var custnum_obj = document.getElementById('custnum'+searchrow);
232 custnum_obj.value = 'searching...';
233 custnum_obj.disabled = true;
234 custnum_obj.style.color = '#000000';
235 custnum_obj.style.backgroundColor = '#dddddd';
237 var customer_select = document.getElementById('cust_select'+searchrow);
239 function search_customer_update(customers) {
241 var customerArrayArray = eval('(' + customers + ')') || [ [] ];
243 custnum_obj.disabled = false;
244 custnum_obj.style.backgroundColor = '#ffffff';
246 if ( customerArrayArray.length == 1 ) {
248 update_customer(customerArrayArray[1]);
249 % if ( $opt{custnum_update_callback} ) {
250 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
255 custnum_obj.value = 'Multiple'; // or something
256 custnum_obj.style.color = '#ff0000';
258 //blank the current list
259 customer_select.options.length = 0;
261 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
262 //add the multiple customers
263 for ( var s = 0; s < customerArrayArray.length; s++ ) {
265 JSON.stringify(customerArrayArray[s]),
266 customerArrayArray[s][1],
270 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
272 customer_obj.style.display = 'none';
274 customer_select.style.display = '';
280 smart_search( customer, search_customer_update );
284 function select_customer() {
286 var custnum_balance_status = this.options[this.selectedIndex].value;
287 var customer = this.options[this.selectedIndex].text;
289 var searchrow = this.getAttribute('rownum');
290 var custnum_obj = document.getElementById('custnum'+searchrow);
291 var customer_obj = document.getElementById('customer'+searchrow);
292 var balance_obj = document.getElementById('balance'+searchrow);
293 var status_obj = document.getElementById('status'+searchrow);
295 if ( custnum_balance_status == '' ) {
297 } else if ( custnum_balance_status == 'cancel' ) {
299 custnum_obj.value = '';
300 custnum_obj.style.color = '#000000';
302 this.style.display = 'none';
303 customer_obj.style.display = '';
304 customer_obj.focus();
308 update_customer(searchrow, JSON.parse(custnum_balance_status));
310 % if ( $opt{custnum_update_callback} ) {
311 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
318 function opt(what,value,text,color) {
319 var optionName = new Option(text, value, false, false);
320 optionName.style.color = color;
321 var length = what.length;
322 what.options[length] = optionName;
325 function update_status_text(rownum, newval) {
326 document.getElementById('status'+rownum).value = newval;
327 document.getElementById('status'+rownum+'_text').innerHTML = newval;
330 function update_status_color(rownum, newval) {
331 document.getElementById('statuscolor'+rownum).value = newval;
332 document.getElementById('status'+rownum+'_text').style.color = newval;
335 function update_balance_text(rownum, newval) {
336 document.getElementById('balance'+rownum).value = newval;
337 document.getElementById('balance'+rownum+'_text').innerHTML = newval;
344 <TABLE ID="<% $opt{prefix} %>OneTrueTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
352 % foreach my $header ( @{$opt{header}} ) {
353 <TH><% $header %></TH>
357 % for ( $row = 0; exists($param->{"custnum$row"}); $row++ ) {
362 NAME = "invnum<% $row %>"
363 ID = "invnum<% $row %>"
366 STYLE = "text-align:right;"
367 VALUE = "<% $param->{"invnum$row"} %>"
368 rownum = "<% $row %>"
370 <SCRIPT TYPE="text/javascript">
371 var invnum_input<% $row %> = document.getElementById("invnum<% $row %>");
372 invnum_input<% $row %>.onfocus = clearhint_invnum;
373 invnum_input<% $row %>.onchange = <% $opt{prefix} %>search_invnum;
379 NAME = "custnum<% $row %>"
380 ID = "custnum<% $row %>"
383 STYLE = "text-align:right;"
384 VALUE = "<% $param->{"custnum$row"} %>"
385 rownum = "<% $row %>"
387 <SCRIPT TYPE="text/javascript">
388 var custnum_input<% $row %> = document.getElementById("custnum<% $row %>");
389 custnum_input<% $row %>.onfocus = clearhint_custnum;
390 custnum_input<% $row %>.onchange = <% $opt{prefix} %>search_custnum;
394 <TD STYLE="text-align: center">
396 ID = "status<% $row %>_text"
397 rownum = "<% $row %>"
398 STYLE = "font-weight: bold;
399 color: <%$param->{"statuscolor$row"} || '#000000'%>"
401 ><% $param->{"status$row"} %></SPAN>
402 <INPUT TYPE = "hidden"
403 NAME = "status<% $row %>"
404 ID = "status<% $row %>"
405 VALUE = "<% $param->{"status$row"} %>"
406 rownum = "<% $row %>"
408 <INPUT TYPE = "hidden"
409 NAME = "statuscolor<% $row %>"
410 ID = "statuscolor<% $row %>"
411 VALUE = "<% $param->{"statuscolor$row"} %>"
412 rownum = "<% $row %>"
417 <INPUT TYPE="text" NAME="customer<% $row %>" ID="customer<% $row %>" SIZE=64 VALUE="<% $param->{"customer$row"} %>" rownum="<% $row %>">
418 <SCRIPT TYPE="text/javascript">
419 var customer_input<% $row %> = document.getElementById("customer<% $row %>");
420 customer_input<% $row %>.onfocus = clearhint_customer;
421 customer_input<% $row %>.onclick = clearhint_customer;
422 customer_input<% $row %>.onchange = <% $opt{prefix} %>search_customer;
424 <SELECT NAME="cust_select<% $row %>" ID="cust_select<% $row %>" rownum="<% $row %>" STYLE="color:#ff0000; display:none">
426 <SCRIPT TYPE="text/javascript">
427 var customer_select<% $row %> = document.getElementById("cust_select<% $row %>");
428 customer_select<% $row %>.onchange = select_customer;
432 <TD STYLE="text-align:right">
435 ID = "balance<% $row %>_text"
436 rownum = "<% $row %>"
437 ><% $param->{"balance$row"} %></SPAN>
439 <INPUT TYPE = "hidden"
440 NAME = "balance<% $row %>"
441 ID = "balance<% $row %>"
442 VALUE = "<% $param->{"balance$row"} %>"
443 rownum = "<% $row %>"
448 % foreach my $field ( @{$opt{fields}} ) {
450 % if ( ref($field) eq 'CODE' ) {
451 % $value = &{$field}($row,$param);
453 % $value = $param->{"$field$row"};
455 % my $name = (ref($field) eq 'CODE') ? "column${col}_$row" : "$field$row";
456 % my $align = $align{ $opt{align}->[$col] || 'l' };
457 % my $size = $sizes->[$col] || 10;
458 % my $color = $opt{color}->[$col];
459 % my $font = $color ? qq(<FONT COLOR="$color">) : '';
461 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
462 % $total[$col] += $value;
463 % $onchange = $opt{prefix}. "calc_total$col();";
464 % $onchange = qq(onchange="$onchange" onkeyup="$onchange");
466 <TD ALIGN="<% $align %>">
467 % if (! $types->[$col] || $types->[$col] eq 'text') {
472 STYLE = "text-align: <% $align %>;"
473 VALUE = "<% $value %>"
476 % } elsif ($types->[$col] eq 'immutable') {
477 <% $font %><% $value %><% $font ? '</FONT>' : '' %>
478 <INPUT TYPE="hidden" ID="<% $name %>" NAME="<% $name %>" VALUE="<% $value %>" >
480 Cannot represent unknown type: <% $types->[$col] %>
489 <TH COLSPAN=5 ID="<% $opt{'prefix'} %>_TOTAL_TOTAL">
490 Total <% $row ? $row-1 : 0 %>
491 <% PL($opt{name_singular} || 'customer', ( $row ? $row-1 : 0 ) ) %>
494 % foreach my $footer ( @{$opt{footer}} ) {
495 % my $align = $align{ $opt{'footer_align'}->[$col] || 'c' };
496 % if ($footer eq '_TOTAL' ) {
497 % my $id = $opt{'fields'}->[$col];
498 % $id = ref($id) ? "column${col}_TOTAL" : "${id}_TOTAL";
499 <TH ALIGN="<% $align %>" ID="<% $id %>"> <% sprintf('%.2f', $total[$col] ) %></TH>
501 <TH ALIGN="<% $align %>"><% $footer %></TH>
509 <SCRIPT TYPE="text/javascript">
511 % foreach my $footer ( @{$opt{footer}} ) {
512 % if ($footer eq '_TOTAL' ) {
513 % my $name = $opt{fields}->[$col];
514 % $name = ref($name) ? "column$col" : $name;
515 var <% $opt{prefix}.$name %>_CACHE = new Array ();
516 var <% $opt{prefix} %>th_el = document.getElementById("<%$name%>_TOTAL");
517 function <% $opt{prefix} %>calc_total<% $col %>() {
522 ( <% $opt{prefix}.$name%>_CACHE[row] =
523 <% $opt{prefix}.$name%>_CACHE[row]
524 || document.getElementById("<%$name%>"+row)
530 var value = <%$name%>_CACHE[row].value;
531 value = parseFloat(value);
532 if ( ! isNaN(value) ) {
533 total = total + value;
536 <% $opt{prefix} %>th_el.innerHTML = ' ' + total.toFixed(2);
544 <% include('/elements/xmlhttp.html',
545 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
546 'subs' => [qw( custnum_search smart_search invnum_search )],
550 <SCRIPT TYPE="text/javascript">
552 var <% $opt{prefix} %>total_el =
553 document.getElementById("<% $opt{'prefix'} %>_TOTAL_TOTAL");
555 var <% $opt{prefix} %>rownum = <% $row %>;
557 function <% $opt{prefix} %>addRow() {
559 var table = document.getElementById('<% $opt{prefix} %>OneTrueTable');
560 var tablebody = table.getElementsByTagName('tbody').item(0);
562 var row = table.insertRow(rownum+1);
564 var invnum_cell = document.createElement('TD');
566 var invnum_input = document.createElement('INPUT');
567 invnum_input.setAttribute('name', 'invnum'+<% $opt{prefix} %>rownum);
568 invnum_input.setAttribute('id', 'invnum'+<% $opt{prefix} %>rownum);
569 invnum_input.style.textAlign = 'right';
570 invnum_input.setAttribute('size', 8);
571 invnum_input.setAttribute('maxlength', 12);
572 invnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
573 invnum_input.onfocus = clearhint_invnum;
574 invnum_input.onchange = <% $opt{prefix} %>search_invnum;
575 invnum_cell.appendChild(invnum_input);
577 row.appendChild(invnum_cell);
579 var custnum_cell = document.createElement('TD');
581 var custnum_input = document.createElement('INPUT');
582 custnum_input.setAttribute('name', 'custnum'+<% $opt{prefix} %>rownum);
583 custnum_input.setAttribute('id', 'custnum'+<% $opt{prefix} %>rownum);
584 custnum_input.style.textAlign = 'right';
585 custnum_input.setAttribute('size', 8);
586 custnum_input.setAttribute('maxlength', 12);
587 custnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
588 custnum_input.onfocus = clearhint_custnum;
589 custnum_input.onchange = <% $opt{prefix} %>search_custnum;
590 custnum_cell.appendChild(custnum_input);
592 row.appendChild(custnum_cell);
594 var status_cell = document.createElement('TD');
595 status_cell.style.textAlign = 'center';
597 var status_span = document.createElement('SPAN');
598 status_span.setAttribute('id', 'status'+<% $opt{prefix} %>rownum+'_text');
599 status_span.style.fontWeight = 'bold';
600 status_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
601 status_cell.appendChild(status_span);
603 var status_input = document.createElement('INPUT');
604 status_input.setAttribute('type', 'hidden');
605 status_input.setAttribute('name', 'status'+<% $opt{prefix} %>rownum);
606 status_input.setAttribute('id', 'status'+<% $opt{prefix} %>rownum);
607 status_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
608 status_cell.appendChild(status_input);
610 var statuscolor_input = document.createElement('INPUT');
611 statuscolor_input.setAttribute('type', 'hidden');
612 statuscolor_input.setAttribute('name', 'statuscolor'+<% $opt{prefix} %>rownum);
613 statuscolor_input.setAttribute('id', 'statuscolor'+<% $opt{prefix} %>rownum);
614 statuscolor_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
615 status_cell.appendChild(statuscolor_input);
617 row.appendChild(status_cell);
619 var customer_cell = document.createElement('TD');
621 var customer_input = document.createElement('INPUT');
622 customer_input.setAttribute('name', 'customer'+<% $opt{prefix} %>rownum);
623 customer_input.setAttribute('id', 'customer'+<% $opt{prefix} %>rownum);
624 customer_input.setAttribute('size', 64);
625 customer_input.setAttribute('value', '(last name or company)' );
626 customer_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
627 customer_input.onfocus = clearhint_customer;
628 customer_input.onclick = clearhint_customer;
629 customer_input.onchange = <% $opt{prefix} %>search_customer;
630 customer_cell.appendChild(customer_input);
632 var customer_select = document.createElement('SELECT');
633 customer_select.setAttribute('name', 'cust_select'+<% $opt{prefix} %>rownum);
634 customer_select.setAttribute('id', 'cust_select'+<% $opt{prefix} %>rownum);
635 customer_select.setAttribute('rownum', <% $opt{prefix} %>rownum);
636 customer_select.style.color = '#ff0000';
637 customer_select.style.display = 'none';
638 customer_select.onchange = select_customer;
639 customer_cell.appendChild(customer_select);
641 row.appendChild(customer_cell);
643 var balance_cell = document.createElement('TD');
645 balance_cell.style.textAlign = 'right';
646 balance_cell.appendChild(document.createTextNode('<%$money_char%>'));
648 var balance_span = document.createElement('SPAN');
649 balance_span.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum+'_text');
650 balance_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
651 balance_cell.appendChild(balance_span);
653 balance_cell.appendChild(
654 document.createTextNode(String.fromCharCode(160)) //
657 var balance_input = document.createElement('INPUT');
658 balance_input.setAttribute('type', 'hidden');
659 balance_input.setAttribute('name', 'balance'+<% $opt{prefix} %>rownum);
660 balance_input.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum);
661 balance_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
662 balance_cell.appendChild(balance_input);
664 row.appendChild(balance_cell);
667 % foreach my $field ( @{$opt{fields}} ) {
669 var my_cell = document.createElement('TD');
670 my_cell.setAttribute('align', '<% $align{ $opt{align}->[$col] || 'l' } %>');
672 % if ($types->[$col] eq 'immutable') {
674 % if ( ref($field) eq 'CODE' ) {
675 % $value = &{$field}($row,$param);
677 % $value = $param->{"$field$row"};
679 var my_text = document.createTextNode('<% $value %>');
680 my_cell.appendChild(my_text);
683 % my $name = (ref($field) eq 'CODE') ? "column${col}_" : $field;
684 var my_input = document.createElement('INPUT');
685 my_input.setAttribute('name', '<% $name %>'+<% $opt{prefix} %>rownum);
686 my_input.setAttribute('id', '<% $name %>'+<% $opt{prefix} %>rownum);
687 my_input.style.textAlign = '<% $align{ $opt{align}->[$col] || 'l' } %>';
688 my_input.setAttribute('size', <% $sizes->[$col] || 10 %>);
689 % if ($types->[$col] eq 'immutable') {
690 my_input.setAttribute('type', 'hidden');
692 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
693 my_input.onchange = <% $opt{prefix} %>calc_total<%$col%>;
694 my_input.onkeyup = <% $opt{prefix} %>calc_total<%$col%>;
696 my_cell.appendChild(my_input);
698 row.appendChild(my_cell);
703 //update the total # of rows display
704 if ( <% $opt{prefix} %>rownum == 1 ) {
705 <% $opt{prefix} %>total_el.innerHTML =
707 + <% $opt{prefix} %>rownum
708 + ' <% $opt{name_singular} || 'customer' %>';
710 <% $opt{prefix} %>total_el.innerHTML =
712 + <% $opt{prefix} %>rownum
713 + ' <% PL($opt{name_singular} || 'customer') %>';
716 <% $opt{prefix} %>rownum++;
720 % unless ($cgi->param('error')) {
721 <% $opt{prefix} %>addRow();
728 my $conf = new FS::Conf;
730 $opt{prefix} = '' unless defined $opt{prefix};
731 $opt{prefix} .= '_' if $opt{prefix};
733 my $types = $opt{'type'} ? [ @{$opt{'type'}} ] : [];
734 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
736 my $param = $opt{param};
737 $param = $cgi->Vars if $cgi->param('error');
739 $opt{$_} ||= [] foreach qw(align color footer footer_align);
741 my @total = map 0, @{$opt{footer}};
749 my $money_char = $conf->config('money_char') || '$';