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
105 &ModReserveMinusPriority
114 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
120 $branch, $borrowernumber, $biblionumber,
121 $constraint, $bibitems, $priority, $notes,
122 $title, $checkitem, $found
125 GetReserveFee($borrowernumber, $biblionumber, $constraint,
127 my $dbh = C4::Context->dbh;
128 my $const = lc substr( $constraint, 0, 1 );
129 my @datearr = localtime(time);
131 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
134 # If the reserv had the waiting status, we had the value of the resdate
135 if ( $found eq 'W' ) {
136 $waitingdate = $resdate;
140 # updates take place here
142 my $nextacctno = &getnextacctno( $borrowernumber );
144 INSERT INTO accountlines
145 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
147 (?,?,now(),?,?,'Res',?)
149 my $usth = $dbh->prepare($query);
150 $usth->execute( $borrowernumber, $nextacctno, $fee,
151 "Reserve Charge - $title", $fee );
158 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
159 priority,reservenotes,itemnumber,found,waitingdate)
164 my $sth = $dbh->prepare($query);
166 $borrowernumber, $biblionumber, $resdate, $branch,
167 $const, $priority, $notes, $checkitem,
173 if ( ( $const eq "o" ) || ( $const eq "e" ) ) {
174 my $numitems = @$bibitems;
176 while ( $i < $numitems ) {
177 my $biblioitem = @$bibitems[$i];
179 INSERT INTO reserveconstraints
180 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
184 my $sth = $dbh->prepare("");
185 $sth->execute( $borrowernumber, $biblionumber, $resdate,
194 =item GetReservesFromBiblionumber
196 @borrowerreserv=&GetReserves($biblionumber,$itemnumber,$borrowernumber);
198 this function get the list of reservation for an C<$biblionumber>, C<$itemnumber> or C<$borrowernumber>
200 Only 1 argument has to be passed.
204 sub GetReservesFromBiblionumber {
205 my ( $biblionumber, $itemnumber, $borrowernumber ) = @_;
206 my $dbh = C4::Context->dbh;
208 # Find the desired items in the reserves
211 timestamp AS rtimestamp,
221 WHERE biblionumber = ?
223 my $sth = $dbh->prepare($query);
224 $sth->execute($biblionumber);
227 while ( my $data = $sth->fetchrow_hashref ) {
229 # FIXME - What is this if-statement doing? How do constraints work?
230 if ( $data->{constrainttype} eq 'o' ) {
232 SELECT biblioitemnumber
233 FROM reserveconstraints
234 WHERE biblionumber = ?
235 AND borrowernumber = ?
238 my $csth = $dbh->prepare($query);
239 $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
240 $data->{reservedate}, );
243 while ( my $bibitemnos = $csth->fetchrow_array ) {
244 push( @bibitemno, $bibitemnos );
246 my $count = @bibitemno;
248 # if we have two or more different specific itemtypes
249 # reserved by same person on same day
252 $bdata = GetBiblioItemData( $bibitemno[$i] );
257 # Look up the book we just found.
258 $bdata = GetBiblioItemData( $bibitemno[0] );
262 # Add the results of this latest search to the current
264 # FIXME - An 'each' would probably be more efficient.
265 foreach my $key ( keys %$bdata ) {
266 $data->{$key} = $bdata->{$key};
269 push @results, $data;
272 return ( $#results + 1, \@results );
275 =item GetReservesFromItemnumber
277 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
279 TODO :: Description here
283 sub GetReservesFromItemnumber {
284 my ( $itemnumber ) = @_;
285 my $dbh = C4::Context->dbh;
287 SELECT reservedate,borrowernumber,branchcode
291 my $sth_res = $dbh->prepare($query);
292 $sth_res->execute($itemnumber);
293 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
294 return ( $reservedate, $borrowernumber, $branchcode );
297 =item GetReservesFromBorrowernumber
299 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
305 sub GetReservesFromBorrowernumber {
306 my ( $borrowernumber, $status ) = @_;
307 my $dbh = C4::Context->dbh;
310 $sth = $dbh->prepare("
313 WHERE borrowernumber=?
317 $sth->execute($borrowernumber,$status);
319 $sth = $dbh->prepare("
322 WHERE borrowernumber=?
325 $sth->execute($borrowernumber);
327 my $data = $sth->fetchall_arrayref({});
330 #-------------------------------------------------------------------------------------
332 =item GetReserveCount
334 $number = &GetReserveCount($borrowernumber);
336 this function returns the number of reservation for a borrower given on input arg.
340 sub GetReserveCount {
341 my ($borrowernumber) = @_;
343 my $dbh = C4::Context->dbh;
346 SELECT COUNT(*) AS counter
348 WHERE borrowernumber = ?
350 my $sth = $dbh->prepare($query);
351 $sth->execute($borrowernumber);
352 my $row = $sth->fetchrow_hashref;
355 return $row->{counter};
358 =item GetOtherReserves
360 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
362 Check queued list of this document and check if this document must be transfered
366 sub GetOtherReserves {
367 my ($itemnumber) = @_;
370 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
371 if ($checkreserves) {
372 my $iteminfo = GetItem($itemnumber);
373 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
374 $messages->{'transfert'} = $checkreserves->{'branchcode'};
375 #minus priorities of others reservs
376 ModReserveMinusPriority(
378 $checkreserves->{'borrowernumber'},
379 $iteminfo->{'biblionumber'}
382 #launch the subroutine dotransfer
383 C4::Circulation::ModItemTransfer(
385 $iteminfo->{'holdingbranch'},
386 $checkreserves->{'branchcode'}
391 #step 2b : case of a reservation on the same branch, set the waiting status
393 $messages->{'waiting'} = 1;
394 ModReserveMinusPriority(
396 $checkreserves->{'borrowernumber'},
397 $iteminfo->{'biblionumber'}
399 ModReserveStatus($itemnumber,'W');
402 $nextreservinfo = $checkreserves->{'borrowernumber'};
405 return ( $messages, $nextreservinfo );
410 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
412 Calculate the fee for a reserve
417 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
420 my $dbh = C4::Context->dbh;
421 my $const = lc substr( $constraint, 0, 1 );
423 SELECT * FROM borrowers
424 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
425 WHERE borrowernumber = ?
427 my $sth = $dbh->prepare($query);
428 $sth->execute($borrowernumber);
429 my $data = $sth->fetchrow_hashref;
431 my $fee = $data->{'reservefee'};
432 my $cntitems = @- > $bibitems;
436 # check for items on issue
437 # first find biblioitem records
439 my $sth1 = $dbh->prepare(
440 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
441 WHERE (biblio.biblionumber = ?)"
443 $sth1->execute($biblionumber);
444 while ( my $data1 = $sth1->fetchrow_hashref ) {
445 if ( $const eq "a" ) {
446 push @biblioitems, $data1;
451 while ( $x < $cntitems ) {
452 if ( @$bibitems->{'biblioitemnumber'} ==
453 $data->{'biblioitemnumber'} )
459 if ( $const eq 'o' ) {
461 push @biblioitems, $data1;
466 push @biblioitems, $data1;
472 my $cntitemsfound = @biblioitems;
476 while ( $x < $cntitemsfound ) {
477 my $bitdata = $biblioitems[$x];
478 my $sth2 = $dbh->prepare(
480 WHERE biblioitemnumber = ?"
482 $sth2->execute( $bitdata->{'biblioitemnumber'} );
483 while ( my $itdata = $sth2->fetchrow_hashref ) {
484 my $sth3 = $dbh->prepare(
485 "SELECT * FROM issues
486 WHERE itemnumber = ?"
488 $sth3->execute( $itdata->{'itemnumber'} );
489 if ( my $isdata = $sth3->fetchrow_hashref ) {
497 if ( $allissued == 0 ) {
499 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
500 $rsth->execute($biblionumber);
501 if ( my $rdata = $rsth->fetchrow_hashref ) {
511 =item GetReservesToBranch
513 @transreserv = GetReservesToBranch( $frombranch );
515 Get reserve list for a given branch
519 sub GetReservesToBranch {
520 my ( $frombranch ) = @_;
521 my $dbh = C4::Context->dbh;
522 my $sth = $dbh->prepare(
523 "SELECT borrowernumber,reservedate,itemnumber,timestamp
528 $sth->execute( $frombranch );
531 while ( my $data = $sth->fetchrow_hashref ) {
532 $transreserv[$i] = $data;
536 return (@transreserv);
539 =item GetReservesForBranch
541 @transreserv = GetReservesForBranch($frombranch);
545 sub GetReservesForBranch {
546 my ($frombranch) = @_;
547 my $dbh = C4::Context->dbh;
548 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
553 $query .= " AND branchcode=? ";
555 $query .= "ORDER BY waitingdate" ;
556 my $sth = $dbh->prepare($query);
558 $sth->execute($frombranch);
565 while ( my $data = $sth->fetchrow_hashref ) {
566 $transreserv[$i] = $data;
570 return (@transreserv);
575 ($status, $reserve) = &CheckReserves($itemnumber);
577 Find a book in the reserves.
579 C<$itemnumber> is the book's item number.
581 As I understand it, C<&CheckReserves> looks for the given item in the
582 reserves. If it is found, that's a match, and C<$status> is set to
585 Otherwise, it finds the most important item in the reserves with the
586 same biblio number as this book (I'm not clear on this) and returns it
587 with C<$status> set to C<Reserved>.
589 C<&CheckReserves> returns a two-element list:
591 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
593 C<$reserve> is the reserve item that matched. It is a
594 reference-to-hash whose keys are mostly the fields of the reserves
595 table in the Koha database.
600 my ( $item, $barcode ) = @_;
601 my $dbh = C4::Context->dbh;
604 my $qitem = $dbh->quote($item);
605 # Look up the item by itemnumber
607 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan
609 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
610 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
611 WHERE itemnumber=$qitem
613 $sth = $dbh->prepare($query);
616 my $qbc = $dbh->quote($barcode);
617 # Look up the item by barcode
619 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan
621 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
622 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
623 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
624 AND biblioitems.itemtype = itemtypes.itemtype
627 $sth = $dbh->prepare($query);
629 # FIXME - This function uses $item later on. Ought to set it here.
632 my ( $biblio, $bibitem, $notforloan ) = $sth->fetchrow_array;
635 # if item is not for loan it cannot be reserved either.....
636 return ( 0, 0 ) if $notforloan;
638 # get the reserves...
639 # Find this item in the reserves
640 my @reserves = _Findgroupreserve( $bibitem, $biblio );
641 my $count = scalar @reserves;
643 # $priority and $highest are used to find the most important item
644 # in the list returned by &_Findgroupreserve. (The lower $priority,
645 # the more important the item.)
646 # $highest is the most important item we've seen so far.
647 my $priority = 10000000;
650 foreach my $res (@reserves) {
651 # FIXME - $item might be undefined or empty: the caller
652 # might be searching by barcode.
653 if ( $res->{'itemnumber'} == $item ) {
655 return ( "Waiting", $res );
658 # See if this item is more important than what we've got
660 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
662 $priority = $res->{'priority'};
669 # If we get this far, then no exact match was found. Print the
670 # most important item on the list. I think this tells us who's
671 # next in line to get this book.
672 if ($highest) { # FIXME - $highest might be undefined
673 $highest->{'itemnumber'} = $item;
674 return ( "Reserved", $highest );
683 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
687 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
688 cancel, but not both: if both are given, C<&CancelReserve> does
691 C<$borrowernumber> is the borrower number of the patron on whose
692 behalf the book was reserved.
694 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
695 priorities of the other people who are waiting on the book.
700 my ( $biblio, $item, $borr ) = @_;
701 my $dbh = C4::Context->dbh;
702 if ( $item and $borr ) {
703 # removing a waiting reserve record....
704 # update the database...
707 SET cancellationdate = now(),
711 AND borrowernumber = ?
713 my $sth = $dbh->prepare($query);
714 $sth->execute( $item, $borr );
717 INSERT INTO old_reserves
718 SELECT * FROM reserves
720 AND borrowernumber = ?
722 $sth = $dbh->prepare($query);
723 $sth->execute( $item, $borr );
727 AND borrowernumber = ?
729 $sth = $dbh->prepare($query);
730 $sth->execute( $item, $borr );
733 # removing a reserve record....
734 # get the prioritiy on this record....
737 SELECT priority FROM reserves
738 WHERE biblionumber = ?
739 AND borrowernumber = ?
740 AND cancellationdate IS NULL
741 AND itemnumber IS NULL
743 my $sth = $dbh->prepare($query);
744 $sth->execute( $biblio, $borr );
745 ($priority) = $sth->fetchrow_array;
749 SET cancellationdate = now(),
752 WHERE biblionumber = ?
753 AND borrowernumber = ?
756 # update the database, removing the record...
757 $sth = $dbh->prepare($query);
758 $sth->execute( $biblio, $borr );
762 INSERT INTO old_reserves
763 SELECT * FROM reserves
764 WHERE biblionumber = ?
765 AND borrowernumber = ?
767 $sth = $dbh->prepare($query);
768 $sth->execute( $biblio, $borr );
772 WHERE biblionumber = ?
773 AND borrowernumber = ?
775 $sth = $dbh->prepare($query);
776 $sth->execute( $biblio, $borr );
778 # now fix the priority on the others....
779 _FixPriority( $priority, $biblio );
785 &ModReserve($rank,$biblio,$borrower,$branch)
790 #subroutine to update a reserve
791 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
792 return if $rank eq "W";
793 return if $rank eq "n";
794 my $dbh = C4::Context->dbh;
795 if ( $rank eq "del" ) {
798 SET cancellationdate=now()
799 WHERE biblionumber = ?
800 AND borrowernumber = ?
802 my $sth = $dbh->prepare($query);
803 $sth->execute( $biblio, $borrower );
806 INSERT INTO old_reserves
809 WHERE biblionumber = ?
810 AND borrowernumber = ?
812 $sth = $dbh->prepare($query);
813 $sth->execute( $biblio, $borrower );
816 WHERE biblionumber = ?
817 AND borrowernumber = ?
819 $sth = $dbh->prepare($query);
820 $sth->execute( $biblio, $borrower );
825 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL
826 WHERE biblionumber = ?
827 AND borrowernumber = ?
829 my $sth = $dbh->prepare($query);
830 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
832 _FixPriority( $biblio, $borrower, $rank);
838 &ModReserveFill($reserve);
840 Fill a reserve. If I understand this correctly, this means that the
841 reserved book has been found and given to the patron who reserved it.
843 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
844 whose keys are fields from the reserves table in the Koha database.
850 my $dbh = C4::Context->dbh;
851 # fill in a reserve record....
852 my $biblionumber = $res->{'biblionumber'};
853 my $borrowernumber = $res->{'borrowernumber'};
854 my $resdate = $res->{'reservedate'};
856 # get the priority on this record....
858 my $query = "SELECT priority
860 WHERE biblionumber = ?
861 AND borrowernumber = ?
862 AND reservedate = ?";
863 my $sth = $dbh->prepare($query);
864 $sth->execute( $biblionumber, $borrowernumber, $resdate );
865 ($priority) = $sth->fetchrow_array;
868 # update the database...
869 $query = "UPDATE reserves
872 WHERE biblionumber = ?
874 AND borrowernumber = ?
876 $sth = $dbh->prepare($query);
877 $sth->execute( $biblionumber, $resdate, $borrowernumber );
880 # move to old_reserves
881 $query = "INSERT INTO old_reserves
882 SELECT * FROM reserves
883 WHERE biblionumber = ?
885 AND borrowernumber = ?
887 $sth = $dbh->prepare($query);
888 $sth->execute( $biblionumber, $resdate, $borrowernumber );
889 $query = "DELETE FROM reserves
890 WHERE biblionumber = ?
892 AND borrowernumber = ?
894 $sth = $dbh->prepare($query);
895 $sth->execute( $biblionumber, $resdate, $borrowernumber );
897 # now fix the priority on the others (if the priority wasn't
898 # already sorted!)....
899 unless ( $priority == 0 ) {
900 _FixPriority( $priority, $biblionumber );
904 =item ModReserveStatus
906 &ModReserveStatus($itemnumber, $newstatus);
908 Update the reserve status for the active (priority=0) reserve.
910 $itemnumber is the itemnumber the reserve is on
912 $newstatus is the new status.
916 sub ModReserveStatus {
918 #first : check if we have a reservation for this item .
919 my ($itemnumber, $newstatus) = @_;
920 my $dbh = C4::Context->dbh;
921 my $query = " UPDATE reserves
922 SET found=?,waitingdate = now()
927 my $sth_set = $dbh->prepare($query);
928 $sth_set->execute( $newstatus, $itemnumber );
932 =item ModReserveAffect
934 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
936 This function affect an item and a status for a given reserve
937 The itemnumber parameter is used to find the biblionumber.
938 with the biblionumber & the borrowernumber, we can affect the itemnumber
939 to the correct reserve.
941 if $transferToDo is not set, then the status is set to "Waiting" as well.
942 otherwise, a transfer is on the way, and the end of the transfer will
943 take care of the waiting status
946 sub ModReserveAffect {
947 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
948 my $dbh = C4::Context->dbh;
950 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
951 # attached to $itemnumber
952 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
953 $sth->execute($itemnumber);
954 my ($biblionumber) = $sth->fetchrow;
955 # If we affect a reserve that has to be transfered, don't set to Waiting
962 WHERE borrowernumber = ?
967 # affect the reserve to Waiting as well.
974 WHERE borrowernumber = ?
978 $sth = $dbh->prepare($query);
979 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
984 =item ModReserveCancelAll
986 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
988 function to cancel reserv,check other reserves, and transfer document if it's necessary
992 sub ModReserveCancelAll {
995 my ( $itemnumber, $borrowernumber ) = @_;
997 #step 1 : cancel the reservation
998 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1000 #step 2 launch the subroutine of the others reserves
1001 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1003 return ( $messages, $nextreservinfo );
1006 =item ModReserveMinusPriority
1008 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1010 Reduce the values of queuded list
1014 sub ModReserveMinusPriority {
1015 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1017 #first step update the value of the first person on reserv
1018 my $dbh = C4::Context->dbh;
1021 SET priority = 0 , itemnumber = ?
1022 WHERE borrowernumber=?
1025 my $sth_upd = $dbh->prepare($query);
1026 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1028 # second step update all others reservs
1031 SET priority = priority-1
1032 WHERE biblionumber = ?
1035 $sth_upd = $dbh->prepare($query);
1036 $sth_upd->execute( $biblionumber );
1043 &_FixPriority($biblio,$borrowernumber,$rank);
1045 Only used internally (so don't export it)
1046 Changed how this functions works #
1047 Now just gets an array of reserves in the rank order and updates them with
1048 the array index (+1 as array starts from 0)
1049 and if $rank is supplied will splice item from the array and splice it back in again
1050 in new priority rank
1055 my ( $biblio, $borrowernumber, $rank ) = @_;
1056 my $dbh = C4::Context->dbh;
1057 if ( $rank eq "del" ) {
1058 CancelReserve( $biblio, undef, $borrowernumber );
1060 if ( $rank eq "W" || $rank eq "0" ) {
1062 # make sure priority for waiting items is 0
1066 WHERE biblionumber = ?
1067 AND borrowernumber = ?
1070 my $sth = $dbh->prepare($query);
1071 $sth->execute( $biblio, $borrowernumber );
1077 # FIXME adding a new security in returned elements for changing priority,
1078 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1079 # This is wrong a waiting reserve has W set
1080 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1082 SELECT borrowernumber, reservedate, constrainttype
1084 WHERE biblionumber = ?
1085 AND ((found <> 'W') or found is NULL)
1086 ORDER BY priority ASC
1088 my $sth = $dbh->prepare($query);
1089 $sth->execute($biblio);
1090 while ( my $line = $sth->fetchrow_hashref ) {
1091 push( @reservedates, $line );
1092 push( @priority, $line );
1095 # To find the matching index
1097 my $key = -1; # to allow for 0 to be a valid result
1098 for ( $i = 0 ; $i < @priority ; $i++ ) {
1099 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1100 $key = $i; # save the index
1105 # if index exists in array then move it to new position
1106 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1107 my $new_rank = $rank -
1108 1; # $new_rank is what you want the new index to be in the array
1109 my $moving_item = splice( @priority, $key, 1 );
1110 splice( @priority, $new_rank, 0, $moving_item );
1113 # now fix the priority on those that are left....
1117 WHERE biblionumber = ?
1118 AND borrowernumber = ?
1122 $sth = $dbh->prepare($query);
1123 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1126 $priority[$j]->{'borrowernumber'},
1127 $priority[$j]->{'reservedate'}
1133 =item _Findgroupreserve
1135 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber);
1138 I don't know what this does, because I don't understand how reserve
1139 constraints work. I think the idea is that you reserve a particular
1140 biblio, and the constraint allows you to restrict it to a given
1141 biblioitem (e.g., if you want to borrow the audio book edition of "The
1142 Prophet", rather than the first available publication).
1144 C<&_Findgroupreserve> returns :
1145 C<@results> is an array of references-to-hash whose keys are mostly
1146 fields from the reserves table of the Koha database, plus
1147 C<biblioitemnumber>.
1151 sub _Findgroupreserve {
1152 my ( $bibitem, $biblio ) = @_;
1153 my $dbh = C4::Context->dbh;
1155 SELECT reserves.biblionumber AS biblionumber,
1156 reserves.borrowernumber AS borrowernumber,
1157 reserves.reservedate AS reservedate,
1158 reserves.branchcode AS branchcode,
1159 reserves.cancellationdate AS cancellationdate,
1160 reserves.found AS found,
1161 reserves.reservenotes AS reservenotes,
1162 reserves.priority AS priority,
1163 reserves.timestamp AS timestamp,
1164 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1165 reserves.itemnumber AS itemnumber
1167 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1168 WHERE reserves.biblionumber = ?
1169 AND ( ( reserveconstraints.biblioitemnumber = ?
1170 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1171 AND reserves.reservedate =reserveconstraints.reservedate )
1172 OR reserves.constrainttype='a' )
1174 my $sth = $dbh->prepare($query);
1175 $sth->execute( $biblio, $bibitem );
1177 while ( my $data = $sth->fetchrow_hashref ) {
1178 push( @results, $data );
1188 Koha Developement team <info@koha.org>