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 == 1 ) {
203 update_customer(searchrow, customerArrayArray[0]);
204 % if ( $opt{custnum_update_callback} ) {
205 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
210 custnum_obj.value = 'Multiple'; // or something
211 custnum_obj.style.color = '#ff0000';
213 //blank the current list
214 customer_select.options.length = 0;
216 opt(customer_select, '', 'Multiple customers match "' + custnum + '" - select one', '#ff0000');
217 //add the multiple customers
218 for ( var s = 0; s < customerArrayArray.length; s++ ) {
220 JSON.stringify(customerArrayArray[s]),
221 customerArrayArray[s][1],
225 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
227 customer_obj.style.display = 'none';
229 customer_select.style.display = '';
235 custnum_search(custnum, search_custnum_update );
239 function <% $opt{prefix} %>search_customer() {
241 var customer_obj = this;
242 var searchrow = this.getAttribute('rownum');
243 var customer = this.value;
245 if ( customer == 'searching...' || customer == 'Not found' || customer == '' )
248 if ( this.getAttribute('magic') == 'nosearch' ) {
249 this.setAttribute('magic', '');
253 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
254 <% $opt{prefix} %>addRow();
257 var invnum = document.getElementById('invnum'+searchrow);
260 var custnum_obj = document.getElementById('custnum'+searchrow);
261 custnum_obj.value = 'searching...';
262 custnum_obj.disabled = true;
263 custnum_obj.style.color = '#000000';
264 custnum_obj.style.backgroundColor = '#dddddd';
266 var customer_select = document.getElementById('cust_select'+searchrow);
268 function search_customer_update(customers) {
270 var customerArrayArray = eval('(' + customers + ')') || [ [] ];
272 custnum_obj.disabled = false;
273 custnum_obj.style.backgroundColor = '#ffffff';
275 if ( customerArrayArray.length == 1 ) {
277 update_customer(searchrow, customerArrayArray[0]);
278 % if ( $opt{custnum_update_callback} ) {
279 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
284 custnum_obj.value = 'Multiple'; // or something
285 custnum_obj.style.color = '#ff0000';
287 //blank the current list
288 customer_select.options.length = 0;
290 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
291 //add the multiple customers
292 for ( var s = 0; s < customerArrayArray.length; s++ ) {
294 JSON.stringify(customerArrayArray[s]),
295 customerArrayArray[s][1],
299 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
301 customer_obj.style.display = 'none';
303 customer_select.style.display = '';
309 smart_search( customer, search_customer_update );
313 function select_customer() {
315 var custnum_balance_status = this.options[this.selectedIndex].value;
316 var customer = this.options[this.selectedIndex].text;
318 var searchrow = this.getAttribute('rownum');
319 var custnum_obj = document.getElementById('custnum'+searchrow);
320 var customer_obj = document.getElementById('customer'+searchrow);
321 var balance_obj = document.getElementById('balance'+searchrow);
322 var status_obj = document.getElementById('status'+searchrow);
324 if ( custnum_balance_status == '' ) {
326 } else if ( custnum_balance_status == 'cancel' ) {
328 custnum_obj.value = '';
329 custnum_obj.style.color = '#000000';
331 this.style.display = 'none';
332 customer_obj.style.display = '';
333 customer_obj.focus();
337 update_customer(searchrow, JSON.parse(custnum_balance_status));
339 % if ( $opt{custnum_update_callback} ) {
340 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
347 function opt(what,value,text,color) {
348 var optionName = new Option(text, value, false, false);
349 optionName.style.color = color;
350 var length = what.length;
351 what.options[length] = optionName;
354 function update_status_text(rownum, newval) {
355 document.getElementById('status'+rownum).value = newval;
356 document.getElementById('status'+rownum+'_text').innerHTML = newval;
359 function update_status_color(rownum, newval) {
360 document.getElementById('statuscolor'+rownum).value = newval;
361 document.getElementById('status'+rownum+'_text').style.color = newval;
364 function update_balance_text(rownum, newval) {
365 document.getElementById('balance'+rownum).value = newval;
366 document.getElementById('balance'+rownum+'_text').innerHTML = newval;
373 <TABLE ID="<% $opt{prefix} %>OneTrueTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
381 % foreach my $header ( @{$opt{header}} ) {
382 <TH><% $header %></TH>
386 % for ( $row = 0; exists($param->{"custnum$row"}); $row++ ) {
391 NAME = "invnum<% $row %>"
392 ID = "invnum<% $row %>"
395 STYLE = "text-align:right;"
396 VALUE = "<% $param->{"invnum$row"} %>"
397 rownum = "<% $row %>"
399 <SCRIPT TYPE="text/javascript">
400 var invnum_input<% $row %> = document.getElementById("invnum<% $row %>");
401 invnum_input<% $row %>.onfocus = clearhint_invnum;
402 invnum_input<% $row %>.onchange = <% $opt{prefix} %>search_invnum;
408 NAME = "custnum<% $row %>"
409 ID = "custnum<% $row %>"
412 STYLE = "text-align:right;"
413 VALUE = "<% $param->{"custnum$row"} %>"
414 rownum = "<% $row %>"
416 <SCRIPT TYPE="text/javascript">
417 var custnum_input<% $row %> = document.getElementById("custnum<% $row %>");
418 custnum_input<% $row %>.onfocus = clearhint_custnum;
419 custnum_input<% $row %>.onchange = <% $opt{prefix} %>search_custnum;
423 <TD STYLE="text-align: center">
425 ID = "status<% $row %>_text"
426 rownum = "<% $row %>"
427 STYLE = "font-weight: bold;
428 color: <%$param->{"statuscolor$row"} || '#000000'%>"
430 ><% $param->{"status$row"} %></SPAN>
431 <INPUT TYPE = "hidden"
432 NAME = "status<% $row %>"
433 ID = "status<% $row %>"
434 VALUE = "<% $param->{"status$row"} %>"
435 rownum = "<% $row %>"
437 <INPUT TYPE = "hidden"
438 NAME = "statuscolor<% $row %>"
439 ID = "statuscolor<% $row %>"
440 VALUE = "<% $param->{"statuscolor$row"} %>"
441 rownum = "<% $row %>"
446 <INPUT TYPE="text" NAME="customer<% $row %>" ID="customer<% $row %>" SIZE=64 VALUE="<% $param->{"customer$row"} %>" rownum="<% $row %>">
447 <SCRIPT TYPE="text/javascript">
448 var customer_input<% $row %> = document.getElementById("customer<% $row %>");
449 customer_input<% $row %>.onfocus = clearhint_customer;
450 customer_input<% $row %>.onclick = clearhint_customer;
451 customer_input<% $row %>.onchange = <% $opt{prefix} %>search_customer;
453 <SELECT NAME="cust_select<% $row %>" ID="cust_select<% $row %>" rownum="<% $row %>" STYLE="color:#ff0000; display:none">
455 <SCRIPT TYPE="text/javascript">
456 var customer_select<% $row %> = document.getElementById("cust_select<% $row %>");
457 customer_select<% $row %>.onchange = select_customer;
461 <TD STYLE="text-align:right">
464 ID = "balance<% $row %>_text"
465 rownum = "<% $row %>"
466 ><% $param->{"balance$row"} %></SPAN>
468 <INPUT TYPE = "hidden"
469 NAME = "balance<% $row %>"
470 ID = "balance<% $row %>"
471 VALUE = "<% $param->{"balance$row"} %>"
472 rownum = "<% $row %>"
477 % foreach my $field ( @{$opt{fields}} ) {
479 % if ( ref($field) eq 'CODE' ) {
480 % $value = &{$field}($row,$param);
482 % $value = $param->{"$field$row"};
484 % my $name = (ref($field) eq 'CODE') ? "column${col}_$row" : "$field$row";
485 % my $align = $align{ $opt{align}->[$col] || 'l' };
486 % my $size = $sizes->[$col] || 10;
487 % my $color = $opt{color}->[$col];
488 % my $font = $color ? qq(<FONT COLOR="$color">) : '';
490 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
491 % $total[$col] += $value;
492 % $onchange = $opt{prefix}. "calc_total$col();";
493 % $onchange = qq(onchange="$onchange" onkeyup="$onchange");
495 <TD ALIGN="<% $align %>">
496 % if (! $types->[$col] || $types->[$col] eq 'text') {
501 STYLE = "text-align: <% $align %>;"
502 VALUE = "<% $value %>"
505 % } elsif ($types->[$col] eq 'immutable') {
506 <% $font %><% $value %><% $font ? '</FONT>' : '' %>
507 <INPUT TYPE="hidden" ID="<% $name %>" NAME="<% $name %>" VALUE="<% $value %>" >
509 Cannot represent unknown type: <% $types->[$col] %>
518 <TH COLSPAN=5 ID="<% $opt{'prefix'} %>_TOTAL_TOTAL">
519 Total <% $row ? $row-1 : 0 %>
520 <% PL($opt{name_singular} || 'customer', ( $row ? $row-1 : 0 ) ) %>
523 % foreach my $footer ( @{$opt{footer}} ) {
524 % my $align = $align{ $opt{'footer_align'}->[$col] || 'c' };
525 % if ($footer eq '_TOTAL' ) {
526 % my $id = $opt{'fields'}->[$col];
527 % $id = ref($id) ? "column${col}_TOTAL" : "${id}_TOTAL";
528 <TH ALIGN="<% $align %>" ID="<% $id %>"> <% sprintf('%.2f', $total[$col] ) %></TH>
530 <TH ALIGN="<% $align %>"><% $footer %></TH>
538 <SCRIPT TYPE="text/javascript">
540 % foreach my $footer ( @{$opt{footer}} ) {
541 % if ($footer eq '_TOTAL' ) {
542 % my $name = $opt{fields}->[$col];
543 % $name = ref($name) ? "column$col" : $name;
544 var <% $opt{prefix}.$name %>_CACHE = new Array ();
545 var <% $opt{prefix} %>th_el = document.getElementById("<%$name%>_TOTAL");
546 function <% $opt{prefix} %>calc_total<% $col %>() {
551 ( <% $opt{prefix}.$name%>_CACHE[row] =
552 <% $opt{prefix}.$name%>_CACHE[row]
553 || document.getElementById("<%$name%>"+row)
559 var value = <%$name%>_CACHE[row].value;
560 value = parseFloat(value);
561 if ( ! isNaN(value) ) {
562 total = total + value;
565 <% $opt{prefix} %>th_el.innerHTML = ' ' + total.toFixed(2);
573 <% include('/elements/xmlhttp.html',
574 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
575 'subs' => [qw( custnum_search smart_search invnum_search )],
579 <SCRIPT TYPE="text/javascript">
581 var <% $opt{prefix} %>total_el =
582 document.getElementById("<% $opt{'prefix'} %>_TOTAL_TOTAL");
584 var <% $opt{prefix} %>rownum = <% $row %>;
586 function <% $opt{prefix} %>addRow() {
588 var table = document.getElementById('<% $opt{prefix} %>OneTrueTable');
589 var tablebody = table.getElementsByTagName('tbody').item(0);
591 var row = table.insertRow(rownum+1);
593 var invnum_cell = document.createElement('TD');
595 var invnum_input = document.createElement('INPUT');
596 invnum_input.setAttribute('name', 'invnum'+<% $opt{prefix} %>rownum);
597 invnum_input.setAttribute('id', 'invnum'+<% $opt{prefix} %>rownum);
598 invnum_input.style.textAlign = 'right';
599 invnum_input.setAttribute('size', 8);
600 invnum_input.setAttribute('maxlength', 12);
601 invnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
602 invnum_input.onfocus = clearhint_invnum;
603 invnum_input.onchange = <% $opt{prefix} %>search_invnum;
604 invnum_cell.appendChild(invnum_input);
606 row.appendChild(invnum_cell);
608 var custnum_cell = document.createElement('TD');
610 var custnum_input = document.createElement('INPUT');
611 custnum_input.setAttribute('name', 'custnum'+<% $opt{prefix} %>rownum);
612 custnum_input.setAttribute('id', 'custnum'+<% $opt{prefix} %>rownum);
613 custnum_input.style.textAlign = 'right';
614 custnum_input.setAttribute('size', 8);
615 custnum_input.setAttribute('maxlength', 12);
616 custnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
617 custnum_input.onfocus = clearhint_custnum;
618 custnum_input.onchange = <% $opt{prefix} %>search_custnum;
619 custnum_cell.appendChild(custnum_input);
621 row.appendChild(custnum_cell);
623 var status_cell = document.createElement('TD');
624 status_cell.style.textAlign = 'center';
626 var status_span = document.createElement('SPAN');
627 status_span.setAttribute('id', 'status'+<% $opt{prefix} %>rownum+'_text');
628 status_span.style.fontWeight = 'bold';
629 status_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
630 status_cell.appendChild(status_span);
632 var status_input = document.createElement('INPUT');
633 status_input.setAttribute('type', 'hidden');
634 status_input.setAttribute('name', 'status'+<% $opt{prefix} %>rownum);
635 status_input.setAttribute('id', 'status'+<% $opt{prefix} %>rownum);
636 status_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
637 status_cell.appendChild(status_input);
639 var statuscolor_input = document.createElement('INPUT');
640 statuscolor_input.setAttribute('type', 'hidden');
641 statuscolor_input.setAttribute('name', 'statuscolor'+<% $opt{prefix} %>rownum);
642 statuscolor_input.setAttribute('id', 'statuscolor'+<% $opt{prefix} %>rownum);
643 statuscolor_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
644 status_cell.appendChild(statuscolor_input);
646 row.appendChild(status_cell);
648 var customer_cell = document.createElement('TD');
650 var customer_input = document.createElement('INPUT');
651 customer_input.setAttribute('name', 'customer'+<% $opt{prefix} %>rownum);
652 customer_input.setAttribute('id', 'customer'+<% $opt{prefix} %>rownum);
653 customer_input.setAttribute('size', 64);
654 customer_input.setAttribute('value', '(last name or company)' );
655 customer_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
656 customer_input.onfocus = clearhint_customer;
657 customer_input.onclick = clearhint_customer;
658 customer_input.onchange = <% $opt{prefix} %>search_customer;
659 customer_cell.appendChild(customer_input);
661 var customer_select = document.createElement('SELECT');
662 customer_select.setAttribute('name', 'cust_select'+<% $opt{prefix} %>rownum);
663 customer_select.setAttribute('id', 'cust_select'+<% $opt{prefix} %>rownum);
664 customer_select.setAttribute('rownum', <% $opt{prefix} %>rownum);
665 customer_select.style.color = '#ff0000';
666 customer_select.style.display = 'none';
667 customer_select.onchange = select_customer;
668 customer_cell.appendChild(customer_select);
670 row.appendChild(customer_cell);
672 var balance_cell = document.createElement('TD');
674 balance_cell.style.textAlign = 'right';
675 balance_cell.appendChild(document.createTextNode('<%$money_char%>'));
677 var balance_span = document.createElement('SPAN');
678 balance_span.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum+'_text');
679 balance_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
680 balance_cell.appendChild(balance_span);
682 balance_cell.appendChild(
683 document.createTextNode(String.fromCharCode(160)) //
686 var balance_input = document.createElement('INPUT');
687 balance_input.setAttribute('type', 'hidden');
688 balance_input.setAttribute('name', 'balance'+<% $opt{prefix} %>rownum);
689 balance_input.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum);
690 balance_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
691 balance_cell.appendChild(balance_input);
693 row.appendChild(balance_cell);
696 % foreach my $field ( @{$opt{fields}} ) {
698 var my_cell = document.createElement('TD');
699 my_cell.setAttribute('align', '<% $align{ $opt{align}->[$col] || 'l' } %>');
701 % if ($types->[$col] eq 'immutable') {
703 % if ( ref($field) eq 'CODE' ) {
704 % $value = &{$field}($row,$param);
706 % $value = $param->{"$field$row"};
708 var my_text = document.createTextNode('<% $value %>');
709 my_cell.appendChild(my_text);
712 % my $name = (ref($field) eq 'CODE') ? "column${col}_" : $field;
713 var my_input = document.createElement('INPUT');
714 my_input.setAttribute('name', '<% $name %>'+<% $opt{prefix} %>rownum);
715 my_input.setAttribute('id', '<% $name %>'+<% $opt{prefix} %>rownum);
716 my_input.style.textAlign = '<% $align{ $opt{align}->[$col] || 'l' } %>';
717 my_input.setAttribute('size', <% $sizes->[$col] || 10 %>);
718 % if ($types->[$col] eq 'immutable') {
719 my_input.setAttribute('type', 'hidden');
721 % if ( $opt{footer}->[$col] eq '_TOTAL' ) {
722 my_input.onchange = <% $opt{prefix} %>calc_total<%$col%>;
723 my_input.onkeyup = <% $opt{prefix} %>calc_total<%$col%>;
725 my_cell.appendChild(my_input);
727 row.appendChild(my_cell);
732 //update the total # of rows display
733 if ( <% $opt{prefix} %>rownum == 1 ) {
734 <% $opt{prefix} %>total_el.innerHTML =
736 + <% $opt{prefix} %>rownum
737 + ' <% $opt{name_singular} || 'customer' %>';
739 <% $opt{prefix} %>total_el.innerHTML =
741 + <% $opt{prefix} %>rownum
742 + ' <% PL($opt{name_singular} || 'customer') %>';
745 <% $opt{prefix} %>rownum++;
749 % unless ($cgi->param('error')) {
750 <% $opt{prefix} %>addRow();
757 my $conf = new FS::Conf;
759 $opt{prefix} = '' unless defined $opt{prefix};
760 $opt{prefix} .= '_' if $opt{prefix};
762 my $types = $opt{'type'} ? [ @{$opt{'type'}} ] : [];
763 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
765 my $param = $opt{param};
766 $param = $cgi->Vars if $cgi->param('error');
768 $opt{$_} ||= [] foreach qw(align color footer footer_align);
770 my @total = map 0, @{$opt{footer}};
778 my $money_char = $conf->config('money_char') || '$';