3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN Ouest Provence
5 # 2007 BibLibre Paul POULAIN
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
24 # use warnings; # FIXME: someday
33 # for _koha_notify_reserve
34 use C4::Members::Messaging;
36 use C4::Branch qw( GetBranchDetail );
37 use List::MoreUtils qw( firstidx );
39 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
43 C4::Reserves - Koha functions for dealing with reservation.
51 this modules provides somes functions to deal with reservations.
53 Reserves are stored in reserves table.
54 The following columns contains important values :
55 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
56 =0 : then the reserve is being dealed
57 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
58 W(aiting) : the reserve has an itemnumber affected, and is on the way
59 F(inished) : the reserve has been completed, and is done
60 - itemnumber : empty : the reserve is still unaffected to an item
61 filled: the reserve is attached to an item
62 The complete workflow is :
63 ==== 1st use case ====
64 patron request a document, 1st available : P >0, F=NULL, I=NULL
65 a library having it run "transfertodo", and clic on the list
66 if there is no transfer to do, the reserve waiting
67 patron can pick it up P =0, F=W, I=filled
68 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
69 The pickup library recieve the book, it check in P =0, F=W, I=filled
70 The patron borrow the book P =0, F=F, I=filled
72 ==== 2nd use case ====
73 patron requests a document, a given item,
74 If pickup is holding branch P =0, F=W, I=filled
75 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
76 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
77 The patron borrow the book P =0, F=F, I=filled
86 # set the version for version checking
93 &GetReservesFromItemnumber
94 &GetReservesFromBiblionumber
95 &GetReservesFromBorrowernumber
109 &ModReserveMinusPriority
114 &IsAvailableForItemLevelRequest
120 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
126 $branch, $borrowernumber, $biblionumber,
127 $constraint, $bibitems, $priority, $notes,
128 $title, $checkitem, $found
131 GetReserveFee($borrowernumber, $biblionumber, $constraint,
133 my $dbh = C4::Context->dbh;
134 my $const = lc substr( $constraint, 0, 1 );
135 my @datearr = localtime(time);
137 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
140 # If the reserv had the waiting status, we had the value of the resdate
141 if ( $found eq 'W' ) {
142 $waitingdate = $resdate;
146 # updates take place here
148 my $nextacctno = &getnextacctno( $borrowernumber );
150 INSERT INTO accountlines
151 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
153 (?,?,now(),?,?,'Res',?)
155 my $usth = $dbh->prepare($query);
156 $usth->execute( $borrowernumber, $nextacctno, $fee,
157 "Reserve Charge - $title", $fee );
163 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
164 priority,reservenotes,itemnumber,found,waitingdate)
169 my $sth = $dbh->prepare($query);
171 $borrowernumber, $biblionumber, $resdate, $branch,
172 $const, $priority, $notes, $checkitem,
176 # Send e-mail to librarian if syspref is active
177 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
178 my $borrower = GetMemberDetails($borrowernumber);
179 my $biblio = GetBiblioData($biblionumber);
180 my $letter = C4::Letters::getletter( 'reserves', 'HOLDPLACED');
181 my $admin_email_address = C4::Context->preference('KohaAdminEmailAddress');
183 my %keys = (%$borrower, %$biblio);
184 foreach my $key (keys %keys) {
185 my $replacefield = "<<$key>>";
186 $letter->{content} =~ s/$replacefield/$keys{$key}/g;
187 $letter->{title} =~ s/$replacefield/$keys{$key}/g;
190 C4::Letters::EnqueueLetter(
192 borrowernumber => $borrowernumber,
193 message_transport_type => 'email',
194 from_address => $admin_email_address,
195 to_address => $admin_email_address,
204 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
206 INSERT INTO reserveconstraints
207 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
211 $sth = $dbh->prepare($query); # keep prepare outside the loop!
212 foreach (@$bibitems) {
213 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
216 return; # FIXME: why not have a useful return value?
219 =item GetReservesFromBiblionumber
221 ($count, $title_reserves) = &GetReserves($biblionumber);
223 This function gets the list of reservations for one C<$biblionumber>, returning a count
224 of the reserves and an arrayref pointing to the reserves for C<$biblionumber>.
228 sub GetReservesFromBiblionumber {
229 my ($biblionumber) = shift or return (0, []);
230 my $dbh = C4::Context->dbh;
232 # Find the desired items in the reserves
235 timestamp AS rtimestamp,
245 WHERE biblionumber = ?
247 my $sth = $dbh->prepare($query);
248 $sth->execute($biblionumber);
251 while ( my $data = $sth->fetchrow_hashref ) {
253 # FIXME - What is this doing? How do constraints work?
254 if ($data->{constrainttype} eq 'o') {
256 SELECT biblioitemnumber
257 FROM reserveconstraints
258 WHERE biblionumber = ?
259 AND borrowernumber = ?
262 my $csth = $dbh->prepare($query);
263 $csth->execute($data->{biblionumber}, $data->{borrowernumber}, $data->{reservedate});
265 while ( my $bibitemnos = $csth->fetchrow_array ) {
266 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
268 my $count = scalar @bibitemno;
270 # if we have two or more different specific itemtypes
271 # reserved by same person on same day
274 $bdata = GetBiblioItemData( $bibitemno[$i] ); # FIXME: This doesn't make sense.
275 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
278 # Look up the book we just found.
279 $bdata = GetBiblioItemData( $bibitemno[0] );
281 # Add the results of this latest search to the current
283 # FIXME - An 'each' would probably be more efficient.
284 foreach my $key ( keys %$bdata ) {
285 $data->{$key} = $bdata->{$key};
288 push @results, $data;
290 return ( $#results + 1, \@results );
293 =item GetReservesFromItemnumber
295 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
297 TODO :: Description here
301 sub GetReservesFromItemnumber {
302 my ( $itemnumber ) = @_;
303 my $dbh = C4::Context->dbh;
305 SELECT reservedate,borrowernumber,branchcode
309 my $sth_res = $dbh->prepare($query);
310 $sth_res->execute($itemnumber);
311 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
312 return ( $reservedate, $borrowernumber, $branchcode );
315 =item GetReservesFromBorrowernumber
317 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
323 sub GetReservesFromBorrowernumber {
324 my ( $borrowernumber, $status ) = @_;
325 my $dbh = C4::Context->dbh;
328 $sth = $dbh->prepare("
331 WHERE borrowernumber=?
335 $sth->execute($borrowernumber,$status);
337 $sth = $dbh->prepare("
340 WHERE borrowernumber=?
343 $sth->execute($borrowernumber);
345 my $data = $sth->fetchall_arrayref({});
348 #-------------------------------------------------------------------------------------
350 =item GetReserveCount
352 $number = &GetReserveCount($borrowernumber);
354 this function returns the number of reservation for a borrower given on input arg.
358 sub GetReserveCount {
359 my ($borrowernumber) = @_;
361 my $dbh = C4::Context->dbh;
364 SELECT COUNT(*) AS counter
366 WHERE borrowernumber = ?
368 my $sth = $dbh->prepare($query);
369 $sth->execute($borrowernumber);
370 my $row = $sth->fetchrow_hashref;
371 return $row->{counter};
374 =item GetOtherReserves
376 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
378 Check queued list of this document and check if this document must be transfered
382 sub GetOtherReserves {
383 my ($itemnumber) = @_;
386 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
387 if ($checkreserves) {
388 my $iteminfo = GetItem($itemnumber);
389 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
390 $messages->{'transfert'} = $checkreserves->{'branchcode'};
391 #minus priorities of others reservs
392 ModReserveMinusPriority(
394 $checkreserves->{'borrowernumber'},
395 $iteminfo->{'biblionumber'}
398 #launch the subroutine dotransfer
399 C4::Items::ModItemTransfer(
401 $iteminfo->{'holdingbranch'},
402 $checkreserves->{'branchcode'}
407 #step 2b : case of a reservation on the same branch, set the waiting status
409 $messages->{'waiting'} = 1;
410 ModReserveMinusPriority(
412 $checkreserves->{'borrowernumber'},
413 $iteminfo->{'biblionumber'}
415 ModReserveStatus($itemnumber,'W');
418 $nextreservinfo = $checkreserves->{'borrowernumber'};
421 return ( $messages, $nextreservinfo );
426 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
428 Calculate the fee for a reserve
433 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
436 my $dbh = C4::Context->dbh;
437 my $const = lc substr( $constraint, 0, 1 );
439 SELECT * FROM borrowers
440 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
441 WHERE borrowernumber = ?
443 my $sth = $dbh->prepare($query);
444 $sth->execute($borrowernumber);
445 my $data = $sth->fetchrow_hashref;
447 my $fee = $data->{'reservefee'};
448 my $cntitems = @- > $bibitems;
452 # check for items on issue
453 # first find biblioitem records
455 my $sth1 = $dbh->prepare(
456 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
457 WHERE (biblio.biblionumber = ?)"
459 $sth1->execute($biblionumber);
460 while ( my $data1 = $sth1->fetchrow_hashref ) {
461 if ( $const eq "a" ) {
462 push @biblioitems, $data1;
467 while ( $x < $cntitems ) {
468 if ( @$bibitems->{'biblioitemnumber'} ==
469 $data->{'biblioitemnumber'} )
475 if ( $const eq 'o' ) {
477 push @biblioitems, $data1;
482 push @biblioitems, $data1;
488 my $cntitemsfound = @biblioitems;
492 while ( $x < $cntitemsfound ) {
493 my $bitdata = $biblioitems[$x];
494 my $sth2 = $dbh->prepare(
496 WHERE biblioitemnumber = ?"
498 $sth2->execute( $bitdata->{'biblioitemnumber'} );
499 while ( my $itdata = $sth2->fetchrow_hashref ) {
500 my $sth3 = $dbh->prepare(
501 "SELECT * FROM issues
502 WHERE itemnumber = ?"
504 $sth3->execute( $itdata->{'itemnumber'} );
505 if ( my $isdata = $sth3->fetchrow_hashref ) {
513 if ( $allissued == 0 ) {
515 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
516 $rsth->execute($biblionumber);
517 if ( my $rdata = $rsth->fetchrow_hashref ) {
527 =item GetReservesToBranch
529 @transreserv = GetReservesToBranch( $frombranch );
531 Get reserve list for a given branch
535 sub GetReservesToBranch {
536 my ( $frombranch ) = @_;
537 my $dbh = C4::Context->dbh;
538 my $sth = $dbh->prepare(
539 "SELECT borrowernumber,reservedate,itemnumber,timestamp
544 $sth->execute( $frombranch );
547 while ( my $data = $sth->fetchrow_hashref ) {
548 $transreserv[$i] = $data;
551 return (@transreserv);
554 =item GetReservesForBranch
556 @transreserv = GetReservesForBranch($frombranch);
560 sub GetReservesForBranch {
561 my ($frombranch) = @_;
562 my $dbh = C4::Context->dbh;
563 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
568 $query .= " AND branchcode=? ";
570 $query .= "ORDER BY waitingdate" ;
571 my $sth = $dbh->prepare($query);
573 $sth->execute($frombranch);
580 while ( my $data = $sth->fetchrow_hashref ) {
581 $transreserv[$i] = $data;
584 return (@transreserv);
589 ($status, $reserve) = &CheckReserves($itemnumber);
591 Find a book in the reserves.
593 C<$itemnumber> is the book's item number.
595 As I understand it, C<&CheckReserves> looks for the given item in the
596 reserves. If it is found, that's a match, and C<$status> is set to
599 Otherwise, it finds the most important item in the reserves with the
600 same biblio number as this book (I'm not clear on this) and returns it
601 with C<$status> set to C<Reserved>.
603 C<&CheckReserves> returns a two-element list:
605 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
607 C<$reserve> is the reserve item that matched. It is a
608 reference-to-hash whose keys are mostly the fields of the reserves
609 table in the Koha database.
614 my ( $item, $barcode ) = @_;
615 my $dbh = C4::Context->dbh;
618 my $qitem = $dbh->quote($item);
619 # Look up the item by itemnumber
621 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
623 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
624 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
625 WHERE itemnumber=$qitem
627 $sth = $dbh->prepare($query);
630 my $qbc = $dbh->quote($barcode);
631 # Look up the item by barcode
633 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
635 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
636 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
637 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
638 AND biblioitems.itemtype = itemtypes.itemtype
641 $sth = $dbh->prepare($query);
643 # FIXME - This function uses $item later on. Ought to set it here.
646 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item ) = $sth->fetchrow_array;
648 # if item is not for loan it cannot be reserved either.....
649 # execption to notforloan is where items.notforloan < 0 : This indicates the item is holdable.
650 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
652 # get the reserves...
653 # Find this item in the reserves
654 my @reserves = _Findgroupreserve( $bibitem, $biblio, $item );
655 my $count = scalar @reserves;
657 # $priority and $highest are used to find the most important item
658 # in the list returned by &_Findgroupreserve. (The lower $priority,
659 # the more important the item.)
660 # $highest is the most important item we've seen so far.
661 my $priority = 10000000;
664 foreach my $res (@reserves) {
665 # FIXME - $item might be undefined or empty: the caller
666 # might be searching by barcode.
667 if ( $res->{'itemnumber'} == $item && $res->{'priority'} == 0) {
669 return ( "Waiting", $res );
672 # See if this item is more important than what we've got
674 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
676 $priority = $res->{'priority'};
683 # If we get this far, then no exact match was found. Print the
684 # most important item on the list. I think this tells us who's
685 # next in line to get this book.
686 if ($highest) { # FIXME - $highest might be undefined
687 $highest->{'itemnumber'} = $item;
688 return ( "Reserved", $highest );
697 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
701 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
702 cancel, but not both: if both are given, C<&CancelReserve> does
705 C<$borrowernumber> is the borrower number of the patron on whose
706 behalf the book was reserved.
708 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
709 priorities of the other people who are waiting on the book.
714 my ( $biblio, $item, $borr ) = @_;
715 my $dbh = C4::Context->dbh;
716 if ( $item and $borr ) {
717 # removing a waiting reserve record....
718 # update the database...
721 SET cancellationdate = now(),
725 AND borrowernumber = ?
727 my $sth = $dbh->prepare($query);
728 $sth->execute( $item, $borr );
731 INSERT INTO old_reserves
732 SELECT * FROM reserves
734 AND borrowernumber = ?
736 $sth = $dbh->prepare($query);
737 $sth->execute( $item, $borr );
741 AND borrowernumber = ?
743 $sth = $dbh->prepare($query);
744 $sth->execute( $item, $borr );
747 # removing a reserve record....
748 # get the prioritiy on this record....
751 SELECT priority FROM reserves
752 WHERE biblionumber = ?
753 AND borrowernumber = ?
754 AND cancellationdate IS NULL
755 AND itemnumber IS NULL
757 my $sth = $dbh->prepare($query);
758 $sth->execute( $biblio, $borr );
759 ($priority) = $sth->fetchrow_array;
763 SET cancellationdate = now(),
766 WHERE biblionumber = ?
767 AND borrowernumber = ?
770 # update the database, removing the record...
771 $sth = $dbh->prepare($query);
772 $sth->execute( $biblio, $borr );
776 INSERT INTO old_reserves
777 SELECT * FROM reserves
778 WHERE biblionumber = ?
779 AND borrowernumber = ?
781 $sth = $dbh->prepare($query);
782 $sth->execute( $biblio, $borr );
786 WHERE biblionumber = ?
787 AND borrowernumber = ?
789 $sth = $dbh->prepare($query);
790 $sth->execute( $biblio, $borr );
792 # now fix the priority on the others....
793 _FixPriority( $priority, $biblio );
801 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
805 Change a hold request's priority or cancel it.
807 C<$rank> specifies the effect of the change. If C<$rank>
808 is 'W' or 'n', nothing happens. This corresponds to leaving a
809 request alone when changing its priority in the holds queue
812 If C<$rank> is 'del', the hold request is cancelled.
814 If C<$rank> is an integer greater than zero, the priority of
815 the request is set to that value. Since priority != 0 means
816 that the item is not waiting on the hold shelf, setting the
817 priority to a non-zero value also sets the request's found
818 status and waiting date to NULL.
820 The optional C<$itemnumber> parameter is used only when
821 C<$rank> is a non-zero integer; if supplied, the itemnumber
822 of the hold request is set accordingly; if omitted, the itemnumber
825 FIXME: Note that the forgoing can have the effect of causing
826 item-level hold requests to turn into title-level requests. This
827 will be fixed once reserves has separate columns for requested
828 itemnumber and supplying itemnumber.
833 #subroutine to update a reserve
834 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
835 return if $rank eq "W";
836 return if $rank eq "n";
837 my $dbh = C4::Context->dbh;
838 if ( $rank eq "del" ) {
841 SET cancellationdate=now()
842 WHERE biblionumber = ?
843 AND borrowernumber = ?
845 my $sth = $dbh->prepare($query);
846 $sth->execute( $biblio, $borrower );
849 INSERT INTO old_reserves
852 WHERE biblionumber = ?
853 AND borrowernumber = ?
855 $sth = $dbh->prepare($query);
856 $sth->execute( $biblio, $borrower );
859 WHERE biblionumber = ?
860 AND borrowernumber = ?
862 $sth = $dbh->prepare($query);
863 $sth->execute( $biblio, $borrower );
866 elsif ($rank =~ /^\d+/ and $rank > 0) {
868 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
869 WHERE biblionumber = ?
870 AND borrowernumber = ?
872 my $sth = $dbh->prepare($query);
873 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
875 _FixPriority( $biblio, $borrower, $rank);
881 &ModReserveFill($reserve);
883 Fill a reserve. If I understand this correctly, this means that the
884 reserved book has been found and given to the patron who reserved it.
886 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
887 whose keys are fields from the reserves table in the Koha database.
893 my $dbh = C4::Context->dbh;
894 # fill in a reserve record....
895 my $biblionumber = $res->{'biblionumber'};
896 my $borrowernumber = $res->{'borrowernumber'};
897 my $resdate = $res->{'reservedate'};
899 # get the priority on this record....
901 my $query = "SELECT priority
903 WHERE biblionumber = ?
904 AND borrowernumber = ?
905 AND reservedate = ?";
906 my $sth = $dbh->prepare($query);
907 $sth->execute( $biblionumber, $borrowernumber, $resdate );
908 ($priority) = $sth->fetchrow_array;
911 # update the database...
912 $query = "UPDATE reserves
915 WHERE biblionumber = ?
917 AND borrowernumber = ?
919 $sth = $dbh->prepare($query);
920 $sth->execute( $biblionumber, $resdate, $borrowernumber );
923 # move to old_reserves
924 $query = "INSERT INTO old_reserves
925 SELECT * FROM reserves
926 WHERE biblionumber = ?
928 AND borrowernumber = ?
930 $sth = $dbh->prepare($query);
931 $sth->execute( $biblionumber, $resdate, $borrowernumber );
932 $query = "DELETE FROM reserves
933 WHERE biblionumber = ?
935 AND borrowernumber = ?
937 $sth = $dbh->prepare($query);
938 $sth->execute( $biblionumber, $resdate, $borrowernumber );
940 # now fix the priority on the others (if the priority wasn't
941 # already sorted!)....
942 unless ( $priority == 0 ) {
943 _FixPriority( $priority, $biblionumber );
947 =item ModReserveStatus
949 &ModReserveStatus($itemnumber, $newstatus);
951 Update the reserve status for the active (priority=0) reserve.
953 $itemnumber is the itemnumber the reserve is on
955 $newstatus is the new status.
959 sub ModReserveStatus {
961 #first : check if we have a reservation for this item .
962 my ($itemnumber, $newstatus) = @_;
963 my $dbh = C4::Context->dbh;
964 my $query = " UPDATE reserves
965 SET found=?,waitingdate = now()
970 my $sth_set = $dbh->prepare($query);
971 $sth_set->execute( $newstatus, $itemnumber );
974 =item ModReserveAffect
976 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
978 This function affect an item and a status for a given reserve
979 The itemnumber parameter is used to find the biblionumber.
980 with the biblionumber & the borrowernumber, we can affect the itemnumber
981 to the correct reserve.
983 if $transferToDo is not set, then the status is set to "Waiting" as well.
984 otherwise, a transfer is on the way, and the end of the transfer will
985 take care of the waiting status
988 sub ModReserveAffect {
989 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
990 my $dbh = C4::Context->dbh;
992 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
993 # attached to $itemnumber
994 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
995 $sth->execute($itemnumber);
996 my ($biblionumber) = $sth->fetchrow;
997 # If we affect a reserve that has to be transfered, don't set to Waiting
1004 WHERE borrowernumber = ?
1005 AND biblionumber = ?
1009 # affect the reserve to Waiting as well.
1016 WHERE borrowernumber = ?
1017 AND biblionumber = ?
1020 $sth = $dbh->prepare($query);
1021 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1024 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo );
1029 =item ModReserveCancelAll
1031 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1033 function to cancel reserv,check other reserves, and transfer document if it's necessary
1037 sub ModReserveCancelAll {
1040 my ( $itemnumber, $borrowernumber ) = @_;
1042 #step 1 : cancel the reservation
1043 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1045 #step 2 launch the subroutine of the others reserves
1046 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1048 return ( $messages, $nextreservinfo );
1051 =item ModReserveMinusPriority
1053 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1055 Reduce the values of queuded list
1059 sub ModReserveMinusPriority {
1060 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1062 #first step update the value of the first person on reserv
1063 my $dbh = C4::Context->dbh;
1066 SET priority = 0 , itemnumber = ?
1067 WHERE borrowernumber=?
1070 my $sth_upd = $dbh->prepare($query);
1071 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1072 # second step update all others reservs
1073 _FixPriority($biblionumber, $borrowernumber, '0');
1076 =item GetReserveInfo
1078 &GetReserveInfo($borrowernumber,$biblionumber);
1080 Get item and borrower details for a current hold.
1081 Current implementation this query should have a single result.
1084 sub GetReserveInfo {
1085 my ( $borrowernumber, $biblionumber ) = @_;
1086 my $dbh = C4::Context->dbh;
1087 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1088 reserves.biblionumber, reserves.branchcode,
1089 notificationdate, reminderdate, priority, found,
1090 firstname, surname, phone,
1091 email, address, address2,
1092 cardnumber, city, zipcode,
1093 biblio.title, biblio.author,
1094 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1096 FROM reserves left join items
1097 ON items.itemnumber=reserves.itemnumber ,
1100 reserves.borrowernumber=? &&
1101 reserves.biblionumber=? &&
1102 reserves.borrowernumber=borrowers.borrowernumber &&
1103 reserves.biblionumber=biblio.biblionumber ";
1104 my $sth = $dbh->prepare($strsth);
1105 $sth->execute($borrowernumber,$biblionumber);
1107 my $data = $sth->fetchrow_hashref;
1112 =item IsAvailableForItemLevelRequest
1116 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1120 Checks whether a given item record is available for an
1121 item-level hold request. An item is available if
1123 * it is not lost AND
1124 * it is not damaged AND
1125 * it is not withdrawn AND
1126 * does not have a not for loan value > 0
1128 Whether or not the item is currently on loan is
1129 also checked - if the AllowOnShelfHolds system preference
1130 is ON, an item can be requested even if it is currently
1131 on loan to somebody else. If the system preference
1132 is OFF, an item that is currently checked out cannot
1133 be the target of an item-level hold request.
1135 Note that IsAvailableForItemLevelRequest() does not
1136 check if the staff operator is authorized to place
1137 a request on the item - in particular,
1138 this routine does not check IndependantBranches
1139 and canreservefromotherbranches.
1143 sub IsAvailableForItemLevelRequest {
1144 my $itemnumber = shift;
1146 my $item = GetItem($itemnumber);
1148 # must check the notforloan setting of the itemtype
1149 # FIXME - a lot of places in the code do this
1150 # or something similar - need to be
1152 my $dbh = C4::Context->dbh;
1153 my $notforloan_query;
1154 if (C4::Context->preference('item-level_itypes')) {
1155 $notforloan_query = "SELECT itemtypes.notforloan
1157 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1158 WHERE itemnumber = ?";
1160 $notforloan_query = "SELECT itemtypes.notforloan
1162 JOIN biblioitems USING (biblioitemnumber)
1163 JOIN itemtypes USING (itemtype)
1164 WHERE itemnumber = ?";
1166 my $sth = $dbh->prepare($notforloan_query);
1167 $sth->execute($itemnumber);
1168 my $notforloan_per_itemtype = 0;
1169 if (my ($notforloan) = $sth->fetchrow_array) {
1170 $notforloan_per_itemtype = 1 if $notforloan;
1173 my $available_per_item = 1;
1174 $available_per_item = 0 if $item->{itemlost} or
1175 ( $item->{notforloan} > 0 ) or
1176 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1177 $item->{wthdrawn} or
1178 $notforloan_per_itemtype;
1180 if (C4::Context->preference('AllowOnShelfHolds')) {
1181 return $available_per_item;
1183 return ($available_per_item and $item->{onloan});
1189 &_FixPriority($biblio,$borrowernumber,$rank);
1191 Only used internally (so don't export it)
1192 Changed how this functions works #
1193 Now just gets an array of reserves in the rank order and updates them with
1194 the array index (+1 as array starts from 0)
1195 and if $rank is supplied will splice item from the array and splice it back in again
1196 in new priority rank
1201 my ( $biblio, $borrowernumber, $rank ) = @_;
1202 my $dbh = C4::Context->dbh;
1203 if ( $rank eq "del" ) {
1204 CancelReserve( $biblio, undef, $borrowernumber );
1206 if ( $rank eq "W" || $rank eq "0" ) {
1208 # make sure priority for waiting items is 0
1212 WHERE biblionumber = ?
1213 AND borrowernumber = ?
1216 my $sth = $dbh->prepare($query);
1217 $sth->execute( $biblio, $borrowernumber );
1223 # FIXME adding a new security in returned elements for changing priority,
1224 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1225 # This is wrong a waiting reserve has W set
1226 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1228 SELECT borrowernumber, reservedate, constrainttype
1230 WHERE biblionumber = ?
1231 AND ((found <> 'W') or found is NULL)
1232 ORDER BY priority ASC
1234 my $sth = $dbh->prepare($query);
1235 $sth->execute($biblio);
1236 while ( my $line = $sth->fetchrow_hashref ) {
1237 push( @reservedates, $line );
1238 push( @priority, $line );
1241 # To find the matching index
1243 my $key = -1; # to allow for 0 to be a valid result
1244 for ( $i = 0 ; $i < @priority ; $i++ ) {
1245 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1246 $key = $i; # save the index
1251 # if index exists in array then move it to new position
1252 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1253 my $new_rank = $rank -
1254 1; # $new_rank is what you want the new index to be in the array
1255 my $moving_item = splice( @priority, $key, 1 );
1256 splice( @priority, $new_rank, 0, $moving_item );
1259 # now fix the priority on those that are left....
1263 WHERE biblionumber = ?
1264 AND borrowernumber = ?
1268 $sth = $dbh->prepare($query);
1269 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1272 $priority[$j]->{'borrowernumber'},
1273 $priority[$j]->{'reservedate'}
1279 =item _Findgroupreserve
1281 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1283 Looks for an item-specific match first, then for a title-level match, returning the
1284 first match found. If neither, then we look for a 3rd kind of match based on
1285 reserve constraints.
1287 TODO: add more explanation about reserve constraints
1289 C<&_Findgroupreserve> returns :
1290 C<@results> is an array of references-to-hash whose keys are mostly
1291 fields from the reserves table of the Koha database, plus
1292 C<biblioitemnumber>.
1296 sub _Findgroupreserve {
1297 my ( $bibitem, $biblio, $itemnumber ) = @_;
1298 my $dbh = C4::Context->dbh;
1300 # check for exact targetted match
1301 # This select is valid for both item_level and biblio_level
1302 my $item_level_target_query = qq/
1303 SELECT reserves.biblionumber AS biblionumber,
1304 reserves.borrowernumber AS borrowernumber,
1305 reserves.reservedate AS reservedate,
1306 reserves.branchcode AS branchcode,
1307 reserves.cancellationdate AS cancellationdate,
1308 reserves.found AS found,
1309 reserves.reservenotes AS reservenotes,
1310 reserves.priority AS priority,
1311 reserves.timestamp AS timestamp,
1312 biblioitems.biblioitemnumber AS biblioitemnumber,
1313 reserves.itemnumber AS itemnumber
1315 JOIN biblioitems USING (biblionumber)
1316 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1319 AND hold_fill_targets.itemnumber = ?
1322 my $sth = $dbh->prepare($item_level_target_query);
1323 $sth->execute($itemnumber);
1324 my $data = $sth->fetchall_arrayref({});
1325 return @$data if (@$data);
1327 # check for title-level targetted match
1328 my $title_level_target_query = qq/
1329 SELECT reserves.biblionumber AS biblionumber,
1330 reserves.borrowernumber AS borrowernumber,
1331 reserves.reservedate AS reservedate,
1332 reserves.branchcode AS branchcode,
1333 reserves.cancellationdate AS cancellationdate,
1334 reserves.found AS found,
1335 reserves.reservenotes AS reservenotes,
1336 reserves.priority AS priority,
1337 reserves.timestamp AS timestamp,
1338 biblioitems.biblioitemnumber AS biblioitemnumber,
1339 reserves.itemnumber AS itemnumber
1341 JOIN biblioitems USING (biblionumber)
1342 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1345 AND item_level_request = 0
1346 AND hold_fill_targets.itemnumber = ?
1348 $sth = $dbh->prepare($title_level_target_query);
1349 $sth->execute($itemnumber);
1350 $data = $sth->fetchall_arrayref({});
1351 return @$data if (@$data);
1354 SELECT reserves.biblionumber AS biblionumber,
1355 reserves.borrowernumber AS borrowernumber,
1356 reserves.reservedate AS reservedate,
1357 reserves.branchcode AS branchcode,
1358 reserves.cancellationdate AS cancellationdate,
1359 reserves.found AS found,
1360 reserves.reservenotes AS reservenotes,
1361 reserves.priority AS priority,
1362 reserves.timestamp AS timestamp,
1363 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1364 reserves.itemnumber AS itemnumber
1366 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1367 WHERE reserves.biblionumber = ?
1368 AND ( ( reserveconstraints.biblioitemnumber = ?
1369 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1370 AND reserves.reservedate = reserveconstraints.reservedate )
1371 OR reserves.constrainttype='a' )
1372 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1374 $sth = $dbh->prepare($query);
1375 $sth->execute( $biblio, $bibitem, $itemnumber );
1376 $data = $sth->fetchall_arrayref({});
1377 return @$data if (@$data);
1381 =item _koha_notify_reserve
1385 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1389 Sends a notification to the patron that their hold has been filled (through
1390 ModReserveAffect, _not_ ModReserveFill)
1394 sub _koha_notify_reserve {
1395 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1397 my $dbh = C4::Context->dbh;
1398 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1400 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1402 my $sth = $dbh->prepare("
1405 WHERE borrowernumber = ?
1406 AND biblionumber = ?
1408 $sth->execute( $borrowernumber, $biblionumber );
1409 my $reserve = $sth->fetchrow_hashref;
1410 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1412 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1414 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1416 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1417 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1418 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1419 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1421 if ( $reserve->{'itemnumber'} ) {
1422 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1424 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1426 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1427 # aka, 'email' in ->{'transports'}
1428 C4::Letters::EnqueueLetter(
1429 { letter => $letter,
1430 borrowernumber => $borrowernumber,
1431 message_transport_type => 'email',
1432 from_address => $admin_email_address,
1437 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1438 C4::Letters::EnqueueLetter(
1439 { letter => $letter,
1440 borrowernumber => $borrowernumber,
1441 message_transport_type => 'sms',
1451 Koha Developement team <info@koha.org>