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->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 foreach my $biblioitemnumber (@biblioitemnumbers) {
325 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
326 my $num_available = 0;
327 my $num_override = 0;
330 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
331 $biblioitem->{hostitemsflag
} = 1;
334 $biblioloopiter{description
} = $biblioitem->{description
};
335 $biblioloopiter{itypename
} = $biblioitem->{description
};
336 if ( $biblioitem->{itemtype
} ) {
338 $biblioitem->{description
} =
339 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
341 $biblioloopiter{imageurl
} =
342 getitemtypeimagelocation
( 'intranet',
343 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
346 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
347 my $item = $iteminfos_of->{$itemnumber};
349 unless (C4
::Context
->preference('item-level_itypes')) {
350 $item->{itype
} = $biblioitem->{itemtype
};
353 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
354 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
355 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
357 # if the holdingbranch is different than the homebranch, we show the
358 # holdingbranch of the document too
359 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
360 $item->{holdingbranchname
} =
361 $branches->{ $item->{holdingbranch
} }{branchname
};
364 if($item->{biblionumber
} ne $biblionumber){
365 $item->{hostitemsflag
}=1;
366 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
369 # if the item is currently on loan, we display its return date and
370 # change the background color
371 my $issues= GetItemIssue
($itemnumber);
372 if ( $issues->{'date_due'} ) {
373 $item->{date_due
} = $issues->{date_due_sql
};
374 $item->{backgroundcolor
} = 'onloan';
378 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber
($itemnumber);
379 if ( defined $reservedate ) {
380 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
382 $item->{backgroundcolor
} = 'reserved';
383 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $reservedate ), dateonly
=> 1 });
384 $item->{ReservedForBorrowernumber
} = $reservedfor;
385 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
386 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
387 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
388 $item->{waitingdate
} = $wait;
391 # Management of the notforloan document
392 if ( $item->{notforloan
} ) {
393 $item->{backgroundcolor
} = 'other';
394 $item->{notforloanvalue
} =
395 $notforloan_label_of->{ $item->{notforloan
} };
398 # Management of lost or long overdue items
399 if ( $item->{itemlost
} ) {
401 # FIXME localized strings should never be in Perl code
403 $item->{itemlost
} == 1 ?
"(lost)"
404 : $item->{itemlost
} == 2 ?
"(long overdue)"
406 $item->{backgroundcolor
} = 'other';
407 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
413 # Check the transit status
414 my ( $transfertwhen, $transfertfrom, $transfertto ) =
415 GetTransfers
($itemnumber);
417 if ( defined $transfertwhen && $transfertwhen ne '' ) {
418 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
419 $item->{transfertfrom
} =
420 $branches->{$transfertfrom}{branchname
};
421 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
422 $item->{nocancel
} = 1;
425 # If there is no loan, return and transfer, we show a checkbox.
426 $item->{notforloan
} ||= 0;
428 # if independent branches is on we need to check if the person can reserve
429 # for branches they arent logged in to
430 if ( C4
::Context
->preference("IndependentBranches") ) {
431 if (! C4
::Context
->preference("canreservefromotherbranches")){
432 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
433 my $userenv = C4
::Context
->userenv;
434 unless ( C4
::Context
->IsSuperLibrarian ) {
435 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
440 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
442 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
444 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
447 !$item->{cantreserve
}
448 && !$exceeded_maxreserves
449 && IsAvailableForItemLevelRequest
($item, $borrowerinfo)
450 && CanItemBeReserved
(
451 $borrowerinfo->{borrowernumber
}, $itemnumber
455 $item->{available
} = 1;
458 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
459 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
460 $item->{override
} = 1;
464 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
466 # Show serial enumeration when needed
467 if ($item->{enumchron
}) {
468 $itemdata_enumchron = 1;
471 push @
{ $biblioitem->{itemloop
} }, $item;
474 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
475 $template->param( override_required
=> 1 );
476 } elsif ( $num_available == 0 ) {
477 $template->param( none_available
=> 1 );
478 $biblioloopiter{warn} = 1;
479 $biblioloopiter{none_avail
} = 1;
481 $template->param( hiddencount
=> $hiddencount);
483 push @bibitemloop, $biblioitem;
486 # existingreserves building
488 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
491 my $a_found = $a->found() || '';
492 my $b_found = $a->found() || '';
493 $a_found cmp $b_found;
499 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
504 selected
=> ( $i == $res->priority() ),
509 if ( $res->is_found() ) {
510 $reserve{'holdingbranch'} = $res->item()->holdingbranch();
511 $reserve{'biblionumber'} = $res->item()->biblionumber();
512 $reserve{'barcodenumber'} = $res->item()->barcode();
513 $reserve{'wbrcode'} = $res->branchcode();
514 $reserve{'itemnumber'} = $res->itemnumber();
515 $reserve{'wbrname'} = $res->branch()->branchname();
517 if ( $reserve{'holdingbranch'} eq $reserve{'wbrcode'} ) {
519 # Just because the holdingbranch matches the reserve branch doesn't mean the item
520 # has arrived at the destination, check for an open transfer for the item as well
521 my ( $transfertwhen, $transfertfrom, $transferto ) =
522 C4
::Circulation
::GetTransfers
( $res->itemnumber() );
523 if ( not $transferto or $transferto ne $res->branchcode() ) {
524 $reserve{'atdestination'} = 1;
528 # set found to 1 if reserve is waiting for patron pickup
529 $reserve{'found'} = $res->is_found();
530 $reserve{'intransit'} = $res->is_in_transit();
532 elsif ( $res->priority() > 0 ) {
533 if ( my $item = $res->item() ) {
534 $reserve{'itemnumber'} = $item->id();
535 $reserve{'barcodenumber'} = $item->barcode();
536 $reserve{'item_level_hold'} = 1;
540 # get borrowers reserve info
541 if ( C4
::Context
->preference('HidePatronName') ) {
542 $reserve{'hidename'} = 1;
543 $reserve{'cardnumber'} = $res->borrower()->cardnumber();
545 $reserve{'expirationdate'} = output_pref
( { dt
=> dt_from_string
( $res->expirationdate ), dateonly
=> 1 } )
546 unless ( !defined( $res->expirationdate ) || $res->expirationdate eq '0000-00-00' );
547 $reserve{'date'} = output_pref
( { dt
=> dt_from_string
( $res->reservedate ), dateonly
=> 1 } );
548 $reserve{'borrowernumber'} = $res->borrowernumber();
549 $reserve{'biblionumber'} = $res->biblionumber();
550 $reserve{'borrowernumber'} = $res->borrowernumber();
551 $reserve{'firstname'} = $res->borrower()->firstname();
552 $reserve{'surname'} = $res->borrower()->surname();
553 $reserve{'notes'} = $res->reservenotes();
554 $reserve{'waiting_date'} = $res->waitingdate();
555 $reserve{'waiting_until'} = $res->is_waiting() ?
$res->waiting_expires_on() : undef;
556 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
557 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
558 $reserve{'priority'} = $res->priority();
559 $reserve{'lowestPriority'} = $res->lowestPriority();
560 $reserve{'optionloop'} = \
@optionloop;
561 $reserve{'suspend'} = $res->suspend();
562 $reserve{'suspend_until'} = $res->suspend_until();
563 $reserve{'reserve_id'} = $res->reserve_id();
565 if ( C4
::Context
->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
566 $reserve{'branchloop'} = [ Koha
::Libraries
->find( $res->branchcode() ) ];
569 $reserve{'branchloop'} = GetBranchesLoop
( $res->branchcode() );
572 push( @reserveloop, \
%reserve );
575 # get the time for the form name...
579 branchloop
=> GetBranchesLoop
($userbranch),
581 fixedRank
=> $fixedRank,
586 optionloop
=> \
@optionloop,
587 bibitemloop
=> \
@bibitemloop,
588 itemdata_enumchron
=> $itemdata_enumchron,
590 biblionumber
=> $biblionumber,
591 findborrower
=> $findborrower,
592 title
=> $dat->{title
},
593 author
=> $dat->{author
},
595 C4
::Search
::enabled_staff_search_views
,
597 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
599 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
600 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
604 $biblioloopiter{biblionumber
} = $biblionumber;
605 $biblioloopiter{title
} = $dat->{title
};
606 $biblioloopiter{rank
} = $fixedRank;
607 $biblioloopiter{reserveloop
} = \
@reserveloop;
610 $template->param( reserveloop
=> \
@reserveloop );
613 push @biblioloop, \
%biblioloopiter;
616 $template->param( biblioloop
=> \
@biblioloop );
617 $template->param( biblionumbers
=> $biblionumbers );
618 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
621 $template->param( multi_hold
=> 1 );
624 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
625 $template->param( reserve_in_future
=> 1 );
629 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
630 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
634 output_html_with_http_headers
$input, $cookie, $template->output;
636 sub sort_borrowerlist
{
637 my $borrowerslist = shift;
640 uc( $a->{surname
} . $a->{firstname
} ) cmp
641 uc( $b->{surname
} . $b->{firstname
} )