4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 # Parts Copyright 2011 Catalyst IT
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 script to place reserves/requests
33 use List
::MoreUtils qw
/uniq/;
34 use Date
::Calc qw
/Date_to_Days/;
42 use C4
::Dates qw
/format_date/;
44 use C4
::Search
; # enabled_staff_search_views
47 my $dbh = C4
::Context
->dbh;
50 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user
(
52 template_name
=> "reserve/request.tt",
56 flagsrequired
=> { reserveforothers
=> 'place_holds' },
60 my $multihold = $input->param('multi_hold');
61 $template->param(multi_hold
=> $multihold);
62 my $showallitems = $input->param('showallitems');
64 # get Branches and Itemtypes
65 my $branches = GetBranches
();
66 my $itemtypes = GetItemTypes
();
69 if (C4
::Context
->userenv && C4
::Context
->userenv->{'branch'}) {
70 $userbranch = C4
::Context
->userenv->{'branch'};
74 # Select borrowers infos
75 my $findborrower = $input->param('findborrower');
76 $findborrower = '' unless defined $findborrower;
77 $findborrower =~ s
|,| |g
;
78 my $borrowernumber_hold = $input->param('borrowernumber') || '';
84 my $date = C4
::Dates
->today('iso');
85 my $action = $input->param('action');
88 if ( $action eq 'move' ) {
89 my $where = $input->param('where');
90 my $reserve_id = $input->param('reserve_id');
91 AlterPriority
( $where, $reserve_id );
92 } elsif ( $action eq 'cancel' ) {
93 my $reserve_id = $input->param('reserve_id');
94 CancelReserve
({ reserve_id
=> $reserve_id });
95 } elsif ( $action eq 'setLowestPriority' ) {
96 my $reserve_id = $input->param('reserve_id');
97 ToggleLowestPriority
( $reserve_id );
98 } elsif ( $action eq 'toggleSuspend' ) {
99 my $reserve_id = $input->param('reserve_id');
100 my $suspend_until = $input->param('suspend_until');
101 ToggleSuspend
( $reserve_id, $suspend_until );
105 my $borrowers = Search
($findborrower, 'cardnumber');
107 if ($borrowers && @
$borrowers) {
108 if ( @
$borrowers == 1 ) {
109 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
112 $template->param( borrower_list
=> sort_borrowerlist
($borrowers));
115 $messageborrower = "'$findborrower'";
119 # If we have the borrowernumber because we've performed an action, then we
120 # don't want to try to place another reserve.
121 if ($borrowernumber_hold && !$action) {
122 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
125 my $count_reserv = 0;
127 # we check the reserves of the borrower, and if he can reserv a document
128 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
130 my $number_reserves =
131 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
133 if ( C4
::Context
->preference('maxreserves') && ($number_reserves >= C4
::Context
->preference('maxreserves')) ) {
138 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
139 my $expiry_date = $borrowerinfo->{dateexpiry
};
140 my $expiry = 0; # flag set if patron account has expired
141 if ($expiry_date and $expiry_date ne '0000-00-00' and
142 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
146 # check if the borrower make the reserv in a different branch
147 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
152 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
153 borrowersurname
=> $borrowerinfo->{'surname'},
154 borrowerfirstname
=> $borrowerinfo->{'firstname'},
155 borrowerstreetaddress
=> $borrowerinfo->{'address'},
156 borrowercity
=> $borrowerinfo->{'city'},
157 borrowerphone
=> $borrowerinfo->{'phone'},
158 borrowermobile
=> $borrowerinfo->{'mobile'},
159 borrowerfax
=> $borrowerinfo->{'fax'},
160 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
161 borroweremail
=> $borrowerinfo->{'email'},
162 borroweremailpro
=> $borrowerinfo->{'emailpro'},
163 borrowercategory
=> $borrowerinfo->{'category'},
164 borrowerreservs
=> $count_reserv,
165 cardnumber
=> $borrowerinfo->{'cardnumber'},
167 diffbranch
=> $diffbranch,
168 messages
=> $messages,
169 warnings
=> $warnings
173 $template->param( messageborrower
=> $messageborrower );
175 # FIXME launch another time GetMember perhaps until
176 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
178 my @biblionumbers = ();
179 my $biblionumbers = $input->param('biblionumbers');
181 @biblionumbers = split '/', $biblionumbers;
183 push @biblionumbers, $input->param('biblionumber');
186 my $itemdata_enumchron = 0;
188 foreach my $biblionumber (@biblionumbers) {
190 my %biblioloopiter = ();
192 my $dat = GetBiblioData
($biblionumber);
194 my $canReserve = CanBookBeReserved
( $borrowerinfo->{borrowernumber
}, $biblionumber );
196 if ( $canReserve eq 'OK' ) {
198 #All is OK and we can continue
200 elsif ( $canReserve eq 'tooManyReserves' ) {
203 elsif ( $canReserve eq 'ageRestricted' ) {
204 $template->param( $canReserve => 1 );
205 $biblioloopiter{$canReserve} = 1;
208 $biblioloopiter{$canReserve} = 1;
211 my $alreadypossession;
212 if (not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowerinfo->{borrowernumber
},$biblionumber)) {
213 $alreadypossession = 1;
216 # get existing reserves .....
217 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
218 my $count = scalar( @
$reserves );
219 my $totalcount = $count;
221 my $alreadyreserved = 0;
223 foreach my $res (@
$reserves) {
224 if ( defined $res->{found
} ) { # found can be 'W' or 'T'
228 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber
}) && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
233 if ( $holds_count ) {
234 $alreadyreserved = 1;
235 $biblioloopiter{warn} = 1;
236 $biblioloopiter{alreadyres
} = 1;
240 alreadyreserved
=> $alreadyreserved,
241 alreadypossession
=> $alreadypossession,
244 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
245 # make priorities options
248 for ( 1 .. $count + 1 ) {
253 selected
=> ( $_ == $count + 1 ),
257 # adding a fixed value for priority options
258 my $fixedRank = $count+1;
261 my %itemnumbers_of_biblioitem;
264 ## $items is array of 'item' table numbers
265 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
266 @itemnumbers = @
$items;
268 my @hostitems = get_hostitemnumbers_of
($biblionumber);
270 $template->param('hostitemsflag' => 1);
271 push(@itemnumbers, @hostitems);
275 $template->param('noitems' => 1);
276 $biblioloopiter{noitems
} = 1;
279 ## Hash of item number to 'item' table fields
280 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
282 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
283 ## when by definition all of the itemnumber have the same biblioitemnumber
284 foreach my $itemnumber (@itemnumbers) {
285 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
286 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
289 ## Should be same as biblionumber
290 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
292 my $notforloan_label_of = get_notforloan_label_of
();
294 ## Hash of biblioitemnumber to 'biblioitem' table records
295 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
299 foreach my $biblioitemnumber (@biblioitemnumbers) {
300 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
301 my $num_available = 0;
302 my $num_override = 0;
305 $biblioitem->{description
} =
306 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
307 if($biblioitem->{biblioitemnumber
} ne $biblionumber){
308 $biblioitem->{hostitemsflag
}=1;
310 $biblioloopiter{description
} = $biblioitem->{description
};
311 $biblioloopiter{itypename
} = $biblioitem->{description
};
312 $biblioloopiter{imageurl
} =
313 getitemtypeimagelocation
('intranet', $itemtypes->{$biblioitem->{itemtype
}}{imageurl
});
315 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
316 my $item = $iteminfos_of->{$itemnumber};
318 unless (C4
::Context
->preference('item-level_itypes')) {
319 $item->{itype
} = $biblioitem->{itemtype
};
322 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
323 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
324 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
326 # if the holdingbranch is different than the homebranch, we show the
327 # holdingbranch of the document too
328 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
329 $item->{holdingbranchname
} =
330 $branches->{ $item->{holdingbranch
} }{branchname
};
333 if($item->{biblionumber
} ne $biblionumber){
334 $item->{hostitemsflag
}=1;
335 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
339 $item->{itemcallnumber
} = $item->{itemcallnumber
};
341 # if the item is currently on loan, we display its return date and
342 # change the background color
343 my $issues= GetItemIssue
($itemnumber);
344 if ( $issues->{'date_due'} ) {
345 $item->{date_due
} = format_sqldatetime
($issues->{date_due
});
346 $item->{backgroundcolor
} = 'onloan';
350 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber
($itemnumber);
351 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
353 if ( defined $reservedate ) {
354 $item->{backgroundcolor
} = 'reserved';
355 $item->{reservedate
} = format_date
($reservedate);
356 $item->{ReservedForBorrowernumber
} = $reservedfor;
357 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
358 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
359 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
360 $item->{waitingdate
} = $wait;
363 # Management of the notforloan document
364 if ( $item->{notforloan
} ) {
365 $item->{backgroundcolor
} = 'other';
366 $item->{notforloanvalue
} =
367 $notforloan_label_of->{ $item->{notforloan
} };
370 # Management of lost or long overdue items
371 if ( $item->{itemlost
} ) {
373 # FIXME localized strings should never be in Perl code
375 $item->{itemlost
} == 1 ?
"(lost)"
376 : $item->{itemlost
} == 2 ?
"(long overdue)"
378 $item->{backgroundcolor
} = 'other';
379 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
385 # Check the transit status
386 my ( $transfertwhen, $transfertfrom, $transfertto ) =
387 GetTransfers
($itemnumber);
389 if ( defined $transfertwhen && $transfertwhen ne '' ) {
390 $item->{transfertwhen
} = format_date
($transfertwhen);
391 $item->{transfertfrom
} =
392 $branches->{$transfertfrom}{branchname
};
393 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
394 $item->{nocancel
} = 1;
397 # If there is no loan, return and transfer, we show a checkbox.
398 $item->{notforloan
} = $item->{notforloan
} || 0;
400 # if independent branches is on we need to check if the person can reserve
401 # for branches they arent logged in to
402 if ( C4
::Context
->preference("IndependentBranches") ) {
403 if (! C4
::Context
->preference("canreservefromotherbranches")){
404 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
405 my $userenv = C4
::Context
->userenv;
406 unless ( C4
::Context
->IsSuperLibrarian ) {
407 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
412 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
414 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
415 my $policy_holdallowed = 1;
417 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
419 if ( $branchitemrule->{'holdallowed'} == 0 ||
420 ( $branchitemrule->{'holdallowed'} == 1 &&
421 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
422 $policy_holdallowed = 0;
427 && !$item->{cantreserve
}
428 && IsAvailableForItemLevelRequest
($itemnumber)
429 && CanItemBeReserved
(
430 $borrowerinfo->{borrowernumber
}, $itemnumber
434 $item->{available
} = 1;
437 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
438 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
439 $item->{override
} = 1;
443 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
445 # Show serial enumeration when needed
446 if ($item->{enumchron
}) {
447 $itemdata_enumchron = 1;
450 push @
{ $biblioitem->{itemloop
} }, $item;
453 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
454 $template->param( override_required
=> 1 );
455 } elsif ( $num_available == 0 ) {
456 $template->param( none_available
=> 1 );
457 $biblioloopiter{warn} = 1;
458 $biblioloopiter{none_avail
} = 1;
460 $template->param( hiddencount
=> $hiddencount);
462 push @bibitemloop, $biblioitem;
465 # existingreserves building
467 $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
468 foreach my $res ( sort {
469 my $a_found = $a->{found
} || '';
470 my $b_found = $a->{found
} || '';
471 $a_found cmp $b_found;
475 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
480 selected
=> ( $i == $res->{priority
} ),
485 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
486 my $item = $res->{'itemnumber'};
487 $item = GetBiblioFromItemNumber
($item,undef);
489 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
490 $reserve{'biblionumber'}=$item->{'biblionumber'};
491 $reserve{'barcodenumber'} = $item->{'barcode'};
492 $reserve{'wbrcode'} = $res->{'branchcode'};
493 $reserve{'itemnumber'} = $res->{'itemnumber'};
494 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
495 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
496 # Just because the holdingbranch matches the reserve branch doesn't mean the item
497 # has arrived at the destination, check for an open transfer for the item as well
498 my ( $transfertwhen, $transfertfrom, $transferto ) = C4
::Circulation
::GetTransfers
( $res->{itemnumber
} );
499 if ( not $transferto or $transferto ne $res->{branchcode
} ) {
500 $reserve{'atdestination'} = 1;
503 # set found to 1 if reserve is waiting for patron pickup
504 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
505 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
506 } elsif ($res->{priority
} > 0) {
507 if (defined($res->{itemnumber
})) {
508 my $item = GetItem
($res->{itemnumber
});
509 $reserve{'itemnumber'} = $res->{'itemnumber'};
510 $reserve{'barcodenumber'} = $item->{'barcode'};
511 $reserve{'item_level_hold'} = 1;
515 # get borrowers reserve info
516 my $reserveborrowerinfo = GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
517 if (C4
::Context
->preference('HidePatronName')){
518 $reserve{'hidename'} = 1;
519 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
521 $reserve{'expirationdate'} = format_date
( $res->{'expirationdate'} )
522 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
523 $reserve{'date'} = format_date
( $res->{'reservedate'} );
524 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
525 $reserve{'biblionumber'} = $res->{'biblionumber'};
526 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
527 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
528 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
529 $reserve{'notes'} = $res->{'reservenotes'};
531 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
532 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
533 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
534 $reserve{'voldesc'} = $res->{'volumeddesc'};
535 $reserve{'ccode'} = $res->{'ccode'};
536 $reserve{'barcode'} = $res->{'barcode'};
537 $reserve{'priority'} = $res->{'priority'};
538 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
539 $reserve{'optionloop'} = \
@optionloop;
540 $reserve{'suspend'} = $res->{'suspend'};
541 $reserve{'suspend_until'} = $res->{'suspend_until'};
542 $reserve{'reserve_id'} = $res->{'reserve_id'};
544 if ( C4
::Context
->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
545 $reserve{'branchloop'} = [ GetBranchDetail
($res->{'branchcode'}) ];
547 $reserve{'branchloop'} = GetBranchesLoop
($res->{'branchcode'});
550 push( @reserveloop, \
%reserve );
553 # get the time for the form name...
557 branchloop
=> GetBranchesLoop
($userbranch),
559 fixedRank
=> $fixedRank,
564 optionloop
=> \
@optionloop,
565 bibitemloop
=> \
@bibitemloop,
566 itemdata_enumchron
=> $itemdata_enumchron,
568 biblionumber
=> $biblionumber,
569 findborrower
=> $findborrower,
570 title
=> $dat->{title
},
571 author
=> $dat->{author
},
573 C4
::Search
::enabled_staff_search_views
,
575 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
577 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
578 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
582 $biblioloopiter{biblionumber
} = $biblionumber;
583 $biblioloopiter{title
} = $dat->{title
};
584 $biblioloopiter{rank
} = $fixedRank;
585 $biblioloopiter{reserveloop
} = \
@reserveloop;
588 $template->param( reserveloop
=> \
@reserveloop );
591 push @biblioloop, \
%biblioloopiter;
594 $template->param( biblioloop
=> \
@biblioloop );
595 $template->param( biblionumbers
=> $biblionumbers );
596 $template->param( maxreserves
=> $maxreserves );
599 $template->param( multi_hold
=> 1 );
602 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
603 $template->param( reserve_in_future
=> 1 );
607 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
608 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
612 output_html_with_http_headers
$input, $cookie, $template->output;
614 sub sort_borrowerlist
{
615 my $borrowerslist = shift;
618 uc( $a->{surname
} . $a->{firstname
} ) cmp
619 uc( $b->{surname
} . $b->{firstname
} )