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