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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 script to place reserves/requests
33 use List
::MoreUtils qw
/uniq/;
34 use Date
::Calc qw
/Date_to_Days/;
42 use C4
::Dates qw
/format_date/;
44 use C4
::Search
; # enabled_staff_search_views
47 my $dbh = C4
::Context
->dbh;
50 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user
(
52 template_name
=> "reserve/request.tmpl",
56 flagsrequired
=> { reserveforothers
=> 'place_holds' },
60 my $multihold = $input->param('multi_hold');
61 $template->param(multi_hold
=> $multihold);
62 my $showallitems = $input->param('showallitems');
64 # get Branches and Itemtypes
65 my $branches = GetBranches
();
66 my $itemtypes = GetItemTypes
();
69 if (C4
::Context
->userenv && C4
::Context
->userenv->{'branch'}) {
70 $userbranch = C4
::Context
->userenv->{'branch'};
74 # Select borrowers infos
75 my $findborrower = $input->param('findborrower');
76 $findborrower = '' unless defined $findborrower;
77 $findborrower =~ s
|,| |g
;
78 my $borrowernumber_hold = $input->param('borrowernumber') || '';
82 my $date = C4
::Dates
->today('iso');
83 my $action = $input->param('action');
86 if ( $action eq 'move' ) {
87 my $where = $input->param('where');
88 my $reserve_id = $input->param('reserve_id');
89 AlterPriority
( $where, $reserve_id );
90 } elsif ( $action eq 'cancel' ) {
91 my $reserve_id = $input->param('reserve_id');
92 CancelReserve
({ reserve_id
=> $reserve_id });
93 } elsif ( $action eq 'setLowestPriority' ) {
94 my $reserve_id = $input->param('reserve_id');
95 ToggleLowestPriority
( $reserve_id );
96 } elsif ( $action eq 'toggleSuspend' ) {
97 my $reserve_id = $input->param('reserve_id');
98 my $suspend_until = $input->param('suspend_until');
99 ToggleSuspend
( $reserve_id, $suspend_until );
103 my $borrowers = Search
($findborrower, 'cardnumber');
105 if ($borrowers && @
$borrowers) {
106 if ( @
$borrowers == 1 ) {
107 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
110 $template->param( borrower_list
=> sort_borrowerlist
($borrowers));
113 $messageborrower = "'$findborrower'";
117 # If we have the borrowernumber because we've performed an action, then we
118 # don't want to try to place another reserve.
119 if ($borrowernumber_hold && !$action) {
120 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
123 my $count_reserv = 0;
125 # we check the reserves of the borrower, and if he can reserv a document
126 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
128 my $number_reserves =
129 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
131 if ( C4
::Context
->preference('maxreserves') && ($number_reserves >= C4
::Context
->preference('maxreserves')) ) {
135 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
136 my $expiry_date = $borrowerinfo->{dateexpiry
};
137 my $expiry = 0; # flag set if patron account has expired
138 if ($expiry_date and $expiry_date ne '0000-00-00' and
139 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
143 # check if the borrower make the reserv in a different branch
144 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
149 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
150 borrowersurname
=> $borrowerinfo->{'surname'},
151 borrowerfirstname
=> $borrowerinfo->{'firstname'},
152 borrowerstreetaddress
=> $borrowerinfo->{'address'},
153 borrowercity
=> $borrowerinfo->{'city'},
154 borrowerphone
=> $borrowerinfo->{'phone'},
155 borrowermobile
=> $borrowerinfo->{'mobile'},
156 borrowerfax
=> $borrowerinfo->{'fax'},
157 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
158 borroweremail
=> $borrowerinfo->{'email'},
159 borroweremailpro
=> $borrowerinfo->{'emailpro'},
160 borrowercategory
=> $borrowerinfo->{'category'},
161 borrowerreservs
=> $count_reserv,
162 cardnumber
=> $borrowerinfo->{'cardnumber'},
164 diffbranch
=> $diffbranch,
168 $template->param( messageborrower
=> $messageborrower );
170 # FIXME launch another time GetMember perhaps until
171 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
173 my @biblionumbers = ();
174 my $biblionumbers = $input->param('biblionumbers');
176 @biblionumbers = split '/', $biblionumbers;
178 push @biblionumbers, $input->param('biblionumber');
181 my $itemdata_enumchron = 0;
183 foreach my $biblionumber (@biblionumbers) {
185 my %biblioloopiter = ();
187 my $dat = GetBiblioData
($biblionumber);
189 unless ( CanBookBeReserved
($borrowerinfo->{borrowernumber
}, $biblionumber) ) {
193 my $alreadypossession;
194 if (not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowerinfo->{borrowernumber
},$biblionumber)) {
195 $alreadypossession = 1;
198 # get existing reserves .....
199 my $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
200 my $count = scalar( @
$reserves );
201 my $totalcount = $count;
203 my $alreadyreserved = 0;
205 foreach my $res (@
$reserves) {
206 if ( defined $res->{found
} && $res->{found
} eq 'W' ) {
210 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber
}) && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
215 if ( $holds_count ) {
216 $alreadyreserved = 1;
217 $biblioloopiter{warn} = 1;
218 $biblioloopiter{alreadyres
} = 1;
222 alreadyreserved
=> $alreadyreserved,
223 alreadypossession
=> $alreadypossession,
226 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
227 # make priorities options
230 for ( 1 .. $count + 1 ) {
235 selected
=> ( $_ == $count + 1 ),
239 # adding a fixed value for priority options
240 my $fixedRank = $count+1;
243 my %itemnumbers_of_biblioitem;
246 ## $items is array of 'item' table numbers
247 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
248 @itemnumbers = @
$items;
250 my @hostitems = get_hostitemnumbers_of
($biblionumber);
252 $template->param('hostitemsflag' => 1);
253 push(@itemnumbers, @hostitems);
257 $template->param('noitems' => 1);
258 $biblioloopiter{noitems
} = 1;
261 ## Hash of item number to 'item' table fields
262 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
264 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
265 ## when by definition all of the itemnumber have the same biblioitemnumber
266 foreach my $itemnumber (@itemnumbers) {
267 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
268 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
271 ## Should be same as biblionumber
272 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
274 my $notforloan_label_of = get_notforloan_label_of
();
276 ## Hash of biblioitemnumber to 'biblioitem' table records
277 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
281 foreach my $biblioitemnumber (@biblioitemnumbers) {
282 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
283 my $num_available = 0;
284 my $num_override = 0;
287 $biblioitem->{description
} =
288 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
289 if($biblioitem->{biblioitemnumber
} ne $biblionumber){
290 $biblioitem->{hostitemsflag
}=1;
292 $biblioloopiter{description
} = $biblioitem->{description
};
293 $biblioloopiter{itypename
} = $biblioitem->{description
};
294 $biblioloopiter{imageurl
} =
295 getitemtypeimagelocation
('intranet', $itemtypes->{$biblioitem->{itemtype
}}{imageurl
});
297 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
298 my $item = $iteminfos_of->{$itemnumber};
300 unless (C4
::Context
->preference('item-level_itypes')) {
301 $item->{itype
} = $biblioitem->{itemtype
};
304 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
305 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
306 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
308 # if the holdingbranch is different than the homebranch, we show the
309 # holdingbranch of the document too
310 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
311 $item->{holdingbranchname
} =
312 $branches->{ $item->{holdingbranch
} }{branchname
};
315 if($item->{biblionumber
} ne $biblionumber){
316 $item->{hostitemsflag
}=1;
317 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
321 $item->{itemcallnumber
} = $item->{itemcallnumber
};
323 # if the item is currently on loan, we display its return date and
324 # change the background color
325 my $issues= GetItemIssue
($itemnumber);
326 if ( $issues->{'date_due'} ) {
327 $item->{date_due
} = format_sqldatetime
($issues->{date_due
});
328 $item->{backgroundcolor
} = 'onloan';
332 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber
($itemnumber);
333 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
335 if ( defined $reservedate ) {
336 $item->{backgroundcolor
} = 'reserved';
337 $item->{reservedate
} = format_date
($reservedate);
338 $item->{ReservedForBorrowernumber
} = $reservedfor;
339 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
340 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
341 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
342 $item->{waitingdate
} = $wait;
345 # Management of the notforloan document
346 if ( $item->{notforloan
} ) {
347 $item->{backgroundcolor
} = 'other';
348 $item->{notforloanvalue
} =
349 $notforloan_label_of->{ $item->{notforloan
} };
352 # Management of lost or long overdue items
353 if ( $item->{itemlost
} ) {
355 # FIXME localized strings should never be in Perl code
357 $item->{itemlost
} == 1 ?
"(lost)"
358 : $item->{itemlost
} == 2 ?
"(long overdue)"
360 $item->{backgroundcolor
} = 'other';
361 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
367 # Check the transit status
368 my ( $transfertwhen, $transfertfrom, $transfertto ) =
369 GetTransfers
($itemnumber);
371 if ( defined $transfertwhen && $transfertwhen ne '' ) {
372 $item->{transfertwhen
} = format_date
($transfertwhen);
373 $item->{transfertfrom
} =
374 $branches->{$transfertfrom}{branchname
};
375 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
376 $item->{nocancel
} = 1;
379 # If there is no loan, return and transfer, we show a checkbox.
380 $item->{notforloan
} = $item->{notforloan
} || 0;
382 # if independent branches is on we need to check if the person can reserve
383 # for branches they arent logged in to
384 if ( C4
::Context
->preference("IndependentBranches") ) {
385 if (! C4
::Context
->preference("canreservefromotherbranches")){
386 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
387 my $userenv = C4
::Context
->userenv;
388 unless ( C4
::Context
->IsSuperLibrarian ) {
389 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
394 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
396 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
397 my $policy_holdallowed = 1;
399 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
401 if ( $branchitemrule->{'holdallowed'} == 0 ||
402 ( $branchitemrule->{'holdallowed'} == 1 &&
403 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
404 $policy_holdallowed = 0;
409 && !$item->{cantreserve
}
410 && IsAvailableForItemLevelRequest
($itemnumber)
411 && CanItemBeReserved
(
412 $borrowerinfo->{borrowernumber
}, $itemnumber
416 $item->{available
} = 1;
419 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
421 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
422 $item->{override
} = 1;
426 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
428 # Show serial enumeration when needed
429 if ($item->{enumchron
}) {
430 $itemdata_enumchron = 1;
433 push @
{ $biblioitem->{itemloop
} }, $item;
436 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
437 $template->param( override_required
=> 1 );
438 } elsif ( $num_available == 0 ) {
439 $template->param( none_available
=> 1 );
440 $biblioloopiter{warn} = 1;
441 $biblioloopiter{none_avail
} = 1;
443 $template->param( hiddencount
=> $hiddencount);
445 push @bibitemloop, $biblioitem;
448 # existingreserves building
450 $reserves = GetReservesFromBiblionumber
({ biblionumber
=> $biblionumber, all_dates
=> 1 });
451 foreach my $res ( sort {
452 my $a_found = $a->{found
} || '';
453 my $b_found = $a->{found
} || '';
454 $a_found cmp $b_found;
458 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
463 selected
=> ( $i == $res->{priority
} ),
468 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
469 my $item = $res->{'itemnumber'};
470 $item = GetBiblioFromItemNumber
($item,undef);
472 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
473 $reserve{'biblionumber'}=$item->{'biblionumber'};
474 $reserve{'barcodenumber'} = $item->{'barcode'};
475 $reserve{'wbrcode'} = $res->{'branchcode'};
476 $reserve{'itemnumber'} = $res->{'itemnumber'};
477 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
478 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
479 $reserve{'atdestination'} = 1;
481 # set found to 1 if reserve is waiting for patron pickup
482 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
483 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
484 } elsif ($res->{priority
} > 0) {
485 if (defined($res->{itemnumber
})) {
486 my $item = GetItem
($res->{itemnumber
});
487 $reserve{'itemnumber'} = $res->{'itemnumber'};
488 $reserve{'barcodenumber'} = $item->{'barcode'};
489 $reserve{'item_level_hold'} = 1;
493 # get borrowers reserve info
494 my $reserveborrowerinfo = GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
495 if (C4
::Context
->preference('HidePatronName')){
496 $reserve{'hidename'} = 1;
497 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
499 $reserve{'expirationdate'} = format_date
( $res->{'expirationdate'} )
500 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
501 $reserve{'date'} = format_date
( $res->{'reservedate'} );
502 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
503 $reserve{'biblionumber'} = $res->{'biblionumber'};
504 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
505 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
506 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
507 $reserve{'notes'} = $res->{'reservenotes'};
509 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
510 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
511 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
512 $reserve{'voldesc'} = $res->{'volumeddesc'};
513 $reserve{'ccode'} = $res->{'ccode'};
514 $reserve{'barcode'} = $res->{'barcode'};
515 $reserve{'priority'} = $res->{'priority'};
516 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
517 $reserve{'optionloop'} = \
@optionloop;
518 $reserve{'suspend'} = $res->{'suspend'};
519 $reserve{'suspend_until'} = $res->{'suspend_until'};
520 $reserve{'reserve_id'} = $res->{'reserve_id'};
522 if ( C4
::Context
->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
523 $reserve{'branchloop'} = [ GetBranchDetail
($res->{'branchcode'}) ];
525 $reserve{'branchloop'} = GetBranchesLoop
($res->{'branchcode'});
528 push( @reserveloop, \
%reserve );
531 # get the time for the form name...
535 branchloop
=> GetBranchesLoop
($userbranch),
537 fixedRank
=> $fixedRank,
542 optionloop
=> \
@optionloop,
543 bibitemloop
=> \
@bibitemloop,
544 itemdata_enumchron
=> $itemdata_enumchron,
546 biblionumber
=> $biblionumber,
547 findborrower
=> $findborrower,
548 title
=> $dat->{title
},
549 author
=> $dat->{author
},
551 C4
::Search
::enabled_staff_search_views
,
553 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
555 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
556 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
560 $biblioloopiter{biblionumber
} = $biblionumber;
561 $biblioloopiter{title
} = $dat->{title
};
562 $biblioloopiter{rank
} = $fixedRank;
563 $biblioloopiter{reserveloop
} = \
@reserveloop;
566 $template->param( reserveloop
=> \
@reserveloop );
569 push @biblioloop, \
%biblioloopiter;
572 $template->param( biblioloop
=> \
@biblioloop );
573 $template->param( biblionumbers
=> $biblionumbers );
574 $template->param( maxreserves
=> $maxreserves );
577 $template->param( multi_hold
=> 1 );
580 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
581 $template->param( reserve_in_future
=> 1 );
585 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
586 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
590 output_html_with_http_headers
$input, $cookie, $template->output;
592 sub sort_borrowerlist
{
593 my $borrowerslist = shift;
596 uc( $a->{surname
} . $a->{firstname
} ) cmp
597 uc( $b->{surname
} . $b->{firstname
} )