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