summaryrefslogtreecommitdiff
path: root/htdocs/search/cust_main.cgi
blob: 70ce991f704421abf275af1bac0093413b84ae37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/perl -Tw
#
# process/cust_main.cgi: Search for customers (process form)
#
# Usage: post form to:
#        http://server.name/path/cust_main.cgi
#
# Note: Should be run setuid freeside as user nobody.
#
# ivan@voicenet.com 96-dec-12
#
# rewrite ivan@sisd.com 98-mar-4
#
# now does browsing too ivan@sisd.com 98-mar-6
#
# Changes to allow page to work at a relative position in server
#       bmccane@maxbaud.net     98-apr-3
#
# display total, use FS::CGI ivan@sisd.com 98-jul-17

use strict;
use CGI::Request;
use CGI::Carp qw(fatalsToBrowser);
use IO::Handle;
use IPC::Open2;
use FS::UID qw(cgisuidsetup);
use FS::Record qw(qsearch qsearchs);
use FS::CGI qw(header idiot);

my($fuzziness)=2; #fuzziness for fuzzy searches, see man agrep
                  #0-4: 0=no fuzz, 4=very fuzzy (too much fuzz!)

my($req)=new CGI::Request;
&cgisuidsetup($req->cgi);

my(@cust_main);
my($sortby);

my($query)=$req->cgi->var('QUERY_STRING');
if ( $query eq 'custnum' ) {
  $sortby=\*custnum_sort;
  @cust_main=qsearch('cust_main',{});  
} elsif ( $query eq 'last' ) {
  $sortby=\*last_sort;
  @cust_main=qsearch('cust_main',{});  
} elsif ( $query eq 'company' ) {
  $sortby=\*company_sort;
  @cust_main=qsearch('cust_main',{});  
} else {
  &cardsearch if ($req->param('card_on') );
  &lastsearch if ($req->param('last_on') );
  &companysearch if ($req->param('company_on') );
}

if ( scalar(@cust_main) == 1 ) {
  $req->cgi->redirect("../view/cust_main.cgi?". $cust_main[0]->custnum);
  exit;
} elsif ( scalar(@cust_main) == 0 ) {
  idiot "No matching customers found!\n";
  exit;
} else { 

  my($total)=scalar(@cust_main);
  CGI::Base::SendHeaders(); # one guess
  print header("Customer Search Results",''), <<END;

    $total matching customers found
    <TABLE BORDER=4 CELLSPACING=0 CELLPADDING=0>
      <TR>
        <TH>Cust. #</TH>
        <TH>Contact name</TH>
        <TH>Company</TH>
      </TR>
END

  my($lines)=16;
  my($lcount)=$lines;
  my(%saw,$cust_main);
  foreach $cust_main (
    sort $sortby grep(!$saw{$_->custnum}++, @cust_main)
  ) {
    my($custnum,$last,$first,$company)=(
      $cust_main->custnum,
      $cust_main->getfield('last'),
      $cust_main->getfield('first'),
      $cust_main->company,
    );
    print <<END;
    <TR>
      <TD><A HREF="../view/cust_main.cgi?$custnum"><FONT SIZE=-1>$custnum</FONT></A></TD>
      <TD><FONT SIZE=-1>$last, $first</FONT></TD>
      <TD><FONT SIZE=-1>$company</FONT></TD>
    </TR>
END
    if ($lcount-- == 0) { # lots of little tables instead of one big one
      $lcount=$lines;
      print <<END;   
  </TABLE>
  <TABLE BORDER=4 CELLSPACING=0 CELLPADDING=0>
    <TR>
      <TH>Cust. #</TH>
      <TH>Contact name</TH>
      <TH>Company<TH>
    </TR>
END
    }
  }
 
  print <<END;
    </TABLE>
    </CENTER>
  </BODY>
</HTML>
END

}

#

sub last_sort {
  $a->getfield('last') cmp $b->getfield('last');
}

sub company_sort {
  $a->getfield('company') cmp $b->getfield('company');
}

sub custnum_sort {
  $a->getfield('custnum') <=> $b->getfield('custnum');
}

sub cardsearch {

  my($card)=$req->param('card');
  $card =~ s/\D//g;
  $card =~ /^(\d{13,16})$/ or do { idiot "Illegal card number\n"; exit; };
  my($payinfo)=$1;

  push @cust_main, qsearch('cust_main',{'payinfo'=>$payinfo, 'payby'=>'CARD'});

}

sub lastsearch {
  my(%last_type);
  foreach ( $req->param('last_type') ) {
    $last_type{$_}++;
  }

  $req->param('last_text') =~ /^([\w \,\.\-\']*)$/
    or do { idiot "Illegal last name"; exit; };
  my($last)=$1;

  if ( $last_type{'Exact'}
       && ! $last_type{'Fuzzy'} 
     #  && ! $last_type{'Sound-alike'}
  ) {

    push @cust_main, qsearch('cust_main',{'last'=>$last});

  } else {

    my(%last);

    my(@all_last)=map $_->getfield('last'), qsearch('cust_main',{});
    if ($last_type{'Fuzzy'}) { 
      my($reader,$writer) = ( new IO::Handle, new IO::Handle );
      open2($reader,$writer,'agrep',"-$fuzziness",'-i','-k',
            substr($last,0,30));
      print $writer join("\n",@all_last),"\n";
      close $writer;
      while (<$reader>) {
        chop;
        $last{$_}++;
      } 
      close $reader;
    }

    #if ($last_type{'Sound-alike'}) {
    #}

    foreach ( keys %last ) {
      push @cust_main, qsearch('cust_main',{'last'=>$_});
    }

  }
  $sortby=\*last_sort;
}

sub companysearch {

  my(%company_type);
  foreach ( $req->param('company_type') ) {
    $company_type{$_}++ 
  };

  $req->param('company_text') =~ /^([\w \,\.\-\']*)$/
    or do { idiot "Illegal company"; exit; };
  my($company)=$1;

  if ( $company_type{'Exact'}
       && ! $company_type{'Fuzzy'} 
     #  && ! $company_type{'Sound-alike'}
  ) {

    push @cust_main, qsearch('cust_main',{'company'=>$company});

  } else {

    my(%company);
    my(@all_company)=map $_->company, qsearch('cust_main',{});

    if ($company_type{'Fuzzy'}) { 
      my($reader,$writer) = ( new IO::Handle, new IO::Handle );
      open2($reader,$writer,'agrep',"-$fuzziness",'-i','-k',
            substr($company,0,30));
      print $writer join("\n",@all_company),"\n";
      close $writer;
      while (<$reader>) {
        chop;
        $company{$_}++;
      }
      close $reader;
    }

    #if ($company_type{'Sound-alike'}) {
    #}

    foreach ( keys %company ) {
      push @cust_main, qsearch('cust_main',{'company'=>$_});
    }

  }
  $sortby=\*company_sort;

}