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