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
35 # $Log: cust_main.cgi,v $
36 # Revision 1.2 1998-11-13 11:28:08 ivan
37 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
42 use CGI::Carp qw(fatalsToBrowser);
44 use FS::UID qw(cgisuidsetup);
45 use FS::Record qw(qsearchs qsearch);
46 use FS::CGI qw(header menubar popurl table);
52 use FS::part_referral;
59 print $cgi->header, header("Customer View", menubar(
60 'Main Menu' => popurl(2)
65 die "No customer specified (bad URL)!" unless $cgi->keywords;
66 my($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
69 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
70 die "Customer not found!" unless $cust_main;
71 my($hashref)=$cust_main->hashref;
74 print "<FONT SIZE=+1><CENTER>Customer #<B>$custnum</B></CENTER></FONT>",
75 qq!<CENTER><A HREF="#cust_main">Customer Information</A> | !,
76 qq!<A HREF="#cust_comments">Comments</A> | !,
77 qq!<A HREF="#cust_pkg">Packages</A> | !,
78 qq!<A HREF="#history">Payment History</A> </CENTER>!;
81 print qq!<HR><CENTER><A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!,
82 qq!Bill this customer now</A></CENTER>!;
85 print qq!<HR><A NAME="cust_main"><CENTER><FONT SIZE=+1>Customer Information!,
87 qq!<BR><A HREF="!, popurl(2), qq!/edit/cust_main.cgi?$custnum!,
88 qq!">Edit this information</A></CENTER><FONT SIZE=-1>!;
91 my($agent)=qsearchs('agent',{
92 'agentnum' => $cust_main->getfield('agentnum')
94 die "Agent not found!" unless $agent;
95 print "<BR>Agent #<B>" , $agent->getfield('agentnum') , ": " ,
96 $agent->getfield('agent') , "</B>";
99 my($referral)=qsearchs('part_referral',{'refnum' => $cust_main->refnum});
100 die "Referral not found!" unless $referral;
101 print "<BR>Referral #<B>", $referral->refnum, ": ",
102 $referral->referral, "<\B>";
105 print "<P><B>", $hashref->{'last'}, ", ", $hashref->{first}, "</B>";
108 print " (SS# <B>", $hashref->{ss}, "</B>)" if $hashref->{ss};
111 print "<BR><B>", $hashref->{company}, "</B>" if $hashref->{company};
114 print "<BR><B>", $hashref->{address1}, "</B>";
117 print "<BR><B>", $hashref->{address2}, "</B>" if $hashref->{address2};
120 print "<BR><B>", $hashref->{city}, "</B>";
123 print " (<B>", $hashref->{county}, "</B> county)" if $hashref->{county};
126 print ",<B>", $hashref->{state}, "</B>";
129 print " <B>", $hashref->{zip}, "</B>";
132 print "<BR><B>", $hashref->{country}, "</B>"
133 unless $hashref->{country} eq "US";
136 print "<P><B>", $hashref->{daytime}, "</B>" if $hashref->{daytime};
137 print " (Day)" if $hashref->{daytime} && $hashref->{night};
140 print "<BR><B>", $hashref->{night}, "</B>" if $hashref->{night};
141 print " (Night)" if $hashref->{daytime} && $hashref->{night};
144 print "<BR><B>", $hashref->{fax}, "</B> (Fax)" if $hashref->{fax};
146 #payby/payinfo/paydate/payname
147 if ($hashref->{payby} eq "CARD") {
148 print "<P>Card #<B>", $hashref->{payinfo}, "</B> Exp. <B>",
149 $hashref->{paydate}, "</B>";
150 print " (<B>", $hashref->{payname}, "</B>)" if $hashref->{payname};
151 } elsif ($hashref->{payby} eq "BILL") {
153 print " on P.O. #<B>", $hashref->{payinfo}, "</B>"
154 if $hashref->{payinfo};
155 print " until <B>", $hashref->{paydate}, "</B>"
156 if $hashref->{paydate};
157 print " to <B>", $hashref->{payname}, "</B> at above address"
158 if $hashref->{payname};
159 } elsif ($hashref->{payby} eq "COMP") {
160 print "<P>Access complimentary";
161 print " courtesy of <B>", $hashref->{payinfo}, "</B>"
162 if $hashref->{payinfo};
163 print " until <B>", $hashref->{paydate}, "</B>"
164 if $hashref->{paydate};
166 print "Unknown payment type ", $hashref->{payby}, "!";
170 print "<BR>(Tax exempt)" if $hashref->{tax};
173 print "<P>Order taken by <B>", $hashref->{otaker}, "</B>";
176 print qq!<HR><FONT SIZE=+1><A NAME="cust_pkg"><CENTER>Packages</A></FONT>!,
177 qq!<BR>Click on package number to view/edit package.!,
178 qq!<BR><A HREF="!, popurl(2), qq!/edit/cust_pkg.cgi?$custnum">Add/Edit packages</A>!,
184 print qq!<CENTER>!, table, "\n",
185 qq!<TR><TH ROWSPAN=2>#</TH><TH ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
186 qq!Dates</TH></TR>\n!,
187 qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
188 qq!<FONT SIZE=-1>Next bill</FONT>!,
189 qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
191 qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
195 my(@packages)=qsearch('cust_pkg',{'custnum'=>$custnum});
197 foreach $package (@packages) {
198 my($pref)=$package->hashref;
199 my($part_pkg)=qsearchs('part_pkg',{
200 'pkgpart' => $pref->{pkgpart}
202 print qq!<TR><TD><FONT SIZE=-1><A HREF="!, popurl(2), qq!/view/cust_pkg.cgi?!,
203 $pref->{pkgnum}, qq!">!,
204 $pref->{pkgnum}, qq!</A></FONT></TD>!,
205 "<TD><FONT SIZE=-1>", $part_pkg->getfield('pkg'), " - ",
206 $part_pkg->getfield('comment'), "</FONT></TD>",
207 "<TD><FONT SIZE=-1>",
208 $pref->{setup} ? time2str("%D",$pref->{setup} ) : "" ,
210 "<TD><FONT SIZE=-1>",
211 $pref->{bill} ? time2str("%D",$pref->{bill} ) : "" ,
213 "<TD><FONT SIZE=-1>",
214 $pref->{susp} ? time2str("%D",$pref->{susp} ) : "" ,
216 "<TD><FONT SIZE=-1>",
217 $pref->{expire} ? time2str("%D",$pref->{expire} ) : "" ,
219 "<TD><FONT SIZE=-1>",
220 $pref->{cancel} ? time2str("%D",$pref->{cancel} ) : "" ,
226 print "</TABLE></CENTER>";
229 print qq!<CENTER><HR><A NAME="history"><FONT SIZE=+1>Payment History!,
231 qq!Click on invoice to view invoice/enter payment.<BR>!,
232 qq!<A HREF="!, popurl(2), qq!/edit/cust_credit.cgi?$custnum">!,
233 qq!Post Credit / Refund</A></CENTER><BR>!;
237 # major problem: this whole thing is way too sloppy.
238 # minor problem: the description lines need better formatting.
242 my(@bills)=qsearch('cust_bill',{'custnum'=>$custnum});
244 foreach $bill (@bills) {
245 my($bref)=$bill->hashref;
247 $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!/view/cust_bill.cgi?! .
248 $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
249 qq! (Balance \$! . $bref->{owed} . qq!)</A>\t! .
250 $bref->{charged} . qq!\t\t\t!;
252 my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
254 foreach $payment (@payments) {
255 # my($pref)=$payment->hashref;
256 my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
257 $payment->getfield('invnum'),
258 $payment->getfield('payby'),
259 $payment->getfield('payinfo'),
260 $payment->getfield('paid'),
263 "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
267 my(@credits)=qsearch('cust_credit',{'custnum'=>$custnum});
269 foreach $credit (@credits) {
270 my($cref)=$credit->hashref;
272 $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
273 $cref->{credited} . ") by " . $cref->{otaker} . " - " .
274 $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
276 my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
278 foreach $refund (@refunds) {
279 my($rref)=$refund->hashref;
281 $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
282 $rref->{payby} . " " . $rref->{payinfo} . ") by " .
283 $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
289 print "<CENTER>", table, <<END;
293 <TH><FONT SIZE=-1>Charge</FONT></TH>
294 <TH><FONT SIZE=-1>Payment</FONT></TH>
295 <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
296 <TH><FONT SIZE=-1>Refund</FONT></TH>
297 <TH><FONT SIZE=-1>Balance</FONT></TH>
301 #display payment history
305 foreach $item (sort keyfield_numerically @history) {
306 my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
311 $balance += $charge - $payment;
312 $balance -= $credit - $refund;
314 print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
315 "<TD><FONT SIZE=-1>$desc</FONT></TD>",
316 "<TD><FONT SIZE=-1>",
317 ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
319 "<TD><FONT SIZE=-1>",
320 ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
322 "<TD><FONT SIZE=-1>",
323 ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
325 "<TD><FONT SIZE=-1>",
326 ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
328 "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
334 print "</TABLE></CENTER>";
346 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }