2 #<!-- $Id: cust_main.cgi,v 1.18 2001-12-03 11:33:19 ivan Exp $ -->
5 #use vars qw( $conf %ncancelled_pkgs %all_pkgs $cgi @cust_main $sortby );
6 #use vars qw( $conf %all_pkgs $cgi @cust_main $sortby );
7 use vars qw( $conf %all_pkgs $cgi @cust_main $sortby
8 $orderby $maxrecords $limit $offset );
10 use CGI::Carp qw(fatalsToBrowser);
12 use String::Approx qw(amatch);
13 use FS::UID qw(dbh cgisuidsetup);
15 use FS::Record qw(qsearch qsearchs dbdef jsearch);
16 use FS::CGI qw(header menubar eidiot popurl table);
24 $maxrecords = $conf->config('maxsearchrecordsperpage');
28 #my $monsterjoin = <<END;
29 #cust_main left outer join (
30 # ( cust_pkg left outer join part_pkg using(pkgpart)
34 # ( cust_svc left outer join part_svc using (svcpart)
35 # ) left outer join svc_acct using (svcnum)
36 # ) left outer join svc_domain using(svcnum)
37 # ) left outer join svc_forward using(svcnum)
42 #my $monsterjoin = <<END;
43 #cust_main left outer join (
44 # ( cust_pkg left outer join part_pkg using(pkgpart)
48 # ( cust_svc left outer join part_svc using (svcpart)
50 # svc_acct left outer join (
51 # select svcnum, domain, catchall from svc_domain
52 # ) as svc_acct_domsvc (
53 # svc_acct_svcnum, svc_acct_domain, svc_acct_catchall
54 # ) on svc_acct.domsvc = svc_acct_domsvc.svc_acct_svcnum
56 # ) left outer join svc_domain using(svcnum)
57 # ) left outer join svc_forward using(svcnum)
62 $orderby = ''; #removeme
65 $limit .= "LIMIT $maxrecords" if $maxrecords;
67 $offset = $cgi->param('offset') || 0;
68 $limit .= " OFFSET $offset" if $offset;
72 if ( $cgi->param('browse') ) {
73 my $query = $cgi->param('browse');
74 if ( $query eq 'custnum' ) {
75 $sortby=\*custnum_sort;
76 $orderby = 'ORDER BY custnum';
77 } elsif ( $query eq 'last' ) {
79 $orderby = 'ORDER BY last';
80 } elsif ( $query eq 'company' ) {
81 $sortby=\*company_sort;
82 $orderby = 'ORDER BY company';
84 die "unknown browse field $query";
89 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
90 || ( $conf->exists('hidecancelledcustomers')
91 && ! $cgi->param('showcancelledcustomers') )
93 #grep { $_->ncancelled_pkgs || ! $_->all_pkgs }
94 #needed for MySQL??? OR cust_pkg.cancel = \"\"
96 WHERE 0 < ( SELECT COUNT(*) FROM cust_pkg
97 WHERE cust_pkg.custnum = cust_main.custnum
98 AND ( cust_pkg.cancel IS NULL
99 OR cust_pkg.cancel = 0
102 OR 0 = ( SELECT COUNT(*) FROM cust_pkg
103 WHERE cust_pkg.custnum = cust_main.custnum
108 my $statement = "SELECT COUNT(*) FROM cust_main $ncancelled";
109 my $sth = dbh->prepare($statement)
110 or die dbh->errstr. " doing $statement";
111 $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
113 $total = $sth->fetchrow_arrayref->[0];
115 my @just_cust_main = qsearch('cust_main',{}, '',
116 "$ncancelled $orderby $limit"
119 @cust_main = @just_cust_main;
121 # foreach my $cust_main ( @just_cust_main ) {
124 # $FS::Record::DEBUG=1;
125 # ( $cache, @one_cust_main ) = jsearch(
127 # { 'custnum' => $cust_main->custnum },
133 # push @cust_main, @one_cust_main;
138 &cardsearch if $cgi->param('card_on') && $cgi->param('card');
139 &lastsearch if $cgi->param('last_on') && $cgi->param('last_text');
140 &companysearch if $cgi->param('company_on') && $cgi->param('company_text');
141 &referralsearch if $cgi->param('referral_custnum');
143 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
144 if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
145 || ( $conf->exists('hidecancelledcustomers')
146 && ! $cgi->param('showcancelledcustomers') );
149 if ( $conf->exists('hidecancelledpackages' ) ) {
150 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
152 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
156 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
157 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
159 } elsif ( scalar(@cust_main) == 0 ) {
160 eidiot "No matching customers found!\n";
163 $total ||= scalar(@cust_main);
164 print header("Customer Search Results",menubar(
165 'Main Menu', popurl(2)
166 )), "$total matching customers found ";
170 if ( $total != scalar(@cust_main) && $maxrecords ) {
171 unless ( $offset == 0 ) {
172 $cgi->param('offset', $offset - $maxrecords);
173 $pager .= '<A HREF="'. $cgi->self_url.
174 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
178 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
180 if ( $offset == $poff ) {
181 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
183 $cgi->param('offset', $poff);
184 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
187 unless ( $offset + $maxrecords > $total ) {
188 $cgi->param('offset', $offset + $maxrecords);
189 $pager .= '<A HREF="'. $cgi->self_url.
190 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
195 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
196 || ( $conf->exists('hidecancelledcustomers')
197 && ! $cgi->param('showcancelledcustomers')
200 $cgi->param('showcancelledcustomers', 1);
201 $cgi->param('offset', 0);
202 print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
204 $cgi->param('showcancelledcustomers', 0);
205 $cgi->param('offset', 0);
206 print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
208 if ( $cgi->param('referral_custnum') ) {
209 $cgi->param('referral_custnum') =~ /^(\d+)$/
210 or eidiot "Illegal referral_custnum\n";
211 my $referral_custnum = $1;
212 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
213 print '<FORM METHOD=POST>'.
214 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
215 'referrals of <A HREF="'. popurl(2).
216 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
217 ( $cust_main->company
218 || $cust_main->last. ', '. $cust_main->first ).
222 function changed(what) {
227 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
228 my $max = 8; #config file
229 $cgi->param('referral_depth') =~ /^(\d*)$/
230 or eidiot "Illegal referral_depth";
231 my $referral_depth = $1;
233 foreach my $depth ( 1 .. $max ) {
235 ' SELECTED'x($depth == $referral_depth),
238 print "</SELECT> levels deep".
239 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
243 print "<BR><BR>". $pager. &table(). <<END;
250 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
252 <TH>(service) name</TH>
259 <TH COLSPAN=2>Services</TH>
266 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
268 my($custnum,$last,$first,$company)=(
270 $cust_main->getfield('last'),
271 $cust_main->getfield('first'),
276 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
277 foreach ( @{$all_pkgs{$custnum}} ) {
278 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
279 my @cust_svc = $_->cust_svc;
280 push @lol_cust_svc, \@cust_svc;
281 $rowspan += scalar(@cust_svc) || 1;
284 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
285 my $view = $p. 'view/cust_main.cgi?'. $custnum;
288 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
289 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
290 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
292 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
293 my($ship_last,$ship_first,$ship_company)=(
294 $cust_main->ship_last || $cust_main->getfield('last'),
295 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
296 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
299 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
300 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A></TD>
305 foreach ( @{$all_pkgs{$custnum}} ) {
306 my $pkgnum = $_->pkgnum;
307 # my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $_->pkgpart } );
308 my $part_pkg = $_->part_pkg;
310 my $pkg = $part_pkg->pkg;
311 my $comment = $part_pkg->comment;
312 my $pkgview = $p. 'view/cust_pkg.cgi?'. $pkgnum;
313 my @cust_svc = @{shift @lol_cust_svc};
314 #my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
315 my $rowspan = scalar(@cust_svc) || 1;
317 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
319 foreach my $cust_svc ( @cust_svc ) {
320 my($label, $value, $svcdb) = $cust_svc->label;
321 my($svcnum) = $cust_svc->svcnum;
322 my($sview) = $p.'view';
323 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
324 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
327 #print qq!</TR><TR>\n!;
333 print "</TABLE>$pager</BODY></HTML>";
337 #undef $cache; #does this help?
342 $a->getfield('last') cmp $b->getfield('last');
346 return -1 if $a->company && ! $b->company;
347 return 1 if ! $a->company && $b->company;
348 $a->getfield('company') cmp $b->getfield('company');
352 $a->getfield('custnum') <=> $b->getfield('custnum');
357 my($card)=$cgi->param('card');
359 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
362 push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
367 $cgi->param('referral_custnum') =~ /^(\d+)$/
368 or eidiot "Illegal referral_custnum";
369 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
370 or eidiot "Customer $1 not found";
372 if ( $cgi->param('referral_depth') ) {
373 $cgi->param('referral_depth') =~ /^(\d+)$/
374 or eidiot "Illegal referral_depth";
379 push @cust_main, $cust_main->referral_cust_main($depth);
385 foreach ( $cgi->param('last_type') ) {
389 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
390 or eidiot "Illegal last name";
393 if ( $last_type{'Exact'}
394 && ! $last_type{'Fuzzy'}
395 # && ! $last_type{'Sound-alike'}
398 push @cust_main, qsearch('cust_main',{'last'=>$last});
400 push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
401 if defined dbdef->table('cust_main')->column('ship_last');
405 &FS::cust_main::check_and_rebuild_fuzzyfiles;
406 my $all_last = &FS::cust_main::all_last;
409 if ($last_type{'Fuzzy'}) {
410 foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
415 #if ($last_type{'Sound-alike'}) {
418 foreach ( keys %last ) {
419 push @cust_main, qsearch('cust_main',{'last'=>$_});
420 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
421 if defined dbdef->table('cust_main')->column('ship_last');
431 foreach ( $cgi->param('company_type') ) {
435 $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
436 or eidiot "Illegal company";
439 if ( $company_type{'Exact'}
440 && ! $company_type{'Fuzzy'}
441 # && ! $company_type{'Sound-alike'}
444 push @cust_main, qsearch('cust_main',{'company'=>$company});
446 push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
447 if defined dbdef->table('cust_main')->column('ship_last');
451 &FS::cust_main::check_and_rebuild_fuzzyfiles;
452 my $all_company = &FS::cust_main::all_company;
455 if ($company_type{'Fuzzy'}) {
456 foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
461 #if ($company_type{'Sound-alike'}) {
464 foreach ( keys %company ) {
465 push @cust_main, qsearch('cust_main',{'company'=>$_});
466 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
467 if defined dbdef->table('cust_main')->column('ship_last');
471 $sortby=\*company_sort;