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;
35 use C4::Members qw( GetMember );
37 use C4::Branch qw( GetBranchDetail );
38 use List::MoreUtils qw( firstidx );
40 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
44 C4::Reserves - Koha functions for dealing with reservation.
52 this modules provides somes functions to deal with reservations.
54 Reserves are stored in reserves table.
55 The following columns contains important values :
56 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
57 =0 : then the reserve is being dealed
58 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
59 W(aiting) : the reserve has an itemnumber affected, and is on the way
60 F(inished) : the reserve has been completed, and is done
61 - itemnumber : empty : the reserve is still unaffected to an item
62 filled: the reserve is attached to an item
63 The complete workflow is :
64 ==== 1st use case ====
65 patron request a document, 1st available : P >0, F=NULL, I=NULL
66 a library having it run "transfertodo", and clic on the list
67 if there is no transfer to do, the reserve waiting
68 patron can pick it up P =0, F=W, I=filled
69 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
70 The pickup library recieve the book, it check in P =0, F=W, I=filled
71 The patron borrow the book P =0, F=F, I=filled
73 ==== 2nd use case ====
74 patron requests a document, a given item,
75 If pickup is holding branch P =0, F=W, I=filled
76 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
77 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
78 The patron borrow the book P =0, F=F, I=filled
87 # set the version for version checking
94 &GetReservesFromItemnumber
95 &GetReservesFromBiblionumber
96 &GetReservesFromBorrowernumber
110 &ModReserveMinusPriority
115 &IsAvailableForItemLevelRequest
121 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
127 $branch, $borrowernumber, $biblionumber,
128 $constraint, $bibitems, $priority, $notes,
129 $title, $checkitem, $found
132 GetReserveFee($borrowernumber, $biblionumber, $constraint,
134 my $dbh = C4::Context->dbh;
135 my $const = lc substr( $constraint, 0, 1 );
136 my @datearr = localtime(time);
138 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
141 # If the reserv had the waiting status, we had the value of the resdate
142 if ( $found eq 'W' ) {
143 $waitingdate = $resdate;
147 # updates take place here
149 my $nextacctno = &getnextacctno( $borrowernumber );
151 INSERT INTO accountlines
152 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
154 (?,?,now(),?,?,'Res',?)
156 my $usth = $dbh->prepare($query);
157 $usth->execute( $borrowernumber, $nextacctno, $fee,
158 "Reserve Charge - $title", $fee );
164 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
165 priority,reservenotes,itemnumber,found,waitingdate)
170 my $sth = $dbh->prepare($query);
172 $borrowernumber, $biblionumber, $resdate, $branch,
173 $const, $priority, $notes, $checkitem,
177 # Send e-mail to librarian if syspref is active
178 if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
179 my $borrower = GetMemberDetails($borrowernumber);
180 my $biblio = GetBiblioData($biblionumber);
181 my $letter = C4::Letters::getletter( 'reserves', 'HOLDPLACED');
182 my $admin_email_address = C4::Context->preference('KohaAdminEmailAddress');
184 my %keys = (%$borrower, %$biblio);
185 foreach my $key (keys %keys) {
186 my $replacefield = "<<$key>>";
187 $letter->{content} =~ s/$replacefield/$keys{$key}/g;
188 $letter->{title} =~ s/$replacefield/$keys{$key}/g;
191 C4::Letters::EnqueueLetter(
193 borrowernumber => $borrowernumber,
194 message_transport_type => 'email',
195 from_address => $admin_email_address,
196 to_address => $admin_email_address,
205 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
207 INSERT INTO reserveconstraints
208 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
212 $sth = $dbh->prepare($query); # keep prepare outside the loop!
213 foreach (@$bibitems) {
214 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
217 return; # FIXME: why not have a useful return value?
220 =item GetReservesFromBiblionumber
222 ($count, $title_reserves) = &GetReserves($biblionumber);
224 This function gets the list of reservations for one C<$biblionumber>, returning a count
225 of the reserves and an arrayref pointing to the reserves for C<$biblionumber>.
229 sub GetReservesFromBiblionumber {
230 my ($biblionumber) = shift or return (0, []);
231 my $dbh = C4::Context->dbh;
233 # Find the desired items in the reserves
236 timestamp AS rtimestamp,
246 WHERE biblionumber = ?
248 my $sth = $dbh->prepare($query);
249 $sth->execute($biblionumber);
252 while ( my $data = $sth->fetchrow_hashref ) {
254 # FIXME - What is this doing? How do constraints work?
255 if ($data->{constrainttype} eq 'o') {
257 SELECT biblioitemnumber
258 FROM reserveconstraints
259 WHERE biblionumber = ?
260 AND borrowernumber = ?
263 my $csth = $dbh->prepare($query);
264 $csth->execute($data->{biblionumber}, $data->{borrowernumber}, $data->{reservedate});
266 while ( my $bibitemnos = $csth->fetchrow_array ) {
267 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
269 my $count = scalar @bibitemno;
271 # if we have two or more different specific itemtypes
272 # reserved by same person on same day
275 $bdata = GetBiblioItemData( $bibitemno[$i] ); # FIXME: This doesn't make sense.
276 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
279 # Look up the book we just found.
280 $bdata = GetBiblioItemData( $bibitemno[0] );
282 # Add the results of this latest search to the current
284 # FIXME - An 'each' would probably be more efficient.
285 foreach my $key ( keys %$bdata ) {
286 $data->{$key} = $bdata->{$key};
289 push @results, $data;
291 return ( $#results + 1, \@results );
294 =item GetReservesFromItemnumber
296 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
298 TODO :: Description here
302 sub GetReservesFromItemnumber {
303 my ( $itemnumber ) = @_;
304 my $dbh = C4::Context->dbh;
306 SELECT reservedate,borrowernumber,branchcode
310 my $sth_res = $dbh->prepare($query);
311 $sth_res->execute($itemnumber);
312 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
313 return ( $reservedate, $borrowernumber, $branchcode );
316 =item GetReservesFromBorrowernumber
318 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
324 sub GetReservesFromBorrowernumber {
325 my ( $borrowernumber, $status ) = @_;
326 my $dbh = C4::Context->dbh;
329 $sth = $dbh->prepare("
332 WHERE borrowernumber=?
336 $sth->execute($borrowernumber,$status);
338 $sth = $dbh->prepare("
341 WHERE borrowernumber=?
344 $sth->execute($borrowernumber);
346 my $data = $sth->fetchall_arrayref({});
349 #-------------------------------------------------------------------------------------
351 =item GetReserveCount
353 $number = &GetReserveCount($borrowernumber);
355 this function returns the number of reservation for a borrower given on input arg.
359 sub GetReserveCount {
360 my ($borrowernumber) = @_;
362 my $dbh = C4::Context->dbh;
365 SELECT COUNT(*) AS counter
367 WHERE borrowernumber = ?
369 my $sth = $dbh->prepare($query);
370 $sth->execute($borrowernumber);
371 my $row = $sth->fetchrow_hashref;
372 return $row->{counter};
375 =item GetOtherReserves
377 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
379 Check queued list of this document and check if this document must be transfered
383 sub GetOtherReserves {
384 my ($itemnumber) = @_;
387 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
388 if ($checkreserves) {
389 my $iteminfo = GetItem($itemnumber);
390 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
391 $messages->{'transfert'} = $checkreserves->{'branchcode'};
392 #minus priorities of others reservs
393 ModReserveMinusPriority(
395 $checkreserves->{'borrowernumber'},
396 $iteminfo->{'biblionumber'}
399 #launch the subroutine dotransfer
400 C4::Items::ModItemTransfer(
402 $iteminfo->{'holdingbranch'},
403 $checkreserves->{'branchcode'}
408 #step 2b : case of a reservation on the same branch, set the waiting status
410 $messages->{'waiting'} = 1;
411 ModReserveMinusPriority(
413 $checkreserves->{'borrowernumber'},
414 $iteminfo->{'biblionumber'}
416 ModReserveStatus($itemnumber,'W');
419 $nextreservinfo = $checkreserves->{'borrowernumber'};
422 return ( $messages, $nextreservinfo );
427 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
429 Calculate the fee for a reserve
434 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
437 my $dbh = C4::Context->dbh;
438 my $const = lc substr( $constraint, 0, 1 );
440 SELECT * FROM borrowers
441 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
442 WHERE borrowernumber = ?
444 my $sth = $dbh->prepare($query);
445 $sth->execute($borrowernumber);
446 my $data = $sth->fetchrow_hashref;
448 my $fee = $data->{'reservefee'};
449 my $cntitems = @- > $bibitems;
453 # check for items on issue
454 # first find biblioitem records
456 my $sth1 = $dbh->prepare(
457 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
458 WHERE (biblio.biblionumber = ?)"
460 $sth1->execute($biblionumber);
461 while ( my $data1 = $sth1->fetchrow_hashref ) {
462 if ( $const eq "a" ) {
463 push @biblioitems, $data1;
468 while ( $x < $cntitems ) {
469 if ( @$bibitems->{'biblioitemnumber'} ==
470 $data->{'biblioitemnumber'} )
476 if ( $const eq 'o' ) {
478 push @biblioitems, $data1;
483 push @biblioitems, $data1;
489 my $cntitemsfound = @biblioitems;
493 while ( $x < $cntitemsfound ) {
494 my $bitdata = $biblioitems[$x];
495 my $sth2 = $dbh->prepare(
497 WHERE biblioitemnumber = ?"
499 $sth2->execute( $bitdata->{'biblioitemnumber'} );
500 while ( my $itdata = $sth2->fetchrow_hashref ) {
501 my $sth3 = $dbh->prepare(
502 "SELECT * FROM issues
503 WHERE itemnumber = ?"
505 $sth3->execute( $itdata->{'itemnumber'} );
506 if ( my $isdata = $sth3->fetchrow_hashref ) {
514 if ( $allissued == 0 ) {
516 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
517 $rsth->execute($biblionumber);
518 if ( my $rdata = $rsth->fetchrow_hashref ) {
528 =item GetReservesToBranch
530 @transreserv = GetReservesToBranch( $frombranch );
532 Get reserve list for a given branch
536 sub GetReservesToBranch {
537 my ( $frombranch ) = @_;
538 my $dbh = C4::Context->dbh;
539 my $sth = $dbh->prepare(
540 "SELECT borrowernumber,reservedate,itemnumber,timestamp
545 $sth->execute( $frombranch );
548 while ( my $data = $sth->fetchrow_hashref ) {
549 $transreserv[$i] = $data;
552 return (@transreserv);
555 =item GetReservesForBranch
557 @transreserv = GetReservesForBranch($frombranch);
561 sub GetReservesForBranch {
562 my ($frombranch) = @_;
563 my $dbh = C4::Context->dbh;
564 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
569 $query .= " AND branchcode=? ";
571 $query .= "ORDER BY waitingdate" ;
572 my $sth = $dbh->prepare($query);
574 $sth->execute($frombranch);
581 while ( my $data = $sth->fetchrow_hashref ) {
582 $transreserv[$i] = $data;
585 return (@transreserv);
590 ($status, $reserve) = &CheckReserves($itemnumber);
592 Find a book in the reserves.
594 C<$itemnumber> is the book's item number.
596 As I understand it, C<&CheckReserves> looks for the given item in the
597 reserves. If it is found, that's a match, and C<$status> is set to
600 Otherwise, it finds the most important item in the reserves with the
601 same biblio number as this book (I'm not clear on this) and returns it
602 with C<$status> set to C<Reserved>.
604 C<&CheckReserves> returns a two-element list:
606 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
608 C<$reserve> is the reserve item that matched. It is a
609 reference-to-hash whose keys are mostly the fields of the reserves
610 table in the Koha database.
615 my ( $item, $barcode ) = @_;
616 my $dbh = C4::Context->dbh;
619 my $qitem = $dbh->quote($item);
620 # Look up the item by itemnumber
622 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
624 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
625 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
626 WHERE itemnumber=$qitem
628 $sth = $dbh->prepare($query);
631 my $qbc = $dbh->quote($barcode);
632 # Look up the item by barcode
634 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
636 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
637 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
638 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
639 AND biblioitems.itemtype = itemtypes.itemtype
642 $sth = $dbh->prepare($query);
644 # FIXME - This function uses $item later on. Ought to set it here.
647 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item ) = $sth->fetchrow_array;
649 # if item is not for loan it cannot be reserved either.....
650 # execption to notforloan is where items.notforloan < 0 : This indicates the item is holdable.
651 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
653 # get the reserves...
654 # Find this item in the reserves
655 my @reserves = _Findgroupreserve( $bibitem, $biblio, $item );
656 my $count = scalar @reserves;
658 # $priority and $highest are used to find the most important item
659 # in the list returned by &_Findgroupreserve. (The lower $priority,
660 # the more important the item.)
661 # $highest is the most important item we've seen so far.
662 my $priority = 10000000;
665 foreach my $res (@reserves) {
666 # FIXME - $item might be undefined or empty: the caller
667 # might be searching by barcode.
668 if ( $res->{'itemnumber'} == $item && $res->{'priority'} == 0) {
670 return ( "Waiting", $res );
673 # See if this item is more important than what we've got
675 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
677 $priority = $res->{'priority'};
684 # If we get this far, then no exact match was found. Print the
685 # most important item on the list. I think this tells us who's
686 # next in line to get this book.
687 if ($highest) { # FIXME - $highest might be undefined
688 $highest->{'itemnumber'} = $item;
689 return ( "Reserved", $highest );
698 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
702 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
703 cancel, but not both: if both are given, C<&CancelReserve> does
706 C<$borrowernumber> is the borrower number of the patron on whose
707 behalf the book was reserved.
709 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
710 priorities of the other people who are waiting on the book.
715 my ( $biblio, $item, $borr ) = @_;
716 my $dbh = C4::Context->dbh;
717 if ( $item and $borr ) {
718 # removing a waiting reserve record....
719 # update the database...
722 SET cancellationdate = now(),
726 AND borrowernumber = ?
728 my $sth = $dbh->prepare($query);
729 $sth->execute( $item, $borr );
732 INSERT INTO old_reserves
733 SELECT * FROM reserves
735 AND borrowernumber = ?
737 $sth = $dbh->prepare($query);
738 $sth->execute( $item, $borr );
742 AND borrowernumber = ?
744 $sth = $dbh->prepare($query);
745 $sth->execute( $item, $borr );
748 # removing a reserve record....
749 # get the prioritiy on this record....
752 SELECT priority FROM reserves
753 WHERE biblionumber = ?
754 AND borrowernumber = ?
755 AND cancellationdate IS NULL
756 AND itemnumber IS NULL
758 my $sth = $dbh->prepare($query);
759 $sth->execute( $biblio, $borr );
760 ($priority) = $sth->fetchrow_array;
764 SET cancellationdate = now(),
767 WHERE biblionumber = ?
768 AND borrowernumber = ?
771 # update the database, removing the record...
772 $sth = $dbh->prepare($query);
773 $sth->execute( $biblio, $borr );
777 INSERT INTO old_reserves
778 SELECT * FROM reserves
779 WHERE biblionumber = ?
780 AND borrowernumber = ?
782 $sth = $dbh->prepare($query);
783 $sth->execute( $biblio, $borr );
787 WHERE biblionumber = ?
788 AND borrowernumber = ?
790 $sth = $dbh->prepare($query);
791 $sth->execute( $biblio, $borr );
793 # now fix the priority on the others....
794 _FixPriority( $priority, $biblio );
802 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
806 Change a hold request's priority or cancel it.
808 C<$rank> specifies the effect of the change. If C<$rank>
809 is 'W' or 'n', nothing happens. This corresponds to leaving a
810 request alone when changing its priority in the holds queue
813 If C<$rank> is 'del', the hold request is cancelled.
815 If C<$rank> is an integer greater than zero, the priority of
816 the request is set to that value. Since priority != 0 means
817 that the item is not waiting on the hold shelf, setting the
818 priority to a non-zero value also sets the request's found
819 status and waiting date to NULL.
821 The optional C<$itemnumber> parameter is used only when
822 C<$rank> is a non-zero integer; if supplied, the itemnumber
823 of the hold request is set accordingly; if omitted, the itemnumber
826 FIXME: Note that the forgoing can have the effect of causing
827 item-level hold requests to turn into title-level requests. This
828 will be fixed once reserves has separate columns for requested
829 itemnumber and supplying itemnumber.
834 #subroutine to update a reserve
835 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
836 return if $rank eq "W";
837 return if $rank eq "n";
838 my $dbh = C4::Context->dbh;
839 if ( $rank eq "del" ) {
842 SET cancellationdate=now()
843 WHERE biblionumber = ?
844 AND borrowernumber = ?
846 my $sth = $dbh->prepare($query);
847 $sth->execute( $biblio, $borrower );
850 INSERT INTO old_reserves
853 WHERE biblionumber = ?
854 AND borrowernumber = ?
856 $sth = $dbh->prepare($query);
857 $sth->execute( $biblio, $borrower );
860 WHERE biblionumber = ?
861 AND borrowernumber = ?
863 $sth = $dbh->prepare($query);
864 $sth->execute( $biblio, $borrower );
867 elsif ($rank =~ /^\d+/ and $rank > 0) {
869 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
870 WHERE biblionumber = ?
871 AND borrowernumber = ?
873 my $sth = $dbh->prepare($query);
874 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
876 _FixPriority( $biblio, $borrower, $rank);
882 &ModReserveFill($reserve);
884 Fill a reserve. If I understand this correctly, this means that the
885 reserved book has been found and given to the patron who reserved it.
887 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
888 whose keys are fields from the reserves table in the Koha database.
894 my $dbh = C4::Context->dbh;
895 # fill in a reserve record....
896 my $biblionumber = $res->{'biblionumber'};
897 my $borrowernumber = $res->{'borrowernumber'};
898 my $resdate = $res->{'reservedate'};
900 # get the priority on this record....
902 my $query = "SELECT priority
904 WHERE biblionumber = ?
905 AND borrowernumber = ?
906 AND reservedate = ?";
907 my $sth = $dbh->prepare($query);
908 $sth->execute( $biblionumber, $borrowernumber, $resdate );
909 ($priority) = $sth->fetchrow_array;
912 # update the database...
913 $query = "UPDATE reserves
916 WHERE biblionumber = ?
918 AND borrowernumber = ?
920 $sth = $dbh->prepare($query);
921 $sth->execute( $biblionumber, $resdate, $borrowernumber );
924 # move to old_reserves
925 $query = "INSERT INTO old_reserves
926 SELECT * FROM reserves
927 WHERE biblionumber = ?
929 AND borrowernumber = ?
931 $sth = $dbh->prepare($query);
932 $sth->execute( $biblionumber, $resdate, $borrowernumber );
933 $query = "DELETE FROM reserves
934 WHERE biblionumber = ?
936 AND borrowernumber = ?
938 $sth = $dbh->prepare($query);
939 $sth->execute( $biblionumber, $resdate, $borrowernumber );
941 # now fix the priority on the others (if the priority wasn't
942 # already sorted!)....
943 unless ( $priority == 0 ) {
944 _FixPriority( $priority, $biblionumber );
948 =item ModReserveStatus
950 &ModReserveStatus($itemnumber, $newstatus);
952 Update the reserve status for the active (priority=0) reserve.
954 $itemnumber is the itemnumber the reserve is on
956 $newstatus is the new status.
960 sub ModReserveStatus {
962 #first : check if we have a reservation for this item .
963 my ($itemnumber, $newstatus) = @_;
964 my $dbh = C4::Context->dbh;
965 my $query = " UPDATE reserves
966 SET found=?,waitingdate = now()
971 my $sth_set = $dbh->prepare($query);
972 $sth_set->execute( $newstatus, $itemnumber );
975 =item ModReserveAffect
977 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
979 This function affect an item and a status for a given reserve
980 The itemnumber parameter is used to find the biblionumber.
981 with the biblionumber & the borrowernumber, we can affect the itemnumber
982 to the correct reserve.
984 if $transferToDo is not set, then the status is set to "Waiting" as well.
985 otherwise, a transfer is on the way, and the end of the transfer will
986 take care of the waiting status
989 sub ModReserveAffect {
990 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
991 my $dbh = C4::Context->dbh;
993 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
994 # attached to $itemnumber
995 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
996 $sth->execute($itemnumber);
997 my ($biblionumber) = $sth->fetchrow;
998 # If we affect a reserve that has to be transfered, don't set to Waiting
1000 if ($transferToDo) {
1005 WHERE borrowernumber = ?
1006 AND biblionumber = ?
1010 # affect the reserve to Waiting as well.
1017 WHERE borrowernumber = ?
1018 AND biblionumber = ?
1021 $sth = $dbh->prepare($query);
1022 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1025 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo );
1030 =item ModReserveCancelAll
1032 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1034 function to cancel reserv,check other reserves, and transfer document if it's necessary
1038 sub ModReserveCancelAll {
1041 my ( $itemnumber, $borrowernumber ) = @_;
1043 #step 1 : cancel the reservation
1044 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1046 #step 2 launch the subroutine of the others reserves
1047 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1049 return ( $messages, $nextreservinfo );
1052 =item ModReserveMinusPriority
1054 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1056 Reduce the values of queuded list
1060 sub ModReserveMinusPriority {
1061 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1063 #first step update the value of the first person on reserv
1064 my $dbh = C4::Context->dbh;
1067 SET priority = 0 , itemnumber = ?
1068 WHERE borrowernumber=?
1071 my $sth_upd = $dbh->prepare($query);
1072 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1073 # second step update all others reservs
1074 _FixPriority($biblionumber, $borrowernumber, '0');
1077 =item GetReserveInfo
1079 &GetReserveInfo($borrowernumber,$biblionumber);
1081 Get item and borrower details for a current hold.
1082 Current implementation this query should have a single result.
1085 sub GetReserveInfo {
1086 my ( $borrowernumber, $biblionumber ) = @_;
1087 my $dbh = C4::Context->dbh;
1088 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1089 reserves.biblionumber, reserves.branchcode,
1090 notificationdate, reminderdate, priority, found,
1091 firstname, surname, phone,
1092 email, address, address2,
1093 cardnumber, city, zipcode,
1094 biblio.title, biblio.author,
1095 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1097 FROM reserves left join items
1098 ON items.itemnumber=reserves.itemnumber ,
1101 reserves.borrowernumber=? &&
1102 reserves.biblionumber=? &&
1103 reserves.borrowernumber=borrowers.borrowernumber &&
1104 reserves.biblionumber=biblio.biblionumber ";
1105 my $sth = $dbh->prepare($strsth);
1106 $sth->execute($borrowernumber,$biblionumber);
1108 my $data = $sth->fetchrow_hashref;
1113 =item IsAvailableForItemLevelRequest
1117 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1121 Checks whether a given item record is available for an
1122 item-level hold request. An item is available if
1124 * it is not lost AND
1125 * it is not damaged AND
1126 * it is not withdrawn AND
1127 * does not have a not for loan value > 0
1129 Whether or not the item is currently on loan is
1130 also checked - if the AllowOnShelfHolds system preference
1131 is ON, an item can be requested even if it is currently
1132 on loan to somebody else. If the system preference
1133 is OFF, an item that is currently checked out cannot
1134 be the target of an item-level hold request.
1136 Note that IsAvailableForItemLevelRequest() does not
1137 check if the staff operator is authorized to place
1138 a request on the item - in particular,
1139 this routine does not check IndependantBranches
1140 and canreservefromotherbranches.
1144 sub IsAvailableForItemLevelRequest {
1145 my $itemnumber = shift;
1147 my $item = GetItem($itemnumber);
1149 # must check the notforloan setting of the itemtype
1150 # FIXME - a lot of places in the code do this
1151 # or something similar - need to be
1153 my $dbh = C4::Context->dbh;
1154 my $notforloan_query;
1155 if (C4::Context->preference('item-level_itypes')) {
1156 $notforloan_query = "SELECT itemtypes.notforloan
1158 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1159 WHERE itemnumber = ?";
1161 $notforloan_query = "SELECT itemtypes.notforloan
1163 JOIN biblioitems USING (biblioitemnumber)
1164 JOIN itemtypes USING (itemtype)
1165 WHERE itemnumber = ?";
1167 my $sth = $dbh->prepare($notforloan_query);
1168 $sth->execute($itemnumber);
1169 my $notforloan_per_itemtype = 0;
1170 if (my ($notforloan) = $sth->fetchrow_array) {
1171 $notforloan_per_itemtype = 1 if $notforloan;
1174 my $available_per_item = 1;
1175 $available_per_item = 0 if $item->{itemlost} or
1176 ( $item->{notforloan} > 0 ) or
1177 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1178 $item->{wthdrawn} or
1179 $notforloan_per_itemtype;
1181 if (C4::Context->preference('AllowOnShelfHolds')) {
1182 return $available_per_item;
1184 return ($available_per_item and $item->{onloan});
1190 &_FixPriority($biblio,$borrowernumber,$rank);
1192 Only used internally (so don't export it)
1193 Changed how this functions works #
1194 Now just gets an array of reserves in the rank order and updates them with
1195 the array index (+1 as array starts from 0)
1196 and if $rank is supplied will splice item from the array and splice it back in again
1197 in new priority rank
1202 my ( $biblio, $borrowernumber, $rank ) = @_;
1203 my $dbh = C4::Context->dbh;
1204 if ( $rank eq "del" ) {
1205 CancelReserve( $biblio, undef, $borrowernumber );
1207 if ( $rank eq "W" || $rank eq "0" ) {
1209 # make sure priority for waiting items is 0
1213 WHERE biblionumber = ?
1214 AND borrowernumber = ?
1217 my $sth = $dbh->prepare($query);
1218 $sth->execute( $biblio, $borrowernumber );
1224 # FIXME adding a new security in returned elements for changing priority,
1225 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1226 # This is wrong a waiting reserve has W set
1227 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1229 SELECT borrowernumber, reservedate, constrainttype
1231 WHERE biblionumber = ?
1232 AND ((found <> 'W') or found is NULL)
1233 ORDER BY priority ASC
1235 my $sth = $dbh->prepare($query);
1236 $sth->execute($biblio);
1237 while ( my $line = $sth->fetchrow_hashref ) {
1238 push( @reservedates, $line );
1239 push( @priority, $line );
1242 # To find the matching index
1244 my $key = -1; # to allow for 0 to be a valid result
1245 for ( $i = 0 ; $i < @priority ; $i++ ) {
1246 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1247 $key = $i; # save the index
1252 # if index exists in array then move it to new position
1253 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1254 my $new_rank = $rank -
1255 1; # $new_rank is what you want the new index to be in the array
1256 my $moving_item = splice( @priority, $key, 1 );
1257 splice( @priority, $new_rank, 0, $moving_item );
1260 # now fix the priority on those that are left....
1264 WHERE biblionumber = ?
1265 AND borrowernumber = ?
1269 $sth = $dbh->prepare($query);
1270 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1273 $priority[$j]->{'borrowernumber'},
1274 $priority[$j]->{'reservedate'}
1280 =item _Findgroupreserve
1282 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1284 Looks for an item-specific match first, then for a title-level match, returning the
1285 first match found. If neither, then we look for a 3rd kind of match based on
1286 reserve constraints.
1288 TODO: add more explanation about reserve constraints
1290 C<&_Findgroupreserve> returns :
1291 C<@results> is an array of references-to-hash whose keys are mostly
1292 fields from the reserves table of the Koha database, plus
1293 C<biblioitemnumber>.
1297 sub _Findgroupreserve {
1298 my ( $bibitem, $biblio, $itemnumber ) = @_;
1299 my $dbh = C4::Context->dbh;
1301 # check for exact targetted match
1302 # This select is valid for both item_level and biblio_level
1303 my $item_level_target_query = qq/
1304 SELECT reserves.biblionumber AS biblionumber,
1305 reserves.borrowernumber AS borrowernumber,
1306 reserves.reservedate AS reservedate,
1307 reserves.branchcode AS branchcode,
1308 reserves.cancellationdate AS cancellationdate,
1309 reserves.found AS found,
1310 reserves.reservenotes AS reservenotes,
1311 reserves.priority AS priority,
1312 reserves.timestamp AS timestamp,
1313 biblioitems.biblioitemnumber AS biblioitemnumber,
1314 reserves.itemnumber AS itemnumber
1316 JOIN biblioitems USING (biblionumber)
1317 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1320 AND hold_fill_targets.itemnumber = ?
1323 my $sth = $dbh->prepare($item_level_target_query);
1324 $sth->execute($itemnumber);
1325 my $data = $sth->fetchall_arrayref({});
1326 return @$data if (@$data);
1328 # check for title-level targetted match
1329 my $title_level_target_query = qq/
1330 SELECT reserves.biblionumber AS biblionumber,
1331 reserves.borrowernumber AS borrowernumber,
1332 reserves.reservedate AS reservedate,
1333 reserves.branchcode AS branchcode,
1334 reserves.cancellationdate AS cancellationdate,
1335 reserves.found AS found,
1336 reserves.reservenotes AS reservenotes,
1337 reserves.priority AS priority,
1338 reserves.timestamp AS timestamp,
1339 biblioitems.biblioitemnumber AS biblioitemnumber,
1340 reserves.itemnumber AS itemnumber
1342 JOIN biblioitems USING (biblionumber)
1343 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1346 AND item_level_request = 0
1347 AND hold_fill_targets.itemnumber = ?
1349 $sth = $dbh->prepare($title_level_target_query);
1350 $sth->execute($itemnumber);
1351 $data = $sth->fetchall_arrayref({});
1352 return @$data if (@$data);
1355 SELECT reserves.biblionumber AS biblionumber,
1356 reserves.borrowernumber AS borrowernumber,
1357 reserves.reservedate AS reservedate,
1358 reserves.branchcode AS branchcode,
1359 reserves.cancellationdate AS cancellationdate,
1360 reserves.found AS found,
1361 reserves.reservenotes AS reservenotes,
1362 reserves.priority AS priority,
1363 reserves.timestamp AS timestamp,
1364 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1365 reserves.itemnumber AS itemnumber
1367 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1368 WHERE reserves.biblionumber = ?
1369 AND ( ( reserveconstraints.biblioitemnumber = ?
1370 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1371 AND reserves.reservedate = reserveconstraints.reservedate )
1372 OR reserves.constrainttype='a' )
1373 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1375 $sth = $dbh->prepare($query);
1376 $sth->execute( $biblio, $bibitem, $itemnumber );
1377 $data = $sth->fetchall_arrayref({});
1378 return @$data if (@$data);
1382 =item _koha_notify_reserve
1386 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1390 Sends a notification to the patron that their hold has been filled (through
1391 ModReserveAffect, _not_ ModReserveFill)
1395 sub _koha_notify_reserve {
1396 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1398 my $dbh = C4::Context->dbh;
1399 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1401 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1403 my $sth = $dbh->prepare("
1406 WHERE borrowernumber = ?
1407 AND biblionumber = ?
1409 $sth->execute( $borrowernumber, $biblionumber );
1410 my $reserve = $sth->fetchrow_hashref;
1411 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1413 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1415 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1417 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1418 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1419 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1420 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1422 if ( $reserve->{'itemnumber'} ) {
1423 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1425 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1427 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1428 # aka, 'email' in ->{'transports'}
1429 C4::Letters::EnqueueLetter(
1430 { letter => $letter,
1431 borrowernumber => $borrowernumber,
1432 message_transport_type => 'email',
1433 from_address => $admin_email_address,
1438 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1439 C4::Letters::EnqueueLetter(
1440 { letter => $letter,
1441 borrowernumber => $borrowernumber,
1442 message_transport_type => 'sms',
1452 Koha Developement team <info@koha.org>