POD added, Call to Bookseller.pm added. Some functions has been renamed/changed/rewri...
[koha.git] / acqui / newbasket2.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 newbasket2.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 if ( $type eq '' ) {
85     $type = 'intra';
86 }
87
88 #print $input->dump;
89 my $blah;
90 my %search;
91
92 #build hash of users input
93 my $title = $input->param('search');
94 $search{'title'} = $title;
95 my $keyword = $input->param('d');
96 $search{'keyword'} = $keyword;
97 my $author = $input->param('author');
98 $search{'author'} = $author;
99
100 my @results;
101 my $offset = $input->param('offset');
102
103 #default value for offset
104 my $offset = 0 unless $offset;
105
106 my $num = $input->param('num');
107
108 #default value for num
109 my $num = 10 unless $num;
110
111 my $donation;
112 my $booksellerid = $input->param('booksellerid');
113 if ( $booksellerid == 72 ) {
114     $donation = 'yes';
115 }
116 my $basketno = $input->param('basketno');
117 my $sub      = $input->param('sub');
118
119 #print $sub;
120 my @booksellers = GetBookSeller($booksellerid);
121 my $count = scalar @booksellers;
122
123 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
124     {
125         template_name   => "acqui/newbasket2.tmpl",
126         query           => $input,
127         type            => "intranet",
128         authnotrequired => 0,
129         flagsrequired   => { order => 1 },
130         debug           => 1,
131     }
132 );
133
134 #my $template = gettemplate("acqui/newbasket2.tmpl");
135 #print startpage();
136 #print startmenu('acquisitions');
137 my $invalidsearch;
138
139 if ( $keyword ne '' ) {
140     ( $count, @results ) =
141       KeywordSearch( undef, 'intra', \%search, $num, $offset );
142 }
143 elsif ( $search{'front'} ne '' ) {
144     ( $count, @results ) =
145       FrontSearch( undef, 'intra', \%search, $num, $offset );
146 }
147 elsif ( $search{'author'} || $search{'title'} ) {
148     ( $count, @results ) = CatSearch( undef, 'loose', \%search, $num, $offset );
149 }
150 else {
151     $invalidsearch = 1;
152 }
153
154 my @loopsearch;
155
156 while ( my ( $key, $value ) = each %search ) {
157     if ( $value ne '' ) {
158         my %linesearch;
159         $value =~ s/\\//g;
160         $linesearch{key}   = $key;
161         $linesearch{value} = $value;
162         push( @loopsearch, \%linesearch );
163     }
164 }
165
166 my $offset2 = $num + $offset;
167 my $dispnum = $offset + 1;
168 if ( $offset2 > $count ) {
169     $offset2 = $count;
170 }
171
172 my $count2 = @results;
173 if ( $keyword ne '' && $offset > 0 ) {
174     $count2 = $count - $offset;
175     if ( $count2 > 10 ) {
176         $count2 = 10;
177     }
178 }
179 my $i      = 0;
180 my $colour = 0;
181
182 my @loopresult;
183
184 while ( $i < $count2 ) {
185     my %lineres;
186     my $toggle;
187
188     my $result = $results[$i];
189     $result->{'title'} =~ s/\`/\\\'/g;
190     my $title2  = $result->{'title'};
191     my $author2 = $result->{'author'};
192     $author2 =~ s/ /%20/g;
193     $title2  =~ s/ /%20/g;
194     $title2  =~ s/\#/\&\#x23;/g;
195     $title2  =~ s/\"/\&quot\;/g;
196
197     my $itemcount;
198     my $location      = '';
199     my $location_only = '';
200     my $word          = $result->{'author'};
201     $word =~ s/([a-z]) +([a-z])/$1%20$2/ig;
202     $word =~ s/  //g;
203     $word =~ s/ /%20/g;
204     $word =~ s/\,/\,%20/g;
205     $word =~ s/\n//g;
206     $lineres{word} = $word;
207     $lineres{type} = $type;
208
209     my ( $counts, $branchcounts ) =
210       C4::Search::itemcount( $env, $result->{'biblionumber'}, $type );
211
212     if ( $counts->{'nacount'} > 0 ) {
213         $location .= "On Loan";
214         if ( $counts->{'nacount'} > 1 ) {
215             $location .= "=($counts->{'nacount'})";
216         }
217         $location .= " ";
218         $lineres{'on-loan-p'} = 1;
219     }
220     foreach my $key ( keys %$branchcounts ) {
221         if ( $branchcounts->{$key} > 0 ) {
222             $location      .= $key;
223             $location_only .= $key;
224
225             if ( $branchcounts->{$key} > 1 ) {
226                 $location      .= "=$branchcounts->{$key}";
227                 $location_only .= "=$branchcounts->{$key}";
228             }
229             $location      .= " ";
230             $location_only .= " ";
231         }
232     }
233     if ( $counts->{'lostcount'} > 0 ) {
234         $location .= "Lost";
235         if ( $counts->{'lostcount'} > 1 ) {
236             $location .= "=($counts->{'lostcount'})";
237         }
238         $location .= " ";
239         $lineres{'lost-p'} = 1;
240     }
241     if ( $counts->{'mending'} > 0 ) {
242         $location .= "Mending";
243         if ( $counts->{'mending'} > 1 ) {
244             $location .= "=($counts->{'mending'})";
245         }
246         $location .= " ";
247         $lineres{'mending-p'} = 1;
248     }
249     if ( $counts->{'transit'} > 0 ) {
250         $location .= "In Transit";
251         if ( $counts->{'transit'} > 1 ) {
252             $location .= "=($counts->{'transit'})";
253         }
254         $location .= " ";
255         $lineres{'in-transit-p'} = 1;
256     }
257     if ( $colour eq 0 ) {
258         $toggle = 1;
259         $colour = 1;
260     }
261     else {
262         $colour = 0;
263         $toggle = 0;
264     }
265     $lineres{author2}         = $author2;
266     $lineres{title2}          = $title2;
267     $lineres{copyright}       = $result->{'copyrightdate'};
268     $lineres{booksellerid}    = $booksellerid;
269     $lineres{basketno}        = $basketno;
270     $lineres{sub}             = $sub;
271     $lineres{biblionumber}    = $result->{biblionumber};
272     $lineres{title}           = $result->{title};
273     $lineres{author}          = $result->{author};
274     $lineres{toggle}          = $toggle;
275     $lineres{itemcount}       = $counts->{'count'};
276     $lineres{location}        = $location;
277     $lineres{'location-only'} = $location_only;
278
279     # lets get a list on existing orders for all bibitems.
280     my @bibitems = GetBiblioItemByBiblioNumber( $result->{biblionumber} );
281     my $count1 = scalar @bibitems; 
282     my $order, my $ordernumber;
283
284     my $i1 = 0;
285
286     my @ordernumbers;
287     foreach my $bibitem (@bibitems) {
288
289         ( $order, $ordernumber ) =
290           &GetOrder($result->{biblionumber},$bibitem->{biblioitemnumber} );
291
292         #only show order if its current;
293         my %order;
294         $order{'number'} = $ordernumber;
295         if (   ( !$order->{cancelledby} )
296             && ( $order->{quantityreceived} < $order->{quantity} ) )
297         {
298             push @ordernumbers, \%order;
299         }
300     }
301     $lineres{existingorder} = \@ordernumbers;
302     push( @loopresult, \%lineres );
303     $i++;
304 }
305
306 my $prevoffset = $offset - $num;
307 my $offsetprev = 1;
308 if ( $prevoffset < 0 ) {
309     $offsetprev = 0;
310 }
311
312 $offset = $num + $offset;
313
314 my @numbers = ();
315 if ( $count > 10 ) {
316     for ( my $i = 0 ; $i < ( $count / $num ) ; $i++ ) {
317         my $highlight    = 0;
318         my $numberoffset = $i * $num;
319         if ( ( $numberoffset + $num ) == $offset ) { $highlight = 1 }
320
321      #       warn "I $i | N $num | O $offset | NO $numberoffset | H $highlight";
322         push @numbers,
323           {
324             number       => ( $i + 1 ),
325             highlight    => $highlight,
326             numberoffset => $numberoffset
327           };
328     }
329 }
330
331 $template->param(
332     bookselname            => $booksellers[0]->{'name'},
333     booksellerid           => $booksellerid,
334     basketno               => $basketno,
335     parsub                 => $sub,
336     count                  => $count,
337     offset2                => $offset2,
338     dispnum                => $dispnum,
339     offsetover             => ( $offset < $count ),
340     num                    => $num,
341     offset                 => $prevoffset,
342     offsetprev             => $offsetprev,
343     type                   => $type,
344     title                  => $title,
345     author                 => $author,
346     donation               => $donation,
347     loopsearch             => \@loopsearch,
348     loopresult             => \@loopresult,
349     numbers                => \@numbers,
350     invalidsearch          => $invalidsearch,
351     'use-location-flags-p' => 1
352 );
353
354 output_html_with_http_headers $input, $cookie, $template->output;
355