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
36 # an ugly hack to ensure that
37 # various subs get imported
38 # into C4::Reserves' symbol table
39 # FIXME: hopefully can remove once
40 # we get a better idea of exactly
41 # how Exporter/use/require/import
42 # should be used with modules
43 # that currently call functions
48 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,%EXPORT_TAGS);
50 my $library_name = C4
::Context
->preference("LibraryName");
52 # set the version for version checking
57 C4::Reserves - Koha functions for dealing with reservation.
65 this modules provides somes functions to deal with reservations.
67 Reserves are stored in reserves table.
68 The following columns contains important values :
69 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
70 =0 : then the reserve is being dealed
71 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
72 W(aiting) : the reserve has an itemnumber affected, and is on the way
73 F(inished) : the reserve has been completed, and is done
74 - itemnumber : empty : the reserve is still unaffected to an item
75 filled: the reserve is attached to an item
76 The complete workflow is :
77 ==== 1st use case ====
78 patron request a document, 1st available : P >0, F=NULL, I=NULL
79 a library having it run "transfertodo", and clic on the list
80 if there is no transfer to do, the reserve waiting
81 patron can pick it up P =0, F=W, I=filled
82 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
83 The pickup library recieve the book, it check in P =0, F=W, I=filled
84 The patron borrow the book P =0, F=F, I=filled
86 ==== 2nd use case ====
87 patron requests a document, a given item,
88 If pickup is holding branch P =0, F=W, I=filled
89 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
90 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
91 The patron borrow the book P =0, F=F, I=filled
104 &GetReservesFromItemnumber
105 &GetReservesFromBiblionumber
106 &GetReservesFromBorrowernumber
107 &GetReservesForBranch
119 &ModReserveMinusPriority
128 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
134 $branch, $borrowernumber, $biblionumber,
135 $constraint, $bibitems, $priority, $notes,
136 $title, $checkitem, $found
139 GetReserveFee
($borrowernumber, $biblionumber, $constraint,
141 my $dbh = C4
::Context
->dbh;
142 my $const = lc substr( $constraint, 0, 1 );
143 my @datearr = localtime(time);
145 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
148 # If the reserv had the waiting status, we had the value of the resdate
149 if ( $found eq 'W' ) {
150 $waitingdate = $resdate;
154 # updates take place here
156 my $nextacctno = &getnextacctno
( $borrowernumber );
158 INSERT INTO accountlines
159 (borrowernumber
,accountno
,date
,amount
,description
,accounttype
,amountoutstanding
)
161 (?
,?
,now
(),?
,?
,'Res',?
)
163 my $usth = $dbh->prepare($query);
164 $usth->execute( $borrowernumber, $nextacctno, $fee,
165 "Reserve Charge - $title", $fee );
172 (borrowernumber
,biblionumber
,reservedate
,branchcode
,constrainttype
,
173 priority
,reservenotes
,itemnumber
,found
,waitingdate
)
178 my $sth = $dbh->prepare($query);
180 $borrowernumber, $biblionumber, $resdate, $branch,
181 $const, $priority, $notes, $checkitem,
187 if ( ( $const eq "o" ) || ( $const eq "e" ) ) {
188 my $numitems = @
$bibitems;
190 while ( $i < $numitems ) {
191 my $biblioitem = @
$bibitems[$i];
193 INSERT INTO reserveconstraints
194 (borrowernumber
,biblionumber
,reservedate
,biblioitemnumber
)
198 my $sth = $dbh->prepare("");
199 $sth->execute( $borrowernumber, $biblionumber, $resdate,
208 =item GetReservesFromBiblionumber
210 @borrowerreserv=&GetReserves($biblionumber,$itemnumber,$borrowernumber);
212 this function get the list of reservation for an C<$biblionumber>, C<$itemnumber> or C<$borrowernumber>
214 Only 1 argument has to be passed.
218 sub GetReservesFromBiblionumber
{
219 my ( $biblionumber, $itemnumber, $borrowernumber ) = @_;
220 my $dbh = C4
::Context
->dbh;
222 # Find the desired items in the reserves
225 timestamp AS rtimestamp,
235 WHERE cancellationdate IS NULL
236 AND (found <> \'F\' OR found IS NULL)
239 my $sth = $dbh->prepare($query);
240 $sth->execute($biblionumber);
243 while ( my $data = $sth->fetchrow_hashref ) {
245 # FIXME - What is this if-statement doing? How do constraints work?
246 if ( $data->{constrainttype
} eq 'o' ) {
248 SELECT biblioitemnumber
249 FROM reserveconstraints
250 WHERE biblionumber = ?
251 AND borrowernumber = ?
254 my $csth = $dbh->prepare($query);
255 $csth->execute( $data->{biblionumber
}, $data->{borrowernumber
},
256 $data->{reservedate
}, );
259 while ( my $bibitemnos = $csth->fetchrow_array ) {
260 push( @bibitemno, $bibitemnos );
262 my $count = @bibitemno;
264 # if we have two or more different specific itemtypes
265 # reserved by same person on same day
268 $bdata = GetBiblioItemData
( $bibitemno[$i] );
273 # Look up the book we just found.
274 $bdata = GetBiblioItemData
( $bibitemno[0] );
278 # Add the results of this latest search to the current
280 # FIXME - An 'each' would probably be more efficient.
281 foreach my $key ( keys %$bdata ) {
282 $data->{$key} = $bdata->{$key};
285 push @results, $data;
288 return ( $#results + 1, \
@results );
291 =item GetReservesFromItemnumber
293 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
295 TODO :: Description here
299 sub GetReservesFromItemnumber
{
300 my ( $itemnumber ) = @_;
301 my $dbh = C4
::Context
->dbh;
303 SELECT reservedate,borrowernumber,branchcode
306 AND cancellationdate IS NULL
307 AND (found <> 'F' OR found IS NULL)
309 my $sth_res = $dbh->prepare($query);
310 $sth_res->execute($itemnumber);
311 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
312 return ( $reservedate, $borrowernumber, $branchcode );
315 =item GetReservesFromBorrowernumber
317 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
323 sub GetReservesFromBorrowernumber
{
324 my ( $borrowernumber, $status ) = @_;
325 my $dbh = C4
::Context
->dbh;
328 $sth = $dbh->prepare("
331 WHERE borrowernumber=?
332 AND cancellationdate IS NULL
336 $sth->execute($borrowernumber,$status);
338 $sth = $dbh->prepare("
341 WHERE borrowernumber=?
342 AND cancellationdate IS NULL
343 AND (found != 'F' or found is null)
346 $sth->execute($borrowernumber);
348 my $data = $sth->fetchall_arrayref({});
351 #-------------------------------------------------------------------------------------
353 =item GetReserveCount
355 $number = &GetReserveCount($borrowernumber);
357 this function returns the number of reservation for a borrower given on input arg.
361 sub GetReserveCount
{
362 my ($borrowernumber) = @_;
364 my $dbh = C4
::Context
->dbh;
367 SELECT COUNT(*) AS counter
369 WHERE borrowernumber = ?
370 AND cancellationdate IS NULL
371 AND (found != \'F\' OR found IS NULL)
373 my $sth = $dbh->prepare($query);
374 $sth->execute($borrowernumber);
375 my $row = $sth->fetchrow_hashref;
378 return $row->{counter
};
381 =item GetOtherReserves
383 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
385 Check queued list of this document and check if this document must be transfered
389 sub GetOtherReserves
{
390 my ($itemnumber) = @_;
393 my ( $restype, $checkreserves ) = CheckReserves
($itemnumber);
394 if ($checkreserves) {
395 my $iteminfo = GetItem
($itemnumber);
396 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
397 $messages->{'transfert'} = $checkreserves->{'branchcode'};
398 #minus priorities of others reservs
399 ModReserveMinusPriority
(
401 $checkreserves->{'borrowernumber'},
402 $iteminfo->{'biblionumber'}
405 #launch the subroutine dotransfer
406 C4
::Circulation
::ModItemTransfer
(
408 $iteminfo->{'holdingbranch'},
409 $checkreserves->{'branchcode'}
414 #step 2b : case of a reservation on the same branch, set the waiting status
416 $messages->{'waiting'} = 1;
417 ModReserveMinusPriority
(
419 $checkreserves->{'borrowernumber'},
420 $iteminfo->{'biblionumber'}
422 ModReserveStatus
($itemnumber,'W');
425 $nextreservinfo = $checkreserves->{'borrowernumber'};
428 return ( $messages, $nextreservinfo );
433 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
435 Calculate the fee for a reserve
440 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
443 my $dbh = C4
::Context
->dbh;
444 my $const = lc substr( $constraint, 0, 1 );
446 SELECT
* FROM borrowers
447 LEFT JOIN categories ON borrowers
.categorycode
= categories
.categorycode
448 WHERE borrowernumber
= ?
450 my $sth = $dbh->prepare($query);
451 $sth->execute($borrowernumber);
452 my $data = $sth->fetchrow_hashref;
454 my $fee = $data->{'reservefee'};
455 my $cntitems = @
- > $bibitems;
459 # check for items on issue
460 # first find biblioitem records
462 my $sth1 = $dbh->prepare(
463 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
464 WHERE (biblio.biblionumber = ?)"
466 $sth1->execute($biblionumber);
467 while ( my $data1 = $sth1->fetchrow_hashref ) {
468 if ( $const eq "a" ) {
469 push @biblioitems, $data1;
474 while ( $x < $cntitems ) {
475 if ( @
$bibitems->{'biblioitemnumber'} ==
476 $data->{'biblioitemnumber'} )
482 if ( $const eq 'o' ) {
484 push @biblioitems, $data1;
489 push @biblioitems, $data1;
495 my $cntitemsfound = @biblioitems;
499 while ( $x < $cntitemsfound ) {
500 my $bitdata = $biblioitems[$x];
501 my $sth2 = $dbh->prepare(
503 WHERE biblioitemnumber = ?"
505 $sth2->execute( $bitdata->{'biblioitemnumber'} );
506 while ( my $itdata = $sth2->fetchrow_hashref ) {
507 my $sth3 = $dbh->prepare(
508 "SELECT * FROM issues
510 AND returndate IS NULL"
512 $sth3->execute( $itdata->{'itemnumber'} );
513 if ( my $isdata = $sth3->fetchrow_hashref ) {
521 if ( $allissued == 0 ) {
523 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
524 $rsth->execute($biblionumber);
525 if ( my $rdata = $rsth->fetchrow_hashref ) {
535 =item GetReservesToBranch
537 @transreserv = GetReservesToBranch( $frombranch );
539 Get reserve list for a given branch
543 sub GetReservesToBranch
{
544 my ( $frombranch ) = @_;
545 my $dbh = C4
::Context
->dbh;
546 my $sth = $dbh->prepare(
547 "SELECT borrowernumber,reservedate,itemnumber,timestamp
549 WHERE priority='0' AND cancellationdate is null
553 $sth->execute( $frombranch );
556 while ( my $data = $sth->fetchrow_hashref ) {
557 $transreserv[$i] = $data;
561 return (@transreserv);
564 =item GetReservesForBranch
566 @transreserv = GetReservesForBranch($frombranch);
570 sub GetReservesForBranch
{
571 my ($frombranch) = @_;
572 my $dbh = C4
::Context
->dbh;
573 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
576 AND cancellationdate IS NULL
579 $query .= " AND branchcode=? ";
581 $query .= "ORDER BY waitingdate" ;
582 my $sth = $dbh->prepare($query);
584 $sth->execute($frombranch);
591 while ( my $data = $sth->fetchrow_hashref ) {
592 $transreserv[$i] = $data;
596 return (@transreserv);
601 ($status, $reserve) = &CheckReserves($itemnumber);
603 Find a book in the reserves.
605 C<$itemnumber> is the book's item number.
607 As I understand it, C<&CheckReserves> looks for the given item in the
608 reserves. If it is found, that's a match, and C<$status> is set to
611 Otherwise, it finds the most important item in the reserves with the
612 same biblio number as this book (I'm not clear on this) and returns it
613 with C<$status> set to C<Reserved>.
615 C<&CheckReserves> returns a two-element list:
617 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
619 C<$reserve> is the reserve item that matched. It is a
620 reference-to-hash whose keys are mostly the fields of the reserves
621 table in the Koha database.
626 my ( $item, $barcode ) = @_;
627 my $dbh = C4
::Context
->dbh;
630 my $qitem = $dbh->quote($item);
631 # Look up the item by itemnumber
633 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan
635 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
636 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
637 WHERE itemnumber=$qitem
639 $sth = $dbh->prepare($query);
642 my $qbc = $dbh->quote($barcode);
643 # Look up the item by barcode
645 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan
647 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
648 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
649 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
650 AND biblioitems.itemtype = itemtypes.itemtype
653 $sth = $dbh->prepare($query);
655 # FIXME - This function uses $item later on. Ought to set it here.
658 my ( $biblio, $bibitem, $notforloan ) = $sth->fetchrow_array;
661 # if item is not for loan it cannot be reserved either.....
662 return ( 0, 0 ) if $notforloan;
664 # get the reserves...
665 # Find this item in the reserves
666 my @reserves = _Findgroupreserve
( $bibitem, $biblio );
667 my $count = scalar @reserves;
669 # $priority and $highest are used to find the most important item
670 # in the list returned by &_Findgroupreserve. (The lower $priority,
671 # the more important the item.)
672 # $highest is the most important item we've seen so far.
673 my $priority = 10000000;
676 foreach my $res (@reserves) {
677 # FIXME - $item might be undefined or empty: the caller
678 # might be searching by barcode.
679 if ( $res->{'itemnumber'} == $item ) {
681 return ( "Waiting", $res );
684 # See if this item is more important than what we've got
686 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
688 $priority = $res->{'priority'};
695 # If we get this far, then no exact match was found. Print the
696 # most important item on the list. I think this tells us who's
697 # next in line to get this book.
698 if ($highest) { # FIXME - $highest might be undefined
699 $highest->{'itemnumber'} = $item;
700 return ( "Reserved", $highest );
709 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
713 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
714 cancel, but not both: if both are given, C<&CancelReserve> does
717 C<$borrowernumber> is the borrower number of the patron on whose
718 behalf the book was reserved.
720 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
721 priorities of the other people who are waiting on the book.
726 my ( $biblio, $item, $borr ) = @_;
727 my $dbh = C4
::Context
->dbh;
728 if ( $item and $borr ) {
729 # removing a waiting reserve record....
730 # update the database...
733 SET cancellationdate = now(),
737 AND borrowernumber = ?
739 my $sth = $dbh->prepare($query);
740 $sth->execute( $item, $borr );
744 # removing a reserve record....
745 # get the prioritiy on this record....
748 SELECT priority FROM reserves
749 WHERE biblionumber
= ?
750 AND borrowernumber
= ?
751 AND cancellationdate IS NULL
752 AND itemnumber IS NULL
753 AND
(found
<> 'F' OR found IS NULL
)
755 my $sth = $dbh->prepare($query);
756 $sth->execute( $biblio, $borr );
757 ($priority) = $sth->fetchrow_array;
761 SET cancellationdate
= now
(),
764 WHERE biblionumber
= ?
765 AND borrowernumber
= ?
766 AND cancellationdate IS NULL
767 AND
(found
<> 'F' or found IS NULL
)
770 # update the database, removing the record...
771 $sth = $dbh->prepare($query);
772 $sth->execute( $biblio, $borr );
775 # now fix the priority on the others....
776 _FixPriority
( $priority, $biblio );
782 &ModReserve($rank,$biblio,$borrower,$branch)
787 #subroutine to update a reserve
788 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
789 return if $rank eq "W";
790 return if $rank eq "n";
791 my $dbh = C4
::Context
->dbh;
792 if ( $rank eq "del" ) {
795 SET cancellationdate
=now
()
796 WHERE biblionumber
= ?
797 AND borrowernumber
= ?
798 AND cancellationdate is NULL
799 AND
(found
<> 'F' or found is NULL
)
801 my $sth = $dbh->prepare($query);
802 $sth->execute( $biblio, $borrower );
808 UPDATE reserves SET priority
= ?
,branchcode
= ?
, itemnumber
= ?
, found
= NULL
809 WHERE biblionumber
= ?
810 AND borrowernumber
= ?
811 AND cancellationdate is NULL
812 AND
(found
<> 'F' or found is NULL
)
814 my $sth = $dbh->prepare($query);
815 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
817 _FixPriority
( $biblio, $borrower, $rank);
823 &ModReserveFill($reserve);
825 Fill a reserve. If I understand this correctly, this means that the
826 reserved book has been found and given to the patron who reserved it.
828 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
829 whose keys are fields from the reserves table in the Koha database.
835 my $dbh = C4
::Context
->dbh;
836 # fill in a reserve record....
837 my $biblionumber = $res->{'biblionumber'};
838 my $borrowernumber = $res->{'borrowernumber'};
839 my $resdate = $res->{'reservedate'};
841 # get the priority on this record....
843 my $query = "SELECT priority
845 WHERE biblionumber = ?
846 AND borrowernumber = ?
847 AND reservedate = ?";
848 my $sth = $dbh->prepare($query);
849 $sth->execute( $biblionumber, $borrowernumber, $resdate );
850 ($priority) = $sth->fetchrow_array;
853 # update the database...
854 $query = "UPDATE reserves
857 WHERE biblionumber = ?
859 AND borrowernumber = ?
861 $sth = $dbh->prepare($query);
862 $sth->execute( $biblionumber, $resdate, $borrowernumber );
865 # now fix the priority on the others (if the priority wasn't
866 # already sorted!)....
867 unless ( $priority == 0 ) {
868 _FixPriority
( $priority, $biblionumber );
872 =item ModReserveStatus
874 &ModReserveStatus($itemnumber, $newstatus);
876 Update the reserve status for the active (priority=0) reserve.
878 $itemnumber is the itemnumber the reserve is on
880 $newstatus is the new status.
884 sub ModReserveStatus
{
886 #first : check if we have a reservation for this item .
887 my ($itemnumber, $newstatus) = @_;
888 my $dbh = C4
::Context
->dbh;
889 my $query = " UPDATE reserves
890 SET found=?,waitingdate = now()
895 my $sth_set = $dbh->prepare($query);
896 $sth_set->execute( $newstatus, $itemnumber );
900 =item ModReserveAffect
902 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
904 This function affect an item and a status for a given reserve
905 The itemnumber parameter is used to find the biblionumber.
906 with the biblionumber & the borrowernumber, we can affect the itemnumber
907 to the correct reserve.
909 if $transferToDo is not set, then the status is set to "Waiting" as well.
910 otherwise, a transfer is on the way, and the end of the transfer will
911 take care of the waiting status
914 sub ModReserveAffect
{
915 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
916 my $dbh = C4
::Context
->dbh;
918 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
919 # attached to $itemnumber
920 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
921 $sth->execute($itemnumber);
922 my ($biblionumber) = $sth->fetchrow;
923 # If we affect a reserve that has to be transfered, don't set to Waiting
930 WHERE borrowernumber = ?
932 AND reserves.cancellationdate IS NULL
933 AND (reserves.found <> 'F' OR reserves.found IS NULL)
937 # affect the reserve to Waiting as well.
944 WHERE borrowernumber = ?
946 AND reserves.cancellationdate IS NULL
947 AND (reserves.found <> 'F' OR reserves.found IS NULL)
950 $sth = $dbh->prepare($query);
951 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
956 =item ModReserveCancelAll
958 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
960 function to cancel reserv,check other reserves, and transfer document if it's necessary
964 sub ModReserveCancelAll
{
967 my ( $itemnumber, $borrowernumber ) = @_;
969 #step 1 : cancel the reservation
970 my $CancelReserve = CancelReserve
( undef, $itemnumber, $borrowernumber );
972 #step 2 launch the subroutine of the others reserves
973 ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
975 return ( $messages, $nextreservinfo );
978 =item ModReserveMinusPriority
980 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
982 Reduce the values of queuded list
986 sub ModReserveMinusPriority
{
987 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
989 #first step update the value of the first person on reserv
990 my $dbh = C4
::Context
->dbh;
993 SET priority = 0 , itemnumber = ?
994 WHERE cancellationdate IS NULL
998 my $sth_upd = $dbh->prepare($query);
999 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1001 # second step update all others reservs
1004 SET priority = priority-1
1005 WHERE biblionumber = ?
1007 AND cancellationdate IS NULL
1009 $sth_upd = $dbh->prepare($query);
1010 $sth_upd->execute( $biblionumber );
1017 &_FixPriority($biblio,$borrowernumber,$rank);
1019 Only used internally (so don't export it)
1020 Changed how this functions works #
1021 Now just gets an array of reserves in the rank order and updates them with
1022 the array index (+1 as array starts from 0)
1023 and if $rank is supplied will splice item from the array and splice it back in again
1024 in new priority rank
1029 my ( $biblio, $borrowernumber, $rank ) = @_;
1030 my $dbh = C4
::Context
->dbh;
1031 if ( $rank eq "del" ) {
1032 CancelReserve
( $biblio, undef, $borrowernumber );
1034 if ( $rank eq "W" || $rank eq "0" ) {
1036 # make sure priority for waiting items is 0
1040 WHERE biblionumber
= ?
1041 AND borrowernumber
= ?
1042 AND cancellationdate IS NULL
1045 my $sth = $dbh->prepare($query);
1046 $sth->execute( $biblio, $borrowernumber );
1052 # FIXME adding a new security in returned elements for changing priority,
1053 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1054 # This is wrong a waiting reserve has W set
1055 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1057 SELECT borrowernumber
, reservedate
, constrainttype
1059 WHERE biblionumber
= ?
1060 AND cancellationdate IS NULL
1061 AND
((found
<> 'F' and found
<> 'W') or found is NULL
)
1062 ORDER BY priority ASC
1064 my $sth = $dbh->prepare($query);
1065 $sth->execute($biblio);
1066 while ( my $line = $sth->fetchrow_hashref ) {
1067 push( @reservedates, $line );
1068 push( @priority, $line );
1071 # To find the matching index
1073 my $key = -1; # to allow for 0 to be a valid result
1074 for ( $i = 0 ; $i < @priority ; $i++ ) {
1075 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1076 $key = $i; # save the index
1081 # if index exists in array then move it to new position
1082 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1083 my $new_rank = $rank -
1084 1; # $new_rank is what you want the new index to be in the array
1085 my $moving_item = splice( @priority, $key, 1 );
1086 splice( @priority, $new_rank, 0, $moving_item );
1089 # now fix the priority on those that are left....
1093 WHERE biblionumber = ?
1094 AND borrowernumber = ?
1098 $sth = $dbh->prepare($query);
1099 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1102 $priority[$j]->{'borrowernumber'},
1103 $priority[$j]->{'reservedate'}
1109 =item _Findgroupreserve
1111 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber);
1114 I don't know what this does, because I don't understand how reserve
1115 constraints work. I think the idea is that you reserve a particular
1116 biblio, and the constraint allows you to restrict it to a given
1117 biblioitem (e.g., if you want to borrow the audio book edition of "The
1118 Prophet", rather than the first available publication).
1120 C<&_Findgroupreserve> returns :
1121 C<@results> is an array of references-to-hash whose keys are mostly
1122 fields from the reserves table of the Koha database, plus
1123 C<biblioitemnumber>.
1127 sub _Findgroupreserve
{
1128 my ( $bibitem, $biblio ) = @_;
1129 my $dbh = C4
::Context
->dbh;
1131 SELECT reserves
.biblionumber AS biblionumber
,
1132 reserves
.borrowernumber AS borrowernumber
,
1133 reserves
.reservedate AS reservedate
,
1134 reserves
.branchcode AS branchcode
,
1135 reserves
.cancellationdate AS cancellationdate
,
1136 reserves
.found AS found
,
1137 reserves
.reservenotes AS reservenotes
,
1138 reserves
.priority AS priority
,
1139 reserves
.timestamp AS timestamp
,
1140 reserveconstraints
.biblioitemnumber AS biblioitemnumber
,
1141 reserves
.itemnumber AS itemnumber
1143 LEFT JOIN reserveconstraints ON reserves
.biblionumber
= reserveconstraints
.biblionumber
1144 WHERE reserves
.biblionumber
= ?
1145 AND
( ( reserveconstraints
.biblioitemnumber
= ?
1146 AND reserves
.borrowernumber
= reserveconstraints
.borrowernumber
1147 AND reserves
.reservedate
=reserveconstraints
.reservedate
)
1148 OR reserves
.constrainttype
='a' )
1149 AND reserves
.cancellationdate is NULL
1150 AND
(reserves
.found
<> 'F' or reserves
.found is NULL
)
1152 my $sth = $dbh->prepare($query);
1153 $sth->execute( $biblio, $bibitem );
1155 while ( my $data = $sth->fetchrow_hashref ) {
1156 push( @results, $data );
1166 Koha Developement team <info@koha.org>