shipping address additions
[freeside.git] / httemplate / search / cust_main.cgi
1 <%
2 #<!-- $Id: cust_main.cgi,v 1.2 2001-07-30 10:41:44 ivan Exp $ -->
3
4 use strict;
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 CGI;
8 use CGI::Carp qw(fatalsToBrowser);
9 use IO::Handle;
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);
14 use FS::cust_main;
15 use FS::cust_svc;
16
17 $cgi = new CGI;
18 cgisuidsetup($cgi);
19
20 $conf = new FS::Conf;
21
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' ) {
28     $sortby=\*last_sort;
29     @cust_main=qsearch('cust_main',{});  
30   } elsif ( $query eq 'company' ) {
31     $sortby=\*company_sort;
32     @cust_main=qsearch('cust_main',{});
33   } else {
34     die "unknown query string $query";
35   }
36 } else {
37   @cust_main=();
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 }
42
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;
47 } else {
48   %all_pkgs = map { $_->custnum => [ $_->all_pkgs ] } @cust_main;
49 }
50
51 if ( scalar(@cust_main) == 1 ) {
52   print $cgi->redirect(popurl(2). "view/cust_main.cgi?". $cust_main[0]->custnum);
53   exit;
54 } elsif ( scalar(@cust_main) == 0 ) {
55   eidiot "No matching customers found!\n";
56 } else { 
57
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;
62       <TR>
63         <TH></TH>
64         <TH>(bill) name</TH>
65         <TH>company</TH>
66 END
67
68 if ( defined dbdef->table('cust_main')->column('ship_last') ) {
69   print <<END;
70       <TH>(service) name</TH>
71       <TH>company</TH>
72 END
73 }
74
75 print <<END;
76         <TH>Packages</TH>
77         <TH COLSPAN=2>Services</TH>
78       </TR>
79 END
80
81   my(%saw,$cust_main);
82   foreach $cust_main (
83     sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
84   ) {
85     my($custnum,$last,$first,$company)=(
86       $cust_main->custnum,
87       $cust_main->getfield('last'),
88       $cust_main->getfield('first'),
89       $cust_main->company,
90     );
91
92     my(@lol_cust_svc);
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;
98     }
99
100     #my($rowspan) = scalar(@{$all_pkgs{$custnum}});
101     my($view) = popurl(2). "view/cust_main.cgi?$custnum";
102     print <<END;
103     <TR>
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>
107 END
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,
113       );
114 print <<END;
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>
117 END
118     }
119
120     my($n1)='';
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;
129
130       print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview"><FONT SIZE=-1>$pkg - $comment</FONT></A></TD>!;
131       my($n2)='';
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>!;
138          $n2="</TR><TR>";
139       }
140       #print qq!</TR><TR>\n!;
141       $n1="</TR><TR>";
142     }
143     print "</TR>";
144   }
145  
146   print <<END;
147     </TABLE>
148   </BODY>
149 </HTML>
150 END
151
152 }
153
154 #
155
156 sub last_sort {
157   $a->getfield('last') cmp $b->getfield('last');
158 }
159
160 sub company_sort {
161   return -1 if $a->company && ! $b->company;
162   return 1 if ! $a->company && $b->company;
163   $a->getfield('company') cmp $b->getfield('company');
164 }
165
166 sub custnum_sort {
167   $a->getfield('custnum') <=> $b->getfield('custnum');
168 }
169
170 sub cardsearch {
171
172   my($card)=$cgi->param('card');
173   $card =~ s/\D//g;
174   $card =~ /^(\d{13,16})$/ or eidiot "Illegal card number\n";
175   my($payinfo)=$1;
176
177   push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});
178
179 }
180
181 sub lastsearch {
182   my(%last_type);
183   foreach ( $cgi->param('last_type') ) {
184     $last_type{$_}++;
185   }
186
187   $cgi->param('last_text') =~ /^([\w \,\.\-\']*)$/
188     or eidiot "Illegal last name";
189   my($last)=$1;
190
191   if ( $last_type{'Exact'}
192        && ! $last_type{'Fuzzy'} 
193      #  && ! $last_type{'Sound-alike'}
194   ) {
195
196     push @cust_main, qsearch('cust_main',{'last'=>$last});
197
198     push @cust_main, qsearch('cust_main',{'ship_last'=>$last})
199       if defined dbdef->table('cust_main')->column('ship_last');
200
201   } else {
202
203     my(%last);
204
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) ) {
210         $last{$_}++; 
211       }
212     }
213
214     #if ($last_type{'Sound-alike'}) {
215     #}
216
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');
221     }
222
223   }
224   $sortby=\*last_sort;
225 }
226
227 sub companysearch {
228
229   my(%company_type);
230   foreach ( $cgi->param('company_type') ) {
231     $company_type{$_}++ 
232   };
233
234   $cgi->param('company_text') =~ /^([\w \,\.\-\']*)$/
235     or eidiot "Illegal company";
236   my($company)=$1;
237
238   if ( $company_type{'Exact'}
239        && ! $company_type{'Fuzzy'} 
240      #  && ! $company_type{'Sound-alike'}
241   ) {
242
243     push @cust_main, qsearch('cust_main',{'company'=>$company});
244
245     push @cust_main, qsearch('cust_main',{'ship_company'=>$company})
246       if defined dbdef->table('cust_main')->column('ship_last');
247
248   } else {
249
250     my(%company);
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');
254
255     if ($company_type{'Fuzzy'}) { 
256       foreach ( amatch($company, [ qw(i) ], @all_company ) ) {
257         $company{$_}++;
258       }
259     }
260
261     #if ($company_type{'Sound-alike'}) {
262     #}
263
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');
268     }
269
270   }
271   $sortby=\*company_sort;
272
273 }
274 %>