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