3 # $Id: cust_main.cgi,v 1.10 1999-01-19 05:14:19 ivan Exp $
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.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/;
40 # Revision 1.9 1999/01/18 09:41:43 ivan
41 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
44 # Revision 1.8 1999/01/18 09:22:35 ivan
45 # changes to track email addresses for email invoicing
47 # Revision 1.7 1998/12/30 23:03:34 ivan
48 # bugfixes; fields isn't exported by derived classes
50 # Revision 1.6 1998/12/23 02:42:33 ivan
51 # remove double '/' in link urls
53 # Revision 1.5 1998/12/23 02:36:28 ivan
54 # use FS::cust_refund; to eliminate warning
56 # Revision 1.4 1998/12/17 09:57:21 ivan
57 # s/CGI::(Base|Request)/CGI.pm/;
59 # Revision 1.3 1998/11/15 13:14:20 ivan
60 # first pass as per-customer custom pricing
62 # Revision 1.2 1998/11/13 11:28:08 ivan
63 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
67 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral
68 @packages $package @history @bills $bill @credits $credit
71 use CGI::Carp qw(fatalsToBrowser);
73 use FS::UID qw(cgisuidsetup);
74 use FS::Record qw(qsearchs qsearch);
75 use FS::CGI qw(header menubar popurl table);
81 use FS::part_referral;
89 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
90 'Main Menu' => popurl(2)
95 die "No customer specified (bad URL)!" unless $cgi->keywords;
96 ($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
99 $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
100 die "Customer not found!" unless $cust_main;
101 $hashref = $cust_main->hashref;
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>!;
111 print qq!<HR><CENTER><A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!,
112 qq!Bill this customer now</A></CENTER>!;
115 print qq!<HR><A NAME="cust_main"><CENTER><FONT SIZE=+1>Customer Information!,
117 qq!<BR><A HREF="!, popurl(2), qq!edit/cust_main.cgi?$custnum!,
118 qq!">Edit this information</A></CENTER><FONT SIZE=-1>!;
121 $agent = qsearchs('agent',{
122 'agentnum' => $cust_main->getfield('agentnum')
124 die "Agent not found!" unless $agent;
125 print "<BR>Agent #<B>" , $agent->getfield('agentnum') , ": " ,
126 $agent->getfield('agent') , "</B>";
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>";
135 print "<P><B>", $hashref->{'last'}, ", ", $hashref->{first}, "</B>";
138 print " (SS# <B>", $hashref->{ss}, "</B>)" if $hashref->{ss};
141 print "<BR><B>", $hashref->{company}, "</B>" if $hashref->{company};
144 print "<BR><B>", $hashref->{address1}, "</B>";
147 print "<BR><B>", $hashref->{address2}, "</B>" if $hashref->{address2};
150 print "<BR><B>", $hashref->{city}, "</B>";
153 print " (<B>", $hashref->{county}, "</B> county)" if $hashref->{county};
156 print ",<B>", $hashref->{state}, "</B>";
159 print " <B>", $hashref->{zip}, "</B>";
162 print "<BR><B>", $hashref->{country}, "</B>"
163 unless $hashref->{country} eq "US";
166 print "<P><B>", $hashref->{daytime}, "</B>" if $hashref->{daytime};
167 print " (Day)" if $hashref->{daytime} && $hashref->{night};
170 print "<BR><B>", $hashref->{night}, "</B>" if $hashref->{night};
171 print " (Night)" if $hashref->{daytime} && $hashref->{night};
174 print "<BR><B>", $hashref->{fax}, "</B> (Fax)" if $hashref->{fax};
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") {
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};
196 print "Unknown payment type ", $hashref->{payby}, "!";
200 print "<BR>(Tax exempt)" if $hashref->{tax};
203 print "<P>Order taken by <B>", $hashref->{otaker}, "</B>";
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>!,
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!,
221 qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
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}
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} ) : "" ,
244 "<TD><FONT SIZE=-1>",
245 $pref->{bill} ? time2str("%D",$pref->{bill} ) : "" ,
247 "<TD><FONT SIZE=-1>",
248 $pref->{susp} ? time2str("%D",$pref->{susp} ) : "" ,
250 "<TD><FONT SIZE=-1>",
251 $pref->{expire} ? time2str("%D",$pref->{expire} ) : "" ,
253 "<TD><FONT SIZE=-1>",
254 $pref->{cancel} ? time2str("%D",$pref->{cancel} ) : "" ,
260 print "</TABLE></CENTER>";
263 print qq!<CENTER><HR><A NAME="history"><FONT SIZE=+1>Payment History!,
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>!;
271 # major problem: this whole thing is way too sloppy.
272 # minor problem: the description lines need better formatting.
274 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
275 foreach $bill (@bills) {
276 my($bref)=$bill->hashref;
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!;
283 my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
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'),
294 "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
298 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
299 foreach $credit (@credits) {
300 my($cref)=$credit->hashref;
302 $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
303 $cref->{credited} . ") by " . $cref->{otaker} . " - " .
304 $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
306 my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
308 foreach $refund (@refunds) {
309 my($rref)=$refund->hashref;
311 $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
312 $rref->{payby} . " " . $rref->{payinfo} . ") by " .
313 $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
319 print "<CENTER>", table, <<END;
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>
331 #display payment history
334 foreach $item (sort keyfield_numerically @history) {
335 my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
340 $balance += $charge - $payment;
341 $balance -= $credit - $refund;
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) : '' ),
348 "<TD><FONT SIZE=-1>",
349 ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
351 "<TD><FONT SIZE=-1>",
352 ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
354 "<TD><FONT SIZE=-1>",
355 ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
357 "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
363 print "</TABLE></CENTER>";
375 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }