3 # $Id: cust_main.cgi,v 1.20 2001-06-03 11:40:48 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.20 2001-06-03 11:40:48 ivan
35 # inline doc clarification
37 # Revision 1.19 2001/04/22 01:38:39 ivan
38 # svc_domain needs to import dbh sub from Record
39 # view/cust_main.cgi needs to use ->owed method, not check (depriciated) owed field
40 # search/cust_bill.cgi redirect error when there's only one invoice
42 # Revision 1.18 1999/08/12 04:16:01 ivan
43 # hidecancelledpackages config option
45 # Revision 1.17 1999/04/15 16:44:36 ivan
48 # Revision 1.16 1999/04/09 04:22:34 ivan
51 # Revision 1.15 1999/04/09 03:52:55 ivan
52 # explicit & for table/itable/ntable
54 # Revision 1.14 1999/04/08 04:04:37 ivan
55 # eliminate double // in links
57 # Revision 1.13 1999/02/28 00:04:00 ivan
58 # removed misleading comments
60 # Revision 1.12 1999/02/07 09:59:40 ivan
61 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
63 # Revision 1.11 1999/01/25 12:26:04 ivan
64 # yet more mod_perl stuff
66 # Revision 1.10 1999/01/19 05:14:19 ivan
67 # for mod_perl: no more top-level my() variables; use vars instead
68 # also the last s/create/new/;
70 # Revision 1.9 1999/01/18 09:41:43 ivan
71 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
74 # Revision 1.8 1999/01/18 09:22:35 ivan
75 # changes to track email addresses for email invoicing
77 # Revision 1.7 1998/12/30 23:03:34 ivan
78 # bugfixes; fields isn't exported by derived classes
80 # Revision 1.6 1998/12/23 02:42:33 ivan
81 # remove double '/' in link urls
83 # Revision 1.5 1998/12/23 02:36:28 ivan
84 # use FS::cust_refund; to eliminate warning
86 # Revision 1.4 1998/12/17 09:57:21 ivan
87 # s/CGI::(Base|Request)/CGI.pm/;
89 # Revision 1.3 1998/11/15 13:14:20 ivan
90 # first pass as per-customer custom pricing
92 # Revision 1.2 1998/11/13 11:28:08 ivan
93 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
97 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral
98 @packages $package @history @bills $bill @credits $credit
99 $balance $item @agents @referrals @invoicing_list $n1 $conf );
101 use CGI::Carp qw(fatalsToBrowser);
103 use FS::UID qw(cgisuidsetup);
104 use FS::Record qw(qsearchs qsearch);
105 use FS::CGI qw(header menubar popurl table itable ntable);
111 use FS::part_referral;
119 $conf = new FS::Conf;
121 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
122 'Main Menu' => popurl(2)
125 die "No customer specified (bad URL)!" unless $cgi->keywords;
126 ($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
129 $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
130 die "Customer not found!" unless $cust_main;
131 $hashref = $cust_main->hashref;
133 print &itable(), '<TR><TD><A NAME="cust_main"></A>';
135 print qq!<A HREF="!, popurl(2),
136 qq!edit/cust_main.cgi?$custnum">Edit this customer</A>!;
137 print qq! | <A HREF="!, popurl(2),
138 qq!misc/delete-customer.cgi?$custnum"> Delete this customer</A>!
139 if $conf->exists('deletecustomers');
140 print &ntable("#c0c0c0"), "<TR><TD>", &ntable("#c0c0c0",2),
141 '<TR><TD ALIGN="right">Customer number</TD><TD BGCOLOR="#ffffff">',
142 $custnum, '</TD></TR>',
145 @agents = qsearch( 'agent', {} );
146 unless ( scalar(@agents) == 1 ) {
147 $agent = qsearchs('agent',{
148 'agentnum' => $cust_main->agentnum
150 print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
151 $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
153 @referrals = qsearch( 'part_referral', {} );
154 unless ( scalar(@referrals) == 1 ) {
155 my $referral = qsearchs('part_referral', {
156 'refnum' => $cust_main->refnum
158 print '<TR><TD ALIGN="right">Referral</TD><TD BGCOLOR="#ffffff">',
159 $referral->refnum, ": ", $referral->referral, '</TD></TR>';
161 print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
162 $cust_main->otaker, '</TD></TR>';
164 print '</TABLE></TD></TR></TABLE>';
166 print '</TD><TD ROWSPAN=2>';
168 print "Contact information", &ntable("#c0c0c0"), "<TR><TD>",
169 &ntable("#c0c0c0",2),
170 '<TR><TD ALIGN="right">Contact name<BR>(last, first)</TD>',
171 '<TD COLSPAN=3 BGCOLOR="#ffffff">',
172 $cust_main->last, ', ', $cust_main->first,
173 '</TD><TD ALIGN="right">SS#</TD><TD BGCOLOR="#ffffff">',
174 $cust_main->ss || ' ', '</TD></TR>',
175 '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
178 '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
179 $cust_main->address1,
182 print '<TR><TD ALIGN="right"> </TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
183 $cust_main->address2, '</TD></TR>'
184 if $cust_main->address2;
185 print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
187 '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
189 '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
190 $cust_main->zip, '</TD></TR>',
191 '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
195 print '<TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
196 $cust_main->daytime || ' ', '</TD></TR>',
197 '<TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
198 $cust_main->night || ' ', '</TD></TR>',
199 '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
200 $cust_main->fax || ' ', '</TD></TR>',
201 '</TABLE>', "</TD></TR></TABLE>"
204 print '</TD></TR><TR><TD>';
206 @invoicing_list = $cust_main->invoicing_list;
207 print "Billing information (",
208 qq!<A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!, "Bill now</A>)",
209 &ntable("#c0c0c0"), "<TR><TD>", &ntable("#c0c0c0",2),
210 '<TR><TD ALIGN="right">Tax exempt</TD><TD BGCOLOR="#ffffff">',
211 $cust_main->tax ? 'yes' : 'no',
213 '<TR><TD ALIGN="right">Postal invoices</TD><TD BGCOLOR="#ffffff">',
214 ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
216 '<TR><TD ALIGN="right">Email invoices</TD><TD BGCOLOR="#ffffff">',
217 join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
219 '<TR><TD ALIGN="right">Billing type</TD><TD BGCOLOR="#ffffff">',
222 if ( $cust_main->payby eq 'CARD' ) {
223 print 'Credit card</TD></TR>',
224 '<TR><TD ALIGN="right">Card number</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 '<TR><TD ALIGN="right">Name on card</TD><TD BGCOLOR="#ffffff">',
229 $cust_main->payname, '</TD></TR>'
231 } elsif ( $cust_main->payby eq 'BILL' ) {
232 print 'Billing</TD></TR>';
233 print '<TR><TD ALIGN="right">P.O. </TD><TD BGCOLOR="#ffffff">',
234 $cust_main->payinfo, '</TD></TR>',
235 if $cust_main->payinfo;
236 print '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
237 $cust_main->paydate, '</TD></TR>',
238 '<TR><TD ALIGN="right">Attention</TD><TD BGCOLOR="#ffffff">',
239 $cust_main->payname, '</TD></TR>',
241 } elsif ( $cust_main->payby eq 'COMP' ) {
242 print 'Complimentary</TD></TR>',
243 '<TR><TD ALIGN="right">Authorized by</TD><TD BGCOLOR="#ffffff">',
244 $cust_main->payinfo, '</TD></TR>',
245 '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
246 $cust_main->paydate, '</TD></TR>',
250 print "</TABLE></TD></TR></TABLE></TD></TR></TABLE>";
252 print qq!<BR><BR><A NAME="cust_pkg">Packages</A> !,
253 # qq!<BR>Click on package number to view/edit package.!,
254 qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> (preserves services) )!,
260 print qq!!, &table(), "\n",
261 qq!<TR><TH COLSPAN=2 ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
262 qq!Dates</TH><TH COLSPAN=2 ROWSPAN=2>Services</TH></TR>\n!,
263 qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
264 qq!<FONT SIZE=-1>Next bill</FONT>!,
265 qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
267 qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
271 if ( $conf->exists('hidecancelledpackages') ) {
272 @packages = $cust_main->ncancelled_pkgs;
274 @packages = $cust_main->all_pkgs;
278 foreach $package (@packages) {
279 my $pkgnum = $package->pkgnum;
280 my $pkg = $package->part_pkg->pkg;
281 my $comment = $package->part_pkg->comment;
282 my $pkgview = popurl(2). "view/cust_pkg.cgi?$pkgnum";
283 my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
284 my $rowspan = scalar(@cust_svc) || 1;
286 my $button_cgi = new CGI;
287 $button_cgi->param('clone', $package->part_pkg->pkgpart);
288 $button_cgi->param('pkgnum', $package->pkgnum);
289 my $button_url = popurl(2). "edit/part_pkg.cgi?". $button_cgi->query_string;
291 #print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview">$pkgnum</A></TD>!,
292 print $n1, qq!<TD ROWSPAN=$rowspan>$pkgnum</TD>!,
293 qq!<TD ROWSPAN=$rowspan><FONT SIZE=-1>!,
294 #qq!<A HREF="$pkgview">$pkg - $comment</A>!,
296 qq! ( <A HREF="$pkgview">Edit</A> | <A HREF="$button_url">Customize pricing</A> )</FONT></TD>!,
298 for ( qw( setup bill susp expire cancel ) ) {
299 print "<TD ROWSPAN=$rowspan><FONT SIZE=-1>", ( $package->getfield($_)
300 ? time2str("%D", $package->getfield($_) )
307 foreach my $cust_svc ( @cust_svc ) {
308 my($label, $value, $svcdb) = $cust_svc->label;
309 my($svcnum) = $cust_svc->svcnum;
310 my($sview) = popurl(2). "view";
311 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
312 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
323 print qq!<BR><BR><A NAME="history">Payment History!,
325 qq! ( Click on invoice to view invoice/enter payment. | !,
326 qq!<A HREF="!, popurl(2), qq!edit/cust_credit.cgi?$custnum">!,
327 qq!Post credit / refund</A> )!;
331 # major problem: this whole thing is way too sloppy.
332 # minor problem: the description lines need better formatting.
334 @history = (); #needed for mod_perl :)
336 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
337 foreach $bill (@bills) {
338 my($bref)=$bill->hashref;
340 $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!view/cust_bill.cgi?! .
341 $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
342 qq! (Balance \$! . $bill->owed . qq!)</A>\t! .
343 $bref->{charged} . qq!\t\t\t!;
345 my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
347 foreach $payment (@payments) {
348 my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
349 $payment->getfield('invnum'),
350 $payment->getfield('payby'),
351 $payment->getfield('payinfo'),
352 $payment->getfield('paid'),
355 "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
359 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
360 foreach $credit (@credits) {
361 my($cref)=$credit->hashref;
363 $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
364 $cref->{credited} . ") by " . $cref->{otaker} . " - " .
365 $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
367 my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
369 foreach $refund (@refunds) {
370 my($rref)=$refund->hashref;
372 $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
373 $rref->{payby} . " " . $rref->{payinfo} . ") by " .
374 $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
380 print &table(), <<END;
384 <TH><FONT SIZE=-1>Charge</FONT></TH>
385 <TH><FONT SIZE=-1>Payment</FONT></TH>
386 <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
387 <TH><FONT SIZE=-1>Refund</FONT></TH>
388 <TH><FONT SIZE=-1>Balance</FONT></TH>
392 #display payment history
395 foreach $item (sort keyfield_numerically @history) {
396 my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
401 $balance += $charge - $payment;
402 $balance -= $credit - $refund;
404 print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
405 "<TD><FONT SIZE=-1>$desc</FONT></TD>",
406 "<TD><FONT SIZE=-1>",
407 ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
409 "<TD><FONT SIZE=-1>",
410 ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
412 "<TD><FONT SIZE=-1>",
413 ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
415 "<TD><FONT SIZE=-1>",
416 ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
418 "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
436 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }