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