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