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