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) {
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::Items::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;
977 # get request - need to find out if item is already
978 # waiting in order to not send duplicate hold filled notifications
979 my $request = GetReserveInfo($borrowernumber, $biblionumber);
980 my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
982 # If we affect a reserve that has to be transfered, don't set to Waiting
989 WHERE borrowernumber = ?
994 # affect the reserve to Waiting as well.
1001 WHERE borrowernumber = ?
1002 AND biblionumber = ?
1005 $sth = $dbh->prepare($query);
1006 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1007 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1012 =item ModReserveCancelAll
1014 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1016 function to cancel reserv,check other reserves, and transfer document if it's necessary
1020 sub ModReserveCancelAll {
1023 my ( $itemnumber, $borrowernumber ) = @_;
1025 #step 1 : cancel the reservation
1026 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1028 #step 2 launch the subroutine of the others reserves
1029 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1031 return ( $messages, $nextreservinfo );
1034 =item ModReserveMinusPriority
1036 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1038 Reduce the values of queuded list
1042 sub ModReserveMinusPriority {
1043 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1045 #first step update the value of the first person on reserv
1046 my $dbh = C4::Context->dbh;
1049 SET priority = 0 , itemnumber = ?
1050 WHERE borrowernumber=?
1053 my $sth_upd = $dbh->prepare($query);
1054 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1055 # second step update all others reservs
1056 _FixPriority($biblionumber, $borrowernumber, '0');
1059 =item GetReserveInfo
1061 &GetReserveInfo($borrowernumber,$biblionumber);
1063 Get item and borrower details for a current hold.
1064 Current implementation this query should have a single result.
1067 sub GetReserveInfo {
1068 my ( $borrowernumber, $biblionumber ) = @_;
1069 my $dbh = C4::Context->dbh;
1070 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1071 reserves.biblionumber, reserves.branchcode,
1072 notificationdate, reminderdate, priority, found,
1073 firstname, surname, phone,
1074 email, address, address2,
1075 cardnumber, city, zipcode,
1076 biblio.title, biblio.author,
1077 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1079 FROM reserves left join items
1080 ON items.itemnumber=reserves.itemnumber ,
1083 reserves.borrowernumber=? &&
1084 reserves.biblionumber=? &&
1085 reserves.borrowernumber=borrowers.borrowernumber &&
1086 reserves.biblionumber=biblio.biblionumber ";
1087 my $sth = $dbh->prepare($strsth);
1088 $sth->execute($borrowernumber,$biblionumber);
1090 my $data = $sth->fetchrow_hashref;
1095 =item IsAvailableForItemLevelRequest
1099 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1103 Checks whether a given item record is available for an
1104 item-level hold request. An item is available if
1106 * it is not lost AND
1107 * it is not damaged AND
1108 * it is not withdrawn AND
1109 * does not have a not for loan value > 0
1111 Whether or not the item is currently on loan is
1112 also checked - if the AllowOnShelfHolds system preference
1113 is ON, an item can be requested even if it is currently
1114 on loan to somebody else. If the system preference
1115 is OFF, an item that is currently checked out cannot
1116 be the target of an item-level hold request.
1118 Note that IsAvailableForItemLevelRequest() does not
1119 check if the staff operator is authorized to place
1120 a request on the item - in particular,
1121 this routine does not check IndependantBranches
1122 and canreservefromotherbranches.
1126 sub IsAvailableForItemLevelRequest {
1127 my $itemnumber = shift;
1129 my $item = GetItem($itemnumber);
1131 # must check the notforloan setting of the itemtype
1132 # FIXME - a lot of places in the code do this
1133 # or something similar - need to be
1135 my $dbh = C4::Context->dbh;
1136 my $notforloan_query;
1137 if (C4::Context->preference('item-level_itypes')) {
1138 $notforloan_query = "SELECT itemtypes.notforloan
1140 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1141 WHERE itemnumber = ?";
1143 $notforloan_query = "SELECT itemtypes.notforloan
1145 JOIN biblioitems USING (biblioitemnumber)
1146 JOIN itemtypes USING (itemtype)
1147 WHERE itemnumber = ?";
1149 my $sth = $dbh->prepare($notforloan_query);
1150 $sth->execute($itemnumber);
1151 my $notforloan_per_itemtype = 0;
1152 if (my ($notforloan) = $sth->fetchrow_array) {
1153 $notforloan_per_itemtype = 1 if $notforloan;
1156 my $available_per_item = 1;
1157 $available_per_item = 0 if $item->{itemlost} or
1158 ( $item->{notforloan} > 0 ) or
1159 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1160 $item->{wthdrawn} or
1161 $notforloan_per_itemtype;
1164 if (C4::Context->preference('AllowOnShelfHolds')) {
1165 return $available_per_item;
1167 return ($available_per_item and $item->{onloan});
1173 &_FixPriority($biblio,$borrowernumber,$rank);
1175 Only used internally (so don't export it)
1176 Changed how this functions works #
1177 Now just gets an array of reserves in the rank order and updates them with
1178 the array index (+1 as array starts from 0)
1179 and if $rank is supplied will splice item from the array and splice it back in again
1180 in new priority rank
1185 my ( $biblio, $borrowernumber, $rank ) = @_;
1186 my $dbh = C4::Context->dbh;
1187 if ( $rank eq "del" ) {
1188 CancelReserve( $biblio, undef, $borrowernumber );
1190 if ( $rank eq "W" || $rank eq "0" ) {
1192 # make sure priority for waiting items is 0
1196 WHERE biblionumber = ?
1197 AND borrowernumber = ?
1200 my $sth = $dbh->prepare($query);
1201 $sth->execute( $biblio, $borrowernumber );
1207 # FIXME adding a new security in returned elements for changing priority,
1208 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1209 # This is wrong a waiting reserve has W set
1210 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1212 SELECT borrowernumber, reservedate, constrainttype
1214 WHERE biblionumber = ?
1215 AND ((found <> 'W') or found is NULL)
1216 ORDER BY priority ASC
1218 my $sth = $dbh->prepare($query);
1219 $sth->execute($biblio);
1220 while ( my $line = $sth->fetchrow_hashref ) {
1221 push( @reservedates, $line );
1222 push( @priority, $line );
1225 # To find the matching index
1227 my $key = -1; # to allow for 0 to be a valid result
1228 for ( $i = 0 ; $i < @priority ; $i++ ) {
1229 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1230 $key = $i; # save the index
1235 # if index exists in array then move it to new position
1236 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1237 my $new_rank = $rank -
1238 1; # $new_rank is what you want the new index to be in the array
1239 my $moving_item = splice( @priority, $key, 1 );
1240 splice( @priority, $new_rank, 0, $moving_item );
1243 # now fix the priority on those that are left....
1247 WHERE biblionumber = ?
1248 AND borrowernumber = ?
1252 $sth = $dbh->prepare($query);
1253 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1256 $priority[$j]->{'borrowernumber'},
1257 $priority[$j]->{'reservedate'}
1263 =item _Findgroupreserve
1265 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1268 I don't know what this does, because I don't understand how reserve
1269 constraints work. I think the idea is that you reserve a particular
1270 biblio, and the constraint allows you to restrict it to a given
1271 biblioitem (e.g., if you want to borrow the audio book edition of "The
1272 Prophet", rather than the first available publication).
1274 C<&_Findgroupreserve> returns :
1275 C<@results> is an array of references-to-hash whose keys are mostly
1276 fields from the reserves table of the Koha database, plus
1277 C<biblioitemnumber>.
1281 sub _Findgroupreserve {
1282 my ( $bibitem, $biblio, $itemnumber ) = @_;
1283 my $dbh = C4::Context->dbh;
1285 # check for exact targetted match
1286 my $item_level_target_query = qq/
1287 SELECT reserves.biblionumber AS biblionumber,
1288 reserves.borrowernumber AS borrowernumber,
1289 reserves.reservedate AS reservedate,
1290 reserves.branchcode AS branchcode,
1291 reserves.cancellationdate AS cancellationdate,
1292 reserves.found AS found,
1293 reserves.reservenotes AS reservenotes,
1294 reserves.priority AS priority,
1295 reserves.timestamp AS timestamp,
1296 biblioitems.biblioitemnumber AS biblioitemnumber,
1297 reserves.itemnumber AS itemnumber
1299 JOIN biblioitems USING (biblionumber)
1300 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1303 AND item_level_request = 1
1306 my $sth = $dbh->prepare($item_level_target_query);
1307 $sth->execute($itemnumber);
1309 if ( my $data = $sth->fetchrow_hashref ) {
1310 push( @results, $data );
1312 return @results if @results;
1314 # check for title-level targetted match
1315 my $title_level_target_query = qq/
1316 SELECT reserves.biblionumber AS biblionumber,
1317 reserves.borrowernumber AS borrowernumber,
1318 reserves.reservedate AS reservedate,
1319 reserves.branchcode AS branchcode,
1320 reserves.cancellationdate AS cancellationdate,
1321 reserves.found AS found,
1322 reserves.reservenotes AS reservenotes,
1323 reserves.priority AS priority,
1324 reserves.timestamp AS timestamp,
1325 biblioitems.biblioitemnumber AS biblioitemnumber,
1326 reserves.itemnumber AS itemnumber
1328 JOIN biblioitems USING (biblionumber)
1329 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1332 AND item_level_request = 0
1333 AND hold_fill_targets.itemnumber = ?
1335 $sth = $dbh->prepare($title_level_target_query);
1336 $sth->execute($itemnumber);
1338 if ( my $data = $sth->fetchrow_hashref ) {
1339 push( @results, $data );
1341 return @results if @results;
1344 SELECT reserves.biblionumber AS biblionumber,
1345 reserves.borrowernumber AS borrowernumber,
1346 reserves.reservedate AS reservedate,
1347 reserves.branchcode AS branchcode,
1348 reserves.cancellationdate AS cancellationdate,
1349 reserves.found AS found,
1350 reserves.reservenotes AS reservenotes,
1351 reserves.priority AS priority,
1352 reserves.timestamp AS timestamp,
1353 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1354 reserves.itemnumber AS itemnumber
1356 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1357 WHERE reserves.biblionumber = ?
1358 AND ( ( reserveconstraints.biblioitemnumber = ?
1359 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1360 AND reserves.reservedate =reserveconstraints.reservedate )
1361 OR reserves.constrainttype='a' )
1362 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1364 $sth = $dbh->prepare($query);
1365 $sth->execute( $biblio, $bibitem, $itemnumber );
1367 while ( my $data = $sth->fetchrow_hashref ) {
1368 push( @results, $data );
1373 =item _koha_notify_reserve
1377 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1381 Sends a notification to the patron that their hold has been filled (through
1382 ModReserveAffect, _not_ ModReserveFill)
1386 sub _koha_notify_reserve {
1387 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1389 my $dbh = C4::Context->dbh;
1390 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1392 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1394 my $sth = $dbh->prepare("
1397 WHERE borrowernumber = ?
1398 AND biblionumber = ?
1400 $sth->execute( $borrowernumber, $biblionumber );
1401 my $reserve = $sth->fetchrow_hashref;
1402 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1404 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1406 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1408 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1409 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1410 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1411 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1413 if ( $reserve->{'itemnumber'} ) {
1414 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1416 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1418 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1419 # aka, 'email' in ->{'transports'}
1420 C4::Letters::EnqueueLetter(
1421 { letter => $letter,
1422 borrowernumber => $borrowernumber,
1423 message_transport_type => 'email',
1424 from_address => $admin_email_address,
1429 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1430 C4::Letters::EnqueueLetter(
1431 { letter => $letter,
1432 borrowernumber => $borrowernumber,
1433 message_transport_type => 'sms',
1443 Koha Developement team <info@koha.org>