2 #<!-- $Id: cust_main.cgi,v 1.6 2001-09-01 21:52:20 jeff Exp $ -->
5 use vars qw ( $cgi $query $custnum $cust_main $hashref $agent $referral
6 @packages $package @history @bills $bill @credits $credit
7 $balance $item @agents @referrals @invoicing_list $n1 $conf );
9 use CGI::Carp qw(fatalsToBrowser);
11 use FS::UID qw(cgisuidsetup);
12 use FS::Record qw(qsearchs qsearch);
13 use FS::CGI qw(header menubar popurl table itable ntable);
19 use FS::part_referral;
29 print $cgi->header( '-expires' => 'now' ), header("Customer View", menubar(
30 'Main Menu' => popurl(2)
33 die "No customer specified (bad URL)!" unless $cgi->keywords;
34 ($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
37 $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
38 die "Customer not found!" unless $cust_main;
39 $hashref = $cust_main->hashref;
41 print qq!<A HREF="!, popurl(2),
42 qq!edit/cust_main.cgi?$custnum">Edit this customer</A>!;
43 print qq! | <A HREF="!, popurl(2),
44 qq!misc/delete-customer.cgi?$custnum"> Delete this customer</A>!
45 if $conf->exists('deletecustomers');
47 unless ( $conf->exists('disable_customer_referrals') ) {
48 print qq! | <A HREF="!, popurl(2),
49 qq!edit/cust_main.cgi?referral_custnum=$custnum">!,
50 qq!Refer a new customer</A>!;
52 print qq! | <A HREF="!, popurl(2),
53 qq!search/cust_main.cgi?referral_custnum=$custnum">!,
54 qq!View this customer's referrals<A>!;
58 print '<A NAME="cust_main"></A>';
60 print &itable(), '<TR>';
62 print '<TD VALIGN="top">';
64 print "Billing address", &ntable("#cccccc"), "<TR><TD>",
66 '<TR><TD ALIGN="right">Contact name</TD>',
67 '<TD COLSPAN=3 BGCOLOR="#ffffff">',
68 $cust_main->last, ', ', $cust_main->first,
69 '</TD><TD ALIGN="right">SS#</TD><TD BGCOLOR="#ffffff">',
70 $cust_main->ss || ' ', '</TD></TR>',
71 '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
74 '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
78 print '<TR><TD ALIGN="right"> </TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
79 $cust_main->address2, '</TD></TR>'
80 if $cust_main->address2;
81 print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
83 '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
85 '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
86 $cust_main->zip, '</TD></TR>',
87 '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
91 print '<TR><TD ALIGN="right">Day Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
92 $cust_main->daytime || ' ', '</TD></TR>',
93 '<TR><TD ALIGN="right">Night Phone</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
94 $cust_main->night || ' ', '</TD></TR>',
95 '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
96 $cust_main->fax || ' ', '</TD></TR>',
97 '</TABLE>', "</TD></TR></TABLE>"
100 if ( defined $cust_main->dbdef_table->column('ship_last') ) {
102 my $pre = $cust_main->ship_last ? 'ship_' : '';
104 print "<BR>Service address", &ntable("#cccccc"), "<TR><TD>",
105 &ntable("#cccccc",2),
106 '<TR><TD ALIGN="right">Contact name</TD>',
107 '<TD COLSPAN=5 BGCOLOR="#ffffff">',
108 $cust_main->get("${pre}last"), ', ', $cust_main->get("${pre}first"),
110 '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
111 $cust_main->get("${pre}company"),
113 '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
114 $cust_main->get("${pre}address1"),
117 print '<TR><TD ALIGN="right"> </TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
118 $cust_main->get("${pre}address2"), '</TD></TR>'
119 if $cust_main->get("${pre}address2");
120 print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
121 $cust_main->get("${pre}city"),
122 '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
123 $cust_main->get("${pre}state"),
124 '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
125 $cust_main->get("${pre}zip"), '</TD></TR>',
126 '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
127 $cust_main->get("${pre}country"),
130 print '<TR><TD ALIGN="right">Day Phone</TD>',
131 '<TD COLSPAN=5 BGCOLOR="#ffffff">',
132 $cust_main->get("${pre}daytime") || ' ', '</TD></TR>',
133 '<TR><TD ALIGN="right">Night Phone</TD>'.
134 '<TD COLSPAN=5 BGCOLOR="#ffffff">',
135 $cust_main->get("${pre}night") || ' ', '</TD></TR>',
136 '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
137 $cust_main->get("${pre}fax") || ' ', '</TD></TR>',
138 '</TABLE>', "</TD></TR></TABLE>"
145 print '<TD VALIGN="top">';
147 print &ntable("#cccccc"), "<TR><TD>", &ntable("#cccccc",2),
148 '<TR><TD ALIGN="right">Customer number</TD><TD BGCOLOR="#ffffff">',
149 $custnum, '</TD></TR>',
152 @agents = qsearch( 'agent', {} );
153 unless ( scalar(@agents) == 1 ) {
154 $agent = qsearchs('agent',{
155 'agentnum' => $cust_main->agentnum
157 print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
158 $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
160 @referrals = qsearch( 'part_referral', {} );
161 unless ( scalar(@referrals) == 1 ) {
162 my $referral = qsearchs('part_referral', {
163 'refnum' => $cust_main->refnum
165 print '<TR><TD ALIGN="right">Referral</TD><TD BGCOLOR="#ffffff">',
166 $referral->refnum, ": ", $referral->referral, '</TD></TR>';
168 print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
169 $cust_main->otaker, '</TD></TR>';
171 print '<TR><TD ALIGN="right">Referring Customer</TD><TD BGCOLOR="#ffffff">';
172 my $referring_cust_main = '';
173 if ( $cust_main->referral_custnum
174 && ( $referring_cust_main =
175 qsearchs('cust_main', { custnum => $cust_main->referral_custnum } )
178 print '<A HREF="'. popurl(1). 'cust_main.cgi?'.
179 $cust_main->referral_custnum. '">'.
180 $cust_main->referral_custnum. ': '.
181 ( $referring_cust_main->company
182 || $referring_cust_main->last. ', '. $referring_cust_main->first ).
187 print '</TABLE></TD></TR></TABLE>';
191 @invoicing_list = $cust_main->invoicing_list;
192 print "Billing information (",
193 qq!<A HREF="!, popurl(2), qq!/misc/bill.cgi?$custnum">!, "Bill now</A>)",
194 &ntable("#cccccc"), "<TR><TD>", &ntable("#cccccc",2),
195 '<TR><TD ALIGN="right">Tax exempt</TD><TD BGCOLOR="#ffffff">',
196 $cust_main->tax ? 'yes' : 'no',
198 '<TR><TD ALIGN="right">Postal invoices</TD><TD BGCOLOR="#ffffff">',
199 ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
201 '<TR><TD ALIGN="right">Email invoices</TD><TD BGCOLOR="#ffffff">',
202 join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
204 '<TR><TD ALIGN="right">Billing type</TD><TD BGCOLOR="#ffffff">',
207 if ( $cust_main->payby eq 'CARD' ) {
208 print 'Credit card</TD></TR>',
209 '<TR><TD ALIGN="right">Card number</TD><TD BGCOLOR="#ffffff">',
210 $cust_main->payinfo, '</TD></TR>',
211 '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
212 $cust_main->paydate, '</TD></TR>',
213 '<TR><TD ALIGN="right">Name on card</TD><TD BGCOLOR="#ffffff">',
214 $cust_main->payname, '</TD></TR>'
216 } elsif ( $cust_main->payby eq 'BILL' ) {
217 print 'Billing</TD></TR>';
218 print '<TR><TD ALIGN="right">P.O. </TD><TD BGCOLOR="#ffffff">',
219 $cust_main->payinfo, '</TD></TR>',
220 if $cust_main->payinfo;
221 print '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
222 $cust_main->paydate, '</TD></TR>',
223 '<TR><TD ALIGN="right">Attention</TD><TD BGCOLOR="#ffffff">',
224 $cust_main->payname, '</TD></TR>',
226 } elsif ( $cust_main->payby eq 'COMP' ) {
227 print 'Complimentary</TD></TR>',
228 '<TR><TD ALIGN="right">Authorized by</TD><TD BGCOLOR="#ffffff">',
229 $cust_main->payinfo, '</TD></TR>',
230 '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
231 $cust_main->paydate, '</TD></TR>',
235 print "</TABLE></TD></TR></TABLE>";
237 print '</TD></TR></TABLE>';
239 if ( defined $cust_main->dbdef_table->column('comments')
240 && $cust_main->comments )
242 print "<BR>Comments", &ntable("#cccccc"), "<TR><TD>",
243 &ntable("#cccccc",2),
244 '<TR><TD BGCOLOR="#ffffff"><PRE>', $cust_main->comments,
245 '</PRE></TD></TR></TABLE></TABLE>';
248 print '</TD></TR></TABLE>';
250 print qq!<BR><A NAME="cust_pkg">Packages</A> !,
251 # qq!<BR>Click on package number to view/edit package.!,
252 qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> (preserves services) )!,
258 print qq!!, &table(), "\n",
259 qq!<TR><TH COLSPAN=2 ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
260 qq!Dates</TH><TH COLSPAN=2 ROWSPAN=2>Services</TH></TR>\n!,
261 qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
262 qq!<FONT SIZE=-1>Next bill</FONT>!,
263 qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
265 qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
269 if ( $conf->exists('hidecancelledpackages') ) {
270 @packages = $cust_main->ncancelled_pkgs;
272 @packages = $cust_main->all_pkgs;
276 foreach $package (@packages) {
277 my $pkgnum = $package->pkgnum;
278 my $pkg = $package->part_pkg->pkg;
279 my $comment = $package->part_pkg->comment;
280 my $pkgview = popurl(2). "view/cust_pkg.cgi?$pkgnum";
281 my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
282 my $rowspan = scalar(@cust_svc) || 1;
284 my $button_cgi = new CGI;
285 $button_cgi->param('clone', $package->part_pkg->pkgpart);
286 $button_cgi->param('pkgnum', $package->pkgnum);
287 my $button_url = popurl(2). "edit/part_pkg.cgi?". $button_cgi->query_string;
289 #print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview">$pkgnum</A></TD>!,
290 print $n1, qq!<TD ROWSPAN=$rowspan>$pkgnum</TD>!,
291 qq!<TD ROWSPAN=$rowspan><FONT SIZE=-1>!,
292 #qq!<A HREF="$pkgview">$pkg - $comment</A>!,
294 qq! ( <A HREF="$pkgview">Edit</A> | <A HREF="$button_url">Customize pricing</A> )</FONT></TD>!,
296 for ( qw( setup bill susp expire cancel ) ) {
297 print "<TD ROWSPAN=$rowspan><FONT SIZE=-1>", ( $package->getfield($_)
298 ? time2str("%D", $package->getfield($_) )
305 foreach my $cust_svc ( @cust_svc ) {
306 my($label, $value, $svcdb) = $cust_svc->label;
307 my($svcnum) = $cust_svc->svcnum;
308 my($sview) = popurl(2). "view";
309 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
310 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
321 print qq!<BR><BR><A NAME="history">Payment History!,
323 qq! ( Click on invoice to view invoice/enter payment. | !,
324 qq!<A HREF="!, popurl(2), qq!edit/cust_credit.cgi?$custnum">!,
325 qq!Post credit / refund</A> )!;
329 # major problem: this whole thing is way too sloppy.
330 # minor problem: the description lines need better formatting.
332 @history = (); #needed for mod_perl :)
334 @bills = qsearch('cust_bill',{'custnum'=>$custnum});
335 foreach $bill (@bills) {
336 my($bref)=$bill->hashref;
338 $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!view/cust_bill.cgi?! .
339 $bref->{invnum} . qq!">Invoice #! . $bref->{invnum} .
340 qq! (Balance \$! . $bill->owed . qq!)</A>\t! .
341 $bref->{charged} . qq!\t\t\t!;
343 my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
345 foreach $payment (@payments) {
346 my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'),
347 $payment->getfield('invnum'),
348 $payment->getfield('payby'),
349 $payment->getfield('payinfo'),
350 $payment->getfield('paid'),
353 "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t";
357 @credits = qsearch('cust_credit',{'custnum'=>$custnum});
358 foreach $credit (@credits) {
359 my($cref)=$credit->hashref;
360 my($credited)=$credit->credited;
362 $cref->{_date} . "\t" .
364 (qq!<A HREF="! . popurl(2). qq!edit/cust_credit_bill.cgi?!. $cref->{crednum} . qq!">!) :
367 $cref->{crednum} . ", (Balance \$" .
368 $credited . ")" . ($credited ? "</A>" : "") .
369 $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t";
371 my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } );
373 foreach $refund (@refunds) {
374 my($rref)=$refund->hashref;
376 $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" .
377 $rref->{payby} . " " . $rref->{payinfo} . ") by " .
378 $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
384 print &table(), <<END;
388 <TH><FONT SIZE=-1>Charge</FONT></TH>
389 <TH><FONT SIZE=-1>Payment</FONT></TH>
390 <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
391 <TH><FONT SIZE=-1>Refund</FONT></TH>
392 <TH><FONT SIZE=-1>Balance</FONT></TH>
396 #display payment history
399 foreach $item (sort keyfield_numerically @history) {
400 my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item);
405 $balance += $charge - $payment;
406 $balance -= $credit - $refund;
408 print "<TR><TD><FONT SIZE=-1>",time2str("%D",$date),"</FONT></TD>",
409 "<TD><FONT SIZE=-1>$desc</FONT></TD>",
410 "<TD><FONT SIZE=-1>",
411 ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
413 "<TD><FONT SIZE=-1>",
414 ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ),
416 "<TD><FONT SIZE=-1>",
417 ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ),
419 "<TD><FONT SIZE=-1>",
420 ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
422 "<TD><FONT SIZE=-1>\$" . sprintf("%.2f",$balance),
440 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }