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