4 #written 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
::Patron
::Debarments
qw(IsDebarred);
51 my $dbh = C4
::Context
->dbh;
53 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user
(
55 template_name
=> "reserve/request.tt",
59 flagsrequired
=> { reserveforothers
=> 'place_holds' },
63 my $multihold = $input->param('multi_hold');
64 $template->param(multi_hold
=> $multihold);
65 my $showallitems = $input->param('showallitems');
67 # get Branches and Itemtypes
68 my $branches = GetBranches
();
69 my $itemtypes = GetItemTypes
();
72 if (C4
::Context
->userenv && C4
::Context
->userenv->{'branch'}) {
73 $userbranch = C4
::Context
->userenv->{'branch'};
77 # Select borrowers infos
78 my $findborrower = $input->param('findborrower');
79 $findborrower = '' unless defined $findborrower;
80 $findborrower =~ s
|,| |g
;
81 my $borrowernumber_hold = $input->param('borrowernumber') || '';
85 my $exceeded_maxreserves;
87 my $date = output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
88 my $action = $input->param('action');
91 if ( $action eq 'move' ) {
92 my $where = $input->param('where');
93 my $reserve_id = $input->param('reserve_id');
94 AlterPriority
( $where, $reserve_id );
95 } elsif ( $action eq 'cancel' ) {
96 my $reserve_id = $input->param('reserve_id');
97 CancelReserve
({ reserve_id
=> $reserve_id });
98 } elsif ( $action eq 'setLowestPriority' ) {
99 my $reserve_id = $input->param('reserve_id');
100 ToggleLowestPriority
( $reserve_id );
101 } elsif ( $action eq 'toggleSuspend' ) {
102 my $reserve_id = $input->param('reserve_id');
103 my $suspend_until = $input->param('suspend_until');
104 ToggleSuspend
( $reserve_id, $suspend_until );
108 my $borrower = C4
::Members
::GetMember
( cardnumber
=> $findborrower );
110 $borrowernumber_hold = $borrower->{borrowernumber
};
112 my $dt_params = { iDisplayLength
=> -1 };
113 my $results = C4
::Utils
::DataTables
::Members
::search
(
115 searchmember
=> $findborrower,
116 dt_params
=> $dt_params,
119 my $borrowers = $results->{patrons
};
120 if ( scalar @
$borrowers == 1 ) {
121 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
122 } elsif ( @
$borrowers ) {
123 $template->param( borrowers
=> $borrowers );
125 $messageborrower = "'$findborrower'";
130 my @biblionumbers = ();
131 my $biblionumbers = $input->param('biblionumbers');
133 @biblionumbers = split '/', $biblionumbers;
135 push @biblionumbers, $input->multi_param('biblionumber');
139 # If we have the borrowernumber because we've performed an action, then we
140 # don't want to try to place another reserve.
141 if ($borrowernumber_hold && !$action) {
142 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
145 # we check the reserves of the borrower, and if he can reserv a document
146 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
149 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
151 my $new_reserves_count = scalar( @biblionumbers );
153 my $maxreserves = C4
::Context
->preference('maxreserves');
155 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
157 my $new_reserves_allowed =
158 $maxreserves - $reserves_count > 0
159 ?
$maxreserves - $reserves_count
162 $exceeded_maxreserves = 1;
164 new_reserves_allowed
=> $new_reserves_allowed,
165 new_reserves_count
=> $new_reserves_count,
166 reserves_count
=> $reserves_count,
167 maxreserves
=> $maxreserves,
171 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
172 my $expiry_date = $borrowerinfo->{dateexpiry
};
173 my $expiry = 0; # flag set if patron account has expired
174 if ($expiry_date and $expiry_date ne '0000-00-00' and
175 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
179 # check if the borrower make the reserv in a different branch
180 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
185 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
186 borrowersurname
=> $borrowerinfo->{'surname'},
187 borrowerfirstname
=> $borrowerinfo->{'firstname'},
188 borrowerstreetaddress
=> $borrowerinfo->{'address'},
189 borrowercity
=> $borrowerinfo->{'city'},
190 borrowerphone
=> $borrowerinfo->{'phone'},
191 borrowermobile
=> $borrowerinfo->{'mobile'},
192 borrowerfax
=> $borrowerinfo->{'fax'},
193 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
194 borroweremail
=> $borrowerinfo->{'email'},
195 borroweremailpro
=> $borrowerinfo->{'emailpro'},
196 borrowercategory
=> $borrowerinfo->{'category'},
197 cardnumber
=> $borrowerinfo->{'cardnumber'},
199 diffbranch
=> $diffbranch,
200 messages
=> $messages,
201 warnings
=> $warnings,
202 restricted
=> IsDebarred
($borrowerinfo->{'borrowernumber'}),
203 amount_outstanding
=> GetMemberAccountRecords
($borrowerinfo->{borrowernumber
}),
207 $template->param( messageborrower
=> $messageborrower );
209 # FIXME launch another time GetMember perhaps until
210 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
212 my $itemdata_enumchron = 0;
214 foreach my $biblionumber (@biblionumbers) {
216 my %biblioloopiter = ();
218 my $dat = GetBiblioData
($biblionumber);
220 my $canReserve = CanBookBeReserved
( $borrowerinfo->{borrowernumber
}, $biblionumber );
222 if ( $canReserve eq 'OK' ) {
224 #All is OK and we can continue
226 elsif ( $canReserve eq 'tooManyReserves' ) {
227 $exceeded_maxreserves = 1;
229 elsif ( $canReserve eq 'ageRestricted' ) {
230 $template->param( $canReserve => 1 );
231 $biblioloopiter{$canReserve} = 1;
234 $biblioloopiter{$canReserve} = 1;
237 my $alreadypossession;
238 if (not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowerinfo->{borrowernumber
},$biblionumber)) {
239 $alreadypossession = 1;
242 # get existing reserves .....
243 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
244 my $count = scalar( @
$reserves );
245 my $totalcount = $count;
247 my $alreadyreserved = 0;
249 foreach my $res (@
$reserves) {
250 if ( defined $res->{found
} ) { # found can be 'W' or 'T'
254 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber
}) && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
259 if ( $holds_count ) {
260 $alreadyreserved = 1;
261 $biblioloopiter{warn} = 1;
262 $biblioloopiter{alreadyres
} = 1;
266 alreadyreserved
=> $alreadyreserved,
267 alreadypossession
=> $alreadypossession,
270 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
271 # make priorities options
274 for ( 1 .. $count + 1 ) {
279 selected
=> ( $_ == $count + 1 ),
283 # adding a fixed value for priority options
284 my $fixedRank = $count+1;
286 my %itemnumbers_of_biblioitem;
289 ## $items is array of 'item' table numbers
290 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
291 @itemnumbers = @
$items;
293 my @hostitems = get_hostitemnumbers_of
($biblionumber);
295 $template->param('hostitemsflag' => 1);
296 push(@itemnumbers, @hostitems);
300 $template->param('noitems' => 1);
301 $biblioloopiter{noitems
} = 1;
304 ## Hash of item number to 'item' table fields
305 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
307 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
308 ## when by definition all of the itemnumber have the same biblioitemnumber
309 foreach my $itemnumber (@itemnumbers) {
310 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
311 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
314 ## Should be same as biblionumber
315 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
317 my $notforloan_label_of = get_notforloan_label_of
();
319 ## Hash of biblioitemnumber to 'biblioitem' table records
320 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
324 my @available_itemtypes;
325 foreach my $biblioitemnumber (@biblioitemnumbers) {
326 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
327 my $num_available = 0;
328 my $num_override = 0;
331 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
332 $biblioitem->{hostitemsflag
} = 1;
335 $biblioloopiter{description
} = $biblioitem->{description
};
336 $biblioloopiter{itypename
} = $biblioitem->{description
};
337 if ( $biblioitem->{itemtype
} ) {
339 $biblioitem->{description
} =
340 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
342 $biblioloopiter{imageurl
} =
343 getitemtypeimagelocation
( 'intranet',
344 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
347 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
348 my $item = $iteminfos_of->{$itemnumber};
350 unless (C4
::Context
->preference('item-level_itypes')) {
351 $item->{itype
} = $biblioitem->{itemtype
};
354 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
355 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
356 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
358 # if the holdingbranch is different than the homebranch, we show the
359 # holdingbranch of the document too
360 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
361 $item->{holdingbranchname
} =
362 $branches->{ $item->{holdingbranch
} }{branchname
};
365 if($item->{biblionumber
} ne $biblionumber){
366 $item->{hostitemsflag
}=1;
367 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
370 # if the item is currently on loan, we display its return date and
371 # change the background color
372 my $issues= GetItemIssue
($itemnumber);
373 if ( $issues->{'date_due'} ) {
374 $item->{date_due
} = $issues->{date_due_sql
};
375 $item->{backgroundcolor
} = 'onloan';
379 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber
($itemnumber);
380 if ( defined $reservedate ) {
381 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
383 $item->{backgroundcolor
} = 'reserved';
384 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $reservedate ), dateonly
=> 1 });
385 $item->{ReservedForBorrowernumber
} = $reservedfor;
386 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
387 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
388 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
389 $item->{waitingdate
} = $wait;
392 # Management of the notforloan document
393 if ( $item->{notforloan
} ) {
394 $item->{backgroundcolor
} = 'other';
395 $item->{notforloanvalue
} =
396 $notforloan_label_of->{ $item->{notforloan
} };
399 # Management of lost or long overdue items
400 if ( $item->{itemlost
} ) {
402 # FIXME localized strings should never be in Perl code
404 $item->{itemlost
} == 1 ?
"(lost)"
405 : $item->{itemlost
} == 2 ?
"(long overdue)"
407 $item->{backgroundcolor
} = 'other';
408 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
414 # Check the transit status
415 my ( $transfertwhen, $transfertfrom, $transfertto ) =
416 GetTransfers
($itemnumber);
418 if ( defined $transfertwhen && $transfertwhen ne '' ) {
419 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
420 $item->{transfertfrom
} =
421 $branches->{$transfertfrom}{branchname
};
422 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
423 $item->{nocancel
} = 1;
426 # If there is no loan, return and transfer, we show a checkbox.
427 $item->{notforloan
} ||= 0;
429 # if independent branches is on we need to check if the person can reserve
430 # for branches they arent logged in to
431 if ( C4
::Context
->preference("IndependentBranches") ) {
432 if (! C4
::Context
->preference("canreservefromotherbranches")){
433 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
434 my $userenv = C4
::Context
->userenv;
435 unless ( C4
::Context
->IsSuperLibrarian ) {
436 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
441 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
443 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
445 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
448 !$item->{cantreserve
}
449 && !$exceeded_maxreserves
450 && IsAvailableForItemLevelRequest
($item, $borrowerinfo)
451 && CanItemBeReserved
(
452 $borrowerinfo->{borrowernumber
}, $itemnumber
456 $item->{available
} = 1;
459 push( @available_itemtypes, $item->{itype
} );
461 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
462 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
463 $item->{override
} = 1;
467 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
469 # Show serial enumeration when needed
470 if ($item->{enumchron
}) {
471 $itemdata_enumchron = 1;
474 push @
{ $biblioitem->{itemloop
} }, $item;
477 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
478 $template->param( override_required
=> 1 );
479 } elsif ( $num_available == 0 ) {
480 $template->param( none_available
=> 1 );
481 $biblioloopiter{warn} = 1;
482 $biblioloopiter{none_avail
} = 1;
484 $template->param( hiddencount
=> $hiddencount);
486 push @bibitemloop, $biblioitem;
489 @available_itemtypes = uniq
( @available_itemtypes );
490 $template->param( available_itemtypes
=> \
@available_itemtypes );
492 # existingreserves building
494 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
497 my $a_found = $a->found() || '';
498 my $b_found = $a->found() || '';
499 $a_found cmp $b_found;
505 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
510 selected
=> ( $i == $res->priority() ),
515 if ( $res->is_found() ) {
516 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
517 $reserve{'biblionumber'} = $res->item()->biblionumber();
518 $reserve{'barcodenumber'} = $res->item()->barcode();
519 $reserve{'wbrcode'} = $res->branchcode();
520 $reserve{'itemnumber'} = $res->itemnumber();
521 $reserve{'wbrname'} = $res->branch()->branchname();
523 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
525 # Just because the holdingbranch matches the reserve branch doesn't mean the item
526 # has arrived at the destination, check for an open transfer for the item as well
527 my ( $transfertwhen, $transfertfrom, $transferto ) =
528 C4
::Circulation
::GetTransfers
( $res->itemnumber() );
529 if ( not $transferto or $transferto ne $res->branchcode() ) {
530 $reserve{'atdestination'} = 1;
534 # set found to 1 if reserve is waiting for patron pickup
535 $reserve{'found'} = $res->is_found();
536 $reserve{'intransit'} = $res->is_in_transit();
538 elsif ( $res->priority() > 0 ) {
539 if ( my $item = $res->item() ) {
540 $reserve{'itemnumber'} = $item->id();
541 $reserve{'barcodenumber'} = $item->barcode();
542 $reserve{'item_level_hold'} = 1;
546 # get borrowers reserve info
547 if ( C4
::Context
->preference('HidePatronName') ) {
548 $reserve{'hidename'} = 1;
549 $reserve{'cardnumber'} = $res->borrower()->cardnumber();
551 $reserve{'expirationdate'} = output_pref
( { dt
=> dt_from_string
( $res->expirationdate ), dateonly
=> 1 } )
552 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
553 $reserve{'date'} = output_pref
( { dt
=> dt_from_string
( $res->reservedate ), dateonly
=> 1 } );
554 $reserve{'borrowernumber'} = $res->borrowernumber();
555 $reserve{'biblionumber'} = $res->biblionumber();
556 $reserve{'borrowernumber'} = $res->borrowernumber();
557 $reserve{'firstname'} = $res->borrower()->firstname();
558 $reserve{'surname'} = $res->borrower()->surname();
559 $reserve{'notes'} = $res->reservenotes();
560 $reserve{'waiting_date'} = $res->waitingdate();
561 $reserve{'waiting_until'} = $res->is_waiting() ?
$res->waiting_expires_on() : undef;
562 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
563 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
564 $reserve{'priority'} = $res->priority();
565 $reserve{'lowestPriority'} = $res->lowestPriority();
566 $reserve{'optionloop'} = \
@optionloop;
567 $reserve{'suspend'} = $res->suspend();
568 $reserve{'suspend_until'} = $res->suspend_until();
569 $reserve{'reserve_id'} = $res->reserve_id();
570 $reserve{itemtype
} = $res->itemtype();
572 if ( C4
::Context
->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
573 $reserve{'branchloop'} = [ Koha
::Libraries
->find( $res->branchcode() ) ];
576 $reserve{'branchloop'} = GetBranchesLoop
( $res->branchcode() );
579 push( @reserveloop, \
%reserve );
582 # get the time for the form name...
586 branchloop
=> GetBranchesLoop
($userbranch),
588 fixedRank
=> $fixedRank,
593 optionloop
=> \
@optionloop,
594 bibitemloop
=> \
@bibitemloop,
595 itemdata_enumchron
=> $itemdata_enumchron,
597 biblionumber
=> $biblionumber,
598 findborrower
=> $findborrower,
599 title
=> $dat->{title
},
600 author
=> $dat->{author
},
602 C4
::Search
::enabled_staff_search_views
,
604 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
606 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
607 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
611 $biblioloopiter{biblionumber
} = $biblionumber;
612 $biblioloopiter{title
} = $dat->{title
};
613 $biblioloopiter{rank
} = $fixedRank;
614 $biblioloopiter{reserveloop
} = \
@reserveloop;
617 $template->param( reserveloop
=> \
@reserveloop );
620 push @biblioloop, \
%biblioloopiter;
623 $template->param( biblioloop
=> \
@biblioloop );
624 $template->param( biblionumbers
=> $biblionumbers );
625 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
628 $template->param( multi_hold
=> 1 );
631 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
632 $template->param( reserve_in_future
=> 1 );
636 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
637 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
641 output_html_with_http_headers
$input, $cookie, $template->output;
643 sub sort_borrowerlist
{
644 my $borrowerslist = shift;
647 uc( $a->{surname
} . $a->{firstname
} ) cmp
648 uc( $b->{surname
} . $b->{firstname
} )