3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN Ouest Provence
5 # 2007-2010 BibLibre Paul POULAIN
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 #use warnings; FIXME - Bug 2505
33 # for _koha_notify_reserve
34 use C4
::Members
::Messaging
;
37 use C4
::Branch
qw( GetBranchDetail );
38 use C4
::Dates
qw( format_date_in_iso );
43 use List
::MoreUtils
qw( firstidx );
45 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
49 C4::Reserves - Koha functions for dealing with reservation.
57 This modules provides somes functions to deal with reservations.
59 Reserves are stored in reserves table.
60 The following columns contains important values :
61 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
62 =0 : then the reserve is being dealed
63 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
64 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
65 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
66 F(inished) : the reserve has been completed, and is done
67 - itemnumber : empty : the reserve is still unaffected to an item
68 filled: the reserve is attached to an item
69 The complete workflow is :
70 ==== 1st use case ====
71 patron request a document, 1st available : P >0, F=NULL, I=NULL
72 a library having it run "transfertodo", and clic on the list
73 if there is no transfer to do, the reserve waiting
74 patron can pick it up P =0, F=W, I=filled
75 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
76 The pickup library recieve the book, it check in P =0, F=W, I=filled
77 The patron borrow the book P =0, F=F, I=filled
79 ==== 2nd use case ====
80 patron requests a document, a given item,
81 If pickup is holding branch P =0, F=W, I=filled
82 If transfer needed, write in branchtransfer P =0, F=T, I=filled
83 The pickup library receive the book, it checks it in P =0, F=W, I=filled
84 The patron borrow the book P =0, F=F, I=filled
91 # set the version for version checking
92 $VERSION = 3.07.00.049;
99 &GetReservesFromItemnumber
100 &GetReservesFromBiblionumber
101 &GetReservesFromBorrowernumber
102 &GetReservesForBranch
116 &ModReserveMinusPriority
123 &CancelExpiredReserves
125 &AutoUnsuspendReserves
127 &IsAvailableForItemLevelRequest
130 &ToggleLowestPriority
136 &GetReservesControlBranch
138 @EXPORT_OK = qw( MergeHolds );
143 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
149 $branch, $borrowernumber, $biblionumber,
150 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
151 $title, $checkitem, $found
154 GetReserveFee
($borrowernumber, $biblionumber, $constraint,
156 my $dbh = C4
::Context
->dbh;
157 my $const = lc substr( $constraint, 0, 1 );
158 $resdate = format_date_in_iso
( $resdate ) if ( $resdate );
159 $resdate = C4
::Dates
->today( 'iso' ) unless ( $resdate );
161 $expdate = format_date_in_iso
( $expdate );
163 undef $expdate; # make reserves.expirationdate default to null rather than '0000-00-00'
165 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
166 # Make room in reserves for this before those of a later reserve date
167 $priority = _ShiftPriorityByDateAndPriority
( $biblionumber, $resdate, $priority );
171 # If the reserv had the waiting status, we had the value of the resdate
172 if ( $found eq 'W' ) {
173 $waitingdate = $resdate;
177 # updates take place here
179 my $nextacctno = &getnextacctno
( $borrowernumber );
181 INSERT INTO accountlines
182 (borrowernumber
,accountno
,date
,amount
,description
,accounttype
,amountoutstanding
)
184 (?
,?
,now
(),?
,?
,'Res',?
)
186 my $usth = $dbh->prepare($query);
187 $usth->execute( $borrowernumber, $nextacctno, $fee,
188 "Reserve Charge - $title", $fee );
194 (borrowernumber
,biblionumber
,reservedate
,branchcode
,constrainttype
,
195 priority
,reservenotes
,itemnumber
,found
,waitingdate
,expirationdate
)
200 my $sth = $dbh->prepare($query);
202 $borrowernumber, $biblionumber, $resdate, $branch,
203 $const, $priority, $notes, $checkitem,
204 $found, $waitingdate, $expdate
207 # Send e-mail to librarian if syspref is active
208 if(C4
::Context
->preference("emailLibrarianWhenHoldIsPlaced")){
209 my $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrowernumber);
210 my $branch_details = C4
::Branch
::GetBranchDetail
($borrower->{branchcode
});
211 if ( my $letter = C4
::Letters
::GetPreparedLetter
(
212 module
=> 'reserves',
213 letter_code
=> 'HOLDPLACED',
214 branchcode
=> $branch,
216 'branches' => $branch_details,
217 'borrowers' => $borrower,
218 'biblio' => $biblionumber,
219 'items' => $checkitem,
223 my $admin_email_address =$branch_details->{'branchemail'} || C4
::Context
->preference('KohaAdminEmailAddress');
225 C4
::Letters
::EnqueueLetter
(
227 borrowernumber
=> $borrowernumber,
228 message_transport_type
=> 'email',
229 from_address
=> $admin_email_address,
230 to_address
=> $admin_email_address,
237 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
239 INSERT INTO reserveconstraints
240 (borrowernumber
,biblionumber
,reservedate
,biblioitemnumber
)
244 $sth = $dbh->prepare($query); # keep prepare outside the loop!
245 foreach (@
$bibitems) {
246 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
249 return; # FIXME: why not have a useful return value?
254 $res = GetReserve( $reserve_id );
256 Return the current reserve.
261 my ($reserve_id) = @_;
263 my $dbh = C4
::Context
->dbh;
264 my $query = "SELECT * FROM reserves WHERE reserve_id = ?";
265 my $sth = $dbh->prepare( $query );
266 $sth->execute( $reserve_id );
267 return $sth->fetchrow_hashref();
270 =head2 GetReservesFromBiblionumber
272 my $reserves = GetReservesFromBiblionumber({
273 biblionumber => $biblionumber,
274 [ itemnumber => $itemnumber, ]
278 This function gets the list of reservations for one C<$biblionumber>,
279 returning an arrayref pointing to the reserves for C<$biblionumber>.
281 By default, only reserves whose start date falls before the current
282 time are returned. To return all reserves, including future ones,
283 the C<all_dates> parameter can be included and set to a true value.
285 If the C<itemnumber> parameter is supplied, reserves must be targeted
286 to that item or not targeted to any item at all; otherwise, they
287 are excluded from the list.
291 sub GetReservesFromBiblionumber
{
293 my $biblionumber = $params->{biblionumber
} or return [];
294 my $itemnumber = $params->{itemnumber
};
295 my $all_dates = $params->{all_dates
} // 0;
296 my $dbh = C4
::Context
->dbh;
298 # Find the desired items in the reserves
303 timestamp AS rtimestamp,
317 WHERE biblionumber = ? ";
318 push( @params, $biblionumber );
319 unless ( $all_dates ) {
320 $query .= " AND reservedate <= CAST(NOW() AS DATE) ";
323 $query .= " AND ( itemnumber IS NULL OR itemnumber = ? )";
324 push( @params, $itemnumber );
326 $query .= "ORDER BY priority";
327 my $sth = $dbh->prepare($query);
328 $sth->execute( @params );
331 while ( my $data = $sth->fetchrow_hashref ) {
333 # FIXME - What is this doing? How do constraints work?
334 if ($data->{constrainttype
} eq 'o') {
336 SELECT biblioitemnumber
337 FROM reserveconstraints
338 WHERE biblionumber = ?
339 AND borrowernumber = ?
342 my $csth = $dbh->prepare($query);
343 $csth->execute($data->{biblionumber
}, $data->{borrowernumber
}, $data->{reservedate
});
345 while ( my $bibitemnos = $csth->fetchrow_array ) {
346 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
348 my $count = scalar @bibitemno;
350 # if we have two or more different specific itemtypes
351 # reserved by same person on same day
354 $bdata = GetBiblioItemData
( $bibitemno[$i] ); # FIXME: This doesn't make sense.
355 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
358 # Look up the book we just found.
359 $bdata = GetBiblioItemData
( $bibitemno[0] );
361 # Add the results of this latest search to the current
363 # FIXME - An 'each' would probably be more efficient.
364 foreach my $key ( keys %$bdata ) {
365 $data->{$key} = $bdata->{$key};
368 push @results, $data;
373 =head2 GetReservesFromItemnumber
375 ( $reservedate, $borrowernumber, $branchcode, $reserve_id, $waitingdate ) = GetReservesFromItemnumber($itemnumber);
377 Get the first reserve for a specific item number (based on priority). Returns the abovementioned values for that reserve.
379 The routine does not look at future reserves (read: item level holds), but DOES include future waits (a confirmed future hold).
383 sub GetReservesFromItemnumber
{
384 my ( $itemnumber ) = @_;
385 my $dbh = C4
::Context
->dbh;
387 SELECT reservedate,borrowernumber,branchcode,reserve_id,waitingdate
389 WHERE itemnumber=? AND ( reservedate <= CAST(now() AS date) OR
390 waitingdate IS NOT NULL )
393 my $sth_res = $dbh->prepare($query);
394 $sth_res->execute($itemnumber);
395 my ( $reservedate, $borrowernumber,$branchcode, $reserve_id, $wait ) = $sth_res->fetchrow_array;
396 return ( $reservedate, $borrowernumber, $branchcode, $reserve_id, $wait );
399 =head2 GetReservesFromBorrowernumber
401 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
407 sub GetReservesFromBorrowernumber
{
408 my ( $borrowernumber, $status ) = @_;
409 my $dbh = C4
::Context
->dbh;
412 $sth = $dbh->prepare("
415 WHERE borrowernumber=?
419 $sth->execute($borrowernumber,$status);
421 $sth = $dbh->prepare("
424 WHERE borrowernumber=?
427 $sth->execute($borrowernumber);
429 my $data = $sth->fetchall_arrayref({});
432 #-------------------------------------------------------------------------------------
433 =head2 CanBookBeReserved
435 $error = &CanBookBeReserved($borrowernumber, $biblionumber)
439 sub CanBookBeReserved
{
440 my ($borrowernumber, $biblionumber) = @_;
442 my $items = GetItemnumbersForBiblio
($biblionumber);
443 #get items linked via host records
444 my @hostitems = get_hostitemnumbers_of
($biblionumber);
446 push (@
$items,@hostitems);
449 foreach my $item (@
$items){
450 return 1 if CanItemBeReserved
($borrowernumber, $item);
455 =head2 CanItemBeReserved
457 $error = &CanItemBeReserved($borrowernumber, $itemnumber)
459 This function return 1 if an item can be issued by this borrower.
463 sub CanItemBeReserved
{
464 my ($borrowernumber, $itemnumber) = @_;
466 my $dbh = C4
::Context
->dbh;
467 my $ruleitemtype; # itemtype of the matching issuing rule
468 my $allowedreserves = 0;
470 # we retrieve borrowers and items informations #
471 # item->{itype} will come for biblioitems if necessery
472 my $item = GetItem
($itemnumber);
474 # If an item is damaged and we don't allow holds on damaged items, we can stop right here
475 return 0 if ( $item->{damaged
} && !C4
::Context
->preference('AllowHoldsOnDamagedItems') );
477 my $borrower = C4
::Members
::GetMember
('borrowernumber'=>$borrowernumber);
479 my $controlbranch = C4
::Context
->preference('ReservesControlBranch');
480 my $itemtypefield = C4
::Context
->preference('item-level_itypes') ?
"itype" : "itemtype";
482 # we retrieve user rights on this itemtype and branchcode
483 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
485 WHERE (categorycode in (?,'*') )
486 AND (itemtype IN (?,'*'))
487 AND (branchcode IN (?,'*'))
494 my $querycount ="SELECT
497 LEFT JOIN items USING (itemnumber)
498 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
499 LEFT JOIN borrowers USING (borrowernumber)
500 WHERE borrowernumber = ?
505 my $branchfield = "reserves.branchcode";
507 if( $controlbranch eq "ItemHomeLibrary" ){
508 $branchfield = "items.homebranch";
509 $branchcode = $item->{homebranch
};
510 }elsif( $controlbranch eq "PatronLibrary" ){
511 $branchfield = "borrowers.branchcode";
512 $branchcode = $borrower->{branchcode
};
516 $sth->execute($borrower->{'categorycode'}, $item->{'itype'}, $branchcode);
517 if(my $rights = $sth->fetchrow_hashref()){
518 $ruleitemtype = $rights->{itemtype
};
519 $allowedreserves = $rights->{reservesallowed
};
526 $querycount .= "AND $branchfield = ?";
528 $querycount .= " AND $itemtypefield = ?" if ($ruleitemtype ne "*");
529 my $sthcount = $dbh->prepare($querycount);
531 if($ruleitemtype eq "*"){
532 $sthcount->execute($borrowernumber, $branchcode);
534 $sthcount->execute($borrowernumber, $branchcode, $ruleitemtype);
537 my $reservecount = "0";
538 if(my $rowcount = $sthcount->fetchrow_hashref()){
539 $reservecount = $rowcount->{count
};
542 # we check if it's ok or not
543 if( $reservecount >= $allowedreserves ){
547 # If reservecount is ok, we check item branch if IndependentBranches is ON
548 # and canreservefromotherbranches is OFF
549 if ( C4
::Context
->preference('IndependentBranches')
550 and !C4
::Context
->preference('canreservefromotherbranches') )
552 my $itembranch = $item->{homebranch
};
553 if ($itembranch ne $borrower->{branchcode
}) {
560 #--------------------------------------------------------------------------------
561 =head2 GetReserveCount
563 $number = &GetReserveCount($borrowernumber);
565 this function returns the number of reservation for a borrower given on input arg.
569 sub GetReserveCount
{
570 my ($borrowernumber) = @_;
572 my $dbh = C4
::Context
->dbh;
575 SELECT COUNT(*) AS counter
577 WHERE borrowernumber = ?
579 my $sth = $dbh->prepare($query);
580 $sth->execute($borrowernumber);
581 my $row = $sth->fetchrow_hashref;
582 return $row->{counter
};
585 =head2 GetOtherReserves
587 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
589 Check queued list of this document and check if this document must be transfered
593 sub GetOtherReserves
{
594 my ($itemnumber) = @_;
597 my ( undef, $checkreserves, undef ) = CheckReserves
($itemnumber);
598 if ($checkreserves) {
599 my $iteminfo = GetItem
($itemnumber);
600 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
601 $messages->{'transfert'} = $checkreserves->{'branchcode'};
602 #minus priorities of others reservs
603 ModReserveMinusPriority
(
605 $checkreserves->{'reserve_id'},
608 #launch the subroutine dotransfer
609 C4
::Items
::ModItemTransfer
(
611 $iteminfo->{'holdingbranch'},
612 $checkreserves->{'branchcode'}
617 #step 2b : case of a reservation on the same branch, set the waiting status
619 $messages->{'waiting'} = 1;
620 ModReserveMinusPriority
(
622 $checkreserves->{'reserve_id'},
624 ModReserveStatus
($itemnumber,'W');
627 $nextreservinfo = $checkreserves->{'borrowernumber'};
630 return ( $messages, $nextreservinfo );
635 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
637 Calculate the fee for a reserve
642 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
645 my $dbh = C4
::Context
->dbh;
646 my $const = lc substr( $constraint, 0, 1 );
648 SELECT
* FROM borrowers
649 LEFT JOIN categories ON borrowers
.categorycode
= categories
.categorycode
650 WHERE borrowernumber
= ?
652 my $sth = $dbh->prepare($query);
653 $sth->execute($borrowernumber);
654 my $data = $sth->fetchrow_hashref;
655 my $fee = $data->{'reservefee'};
656 my $cntitems = @
- > $bibitems;
660 # check for items on issue
661 # first find biblioitem records
663 my $sth1 = $dbh->prepare(
664 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
665 WHERE (biblio.biblionumber = ?)"
667 $sth1->execute($biblionumber);
668 while ( my $data1 = $sth1->fetchrow_hashref ) {
669 if ( $const eq "a" ) {
670 push @biblioitems, $data1;
675 while ( $x < $cntitems ) {
676 if ( @
$bibitems->{'biblioitemnumber'} ==
677 $data->{'biblioitemnumber'} )
683 if ( $const eq 'o' ) {
685 push @biblioitems, $data1;
690 push @biblioitems, $data1;
695 my $cntitemsfound = @biblioitems;
699 while ( $x < $cntitemsfound ) {
700 my $bitdata = $biblioitems[$x];
701 my $sth2 = $dbh->prepare(
703 WHERE biblioitemnumber = ?"
705 $sth2->execute( $bitdata->{'biblioitemnumber'} );
706 while ( my $itdata = $sth2->fetchrow_hashref ) {
707 my $sth3 = $dbh->prepare(
708 "SELECT * FROM issues
709 WHERE itemnumber = ?"
711 $sth3->execute( $itdata->{'itemnumber'} );
712 if ( my $isdata = $sth3->fetchrow_hashref ) {
720 if ( $allissued == 0 ) {
722 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
723 $rsth->execute($biblionumber);
724 if ( my $rdata = $rsth->fetchrow_hashref ) {
734 =head2 GetReservesToBranch
736 @transreserv = GetReservesToBranch( $frombranch );
738 Get reserve list for a given branch
742 sub GetReservesToBranch
{
743 my ( $frombranch ) = @_;
744 my $dbh = C4
::Context
->dbh;
745 my $sth = $dbh->prepare(
746 "SELECT reserve_id,borrowernumber,reservedate,itemnumber,timestamp
751 $sth->execute( $frombranch );
754 while ( my $data = $sth->fetchrow_hashref ) {
755 $transreserv[$i] = $data;
758 return (@transreserv);
761 =head2 GetReservesForBranch
763 @transreserv = GetReservesForBranch($frombranch);
767 sub GetReservesForBranch
{
768 my ($frombranch) = @_;
769 my $dbh = C4
::Context
->dbh;
772 SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate
777 $query .= " AND branchcode=? " if ( $frombranch );
778 $query .= "ORDER BY waitingdate" ;
780 my $sth = $dbh->prepare($query);
782 $sth->execute($frombranch);
789 while ( my $data = $sth->fetchrow_hashref ) {
790 $transreserv[$i] = $data;
793 return (@transreserv);
796 =head2 GetReserveStatus
798 $reservestatus = GetReserveStatus($itemnumber, $biblionumber);
800 Take an itemnumber or a biblionumber and return the status of the reserve places on it.
801 If several reserves exist, the reserve with the lower priority is given.
805 ## FIXME: I don't think this does what it thinks it does.
806 ## It only ever checks the first reserve result, even though
807 ## multiple reserves for that bib can have the itemnumber set
808 ## the sub is only used once in the codebase.
809 sub GetReserveStatus
{
810 my ($itemnumber, $biblionumber) = @_;
812 my $dbh = C4
::Context
->dbh;
814 my ($sth, $found, $priority);
816 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE itemnumber = ? order by priority LIMIT 1");
817 $sth->execute($itemnumber);
818 ($found, $priority) = $sth->fetchrow_array;
821 if ( $biblionumber and not defined $found and not defined $priority ) {
822 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE biblionumber = ? order by priority LIMIT 1");
823 $sth->execute($biblionumber);
824 ($found, $priority) = $sth->fetchrow_array;
828 return 'Waiting' if $found eq 'W' and $priority == 0;
829 return 'Finished' if $found eq 'F';
832 return 'Reserved' if $priority > 0;
834 return ''; # empty string here will remove need for checking undef, or less log lines
839 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
840 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
841 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber,undef,$lookahead);
843 Find a book in the reserves.
845 C<$itemnumber> is the book's item number.
846 C<$lookahead> is the number of days to look in advance for future reserves.
848 As I understand it, C<&CheckReserves> looks for the given item in the
849 reserves. If it is found, that's a match, and C<$status> is set to
852 Otherwise, it finds the most important item in the reserves with the
853 same biblio number as this book (I'm not clear on this) and returns it
854 with C<$status> set to C<Reserved>.
856 C<&CheckReserves> returns a two-element list:
858 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
860 C<$reserve> is the reserve item that matched. It is a
861 reference-to-hash whose keys are mostly the fields of the reserves
862 table in the Koha database.
867 my ( $item, $barcode, $lookahead_days) = @_;
868 my $dbh = C4
::Context
->dbh;
871 if (C4
::Context
->preference('item-level_itypes')){
873 SELECT items.biblionumber,
874 items.biblioitemnumber,
875 itemtypes.notforloan,
876 items.notforloan AS itemnotforloan,
880 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
881 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
886 SELECT items.biblionumber,
887 items.biblioitemnumber,
888 itemtypes.notforloan,
889 items.notforloan AS itemnotforloan,
893 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
894 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
899 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
900 $sth->execute($item);
903 $sth = $dbh->prepare("$select WHERE barcode = ?");
904 $sth->execute($barcode);
906 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
907 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber, $damaged ) = $sth->fetchrow_array;
909 return if ( $damaged && !C4
::Context
->preference('AllowHoldsOnDamagedItems') );
911 return unless $itemnumber; # bail if we got nothing.
913 # if item is not for loan it cannot be reserved either.....
914 # execpt where items.notforloan < 0 : This indicates the item is holdable.
915 return if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
917 # Find this item in the reserves
918 my @reserves = _Findgroupreserve
( $bibitem, $biblio, $itemnumber, $lookahead_days);
920 # $priority and $highest are used to find the most important item
921 # in the list returned by &_Findgroupreserve. (The lower $priority,
922 # the more important the item.)
923 # $highest is the most important item we've seen so far.
925 if (scalar @reserves) {
926 my $priority = 10000000;
927 foreach my $res (@reserves) {
928 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
929 return ( "Waiting", $res, \
@reserves ); # Found it
931 # See if this item is more important than what we've got so far
932 if ( $res->{'priority'} && $res->{'priority'} < $priority ) {
933 my $borrowerinfo=C4
::Members
::GetMember
(borrowernumber
=> $res->{'borrowernumber'});
934 my $iteminfo=C4
::Items
::GetItem
($itemnumber);
935 my $branch = GetReservesControlBranch
( $iteminfo, $borrowerinfo );
936 my $branchitemrule = C4
::Circulation
::GetBranchItemRule
($branch,$iteminfo->{'itype'});
937 next if ($branchitemrule->{'holdallowed'} == 0);
938 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
939 $priority = $res->{'priority'};
946 # If we get this far, then no exact match was found.
947 # We return the most important (i.e. next) reservation.
949 $highest->{'itemnumber'} = $item;
950 return ( "Reserved", $highest, \
@reserves );
956 =head2 CancelExpiredReserves
958 CancelExpiredReserves();
960 Cancels all reserves with an expiration date from before today.
964 sub CancelExpiredReserves
{
966 # Cancel reserves that have passed their expiration date.
967 my $dbh = C4
::Context
->dbh;
968 my $sth = $dbh->prepare( "
969 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
970 AND expirationdate IS NOT NULL
975 while ( my $res = $sth->fetchrow_hashref() ) {
976 CancelReserve
({ reserve_id
=> $res->{'reserve_id'} });
979 # Cancel reserves that have been waiting too long
980 if ( C4
::Context
->preference("ExpireReservesMaxPickUpDelay") ) {
981 my $max_pickup_delay = C4
::Context
->preference("ReservesMaxPickUpDelay");
982 my $charge = C4
::Context
->preference("ExpireReservesMaxPickUpDelayCharge");
983 my $cancel_on_holidays = C4
::Context
->preference('ExpireReservesOnHolidays');
985 my $today = dt_from_string
();
987 my $query = "SELECT * FROM reserves WHERE TO_DAYS( NOW() ) - TO_DAYS( waitingdate ) > ? AND found = 'W' AND priority = 0";
988 $sth = $dbh->prepare( $query );
989 $sth->execute( $max_pickup_delay );
991 while ( my $res = $sth->fetchrow_hashref ) {
993 unless ( $cancel_on_holidays ) {
994 my $calendar = Koha
::Calendar
->new( branchcode
=> $res->{'branchcode'} );
995 my $is_holiday = $calendar->is_holiday( $today );
1004 manualinvoice
($res->{'borrowernumber'}, $res->{'itemnumber'}, 'Hold waiting too long', 'F', $charge);
1007 CancelReserve
({ reserve_id
=> $res->{'reserve_id'} });
1014 =head2 AutoUnsuspendReserves
1016 AutoUnsuspendReserves();
1018 Unsuspends all suspended reserves with a suspend_until date from before today.
1022 sub AutoUnsuspendReserves
{
1024 my $dbh = C4
::Context
->dbh;
1026 my $query = "UPDATE reserves SET suspend = 0, suspend_until = NULL WHERE DATE( suspend_until ) < DATE( CURDATE() )";
1027 my $sth = $dbh->prepare( $query );
1032 =head2 CancelReserve
1034 CancelReserve({ reserve_id => $reserve_id, [ biblionumber => $biblionumber, borrowernumber => $borrrowernumber, itemnumber => $itemnumber ] });
1041 my ( $params ) = @_;
1043 my $reserve_id = $params->{'reserve_id'};
1044 $reserve_id = GetReserveId
( $params ) unless ( $reserve_id );
1046 return unless ( $reserve_id );
1048 my $dbh = C4
::Context
->dbh;
1050 my $reserve = GetReserve
( $reserve_id );
1054 SET cancellationdate = now(),
1057 WHERE reserve_id = ?
1059 my $sth = $dbh->prepare($query);
1060 $sth->execute( $reserve_id );
1063 INSERT INTO old_reserves
1064 SELECT * FROM reserves
1065 WHERE reserve_id = ?
1067 $sth = $dbh->prepare($query);
1068 $sth->execute( $reserve_id );
1071 DELETE FROM reserves
1072 WHERE reserve_id = ?
1074 $sth = $dbh->prepare($query);
1075 $sth->execute( $reserve_id );
1077 # now fix the priority on the others....
1078 _FixPriority
({ biblionumber
=> $reserve->{biblionumber
} });
1083 ModReserve({ rank => $rank,
1084 reserve_id => $reserve_id,
1085 branchcode => $branchcode
1086 [, itemnumber => $itemnumber ]
1087 [, biblionumber => $biblionumber, $borrowernumber => $borrowernumber ]
1090 Change a hold request's priority or cancel it.
1092 C<$rank> specifies the effect of the change. If C<$rank>
1093 is 'W' or 'n', nothing happens. This corresponds to leaving a
1094 request alone when changing its priority in the holds queue
1097 If C<$rank> is 'del', the hold request is cancelled.
1099 If C<$rank> is an integer greater than zero, the priority of
1100 the request is set to that value. Since priority != 0 means
1101 that the item is not waiting on the hold shelf, setting the
1102 priority to a non-zero value also sets the request's found
1103 status and waiting date to NULL.
1105 The optional C<$itemnumber> parameter is used only when
1106 C<$rank> is a non-zero integer; if supplied, the itemnumber
1107 of the hold request is set accordingly; if omitted, the itemnumber
1110 B<FIXME:> Note that the forgoing can have the effect of causing
1111 item-level hold requests to turn into title-level requests. This
1112 will be fixed once reserves has separate columns for requested
1113 itemnumber and supplying itemnumber.
1118 my ( $params ) = @_;
1120 my $rank = $params->{'rank'};
1121 my $reserve_id = $params->{'reserve_id'};
1122 my $branchcode = $params->{'branchcode'};
1123 my $itemnumber = $params->{'itemnumber'};
1124 my $suspend_until = $params->{'suspend_until'};
1125 my $borrowernumber = $params->{'borrowernumber'};
1126 my $biblionumber = $params->{'biblionumber'};
1128 return if $rank eq "W";
1129 return if $rank eq "n";
1131 return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
1132 $reserve_id = GetReserveId
({ biblionumber
=> $biblionumber, borrowernumber
=> $borrowernumber, itemnumber
=> $itemnumber }) unless ( $reserve_id );
1134 my $dbh = C4
::Context
->dbh;
1135 if ( $rank eq "del" ) {
1136 CancelReserve
({ reserve_id
=> $reserve_id });
1138 elsif ($rank =~ /^\d+/ and $rank > 0) {
1140 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
1141 WHERE reserve_id = ?
1143 my $sth = $dbh->prepare($query);
1144 $sth->execute( $rank, $branchcode, $itemnumber, $reserve_id );
1146 if ( defined( $suspend_until ) ) {
1147 if ( $suspend_until ) {
1148 $suspend_until = C4
::Dates
->new( $suspend_until )->output("iso");
1149 $dbh->do("UPDATE reserves SET suspend = 1, suspend_until = ? WHERE reserve_id = ?", undef, ( $suspend_until, $reserve_id ) );
1151 $dbh->do("UPDATE reserves SET suspend_until = NULL WHERE reserve_id = ?", undef, ( $reserve_id ) );
1155 _FixPriority
({ reserve_id
=> $reserve_id, rank
=>$rank });
1159 =head2 ModReserveFill
1161 &ModReserveFill($reserve);
1163 Fill a reserve. If I understand this correctly, this means that the
1164 reserved book has been found and given to the patron who reserved it.
1166 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1167 whose keys are fields from the reserves table in the Koha database.
1171 sub ModReserveFill
{
1173 my $dbh = C4
::Context
->dbh;
1174 # fill in a reserve record....
1175 my $reserve_id = $res->{'reserve_id'};
1176 my $biblionumber = $res->{'biblionumber'};
1177 my $borrowernumber = $res->{'borrowernumber'};
1178 my $resdate = $res->{'reservedate'};
1180 # get the priority on this record....
1182 my $query = "SELECT priority
1184 WHERE biblionumber = ?
1185 AND borrowernumber = ?
1186 AND reservedate = ?";
1187 my $sth = $dbh->prepare($query);
1188 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1189 ($priority) = $sth->fetchrow_array;
1191 # update the database...
1192 $query = "UPDATE reserves
1195 WHERE biblionumber = ?
1197 AND borrowernumber = ?
1199 $sth = $dbh->prepare($query);
1200 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1202 # move to old_reserves
1203 $query = "INSERT INTO old_reserves
1204 SELECT * FROM reserves
1205 WHERE biblionumber = ?
1207 AND borrowernumber = ?
1209 $sth = $dbh->prepare($query);
1210 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1211 $query = "DELETE FROM reserves
1212 WHERE biblionumber = ?
1214 AND borrowernumber = ?
1216 $sth = $dbh->prepare($query);
1217 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1219 # now fix the priority on the others (if the priority wasn't
1220 # already sorted!)....
1221 unless ( $priority == 0 ) {
1222 _FixPriority
({ reserve_id
=> $reserve_id });
1226 =head2 ModReserveStatus
1228 &ModReserveStatus($itemnumber, $newstatus);
1230 Update the reserve status for the active (priority=0) reserve.
1232 $itemnumber is the itemnumber the reserve is on
1234 $newstatus is the new status.
1238 sub ModReserveStatus
{
1240 #first : check if we have a reservation for this item .
1241 my ($itemnumber, $newstatus) = @_;
1242 my $dbh = C4
::Context
->dbh;
1244 my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1245 my $sth_set = $dbh->prepare($query);
1246 $sth_set->execute( $newstatus, $itemnumber );
1248 if ( C4
::Context
->preference("ReturnToShelvingCart") && $newstatus ) {
1249 CartToShelf
( $itemnumber );
1253 =head2 ModReserveAffect
1255 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1257 This function affect an item and a status for a given reserve
1258 The itemnumber parameter is used to find the biblionumber.
1259 with the biblionumber & the borrowernumber, we can affect the itemnumber
1260 to the correct reserve.
1262 if $transferToDo is not set, then the status is set to "Waiting" as well.
1263 otherwise, a transfer is on the way, and the end of the transfer will
1264 take care of the waiting status
1268 sub ModReserveAffect
{
1269 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1270 my $dbh = C4
::Context
->dbh;
1272 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1273 # attached to $itemnumber
1274 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1275 $sth->execute($itemnumber);
1276 my ($biblionumber) = $sth->fetchrow;
1278 # get request - need to find out if item is already
1279 # waiting in order to not send duplicate hold filled notifications
1280 my $reserve_id = GetReserveId
({
1281 borrowernumber
=> $borrowernumber,
1282 biblionumber
=> $biblionumber,
1284 return unless defined $reserve_id;
1285 my $request = GetReserveInfo
($reserve_id);
1286 my $already_on_shelf = ($request && $request->{found
} eq 'W') ?
1 : 0;
1288 # If we affect a reserve that has to be transfered, don't set to Waiting
1290 if ($transferToDo) {
1296 WHERE borrowernumber = ?
1297 AND biblionumber = ?
1301 # affect the reserve to Waiting as well.
1306 waitingdate = NOW(),
1308 WHERE borrowernumber = ?
1309 AND biblionumber = ?
1312 $sth = $dbh->prepare($query);
1313 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1314 _koha_notify_reserve
( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1315 _FixPriority
( { biblionumber
=> $biblionumber } );
1316 if ( C4
::Context
->preference("ReturnToShelvingCart") ) {
1317 CartToShelf
( $itemnumber );
1323 =head2 ModReserveCancelAll
1325 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1327 function to cancel reserv,check other reserves, and transfer document if it's necessary
1331 sub ModReserveCancelAll
{
1334 my ( $itemnumber, $borrowernumber ) = @_;
1336 #step 1 : cancel the reservation
1337 my $CancelReserve = CancelReserve
({ itemnumber
=> $itemnumber, borrowernumber
=> $borrowernumber });
1339 #step 2 launch the subroutine of the others reserves
1340 ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
1342 return ( $messages, $nextreservinfo );
1345 =head2 ModReserveMinusPriority
1347 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1349 Reduce the values of queued list
1353 sub ModReserveMinusPriority
{
1354 my ( $itemnumber, $reserve_id ) = @_;
1356 #first step update the value of the first person on reserv
1357 my $dbh = C4
::Context
->dbh;
1360 SET priority = 0 , itemnumber = ?
1361 WHERE reserve_id = ?
1363 my $sth_upd = $dbh->prepare($query);
1364 $sth_upd->execute( $itemnumber, $reserve_id );
1365 # second step update all others reserves
1366 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> '0' });
1369 =head2 GetReserveInfo
1371 &GetReserveInfo($reserve_id);
1373 Get item and borrower details for a current hold.
1374 Current implementation this query should have a single result.
1378 sub GetReserveInfo
{
1379 my ( $reserve_id ) = @_;
1380 my $dbh = C4
::Context
->dbh;
1385 reserves.borrowernumber,
1386 reserves.biblionumber,
1387 reserves.branchcode,
1388 reserves.waitingdate,
1404 items.holdingbranch,
1405 items.itemcallnumber,
1411 LEFT JOIN items USING(itemnumber)
1412 LEFT JOIN borrowers USING(borrowernumber)
1413 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1414 WHERE reserves.reserve_id = ?";
1415 my $sth = $dbh->prepare($strsth);
1416 $sth->execute($reserve_id);
1418 my $data = $sth->fetchrow_hashref;
1422 =head2 IsAvailableForItemLevelRequest
1424 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1426 Checks whether a given item record is available for an
1427 item-level hold request. An item is available if
1429 * it is not lost AND
1430 * it is not damaged AND
1431 * it is not withdrawn AND
1432 * does not have a not for loan value > 0
1434 Whether or not the item is currently on loan is
1435 also checked - if the AllowOnShelfHolds system preference
1436 is ON, an item can be requested even if it is currently
1437 on loan to somebody else. If the system preference
1438 is OFF, an item that is currently checked out cannot
1439 be the target of an item-level hold request.
1441 Note that IsAvailableForItemLevelRequest() does not
1442 check if the staff operator is authorized to place
1443 a request on the item - in particular,
1444 this routine does not check IndependentBranches
1445 and canreservefromotherbranches.
1449 sub IsAvailableForItemLevelRequest
{
1450 my $itemnumber = shift;
1452 my $item = GetItem
($itemnumber);
1454 # must check the notforloan setting of the itemtype
1455 # FIXME - a lot of places in the code do this
1456 # or something similar - need to be
1458 my $dbh = C4
::Context
->dbh;
1459 my $notforloan_query;
1460 if (C4
::Context
->preference('item-level_itypes')) {
1461 $notforloan_query = "SELECT itemtypes.notforloan
1463 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1464 WHERE itemnumber = ?";
1466 $notforloan_query = "SELECT itemtypes.notforloan
1468 JOIN biblioitems USING (biblioitemnumber)
1469 JOIN itemtypes USING (itemtype)
1470 WHERE itemnumber = ?";
1472 my $sth = $dbh->prepare($notforloan_query);
1473 $sth->execute($itemnumber);
1474 my $notforloan_per_itemtype = 0;
1475 if (my ($notforloan) = $sth->fetchrow_array) {
1476 $notforloan_per_itemtype = 1 if $notforloan;
1479 my $available_per_item = 1;
1480 $available_per_item = 0 if $item->{itemlost
} or
1481 ( $item->{notforloan
} > 0 ) or
1482 ($item->{damaged
} and not C4
::Context
->preference('AllowHoldsOnDamagedItems')) or
1483 $item->{withdrawn
} or
1484 $notforloan_per_itemtype;
1487 if (C4
::Context
->preference('AllowOnShelfHolds')) {
1488 return $available_per_item;
1490 return ($available_per_item and ($item->{onloan
} or GetReserveStatus
($itemnumber) eq "Waiting"));
1494 =head2 AlterPriority
1496 AlterPriority( $where, $reserve_id );
1498 This function changes a reserve's priority up, down, to the top, or to the bottom.
1499 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1504 my ( $where, $reserve_id ) = @_;
1506 my $dbh = C4
::Context
->dbh;
1508 my $reserve = GetReserve
( $reserve_id );
1510 if ( $reserve->{cancellationdate
} ) {
1511 warn "I cannot alter the priority for reserve_id $reserve_id, the reserve has been cancelled (".$reserve->{cancellationdate
}.')';
1515 if ( $where eq 'up' || $where eq 'down' ) {
1517 my $priority = $reserve->{'priority'};
1518 $priority = $where eq 'up' ?
$priority - 1 : $priority + 1;
1519 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> $priority })
1521 } elsif ( $where eq 'top' ) {
1523 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> '1' })
1525 } elsif ( $where eq 'bottom' ) {
1527 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> '999999' });
1532 =head2 ToggleLowestPriority
1534 ToggleLowestPriority( $borrowernumber, $biblionumber );
1536 This function sets the lowestPriority field to true if is false, and false if it is true.
1540 sub ToggleLowestPriority
{
1541 my ( $reserve_id ) = @_;
1543 my $dbh = C4
::Context
->dbh;
1545 my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
1546 $sth->execute( $reserve_id );
1548 _FixPriority
({ reserve_id
=> $reserve_id, rank
=> '999999' });
1551 =head2 ToggleSuspend
1553 ToggleSuspend( $reserve_id );
1555 This function sets the suspend field to true if is false, and false if it is true.
1556 If the reserve is currently suspended with a suspend_until date, that date will
1557 be cleared when it is unsuspended.
1562 my ( $reserve_id, $suspend_until ) = @_;
1564 $suspend_until = output_pref
(
1566 dt
=> dt_from_string
($suspend_until),
1567 dateformat
=> 'iso',
1570 ) if ($suspend_until);
1572 my $do_until = ( $suspend_until ) ?
'?' : 'NULL';
1574 my $dbh = C4
::Context
->dbh;
1576 my $sth = $dbh->prepare(
1577 "UPDATE reserves SET suspend = NOT suspend,
1578 suspend_until = CASE WHEN suspend = 0 THEN NULL ELSE $do_until END
1579 WHERE reserve_id = ?
1583 push( @params, $suspend_until ) if ( $suspend_until );
1584 push( @params, $reserve_id );
1586 $sth->execute( @params );
1592 borrowernumber => $borrowernumber,
1593 [ biblionumber => $biblionumber, ]
1594 [ suspend_until => $suspend_until, ]
1595 [ suspend => $suspend ]
1598 This function accepts a set of hash keys as its parameters.
1599 It requires either borrowernumber or biblionumber, or both.
1601 suspend_until is wholly optional.
1608 my $borrowernumber = $params{'borrowernumber'} || undef;
1609 my $biblionumber = $params{'biblionumber'} || undef;
1610 my $suspend_until = $params{'suspend_until'} || undef;
1611 my $suspend = defined( $params{'suspend'} ) ?
$params{'suspend'} : 1;
1613 $suspend_until = C4
::Dates
->new( $suspend_until )->output("iso") if ( defined( $suspend_until ) );
1615 return unless ( $borrowernumber || $biblionumber );
1617 my ( $query, $sth, $dbh, @query_params );
1619 $query = "UPDATE reserves SET suspend = ? ";
1620 push( @query_params, $suspend );
1622 $query .= ", suspend_until = NULL ";
1623 } elsif ( $suspend_until ) {
1624 $query .= ", suspend_until = ? ";
1625 push( @query_params, $suspend_until );
1627 $query .= " WHERE ";
1628 if ( $borrowernumber ) {
1629 $query .= " borrowernumber = ? ";
1630 push( @query_params, $borrowernumber );
1632 $query .= " AND " if ( $borrowernumber && $biblionumber );
1633 if ( $biblionumber ) {
1634 $query .= " biblionumber = ? ";
1635 push( @query_params, $biblionumber );
1637 $query .= " AND found IS NULL ";
1639 $dbh = C4
::Context
->dbh;
1640 $sth = $dbh->prepare( $query );
1641 $sth->execute( @query_params );
1648 reserve_id => $reserve_id,
1650 [ignoreSetLowestRank => $ignoreSetLowestRank]
1655 _FixPriority({ biblionumber => $biblionumber});
1657 This routine adjusts the priority of a hold request and holds
1660 In the first form, where a reserve_id is passed, the priority of the
1661 hold is set to supplied rank, and other holds for that bib are adjusted
1662 accordingly. If the rank is "del", the hold is cancelled. If no rank
1663 is supplied, all of the holds on that bib have their priority adjusted
1664 as if the second form had been used.
1666 In the second form, where a biblionumber is passed, the holds on that
1667 bib (that are not captured) are sorted in order of increasing priority,
1668 then have reserves.priority set so that the first non-captured hold
1669 has its priority set to 1, the second non-captured hold has its priority
1670 set to 2, and so forth.
1672 In both cases, holds that have the lowestPriority flag on are have their
1673 priority adjusted to ensure that they remain at the end of the line.
1675 Note that the ignoreSetLowestRank parameter is meant to be used only
1676 when _FixPriority calls itself.
1681 my ( $params ) = @_;
1682 my $reserve_id = $params->{reserve_id
};
1683 my $rank = $params->{rank
} // '';
1684 my $ignoreSetLowestRank = $params->{ignoreSetLowestRank
};
1685 my $biblionumber = $params->{biblionumber
};
1687 my $dbh = C4
::Context
->dbh;
1689 unless ( $biblionumber ) {
1690 my $res = GetReserve
( $reserve_id );
1691 $biblionumber = $res->{biblionumber
};
1694 if ( $rank eq "del" ) {
1695 CancelReserve
({ reserve_id
=> $reserve_id });
1697 elsif ( $rank eq "W" || $rank eq "0" ) {
1699 # make sure priority for waiting or in-transit items is 0
1703 WHERE reserve_id = ?
1704 AND found IN ('W', 'T')
1706 my $sth = $dbh->prepare($query);
1707 $sth->execute( $reserve_id );
1713 SELECT reserve_id, borrowernumber, reservedate, constrainttype
1715 WHERE biblionumber = ?
1716 AND ((found <> 'W' AND found <> 'T') OR found IS NULL)
1717 ORDER BY priority ASC
1719 my $sth = $dbh->prepare($query);
1720 $sth->execute( $biblionumber );
1721 while ( my $line = $sth->fetchrow_hashref ) {
1722 push( @priority, $line );
1725 # To find the matching index
1727 my $key = -1; # to allow for 0 to be a valid result
1728 for ( $i = 0 ; $i < @priority ; $i++ ) {
1729 if ( $reserve_id == $priority[$i]->{'reserve_id'} ) {
1730 $key = $i; # save the index
1735 # if index exists in array then move it to new position
1736 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1737 my $new_rank = $rank -
1738 1; # $new_rank is what you want the new index to be in the array
1739 my $moving_item = splice( @priority, $key, 1 );
1740 splice( @priority, $new_rank, 0, $moving_item );
1743 # now fix the priority on those that are left....
1747 WHERE reserve_id = ?
1749 $sth = $dbh->prepare($query);
1750 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1753 $priority[$j]->{'reserve_id'}
1757 $sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1760 unless ( $ignoreSetLowestRank ) {
1761 while ( my $res = $sth->fetchrow_hashref() ) {
1763 reserve_id
=> $res->{'reserve_id'},
1765 ignoreSetLowestRank
=> 1
1771 =head2 _Findgroupreserve
1773 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber, $lookahead);
1775 Looks for an item-specific match first, then for a title-level match, returning the
1776 first match found. If neither, then we look for a 3rd kind of match based on
1777 reserve constraints.
1778 Lookahead is the number of days to look in advance.
1780 TODO: add more explanation about reserve constraints
1782 C<&_Findgroupreserve> returns :
1783 C<@results> is an array of references-to-hash whose keys are mostly
1784 fields from the reserves table of the Koha database, plus
1785 C<biblioitemnumber>.
1789 sub _Findgroupreserve
{
1790 my ( $bibitem, $biblio, $itemnumber, $lookahead) = @_;
1791 my $dbh = C4
::Context
->dbh;
1793 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1794 # check for exact targetted match
1795 my $item_level_target_query = qq/
1796 SELECT reserves
.biblionumber AS biblionumber
,
1797 reserves
.borrowernumber AS borrowernumber
,
1798 reserves
.reservedate AS reservedate
,
1799 reserves
.branchcode AS branchcode
,
1800 reserves
.cancellationdate AS cancellationdate
,
1801 reserves
.found AS found
,
1802 reserves
.reservenotes AS reservenotes
,
1803 reserves
.priority AS priority
,
1804 reserves
.timestamp AS timestamp
,
1805 biblioitems
.biblioitemnumber AS biblioitemnumber
,
1806 reserves
.itemnumber AS itemnumber
,
1807 reserves
.reserve_id AS reserve_id
1809 JOIN biblioitems USING
(biblionumber
)
1810 JOIN hold_fill_targets USING
(biblionumber
, borrowernumber
, itemnumber
)
1813 AND item_level_request
= 1
1815 AND reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1818 my $sth = $dbh->prepare($item_level_target_query);
1819 $sth->execute($itemnumber, $lookahead||0);
1821 if ( my $data = $sth->fetchrow_hashref ) {
1822 push( @results, $data );
1824 return @results if @results;
1826 # check for title-level targetted match
1827 my $title_level_target_query = qq/
1828 SELECT reserves
.biblionumber AS biblionumber
,
1829 reserves
.borrowernumber AS borrowernumber
,
1830 reserves
.reservedate AS reservedate
,
1831 reserves
.branchcode AS branchcode
,
1832 reserves
.cancellationdate AS cancellationdate
,
1833 reserves
.found AS found
,
1834 reserves
.reservenotes AS reservenotes
,
1835 reserves
.priority AS priority
,
1836 reserves
.timestamp AS timestamp
,
1837 biblioitems
.biblioitemnumber AS biblioitemnumber
,
1838 reserves
.itemnumber AS itemnumber
,
1839 reserves
.reserve_id AS reserve_id
1841 JOIN biblioitems USING
(biblionumber
)
1842 JOIN hold_fill_targets USING
(biblionumber
, borrowernumber
)
1845 AND item_level_request
= 0
1846 AND hold_fill_targets
.itemnumber
= ?
1847 AND reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1850 $sth = $dbh->prepare($title_level_target_query);
1851 $sth->execute($itemnumber, $lookahead||0);
1853 if ( my $data = $sth->fetchrow_hashref ) {
1854 push( @results, $data );
1856 return @results if @results;
1859 SELECT reserves
.biblionumber AS biblionumber
,
1860 reserves
.borrowernumber AS borrowernumber
,
1861 reserves
.reservedate AS reservedate
,
1862 reserves
.waitingdate AS waitingdate
,
1863 reserves
.branchcode AS branchcode
,
1864 reserves
.cancellationdate AS cancellationdate
,
1865 reserves
.found AS found
,
1866 reserves
.reservenotes AS reservenotes
,
1867 reserves
.priority AS priority
,
1868 reserves
.timestamp AS timestamp
,
1869 reserveconstraints
.biblioitemnumber AS biblioitemnumber
,
1870 reserves
.itemnumber AS itemnumber
,
1871 reserves
.reserve_id AS reserve_id
1873 LEFT JOIN reserveconstraints ON reserves
.biblionumber
= reserveconstraints
.biblionumber
1874 WHERE reserves
.biblionumber
= ?
1875 AND
( ( reserveconstraints
.biblioitemnumber
= ?
1876 AND reserves
.borrowernumber
= reserveconstraints
.borrowernumber
1877 AND reserves
.reservedate
= reserveconstraints
.reservedate
)
1878 OR reserves
.constrainttype
='a' )
1879 AND
(reserves
.itemnumber IS NULL OR reserves
.itemnumber
= ?
)
1880 AND reserves
.reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1883 $sth = $dbh->prepare($query);
1884 $sth->execute( $biblio, $bibitem, $itemnumber, $lookahead||0);
1886 while ( my $data = $sth->fetchrow_hashref ) {
1887 push( @results, $data );
1892 =head2 _koha_notify_reserve
1894 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1896 Sends a notification to the patron that their hold has been filled (through
1897 ModReserveAffect, _not_ ModReserveFill)
1901 sub _koha_notify_reserve
{
1902 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1904 my $dbh = C4
::Context
->dbh;
1905 my $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrowernumber);
1907 # Try to get the borrower's email address
1908 my $to_address = C4
::Members
::GetNoticeEmailAddress
($borrowernumber);
1910 my $messagingprefs = C4
::Members
::Messaging
::GetMessagingPreferences
( {
1911 borrowernumber
=> $borrowernumber,
1912 message_name
=> 'Hold_Filled'
1915 my $sth = $dbh->prepare("
1918 WHERE borrowernumber = ?
1919 AND biblionumber = ?
1921 $sth->execute( $borrowernumber, $biblionumber );
1922 my $reserve = $sth->fetchrow_hashref;
1923 my $branch_details = GetBranchDetail
( $reserve->{'branchcode'} );
1925 my $admin_email_address = $branch_details->{'branchemail'} || C4
::Context
->preference('KohaAdminEmailAddress');
1927 my %letter_params = (
1928 module
=> 'reserves',
1929 branchcode
=> $reserve->{branchcode
},
1931 'branches' => $branch_details,
1932 'borrowers' => $borrower,
1933 'biblio' => $biblionumber,
1934 'reserves' => $reserve,
1935 'items', $reserve->{'itemnumber'},
1937 substitute
=> { today
=> C4
::Dates
->new()->output() },
1940 my $notification_sent = 0; #Keeping track if a Hold_filled message is sent. If no message can be sent, then default to a print message.
1941 my $send_notification = sub {
1942 my ( $mtt, $letter_code ) = (@_);
1943 return unless defined $letter_code;
1944 $letter_params{letter_code
} = $letter_code;
1945 $letter_params{message_transport_type
} = $mtt;
1946 my $letter = C4
::Letters
::GetPreparedLetter
( %letter_params );
1948 warn "Could not find a letter called '$letter_params{'letter_code'}' for $mtt in the 'reserves' module";
1952 C4
::Letters
::EnqueueLetter
( {
1954 borrowernumber
=> $borrowernumber,
1955 from_address
=> $admin_email_address,
1956 message_transport_type
=> $mtt,
1960 while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports
} } ) {
1961 if ( ($mtt eq 'email' and not $to_address) or ($mtt eq 'sms' and not $borrower->{smsalertnumber
}) ) {
1962 # email or sms is requested but not exist
1965 &$send_notification($mtt, $letter_code);
1966 $notification_sent++;
1968 #Making sure that a print notification is sent if no other transport types can be utilized.
1969 if (! $notification_sent) {
1970 &$send_notification('print', 'HOLD');
1975 =head2 _ShiftPriorityByDateAndPriority
1977 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1979 This increments the priority of all reserves after the one
1980 with either the lowest date after C<$reservedate>
1981 or the lowest priority after C<$priority>.
1983 It effectively makes room for a new reserve to be inserted with a certain
1984 priority, which is returned.
1986 This is most useful when the reservedate can be set by the user. It allows
1987 the new reserve to be placed before other reserves that have a later
1988 reservedate. Since priority also is set by the form in reserves/request.pl
1989 the sub accounts for that too.
1993 sub _ShiftPriorityByDateAndPriority
{
1994 my ( $biblio, $resdate, $new_priority ) = @_;
1996 my $dbh = C4
::Context
->dbh;
1997 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
1998 my $sth = $dbh->prepare( $query );
1999 $sth->execute( $biblio, $resdate, $new_priority );
2000 my $min_priority = $sth->fetchrow;
2001 # if no such matches are found, $new_priority remains as original value
2002 $new_priority = $min_priority if ( $min_priority );
2004 # Shift the priority up by one; works in conjunction with the next SQL statement
2005 $query = "UPDATE reserves
2006 SET priority = priority+1
2007 WHERE biblionumber = ?
2008 AND borrowernumber = ?
2011 my $sth_update = $dbh->prepare( $query );
2013 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
2014 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
2015 $sth = $dbh->prepare( $query );
2016 $sth->execute( $new_priority, $biblio );
2017 while ( my $row = $sth->fetchrow_hashref ) {
2018 $sth_update->execute( $biblio, $row->{borrowernumber
}, $row->{reservedate
} );
2021 return $new_priority; # so the caller knows what priority they wind up receiving
2026 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
2028 Use when checking out an item to handle reserves
2029 If $cancelreserve boolean is set to true, it will remove existing reserve
2034 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
2036 my ( $restype, $res, $all_reserves ) = CheckReserves
( $itemnumber );
2039 my $biblionumber = $res->{biblionumber
};
2040 my $biblioitemnumber = $res->{biblioitemnumber
};
2042 if ($res->{borrowernumber
} == $borrowernumber) {
2043 ModReserveFill
($res);
2047 # The item is reserved by someone else.
2048 # Find this item in the reserves
2051 foreach (@
$all_reserves) {
2052 $_->{'borrowernumber'} == $borrowernumber or next;
2053 $_->{'biblionumber'} == $biblionumber or next;
2060 # The item is reserved by the current patron
2061 ModReserveFill
($borr_res);
2064 if ( $cancelreserve eq 'revert' ) { ## Revert waiting reserve to priority 1
2065 RevertWaitingStatus
({ itemnumber
=> $itemnumber });
2067 elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
2069 biblionumber
=> $res->{'biblionumber'},
2070 itemnumber
=> $res->{'itemnumber'},
2071 borrowernumber
=> $res->{'borrowernumber'}
2079 MergeHolds($dbh,$to_biblio, $from_biblio);
2081 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
2086 my ( $dbh, $to_biblio, $from_biblio ) = @_;
2087 my $sth = $dbh->prepare(
2088 "SELECT count(*) as reserve_count FROM reserves WHERE biblionumber = ?"
2090 $sth->execute($from_biblio);
2091 if ( my $data = $sth->fetchrow_hashref() ) {
2093 # holds exist on old record, if not we don't need to do anything
2094 $sth = $dbh->prepare(
2095 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
2096 $sth->execute( $to_biblio, $from_biblio );
2099 # don't reorder those already waiting
2101 $sth = $dbh->prepare(
2102 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
2104 my $upd_sth = $dbh->prepare(
2105 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
2106 AND reservedate = ? AND constrainttype = ? AND (itemnumber = ? or itemnumber is NULL) "
2108 $sth->execute( $to_biblio, 'W', 'T' );
2110 while ( my $reserve = $sth->fetchrow_hashref() ) {
2112 $priority, $to_biblio,
2113 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
2114 $reserve->{'constrainttype'}, $reserve->{'itemnumber'}
2121 =head2 RevertWaitingStatus
2123 RevertWaitingStatus({ itemnumber => $itemnumber });
2125 Reverts a 'waiting' hold back to a regular hold with a priority of 1.
2127 Caveat: Any waiting hold fixed with RevertWaitingStatus will be an
2128 item level hold, even if it was only a bibliolevel hold to
2129 begin with. This is because we can no longer know if a hold
2130 was item-level or bib-level after a hold has been set to
2135 sub RevertWaitingStatus
{
2136 my ( $params ) = @_;
2137 my $itemnumber = $params->{'itemnumber'};
2139 return unless ( $itemnumber );
2141 my $dbh = C4
::Context
->dbh;
2143 ## Get the waiting reserve we want to revert
2145 SELECT * FROM reserves
2146 WHERE itemnumber = ?
2147 AND found IS NOT NULL
2149 my $sth = $dbh->prepare( $query );
2150 $sth->execute( $itemnumber );
2151 my $reserve = $sth->fetchrow_hashref();
2153 ## Increment the priority of all other non-waiting
2154 ## reserves for this bib record
2158 priority = priority + 1
2164 $sth = $dbh->prepare( $query );
2165 $sth->execute( $reserve->{'biblionumber'} );
2167 ## Fix up the currently waiting reserve
2177 $sth = $dbh->prepare( $query );
2178 $sth->execute( $reserve->{'reserve_id'} );
2179 _FixPriority
( { biblionumber
=> $reserve->{biblionumber
} } );
2184 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber [, itemnumber => $itemnumber ] });
2186 Returnes the first reserve id that matches the given criteria
2191 my ( $params ) = @_;
2193 return unless ( ( $params->{'biblionumber'} || $params->{'itemnumber'} ) && $params->{'borrowernumber'} );
2195 my $dbh = C4
::Context
->dbh();
2197 my $sql = "SELECT reserve_id FROM reserves WHERE ";
2201 foreach my $key ( keys %$params ) {
2202 if ( defined( $params->{$key} ) ) {
2203 push( @limits, "$key = ?" );
2204 push( @params, $params->{$key} );
2208 $sql .= join( " AND ", @limits );
2210 my $sth = $dbh->prepare( $sql );
2211 $sth->execute( @params );
2212 my $row = $sth->fetchrow_hashref();
2214 return $row->{'reserve_id'};
2219 ReserveSlip($branchcode, $borrowernumber, $biblionumber)
2221 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
2226 my ($branch, $borrowernumber, $biblionumber) = @_;
2228 # return unless ( C4::Context->boolean_preference('printreserveslips') );
2230 my $reserve_id = GetReserveId
({
2231 biblionumber
=> $biblionumber,
2232 borrowernumber
=> $borrowernumber
2234 my $reserve = GetReserveInfo
($reserve_id) or return;
2236 return C4
::Letters
::GetPreparedLetter
(
2237 module
=> 'circulation',
2238 letter_code
=> 'RESERVESLIP',
2239 branchcode
=> $branch,
2241 'reserves' => $reserve,
2242 'branches' => $reserve->{branchcode
},
2243 'borrowers' => $reserve->{borrowernumber
},
2244 'biblio' => $reserve->{biblionumber
},
2245 'items' => $reserve->{itemnumber
},
2250 =head2 GetReservesControlBranch
2252 my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
2254 Return the branchcode to be used to determine which reserves
2255 policy applies to a transaction.
2257 C<$item> is a hashref for an item. Only 'homebranch' is used.
2259 C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
2263 sub GetReservesControlBranch
{
2264 my ( $item, $borrower ) = @_;
2266 my $reserves_control = C4
::Context
->preference('ReservesControlBranch');
2269 ( $reserves_control eq 'ItemHomeLibrary' ) ?
$item->{'homebranch'}
2270 : ( $reserves_control eq 'PatronLibrary' ) ?
$borrower->{'branchcode'}
2276 =head2 CalculatePriority
2278 my $p = CalculatePriority($biblionumber, $resdate);
2280 Calculate priority for a new reserve on biblionumber, placing it at
2281 the end of the line of all holds whose start date falls before
2282 the current system time and that are neither on the hold shelf
2285 The reserve date parameter is optional; if it is supplied, the
2286 priority is based on the set of holds whose start date falls before
2287 the parameter value.
2289 After calculation of this priority, it is recommended to call
2290 _ShiftPriorityByDateAndPriority. Note that this is currently done in
2295 sub CalculatePriority
{
2296 my ( $biblionumber, $resdate ) = @_;
2299 SELECT COUNT(*) FROM reserves
2300 WHERE biblionumber = ?
2302 AND (found IS NULL OR found = '')
2304 #skip found==W or found==T (waiting or transit holds)
2306 $sql.= ' AND ( reservedate <= ? )';
2309 $sql.= ' AND ( reservedate < NOW() )';
2311 my $dbh = C4
::Context
->dbh();
2312 my @row = $dbh->selectrow_array(
2315 $resdate ?
($biblionumber, $resdate) : ($biblionumber)
2318 return @row ?
$row[0]+1 : 1;
2323 Koha Development Team <http://koha-community.org/>