3 # $Id: cust_main.cgi,v 1.13 1999-02-28 00:04:00 ivan Exp $
5 # Usage: cust_main.cgi custnum
6 # http://server.name/path/cust_main.cgi?custnum
8 # the payment history section could use some work, see below
10 # ivan@voicenet.com 96-nov-29 -> 96-dec-11
12 # added navigation bar (go to main menu ;)
13 # ivan@voicenet.com 97-jan-30
15 # changes to the way credits/payments are applied (the links are here).
16 # ivan@voicenet.com 97-apr-21
18 # added debugging code to diagnose CPU sucking problem.
19 # ivan@voicenet.com 97-may-19
21 # CPU sucking problem was in comment code? fixed?
22 # ivan@voicenet.com 97-may-22
25 # ivan@voicenet.com 97-jul-22
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
31 # lose background, FS::CGI ivan@sisd.com 98-sep-2
33 # $Log: cust_main.cgi,v $
34 # Revision 1.13 1999-02-28 00:04:00 ivan
35 # removed misleading comments
37 # Revision 1.12 1999/02/07 09:59:40 ivan
38 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
40 # Revision 1.11 1999/01/25 12:26:04 ivan
41 # yet more mod_perl stuff
43 # Revision 1.10 1999/01/19 05:14:19 ivan
44 # for mod_perl: no more top-level my() variables; use vars instead
45 # also the last s/create/new/;
47 # Revision 1.9 1999/01/18 09:41:43 ivan
48 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
51 # Revision 1.8 1999/01/18 09:22:35 ivan
52 # changes to track email addresses for email invoicing
54 # Revision 1.7 1998/12/30 23:03:34 ivan
55 # bugfixes; fields isn't exported by derived classes
57 # Revision 1.6 1998/12/23 02:42:33 ivan
58 # remove double '/' in link urls
60 # Revision 1.5 1998/12/23 02:36:28 ivan
61 # use FS::cust_refund; to eliminate warning
63 # Revision 1.4 1998/12/17 09:57:21 ivan
64 # s/CGI::(Base|Request)/CGI.pm/;
66 # Revision 1.3 1998/11/15 13:14:20 ivan
67 # first pass as per-customer custom pricing
69 # Revision 1.2 1998/11/13 11:28:08 ivan
70 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
74 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral
75 @packages $package @history @bills $bill @credits $credit
76 $balance $item @agents @referrals @invoicing_list $n1 );
78 use CGI::Carp qw(fatalsToBrowser);
80 use FS::UID qw(cgisuidsetup);
81 use FS::Record qw(qsearchs qsearch);
82 use FS::CGI qw(header menubar popurl table itable ntable);
88 use FS::part_referral;
96 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
97 'Main Menu' => popurl(2)
100 die "No customer specified (bad URL)!" unless $cgi->keywords;
101 ($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
104 $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
105 die "Customer not found!" unless $cust_main;
106 $hashref = $cust_main->hashref;
108 print itable(), '<TR><TD><A NAME="cust_main"></A>';
110 print qq!<A HREF="!, popurl(2),
111 qq!edit/cust_main.cgi?$custnum">Edit this customer</A>!,
112 ntable("#c0c0c0"), "<TR><TD>", ntable("#c0c0c0",2),
113 '<TR><TD ALIGN="right">Customer number</TD><TD BGCOLOR="#ffffff">',
114 $custnum, '</TD></TR>',
117 @agents = qsearch( 'agent', {} );
118 unless ( scalar(@agents) == 1 ) {
119 $agent = qsearchs('agent',{
120 'agentnum' => $cust_main->agentnum
122 print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
123 $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
125 @referrals = qsearch( 'part_referral', {} );
126 unless ( scalar(@referrals) == 1 ) {
127 my $referral = qsearchs('part_referral', {
128 'refnum' => $cust_main->refnum
130 print '<TR><TD ALIGN="right">Referral</TD><TD BGCOLOR="#ffffff">',
131 $referral->refnum, ": ", $referral->referral, '</TD></TR>';
133 print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
134 $cust_main->otaker, '</TD></TR>';
136 print '</TABLE></TD></TR></TABLE>';
138 print '</TD><TD ROWSPAN=2>';
140 print "Contact information", ntable("#c0c0c0"), "<TR><TD>",
142 '<TR><TD ALIGN="right">Contact name<BR>(last, first)</TD>',
143 '<TD COLSPAN=3 BGCOLOR="#ffffff">',
144 $cust_main->last, ', ', $cust_main->first,
145 '</TD><TD ALIGN="right">SS#</TD><TD BGCOLOR="#ffffff">',
146 $cust_main->ss || ' ', '</TD></TR>',
147 '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
150 '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
151 $cust_main->address1,
154 print '<TR><TD ALIGN="right"> </TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
155 $cust_main->address2, '</TD></TR>'
156 if $cust_main->address2;
157 print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
159 '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
161 '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
162 $cust_main->zip, '</TD></TR>',
163 '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
167 print '<TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
168 $cust_main->daytime || ' ', '</TD></TR>',
169 '<TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
170 $cust_main->night || ' ', '</TD></TR>',
171 '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
172 $cust_main->fax || ' ', '</TD></TR>',
173 '</TABLE>', "</TD></TR></TABLE>"
176 print '</TD></TR><TR><TD>';
178 @invoicing_list = $cust_main->invoicing_list;
179 print "Billing information (",
180 qq!<A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!, "Bill now</A>)",
181 ntable("#c0c0c0"), "<TR><TD>", ntable("#c0c0c0",2),
182 '<TR><TD ALIGN="right">Tax exempt</TD><TD BGCOLOR="#ffffff">',
183 $cust_main->tax ? 'yes' : 'no',
185 '<TR><TD ALIGN="right">Postal invoices</TD><TD BGCOLOR="#ffffff">',
186 ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
188 '<TR><TD ALIGN="right">Email invoices</TD><TD BGCOLOR="#ffffff">',
189 join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
191 '<TR><TD ALIGN="right">Billing type</TD><TD BGCOLOR="#ffffff">',
194 if ( $cust_main->payby eq 'CARD' ) {
195 print 'Credit card</TD></TR>',
196 '<TR><TD ALIGN="right">Card number</TD><TD BGCOLOR="#ffffff">',
197 $cust_main->payinfo, '</TD></TR>',
198 '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
199 $cust_main->paydate, '</TD></TR>',
200 '<TR><TD ALIGN="right">Name on card</TD><TD BGCOLOR="#ffffff">',
201 $cust_main->payname, '</TD></TR>'
203 } elsif ( $cust_main->payby eq 'BILL' ) {
204 print 'Billing</TD></TR>';
205 print '<TR><TD ALIGN="right">P.O. </TD><TD BGCOLOR="#ffffff">',
206 $cust_main->payinfo, '</TD></TR>',
207 if $cust_main->payinfo;
208 print '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
209 $cust_main->paydate, '</TD></TR>',
210 '<TR><TD ALIGN="right">Attention</TD><TD BGCOLOR="#ffffff">',
211 $cust_main->payname, '</TD></TR>',
213 } elsif ( $cust_main->payby eq 'COMP' ) {
214 print 'Complimentary</TD></TR>',
215 '<TR><TD ALIGN="right">Authorized by</TD><TD BGCOLOR="#ffffff">',
216 $cust_main->payinfo, '</TD></TR>',
217 '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
218 $cust_main->paydate, '</TD></TR>',
222 print "</TABLE></TD></TR></TABLE></TD></TR></TABLE>";
224 print qq!<BR><BR><A NAME="cust_pkg">Packages</A> !,
225 # qq!<BR>Click on package number to view/edit package.!,
226 qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> )!,
232 print qq!!, table, "\n",
233 qq!<TR><TH COLSPAN=2 ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
234 qq!Dates</TH><TH COLSPAN=2 ROWSPAN=2>Services</TH></TR>\n!,
235 qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
236 qq!<FONT SIZE=-1>Next bill</FONT>!,
237 qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
239 qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
243 @packages = $cust_main->all_pkgs;
244 #@packages = $cust_main->ncancelled_pkgs;
247 foreach $package (@packages) {
248 my $pkgnum = $package->pkgnum;
249 my $pkg = $package->part_pkg->pkg;
250 my $comment = $package->part_pkg->comment;
251 my $pkgview = popurl(2). "view/cust_pkg.cgi?$pkgnum";
252 my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
253 my $rowspan = scalar(@cust_svc) || 1;
255 my $button_cgi = new CGI;
256 $button_cgi->param('clone', $package->part_pkg->pkgpart);
257 $button_cgi->param('pkgnum', $package->pkgnum);
258 my $button_url = popurl(2). "edit/part_pkg.cgi?". $button_cgi->query_string;
260 #print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview">$pkgnum</A></TD>!,
261 print $n1, qq!<TD ROWSPAN=$rowspan>$pkgnum</TD>!,
262 qq!<TD ROWSPAN=$rowspan><FONT SIZE=-1>!,
263 #qq!<A HREF="$pkgview">$pkg - $comment</A>!,
265 qq! ( <A HREF="$pkgview">Edit</A> | <A HREF="$button_url">Customize pricing</A> )</FONT></TD>!,
267 for ( qw( setup bill susp expire cancel ) ) {
268 print "<TD ROWSPAN=$rowspan><FONT SIZE=-1>", ( $package->getfield($_)
269 ? time2str("%D", $package->getfield($_) )
276 foreach my $cust_svc ( @cust_svc ) {
277 my($label, $value, $svcdb) = $cust_svc->label;
278 my($svcnum) = $cust_svc->svcnum;
279 my($sview) = popurl(2). "/view";
280 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
281 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
292 print qq!<BR><BR><A NAME="history">Payment History!,
294 qq! ( Click on invoice to view invoice/enter payment. | !,
295 qq!<A HREF="!, popurl(2), qq!edit/cust_credit.cgi?$custnum">!,
296 qq!Post credit / refund</A> )!;
300 # major problem: this whole thing is way too sloppy.
301 # minor problem: the description lines need better formatting.
303 @history = (); #needed for mod_perl :)
305 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
306 foreach $bill (@bills) {
307 my($bref)=$bill->hashref;
309 $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!view/cust_bill.cgi?! .
310 $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
311 qq! (Balance \$! . $bref->{owed} . qq!)</A>\t! .
312 $bref->{charged} . qq!\t\t\t!;
314 my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
316 foreach $payment (@payments) {
317 my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
318 $payment->getfield('invnum'),
319 $payment->getfield('payby'),
320 $payment->getfield('payinfo'),
321 $payment->getfield('paid'),
324 "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
328 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
329 foreach $credit (@credits) {
330 my($cref)=$credit->hashref;
332 $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
333 $cref->{credited} . ") by " . $cref->{otaker} . " - " .
334 $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
336 my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
338 foreach $refund (@refunds) {
339 my($rref)=$refund->hashref;
341 $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
342 $rref->{payby} . " " . $rref->{payinfo} . ") by " .
343 $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
349 print table(), <<END;
353 <TH><FONT SIZE=-1>Charge</FONT></TH>
354 <TH><FONT SIZE=-1>Payment</FONT></TH>
355 <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
356 <TH><FONT SIZE=-1>Refund</FONT></TH>
357 <TH><FONT SIZE=-1>Balance</FONT></TH>
361 #display payment history
364 foreach $item (sort keyfield_numerically @history) {
365 my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
370 $balance += $charge - $payment;
371 $balance -= $credit - $refund;
373 print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
374 "<TD><FONT SIZE=-1>$desc</FONT></TD>",
375 "<TD><FONT SIZE=-1>",
376 ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
378 "<TD><FONT SIZE=-1>",
379 ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
381 "<TD><FONT SIZE=-1>",
382 ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
384 "<TD><FONT SIZE=-1>",
385 ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
387 "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
405 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }