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();
179 var customer_obj = document.getElementById('customer'+searchrow);
180 customer_obj.value = 'searching...';
181 customer_obj.disabled = true;
182 customer_obj.style.color = '#000000';
183 customer_obj.style.backgroundColor = '#dddddd';
185 var customer_select = document.getElementById('cust_select'+searchrow);
187 customer_obj.style.display = '';
188 customer_select.style.display = 'none';
190 var invnum = document.getElementById('invnum'+searchrow);
193 update_balance_text(searchrow, '');
194 update_status_text( searchrow, '');
195 update_status_color(searchrow, '#000000');
197 function search_custnum_update(customers) {
199 var customerArrayArray = eval('(' + customers + ')') || [];
201 if ( customerArrayArray.length == 0 ) {
203 update_customer(searchrow, []);
205 } else if ( customerArrayArray.length == 1 ) {
207 update_customer(searchrow, customerArrayArray[0]);
208 % if ( $opt{custnum_update_callback} ) {
209 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
214 custnum_obj.value = 'Multiple'; // or something
215 custnum_obj.style.color = '#ff0000';
217 //blank the current list
218 customer_select.options.length = 0;
220 opt(customer_select, '', 'Multiple customers match "' + custnum + '" - select one', '#ff0000');
221 //add the multiple customers
222 for ( var s = 0; s < customerArrayArray.length; s++ ) {
224 JSON.stringify(customerArrayArray[s]),
225 customerArrayArray[s][1],
229 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
231 customer_obj.style.display = 'none';
233 customer_select.style.display = '';
239 custnum_search(custnum, search_custnum_update );
243 function <% $opt{prefix} %>search_customer() {
245 var customer_obj = this;
246 var searchrow = this.getAttribute('rownum');
247 var customer = this.value;
249 if ( customer == 'searching...' || customer == 'Not found' || customer == '' )
252 if ( this.getAttribute('magic') == 'nosearch' ) {
253 this.setAttribute('magic', '');
257 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
258 <% $opt{prefix} %>addRow();
261 var invnum = document.getElementById('invnum'+searchrow);
264 var custnum_obj = document.getElementById('custnum'+searchrow);
265 custnum_obj.value = 'searching...';
266 custnum_obj.disabled = true;
267 custnum_obj.style.color = '#000000';
268 custnum_obj.style.backgroundColor = '#dddddd';
270 var customer_select = document.getElementById('cust_select'+searchrow);
272 function search_customer_update(customers) {
274 var customerArrayArray = eval('(' + customers + ')') || [ [] ];
276 custnum_obj.disabled = false;
277 custnum_obj.style.backgroundColor = '#ffffff';
279 if ( customerArrayArray.length == 0 ) {
281 update_customer(searchrow, []);
283 } else if ( customerArrayArray.length == 1 ) {
285 update_customer(searchrow, customerArrayArray[0]);
286 % if ( $opt{custnum_update_callback} ) {
287 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
292 custnum_obj.value = 'Multiple'; // or something
293 custnum_obj.style.color = '#ff0000';
295 //blank the current list
296 customer_select.options.length = 0;
298 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
299 //add the multiple customers
300 for ( var s = 0; s < customerArrayArray.length; s++ ) {
302 JSON.stringify(customerArrayArray[s]),
303 customerArrayArray[s][1],
307 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
309 customer_obj.style.display = 'none';
311 customer_select.style.display = '';
317 smart_search( customer, search_customer_update );
321 function select_customer() {
323 var custnum_balance_status = this.options[this.selectedIndex].value;
324 var customer = this.options[this.selectedIndex].text;
326 var searchrow = this.getAttribute('rownum');
327 var custnum_obj = document.getElementById('custnum'+searchrow);
328 var customer_obj = document.getElementById('customer'+searchrow);
329 var balance_obj = document.getElementById('balance'+searchrow);
330 var status_obj = document.getElementById('status'+searchrow);
332 if ( custnum_balance_status == '' ) {
334 } else if ( custnum_balance_status == 'cancel' ) {
336 custnum_obj.value = '';
337 custnum_obj.style.color = '#000000';
339 this.style.display = 'none';
340 customer_obj.style.display = '';
341 customer_obj.focus();
345 update_customer(searchrow, JSON.parse(custnum_balance_status));
347 % if ( $opt{custnum_update_callback} ) {
348 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
355 function opt(what,value,text,color) {
356 var optionName = new Option(text, value, false, false);
357 optionName.style.color = color;
358 var length = what.length;
359 what.options[length] = optionName;
362 function update_status_text(rownum, newval) {
363 document.getElementById('status'+rownum).value = newval;
364 document.getElementById('status'+rownum+'_text').innerHTML = newval;
367 function update_status_color(rownum, newval) {
368 document.getElementById('statuscolor'+rownum).value = newval;
369 document.getElementById('status'+rownum+'_text').style.color = newval;
372 function update_balance_text(rownum, newval) {
373 document.getElementById('balance'+rownum).value = newval;
374 document.getElementById('balance'+rownum+'_text').innerHTML = newval;
381 <TABLE ID="<% $opt{prefix} %>OneTrueTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
389 % foreach my $header ( @{$opt{header}} ) {
390 <TH><% $header %></TH>
394 % for ( $row = 0; exists($param->{"custnum$row"}); $row++ ) {
399 NAME = "invnum<% $row %>"
400 ID = "invnum<% $row %>"
403 STYLE = "text-align:right;"
404 VALUE = "<% $param->{"invnum$row"} %>"
405 rownum = "<% $row %>"
407 <SCRIPT TYPE="text/javascript">
408 var invnum_input<% $row %> = document.getElementById("invnum<% $row %>");
409 invnum_input<% $row %>.onfocus = clearhint_invnum;
410 invnum_input<% $row %>.onchange = <% $opt{prefix} %>search_invnum;
416 NAME = "custnum<% $row %>"
417 ID = "custnum<% $row %>"
420 STYLE = "text-align:right;"
421 VALUE = "<% $param->{"custnum$row"} %>"
422 rownum = "<% $row %>"
424 <SCRIPT TYPE="text/javascript">
425 var custnum_input<% $row %> = document.getElementById("custnum<% $row %>");
426 custnum_input<% $row %>.onfocus = clearhint_custnum;
427 custnum_input<% $row %>.onchange = <% $opt{prefix} %>search_custnum;
431 <TD STYLE="text-align: center">
433 ID = "status<% $row %>_text"
434 rownum = "<% $row %>"
435 STYLE = "font-weight: bold;
436 color: <%$param->{"statuscolor$row"} || '#000000'%>"
438 ><% $param->{"status$row"} %></SPAN>
439 <INPUT TYPE = "hidden"
440 NAME = "status<% $row %>"
441 ID = "status<% $row %>"
442 VALUE = "<% $param->{"status$row"} %>"
443 rownum = "<% $row %>"
445 <INPUT TYPE = "hidden"
446 NAME = "statuscolor<% $row %>"
447 ID = "statuscolor<% $row %>"
448 VALUE = "<% $param->{"statuscolor$row"} %>"
449 rownum = "<% $row %>"
454 <INPUT TYPE="text" NAME="customer<% $row %>" ID="customer<% $row %>" SIZE=64 VALUE="<% $param->{"customer$row"} %>" rownum="<% $row %>">
455 <SCRIPT TYPE="text/javascript">
456 var customer_input<% $row %> = document.getElementById("customer<% $row %>");
457 customer_input<% $row %>.onfocus = clearhint_customer;
458 customer_input<% $row %>.onclick = clearhint_customer;
459 customer_input<% $row %>.onchange = <% $opt{prefix} %>search_customer;
461 <SELECT NAME="cust_select<% $row %>" ID="cust_select<% $row %>" rownum="<% $row %>" STYLE="color:#ff0000; display:none">
463 <SCRIPT TYPE="text/javascript">
464 var customer_select<% $row %> = document.getElementById("cust_select<% $row %>");
465 customer_select<% $row %>.onchange = select_customer;
469 <TD STYLE="text-align:right">
472 ID = "balance<% $row %>_text"
473 rownum = "<% $row %>"
474 ><% $param->{"balance$row"} %></SPAN>
476 <INPUT TYPE = "hidden"
477 NAME = "balance<% $row %>"
478 ID = "balance<% $row %>"
479 VALUE = "<% $param->{"balance$row"} %>"
480 rownum = "<% $row %>"
485 % foreach my $field ( @{$opt{fields}} ) {
487 % if ( ref($field) eq 'CODE' ) {
488 % $value = &{$field}($row,$param);
490 % $value = $param->{"$field$row"};
492 % my $name = (ref($field) eq 'CODE') ? "column${col}_$row" : "$field$row";
493 % my $align = $align{ $opt{align}->[$col] || 'l' };
494 % my $size = $sizes->[$col] || 10;
495 % my $color = $opt{color}->[$col];
496 % my $font = $color ? qq(<FONT COLOR="$color">) : '';
498 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
499 % $total[$col] += $value;
500 % $onchange = $opt{prefix}. "calc_total$col();";
501 % $onchange = qq(onchange="$onchange" onkeyup="$onchange");
503 <TD ALIGN="<% $align %>">
504 % if (! $types->[$col] || $types->[$col] eq 'text') {
509 STYLE = "text-align: <% $align %>;"
510 VALUE = "<% $value %>"
513 % } elsif ($types->[$col] eq 'immutable') {
514 <% $font %><% $value %><% $font ? '</FONT>' : '' %>
515 <INPUT TYPE="hidden" ID="<% $name %>" NAME="<% $name %>" VALUE="<% $value %>" >
517 Cannot represent unknown type: <% $types->[$col] %>
526 <TH COLSPAN=5 ID="<% $opt{'prefix'} %>_TOTAL_TOTAL">
527 Total <% $row ? $row-1 : 0 %>
528 <% PL($opt{name_singular} || 'customer', ( $row ? $row-1 : 0 ) ) %>
531 % foreach my $footer ( @{$opt{footer}} ) {
532 % my $align = $align{ $opt{'footer_align'}->[$col] || 'c' };
533 % if ($footer eq '_TOTAL' ) {
534 % my $id = $opt{'fields'}->[$col];
535 % $id = ref($id) ? "column${col}_TOTAL" : "${id}_TOTAL";
536 <TH ALIGN="<% $align %>" ID="<% $id %>"> <% sprintf('%.2f', $total[$col] ) %></TH>
538 <TH ALIGN="<% $align %>"><% $footer %></TH>
546 <SCRIPT TYPE="text/javascript">
548 % foreach my $footer ( @{$opt{footer}} ) {
549 % if ($footer eq '_TOTAL' ) {
550 % my $name = $opt{fields}->[$col];
551 % $name = ref($name) ? "column$col" : $name;
552 var <% $opt{prefix}.$name %>_CACHE = new Array ();
553 var <% $opt{prefix} %>th_el = document.getElementById("<%$name%>_TOTAL");
554 function <% $opt{prefix} %>calc_total<% $col %>() {
559 ( <% $opt{prefix}.$name%>_CACHE[row] =
560 <% $opt{prefix}.$name%>_CACHE[row]
561 || document.getElementById("<%$name%>"+row)
567 var value = <%$name%>_CACHE[row].value;
568 value = parseFloat(value);
569 if ( ! isNaN(value) ) {
570 total = total + value;
573 <% $opt{prefix} %>th_el.innerHTML = ' ' + total.toFixed(2);
581 <% include('/elements/xmlhttp.html',
582 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
583 'subs' => [qw( custnum_search smart_search invnum_search )],
587 <SCRIPT TYPE="text/javascript">
589 var <% $opt{prefix} %>total_el =
590 document.getElementById("<% $opt{'prefix'} %>_TOTAL_TOTAL");
592 var <% $opt{prefix} %>rownum = <% $row %>;
594 function <% $opt{prefix} %>addRow() {
596 var table = document.getElementById('<% $opt{prefix} %>OneTrueTable');
597 var tablebody = table.getElementsByTagName('tbody').item(0);
599 var row = table.insertRow(rownum+1);
601 var invnum_cell = document.createElement('TD');
603 var invnum_input = document.createElement('INPUT');
604 invnum_input.setAttribute('name', 'invnum'+<% $opt{prefix} %>rownum);
605 invnum_input.setAttribute('id', 'invnum'+<% $opt{prefix} %>rownum);
606 invnum_input.style.textAlign = 'right';
607 invnum_input.setAttribute('size', 8);
608 invnum_input.setAttribute('maxlength', 12);
609 invnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
610 invnum_input.onfocus = clearhint_invnum;
611 invnum_input.onchange = <% $opt{prefix} %>search_invnum;
612 invnum_cell.appendChild(invnum_input);
614 row.appendChild(invnum_cell);
616 var custnum_cell = document.createElement('TD');
618 var custnum_input = document.createElement('INPUT');
619 custnum_input.setAttribute('name', 'custnum'+<% $opt{prefix} %>rownum);
620 custnum_input.setAttribute('id', 'custnum'+<% $opt{prefix} %>rownum);
621 custnum_input.style.textAlign = 'right';
622 custnum_input.setAttribute('size', 8);
623 custnum_input.setAttribute('maxlength', 12);
624 custnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
625 custnum_input.onfocus = clearhint_custnum;
626 custnum_input.onchange = <% $opt{prefix} %>search_custnum;
627 custnum_cell.appendChild(custnum_input);
629 row.appendChild(custnum_cell);
631 var status_cell = document.createElement('TD');
632 status_cell.style.textAlign = 'center';
634 var status_span = document.createElement('SPAN');
635 status_span.setAttribute('id', 'status'+<% $opt{prefix} %>rownum+'_text');
636 status_span.style.fontWeight = 'bold';
637 status_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
638 status_cell.appendChild(status_span);
640 var status_input = document.createElement('INPUT');
641 status_input.setAttribute('type', 'hidden');
642 status_input.setAttribute('name', 'status'+<% $opt{prefix} %>rownum);
643 status_input.setAttribute('id', 'status'+<% $opt{prefix} %>rownum);
644 status_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
645 status_cell.appendChild(status_input);
647 var statuscolor_input = document.createElement('INPUT');
648 statuscolor_input.setAttribute('type', 'hidden');
649 statuscolor_input.setAttribute('name', 'statuscolor'+<% $opt{prefix} %>rownum);
650 statuscolor_input.setAttribute('id', 'statuscolor'+<% $opt{prefix} %>rownum);
651 statuscolor_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
652 status_cell.appendChild(statuscolor_input);
654 row.appendChild(status_cell);
656 var customer_cell = document.createElement('TD');
658 var customer_input = document.createElement('INPUT');
659 customer_input.setAttribute('name', 'customer'+<% $opt{prefix} %>rownum);
660 customer_input.setAttribute('id', 'customer'+<% $opt{prefix} %>rownum);
661 customer_input.setAttribute('size', 64);
662 customer_input.setAttribute('value', '(last name or company)' );
663 customer_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
664 customer_input.onfocus = clearhint_customer;
665 customer_input.onclick = clearhint_customer;
666 customer_input.onchange = <% $opt{prefix} %>search_customer;
667 customer_cell.appendChild(customer_input);
669 var customer_select = document.createElement('SELECT');
670 customer_select.setAttribute('name', 'cust_select'+<% $opt{prefix} %>rownum);
671 customer_select.setAttribute('id', 'cust_select'+<% $opt{prefix} %>rownum);
672 customer_select.setAttribute('rownum', <% $opt{prefix} %>rownum);
673 customer_select.style.color = '#ff0000';
674 customer_select.style.display = 'none';
675 customer_select.onchange = select_customer;
676 customer_cell.appendChild(customer_select);
678 row.appendChild(customer_cell);
680 var balance_cell = document.createElement('TD');
682 balance_cell.style.textAlign = 'right';
683 balance_cell.appendChild(document.createTextNode('<%$money_char%>'));
685 var balance_span = document.createElement('SPAN');
686 balance_span.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum+'_text');
687 balance_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
688 balance_cell.appendChild(balance_span);
690 balance_cell.appendChild(
691 document.createTextNode(String.fromCharCode(160)) //
694 var balance_input = document.createElement('INPUT');
695 balance_input.setAttribute('type', 'hidden');
696 balance_input.setAttribute('name', 'balance'+<% $opt{prefix} %>rownum);
697 balance_input.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum);
698 balance_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
699 balance_cell.appendChild(balance_input);
701 row.appendChild(balance_cell);
704 % foreach my $field ( @{$opt{fields}} ) {
706 var my_cell = document.createElement('TD');
707 my_cell.setAttribute('align', '<% $align{ $opt{align}->[$col] || 'l' } %>');
709 % if ($types->[$col] eq 'immutable') {
711 % if ( ref($field) eq 'CODE' ) {
712 % $value = &{$field}($row,$param);
714 % $value = $param->{"$field$row"};
716 var my_text = document.createTextNode('<% $value %>');
717 my_cell.appendChild(my_text);
720 % my $name = (ref($field) eq 'CODE') ? "column${col}_" : $field;
721 var my_input = document.createElement('INPUT');
722 my_input.setAttribute('name', '<% $name %>'+<% $opt{prefix} %>rownum);
723 my_input.setAttribute('id', '<% $name %>'+<% $opt{prefix} %>rownum);
724 my_input.style.textAlign = '<% $align{ $opt{align}->[$col] || 'l' } %>';
725 my_input.setAttribute('size', <% $sizes->[$col] || 10 %>);
726 % if ($types->[$col] eq 'immutable') {
727 my_input.setAttribute('type', 'hidden');
729 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
730 my_input.onchange = <% $opt{prefix} %>calc_total<%$col%>;
731 my_input.onkeyup = <% $opt{prefix} %>calc_total<%$col%>;
733 my_cell.appendChild(my_input);
735 row.appendChild(my_cell);
740 //update the total # of rows display
741 if ( <% $opt{prefix} %>rownum == 1 ) {
742 <% $opt{prefix} %>total_el.innerHTML =
744 + <% $opt{prefix} %>rownum
745 + ' <% $opt{name_singular} || 'customer' %>';
747 <% $opt{prefix} %>total_el.innerHTML =
749 + <% $opt{prefix} %>rownum
750 + ' <% PL($opt{name_singular} || 'customer') %>';
753 <% $opt{prefix} %>rownum++;
757 % unless ($cgi->param('error')) {
758 <% $opt{prefix} %>addRow();
765 my $conf = new FS::Conf;
767 $opt{prefix} = '' unless defined $opt{prefix};
768 $opt{prefix} .= '_' if $opt{prefix};
770 my $types = $opt{'type'} ? [ @{$opt{'type'}} ] : [];
771 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
773 my $param = $opt{param};
774 $param = $cgi->Vars if $cgi->param('error');
776 $opt{$_} ||= [] foreach qw(align color footer footer_align);
778 my @total = map 0, @{$opt{footer}};
786 my $money_char = $conf->config('money_char') || '$';