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