corrections to host-item, shelf_loc and collection-code indexes
[koha.git] / reserve / request.pl
blobdd78e9d169366714144ec691dfb572f8a7866717
1 #!/usr/bin/perl
4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
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.
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.
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
22 =head1 request.pl
24 script to place reserves/requests
26 =cut
28 use strict;
29 use C4::Branch; # GetBranches get_branchinfos_of
30 use CGI;
31 use List::MoreUtils qw/uniq/;
32 use Date::Calc qw/Today Date_to_Days/;
33 use C4::Output;
34 use C4::Auth;
35 use C4::Reserves;
36 use C4::Biblio;
37 use C4::Items;
38 use C4::Koha;
39 use C4::Circulation;
40 use C4::Dates qw/format_date/;
41 use C4::Members;
43 my $dbh = C4::Context->dbh;
44 my $sth;
45 my $input = new CGI;
46 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
48 template_name => "reserve/request.tmpl",
49 query => $input,
50 type => "intranet",
51 authnotrequired => 0,
52 flagsrequired => { reserveforothers => 1 },
56 # get Branches and Itemtypes
57 my $branches = GetBranches();
58 my $itemtypes = GetItemTypes();
60 # get biblio information....
61 my $biblionumber = $input->param('biblionumber');
62 my $dat = GetBiblioData($biblionumber);
64 # Select borrowers infos
65 my $findborrower = $input->param('findborrower');
66 $findborrower =~ s|,| |g;
67 my $cardnumber = $input->param('cardnumber');
68 my $borrowerslist;
69 my $messageborrower;
70 my $warnings;
71 my $messages;
73 my $date = sprintf( '%04d-%02d-%02d', Today() );
75 if ($findborrower) {
76 my ( $count, $borrowers ) =
77 SearchMember($findborrower, 'cardnumber', 'web' );
79 my @borrowers = @$borrowers;
81 if ( $#borrowers == -1 ) {
82 $input->param( 'findborrower', '' );
83 $messageborrower = "'$findborrower'";
85 elsif ( $#borrowers == 0 ) {
86 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
87 $cardnumber = $borrowers[0]->{'cardnumber'};
89 else {
90 $borrowerslist = \@borrowers;
94 if ($cardnumber) {
95 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
96 my $expiry;
97 my $diffbranch;
98 my @getreservloop;
99 my $count_reserv = 0;
100 my $maxreserves;
102 # we check the reserves of the borrower, and if he can reserv a document
103 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
105 my $number_reserves =
106 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
108 if ( $number_reserves > C4::Context->preference('maxreserves') ) {
109 $warnings = 1;
110 $maxreserves = 1;
113 # we check the date expiricy of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
114 if ($borrowerinfo->{'dateexpiry'} ne '0000-00-00') {
115 my $warning = (Date_to_Days(split /-/,$date) > Date_to_Days( split /-/,$borrowerinfo->{'dateexpiry'}));
116 if ( $warning > 0 ) {
117 $messages = 1;
118 $expiry = 1;
120 } else {
121 $messages = 1;
122 $expiry = 1;
126 # check if the borrower make the reserv in a different branch
127 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
128 $messages = 1;
129 $diffbranch = 1;
132 $template->param(
133 borrowernumber => $borrowerinfo->{'borrowernumber'},
134 borrowersurname => $borrowerinfo->{'surname'},
135 borrowerfirstname => $borrowerinfo->{'firstname'},
136 borrowerstreetaddress => $borrowerinfo->{'address'},
137 borrowercity => $borrowerinfo->{'city'},
138 borrowerphone => $borrowerinfo->{'phone'},
139 borrowermobile => $borrowerinfo->{'mobile'},
140 borrowerfax => $borrowerinfo->{'fax'},
141 borrowerphonepro => $borrowerinfo->{'phonepro'},
142 borroweremail => $borrowerinfo->{'email'},
143 borroweremailpro => $borrowerinfo->{'emailpro'},
144 borrowercategory => $borrowerinfo->{'category'},
145 borrowerreservs => $count_reserv,
146 maxreserves => $maxreserves,
147 expiry => $expiry,
148 diffbranch => $diffbranch,
149 messages => $messages,
150 warnings => $warnings
154 $template->param( messageborrower => $messageborrower );
156 my $CGIselectborrower;
157 if ($borrowerslist) {
158 my @values;
159 my %labels;
161 foreach my $borrower (
162 sort {
163 $a->{surname}
164 . $a->{firstname} cmp $b->{surname}
165 . $b->{firstname}
166 } @{$borrowerslist}
169 push @values, $borrower->{cardnumber};
171 $labels{ $borrower->{cardnumber} } = sprintf(
172 '%s, %s ... (%s - %s) ... %s',
173 $borrower->{surname}, $borrower->{firstname},
174 $borrower->{cardnumber}, $borrower->{categorycode},
175 $borrower->{streetaddress},
179 $CGIselectborrower = CGI::scrolling_list(
180 -name => 'cardnumber',
181 -values => \@values,
182 -labels => \%labels,
183 -size => 7,
184 -multiple => 0,
188 # get existing reserves .....
189 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
190 my $totalcount = $count;
191 my $alreadyreserved;
193 # FIXME launch another time GetMemberDetails perhaps until
194 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
196 foreach my $res (@$reserves) {
197 if ( ( $res->{found} eq 'W' ) ) {
198 $count--;
201 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
202 $warnings = 1;
203 $alreadyreserved = 1;
207 $template->param( alreadyreserved => $alreadyreserved,
208 messages => $messages,
209 warnings => $warnings );
211 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
212 # make priorities options
214 my @optionloop;
215 for ( 1 .. $count + 1 ) {
216 push(
217 @optionloop,
219 num => $_,
220 selected => ( $_ == $count + 1 ),
224 # adding a fixed value for priority options
225 my $fixedRank = $count+1;
227 my @branchcodes;
228 my %itemnumbers_of_biblioitem;
229 my @itemnumbers;
231 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
232 @itemnumbers = @$items;
234 else {
235 $template->param('noitems' => 1);
238 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
240 foreach my $itemnumber (@itemnumbers) {
241 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
242 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
245 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
247 my $notforloan_label_of = get_notforloan_label_of();
248 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
250 my @bibitemloop;
252 foreach my $biblioitemnumber (@biblioitemnumbers) {
253 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
255 $biblioitem->{description} =
256 $itemtypes->{ $biblioitem->{itemtype} }{description};
258 foreach
259 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
261 my $item = $iteminfos_of->{$itemnumber};
262 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
263 $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
264 $item->{homebranchname} =
265 $branches->{ $item->{homebranch} }{branchname};
267 # if the holdingbranch is different than the homebranch, we show the
268 # holdingbranch of the document too
269 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
270 $item->{holdingbranchname} =
271 $branches->{ $item->{holdingbranch} }{branchname};
274 # add information
275 $item->{itemcallnumber} = $item->{itemcallnumber};
277 # if the item is currently on loan, we display its return date and
278 # change the background color
279 my $issues= GetItemIssue($itemnumber);
280 if ( $issues->{'date_due'} ) {
281 $item->{date_due} = format_date($issues->{'date_due'});
282 $item->{backgroundcolor} = 'onloan';
285 # checking reserve
286 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
287 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
289 if ( defined $reservedate ) {
290 $item->{backgroundcolor} = 'reserved';
291 $item->{reservedate} = format_date($reservedate);
292 $item->{ReservedForBorrowernumber} = $reservedfor;
293 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
294 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
295 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
299 # Management of the notforloan document
300 if ( $item->{notforloan} ) {
301 $item->{backgroundcolor} = 'other';
302 $item->{notforloanvalue} =
303 $notforloan_label_of->{ $item->{notforloan} };
306 # Management of lost or long overdue items
307 if ( $item->{itemlost} ) {
309 # FIXME localized strings should never be in Perl code
310 $item->{message} =
311 $item->{itemlost} == 1 ? "(lost)"
312 : $item->{itemlost} == 2 ? "(long overdue)"
313 : "";
314 $item->{backgroundcolor} = 'other';
317 # Check the transit status
318 my ( $transfertwhen, $transfertfrom, $transfertto ) =
319 GetTransfers($itemnumber);
321 if ( $transfertwhen ne '' ) {
322 $item->{transfertwhen} = format_date($transfertwhen);
323 $item->{transfertfrom} =
324 $branches->{$transfertfrom}{branchname};
325 $item->{transfertto} = $branches->{$transfertto}{branchname};
326 $item->{nocancel} = 1;
329 # If there is no loan, return and transfer, we show a checkbox.
330 $item->{notforloan} = $item->{notforloan} || 0;
332 # if independent branches is on we need to check if the person can reserve
333 # for branches they arent logged in to
334 if ( C4::Context->preference("IndependantBranches") ) {
335 if (! C4::Context->preference("canreservefromotherbranches")){
336 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
337 my $userenv = C4::Context->userenv;
338 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
339 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
344 # FIXME: every library will define this differently
345 # An item is available only if:
346 if (
347 not defined $reservedate # not reserved yet
348 and $issues->{'date_due'} eq '' # not currently on loan
349 and not $item->{itemlost} # not lost
350 and not $item->{notforloan} # not forbidden to loan
351 and not $item->{cantreserve}
352 and $transfertwhen eq '' # not currently on transfert
355 $item->{available} = 1;
358 # FIXME: move this to a pm
359 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W' AND cancellationdate IS NULL");
360 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
361 while (my $wait_hashref = $sth2->fetchrow_hashref) {
362 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
364 push @{ $biblioitem->{itemloop} }, $item;
367 push @bibitemloop, $biblioitem;
370 # existingreserves building
371 my @reserveloop;
372 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
373 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
374 my %reserve;
375 my @optionloop;
376 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
377 push(
378 @optionloop,
380 num => $i,
381 selected => ( $i == $res->{priority} ),
385 my @branchloop;
386 foreach my $br ( keys %$branches ) {
387 my %abranch;
388 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
389 $abranch{'branch'} = $br;
390 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
391 push( @branchloop, \%abranch );
394 if ( ( $res->{'found'} eq 'W' ) ) {
395 my $item = $res->{'itemnumber'};
396 $item = GetBiblioFromItemNumber($item,undef);
397 $reserve{'wait'}= 1;
398 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
399 $reserve{'biblionumber'}=$item->{'biblionumber'};
400 $reserve{'barcodenumber'} = $item->{'barcode'};
401 $reserve{'wbrcode'} = $res->{'branchcode'};
402 $reserve{'itemnumber'} = $res->{'itemnumber'};
403 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
404 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
405 $reserve{'atdestination'} = 1;
407 # set found to 1 if reserve is waiting for patron pickup
408 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
411 # get borrowers reserve info
412 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
414 $reserve{'date'} = format_date( $res->{'reservedate'} );
415 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
416 $reserve{'biblionumber'} = $res->{'biblionumber'};
417 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
418 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
419 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
420 $reserve{'notes'} = $res->{'reservenotes'};
421 $reserve{'wait'} =
422 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
423 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
424 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
425 $reserve{'voldesc'} = $res->{'volumeddesc'};
426 $reserve{'ccode'} = $res->{'ccode'};
427 $reserve{'barcode'} = $res->{'barcode'};
428 $reserve{'priority'} = $res->{'priority'};
429 $reserve{'branchloop'} = \@branchloop;
430 $reserve{'optionloop'} = \@optionloop;
432 push( @reserveloop, \%reserve );
435 my $default = C4::Context->userenv->{branch};
436 my @values;
437 my %label_of;
439 foreach my $branchcode (sort keys %{$branches} ) {
440 push @values, $branchcode;
441 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
443 my $CGIbranch = CGI::scrolling_list(
444 -name => 'pickup',
445 -id => 'pickup',
446 -values => \@values,
447 -default => $default,
448 -labels => \%label_of,
449 -size => 1,
450 -multiple => 0,
453 # get the time for the form name...
454 my $time = time();
456 $template->param(
457 CGIbranch => $CGIbranch,
458 reserveloop => \@reserveloop,
459 time => $time,
460 fixedRank => $fixedRank,
463 # display infos
464 $template->param(
465 optionloop => \@optionloop,
466 bibitemloop => \@bibitemloop,
467 date => $date,
468 biblionumber => $biblionumber,
469 findborrower => $findborrower,
470 cardnumber => $cardnumber,
471 CGIselectborrower => $CGIselectborrower,
472 title => $dat->{title},
473 author => $dat->{author},
474 holdsview => 1
477 # printout the page
478 output_html_with_http_headers $input, $cookie, $template->output;