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);
50 my $dbh = C4
::Context
->dbh;
52 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user
(
54 template_name
=> "reserve/request.tt",
58 flagsrequired
=> { reserveforothers
=> 'place_holds' },
62 my $multihold = $input->param('multi_hold');
63 $template->param(multi_hold
=> $multihold);
64 my $showallitems = $input->param('showallitems');
66 # get Branches and Itemtypes
67 my $branches = GetBranches
();
68 my $itemtypes = GetItemTypes
();
71 if (C4
::Context
->userenv && C4
::Context
->userenv->{'branch'}) {
72 $userbranch = C4
::Context
->userenv->{'branch'};
76 # Select borrowers infos
77 my $findborrower = $input->param('findborrower');
78 $findborrower = '' unless defined $findborrower;
79 $findborrower =~ s
|,| |g
;
80 my $borrowernumber_hold = $input->param('borrowernumber') || '';
84 my $exceeded_maxreserves;
86 my $date = output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
87 my $action = $input->param('action');
90 if ( $action eq 'move' ) {
91 my $where = $input->param('where');
92 my $reserve_id = $input->param('reserve_id');
93 AlterPriority
( $where, $reserve_id );
94 } elsif ( $action eq 'cancel' ) {
95 my $reserve_id = $input->param('reserve_id');
96 CancelReserve
({ reserve_id
=> $reserve_id });
97 } elsif ( $action eq 'setLowestPriority' ) {
98 my $reserve_id = $input->param('reserve_id');
99 ToggleLowestPriority
( $reserve_id );
100 } elsif ( $action eq 'toggleSuspend' ) {
101 my $reserve_id = $input->param('reserve_id');
102 my $suspend_until = $input->param('suspend_until');
103 ToggleSuspend
( $reserve_id, $suspend_until );
107 my $borrower = C4
::Members
::GetMember
( cardnumber
=> $findborrower );
109 $borrowernumber_hold = $borrower->{borrowernumber
};
111 my $dt_params = { iDisplayLength
=> -1 };
112 my $results = C4
::Utils
::DataTables
::Members
::search
(
114 searchmember
=> $findborrower,
115 dt_params
=> $dt_params,
118 my $borrowers = $results->{patrons
};
119 if ( scalar @
$borrowers == 1 ) {
120 $borrowernumber_hold = $borrowers->[0]->{borrowernumber
};
121 } elsif ( @
$borrowers ) {
122 $template->param( borrowers
=> $borrowers );
124 $messageborrower = "'$findborrower'";
129 my @biblionumbers = ();
130 my $biblionumbers = $input->param('biblionumbers');
132 @biblionumbers = split '/', $biblionumbers;
134 push @biblionumbers, $input->param('biblionumber');
138 # If we have the borrowernumber because we've performed an action, then we
139 # don't want to try to place another reserve.
140 if ($borrowernumber_hold && !$action) {
141 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
144 # we check the reserves of the borrower, and if he can reserv a document
145 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
148 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
150 my $new_reserves_count = scalar( @biblionumbers );
152 my $maxreserves = C4
::Context
->preference('maxreserves');
154 && ( $reserves_count + $new_reserves_count > $maxreserves ) )
156 my $new_reserves_allowed =
157 $maxreserves - $reserves_count > 0
158 ?
$maxreserves - $reserves_count
161 $exceeded_maxreserves = 1;
163 new_reserves_allowed
=> $new_reserves_allowed,
164 new_reserves_count
=> $new_reserves_count,
165 reserves_count
=> $reserves_count,
166 maxreserves
=> $maxreserves,
170 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
171 my $expiry_date = $borrowerinfo->{dateexpiry
};
172 my $expiry = 0; # flag set if patron account has expired
173 if ($expiry_date and $expiry_date ne '0000-00-00' and
174 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
178 # check if the borrower make the reserv in a different branch
179 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
184 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
185 borrowersurname
=> $borrowerinfo->{'surname'},
186 borrowerfirstname
=> $borrowerinfo->{'firstname'},
187 borrowerstreetaddress
=> $borrowerinfo->{'address'},
188 borrowercity
=> $borrowerinfo->{'city'},
189 borrowerphone
=> $borrowerinfo->{'phone'},
190 borrowermobile
=> $borrowerinfo->{'mobile'},
191 borrowerfax
=> $borrowerinfo->{'fax'},
192 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
193 borroweremail
=> $borrowerinfo->{'email'},
194 borroweremailpro
=> $borrowerinfo->{'emailpro'},
195 borrowercategory
=> $borrowerinfo->{'category'},
196 cardnumber
=> $borrowerinfo->{'cardnumber'},
198 diffbranch
=> $diffbranch,
199 messages
=> $messages,
200 warnings
=> $warnings,
201 restricted
=> IsDebarred
($borrowerinfo->{'borrowernumber'}),
202 amount_outstanding
=> GetMemberAccountRecords
($borrowerinfo->{borrowernumber
}),
206 $template->param( messageborrower
=> $messageborrower );
208 # FIXME launch another time GetMember perhaps until
209 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
211 my $itemdata_enumchron = 0;
213 foreach my $biblionumber (@biblionumbers) {
215 my %biblioloopiter = ();
217 my $dat = GetBiblioData
($biblionumber);
219 my $canReserve = CanBookBeReserved
( $borrowerinfo->{borrowernumber
}, $biblionumber );
221 if ( $canReserve eq 'OK' ) {
223 #All is OK and we can continue
225 elsif ( $canReserve eq 'tooManyReserves' ) {
226 $exceeded_maxreserves = 1;
228 elsif ( $canReserve eq 'ageRestricted' ) {
229 $template->param( $canReserve => 1 );
230 $biblioloopiter{$canReserve} = 1;
233 $biblioloopiter{$canReserve} = 1;
236 my $alreadypossession;
237 if (not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowerinfo->{borrowernumber
},$biblionumber)) {
238 $alreadypossession = 1;
241 # get existing reserves .....
242 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
243 my $count = scalar( @
$reserves );
244 my $totalcount = $count;
246 my $alreadyreserved = 0;
248 foreach my $res (@
$reserves) {
249 if ( defined $res->{found
} ) { # found can be 'W' or 'T'
253 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber
}) && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
258 if ( $holds_count ) {
259 $alreadyreserved = 1;
260 $biblioloopiter{warn} = 1;
261 $biblioloopiter{alreadyres
} = 1;
265 alreadyreserved
=> $alreadyreserved,
266 alreadypossession
=> $alreadypossession,
269 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
270 # make priorities options
273 for ( 1 .. $count + 1 ) {
278 selected
=> ( $_ == $count + 1 ),
282 # adding a fixed value for priority options
283 my $fixedRank = $count+1;
285 my %itemnumbers_of_biblioitem;
288 ## $items is array of 'item' table numbers
289 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
290 @itemnumbers = @
$items;
292 my @hostitems = get_hostitemnumbers_of
($biblionumber);
294 $template->param('hostitemsflag' => 1);
295 push(@itemnumbers, @hostitems);
299 $template->param('noitems' => 1);
300 $biblioloopiter{noitems
} = 1;
303 ## Hash of item number to 'item' table fields
304 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
306 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
307 ## when by definition all of the itemnumber have the same biblioitemnumber
308 foreach my $itemnumber (@itemnumbers) {
309 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
310 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
313 ## Should be same as biblionumber
314 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
316 my $notforloan_label_of = get_notforloan_label_of
();
318 ## Hash of biblioitemnumber to 'biblioitem' table records
319 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
323 foreach my $biblioitemnumber (@biblioitemnumbers) {
324 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
325 my $num_available = 0;
326 my $num_override = 0;
329 if ( $biblioitem->{biblioitemnumber
} ne $biblionumber ) {
330 $biblioitem->{hostitemsflag
} = 1;
333 $biblioloopiter{description
} = $biblioitem->{description
};
334 $biblioloopiter{itypename
} = $biblioitem->{description
};
335 if ( $biblioitem->{itemtype
} ) {
337 $biblioitem->{description
} =
338 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
340 $biblioloopiter{imageurl
} =
341 getitemtypeimagelocation
( 'intranet',
342 $itemtypes->{ $biblioitem->{itemtype
} }{imageurl
} );
345 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
346 my $item = $iteminfos_of->{$itemnumber};
348 unless (C4
::Context
->preference('item-level_itypes')) {
349 $item->{itype
} = $biblioitem->{itemtype
};
352 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
353 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
354 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
356 # if the holdingbranch is different than the homebranch, we show the
357 # holdingbranch of the document too
358 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
359 $item->{holdingbranchname
} =
360 $branches->{ $item->{holdingbranch
} }{branchname
};
363 if($item->{biblionumber
} ne $biblionumber){
364 $item->{hostitemsflag
}=1;
365 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
368 # if the item is currently on loan, we display its return date and
369 # change the background color
370 my $issues= GetItemIssue
($itemnumber);
371 if ( $issues->{'date_due'} ) {
372 $item->{date_due
} = $issues->{date_due_sql
};
373 $item->{backgroundcolor
} = 'onloan';
377 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber
($itemnumber);
378 if ( defined $reservedate ) {
379 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
381 $item->{backgroundcolor
} = 'reserved';
382 $item->{reservedate
} = output_pref
({ dt
=> dt_from_string
( $reservedate ), dateonly
=> 1 });
383 $item->{ReservedForBorrowernumber
} = $reservedfor;
384 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
385 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
386 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
387 $item->{waitingdate
} = $wait;
390 # Management of the notforloan document
391 if ( $item->{notforloan
} ) {
392 $item->{backgroundcolor
} = 'other';
393 $item->{notforloanvalue
} =
394 $notforloan_label_of->{ $item->{notforloan
} };
397 # Management of lost or long overdue items
398 if ( $item->{itemlost
} ) {
400 # FIXME localized strings should never be in Perl code
402 $item->{itemlost
} == 1 ?
"(lost)"
403 : $item->{itemlost
} == 2 ?
"(long overdue)"
405 $item->{backgroundcolor
} = 'other';
406 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
412 # Check the transit status
413 my ( $transfertwhen, $transfertfrom, $transfertto ) =
414 GetTransfers
($itemnumber);
416 if ( defined $transfertwhen && $transfertwhen ne '' ) {
417 $item->{transfertwhen
} = output_pref
({ dt
=> dt_from_string
( $transfertwhen ), dateonly
=> 1 });
418 $item->{transfertfrom
} =
419 $branches->{$transfertfrom}{branchname
};
420 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
421 $item->{nocancel
} = 1;
424 # If there is no loan, return and transfer, we show a checkbox.
425 $item->{notforloan
} ||= 0;
427 # if independent branches is on we need to check if the person can reserve
428 # for branches they arent logged in to
429 if ( C4
::Context
->preference("IndependentBranches") ) {
430 if (! C4
::Context
->preference("canreservefromotherbranches")){
431 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
432 my $userenv = C4
::Context
->userenv;
433 unless ( C4
::Context
->IsSuperLibrarian ) {
434 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
439 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
441 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
443 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
446 !$item->{cantreserve
}
447 && !$exceeded_maxreserves
448 && IsAvailableForItemLevelRequest
($item, $borrowerinfo)
449 && CanItemBeReserved
(
450 $borrowerinfo->{borrowernumber
}, $itemnumber
454 $item->{available
} = 1;
457 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
458 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
459 $item->{override
} = 1;
463 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
465 # Show serial enumeration when needed
466 if ($item->{enumchron
}) {
467 $itemdata_enumchron = 1;
470 push @
{ $biblioitem->{itemloop
} }, $item;
473 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
474 $template->param( override_required
=> 1 );
475 } elsif ( $num_available == 0 ) {
476 $template->param( none_available
=> 1 );
477 $biblioloopiter{warn} = 1;
478 $biblioloopiter{none_avail
} = 1;
480 $template->param( hiddencount
=> $hiddencount);
482 push @bibitemloop, $biblioitem;
485 # existingreserves building
487 my @reserves = Koha
::Holds
->search( { biblionumber
=> $biblionumber }, { order_by
=> 'priority' } );
490 my $a_found = $a->found() || '';
491 my $b_found = $a->found() || '';
492 $a_found cmp $b_found;
498 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
503 selected
=> ( $i == $res->priority() ),
508 if ( $res->is_found() ) {
509 $reserve{'wait'} = 1;
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{'wait'} = $res->is_waiting();
555 $reserve{'waiting_date'} = $res->waitingdate();
556 $reserve{'waiting_until'} = $res->is_waiting() ?
$res->waiting_expires_on() : undef;
557 $reserve{'ccode'} = $res->item() ?
$res->item()->ccode() : undef;
558 $reserve{'barcode'} = $res->item() ?
$res->item()->barcode() : undef;
559 $reserve{'priority'} = $res->priority();
560 $reserve{'lowestPriority'} = $res->lowestPriority();
561 $reserve{'optionloop'} = \
@optionloop;
562 $reserve{'suspend'} = $res->suspend();
563 $reserve{'suspend_until'} = $res->suspend_until();
564 $reserve{'reserve_id'} = $res->reserve_id();
566 if ( C4
::Context
->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
567 $reserve{'branchloop'} = [ GetBranchDetail
( $res->branchcode() ) ];
570 $reserve{'branchloop'} = GetBranchesLoop
( $res->branchcode() );
573 push( @reserveloop, \
%reserve );
576 # get the time for the form name...
580 branchloop
=> GetBranchesLoop
($userbranch),
582 fixedRank
=> $fixedRank,
587 optionloop
=> \
@optionloop,
588 bibitemloop
=> \
@bibitemloop,
589 itemdata_enumchron
=> $itemdata_enumchron,
591 biblionumber
=> $biblionumber,
592 findborrower
=> $findborrower,
593 title
=> $dat->{title
},
594 author
=> $dat->{author
},
596 C4
::Search
::enabled_staff_search_views
,
598 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
600 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
601 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
605 $biblioloopiter{biblionumber
} = $biblionumber;
606 $biblioloopiter{title
} = $dat->{title
};
607 $biblioloopiter{rank
} = $fixedRank;
608 $biblioloopiter{reserveloop
} = \
@reserveloop;
611 $template->param( reserveloop
=> \
@reserveloop );
614 push @biblioloop, \
%biblioloopiter;
617 $template->param( biblioloop
=> \
@biblioloop );
618 $template->param( biblionumbers
=> $biblionumbers );
619 $template->param( exceeded_maxreserves
=> $exceeded_maxreserves );
622 $template->param( multi_hold
=> 1 );
625 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
626 $template->param( reserve_in_future
=> 1 );
630 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
631 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
635 output_html_with_http_headers
$input, $cookie, $template->output;
637 sub sort_borrowerlist
{
638 my $borrowerslist = shift;
641 uc( $a->{surname
} . $a->{firstname
} ) cmp
642 uc( $b->{surname
} . $b->{firstname
} )