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 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
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.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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.tmpl",
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') || '';
83 my $date = C4
::Dates
->today('iso');
84 my $action = $input->param('action');
87 if ( $action eq 'move' ) {
88 my $where = $input->param('where');
89 my $reserve_id = $input->param('reserve_id');
90 AlterPriority
( $where, $reserve_id );
91 } elsif ( $action eq 'cancel' ) {
92 my $reserve_id = $input->param('reserve_id');
93 CancelReserve
({ reserve_id
=> $reserve_id });
94 } elsif ( $action eq 'setLowestPriority' ) {
95 my $reserve_id = $input->param('reserve_id');
96 ToggleLowestPriority
( $reserve_id );
97 } elsif ( $action eq 'toggleSuspend' ) {
98 my $reserve_id = $input->param('reserve_id');
99 my $suspend_until = $input->param('suspend_until');
100 ToggleSuspend
( $reserve_id, $suspend_until );
104 my $borrowers = Search
($findborrower, 'cardnumber');
106 if ($borrowers && @
$borrowers) {
107 if ( @
$borrowers == 1 ) {
108 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
111 $template->param( borrower_list
=> sort_borrowerlist
($borrowers));
114 $messageborrower = "'$findborrower'";
118 # If we have the borrowernumber because we've performed an action, then we
119 # don't want to try to place another reserve.
120 if ($borrowernumber_hold && !$action) {
121 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
124 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)) {
143 $messages = $expiry = 1;
146 # check if the borrower make the reserv in a different branch
147 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
153 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
154 borrowersurname
=> $borrowerinfo->{'surname'},
155 borrowerfirstname
=> $borrowerinfo->{'firstname'},
156 borrowerstreetaddress
=> $borrowerinfo->{'address'},
157 borrowercity
=> $borrowerinfo->{'city'},
158 borrowerphone
=> $borrowerinfo->{'phone'},
159 borrowermobile
=> $borrowerinfo->{'mobile'},
160 borrowerfax
=> $borrowerinfo->{'fax'},
161 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
162 borroweremail
=> $borrowerinfo->{'email'},
163 borroweremailpro
=> $borrowerinfo->{'emailpro'},
164 borrowercategory
=> $borrowerinfo->{'category'},
165 borrowerreservs
=> $count_reserv,
166 cardnumber
=> $borrowerinfo->{'cardnumber'},
167 maxreserves
=> $maxreserves,
169 diffbranch
=> $diffbranch,
170 messages
=> $messages,
171 warnings
=> $warnings
175 $template->param( messageborrower
=> $messageborrower );
177 # FIXME launch another time GetMember perhaps until
178 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
180 my @biblionumbers = ();
181 my $biblionumbers = $input->param('biblionumbers');
183 @biblionumbers = split '/', $biblionumbers;
185 push @biblionumbers, $input->param('biblionumber');
188 my $itemdata_enumchron = 0;
190 foreach my $biblionumber (@biblionumbers) {
192 my %biblioloopiter = ();
195 my $dat = GetBiblioData
($biblionumber);
197 unless ( CanBookBeReserved
($borrowerinfo->{borrowernumber
}, $biblionumber) ) {
202 my $alreadypossession;
203 if (not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowerinfo->{borrowernumber
},$biblionumber)) {
205 $alreadypossession = 1;
208 # get existing reserves .....
209 my ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
210 my $totalcount = $count;
212 my $alreadyreserved = 0;
214 foreach my $res (@
$reserves) {
215 if ( defined $res->{found
} && $res->{found
} eq 'W' ) {
219 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber
}) && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
224 if ( $holds_count ) {
225 $alreadyreserved = 1;
227 $biblioloopiter{warn} = 1;
228 $biblioloopiter{alreadyres
} = 1;
231 $template->param( alreadyreserved
=> $alreadyreserved,
232 messages
=> $messages,
233 warnings
=> $warnings,
234 maxreserves
=> $maxreserves,
235 alreadypossession
=> $alreadypossession,
238 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
239 # make priorities options
242 for ( 1 .. $count + 1 ) {
247 selected
=> ( $_ == $count + 1 ),
251 # adding a fixed value for priority options
252 my $fixedRank = $count+1;
255 my %itemnumbers_of_biblioitem;
258 ## $items is array of 'item' table numbers
259 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
260 @itemnumbers = @
$items;
262 my @hostitems = get_hostitemnumbers_of
($biblionumber);
264 $template->param('hostitemsflag' => 1);
265 push(@itemnumbers, @hostitems);
269 $template->param('noitems' => 1);
270 $biblioloopiter{noitems
} = 1;
273 ## Hash of item number to 'item' table fields
274 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
276 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
277 ## when by definition all of the itemnumber have the same biblioitemnumber
278 foreach my $itemnumber (@itemnumbers) {
279 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
280 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
283 ## Should be same as biblionumber
284 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
286 my $notforloan_label_of = get_notforloan_label_of
();
288 ## Hash of biblioitemnumber to 'biblioitem' table records
289 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
293 foreach my $biblioitemnumber (@biblioitemnumbers) {
294 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
295 my $num_available = 0;
296 my $num_override = 0;
299 $biblioitem->{description
} =
300 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
301 if($biblioitem->{biblioitemnumber
} ne $biblionumber){
302 $biblioitem->{hostitemsflag
}=1;
304 $biblioloopiter{description
} = $biblioitem->{description
};
305 $biblioloopiter{itypename
} = $biblioitem->{description
};
306 $biblioloopiter{imageurl
} =
307 getitemtypeimagelocation
('intranet', $itemtypes->{$biblioitem->{itemtype
}}{imageurl
});
309 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
310 my $item = $iteminfos_of->{$itemnumber};
312 unless (C4
::Context
->preference('item-level_itypes')) {
313 $item->{itype
} = $biblioitem->{itemtype
};
316 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
317 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
318 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
320 # if the holdingbranch is different than the homebranch, we show the
321 # holdingbranch of the document too
322 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
323 $item->{holdingbranchname
} =
324 $branches->{ $item->{holdingbranch
} }{branchname
};
327 if($item->{biblionumber
} ne $biblionumber){
328 $item->{hostitemsflag
}=1;
329 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
333 $item->{itemcallnumber
} = $item->{itemcallnumber
};
335 # if the item is currently on loan, we display its return date and
336 # change the background color
337 my $issues= GetItemIssue
($itemnumber);
338 if ( $issues->{'date_due'} ) {
339 $item->{date_due
} = format_sqldatetime
($issues->{date_due
});
340 $item->{backgroundcolor
} = 'onloan';
344 my ($reservedate,$reservedfor,$expectedAt,$reserve_id) = GetReservesFromItemnumber
($itemnumber);
345 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
347 if ( defined $reservedate ) {
348 $item->{backgroundcolor
} = 'reserved';
349 $item->{reservedate
} = format_date
($reservedate);
350 $item->{ReservedForBorrowernumber
} = $reservedfor;
351 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
352 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
353 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
357 # Management of the notforloan document
358 if ( $item->{notforloan
} ) {
359 $item->{backgroundcolor
} = 'other';
360 $item->{notforloanvalue
} =
361 $notforloan_label_of->{ $item->{notforloan
} };
364 # Management of lost or long overdue items
365 if ( $item->{itemlost
} ) {
367 # FIXME localized strings should never be in Perl code
369 $item->{itemlost
} == 1 ?
"(lost)"
370 : $item->{itemlost
} == 2 ?
"(long overdue)"
372 $item->{backgroundcolor
} = 'other';
373 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
379 # Check the transit status
380 my ( $transfertwhen, $transfertfrom, $transfertto ) =
381 GetTransfers
($itemnumber);
383 if ( defined $transfertwhen && $transfertwhen ne '' ) {
384 $item->{transfertwhen
} = format_date
($transfertwhen);
385 $item->{transfertfrom
} =
386 $branches->{$transfertfrom}{branchname
};
387 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
388 $item->{nocancel
} = 1;
391 # If there is no loan, return and transfer, we show a checkbox.
392 $item->{notforloan
} = $item->{notforloan
} || 0;
394 # if independent branches is on we need to check if the person can reserve
395 # for branches they arent logged in to
396 if ( C4
::Context
->preference("IndependentBranches") ) {
397 if (! C4
::Context
->preference("canreservefromotherbranches")){
398 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
399 my $userenv = C4
::Context
->userenv;
400 if ( ($userenv) && ( $userenv->{flags
} % 2 != 1 ) ) {
401 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
406 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
408 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
409 my $policy_holdallowed = 1;
411 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
413 if ( $branchitemrule->{'holdallowed'} == 0 ||
414 ( $branchitemrule->{'holdallowed'} == 1 &&
415 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
416 $policy_holdallowed = 0;
421 && !$item->{cantreserve
}
422 && IsAvailableForItemLevelRequest
($itemnumber)
423 && CanItemBeReserved
(
424 $borrowerinfo->{borrowernumber
}, $itemnumber
428 $item->{available
} = 1;
431 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
433 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
434 $item->{override
} = 1;
438 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
440 # FIXME: move this to a pm
441 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
442 $sth2->execute($item->{ReservedForBorrowernumber
},$item->{itemnumber
});
443 while (my $wait_hashref = $sth2->fetchrow_hashref) {
444 $item->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
447 # Show serial enumeration when needed
448 if ($item->{enumchron
}) {
449 $itemdata_enumchron = 1;
452 push @
{ $biblioitem->{itemloop
} }, $item;
455 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
456 $template->param( override_required
=> 1 );
457 } elsif ( $num_available == 0 ) {
458 $template->param( none_available
=> 1 );
459 $template->param( warnings
=> 1 );
460 $biblioloopiter{warn} = 1;
461 $biblioloopiter{none_avail
} = 1;
463 $template->param( hiddencount
=> $hiddencount);
465 push @bibitemloop, $biblioitem;
468 # existingreserves building
470 ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
471 foreach my $res ( sort {
472 my $a_found = $a->{found
} || '';
473 my $b_found = $a->{found
} || '';
474 $a_found cmp $b_found;
478 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
483 selected
=> ( $i == $res->{priority
} ),
488 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
489 my $item = $res->{'itemnumber'};
490 $item = GetBiblioFromItemNumber
($item,undef);
492 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
493 $reserve{'biblionumber'}=$item->{'biblionumber'};
494 $reserve{'barcodenumber'} = $item->{'barcode'};
495 $reserve{'wbrcode'} = $res->{'branchcode'};
496 $reserve{'itemnumber'} = $res->{'itemnumber'};
497 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
498 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
499 $reserve{'atdestination'} = 1;
501 # set found to 1 if reserve is waiting for patron pickup
502 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
503 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
504 } elsif ($res->{priority
} > 0) {
505 if (defined($res->{itemnumber
})) {
506 my $item = GetItem
($res->{itemnumber
});
507 $reserve{'itemnumber'} = $res->{'itemnumber'};
508 $reserve{'barcodenumber'} = $item->{'barcode'};
509 $reserve{'item_level_hold'} = 1;
513 # get borrowers reserve info
514 my $reserveborrowerinfo = GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
515 if (C4
::Context
->preference('HidePatronName')){
516 $reserve{'hidename'} = 1;
517 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
519 $reserve{'expirationdate'} = format_date
( $res->{'expirationdate'} )
520 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
521 $reserve{'date'} = format_date
( $res->{'reservedate'} );
522 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
523 $reserve{'biblionumber'} = $res->{'biblionumber'};
524 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
525 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
526 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
527 $reserve{'notes'} = $res->{'reservenotes'};
529 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
530 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
531 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
532 $reserve{'voldesc'} = $res->{'volumeddesc'};
533 $reserve{'ccode'} = $res->{'ccode'};
534 $reserve{'barcode'} = $res->{'barcode'};
535 $reserve{'priority'} = $res->{'priority'};
536 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
537 $reserve{'optionloop'} = \
@optionloop;
538 $reserve{'suspend'} = $res->{'suspend'};
539 $reserve{'suspend_until'} = $res->{'suspend_until'};
540 $reserve{'reserve_id'} = $res->{'reserve_id'};
542 if ( C4
::Context
->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
543 $reserve{'branchloop'} = [ GetBranchDetail
($res->{'branchcode'}) ];
545 $reserve{'branchloop'} = GetBranchesLoop
($res->{'branchcode'});
548 push( @reserveloop, \
%reserve );
551 # get the time for the form name...
555 branchloop
=> GetBranchesLoop
($userbranch),
557 fixedRank
=> $fixedRank,
562 optionloop
=> \
@optionloop,
563 bibitemloop
=> \
@bibitemloop,
564 itemdata_enumchron
=> $itemdata_enumchron,
566 biblionumber
=> $biblionumber,
567 findborrower
=> $findborrower,
568 title
=> $dat->{title
},
569 author
=> $dat->{author
},
571 C4
::Search
::enabled_staff_search_views
,
573 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
575 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
576 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
580 $biblioloopiter{biblionumber
} = $biblionumber;
581 $biblioloopiter{title
} = $dat->{title
};
582 $biblioloopiter{rank
} = $fixedRank;
583 $biblioloopiter{reserveloop
} = \
@reserveloop;
586 $template->param( reserveloop
=> \
@reserveloop );
589 push @biblioloop, \
%biblioloopiter;
592 $template->param( biblioloop
=> \
@biblioloop );
593 $template->param( biblionumbers
=> $biblionumbers );
596 $template->param( multi_hold
=> 1 );
599 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
600 $template->param( reserve_in_future
=> 1 );
604 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
605 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
609 output_html_with_http_headers
$input, $cookie, $template->output;
611 sub sort_borrowerlist
{
612 my $borrowerslist = shift;
615 uc( $a->{surname
} . $a->{firstname
} ) cmp
616 uc( $b->{surname
} . $b->{firstname
} )