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;
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);
1003 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo );
1008 =item ModReserveCancelAll
1010 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1012 function to cancel reserv,check other reserves, and transfer document if it's necessary
1016 sub ModReserveCancelAll {
1019 my ( $itemnumber, $borrowernumber ) = @_;
1021 #step 1 : cancel the reservation
1022 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1024 #step 2 launch the subroutine of the others reserves
1025 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1027 return ( $messages, $nextreservinfo );
1030 =item ModReserveMinusPriority
1032 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1034 Reduce the values of queuded list
1038 sub ModReserveMinusPriority {
1039 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1041 #first step update the value of the first person on reserv
1042 my $dbh = C4::Context->dbh;
1045 SET priority = 0 , itemnumber = ?
1046 WHERE borrowernumber=?
1049 my $sth_upd = $dbh->prepare($query);
1050 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1051 # second step update all others reservs
1052 _FixPriority($biblionumber, $borrowernumber, '0');
1055 =item GetReserveInfo
1057 &GetReserveInfo($borrowernumber,$biblionumber);
1059 Get item and borrower details for a current hold.
1060 Current implementation this query should have a single result.
1063 sub GetReserveInfo {
1064 my ( $borrowernumber, $biblionumber ) = @_;
1065 my $dbh = C4::Context->dbh;
1066 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1067 reserves.biblionumber, reserves.branchcode,
1068 notificationdate, reminderdate, priority, found,
1069 firstname, surname, phone,
1070 email, address, address2,
1071 cardnumber, city, zipcode,
1072 biblio.title, biblio.author,
1073 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1075 FROM reserves left join items
1076 ON items.itemnumber=reserves.itemnumber ,
1079 reserves.borrowernumber=? &&
1080 reserves.biblionumber=? &&
1081 reserves.borrowernumber=borrowers.borrowernumber &&
1082 reserves.biblionumber=biblio.biblionumber ";
1083 my $sth = $dbh->prepare($strsth);
1084 $sth->execute($borrowernumber,$biblionumber);
1086 my $data = $sth->fetchrow_hashref;
1091 =item IsAvailableForItemLevelRequest
1095 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1099 Checks whether a given item record is available for an
1100 item-level hold request. An item is available if
1102 * it is not lost AND
1103 * it is not damaged AND
1104 * it is not withdrawn AND
1105 * does not have a not for loan value > 0
1107 Whether or not the item is currently on loan is
1108 also checked - if the AllowOnShelfHolds system preference
1109 is ON, an item can be requested even if it is currently
1110 on loan to somebody else. If the system preference
1111 is OFF, an item that is currently checked out cannot
1112 be the target of an item-level hold request.
1114 Note that IsAvailableForItemLevelRequest() does not
1115 check if the staff operator is authorized to place
1116 a request on the item - in particular,
1117 this routine does not check IndependantBranches
1118 and canreservefromotherbranches.
1122 sub IsAvailableForItemLevelRequest {
1123 my $itemnumber = shift;
1125 my $item = GetItem($itemnumber);
1127 # must check the notforloan setting of the itemtype
1128 # FIXME - a lot of places in the code do this
1129 # or something similar - need to be
1131 my $dbh = C4::Context->dbh;
1132 my $notforloan_query;
1133 if (C4::Context->preference('item-level_itypes')) {
1134 $notforloan_query = "SELECT itemtypes.notforloan
1136 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1137 WHERE itemnumber = ?";
1139 $notforloan_query = "SELECT itemtypes.notforloan
1141 JOIN biblioitems USING (biblioitemnumber)
1142 JOIN itemtypes USING (itemtype)
1143 WHERE itemnumber = ?";
1145 my $sth = $dbh->prepare($notforloan_query);
1146 $sth->execute($itemnumber);
1147 my $notforloan_per_itemtype = 0;
1148 if (my ($notforloan) = $sth->fetchrow_array) {
1149 $notforloan_per_itemtype = 1 if $notforloan;
1152 my $available_per_item = 1;
1153 $available_per_item = 0 if $item->{itemlost} or
1154 ( $item->{notforloan} > 0 ) or
1155 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1156 $item->{wthdrawn} or
1157 $notforloan_per_itemtype;
1159 if (C4::Context->preference('AllowOnShelfHolds')) {
1160 return $available_per_item;
1162 return ($available_per_item and $item->{onloan});
1168 &_FixPriority($biblio,$borrowernumber,$rank);
1170 Only used internally (so don't export it)
1171 Changed how this functions works #
1172 Now just gets an array of reserves in the rank order and updates them with
1173 the array index (+1 as array starts from 0)
1174 and if $rank is supplied will splice item from the array and splice it back in again
1175 in new priority rank
1180 my ( $biblio, $borrowernumber, $rank ) = @_;
1181 my $dbh = C4::Context->dbh;
1182 if ( $rank eq "del" ) {
1183 CancelReserve( $biblio, undef, $borrowernumber );
1185 if ( $rank eq "W" || $rank eq "0" ) {
1187 # make sure priority for waiting items is 0
1191 WHERE biblionumber = ?
1192 AND borrowernumber = ?
1195 my $sth = $dbh->prepare($query);
1196 $sth->execute( $biblio, $borrowernumber );
1202 # FIXME adding a new security in returned elements for changing priority,
1203 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1204 # This is wrong a waiting reserve has W set
1205 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1207 SELECT borrowernumber, reservedate, constrainttype
1209 WHERE biblionumber = ?
1210 AND ((found <> 'W') or found is NULL)
1211 ORDER BY priority ASC
1213 my $sth = $dbh->prepare($query);
1214 $sth->execute($biblio);
1215 while ( my $line = $sth->fetchrow_hashref ) {
1216 push( @reservedates, $line );
1217 push( @priority, $line );
1220 # To find the matching index
1222 my $key = -1; # to allow for 0 to be a valid result
1223 for ( $i = 0 ; $i < @priority ; $i++ ) {
1224 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1225 $key = $i; # save the index
1230 # if index exists in array then move it to new position
1231 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1232 my $new_rank = $rank -
1233 1; # $new_rank is what you want the new index to be in the array
1234 my $moving_item = splice( @priority, $key, 1 );
1235 splice( @priority, $new_rank, 0, $moving_item );
1238 # now fix the priority on those that are left....
1242 WHERE biblionumber = ?
1243 AND borrowernumber = ?
1247 $sth = $dbh->prepare($query);
1248 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1251 $priority[$j]->{'borrowernumber'},
1252 $priority[$j]->{'reservedate'}
1258 =item _Findgroupreserve
1260 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1262 Looks for an item-specific match first, then for a title-level match, returning the
1263 first match found. If neither, then we look for a 3rd kind of match based on
1264 reserve constraints.
1266 TODO: add more explanation about reserve constraints
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 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1280 # check for exact targetted match
1281 my $item_level_target_query = qq/
1282 SELECT reserves.biblionumber AS biblionumber,
1283 reserves.borrowernumber AS borrowernumber,
1284 reserves.reservedate AS reservedate,
1285 reserves.branchcode AS branchcode,
1286 reserves.cancellationdate AS cancellationdate,
1287 reserves.found AS found,
1288 reserves.reservenotes AS reservenotes,
1289 reserves.priority AS priority,
1290 reserves.timestamp AS timestamp,
1291 biblioitems.biblioitemnumber AS biblioitemnumber,
1292 reserves.itemnumber AS itemnumber
1294 JOIN biblioitems USING (biblionumber)
1295 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1298 AND item_level_request = 1
1301 my $sth = $dbh->prepare($item_level_target_query);
1302 $sth->execute($itemnumber);
1304 if ( my $data = $sth->fetchrow_hashref ) {
1305 push( @results, $data );
1307 return @results if @results;
1309 # check for title-level targetted match
1310 my $title_level_target_query = qq/
1311 SELECT reserves.biblionumber AS biblionumber,
1312 reserves.borrowernumber AS borrowernumber,
1313 reserves.reservedate AS reservedate,
1314 reserves.branchcode AS branchcode,
1315 reserves.cancellationdate AS cancellationdate,
1316 reserves.found AS found,
1317 reserves.reservenotes AS reservenotes,
1318 reserves.priority AS priority,
1319 reserves.timestamp AS timestamp,
1320 biblioitems.biblioitemnumber AS biblioitemnumber,
1321 reserves.itemnumber AS itemnumber
1323 JOIN biblioitems USING (biblionumber)
1324 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1327 AND item_level_request = 0
1328 AND hold_fill_targets.itemnumber = ?
1330 $sth = $dbh->prepare($title_level_target_query);
1331 $sth->execute($itemnumber);
1333 if ( my $data = $sth->fetchrow_hashref ) {
1334 push( @results, $data );
1336 return @results if @results;
1339 SELECT reserves.biblionumber AS biblionumber,
1340 reserves.borrowernumber AS borrowernumber,
1341 reserves.reservedate AS reservedate,
1342 reserves.branchcode AS branchcode,
1343 reserves.cancellationdate AS cancellationdate,
1344 reserves.found AS found,
1345 reserves.reservenotes AS reservenotes,
1346 reserves.priority AS priority,
1347 reserves.timestamp AS timestamp,
1348 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1349 reserves.itemnumber AS itemnumber
1351 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1352 WHERE reserves.biblionumber = ?
1353 AND ( ( reserveconstraints.biblioitemnumber = ?
1354 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1355 AND reserves.reservedate = reserveconstraints.reservedate )
1356 OR reserves.constrainttype='a' )
1357 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1359 $sth = $dbh->prepare($query);
1360 $sth->execute( $biblio, $bibitem, $itemnumber );
1362 while ( my $data = $sth->fetchrow_hashref ) {
1363 push( @results, $data );
1368 =item _koha_notify_reserve
1372 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1376 Sends a notification to the patron that their hold has been filled (through
1377 ModReserveAffect, _not_ ModReserveFill)
1381 sub _koha_notify_reserve {
1382 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1384 my $dbh = C4::Context->dbh;
1385 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1387 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1389 my $sth = $dbh->prepare("
1392 WHERE borrowernumber = ?
1393 AND biblionumber = ?
1395 $sth->execute( $borrowernumber, $biblionumber );
1396 my $reserve = $sth->fetchrow_hashref;
1397 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1399 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1401 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1403 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1404 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1405 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1406 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1408 if ( $reserve->{'itemnumber'} ) {
1409 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1411 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1413 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1414 # aka, 'email' in ->{'transports'}
1415 C4::Letters::EnqueueLetter(
1416 { letter => $letter,
1417 borrowernumber => $borrowernumber,
1418 message_transport_type => 'email',
1419 from_address => $admin_email_address,
1424 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1425 C4::Letters::EnqueueLetter(
1426 { letter => $letter,
1427 borrowernumber => $borrowernumber,
1428 message_transport_type => 'sms',
1438 Koha Developement team <info@koha.org>