4ed30b8139548f127a67a253c99aea43c3e81a68
[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 print '<A NAME="cust_main"></A>';
74
75 print &itable(), '<TR>';
76
77 print '<TD VALIGN="top">';
78
79   print "Billing address", &ntable("#cccccc"), "<TR><TD>",
80         &ntable("#cccccc",2),
81     '<TR><TD ALIGN="right">Contact&nbsp;name</TD>',
82       '<TD COLSPAN=3 BGCOLOR="#ffffff">',
83       $cust_main->last, ', ', $cust_main->first,
84       '</TD>';
85 print '<TD ALIGN="right">SS#</TD><TD BGCOLOR="#ffffff">',
86       $cust_main->ss || '&nbsp', '</TD>'
87   if $conf->exists('show_ss');
88
89 print '</TR>',
90     '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
91       $cust_main->company,
92       '</TD></TR>',
93     '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
94       $cust_main->address1,
95       '</TD></TR>',
96   ;
97   print '<TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
98         $cust_main->address2, '</TD></TR>'
99     if $cust_main->address2;
100   print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
101           $cust_main->city,
102           '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
103           $cust_main->state,
104           '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
105           $cust_main->zip, '</TD></TR>',
106         '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
107           $cust_main->country,
108           '</TD></TR>',
109   ;
110   my $daytime_label = FS::Msgcat::_gettext('daytime') || 'Day&nbsp;Phone';
111   my $night_label = FS::Msgcat::_gettext('night') || 'Night&nbsp;Phone';
112   print '<TR><TD ALIGN="right">'. $daytime_label.
113           '</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
114           $cust_main->daytime || '&nbsp', '</TD></TR>',
115         '<TR><TD ALIGN="right">'. $night_label. 
116           '</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
117           $cust_main->night || '&nbsp', '</TD></TR>',
118         '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
119           $cust_main->fax || '&nbsp', '</TD></TR>',
120         '</TABLE>', "</TD></TR></TABLE>"
121   ;
122
123   if ( defined $cust_main->dbdef_table->column('ship_last') ) {
124
125     my $pre = $cust_main->ship_last ? 'ship_' : '';
126
127     print "<BR>Service address", &ntable("#cccccc"), "<TR><TD>",
128           &ntable("#cccccc",2),
129       '<TR><TD ALIGN="right">Contact name</TD>',
130         '<TD COLSPAN=5 BGCOLOR="#ffffff">',
131         $cust_main->get("${pre}last"), ', ', $cust_main->get("${pre}first"),
132         '</TD></TR>',
133       '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
134         $cust_main->get("${pre}company"),
135         '</TD></TR>',
136       '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
137         $cust_main->get("${pre}address1"),
138         '</TD></TR>',
139     ;
140     print '<TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
141           $cust_main->get("${pre}address2"), '</TD></TR>'
142       if $cust_main->get("${pre}address2");
143     print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
144             $cust_main->get("${pre}city"),
145             '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
146             $cust_main->get("${pre}state"),
147             '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
148             $cust_main->get("${pre}zip"), '</TD></TR>',
149           '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
150             $cust_main->get("${pre}country"),
151             '</TD></TR>',
152     ;
153     print '<TR><TD ALIGN="right">'. $daytime_label. '</TD>',
154           '<TD COLSPAN=5 BGCOLOR="#ffffff">',
155             $cust_main->get("${pre}daytime") || '&nbsp', '</TD></TR>',
156           '<TR><TD ALIGN="right">'. $night_label. '</TD>'.
157           '<TD COLSPAN=5 BGCOLOR="#ffffff">',
158             $cust_main->get("${pre}night") || '&nbsp', '</TD></TR>',
159           '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
160             $cust_main->get("${pre}fax") || '&nbsp', '</TD></TR>',
161           '</TABLE>', "</TD></TR></TABLE>"
162     ;
163
164   }
165
166 print '</TD>';
167
168 print '<TD VALIGN="top">';
169
170   print &ntable("#cccccc"), "<TR><TD>", &ntable("#cccccc",2),
171         '<TR><TD ALIGN="right">Customer&nbsp;number</TD><TD BGCOLOR="#ffffff">',
172         $custnum, '</TD></TR>',
173   ;
174
175   my @agents = qsearch( 'agent', {} );
176   my $agent;
177   unless ( scalar(@agents) == 1 ) {
178     $agent = qsearchs('agent',{ 'agentnum' => $cust_main->agentnum } );
179     print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
180         $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
181   } else {
182     $agent = $agents[0];
183   }
184   my @referrals = qsearch( 'part_referral', {} );
185   unless ( scalar(@referrals) == 1 ) {
186     my $referral = qsearchs('part_referral', {
187       'refnum' => $cust_main->refnum
188     } );
189     print '<TR><TD ALIGN="right">Advertising&nbsp;source</TD><TD BGCOLOR="#ffffff">',
190           $referral->refnum, ": ", $referral->referral, '</TD></TR>';
191   }
192   print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
193     $cust_main->otaker, '</TD></TR>';
194
195   print '<TR><TD ALIGN="right">Referring&nbsp;Customer</TD><TD BGCOLOR="#ffffff">';
196   my $referring_cust_main = '';
197   if ( $cust_main->referral_custnum
198        && ( $referring_cust_main =
199             qsearchs('cust_main', { custnum => $cust_main->referral_custnum } )
200           )
201      ) {
202     print '<A HREF="'. popurl(1). 'cust_main.cgi?'.
203           $cust_main->referral_custnum. '">'.
204           $cust_main->referral_custnum. ': '.
205           ( $referring_cust_main->company
206               ? $referring_cust_main->company. ' ('.
207                   $referring_cust_main->last. ', '. $referring_cust_main->first.
208                   ')'
209               : $referring_cust_main->last. ', '. $referring_cust_main->first
210           ).
211           '</A>';
212   }
213   print '</TD></TR>';
214
215   print '</TABLE></TD></TR></TABLE>';
216
217 print '<BR>';
218
219 if ( $conf->config('payby-default') ne 'HIDE' ) {
220
221   my @invoicing_list = $cust_main->invoicing_list;
222   print "Billing information (",
223        qq!<A HREF="!, popurl(2), qq!misc/bill.cgi?$custnum">!, "Bill now</A>)",
224         &ntable("#cccccc"), "<TR><TD>", &ntable("#cccccc",2),
225         '<TR><TD ALIGN="right">Tax&nbsp;exempt</TD><TD BGCOLOR="#ffffff">',
226         $cust_main->tax ? 'yes' : 'no',
227         '</TD></TR>',
228         '<TR><TD ALIGN="right">Postal&nbsp;invoices</TD><TD BGCOLOR="#ffffff">',
229         ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
230         '</TD></TR>',
231         '<TR><TD ALIGN="right">Email&nbsp;invoices</TD><TD BGCOLOR="#ffffff">',
232         join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
233         '</TD></TR>',
234         '<TR><TD ALIGN="right">Billing&nbsp;type</TD><TD BGCOLOR="#ffffff">',
235   ;
236
237   if ( $cust_main->payby eq 'CARD' || $cust_main->payby eq 'DCRD' ) {
238     my $payinfo = $cust_main->payinfo_masked;
239     print 'Credit&nbsp;card&nbsp;',
240           ( $cust_main->payby eq 'CARD' ? '(automatic)' : '(on-demand)' ),
241           '</TD></TR>',
242           '<TR><TD ALIGN="right">Card number</TD><TD BGCOLOR="#ffffff">',
243           $payinfo, '</TD></TR>',
244           '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
245           $cust_main->paydate, '</TD></TR>',
246           '<TR><TD ALIGN="right">Name on card</TD><TD BGCOLOR="#ffffff">',
247           $cust_main->payname, '</TD></TR>'
248     ;
249   } elsif ( $cust_main->payby eq 'CHEK' || $cust_main->payby eq 'DCHK') {
250     my( $account, $aba ) = split('@', $cust_main->payinfo );
251     print 'Electronic&nbsp;check&nbsp;',
252           ( $cust_main->payby eq 'CHEK' ? '(automatic)' : '(on-demand)' ),
253           '</TD></TR>',
254           '<TR><TD ALIGN="right">Account number</TD><TD BGCOLOR="#ffffff">',
255           $account, '</TD></TR>',
256           '<TR><TD ALIGN="right">ABA/Routing code</TD><TD BGCOLOR="#ffffff">',
257           $aba, '</TD></TR>',
258           '<TR><TD ALIGN="right">Bank name</TD><TD BGCOLOR="#ffffff">',
259           $cust_main->payname, '</TD></TR>'
260     ;
261   } elsif ( $cust_main->payby eq 'LECB' ) {
262     $cust_main->payinfo =~ /^(\d{3})(\d{3})(\d{4})$/;
263     my $payinfo = "$1-$2-$3";
264     print 'Phone&nbsp;bill&nbsp;billing</TD></TR>',
265           '<TR><TD ALIGN="right">Phone number</TD><TD BGCOLOR="#ffffff">',
266           $payinfo, '</TD></TR>',
267     ;
268   } elsif ( $cust_main->payby eq 'BILL' ) {
269     print 'Billing</TD></TR>';
270     print '<TR><TD ALIGN="right">P.O. </TD><TD BGCOLOR="#ffffff">',
271           $cust_main->payinfo, '</TD></TR>',
272       if $cust_main->payinfo;
273     print '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
274           $cust_main->paydate, '</TD></TR>',
275           '<TR><TD ALIGN="right">Attention</TD><TD BGCOLOR="#ffffff">',
276           $cust_main->payname, '</TD></TR>',
277     ;
278   } elsif ( $cust_main->payby eq 'COMP' ) {
279     print 'Complimentary</TD></TR>',
280           '<TR><TD ALIGN="right">Authorized&nbsp;by</TD><TD BGCOLOR="#ffffff">',
281           $cust_main->payinfo, '</TD></TR>',
282           '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
283           $cust_main->paydate, '</TD></TR>',
284     ;
285   }
286
287   print "</TABLE></TD></TR></TABLE>";
288
289 }
290
291 print '</TD></TR></TABLE>';
292
293 if ( defined $cust_main->dbdef_table->column('comments')
294      && $cust_main->comments =~ /[^\s\n\r]/ )
295 {
296   print "<BR>Comments". &ntable("#cccccc"). "<TR><TD>".
297         &ntable("#cccccc",2).
298         '<TR><TD BGCOLOR="#ffffff"><PRE>'.
299         encode_entities($cust_main->comments).
300         '</PRE></TD></TR></TABLE></TABLE>';
301 }
302
303 print '</TD></TR></TABLE>';
304
305 print '<BR>'.
306   '<FORM ACTION="'.popurl(2).'edit/process/quick-cust_pkg.cgi" METHOD="POST">'.
307   qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!.
308   '<SELECT NAME="pkgpart"><OPTION> ';
309
310 foreach my $part_pkg (
311   qsearch( 'part_pkg', { 'disabled' => '' }, '',
312            ' AND 0 < ( SELECT COUNT(*) FROM type_pkgs '.
313            '             WHERE typenum = '. $agent->typenum.
314            '             AND type_pkgs.pkgpart = part_pkg.pkgpart )'
315          )
316 ) {
317   print '<OPTION VALUE="'. $part_pkg->pkgpart. '">'. $part_pkg->pkg. ' - '.
318         $part_pkg->comment;
319 }
320
321 print '</SELECT><INPUT TYPE="submit" VALUE="Order Package"></FORM><BR>';
322
323 if ( $conf->config('payby-default') ne 'HIDE' ) {
324
325   print
326     qq!<FORM ACTION="${p}edit/process/quick-charge.cgi" METHOD="POST">!.
327     qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!.
328     qq!Description:<INPUT TYPE="text" NAME="pkg">!.
329     qq!&nbsp;Amount:<INPUT TYPE="text" NAME="amount" SIZE=6>!.
330     qq!&nbsp;!;
331   
332   #false laziness w/ edit/part_pkg.cgi
333   if ( $conf->exists('enable_taxclasses') ) {
334     print '<SELECT NAME="taxclass">';
335     my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
336       or die dbh->errstr;
337     $sth->execute or die $sth->errstr;
338     foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
339       print qq!<OPTION VALUE="$taxclass"!;
340       #print ' SELECTED' if $taxclass eq $hashref->{taxclass};
341       print qq!>$taxclass</OPTION>!;
342     }
343     print '</SELECT>';
344   } else {
345     print '<INPUT TYPE="hidden" NAME="taxclass" VALUE="">';
346   }
347   
348   print qq!<INPUT TYPE="submit" VALUE="One-time charge"></FORM><BR>!;
349
350 }
351
352 print qq!<A NAME="cust_pkg">Packages</A> !,
353       qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> (preserves services) )!,
354 ;
355
356 #begin display packages
357
358 #get package info
359
360 my $packages = get_packages($cust_main, $conf);
361
362 if ( @$packages ) {
363 %>
364 <TABLE CLASS="package" BORDER=1 CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">
365 <TR>
366   <TH>Package</TH>
367   <TH>Status</TH>
368   <TH COLSPAN=2>Services</TH>
369 </TR>
370 <%
371 foreach my $pkg (sort pkgsort_pkgnum_cancel @$packages) {
372   my $rowspan = 0;
373
374   if ($pkg->{cancel}) {
375     $rowspan = 0;
376   } else {
377     foreach my $svcpart (@{$pkg->{svcparts}}) {
378       $rowspan += $svcpart->{count};
379       $rowspan++ if ($svcpart->{count} < $svcpart->{quantity});
380     }
381   } 
382
383 %>
384 <!--pkgnum: <%=$pkg->{pkgnum}%>-->
385 <TR>
386   <TD ROWSPAN=<%=$rowspan%>>
387     <A NAME="cust_pkg<%=$pkg->{pkgnum}%>"><%=$pkg->{pkgnum}%></A>:
388     <%=$pkg->{pkg}%> - <%=$pkg->{comment}%><BR>
389 <% unless ($pkg->{cancel}) { %>
390     (&nbsp;<%=pkg_change_link($pkg)%>&nbsp;)
391     (&nbsp;<%=pkg_dates_link($pkg)%>&nbsp;|&nbsp;<%=pkg_customize_link($pkg,$custnum)%>&nbsp;)
392 <% } %>
393   </TD>
394 <%
395   #foreach (qw(setup last_bill next_bill susp expire cancel)) {
396   #  print qq!  <TD ROWSPAN=$rowspan>! . pkg_datestr($pkg,$_,$conf) . qq!</TD>\n!;
397   #}
398   print "<TD ROWSPAN=$rowspan>". &itable('');
399
400   sub freq {
401
402     #false laziness w/edit/part_pkg.cgi
403     my %freq = ( #move this
404       '1d' => 'daily',
405       '1w' => 'weekly',
406       '2w' => 'biweekly (every 2 weeks)',
407       '1'  => 'monthly',
408       '2'  => 'bimonthly (every 2 months)',
409       '3'  => 'quarterly (every 3 months)',
410       '6'  => 'semiannually (every 6 months)',
411       '12' => 'annually',
412       '24' => 'biannually (every 2 years)',
413     );
414
415     my $freq = shift;
416     exists $freq{$freq} ? $freq{$freq} : "every&nbsp;$freq&nbsp;months";
417   }
418
419   #eomove
420
421   if ( $pkg->{cancel} ) { #status: cancelled
422
423     print '<TR><TD><FONT COLOR="#ff0000"><B>Cancelled&nbsp;</B></FONT></TD>'.
424           '<TD>'. pkg_datestr($pkg,'cancel',$conf). '</TD></TR>';
425     unless ( $pkg->{setup} ) {
426       print '<TR><TD COLSPAN=2>Never billed</TD></TR>';
427     } else {
428       print "<TR><TD>Setup&nbsp;</TD><TD>".
429             pkg_datestr($pkg, 'setup',$conf). '</TD></TR>';
430       print "<TR><TD>Last&nbsp;bill&nbsp;</TD><TD>".
431             pkg_datestr($pkg, 'last_bill',$conf). '</TD></TR>'
432         if $pkg->{'last_bill'};
433       print "<TR><TD>Suspended&nbsp;</TD><TD>".
434             pkg_datestr($pkg, 'susp',$conf). '</TD></TR>'
435         if $pkg->{'susp'};
436     }
437
438   } else {
439
440     if ( $pkg->{susp} ) { #status: suspended
441       print '<TR><TD><FONT COLOR="#FF9900"><B>Suspended</B>&nbsp;</FONT></TD>'.
442             '<TD>'. pkg_datestr($pkg,'susp',$conf). '</TD></TR>';
443       unless ( $pkg->{setup} ) {
444         print '<TR><TD COLSPAN=2>Never billed</TD></TR>';
445       } else {
446         print "<TR><TD>Setup&nbsp;</TD><TD>". 
447               pkg_datestr($pkg, 'setup',$conf). '</TD></TR>';
448       }
449       print "<TR><TD>Last&nbsp;bill&nbsp;</TD><TD>".
450             pkg_datestr($pkg, 'last_bill',$conf). '</TD></TR>'
451         if $pkg->{'last_bill'};
452       # next bill ??
453       print "<TR><TD>Expires&nbsp;</TD><TD>".
454             pkg_datestr($pkg, 'expire',$conf). '</TD></TR>'
455         if $pkg->{'expire'};
456       print '<TR><TD COLSPAN=2>(&nbsp;'. pkg_unsuspend_link($pkg).
457             '&nbsp;|&nbsp;'. pkg_cancel_link($pkg). '&nbsp;)</TD></TR>';
458
459     } else { #status: active
460
461       unless ( $pkg->{setup} ) { #not setup
462
463         print '<TR><TD COLSPAN=2>Not&nbsp;yet&nbsp;billed&nbsp;(';
464         unless ( $pkg->{freq} ) {
465           print 'one-time&nbsp;charge)</TD></TR>';
466           print '<TR><TD COLSPAN=2>(&nbsp;'. pkg_cancel_link($pkg).
467                 '&nbsp;)</TD</TR>';
468         } else {
469           print 'billed&nbsp;'. freq($pkg->{freq}). ')</TD></TR>';
470         }
471
472       } else { #setup
473
474         unless ( $pkg->{freq} ) {
475           print "<TR><TD COLSPAN=2>One-time&nbsp;charge</TD></TR>".
476                 '<TR><TD>Billed&nbsp;</TD><TD>'.
477                 pkg_datestr($pkg,'setup',$conf). '</TD></TR>';
478         } else {
479           print '<TR><TD COLSPAN=2><FONT COLOR="#00CC00"><B>Active</B></FONT>'.
480                 ',&nbsp;billed&nbsp;'. freq($pkg->{freq}). '</TD></TR>'.
481                 '<TR><TD>Setup&nbsp;</TD><TD>'.
482                 pkg_datestr($pkg, 'setup',$conf). '</TD></TR>';
483         }
484
485       }
486
487       print "<TR><TD>Last&nbsp;bill&nbsp;</TD><TD>".
488             pkg_datestr($pkg, 'last_bill',$conf). '</TD></TR>'
489         if $pkg->{'last_bill'};
490       print "<TR><TD>Next&nbsp;bill&nbsp;</TD><TD>".
491             pkg_datestr($pkg, 'next_bill',$conf). '</TD></TR>'
492         if $pkg->{'next_bill'};
493       print "<TR><TD>Expires&nbsp;</TD><TD>".
494             pkg_datestr($pkg, 'expire',$conf). '</TD></TR>'
495         if $pkg->{'expire'};
496       if ( $pkg->{freq} ) {
497         print '<TR><TD COLSPAN=2>(&nbsp;'. pkg_suspend_link($pkg).
498               '&nbsp;|&nbsp;'. pkg_cancel_link($pkg). '&nbsp;)</TD></TR>';
499       }
500
501     }
502
503   }
504
505   print "</TABLE></TD>\n";
506
507   if ($rowspan == 0) { print qq!</TR>\n!; next; }
508
509   my $cnt = 0;
510   foreach my $svcpart (sort {$a->{svcpart} <=> $b->{svcpart}} @{$pkg->{svcparts}}) {
511     foreach my $service (@{$svcpart->{services}}) {
512       print '<TR>' if ($cnt > 0);
513 %>
514   <TD><%=svc_link($svcpart,$service)%></TD>
515   <TD><%=svc_label_link($svcpart,$service)%><BR>(&nbsp;<%=svc_unprovision_link($service)%>&nbsp;)</TD>
516 </TR>
517 <%
518       $cnt++;
519     }
520     if ($svcpart->{count} < $svcpart->{quantity}) {
521       print qq!<TR>\n! if ($cnt > 0);
522       print qq!  <TD COLSPAN=2>!.svc_provision_link($pkg, $svcpart, $conf).qq!</TD>\n</TR>\n!;
523     }
524   }
525 }
526 print '</TABLE>';
527 }
528
529 #end display packages
530 %>
531
532 <% if ( $conf->config('payby-default') ne 'HIDE' ) { %>
533   
534   <BR><BR><A NAME="history"><FONT SIZE="+2">Payment History</FONT></A><BR>
535   <A HREF="<%= $p %>edit/cust_pay.cgi?custnum=<%= $custnum %>">Post cash/check payment</A>
536   | <A HREF="<%= $p %>misc/payment.cgi?payby=CARD;custnum=<%= $custnum %>">Process credit card payment</A>
537   | <A HREF="<%= $p %>misc/payment.cgi?payby=CHEK;custnum=<%= $custnum %>">Process electronic check (ACH) payment</A>
538   <BR><A HREF="<%= $p %>edit/cust_credit.cgi?<%= $custnum %>">Post credit</A>
539   <BR>
540
541   <%
542   #get payment history
543   my @history = ();
544
545   #invoices
546   foreach my $cust_bill ($cust_main->cust_bill) {
547     my $pre = ( $cust_bill->owed > 0 )
548                 ? '<B><FONT SIZE="+1" COLOR="#FF0000">Open '
549                 : '';
550     my $post = ( $cust_bill->owed > 0 ) ? '</FONT></B>' : '';
551     my $invnum = $cust_bill->invnum;
552     push @history, {
553       'date'   => $cust_bill->_date,
554       'desc'   => qq!<A HREF="${p}view/cust_bill.cgi?$invnum">!. $pre.
555                   "Invoice #$invnum (Balance \$". $cust_bill->owed. ')'.
556                   $post. '</A>',
557       'charge' => $cust_bill->charged,
558     };
559   }
560
561   #payments (some false laziness w/credits)
562   foreach my $cust_pay ($cust_main->cust_pay) {
563
564     my $payby = $cust_pay->payby;
565     my $payinfo = $payby eq 'CARD'
566                     ? $cust_pay->payinfo_masked
567                     : $cust_pay->payinfo;
568     my @cust_bill_pay = $cust_pay->cust_bill_pay;
569     my @cust_pay_refund = $cust_pay->cust_pay_refund;
570
571     my $target = "$payby$payinfo";
572     $payby =~ s/^BILL$/Check #/ if $payinfo;
573     $payby =~ s/^BILL$//;
574     $payby =~ s/^(CARD|COMP)$/$1 /;
575     my $info = $payby ? " ($payby$payinfo)" : '';
576
577     my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
578     if (    scalar(@cust_bill_pay)   == 0
579          && scalar(@cust_pay_refund) == 0 ) {
580       #completely unapplied
581       $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
582       $post = '</FONT></B>';
583       $apply = qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
584                $cust_pay->paynum. '">apply</A>)';
585     } elsif (    scalar(@cust_bill_pay)   == 1
586               && scalar(@cust_pay_refund) == 0
587               && $cust_pay->unapplied == 0     ) {
588       #applied to one invoice, the usual situation
589       $desc = ' applied to Invoice #'. $cust_bill_pay[0]->invnum;
590     } elsif (    scalar(@cust_bill_pay)   == 0
591               && scalar(@cust_pay_refund) == 1
592               && $cust_pay->unapplied == 0     ) {
593       #applied to one refund
594       $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
595     } else {
596       #complicated
597       $desc = '<BR>';
598       foreach my $app ( sort { $a->_date <=> $b->_date }
599                              ( @cust_bill_pay, @cust_pay_refund ) ) {
600         if ( $app->isa('FS::cust_bill_pay') ) {
601           $desc .= '&nbsp;&nbsp;'.
602                    '$'. $app->amount.
603                    ' applied to Invoice #'. $app->invnum.
604                    '<BR>';
605                    #' on '. time2str("%D", $cust_bill_pay->_date).
606         } elsif ( $app->isa('FS::cust_pay_refund') ) {
607           $desc .= '&nbsp;&nbsp;'.
608                    '$'. $app->amount.
609                    ' refunded on'. time2str("%D", $app->_date).
610                    '<BR>';
611         } else {
612           die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
613         }
614       }
615       if ( $cust_pay->unapplied > 0 ) {
616         $desc .= '&nbsp;&nbsp;'.
617                  '<B><FONT COLOR="#FF0000">$'.
618                  $cust_pay->unapplied. ' unapplied</FONT></B>'.
619                  qq! (<A HREF="${p}edit/cust_bill_pay.cgi?!.
620                  $cust_pay->paynum. '">apply</A>)'.
621                  '<BR>';
622       }
623     }
624
625     my $refund = '';
626     my $refund_days = $conf->config('card_refund-days') || 120;
627     if (    $cust_pay->closed !~ /^Y/i
628          && $cust_pay->payby eq 'CARD' 
629          && time-$cust_pay->_date < $refund_days*86400
630          && $cust_pay->unrefunded > 0
631     ) {
632       $refund = qq! (<A HREF="!. qq!${p}edit/cust_refund.cgi?payby=CARD;!.
633                 qq!paynum=!. $cust_pay->paynum. qq!">refund</A>)!;
634     }
635
636     my $void = '';
637     if (    $cust_pay->closed !~ /^Y/i
638          && $cust_pay->payby ne 'CARD'
639        ) {
640       $void = qq! (<A HREF="javascript:areyousure('!.
641               qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
642               qq!', 'Are you sure you want to void this payment?')">!.
643               qq!void</A>)!;
644     }
645
646     my $delete = '';
647     if ( $cust_pay->closed !~ /^Y/i && $conf->exists('deletepayments') ) {
648       $delete = qq! (<A HREF="javascript:areyousure('!.
649                 qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
650                 qq!', 'Are you sure you want to delete this payment?')">!.
651                 qq!delete</A>)!;
652     }
653
654     my $unapply = '';
655     if (    $cust_pay->closed !~ /^Y/i
656          && $conf->exists('unapplypayments')
657          && scalar(@cust_bill_pay)           ) {
658       $unapply = qq! (<A HREF="javascript:areyousure('!.
659                  qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
660                  qq!', 'Are you sure you want to unapply this payment?')">!.
661                  qq!unapply</A>)!;
662     }
663
664     push @history, {
665       'date'    => $cust_pay->_date,
666       'desc'    => $pre. "Payment$post$info$desc".
667                    "$apply$refund$void$delete$unapply",
668       'payment' => $cust_pay->paid,
669       'target'  => $target,
670     };
671   }
672
673   #voided payments
674   foreach my $cust_pay_void ($cust_main->cust_pay_void) {
675
676     my $payby = $cust_pay_void->payby;
677     my $payinfo = $payby eq 'CARD'
678                     ? $cust_pay_void->payinfo_masked
679                     : $cust_pay_void->payinfo;
680
681     $payby =~ s/^BILL$/Check #/ if $payinfo;
682     $payby =~ s/^BILL$//;
683     $payby =~ s/^(CARD|COMP)$/$1 /;
684     my $info = $payby ? " ($payby$payinfo)" : '';
685
686     push @history, {
687       'date'   => $cust_pay_void->_date,
688       'desc'   => "<DEL>Payment $info</DEL> <I>voided ".
689                   time2str("%D", $cust_pay_void->void_date).
690                   " by ". $cust_pay_void->otaker. '</i>',
691       'void_payment' => $cust_pay_void->paid,
692     };
693   
694   }
695
696   #credits (some false laziness w/payments)
697   foreach my $cust_credit ($cust_main->cust_credit) {
698
699     my @cust_credit_bill = $cust_credit->cust_credit_bill;
700     my @cust_credit_refund = $cust_credit->cust_credit_refund;
701
702     my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
703     if (    scalar(@cust_credit_bill)   == 0
704          && scalar(@cust_credit_refund) == 0 ) {
705       #completely unapplied
706       $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
707       $post = '</FONT></B>';
708       $apply = qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
709                $cust_credit->crednum. '">apply</A>)';
710     } elsif (    scalar(@cust_credit_bill)   == 1
711               && scalar(@cust_credit_refund) == 0
712               && $cust_credit->credited == 0      ) {
713       #applied to one invoice, the usual situation
714       $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
715     } elsif (    scalar(@cust_credit_bill)   == 0
716               && scalar(@cust_credit_refund) == 1
717               && $cust_credit->credited == 0      ) {
718       #applied to one refund
719       $desc = ' refunded on '.  time2str("%D", $cust_credit_refund[0]->_date);
720     } else {
721       #complicated
722       $desc = '<BR>';
723       foreach my $app ( sort { $a->_date <=> $b->_date }
724                              ( @cust_credit_bill, @cust_credit_refund ) ) {
725         if ( $app->isa('FS::cust_credit_bill') ) {
726           $desc .= '&nbsp;&nbsp;'.
727                    '$'. $app->amount.
728                    ' applied to Invoice #'. $app->invnum.
729                    '<BR>';
730                    #' on '. time2str("%D", $app->_date).
731         } elsif ( $app->isa('FS::cust_credit_refund') ) {
732           $desc .= '&nbsp;&nbsp;'.
733                    '$'. $app->amount.
734                    ' refunded on'. time2str("%D", $app->_date).
735                    '<BR>';
736         } else {
737           die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
738         }
739       }
740       if ( $cust_credit->credited > 0 ) {
741         $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
742                  $cust_credit->credited. ' unapplied</FONT></B>'.
743                  qq! (<A HREF="${p}edit/cust_credit_bill.cgi?!.
744                  $cust_credit->crednum. '">apply</A>)'.
745                  '<BR>';
746       }
747     }
748 #
749     my $delete = '';
750     if ( $cust_credit->closed !~ /^Y/i && $conf->exists('deletecredits') ) {
751       $delete = qq! (<A HREF="javascript:areyousure('!.
752                 qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
753                 qq!', 'Are you sure you want to delete this credit?')">!.
754                 qq!delete</A>)!;
755     }
756     
757     my $unapply = '';
758     if (    $cust_credit->closed !~ /^Y/i
759          && $conf->exists('unapplycredits')
760          && scalar(@cust_credit_bill)       ) {
761       $unapply = qq! (<A HREF="javascript:areyousure('!.
762                  qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
763                  qq!', 'Are you sure you want to unapply this credit?')">!.
764                  qq!unapply</A>)!;
765     }
766     
767     push @history, {
768       'date'   => $cust_credit->_date,
769       'desc'   => $pre. "Credit$post by ". $cust_credit->otaker.
770                   ' ('. $cust_credit->reason. ')'.
771                   "$desc$apply$delete$unapply",
772       'credit' => $cust_credit->amount,
773     };
774
775   }
776
777   #refunds
778   foreach my $cust_refund ($cust_main->cust_refund) {
779
780     my $payby = $cust_refund->payby;
781     my $payinfo = $payby eq 'CARD'
782                     ? $cust_refund->payinfo_masked
783                     : $cust_refund->payinfo;
784
785     $payby =~ s/^BILL$/Check #/ if $payinfo;
786     $payby =~ s/^(CARD|COMP)$/$1 /;
787
788     push @history, {
789       'date'   => $cust_refund->_date,
790       'desc'   => "Refund ($payby$payinfo) by ". $cust_refund->otaker,
791       'refund' => $cust_refund->refund,
792     };
793   
794   }
795   
796   %>
797   
798   <%= table() %>
799   <TR>
800     <TH>Date</TH>
801     <TH>Description</TH>
802     <TH><FONT SIZE=-1>Charge</FONT></TH>
803     <TH><FONT SIZE=-1>Payment</FONT></TH>
804     <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
805     <TH><FONT SIZE=-1>Refund</FONT></TH>
806     <TH><FONT SIZE=-1>Balance</FONT></TH>
807   </TR>
808
809   <%
810   #display payment history
811
812   my %target;
813   my $balance = 0;
814   foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
815
816     my $charge  = exists($item->{'charge'})
817                     ? sprintf('$%.2f', $item->{'charge'})
818                     : '';
819     my $payment = exists($item->{'payment'})
820                     ? sprintf('-&nbsp;$%.2f', $item->{'payment'})
821                     : '';
822     $payment ||= sprintf('<DEL>-&nbsp;$%.2f</DEL>', $item->{'void_payment'})
823       if exists($item->{'void_payment'});
824     my $credit  = exists($item->{'credit'})
825                     ? sprintf('-&nbsp;$%.2f', $item->{'credit'})
826                     : '';
827     my $refund  = exists($item->{'refund'})
828                     ? sprintf('$%.2f', $item->{'refund'})
829                     : '';
830
831     my $target = exists($item->{'target'}) ? $item->{'target'} : '';
832
833     $balance += $item->{'charge'}  if exists $item->{'charge'};
834     $balance -= $item->{'payment'} if exists $item->{'payment'};
835     $balance -= $item->{'credit'}  if exists $item->{'credit'};
836     $balance += $item->{'refund'}  if exists $item->{'refund'};
837     $balance = sprintf("%.2f", $balance);
838     $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
839     ( my $showbalance = '$'. $balance ) =~ s/^\$\-/-&nbsp;\$/;
840
841   %>
842   
843     <TR>
844       <TD>
845         <% unless ( !$target || $target{$target}++ ) { %>
846           <A NAME="<%= $target %>">
847         <% } %>
848         <%= time2str("%D",$item->{'date'}) %>
849         <% if ( $target && $target{$target} == 1 ) { %>
850           </A>
851         <% } %>
852         </FONT>
853       </TD>
854       <TD><%= $item->{'desc'} %></TD>
855       <TD ALIGN="right"><%= $charge  %></TD>
856       <TD ALIGN="right"><%= $payment %></TD>
857       <TD ALIGN="right"><%= $credit  %></TD>
858       <TD ALIGN="right"><%= $refund  %></TD>
859       <TD ALIGN="right"><%= $showbalance %></TD>
860     </TR>
861
862   <% } %>
863   
864   </TABLE>
865
866 <% } %>
867
868 </BODY></HTML>
869
870 <%
871 #subroutines
872
873 sub get_packages {
874   my $cust_main = shift or return undef;
875   my $conf = shift;
876   
877   my @packages = ();
878   
879   foreach my $cust_pkg (
880     $conf->exists('hidecancelledpackages')
881       ? $cust_main->ncancelled_pkgs
882       : $cust_main->all_pkgs
883   ) { 
884   
885     my $part_pkg = $cust_pkg->part_pkg;
886   
887     my %pkg = ();
888     $pkg{pkgnum} = $cust_pkg->pkgnum;
889     $pkg{pkg} = $part_pkg->pkg;
890     $pkg{pkgpart} = $part_pkg->pkgpart;
891     $pkg{comment} = $part_pkg->getfield('comment');
892     $pkg{freq} = $part_pkg->freq;
893     $pkg{setup} = $cust_pkg->getfield('setup');
894     $pkg{last_bill} = $cust_pkg->getfield('last_bill');
895     $pkg{next_bill} = $cust_pkg->getfield('bill');
896     $pkg{susp} = $cust_pkg->getfield('susp');
897     $pkg{expire} = $cust_pkg->getfield('expire');
898     $pkg{cancel} = $cust_pkg->getfield('cancel');
899   
900     my %svcparts = map {
901       $_->svcpart => {
902                        $_->part_svc->hash,
903                        'quantity' => $_->quantity,
904                        'count'    => $cust_pkg->num_cust_svc($_->svcpart),
905                        #'services' => [],
906                      };
907     } $part_pkg->pkg_svc;
908
909     foreach my $cust_svc ( $cust_pkg->cust_svc ) {
910       #warn "svcnum ". $cust_svc->svcnum. " / svcpart ". $cust_svc->svcpart. "\n";
911       my $svc = {
912         'svcnum' => $cust_svc->svcnum,
913         'label'  => ($cust_svc->label)[1],
914       };
915
916       #false laziness with above, to catch extraneous services.  whole
917       #damn thing should be OO...
918       my $svcpart = ( $svcparts{$cust_svc->svcpart} ||= {
919         $cust_svc->part_svc->hash,
920         'quantity' => 0,
921         'count'    => $cust_pkg->num_cust_svc($cust_svc->svcpart),
922         #'services' => [],
923       } );
924
925       push @{$svcpart->{services}}, $svc;
926
927     }
928
929     $pkg{svcparts} = [ values %svcparts ];
930
931     push @packages, \%pkg;
932   
933   }
934   
935   return \@packages;
936
937 }
938
939 sub svc_link {
940
941   my ($svcpart, $svc) = (shift,shift) or return '';
942   return qq!<A HREF="${p}view/$svcpart->{svcdb}.cgi?$svc->{svcnum}">$svcpart->{svc}</A>!;
943
944 }
945
946 sub svc_label_link {
947
948   my ($svcpart, $svc) = (shift,shift) or return '';
949   return qq!<A HREF="${p}view/$svcpart->{svcdb}.cgi?$svc->{svcnum}">$svc->{label}</A>!;
950
951 }
952
953 sub svc_provision_link {
954   my ($pkg, $svcpart, $conf) = @_;
955   ( my $svc_nbsp = $svcpart->{svc} ) =~ s/\s+/&nbsp;/g;
956   my $pkgnum_svcpart = "pkgnum$pkg->{pkgnum}-svcpart$svcpart->{svcpart}";
957   my $num_left = $svcpart->{quantity} - $svcpart->{count};
958
959   my $link = qq!<A CLASS="provision" HREF="${p}edit/$svcpart->{svcdb}.cgi?!.
960              qq!$pkgnum_svcpart">!.
961              "Provision&nbsp;$svc_nbsp&nbsp;($num_left)</A>";
962   if ( $conf->exists('legacy_link') ) {
963     $link .= '<BR>'.
964              qq!<A CLASS="provision" HREF="${p}misc/link.cgi?!.
965              qq!$pkgnum_svcpart">!.
966             "Link&nbsp;to&nbsp;legacy&nbsp;$svc_nbsp&nbsp;($num_left)</A>";
967   }
968   $link;
969 }
970
971 sub svc_unprovision_link {
972   my $svc = shift or return '';
973   qq!<A HREF="javascript:areyousure('${p}misc/unprovision.cgi?$svc->{svcnum}',!.
974   qq!'Permanently unprovision and delete this service?')">Unprovision</A>!;
975 }
976
977 # This should be generalized to use config options to determine order.
978 sub pkgsort_pkgnum_cancel {
979   if ($a->{cancel} and $b->{cancel}) {
980     return ($a->{pkgnum} <=> $b->{pkgnum});
981   } elsif ($a->{cancel} or $b->{cancel}) {
982     return (-1) if ($b->{cancel});
983     return (1) if ($a->{cancel});
984     return (0);
985   } else {
986     return($a->{pkgnum} <=> $b->{pkgnum});
987   }
988 }
989
990 sub pkg_datestr {
991   my($pkg, $field, $conf) = @_ or return '';
992   return '&nbsp;' unless $pkg->{$field};
993   my $format = $conf->exists('pkg_showtimes')
994                ? '<B>%D</B>&nbsp;<FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>'
995                : '<B>%b&nbsp;%o,&nbsp;%Y</B>';
996   ( my $strip = time2str($format, $pkg->{$field}) ) =~ s/ (\d)/$1/g;
997   $strip;
998 }
999
1000 sub pkg_change_link {
1001   my $pkg = shift or return '';
1002   return qq!<a href="${p}misc/change_pkg.cgi?$pkg->{pkgnum}">!.
1003          qq!Change&nbsp;package</a>!;
1004 }
1005
1006 sub pkg_suspend_link {
1007   my $pkg = shift or return '';
1008   return qq!<a href="${p}misc/susp_pkg.cgi?$pkg->{pkgnum}">Suspend</a>!;
1009 }
1010
1011 sub pkg_unsuspend_link {
1012   my $pkg = shift or return '';
1013   return qq!<a href="${p}misc/unsusp_pkg.cgi?$pkg->{pkgnum}">Unsuspend</a>!;
1014 }
1015
1016 sub pkg_cancel_link {
1017   my $pkg = shift or return '';
1018   qq!<A HREF="javascript:areyousure('${p}misc/cancel_pkg.cgi?$pkg->{pkgnum}', !.
1019   qq!'Permanently delete included services and cancel this package?')">!.
1020   qq!Cancel now</A> | !.
1021   qq!<A HREF="${p}misc/expire_pkg.cgi?$pkg->{pkgnum}">Cancel later</A>!;
1022 }
1023
1024 sub pkg_dates_link {
1025   my $pkg = shift or return '';
1026   qq!<A HREF="${p}edit/REAL_cust_pkg.cgi?$pkg->{pkgnum}">Edit&nbsp;dates</A>!;
1027 }
1028
1029 sub pkg_customize_link {
1030   my $pkg = shift or return '';
1031   my $custnum = shift;
1032   qq!<A HREF="${p}edit/part_pkg.cgi?keywords=$custnum;clone=$pkg->{pkgpart};!.
1033   qq!pkgnum=$pkg->{pkgnum}">Customize</A>!;
1034 }
1035
1036 %>
1037