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 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 my $library_name = C4::Context->preference("LibraryName");
40 C4::Reserves - Koha functions for dealing with reservation.
48 this modules provides somes functions to deal with reservations.
50 Reserves are stored in reserves table.
51 The following columns contains important values :
52 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
53 =0 : then the reserve is being dealed
54 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
55 W(aiting) : the reserve has an itemnumber affected, and is on the way
56 F(inished) : the reserve has been completed, and is done
57 - itemnumber : empty : the reserve is still unaffected to an item
58 filled: the reserve is attached to an item
59 The complete workflow is :
60 ==== 1st use case ====
61 patron request a document, 1st available : P >0, F=NULL, I=NULL
62 a library having it run "transfertodo", and clic on the list
63 if there is no transfer to do, the reserve waiting
64 patron can pick it up P =0, F=W, I=filled
65 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
66 The pickup library recieve the book, it check in P =0, F=W, I=filled
67 The patron borrow the book P =0, F=F, I=filled
69 ==== 2nd use case ====
70 patron requests a document, a given item,
71 If pickup is holding branch P =0, F=W, I=filled
72 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
73 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
74 The patron borrow the book P =0, F=F, I=filled
83 # set the version for version checking
90 &GetReservesFromItemnumber
91 &GetReservesFromBiblionumber
92 &GetReservesFromBorrowernumber
106 &ModReserveMinusPriority
111 &IsAvailableForItemLevelRequest
117 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
123 $branch, $borrowernumber, $biblionumber,
124 $constraint, $bibitems, $priority, $notes,
125 $title, $checkitem, $found
128 GetReserveFee($borrowernumber, $biblionumber, $constraint,
130 my $dbh = C4::Context->dbh;
131 my $const = lc substr( $constraint, 0, 1 );
132 my @datearr = localtime(time);
134 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
137 # If the reserv had the waiting status, we had the value of the resdate
138 if ( $found eq 'W' ) {
139 $waitingdate = $resdate;
143 # updates take place here
145 my $nextacctno = &getnextacctno( $borrowernumber );
147 INSERT INTO accountlines
148 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
150 (?,?,now(),?,?,'Res',?)
152 my $usth = $dbh->prepare($query);
153 $usth->execute( $borrowernumber, $nextacctno, $fee,
154 "Reserve Charge - $title", $fee );
161 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
162 priority,reservenotes,itemnumber,found,waitingdate)
167 my $sth = $dbh->prepare($query);
169 $borrowernumber, $biblionumber, $resdate, $branch,
170 $const, $priority, $notes, $checkitem,
176 if ( ( $const eq "o" ) || ( $const eq "e" ) ) {
177 my $numitems = @$bibitems;
179 while ( $i < $numitems ) {
180 my $biblioitem = @$bibitems[$i];
182 INSERT INTO reserveconstraints
183 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
187 my $sth = $dbh->prepare("");
188 $sth->execute( $borrowernumber, $biblionumber, $resdate,
197 =item GetReservesFromBiblionumber
199 @borrowerreserv=&GetReserves($biblionumber,$itemnumber,$borrowernumber);
201 this function get the list of reservation for an C<$biblionumber>, C<$itemnumber> or C<$borrowernumber>
203 Only 1 argument has to be passed.
207 sub GetReservesFromBiblionumber {
208 my ( $biblionumber, $itemnumber, $borrowernumber ) = @_;
209 my $dbh = C4::Context->dbh;
211 # Find the desired items in the reserves
214 timestamp AS rtimestamp,
224 WHERE biblionumber = ?
226 my $sth = $dbh->prepare($query);
227 $sth->execute($biblionumber);
230 while ( my $data = $sth->fetchrow_hashref ) {
232 # FIXME - What is this if-statement doing? How do constraints work?
233 if ( $data->{constrainttype} eq 'o' ) {
235 SELECT biblioitemnumber
236 FROM reserveconstraints
237 WHERE biblionumber = ?
238 AND borrowernumber = ?
241 my $csth = $dbh->prepare($query);
242 $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
243 $data->{reservedate}, );
246 while ( my $bibitemnos = $csth->fetchrow_array ) {
247 push( @bibitemno, $bibitemnos );
249 my $count = @bibitemno;
251 # if we have two or more different specific itemtypes
252 # reserved by same person on same day
255 $bdata = GetBiblioItemData( $bibitemno[$i] );
260 # Look up the book we just found.
261 $bdata = GetBiblioItemData( $bibitemno[0] );
265 # Add the results of this latest search to the current
267 # FIXME - An 'each' would probably be more efficient.
268 foreach my $key ( keys %$bdata ) {
269 $data->{$key} = $bdata->{$key};
272 push @results, $data;
275 return ( $#results + 1, \@results );
278 =item GetReservesFromItemnumber
280 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
282 TODO :: Description here
286 sub GetReservesFromItemnumber {
287 my ( $itemnumber ) = @_;
288 my $dbh = C4::Context->dbh;
290 SELECT reservedate,borrowernumber,branchcode
294 my $sth_res = $dbh->prepare($query);
295 $sth_res->execute($itemnumber);
296 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
297 return ( $reservedate, $borrowernumber, $branchcode );
300 =item GetReservesFromBorrowernumber
302 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
308 sub GetReservesFromBorrowernumber {
309 my ( $borrowernumber, $status ) = @_;
310 my $dbh = C4::Context->dbh;
313 $sth = $dbh->prepare("
316 WHERE borrowernumber=?
320 $sth->execute($borrowernumber,$status);
322 $sth = $dbh->prepare("
325 WHERE borrowernumber=?
328 $sth->execute($borrowernumber);
330 my $data = $sth->fetchall_arrayref({});
333 #-------------------------------------------------------------------------------------
335 =item GetReserveCount
337 $number = &GetReserveCount($borrowernumber);
339 this function returns the number of reservation for a borrower given on input arg.
343 sub GetReserveCount {
344 my ($borrowernumber) = @_;
346 my $dbh = C4::Context->dbh;
349 SELECT COUNT(*) AS counter
351 WHERE borrowernumber = ?
353 my $sth = $dbh->prepare($query);
354 $sth->execute($borrowernumber);
355 my $row = $sth->fetchrow_hashref;
358 return $row->{counter};
361 =item GetOtherReserves
363 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
365 Check queued list of this document and check if this document must be transfered
369 sub GetOtherReserves {
370 my ($itemnumber) = @_;
373 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
374 if ($checkreserves) {
375 my $iteminfo = GetItem($itemnumber);
376 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
377 $messages->{'transfert'} = $checkreserves->{'branchcode'};
378 #minus priorities of others reservs
379 ModReserveMinusPriority(
381 $checkreserves->{'borrowernumber'},
382 $iteminfo->{'biblionumber'}
385 #launch the subroutine dotransfer
386 C4::Circulation::ModItemTransfer(
388 $iteminfo->{'holdingbranch'},
389 $checkreserves->{'branchcode'}
394 #step 2b : case of a reservation on the same branch, set the waiting status
396 $messages->{'waiting'} = 1;
397 ModReserveMinusPriority(
399 $checkreserves->{'borrowernumber'},
400 $iteminfo->{'biblionumber'}
402 ModReserveStatus($itemnumber,'W');
405 $nextreservinfo = $checkreserves->{'borrowernumber'};
408 return ( $messages, $nextreservinfo );
413 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
415 Calculate the fee for a reserve
420 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
423 my $dbh = C4::Context->dbh;
424 my $const = lc substr( $constraint, 0, 1 );
426 SELECT * FROM borrowers
427 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
428 WHERE borrowernumber = ?
430 my $sth = $dbh->prepare($query);
431 $sth->execute($borrowernumber);
432 my $data = $sth->fetchrow_hashref;
434 my $fee = $data->{'reservefee'};
435 my $cntitems = @- > $bibitems;
439 # check for items on issue
440 # first find biblioitem records
442 my $sth1 = $dbh->prepare(
443 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
444 WHERE (biblio.biblionumber = ?)"
446 $sth1->execute($biblionumber);
447 while ( my $data1 = $sth1->fetchrow_hashref ) {
448 if ( $const eq "a" ) {
449 push @biblioitems, $data1;
454 while ( $x < $cntitems ) {
455 if ( @$bibitems->{'biblioitemnumber'} ==
456 $data->{'biblioitemnumber'} )
462 if ( $const eq 'o' ) {
464 push @biblioitems, $data1;
469 push @biblioitems, $data1;
475 my $cntitemsfound = @biblioitems;
479 while ( $x < $cntitemsfound ) {
480 my $bitdata = $biblioitems[$x];
481 my $sth2 = $dbh->prepare(
483 WHERE biblioitemnumber = ?"
485 $sth2->execute( $bitdata->{'biblioitemnumber'} );
486 while ( my $itdata = $sth2->fetchrow_hashref ) {
487 my $sth3 = $dbh->prepare(
488 "SELECT * FROM issues
489 WHERE itemnumber = ?"
491 $sth3->execute( $itdata->{'itemnumber'} );
492 if ( my $isdata = $sth3->fetchrow_hashref ) {
500 if ( $allissued == 0 ) {
502 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
503 $rsth->execute($biblionumber);
504 if ( my $rdata = $rsth->fetchrow_hashref ) {
514 =item GetReservesToBranch
516 @transreserv = GetReservesToBranch( $frombranch );
518 Get reserve list for a given branch
522 sub GetReservesToBranch {
523 my ( $frombranch ) = @_;
524 my $dbh = C4::Context->dbh;
525 my $sth = $dbh->prepare(
526 "SELECT borrowernumber,reservedate,itemnumber,timestamp
531 $sth->execute( $frombranch );
534 while ( my $data = $sth->fetchrow_hashref ) {
535 $transreserv[$i] = $data;
539 return (@transreserv);
542 =item GetReservesForBranch
544 @transreserv = GetReservesForBranch($frombranch);
548 sub GetReservesForBranch {
549 my ($frombranch) = @_;
550 my $dbh = C4::Context->dbh;
551 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
556 $query .= " AND branchcode=? ";
558 $query .= "ORDER BY waitingdate" ;
559 my $sth = $dbh->prepare($query);
561 $sth->execute($frombranch);
568 while ( my $data = $sth->fetchrow_hashref ) {
569 $transreserv[$i] = $data;
573 return (@transreserv);
578 ($status, $reserve) = &CheckReserves($itemnumber);
580 Find a book in the reserves.
582 C<$itemnumber> is the book's item number.
584 As I understand it, C<&CheckReserves> looks for the given item in the
585 reserves. If it is found, that's a match, and C<$status> is set to
588 Otherwise, it finds the most important item in the reserves with the
589 same biblio number as this book (I'm not clear on this) and returns it
590 with C<$status> set to C<Reserved>.
592 C<&CheckReserves> returns a two-element list:
594 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
596 C<$reserve> is the reserve item that matched. It is a
597 reference-to-hash whose keys are mostly the fields of the reserves
598 table in the Koha database.
603 my ( $item, $barcode ) = @_;
604 my $dbh = C4::Context->dbh;
607 my $qitem = $dbh->quote($item);
608 # Look up the item by itemnumber
610 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
612 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
613 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
614 WHERE itemnumber=$qitem
616 $sth = $dbh->prepare($query);
619 my $qbc = $dbh->quote($barcode);
620 # Look up the item by barcode
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 items.biblioitemnumber = biblioitems.biblioitemnumber
627 AND biblioitems.itemtype = itemtypes.itemtype
630 $sth = $dbh->prepare($query);
632 # FIXME - This function uses $item later on. Ought to set it here.
635 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item ) = $sth->fetchrow_array;
637 # if item is not for loan it cannot be reserved either.....
638 # execption to notforloan is where items.notforloan < 0 : This indicates the item is holdable.
639 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
641 # get the reserves...
642 # Find this item in the reserves
643 my @reserves = _Findgroupreserve( $bibitem, $biblio, $item );
644 my $count = scalar @reserves;
646 # $priority and $highest are used to find the most important item
647 # in the list returned by &_Findgroupreserve. (The lower $priority,
648 # the more important the item.)
649 # $highest is the most important item we've seen so far.
650 my $priority = 10000000;
653 foreach my $res (@reserves) {
654 # FIXME - $item might be undefined or empty: the caller
655 # might be searching by barcode.
656 if ( $res->{'itemnumber'} == $item && $res->{'priority'} == 0) {
658 return ( "Waiting", $res );
661 # See if this item is more important than what we've got
663 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
665 $priority = $res->{'priority'};
672 # If we get this far, then no exact match was found. Print the
673 # most important item on the list. I think this tells us who's
674 # next in line to get this book.
675 if ($highest) { # FIXME - $highest might be undefined
676 $highest->{'itemnumber'} = $item;
677 return ( "Reserved", $highest );
686 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
690 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
691 cancel, but not both: if both are given, C<&CancelReserve> does
694 C<$borrowernumber> is the borrower number of the patron on whose
695 behalf the book was reserved.
697 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
698 priorities of the other people who are waiting on the book.
703 my ( $biblio, $item, $borr ) = @_;
704 my $dbh = C4::Context->dbh;
705 if ( $item and $borr ) {
706 # removing a waiting reserve record....
707 # update the database...
710 SET cancellationdate = now(),
714 AND borrowernumber = ?
716 my $sth = $dbh->prepare($query);
717 $sth->execute( $item, $borr );
720 INSERT INTO old_reserves
721 SELECT * FROM reserves
723 AND borrowernumber = ?
725 $sth = $dbh->prepare($query);
726 $sth->execute( $item, $borr );
730 AND borrowernumber = ?
732 $sth = $dbh->prepare($query);
733 $sth->execute( $item, $borr );
736 # removing a reserve record....
737 # get the prioritiy on this record....
740 SELECT priority FROM reserves
741 WHERE biblionumber = ?
742 AND borrowernumber = ?
743 AND cancellationdate IS NULL
744 AND itemnumber IS NULL
746 my $sth = $dbh->prepare($query);
747 $sth->execute( $biblio, $borr );
748 ($priority) = $sth->fetchrow_array;
752 SET cancellationdate = now(),
755 WHERE biblionumber = ?
756 AND borrowernumber = ?
759 # update the database, removing the record...
760 $sth = $dbh->prepare($query);
761 $sth->execute( $biblio, $borr );
765 INSERT INTO old_reserves
766 SELECT * FROM reserves
767 WHERE biblionumber = ?
768 AND borrowernumber = ?
770 $sth = $dbh->prepare($query);
771 $sth->execute( $biblio, $borr );
775 WHERE biblionumber = ?
776 AND borrowernumber = ?
778 $sth = $dbh->prepare($query);
779 $sth->execute( $biblio, $borr );
781 # now fix the priority on the others....
782 _FixPriority( $priority, $biblio );
790 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
794 Change a hold request's priority or cancel it.
796 C<$rank> specifies the effect of the change. If C<$rank>
797 is 'W' or 'n', nothing happens. This corresponds to leaving a
798 request alone when changing its priority in the holds queue
801 If C<$rank> is 'del', the hold request is cancelled.
803 If C<$rank> is an integer greater than zero, the priority of
804 the request is set to that value. Since priority != 0 means
805 that the item is not waiting on the hold shelf, setting the
806 priority to a non-zero value also sets the request's found
807 status and waiting date to NULL.
809 The optional C<$itemnumber> parameter is used only when
810 C<$rank> is a non-zero integer; if supplied, the itemnumber
811 of the hold request is set accordingly; if omitted, the itemnumber
814 FIXME: Note that the forgoing can have the effect of causing
815 item-level hold requests to turn into title-level requests. This
816 will be fixed once reserves has separate columns for requested
817 itemnumber and supplying itemnumber.
822 #subroutine to update a reserve
823 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
824 return if $rank eq "W";
825 return if $rank eq "n";
826 my $dbh = C4::Context->dbh;
827 if ( $rank eq "del" ) {
830 SET cancellationdate=now()
831 WHERE biblionumber = ?
832 AND borrowernumber = ?
834 my $sth = $dbh->prepare($query);
835 $sth->execute( $biblio, $borrower );
838 INSERT INTO old_reserves
841 WHERE biblionumber = ?
842 AND borrowernumber = ?
844 $sth = $dbh->prepare($query);
845 $sth->execute( $biblio, $borrower );
848 WHERE biblionumber = ?
849 AND borrowernumber = ?
851 $sth = $dbh->prepare($query);
852 $sth->execute( $biblio, $borrower );
855 elsif ($rank =~ /^\d+/ and $rank > 0) {
857 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
858 WHERE biblionumber = ?
859 AND borrowernumber = ?
861 my $sth = $dbh->prepare($query);
862 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
864 _FixPriority( $biblio, $borrower, $rank);
870 &ModReserveFill($reserve);
872 Fill a reserve. If I understand this correctly, this means that the
873 reserved book has been found and given to the patron who reserved it.
875 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
876 whose keys are fields from the reserves table in the Koha database.
882 my $dbh = C4::Context->dbh;
883 # fill in a reserve record....
884 my $biblionumber = $res->{'biblionumber'};
885 my $borrowernumber = $res->{'borrowernumber'};
886 my $resdate = $res->{'reservedate'};
888 # get the priority on this record....
890 my $query = "SELECT priority
892 WHERE biblionumber = ?
893 AND borrowernumber = ?
894 AND reservedate = ?";
895 my $sth = $dbh->prepare($query);
896 $sth->execute( $biblionumber, $borrowernumber, $resdate );
897 ($priority) = $sth->fetchrow_array;
900 # update the database...
901 $query = "UPDATE reserves
904 WHERE biblionumber = ?
906 AND borrowernumber = ?
908 $sth = $dbh->prepare($query);
909 $sth->execute( $biblionumber, $resdate, $borrowernumber );
912 # move to old_reserves
913 $query = "INSERT INTO old_reserves
914 SELECT * FROM reserves
915 WHERE biblionumber = ?
917 AND borrowernumber = ?
919 $sth = $dbh->prepare($query);
920 $sth->execute( $biblionumber, $resdate, $borrowernumber );
921 $query = "DELETE FROM reserves
922 WHERE biblionumber = ?
924 AND borrowernumber = ?
926 $sth = $dbh->prepare($query);
927 $sth->execute( $biblionumber, $resdate, $borrowernumber );
929 # now fix the priority on the others (if the priority wasn't
930 # already sorted!)....
931 unless ( $priority == 0 ) {
932 _FixPriority( $priority, $biblionumber );
936 =item ModReserveStatus
938 &ModReserveStatus($itemnumber, $newstatus);
940 Update the reserve status for the active (priority=0) reserve.
942 $itemnumber is the itemnumber the reserve is on
944 $newstatus is the new status.
948 sub ModReserveStatus {
950 #first : check if we have a reservation for this item .
951 my ($itemnumber, $newstatus) = @_;
952 my $dbh = C4::Context->dbh;
953 my $query = " UPDATE reserves
954 SET found=?,waitingdate = now()
959 my $sth_set = $dbh->prepare($query);
960 $sth_set->execute( $newstatus, $itemnumber );
964 =item ModReserveAffect
966 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
968 This function affect an item and a status for a given reserve
969 The itemnumber parameter is used to find the biblionumber.
970 with the biblionumber & the borrowernumber, we can affect the itemnumber
971 to the correct reserve.
973 if $transferToDo is not set, then the status is set to "Waiting" as well.
974 otherwise, a transfer is on the way, and the end of the transfer will
975 take care of the waiting status
978 sub ModReserveAffect {
979 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
980 my $dbh = C4::Context->dbh;
982 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
983 # attached to $itemnumber
984 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
985 $sth->execute($itemnumber);
986 my ($biblionumber) = $sth->fetchrow;
987 # If we affect a reserve that has to be transfered, don't set to Waiting
994 WHERE borrowernumber = ?
999 # affect the reserve to Waiting as well.
1006 WHERE borrowernumber = ?
1007 AND biblionumber = ?
1010 $sth = $dbh->prepare($query);
1011 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1016 =item ModReserveCancelAll
1018 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1020 function to cancel reserv,check other reserves, and transfer document if it's necessary
1024 sub ModReserveCancelAll {
1027 my ( $itemnumber, $borrowernumber ) = @_;
1029 #step 1 : cancel the reservation
1030 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1032 #step 2 launch the subroutine of the others reserves
1033 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1035 return ( $messages, $nextreservinfo );
1038 =item ModReserveMinusPriority
1040 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1042 Reduce the values of queuded list
1046 sub ModReserveMinusPriority {
1047 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1049 #first step update the value of the first person on reserv
1050 my $dbh = C4::Context->dbh;
1053 SET priority = 0 , itemnumber = ?
1054 WHERE borrowernumber=?
1057 my $sth_upd = $dbh->prepare($query);
1058 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1060 # second step update all others reservs
1061 _FixPriority($biblionumber, $borrowernumber, '0');
1064 =item GetReserveInfo
1066 &GetReserveInfo($borrowernumber,$biblionumber);
1068 Get item and borrower details for a current hold.
1069 Current implementation this query should have a single result.
1072 sub GetReserveInfo {
1073 my ( $borrowernumber, $biblionumber ) = @_;
1074 my $dbh = C4::Context->dbh;
1075 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1076 reserves.biblionumber, reserves.branchcode,
1077 notificationdate, reminderdate, priority, found,
1078 firstname, surname, phone,
1079 email, address, address2,
1080 cardnumber, city, zipcode,
1081 biblio.title, biblio.author,
1082 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1084 FROM reserves left join items
1085 ON items.itemnumber=reserves.itemnumber ,
1088 reserves.borrowernumber=? &&
1089 reserves.biblionumber=? &&
1090 reserves.borrowernumber=borrowers.borrowernumber &&
1091 reserves.biblionumber=biblio.biblionumber ";
1092 my $sth = $dbh->prepare($strsth);
1093 $sth->execute($borrowernumber,$biblionumber);
1095 my $data = $sth->fetchrow_hashref;
1100 =item IsAvailableForItemLevelRequest
1104 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1108 Checks whether a given item record is available for an
1109 item-level hold request. An item is available if
1111 * it is not lost AND
1112 * it is not damaged AND
1113 * it is not withdrawn AND
1114 * does not have a not for loan value > 0
1116 Whether or not the item is currently on loan is
1117 also checked - if the AllowOnShelfHolds system preference
1118 is ON, an item can be requested even if it is currently
1119 on loan to somebody else. If the system preference
1120 is OFF, an item that is currently checked out cannot
1121 be the target of an item-level hold request.
1123 Note that IsAvailableForItemLevelRequest() does not
1124 check if the staff operator is authorized to place
1125 a request on the item - in particular,
1126 this routine does not check IndependantBranches
1127 and canreservefromotherbranches.
1131 sub IsAvailableForItemLevelRequest {
1132 my $itemnumber = shift;
1134 my $item = GetItem($itemnumber);
1136 # must check the notforloan setting of the itemtype
1137 # FIXME - a lot of places in the code do this
1138 # or something similar - need to be
1140 my $dbh = C4::Context->dbh;
1141 my $notforloan_query;
1142 if (C4::Context->preference('item-level_itypes')) {
1143 $notforloan_query = "SELECT itemtypes.notforloan
1145 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1146 WHERE itemnumber = ?";
1148 $notforloan_query = "SELECT itemtypes.notforloan
1150 JOIN biblioitems USING (biblioitemnumber)
1151 JOIN itemtypes USING (itemtype)
1152 WHERE itemnumber = ?";
1154 my $sth = $dbh->prepare($notforloan_query);
1155 $sth->execute($itemnumber);
1156 my $notforloan_per_itemtype = 0;
1157 if (my ($notforloan) = $sth->fetchrow_array) {
1158 $notforloan_per_itemtype = 1 if $notforloan;
1161 my $available_per_item = 1;
1162 $available_per_item = 0 if $item->{itemlost} or
1163 ( $item->{notforloan} > 0 ) or
1164 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1165 $item->{wthdrawn} or
1166 $notforloan_per_itemtype;
1168 if (C4::Context->preference('AllowOnShelfHolds')) {
1169 return $available_per_item;
1171 return ($available_per_item and $item->{onloan});
1177 &_FixPriority($biblio,$borrowernumber,$rank);
1179 Only used internally (so don't export it)
1180 Changed how this functions works #
1181 Now just gets an array of reserves in the rank order and updates them with
1182 the array index (+1 as array starts from 0)
1183 and if $rank is supplied will splice item from the array and splice it back in again
1184 in new priority rank
1189 my ( $biblio, $borrowernumber, $rank ) = @_;
1190 my $dbh = C4::Context->dbh;
1191 if ( $rank eq "del" ) {
1192 CancelReserve( $biblio, undef, $borrowernumber );
1194 if ( $rank eq "W" || $rank eq "0" ) {
1196 # make sure priority for waiting items is 0
1200 WHERE biblionumber = ?
1201 AND borrowernumber = ?
1204 my $sth = $dbh->prepare($query);
1205 $sth->execute( $biblio, $borrowernumber );
1211 # FIXME adding a new security in returned elements for changing priority,
1212 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1213 # This is wrong a waiting reserve has W set
1214 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1216 SELECT borrowernumber, reservedate, constrainttype
1218 WHERE biblionumber = ?
1219 AND ((found <> 'W') or found is NULL)
1220 ORDER BY priority ASC
1222 my $sth = $dbh->prepare($query);
1223 $sth->execute($biblio);
1224 while ( my $line = $sth->fetchrow_hashref ) {
1225 push( @reservedates, $line );
1226 push( @priority, $line );
1229 # To find the matching index
1231 my $key = -1; # to allow for 0 to be a valid result
1232 for ( $i = 0 ; $i < @priority ; $i++ ) {
1233 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1234 $key = $i; # save the index
1239 # if index exists in array then move it to new position
1240 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1241 my $new_rank = $rank -
1242 1; # $new_rank is what you want the new index to be in the array
1243 my $moving_item = splice( @priority, $key, 1 );
1244 splice( @priority, $new_rank, 0, $moving_item );
1247 # now fix the priority on those that are left....
1251 WHERE biblionumber = ?
1252 AND borrowernumber = ?
1256 $sth = $dbh->prepare($query);
1257 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1260 $priority[$j]->{'borrowernumber'},
1261 $priority[$j]->{'reservedate'}
1267 =item _Findgroupreserve
1269 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1272 I don't know what this does, because I don't understand how reserve
1273 constraints work. I think the idea is that you reserve a particular
1274 biblio, and the constraint allows you to restrict it to a given
1275 biblioitem (e.g., if you want to borrow the audio book edition of "The
1276 Prophet", rather than the first available publication).
1278 C<&_Findgroupreserve> returns :
1279 C<@results> is an array of references-to-hash whose keys are mostly
1280 fields from the reserves table of the Koha database, plus
1281 C<biblioitemnumber>.
1285 sub _Findgroupreserve {
1286 my ( $bibitem, $biblio, $itemnumber ) = @_;
1287 my $dbh = C4::Context->dbh;
1289 # check for exact targetted match
1290 my $item_level_target_query = qq/
1291 SELECT reserves.biblionumber AS biblionumber,
1292 reserves.borrowernumber AS borrowernumber,
1293 reserves.reservedate AS reservedate,
1294 reserves.branchcode AS branchcode,
1295 reserves.cancellationdate AS cancellationdate,
1296 reserves.found AS found,
1297 reserves.reservenotes AS reservenotes,
1298 reserves.priority AS priority,
1299 reserves.timestamp AS timestamp,
1300 biblioitems.biblioitemnumber AS biblioitemnumber,
1301 reserves.itemnumber AS itemnumber
1303 JOIN biblioitems USING (biblionumber)
1304 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1307 AND item_level_request = 1
1310 my $sth = $dbh->prepare($item_level_target_query);
1311 $sth->execute($itemnumber);
1313 if ( my $data = $sth->fetchrow_hashref ) {
1314 push( @results, $data );
1316 return @results if @results;
1318 # check for title-level targetted match
1319 my $title_level_target_query = qq/
1320 SELECT reserves.biblionumber AS biblionumber,
1321 reserves.borrowernumber AS borrowernumber,
1322 reserves.reservedate AS reservedate,
1323 reserves.branchcode AS branchcode,
1324 reserves.cancellationdate AS cancellationdate,
1325 reserves.found AS found,
1326 reserves.reservenotes AS reservenotes,
1327 reserves.priority AS priority,
1328 reserves.timestamp AS timestamp,
1329 biblioitems.biblioitemnumber AS biblioitemnumber,
1330 reserves.itemnumber AS itemnumber
1332 JOIN biblioitems USING (biblionumber)
1333 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1336 AND item_level_request = 0
1337 AND hold_fill_targets.itemnumber = ?
1339 $sth = $dbh->prepare($title_level_target_query);
1340 $sth->execute($itemnumber);
1342 if ( my $data = $sth->fetchrow_hashref ) {
1343 push( @results, $data );
1345 return @results if @results;
1348 SELECT reserves.biblionumber AS biblionumber,
1349 reserves.borrowernumber AS borrowernumber,
1350 reserves.reservedate AS reservedate,
1351 reserves.branchcode AS branchcode,
1352 reserves.cancellationdate AS cancellationdate,
1353 reserves.found AS found,
1354 reserves.reservenotes AS reservenotes,
1355 reserves.priority AS priority,
1356 reserves.timestamp AS timestamp,
1357 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1358 reserves.itemnumber AS itemnumber
1360 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1361 WHERE reserves.biblionumber = ?
1362 AND ( ( reserveconstraints.biblioitemnumber = ?
1363 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1364 AND reserves.reservedate =reserveconstraints.reservedate )
1365 OR reserves.constrainttype='a' )
1366 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1368 $sth = $dbh->prepare($query);
1369 $sth->execute( $biblio, $bibitem, $itemnumber );
1371 while ( my $data = $sth->fetchrow_hashref ) {
1372 push( @results, $data );
1382 Koha Developement team <info@koha.org>