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; # FIXME: @- is a regexp match var. Unclear and probably INCORRECT usage here.
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'} )
461 ($found == 1 and $const eq 'o')) {
462 push @biblioitems, $data1;
467 my $cntitemsfound = @biblioitems;
471 while ( $x < $cntitemsfound ) {
472 my $bitdata = $biblioitems[$x];
473 my $sth2 = $dbh->prepare("SELECT * FROM items WHERE biblioitemnumber = ?");
474 $sth2->execute( $bitdata->{'biblioitemnumber'} );
475 while ( my $itdata = $sth2->fetchrow_hashref ) {
476 my $sth3 = $dbh->prepare("SELECT * FROM issues WHERE itemnumber = ?");
477 $sth3->execute( $itdata->{'itemnumber'} );
478 unless ($sth3->rows()) {
484 if ( $allissued == 0 ) {
485 my $rsth = $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
486 $rsth->execute($biblionumber);
487 unless ($rsth->rows()) {
495 =item GetReservesToBranch
497 @transreserv = GetReservesToBranch( $frombranch );
499 Get reserve list for a given branch
503 sub GetReservesToBranch {
504 my ( $frombranch ) = @_;
505 my $dbh = C4::Context->dbh;
506 my $sth = $dbh->prepare(
507 "SELECT borrowernumber,reservedate,itemnumber,timestamp
512 $sth->execute( $frombranch );
515 while ( my $data = $sth->fetchrow_hashref ) {
516 $transreserv[$i] = $data;
520 return (@transreserv);
523 =item GetReservesForBranch
525 @transreserv = GetReservesForBranch($frombranch);
529 sub GetReservesForBranch {
530 my ($frombranch) = @_;
531 my $dbh = C4::Context->dbh;
532 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
537 $query .= " AND branchcode=? ";
539 $query .= "ORDER BY waitingdate" ;
540 my $sth = $dbh->prepare($query);
542 $sth->execute($frombranch);
549 while ( my $data = $sth->fetchrow_hashref ) {
550 $transreserv[$i] = $data;
554 return (@transreserv);
559 ($status, $reserve) = &CheckReserves($itemnumber);
561 Find a book in the reserves.
563 C<$itemnumber> is the book's item number.
565 As I understand it, C<&CheckReserves> looks for the given item in the
566 reserves. If it is found, that's a match, and C<$status> is set to
569 Otherwise, it finds the most important item in the reserves with the
570 same biblio number as this book (I'm not clear on this) and returns it
571 with C<$status> set to C<Reserved>.
573 C<&CheckReserves> returns a two-element list:
575 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
577 C<$reserve> is the reserve item that matched. It is a
578 reference-to-hash whose keys are mostly the fields of the reserves
579 table in the Koha database.
584 my ( $item, $barcode ) = @_;
585 my $dbh = C4::Context->dbh;
588 my $qitem = $dbh->quote($item);
589 # Look up the item by itemnumber
591 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan
593 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
594 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
595 WHERE itemnumber=$qitem
597 $sth = $dbh->prepare($query);
600 my $qbc = $dbh->quote($barcode);
601 # Look up the item by barcode
603 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan
605 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
606 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
607 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
608 AND biblioitems.itemtype = itemtypes.itemtype
611 $sth = $dbh->prepare($query);
613 # FIXME - This function uses $item later on. Ought to set it here.
616 my ( $biblio, $bibitem, $notforloan ) = $sth->fetchrow_array;
619 # if item is not for loan it cannot be reserved either.....
620 return ( 0, 0 ) if $notforloan;
622 # get the reserves...
623 # Find this item in the reserves
624 my @reserves = _Findgroupreserve( $bibitem, $biblio );
625 my $count = scalar @reserves;
627 # $priority and $highest are used to find the most important item
628 # in the list returned by &_Findgroupreserve. (The lower $priority,
629 # the more important the item.)
630 # $highest is the most important item we've seen so far.
631 my $priority = 10000000;
634 foreach my $res (@reserves) {
635 # FIXME - $item might be undefined or empty: the caller
636 # might be searching by barcode.
637 if ( $res->{'itemnumber'} == $item ) {
639 return ( "Waiting", $res );
642 # See if this item is more important than what we've got
644 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
646 $priority = $res->{'priority'};
653 # If we get this far, then no exact match was found. Print the
654 # most important item on the list. I think this tells us who's
655 # next in line to get this book.
656 if ($highest) { # FIXME - $highest might be undefined
657 $highest->{'itemnumber'} = $item;
658 return ( "Reserved", $highest );
667 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
671 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
672 cancel, but not both: if both are given, C<&CancelReserve> does
675 C<$borrowernumber> is the borrower number of the patron on whose
676 behalf the book was reserved.
678 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
679 priorities of the other people who are waiting on the book.
684 my ( $biblio, $item, $borr ) = @_;
685 my $dbh = C4::Context->dbh;
686 if ( $item and $borr ) {
687 # removing a waiting reserve record....
688 # update the database...
691 SET cancellationdate = now(),
695 AND borrowernumber = ?
697 my $sth = $dbh->prepare($query);
698 $sth->execute( $item, $borr );
701 INSERT INTO old_reserves
702 SELECT * FROM reserves
704 AND borrowernumber = ?
706 $sth = $dbh->prepare($query);
707 $sth->execute( $item, $borr );
711 AND borrowernumber = ?
713 $sth = $dbh->prepare($query);
714 $sth->execute( $item, $borr );
717 # removing a reserve record....
718 # get the prioritiy on this record....
721 SELECT priority FROM reserves
722 WHERE biblionumber = ?
723 AND borrowernumber = ?
724 AND cancellationdate IS NULL
725 AND itemnumber IS NULL
727 my $sth = $dbh->prepare($query);
728 $sth->execute( $biblio, $borr );
729 ($priority) = $sth->fetchrow_array;
733 SET cancellationdate = now(),
736 WHERE biblionumber = ?
737 AND borrowernumber = ?
740 # update the database, removing the record...
741 $sth = $dbh->prepare($query);
742 $sth->execute( $biblio, $borr );
746 INSERT INTO old_reserves
747 SELECT * FROM reserves
748 WHERE biblionumber = ?
749 AND borrowernumber = ?
751 $sth = $dbh->prepare($query);
752 $sth->execute( $biblio, $borr );
756 WHERE biblionumber = ?
757 AND borrowernumber = ?
759 $sth = $dbh->prepare($query);
760 $sth->execute( $biblio, $borr );
762 # now fix the priority on the others....
763 _FixPriority($biblio,$borr,$priority);
769 &ModReserve($rank,$biblio,$borrower,$branch)
774 #subroutine to update a reserve
775 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
776 return if $rank eq "W";
777 return if $rank eq "n";
778 my $dbh = C4::Context->dbh;
779 if ( $rank eq "del" ) {
782 SET cancellationdate=now()
783 WHERE biblionumber = ?
784 AND borrowernumber = ?
786 my $sth = $dbh->prepare($query);
787 $sth->execute( $biblio, $borrower );
790 INSERT INTO old_reserves
793 WHERE biblionumber = ?
794 AND borrowernumber = ?
796 $sth = $dbh->prepare($query);
797 $sth->execute( $biblio, $borrower );
800 WHERE biblionumber = ?
801 AND borrowernumber = ?
803 $sth = $dbh->prepare($query);
804 $sth->execute( $biblio, $borrower );
809 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL
810 WHERE biblionumber = ?
811 AND borrowernumber = ?
813 my $sth = $dbh->prepare($query);
814 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
816 _FixPriority( $biblio, $borrower, $rank);
822 &ModReserveFill($reserve);
824 Fill a reserve. If I understand this correctly, this means that the
825 reserved book has been found and given to the patron who reserved it.
827 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
828 whose keys are fields from the reserves table in the Koha database.
834 my $dbh = C4::Context->dbh;
835 # fill in a reserve record....
836 my $biblionumber = $res->{'biblionumber'};
837 my $borrowernumber = $res->{'borrowernumber'};
838 my $resdate = $res->{'reservedate'};
840 # get the priority on this record....
842 my $query = "SELECT priority
844 WHERE biblionumber = ?
845 AND borrowernumber = ?
846 AND reservedate = ?";
847 my $sth = $dbh->prepare($query);
848 $sth->execute( $biblionumber, $borrowernumber, $resdate );
849 ($priority) = $sth->fetchrow_array;
852 # update the database...
853 $query = "UPDATE reserves
856 WHERE biblionumber = ?
858 AND borrowernumber = ?
860 $sth = $dbh->prepare($query);
861 $sth->execute( $biblionumber, $resdate, $borrowernumber );
864 # move to old_reserves
865 $query = "INSERT INTO old_reserves
866 SELECT * FROM reserves
867 WHERE biblionumber = ?
869 AND borrowernumber = ?
871 $sth = $dbh->prepare($query);
872 $sth->execute( $biblionumber, $resdate, $borrowernumber );
873 $query = "DELETE FROM reserves
874 WHERE biblionumber = ?
876 AND borrowernumber = ?
878 $sth = $dbh->prepare($query);
879 $sth->execute( $biblionumber, $resdate, $borrowernumber );
881 # now fix the priority on the others (if the priority wasn't
882 # already sorted!)....
883 unless ( $priority == 0 ) {
884 _FixPriority($biblionumber, $borrowernumber, $priority);
888 =item ModReserveStatus
890 &ModReserveStatus($itemnumber, $newstatus);
892 Update the reserve status for the active (priority=0) reserve.
894 $itemnumber is the itemnumber the reserve is on
896 $newstatus is the new status.
900 sub ModReserveStatus {
902 #first : check if we have a reservation for this item .
903 my ($itemnumber, $newstatus) = @_;
904 my $dbh = C4::Context->dbh;
905 my $query = " UPDATE reserves
906 SET found=?,waitingdate = now()
911 my $sth_set = $dbh->prepare($query);
912 $sth_set->execute( $newstatus, $itemnumber );
916 =item ModReserveAffect
918 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
920 This function affect an item and a status for a given reserve
921 The itemnumber parameter is used to find the biblionumber.
922 with the biblionumber & the borrowernumber, we can affect the itemnumber
923 to the correct reserve.
925 if $transferToDo is not set, then the status is set to "Waiting" as well.
926 otherwise, a transfer is on the way, and the end of the transfer will
927 take care of the waiting status
930 sub ModReserveAffect {
931 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
932 my $dbh = C4::Context->dbh;
934 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
935 # attached to $itemnumber
936 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
937 $sth->execute($itemnumber);
938 my ($biblionumber) = $sth->fetchrow;
939 # If we affect a reserve that has to be transfered, don't set to Waiting
946 WHERE borrowernumber = ?
951 # affect the reserve to Waiting as well.
958 WHERE borrowernumber = ?
962 $sth = $dbh->prepare($query);
963 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
968 =item ModReserveCancelAll
970 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
972 function to cancel reserv,check other reserves, and transfer document if it's necessary
976 sub ModReserveCancelAll {
979 my ( $itemnumber, $borrowernumber ) = @_;
981 #step 1 : cancel the reservation
982 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
984 #step 2 launch the subroutine of the others reserves
985 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
987 return ( $messages, $nextreservinfo );
990 =item ModReserveMinusPriority
992 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
994 Reduce the values of queuded list
998 sub ModReserveMinusPriority {
999 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1001 #first step update the value of the first person on reserv
1002 my $dbh = C4::Context->dbh;
1005 SET priority = 0 , itemnumber = ?
1006 WHERE borrowernumber=?
1009 my $sth_upd = $dbh->prepare($query);
1010 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1012 # second step update all others reservs
1015 SET priority = priority-1
1016 WHERE biblionumber = ?
1019 $sth_upd = $dbh->prepare($query);
1020 $sth_upd->execute( $biblionumber );
1025 =item GetReserveInfo
1027 &GetReserveInfo($borrowernumber,$biblionumber);
1029 Get item and borrower details for a current hold.
1030 Current implementation this query should have a single result.
1033 sub GetReserveInfo {
1034 my ( $borrowernumber, $biblionumber ) = @_;
1035 my $dbh = C4::Context->dbh;
1036 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1037 reserves.biblionumber, reserves.branchcode,
1038 notificationdate, reminderdate, priority, found,
1039 firstname, surname, phone,
1040 email, address, address2,
1041 cardnumber, city, zipcode,
1042 biblio.title, biblio.author,
1043 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1045 FROM reserves left join items
1046 ON items.itemnumber=reserves.itemnumber ,
1049 reserves.borrowernumber=? &&
1050 reserves.biblionumber=? &&
1051 reserves.borrowernumber=borrowers.borrowernumber &&
1052 reserves.biblionumber=biblio.biblionumber ";
1053 my $sth = $dbh->prepare($strsth);
1054 $sth->execute($borrowernumber,$biblionumber);
1056 my $data = $sth->fetchrow_hashref;
1063 &_FixPriority($biblio,$borrowernumber,$rank);
1065 Only used internally (so don't export it)
1066 Changed how this functions works #
1067 Now just gets an array of reserves in the rank order and updates them with
1068 the array index (+1 as array starts from 0)
1069 and if $rank is supplied will splice item from the array and splice it back in again
1070 in new priority rank
1075 my ( $biblio, $borrowernumber, $rank ) = @_;
1076 my $dbh = C4::Context->dbh;
1077 if ( $rank eq "del" ) {
1078 CancelReserve( $biblio, undef, $borrowernumber );
1080 if ( $rank eq "W" || $rank eq "0" ) {
1082 # make sure priority for waiting items is 0
1086 WHERE biblionumber = ?
1087 AND borrowernumber = ?
1090 my $sth = $dbh->prepare($query);
1091 $sth->execute( $biblio, $borrowernumber );
1097 # FIXME adding a new security in returned elements for changing priority,
1098 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1099 # This is wrong a waiting reserve has W set
1100 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1102 SELECT borrowernumber, reservedate, constrainttype
1104 WHERE biblionumber = ?
1105 AND ((found <> 'W') or found is NULL)
1106 ORDER BY priority ASC
1108 my $sth = $dbh->prepare($query);
1109 $sth->execute($biblio);
1110 while ( my $line = $sth->fetchrow_hashref ) {
1111 push( @reservedates, $line );
1112 push( @priority, $line );
1115 # To find the matching index
1117 my $key = -1; # to allow for 0 to be a valid result
1118 for ( $i = 0 ; $i < @priority ; $i++ ) {
1119 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1120 $key = $i; # save the index
1125 # if index exists in array then move it to new position
1126 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1127 my $new_rank = $rank -
1128 1; # $new_rank is what you want the new index to be in the array
1129 my $moving_item = splice( @priority, $key, 1 );
1130 splice( @priority, $new_rank, 0, $moving_item );
1133 # now fix the priority on those that are left....
1137 WHERE biblionumber = ?
1138 AND borrowernumber = ?
1142 $sth = $dbh->prepare($query);
1143 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1146 $priority[$j]->{'borrowernumber'},
1147 $priority[$j]->{'reservedate'}
1153 =item _Findgroupreserve
1155 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber);
1158 I don't know what this does, because I don't understand how reserve
1159 constraints work. I think the idea is that you reserve a particular
1160 biblio, and the constraint allows you to restrict it to a given
1161 biblioitem (e.g., if you want to borrow the audio book edition of "The
1162 Prophet", rather than the first available publication).
1164 C<&_Findgroupreserve> returns :
1165 C<@results> is an array of references-to-hash whose keys are mostly
1166 fields from the reserves table of the Koha database, plus
1167 C<biblioitemnumber>.
1171 sub _Findgroupreserve {
1172 my ( $bibitem, $biblio ) = @_;
1173 my $dbh = C4::Context->dbh;
1175 SELECT reserves.biblionumber AS biblionumber,
1176 reserves.borrowernumber AS borrowernumber,
1177 reserves.reservedate AS reservedate,
1178 reserves.branchcode AS branchcode,
1179 reserves.cancellationdate AS cancellationdate,
1180 reserves.found AS found,
1181 reserves.reservenotes AS reservenotes,
1182 reserves.priority AS priority,
1183 reserves.timestamp AS timestamp,
1184 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1185 reserves.itemnumber AS itemnumber
1187 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1188 WHERE reserves.biblionumber = ?
1189 AND ( ( reserveconstraints.biblioitemnumber = ?
1190 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1191 AND reserves.reservedate =reserveconstraints.reservedate )
1192 OR reserves.constrainttype='a' )
1194 my $sth = $dbh->prepare($query);
1195 $sth->execute( $biblio, $bibitem );
1196 return $sth->fetchall_arrayref({});
1203 Koha Developement team <info@koha.org>