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