all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
[freeside.git] / htdocs / view / cust_main.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: cust_main.cgi,v 1.9 1999-01-18 09:41:43 ivan Exp $
4 #
5 # Usage: cust_main.cgi custnum
6 #        http://server.name/path/cust_main.cgi?custnum
7 #
8 # Note: Should be run setuid freeside as user nobody.
9 #
10 # the payment history section could use some work, see below
11
12 # ivan@voicenet.com 96-nov-29 -> 96-dec-11
13 #
14 # added navigation bar (go to main menu ;)
15 # ivan@voicenet.com 97-jan-30
16 #
17 # changes to the way credits/payments are applied (the links are here).
18 # ivan@voicenet.com 97-apr-21
19 #
20 # added debugging code to diagnose CPU sucking problem.
21 # ivan@voicenet.com 97-may-19
22 #
23 # CPU sucking problem was in comment code?  fixed?
24 # ivan@voicenet.com 97-may-22
25 #
26 # rewrote for new API
27 # ivan@voicenet.com 97-jul-22
28 #
29 # Changes to allow page to work at a relative position in server
30 # Changed 'day' to 'daytime' because Pg6.3 reserves the day word
31 #       bmccane@maxbaud.net     98-apr-3
32 #
33 # lose background, FS::CGI ivan@sisd.com 98-sep-2
34 #
35 # $Log: cust_main.cgi,v $
36 # Revision 1.9  1999-01-18 09:41:43  ivan
37 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
38 # (good idea anyway)
39 #
40 # Revision 1.8  1999/01/18 09:22:35  ivan
41 # changes to track email addresses for email invoicing
42 #
43 # Revision 1.7  1998/12/30 23:03:34  ivan
44 # bugfixes; fields isn't exported by derived classes
45 #
46 # Revision 1.6  1998/12/23 02:42:33  ivan
47 # remove double '/' in link urls
48 #
49 # Revision 1.5  1998/12/23 02:36:28  ivan
50 # use FS::cust_refund; to eliminate warning
51 #
52 # Revision 1.4  1998/12/17 09:57:21  ivan
53 # s/CGI::(Base|Request)/CGI.pm/;
54 #
55 # Revision 1.3  1998/11/15 13:14:20  ivan
56 # first pass as per-customer custom pricing
57 #
58 # Revision 1.2  1998/11/13 11:28:08  ivan
59 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
60 #
61
62 use strict;
63 use CGI;
64 use CGI::Carp qw(fatalsToBrowser);
65 use Date::Format;
66 use FS::UID qw(cgisuidsetup);
67 use FS::Record qw(qsearchs qsearch);
68 use FS::CGI qw(header menubar popurl table);
69 use FS::cust_credit;
70 use FS::cust_pay;
71 use FS::cust_bill;
72 use FS::part_pkg;
73 use FS::cust_pkg;
74 use FS::part_referral;
75 use FS::agent;
76 use FS::cust_main;
77 use FS::cust_refund;
78
79 my($cgi) = new CGI;
80 &cgisuidsetup($cgi);
81
82 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
83   'Main Menu' => popurl(2)
84 )),<<END;
85     <BASEFONT SIZE=3>
86 END
87
88 die "No customer specified (bad URL)!" unless $cgi->keywords;
89 my($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
90 $query =~ /^(\d+)$/;
91 my($custnum)=$1;
92 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
93 die "Customer not found!" unless $cust_main;
94 my($hashref)=$cust_main->hashref;
95
96 #custnum
97 print "<FONT SIZE=+1><CENTER>Customer #<B>$custnum</B></CENTER></FONT>",
98       qq!<CENTER><A HREF="#cust_main">Customer Information</A> | !,
99       qq!<A HREF="#cust_comments">Comments</A> | !,
100       qq!<A HREF="#cust_pkg">Packages</A> | !,
101       qq!<A HREF="#history">Payment History</A> </CENTER>!;
102
103 #bill now linke
104 print qq!<HR><CENTER><A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!,
105       qq!Bill this customer now</A></CENTER>!;
106
107 #formatting
108 print qq!<HR><A NAME="cust_main"><CENTER><FONT SIZE=+1>Customer Information!,
109       qq!</FONT>!,
110       qq!<BR><A HREF="!, popurl(2), qq!edit/cust_main.cgi?$custnum!,
111       qq!">Edit this information</A></CENTER><FONT SIZE=-1>!;
112
113 #agentnum
114 my($agent)=qsearchs('agent',{
115   'agentnum' => $cust_main->getfield('agentnum')
116 } );
117 die "Agent not found!" unless $agent;
118 print "<BR>Agent #<B>" , $agent->getfield('agentnum') , ": " ,
119                          $agent->getfield('agent') , "</B>";
120
121 #refnum
122 my($referral)=qsearchs('part_referral',{'refnum' => $cust_main->refnum});
123 die "Referral not found!" unless $referral;
124 print "<BR>Referral #<B>", $referral->refnum, ": ",
125       $referral->referral, "<\B>"; 
126
127 #last, first
128 print "<P><B>", $hashref->{'last'}, ", ", $hashref->{first}, "</B>";
129
130 #ss
131 print " (SS# <B>", $hashref->{ss}, "</B>)" if $hashref->{ss};
132
133 #company
134 print "<BR><B>", $hashref->{company}, "</B>" if $hashref->{company};
135
136 #address1
137 print "<BR><B>", $hashref->{address1}, "</B>";
138
139 #address2
140 print "<BR><B>", $hashref->{address2}, "</B>" if $hashref->{address2};
141
142 #city
143 print "<BR><B>", $hashref->{city}, "</B>";
144
145 #county
146 print " (<B>", $hashref->{county}, "</B> county)" if $hashref->{county};
147
148 #state
149 print ",<B>", $hashref->{state}, "</B>";
150
151 #zip
152 print "  <B>", $hashref->{zip}, "</B>";
153
154 #country
155 print "<BR><B>", $hashref->{country}, "</B>"
156   unless $hashref->{country} eq "US";
157
158 #daytime
159 print "<P><B>", $hashref->{daytime}, "</B>" if $hashref->{daytime};
160 print " (Day)" if $hashref->{daytime} && $hashref->{night};
161
162 #night
163 print "<BR><B>", $hashref->{night}, "</B>" if $hashref->{night};
164 print " (Night)" if $hashref->{daytime} && $hashref->{night};
165
166 #fax
167 print "<BR><B>", $hashref->{fax}, "</B> (Fax)" if $hashref->{fax};
168
169 #payby/payinfo/paydate/payname
170 if ($hashref->{payby} eq "CARD") {
171   print "<P>Card #<B>", $hashref->{payinfo}, "</B> Exp. <B>",
172     $hashref->{paydate}, "</B>";
173   print " (<B>", $hashref->{payname}, "</B>)" if $hashref->{payname};
174 } elsif ($hashref->{payby} eq "BILL") {
175   print "<P>Bill";
176   print " on P.O. #<B>", $hashref->{payinfo}, "</B>"
177     if $hashref->{payinfo};
178   print " until <B>", $hashref->{paydate}, "</B>"
179     if $hashref->{paydate};
180   print " to <B>", $hashref->{payname}, "</B> at above address"
181     if $hashref->{payname};
182 } elsif ($hashref->{payby} eq "COMP") {
183   print "<P>Access complimentary";
184   print " courtesy of <B>", $hashref->{payinfo}, "</B>"
185     if $hashref->{payinfo};
186   print " until <B>", $hashref->{paydate}, "</B>"
187     if $hashref->{paydate};
188 } else {
189   print "Unknown payment type ", $hashref->{payby}, "!";
190 }
191
192 #tax
193 print "<BR>(Tax exempt)" if $hashref->{tax};
194
195 #otaker
196 print "<P>Order taken by <B>", $hashref->{otaker}, "</B>";
197
198 #formatting     
199 print qq!<HR><FONT SIZE=+1><A NAME="cust_pkg"><CENTER>Packages</A></FONT>!,
200       qq!<BR>Click on package number to view/edit package.!,
201       qq!<BR><A HREF="!, popurl(2), qq!/edit/cust_pkg.cgi?$custnum">Add/Edit packages</A>!,
202       qq!</CENTER><BR>!;
203
204 #display packages
205
206 #formatting
207 print qq!<CENTER>!, table, "\n",
208       qq!<TR><TH ROWSPAN=2>#</TH><TH ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
209       qq!Dates</TH></TR>\n!,
210       qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
211       qq!<FONT SIZE=-1>Next bill</FONT>!,
212       qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
213       qq!</FONT></TH>!,
214       qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
215       qq!</TR>\n!;
216
217 #get package info
218 my(@packages)=qsearch('cust_pkg',{'custnum'=>$custnum});
219 my($package);
220 foreach $package (@packages) {
221   my($pref)=$package->hashref;
222   my($part_pkg)=qsearchs('part_pkg',{
223     'pkgpart' => $pref->{pkgpart}
224   } );
225   print qq!<TR><TD><FONT SIZE=-1><A HREF="!, popurl(2), qq!view/cust_pkg.cgi?!,
226         $pref->{pkgnum}, qq!">!, 
227         $pref->{pkgnum}, qq!</A></FONT></TD>!,
228         "<TD><FONT SIZE=-1>", $part_pkg->getfield('pkg'), " - ",
229         $part_pkg->getfield('comment'), 
230           qq!<FORM ACTION="!, popurl(2), qq!edit/part_pkg.cgi" METHOD=POST>!,
231           qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $part_pkg->pkgpart, qq!">!,
232           qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $package->pkgnum, qq!">!,
233           qq!<INPUT TYPE="submit" VALUE="Customize Pricing">!,
234           "</FORM></FONT></TD>",
235         "<TD><FONT SIZE=-1>", 
236         $pref->{setup} ? time2str("%D",$pref->{setup} ) : "" ,
237         "</FONT></TD>",
238         "<TD><FONT SIZE=-1>", 
239         $pref->{bill} ? time2str("%D",$pref->{bill} ) : "" ,
240         "</FONT></TD>",
241         "<TD><FONT SIZE=-1>",
242         $pref->{susp} ? time2str("%D",$pref->{susp} ) : "" ,
243         "</FONT></TD>",
244         "<TD><FONT SIZE=-1>",
245         $pref->{expire} ? time2str("%D",$pref->{expire} ) : "" ,
246         "</FONT></TD>",
247         "<TD><FONT SIZE=-1>",
248         $pref->{cancel} ? time2str("%D",$pref->{cancel} ) : "" ,
249         "</FONT></TD>",
250         "</TR>";
251 }
252
253 #formatting
254 print "</TABLE></CENTER>";
255
256 #formatting
257 print qq!<CENTER><HR><A NAME="history"><FONT SIZE=+1>Payment History!,
258       qq!</FONT></A><BR>!,
259       qq!Click on invoice to view invoice/enter payment.<BR>!,
260       qq!<A HREF="!, popurl(2), qq!edit/cust_credit.cgi?$custnum">!,
261       qq!Post Credit / Refund</A></CENTER><BR>!;
262
263 #get payment history
264 #
265 # major problem: this whole thing is way too sloppy.
266 # minor problem: the description lines need better formatting.
267
268 my(@history);
269
270 my(@bills)=qsearch('cust_bill',{'custnum'=>$custnum});
271 my($bill);
272 foreach $bill (@bills) {
273   my($bref)=$bill->hashref;
274   push @history,
275     $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!view/cust_bill.cgi?! .
276     $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
277     qq! (Balance \$! . $bref->{owed} . qq!)</A>\t! .
278     $bref->{charged} . qq!\t\t\t!;
279
280   my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
281   my($payment);
282   foreach $payment (@payments) {
283 #    my($pref)=$payment->hashref;
284     my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
285                                              $payment->getfield('invnum'),
286                                              $payment->getfield('payby'),
287                                              $payment->getfield('payinfo'),
288                                              $payment->getfield('paid'),
289                       );
290     push @history,
291       "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
292   }
293 }
294
295 my(@credits)=qsearch('cust_credit',{'custnum'=>$custnum});
296 my($credit);
297 foreach $credit (@credits) {
298   my($cref)=$credit->hashref;
299   push @history,
300     $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
301     $cref->{credited} . ") by " . $cref->{otaker} . " - " .
302     $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
303
304   my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
305   my($refund);
306   foreach $refund (@refunds) {
307     my($rref)=$refund->hashref;
308     push @history,
309       $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
310       $rref->{payby} . " " . $rref->{payinfo} . ") by " .
311       $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
312       $rref->{refund};
313   }
314 }
315
316         #formatting
317         print "<CENTER>", table, <<END;
318 <TR>
319   <TH>Date</TH>
320   <TH>Description</TH>
321   <TH><FONT SIZE=-1>Charge</FONT></TH>
322   <TH><FONT SIZE=-1>Payment</FONT></TH>
323   <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
324   <TH><FONT SIZE=-1>Refund</FONT></TH>
325   <TH><FONT SIZE=-1>Balance</FONT></TH>
326 </TR>
327 END
328
329 #display payment history
330
331 my($balance)=0;
332 my($item);
333 foreach $item (sort keyfield_numerically @history) {
334   my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
335   $charge ||= 0;
336   $payment ||= 0;
337   $credit ||= 0;
338   $refund ||= 0;
339   $balance += $charge - $payment;
340   $balance -= $credit - $refund;
341
342   print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
343         "<TD><FONT SIZE=-1>$desc</FONT></TD>",
344         "<TD><FONT SIZE=-1>",
345         ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
346         "</FONT></TD>",
347         "<TD><FONT SIZE=-1>",
348         ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
349         "</FONT></TD>",
350         "<TD><FONT SIZE=-1>",
351         ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
352         "</FONT></TD>",
353         "<TD><FONT SIZE=-1>",
354         ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
355         "</FONT></TD>",
356         "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
357         "</FONT></TD>",
358         "\n";
359 }
360
361 #formatting
362 print "</TABLE></CENTER>";
363
364 #end
365
366 #formatting
367 print <<END;
368
369   </BODY>
370 </HTML>
371 END
372
373 #subroutiens
374 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }
375