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/;
43 use C4
::Utils
::DataTables
::Members
;
45 use C4
::Search
; # enabled_staff_search_views
47 use Koha
::Borrower
::Debarments
qw(IsDebarred);
49 my $dbh = C4
::Context
->dbh;
51 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user
(
53 template_name
=> "reserve/request.tt",
57 flagsrequired
=> { reserveforothers
=> 'place_holds' },
61 my $multihold = $input->param('multi_hold');
62 $template->param(multi_hold
=> $multihold);
63 my $showallitems = $input->param('showallitems');
65 # get Branches and Itemtypes
66 my $branches = GetBranches
();
67 my $itemtypes = GetItemTypes
();
70 if (C4
::Context
->userenv && C4
::Context
->userenv->{'branch'}) {
71 $userbranch = C4
::Context
->userenv->{'branch'};
75 # Select borrowers infos
76 my $findborrower = $input->param('findborrower');
77 $findborrower = '' unless defined $findborrower;
78 $findborrower =~ s
|,| |g
;
79 my $borrowernumber_hold = $input->param('borrowernumber') || '';
83 my $exceeded_maxreserves;
85 my $date = output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
86 my $action = $input->param('action');
89 if ( $action eq 'move' ) {
90 my $where = $input->param('where');
91 my $reserve_id = $input->param('reserve_id');
92 AlterPriority
( $where, $reserve_id );
93 } elsif ( $action eq 'cancel' ) {
94 my $reserve_id = $input->param('reserve_id');
95 CancelReserve
({ reserve_id
=> $reserve_id });
96 } elsif ( $action eq 'setLowestPriority' ) {
97 my $reserve_id = $input->param('reserve_id');
98 ToggleLowestPriority
( $reserve_id );
99 } elsif ( $action eq 'toggleSuspend' ) {
100 my $reserve_id = $input->param('reserve_id');
101 my $suspend_until = $input->param('suspend_until');
102 ToggleSuspend
( $reserve_id, $suspend_until );
106 my $borrower = C4
::Members
::GetMember
( cardnumber
=> $findborrower );
108 $borrowernumber_hold = $borrower->{borrowernumber
};
110 my $dt_params = { iDisplayLength
=> -1 };
111 my $results = C4
::Utils
::DataTables
::Members
::search
(
113 searchmember
=> $findborrower,
114 dt_params
=> $dt_params,
117 my $borrowers = $results->{patrons
};
118 if ( scalar @
$borrowers == 1 ) {
119 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
120 } elsif ( @
$borrowers ) {
121 $template->param( borrowers
=> $borrowers );
123 $messageborrower = "'$findborrower'";
128 my @biblionumbers = ();
129 my $biblionumbers = $input->param('biblionumbers');
131 @biblionumbers = split '/', $biblionumbers;
133 push @biblionumbers, $input->param('biblionumber');
137 # If we have the borrowernumber because we've performed an action, then we
138 # don't want to try to place another reserve.
139 if ($borrowernumber_hold && !$action) {
140 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
143 # we check the reserves of the borrower, and if he can reserv a document
144 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
147 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
149 my $new_reserves_count = scalar( @biblionumbers );
151 my $maxreserves = C4
::Context
->preference('maxreserves');
153 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
155 my $new_reserves_allowed =
156 $maxreserves - $reserves_count > 0
157 ?
$maxreserves - $reserves_count
160 $exceeded_maxreserves = 1;
162 new_reserves_allowed
=> $new_reserves_allowed,
163 new_reserves_count
=> $new_reserves_count,
164 reserves_count
=> $reserves_count,
165 maxreserves
=> $maxreserves,
169 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
170 my $expiry_date = $borrowerinfo->{dateexpiry
};
171 my $expiry = 0; # flag set if patron account has expired
172 if ($expiry_date and $expiry_date ne '0000-00-00' and
173 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
177 # check if the borrower make the reserv in a different branch
178 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
183 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
184 borrowersurname
=> $borrowerinfo->{'surname'},
185 borrowerfirstname
=> $borrowerinfo->{'firstname'},
186 borrowerstreetaddress
=> $borrowerinfo->{'address'},
187 borrowercity
=> $borrowerinfo->{'city'},
188 borrowerphone
=> $borrowerinfo->{'phone'},
189 borrowermobile
=> $borrowerinfo->{'mobile'},
190 borrowerfax
=> $borrowerinfo->{'fax'},
191 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
192 borroweremail
=> $borrowerinfo->{'email'},
193 borroweremailpro
=> $borrowerinfo->{'emailpro'},
194 borrowercategory
=> $borrowerinfo->{'category'},
195 cardnumber
=> $borrowerinfo->{'cardnumber'},
197 diffbranch
=> $diffbranch,
198 messages
=> $messages,
199 warnings
=> $warnings,
200 restricted
=> IsDebarred
($borrowerinfo->{'borrowernumber'}),
201 amount_outstanding
=> GetMemberAccountRecords
($borrowerinfo->{borrowernumber
}),
205 $template->param( messageborrower
=> $messageborrower );
207 # FIXME launch another time GetMember perhaps until
208 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
210 my $itemdata_enumchron = 0;
212 foreach my $biblionumber (@biblionumbers) {
214 my %biblioloopiter = ();
216 my $dat = GetBiblioData
($biblionumber);
218 my $canReserve = CanBookBeReserved
( $borrowerinfo->{borrowernumber
}, $biblionumber );
220 if ( $canReserve eq 'OK' ) {
222 #All is OK and we can continue
224 elsif ( $canReserve eq 'tooManyReserves' ) {
225 $exceeded_maxreserves = 1;
227 elsif ( $canReserve eq 'ageRestricted' ) {
228 $template->param( $canReserve => 1 );
229 $biblioloopiter{$canReserve} = 1;
232 $biblioloopiter{$canReserve} = 1;
235 my $alreadypossession;
236 if (not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowerinfo->{borrowernumber
},$biblionumber)) {
237 $alreadypossession = 1;
240 # get existing reserves .....
241 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
242 my $count = scalar( @
$reserves );
243 my $totalcount = $count;
245 my $alreadyreserved = 0;
247 foreach my $res (@
$reserves) {
248 if ( defined $res->{found
} ) { # found can be 'W' or 'T'
252 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber
}) && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
257 if ( $holds_count ) {
258 $alreadyreserved = 1;
259 $biblioloopiter{warn} = 1;
260 $biblioloopiter{alreadyres
} = 1;
264 alreadyreserved
=> $alreadyreserved,
265 alreadypossession
=> $alreadypossession,
268 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
269 # make priorities options
272 for ( 1 .. $count + 1 ) {
277 selected
=> ( $_ == $count + 1 ),
281 # adding a fixed value for priority options
282 my $fixedRank = $count+1;
284 my %itemnumbers_of_biblioitem;
287 ## $items is array of 'item' table numbers
288 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
289 @itemnumbers = @
$items;
291 my @hostitems = get_hostitemnumbers_of
($biblionumber);
293 $template->param('hostitemsflag' => 1);
294 push(@itemnumbers, @hostitems);
298 $template->param('noitems' => 1);
299 $biblioloopiter{noitems
} = 1;
302 ## Hash of item number to 'item' table fields
303 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
305 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
306 ## when by definition all of the itemnumber have the same biblioitemnumber
307 foreach my $itemnumber (@itemnumbers) {
308 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
309 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
312 ## Should be same as biblionumber
313 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
315 my $notforloan_label_of = get_notforloan_label_of
();
317 ## Hash of biblioitemnumber to 'biblioitem' table records
318 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
322 foreach my $biblioitemnumber (@biblioitemnumbers) {
323 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
324 my $num_available = 0;
325 my $num_override = 0;
328 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
329 $biblioitem->{hostitemsflag
} = 1;
332 $biblioloopiter{description
} = $biblioitem->{description
};
333 $biblioloopiter{itypename
} = $biblioitem->{description
};
334 if ( $biblioitem->{itemtype
} ) {
336 $biblioitem->{description
} =
337 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
339 $biblioloopiter{imageurl
} =
340 getitemtypeimagelocation
( 'intranet',
341 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
344 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
345 my $item = $iteminfos_of->{$itemnumber};
347 unless (C4
::Context
->preference('item-level_itypes')) {
348 $item->{itype
} = $biblioitem->{itemtype
};
351 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
352 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
353 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
355 # if the holdingbranch is different than the homebranch, we show the
356 # holdingbranch of the document too
357 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
358 $item->{holdingbranchname
} =
359 $branches->{ $item->{holdingbranch
} }{branchname
};
362 if($item->{biblionumber
} ne $biblionumber){
363 $item->{hostitemsflag
}=1;
364 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
367 # if the item is currently on loan, we display its return date and
368 # change the background color
369 my $issues= GetItemIssue
($itemnumber);
370 if ( $issues->{'date_due'} ) {
371 $item->{date_due
} = $issues->{date_due_sql
};
372 $item->{backgroundcolor
} = 'onloan';
376 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber
($itemnumber);
377 if ( defined $reservedate ) {
378 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
380 $item->{backgroundcolor
} = 'reserved';
381 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $reservedate ), dateonly
=> 1 });
382 $item->{ReservedForBorrowernumber
} = $reservedfor;
383 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
384 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
385 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
386 $item->{waitingdate
} = $wait;
389 # Management of the notforloan document
390 if ( $item->{notforloan
} ) {
391 $item->{backgroundcolor
} = 'other';
392 $item->{notforloanvalue
} =
393 $notforloan_label_of->{ $item->{notforloan
} };
396 # Management of lost or long overdue items
397 if ( $item->{itemlost
} ) {
399 # FIXME localized strings should never be in Perl code
401 $item->{itemlost
} == 1 ?
"(lost)"
402 : $item->{itemlost
} == 2 ?
"(long overdue)"
404 $item->{backgroundcolor
} = 'other';
405 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
411 # Check the transit status
412 my ( $transfertwhen, $transfertfrom, $transfertto ) =
413 GetTransfers
($itemnumber);
415 if ( defined $transfertwhen && $transfertwhen ne '' ) {
416 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
417 $item->{transfertfrom
} =
418 $branches->{$transfertfrom}{branchname
};
419 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
420 $item->{nocancel
} = 1;
423 # If there is no loan, return and transfer, we show a checkbox.
424 $item->{notforloan
} ||= 0;
426 # if independent branches is on we need to check if the person can reserve
427 # for branches they arent logged in to
428 if ( C4
::Context
->preference("IndependentBranches") ) {
429 if (! C4
::Context
->preference("canreservefromotherbranches")){
430 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
431 my $userenv = C4
::Context
->userenv;
432 unless ( C4
::Context
->IsSuperLibrarian ) {
433 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
438 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
440 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
442 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
445 !$item->{cantreserve
}
446 && !$exceeded_maxreserves
447 && IsAvailableForItemLevelRequest
($item, $borrowerinfo)
448 && CanItemBeReserved
(
449 $borrowerinfo->{borrowernumber
}, $itemnumber
453 $item->{available
} = 1;
456 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
457 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
458 $item->{override
} = 1;
462 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
464 # Show serial enumeration when needed
465 if ($item->{enumchron
}) {
466 $itemdata_enumchron = 1;
469 push @
{ $biblioitem->{itemloop
} }, $item;
472 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
473 $template->param( override_required
=> 1 );
474 } elsif ( $num_available == 0 ) {
475 $template->param( none_available
=> 1 );
476 $biblioloopiter{warn} = 1;
477 $biblioloopiter{none_avail
} = 1;
479 $template->param( hiddencount
=> $hiddencount);
481 push @bibitemloop, $biblioitem;
484 # existingreserves building
486 $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
487 foreach my $res ( sort {
488 my $a_found = $a->{found
} || '';
489 my $b_found = $a->{found
} || '';
490 $a_found cmp $b_found;
494 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
499 selected
=> ( $i == $res->{priority
} ),
504 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
505 my $item = $res->{'itemnumber'};
506 $item = GetBiblioFromItemNumber
($item,undef);
508 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
509 $reserve{'biblionumber'}=$item->{'biblionumber'};
510 $reserve{'barcodenumber'} = $item->{'barcode'};
511 $reserve{'wbrcode'} = $res->{'branchcode'};
512 $reserve{'itemnumber'} = $res->{'itemnumber'};
513 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
514 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
515 # Just because the holdingbranch matches the reserve branch doesn't mean the item
516 # has arrived at the destination, check for an open transfer for the item as well
517 my ( $transfertwhen, $transfertfrom, $transferto ) = C4
::Circulation
::GetTransfers
( $res->{itemnumber
} );
518 if ( not $transferto or $transferto ne $res->{branchcode
} ) {
519 $reserve{'atdestination'} = 1;
522 # set found to 1 if reserve is waiting for patron pickup
523 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
524 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
525 } elsif ($res->{priority
} > 0) {
526 if (defined($res->{itemnumber
})) {
527 my $item = GetItem
($res->{itemnumber
});
528 $reserve{'itemnumber'} = $res->{'itemnumber'};
529 $reserve{'barcodenumber'} = $item->{'barcode'};
530 $reserve{'item_level_hold'} = 1;
534 # get borrowers reserve info
535 my $reserveborrowerinfo = GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
536 if (C4
::Context
->preference('HidePatronName')){
537 $reserve{'hidename'} = 1;
538 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
540 $reserve{'expirationdate'} = output_pref
({ dt
=> dt_from_string
( $res->{'expirationdate'} ), dateonly
=> 1 })
541 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
542 $reserve{'date'} = output_pref
({ dt
=> dt_from_string
( $res->{'reservedate'} ), dateonly
=> 1 });
543 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
544 $reserve{'biblionumber'} = $res->{'biblionumber'};
545 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
546 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
547 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
548 $reserve{'notes'} = $res->{'reservenotes'};
550 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
551 $reserve{'voldesc'} = $res->{'volumeddesc'};
552 $reserve{'ccode'} = $res->{'ccode'};
553 $reserve{'barcode'} = $res->{'barcode'};
554 $reserve{'priority'} = $res->{'priority'};
555 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
556 $reserve{'optionloop'} = \
@optionloop;
557 $reserve{'suspend'} = $res->{'suspend'};
558 $reserve{'suspend_until'} = $res->{'suspend_until'};
559 $reserve{'reserve_id'} = $res->{'reserve_id'};
561 if ( C4
::Context
->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
562 $reserve{'branchloop'} = [ GetBranchDetail
($res->{'branchcode'}) ];
564 $reserve{'branchloop'} = GetBranchesLoop
($res->{'branchcode'});
567 push( @reserveloop, \
%reserve );
570 # get the time for the form name...
574 branchloop
=> GetBranchesLoop
($userbranch),
576 fixedRank
=> $fixedRank,
581 optionloop
=> \
@optionloop,
582 bibitemloop
=> \
@bibitemloop,
583 itemdata_enumchron
=> $itemdata_enumchron,
585 biblionumber
=> $biblionumber,
586 findborrower
=> $findborrower,
587 title
=> $dat->{title
},
588 author
=> $dat->{author
},
590 C4
::Search
::enabled_staff_search_views
,
592 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
594 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
595 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
599 $biblioloopiter{biblionumber
} = $biblionumber;
600 $biblioloopiter{title
} = $dat->{title
};
601 $biblioloopiter{rank
} = $fixedRank;
602 $biblioloopiter{reserveloop
} = \
@reserveloop;
605 $template->param( reserveloop
=> \
@reserveloop );
608 push @biblioloop, \
%biblioloopiter;
611 $template->param( biblioloop
=> \
@biblioloop );
612 $template->param( biblionumbers
=> $biblionumbers );
613 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
616 $template->param( multi_hold
=> 1 );
619 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
620 $template->param( reserve_in_future
=> 1 );
624 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
625 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
629 output_html_with_http_headers
$input, $cookie, $template->output;
631 sub sort_borrowerlist
{
632 my $borrowerslist = shift;
635 uc( $a->{surname
} . $a->{firstname
} ) cmp
636 uc( $b->{surname
} . $b->{firstname
} )