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
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
24 script to place reserves/requests
30 use C4::Branch; # GetBranches get_branchinfos_of
32 use List::MoreUtils qw/uniq/;
33 use Date::Calc qw/Date_to_Days/;
41 use C4::Dates qw/format_date/;
43 use C4::Search; # enabled_staff_search_views
45 my $dbh = C4::Context->dbh;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50 template_name => "reserve/request.tmpl",
54 flagsrequired => { reserveforothers => 1 },
58 # get Branches and Itemtypes
59 my $branches = GetBranches();
60 my $itemtypes = GetItemTypes();
62 # get biblio information....
63 my $biblionumber = $input->param('biblionumber');
64 my $dat = GetBiblioData($biblionumber);
66 # Select borrowers infos
67 my $findborrower = $input->param('findborrower');
68 $findborrower =~ s|,| |g;
69 my $cardnumber = $input->param('cardnumber');
75 my $date = C4::Dates->today('iso');
78 my ( $count, $borrowers ) =
79 SearchMember($findborrower, 'cardnumber', 'web' );
81 my @borrowers = @$borrowers;
83 if ( $#borrowers == -1 ) {
84 $input->param( 'findborrower', '' );
85 $messageborrower = "'$findborrower'";
87 elsif ( $#borrowers == 0 ) {
88 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
89 $cardnumber = $borrowers[0]->{'cardnumber'};
92 $borrowerslist = \@borrowers;
97 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
100 my $count_reserv = 0;
103 # we check the reserves of the borrower, and if he can reserv a document
104 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
106 my $number_reserves =
107 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
109 if ( $number_reserves > C4::Context->preference('maxreserves') ) {
114 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
115 my $expiry_date = $borrowerinfo->{dateexpiry};
116 my $expiry = 0; # flag set if patron account has expired
117 if ($expiry_date and $expiry_date ne '0000-00-00' and
118 Date_to_Days(split (/-/,$date)) > Date_to_Days(split (/-/,$expiry_date))) {
119 $messages = $expiry = 1;
125 # check if the borrower make the reserv in a different branch
126 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
132 borrowernumber => $borrowerinfo->{'borrowernumber'},
133 borrowersurname => $borrowerinfo->{'surname'},
134 borrowerfirstname => $borrowerinfo->{'firstname'},
135 borrowerstreetaddress => $borrowerinfo->{'address'},
136 borrowercity => $borrowerinfo->{'city'},
137 borrowerphone => $borrowerinfo->{'phone'},
138 borrowermobile => $borrowerinfo->{'mobile'},
139 borrowerfax => $borrowerinfo->{'fax'},
140 borrowerphonepro => $borrowerinfo->{'phonepro'},
141 borroweremail => $borrowerinfo->{'email'},
142 borroweremailpro => $borrowerinfo->{'emailpro'},
143 borrowercategory => $borrowerinfo->{'category'},
144 borrowerreservs => $count_reserv,
145 maxreserves => $maxreserves,
147 diffbranch => $diffbranch,
148 messages => $messages,
149 warnings => $warnings
153 $template->param( messageborrower => $messageborrower );
155 my $CGIselectborrower;
156 if ($borrowerslist) {
160 foreach my $borrower (
163 . $a->{firstname} cmp $b->{surname}
168 push @values, $borrower->{cardnumber};
170 $labels{ $borrower->{cardnumber} } = sprintf(
171 '%s, %s ... (%s - %s) ... %s',
172 $borrower->{surname}, $borrower->{firstname},
173 $borrower->{cardnumber}, $borrower->{categorycode},
174 $borrower->{address},
178 $CGIselectborrower = CGI::scrolling_list(
179 -name => 'cardnumber',
187 # get existing reserves .....
188 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
189 my $totalcount = $count;
192 # FIXME launch another time GetMemberDetails perhaps until
193 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
195 foreach my $res (@$reserves) {
196 if ( ( $res->{found} eq 'W' ) ) {
200 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
202 $alreadyreserved = 1;
206 $template->param( alreadyreserved => $alreadyreserved,
207 messages => $messages,
208 warnings => $warnings );
210 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
211 # make priorities options
214 for ( 1 .. $count + 1 ) {
219 selected => ( $_ == $count + 1 ),
223 # adding a fixed value for priority options
224 my $fixedRank = $count+1;
227 my %itemnumbers_of_biblioitem;
230 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
231 @itemnumbers = @$items;
234 $template->param('noitems' => 1);
237 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
239 foreach my $itemnumber (@itemnumbers) {
240 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
241 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
244 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
246 my $notforloan_label_of = get_notforloan_label_of();
247 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
251 foreach my $biblioitemnumber (@biblioitemnumbers) {
252 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
257 $biblioitem->{description} =
258 $itemtypes->{ $biblioitem->{itemtype} }{description};
260 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ){
261 my $item = $iteminfos_of->{$itemnumber};
262 unless (C4::Context->preference('item-level_itypes')) {
263 $item->{itype} = $biblioitem->{itemtype};
266 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
267 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
268 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
270 # if the holdingbranch is different than the homebranch, we show the
271 # holdingbranch of the document too
272 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
273 $item->{holdingbranchname} =
274 $branches->{ $item->{holdingbranch} }{branchname};
278 $item->{itemcallnumber} = $item->{itemcallnumber};
280 # if the item is currently on loan, we display its return date and
281 # change the background color
282 my $issues= GetItemIssue($itemnumber);
283 if ( $issues->{'date_due'} ) {
284 $item->{date_due} = format_date($issues->{'date_due'});
285 $item->{backgroundcolor} = 'onloan';
289 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
290 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
292 if ( defined $reservedate ) {
293 $item->{backgroundcolor} = 'reserved';
294 $item->{reservedate} = format_date($reservedate);
295 $item->{ReservedForBorrowernumber} = $reservedfor;
296 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
297 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
298 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
302 # Management of the notforloan document
303 if ( $item->{notforloan} ) {
304 $item->{backgroundcolor} = 'other';
305 $item->{notforloanvalue} =
306 $notforloan_label_of->{ $item->{notforloan} };
309 # Management of lost or long overdue items
310 if ( $item->{itemlost} ) {
312 # FIXME localized strings should never be in Perl code
314 $item->{itemlost} == 1 ? "(lost)"
315 : $item->{itemlost} == 2 ? "(long overdue)"
317 $item->{backgroundcolor} = 'other';
320 # Check the transit status
321 my ( $transfertwhen, $transfertfrom, $transfertto ) =
322 GetTransfers($itemnumber);
324 if ( $transfertwhen ne '' ) {
325 $item->{transfertwhen} = format_date($transfertwhen);
326 $item->{transfertfrom} =
327 $branches->{$transfertfrom}{branchname};
328 $item->{transfertto} = $branches->{$transfertto}{branchname};
329 $item->{nocancel} = 1;
332 # If there is no loan, return and transfer, we show a checkbox.
333 $item->{notforloan} = $item->{notforloan} || 0;
335 # if independent branches is on we need to check if the person can reserve
336 # for branches they arent logged in to
337 if ( C4::Context->preference("IndependantBranches") ) {
338 if (! C4::Context->preference("canreservefromotherbranches")){
339 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
340 my $userenv = C4::Context->userenv;
341 if ( ($userenv) && ( $userenv->{flags} %2 != 1 ) ) {
342 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
347 my $branch = C4::Circulation::_GetCircControlBranch($item, $borr);
349 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
350 my $policy_holdallowed = 1;
352 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
354 if ( $branchitemrule->{'holdallowed'} == 0 ||
355 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
356 $policy_holdallowed = 0;
359 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
360 if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
361 $item->{override} = 1;
363 } elsif ( $policy_holdallowed ) {
364 $item->{available} = 1;
368 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
370 # FIXME: move this to a pm
371 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
372 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
373 while (my $wait_hashref = $sth2->fetchrow_hashref) {
374 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
376 push @{ $biblioitem->{itemloop} }, $item;
379 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
380 $template->param( override_required => 1 );
381 } elsif ( $num_available == 0 ) {
382 $template->param( none_available => 1 );
383 $template->param( warnings => 1 );
386 push @bibitemloop, $biblioitem;
389 # existingreserves building
391 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
392 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
395 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
400 selected => ( $i == $res->{priority} ),
405 foreach my $br ( keys %$branches ) {
407 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
408 $abranch{'branch'} = $br;
409 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
410 push( @branchloop, \%abranch );
413 if ( ( $res->{'found'} eq 'W' ) ) {
414 my $item = $res->{'itemnumber'};
415 $item = GetBiblioFromItemNumber($item,undef);
417 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
418 $reserve{'biblionumber'}=$item->{'biblionumber'};
419 $reserve{'barcodenumber'} = $item->{'barcode'};
420 $reserve{'wbrcode'} = $res->{'branchcode'};
421 $reserve{'itemnumber'} = $res->{'itemnumber'};
422 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
423 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
424 $reserve{'atdestination'} = 1;
426 # set found to 1 if reserve is waiting for patron pickup
427 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
428 } elsif ($res->{priority} > 0) {
429 if (defined($res->{itemnumber})) {
430 my $item = GetItem($res->{itemnumber});
431 $reserve{'itemnumber'} = $res->{'itemnumber'};
432 $reserve{'barcodenumber'} = $item->{'barcode'};
433 $reserve{'item_level_hold'} = 1;
437 # get borrowers reserve info
438 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
440 $reserve{'date'} = format_date( $res->{'reservedate'} );
441 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
442 $reserve{'biblionumber'} = $res->{'biblionumber'};
443 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
444 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
445 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
446 $reserve{'notes'} = $res->{'reservenotes'};
448 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
449 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
450 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
451 $reserve{'voldesc'} = $res->{'volumeddesc'};
452 $reserve{'ccode'} = $res->{'ccode'};
453 $reserve{'barcode'} = $res->{'barcode'};
454 $reserve{'priority'} = $res->{'priority'};
455 $reserve{'branchloop'} = \@branchloop;
456 $reserve{'optionloop'} = \@optionloop;
458 push( @reserveloop, \%reserve );
461 my $default = C4::Context->userenv->{branch};
465 foreach my $branchcode (sort keys %{$branches} ) {
466 push @values, $branchcode;
467 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
469 my $CGIbranch = CGI::scrolling_list(
473 -default => $default,
474 -labels => \%label_of,
479 # get the time for the form name...
483 CGIbranch => $CGIbranch,
484 reserveloop => \@reserveloop,
486 fixedRank => $fixedRank,
491 optionloop => \@optionloop,
492 bibitemloop => \@bibitemloop,
494 biblionumber => $biblionumber,
495 findborrower => $findborrower,
496 cardnumber => $cardnumber,
497 CGIselectborrower => $CGIselectborrower,
498 title => $dat->{title},
499 author => $dat->{author},
501 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
502 borrower_branchcode => $borrowerinfo->{'branchcode'},
506 output_html_with_http_headers $input, $cookie, $template->output;