2 # NOTE: This file uses standard 8-character tabs
6 # Copyright 2000-2002 Katipo Communications
7 # 2006 SAN Ouest Provence
8 # 2007 BibLibre Paul POULAIN
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
34 # for _koha_notify_reserve
35 use C4::Members::Messaging;
36 use C4::Members qw( GetMember );
38 use C4::Branch qw( GetBranchDetail );
39 use List::MoreUtils qw( firstidx );
41 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
43 my $library_name = C4::Context->preference("LibraryName");
47 C4::Reserves - Koha functions for dealing with reservation.
55 this modules provides somes functions to deal with reservations.
57 Reserves are stored in reserves table.
58 The following columns contains important values :
59 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
60 =0 : then the reserve is being dealed
61 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
62 W(aiting) : the reserve has an itemnumber affected, and is on the way
63 F(inished) : the reserve has been completed, and is done
64 - itemnumber : empty : the reserve is still unaffected to an item
65 filled: the reserve is attached to an item
66 The complete workflow is :
67 ==== 1st use case ====
68 patron request a document, 1st available : P >0, F=NULL, I=NULL
69 a library having it run "transfertodo", and clic on the list
70 if there is no transfer to do, the reserve waiting
71 patron can pick it up P =0, F=W, I=filled
72 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
73 The pickup library recieve the book, it check in P =0, F=W, I=filled
74 The patron borrow the book P =0, F=F, I=filled
76 ==== 2nd use case ====
77 patron requests a document, a given item,
78 If pickup is holding branch P =0, F=W, I=filled
79 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
80 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
81 The patron borrow the book P =0, F=F, I=filled
90 # set the version for version checking
97 &GetReservesFromItemnumber
98 &GetReservesFromBiblionumber
99 &GetReservesFromBorrowernumber
100 &GetReservesForBranch
113 &ModReserveMinusPriority
118 &IsAvailableForItemLevelRequest
124 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
130 $branch, $borrowernumber, $biblionumber,
131 $constraint, $bibitems, $priority, $notes,
132 $title, $checkitem, $found
135 GetReserveFee($borrowernumber, $biblionumber, $constraint,
137 my $dbh = C4::Context->dbh;
138 my $const = lc substr( $constraint, 0, 1 );
139 my @datearr = localtime(time);
141 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
144 # If the reserv had the waiting status, we had the value of the resdate
145 if ( $found eq 'W' ) {
146 $waitingdate = $resdate;
150 # updates take place here
152 my $nextacctno = &getnextacctno( $borrowernumber );
154 INSERT INTO accountlines
155 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
157 (?,?,now(),?,?,'Res',?)
159 my $usth = $dbh->prepare($query);
160 $usth->execute( $borrowernumber, $nextacctno, $fee,
161 "Reserve Charge - $title", $fee );
167 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
168 priority,reservenotes,itemnumber,found,waitingdate)
173 my $sth = $dbh->prepare($query);
175 $borrowernumber, $biblionumber, $resdate, $branch,
176 $const, $priority, $notes, $checkitem,
181 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
183 INSERT INTO reserveconstraints
184 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
188 $sth = $dbh->prepare($query); # keep prepare outside the loop!
189 foreach (@$bibitems) {
190 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
192 return; # FIXME: why not have a useful return value?
195 =item GetReservesFromBiblionumber
197 @borrowerreserv=&GetReserves($biblionumber,$itemnumber,$borrowernumber);
199 this function get the list of reservation for an C<$biblionumber>, C<$itemnumber> or C<$borrowernumber>
201 Only 1 argument has to be passed.
205 sub GetReservesFromBiblionumber {
206 my ( $biblionumber, $itemnumber, $borrowernumber ) = @_;
207 my $dbh = C4::Context->dbh;
209 # Find the desired items in the reserves
212 timestamp AS rtimestamp,
222 WHERE biblionumber = ?
224 my $sth = $dbh->prepare($query);
225 $sth->execute($biblionumber);
228 while ( my $data = $sth->fetchrow_hashref ) {
230 # FIXME - What is this doing? How do constraints work?
231 if ($data->{constrainttype} eq 'o') {
233 SELECT biblioitemnumber
234 FROM reserveconstraints
235 WHERE biblionumber = ?
236 AND borrowernumber = ?
239 my $csth = $dbh->prepare($query);
240 $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
241 $data->{reservedate}, );
244 while ( my $bibitemnos = $csth->fetchrow_array ) {
245 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
247 my $count = scalar @bibitemno;
249 # if we have two or more different specific itemtypes
250 # reserved by same person on same day
253 $bdata = GetBiblioItemData( $bibitemno[$i] );
257 # Look up the book we just found.
258 $bdata = GetBiblioItemData( $bibitemno[0] );
260 # Add the results of this latest search to the current
262 # FIXME - An 'each' would probably be more efficient.
263 foreach my $key ( keys %$bdata ) {
264 $data->{$key} = $bdata->{$key};
267 push @results, $data;
269 return ( $#results + 1, \@results );
272 =item GetReservesFromItemnumber
274 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
276 TODO :: Description here
280 sub GetReservesFromItemnumber {
281 my ( $itemnumber ) = @_;
282 my $dbh = C4::Context->dbh;
284 SELECT reservedate,borrowernumber,branchcode
288 my $sth_res = $dbh->prepare($query);
289 $sth_res->execute($itemnumber);
290 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
291 return ( $reservedate, $borrowernumber, $branchcode );
294 =item GetReservesFromBorrowernumber
296 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
302 sub GetReservesFromBorrowernumber {
303 my ( $borrowernumber, $status ) = @_;
304 my $dbh = C4::Context->dbh;
307 $sth = $dbh->prepare("
310 WHERE borrowernumber=?
314 $sth->execute($borrowernumber,$status);
316 $sth = $dbh->prepare("
319 WHERE borrowernumber=?
322 $sth->execute($borrowernumber);
324 my $data = $sth->fetchall_arrayref({});
327 #-------------------------------------------------------------------------------------
329 =item GetReserveCount
331 $number = &GetReserveCount($borrowernumber);
333 this function returns the number of reservation for a borrower given on input arg.
337 sub GetReserveCount {
338 my ($borrowernumber) = @_;
340 my $dbh = C4::Context->dbh;
343 SELECT COUNT(*) AS counter
345 WHERE borrowernumber = ?
347 my $sth = $dbh->prepare($query);
348 $sth->execute($borrowernumber);
349 my $row = $sth->fetchrow_hashref;
350 return $row->{counter};
353 =item GetOtherReserves
355 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
357 Check queued list of this document and check if this document must be transfered
361 sub GetOtherReserves {
362 my ($itemnumber) = @_;
365 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
366 if ($checkreserves and $restype ne "Waiting" ) {
367 my $iteminfo = GetItem($itemnumber);
368 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
369 $messages->{'transfert'} = $checkreserves->{'branchcode'};
370 #minus priorities of others reservs
371 ModReserveMinusPriority(
373 $checkreserves->{'borrowernumber'},
374 $iteminfo->{'biblionumber'}
377 #launch the subroutine dotransfer
378 C4::Circulation::ModItemTransfer(
380 $iteminfo->{'holdingbranch'},
381 $checkreserves->{'branchcode'}
386 #step 2b : case of a reservation on the same branch, set the waiting status
388 $messages->{'waiting'} = 1;
389 ModReserveMinusPriority(
391 $checkreserves->{'borrowernumber'},
392 $iteminfo->{'biblionumber'}
394 ModReserveStatus($itemnumber,'W');
397 $nextreservinfo = $checkreserves->{'borrowernumber'};
400 return ( $messages, $nextreservinfo );
405 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
407 Calculate the fee for a reserve
412 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
415 my $dbh = C4::Context->dbh;
416 my $const = lc substr( $constraint, 0, 1 );
418 SELECT * FROM borrowers
419 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
420 WHERE borrowernumber = ?
422 my $sth = $dbh->prepare($query);
423 $sth->execute($borrowernumber);
424 my $data = $sth->fetchrow_hashref;
426 my $fee = $data->{'reservefee'};
427 my $cntitems = @- > $bibitems;
431 # check for items on issue
432 # first find biblioitem records
434 my $sth1 = $dbh->prepare(
435 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
436 WHERE (biblio.biblionumber = ?)"
438 $sth1->execute($biblionumber);
439 while ( my $data1 = $sth1->fetchrow_hashref ) {
440 if ( $const eq "a" ) {
441 push @biblioitems, $data1;
446 while ( $x < $cntitems ) {
447 if ( @$bibitems->{'biblioitemnumber'} ==
448 $data->{'biblioitemnumber'} )
454 if ( $const eq 'o' ) {
456 push @biblioitems, $data1;
461 push @biblioitems, $data1;
467 my $cntitemsfound = @biblioitems;
471 while ( $x < $cntitemsfound ) {
472 my $bitdata = $biblioitems[$x];
473 my $sth2 = $dbh->prepare(
475 WHERE biblioitemnumber = ?"
477 $sth2->execute( $bitdata->{'biblioitemnumber'} );
478 while ( my $itdata = $sth2->fetchrow_hashref ) {
479 my $sth3 = $dbh->prepare(
480 "SELECT * FROM issues
481 WHERE itemnumber = ?"
483 $sth3->execute( $itdata->{'itemnumber'} );
484 if ( my $isdata = $sth3->fetchrow_hashref ) {
492 if ( $allissued == 0 ) {
494 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
495 $rsth->execute($biblionumber);
496 if ( my $rdata = $rsth->fetchrow_hashref ) {
506 =item GetReservesToBranch
508 @transreserv = GetReservesToBranch( $frombranch );
510 Get reserve list for a given branch
514 sub GetReservesToBranch {
515 my ( $frombranch ) = @_;
516 my $dbh = C4::Context->dbh;
517 my $sth = $dbh->prepare(
518 "SELECT borrowernumber,reservedate,itemnumber,timestamp
523 $sth->execute( $frombranch );
526 while ( my $data = $sth->fetchrow_hashref ) {
527 $transreserv[$i] = $data;
530 return (@transreserv);
533 =item GetReservesForBranch
535 @transreserv = GetReservesForBranch($frombranch);
539 sub GetReservesForBranch {
540 my ($frombranch) = @_;
541 my $dbh = C4::Context->dbh;
542 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
547 $query .= " AND branchcode=? ";
549 $query .= "ORDER BY waitingdate" ;
550 my $sth = $dbh->prepare($query);
552 $sth->execute($frombranch);
559 while ( my $data = $sth->fetchrow_hashref ) {
560 $transreserv[$i] = $data;
563 return (@transreserv);
568 ($status, $reserve) = &CheckReserves($itemnumber);
570 Find a book in the reserves.
572 C<$itemnumber> is the book's item number.
574 As I understand it, C<&CheckReserves> looks for the given item in the
575 reserves. If it is found, that's a match, and C<$status> is set to
578 Otherwise, it finds the most important item in the reserves with the
579 same biblio number as this book (I'm not clear on this) and returns it
580 with C<$status> set to C<Reserved>.
582 C<&CheckReserves> returns a two-element list:
584 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
586 C<$reserve> is the reserve item that matched. It is a
587 reference-to-hash whose keys are mostly the fields of the reserves
588 table in the Koha database.
593 my ( $item, $barcode ) = @_;
594 my $dbh = C4::Context->dbh;
597 my $qitem = $dbh->quote($item);
598 # Look up the item by itemnumber
600 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
602 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
603 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
604 WHERE itemnumber=$qitem
606 $sth = $dbh->prepare($query);
609 my $qbc = $dbh->quote($barcode);
610 # Look up the item by barcode
612 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
614 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
615 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
616 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
617 AND biblioitems.itemtype = itemtypes.itemtype
620 $sth = $dbh->prepare($query);
622 # FIXME - This function uses $item later on. Ought to set it here.
625 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item ) = $sth->fetchrow_array;
627 # if item is not for loan it cannot be reserved either.....
628 # execption to notforloan is where items.notforloan < 0 : This indicates the item is holdable.
629 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
631 # get the reserves...
632 # Find this item in the reserves
633 my @reserves = _Findgroupreserve( $bibitem, $biblio, $item );
634 my $count = scalar @reserves;
636 # $priority and $highest are used to find the most important item
637 # in the list returned by &_Findgroupreserve. (The lower $priority,
638 # the more important the item.)
639 # $highest is the most important item we've seen so far.
640 my $priority = 10000000;
643 foreach my $res (@reserves) {
644 # FIXME - $item might be undefined or empty: the caller
645 # might be searching by barcode.
646 if ( $res->{'itemnumber'} == $item && $res->{'priority'} == 0) {
648 return ( "Waiting", $res );
651 # See if this item is more important than what we've got
653 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
655 $priority = $res->{'priority'};
662 # If we get this far, then no exact match was found. Print the
663 # most important item on the list. I think this tells us who's
664 # next in line to get this book.
665 if ($highest) { # FIXME - $highest might be undefined
666 $highest->{'itemnumber'} = $item;
667 return ( "Reserved", $highest );
676 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
680 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
681 cancel, but not both: if both are given, C<&CancelReserve> does
684 C<$borrowernumber> is the borrower number of the patron on whose
685 behalf the book was reserved.
687 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
688 priorities of the other people who are waiting on the book.
693 my ( $biblio, $item, $borr ) = @_;
694 my $dbh = C4::Context->dbh;
695 if ( $item and $borr ) {
696 # removing a waiting reserve record....
697 # update the database...
700 SET cancellationdate = now(),
704 AND borrowernumber = ?
706 my $sth = $dbh->prepare($query);
707 $sth->execute( $item, $borr );
710 INSERT INTO old_reserves
711 SELECT * FROM reserves
713 AND borrowernumber = ?
715 $sth = $dbh->prepare($query);
716 $sth->execute( $item, $borr );
720 AND borrowernumber = ?
722 $sth = $dbh->prepare($query);
723 $sth->execute( $item, $borr );
726 # removing a reserve record....
727 # get the prioritiy on this record....
730 SELECT priority FROM reserves
731 WHERE biblionumber = ?
732 AND borrowernumber = ?
733 AND cancellationdate IS NULL
734 AND itemnumber IS NULL
736 my $sth = $dbh->prepare($query);
737 $sth->execute( $biblio, $borr );
738 ($priority) = $sth->fetchrow_array;
742 SET cancellationdate = now(),
745 WHERE biblionumber = ?
746 AND borrowernumber = ?
749 # update the database, removing the record...
750 $sth = $dbh->prepare($query);
751 $sth->execute( $biblio, $borr );
755 INSERT INTO old_reserves
756 SELECT * FROM reserves
757 WHERE biblionumber = ?
758 AND borrowernumber = ?
760 $sth = $dbh->prepare($query);
761 $sth->execute( $biblio, $borr );
765 WHERE biblionumber = ?
766 AND borrowernumber = ?
768 $sth = $dbh->prepare($query);
769 $sth->execute( $biblio, $borr );
771 # now fix the priority on the others....
772 _FixPriority( $priority, $biblio );
780 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
784 Change a hold request's priority or cancel it.
786 C<$rank> specifies the effect of the change. If C<$rank>
787 is 'W' or 'n', nothing happens. This corresponds to leaving a
788 request alone when changing its priority in the holds queue
791 If C<$rank> is 'del', the hold request is cancelled.
793 If C<$rank> is an integer greater than zero, the priority of
794 the request is set to that value. Since priority != 0 means
795 that the item is not waiting on the hold shelf, setting the
796 priority to a non-zero value also sets the request's found
797 status and waiting date to NULL.
799 The optional C<$itemnumber> parameter is used only when
800 C<$rank> is a non-zero integer; if supplied, the itemnumber
801 of the hold request is set accordingly; if omitted, the itemnumber
804 FIXME: Note that the forgoing can have the effect of causing
805 item-level hold requests to turn into title-level requests. This
806 will be fixed once reserves has separate columns for requested
807 itemnumber and supplying itemnumber.
812 #subroutine to update a reserve
813 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
814 return if $rank eq "W";
815 return if $rank eq "n";
816 my $dbh = C4::Context->dbh;
817 if ( $rank eq "del" ) {
820 SET cancellationdate=now()
821 WHERE biblionumber = ?
822 AND borrowernumber = ?
824 my $sth = $dbh->prepare($query);
825 $sth->execute( $biblio, $borrower );
828 INSERT INTO old_reserves
831 WHERE biblionumber = ?
832 AND borrowernumber = ?
834 $sth = $dbh->prepare($query);
835 $sth->execute( $biblio, $borrower );
838 WHERE biblionumber = ?
839 AND borrowernumber = ?
841 $sth = $dbh->prepare($query);
842 $sth->execute( $biblio, $borrower );
845 elsif ($rank =~ /^\d+/ and $rank > 0) {
847 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
848 WHERE biblionumber = ?
849 AND borrowernumber = ?
851 my $sth = $dbh->prepare($query);
852 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
854 _FixPriority( $biblio, $borrower, $rank);
860 &ModReserveFill($reserve);
862 Fill a reserve. If I understand this correctly, this means that the
863 reserved book has been found and given to the patron who reserved it.
865 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
866 whose keys are fields from the reserves table in the Koha database.
872 my $dbh = C4::Context->dbh;
873 # fill in a reserve record....
874 my $biblionumber = $res->{'biblionumber'};
875 my $borrowernumber = $res->{'borrowernumber'};
876 my $resdate = $res->{'reservedate'};
878 # get the priority on this record....
880 my $query = "SELECT priority
882 WHERE biblionumber = ?
883 AND borrowernumber = ?
884 AND reservedate = ?";
885 my $sth = $dbh->prepare($query);
886 $sth->execute( $biblionumber, $borrowernumber, $resdate );
887 ($priority) = $sth->fetchrow_array;
890 # update the database...
891 $query = "UPDATE reserves
894 WHERE biblionumber = ?
896 AND borrowernumber = ?
898 $sth = $dbh->prepare($query);
899 $sth->execute( $biblionumber, $resdate, $borrowernumber );
902 # move to old_reserves
903 $query = "INSERT INTO old_reserves
904 SELECT * FROM reserves
905 WHERE biblionumber = ?
907 AND borrowernumber = ?
909 $sth = $dbh->prepare($query);
910 $sth->execute( $biblionumber, $resdate, $borrowernumber );
911 $query = "DELETE FROM reserves
912 WHERE biblionumber = ?
914 AND borrowernumber = ?
916 $sth = $dbh->prepare($query);
917 $sth->execute( $biblionumber, $resdate, $borrowernumber );
919 # now fix the priority on the others (if the priority wasn't
920 # already sorted!)....
921 unless ( $priority == 0 ) {
922 _FixPriority( $priority, $biblionumber );
926 =item ModReserveStatus
928 &ModReserveStatus($itemnumber, $newstatus);
930 Update the reserve status for the active (priority=0) reserve.
932 $itemnumber is the itemnumber the reserve is on
934 $newstatus is the new status.
938 sub ModReserveStatus {
940 #first : check if we have a reservation for this item .
941 my ($itemnumber, $newstatus) = @_;
942 my $dbh = C4::Context->dbh;
943 my $query = " UPDATE reserves
944 SET found=?,waitingdate = now()
949 my $sth_set = $dbh->prepare($query);
950 $sth_set->execute( $newstatus, $itemnumber );
953 =item ModReserveAffect
955 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
957 This function affect an item and a status for a given reserve
958 The itemnumber parameter is used to find the biblionumber.
959 with the biblionumber & the borrowernumber, we can affect the itemnumber
960 to the correct reserve.
962 if $transferToDo is not set, then the status is set to "Waiting" as well.
963 otherwise, a transfer is on the way, and the end of the transfer will
964 take care of the waiting status
967 sub ModReserveAffect {
968 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
969 my $dbh = C4::Context->dbh;
971 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
972 # attached to $itemnumber
973 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
974 $sth->execute($itemnumber);
975 my ($biblionumber) = $sth->fetchrow;
976 # If we affect a reserve that has to be transfered, don't set to Waiting
983 WHERE borrowernumber = ?
988 # affect the reserve to Waiting as well.
995 WHERE borrowernumber = ?
999 $sth = $dbh->prepare($query);
1000 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1001 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo );
1006 =item ModReserveCancelAll
1008 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1010 function to cancel reserv,check other reserves, and transfer document if it's necessary
1014 sub ModReserveCancelAll {
1017 my ( $itemnumber, $borrowernumber ) = @_;
1019 #step 1 : cancel the reservation
1020 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1022 #step 2 launch the subroutine of the others reserves
1023 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1025 return ( $messages, $nextreservinfo );
1028 =item ModReserveMinusPriority
1030 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1032 Reduce the values of queuded list
1036 sub ModReserveMinusPriority {
1037 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1039 #first step update the value of the first person on reserv
1040 my $dbh = C4::Context->dbh;
1043 SET priority = 0 , itemnumber = ?
1044 WHERE borrowernumber=?
1047 my $sth_upd = $dbh->prepare($query);
1048 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1049 # second step update all others reservs
1050 _FixPriority($biblionumber, $borrowernumber, '0');
1053 =item GetReserveInfo
1055 &GetReserveInfo($borrowernumber,$biblionumber);
1057 Get item and borrower details for a current hold.
1058 Current implementation this query should have a single result.
1061 sub GetReserveInfo {
1062 my ( $borrowernumber, $biblionumber ) = @_;
1063 my $dbh = C4::Context->dbh;
1064 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1065 reserves.biblionumber, reserves.branchcode,
1066 notificationdate, reminderdate, priority, found,
1067 firstname, surname, phone,
1068 email, address, address2,
1069 cardnumber, city, zipcode,
1070 biblio.title, biblio.author,
1071 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1073 FROM reserves left join items
1074 ON items.itemnumber=reserves.itemnumber ,
1077 reserves.borrowernumber=? &&
1078 reserves.biblionumber=? &&
1079 reserves.borrowernumber=borrowers.borrowernumber &&
1080 reserves.biblionumber=biblio.biblionumber ";
1081 my $sth = $dbh->prepare($strsth);
1082 $sth->execute($borrowernumber,$biblionumber);
1084 my $data = $sth->fetchrow_hashref;
1089 =item IsAvailableForItemLevelRequest
1093 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1097 Checks whether a given item record is available for an
1098 item-level hold request. An item is available if
1100 * it is not lost AND
1101 * it is not damaged AND
1102 * it is not withdrawn AND
1103 * does not have a not for loan value > 0
1105 Whether or not the item is currently on loan is
1106 also checked - if the AllowOnShelfHolds system preference
1107 is ON, an item can be requested even if it is currently
1108 on loan to somebody else. If the system preference
1109 is OFF, an item that is currently checked out cannot
1110 be the target of an item-level hold request.
1112 Note that IsAvailableForItemLevelRequest() does not
1113 check if the staff operator is authorized to place
1114 a request on the item - in particular,
1115 this routine does not check IndependantBranches
1116 and canreservefromotherbranches.
1120 sub IsAvailableForItemLevelRequest {
1121 my $itemnumber = shift;
1123 my $item = GetItem($itemnumber);
1125 # must check the notforloan setting of the itemtype
1126 # FIXME - a lot of places in the code do this
1127 # or something similar - need to be
1129 my $dbh = C4::Context->dbh;
1130 my $notforloan_query;
1131 if (C4::Context->preference('item-level_itypes')) {
1132 $notforloan_query = "SELECT itemtypes.notforloan
1134 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1135 WHERE itemnumber = ?";
1137 $notforloan_query = "SELECT itemtypes.notforloan
1139 JOIN biblioitems USING (biblioitemnumber)
1140 JOIN itemtypes USING (itemtype)
1141 WHERE itemnumber = ?";
1143 my $sth = $dbh->prepare($notforloan_query);
1144 $sth->execute($itemnumber);
1145 my $notforloan_per_itemtype = 0;
1146 if (my ($notforloan) = $sth->fetchrow_array) {
1147 $notforloan_per_itemtype = 1 if $notforloan;
1150 my $available_per_item = 1;
1151 $available_per_item = 0 if $item->{itemlost} or
1152 ( $item->{notforloan} > 0 ) or
1153 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1154 $item->{wthdrawn} or
1155 $notforloan_per_itemtype;
1158 if (C4::Context->preference('AllowOnShelfHolds')) {
1159 return $available_per_item;
1161 return ($available_per_item and $item->{onloan});
1167 &_FixPriority($biblio,$borrowernumber,$rank);
1169 Only used internally (so don't export it)
1170 Changed how this functions works #
1171 Now just gets an array of reserves in the rank order and updates them with
1172 the array index (+1 as array starts from 0)
1173 and if $rank is supplied will splice item from the array and splice it back in again
1174 in new priority rank
1179 my ( $biblio, $borrowernumber, $rank ) = @_;
1180 my $dbh = C4::Context->dbh;
1181 if ( $rank eq "del" ) {
1182 CancelReserve( $biblio, undef, $borrowernumber );
1184 if ( $rank eq "W" || $rank eq "0" ) {
1186 # make sure priority for waiting items is 0
1190 WHERE biblionumber = ?
1191 AND borrowernumber = ?
1194 my $sth = $dbh->prepare($query);
1195 $sth->execute( $biblio, $borrowernumber );
1201 # FIXME adding a new security in returned elements for changing priority,
1202 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1203 # This is wrong a waiting reserve has W set
1204 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1206 SELECT borrowernumber, reservedate, constrainttype
1208 WHERE biblionumber = ?
1209 AND ((found <> 'W') or found is NULL)
1210 ORDER BY priority ASC
1212 my $sth = $dbh->prepare($query);
1213 $sth->execute($biblio);
1214 while ( my $line = $sth->fetchrow_hashref ) {
1215 push( @reservedates, $line );
1216 push( @priority, $line );
1219 # To find the matching index
1221 my $key = -1; # to allow for 0 to be a valid result
1222 for ( $i = 0 ; $i < @priority ; $i++ ) {
1223 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1224 $key = $i; # save the index
1229 # if index exists in array then move it to new position
1230 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1231 my $new_rank = $rank -
1232 1; # $new_rank is what you want the new index to be in the array
1233 my $moving_item = splice( @priority, $key, 1 );
1234 splice( @priority, $new_rank, 0, $moving_item );
1237 # now fix the priority on those that are left....
1241 WHERE biblionumber = ?
1242 AND borrowernumber = ?
1246 $sth = $dbh->prepare($query);
1247 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1250 $priority[$j]->{'borrowernumber'},
1251 $priority[$j]->{'reservedate'}
1257 =item _Findgroupreserve
1259 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1262 I don't know what this does, because I don't understand how reserve
1263 constraints work. I think the idea is that you reserve a particular
1264 biblio, and the constraint allows you to restrict it to a given
1265 biblioitem (e.g., if you want to borrow the audio book edition of "The
1266 Prophet", rather than the first available publication).
1268 C<&_Findgroupreserve> returns :
1269 C<@results> is an array of references-to-hash whose keys are mostly
1270 fields from the reserves table of the Koha database, plus
1271 C<biblioitemnumber>.
1275 sub _Findgroupreserve {
1276 my ( $bibitem, $biblio, $itemnumber ) = @_;
1277 my $dbh = C4::Context->dbh;
1279 # check for exact targetted match
1280 my $item_level_target_query = qq/
1281 SELECT reserves.biblionumber AS biblionumber,
1282 reserves.borrowernumber AS borrowernumber,
1283 reserves.reservedate AS reservedate,
1284 reserves.branchcode AS branchcode,
1285 reserves.cancellationdate AS cancellationdate,
1286 reserves.found AS found,
1287 reserves.reservenotes AS reservenotes,
1288 reserves.priority AS priority,
1289 reserves.timestamp AS timestamp,
1290 biblioitems.biblioitemnumber AS biblioitemnumber,
1291 reserves.itemnumber AS itemnumber
1293 JOIN biblioitems USING (biblionumber)
1294 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1297 AND item_level_request = 1
1300 my $sth = $dbh->prepare($item_level_target_query);
1301 $sth->execute($itemnumber);
1303 if ( my $data = $sth->fetchrow_hashref ) {
1304 push( @results, $data );
1306 return @results if @results;
1308 # check for title-level targetted match
1309 my $title_level_target_query = qq/
1310 SELECT reserves.biblionumber AS biblionumber,
1311 reserves.borrowernumber AS borrowernumber,
1312 reserves.reservedate AS reservedate,
1313 reserves.branchcode AS branchcode,
1314 reserves.cancellationdate AS cancellationdate,
1315 reserves.found AS found,
1316 reserves.reservenotes AS reservenotes,
1317 reserves.priority AS priority,
1318 reserves.timestamp AS timestamp,
1319 biblioitems.biblioitemnumber AS biblioitemnumber,
1320 reserves.itemnumber AS itemnumber
1322 JOIN biblioitems USING (biblionumber)
1323 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1326 AND item_level_request = 0
1327 AND hold_fill_targets.itemnumber = ?
1329 $sth = $dbh->prepare($title_level_target_query);
1330 $sth->execute($itemnumber);
1332 if ( my $data = $sth->fetchrow_hashref ) {
1333 push( @results, $data );
1335 return @results if @results;
1338 SELECT reserves.biblionumber AS biblionumber,
1339 reserves.borrowernumber AS borrowernumber,
1340 reserves.reservedate AS reservedate,
1341 reserves.branchcode AS branchcode,
1342 reserves.cancellationdate AS cancellationdate,
1343 reserves.found AS found,
1344 reserves.reservenotes AS reservenotes,
1345 reserves.priority AS priority,
1346 reserves.timestamp AS timestamp,
1347 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1348 reserves.itemnumber AS itemnumber
1350 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1351 WHERE reserves.biblionumber = ?
1352 AND ( ( reserveconstraints.biblioitemnumber = ?
1353 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1354 AND reserves.reservedate =reserveconstraints.reservedate )
1355 OR reserves.constrainttype='a' )
1356 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1358 $sth = $dbh->prepare($query);
1359 $sth->execute( $biblio, $bibitem, $itemnumber );
1361 while ( my $data = $sth->fetchrow_hashref ) {
1362 push( @results, $data );
1367 =item _koha_notify_reserve
1371 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1375 Sends a notification to the patron that their hold has been filled (through
1376 ModReserveAffect, _not_ ModReserveFill)
1380 sub _koha_notify_reserve {
1381 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1383 my $dbh = C4::Context->dbh;
1384 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1386 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1388 my $sth = $dbh->prepare("
1391 WHERE borrowernumber = ?
1392 AND biblionumber = ?
1394 $sth->execute( $borrowernumber, $biblionumber );
1395 my $reserve = $sth->fetchrow_hashref;
1396 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1398 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1400 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1402 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1403 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1404 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1405 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1407 if ( $reserve->{'itemnumber'} ) {
1408 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1410 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1412 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1413 # aka, 'email' in ->{'transports'}
1414 C4::Letters::EnqueueLetter(
1415 { letter => $letter,
1416 borrowernumber => $borrowernumber,
1417 message_transport_type => 'email',
1418 from_address => $admin_email_address,
1423 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1424 C4::Letters::EnqueueLetter(
1425 { letter => $letter,
1426 borrowernumber => $borrowernumber,
1427 message_transport_type => 'sms',
1437 Koha Developement team <info@koha.org>