2 #<!-- $Id: cust_main.cgi,v 1.2 2001-07-30 10:41:44 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 );
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->keywords ) {
23 my($query)=$cgi->keywords;
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 query string $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') );
43 @cust_main = grep { $_->ncancelled_pkgs || ! $_->all_pkgs } @cust_main
44 if $conf->exists('hidecancelledcustomers');
45 if ( $conf->exists('hidecancelledpackages' ) ) {
46 %all_pkgs = map { $_->custnum => [ $_->ncancelled_pkgs ] } @cust_main;
48 %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
51 if ( scalar(@cust_main) == 1 ) {
52 print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
54 } elsif ( scalar(@cust_main) == 0 ) {
55 eidiot "No matching customers found!\n";
58 my($total)=scalar(@cust_main);
59 print $cgi->header( '-expires' => 'now' ), header("Customer Search Results",menubar(
60 'Main Menu', popurl(2)
61 )), "$total matching customers found<BR>", &table(), <<END;
68 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
70 <TH>(service) name</TH>
77 <TH COLSPAN=2>Services</TH>
83 sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
85 my($custnum,$last,$first,$company)=(
87 $cust_main->getfield('last'),
88 $cust_main->getfield('first'),
93 my($rowspan)=0;#scalar( @{$all_pkgs{$custnum}} );
94 foreach ( @{$all_pkgs{$custnum}} ) {
95 my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
96 push @lol_cust_svc, \@cust_svc;
97 $rowspan += scalar(@cust_svc) || 1;
100 #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
101 my($view) = popurl(2). "view/cust_main.cgi?$custnum";
104 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$custnum</FONT></A></TD>
105 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$last, $first</FONT></A></TD>
106 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$company</FONT></A></TD>
108 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
109 my($ship_last,$ship_first,$ship_company)=(
110 $cust_main->ship_last || $cust_main->getfield('last'),
111 $cust_main->ship_last ? $cust_main->ship_first : $cust_main->first,
112 $cust_main->ship_last ? $cust_main->ship_company : $cust_main->company,
115 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_last, $ship_first</FONT></A></TD>
116 <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$ship_company</FONT></A></TD>
121 foreach ( @{$all_pkgs{$custnum}} ) {
122 my($pkgnum) = ($_->pkgnum);
123 my($pkg) = $_->part_pkg->pkg;
124 my $comment = $_->part_pkg->comment;
125 my($pkgview) = popurl(2). "/view/cust_pkg.cgi?$pkgnum";
126 #my(@cust_svc) = shift @lol_cust_svc;
127 my(@cust_svc) = qsearch( 'cust_svc', { 'pkgnum' => $_->pkgnum } );
128 my($rowspan) = scalar(@cust_svc) || 1;
130 print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
132 foreach my $cust_svc ( @cust_svc ) {
133 my($label, $value, $svcdb) = $cust_svc->label;
134 my($svcnum) = $cust_svc->svcnum;
135 my($sview) = popurl(2). "/view";
136 print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
137 qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$value</FONT></A></TD>!;
140 #print qq!</TR><TR>\n!;
157 $a->getfield('last') cmp $b->getfield('last');
161 return -1 if $a->company && ! $b->company;
162 return 1 if ! $a->company && $b->company;
163 $a->getfield('company') cmp $b->getfield('company');
167 $a->getfield('custnum') <=> $b->getfield('custnum');
172 my($card)=$cgi->param('card');
174 $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
177 push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
183 foreach ( $cgi->param('last_type') ) {
187 $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
188 or eidiot "Illegal last name";
191 if ( $last_type{'Exact'}
192 && ! $last_type{'Fuzzy'}
193 # && ! $last_type{'Sound-alike'}
196 push @cust_main, qsearch('cust_main',{'last'=>$last});
198 push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
199 if defined dbdef->table('cust_main')->column('ship_last');
205 my(@all_last)=map $_->getfield('last'), qsearch('cust_main',{});
206 push @all_last, grep $_, map $_->getfield('ship_last'), qsearch('cust_main',{})
207 if defined dbdef->table('cust_main')->column('ship_last');
208 if ($last_type{'Fuzzy'}) {
209 foreach ( amatch($last, [ qw(i) ], @all_last) ) {
214 #if ($last_type{'Sound-alike'}) {
217 foreach ( keys %last ) {
218 push @cust_main, qsearch('cust_main',{'last'=>$_});
219 push @cust_main, qsearch('cust_main',{'ship_last'=>$_})
220 if defined dbdef->table('cust_main')->column('ship_last');
230 foreach ( $cgi->param('company_type') ) {
234 $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
235 or eidiot "Illegal company";
238 if ( $company_type{'Exact'}
239 && ! $company_type{'Fuzzy'}
240 # && ! $company_type{'Sound-alike'}
243 push @cust_main, qsearch('cust_main',{'company'=>$company});
245 push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
246 if defined dbdef->table('cust_main')->column('ship_last');
251 my(@all_company)=map $_->company, qsearch('cust_main',{});
252 push @all_company, grep $_, map $_->getfield('ship_company'), qsearch('cust_main',{})
253 if defined dbdef->table('cust_main')->column('ship_last');
255 if ($company_type{'Fuzzy'}) {
256 foreach ( amatch($company, [ qw(i) ], @all_company ) ) {
261 #if ($company_type{'Sound-alike'}) {
264 foreach ( keys %company ) {
265 push @cust_main, qsearch('cust_main',{'company'=>$_});
266 push @cust_main, qsearch('cust_main',{'ship_company'=>$_})
267 if defined dbdef->table('cust_main')->column('ship_last');
271 $sortby=\*company_sort;