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 var num_open_invoices = new Array;
46 function clearhint_invnum() {
48 if ( this.value == 'Not found' || this.value == 'Multiple' ) {
50 this.style.color = '#000000';
55 function clearhint_custnum() {
57 if ( this.value == 'Not found' || this.value == 'Multiple' ) {
59 this.style.color = '#000000';
64 function clearhint_customer() {
66 this.style.color = '#000000';
68 if ( this.value == '(last name or company)' || this.value == 'Not found' )
73 function update_customer(searchrow, customerArray) {
75 var custnum_obj = document.getElementById('custnum'+searchrow);
76 var customer = document.getElementById('customer'+searchrow);
77 var customer_select = document.getElementById('cust_select'+searchrow);
79 custnum_obj.disabled = false;
80 custnum_obj.style.backgroundColor = '#ffffff';
81 customer.disabled = false;
82 customer.style.backgroundColor = '#ffffff';
84 if ( customerArray.length == 0 ) {
86 custnum_obj.value = 'Not found';
87 customer.value = 'Not found';
88 custnum_obj.style.color = '#ff0000';
89 customer.style.color = '#ff0000';
91 customer.style.display = '';
92 customer_select.style.display = 'none';
95 } else if ( customerArray.length == 6 ) {
97 custnum_obj.value = customerArray[0];
98 custnum_obj.style.color = '#000000';
99 customer.value = customerArray[1];
101 update_balance_text(searchrow, customerArray[2]);
102 update_status_text( searchrow, customerArray[3]);
103 update_status_color(searchrow, '#'+customerArray[4]);
104 update_num_open(searchrow, customerArray[5]);
106 customer.style.display = '';
107 customer_select.style.display = 'none';
112 function <% $opt{prefix} %>search_invnum() {
114 this.style.color = '#000000'
116 var invnum_obj = this;
117 var searchrow = this.getAttribute('rownum');
118 var invnum = this.value;
120 if ( invnum == 'searching...' || invnum == 'Not found' || invnum == '' )
123 if ( this.getAttribute('magic') == 'nosearch' ) {
124 this.setAttribute('magic', '');
128 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
129 <% $opt{prefix} %>addRow();
131 var customer = document.getElementById('customer'+searchrow);
132 customer.value = 'searching...';
133 customer.disabled = true;
134 customer.style.color = '#000000';
135 customer.style.backgroundColor = '#dddddd';
137 var customer_select = document.getElementById('cust_select'+searchrow);
139 customer.style.display = '';
140 customer_select.style.display = 'none';
142 var custnum_obj = document.getElementById('custnum'+searchrow);
143 update_balance_text(searchrow, '');
144 update_status_text(searchrow, '');
145 update_status_color(searchrow, '#000000');
146 update_num_open(searchrow, 0);
148 function search_invnum_update(customers) {
150 var customerArray = eval('(' + customers + ')');
151 update_customer(searchrow, customerArray);
153 % if ( $opt{invnum_update_callback} ) {
154 <% $opt{invnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
159 invnum_search( invnum, search_invnum_update );
163 function <% $opt{prefix} %>search_custnum() {
165 this.style.color = '#000000'
167 var custnum_obj = this;
168 var searchrow = this.getAttribute('rownum');
169 var custnum = this.value;
171 if ( custnum == 'searching...' || custnum == 'Not found' || custnum == '' )
174 if ( this.getAttribute('magic') == 'nosearch' ) {
175 this.setAttribute('magic', '');
179 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
180 <% $opt{prefix} %>addRow();
182 var customer = document.getElementById('customer'+searchrow);
183 customer.value = 'searching...';
184 customer.disabled = true;
185 customer.style.color = '#000000';
186 customer.style.backgroundColor = '#dddddd';
188 var customer_select = document.getElementById('cust_select'+searchrow);
190 customer.style.display = '';
191 customer_select.style.display = 'none';
193 var invnum = document.getElementById('invnum'+searchrow);
196 update_balance_text(searchrow, '');
197 update_status_text( searchrow, '');
198 update_status_color(searchrow, '#000000');
199 update_num_open(searchrow, 0);
201 function search_custnum_update(customers) {
203 var customerArray = eval('(' + customers + ')') || [];
204 update_customer(searchrow, customerArray);
206 % if ( $opt{custnum_update_callback} ) {
207 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
211 custnum_search(custnum, search_custnum_update );
215 function <% $opt{prefix} %>search_customer() {
217 var customer_obj = this;
218 var searchrow = this.getAttribute('rownum');
219 var customer = this.value;
221 if ( customer == 'searching...' || customer == 'Not found' || customer == '' )
224 if ( this.getAttribute('magic') == 'nosearch' ) {
225 this.setAttribute('magic', '');
229 if ( ( <% $opt{prefix} %>rownum - searchrow ) == 1 ) {
230 <% $opt{prefix} %>addRow();
233 var invnum = document.getElementById('invnum'+searchrow);
236 var custnum_obj = document.getElementById('custnum'+searchrow);
237 custnum_obj.value = 'searching...';
238 custnum_obj.disabled = true;
239 custnum_obj.style.color = '#000000';
240 custnum_obj.style.backgroundColor = '#dddddd';
242 var customer_select = document.getElementById('cust_select'+searchrow);
244 function search_customer_update(customers) {
246 var customerArrayArray = eval('(' + customers + ')') || [ [] ];
248 custnum_obj.disabled = false;
249 custnum_obj.style.backgroundColor = '#ffffff';
251 if ( customerArrayArray.length == 1 ) {
253 update_customer(customerArrayArray[1]);
254 % if ( $opt{custnum_update_callback} ) {
255 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
260 custnum_obj.value = 'Multiple'; // or something
261 custnum_obj.style.color = '#ff0000';
263 //blank the current list
264 customer_select.options.length = 0;
266 opt(customer_select, '', 'Multiple customers match "' + customer + '" - select one', '#ff0000');
267 //add the multiple customers
268 for ( var s = 0; s < customerArrayArray.length; s++ ) {
270 JSON.stringify(customerArrayArray[s]),
271 customerArrayArray[s][1],
275 opt(customer_select, 'cancel', '(Edit search string)', '#000000');
277 customer_obj.style.display = 'none';
279 customer_select.style.display = '';
285 smart_search( customer, search_customer_update );
289 function select_customer() {
291 var custnum_balance_status = this.options[this.selectedIndex].value;
292 var customer = this.options[this.selectedIndex].text;
294 var searchrow = this.getAttribute('rownum');
295 var custnum_obj = document.getElementById('custnum'+searchrow);
296 var customer_obj = document.getElementById('customer'+searchrow);
297 var balance_obj = document.getElementById('balance'+searchrow);
298 var status_obj = document.getElementById('status'+searchrow);
300 if ( custnum_balance_status == '' ) {
302 } else if ( custnum_balance_status == 'cancel' ) {
304 custnum_obj.value = '';
305 custnum_obj.style.color = '#000000';
307 this.style.display = 'none';
308 customer_obj.style.display = '';
309 customer_obj.focus();
313 update_customer(searchrow, JSON.parse(custnum_balance_status));
315 % if ( $opt{custnum_update_callback} ) {
316 <% $opt{custnum_update_callback} %>(searchrow, '<% $opt{prefix} %>')
323 function opt(what,value,text,color) {
324 var optionName = new Option(text, value, false, false);
325 optionName.style.color = color;
326 var length = what.length;
327 what.options[length] = optionName;
330 function update_status_text(rownum, newval) {
331 document.getElementById('status'+rownum).value = newval;
332 document.getElementById('status'+rownum+'_text').innerHTML = newval;
335 function update_status_color(rownum, newval) {
336 document.getElementById('statuscolor'+rownum).value = newval;
337 document.getElementById('status'+rownum+'_text').style.color = newval;
340 function update_balance_text(rownum, newval) {
341 document.getElementById('balance'+rownum).value = newval;
342 document.getElementById('balance'+rownum+'_text').innerHTML = newval;
345 function update_num_open(rownum, newval) {
346 num_open_invoices[rownum] = newval;
352 <TABLE ID="<% $opt{prefix} %>OneTrueTable" BGCOLOR="#cccccc" BORDER=0 CELLSPACING=0>
360 % foreach my $header ( @{$opt{header}} ) {
361 <TH><% $header %></TH>
365 % for ( $row = 0; exists($param->{"custnum$row"}); $row++ ) {
367 <TR id="row<%$row%>" rownum="<%$row%>">
370 NAME = "invnum<% $row %>"
371 ID = "invnum<% $row %>"
374 STYLE = "text-align:right;"
375 VALUE = "<% $param->{"invnum$row"} %>"
376 rownum = "<% $row %>"
378 <SCRIPT TYPE="text/javascript">
379 var invnum_input<% $row %> = document.getElementById("invnum<% $row %>");
380 invnum_input<% $row %>.onfocus = clearhint_invnum;
381 invnum_input<% $row %>.onchange = <% $opt{prefix} %>search_invnum;
387 NAME = "custnum<% $row %>"
388 ID = "custnum<% $row %>"
391 STYLE = "text-align:right;"
392 VALUE = "<% $param->{"custnum$row"} %>"
393 rownum = "<% $row %>"
395 <SCRIPT TYPE="text/javascript">
396 var custnum_input<% $row %> = document.getElementById("custnum<% $row %>");
397 custnum_input<% $row %>.onfocus = clearhint_custnum;
398 custnum_input<% $row %>.onchange = <% $opt{prefix} %>search_custnum;
402 <TD STYLE="text-align: center">
404 ID = "status<% $row %>_text"
405 rownum = "<% $row %>"
406 STYLE = "font-weight: bold;
407 color: <%$param->{"statuscolor$row"} || '#000000'%>"
409 ><% $param->{"status$row"} %></SPAN>
410 <INPUT TYPE = "hidden"
411 NAME = "status<% $row %>"
412 ID = "status<% $row %>"
413 VALUE = "<% $param->{"status$row"} %>"
414 rownum = "<% $row %>"
416 <INPUT TYPE = "hidden"
417 NAME = "statuscolor<% $row %>"
418 ID = "statuscolor<% $row %>"
419 VALUE = "<% $param->{"statuscolor$row"} %>"
420 rownum = "<% $row %>"
425 <INPUT TYPE="text" NAME="customer<% $row %>" ID="customer<% $row %>" SIZE=64 VALUE="<% $param->{"customer$row"} %>" rownum="<% $row %>">
426 <SCRIPT TYPE="text/javascript">
427 var customer_input<% $row %> = document.getElementById("customer<% $row %>");
428 customer_input<% $row %>.onfocus = clearhint_customer;
429 customer_input<% $row %>.onclick = clearhint_customer;
430 customer_input<% $row %>.onchange = <% $opt{prefix} %>search_customer;
432 <SELECT NAME="cust_select<% $row %>" ID="cust_select<% $row %>" rownum="<% $row %>" STYLE="color:#ff0000; display:none">
434 <SCRIPT TYPE="text/javascript">
435 var customer_select<% $row %> = document.getElementById("cust_select<% $row %>");
436 customer_select<% $row %>.onchange = select_customer;
440 <TD STYLE="text-align:right">
443 ID = "balance<% $row %>_text"
444 rownum = "<% $row %>"
445 ><% $param->{"balance$row"} %></SPAN>
447 <INPUT TYPE = "hidden"
448 NAME = "balance<% $row %>"
449 ID = "balance<% $row %>"
450 VALUE = "<% $param->{"balance$row"} %>"
451 rownum = "<% $row %>"
456 % foreach my $field ( @{$opt{fields}} ) {
458 % if ( ref($field) eq 'CODE' ) {
459 % $value = &{$field}($row,$param);
461 % $value = $param->{"$field$row"};
463 % my $name = (ref($field) eq 'CODE') ? "column${col}_$row" : "$field$row";
464 % my $align = $align{ $opt{align}->[$col] || 'l' };
465 % my $size = $sizes->[$col] || 10;
466 % my $color = $opt{color}->[$col];
467 % my $font = $color ? qq(<FONT COLOR="$color">) : '';
469 % if ( $opt{onchange}->[$col] ) {
470 % $onchange = 'onchange="'.$opt{onchange}->[$col].'"';
472 % elsif ( $opt{footer}->[$col] eq '_TOTAL' ) {
473 % $total[$col] += $value;
474 % $onchange = $opt{prefix}. "calc_total$col();";
475 % $onchange = qq(onchange="$onchange" onkeyup="$onchange");
477 <TD ALIGN="<% $align %>">
478 % my $type = $types->[$col] || 'text';
479 % if ($type eq 'text' or $type eq 'checkbox') {
480 <INPUT TYPE = "<% $type %>"
484 STYLE = "text-align: <% $align %>;"
485 VALUE = "<% $value %>"
486 rownum = "<% $row %>"
489 % } elsif ($types->[$col] eq 'immutable') {
490 <% $font %><% $value %><% $font ? '</FONT>' : '' %>
491 <INPUT TYPE="hidden" ID="<% $name %>" NAME="<% $name %>" VALUE="<% $value %>" >
493 Cannot represent unknown type: <% $types->[$col] %>
502 <TH COLSPAN=5 ID="<% $opt{'prefix'} %>_TOTAL_TOTAL">
503 Total <% $row ? $row-1 : 0 %>
504 <% PL($opt{name_singular} || 'customer', ( $row ? $row-1 : 0 ) ) %>
507 % foreach my $footer ( @{$opt{footer}} ) {
508 % my $align = $align{ $opt{'footer_align'}->[$col] || 'c' };
509 % if ($footer eq '_TOTAL' ) {
510 % my $id = $opt{'fields'}->[$col];
511 % $id = ref($id) ? "column${col}_TOTAL" : "${id}_TOTAL";
512 <TH ALIGN="<% $align %>" ID="<% $id %>"> <% sprintf('%.2f', $total[$col] ) %></TH>
514 <TH ALIGN="<% $align %>"><% $footer %></TH>
522 <SCRIPT TYPE="text/javascript">
524 % foreach my $footer ( @{$opt{footer}} ) {
525 % if ($footer eq '_TOTAL' ) {
526 % my $name = $opt{fields}->[$col];
527 % $name = ref($name) ? "column$col" : $name;
528 var <% $opt{prefix}.$name %>_CACHE = new Array ();
529 var <% $opt{prefix} %>th_el = document.getElementById("<%$name%>_TOTAL");
530 function <% $opt{prefix} %>calc_total<% $col %>() {
535 ( <% $opt{prefix}.$name%>_CACHE[row] =
536 <% $opt{prefix}.$name%>_CACHE[row]
537 || document.getElementById("<%$name%>"+row)
543 var value = <%$name%>_CACHE[row].value;
544 value = parseFloat(value);
545 if ( ! isNaN(value) ) {
546 total = total + value;
549 <% $opt{prefix} %>th_el.innerHTML = ' ' + total.toFixed(2);
557 <% include('/elements/xmlhttp.html',
558 'url' => $p. 'misc/xmlhttp-cust_main-search.cgi',
559 'subs' => [qw( custnum_search smart_search invnum_search )],
563 <SCRIPT TYPE="text/javascript">
565 var <% $opt{prefix} %>total_el =
566 document.getElementById("<% $opt{'prefix'} %>_TOTAL_TOTAL");
568 var <% $opt{prefix} %>rownum = <% $row %>;
570 function <% $opt{prefix} %>addRow() {
572 var table = document.getElementById('<% $opt{prefix} %>OneTrueTable');
573 var tablebody = table.getElementsByTagName('tbody').item(0);
575 var row = table.insertRow(table.rows.length - 1);
576 row.setAttribute('id', 'row'+rownum);
578 var invnum_cell = document.createElement('TD');
580 var invnum_input = document.createElement('INPUT');
581 invnum_input.setAttribute('name', 'invnum'+<% $opt{prefix} %>rownum);
582 invnum_input.setAttribute('id', 'invnum'+<% $opt{prefix} %>rownum);
583 invnum_input.style.textAlign = 'right';
584 invnum_input.setAttribute('size', 8);
585 invnum_input.setAttribute('maxlength', 12);
586 invnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
587 invnum_input.onfocus = clearhint_invnum;
588 invnum_input.onchange = <% $opt{prefix} %>search_invnum;
589 invnum_cell.appendChild(invnum_input);
591 row.appendChild(invnum_cell);
593 var custnum_cell = document.createElement('TD');
595 var custnum_input = document.createElement('INPUT');
596 custnum_input.setAttribute('name', 'custnum'+<% $opt{prefix} %>rownum);
597 custnum_input.setAttribute('id', 'custnum'+<% $opt{prefix} %>rownum);
598 custnum_input.style.textAlign = 'right';
599 custnum_input.setAttribute('size', 8);
600 custnum_input.setAttribute('maxlength', 12);
601 custnum_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
602 custnum_input.onfocus = clearhint_custnum;
603 custnum_input.onchange = <% $opt{prefix} %>search_custnum;
604 custnum_cell.appendChild(custnum_input);
606 row.appendChild(custnum_cell);
608 var status_cell = document.createElement('TD');
609 status_cell.style.textAlign = 'center';
611 var status_span = document.createElement('SPAN');
612 status_span.setAttribute('id', 'status'+<% $opt{prefix} %>rownum+'_text');
613 status_span.style.fontWeight = 'bold';
614 status_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
615 status_cell.appendChild(status_span);
617 var status_input = document.createElement('INPUT');
618 status_input.setAttribute('type', 'hidden');
619 status_input.setAttribute('name', 'status'+<% $opt{prefix} %>rownum);
620 status_input.setAttribute('id', 'status'+<% $opt{prefix} %>rownum);
621 status_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
622 status_cell.appendChild(status_input);
624 var statuscolor_input = document.createElement('INPUT');
625 statuscolor_input.setAttribute('type', 'hidden');
626 statuscolor_input.setAttribute('name', 'statuscolor'+<% $opt{prefix} %>rownum);
627 statuscolor_input.setAttribute('id', 'statuscolor'+<% $opt{prefix} %>rownum);
628 statuscolor_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
629 status_cell.appendChild(statuscolor_input);
631 row.appendChild(status_cell);
633 var customer_cell = document.createElement('TD');
635 var customer_input = document.createElement('INPUT');
636 customer_input.setAttribute('name', 'customer'+<% $opt{prefix} %>rownum);
637 customer_input.setAttribute('id', 'customer'+<% $opt{prefix} %>rownum);
638 customer_input.setAttribute('size', 64);
639 customer_input.setAttribute('value', '(last name or company)' );
640 customer_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
641 customer_input.onfocus = clearhint_customer;
642 customer_input.onclick = clearhint_customer;
643 customer_input.onchange = <% $opt{prefix} %>search_customer;
644 customer_cell.appendChild(customer_input);
646 var customer_select = document.createElement('SELECT');
647 customer_select.setAttribute('name', 'cust_select'+<% $opt{prefix} %>rownum);
648 customer_select.setAttribute('id', 'cust_select'+<% $opt{prefix} %>rownum);
649 customer_select.setAttribute('rownum', <% $opt{prefix} %>rownum);
650 customer_select.style.color = '#ff0000';
651 customer_select.style.display = 'none';
652 customer_select.onchange = select_customer;
653 customer_cell.appendChild(customer_select);
655 row.appendChild(customer_cell);
657 var balance_cell = document.createElement('TD');
659 balance_cell.style.textAlign = 'right';
660 balance_cell.appendChild(document.createTextNode('<%$money_char%>'));
662 var balance_span = document.createElement('SPAN');
663 balance_span.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum+'_text');
664 balance_span.setAttribute('rownum', <% $opt{prefix} %>rownum);
665 balance_cell.appendChild(balance_span);
667 balance_cell.appendChild(
668 document.createTextNode(String.fromCharCode(160)) //
671 var balance_input = document.createElement('INPUT');
672 balance_input.setAttribute('type', 'hidden');
673 balance_input.setAttribute('name', 'balance'+<% $opt{prefix} %>rownum);
674 balance_input.setAttribute('id', 'balance'+<% $opt{prefix} %>rownum);
675 balance_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
676 balance_cell.appendChild(balance_input);
678 row.appendChild(balance_cell);
681 % foreach my $field ( @{$opt{fields}} ) {
683 var my_cell = document.createElement('TD');
684 my_cell.setAttribute('align', '<% $align{ $opt{align}->[$col] || 'l' } %>');
686 % if ($types->[$col] eq 'immutable') {
688 % if ( ref($field) eq 'CODE' ) {
689 % $value = &{$field}($row,$param);
691 % $value = $param->{"$field$row"};
693 var my_text = document.createTextNode(<% $value |js_string %>);
694 my_cell.appendChild(my_text);
697 % my $name = (ref($field) eq 'CODE') ? "column${col}_" : $field;
698 var my_input = document.createElement('INPUT');
699 my_input.setAttribute('name', '<% $name %>'+<% $opt{prefix} %>rownum);
700 my_input.setAttribute('id', '<% $name %>'+<% $opt{prefix} %>rownum);
701 my_input.style.textAlign = '<% $align{ $opt{align}->[$col] || 'l' } %>';
702 my_input.setAttribute('size', <% $sizes->[$col] || 10 %>);
703 my_input.setAttribute('rownum', <% $opt{prefix} %>rownum);
704 % if ( $types->[$col] eq 'immutable' ) {
705 my_input.setAttribute('type', 'hidden');
707 % elsif ( $types->[$col] eq 'checkbox' ) {
708 my_input.setAttribute('type', 'checkbox');
710 % if ( $opt{onchange}->[$col] ) {
711 my_input.onchange = <% $opt{onchange}->[$col] %>;
713 % elsif ( $opt{footer}->[$col] eq '_TOTAL' ) {
714 my_input.onchange = <% $opt{prefix} %>calc_total<%$col%>;
715 my_input.onkeyup = <% $opt{prefix} %>calc_total<%$col%>;
717 my_cell.appendChild(my_input);
719 row.appendChild(my_cell);
724 //update the total # of rows display
725 if ( <% $opt{prefix} %>rownum == 1 ) {
726 <% $opt{prefix} %>total_el.innerHTML =
728 + <% $opt{prefix} %>rownum
729 + ' <% $opt{name_singular} || 'customer' %>';
731 <% $opt{prefix} %>total_el.innerHTML =
733 + <% $opt{prefix} %>rownum
734 + ' <% PL($opt{name_singular} || 'customer') %>';
737 % if ( $opt{add_row_callback} ) {
738 <% $opt{add_row_callback} %>(<% $opt{prefix} %>rownum,
739 '<% $opt{prefix} %>');
742 <% $opt{prefix} %>rownum++;
746 % unless ($cgi->param('error')) {
747 <% $opt{prefix} %>addRow();
754 my $conf = new FS::Conf;
756 $opt{prefix} = '' unless defined $opt{prefix};
757 $opt{prefix} .= '_' if $opt{prefix};
759 my $types = $opt{'type'} ? [ @{$opt{'type'}} ] : [];
760 my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
762 my $param = $opt{param};
763 $param = $cgi->Vars if $cgi->param('error');
765 $opt{$_} ||= [] foreach qw(align color footer footer_align);
767 my @total = map 0, @{$opt{footer}};
775 my $money_char = $conf->config('money_char') || '$';