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