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