more RT integration
[freeside.git] / httemplate / view / cust_main.cgi
1 <!-- mason kludge -->
2 <%
3
4 my $conf = new FS::Conf;
5
6 my %uiview = ();
7 my %uiadd = ();
8 foreach my $part_svc ( qsearch('part_svc',{}) ) {
9   $uiview{$part_svc->svcpart} = popurl(2). "view/". $part_svc->svcdb . ".cgi";
10   $uiadd{$part_svc->svcpart}= popurl(2). "edit/". $part_svc->svcdb . ".cgi";
11 }
12
13 print header("Customer View", menubar(
14   'Main Menu' => popurl(2)
15 ));
16
17 %>
18
19 <STYLE TYPE="text/css">
20 .package TH { font-size: medium }
21 .package TR { font-size: smaller }
22 .package .provision { font-weight: bold }
23 </STYLE>
24
25 <%
26
27 die "No customer specified (bad URL)!" unless $cgi->keywords;
28 my($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
29 $query =~ /^(\d+)$/;
30 my $custnum = $1;
31 my $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
32 die "Customer not found!" unless $cust_main;
33
34 print qq!<A HREF="${p}edit/cust_main.cgi?$custnum">Edit this customer</A>!;
35
36 %>
37
38 <SCRIPT>
39 function areyousure(href, message) {
40     if (confirm(message) == true)
41         window.location.href = href;
42 }
43 </SCRIPT>
44
45 <%
46
47 print qq! | <A HREF="javascript:areyousure('${p}misc/cust_main-cancel.cgi?$custnum', 'Perminantly delete all services and cancel this customer?')">!.
48       'Cancel this customer</A>'
49   if $cust_main->ncancelled_pkgs;
50
51 print qq! | <A HREF="${p}misc/delete-customer.cgi?$custnum">!.
52       'Delete this customer</A>'
53   if $conf->exists('deletecustomers');
54
55 unless ( $conf->exists('disable_customer_referrals') ) {
56   print qq! | <A HREF="!, popurl(2),
57         qq!edit/cust_main.cgi?referral_custnum=$custnum">!,
58         qq!Refer a new customer</A>!;
59
60   print qq! | <A HREF="!, popurl(2),
61         qq!search/cust_main.cgi?referral_custnum=$custnum">!,
62         qq!View this customer's referrals</A>!;
63 }
64
65 print '<BR><BR>';
66
67 my $signupurl = $conf->config('signupurl');
68 if ( $signupurl ) {
69 print "This customer's signup URL: ".
70       "<a href=\"$signupurl?ref=$custnum\">$signupurl?ref=$custnum</a><BR><BR>";
71 }
72
73 %>
74
75 <A NAME="cust_main"></A>
76 <%= &itable() %>
77 <TR>
78   <TD VALIGN="top">
79     <%= include('cust_main/contacts.html', $cust_main ) %>
80   </TD>
81   <TD VALIGN="top">
82     <%= include('cust_main/misc.html', $cust_main ) %>
83     <% if ( $conf->config('payby-default') ne 'HIDE' ) { %>
84       <BR>
85       <%= include('cust_main/billing.html', $cust_main ) %>
86     <% } %>
87   </TD>
88 </TR>
89 </TABLE>
90
91 <%
92 if ( defined $cust_main->dbdef_table->column('comments')
93      && $cust_main->comments =~ /[^\s\n\r]/              ) {
94 %>
95 <BR>
96 Comments
97 <%= ntable("#cccccc") %><TR><TD><%= ntable("#cccccc",2) %>
98 <TR>
99   <TD BGCOLOR="#ffffff">
100     <PRE><%= encode_entities($cust_main->comments) %></PRE>
101   </TD>
102 </TR>
103 </TABLE></TABLE>
104 <% } %>
105
106 <% if ( $conf->config('ticket_system') ) { %>
107   <BR>
108   <%= include('cust_main/tickets.html', $cust_main ) %>
109 <% } %>
110
111 <BR>
112 <SCRIPT TYPE="text/javascript">
113 function enable_order_pkg () {
114   if ( document.OrderPkgForm.pkgpart.selectedIndex > 0 ) {
115     document.OrderPkgForm.submit.disabled = false;
116   } else {
117     document.OrderPkgForm.submit.disabled = true;
118   }
119 }
120 </SCRIPT>
121 <FORM NAME="OrderPkgForm" ACTION="<%= $p %>edit/process/quick-cust_pkg.cgi" METHOD="POST">
122 <INPUT TYPE="hidden" NAME="custnum" VALUE="<%= $custnum %>">
123 <SELECT NAME="pkgpart" onChange="enable_order_pkg()"><OPTION>Order additional package
124
125 <%
126
127 my @agents = qsearch( 'agent', {} );
128 my $agent;
129 unless ( scalar(@agents) == 1 ) {
130   $agent = qsearchs('agent',{ 'agentnum' => $cust_main->agentnum } );
131 } else {
132   $agent = $agents[0];
133 }
134
135 foreach my $part_pkg (
136   qsearch( 'part_pkg', { 'disabled' => '' }, '',
137            ' AND 0 < ( SELECT COUNT(*) FROM type_pkgs '.
138            '             WHERE typenum = '. $agent->typenum.
139            '             AND type_pkgs.pkgpart = part_pkg.pkgpart )'
140          )
141 ) {
142 %>
143 <OPTION VALUE="<%= $part_pkg->pkgpart %>"><%= $part_pkg->pkg %> - <%= $part_pkg->comment %>
144 <% } %>
145
146 </SELECT><INPUT NAME="submit" TYPE="submit" VALUE="Order Package" disabled></FORM>
147
148 <%
149
150 if ( $conf->config('payby-default') ne 'HIDE' ) {
151
152   print
153     qq!<FORM ACTION="${p}edit/process/quick-charge.cgi" METHOD="POST">!.
154     qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!.
155     qq!Description:<INPUT TYPE="text" NAME="pkg">!.
156     qq!&nbsp;Amount:<INPUT TYPE="text" NAME="amount" SIZE=6>!.
157     qq!&nbsp;!;
158   
159   #false laziness w/ edit/part_pkg.cgi
160   if ( $conf->exists('enable_taxclasses') ) {
161     print '<SELECT NAME="taxclass">';
162     my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
163       or die dbh->errstr;
164     $sth->execute or die $sth->errstr;
165     foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
166       print qq!<OPTION VALUE="$taxclass"!;
167       #print ' SELECTED' if $taxclass eq $hashref->{taxclass};
168       print qq!>$taxclass</OPTION>!;
169     }
170     print '</SELECT>';
171   } else {
172     print '<INPUT TYPE="hidden" NAME="taxclass" VALUE="">';
173   }
174   
175   print qq!<INPUT TYPE="submit" VALUE="One-time charge"></FORM><BR>!;
176
177 }
178
179 print qq!<A NAME="cust_pkg">Packages</A> !,
180       qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Bulk order and cancel packages</A> (preserves services) )!,
181 ;
182
183 #begin display packages
184
185 #get package info
186
187 my $packages = get_packages($cust_main, $conf);
188
189 if ( @$packages ) {
190 %>
191 <TABLE CLASS="package" BORDER=1 CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">
192 <TR>
193   <TH>Package</TH>
194   <TH>Status</TH>
195   <TH COLSPAN=2>Services</TH>
196 </TR>
197 <%
198 foreach my $pkg (sort pkgsort_pkgnum_cancel @$packages) {
199   my $rowspan = 0;
200
201   if ($pkg->{cancel}) {
202     $rowspan = 0;
203   } else {
204     foreach my $svcpart (@{$pkg->{svcparts}}) {
205       $rowspan += $svcpart->{count};
206       $rowspan++ if ($svcpart->{count} < $svcpart->{quantity});
207     }
208   } 
209
210 %>
211 <!--pkgnum: <%=$pkg->{pkgnum}%>-->
212 <TR>
213   <TD ROWSPAN=<%=$rowspan%>>
214     <A NAME="cust_pkg<%=$pkg->{pkgnum}%>"><%=$pkg->{pkgnum}%></A>:
215     <%=$pkg->{pkg}%> - <%=$pkg->{comment}%><BR>
216 <% unless ($pkg->{cancel}) { %>
217     (&nbsp;<%=pkg_change_link($pkg)%>&nbsp;)
218     (&nbsp;<%=pkg_dates_link($pkg)%>&nbsp;|&nbsp;<%=pkg_customize_link($pkg,$custnum)%>&nbsp;)
219 <% } %>
220   </TD>
221 <%
222   #foreach (qw(setup last_bill next_bill susp expire cancel)) {
223   #  print qq!  <TD ROWSPAN=$rowspan>! . pkg_datestr($pkg,$_,$conf) . qq!</TD>\n!;
224   #}
225   print "<TD ROWSPAN=$rowspan>". &itable('');
226
227   sub myfreq {
228     my $part_pkg = shift;
229     my $freq = $part_pkg->freq_pretty;
230     $freq =~ s/ /&nbsp;/g;
231     $freq;
232   }
233
234   if ( $pkg->{cancel} ) { #status: cancelled
235
236     print '<TR><TD><FONT COLOR="#ff0000"><B>Cancelled&nbsp;</B></FONT></TD>'.
237           '<TD>'. pkg_datestr($pkg,'cancel',$conf). '</TD></TR>';
238     unless ( $pkg->{setup} ) {
239       print '<TR><TD COLSPAN=2>Never billed</TD></TR>';
240     } else {
241       print "<TR><TD>Setup&nbsp;</TD><TD>".
242             pkg_datestr($pkg, 'setup',$conf). '</TD></TR>';
243       print "<TR><TD>Last&nbsp;bill&nbsp;</TD><TD>".
244             pkg_datestr($pkg, 'last_bill',$conf). '</TD></TR>'
245         if $pkg->{'last_bill'};
246       print "<TR><TD>Suspended&nbsp;</TD><TD>".
247             pkg_datestr($pkg, 'susp',$conf). '</TD></TR>'
248         if $pkg->{'susp'};
249     }
250
251   } else {
252
253     if ( $pkg->{susp} ) { #status: suspended
254       print '<TR><TD><FONT COLOR="#FF9900"><B>Suspended</B>&nbsp;</FONT></TD>'.
255             '<TD>'. pkg_datestr($pkg,'susp',$conf). '</TD></TR>';
256       unless ( $pkg->{setup} ) {
257         print '<TR><TD COLSPAN=2>Never billed</TD></TR>';
258       } else {
259         print "<TR><TD>Setup&nbsp;</TD><TD>". 
260               pkg_datestr($pkg, 'setup',$conf). '</TD></TR>';
261       }
262       print "<TR><TD>Last&nbsp;bill&nbsp;</TD><TD>".
263             pkg_datestr($pkg, 'last_bill',$conf). '</TD></TR>'
264         if $pkg->{'last_bill'};
265       # next bill ??
266       print "<TR><TD>Expires&nbsp;</TD><TD>".
267             pkg_datestr($pkg, 'expire',$conf). '</TD></TR>'
268         if $pkg->{'expire'};
269       print '<TR><TD COLSPAN=2>(&nbsp;'. pkg_unsuspend_link($pkg).
270             '&nbsp;|&nbsp;'. pkg_cancel_link($pkg). '&nbsp;)</TD></TR>';
271
272     } else { #status: active
273
274       unless ( $pkg->{setup} ) { #not setup
275
276         print '<TR><TD COLSPAN=2>Not&nbsp;yet&nbsp;billed&nbsp;(';
277         unless ( $pkg->{freq} ) {
278           print 'one-time&nbsp;charge)</TD></TR>';
279           print '<TR><TD COLSPAN=2>(&nbsp;'. pkg_cancel_link($pkg).
280                 '&nbsp;)</TD</TR>';
281         } else {
282           print 'billed&nbsp;'. myfreq($pkg->{part_pkg}). ')</TD></TR>';
283         }
284
285       } else { #setup
286
287         unless ( $pkg->{freq} ) {
288           print "<TR><TD COLSPAN=2>One-time&nbsp;charge</TD></TR>".
289                 '<TR><TD>Billed&nbsp;</TD><TD>'.
290                 pkg_datestr($pkg,'setup',$conf). '</TD></TR>';
291         } else {
292           print '<TR><TD COLSPAN=2><FONT COLOR="#00CC00"><B>Active</B></FONT>'.
293                 ',&nbsp;billed&nbsp;'. myfreq($pkg->{part_pkg}). '</TD></TR>'.
294                 '<TR><TD>Setup&nbsp;</TD><TD>'.
295                 pkg_datestr($pkg, 'setup',$conf). '</TD></TR>';
296         }
297
298       }
299
300       print "<TR><TD>Last&nbsp;bill&nbsp;</TD><TD>".
301             pkg_datestr($pkg, 'last_bill',$conf). '</TD></TR>'
302         if $pkg->{'last_bill'};
303       print "<TR><TD>Next&nbsp;bill&nbsp;</TD><TD>".
304             pkg_datestr($pkg, 'next_bill',$conf). '</TD></TR>'
305         if $pkg->{'next_bill'};
306       print "<TR><TD>Expires&nbsp;</TD><TD>".
307             pkg_datestr($pkg, 'expire',$conf). '</TD></TR>'
308         if $pkg->{'expire'};
309       if ( $pkg->{freq} ) {
310         print '<TR><TD COLSPAN=2>(&nbsp;'. pkg_suspend_link($pkg).
311               '&nbsp;|&nbsp;'. pkg_cancel_link($pkg). '&nbsp;)</TD></TR>';
312       }
313
314     }
315
316   }
317
318   print "</TABLE></TD>\n";
319
320   if ($rowspan == 0) { print qq!</TR>\n!; next; }
321
322   my $cnt = 0;
323   foreach my $svcpart (sort {$a->{svcpart} <=> $b->{svcpart}} @{$pkg->{svcparts}}) {
324     foreach my $service (@{$svcpart->{services}}) {
325       print '<TR>' if ($cnt > 0);
326 %>
327   <TD><%=svc_link($svcpart,$service)%></TD>
328   <TD><%=svc_label_link($svcpart,$service)%><BR>(&nbsp;<%=svc_unprovision_link($service)%>&nbsp;)</TD>
329 </TR>
330 <%
331       $cnt++;
332     }
333     if ($svcpart->{count} < $svcpart->{quantity}) {
334       print qq!<TR>\n! if ($cnt > 0);
335       print qq!  <TD COLSPAN=2>!.svc_provision_link($pkg, $svcpart, $conf).qq!</TD>\n</TR>\n!;
336     }
337   }
338 }
339 print '</TABLE>';
340 }
341
342 #end display packages
343 %>
344
345 <% if ( $conf->config('payby-default') ne 'HIDE' ) { %>
346   
347   <BR><BR><A NAME="history"><FONT SIZE="+2">Payment History</FONT></A><BR>
348   <A HREF="<%= $p %>edit/cust_pay.cgi?custnum=<%= $custnum %>">Post cash/check payment</A>
349   | <A HREF="<%= $p %>misc/payment.cgi?payby=CARD;custnum=<%= $custnum %>">Process credit card payment</A>
350   | <A HREF="<%= $p %>misc/payment.cgi?payby=CHEK;custnum=<%= $custnum %>">Process electronic check (ACH) payment</A>
351   <BR><A HREF="<%= $p %>edit/cust_credit.cgi?<%= $custnum %>">Post credit</A>
352   <BR>
353
354   <%
355   #get payment history
356   my @history = ();
357
358   #invoices
359   foreach my $cust_bill ($cust_main->cust_bill) {
360     my $pre = ( $cust_bill->owed > 0 )
361                 ? '<B><FONT SIZE="+1" COLOR="#FF0000">Open '
362                 : '';
363     my $post = ( $cust_bill->owed > 0 ) ? '</FONT></B>' : '';
364     my $invnum = $cust_bill->invnum;
365     push @history, {
366       'date'   => $cust_bill->_date,
367       'desc'   => qq!<A HREF="${p}view/cust_bill.cgi?$invnum">!. $pre.
368                   "Invoice #$invnum (Balance \$". $cust_bill->owed. ')'.
369                   $post. '</A>',
370       'charge' => $cust_bill->charged,
371     };
372   }
373
374   #payments (some false laziness w/credits)
375   foreach my $cust_pay ($cust_main->cust_pay) {
376
377     my $payby = $cust_pay->payby;
378     my $payinfo = $payby eq 'CARD'
379                     ? $cust_pay->payinfo_masked
380                     : $cust_pay->payinfo;
381     my @cust_bill_pay = $cust_pay->cust_bill_pay;
382     my @cust_pay_refund = $cust_pay->cust_pay_refund;
383
384     my $target = "$payby$payinfo";
385     $payby =~ s/^BILL$/Check #/ if $payinfo;
386     $payby =~ s/^CHEK$/Electronic check /;
387     $payby =~ s/^BILL$//;
388     $payby =~ s/^(CARD|COMP)$/$1 /;
389     my $info = $payby ? " ($payby$payinfo)" : '';
390
391     my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
392     if (    scalar(@cust_bill_pay)   == 0
393          && scalar(@cust_pay_refund) == 0 ) {
394       #completely unapplied
395       $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
396       $post = '</FONT></B>';
397       $apply = qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
398                $cust_pay->paynum. '">apply</A>)';
399     } elsif (    scalar(@cust_bill_pay)   == 1
400               && scalar(@cust_pay_refund) == 0
401               && $cust_pay->unapplied == 0     ) {
402       #applied to one invoice, the usual situation
403       $desc = ' applied to Invoice #'. $cust_bill_pay[0]->invnum;
404     } elsif (    scalar(@cust_bill_pay)   == 0
405               && scalar(@cust_pay_refund) == 1
406               && $cust_pay->unapplied == 0     ) {
407       #applied to one refund
408       $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
409     } else {
410       #complicated
411       $desc = '<BR>';
412       foreach my $app ( sort { $a->_date <=> $b->_date }
413                              ( @cust_bill_pay, @cust_pay_refund ) ) {
414         if ( $app->isa('FS::cust_bill_pay') ) {
415           $desc .= '&nbsp;&nbsp;'.
416                    '$'. $app->amount.
417                    ' applied to Invoice #'. $app->invnum.
418                    '<BR>';
419                    #' on '. time2str("%D", $cust_bill_pay->_date).
420         } elsif ( $app->isa('FS::cust_pay_refund') ) {
421           $desc .= '&nbsp;&nbsp;'.
422                    '$'. $app->amount.
423                    ' refunded on'. time2str("%D", $app->_date).
424                    '<BR>';
425         } else {
426           die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
427         }
428       }
429       if ( $cust_pay->unapplied > 0 ) {
430         $desc .= '&nbsp;&nbsp;'.
431                  '<B><FONT COLOR="#FF0000">$'.
432                  $cust_pay->unapplied. ' unapplied</FONT></B>'.
433                  qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
434                  $cust_pay->paynum. '">apply</A>)'.
435                  '<BR>';
436       }
437     }
438
439     my $refund = '';
440     my $refund_days = $conf->config('card_refund-days') || 120;
441     if (    $cust_pay->closed !~ /^Y/i
442          && $cust_pay->payby =~ /^(CARD|CHEK)$/
443          && time-$cust_pay->_date < $refund_days*86400
444          && $cust_pay->unrefunded > 0
445     ) {
446       $refund = qq! (<A HREF="!. qq!${p}edit/cust_refund.cgi?payby=$1;!.
447                 qq!paynum=!. $cust_pay->paynum. qq!">refund</A>)!;
448     }
449
450     my $void = '';
451     if (    $cust_pay->closed !~ /^Y/i
452          && $cust_pay->payby !~  /^(CARD|CHEK)$/
453        ) {
454       $void = qq! (<A HREF="javascript:areyousure('!.
455               qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
456               qq!', 'Are you sure you want to void this payment?')">!.
457               qq!void</A>)!;
458     }
459
460     my $delete = '';
461     if ( $cust_pay->closed !~ /^Y/i && $conf->exists('deletepayments') ) {
462       $delete = qq! (<A HREF="javascript:areyousure('!.
463                 qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
464                 qq!', 'Are you sure you want to delete this payment?')">!.
465                 qq!delete</A>)!;
466     }
467
468     my $unapply = '';
469     if (    $cust_pay->closed !~ /^Y/i
470          && $conf->exists('unapplypayments')
471          && scalar(@cust_bill_pay)           ) {
472       $unapply = qq! (<A HREF="javascript:areyousure('!.
473                  qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
474                  qq!', 'Are you sure you want to unapply this payment?')">!.
475                  qq!unapply</A>)!;
476     }
477
478     push @history, {
479       'date'    => $cust_pay->_date,
480       'desc'    => $pre. "Payment$post$info$desc".
481                    "$apply$refund$void$delete$unapply",
482       'payment' => $cust_pay->paid,
483       'target'  => $target,
484     };
485   }
486
487   #voided payments
488   foreach my $cust_pay_void ($cust_main->cust_pay_void) {
489
490     my $payby = $cust_pay_void->payby;
491     my $payinfo = $payby eq 'CARD'
492                     ? $cust_pay_void->payinfo_masked
493                     : $cust_pay_void->payinfo;
494
495     $payby =~ s/^BILL$/Check #/ if $payinfo;
496     $payby =~ s/^CHEK$/Electronic check /;
497     $payby =~ s/^BILL$//;
498     $payby =~ s/^(CARD|COMP)$/$1 /;
499     my $info = $payby ? " ($payby$payinfo)" : '';
500
501     push @history, {
502       'date'   => $cust_pay_void->_date,
503       'desc'   => "<DEL>Payment $info</DEL> <I>voided ".
504                   time2str("%D", $cust_pay_void->void_date).
505                   " by ". $cust_pay_void->otaker. '</i>',
506       'void_payment' => $cust_pay_void->paid,
507     };
508   
509   }
510
511   #credits (some false laziness w/payments)
512   foreach my $cust_credit ($cust_main->cust_credit) {
513
514     my @cust_credit_bill = $cust_credit->cust_credit_bill;
515     my @cust_credit_refund = $cust_credit->cust_credit_refund;
516
517     my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
518     if (    scalar(@cust_credit_bill)   == 0
519          && scalar(@cust_credit_refund) == 0 ) {
520       #completely unapplied
521       $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
522       $post = '</FONT></B>';
523       $apply = qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
524                $cust_credit->crednum. '">apply</A>)';
525     } elsif (    scalar(@cust_credit_bill)   == 1
526               && scalar(@cust_credit_refund) == 0
527               && $cust_credit->credited == 0      ) {
528       #applied to one invoice, the usual situation
529       $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
530     } elsif (    scalar(@cust_credit_bill)   == 0
531               && scalar(@cust_credit_refund) == 1
532               && $cust_credit->credited == 0      ) {
533       #applied to one refund
534       $desc = ' refunded on '.  time2str("%D", $cust_credit_refund[0]->_date);
535     } else {
536       #complicated
537       $desc = '<BR>';
538       foreach my $app ( sort { $a->_date <=> $b->_date }
539                              ( @cust_credit_bill, @cust_credit_refund ) ) {
540         if ( $app->isa('FS::cust_credit_bill') ) {
541           $desc .= '&nbsp;&nbsp;'.
542                    '$'. $app->amount.
543                    ' applied to Invoice #'. $app->invnum.
544                    '<BR>';
545                    #' on '. time2str("%D", $app->_date).
546         } elsif ( $app->isa('FS::cust_credit_refund') ) {
547           $desc .= '&nbsp;&nbsp;'.
548                    '$'. $app->amount.
549                    ' refunded on'. time2str("%D", $app->_date).
550                    '<BR>';
551         } else {
552           die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
553         }
554       }
555       if ( $cust_credit->credited > 0 ) {
556         $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
557                  $cust_credit->credited. ' unapplied</FONT></B>'.
558                  qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
559                  $cust_credit->crednum. '">apply</A>)'.
560                  '<BR>';
561       }
562     }
563 #
564     my $delete = '';
565     if ( $cust_credit->closed !~ /^Y/i && $conf->exists('deletecredits') ) {
566       $delete = qq! (<A HREF="javascript:areyousure('!.
567                 qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
568                 qq!', 'Are you sure you want to delete this credit?')">!.
569                 qq!delete</A>)!;
570     }
571     
572     my $unapply = '';
573     if (    $cust_credit->closed !~ /^Y/i
574          && $conf->exists('unapplycredits')
575          && scalar(@cust_credit_bill)       ) {
576       $unapply = qq! (<A HREF="javascript:areyousure('!.
577                  qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
578                  qq!', 'Are you sure you want to unapply this credit?')">!.
579                  qq!unapply</A>)!;
580     }
581     
582     push @history, {
583       'date'   => $cust_credit->_date,
584       'desc'   => $pre. "Credit$post by ". $cust_credit->otaker.
585                   ' ('. $cust_credit->reason. ')'.
586                   "$desc$apply$delete$unapply",
587       'credit' => $cust_credit->amount,
588     };
589
590   }
591
592   #refunds
593   foreach my $cust_refund ($cust_main->cust_refund) {
594
595     my $payby = $cust_refund->payby;
596     my $payinfo = $payby eq 'CARD'
597                     ? $cust_refund->payinfo_masked
598                     : $cust_refund->payinfo;
599
600     $payby =~ s/^BILL$/Check #/ if $payinfo;
601     $payby =~ s/^CHEK$/Electronic check /;
602     $payby =~ s/^(CARD|COMP)$/$1 /;
603
604     push @history, {
605       'date'   => $cust_refund->_date,
606       'desc'   => "Refund ($payby$payinfo) by ". $cust_refund->otaker,
607       'refund' => $cust_refund->refund,
608     };
609   
610   }
611   
612   %>
613   
614   <%= table() %>
615   <TR>
616     <TH>Date</TH>
617     <TH>Description</TH>
618     <TH><FONT SIZE=-1>Charge</FONT></TH>
619     <TH><FONT SIZE=-1>Payment</FONT></TH>
620     <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
621     <TH><FONT SIZE=-1>Refund</FONT></TH>
622     <TH><FONT SIZE=-1>Balance</FONT></TH>
623   </TR>
624
625   <%
626   #display payment history
627
628   my %target;
629   my $balance = 0;
630   foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
631
632     my $charge  = exists($item->{'charge'})
633                     ? sprintf('$%.2f', $item->{'charge'})
634                     : '';
635     my $payment = exists($item->{'payment'})
636                     ? sprintf('-&nbsp;$%.2f', $item->{'payment'})
637                     : '';
638     $payment ||= sprintf('<DEL>-&nbsp;$%.2f</DEL>', $item->{'void_payment'})
639       if exists($item->{'void_payment'});
640     my $credit  = exists($item->{'credit'})
641                     ? sprintf('-&nbsp;$%.2f', $item->{'credit'})
642                     : '';
643     my $refund  = exists($item->{'refund'})
644                     ? sprintf('$%.2f', $item->{'refund'})
645                     : '';
646
647     my $target = exists($item->{'target'}) ? $item->{'target'} : '';
648
649     $balance += $item->{'charge'}  if exists $item->{'charge'};
650     $balance -= $item->{'payment'} if exists $item->{'payment'};
651     $balance -= $item->{'credit'}  if exists $item->{'credit'};
652     $balance += $item->{'refund'}  if exists $item->{'refund'};
653     $balance = sprintf("%.2f", $balance);
654     $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
655     ( my $showbalance = '$'. $balance ) =~ s/^\$\-/-&nbsp;\$/;
656
657   %>
658   
659     <TR>
660       <TD>
661         <% unless ( !$target || $target{$target}++ ) { %>
662           <A NAME="<%= $target %>">
663         <% } %>
664         <%= time2str("%D",$item->{'date'}) %>
665         <% if ( $target && $target{$target} == 1 ) { %>
666           </A>
667         <% } %>
668         </FONT>
669       </TD>
670       <TD><%= $item->{'desc'} %></TD>
671       <TD ALIGN="right"><%= $charge  %></TD>
672       <TD ALIGN="right"><%= $payment %></TD>
673       <TD ALIGN="right"><%= $credit  %></TD>
674       <TD ALIGN="right"><%= $refund  %></TD>
675       <TD ALIGN="right"><%= $showbalance %></TD>
676     </TR>
677
678   <% } %>
679   
680   </TABLE>
681
682 <% } %>
683
684 </BODY></HTML>
685
686 <%
687 #subroutines
688
689 sub get_packages {
690   my $cust_main = shift or return undef;
691   my $conf = shift;
692   
693   my @packages = ();
694   
695   foreach my $cust_pkg (
696     $conf->exists('hidecancelledpackages')
697       ? $cust_main->ncancelled_pkgs
698       : $cust_main->all_pkgs
699   ) { 
700   
701     my $part_pkg = $cust_pkg->part_pkg;
702
703     my %pkg = ();
704
705     #to get back to the original object... should use it in the first place!!
706     $pkg{cust_pkg} = $cust_pkg;
707     $pkg{part_pkg} = $part_pkg;
708
709     $pkg{pkgnum} = $cust_pkg->pkgnum;
710     $pkg{pkg} = $part_pkg->pkg;
711     $pkg{pkgpart} = $part_pkg->pkgpart;
712     $pkg{comment} = $part_pkg->getfield('comment');
713     $pkg{freq} = $part_pkg->freq;
714     $pkg{setup} = $cust_pkg->getfield('setup');
715     $pkg{last_bill} = $cust_pkg->getfield('last_bill');
716     $pkg{next_bill} = $cust_pkg->getfield('bill');
717     $pkg{susp} = $cust_pkg->getfield('susp');
718     $pkg{expire} = $cust_pkg->getfield('expire');
719     $pkg{cancel} = $cust_pkg->getfield('cancel');
720
721   
722     my %svcparts = map {
723       $_->svcpart => {
724                        $_->part_svc->hash,
725                        'quantity' => $_->quantity,
726                        'count'    => $cust_pkg->num_cust_svc($_->svcpart),
727                        #'services' => [],
728                      };
729     } $part_pkg->pkg_svc;
730
731     foreach my $cust_svc ( $cust_pkg->cust_svc ) {
732       #warn "svcnum ". $cust_svc->svcnum. " / svcpart ". $cust_svc->svcpart. "\n";
733       my $svc = {
734         'svcnum' => $cust_svc->svcnum,
735         'label'  => ($cust_svc->label)[1],
736       };
737
738       #false laziness with above, to catch extraneous services.  whole
739       #damn thing should be OO...
740       my $svcpart = ( $svcparts{$cust_svc->svcpart} ||= {
741         $cust_svc->part_svc->hash,
742         'quantity' => 0,
743         'count'    => $cust_pkg->num_cust_svc($cust_svc->svcpart),
744         #'services' => [],
745       } );
746
747       push @{$svcpart->{services}}, $svc;
748
749     }
750
751     $pkg{svcparts} = [ values %svcparts ];
752
753     push @packages, \%pkg;
754   
755   }
756   
757   return \@packages;
758
759 }
760
761 sub svc_link {
762
763   my ($svcpart, $svc) = (shift,shift) or return '';
764   return qq!<A HREF="${p}view/$svcpart->{svcdb}.cgi?$svc->{svcnum}">$svcpart->{svc}</A>!;
765
766 }
767
768 sub svc_label_link {
769
770   my ($svcpart, $svc) = (shift,shift) or return '';
771   return qq!<A HREF="${p}view/$svcpart->{svcdb}.cgi?$svc->{svcnum}">$svc->{label}</A>!;
772
773 }
774
775 sub svc_provision_link {
776   my ($pkg, $svcpart, $conf) = @_;
777   ( my $svc_nbsp = $svcpart->{svc} ) =~ s/\s+/&nbsp;/g;
778   my $num_left = $svcpart->{quantity} - $svcpart->{count};
779   my $pkgnum_svcpart = "pkgnum$pkg->{pkgnum}-svcpart$svcpart->{svcpart}";
780
781   my $url;
782   if ( $svcpart->{svcdb} eq 'svc_external'
783        && $conf->exists('svc_external-skip_manual')
784   ) {
785     $url = "${p}edit/process/$svcpart->{svcdb}.cgi?".
786            "pkgnum=$pkg->{pkgnum}&".
787            "svcpart=$svcpart->{svcpart}";
788   } else {
789     $url = "${p}edit/$svcpart->{svcdb}.cgi?$pkgnum_svcpart";
790   }
791
792   my $link = qq!<A CLASS="provision" HREF="$url">!.
793              "Provision&nbsp;$svc_nbsp&nbsp;($num_left)</A>";
794   if ( $conf->exists('legacy_link') ) {
795     $link .= '<BR>'.
796              qq!<A CLASS="provision" HREF="${p}misc/link.cgi?!.
797              qq!$pkgnum_svcpart">!.
798             "Link&nbsp;to&nbsp;legacy&nbsp;$svc_nbsp&nbsp;($num_left)</A>";
799   }
800   $link;
801 }
802
803 sub svc_unprovision_link {
804   my $svc = shift or return '';
805   qq!<A HREF="javascript:areyousure('${p}misc/unprovision.cgi?$svc->{svcnum}',!.
806   qq!'Permanently unprovision and delete this service?')">Unprovision</A>!;
807 }
808
809 # This should be generalized to use config options to determine order.
810 sub pkgsort_pkgnum_cancel {
811   if ($a->{cancel} and $b->{cancel}) {
812     return ($a->{pkgnum} <=> $b->{pkgnum});
813   } elsif ($a->{cancel} or $b->{cancel}) {
814     return (-1) if ($b->{cancel});
815     return (1) if ($a->{cancel});
816     return (0);
817   } else {
818     return($a->{pkgnum} <=> $b->{pkgnum});
819   }
820 }
821
822 sub pkg_datestr {
823   my($pkg, $field, $conf) = @_ or return '';
824   return '&nbsp;' unless $pkg->{$field};
825   my $format = $conf->exists('pkg_showtimes')
826                ? '<B>%D</B>&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
827                : '<B>%b&nbsp;%o,&nbsp;%Y</B>';
828   ( my $strip = time2str($format, $pkg->{$field}) ) =~ s/ (\d)/$1/g;
829   $strip;
830 }
831
832 sub pkg_change_link {
833   my $pkg = shift or return '';
834   return qq!<a href="${p}misc/change_pkg.cgi?$pkg->{pkgnum}">!.
835          qq!Change&nbsp;package</a>!;
836 }
837
838 sub pkg_suspend_link {
839   my $pkg = shift or return '';
840   return qq!<a href="${p}misc/susp_pkg.cgi?$pkg->{pkgnum}">Suspend</a>!;
841 }
842
843 sub pkg_unsuspend_link {
844   my $pkg = shift or return '';
845   return qq!<a href="${p}misc/unsusp_pkg.cgi?$pkg->{pkgnum}">Unsuspend</a>!;
846 }
847
848 sub pkg_cancel_link {
849   my $pkg = shift or return '';
850   qq!<A HREF="javascript:areyousure('${p}misc/cancel_pkg.cgi?$pkg->{pkgnum}', !.
851   qq!'Permanently delete included services and cancel this package?')">!.
852   qq!Cancel now</A> | !.
853   qq!<A HREF="${p}misc/expire_pkg.cgi?$pkg->{pkgnum}">Cancel later</A>!;
854 }
855
856 sub pkg_dates_link {
857   my $pkg = shift or return '';
858   qq!<A HREF="${p}edit/REAL_cust_pkg.cgi?$pkg->{pkgnum}">Edit&nbsp;dates</A>!;
859 }
860
861 sub pkg_customize_link {
862   my $pkg = shift or return '';
863   my $custnum = shift;
864   qq!<A HREF="${p}edit/part_pkg.cgi?keywords=$custnum;clone=$pkg->{pkgpart};!.
865   qq!pkgnum=$pkg->{pkgnum}">Customize</A>!;
866 }
867
868 %>
869