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') || '';
84 my $date = C4
::Dates
->today('iso');
85 my $action = $input->param('action');
88 if ( $action eq 'move' ) {
89 my $where = $input->param('where');
90 my $borrowernumber = $input->param('borrowernumber');
91 my $biblionumber = $input->param('biblionumber');
92 AlterPriority
( $where, $borrowernumber, $biblionumber );
93 } elsif ( $action eq 'cancel' ) {
94 my $borrowernumber = $input->param('borrowernumber');
95 my $biblionumber = $input->param('biblionumber');
96 CancelReserve
( $biblionumber, '', $borrowernumber );
97 } elsif ( $action eq 'setLowestPriority' ) {
98 my $borrowernumber = $input->param('borrowernumber');
99 my $biblionumber = $input->param('biblionumber');
100 ToggleLowestPriority
( $borrowernumber, $biblionumber );
101 } elsif ( $action eq 'toggleSuspend' ) {
102 my $borrowernumber = $input->param('borrowernumber');
103 my $biblionumber = $input->param('biblionumber');
104 my $suspend_until = $input->param('suspend_until');
105 ToggleSuspend
( $borrowernumber, $biblionumber, $suspend_until );
109 my $borrowers = Search
($findborrower, 'cardnumber');
111 if ($borrowers && @
$borrowers) {
112 if ( @
$borrowers == 1 ) {
113 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
116 $borrowerslist = $borrowers;
119 $messageborrower = "'$findborrower'";
123 # If we have the borrowernumber because we've performed an action, then we
124 # don't want to try to place another reserve.
125 if ($borrowernumber_hold && !$action) {
126 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
129 my $count_reserv = 0;
132 # we check the reserves of the borrower, and if he can reserv a document
133 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
135 my $number_reserves =
136 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
138 if ( C4
::Context
->preference('maxreserves') && ($number_reserves >= C4
::Context
->preference('maxreserves')) ) {
143 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
144 my $expiry_date = $borrowerinfo->{dateexpiry
};
145 my $expiry = 0; # flag set if patron account has expired
146 if ($expiry_date and $expiry_date ne '0000-00-00' and
147 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
148 $messages = $expiry = 1;
151 # check if the borrower make the reserv in a different branch
152 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
158 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
159 borrowersurname
=> $borrowerinfo->{'surname'},
160 borrowerfirstname
=> $borrowerinfo->{'firstname'},
161 borrowerstreetaddress
=> $borrowerinfo->{'address'},
162 borrowercity
=> $borrowerinfo->{'city'},
163 borrowerphone
=> $borrowerinfo->{'phone'},
164 borrowermobile
=> $borrowerinfo->{'mobile'},
165 borrowerfax
=> $borrowerinfo->{'fax'},
166 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
167 borroweremail
=> $borrowerinfo->{'email'},
168 borroweremailpro
=> $borrowerinfo->{'emailpro'},
169 borrowercategory
=> $borrowerinfo->{'category'},
170 borrowerreservs
=> $count_reserv,
171 cardnumber
=> $borrowerinfo->{'cardnumber'},
172 maxreserves
=> $maxreserves,
174 diffbranch
=> $diffbranch,
175 messages
=> $messages,
176 warnings
=> $warnings
180 $template->param( messageborrower
=> $messageborrower );
182 my $CGIselectborrower;
183 if ($borrowerslist) {
187 foreach my $borrower (
190 . $a->{firstname
}) cmp uc($b->{surname
}
195 push @values, $borrower->{borrowernumber
};
197 $labels{ $borrower->{borrowernumber
} } = sprintf(
198 '%s, %s ... (%s - %s) ... %s',
199 $borrower->{surname
} ||'', $borrower->{firstname
} || '',
200 $borrower->{cardnumber
} || '', $borrower->{categorycode
} || '',
201 $borrower->{address
} || '',
205 $CGIselectborrower = CGI
::scrolling_list
(
206 -name
=> 'borrowernumber',
214 # FIXME launch another time GetMember perhaps until
215 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
217 my @biblionumbers = ();
218 my $biblionumbers = $input->param('biblionumbers');
220 @biblionumbers = split '/', $biblionumbers;
222 push @biblionumbers, $input->param('biblionumber');
225 my $itemdata_enumchron = 0;
227 foreach my $biblionumber (@biblionumbers) {
229 my %biblioloopiter = ();
232 my $dat = GetBiblioData
($biblionumber);
234 unless ( CanBookBeReserved
($borrowerinfo->{borrowernumber
}, $biblionumber) ) {
239 my $alreadypossession;
240 if (not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowerinfo->{borrowernumber
},$biblionumber)) {
242 $alreadypossession = 1;
245 # get existing reserves .....
246 my ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
247 my $totalcount = $count;
250 foreach my $res (@
$reserves) {
251 if ( defined $res->{found
} && $res->{found
} eq 'W' ) {
255 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
257 $alreadyreserved = 1;
258 $biblioloopiter{warn} = 1;
259 $biblioloopiter{alreadyres
} = 1;
263 $template->param( alreadyreserved
=> $alreadyreserved,
264 messages
=> $messages,
265 warnings
=> $warnings,
266 maxreserves
=>$maxreserves,
267 alreadypossession
=> $alreadypossession,
271 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
272 # make priorities options
275 for ( 1 .. $count + 1 ) {
280 selected
=> ( $_ == $count + 1 ),
284 # adding a fixed value for priority options
285 my $fixedRank = $count+1;
288 my %itemnumbers_of_biblioitem;
291 ## $items is array of 'item' table numbers
292 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
293 @itemnumbers = @
$items;
295 my @hostitems = get_hostitemnumbers_of
($biblionumber);
297 $template->param('hostitemsflag' => 1);
298 push(@itemnumbers, @hostitems);
302 $template->param('noitems' => 1);
303 $biblioloopiter{noitems
} = 1;
306 ## Hash of item number to 'item' table fields
307 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
309 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
310 ## when by definition all of the itemnumber have the same biblioitemnumber
311 foreach my $itemnumber (@itemnumbers) {
312 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
313 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
316 ## Should be same as biblionumber
317 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
319 my $notforloan_label_of = get_notforloan_label_of
();
321 ## Hash of biblioitemnumber to 'biblioitem' table records
322 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
326 foreach my $biblioitemnumber (@biblioitemnumbers) {
327 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
328 my $num_available = 0;
329 my $num_override = 0;
332 $biblioitem->{description
} =
333 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
334 if($biblioitem->{biblioitemnumber
} ne $biblionumber){
335 $biblioitem->{hostitemsflag
}=1;
337 $biblioloopiter{description
} = $biblioitem->{description
};
338 $biblioloopiter{itypename
} = $biblioitem->{description
};
339 $biblioloopiter{imageurl
} =
340 getitemtypeimagelocation
('intranet', $itemtypes->{$biblioitem->{itemtype
}}{imageurl
});
342 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
343 my $item = $iteminfos_of->{$itemnumber};
345 unless (C4
::Context
->preference('item-level_itypes')) {
346 $item->{itype
} = $biblioitem->{itemtype
};
349 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
350 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
351 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
353 # if the holdingbranch is different than the homebranch, we show the
354 # holdingbranch of the document too
355 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
356 $item->{holdingbranchname
} =
357 $branches->{ $item->{holdingbranch
} }{branchname
};
360 if($item->{biblionumber
} ne $biblionumber){
361 $item->{hostitemsflag
}=1;
362 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
366 $item->{itemcallnumber
} = $item->{itemcallnumber
};
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
} = format_sqldatetime
($issues->{date_due
});
373 $item->{backgroundcolor
} = 'onloan';
377 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber
($itemnumber);
378 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
380 if ( defined $reservedate ) {
381 $item->{backgroundcolor
} = 'reserved';
382 $item->{reservedate
} = format_date
($reservedate);
383 $item->{ReservedForBorrowernumber
} = $reservedfor;
384 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
385 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
386 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
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
} = format_date
($transfertwhen);
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
} = $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 if ( ($userenv) && ( $userenv->{flags
} % 2 != 1 ) ) {
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'} );
442 my $policy_holdallowed = 1;
444 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
446 if ( $branchitemrule->{'holdallowed'} == 0 ||
447 ( $branchitemrule->{'holdallowed'} == 1 &&
448 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
449 $policy_holdallowed = 0;
454 && !$item->{cantreserve
}
455 && IsAvailableForItemLevelRequest
($itemnumber)
456 && CanItemBeReserved
(
457 $borrowerinfo->{borrowernumber
}, $itemnumber
461 $item->{available
} = 1;
464 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
466 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
467 $item->{override
} = 1;
471 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
473 # FIXME: move this to a pm
474 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
475 $sth2->execute($item->{ReservedForBorrowernumber
},$item->{itemnumber
});
476 while (my $wait_hashref = $sth2->fetchrow_hashref) {
477 $item->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
480 # Show serial enumeration when needed
481 if ($item->{enumchron
}) {
482 $itemdata_enumchron = 1;
485 push @
{ $biblioitem->{itemloop
} }, $item;
488 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
489 $template->param( override_required
=> 1 );
490 } elsif ( $num_available == 0 ) {
491 $template->param( none_available
=> 1 );
492 $template->param( warnings
=> 1 );
493 $biblioloopiter{warn} = 1;
494 $biblioloopiter{none_avail
} = 1;
496 $template->param( hiddencount
=> $hiddencount);
498 push @bibitemloop, $biblioitem;
501 # existingreserves building
503 ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
504 foreach my $res ( sort {
505 my $a_found = $a->{found
} || '';
506 my $b_found = $a->{found
} || '';
507 $a_found cmp $b_found;
511 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
516 selected
=> ( $i == $res->{priority
} ),
521 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
522 my $item = $res->{'itemnumber'};
523 $item = GetBiblioFromItemNumber
($item,undef);
525 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
526 $reserve{'biblionumber'}=$item->{'biblionumber'};
527 $reserve{'barcodenumber'} = $item->{'barcode'};
528 $reserve{'wbrcode'} = $res->{'branchcode'};
529 $reserve{'itemnumber'} = $res->{'itemnumber'};
530 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
531 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
532 $reserve{'atdestination'} = 1;
534 # set found to 1 if reserve is waiting for patron pickup
535 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
536 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
537 } elsif ($res->{priority
} > 0) {
538 if (defined($res->{itemnumber
})) {
539 my $item = GetItem
($res->{itemnumber
});
540 $reserve{'itemnumber'} = $res->{'itemnumber'};
541 $reserve{'barcodenumber'} = $item->{'barcode'};
542 $reserve{'item_level_hold'} = 1;
546 # get borrowers reserve info
547 my $reserveborrowerinfo = GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
548 if (C4
::Context
->preference('HidePatronName')){
549 $reserve{'hidename'} = 1;
550 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
552 $reserve{'expirationdate'} = format_date
( $res->{'expirationdate'} )
553 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
554 $reserve{'date'} = format_date
( $res->{'reservedate'} );
555 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
556 $reserve{'biblionumber'} = $res->{'biblionumber'};
557 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
558 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
559 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
560 $reserve{'notes'} = $res->{'reservenotes'};
562 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
563 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
564 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
565 $reserve{'voldesc'} = $res->{'volumeddesc'};
566 $reserve{'ccode'} = $res->{'ccode'};
567 $reserve{'barcode'} = $res->{'barcode'};
568 $reserve{'priority'} = $res->{'priority'};
569 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
570 $reserve{'optionloop'} = \
@optionloop;
571 $reserve{'suspend'} = $res->{'suspend'};
572 $reserve{'suspend_until'} = $res->{'suspend_until'};
574 if ( C4
::Context
->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
575 $reserve{'branchloop'} = [ GetBranchDetail
($res->{'branchcode'}) ];
577 $reserve{'branchloop'} = GetBranchesLoop
($res->{'branchcode'});
580 push( @reserveloop, \
%reserve );
583 # get the time for the form name...
587 branchloop
=> GetBranchesLoop
($userbranch),
589 fixedRank
=> $fixedRank,
594 optionloop
=> \
@optionloop,
595 bibitemloop
=> \
@bibitemloop,
596 itemdata_enumchron
=> $itemdata_enumchron,
598 biblionumber
=> $biblionumber,
599 findborrower
=> $findborrower,
600 title
=> $dat->{title
},
601 author
=> $dat->{author
},
603 C4
::Search
::enabled_staff_search_views
,
605 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
607 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
608 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
611 $template->param(CGIselectborrower
=> $CGIselectborrower) if defined $CGIselectborrower;
613 $biblioloopiter{biblionumber
} = $biblionumber;
614 $biblioloopiter{title
} = $dat->{title
};
615 $biblioloopiter{rank
} = $fixedRank;
616 $biblioloopiter{reserveloop
} = \
@reserveloop;
619 $template->param( reserveloop
=> \
@reserveloop );
622 push @biblioloop, \
%biblioloopiter;
625 $template->param( biblioloop
=> \
@biblioloop );
626 $template->param( biblionumbers
=> $biblionumbers );
629 $template->param( multi_hold
=> 1 );
632 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
633 $template->param( reserve_in_future
=> 1 );
637 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
638 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
642 output_html_with_http_headers
$input, $cookie, $template->output;