*** empty log message ***
[koha.git] / acqui / newbasket2.pl
1 #!/usr/bin/perl
2 #origninally script to provide intranet (librarian) advanced search facility
3 #now script to do searching for acquisitions
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Search;
24 use CGI;
25 use C4::Output;
26 use C4::Acquisition;
27 use C4::Biblio;
28 use HTML::Template;
29 use C4::Auth;
30 use C4::Interface::CGI::Output;
31
32 #use Data::Dumper;
33
34 my $env;
35 my $input = new CGI;
36
37 #print $input->header;
38
39 #whether it is called from the opac of the intranet
40 my $type = $input->param('type');
41 if ( $type eq '' ) {
42     $type = 'intra';
43 }
44
45 #print $input->dump;
46 my $blah;
47 my %search;
48
49 #build hash of users input
50 my $title = $input->param('search');
51 $search{'title'} = $title;
52 my $keyword = $input->param('d');
53 $search{'keyword'} = $keyword;
54 my $author = $input->param('author');
55 $search{'author'} = $author;
56
57 my @results;
58 my $offset = $input->param('offset');
59 if ( $offset eq '' ) {
60     $offset = 0;
61 }
62 my $num = $input->param('num');
63 if ( $num eq '' ) {
64     $num = 10;
65 }
66 my $donation;
67 my $booksellerid = $input->param('booksellerid');
68 if ( $booksellerid == 72 ) {
69     $donation = 'yes';
70 }
71 my $basketno = $input->param('basketno');
72 my $sub      = $input->param('sub');
73
74 #print $sub;
75 my ( $count, @booksellers ) = bookseller($booksellerid);
76 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
77     {
78         template_name   => "acqui/newbasket2.tmpl",
79         query           => $input,
80         type            => "intranet",
81         authnotrequired => 0,
82         flagsrequired   => { order => 1 },
83         debug           => 1,
84     }
85 );
86
87 #my $template = gettemplate("acqui/newbasket2.tmpl");
88 #print startpage();
89 #print startmenu('acquisitions');
90 my $invalidsearch;
91
92 if ( $keyword ne '' ) {
93     ( $count, @results ) =
94       KeywordSearch( undef, 'intra', \%search, $num, $offset );
95 }
96 elsif ( $search{'front'} ne '' ) {
97     ( $count, @results ) =
98       FrontSearch( undef, 'intra', \%search, $num, $offset );
99 }
100 elsif ( $search{'author'} || $search{'title'} ) {
101     ( $count, @results ) = CatSearch( undef, 'loose', \%search, $num, $offset );
102 }
103 else {
104     $invalidsearch = 1;
105 }
106
107 my @loopsearch;
108
109 while ( my ( $key, $value ) = each %search ) {
110     if ( $value ne '' ) {
111         my %linesearch;
112         $value =~ s/\\//g;
113         $linesearch{key}   = $key;
114         $linesearch{value} = $value;
115         push( @loopsearch, \%linesearch );
116     }
117 }
118
119 my $offset2 = $num + $offset;
120 my $dispnum = $offset + 1;
121 if ( $offset2 > $count ) {
122     $offset2 = $count;
123 }
124
125 my $count2 = @results;
126 if ( $keyword ne '' && $offset > 0 ) {
127     $count2 = $count - $offset;
128     if ( $count2 > 10 ) {
129         $count2 = 10;
130     }
131 }
132 my $i      = 0;
133 my $colour = 0;
134
135 my @loopresult;
136
137 while ( $i < $count2 ) {
138     my %lineres;
139     my $toggle;
140
141     my $result = $results[$i];
142     $result->{'title'} =~ s/\`/\\\'/g;
143     my $title2  = $result->{'title'};
144     my $author2 = $result->{'author'};
145     $author2 =~ s/ /%20/g;
146     $title2  =~ s/ /%20/g;
147     $title2  =~ s/\#/\&\#x23;/g;
148     $title2  =~ s/\"/\&quot\;/g;
149
150     my $itemcount;
151     my $location      = '';
152     my $location_only = '';
153     my $word          = $result->{'author'};
154     $word =~ s/([a-z]) +([a-z])/$1%20$2/ig;
155     $word =~ s/  //g;
156     $word =~ s/ /%20/g;
157     $word =~ s/\,/\,%20/g;
158     $word =~ s/\n//g;
159     $lineres{word} = $word;
160     $lineres{type} = $type;
161
162     my ( $counts, $branchcounts ) =
163       C4::Search::itemcount( $env, $result->{'biblionumber'}, $type );
164
165     if ( $counts->{'nacount'} > 0 ) {
166         $location .= "On Loan";
167         if ( $counts->{'nacount'} > 1 ) {
168             $location .= "=($counts->{'nacount'})";
169         }
170         $location .= " ";
171         $lineres{'on-loan-p'} = 1;
172     }
173     foreach my $key ( keys %$branchcounts ) {
174         if ( $branchcounts->{$key} > 0 ) {
175             $location      .= $key;
176             $location_only .= $key;
177
178             if ( $branchcounts->{$key} > 1 ) {
179                 $location      .= "=$branchcounts->{$key}";
180                 $location_only .= "=$branchcounts->{$key}";
181             }
182             $location      .= " ";
183             $location_only .= " ";
184         }
185     }
186     if ( $counts->{'lostcount'} > 0 ) {
187         $location .= "Lost";
188         if ( $counts->{'lostcount'} > 1 ) {
189             $location .= "=($counts->{'lostcount'})";
190         }
191         $location .= " ";
192         $lineres{'lost-p'} = 1;
193     }
194     if ( $counts->{'mending'} > 0 ) {
195         $location .= "Mending";
196         if ( $counts->{'mending'} > 1 ) {
197             $location .= "=($counts->{'mending'})";
198         }
199         $location .= " ";
200         $lineres{'mending-p'} = 1;
201     }
202     if ( $counts->{'transit'} > 0 ) {
203         $location .= "In Transit";
204         if ( $counts->{'transit'} > 1 ) {
205             $location .= "=($counts->{'transit'})";
206         }
207         $location .= " ";
208         $lineres{'in-transit-p'} = 1;
209     }
210     if ( $colour eq 0 ) {
211         $toggle = 1;
212         $colour = 1;
213     }
214     else {
215         $colour = 0;
216         $toggle = 0;
217     }
218     $lineres{author2}         = $author2;
219     $lineres{title2}          = $title2;
220     $lineres{copyright}       = $result->{'copyrightdate'};
221     $lineres{booksellerid}    = $booksellerid;
222     $lineres{basketno}        = $basketno;
223     $lineres{sub}             = $sub;
224     $lineres{biblionumber}    = $result->{biblionumber};
225     $lineres{title}           = $result->{title};
226     $lineres{author}          = $result->{author};
227     $lineres{toggle}          = $toggle;
228     $lineres{itemcount}       = $counts->{'count'};
229     $lineres{location}        = $location;
230     $lineres{'location-only'} = $location_only;
231
232     # lets get a list on existing orders for all bibitems.
233     ( my $count1, my @bibitems ) =
234       getbiblioitembybiblionumber( $result->{biblionumber} );
235
236     my $order, my $ordernumber;
237
238     my $i1 = 0;
239
240     my @ordernumbers;
241     foreach my $bibitem (@bibitems) {
242
243         ( $order, $ordernumber ) =
244           &getorder( $bibitem->{biblioitemnumber}, $result->{biblionumber} );
245
246         #only show order if its current;
247         my %order;
248         $order{'number'} = $ordernumber;
249         if (   ( !$order->{cancelledby} )
250             && ( $order->{quantityreceived} < $order->{quantity} ) )
251         {
252             push @ordernumbers, \%order;
253         }
254     }
255     $lineres{existingorder} = \@ordernumbers;
256     push( @loopresult, \%lineres );
257     $i++;
258 }
259
260 my $prevoffset = $offset - $num;
261 my $offsetprev = 1;
262 if ( $prevoffset < 0 ) {
263     $offsetprev = 0;
264 }
265
266 $offset = $num + $offset;
267
268 my @numbers = ();
269 if ( $count > 10 ) {
270     for ( my $i = 0 ; $i < ( $count / $num ) ; $i++ ) {
271         my $highlight    = 0;
272         my $numberoffset = $i * $num;
273         if ( ( $numberoffset + $num ) == $offset ) { $highlight = 1 }
274
275      #       warn "I $i | N $num | O $offset | NO $numberoffset | H $highlight";
276         push @numbers,
277           {
278             number       => ( $i + 1 ),
279             highlight    => $highlight,
280             numberoffset => $numberoffset
281           };
282     }
283 }
284
285 $template->param(
286     bookselname            => $booksellers[0]->{'name'},
287     booksellerid           => $booksellerid,
288     basketno               => $basketno,
289     parsub                 => $sub,
290     count                  => $count,
291     offset2                => $offset2,
292     dispnum                => $dispnum,
293     offsetover             => ( $offset < $count ),
294     num                    => $num,
295     offset                 => $prevoffset,
296     offsetprev             => $offsetprev,
297     type                   => $type,
298     title                  => $title,
299     author                 => $author,
300     donation               => $donation,
301     loopsearch             => \@loopsearch,
302     loopresult             => \@loopresult,
303     numbers                => \@numbers,
304     invalidsearch          => $invalidsearch,
305     'use-location-flags-p' => 1
306 );
307
308 output_html_with_http_headers $input, $cookie, $template->output;
309