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