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
32 use List
::MoreUtils qw
/uniq/;
33 use Date
::Calc qw
/Date_to_Days/;
42 use C4
::Utils
::DataTables
::Members
;
44 use C4
::Search
; # enabled_staff_search_views
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 my $itemtypes = GetItemTypes
();
67 # Select borrowers infos
68 my $findborrower = $input->param('findborrower');
69 $findborrower = '' unless defined $findborrower;
70 $findborrower =~ s
|,| |g
;
71 my $borrowernumber_hold = $input->param('borrowernumber') || '';
75 my $exceeded_maxreserves;
76 my $exceeded_holds_per_record;
78 my $date = output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
79 my $action = $input->param('action');
82 if ( $action eq 'move' ) {
83 my $where = $input->param('where');
84 my $reserve_id = $input->param('reserve_id');
85 AlterPriority
( $where, $reserve_id );
86 } elsif ( $action eq 'cancel' ) {
87 my $reserve_id = $input->param('reserve_id');
88 CancelReserve
({ reserve_id
=> $reserve_id });
89 } elsif ( $action eq 'setLowestPriority' ) {
90 my $reserve_id = $input->param('reserve_id');
91 ToggleLowestPriority
( $reserve_id );
92 } elsif ( $action eq 'toggleSuspend' ) {
93 my $reserve_id = $input->param('reserve_id');
94 my $suspend_until = $input->param('suspend_until');
95 ToggleSuspend
( $reserve_id, $suspend_until );
99 my $borrower = C4
::Members
::GetMember
( cardnumber
=> $findborrower );
101 $borrowernumber_hold = $borrower->{borrowernumber
};
103 my $dt_params = { iDisplayLength
=> -1 };
104 my $results = C4
::Utils
::DataTables
::Members
::search
(
106 searchmember
=> $findborrower,
107 dt_params
=> $dt_params,
110 my $borrowers = $results->{patrons
};
111 if ( scalar @
$borrowers == 1 ) {
112 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
113 } elsif ( @
$borrowers ) {
114 $template->param( borrowers
=> $borrowers );
116 $messageborrower = "'$findborrower'";
121 my @biblionumbers = ();
122 my $biblionumbers = $input->param('biblionumbers');
124 @biblionumbers = split '/', $biblionumbers;
126 push @biblionumbers, $input->multi_param('biblionumber');
130 # If we have the borrowernumber because we've performed an action, then we
131 # don't want to try to place another reserve.
132 if ($borrowernumber_hold && !$action) {
133 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
136 # we check the reserves of the borrower, and if he can reserv a document
137 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
140 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
142 my $new_reserves_count = scalar( @biblionumbers );
144 my $maxreserves = C4
::Context
->preference('maxreserves');
146 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
148 my $new_reserves_allowed =
149 $maxreserves - $reserves_count > 0
150 ?
$maxreserves - $reserves_count
153 $exceeded_maxreserves = 1;
155 new_reserves_allowed
=> $new_reserves_allowed,
156 new_reserves_count
=> $new_reserves_count,
157 reserves_count
=> $reserves_count,
158 maxreserves
=> $maxreserves,
162 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
163 my $expiry_date = $borrowerinfo->{dateexpiry
};
164 my $expiry = 0; # flag set if patron account has expired
165 if ($expiry_date and $expiry_date ne '0000-00-00' and
166 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
170 # check if the borrower make the reserv in a different branch
171 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
175 my $is_debarred = Koha
::Patrons
->find( $borrowerinfo->{borrowernumber
} )->is_debarred;
177 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
178 borrowersurname
=> $borrowerinfo->{'surname'},
179 borrowerfirstname
=> $borrowerinfo->{'firstname'},
180 borrowerstreetaddress
=> $borrowerinfo->{'address'},
181 borrowercity
=> $borrowerinfo->{'city'},
182 borrowerphone
=> $borrowerinfo->{'phone'},
183 borrowermobile
=> $borrowerinfo->{'mobile'},
184 borrowerfax
=> $borrowerinfo->{'fax'},
185 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
186 borroweremail
=> $borrowerinfo->{'email'},
187 borroweremailpro
=> $borrowerinfo->{'emailpro'},
188 borrowercategory
=> $borrowerinfo->{'category'},
189 cardnumber
=> $borrowerinfo->{'cardnumber'},
191 diffbranch
=> $diffbranch,
192 messages
=> $messages,
193 warnings
=> $warnings,
194 restricted
=> $is_debarred,
195 amount_outstanding
=> GetMemberAccountRecords
($borrowerinfo->{borrowernumber
}),
199 $template->param( messageborrower
=> $messageborrower );
201 # FIXME launch another time GetMember perhaps until
202 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
204 my $itemdata_enumchron = 0;
206 foreach my $biblionumber (@biblionumbers) {
207 next unless $biblionumber =~ m
|^\d
+$|;
209 my %biblioloopiter = ();
211 my $dat = GetBiblioData
($biblionumber);
213 my $canReserve = CanBookBeReserved
( $borrowerinfo->{borrowernumber
}, $biblionumber );
215 if ( $canReserve eq 'OK' ) {
217 #All is OK and we can continue
219 elsif ( $canReserve eq 'tooManyReserves' ) {
220 $exceeded_maxreserves = 1;
222 elsif ( $canReserve eq 'tooManyHoldsForThisRecord' ) {
223 $exceeded_holds_per_record = 1;
224 $biblioloopiter{$canReserve} = 1;
226 elsif ( $canReserve eq 'ageRestricted' ) {
227 $template->param( $canReserve => 1 );
228 $biblioloopiter{$canReserve} = 1;
231 $biblioloopiter{$canReserve} = 1;
234 my $force_hold_level;
235 if ( $borrowerinfo->{borrowernumber
} ) {
236 # For multiple holds per record, if a patron has previously placed a hold,
237 # the patron can only place more holds of the same type. That is, if the
238 # patron placed a record level hold, all the holds the patron places must
239 # be record level. If the patron placed an item level hold, all holds
240 # the patron places must be item level
241 my $holds = Koha
::Holds
->search(
243 borrowernumber
=> $borrowerinfo->{borrowernumber
},
244 biblionumber
=> $biblionumber,
248 $force_hold_level = $holds->forced_hold_level();
249 $biblioloopiter{force_hold_level
} = $force_hold_level;
250 $template->param( force_hold_level
=> $force_hold_level );
252 # For a librarian to be able to place multiple record holds for a patron for a record,
253 # we must find out what the maximum number of holds they can place for the patron is
254 my $max_holds_for_record = GetMaxPatronHoldsForRecord
( $borrowerinfo->{borrowernumber
}, $biblionumber );
255 my $remaining_holds_for_record = $max_holds_for_record - $holds->count();
256 $biblioloopiter{remaining_holds_for_record
} = $max_holds_for_record;
257 $template->param( max_holds_for_record
=> $max_holds_for_record );
258 $template->param( remaining_holds_for_record
=> $remaining_holds_for_record );
261 # Check to see if patron is allowed to place holds on records where the
262 # patron already has an item from that record checked out
263 my $alreadypossession;
264 if ( !C4
::Context
->preference('AllowHoldsOnPatronsPossessions')
265 && CheckIfIssuedToPatron
( $borrowerinfo->{borrowernumber
}, $biblionumber ) )
267 $template->param( alreadypossession
=> $alreadypossession, );
271 my $count = Koha
::Holds
->search( { biblionumber
=> $biblionumber } )->count();
272 my $totalcount = $count;
274 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
275 # make priorities options
278 for ( 1 .. $count + 1 ) {
283 selected
=> ( $_ == $count + 1 ),
287 # adding a fixed value for priority options
288 my $fixedRank = $count+1;
290 my %itemnumbers_of_biblioitem;
293 ## $items is array of 'item' table numbers
294 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
295 @itemnumbers = @
$items;
297 my @hostitems = get_hostitemnumbers_of
($biblionumber);
299 $template->param('hostitemsflag' => 1);
300 push(@itemnumbers, @hostitems);
304 $template->param('noitems' => 1);
305 $biblioloopiter{noitems
} = 1;
308 ## Hash of item number to 'item' table fields
309 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
311 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
312 ## when by definition all of the itemnumber have the same biblioitemnumber
313 foreach my $itemnumber (@itemnumbers) {
314 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
315 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
318 ## Should be same as biblionumber
319 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
321 my $notforloan_label_of = get_notforloan_label_of
();
323 ## Hash of biblioitemnumber to 'biblioitem' table records
324 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
328 my @available_itemtypes;
329 foreach my $biblioitemnumber (@biblioitemnumbers) {
330 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
331 my $num_available = 0;
332 my $num_override = 0;
335 $biblioitem->{force_hold_level
} = $force_hold_level;
337 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
338 $biblioitem->{hostitemsflag
} = 1;
341 $biblioloopiter{description
} = $biblioitem->{description
};
342 $biblioloopiter{itypename
} = $biblioitem->{description
};
343 if ( $biblioitem->{itemtype
} ) {
345 $biblioitem->{description
} =
346 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
348 $biblioloopiter{imageurl
} =
349 getitemtypeimagelocation
( 'intranet',
350 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
353 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
354 my $item = $iteminfos_of->{$itemnumber};
356 $item->{force_hold_level
} = $force_hold_level;
358 unless (C4
::Context
->preference('item-level_itypes')) {
359 $item->{itype
} = $biblioitem->{itemtype
};
362 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
363 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
364 $item->{homebranch
} = $item->{homebranch
};
366 # if the holdingbranch is different than the homebranch, we show the
367 # holdingbranch of the document too
368 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
369 $item->{holdingbranch
} = $item->{holdingbranch
};
372 if($item->{biblionumber
} ne $biblionumber){
373 $item->{hostitemsflag
}=1;
374 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
377 # if the item is currently on loan, we display its return date and
378 # change the background color
379 my $issues= GetItemIssue
($itemnumber);
380 if ( $issues->{'date_due'} ) {
381 $item->{date_due
} = $issues->{date_due_sql
};
382 $item->{backgroundcolor
} = 'onloan';
386 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber
($itemnumber);
387 if ( defined $reservedate ) {
388 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
390 $item->{backgroundcolor
} = 'reserved';
391 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $reservedate ), dateonly
=> 1 });
392 $item->{ReservedForBorrowernumber
} = $reservedfor;
393 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
394 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
395 $item->{ExpectedAtLibrary
} = $expectedAt;
396 $item->{waitingdate
} = $wait;
399 # Management of the notforloan document
400 if ( $item->{notforloan
} ) {
401 $item->{backgroundcolor
} = 'other';
402 $item->{notforloanvalue
} =
403 $notforloan_label_of->{ $item->{notforloan
} };
406 # Management of lost or long overdue items
407 if ( $item->{itemlost
} ) {
409 # FIXME localized strings should never be in Perl code
411 $item->{itemlost
} == 1 ?
"(lost)"
412 : $item->{itemlost
} == 2 ?
"(long overdue)"
414 $item->{backgroundcolor
} = 'other';
415 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
421 # Check the transit status
422 my ( $transfertwhen, $transfertfrom, $transfertto ) =
423 GetTransfers
($itemnumber);
425 if ( defined $transfertwhen && $transfertwhen ne '' ) {
426 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
427 $item->{transfertfrom
} = $transfertfrom;
428 $item->{transfertto
} = $transfertto;
429 $item->{nocancel
} = 1;
432 # If there is no loan, return and transfer, we show a checkbox.
433 $item->{notforloan
} ||= 0;
435 # if independent branches is on we need to check if the person can reserve
436 # for branches they arent logged in to
437 if ( C4
::Context
->preference("IndependentBranches") ) {
438 if (! C4
::Context
->preference("canreservefromotherbranches")){
439 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
440 my $userenv = C4
::Context
->userenv;
441 unless ( C4
::Context
->IsSuperLibrarian ) {
442 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
447 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
449 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
451 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
453 my $can_item_be_reserved = CanItemBeReserved
( $borrowerinfo->{borrowernumber
}, $itemnumber );
454 $item->{not_holdable
} = $can_item_be_reserved unless ( $can_item_be_reserved eq 'OK' );
456 $item->{item_level_holds
} = OPACItemHoldsAllowed
( $item, $borrowerinfo );
459 !$item->{cantreserve
}
460 && !$exceeded_maxreserves
461 && IsAvailableForItemLevelRequest
($item, $borrowerinfo)
462 && $can_item_be_reserved eq 'OK'
465 $item->{available
} = 1;
468 push( @available_itemtypes, $item->{itype
} );
470 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
471 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
472 $item->{override
} = 1;
476 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
478 # Show serial enumeration when needed
479 if ($item->{enumchron
}) {
480 $itemdata_enumchron = 1;
483 push @
{ $biblioitem->{itemloop
} }, $item;
486 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
487 $template->param( override_required
=> 1 );
488 } elsif ( $num_available == 0 ) {
489 $template->param( none_available
=> 1 );
490 $biblioloopiter{warn} = 1;
491 $biblioloopiter{none_avail
} = 1;
493 $template->param( hiddencount
=> $hiddencount);
495 push @bibitemloop, $biblioitem;
498 @available_itemtypes = uniq
( @available_itemtypes );
499 $template->param( available_itemtypes
=> \
@available_itemtypes );
501 # existingreserves building
503 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
506 my $a_found = $a->found() || '';
507 my $b_found = $a->found() || '';
508 $a_found cmp $b_found;
514 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
519 selected
=> ( $i == $res->priority() ),
524 if ( $res->is_found() ) {
525 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
526 $reserve{'biblionumber'} = $res->item()->biblionumber();
527 $reserve{'barcodenumber'} = $res->item()->barcode();
528 $reserve{'wbrcode'} = $res->branchcode();
529 $reserve{'itemnumber'} = $res->itemnumber();
530 $reserve{'wbrname'} = $res->branch()->branchname();
532 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
534 # Just because the holdingbranch matches the reserve branch doesn't mean the item
535 # has arrived at the destination, check for an open transfer for the item as well
536 my ( $transfertwhen, $transfertfrom, $transferto ) =
537 C4
::Circulation
::GetTransfers
( $res->itemnumber() );
538 if ( not $transferto or $transferto ne $res->branchcode() ) {
539 $reserve{'atdestination'} = 1;
543 # set found to 1 if reserve is waiting for patron pickup
544 $reserve{'found'} = $res->is_found();
545 $reserve{'intransit'} = $res->is_in_transit();
547 elsif ( $res->priority() > 0 ) {
548 if ( my $item = $res->item() ) {
549 $reserve{'itemnumber'} = $item->id();
550 $reserve{'barcodenumber'} = $item->barcode();
551 $reserve{'item_level_hold'} = 1;
555 # get borrowers reserve info
556 if ( C4
::Context
->preference('HidePatronName') ) {
557 $reserve{'hidename'} = 1;
558 $reserve{'cardnumber'} = $res->borrower()->cardnumber();
560 $reserve{'expirationdate'} = output_pref
( { dt
=> dt_from_string
( $res->expirationdate ), dateonly
=> 1 } )
561 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
562 $reserve{'date'} = output_pref
( { dt
=> dt_from_string
( $res->reservedate ), dateonly
=> 1 } );
563 $reserve{'borrowernumber'} = $res->borrowernumber();
564 $reserve{'biblionumber'} = $res->biblionumber();
565 $reserve{'borrowernumber'} = $res->borrowernumber();
566 $reserve{'firstname'} = $res->borrower()->firstname();
567 $reserve{'surname'} = $res->borrower()->surname();
568 $reserve{'notes'} = $res->reservenotes();
569 $reserve{'waiting_date'} = $res->waitingdate();
570 $reserve{'waiting_until'} = $res->is_waiting() ?
$res->waiting_expires_on() : undef;
571 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
572 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
573 $reserve{'priority'} = $res->priority();
574 $reserve{'lowestPriority'} = $res->lowestPriority();
575 $reserve{'optionloop'} = \
@optionloop;
576 $reserve{'suspend'} = $res->suspend();
577 $reserve{'suspend_until'} = $res->suspend_until();
578 $reserve{'reserve_id'} = $res->reserve_id();
579 $reserve{itemtype
} = $res->itemtype();
580 $reserve{branchcode
} = $res->branchcode();
582 push( @reserveloop, \
%reserve );
585 # get the time for the form name...
590 fixedRank
=> $fixedRank,
595 optionloop
=> \
@optionloop,
596 bibitemloop
=> \
@bibitemloop,
597 itemdata_enumchron
=> $itemdata_enumchron,
599 biblionumber
=> $biblionumber,
600 findborrower
=> $findborrower,
601 title
=> $dat->{title
},
602 author
=> $dat->{author
},
604 C4
::Search
::enabled_staff_search_views
,
606 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
607 $template->param( borrower_branchcode
=> $borrowerinfo->{'branchcode'},);
610 $biblioloopiter{biblionumber
} = $biblionumber;
611 $biblioloopiter{title
} = $dat->{title
};
612 $biblioloopiter{rank
} = $fixedRank;
613 $biblioloopiter{reserveloop
} = \
@reserveloop;
616 $template->param( reserveloop
=> \
@reserveloop );
619 push @biblioloop, \
%biblioloopiter;
622 $template->param( biblioloop
=> \
@biblioloop );
623 $template->param( biblionumbers
=> $biblionumbers );
624 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
625 $template->param( exceeded_holds_per_record
=> $exceeded_holds_per_record );
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
} )