3 # $Id: cust_main.cgi,v 1.19 2001-04-22 01:38:39 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.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
39 # Revision 1.18 1999/08/12 04:16:01 ivan
40 # hidecancelledpackages config option
42 # Revision 1.17 1999/04/15 16:44:36 ivan
45 # Revision 1.16 1999/04/09 04:22:34 ivan
48 # Revision 1.15 1999/04/09 03:52:55 ivan
49 # explicit & for table/itable/ntable
51 # Revision 1.14 1999/04/08 04:04:37 ivan
52 # eliminate double // in links
54 # Revision 1.13 1999/02/28 00:04:00 ivan
55 # removed misleading comments
57 # Revision 1.12 1999/02/07 09:59:40 ivan
58 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
60 # Revision 1.11 1999/01/25 12:26:04 ivan
61 # yet more mod_perl stuff
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/;
67 # Revision 1.9 1999/01/18 09:41:43 ivan
68 # all $cgi->header calls now include ( '-expires' => 'now' ) for mod_perl
71 # Revision 1.8 1999/01/18 09:22:35 ivan
72 # changes to track email addresses for email invoicing
74 # Revision 1.7 1998/12/30 23:03:34 ivan
75 # bugfixes; fields isn't exported by derived classes
77 # Revision 1.6 1998/12/23 02:42:33 ivan
78 # remove double '/' in link urls
80 # Revision 1.5 1998/12/23 02:36:28 ivan
81 # use FS::cust_refund; to eliminate warning
83 # Revision 1.4 1998/12/17 09:57:21 ivan
84 # s/CGI::(Base|Request)/CGI.pm/;
86 # Revision 1.3 1998/11/15 13:14:20 ivan
87 # first pass as per-customer custom pricing
89 # Revision 1.2 1998/11/13 11:28:08 ivan
90 # s/CGI-modules/CGI.pm/;, relative URL's with popurl
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 );
98 use CGI::Carp qw(fatalsToBrowser);
100 use FS::UID qw(cgisuidsetup);
101 use FS::Record qw(qsearchs qsearch);
102 use FS::CGI qw(header menubar popurl table itable ntable);
108 use FS::part_referral;
116 $conf = new FS::Conf;
118 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
119 'Main Menu' => popurl(2)
122 die "No customer specified (bad URL)!" unless $cgi->keywords;
123 ($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
126 $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
127 die "Customer not found!" unless $cust_main;
128 $hashref = $cust_main->hashref;
130 print &itable(), '<TR><TD><A NAME="cust_main"></A>';
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>',
142 @agents = qsearch( 'agent', {} );
143 unless ( scalar(@agents) == 1 ) {
144 $agent = qsearchs('agent',{
145 'agentnum' => $cust_main->agentnum
147 print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
148 $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
150 @referrals = qsearch( 'part_referral', {} );
151 unless ( scalar(@referrals) == 1 ) {
152 my $referral = qsearchs('part_referral', {
153 'refnum' => $cust_main->refnum
155 print '<TR><TD ALIGN="right">Referral</TD><TD BGCOLOR="#ffffff">',
156 $referral->refnum, ": ", $referral->referral, '</TD></TR>';
158 print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
159 $cust_main->otaker, '</TD></TR>';
161 print '</TABLE></TD></TR></TABLE>';
163 print '</TD><TD ROWSPAN=2>';
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 || ' ', '</TD></TR>',
172 '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
175 '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
176 $cust_main->address1,
179 print '<TR><TD ALIGN="right"> </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">',
184 '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
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">',
192 print '<TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
193 $cust_main->daytime || ' ', '</TD></TR>',
194 '<TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
195 $cust_main->night || ' ', '</TD></TR>',
196 '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
197 $cust_main->fax || ' ', '</TD></TR>',
198 '</TABLE>', "</TD></TR></TABLE>"
201 print '</TD></TR><TR><TD>';
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',
210 '<TR><TD ALIGN="right">Postal invoices</TD><TD BGCOLOR="#ffffff">',
211 ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
213 '<TR><TD ALIGN="right">Email invoices</TD><TD BGCOLOR="#ffffff">',
214 join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
216 '<TR><TD ALIGN="right">Billing type</TD><TD BGCOLOR="#ffffff">',
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>'
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>',
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>',
247 print "</TABLE></TD></TR></TABLE></TD></TR></TABLE>";
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> )!,
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!,
264 qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
268 if ( $conf->exists('hidecancelledpackages') ) {
269 @packages = $cust_main->ncancelled_pkgs;
271 @packages = $cust_main->all_pkgs;
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;
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;
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>!,
293 qq! ( <A HREF="$pkgview">Edit</A> | <A HREF="$button_url">Customize pricing</A> )</FONT></TD>!,
295 for ( qw( setup bill susp expire cancel ) ) {
296 print "<TD ROWSPAN=$rowspan><FONT SIZE=-1>", ( $package->getfield($_)
297 ? time2str("%D", $package->getfield($_) )
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>!;
320 print qq!<BR><BR><A NAME="history">Payment History!,
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> )!;
328 # major problem: this whole thing is way too sloppy.
329 # minor problem: the description lines need better formatting.
331 @history = (); #needed for mod_perl :)
333 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
334 foreach $bill (@bills) {
335 my($bref)=$bill->hashref;
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!;
342 my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
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'),
352 "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
356 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
357 foreach $credit (@credits) {
358 my($cref)=$credit->hashref;
360 $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" .
361 $cref->{credited} . ") by " . $cref->{otaker} . " - " .
362 $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
364 my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
366 foreach $refund (@refunds) {
367 my($rref)=$refund->hashref;
369 $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
370 $rref->{payby} . " " . $rref->{payinfo} . ") by " .
371 $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
377 print &table(), <<END;
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>
389 #display payment history
392 foreach $item (sort keyfield_numerically @history) {
393 my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
398 $balance += $charge - $payment;
399 $balance -= $credit - $refund;
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) : '' ),
406 "<TD><FONT SIZE=-1>",
407 ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
409 "<TD><FONT SIZE=-1>",
410 ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
412 "<TD><FONT SIZE=-1>",
413 ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
415 "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
433 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }