add svc_forward, depriciate svc_acct_sm
[freeside.git] / htdocs / view / cust_main.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: cust_main.cgi,v 1.20 2001-06-03 11:40:48 ivan Exp $
4 #
5 # Usage: cust_main.cgi custnum
6 #        http://server.name/path/cust_main.cgi?custnum
7 #
8 # the payment history section could use some work, see below
9
10 # ivan@voicenet.com 96-nov-29 -> 96-dec-11
11 #
12 # added navigation bar (go to main menu ;)
13 # ivan@voicenet.com 97-jan-30
14 #
15 # changes to the way credits/payments are applied (the links are here).
16 # ivan@voicenet.com 97-apr-21
17 #
18 # added debugging code to diagnose CPU sucking problem.
19 # ivan@voicenet.com 97-may-19
20 #
21 # CPU sucking problem was in comment code?  fixed?
22 # ivan@voicenet.com 97-may-22
23 #
24 # rewrote for new API
25 # ivan@voicenet.com 97-jul-22
26 #
27 # Changes to allow page to work at a relative position in server
28 # Changed 'day' to 'daytime' because Pg6.3 reserves the day word
29 #       bmccane@maxbaud.net     98-apr-3
30 #
31 # lose background, FS::CGI ivan@sisd.com 98-sep-2
32 #
33 # $Log: cust_main.cgi,v $
34 # Revision 1.20  2001-06-03 11:40:48  ivan
35 # inline doc clarification
36 #
37 # Revision 1.19  2001/04/22 01:38:39  ivan
38 # svc_domain needs to import dbh sub from Record
39 # view/cust_main.cgi needs to use ->owed method, not check (depriciated) owed field
40 # search/cust_bill.cgi redirect error when there's only one invoice
41 #
42 # Revision 1.18  1999/08/12 04:16:01  ivan
43 # hidecancelledpackages config option
44 #
45 # Revision 1.17  1999/04/15 16:44:36  ivan
46 # delete customers
47 #
48 # Revision 1.16  1999/04/09 04:22:34  ivan
49 # also table()
50 #
51 # Revision 1.15  1999/04/09 03:52:55  ivan
52 # explicit & for table/itable/ntable
53 #
54 # Revision 1.14  1999/04/08 04:04:37  ivan
55 # eliminate double // in links
56 #
57 # Revision 1.13  1999/02/28 00:04:00  ivan
58 # removed misleading comments
59 #
60 # Revision 1.12  1999/02/07 09:59:40  ivan
61 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
62 #
63 # Revision 1.11  1999/01/25 12:26:04  ivan
64 # yet more mod_perl stuff
65 #
66 # Revision 1.10  1999/01/19 05:14:19  ivan
67 # for mod_perl: no more top-level my() variables; use vars instead
68 # also the last s/create/new/;
69 #
70 # Revision 1.9  1999/01/18 09:41:43  ivan
71 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
72 # (good idea anyway)
73 #
74 # Revision 1.8  1999/01/18 09:22:35  ivan
75 # changes to track email addresses for email invoicing
76 #
77 # Revision 1.7  1998/12/30 23:03:34  ivan
78 # bugfixes; fields isn't exported by derived classes
79 #
80 # Revision 1.6  1998/12/23 02:42:33  ivan
81 # remove double '/' in link urls
82 #
83 # Revision 1.5  1998/12/23 02:36:28  ivan
84 # use FS::cust_refund; to eliminate warning
85 #
86 # Revision 1.4  1998/12/17 09:57:21  ivan
87 # s/CGI::(Base|Request)/CGI.pm/;
88 #
89 # Revision 1.3  1998/11/15 13:14:20  ivan
90 # first pass as per-customer custom pricing
91 #
92 # Revision 1.2  1998/11/13 11:28:08  ivan
93 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
94 #
95
96 use strict;
97 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral 
98               @packages $package @history @bills $bill @credits $credit
99               $balance $item @agents @referrals @invoicing_list $n1 $conf ); 
100 use CGI;
101 use CGI::Carp qw(fatalsToBrowser);
102 use Date::Format;
103 use FS::UID qw(cgisuidsetup);
104 use FS::Record qw(qsearchs qsearch);
105 use FS::CGI qw(header menubar popurl table itable ntable);
106 use FS::cust_credit;
107 use FS::cust_pay;
108 use FS::cust_bill;
109 use FS::part_pkg;
110 use FS::cust_pkg;
111 use FS::part_referral;
112 use FS::agent;
113 use FS::cust_main;
114 use FS::cust_refund;
115
116 $cgi = new CGI;
117 &cgisuidsetup($cgi);
118
119 $conf = new FS::Conf;
120
121 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
122   'Main Menu' => popurl(2)
123 ));
124
125 die "No customer specified (bad URL)!" unless $cgi->keywords;
126 ($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
127 $query =~ /^(\d+)$/;
128 $custnum = $1;
129 $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
130 die "Customer not found!" unless $cust_main;
131 $hashref = $cust_main->hashref;
132
133 print &itable(), '<TR><TD><A NAME="cust_main"></A>';
134
135 print qq!<A HREF="!, popurl(2), 
136       qq!edit/cust_main.cgi?$custnum">Edit this customer</A>!;
137 print qq! | <A HREF="!, popurl(2), 
138       qq!misc/delete-customer.cgi?$custnum"> Delete this customer</A>!
139   if $conf->exists('deletecustomers');
140 print &ntable("#c0c0c0"), "<TR><TD>", &ntable("#c0c0c0",2),
141       '<TR><TD ALIGN="right">Customer number</TD><TD BGCOLOR="#ffffff">',
142       $custnum, '</TD></TR>',
143 ;
144
145 @agents = qsearch( 'agent', {} );
146 unless ( scalar(@agents) == 1 ) {
147   $agent = qsearchs('agent',{
148     'agentnum' => $cust_main->agentnum
149   } );
150   print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
151         $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
152 }
153 @referrals = qsearch( 'part_referral', {} );
154 unless ( scalar(@referrals) == 1 ) {
155   my $referral = qsearchs('part_referral', {
156     'refnum' => $cust_main->refnum
157   } );
158   print '<TR><TD ALIGN="right">Referral</TD><TD BGCOLOR="#ffffff">',
159         $referral->refnum, ": ", $referral->referral, '</TD></TR>';
160 }
161 print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
162   $cust_main->otaker, '</TD></TR>';
163
164 print '</TABLE></TD></TR></TABLE>';
165
166 print '</TD><TD ROWSPAN=2>';
167
168 print "Contact information", &ntable("#c0c0c0"), "<TR><TD>",
169       &ntable("#c0c0c0",2),
170   '<TR><TD ALIGN="right">Contact name<BR>(last, first)</TD>',
171     '<TD COLSPAN=3 BGCOLOR="#ffffff">',
172     $cust_main->last, ', ', $cust_main->first,
173     '</TD><TD ALIGN="right">SS#</TD><TD BGCOLOR="#ffffff">',
174     $cust_main->ss || '&nbsp', '</TD></TR>',
175   '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
176     $cust_main->company,
177     '</TD></TR>',
178   '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
179     $cust_main->address1,
180     '</TD></TR>',
181 ;
182 print '<TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
183       $cust_main->address2, '</TD></TR>'
184   if $cust_main->address2;
185 print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
186         $cust_main->city,
187         '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
188         $cust_main->state,
189         '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
190         $cust_main->zip, '</TD></TR>',
191       '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
192         $cust_main->country,
193         '</TD></TR>',
194 ;
195 print '<TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
196         $cust_main->daytime || '&nbsp', '</TD></TR>',
197       '<TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
198         $cust_main->night || '&nbsp', '</TD></TR>',
199       '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
200         $cust_main->fax || '&nbsp', '</TD></TR>',
201       '</TABLE>', "</TD></TR></TABLE>"
202 ;
203
204 print '</TD></TR><TR><TD>';
205
206 @invoicing_list = $cust_main->invoicing_list;
207 print "Billing information (",
208       qq!<A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!, "Bill now</A>)",
209       &ntable("#c0c0c0"), "<TR><TD>", &ntable("#c0c0c0",2),
210       '<TR><TD ALIGN="right">Tax exempt</TD><TD BGCOLOR="#ffffff">',
211       $cust_main->tax ? 'yes' : 'no',
212       '</TD></TR>',
213       '<TR><TD ALIGN="right">Postal invoices</TD><TD BGCOLOR="#ffffff">',
214       ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
215       '</TD></TR>',
216       '<TR><TD ALIGN="right">Email invoices</TD><TD BGCOLOR="#ffffff">',
217       join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
218       '</TD></TR>',
219       '<TR><TD ALIGN="right">Billing type</TD><TD BGCOLOR="#ffffff">',
220 ;
221
222 if ( $cust_main->payby eq 'CARD' ) {
223   print 'Credit card</TD></TR>',
224         '<TR><TD ALIGN="right">Card number</TD><TD BGCOLOR="#ffffff">',
225         $cust_main->payinfo, '</TD></TR>',
226         '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
227         $cust_main->paydate, '</TD></TR>',
228         '<TR><TD ALIGN="right">Name on card</TD><TD BGCOLOR="#ffffff">',
229         $cust_main->payname, '</TD></TR>'
230   ;
231 } elsif ( $cust_main->payby eq 'BILL' ) {
232   print 'Billing</TD></TR>';
233   print '<TR><TD ALIGN="right">P.O. </TD><TD BGCOLOR="#ffffff">',
234         $cust_main->payinfo, '</TD></TR>',
235     if $cust_main->payinfo;
236   print '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
237         $cust_main->paydate, '</TD></TR>',
238         '<TR><TD ALIGN="right">Attention</TD><TD BGCOLOR="#ffffff">',
239         $cust_main->payname, '</TD></TR>',
240   ;
241 } elsif ( $cust_main->payby eq 'COMP' ) {
242   print 'Complimentary</TD></TR>',
243         '<TR><TD ALIGN="right">Authorized by</TD><TD BGCOLOR="#ffffff">',
244         $cust_main->payinfo, '</TD></TR>',
245         '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
246         $cust_main->paydate, '</TD></TR>',
247   ;
248 }
249
250 print "</TABLE></TD></TR></TABLE></TD></TR></TABLE>";
251
252 print qq!<BR><BR><A NAME="cust_pkg">Packages</A> !,
253 #      qq!<BR>Click on package number to view/edit package.!,
254       qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> (preserves services) )!,
255 ;
256
257 #display packages
258
259 #formatting
260 print qq!!, &table(), "\n",
261       qq!<TR><TH COLSPAN=2 ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
262       qq!Dates</TH><TH COLSPAN=2 ROWSPAN=2>Services</TH></TR>\n!,
263       qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
264       qq!<FONT SIZE=-1>Next bill</FONT>!,
265       qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
266       qq!</FONT></TH>!,
267       qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
268       qq!</TR>\n!;
269
270 #get package info
271 if ( $conf->exists('hidecancelledpackages') ) {
272   @packages = $cust_main->ncancelled_pkgs;
273 } else {
274   @packages = $cust_main->all_pkgs;
275 }
276
277 $n1 = '<TR>';
278 foreach $package (@packages) {
279   my $pkgnum = $package->pkgnum;
280   my $pkg = $package->part_pkg->pkg;
281   my $comment = $package->part_pkg->comment;
282   my $pkgview = popurl(2). "view/cust_pkg.cgi?$pkgnum";
283   my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
284   my $rowspan = scalar(@cust_svc) || 1;
285
286   my $button_cgi = new CGI;
287   $button_cgi->param('clone', $package->part_pkg->pkgpart);
288   $button_cgi->param('pkgnum', $package->pkgnum);
289   my $button_url = popurl(2). "edit/part_pkg.cgi?". $button_cgi->query_string;
290
291   #print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview">$pkgnum</A></TD>!,
292   print $n1, qq!<TD ROWSPAN=$rowspan>$pkgnum</TD>!,
293         qq!<TD ROWSPAN=$rowspan><FONT SIZE=-1>!,
294         #qq!<A HREF="$pkgview">$pkg - $comment</A>!,
295         qq!$pkg - $comment!,
296         qq! ( <A HREF="$pkgview">Edit</A> | <A HREF="$button_url">Customize pricing</A> )</FONT></TD>!,
297   ;
298   for ( qw( setup bill susp expire cancel ) ) {
299     print "<TD ROWSPAN=$rowspan><FONT SIZE=-1>", ( $package->getfield($_)
300             ? time2str("%D", $package->getfield($_) )
301             :  '&nbsp'
302           ), '</FONT></TD>',
303     ;
304   }
305
306   my $n2 = '';
307   foreach my $cust_svc ( @cust_svc ) {
308      my($label, $value, $svcdb) = $cust_svc->label;
309      my($svcnum) = $cust_svc->svcnum;
310      my($sview) = popurl(2). "view";
311      print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
312            qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
313      $n2="</TR><TR>";
314   }
315   $n1="</TR><TR>";
316 }  
317 print "</TR>";
318
319 #formatting
320 print "</TABLE>";
321
322 #formatting
323 print qq!<BR><BR><A NAME="history">Payment History!,
324       qq!</A>!,
325       qq! ( Click on invoice to view invoice/enter payment. | !,
326       qq!<A HREF="!, popurl(2), qq!edit/cust_credit.cgi?$custnum">!,
327       qq!Post credit / refund</A> )!;
328
329 #get payment history
330 #
331 # major problem: this whole thing is way too sloppy.
332 # minor problem: the description lines need better formatting.
333
334 @history = (); #needed for mod_perl :)
335
336 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
337 foreach $bill (@bills) {
338   my($bref)=$bill->hashref;
339   push @history,
340     $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!view/cust_bill.cgi?! .
341     $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
342     qq! (Balance \$! . $bill->owed . qq!)</A>\t! .
343     $bref->{charged} . qq!\t\t\t!;
344
345   my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
346   my($payment);
347   foreach $payment (@payments) {
348     my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
349                                              $payment->getfield('invnum'),
350                                              $payment->getfield('payby'),
351                                              $payment->getfield('payinfo'),
352                                              $payment->getfield('paid'),
353                       );
354     push @history,
355       "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
356   }
357 }
358
359 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
360 foreach $credit (@credits) {
361   my($cref)=$credit->hashref;
362   push @history,
363     $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
364     $cref->{credited} . ") by " . $cref->{otaker} . " - " .
365     $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
366
367   my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
368   my($refund);
369   foreach $refund (@refunds) {
370     my($rref)=$refund->hashref;
371     push @history,
372       $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
373       $rref->{payby} . " " . $rref->{payinfo} . ") by " .
374       $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
375       $rref->{refund};
376   }
377 }
378
379         #formatting
380         print &table(), <<END;
381 <TR>
382   <TH>Date</TH>
383   <TH>Description</TH>
384   <TH><FONT SIZE=-1>Charge</FONT></TH>
385   <TH><FONT SIZE=-1>Payment</FONT></TH>
386   <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
387   <TH><FONT SIZE=-1>Refund</FONT></TH>
388   <TH><FONT SIZE=-1>Balance</FONT></TH>
389 </TR>
390 END
391
392 #display payment history
393
394 $balance = 0;
395 foreach $item (sort keyfield_numerically @history) {
396   my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
397   $charge ||= 0;
398   $payment ||= 0;
399   $credit ||= 0;
400   $refund ||= 0;
401   $balance += $charge - $payment;
402   $balance -= $credit - $refund;
403
404   print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
405         "<TD><FONT SIZE=-1>$desc</FONT></TD>",
406         "<TD><FONT SIZE=-1>",
407         ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
408         "</FONT></TD>",
409         "<TD><FONT SIZE=-1>",
410         ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
411         "</FONT></TD>",
412         "<TD><FONT SIZE=-1>",
413         ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
414         "</FONT></TD>",
415         "<TD><FONT SIZE=-1>",
416         ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
417         "</FONT></TD>",
418         "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
419         "</FONT></TD>",
420         "\n";
421 }
422
423 #formatting
424 print "</TABLE>";
425
426 #end
427
428 #formatting
429 print <<END;
430
431   </BODY>
432 </HTML>
433 END
434
435 #subroutiens
436 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }
437