s/CGI-modules/CGI.pm/;, relative URL's with popurl
[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 # $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
38 #
39
40 use strict;
41 use CGI;
42 use CGI::Carp qw(fatalsToBrowser);
43 use Date::Format;
44 use FS::UID qw(cgisuidsetup);
45 use FS::Record qw(qsearchs qsearch);
46 use FS::CGI qw(header menubar popurl table);
47 use FS::cust_credit;
48 use FS::cust_pay;
49 use FS::cust_bill;
50 use FS::part_pkg;
51 use FS::cust_pkg;
52 use FS::part_referral;
53 use FS::agent;
54 use FS::cust_main;
55
56 my($cgi) = new CGI;
57 &cgisuidsetup($cgi);
58
59 print $cgi->header, header("Customer View", menubar(
60   'Main Menu' => popurl(2)
61 )),<<END;
62     <BASEFONT SIZE=3>
63 END
64
65 die "No customer specified (bad URL)!" unless $cgi->keywords;
66 my($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
67 $query =~ /^(\d+)$/;
68 my($custnum)=$1;
69 my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
70 die "Customer not found!" unless $cust_main;
71 my($hashref)=$cust_main->hashref;
72
73 #custnum
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>!;
79
80 #bill now linke
81 print qq!<HR><CENTER><A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!,
82       qq!Bill this customer now</A></CENTER>!;
83
84 #formatting
85 print qq!<HR><A NAME="cust_main"><CENTER><FONT SIZE=+1>Customer Information!,
86       qq!</FONT>!,
87       qq!<BR><A HREF="!, popurl(2), qq!/edit/cust_main.cgi?$custnum!,
88       qq!">Edit this information</A></CENTER><FONT SIZE=-1>!;
89
90 #agentnum
91 my($agent)=qsearchs('agent',{
92   'agentnum' => $cust_main->getfield('agentnum')
93 } );
94 die "Agent not found!" unless $agent;
95 print "<BR>Agent #<B>" , $agent->getfield('agentnum') , ": " ,
96                          $agent->getfield('agent') , "</B>";
97
98 #refnum
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>"; 
103
104 #last, first
105 print "<P><B>", $hashref->{'last'}, ", ", $hashref->{first}, "</B>";
106
107 #ss
108 print " (SS# <B>", $hashref->{ss}, "</B>)" if $hashref->{ss};
109
110 #company
111 print "<BR><B>", $hashref->{company}, "</B>" if $hashref->{company};
112
113 #address1
114 print "<BR><B>", $hashref->{address1}, "</B>";
115
116 #address2
117 print "<BR><B>", $hashref->{address2}, "</B>" if $hashref->{address2};
118
119 #city
120 print "<BR><B>", $hashref->{city}, "</B>";
121
122 #county
123 print " (<B>", $hashref->{county}, "</B> county)" if $hashref->{county};
124
125 #state
126 print ",<B>", $hashref->{state}, "</B>";
127
128 #zip
129 print "  <B>", $hashref->{zip}, "</B>";
130
131 #country
132 print "<BR><B>", $hashref->{country}, "</B>"
133   unless $hashref->{country} eq "US";
134
135 #daytime
136 print "<P><B>", $hashref->{daytime}, "</B>" if $hashref->{daytime};
137 print " (Day)" if $hashref->{daytime} && $hashref->{night};
138
139 #night
140 print "<BR><B>", $hashref->{night}, "</B>" if $hashref->{night};
141 print " (Night)" if $hashref->{daytime} && $hashref->{night};
142
143 #fax
144 print "<BR><B>", $hashref->{fax}, "</B> (Fax)" if $hashref->{fax};
145
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") {
152   print "<P>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};
165 } else {
166   print "Unknown payment type ", $hashref->{payby}, "!";
167 }
168
169 #tax
170 print "<BR>(Tax exempt)" if $hashref->{tax};
171
172 #otaker
173 print "<P>Order taken by <B>", $hashref->{otaker}, "</B>";
174
175 #formatting     
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>!,
179       qq!</CENTER><BR>!;
180
181 #display packages
182
183 #formatting
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!,
190       qq!</FONT></TH>!,
191       qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
192       qq!</TR>\n!;
193
194 #get package info
195 my(@packages)=qsearch('cust_pkg',{'custnum'=>$custnum});
196 my($package);
197 foreach $package (@packages) {
198   my($pref)=$package->hashref;
199   my($part_pkg)=qsearchs('part_pkg',{
200     'pkgpart' => $pref->{pkgpart}
201   } );
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} ) : "" ,
209         "</FONT></TD>",
210         "<TD><FONT SIZE=-1>", 
211         $pref->{bill} ? time2str("%D",$pref->{bill} ) : "" ,
212         "</FONT></TD>",
213         "<TD><FONT SIZE=-1>",
214         $pref->{susp} ? time2str("%D",$pref->{susp} ) : "" ,
215         "</FONT></TD>",
216         "<TD><FONT SIZE=-1>",
217         $pref->{expire} ? time2str("%D",$pref->{expire} ) : "" ,
218         "</FONT></TD>",
219         "<TD><FONT SIZE=-1>",
220         $pref->{cancel} ? time2str("%D",$pref->{cancel} ) : "" ,
221         "</FONT></TD>",
222         "</TR>";
223 }
224
225 #formatting
226 print "</TABLE></CENTER>";
227
228 #formatting
229 print qq!<CENTER><HR><A NAME="history"><FONT SIZE=+1>Payment History!,
230       qq!</FONT></A><BR>!,
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>!;
234
235 #get payment history
236 #
237 # major problem: this whole thing is way too sloppy.
238 # minor problem: the description lines need better formatting.
239
240 my(@history);
241
242 my(@bills)=qsearch('cust_bill',{'custnum'=>$custnum});
243 my($bill);
244 foreach $bill (@bills) {
245   my($bref)=$bill->hashref;
246   push @history,
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!;
251
252   my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
253   my($payment);
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'),
261                       );
262     push @history,
263       "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
264   }
265 }
266
267 my(@credits)=qsearch('cust_credit',{'custnum'=>$custnum});
268 my($credit);
269 foreach $credit (@credits) {
270   my($cref)=$credit->hashref;
271   push @history,
272     $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
273     $cref->{credited} . ") by " . $cref->{otaker} . " - " .
274     $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
275
276   my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
277   my($refund);
278   foreach $refund (@refunds) {
279     my($rref)=$refund->hashref;
280     push @history,
281       $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
282       $rref->{payby} . " " . $rref->{payinfo} . ") by " .
283       $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
284       $rref->{refund};
285   }
286 }
287
288         #formatting
289         print "<CENTER>", table, <<END;
290 <TR>
291   <TH>Date</TH>
292   <TH>Description</TH>
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>
298 </TR>
299 END
300
301 #display payment history
302
303 my($balance)=0;
304 my($item);
305 foreach $item (sort keyfield_numerically @history) {
306   my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
307   $charge ||= 0;
308   $payment ||= 0;
309   $credit ||= 0;
310   $refund ||= 0;
311   $balance += $charge - $payment;
312   $balance -= $credit - $refund;
313
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) : '' ),
318         "</FONT></TD>",
319         "<TD><FONT SIZE=-1>",
320         ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
321         "</FONT></TD>",
322         "<TD><FONT SIZE=-1>",
323         ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
324         "</FONT></TD>",
325         "<TD><FONT SIZE=-1>",
326         ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
327         "</FONT></TD>",
328         "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
329         "</FONT></TD>",
330         "\n";
331 }
332
333 #formatting
334 print "</TABLE></CENTER>";
335
336 #end
337
338 #formatting
339 print <<END;
340
341   </BODY>
342 </HTML>
343 END
344
345 #subroutiens
346 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }
347