3 # Copyright 2000-2002 Katipo Communications
4 # 2006 SAN Ouest Provence
5 # 2007-2010 BibLibre Paul POULAIN
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #use warnings; FIXME - Bug 2505
33 # for _koha_notify_reserve
34 use C4
::Members
::Messaging
;
37 use C4
::Branch
qw( GetBranchDetail );
38 use C4
::Dates
qw( format_date_in_iso );
42 use List
::MoreUtils
qw( firstidx );
44 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
48 C4::Reserves - Koha functions for dealing with reservation.
56 This modules provides somes functions to deal with reservations.
58 Reserves are stored in reserves table.
59 The following columns contains important values :
60 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
61 =0 : then the reserve is being dealed
62 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
63 T(ransit) : the reserve is linked to an item but is in transit to the pickup branch
64 W(aiting) : the reserve is linked to an item, is at the pickup branch, and is waiting on the hold shelf
65 F(inished) : the reserve has been completed, and is done
66 - itemnumber : empty : the reserve is still unaffected to an item
67 filled: the reserve is attached to an item
68 The complete workflow is :
69 ==== 1st use case ====
70 patron request a document, 1st available : P >0, F=NULL, I=NULL
71 a library having it run "transfertodo", and clic on the list
72 if there is no transfer to do, the reserve waiting
73 patron can pick it up P =0, F=W, I=filled
74 if there is a transfer to do, write in branchtransfer P =0, F=T, I=filled
75 The pickup library recieve the book, it check in P =0, F=W, I=filled
76 The patron borrow the book P =0, F=F, I=filled
78 ==== 2nd use case ====
79 patron requests a document, a given item,
80 If pickup is holding branch P =0, F=W, I=filled
81 If transfer needed, write in branchtransfer P =0, F=T, I=filled
82 The pickup library receive the book, it checks it in P =0, F=W, I=filled
83 The patron borrow the book P =0, F=F, I=filled
90 # set the version for version checking
91 $VERSION = 3.07.00.049;
98 &GetReservesFromItemnumber
99 &GetReservesFromBiblionumber
100 &GetReservesFromBorrowernumber
101 &GetReservesForBranch
115 &ModReserveMinusPriority
122 &CancelExpiredReserves
124 &AutoUnsuspendReserves
126 &IsAvailableForItemLevelRequest
129 &ToggleLowestPriority
135 &GetReservesControlBranch
137 @EXPORT_OK = qw( MergeHolds );
142 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found)
148 $branch, $borrowernumber, $biblionumber,
149 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
150 $title, $checkitem, $found
153 GetReserveFee
($borrowernumber, $biblionumber, $constraint,
155 my $dbh = C4
::Context
->dbh;
156 my $const = lc substr( $constraint, 0, 1 );
157 $resdate = format_date_in_iso
( $resdate ) if ( $resdate );
158 $resdate = C4
::Dates
->today( 'iso' ) unless ( $resdate );
160 $expdate = format_date_in_iso
( $expdate );
162 undef $expdate; # make reserves.expirationdate default to null rather than '0000-00-00'
164 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
165 # Make room in reserves for this before those of a later reserve date
166 $priority = _ShiftPriorityByDateAndPriority
( $biblionumber, $resdate, $priority );
170 # If the reserv had the waiting status, we had the value of the resdate
171 if ( $found eq 'W' ) {
172 $waitingdate = $resdate;
176 # updates take place here
178 my $nextacctno = &getnextacctno
( $borrowernumber );
180 INSERT INTO accountlines
181 (borrowernumber
,accountno
,date
,amount
,description
,accounttype
,amountoutstanding
)
183 (?
,?
,now
(),?
,?
,'Res',?
)
185 my $usth = $dbh->prepare($query);
186 $usth->execute( $borrowernumber, $nextacctno, $fee,
187 "Reserve Charge - $title", $fee );
193 (borrowernumber
,biblionumber
,reservedate
,branchcode
,constrainttype
,
194 priority
,reservenotes
,itemnumber
,found
,waitingdate
,expirationdate
)
199 my $sth = $dbh->prepare($query);
201 $borrowernumber, $biblionumber, $resdate, $branch,
202 $const, $priority, $notes, $checkitem,
203 $found, $waitingdate, $expdate
206 # Send e-mail to librarian if syspref is active
207 if(C4
::Context
->preference("emailLibrarianWhenHoldIsPlaced")){
208 my $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrowernumber);
209 my $branch_details = C4
::Branch
::GetBranchDetail
($borrower->{branchcode
});
210 if ( my $letter = C4
::Letters
::GetPreparedLetter
(
211 module
=> 'reserves',
212 letter_code
=> 'HOLDPLACED',
213 branchcode
=> $branch,
215 'branches' => $branch_details,
216 'borrowers' => $borrower,
217 'biblio' => $biblionumber,
218 'items' => $checkitem,
222 my $admin_email_address =$branch_details->{'branchemail'} || C4
::Context
->preference('KohaAdminEmailAddress');
224 C4
::Letters
::EnqueueLetter
(
226 borrowernumber
=> $borrowernumber,
227 message_transport_type
=> 'email',
228 from_address
=> $admin_email_address,
229 to_address
=> $admin_email_address,
236 ($const eq "o" || $const eq "e") or return; # FIXME: why not have a useful return value?
238 INSERT INTO reserveconstraints
239 (borrowernumber
,biblionumber
,reservedate
,biblioitemnumber
)
243 $sth = $dbh->prepare($query); # keep prepare outside the loop!
244 foreach (@
$bibitems) {
245 $sth->execute($borrowernumber, $biblionumber, $resdate, $_);
248 return; # FIXME: why not have a useful return value?
253 $res = GetReserve( $reserve_id );
258 my ($reserve_id) = @_;
260 my $dbh = C4
::Context
->dbh;
261 my $query = "SELECT * FROM reserves WHERE reserve_id = ?";
262 my $sth = $dbh->prepare( $query );
263 $sth->execute( $reserve_id );
264 my $res = $sth->fetchrow_hashref();
268 =head2 GetReservesFromBiblionumber
270 ($count, $title_reserves) = GetReservesFromBiblionumber($biblionumber);
272 This function gets the list of reservations for one C<$biblionumber>, returning a count
273 of the reserves and an arrayref pointing to the reserves for C<$biblionumber>.
277 sub GetReservesFromBiblionumber
{
278 my ($biblionumber) = shift or return (0, []);
279 my ($all_dates) = shift;
280 my ($itemnumber) = shift;
281 my $dbh = C4
::Context
->dbh;
283 # Find the desired items in the reserves
288 timestamp AS rtimestamp,
302 WHERE biblionumber = ? ";
303 push( @params, $biblionumber );
304 unless ( $all_dates ) {
305 $query .= " AND reservedate <= CAST(NOW() AS DATE) ";
308 $query .= " AND ( itemnumber IS NULL OR itemnumber = ? )";
309 push( @params, $itemnumber );
311 $query .= "ORDER BY priority";
312 my $sth = $dbh->prepare($query);
313 $sth->execute( @params );
316 while ( my $data = $sth->fetchrow_hashref ) {
318 # FIXME - What is this doing? How do constraints work?
319 if ($data->{constrainttype
} eq 'o') {
321 SELECT biblioitemnumber
322 FROM reserveconstraints
323 WHERE biblionumber = ?
324 AND borrowernumber = ?
327 my $csth = $dbh->prepare($query);
328 $csth->execute($data->{biblionumber
}, $data->{borrowernumber
}, $data->{reservedate
});
330 while ( my $bibitemnos = $csth->fetchrow_array ) {
331 push( @bibitemno, $bibitemnos ); # FIXME: inefficient: use fetchall_arrayref
333 my $count = scalar @bibitemno;
335 # if we have two or more different specific itemtypes
336 # reserved by same person on same day
339 $bdata = GetBiblioItemData
( $bibitemno[$i] ); # FIXME: This doesn't make sense.
340 $i++; # $i can increase each pass, but the next @bibitemno might be smaller?
343 # Look up the book we just found.
344 $bdata = GetBiblioItemData
( $bibitemno[0] );
346 # Add the results of this latest search to the current
348 # FIXME - An 'each' would probably be more efficient.
349 foreach my $key ( keys %$bdata ) {
350 $data->{$key} = $bdata->{$key};
353 push @results, $data;
355 return ( $#results + 1, \
@results );
358 =head2 GetReservesFromItemnumber
360 ( $reservedate, $borrowernumber, $branchcode, $reserve_id ) = GetReservesFromItemnumber($itemnumber);
362 TODO :: Description here
366 sub GetReservesFromItemnumber
{
367 my ( $itemnumber, $all_dates ) = @_;
368 my $dbh = C4
::Context
->dbh;
370 SELECT reservedate,borrowernumber,branchcode,reserve_id
374 unless ( $all_dates ) {
375 $query .= " AND reservedate <= CURRENT_DATE()";
377 my $sth_res = $dbh->prepare($query);
378 $sth_res->execute($itemnumber);
379 my ( $reservedate, $borrowernumber, $branchcode, $reserve_id ) = $sth_res->fetchrow_array;
380 return ( $reservedate, $borrowernumber, $branchcode, $reserve_id );
383 =head2 GetReservesFromBorrowernumber
385 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
391 sub GetReservesFromBorrowernumber
{
392 my ( $borrowernumber, $status ) = @_;
393 my $dbh = C4
::Context
->dbh;
396 $sth = $dbh->prepare("
399 WHERE borrowernumber=?
403 $sth->execute($borrowernumber,$status);
405 $sth = $dbh->prepare("
408 WHERE borrowernumber=?
411 $sth->execute($borrowernumber);
413 my $data = $sth->fetchall_arrayref({});
416 #-------------------------------------------------------------------------------------
417 =head2 CanBookBeReserved
419 $error = &CanBookBeReserved($borrowernumber, $biblionumber)
423 sub CanBookBeReserved
{
424 my ($borrowernumber, $biblionumber) = @_;
426 my $items = GetItemnumbersForBiblio
($biblionumber);
427 #get items linked via host records
428 my @hostitems = get_hostitemnumbers_of
($biblionumber);
430 push (@
$items,@hostitems);
433 foreach my $item (@
$items){
434 return 1 if CanItemBeReserved
($borrowernumber, $item);
439 =head2 CanItemBeReserved
441 $error = &CanItemBeReserved($borrowernumber, $itemnumber)
443 This function return 1 if an item can be issued by this borrower.
447 sub CanItemBeReserved
{
448 my ($borrowernumber, $itemnumber) = @_;
450 my $dbh = C4
::Context
->dbh;
451 my $allowedreserves = 0;
453 my $controlbranch = C4
::Context
->preference('ReservesControlBranch');
454 my $itype = C4
::Context
->preference('item-level_itypes') ?
"itype" : "itemtype";
456 # we retrieve borrowers and items informations #
457 my $item = GetItem
($itemnumber);
458 my $borrower = C4
::Members
::GetMember
('borrowernumber'=>$borrowernumber);
460 # we retrieve user rights on this itemtype and branchcode
461 my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
463 WHERE (categorycode in (?,'*') )
464 AND (itemtype IN (?,'*'))
465 AND (branchcode IN (?,'*'))
472 my $querycount ="SELECT
475 LEFT JOIN items USING (itemnumber)
476 LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
477 LEFT JOIN borrowers USING (borrowernumber)
478 WHERE borrowernumber = ?
482 my $itemtype = $item->{$itype};
483 my $categorycode = $borrower->{categorycode
};
485 my $branchfield = "reserves.branchcode";
487 if( $controlbranch eq "ItemHomeLibrary" ){
488 $branchfield = "items.homebranch";
489 $branchcode = $item->{homebranch
};
490 }elsif( $controlbranch eq "PatronLibrary" ){
491 $branchfield = "borrowers.branchcode";
492 $branchcode = $borrower->{branchcode
};
496 $sth->execute($categorycode, $itemtype, $branchcode);
497 if(my $rights = $sth->fetchrow_hashref()){
498 $itemtype = $rights->{itemtype
};
499 $allowedreserves = $rights->{reservesallowed
};
506 $querycount .= "AND $branchfield = ?";
508 $querycount .= " AND $itype = ?" if ($itemtype ne "*");
509 my $sthcount = $dbh->prepare($querycount);
511 if($itemtype eq "*"){
512 $sthcount->execute($borrowernumber, $branchcode);
514 $sthcount->execute($borrowernumber, $branchcode, $itemtype);
517 my $reservecount = "0";
518 if(my $rowcount = $sthcount->fetchrow_hashref()){
519 $reservecount = $rowcount->{count
};
522 # we check if it's ok or not
523 if( $reservecount >= $allowedreserves ){
527 # If reservecount is ok, we check item branch if IndependentBranches is ON
528 # and canreservefromotherbranches is OFF
529 if ( C4
::Context
->preference('IndependentBranches')
530 and !C4
::Context
->preference('canreservefromotherbranches') )
532 my $itembranch = $item->{homebranch
};
533 if ($itembranch ne $borrower->{branchcode
}) {
540 #--------------------------------------------------------------------------------
541 =head2 GetReserveCount
543 $number = &GetReserveCount($borrowernumber);
545 this function returns the number of reservation for a borrower given on input arg.
549 sub GetReserveCount
{
550 my ($borrowernumber) = @_;
552 my $dbh = C4
::Context
->dbh;
555 SELECT COUNT(*) AS counter
557 WHERE borrowernumber = ?
559 my $sth = $dbh->prepare($query);
560 $sth->execute($borrowernumber);
561 my $row = $sth->fetchrow_hashref;
562 return $row->{counter
};
565 =head2 GetOtherReserves
567 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
569 Check queued list of this document and check if this document must be transfered
573 sub GetOtherReserves
{
574 my ($itemnumber) = @_;
577 my ( undef, $checkreserves, undef ) = CheckReserves
($itemnumber);
578 if ($checkreserves) {
579 my $iteminfo = GetItem
($itemnumber);
580 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
581 $messages->{'transfert'} = $checkreserves->{'branchcode'};
582 #minus priorities of others reservs
583 ModReserveMinusPriority
(
585 $checkreserves->{'reserve_id'},
588 #launch the subroutine dotransfer
589 C4
::Items
::ModItemTransfer
(
591 $iteminfo->{'holdingbranch'},
592 $checkreserves->{'branchcode'}
597 #step 2b : case of a reservation on the same branch, set the waiting status
599 $messages->{'waiting'} = 1;
600 ModReserveMinusPriority
(
602 $checkreserves->{'reserve_id'},
604 ModReserveStatus
($itemnumber,'W');
607 $nextreservinfo = $checkreserves->{'borrowernumber'};
610 return ( $messages, $nextreservinfo );
615 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
617 Calculate the fee for a reserve
622 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
625 my $dbh = C4
::Context
->dbh;
626 my $const = lc substr( $constraint, 0, 1 );
628 SELECT
* FROM borrowers
629 LEFT JOIN categories ON borrowers
.categorycode
= categories
.categorycode
630 WHERE borrowernumber
= ?
632 my $sth = $dbh->prepare($query);
633 $sth->execute($borrowernumber);
634 my $data = $sth->fetchrow_hashref;
635 my $fee = $data->{'reservefee'};
636 my $cntitems = @
- > $bibitems;
640 # check for items on issue
641 # first find biblioitem records
643 my $sth1 = $dbh->prepare(
644 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
645 WHERE (biblio.biblionumber = ?)"
647 $sth1->execute($biblionumber);
648 while ( my $data1 = $sth1->fetchrow_hashref ) {
649 if ( $const eq "a" ) {
650 push @biblioitems, $data1;
655 while ( $x < $cntitems ) {
656 if ( @
$bibitems->{'biblioitemnumber'} ==
657 $data->{'biblioitemnumber'} )
663 if ( $const eq 'o' ) {
665 push @biblioitems, $data1;
670 push @biblioitems, $data1;
675 my $cntitemsfound = @biblioitems;
679 while ( $x < $cntitemsfound ) {
680 my $bitdata = $biblioitems[$x];
681 my $sth2 = $dbh->prepare(
683 WHERE biblioitemnumber = ?"
685 $sth2->execute( $bitdata->{'biblioitemnumber'} );
686 while ( my $itdata = $sth2->fetchrow_hashref ) {
687 my $sth3 = $dbh->prepare(
688 "SELECT * FROM issues
689 WHERE itemnumber = ?"
691 $sth3->execute( $itdata->{'itemnumber'} );
692 if ( my $isdata = $sth3->fetchrow_hashref ) {
700 if ( $allissued == 0 ) {
702 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
703 $rsth->execute($biblionumber);
704 if ( my $rdata = $rsth->fetchrow_hashref ) {
714 =head2 GetReservesToBranch
716 @transreserv = GetReservesToBranch( $frombranch );
718 Get reserve list for a given branch
722 sub GetReservesToBranch
{
723 my ( $frombranch ) = @_;
724 my $dbh = C4
::Context
->dbh;
725 my $sth = $dbh->prepare(
726 "SELECT reserve_id,borrowernumber,reservedate,itemnumber,timestamp
731 $sth->execute( $frombranch );
734 while ( my $data = $sth->fetchrow_hashref ) {
735 $transreserv[$i] = $data;
738 return (@transreserv);
741 =head2 GetReservesForBranch
743 @transreserv = GetReservesForBranch($frombranch);
747 sub GetReservesForBranch
{
748 my ($frombranch) = @_;
749 my $dbh = C4
::Context
->dbh;
752 SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate
757 $query .= " AND branchcode=? " if ( $frombranch );
758 $query .= "ORDER BY waitingdate" ;
760 my $sth = $dbh->prepare($query);
762 $sth->execute($frombranch);
769 while ( my $data = $sth->fetchrow_hashref ) {
770 $transreserv[$i] = $data;
773 return (@transreserv);
776 =head2 GetReserveStatus
778 $reservestatus = GetReserveStatus($itemnumber, $biblionumber);
780 Take an itemnumber or a biblionumber and return the status of the reserve places on it.
781 If several reserves exist, the reserve with the lower priority is given.
785 ## FIXME: I don't think this does what it thinks it does.
786 ## It only ever checks the first reserve result, even though
787 ## multiple reserves for that bib can have the itemnumber set
788 ## the sub is only used once in the codebase.
789 sub GetReserveStatus
{
790 my ($itemnumber, $biblionumber) = @_;
792 my $dbh = C4
::Context
->dbh;
794 my ($sth, $found, $priority);
796 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE itemnumber = ? order by priority LIMIT 1");
797 $sth->execute($itemnumber);
798 ($found, $priority) = $sth->fetchrow_array;
801 if ( $biblionumber and not defined $found and not defined $priority ) {
802 $sth = $dbh->prepare("SELECT found, priority FROM reserves WHERE biblionumber = ? order by priority LIMIT 1");
803 $sth->execute($biblionumber);
804 ($found, $priority) = $sth->fetchrow_array;
808 return 'Waiting' if $found eq 'W' and $priority == 0;
809 return 'Finished' if $found eq 'F';
812 return 'Reserved' if $priority > 0;
814 return ''; # empty string here will remove need for checking undef, or less log lines
819 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber);
820 ($status, $reserve, $all_reserves) = &CheckReserves(undef, $barcode);
821 ($status, $reserve, $all_reserves) = &CheckReserves($itemnumber,undef,$lookahead);
823 Find a book in the reserves.
825 C<$itemnumber> is the book's item number.
826 C<$lookahead> is the number of days to look in advance for future reserves.
828 As I understand it, C<&CheckReserves> looks for the given item in the
829 reserves. If it is found, that's a match, and C<$status> is set to
832 Otherwise, it finds the most important item in the reserves with the
833 same biblio number as this book (I'm not clear on this) and returns it
834 with C<$status> set to C<Reserved>.
836 C<&CheckReserves> returns a two-element list:
838 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
840 C<$reserve> is the reserve item that matched. It is a
841 reference-to-hash whose keys are mostly the fields of the reserves
842 table in the Koha database.
847 my ( $item, $barcode, $lookahead_days) = @_;
848 my $dbh = C4
::Context
->dbh;
851 if (C4
::Context
->preference('item-level_itypes')){
853 SELECT items.biblionumber,
854 items.biblioitemnumber,
855 itemtypes.notforloan,
856 items.notforloan AS itemnotforloan,
859 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
860 LEFT JOIN itemtypes ON items.itype = itemtypes.itemtype
865 SELECT items.biblionumber,
866 items.biblioitemnumber,
867 itemtypes.notforloan,
868 items.notforloan AS itemnotforloan,
871 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
872 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
877 $sth = $dbh->prepare("$select WHERE itemnumber = ?");
878 $sth->execute($item);
881 $sth = $dbh->prepare("$select WHERE barcode = ?");
882 $sth->execute($barcode);
884 # note: we get the itemnumber because we might have started w/ just the barcode. Now we know for sure we have it.
885 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item, $itemnumber ) = $sth->fetchrow_array;
887 return ( '' ) unless $itemnumber; # bail if we got nothing.
889 # if item is not for loan it cannot be reserved either.....
890 # execpt where items.notforloan < 0 : This indicates the item is holdable.
891 return ( '' ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
893 # Find this item in the reserves
894 my @reserves = _Findgroupreserve
( $bibitem, $biblio, $itemnumber, $lookahead_days);
896 # $priority and $highest are used to find the most important item
897 # in the list returned by &_Findgroupreserve. (The lower $priority,
898 # the more important the item.)
899 # $highest is the most important item we've seen so far.
901 if (scalar @reserves) {
902 my $priority = 10000000;
903 foreach my $res (@reserves) {
904 if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
905 return ( "Waiting", $res, \
@reserves ); # Found it
907 # See if this item is more important than what we've got so far
908 if ( $res->{'priority'} && $res->{'priority'} < $priority ) {
909 my $borrowerinfo=C4
::Members
::GetMember
(borrowernumber
=> $res->{'borrowernumber'});
910 my $iteminfo=C4
::Items
::GetItem
($itemnumber);
911 my $branch = GetReservesControlBranch
( $iteminfo, $borrowerinfo );
912 my $branchitemrule = C4
::Circulation
::GetBranchItemRule
($branch,$iteminfo->{'itype'});
913 next if ($branchitemrule->{'holdallowed'} == 0);
914 next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
915 $priority = $res->{'priority'};
922 # If we get this far, then no exact match was found.
923 # We return the most important (i.e. next) reservation.
925 $highest->{'itemnumber'} = $item;
926 return ( "Reserved", $highest, \
@reserves );
932 =head2 CancelExpiredReserves
934 CancelExpiredReserves();
936 Cancels all reserves with an expiration date from before today.
940 sub CancelExpiredReserves
{
942 # Cancel reserves that have passed their expiration date.
943 my $dbh = C4
::Context
->dbh;
944 my $sth = $dbh->prepare( "
945 SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
946 AND expirationdate IS NOT NULL
951 while ( my $res = $sth->fetchrow_hashref() ) {
952 CancelReserve
({ reserve_id
=> $res->{'reserve_id'} });
955 # Cancel reserves that have been waiting too long
956 if ( C4
::Context
->preference("ExpireReservesMaxPickUpDelay") ) {
957 my $max_pickup_delay = C4
::Context
->preference("ReservesMaxPickUpDelay");
958 my $charge = C4
::Context
->preference("ExpireReservesMaxPickUpDelayCharge");
960 my $query = "SELECT * FROM reserves WHERE TO_DAYS( NOW() ) - TO_DAYS( waitingdate ) > ? AND found = 'W' AND priority = 0";
961 $sth = $dbh->prepare( $query );
962 $sth->execute( $max_pickup_delay );
964 while (my $res = $sth->fetchrow_hashref ) {
966 manualinvoice
($res->{'borrowernumber'}, $res->{'itemnumber'}, 'Hold waiting too long', 'F', $charge);
969 CancelReserve
({ reserve_id
=> $res->{'reserve_id'} });
975 =head2 AutoUnsuspendReserves
977 AutoUnsuspendReserves();
979 Unsuspends all suspended reserves with a suspend_until date from before today.
983 sub AutoUnsuspendReserves
{
985 my $dbh = C4
::Context
->dbh;
987 my $query = "UPDATE reserves SET suspend = 0, suspend_until = NULL WHERE DATE( suspend_until ) < DATE( CURDATE() )";
988 my $sth = $dbh->prepare( $query );
995 CancelReserve({ reserve_id => $reserve_id, [ biblionumber => $biblionumber, borrowernumber => $borrrowernumber, itemnumber => $itemnumber ] });
1002 my ( $params ) = @_;
1004 my $reserve_id = $params->{'reserve_id'};
1005 $reserve_id = GetReserveId
( $params ) unless ( $reserve_id );
1007 return unless ( $reserve_id );
1009 my $dbh = C4
::Context
->dbh;
1013 SET cancellationdate = now(),
1016 WHERE reserve_id = ?
1018 my $sth = $dbh->prepare($query);
1019 $sth->execute( $reserve_id );
1022 INSERT INTO old_reserves
1023 SELECT * FROM reserves
1024 WHERE reserve_id = ?
1026 $sth = $dbh->prepare($query);
1027 $sth->execute( $reserve_id );
1030 DELETE FROM reserves
1031 WHERE reserve_id = ?
1033 $sth = $dbh->prepare($query);
1034 $sth->execute( $reserve_id );
1036 # now fix the priority on the others....
1037 _FixPriority
( $reserve_id );
1042 ModReserve({ rank => $rank,
1043 reserve_id => $reserve_id,
1044 branchcode => $branchcode
1045 [, itemnumber => $itemnumber ]
1046 [, biblionumber => $biblionumber, $borrowernumber => $borrowernumber ]
1049 Change a hold request's priority or cancel it.
1051 C<$rank> specifies the effect of the change. If C<$rank>
1052 is 'W' or 'n', nothing happens. This corresponds to leaving a
1053 request alone when changing its priority in the holds queue
1056 If C<$rank> is 'del', the hold request is cancelled.
1058 If C<$rank> is an integer greater than zero, the priority of
1059 the request is set to that value. Since priority != 0 means
1060 that the item is not waiting on the hold shelf, setting the
1061 priority to a non-zero value also sets the request's found
1062 status and waiting date to NULL.
1064 The optional C<$itemnumber> parameter is used only when
1065 C<$rank> is a non-zero integer; if supplied, the itemnumber
1066 of the hold request is set accordingly; if omitted, the itemnumber
1069 B<FIXME:> Note that the forgoing can have the effect of causing
1070 item-level hold requests to turn into title-level requests. This
1071 will be fixed once reserves has separate columns for requested
1072 itemnumber and supplying itemnumber.
1077 my ( $params ) = @_;
1079 my $rank = $params->{'rank'};
1080 my $reserve_id = $params->{'reserve_id'};
1081 my $branchcode = $params->{'branchcode'};
1082 my $itemnumber = $params->{'itemnumber'};
1083 my $suspend_until = $params->{'suspend_until'};
1084 my $borrowernumber = $params->{'borrowernumber'};
1085 my $biblionumber = $params->{'biblionumber'};
1087 return if $rank eq "W";
1088 return if $rank eq "n";
1090 return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
1091 $reserve_id = GetReserveId
({ biblionumber
=> $biblionumber, borrowernumber
=> $borrowernumber, itemnumber
=> $itemnumber }) unless ( $reserve_id );
1093 my $dbh = C4
::Context
->dbh;
1094 if ( $rank eq "del" ) {
1097 SET cancellationdate=now()
1098 WHERE reserve_id = ?
1100 my $sth = $dbh->prepare($query);
1101 $sth->execute( $reserve_id );
1103 INSERT INTO old_reserves
1106 WHERE reserve_id = ?
1108 $sth = $dbh->prepare($query);
1109 $sth->execute( $reserve_id );
1111 DELETE FROM reserves
1112 WHERE reserve_id = ?
1114 $sth = $dbh->prepare($query);
1115 $sth->execute( $reserve_id );
1118 elsif ($rank =~ /^\d+/ and $rank > 0) {
1120 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
1121 WHERE reserve_id = ?
1123 my $sth = $dbh->prepare($query);
1124 $sth->execute( $rank, $branchcode, $itemnumber, $reserve_id );
1126 if ( defined( $suspend_until ) ) {
1127 if ( $suspend_until ) {
1128 $suspend_until = C4
::Dates
->new( $suspend_until )->output("iso");
1129 $dbh->do("UPDATE reserves SET suspend = 1, suspend_until = ? WHERE reserve_id = ?", undef, ( $suspend_until, $reserve_id ) );
1131 $dbh->do("UPDATE reserves SET suspend_until = NULL WHERE reserve_id = ?", undef, ( $reserve_id ) );
1135 _FixPriority
( $reserve_id, $rank );
1139 =head2 ModReserveFill
1141 &ModReserveFill($reserve);
1143 Fill a reserve. If I understand this correctly, this means that the
1144 reserved book has been found and given to the patron who reserved it.
1146 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
1147 whose keys are fields from the reserves table in the Koha database.
1151 sub ModReserveFill
{
1153 my $dbh = C4
::Context
->dbh;
1154 # fill in a reserve record....
1155 my $reserve_id = $res->{'reserve_id'};
1156 my $biblionumber = $res->{'biblionumber'};
1157 my $borrowernumber = $res->{'borrowernumber'};
1158 my $resdate = $res->{'reservedate'};
1160 # get the priority on this record....
1162 my $query = "SELECT priority
1164 WHERE biblionumber = ?
1165 AND borrowernumber = ?
1166 AND reservedate = ?";
1167 my $sth = $dbh->prepare($query);
1168 $sth->execute( $biblionumber, $borrowernumber, $resdate );
1169 ($priority) = $sth->fetchrow_array;
1171 # update the database...
1172 $query = "UPDATE reserves
1175 WHERE biblionumber = ?
1177 AND borrowernumber = ?
1179 $sth = $dbh->prepare($query);
1180 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1182 # move to old_reserves
1183 $query = "INSERT INTO old_reserves
1184 SELECT * FROM reserves
1185 WHERE biblionumber = ?
1187 AND borrowernumber = ?
1189 $sth = $dbh->prepare($query);
1190 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1191 $query = "DELETE FROM reserves
1192 WHERE biblionumber = ?
1194 AND borrowernumber = ?
1196 $sth = $dbh->prepare($query);
1197 $sth->execute( $biblionumber, $resdate, $borrowernumber );
1199 # now fix the priority on the others (if the priority wasn't
1200 # already sorted!)....
1201 unless ( $priority == 0 ) {
1202 _FixPriority
( $reserve_id );
1206 =head2 ModReserveStatus
1208 &ModReserveStatus($itemnumber, $newstatus);
1210 Update the reserve status for the active (priority=0) reserve.
1212 $itemnumber is the itemnumber the reserve is on
1214 $newstatus is the new status.
1218 sub ModReserveStatus
{
1220 #first : check if we have a reservation for this item .
1221 my ($itemnumber, $newstatus) = @_;
1222 my $dbh = C4
::Context
->dbh;
1224 my $query = "UPDATE reserves SET found = ?, waitingdate = NOW() WHERE itemnumber = ? AND found IS NULL AND priority = 0";
1225 my $sth_set = $dbh->prepare($query);
1226 $sth_set->execute( $newstatus, $itemnumber );
1228 if ( C4
::Context
->preference("ReturnToShelvingCart") && $newstatus ) {
1229 CartToShelf
( $itemnumber );
1233 =head2 ModReserveAffect
1235 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
1237 This function affect an item and a status for a given reserve
1238 The itemnumber parameter is used to find the biblionumber.
1239 with the biblionumber & the borrowernumber, we can affect the itemnumber
1240 to the correct reserve.
1242 if $transferToDo is not set, then the status is set to "Waiting" as well.
1243 otherwise, a transfer is on the way, and the end of the transfer will
1244 take care of the waiting status
1248 sub ModReserveAffect
{
1249 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
1250 my $dbh = C4
::Context
->dbh;
1252 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
1253 # attached to $itemnumber
1254 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
1255 $sth->execute($itemnumber);
1256 my ($biblionumber) = $sth->fetchrow;
1258 # get request - need to find out if item is already
1259 # waiting in order to not send duplicate hold filled notifications
1260 my $request = GetReserveInfo
($borrowernumber, $biblionumber);
1261 my $already_on_shelf = ($request && $request->{found
} eq 'W') ?
1 : 0;
1263 # If we affect a reserve that has to be transfered, don't set to Waiting
1265 if ($transferToDo) {
1271 WHERE borrowernumber = ?
1272 AND biblionumber = ?
1276 # affect the reserve to Waiting as well.
1281 waitingdate = NOW(),
1283 WHERE borrowernumber = ?
1284 AND biblionumber = ?
1287 $sth = $dbh->prepare($query);
1288 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1289 _koha_notify_reserve
( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
1291 if ( C4
::Context
->preference("ReturnToShelvingCart") ) {
1292 CartToShelf
( $itemnumber );
1298 =head2 ModReserveCancelAll
1300 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1302 function to cancel reserv,check other reserves, and transfer document if it's necessary
1306 sub ModReserveCancelAll
{
1309 my ( $itemnumber, $borrowernumber ) = @_;
1311 #step 1 : cancel the reservation
1312 my $CancelReserve = CancelReserve
({ itemnumber
=> $itemnumber, borrowernumber
=> $borrowernumber });
1314 #step 2 launch the subroutine of the others reserves
1315 ( $messages, $nextreservinfo ) = GetOtherReserves
($itemnumber);
1317 return ( $messages, $nextreservinfo );
1320 =head2 ModReserveMinusPriority
1322 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1324 Reduce the values of queued list
1328 sub ModReserveMinusPriority
{
1329 my ( $itemnumber, $reserve_id ) = @_;
1331 #first step update the value of the first person on reserv
1332 my $dbh = C4
::Context
->dbh;
1335 SET priority = 0 , itemnumber = ?
1336 WHERE reserve_id = ?
1338 my $sth_upd = $dbh->prepare($query);
1339 $sth_upd->execute( $itemnumber, $reserve_id );
1340 # second step update all others reservs
1341 _FixPriority
( $reserve_id, '0');
1344 =head2 GetReserveInfo
1346 &GetReserveInfo($reserve_id);
1348 Get item and borrower details for a current hold.
1349 Current implementation this query should have a single result.
1353 sub GetReserveInfo
{
1354 my ( $reserve_id ) = @_;
1355 my $dbh = C4
::Context
->dbh;
1360 reserves.borrowernumber,
1361 reserves.biblionumber,
1362 reserves.branchcode,
1363 reserves.waitingdate,
1379 items.holdingbranch,
1380 items.itemcallnumber,
1386 LEFT JOIN items USING(itemnumber)
1387 LEFT JOIN borrowers USING(borrowernumber)
1388 LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
1389 WHERE reserves.reserve_id = ?";
1390 my $sth = $dbh->prepare($strsth);
1391 $sth->execute($reserve_id);
1393 my $data = $sth->fetchrow_hashref;
1397 =head2 IsAvailableForItemLevelRequest
1399 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1401 Checks whether a given item record is available for an
1402 item-level hold request. An item is available if
1404 * it is not lost AND
1405 * it is not damaged AND
1406 * it is not withdrawn AND
1407 * does not have a not for loan value > 0
1409 Whether or not the item is currently on loan is
1410 also checked - if the AllowOnShelfHolds system preference
1411 is ON, an item can be requested even if it is currently
1412 on loan to somebody else. If the system preference
1413 is OFF, an item that is currently checked out cannot
1414 be the target of an item-level hold request.
1416 Note that IsAvailableForItemLevelRequest() does not
1417 check if the staff operator is authorized to place
1418 a request on the item - in particular,
1419 this routine does not check IndependentBranches
1420 and canreservefromotherbranches.
1424 sub IsAvailableForItemLevelRequest
{
1425 my $itemnumber = shift;
1427 my $item = GetItem
($itemnumber);
1429 # must check the notforloan setting of the itemtype
1430 # FIXME - a lot of places in the code do this
1431 # or something similar - need to be
1433 my $dbh = C4
::Context
->dbh;
1434 my $notforloan_query;
1435 if (C4
::Context
->preference('item-level_itypes')) {
1436 $notforloan_query = "SELECT itemtypes.notforloan
1438 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1439 WHERE itemnumber = ?";
1441 $notforloan_query = "SELECT itemtypes.notforloan
1443 JOIN biblioitems USING (biblioitemnumber)
1444 JOIN itemtypes USING (itemtype)
1445 WHERE itemnumber = ?";
1447 my $sth = $dbh->prepare($notforloan_query);
1448 $sth->execute($itemnumber);
1449 my $notforloan_per_itemtype = 0;
1450 if (my ($notforloan) = $sth->fetchrow_array) {
1451 $notforloan_per_itemtype = 1 if $notforloan;
1454 my $available_per_item = 1;
1455 $available_per_item = 0 if $item->{itemlost
} or
1456 ( $item->{notforloan
} > 0 ) or
1457 ($item->{damaged
} and not C4
::Context
->preference('AllowHoldsOnDamagedItems')) or
1458 $item->{withdrawn
} or
1459 $notforloan_per_itemtype;
1462 if (C4
::Context
->preference('AllowOnShelfHolds')) {
1463 return $available_per_item;
1465 return ($available_per_item and ($item->{onloan
} or GetReserveStatus
($itemnumber) eq "Waiting"));
1469 =head2 AlterPriority
1471 AlterPriority( $where, $reserve_id );
1473 This function changes a reserve's priority up, down, to the top, or to the bottom.
1474 Input: $where is 'up', 'down', 'top' or 'bottom'. Biblionumber, Date reserve was placed
1479 my ( $where, $reserve_id ) = @_;
1481 my $dbh = C4
::Context
->dbh;
1483 my $reserve = GetReserve
( $reserve_id );
1485 if ( $reserve->{cancellationdate
} ) {
1486 warn "I cannot alter the priority for reserve_id $reserve_id, the reserve has been cancelled (".$reserve->{cancellationdate
}.')';
1490 if ( $where eq 'up' || $where eq 'down' ) {
1492 my $priority = $reserve->{'priority'};
1493 $priority = $where eq 'up' ?
$priority - 1 : $priority + 1;
1494 _FixPriority
( $reserve_id, $priority )
1496 } elsif ( $where eq 'top' ) {
1498 _FixPriority
( $reserve_id, '1' )
1500 } elsif ( $where eq 'bottom' ) {
1502 _FixPriority
( $reserve_id, '999999' )
1507 =head2 ToggleLowestPriority
1509 ToggleLowestPriority( $borrowernumber, $biblionumber );
1511 This function sets the lowestPriority field to true if is false, and false if it is true.
1515 sub ToggleLowestPriority
{
1516 my ( $reserve_id ) = @_;
1518 my $dbh = C4
::Context
->dbh;
1520 my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
1521 $sth->execute( $reserve_id );
1523 _FixPriority
( $reserve_id, '999999' );
1526 =head2 ToggleSuspend
1528 ToggleSuspend( $reserve_id );
1530 This function sets the suspend field to true if is false, and false if it is true.
1531 If the reserve is currently suspended with a suspend_until date, that date will
1532 be cleared when it is unsuspended.
1537 my ( $reserve_id, $suspend_until ) = @_;
1539 $suspend_until = output_pref
({ dt
=> dt_from_string
( $suspend_until ), dateformat
=> 'iso' }) if ( $suspend_until );
1541 my $do_until = ( $suspend_until ) ?
'?' : 'NULL';
1543 my $dbh = C4
::Context
->dbh;
1545 my $sth = $dbh->prepare(
1546 "UPDATE reserves SET suspend = NOT suspend,
1547 suspend_until = CASE WHEN suspend = 0 THEN NULL ELSE $do_until END
1548 WHERE reserve_id = ?
1552 push( @params, $suspend_until ) if ( $suspend_until );
1553 push( @params, $reserve_id );
1555 $sth->execute( @params );
1561 borrowernumber => $borrowernumber,
1562 [ biblionumber => $biblionumber, ]
1563 [ suspend_until => $suspend_until, ]
1564 [ suspend => $suspend ]
1567 This function accepts a set of hash keys as its parameters.
1568 It requires either borrowernumber or biblionumber, or both.
1570 suspend_until is wholly optional.
1577 my $borrowernumber = $params{'borrowernumber'} || undef;
1578 my $biblionumber = $params{'biblionumber'} || undef;
1579 my $suspend_until = $params{'suspend_until'} || undef;
1580 my $suspend = defined( $params{'suspend'} ) ?
$params{'suspend'} : 1;
1582 $suspend_until = C4
::Dates
->new( $suspend_until )->output("iso") if ( defined( $suspend_until ) );
1584 return unless ( $borrowernumber || $biblionumber );
1586 my ( $query, $sth, $dbh, @query_params );
1588 $query = "UPDATE reserves SET suspend = ? ";
1589 push( @query_params, $suspend );
1591 $query .= ", suspend_until = NULL ";
1592 } elsif ( $suspend_until ) {
1593 $query .= ", suspend_until = ? ";
1594 push( @query_params, $suspend_until );
1596 $query .= " WHERE ";
1597 if ( $borrowernumber ) {
1598 $query .= " borrowernumber = ? ";
1599 push( @query_params, $borrowernumber );
1601 $query .= " AND " if ( $borrowernumber && $biblionumber );
1602 if ( $biblionumber ) {
1603 $query .= " biblionumber = ? ";
1604 push( @query_params, $biblionumber );
1606 $query .= " AND found IS NULL ";
1608 $dbh = C4
::Context
->dbh;
1609 $sth = $dbh->prepare( $query );
1610 $sth->execute( @query_params );
1616 &_FixPriority( $reserve_id, $rank, $ignoreSetLowestRank);
1618 Only used internally (so don't export it)
1619 Changed how this functions works #
1620 Now just gets an array of reserves in the rank order and updates them with
1621 the array index (+1 as array starts from 0)
1622 and if $rank is supplied will splice item from the array and splice it back in again
1623 in new priority rank
1628 my ( $reserve_id, $rank, $ignoreSetLowestRank ) = @_;
1629 my $dbh = C4
::Context
->dbh;
1631 my $res = GetReserve
( $reserve_id );
1633 if ( $rank eq "del" ) {
1634 CancelReserve
({ reserve_id
=> $reserve_id });
1636 elsif ( $rank eq "W" || $rank eq "0" ) {
1638 # make sure priority for waiting or in-transit items is 0
1642 WHERE reserve_id = ?
1643 AND found IN ('W', 'T')
1645 my $sth = $dbh->prepare($query);
1646 $sth->execute( $reserve_id );
1652 SELECT reserve_id, borrowernumber, reservedate, constrainttype
1654 WHERE biblionumber = ?
1655 AND ((found <> 'W' AND found <> 'T') OR found IS NULL)
1656 ORDER BY priority ASC
1658 my $sth = $dbh->prepare($query);
1659 $sth->execute( $res->{'biblionumber'} );
1660 while ( my $line = $sth->fetchrow_hashref ) {
1661 push( @priority, $line );
1664 # To find the matching index
1666 my $key = -1; # to allow for 0 to be a valid result
1667 for ( $i = 0 ; $i < @priority ; $i++ ) {
1668 if ( $reserve_id == $priority[$i]->{'reserve_id'} ) {
1669 $key = $i; # save the index
1674 # if index exists in array then move it to new position
1675 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1676 my $new_rank = $rank -
1677 1; # $new_rank is what you want the new index to be in the array
1678 my $moving_item = splice( @priority, $key, 1 );
1679 splice( @priority, $new_rank, 0, $moving_item );
1682 # now fix the priority on those that are left....
1686 WHERE reserve_id = ?
1688 $sth = $dbh->prepare($query);
1689 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1692 $priority[$j]->{'reserve_id'}
1696 $sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
1699 unless ( $ignoreSetLowestRank ) {
1700 while ( my $res = $sth->fetchrow_hashref() ) {
1701 _FixPriority
( $res->{'reserve_id'}, '999999', 1 );
1706 =head2 _Findgroupreserve
1708 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber, $lookahead);
1710 Looks for an item-specific match first, then for a title-level match, returning the
1711 first match found. If neither, then we look for a 3rd kind of match based on
1712 reserve constraints.
1713 Lookahead is the number of days to look in advance.
1715 TODO: add more explanation about reserve constraints
1717 C<&_Findgroupreserve> returns :
1718 C<@results> is an array of references-to-hash whose keys are mostly
1719 fields from the reserves table of the Koha database, plus
1720 C<biblioitemnumber>.
1724 sub _Findgroupreserve
{
1725 my ( $bibitem, $biblio, $itemnumber, $lookahead) = @_;
1726 my $dbh = C4
::Context
->dbh;
1728 # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var.
1729 # check for exact targetted match
1730 my $item_level_target_query = qq/
1731 SELECT reserves
.biblionumber AS biblionumber
,
1732 reserves
.borrowernumber AS borrowernumber
,
1733 reserves
.reservedate AS reservedate
,
1734 reserves
.branchcode AS branchcode
,
1735 reserves
.cancellationdate AS cancellationdate
,
1736 reserves
.found AS found
,
1737 reserves
.reservenotes AS reservenotes
,
1738 reserves
.priority AS priority
,
1739 reserves
.timestamp AS timestamp
,
1740 biblioitems
.biblioitemnumber AS biblioitemnumber
,
1741 reserves
.itemnumber AS itemnumber
,
1742 reserves
.reserve_id AS reserve_id
1744 JOIN biblioitems USING
(biblionumber
)
1745 JOIN hold_fill_targets USING
(biblionumber
, borrowernumber
, itemnumber
)
1748 AND item_level_request
= 1
1750 AND reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1753 my $sth = $dbh->prepare($item_level_target_query);
1754 $sth->execute($itemnumber, $lookahead||0);
1756 if ( my $data = $sth->fetchrow_hashref ) {
1757 push( @results, $data );
1759 return @results if @results;
1761 # check for title-level targetted match
1762 my $title_level_target_query = qq/
1763 SELECT reserves
.biblionumber AS biblionumber
,
1764 reserves
.borrowernumber AS borrowernumber
,
1765 reserves
.reservedate AS reservedate
,
1766 reserves
.branchcode AS branchcode
,
1767 reserves
.cancellationdate AS cancellationdate
,
1768 reserves
.found AS found
,
1769 reserves
.reservenotes AS reservenotes
,
1770 reserves
.priority AS priority
,
1771 reserves
.timestamp AS timestamp
,
1772 biblioitems
.biblioitemnumber AS biblioitemnumber
,
1773 reserves
.itemnumber AS itemnumber
1775 JOIN biblioitems USING
(biblionumber
)
1776 JOIN hold_fill_targets USING
(biblionumber
, borrowernumber
)
1779 AND item_level_request
= 0
1780 AND hold_fill_targets
.itemnumber
= ?
1781 AND reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1784 $sth = $dbh->prepare($title_level_target_query);
1785 $sth->execute($itemnumber, $lookahead||0);
1787 if ( my $data = $sth->fetchrow_hashref ) {
1788 push( @results, $data );
1790 return @results if @results;
1793 SELECT reserves
.biblionumber AS biblionumber
,
1794 reserves
.borrowernumber AS borrowernumber
,
1795 reserves
.reservedate AS reservedate
,
1796 reserves
.waitingdate AS waitingdate
,
1797 reserves
.branchcode AS branchcode
,
1798 reserves
.cancellationdate AS cancellationdate
,
1799 reserves
.found AS found
,
1800 reserves
.reservenotes AS reservenotes
,
1801 reserves
.priority AS priority
,
1802 reserves
.timestamp AS timestamp
,
1803 reserveconstraints
.biblioitemnumber AS biblioitemnumber
,
1804 reserves
.itemnumber AS itemnumber
1806 LEFT JOIN reserveconstraints ON reserves
.biblionumber
= reserveconstraints
.biblionumber
1807 WHERE reserves
.biblionumber
= ?
1808 AND
( ( reserveconstraints
.biblioitemnumber
= ?
1809 AND reserves
.borrowernumber
= reserveconstraints
.borrowernumber
1810 AND reserves
.reservedate
= reserveconstraints
.reservedate
)
1811 OR reserves
.constrainttype
='a' )
1812 AND
(reserves
.itemnumber IS NULL OR reserves
.itemnumber
= ?
)
1813 AND reserves
.reservedate
<= DATE_ADD
(NOW
(),INTERVAL ? DAY
)
1816 $sth = $dbh->prepare($query);
1817 $sth->execute( $biblio, $bibitem, $itemnumber, $lookahead||0);
1819 while ( my $data = $sth->fetchrow_hashref ) {
1820 push( @results, $data );
1825 =head2 _koha_notify_reserve
1827 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1829 Sends a notification to the patron that their hold has been filled (through
1830 ModReserveAffect, _not_ ModReserveFill)
1834 sub _koha_notify_reserve
{
1835 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1837 my $dbh = C4
::Context
->dbh;
1838 my $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrowernumber);
1840 # Try to get the borrower's email address
1841 my $to_address = C4
::Members
::GetNoticeEmailAddress
($borrowernumber);
1846 if ( $to_address || $borrower->{'smsalertnumber'} ) {
1847 $messagingprefs = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $borrowernumber, message_name
=> 'Hold_Filled' } );
1852 my $sth = $dbh->prepare("
1855 WHERE borrowernumber = ?
1856 AND biblionumber = ?
1858 $sth->execute( $borrowernumber, $biblionumber );
1859 my $reserve = $sth->fetchrow_hashref;
1860 my $branch_details = GetBranchDetail
( $reserve->{'branchcode'} );
1862 my $admin_email_address = $branch_details->{'branchemail'} || C4
::Context
->preference('KohaAdminEmailAddress');
1864 my %letter_params = (
1865 module
=> 'reserves',
1866 branchcode
=> $reserve->{branchcode
},
1868 'branches' => $branch_details,
1869 'borrowers' => $borrower,
1870 'biblio' => $biblionumber,
1871 'reserves' => $reserve,
1872 'items', $reserve->{'itemnumber'},
1874 substitute
=> { today
=> C4
::Dates
->new()->output() },
1878 if ( $print_mode ) {
1879 $letter_params{ 'letter_code' } = 'HOLD_PRINT';
1880 my $letter = C4
::Letters
::GetPreparedLetter
( %letter_params ) or die "Could not find a letter called '$letter_params{'letter_code'}' in the 'reserves' module";
1882 C4
::Letters
::EnqueueLetter
( {
1884 borrowernumber
=> $borrowernumber,
1885 message_transport_type
=> 'print',
1891 if ( $to_address && defined $messagingprefs->{transports
}->{'email'} ) {
1892 $letter_params{ 'letter_code' } = $messagingprefs->{transports
}->{'email'};
1893 my $letter = C4
::Letters
::GetPreparedLetter
( %letter_params ) or die "Could not find a letter called '$letter_params{'letter_code'}' in the 'reserves' module";
1895 C4
::Letters
::EnqueueLetter
(
1896 { letter
=> $letter,
1897 borrowernumber
=> $borrowernumber,
1898 message_transport_type
=> 'email',
1899 from_address
=> $admin_email_address,
1904 if ( $borrower->{'smsalertnumber'} && defined $messagingprefs->{transports
}->{'sms'} ) {
1905 $letter_params{ 'letter_code' } = $messagingprefs->{transports
}->{'sms'};
1906 my $letter = C4
::Letters
::GetPreparedLetter
( %letter_params ) or die "Could not find a letter called '$letter_params{'letter_code'}' in the 'reserves' module";
1908 C4
::Letters
::EnqueueLetter
(
1909 { letter
=> $letter,
1910 borrowernumber
=> $borrowernumber,
1911 message_transport_type
=> 'sms',
1917 =head2 _ShiftPriorityByDateAndPriority
1919 $new_priority = _ShiftPriorityByDateAndPriority( $biblionumber, $reservedate, $priority );
1921 This increments the priority of all reserves after the one
1922 with either the lowest date after C<$reservedate>
1923 or the lowest priority after C<$priority>.
1925 It effectively makes room for a new reserve to be inserted with a certain
1926 priority, which is returned.
1928 This is most useful when the reservedate can be set by the user. It allows
1929 the new reserve to be placed before other reserves that have a later
1930 reservedate. Since priority also is set by the form in reserves/request.pl
1931 the sub accounts for that too.
1935 sub _ShiftPriorityByDateAndPriority
{
1936 my ( $biblio, $resdate, $new_priority ) = @_;
1938 my $dbh = C4
::Context
->dbh;
1939 my $query = "SELECT priority FROM reserves WHERE biblionumber = ? AND ( reservedate > ? OR priority > ? ) ORDER BY priority ASC LIMIT 1";
1940 my $sth = $dbh->prepare( $query );
1941 $sth->execute( $biblio, $resdate, $new_priority );
1942 my $min_priority = $sth->fetchrow;
1943 # if no such matches are found, $new_priority remains as original value
1944 $new_priority = $min_priority if ( $min_priority );
1946 # Shift the priority up by one; works in conjunction with the next SQL statement
1947 $query = "UPDATE reserves
1948 SET priority = priority+1
1949 WHERE biblionumber = ?
1950 AND borrowernumber = ?
1953 my $sth_update = $dbh->prepare( $query );
1955 # Select all reserves for the biblio with priority greater than $new_priority, and order greatest to least
1956 $query = "SELECT borrowernumber, reservedate FROM reserves WHERE priority >= ? AND biblionumber = ? ORDER BY priority DESC";
1957 $sth = $dbh->prepare( $query );
1958 $sth->execute( $new_priority, $biblio );
1959 while ( my $row = $sth->fetchrow_hashref ) {
1960 $sth_update->execute( $biblio, $row->{borrowernumber
}, $row->{reservedate
} );
1963 return $new_priority; # so the caller knows what priority they wind up receiving
1968 MoveReserve( $itemnumber, $borrowernumber, $cancelreserve )
1970 Use when checking out an item to handle reserves
1971 If $cancelreserve boolean is set to true, it will remove existing reserve
1976 my ( $itemnumber, $borrowernumber, $cancelreserve ) = @_;
1978 my ( $restype, $res, $all_reserves ) = CheckReserves
( $itemnumber );
1981 my $biblionumber = $res->{biblionumber
};
1982 my $biblioitemnumber = $res->{biblioitemnumber
};
1984 if ($res->{borrowernumber
} == $borrowernumber) {
1985 ModReserveFill
($res);
1989 # The item is reserved by someone else.
1990 # Find this item in the reserves
1993 foreach (@
$all_reserves) {
1994 $_->{'borrowernumber'} == $borrowernumber or next;
1995 $_->{'biblionumber'} == $biblionumber or next;
2002 # The item is reserved by the current patron
2003 ModReserveFill
($borr_res);
2006 if ( $cancelreserve eq 'revert' ) { ## Revert waiting reserve to priority 1
2007 RevertWaitingStatus
({ itemnumber
=> $itemnumber });
2009 elsif ( $cancelreserve eq 'cancel' || $cancelreserve ) { # cancel reserves on this item
2011 biblionumber
=> $res->{'biblionumber'},
2012 itemnumber
=> $res->{'itemnumber'},
2013 borrowernumber
=> $res->{'borrowernumber'}
2021 MergeHolds($dbh,$to_biblio, $from_biblio);
2023 This shifts the holds from C<$from_biblio> to C<$to_biblio> and reorders them by the date they were placed
2028 my ( $dbh, $to_biblio, $from_biblio ) = @_;
2029 my $sth = $dbh->prepare(
2030 "SELECT count(*) as reserve_count FROM reserves WHERE biblionumber = ?"
2032 $sth->execute($from_biblio);
2033 if ( my $data = $sth->fetchrow_hashref() ) {
2035 # holds exist on old record, if not we don't need to do anything
2036 $sth = $dbh->prepare(
2037 "UPDATE reserves SET biblionumber = ? WHERE biblionumber = ?");
2038 $sth->execute( $to_biblio, $from_biblio );
2041 # don't reorder those already waiting
2043 $sth = $dbh->prepare(
2044 "SELECT * FROM reserves WHERE biblionumber = ? AND (found <> ? AND found <> ? OR found is NULL) ORDER BY reservedate ASC"
2046 my $upd_sth = $dbh->prepare(
2047 "UPDATE reserves SET priority = ? WHERE biblionumber = ? AND borrowernumber = ?
2048 AND reservedate = ? AND constrainttype = ? AND (itemnumber = ? or itemnumber is NULL) "
2050 $sth->execute( $to_biblio, 'W', 'T' );
2052 while ( my $reserve = $sth->fetchrow_hashref() ) {
2054 $priority, $to_biblio,
2055 $reserve->{'borrowernumber'}, $reserve->{'reservedate'},
2056 $reserve->{'constrainttype'}, $reserve->{'itemnumber'}
2063 =head2 RevertWaitingStatus
2065 $success = RevertWaitingStatus({ itemnumber => $itemnumber });
2067 Reverts a 'waiting' hold back to a regular hold with a priority of 1.
2069 Caveat: Any waiting hold fixed with RevertWaitingStatus will be an
2070 item level hold, even if it was only a bibliolevel hold to
2071 begin with. This is because we can no longer know if a hold
2072 was item-level or bib-level after a hold has been set to
2077 sub RevertWaitingStatus
{
2078 my ( $params ) = @_;
2079 my $itemnumber = $params->{'itemnumber'};
2081 return unless ( $itemnumber );
2083 my $dbh = C4
::Context
->dbh;
2085 ## Get the waiting reserve we want to revert
2087 SELECT * FROM reserves
2088 WHERE itemnumber = ?
2089 AND found IS NOT NULL
2091 my $sth = $dbh->prepare( $query );
2092 $sth->execute( $itemnumber );
2093 my $reserve = $sth->fetchrow_hashref();
2095 ## Increment the priority of all other non-waiting
2096 ## reserves for this bib record
2100 priority = priority + 1
2106 $sth = $dbh->prepare( $query );
2107 $sth->execute( $reserve->{'biblionumber'} );
2109 ## Fix up the currently waiting reserve
2119 $sth = $dbh->prepare( $query );
2120 return $sth->execute( $reserve->{'reserve_id'} );
2125 $reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber [, itemnumber => $itemnumber ] });
2127 Returnes the first reserve id that matches the given criteria
2132 my ( $params ) = @_;
2134 return unless ( ( $params->{'biblionumber'} || $params->{'itemnumber'} ) && $params->{'borrowernumber'} );
2136 my $dbh = C4
::Context
->dbh();
2138 my $sql = "SELECT reserve_id FROM reserves WHERE ";
2142 foreach my $key ( keys %$params ) {
2143 if ( defined( $params->{$key} ) ) {
2144 push( @limits, "$key = ?" );
2145 push( @params, $params->{$key} );
2149 $sql .= join( " AND ", @limits );
2151 my $sth = $dbh->prepare( $sql );
2152 $sth->execute( @params );
2153 my $row = $sth->fetchrow_hashref();
2155 return $row->{'reserve_id'};
2160 ReserveSlip($branchcode, $borrowernumber, $biblionumber)
2162 Returns letter hash ( see C4::Letters::GetPreparedLetter ) or undef
2167 my ($branch, $borrowernumber, $biblionumber) = @_;
2169 # return unless ( C4::Context->boolean_preference('printreserveslips') );
2171 my $reserve_id = GetReserveId
({
2172 biblionumber
=> $biblionumber,
2173 borrowernumber
=> $borrowernumber
2175 my $reserve = GetReserveInfo
($reserve_id) or return;
2177 return C4
::Letters
::GetPreparedLetter
(
2178 module
=> 'circulation',
2179 letter_code
=> 'RESERVESLIP',
2180 branchcode
=> $branch,
2182 'reserves' => $reserve,
2183 'branches' => $reserve->{branchcode
},
2184 'borrowers' => $reserve->{borrowernumber
},
2185 'biblio' => $reserve->{biblionumber
},
2186 'items' => $reserve->{itemnumber
},
2191 =head2 GetReservesControlBranch
2193 my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
2195 Return the branchcode to be used to determine which reserves
2196 policy applies to a transaction.
2198 C<$item> is a hashref for an item. Only 'homebranch' is used.
2200 C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
2204 sub GetReservesControlBranch
{
2205 my ( $item, $borrower ) = @_;
2207 my $reserves_control = C4
::Context
->preference('ReservesControlBranch');
2210 ( $reserves_control eq 'ItemHomeLibrary' ) ?
$item->{'homebranch'}
2211 : ( $reserves_control eq 'PatronLibrary' ) ?
$borrower->{'branchcode'}
2219 Koha Development Team <http://koha-community.org/>