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