change configuration file layout to support multiple distinct databases (with
[freeside.git] / htdocs / view / cust_main.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # cust_main.cgi: View a customer
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 use strict;
36 use CGI::Base qw(:DEFAULT :CGI); # CGI module
37 use CGI::Carp qw(fatalsToBrowser);
38 use Date::Format;
39 use FS::UID qw(cgisuidsetup);
40 use FS::Record qw(qsearchs qsearch);
41 use FS::CGI qw(header menubar);
42
43 my($cgi) = new CGI::Base;
44 $cgi->get;
45 &cgisuidsetup($cgi);
46
47 SendHeaders(); # one guess.
48 print header("Customer View", menubar(
49   'Main Menu' => '../',
50 )),<<END;
51     <BASEFONT SIZE=3>
52 END
53
54 #untaint custnum & get customer record
55 $QUERY_STRING =~ /^(\d+)$/;
56 my($custnum)=$1;
57 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
58 die "Customer not found!" unless $cust_main;
59 my($hashref)=$cust_main->hashref;
60
61 #custnum
62 print "<FONT SIZE=+1><CENTER>Customer #<B>$custnum</B></CENTER></FONT>",
63       qq!<CENTER><A HREF="#cust_main">Customer Information</A> | !,
64       qq!<A HREF="#cust_comments">Comments</A> | !,
65       qq!<A HREF="#cust_pkg">Packages</A> | !,
66       qq!<A HREF="#history">Payment History</A> </CENTER>!;
67
68 #bill now linke
69 print qq!<HR><CENTER><A HREF="../misc/bill.cgi?$custnum">!,
70       qq!Bill this customer now</A></CENTER>!;
71
72 #formatting
73 print qq!<HR><A NAME="cust_main"><CENTER><FONT SIZE=+1>Customer Information!,
74       qq!</FONT>!,
75       qq!<BR><A HREF="../edit/cust_main.cgi?$custnum!,
76       qq!">Edit this information</A></CENTER><FONT SIZE=-1>!;
77
78 #agentnum
79 my($agent)=qsearchs('agent',{
80   'agentnum' => $cust_main->getfield('agentnum')
81 } );
82 die "Agent not found!" unless $agent;
83 print "<BR>Agent #<B>" , $agent->getfield('agentnum') , ": " ,
84                          $agent->getfield('agent') , "</B>";
85
86 #refnum
87 my($referral)=qsearchs('part_referral',{'refnum' => $cust_main->refnum});
88 die "Referral not found!" unless $referral;
89 print "<BR>Referral #<B>", $referral->refnum, ": ",
90       $referral->referral, "<\B>"; 
91
92 #last, first
93 print "<P><B>", $hashref->{'last'}, ", ", $hashref->{first}, "</B>";
94
95 #ss
96 print " (SS# <B>", $hashref->{ss}, "</B>)" if $hashref->{ss};
97
98 #company
99 print "<BR><B>", $hashref->{company}, "</B>" if $hashref->{company};
100
101 #address1
102 print "<BR><B>", $hashref->{address1}, "</B>";
103
104 #address2
105 print "<BR><B>", $hashref->{address2}, "</B>" if $hashref->{address2};
106
107 #city
108 print "<BR><B>", $hashref->{city}, "</B>";
109
110 #county
111 print " (<B>", $hashref->{county}, "</B> county)" if $hashref->{county};
112
113 #state
114 print ",<B>", $hashref->{state}, "</B>";
115
116 #zip
117 print "  <B>", $hashref->{zip}, "</B>";
118
119 #country
120 print "<BR><B>", $hashref->{country}, "</B>"
121   unless $hashref->{country} eq "US";
122
123 #daytime
124 print "<P><B>", $hashref->{daytime}, "</B>" if $hashref->{daytime};
125 print " (Day)" if $hashref->{daytime} && $hashref->{night};
126
127 #night
128 print "<BR><B>", $hashref->{night}, "</B>" if $hashref->{night};
129 print " (Night)" if $hashref->{daytime} && $hashref->{night};
130
131 #fax
132 print "<BR><B>", $hashref->{fax}, "</B> (Fax)" if $hashref->{fax};
133
134 #payby/payinfo/paydate/payname
135 if ($hashref->{payby} eq "CARD") {
136   print "<P>Card #<B>", $hashref->{payinfo}, "</B> Exp. <B>",
137     $hashref->{paydate}, "</B>";
138   print " (<B>", $hashref->{payname}, "</B>)" if $hashref->{payname};
139 } elsif ($hashref->{payby} eq "BILL") {
140   print "<P>Bill";
141   print " on P.O. #<B>", $hashref->{payinfo}, "</B>"
142     if $hashref->{payinfo};
143   print " until <B>", $hashref->{paydate}, "</B>"
144     if $hashref->{paydate};
145   print " to <B>", $hashref->{payname}, "</B> at above address"
146     if $hashref->{payname};
147 } elsif ($hashref->{payby} eq "COMP") {
148   print "<P>Access complimentary";
149   print " courtesy of <B>", $hashref->{payinfo}, "</B>"
150     if $hashref->{payinfo};
151   print " until <B>", $hashref->{paydate}, "</B>"
152     if $hashref->{paydate};
153 } else {
154   print "Unknown payment type ", $hashref->{payby}, "!";
155 }
156
157 #tax
158 print "<BR>(Tax exempt)" if $hashref->{tax};
159
160 #otaker
161 print "<P>Order taken by <B>", $hashref->{otaker}, "</B>";
162
163 #formatting     
164 print qq!<HR><FONT SIZE=+1><A NAME="cust_pkg"><CENTER>Packages</A></FONT>!,
165       qq!<BR>Click on package number to view/edit package.!,
166       qq!<BR><A HREF="../edit/cust_pkg.cgi?$custnum">Add/Edit packages</A>!,
167       qq!</CENTER><BR>!;
168
169 #display packages
170
171 #formatting
172 print qq!<CENTER><TABLE BORDER=4>\n!,
173       qq!<TR><TH ROWSPAN=2>#</TH><TH ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
174       qq!Dates</TH></TR>\n!,
175       qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
176       qq!<FONT SIZE=-1>Next bill</FONT>!,
177       qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
178       qq!</FONT></TH>!,
179       qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
180       qq!</TR>\n!;
181
182 #get package info
183 my(@packages)=qsearch('cust_pkg',{'custnum'=>$custnum});
184 my($package);
185 foreach $package (@packages) {
186   my($pref)=$package->hashref;
187   my($part_pkg)=qsearchs('part_pkg',{
188     'pkgpart' => $pref->{pkgpart}
189   } );
190   print qq!<TR><TD><FONT SIZE=-1><A HREF="../view/cust_pkg.cgi?!,
191         $pref->{pkgnum}, qq!">!, 
192         $pref->{pkgnum}, qq!</A></FONT></TD>!,
193         "<TD><FONT SIZE=-1>", $part_pkg->getfield('pkg'), " - ",
194         $part_pkg->getfield('comment'), "</FONT></TD>",
195         "<TD><FONT SIZE=-1>", 
196         $pref->{setup} ? time2str("%D",$pref->{setup} ) : "" ,
197         "</FONT></TD>",
198         "<TD><FONT SIZE=-1>", 
199         $pref->{bill} ? time2str("%D",$pref->{bill} ) : "" ,
200         "</FONT></TD>",
201         "<TD><FONT SIZE=-1>",
202         $pref->{susp} ? time2str("%D",$pref->{susp} ) : "" ,
203         "</FONT></TD>",
204         "<TD><FONT SIZE=-1>",
205         $pref->{expire} ? time2str("%D",$pref->{expire} ) : "" ,
206         "</FONT></TD>",
207         "<TD><FONT SIZE=-1>",
208         $pref->{cancel} ? time2str("%D",$pref->{cancel} ) : "" ,
209         "</FONT></TD>",
210         "</TR>";
211 }
212
213 #formatting
214 print "</TABLE></CENTER>";
215
216 #formatting
217 print qq!<CENTER><HR><A NAME="history"><FONT SIZE=+1>Payment History!,
218       qq!</FONT></A><BR>!,
219       qq!Click on invoice to view invoice/enter payment.<BR>!,
220       qq!<A HREF="../edit/cust_credit.cgi?$custnum">!,
221       qq!Post Credit / Refund</A></CENTER><BR>!;
222
223 #get payment history
224 #
225 # major problem: this whole thing is way too sloppy.
226 # minor problem: the description lines need better formatting.
227
228 my(@history);
229
230 my(@bills)=qsearch('cust_bill',{'custnum'=>$custnum});
231 my($bill);
232 foreach $bill (@bills) {
233   my($bref)=$bill->hashref;
234   push @history,
235     $bref->{_date} . qq!\t<A HREF="../view/cust_bill.cgi?! .
236     $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
237     qq! (Balance \$! . $bref->{owed} . qq!)</A>\t! .
238     $bref->{charged} . qq!\t\t\t!;
239
240   my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
241   my($payment);
242   foreach $payment (@payments) {
243 #    my($pref)=$payment->hashref;
244     my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
245                                              $payment->getfield('invnum'),
246                                              $payment->getfield('payby'),
247                                              $payment->getfield('payinfo'),
248                                              $payment->getfield('paid'),
249                       );
250     push @history,
251       "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
252   }
253 }
254
255 my(@credits)=qsearch('cust_credit',{'custnum'=>$custnum});
256 my($credit);
257 foreach $credit (@credits) {
258   my($cref)=$credit->hashref;
259   push @history,
260     $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
261     $cref->{credited} . ") by " . $cref->{otaker} . " - " .
262     $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
263
264   my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
265   my($refund);
266   foreach $refund (@refunds) {
267     my($rref)=$refund->hashref;
268     push @history,
269       $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
270       $rref->{payby} . " " . $rref->{payinfo} . ") by " .
271       $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
272       $rref->{refund};
273   }
274 }
275
276         #formatting
277         print <<END;
278 <CENTER><TABLE BORDER=4>
279 <TR>
280   <TH>Date</TH>
281   <TH>Description</TH>
282   <TH><FONT SIZE=-1>Charge</FONT></TH>
283   <TH><FONT SIZE=-1>Payment</FONT></TH>
284   <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
285   <TH><FONT SIZE=-1>Refund</FONT></TH>
286   <TH><FONT SIZE=-1>Balance</FONT></TH>
287 </TR>
288 END
289
290 #display payment history
291
292 my($balance)=0;
293 my($item);
294 foreach $item (sort keyfield_numerically @history) {
295   my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
296   $charge ||= 0;
297   $payment ||= 0;
298   $credit ||= 0;
299   $refund ||= 0;
300   $balance += $charge - $payment;
301   $balance -= $credit - $refund;
302
303   print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
304         "<TD><FONT SIZE=-1>$desc</FONT></TD>",
305         "<TD><FONT SIZE=-1>",
306         ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
307         "</FONT></TD>",
308         "<TD><FONT SIZE=-1>",
309         ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
310         "</FONT></TD>",
311         "<TD><FONT SIZE=-1>",
312         ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
313         "</FONT></TD>",
314         "<TD><FONT SIZE=-1>",
315         ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
316         "</FONT></TD>",
317         "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
318         "</FONT></TD>",
319         "\n";
320 }
321
322 #formatting
323 print "</TABLE></CENTER>";
324
325 #end
326
327 #formatting
328 print <<END;
329
330   </BODY>
331 </HTML>
332 END
333
334 #subroutiens
335 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }
336