2 #<!-- $Id: cust_main.cgi,v 1.10 2001-10-10 06:22:22 thalakan 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 );
8 use CGI::Carp qw(fatalsToBrowser);
10 use String::Approx qw(amatch);
11 use FS::UID qw(cgisuidsetup);
12 use FS::Record qw(qsearch qsearchs dbdef);
13 use FS::CGI qw(header menubar eidiot popurl table);
22 if ( $cgi->param('browse') ) {
23 my $query = $cgi->param('browse');
24 if ( $query eq 'custnum' ) {
25 $sortby=\*custnum_sort;
26 @cust_main=qsearch('cust_main',{});
27 } elsif ( $query eq 'last' ) {
29 @cust_main=qsearch('cust_main',{});
30 } elsif ( $query eq 'company' ) {
31 $sortby=\*company_sort;
32 @cust_main=qsearch('cust_main',{});
34 die "unknown browse field $query";
38 &cardsearch if $cgi->param('card_on') && $cgi->param('card');
39 &lastsearch if $cgi->param('last_on') && $cgi->param('last_text');
40 &companysearch if $cgi->param('company_on') && $cgi->param('company_text');
41 &referralsearch if $cgi->param('referral_custnum');
44 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
45 if $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
46 || ( $conf->exists('hidecancelledcustomers')
47 && ! $cgi->param('showcancelledcustomers') );
48 if ( $conf->exists('hidecancelledpackages' ) ) {
49 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
51 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
54 if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) {
55 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
57 } elsif ( scalar(@cust_main) == 0 ) {
58 eidiot "No matching customers found!\n";
61 my($total)=scalar(@cust_main);
62 print $cgi->header( '-expires' => 'now' ), header("Customer Search Results",menubar(
63 'Main Menu', popurl(2)
64 )), "$total matching customers found ";
65 if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me
66 || ( $conf->exists('hidecancelledcustomers')
67 && ! $cgi->param('showcancelledcustomers')
70 $cgi->param('showcancelledcustomers', 1);
71 print qq!( <a href="!. $cgi->self_url. qq!">show cancelled customers</a> )!;
73 $cgi->param('showcancelledcustomers', 0);
74 print qq!( <a href="!. $cgi->self_url. qq!">hide cancelled customers</a> )!;
76 if ( $cgi->param('referral_custnum') ) {
77 $cgi->param('referral_custnum') =~ /^(\d+)$/
78 or eidiot "Illegal referral_custnum\n";
79 my $referral_custnum = $1;
80 my $cust_main = qsearchs('cust_main', { custnum => $referral_custnum } );
81 print '<FORM METHOD=POST>'.
82 qq!<INPUT TYPE="hidden" NAME="referral_custnum" VALUE="$referral_custnum">!.
83 'referrals of <A HREF="'. popurl(2).
84 "view/cust_main.cgi?$referral_custnum\">$referral_custnum: ".
86 || $cust_main->last. ', '. $cust_main->first ).
90 function changed(what) {
95 print ' <SELECT NAME="referral_depth" SIZE="1" onChange="changed(this)">';
96 my $max = 8; #config file
97 $cgi->param('referral_depth') =~ /^(\d*)$/
98 or eidiot "Illegal referral_depth";
99 my $referral_depth = $1;
101 foreach my $depth ( 1 .. $max ) {
103 ' SELECTED'x($depth == $referral_depth),
106 print "</SELECT> levels deep".
107 '<NOSCRIPT> <INPUT TYPE="submit" VALUE="change"></NOSCRIPT>'.
110 print "<BR>", &table(), <<END;
117 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
119 <TH>(service) name</TH>
126 <TH COLSPAN=2>Services</TH>
132 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
134 my($custnum,$last,$first,$company)=(
136 $cust_main->getfield('last'),
137 $cust_main->getfield('first'),
142 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
143 foreach ( @{$all_pkgs{$custnum}} ) {
144 my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
145 push @lol_cust_svc, \@cust_svc;
146 $rowspan += scalar(@cust_svc) || 1;
149 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
150 my($view) = popurl(2). "view/cust_main.cgi?$custnum";
153 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
154 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
155 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
157 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
158 my($ship_last,$ship_first,$ship_company)=(
159 $cust_main->ship_last || $cust_main->getfield('last'),
160 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
161 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
164 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
165 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A></TD>
170 foreach ( @{$all_pkgs{$custnum}} ) {
171 my($pkgnum) = ($_->pkgnum);
172 my($pkg) = $_->part_pkg->pkg;
173 my $comment = $_->part_pkg->comment;
174 my($pkgview) = popurl(2). "/view/cust_pkg.cgi?$pkgnum";
175 #my(@cust_svc) = shift @lol_cust_svc;
176 my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
177 my($rowspan) = scalar(@cust_svc) || 1;
179 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
181 foreach my $cust_svc ( @cust_svc ) {
182 my($label, $value, $svcdb) = $cust_svc->label;
183 my($svcnum) = $cust_svc->svcnum;
184 my($sview) = popurl(2). "/view";
185 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
186 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
189 #print qq!</TR><TR>\n!;
206 $a->getfield('last') cmp $b->getfield('last');
210 return -1 if $a->company && ! $b->company;
211 return 1 if ! $a->company && $b->company;
212 $a->getfield('company') cmp $b->getfield('company');
216 $a->getfield('custnum') <=> $b->getfield('custnum');
221 my($card)=$cgi->param('card');
223 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
226 push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
231 $cgi->param('referral_custnum') =~ /^(\d+)$/
232 or eidiot "Illegal referral_custnum";
233 my $cust_main = qsearchs('cust_main', { 'custnum' => $1 } )
234 or eidiot "Customer $1 not found";
236 if ( $cgi->param('referral_depth') ) {
237 $cgi->param('referral_depth') =~ /^(\d+)$/
238 or eidiot "Illegal referral_depth";
243 push @cust_main, $cust_main->referral_cust_main($depth);
249 foreach ( $cgi->param('last_type') ) {
253 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
254 or eidiot "Illegal last name";
257 if ( $last_type{'Exact'}
258 && ! $last_type{'Fuzzy'}
259 # && ! $last_type{'Sound-alike'}
262 push @cust_main, qsearch('cust_main',{'last'=>$last});
264 push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
265 if defined dbdef->table('cust_main')->column('ship_last');
269 &FS::cust_main::check_and_rebuild_fuzzyfiles;
270 my $all_last = &FS::cust_main::all_last;
273 if ($last_type{'Fuzzy'}) {
274 foreach ( amatch($last, [ qw(i) ], @$all_last) ) {
279 #if ($last_type{'Sound-alike'}) {
282 foreach ( keys %last ) {
283 push @cust_main, qsearch('cust_main',{'last'=>$_});
284 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
285 if defined dbdef->table('cust_main')->column('ship_last');
295 foreach ( $cgi->param('company_type') ) {
299 $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
300 or eidiot "Illegal company";
303 if ( $company_type{'Exact'}
304 && ! $company_type{'Fuzzy'}
305 # && ! $company_type{'Sound-alike'}
308 push @cust_main, qsearch('cust_main',{'company'=>$company});
310 push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
311 if defined dbdef->table('cust_main')->column('ship_last');
315 &FS::cust_main::check_and_rebuild_fuzzyfiles;
316 my $all_company = &FS::cust_main::all_company;
319 if ($company_type{'Fuzzy'}) {
320 foreach ( amatch($company, [ qw(i) ], @$all_company ) ) {
325 #if ($company_type{'Sound-alike'}) {
328 foreach ( keys %company ) {
329 push @cust_main, qsearch('cust_main',{'company'=>$_});
330 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
331 if defined dbdef->table('cust_main')->column('ship_last');
335 $sortby=\*company_sort;