3 # $Id: cust_main.cgi,v 1.12 1999-02-07 09:59:40 ivan Exp $
5 # Usage: cust_main.cgi custnum
6 # http://server.name/path/cust_main.cgi?custnum
8 # Note: Should be run setuid freeside as user nobody.
10 # the payment history section could use some work, see below
12 # ivan@voicenet.com 96-nov-29 -> 96-dec-11
14 # added navigation bar (go to main menu ;)
15 # ivan@voicenet.com 97-jan-30
17 # changes to the way credits/payments are applied (the links are here).
18 # ivan@voicenet.com 97-apr-21
20 # added debugging code to diagnose CPU sucking problem.
21 # ivan@voicenet.com 97-may-19
23 # CPU sucking problem was in comment code? fixed?
24 # ivan@voicenet.com 97-may-22
27 # ivan@voicenet.com 97-jul-22
29 # Changes to allow page to work at a relative position in server
30 # Changed 'day' to 'daytime' because Pg6.3 reserves the day word
31 # bmccane@maxbaud.net 98-apr-3
33 # lose background, FS::CGI ivan@sisd.com 98-sep-2
35 # $Log: cust_main.cgi,v $
36 # Revision 1.12 1999-02-07 09:59:40 ivan
37 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
39 # Revision 1.11 1999/01/25 12:26:04 ivan
40 # yet more mod_perl stuff
42 # Revision 1.10 1999/01/19 05:14:19 ivan
43 # for mod_perl: no more top-level my() variables; use vars instead
44 # also the last s/create/new/;
46 # Revision 1.9 1999/01/18 09:41:43 ivan
47 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
50 # Revision 1.8 1999/01/18 09:22:35 ivan
51 # changes to track email addresses for email invoicing
53 # Revision 1.7 1998/12/30 23:03:34 ivan
54 # bugfixes; fields isn't exported by derived classes
56 # Revision 1.6 1998/12/23 02:42:33 ivan
57 # remove double '/' in link urls
59 # Revision 1.5 1998/12/23 02:36:28 ivan
60 # use FS::cust_refund; to eliminate warning
62 # Revision 1.4 1998/12/17 09:57:21 ivan
63 # s/CGI::(Base|Request)/CGI.pm/;
65 # Revision 1.3 1998/11/15 13:14:20 ivan
66 # first pass as per-customer custom pricing
68 # Revision 1.2 1998/11/13 11:28:08 ivan
69 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
73 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral
74 @packages $package @history @bills $bill @credits $credit
75 $balance $item @agents @referrals @invoicing_list $n1 );
77 use CGI::Carp qw(fatalsToBrowser);
79 use FS::UID qw(cgisuidsetup);
80 use FS::Record qw(qsearchs qsearch);
81 use FS::CGI qw(header menubar popurl table itable ntable);
87 use FS::part_referral;
95 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
96 'Main Menu' => popurl(2)
99 die "No customer specified (bad URL)!" unless $cgi->keywords;
100 ($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
103 $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
104 die "Customer not found!" unless $cust_main;
105 $hashref = $cust_main->hashref;
107 print itable(), '<TR><TD><A NAME="cust_main"></A>';
109 print qq!<A HREF="!, popurl(2),
110 qq!edit/cust_main.cgi?$custnum">Edit this customer</A>!,
111 ntable("#c0c0c0"), "<TR><TD>", ntable("#c0c0c0",2),
112 '<TR><TD ALIGN="right">Customer number</TD><TD BGCOLOR="#ffffff">',
113 $custnum, '</TD></TR>',
116 @agents = qsearch( 'agent', {} );
117 unless ( scalar(@agents) == 1 ) {
118 $agent = qsearchs('agent',{
119 'agentnum' => $cust_main->agentnum
121 print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
122 $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
124 @referrals = qsearch( 'part_referral', {} );
125 unless ( scalar(@referrals) == 1 ) {
126 my $referral = qsearchs('part_referral', {
127 'refnum' => $cust_main->refnum
129 print '<TR><TD ALIGN="right">Referral</TD><TD BGCOLOR="#ffffff">',
130 $referral->refnum, ": ", $referral->referral, '</TD></TR>';
132 print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
133 $cust_main->otaker, '</TD></TR>';
135 print '</TABLE></TD></TR></TABLE>';
137 print '</TD><TD ROWSPAN=2>';
139 print "Contact information", ntable("#c0c0c0"), "<TR><TD>",
141 '<TR><TD ALIGN="right">Contact name<BR>(last, first)</TD>',
142 '<TD COLSPAN=3 BGCOLOR="#ffffff">',
143 $cust_main->last, ', ', $cust_main->first,
144 '</TD><TD ALIGN="right">SS#</TD><TD BGCOLOR="#ffffff">',
145 $cust_main->ss || ' ', '</TD></TR>',
146 '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
149 '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
150 $cust_main->address1,
153 print '<TR><TD ALIGN="right"> </TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
154 $cust_main->address2, '</TD></TR>'
155 if $cust_main->address2;
156 print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
158 '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
160 '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
161 $cust_main->zip, '</TD></TR>',
162 '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
166 print '<TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
167 $cust_main->daytime || ' ', '</TD></TR>',
168 '<TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
169 $cust_main->night || ' ', '</TD></TR>',
170 '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
171 $cust_main->fax || ' ', '</TD></TR>',
172 '</TABLE>', "</TD></TR></TABLE>"
175 print '</TD></TR><TR><TD>';
177 @invoicing_list = $cust_main->invoicing_list;
178 print "Billing information (",
179 qq!<A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!, "Bill now</A>)",
180 ntable("#c0c0c0"), "<TR><TD>", ntable("#c0c0c0",2),
181 '<TR><TD ALIGN="right">Tax exempt</TD><TD BGCOLOR="#ffffff">',
182 $cust_main->tax ? 'yes' : 'no',
184 '<TR><TD ALIGN="right">Postal invoices</TD><TD BGCOLOR="#ffffff">',
185 ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
187 '<TR><TD ALIGN="right">Email invoices</TD><TD BGCOLOR="#ffffff">',
188 join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
190 '<TR><TD ALIGN="right">Billing type</TD><TD BGCOLOR="#ffffff">',
193 if ( $cust_main->payby eq 'CARD' ) {
194 print 'Credit card</TD></TR>',
195 '<TR><TD ALIGN="right">Card number</TD><TD BGCOLOR="#ffffff">',
196 $cust_main->payinfo, '</TD></TR>',
197 '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
198 $cust_main->paydate, '</TD></TR>',
199 '<TR><TD ALIGN="right">Name on card</TD><TD BGCOLOR="#ffffff">',
200 $cust_main->payname, '</TD></TR>'
202 } elsif ( $cust_main->payby eq 'BILL' ) {
203 print 'Billing</TD></TR>';
204 print '<TR><TD ALIGN="right">P.O. </TD><TD BGCOLOR="#ffffff">',
205 $cust_main->payinfo, '</TD></TR>',
206 if $cust_main->payinfo;
207 print '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
208 $cust_main->paydate, '</TD></TR>',
209 '<TR><TD ALIGN="right">Attention</TD><TD BGCOLOR="#ffffff">',
210 $cust_main->payname, '</TD></TR>',
212 } elsif ( $cust_main->payby eq 'COMP' ) {
213 print 'Complimentary</TD></TR>',
214 '<TR><TD ALIGN="right">Authorized by</TD><TD BGCOLOR="#ffffff">',
215 $cust_main->payinfo, '</TD></TR>',
216 '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
217 $cust_main->paydate, '</TD></TR>',
221 print "</TABLE></TD></TR></TABLE></TD></TR></TABLE>";
223 print qq!<BR><BR><A NAME="cust_pkg">Packages</A> !,
224 # qq!<BR>Click on package number to view/edit package.!,
225 qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> )!,
231 print qq!!, table, "\n",
232 qq!<TR><TH COLSPAN=2 ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
233 qq!Dates</TH><TH COLSPAN=2 ROWSPAN=2>Services</TH></TR>\n!,
234 qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
235 qq!<FONT SIZE=-1>Next bill</FONT>!,
236 qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
238 qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
242 @packages = $cust_main->all_pkgs;
243 #@packages = $cust_main->ncancelled_pkgs;
246 foreach $package (@packages) {
247 my $pkgnum = $package->pkgnum;
248 my $pkg = $package->part_pkg->pkg;
249 my $comment = $package->part_pkg->comment;
250 my $pkgview = popurl(2). "view/cust_pkg.cgi?$pkgnum";
251 my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
252 my $rowspan = scalar(@cust_svc) || 1;
254 my $button_cgi = new CGI;
255 $button_cgi->param('clone', $package->part_pkg->pkgpart);
256 $button_cgi->param('pkgnum', $package->pkgnum);
257 my $button_url = popurl(2). "edit/part_pkg.cgi?". $button_cgi->query_string;
259 #print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview">$pkgnum</A></TD>!,
260 print $n1, qq!<TD ROWSPAN=$rowspan>$pkgnum</TD>!,
261 qq!<TD ROWSPAN=$rowspan><FONT SIZE=-1>!,
262 #qq!<A HREF="$pkgview">$pkg - $comment</A>!,
264 qq! ( <A HREF="$pkgview">Edit</A> | <A HREF="$button_url">Customize pricing</A> )</FONT></TD>!,
266 for ( qw( setup bill susp expire cancel ) ) {
267 print "<TD ROWSPAN=$rowspan><FONT SIZE=-1>", ( $package->getfield($_)
268 ? time2str("%D", $package->getfield($_) )
275 foreach my $cust_svc ( @cust_svc ) {
276 my($label, $value, $svcdb) = $cust_svc->label;
277 my($svcnum) = $cust_svc->svcnum;
278 my($sview) = popurl(2). "/view";
279 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
280 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
291 print qq!<BR><BR><A NAME="history">Payment History!,
293 qq! ( Click on invoice to view invoice/enter payment. | !,
294 qq!<A HREF="!, popurl(2), qq!edit/cust_credit.cgi?$custnum">!,
295 qq!Post credit / refund</A> )!;
299 # major problem: this whole thing is way too sloppy.
300 # minor problem: the description lines need better formatting.
302 @history = (); #needed for mod_perl :)
304 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
305 foreach $bill (@bills) {
306 my($bref)=$bill->hashref;
308 $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!view/cust_bill.cgi?! .
309 $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
310 qq! (Balance \$! . $bref->{owed} . qq!)</A>\t! .
311 $bref->{charged} . qq!\t\t\t!;
313 my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
315 foreach $payment (@payments) {
316 my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
317 $payment->getfield('invnum'),
318 $payment->getfield('payby'),
319 $payment->getfield('payinfo'),
320 $payment->getfield('paid'),
323 "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
327 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
328 foreach $credit (@credits) {
329 my($cref)=$credit->hashref;
331 $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
332 $cref->{credited} . ") by " . $cref->{otaker} . " - " .
333 $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
335 my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
337 foreach $refund (@refunds) {
338 my($rref)=$refund->hashref;
340 $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
341 $rref->{payby} . " " . $rref->{payinfo} . ") by " .
342 $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
348 print table(), <<END;
352 <TH><FONT SIZE=-1>Charge</FONT></TH>
353 <TH><FONT SIZE=-1>Payment</FONT></TH>
354 <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
355 <TH><FONT SIZE=-1>Refund</FONT></TH>
356 <TH><FONT SIZE=-1>Balance</FONT></TH>
360 #display payment history
363 foreach $item (sort keyfield_numerically @history) {
364 my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
369 $balance += $charge - $payment;
370 $balance -= $credit - $refund;
372 print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
373 "<TD><FONT SIZE=-1>$desc</FONT></TD>",
374 "<TD><FONT SIZE=-1>",
375 ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
377 "<TD><FONT SIZE=-1>",
378 ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
380 "<TD><FONT SIZE=-1>",
381 ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
383 "<TD><FONT SIZE=-1>",
384 ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
386 "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
404 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }