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