3 # cust_main.cgi: View a customer
5 # Usage: cust_main.cgi custnum
6 # http://server.name/path/cust_main.cgi?custnum
8 # Note: Should be run setuid freeside as user nobody.
10 # the payment history section could use some work, see below
12 # ivan@voicenet.com 96-nov-29 -> 96-dec-11
14 # added navigation bar (go to main menu ;)
15 # ivan@voicenet.com 97-jan-30
17 # changes to the way credits/payments are applied (the links are here).
18 # ivan@voicenet.com 97-apr-21
20 # added debugging code to diagnose CPU sucking problem.
21 # ivan@voicenet.com 97-may-19
23 # CPU sucking problem was in comment code? fixed?
24 # ivan@voicenet.com 97-may-22
27 # ivan@voicenet.com 97-jul-22
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
33 # lose background, FS::CGI ivan@sisd.com 98-sep-2
36 use CGI::Base qw(:DEFAULT :CGI); # CGI module
37 use CGI::Carp qw(fatalsToBrowser);
39 use FS::UID qw(cgisuidsetup);
40 use FS::Record qw(qsearchs qsearch);
41 use FS::CGI qw(header menubar);
43 my($cgi) = new CGI::Base;
47 SendHeaders(); # one guess.
48 print header("Customer View", menubar(
54 #untaint custnum & get customer record
55 $QUERY_STRING =~ /^(\d+)$/;
57 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
58 die "Customer not found!" unless $cust_main;
59 my($hashref)=$cust_main->hashref;
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>!;
69 print qq!<HR><CENTER><A HREF="../misc/bill.cgi?$custnum">!,
70 qq!Bill this customer now</A></CENTER>!;
73 print qq!<HR><A NAME="cust_main"><CENTER><FONT SIZE=+1>Customer Information!,
75 qq!<BR><A HREF="../edit/cust_main.cgi?$custnum!,
76 qq!">Edit this information</A></CENTER><FONT SIZE=-1>!;
79 my($agent)=qsearchs('agent',{
80 'agentnum' => $cust_main->getfield('agentnum')
82 die "Agent not found!" unless $agent;
83 print "<BR>Agent #<B>" , $agent->getfield('agentnum') , ": " ,
84 $agent->getfield('agent') , "</B>";
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>";
93 print "<P><B>", $hashref->{'last'}, ", ", $hashref->{first}, "</B>";
96 print " (SS# <B>", $hashref->{ss}, "</B>)" if $hashref->{ss};
99 print "<BR><B>", $hashref->{company}, "</B>" if $hashref->{company};
102 print "<BR><B>", $hashref->{address1}, "</B>";
105 print "<BR><B>", $hashref->{address2}, "</B>" if $hashref->{address2};
108 print "<BR><B>", $hashref->{city}, "</B>";
111 print " (<B>", $hashref->{county}, "</B> county)" if $hashref->{county};
114 print ",<B>", $hashref->{state}, "</B>";
117 print " <B>", $hashref->{zip}, "</B>";
120 print "<BR><B>", $hashref->{country}, "</B>"
121 unless $hashref->{country} eq "US";
124 print "<P><B>", $hashref->{daytime}, "</B>" if $hashref->{daytime};
125 print " (Day)" if $hashref->{daytime} && $hashref->{night};
128 print "<BR><B>", $hashref->{night}, "</B>" if $hashref->{night};
129 print " (Night)" if $hashref->{daytime} && $hashref->{night};
132 print "<BR><B>", $hashref->{fax}, "</B> (Fax)" if $hashref->{fax};
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") {
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};
154 print "Unknown payment type ", $hashref->{payby}, "!";
158 print "<BR>(Tax exempt)" if $hashref->{tax};
161 print "<P>Order taken by <B>", $hashref->{otaker}, "</B>";
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>!,
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!,
179 qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
183 my(@packages)=qsearch('cust_pkg',{'custnum'=>$custnum});
185 foreach $package (@packages) {
186 my($pref)=$package->hashref;
187 my($part_pkg)=qsearchs('part_pkg',{
188 'pkgpart' => $pref->{pkgpart}
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} ) : "" ,
198 "<TD><FONT SIZE=-1>",
199 $pref->{bill} ? time2str("%D",$pref->{bill} ) : "" ,
201 "<TD><FONT SIZE=-1>",
202 $pref->{susp} ? time2str("%D",$pref->{susp} ) : "" ,
204 "<TD><FONT SIZE=-1>",
205 $pref->{expire} ? time2str("%D",$pref->{expire} ) : "" ,
207 "<TD><FONT SIZE=-1>",
208 $pref->{cancel} ? time2str("%D",$pref->{cancel} ) : "" ,
214 print "</TABLE></CENTER>";
217 print qq!<CENTER><HR><A NAME="history"><FONT SIZE=+1>Payment History!,
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>!;
225 # major problem: this whole thing is way too sloppy.
226 # minor problem: the description lines need better formatting.
230 my(@bills)=qsearch('cust_bill',{'custnum'=>$custnum});
232 foreach $bill (@bills) {
233 my($bref)=$bill->hashref;
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!;
240 my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
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'),
251 "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
255 my(@credits)=qsearch('cust_credit',{'custnum'=>$custnum});
257 foreach $credit (@credits) {
258 my($cref)=$credit->hashref;
260 $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
261 $cref->{credited} . ") by " . $cref->{otaker} . " - " .
262 $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
264 my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
266 foreach $refund (@refunds) {
267 my($rref)=$refund->hashref;
269 $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
270 $rref->{payby} . " " . $rref->{payinfo} . ") by " .
271 $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
278 <CENTER><TABLE BORDER=4>
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>
290 #display payment history
294 foreach $item (sort keyfield_numerically @history) {
295 my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
300 $balance += $charge - $payment;
301 $balance -= $credit - $refund;
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) : '' ),
308 "<TD><FONT SIZE=-1>",
309 ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
311 "<TD><FONT SIZE=-1>",
312 ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
314 "<TD><FONT SIZE=-1>",
315 ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
317 "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
323 print "</TABLE></CENTER>";
335 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }