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
115 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
121 $branch, $borrowernumber, $biblionumber,
122 $constraint, $bibitems, $priority, $notes,
123 $title, $checkitem, $found
126 GetReserveFee($borrowernumber, $biblionumber, $constraint,
128 my $dbh = C4::Context->dbh;
129 my $const = lc substr( $constraint, 0, 1 );
130 my @datearr = localtime(time);
132 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
135 # If the reserv had the waiting status, we had the value of the resdate
136 if ( $found eq 'W' ) {
137 $waitingdate = $resdate;
141 # updates take place here
143 my $nextacctno = &getnextacctno( $borrowernumber );
145 INSERT INTO accountlines
146 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
148 (?,?,now(),?,?,'Res',?)
150 my $usth = $dbh->prepare($query);
151 $usth->execute( $borrowernumber, $nextacctno, $fee,
152 "Reserve Charge - $title", $fee );
159 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
160 priority,reservenotes,itemnumber,found,waitingdate)
165 my $sth = $dbh->prepare($query);
167 $borrowernumber, $biblionumber, $resdate, $branch,
168 $const, $priority, $notes, $checkitem,
174 if ( ( $const eq "o" ) || ( $const eq "e" ) ) {
175 my $numitems = @$bibitems;
177 while ( $i < $numitems ) {
178 my $biblioitem = @$bibitems[$i];
180 INSERT INTO reserveconstraints
181 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
185 my $sth = $dbh->prepare("");
186 $sth->execute( $borrowernumber, $biblionumber, $resdate,
195 =item GetReservesFromBiblionumber
197 @borrowerreserv=&GetReserves($biblionumber,$itemnumber,$borrowernumber);
199 this function get the list of reservation for an C<$biblionumber>, C<$itemnumber> or C<$borrowernumber>
201 Only 1 argument has to be passed.
205 sub GetReservesFromBiblionumber {
206 my ( $biblionumber, $itemnumber, $borrowernumber ) = @_;
207 my $dbh = C4::Context->dbh;
209 # Find the desired items in the reserves
212 timestamp AS rtimestamp,
222 WHERE biblionumber = ?
224 my $sth = $dbh->prepare($query);
225 $sth->execute($biblionumber);
228 while ( my $data = $sth->fetchrow_hashref ) {
230 # FIXME - What is this if-statement doing? How do constraints work?
231 if ( $data->{constrainttype} eq 'o' ) {
233 SELECT biblioitemnumber
234 FROM reserveconstraints
235 WHERE biblionumber = ?
236 AND borrowernumber = ?
239 my $csth = $dbh->prepare($query);
240 $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
241 $data->{reservedate}, );
244 while ( my $bibitemnos = $csth->fetchrow_array ) {
245 push( @bibitemno, $bibitemnos );
247 my $count = @bibitemno;
249 # if we have two or more different specific itemtypes
250 # reserved by same person on same day
253 $bdata = GetBiblioItemData( $bibitemno[$i] );
258 # Look up the book we just found.
259 $bdata = GetBiblioItemData( $bibitemno[0] );
263 # Add the results of this latest search to the current
265 # FIXME - An 'each' would probably be more efficient.
266 foreach my $key ( keys %$bdata ) {
267 $data->{$key} = $bdata->{$key};
270 push @results, $data;
273 return ( $#results + 1, \@results );
276 =item GetReservesFromItemnumber
278 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
280 TODO :: Description here
284 sub GetReservesFromItemnumber {
285 my ( $itemnumber ) = @_;
286 my $dbh = C4::Context->dbh;
288 SELECT reservedate,borrowernumber,branchcode
292 my $sth_res = $dbh->prepare($query);
293 $sth_res->execute($itemnumber);
294 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
295 return ( $reservedate, $borrowernumber, $branchcode );
298 =item GetReservesFromBorrowernumber
300 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
306 sub GetReservesFromBorrowernumber {
307 my ( $borrowernumber, $status ) = @_;
308 my $dbh = C4::Context->dbh;
311 $sth = $dbh->prepare("
314 WHERE borrowernumber=?
318 $sth->execute($borrowernumber,$status);
320 $sth = $dbh->prepare("
323 WHERE borrowernumber=?
326 $sth->execute($borrowernumber);
328 my $data = $sth->fetchall_arrayref({});
331 #-------------------------------------------------------------------------------------
333 =item GetReserveCount
335 $number = &GetReserveCount($borrowernumber);
337 this function returns the number of reservation for a borrower given on input arg.
341 sub GetReserveCount {
342 my ($borrowernumber) = @_;
344 my $dbh = C4::Context->dbh;
347 SELECT COUNT(*) AS counter
349 WHERE borrowernumber = ?
351 my $sth = $dbh->prepare($query);
352 $sth->execute($borrowernumber);
353 my $row = $sth->fetchrow_hashref;
356 return $row->{counter};
359 =item GetOtherReserves
361 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
363 Check queued list of this document and check if this document must be transfered
367 sub GetOtherReserves {
368 my ($itemnumber) = @_;
371 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
372 if ($checkreserves) {
373 my $iteminfo = GetItem($itemnumber);
374 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
375 $messages->{'transfert'} = $checkreserves->{'branchcode'};
376 #minus priorities of others reservs
377 ModReserveMinusPriority(
379 $checkreserves->{'borrowernumber'},
380 $iteminfo->{'biblionumber'}
383 #launch the subroutine dotransfer
384 C4::Circulation::ModItemTransfer(
386 $iteminfo->{'holdingbranch'},
387 $checkreserves->{'branchcode'}
392 #step 2b : case of a reservation on the same branch, set the waiting status
394 $messages->{'waiting'} = 1;
395 ModReserveMinusPriority(
397 $checkreserves->{'borrowernumber'},
398 $iteminfo->{'biblionumber'}
400 ModReserveStatus($itemnumber,'W');
403 $nextreservinfo = $checkreserves->{'borrowernumber'};
406 return ( $messages, $nextreservinfo );
411 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
413 Calculate the fee for a reserve
418 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
421 my $dbh = C4::Context->dbh;
422 my $const = lc substr( $constraint, 0, 1 );
424 SELECT * FROM borrowers
425 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
426 WHERE borrowernumber = ?
428 my $sth = $dbh->prepare($query);
429 $sth->execute($borrowernumber);
430 my $data = $sth->fetchrow_hashref;
432 my $fee = $data->{'reservefee'};
433 my $cntitems = @- > $bibitems;
437 # check for items on issue
438 # first find biblioitem records
440 my $sth1 = $dbh->prepare(
441 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
442 WHERE (biblio.biblionumber = ?)"
444 $sth1->execute($biblionumber);
445 while ( my $data1 = $sth1->fetchrow_hashref ) {
446 if ( $const eq "a" ) {
447 push @biblioitems, $data1;
452 while ( $x < $cntitems ) {
453 if ( @$bibitems->{'biblioitemnumber'} ==
454 $data->{'biblioitemnumber'} )
460 if ( $const eq 'o' ) {
462 push @biblioitems, $data1;
467 push @biblioitems, $data1;
473 my $cntitemsfound = @biblioitems;
477 while ( $x < $cntitemsfound ) {
478 my $bitdata = $biblioitems[$x];
479 my $sth2 = $dbh->prepare(
481 WHERE biblioitemnumber = ?"
483 $sth2->execute( $bitdata->{'biblioitemnumber'} );
484 while ( my $itdata = $sth2->fetchrow_hashref ) {
485 my $sth3 = $dbh->prepare(
486 "SELECT * FROM issues
487 WHERE itemnumber = ?"
489 $sth3->execute( $itdata->{'itemnumber'} );
490 if ( my $isdata = $sth3->fetchrow_hashref ) {
498 if ( $allissued == 0 ) {
500 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
501 $rsth->execute($biblionumber);
502 if ( my $rdata = $rsth->fetchrow_hashref ) {
512 =item GetReservesToBranch
514 @transreserv = GetReservesToBranch( $frombranch );
516 Get reserve list for a given branch
520 sub GetReservesToBranch {
521 my ( $frombranch ) = @_;
522 my $dbh = C4::Context->dbh;
523 my $sth = $dbh->prepare(
524 "SELECT borrowernumber,reservedate,itemnumber,timestamp
529 $sth->execute( $frombranch );
532 while ( my $data = $sth->fetchrow_hashref ) {
533 $transreserv[$i] = $data;
537 return (@transreserv);
540 =item GetReservesForBranch
542 @transreserv = GetReservesForBranch($frombranch);
546 sub GetReservesForBranch {
547 my ($frombranch) = @_;
548 my $dbh = C4::Context->dbh;
549 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
554 $query .= " AND branchcode=? ";
556 $query .= "ORDER BY waitingdate" ;
557 my $sth = $dbh->prepare($query);
559 $sth->execute($frombranch);
566 while ( my $data = $sth->fetchrow_hashref ) {
567 $transreserv[$i] = $data;
571 return (@transreserv);
576 ($status, $reserve) = &CheckReserves($itemnumber);
578 Find a book in the reserves.
580 C<$itemnumber> is the book's item number.
582 As I understand it, C<&CheckReserves> looks for the given item in the
583 reserves. If it is found, that's a match, and C<$status> is set to
586 Otherwise, it finds the most important item in the reserves with the
587 same biblio number as this book (I'm not clear on this) and returns it
588 with C<$status> set to C<Reserved>.
590 C<&CheckReserves> returns a two-element list:
592 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
594 C<$reserve> is the reserve item that matched. It is a
595 reference-to-hash whose keys are mostly the fields of the reserves
596 table in the Koha database.
601 my ( $item, $barcode ) = @_;
602 my $dbh = C4::Context->dbh;
605 my $qitem = $dbh->quote($item);
606 # Look up the item by itemnumber
608 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan
610 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
611 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
612 WHERE itemnumber=$qitem
614 $sth = $dbh->prepare($query);
617 my $qbc = $dbh->quote($barcode);
618 # Look up the item by barcode
620 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan
622 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
623 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
624 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
625 AND biblioitems.itemtype = itemtypes.itemtype
628 $sth = $dbh->prepare($query);
630 # FIXME - This function uses $item later on. Ought to set it here.
633 my ( $biblio, $bibitem, $notforloan ) = $sth->fetchrow_array;
636 # if item is not for loan it cannot be reserved either.....
637 return ( 0, 0 ) if $notforloan;
639 # get the reserves...
640 # Find this item in the reserves
641 my @reserves = _Findgroupreserve( $bibitem, $biblio );
642 my $count = scalar @reserves;
644 # $priority and $highest are used to find the most important item
645 # in the list returned by &_Findgroupreserve. (The lower $priority,
646 # the more important the item.)
647 # $highest is the most important item we've seen so far.
648 my $priority = 10000000;
651 foreach my $res (@reserves) {
652 # FIXME - $item might be undefined or empty: the caller
653 # might be searching by barcode.
654 if ( $res->{'itemnumber'} == $item ) {
656 return ( "Waiting", $res );
659 # See if this item is more important than what we've got
661 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
663 $priority = $res->{'priority'};
670 # If we get this far, then no exact match was found. Print the
671 # most important item on the list. I think this tells us who's
672 # next in line to get this book.
673 if ($highest) { # FIXME - $highest might be undefined
674 $highest->{'itemnumber'} = $item;
675 return ( "Reserved", $highest );
684 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
688 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
689 cancel, but not both: if both are given, C<&CancelReserve> does
692 C<$borrowernumber> is the borrower number of the patron on whose
693 behalf the book was reserved.
695 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
696 priorities of the other people who are waiting on the book.
701 my ( $biblio, $item, $borr ) = @_;
702 my $dbh = C4::Context->dbh;
703 if ( $item and $borr ) {
704 # removing a waiting reserve record....
705 # update the database...
708 SET cancellationdate = now(),
712 AND borrowernumber = ?
714 my $sth = $dbh->prepare($query);
715 $sth->execute( $item, $borr );
718 INSERT INTO old_reserves
719 SELECT * FROM reserves
721 AND borrowernumber = ?
723 $sth = $dbh->prepare($query);
724 $sth->execute( $item, $borr );
728 AND borrowernumber = ?
730 $sth = $dbh->prepare($query);
731 $sth->execute( $item, $borr );
734 # removing a reserve record....
735 # get the prioritiy on this record....
738 SELECT priority FROM reserves
739 WHERE biblionumber = ?
740 AND borrowernumber = ?
741 AND cancellationdate IS NULL
742 AND itemnumber IS NULL
744 my $sth = $dbh->prepare($query);
745 $sth->execute( $biblio, $borr );
746 ($priority) = $sth->fetchrow_array;
750 SET cancellationdate = now(),
753 WHERE biblionumber = ?
754 AND borrowernumber = ?
757 # update the database, removing the record...
758 $sth = $dbh->prepare($query);
759 $sth->execute( $biblio, $borr );
763 INSERT INTO old_reserves
764 SELECT * FROM reserves
765 WHERE biblionumber = ?
766 AND borrowernumber = ?
768 $sth = $dbh->prepare($query);
769 $sth->execute( $biblio, $borr );
773 WHERE biblionumber = ?
774 AND borrowernumber = ?
776 $sth = $dbh->prepare($query);
777 $sth->execute( $biblio, $borr );
779 # now fix the priority on the others....
780 _FixPriority( $priority, $biblio );
786 &ModReserve($rank,$biblio,$borrower,$branch)
791 #subroutine to update a reserve
792 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
793 return if $rank eq "W";
794 return if $rank eq "n";
795 my $dbh = C4::Context->dbh;
796 if ( $rank eq "del" ) {
799 SET cancellationdate=now()
800 WHERE biblionumber = ?
801 AND borrowernumber = ?
803 my $sth = $dbh->prepare($query);
804 $sth->execute( $biblio, $borrower );
807 INSERT INTO old_reserves
810 WHERE biblionumber = ?
811 AND borrowernumber = ?
813 $sth = $dbh->prepare($query);
814 $sth->execute( $biblio, $borrower );
817 WHERE biblionumber = ?
818 AND borrowernumber = ?
820 $sth = $dbh->prepare($query);
821 $sth->execute( $biblio, $borrower );
826 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL
827 WHERE biblionumber = ?
828 AND borrowernumber = ?
830 my $sth = $dbh->prepare($query);
831 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
833 _FixPriority( $biblio, $borrower, $rank);
839 &ModReserveFill($reserve);
841 Fill a reserve. If I understand this correctly, this means that the
842 reserved book has been found and given to the patron who reserved it.
844 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
845 whose keys are fields from the reserves table in the Koha database.
851 my $dbh = C4::Context->dbh;
852 # fill in a reserve record....
853 my $biblionumber = $res->{'biblionumber'};
854 my $borrowernumber = $res->{'borrowernumber'};
855 my $resdate = $res->{'reservedate'};
857 # get the priority on this record....
859 my $query = "SELECT priority
861 WHERE biblionumber = ?
862 AND borrowernumber = ?
863 AND reservedate = ?";
864 my $sth = $dbh->prepare($query);
865 $sth->execute( $biblionumber, $borrowernumber, $resdate );
866 ($priority) = $sth->fetchrow_array;
869 # update the database...
870 $query = "UPDATE reserves
873 WHERE biblionumber = ?
875 AND borrowernumber = ?
877 $sth = $dbh->prepare($query);
878 $sth->execute( $biblionumber, $resdate, $borrowernumber );
881 # move to old_reserves
882 $query = "INSERT INTO old_reserves
883 SELECT * FROM reserves
884 WHERE biblionumber = ?
886 AND borrowernumber = ?
888 $sth = $dbh->prepare($query);
889 $sth->execute( $biblionumber, $resdate, $borrowernumber );
890 $query = "DELETE FROM reserves
891 WHERE biblionumber = ?
893 AND borrowernumber = ?
895 $sth = $dbh->prepare($query);
896 $sth->execute( $biblionumber, $resdate, $borrowernumber );
898 # now fix the priority on the others (if the priority wasn't
899 # already sorted!)....
900 unless ( $priority == 0 ) {
901 _FixPriority( $priority, $biblionumber );
905 =item ModReserveStatus
907 &ModReserveStatus($itemnumber, $newstatus);
909 Update the reserve status for the active (priority=0) reserve.
911 $itemnumber is the itemnumber the reserve is on
913 $newstatus is the new status.
917 sub ModReserveStatus {
919 #first : check if we have a reservation for this item .
920 my ($itemnumber, $newstatus) = @_;
921 my $dbh = C4::Context->dbh;
922 my $query = " UPDATE reserves
923 SET found=?,waitingdate = now()
928 my $sth_set = $dbh->prepare($query);
929 $sth_set->execute( $newstatus, $itemnumber );
933 =item ModReserveAffect
935 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
937 This function affect an item and a status for a given reserve
938 The itemnumber parameter is used to find the biblionumber.
939 with the biblionumber & the borrowernumber, we can affect the itemnumber
940 to the correct reserve.
942 if $transferToDo is not set, then the status is set to "Waiting" as well.
943 otherwise, a transfer is on the way, and the end of the transfer will
944 take care of the waiting status
947 sub ModReserveAffect {
948 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
949 my $dbh = C4::Context->dbh;
951 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
952 # attached to $itemnumber
953 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
954 $sth->execute($itemnumber);
955 my ($biblionumber) = $sth->fetchrow;
956 # If we affect a reserve that has to be transfered, don't set to Waiting
963 WHERE borrowernumber = ?
968 # affect the reserve to Waiting as well.
975 WHERE borrowernumber = ?
979 $sth = $dbh->prepare($query);
980 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
985 =item ModReserveCancelAll
987 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
989 function to cancel reserv,check other reserves, and transfer document if it's necessary
993 sub ModReserveCancelAll {
996 my ( $itemnumber, $borrowernumber ) = @_;
998 #step 1 : cancel the reservation
999 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1001 #step 2 launch the subroutine of the others reserves
1002 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1004 return ( $messages, $nextreservinfo );
1007 =item ModReserveMinusPriority
1009 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1011 Reduce the values of queuded list
1015 sub ModReserveMinusPriority {
1016 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1018 #first step update the value of the first person on reserv
1019 my $dbh = C4::Context->dbh;
1022 SET priority = 0 , itemnumber = ?
1023 WHERE borrowernumber=?
1026 my $sth_upd = $dbh->prepare($query);
1027 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1029 # second step update all others reservs
1032 SET priority = priority-1
1033 WHERE biblionumber = ?
1036 $sth_upd = $dbh->prepare($query);
1037 $sth_upd->execute( $biblionumber );
1042 =item GetReserveInfo
1044 &GetReserveInfo($borrowernumber,$biblionumber);
1046 Get item and borrower details for a current hold.
1047 Current implementation this query should have a single result.
1050 sub GetReserveInfo {
1051 my ( $borrowernumber, $biblionumber ) = @_;
1052 my $dbh = C4::Context->dbh;
1053 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1054 reserves.biblionumber, reserves.branchcode,
1055 notificationdate, reminderdate, priority, found,
1056 firstname, surname, phone,
1057 email, address, address2,
1058 cardnumber, city, zipcode,
1059 biblio.title, biblio.author,
1060 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1062 FROM reserves left join items
1063 ON items.itemnumber=reserves.itemnumber ,
1066 reserves.borrowernumber=? &&
1067 reserves.biblionumber=? &&
1068 reserves.borrowernumber=borrowers.borrowernumber &&
1069 reserves.biblionumber=biblio.biblionumber ";
1070 my $sth = $dbh->prepare($strsth);
1071 $sth->execute($borrowernumber,$biblionumber);
1073 my $data = $sth->fetchrow_hashref;
1080 &_FixPriority($biblio,$borrowernumber,$rank);
1082 Only used internally (so don't export it)
1083 Changed how this functions works #
1084 Now just gets an array of reserves in the rank order and updates them with
1085 the array index (+1 as array starts from 0)
1086 and if $rank is supplied will splice item from the array and splice it back in again
1087 in new priority rank
1092 my ( $biblio, $borrowernumber, $rank ) = @_;
1093 my $dbh = C4::Context->dbh;
1094 if ( $rank eq "del" ) {
1095 CancelReserve( $biblio, undef, $borrowernumber );
1097 if ( $rank eq "W" || $rank eq "0" ) {
1099 # make sure priority for waiting items is 0
1103 WHERE biblionumber = ?
1104 AND borrowernumber = ?
1107 my $sth = $dbh->prepare($query);
1108 $sth->execute( $biblio, $borrowernumber );
1114 # FIXME adding a new security in returned elements for changing priority,
1115 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1116 # This is wrong a waiting reserve has W set
1117 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1119 SELECT borrowernumber, reservedate, constrainttype
1121 WHERE biblionumber = ?
1122 AND ((found <> 'W') or found is NULL)
1123 ORDER BY priority ASC
1125 my $sth = $dbh->prepare($query);
1126 $sth->execute($biblio);
1127 while ( my $line = $sth->fetchrow_hashref ) {
1128 push( @reservedates, $line );
1129 push( @priority, $line );
1132 # To find the matching index
1134 my $key = -1; # to allow for 0 to be a valid result
1135 for ( $i = 0 ; $i < @priority ; $i++ ) {
1136 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1137 $key = $i; # save the index
1142 # if index exists in array then move it to new position
1143 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1144 my $new_rank = $rank -
1145 1; # $new_rank is what you want the new index to be in the array
1146 my $moving_item = splice( @priority, $key, 1 );
1147 splice( @priority, $new_rank, 0, $moving_item );
1150 # now fix the priority on those that are left....
1154 WHERE biblionumber = ?
1155 AND borrowernumber = ?
1159 $sth = $dbh->prepare($query);
1160 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1163 $priority[$j]->{'borrowernumber'},
1164 $priority[$j]->{'reservedate'}
1170 =item _Findgroupreserve
1172 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber);
1175 I don't know what this does, because I don't understand how reserve
1176 constraints work. I think the idea is that you reserve a particular
1177 biblio, and the constraint allows you to restrict it to a given
1178 biblioitem (e.g., if you want to borrow the audio book edition of "The
1179 Prophet", rather than the first available publication).
1181 C<&_Findgroupreserve> returns :
1182 C<@results> is an array of references-to-hash whose keys are mostly
1183 fields from the reserves table of the Koha database, plus
1184 C<biblioitemnumber>.
1188 sub _Findgroupreserve {
1189 my ( $bibitem, $biblio ) = @_;
1190 my $dbh = C4::Context->dbh;
1192 SELECT reserves.biblionumber AS biblionumber,
1193 reserves.borrowernumber AS borrowernumber,
1194 reserves.reservedate AS reservedate,
1195 reserves.branchcode AS branchcode,
1196 reserves.cancellationdate AS cancellationdate,
1197 reserves.found AS found,
1198 reserves.reservenotes AS reservenotes,
1199 reserves.priority AS priority,
1200 reserves.timestamp AS timestamp,
1201 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1202 reserves.itemnumber AS itemnumber
1204 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1205 WHERE reserves.biblionumber = ?
1206 AND ( ( reserveconstraints.biblioitemnumber = ?
1207 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1208 AND reserves.reservedate =reserveconstraints.reservedate )
1209 OR reserves.constrainttype='a' )
1211 my $sth = $dbh->prepare($query);
1212 $sth->execute( $biblio, $bibitem );
1214 while ( my $data = $sth->fetchrow_hashref ) {
1215 push( @results, $data );
1225 Koha Developement team <info@koha.org>