2119b3e0c70c53e158f4f59fcc5285f795f41dd7
[freeside.git] / htdocs / view / cust_main.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: cust_main.cgi,v 1.11 1999-01-25 12:26:04 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.11  1999-01-25 12:26:04  ivan
37 # yet more mod_perl stuff
38 #
39 # Revision 1.10  1999/01/19 05:14:19  ivan
40 # for mod_perl: no more top-level my() variables; use vars instead
41 # also the last s/create/new/;
42 #
43 # Revision 1.9  1999/01/18 09:41:43  ivan
44 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
45 # (good idea anyway)
46 #
47 # Revision 1.8  1999/01/18 09:22:35  ivan
48 # changes to track email addresses for email invoicing
49 #
50 # Revision 1.7  1998/12/30 23:03:34  ivan
51 # bugfixes; fields isn't exported by derived classes
52 #
53 # Revision 1.6  1998/12/23 02:42:33  ivan
54 # remove double '/' in link urls
55 #
56 # Revision 1.5  1998/12/23 02:36:28  ivan
57 # use FS::cust_refund; to eliminate warning
58 #
59 # Revision 1.4  1998/12/17 09:57:21  ivan
60 # s/CGI::(Base|Request)/CGI.pm/;
61 #
62 # Revision 1.3  1998/11/15 13:14:20  ivan
63 # first pass as per-customer custom pricing
64 #
65 # Revision 1.2  1998/11/13 11:28:08  ivan
66 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
67 #
68
69 use strict;
70 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral 
71               @packages $package @history @bills $bill @credits $credit
72               $balance $item @agents @referrals @invoicing_list $n1 ); 
73 use CGI;
74 use CGI::Carp qw(fatalsToBrowser);
75 use Date::Format;
76 use FS::UID qw(cgisuidsetup);
77 use FS::Record qw(qsearchs qsearch);
78 use FS::CGI qw(header menubar popurl table itable ntable);
79 use FS::cust_credit;
80 use FS::cust_pay;
81 use FS::cust_bill;
82 use FS::part_pkg;
83 use FS::cust_pkg;
84 use FS::part_referral;
85 use FS::agent;
86 use FS::cust_main;
87 use FS::cust_refund;
88
89 $cgi = new CGI;
90 &cgisuidsetup($cgi);
91
92 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
93   'Main Menu' => popurl(2)
94 ));
95
96 die "No customer specified (bad URL)!" unless $cgi->keywords;
97 ($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
98 $query =~ /^(\d+)$/;
99 $custnum = $1;
100 $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
101 die "Customer not found!" unless $cust_main;
102 $hashref = $cust_main->hashref;
103
104 print itable(), '<TR><TD><A NAME="cust_main"></A>';
105
106 print qq!<A HREF="!, popurl(2), 
107       qq!edit/cust_main.cgi?$custnum">Edit this customer</A>!,
108       ntable("#c0c0c0"), "<TR><TD>", ntable("#c0c0c0",2),
109       '<TR><TD ALIGN="right">Customer number</TD><TD BGCOLOR="#ffffff">',
110       $custnum, '</TD></TR>',
111 ;
112
113 @agents = qsearch( 'agent', {} );
114 unless ( scalar(@agents) == 1 ) {
115   $agent = qsearchs('agent',{
116     'agentnum' => $cust_main->agentnum
117   } );
118   print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
119         $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
120 }
121 @referrals = qsearch( 'part_referral', {} );
122 unless ( scalar(@referrals) == 1 ) {
123   my $referral = qsearchs('part_referral', {
124     'refnum' => $cust_main->refnum
125   } );
126   print '<TR><TD ALIGN="right">Referral</TD><TD BGCOLOR="#ffffff">',
127         $referral->refnum, ": ", $referral->referral, '</TD></TR>';
128 }
129 print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
130   $cust_main->otaker, '</TD></TR>';
131
132 print '</TABLE></TD></TR></TABLE>';
133
134 print '</TD><TD ROWSPAN=2>';
135
136 print "Contact information", ntable("#c0c0c0"), "<TR><TD>",
137       ntable("#c0c0c0",2),
138   '<TR><TD ALIGN="right">Contact name<BR>(last, first)</TD>',
139     '<TD COLSPAN=3 BGCOLOR="#ffffff">',
140     $cust_main->last, ', ', $cust_main->first,
141     '</TD><TD ALIGN="right">SS#</TD><TD BGCOLOR="#ffffff">',
142     $cust_main->ss || '&nbsp', '</TD></TR>',
143   '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
144     $cust_main->company,
145     '</TD></TR>',
146   '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
147     $cust_main->address1,
148     '</TD></TR>',
149 ;
150 print '<TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
151       $cust_main->address2, '</TD></TR>'
152   if $cust_main->address2;
153 print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
154         $cust_main->city,
155         '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
156         $cust_main->state,
157         '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
158         $cust_main->zip, '</TD></TR>',
159       '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
160         $cust_main->country,
161         '</TD></TR>',
162 ;
163 print '<TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
164         $cust_main->daytime || '&nbsp', '</TD></TR>',
165       '<TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
166         $cust_main->night || '&nbsp', '</TD></TR>',
167       '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
168         $cust_main->fax || '&nbsp', '</TD></TR>',
169       '</TABLE>', "</TD></TR></TABLE>"
170 ;
171
172 print '</TD></TR><TR><TD>';
173
174 @invoicing_list = $cust_main->invoicing_list;
175 print "Billing information (",
176       qq!<A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!, "Bill now</A>)",
177       ntable("#c0c0c0"), "<TR><TD>", ntable("#c0c0c0",2),
178       '<TR><TD ALIGN="right">Tax exempt</TD><TD BGCOLOR="#ffffff">',
179       $cust_main->tax ? 'yes' : 'no',
180       '</TD></TR>',
181       '<TR><TD ALIGN="right">Postal invoices</TD><TD BGCOLOR="#ffffff">',
182       ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
183       '</TD></TR>',
184       '<TR><TD ALIGN="right">Email invoices</TD><TD BGCOLOR="#ffffff">',
185       join(', ', grep { $_ ne 'POST' } @invoicing_list ),
186       '</TD></TR>',
187       '<TR><TD ALIGN="right">Billing type</TD><TD BGCOLOR="#ffffff">',
188 ;
189
190 if ( $cust_main->payby eq 'CARD' ) {
191   print 'Credit card</TD></TR>',
192         '<TR><TD ALIGN="right">Card number</TD><TD BGCOLOR="#ffffff">',
193         $cust_main->payinfo, '</TD></TR>',
194         '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
195         $cust_main->paydate, '</TD></TR>',
196         '<TR><TD ALIGN="right">Name on card</TD><TD BGCOLOR="#ffffff">',
197         $cust_main->payname, '</TD></TR>'
198   ;
199 } elsif ( $cust_main->payby eq 'BILL' ) {
200   print 'Billing</TD></TR>';
201   print '<TR><TD ALIGN="right">P.O. </TD><TD BGCOLOR="#ffffff">',
202         $cust_main->payinfo, '</TD></TR>',
203     if $cust_main->payinfo;
204   print '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
205         $cust_main->paydate, '</TD></TR>',
206         '<TR><TD ALIGN="right">Attention</TD><TD BGCOLOR="#ffffff">',
207         $cust_main->payname, '</TD></TR>',
208   ;
209 } elsif ( $cust_main->payby eq 'COMP' ) {
210   print 'Complimentary</TD></TR>',
211         '<TR><TD ALIGN="right">Authorized by</TD><TD BGCOLOR="#ffffff">',
212         $cust_main->payinfo, '</TD></TR>',
213         '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
214         $cust_main->paydate, '</TD></TR>',
215   ;
216 }
217
218 print "</TABLE></TD></TR></TABLE></TD></TR></TABLE>";
219
220 print qq!<BR><BR><A NAME="cust_pkg">Packages</A> !,
221 #      qq!<BR>Click on package number to view/edit package.!,
222       qq!( <A HREF="!, popurl(2), qq!/edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> )!,
223 ;
224
225 #display packages
226
227 #formatting
228 print qq!!, table, "\n",
229       qq!<TR><TH COLSPAN=2 ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
230       qq!Dates</TH><TH COLSPAN=2 ROWSPAN=2>Services</TH></TR>\n!,
231       qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
232       qq!<FONT SIZE=-1>Next bill</FONT>!,
233       qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
234       qq!</FONT></TH>!,
235       qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
236       qq!</TR>\n!;
237
238 #get package info
239 @packages = $cust_main->all_pkgs;
240 #@packages = $cust_main->ncancelled_pkgs;
241
242 $n1 = '';
243 foreach $package (@packages) {
244   my $pkgnum = $package->pkgnum;
245   my $pkg = $package->part_pkg->pkg;
246   my $comment = $package->part_pkg->comment;
247   my $pkgview = popurl(2). "view/cust_pkg.cgi?$pkgnum";
248   my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
249   my $rowspan = scalar(@cust_svc) || 1;
250
251   my $button_cgi = new CGI;
252   $button_cgi->param('clone', $package->part_pkg->pkgpart);
253   $button_cgi->param('pkgnum', $package->pkgnum);
254   my $button_url = popurl(2). "edit/part_pkg.cgi?". $button_cgi->query_string;
255
256   #print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview">$pkgnum</A></TD>!,
257   print $n1, qq!<TD ROWSPAN=$rowspan>$pkgnum</TD>!,
258         qq!<TD ROWSPAN=$rowspan><FONT SIZE=-1>!,
259         #qq!<A HREF="$pkgview">$pkg - $comment</A>!,
260         qq!$pkg - $comment!,
261         qq! ( <A HREF="$pkgview">Edit</A> | <A HREF="$button_url">Customize pricing</A>)</FONT></TD>!,
262   ;
263   for ( qw( setup bill susp expire cancel ) ) {
264     print "<TD ROWSPAN=$rowspan><FONT SIZE=-1>", ( $package->getfield($_)
265             ? time2str("%D", $package->getfield($_) )
266             :  '&nbsp'
267           ), '</FONT></TD>',
268     ;
269   }
270
271   my $n2 = '';
272   foreach my $cust_svc ( @cust_svc ) {
273      my($label, $value, $svcdb) = $cust_svc->label;
274      my($svcnum) = $cust_svc->svcnum;
275      my($sview) = popurl(2). "/view";
276      print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
277            qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
278      $n2="</TR><TR>";
279   }
280   $n1="</TR><TR>";
281 }  
282 print "</TR>";
283
284 #formatting
285 print "</TABLE>";
286
287 #formatting
288 print qq!<BR><BR><A NAME="history">Payment History!,
289       qq!</A>!,
290       qq! ( Click on invoice to view invoice/enter payment. | !,
291       qq!<A HREF="!, popurl(2), qq!edit/cust_credit.cgi?$custnum">!,
292       qq!Post credit / refund</A> )!;
293
294 #get payment history
295 #
296 # major problem: this whole thing is way too sloppy.
297 # minor problem: the description lines need better formatting.
298
299 @history = (); #needed for mod_perl :)
300
301 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
302 foreach $bill (@bills) {
303   my($bref)=$bill->hashref;
304   push @history,
305     $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!view/cust_bill.cgi?! .
306     $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
307     qq! (Balance \$! . $bref->{owed} . qq!)</A>\t! .
308     $bref->{charged} . qq!\t\t\t!;
309
310   my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
311   my($payment);
312   foreach $payment (@payments) {
313     my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
314                                              $payment->getfield('invnum'),
315                                              $payment->getfield('payby'),
316                                              $payment->getfield('payinfo'),
317                                              $payment->getfield('paid'),
318                       );
319     push @history,
320       "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
321   }
322 }
323
324 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
325 foreach $credit (@credits) {
326   my($cref)=$credit->hashref;
327   push @history,
328     $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
329     $cref->{credited} . ") by " . $cref->{otaker} . " - " .
330     $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
331
332   my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
333   my($refund);
334   foreach $refund (@refunds) {
335     my($rref)=$refund->hashref;
336     push @history,
337       $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
338       $rref->{payby} . " " . $rref->{payinfo} . ") by " .
339       $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
340       $rref->{refund};
341   }
342 }
343
344         #formatting
345         print table(), <<END;
346 <TR>
347   <TH>Date</TH>
348   <TH>Description</TH>
349   <TH><FONT SIZE=-1>Charge</FONT></TH>
350   <TH><FONT SIZE=-1>Payment</FONT></TH>
351   <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
352   <TH><FONT SIZE=-1>Refund</FONT></TH>
353   <TH><FONT SIZE=-1>Balance</FONT></TH>
354 </TR>
355 END
356
357 #display payment history
358
359 $balance = 0;
360 foreach $item (sort keyfield_numerically @history) {
361   my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
362   $charge ||= 0;
363   $payment ||= 0;
364   $credit ||= 0;
365   $refund ||= 0;
366   $balance += $charge - $payment;
367   $balance -= $credit - $refund;
368
369   print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
370         "<TD><FONT SIZE=-1>$desc</FONT></TD>",
371         "<TD><FONT SIZE=-1>",
372         ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
373         "</FONT></TD>",
374         "<TD><FONT SIZE=-1>",
375         ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
376         "</FONT></TD>",
377         "<TD><FONT SIZE=-1>",
378         ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
379         "</FONT></TD>",
380         "<TD><FONT SIZE=-1>",
381         ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
382         "</FONT></TD>",
383         "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
384         "</FONT></TD>",
385         "\n";
386 }
387
388 #formatting
389 print "</TABLE>";
390
391 #end
392
393 #formatting
394 print <<END;
395
396   </BODY>
397 </HTML>
398 END
399
400 #subroutiens
401 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }
402