Avoid XSLT stylesheet building for each biblio record to transform
[koha.git] / C4 / Reserves.pm
blob05015289e3d927b50a4f3467f9361fcfd0f5d588
1 # -*- tab-width: 8 -*-
2 # NOTE: This file uses standard 8-character tabs
4 package C4::Reserves;
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
15 # version.
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
26 use strict;
27 use C4::Context;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Search;
31 use C4::Circulation;
32 use C4::Accounts;
34 # for _koha_notify_reserve
35 use C4::Members::Messaging;
36 use C4::Members qw( GetMember );
37 use C4::Letters;
38 use C4::Branch qw( GetBranchDetail );
39 use List::MoreUtils qw( firstidx );
41 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
43 my $library_name = C4::Context->preference("LibraryName");
45 =head1 NAME
47 C4::Reserves - Koha functions for dealing with reservation.
49 =head1 SYNOPSIS
51 use C4::Reserves;
53 =head1 DESCRIPTION
55 this modules provides somes functions to deal with reservations.
57 Reserves are stored in reserves table.
58 The following columns contains important values :
59 - priority >0 : then the reserve is at 1st stage, and not yet affected to any item.
60 =0 : then the reserve is being dealed
61 - found : NULL : means the patron requested the 1st available, and we haven't choosen the item
62 W(aiting) : the reserve has an itemnumber affected, and is on the way
63 F(inished) : the reserve has been completed, and is done
64 - itemnumber : empty : the reserve is still unaffected to an item
65 filled: the reserve is attached to an item
66 The complete workflow is :
67 ==== 1st use case ====
68 patron request a document, 1st available : P >0, F=NULL, I=NULL
69 a library having it run "transfertodo", and clic on the list
70 if there is no transfer to do, the reserve waiting
71 patron can pick it up P =0, F=W, I=filled
72 if there is a transfer to do, write in branchtransfer P =0, F=NULL, I=filled
73 The pickup library recieve the book, it check in P =0, F=W, I=filled
74 The patron borrow the book P =0, F=F, I=filled
76 ==== 2nd use case ====
77 patron requests a document, a given item,
78 If pickup is holding branch P =0, F=W, I=filled
79 If transfer needed, write in branchtransfer P =0, F=NULL, I=filled
80 The pickup library recieve the book, it checks it in P =0, F=W, I=filled
81 The patron borrow the book P =0, F=F, I=filled
83 =head1 FUNCTIONS
85 =over 2
87 =cut
89 BEGIN {
90 # set the version for version checking
91 $VERSION = 3.01;
92 require Exporter;
93 @ISA = qw(Exporter);
94 @EXPORT = qw(
95 &AddReserve
97 &GetReservesFromItemnumber
98 &GetReservesFromBiblionumber
99 &GetReservesFromBorrowernumber
100 &GetReservesForBranch
101 &GetReservesToBranch
102 &GetReserveCount
103 &GetReserveFee
104 &GetReserveInfo
106 &GetOtherReserves
108 &ModReserveFill
109 &ModReserveAffect
110 &ModReserve
111 &ModReserveStatus
112 &ModReserveCancelAll
113 &ModReserveMinusPriority
115 &CheckReserves
116 &CancelReserve
118 &IsAvailableForItemLevelRequest
122 =item AddReserve
124 AddReserve($branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
126 =cut
128 sub AddReserve {
129 my (
130 $branch, $borrowernumber, $biblionumber,
131 $constraint, $bibitems, $priority, $notes,
132 $title, $checkitem, $found
133 ) = @_;
134 my $fee =
135 GetReserveFee($borrowernumber, $biblionumber, $constraint,
136 $bibitems );
137 my $dbh = C4::Context->dbh;
138 my $const = lc substr( $constraint, 0, 1 );
139 my @datearr = localtime(time);
140 my $resdate =
141 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
142 my $waitingdate;
144 # If the reserv had the waiting status, we had the value of the resdate
145 if ( $found eq 'W' ) {
146 $waitingdate = $resdate;
149 #eval {
150 # updates take place here
151 if ( $fee > 0 ) {
152 my $nextacctno = &getnextacctno( $borrowernumber );
153 my $query = qq/
154 INSERT INTO accountlines
155 (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
156 VALUES
157 (?,?,now(),?,?,'Res',?)
159 my $usth = $dbh->prepare($query);
160 $usth->execute( $borrowernumber, $nextacctno, $fee,
161 "Reserve Charge - $title", $fee );
162 $usth->finish;
165 #if ($const eq 'a'){
166 my $query = qq/
167 INSERT INTO reserves
168 (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
169 priority,reservenotes,itemnumber,found,waitingdate)
170 VALUES
171 (?,?,?,?,?,
172 ?,?,?,?,?)
174 my $sth = $dbh->prepare($query);
175 $sth->execute(
176 $borrowernumber, $biblionumber, $resdate, $branch,
177 $const, $priority, $notes, $checkitem,
178 $found, $waitingdate
180 $sth->finish;
183 if ( ( $const eq "o" ) || ( $const eq "e" ) ) {
184 my $numitems = @$bibitems;
185 my $i = 0;
186 while ( $i < $numitems ) {
187 my $biblioitem = @$bibitems[$i];
188 my $query = qq/
189 INSERT INTO reserveconstraints
190 (borrowernumber,biblionumber,reservedate,biblioitemnumber)
191 VALUES
192 (?,?,?,?)
194 my $sth = $dbh->prepare("");
195 $sth->execute( $borrowernumber, $biblionumber, $resdate,
196 $biblioitem );
197 $sth->finish;
198 $i++;
201 return;
204 =item GetReservesFromBiblionumber
206 @borrowerreserv=&GetReserves($biblionumber,$itemnumber,$borrowernumber);
208 this function get the list of reservation for an C<$biblionumber>, C<$itemnumber> or C<$borrowernumber>
209 given on input arg.
210 Only 1 argument has to be passed.
212 =cut
214 sub GetReservesFromBiblionumber {
215 my ( $biblionumber, $itemnumber, $borrowernumber ) = @_;
216 my $dbh = C4::Context->dbh;
218 # Find the desired items in the reserves
219 my $query = "
220 SELECT branchcode,
221 timestamp AS rtimestamp,
222 priority,
223 biblionumber,
224 borrowernumber,
225 reservedate,
226 constrainttype,
227 found,
228 itemnumber,
229 reservenotes
230 FROM reserves
231 WHERE biblionumber = ?
232 ORDER BY priority";
233 my $sth = $dbh->prepare($query);
234 $sth->execute($biblionumber);
235 my @results;
236 my $i = 0;
237 while ( my $data = $sth->fetchrow_hashref ) {
239 # FIXME - What is this if-statement doing? How do constraints work?
240 if ( $data->{constrainttype} eq 'o' ) {
241 $query = '
242 SELECT biblioitemnumber
243 FROM reserveconstraints
244 WHERE biblionumber = ?
245 AND borrowernumber = ?
246 AND reservedate = ?
248 my $csth = $dbh->prepare($query);
249 $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
250 $data->{reservedate}, );
252 my @bibitemno;
253 while ( my $bibitemnos = $csth->fetchrow_array ) {
254 push( @bibitemno, $bibitemnos );
256 my $count = @bibitemno;
258 # if we have two or more different specific itemtypes
259 # reserved by same person on same day
260 my $bdata;
261 if ( $count > 1 ) {
262 $bdata = GetBiblioItemData( $bibitemno[$i] );
263 $i++;
265 else {
267 # Look up the book we just found.
268 $bdata = GetBiblioItemData( $bibitemno[0] );
270 $csth->finish;
272 # Add the results of this latest search to the current
273 # results.
274 # FIXME - An 'each' would probably be more efficient.
275 foreach my $key ( keys %$bdata ) {
276 $data->{$key} = $bdata->{$key};
279 push @results, $data;
281 $sth->finish;
282 return ( $#results + 1, \@results );
285 =item GetReservesFromItemnumber
287 ( $reservedate, $borrowernumber, $branchcode ) = GetReservesFromItemnumber($itemnumber);
289 TODO :: Description here
291 =cut
293 sub GetReservesFromItemnumber {
294 my ( $itemnumber ) = @_;
295 my $dbh = C4::Context->dbh;
296 my $query = "
297 SELECT reservedate,borrowernumber,branchcode
298 FROM reserves
299 WHERE itemnumber=?
301 my $sth_res = $dbh->prepare($query);
302 $sth_res->execute($itemnumber);
303 my ( $reservedate, $borrowernumber,$branchcode ) = $sth_res->fetchrow_array;
304 return ( $reservedate, $borrowernumber, $branchcode );
307 =item GetReservesFromBorrowernumber
309 $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
311 TODO :: Descritpion
313 =cut
315 sub GetReservesFromBorrowernumber {
316 my ( $borrowernumber, $status ) = @_;
317 my $dbh = C4::Context->dbh;
318 my $sth;
319 if ($status) {
320 $sth = $dbh->prepare("
321 SELECT *
322 FROM reserves
323 WHERE borrowernumber=?
324 AND found =?
325 ORDER BY reservedate
327 $sth->execute($borrowernumber,$status);
328 } else {
329 $sth = $dbh->prepare("
330 SELECT *
331 FROM reserves
332 WHERE borrowernumber=?
333 ORDER BY reservedate
335 $sth->execute($borrowernumber);
337 my $data = $sth->fetchall_arrayref({});
338 return @$data;
340 #-------------------------------------------------------------------------------------
342 =item GetReserveCount
344 $number = &GetReserveCount($borrowernumber);
346 this function returns the number of reservation for a borrower given on input arg.
348 =cut
350 sub GetReserveCount {
351 my ($borrowernumber) = @_;
353 my $dbh = C4::Context->dbh;
355 my $query = '
356 SELECT COUNT(*) AS counter
357 FROM reserves
358 WHERE borrowernumber = ?
360 my $sth = $dbh->prepare($query);
361 $sth->execute($borrowernumber);
362 my $row = $sth->fetchrow_hashref;
363 $sth->finish;
365 return $row->{counter};
368 =item GetOtherReserves
370 ($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
372 Check queued list of this document and check if this document must be transfered
374 =cut
376 sub GetOtherReserves {
377 my ($itemnumber) = @_;
378 my $messages;
379 my $nextreservinfo;
380 my ( $restype, $checkreserves ) = CheckReserves($itemnumber);
381 if ($checkreserves) {
382 my $iteminfo = GetItem($itemnumber);
383 if ( $iteminfo->{'holdingbranch'} ne $checkreserves->{'branchcode'} ) {
384 $messages->{'transfert'} = $checkreserves->{'branchcode'};
385 #minus priorities of others reservs
386 ModReserveMinusPriority(
387 $itemnumber,
388 $checkreserves->{'borrowernumber'},
389 $iteminfo->{'biblionumber'}
392 #launch the subroutine dotransfer
393 C4::Circulation::ModItemTransfer(
394 $itemnumber,
395 $iteminfo->{'holdingbranch'},
396 $checkreserves->{'branchcode'}
401 #step 2b : case of a reservation on the same branch, set the waiting status
402 else {
403 $messages->{'waiting'} = 1;
404 ModReserveMinusPriority(
405 $itemnumber,
406 $checkreserves->{'borrowernumber'},
407 $iteminfo->{'biblionumber'}
409 ModReserveStatus($itemnumber,'W');
412 $nextreservinfo = $checkreserves->{'borrowernumber'};
415 return ( $messages, $nextreservinfo );
418 =item GetReserveFee
420 $fee = GetReserveFee($borrowernumber,$biblionumber,$constraint,$biblionumber);
422 Calculate the fee for a reserve
424 =cut
426 sub GetReserveFee {
427 my ($borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
429 #check for issues;
430 my $dbh = C4::Context->dbh;
431 my $const = lc substr( $constraint, 0, 1 );
432 my $query = qq/
433 SELECT * FROM borrowers
434 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
435 WHERE borrowernumber = ?
437 my $sth = $dbh->prepare($query);
438 $sth->execute($borrowernumber);
439 my $data = $sth->fetchrow_hashref;
440 $sth->finish();
441 my $fee = $data->{'reservefee'};
442 my $cntitems = @- > $bibitems;
444 if ( $fee > 0 ) {
446 # check for items on issue
447 # first find biblioitem records
448 my @biblioitems;
449 my $sth1 = $dbh->prepare(
450 "SELECT * FROM biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
451 WHERE (biblio.biblionumber = ?)"
453 $sth1->execute($biblionumber);
454 while ( my $data1 = $sth1->fetchrow_hashref ) {
455 if ( $const eq "a" ) {
456 push @biblioitems, $data1;
458 else {
459 my $found = 0;
460 my $x = 0;
461 while ( $x < $cntitems ) {
462 if ( @$bibitems->{'biblioitemnumber'} ==
463 $data->{'biblioitemnumber'} )
465 $found = 1;
467 $x++;
469 if ( $const eq 'o' ) {
470 if ( $found == 1 ) {
471 push @biblioitems, $data1;
474 else {
475 if ( $found == 0 ) {
476 push @biblioitems, $data1;
481 $sth1->finish;
482 my $cntitemsfound = @biblioitems;
483 my $issues = 0;
484 my $x = 0;
485 my $allissued = 1;
486 while ( $x < $cntitemsfound ) {
487 my $bitdata = $biblioitems[$x];
488 my $sth2 = $dbh->prepare(
489 "SELECT * FROM items
490 WHERE biblioitemnumber = ?"
492 $sth2->execute( $bitdata->{'biblioitemnumber'} );
493 while ( my $itdata = $sth2->fetchrow_hashref ) {
494 my $sth3 = $dbh->prepare(
495 "SELECT * FROM issues
496 WHERE itemnumber = ?"
498 $sth3->execute( $itdata->{'itemnumber'} );
499 if ( my $isdata = $sth3->fetchrow_hashref ) {
501 else {
502 $allissued = 0;
505 $x++;
507 if ( $allissued == 0 ) {
508 my $rsth =
509 $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
510 $rsth->execute($biblionumber);
511 if ( my $rdata = $rsth->fetchrow_hashref ) {
513 else {
514 $fee = 0;
518 return $fee;
521 =item GetReservesToBranch
523 @transreserv = GetReservesToBranch( $frombranch );
525 Get reserve list for a given branch
527 =cut
529 sub GetReservesToBranch {
530 my ( $frombranch ) = @_;
531 my $dbh = C4::Context->dbh;
532 my $sth = $dbh->prepare(
533 "SELECT borrowernumber,reservedate,itemnumber,timestamp
534 FROM reserves
535 WHERE priority='0'
536 AND branchcode=?"
538 $sth->execute( $frombranch );
539 my @transreserv;
540 my $i = 0;
541 while ( my $data = $sth->fetchrow_hashref ) {
542 $transreserv[$i] = $data;
543 $i++;
545 $sth->finish;
546 return (@transreserv);
549 =item GetReservesForBranch
551 @transreserv = GetReservesForBranch($frombranch);
553 =cut
555 sub GetReservesForBranch {
556 my ($frombranch) = @_;
557 my $dbh = C4::Context->dbh;
558 my $query = "SELECT borrowernumber,reservedate,itemnumber,waitingdate
559 FROM reserves
560 WHERE priority='0'
561 AND found='W' ";
562 if ($frombranch){
563 $query .= " AND branchcode=? ";
565 $query .= "ORDER BY waitingdate" ;
566 my $sth = $dbh->prepare($query);
567 if ($frombranch){
568 $sth->execute($frombranch);
570 else {
571 $sth->execute();
573 my @transreserv;
574 my $i = 0;
575 while ( my $data = $sth->fetchrow_hashref ) {
576 $transreserv[$i] = $data;
577 $i++;
579 $sth->finish;
580 return (@transreserv);
583 =item CheckReserves
585 ($status, $reserve) = &CheckReserves($itemnumber);
587 Find a book in the reserves.
589 C<$itemnumber> is the book's item number.
591 As I understand it, C<&CheckReserves> looks for the given item in the
592 reserves. If it is found, that's a match, and C<$status> is set to
593 C<Waiting>.
595 Otherwise, it finds the most important item in the reserves with the
596 same biblio number as this book (I'm not clear on this) and returns it
597 with C<$status> set to C<Reserved>.
599 C<&CheckReserves> returns a two-element list:
601 C<$status> is either C<Waiting>, C<Reserved> (see above), or 0.
603 C<$reserve> is the reserve item that matched. It is a
604 reference-to-hash whose keys are mostly the fields of the reserves
605 table in the Koha database.
607 =cut
609 sub CheckReserves {
610 my ( $item, $barcode ) = @_;
611 my $dbh = C4::Context->dbh;
612 my $sth;
613 if ($item) {
614 my $qitem = $dbh->quote($item);
615 # Look up the item by itemnumber
616 my $query = "
617 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
618 FROM items
619 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
620 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
621 WHERE itemnumber=$qitem
623 $sth = $dbh->prepare($query);
625 else {
626 my $qbc = $dbh->quote($barcode);
627 # Look up the item by barcode
628 my $query = "
629 SELECT items.biblionumber, items.biblioitemnumber, itemtypes.notforloan, items.notforloan AS itemnotforloan
630 FROM items
631 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
632 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
633 WHERE items.biblioitemnumber = biblioitems.biblioitemnumber
634 AND biblioitems.itemtype = itemtypes.itemtype
635 AND barcode=$qbc
637 $sth = $dbh->prepare($query);
639 # FIXME - This function uses $item later on. Ought to set it here.
641 $sth->execute;
642 my ( $biblio, $bibitem, $notforloan_per_itemtype, $notforloan_per_item ) = $sth->fetchrow_array;
643 $sth->finish;
644 # if item is not for loan it cannot be reserved either.....
645 # execption to notforloan is where items.notforloan < 0 : This indicates the item is holdable.
646 return ( 0, 0 ) if ( $notforloan_per_item > 0 ) or $notforloan_per_itemtype;
648 # get the reserves...
649 # Find this item in the reserves
650 my @reserves = _Findgroupreserve( $bibitem, $biblio, $item );
651 my $count = scalar @reserves;
653 # $priority and $highest are used to find the most important item
654 # in the list returned by &_Findgroupreserve. (The lower $priority,
655 # the more important the item.)
656 # $highest is the most important item we've seen so far.
657 my $priority = 10000000;
658 my $highest;
659 if ($count) {
660 foreach my $res (@reserves) {
661 # FIXME - $item might be undefined or empty: the caller
662 # might be searching by barcode.
663 if ( $res->{'itemnumber'} == $item && $res->{'priority'} == 0) {
664 # Found it
665 return ( "Waiting", $res );
667 else {
668 # See if this item is more important than what we've got
669 # so far.
670 if ( $res->{'priority'} != 0 && $res->{'priority'} < $priority )
672 $priority = $res->{'priority'};
673 $highest = $res;
679 # If we get this far, then no exact match was found. Print the
680 # most important item on the list. I think this tells us who's
681 # next in line to get this book.
682 if ($highest) { # FIXME - $highest might be undefined
683 $highest->{'itemnumber'} = $item;
684 return ( "Reserved", $highest );
686 else {
687 return ( 0, 0 );
691 =item CancelReserve
693 &CancelReserve($biblionumber, $itemnumber, $borrowernumber);
695 Cancels a reserve.
697 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
698 cancel, but not both: if both are given, C<&CancelReserve> does
699 nothing.
701 C<$borrowernumber> is the borrower number of the patron on whose
702 behalf the book was reserved.
704 If C<$biblionumber> was given, C<&CancelReserve> also adjusts the
705 priorities of the other people who are waiting on the book.
707 =cut
709 sub CancelReserve {
710 my ( $biblio, $item, $borr ) = @_;
711 my $dbh = C4::Context->dbh;
712 if ( $item and $borr ) {
713 # removing a waiting reserve record....
714 # update the database...
715 my $query = "
716 UPDATE reserves
717 SET cancellationdate = now(),
718 found = Null,
719 priority = 0
720 WHERE itemnumber = ?
721 AND borrowernumber = ?
723 my $sth = $dbh->prepare($query);
724 $sth->execute( $item, $borr );
725 $sth->finish;
726 $query = "
727 INSERT INTO old_reserves
728 SELECT * FROM reserves
729 WHERE itemnumber = ?
730 AND borrowernumber = ?
732 $sth = $dbh->prepare($query);
733 $sth->execute( $item, $borr );
734 $query = "
735 DELETE FROM reserves
736 WHERE itemnumber = ?
737 AND borrowernumber = ?
739 $sth = $dbh->prepare($query);
740 $sth->execute( $item, $borr );
742 else {
743 # removing a reserve record....
744 # get the prioritiy on this record....
745 my $priority;
746 my $query = qq/
747 SELECT priority FROM reserves
748 WHERE biblionumber = ?
749 AND borrowernumber = ?
750 AND cancellationdate IS NULL
751 AND itemnumber IS NULL
753 my $sth = $dbh->prepare($query);
754 $sth->execute( $biblio, $borr );
755 ($priority) = $sth->fetchrow_array;
756 $sth->finish;
757 $query = qq/
758 UPDATE reserves
759 SET cancellationdate = now(),
760 found = Null,
761 priority = 0
762 WHERE biblionumber = ?
763 AND borrowernumber = ?
766 # update the database, removing the record...
767 $sth = $dbh->prepare($query);
768 $sth->execute( $biblio, $borr );
769 $sth->finish;
771 $query = qq/
772 INSERT INTO old_reserves
773 SELECT * FROM reserves
774 WHERE biblionumber = ?
775 AND borrowernumber = ?
777 $sth = $dbh->prepare($query);
778 $sth->execute( $biblio, $borr );
780 $query = qq/
781 DELETE FROM reserves
782 WHERE biblionumber = ?
783 AND borrowernumber = ?
785 $sth = $dbh->prepare($query);
786 $sth->execute( $biblio, $borr );
788 # now fix the priority on the others....
789 _FixPriority( $priority, $biblio );
793 =item ModReserve
795 =over 4
797 ModReserve($rank, $biblio, $borrower, $branch[, $itemnumber])
799 =back
801 Change a hold request's priority or cancel it.
803 C<$rank> specifies the effect of the change. If C<$rank>
804 is 'W' or 'n', nothing happens. This corresponds to leaving a
805 request alone when changing its priority in the holds queue
806 for a bib.
808 If C<$rank> is 'del', the hold request is cancelled.
810 If C<$rank> is an integer greater than zero, the priority of
811 the request is set to that value. Since priority != 0 means
812 that the item is not waiting on the hold shelf, setting the
813 priority to a non-zero value also sets the request's found
814 status and waiting date to NULL.
816 The optional C<$itemnumber> parameter is used only when
817 C<$rank> is a non-zero integer; if supplied, the itemnumber
818 of the hold request is set accordingly; if omitted, the itemnumber
819 is cleared.
821 FIXME: Note that the forgoing can have the effect of causing
822 item-level hold requests to turn into title-level requests. This
823 will be fixed once reserves has separate columns for requested
824 itemnumber and supplying itemnumber.
826 =cut
828 sub ModReserve {
829 #subroutine to update a reserve
830 my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
831 return if $rank eq "W";
832 return if $rank eq "n";
833 my $dbh = C4::Context->dbh;
834 if ( $rank eq "del" ) {
835 my $query = qq/
836 UPDATE reserves
837 SET cancellationdate=now()
838 WHERE biblionumber = ?
839 AND borrowernumber = ?
841 my $sth = $dbh->prepare($query);
842 $sth->execute( $biblio, $borrower );
843 $sth->finish;
844 $query = qq/
845 INSERT INTO old_reserves
846 SELECT *
847 FROM reserves
848 WHERE biblionumber = ?
849 AND borrowernumber = ?
851 $sth = $dbh->prepare($query);
852 $sth->execute( $biblio, $borrower );
853 $query = qq/
854 DELETE FROM reserves
855 WHERE biblionumber = ?
856 AND borrowernumber = ?
858 $sth = $dbh->prepare($query);
859 $sth->execute( $biblio, $borrower );
862 elsif ($rank =~ /^\d+/ and $rank > 0) {
863 my $query = qq/
864 UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
865 WHERE biblionumber = ?
866 AND borrowernumber = ?
868 my $sth = $dbh->prepare($query);
869 $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
870 $sth->finish;
871 _FixPriority( $biblio, $borrower, $rank);
875 =item ModReserveFill
877 &ModReserveFill($reserve);
879 Fill a reserve. If I understand this correctly, this means that the
880 reserved book has been found and given to the patron who reserved it.
882 C<$reserve> specifies the reserve to fill. It is a reference-to-hash
883 whose keys are fields from the reserves table in the Koha database.
885 =cut
887 sub ModReserveFill {
888 my ($res) = @_;
889 my $dbh = C4::Context->dbh;
890 # fill in a reserve record....
891 my $biblionumber = $res->{'biblionumber'};
892 my $borrowernumber = $res->{'borrowernumber'};
893 my $resdate = $res->{'reservedate'};
895 # get the priority on this record....
896 my $priority;
897 my $query = "SELECT priority
898 FROM reserves
899 WHERE biblionumber = ?
900 AND borrowernumber = ?
901 AND reservedate = ?";
902 my $sth = $dbh->prepare($query);
903 $sth->execute( $biblionumber, $borrowernumber, $resdate );
904 ($priority) = $sth->fetchrow_array;
905 $sth->finish;
907 # update the database...
908 $query = "UPDATE reserves
909 SET found = 'F',
910 priority = 0
911 WHERE biblionumber = ?
912 AND reservedate = ?
913 AND borrowernumber = ?
915 $sth = $dbh->prepare($query);
916 $sth->execute( $biblionumber, $resdate, $borrowernumber );
917 $sth->finish;
919 # move to old_reserves
920 $query = "INSERT INTO old_reserves
921 SELECT * FROM reserves
922 WHERE biblionumber = ?
923 AND reservedate = ?
924 AND borrowernumber = ?
926 $sth = $dbh->prepare($query);
927 $sth->execute( $biblionumber, $resdate, $borrowernumber );
928 $query = "DELETE FROM reserves
929 WHERE biblionumber = ?
930 AND reservedate = ?
931 AND borrowernumber = ?
933 $sth = $dbh->prepare($query);
934 $sth->execute( $biblionumber, $resdate, $borrowernumber );
936 # now fix the priority on the others (if the priority wasn't
937 # already sorted!)....
938 unless ( $priority == 0 ) {
939 _FixPriority( $priority, $biblionumber );
943 =item ModReserveStatus
945 &ModReserveStatus($itemnumber, $newstatus);
947 Update the reserve status for the active (priority=0) reserve.
949 $itemnumber is the itemnumber the reserve is on
951 $newstatus is the new status.
953 =cut
955 sub ModReserveStatus {
957 #first : check if we have a reservation for this item .
958 my ($itemnumber, $newstatus) = @_;
959 my $dbh = C4::Context->dbh;
960 my $query = " UPDATE reserves
961 SET found=?,waitingdate = now()
962 WHERE itemnumber=?
963 AND found IS NULL
964 AND priority = 0
966 my $sth_set = $dbh->prepare($query);
967 $sth_set->execute( $newstatus, $itemnumber );
968 $sth_set->finish;
971 =item ModReserveAffect
973 &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
975 This function affect an item and a status for a given reserve
976 The itemnumber parameter is used to find the biblionumber.
977 with the biblionumber & the borrowernumber, we can affect the itemnumber
978 to the correct reserve.
980 if $transferToDo is not set, then the status is set to "Waiting" as well.
981 otherwise, a transfer is on the way, and the end of the transfer will
982 take care of the waiting status
983 =cut
985 sub ModReserveAffect {
986 my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
987 my $dbh = C4::Context->dbh;
989 # we want to attach $itemnumber to $borrowernumber, find the biblionumber
990 # attached to $itemnumber
991 my $sth = $dbh->prepare("SELECT biblionumber FROM items WHERE itemnumber=?");
992 $sth->execute($itemnumber);
993 my ($biblionumber) = $sth->fetchrow;
994 # If we affect a reserve that has to be transfered, don't set to Waiting
995 my $query;
996 if ($transferToDo) {
997 $query = "
998 UPDATE reserves
999 SET priority = 0,
1000 itemnumber = ?
1001 WHERE borrowernumber = ?
1002 AND biblionumber = ?
1005 else {
1006 # affect the reserve to Waiting as well.
1007 $query = "
1008 UPDATE reserves
1009 SET priority = 0,
1010 found = 'W',
1011 waitingdate=now(),
1012 itemnumber = ?
1013 WHERE borrowernumber = ?
1014 AND biblionumber = ?
1017 $sth = $dbh->prepare($query);
1018 $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
1019 $sth->finish;
1021 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo );
1023 return;
1026 =item ModReserveCancelAll
1028 ($messages,$nextreservinfo) = &ModReserveCancelAll($itemnumber,$borrowernumber);
1030 function to cancel reserv,check other reserves, and transfer document if it's necessary
1032 =cut
1034 sub ModReserveCancelAll {
1035 my $messages;
1036 my $nextreservinfo;
1037 my ( $itemnumber, $borrowernumber ) = @_;
1039 #step 1 : cancel the reservation
1040 my $CancelReserve = CancelReserve( undef, $itemnumber, $borrowernumber );
1042 #step 2 launch the subroutine of the others reserves
1043 ( $messages, $nextreservinfo ) = GetOtherReserves($itemnumber);
1045 return ( $messages, $nextreservinfo );
1048 =item ModReserveMinusPriority
1050 &ModReserveMinusPriority($itemnumber,$borrowernumber,$biblionumber)
1052 Reduce the values of queuded list
1054 =cut
1056 sub ModReserveMinusPriority {
1057 my ( $itemnumber, $borrowernumber, $biblionumber ) = @_;
1059 #first step update the value of the first person on reserv
1060 my $dbh = C4::Context->dbh;
1061 my $query = "
1062 UPDATE reserves
1063 SET priority = 0 , itemnumber = ?
1064 WHERE borrowernumber=?
1065 AND biblionumber=?
1067 my $sth_upd = $dbh->prepare($query);
1068 $sth_upd->execute( $itemnumber, $borrowernumber, $biblionumber );
1069 $sth_upd->finish;
1070 # second step update all others reservs
1071 _FixPriority($biblionumber, $borrowernumber, '0');
1074 =item GetReserveInfo
1076 &GetReserveInfo($borrowernumber,$biblionumber);
1078 Get item and borrower details for a current hold.
1079 Current implementation this query should have a single result.
1080 =cut
1082 sub GetReserveInfo {
1083 my ( $borrowernumber, $biblionumber ) = @_;
1084 my $dbh = C4::Context->dbh;
1085 my $strsth="SELECT reservedate, reservenotes, reserves.borrowernumber,
1086 reserves.biblionumber, reserves.branchcode,
1087 notificationdate, reminderdate, priority, found,
1088 firstname, surname, phone,
1089 email, address, address2,
1090 cardnumber, city, zipcode,
1091 biblio.title, biblio.author,
1092 items.holdingbranch, items.itemcallnumber, items.itemnumber,
1093 barcode, notes
1094 FROM reserves left join items
1095 ON items.itemnumber=reserves.itemnumber ,
1096 borrowers, biblio
1097 WHERE
1098 reserves.borrowernumber=? &&
1099 reserves.biblionumber=? &&
1100 reserves.borrowernumber=borrowers.borrowernumber &&
1101 reserves.biblionumber=biblio.biblionumber ";
1102 my $sth = $dbh->prepare($strsth);
1103 $sth->execute($borrowernumber,$biblionumber);
1105 my $data = $sth->fetchrow_hashref;
1106 return $data;
1110 =item IsAvailableForItemLevelRequest
1112 =over 4
1114 my $is_available = IsAvailableForItemLevelRequest($itemnumber);
1116 =back
1118 Checks whether a given item record is available for an
1119 item-level hold request. An item is available if
1121 * it is not lost AND
1122 * it is not damaged AND
1123 * it is not withdrawn AND
1124 * does not have a not for loan value > 0
1126 Whether or not the item is currently on loan is
1127 also checked - if the AllowOnShelfHolds system preference
1128 is ON, an item can be requested even if it is currently
1129 on loan to somebody else. If the system preference
1130 is OFF, an item that is currently checked out cannot
1131 be the target of an item-level hold request.
1133 Note that IsAvailableForItemLevelRequest() does not
1134 check if the staff operator is authorized to place
1135 a request on the item - in particular,
1136 this routine does not check IndependantBranches
1137 and canreservefromotherbranches.
1139 =cut
1141 sub IsAvailableForItemLevelRequest {
1142 my $itemnumber = shift;
1144 my $item = GetItem($itemnumber);
1146 # must check the notforloan setting of the itemtype
1147 # FIXME - a lot of places in the code do this
1148 # or something similar - need to be
1149 # consolidated
1150 my $dbh = C4::Context->dbh;
1151 my $notforloan_query;
1152 if (C4::Context->preference('item-level_itypes')) {
1153 $notforloan_query = "SELECT itemtypes.notforloan
1154 FROM items
1155 JOIN itemtypes ON (itemtypes.itemtype = items.itype)
1156 WHERE itemnumber = ?";
1157 } else {
1158 $notforloan_query = "SELECT itemtypes.notforloan
1159 FROM items
1160 JOIN biblioitems USING (biblioitemnumber)
1161 JOIN itemtypes USING (itemtype)
1162 WHERE itemnumber = ?";
1164 my $sth = $dbh->prepare($notforloan_query);
1165 $sth->execute($itemnumber);
1166 my $notforloan_per_itemtype = 0;
1167 if (my ($notforloan) = $sth->fetchrow_array) {
1168 $notforloan_per_itemtype = 1 if $notforloan;
1171 my $available_per_item = 1;
1172 $available_per_item = 0 if $item->{itemlost} or
1173 ( $item->{notforloan} > 0 ) or
1174 ($item->{damaged} and not C4::Context->preference('AllowHoldsOnDamagedItems')) or
1175 $item->{wthdrawn} or
1176 $notforloan_per_itemtype;
1179 if (C4::Context->preference('AllowOnShelfHolds')) {
1180 return $available_per_item;
1181 } else {
1182 return ($available_per_item and $item->{onloan});
1186 =item _FixPriority
1188 &_FixPriority($biblio,$borrowernumber,$rank);
1190 Only used internally (so don't export it)
1191 Changed how this functions works #
1192 Now just gets an array of reserves in the rank order and updates them with
1193 the array index (+1 as array starts from 0)
1194 and if $rank is supplied will splice item from the array and splice it back in again
1195 in new priority rank
1197 =cut
1199 sub _FixPriority {
1200 my ( $biblio, $borrowernumber, $rank ) = @_;
1201 my $dbh = C4::Context->dbh;
1202 if ( $rank eq "del" ) {
1203 CancelReserve( $biblio, undef, $borrowernumber );
1205 if ( $rank eq "W" || $rank eq "0" ) {
1207 # make sure priority for waiting items is 0
1208 my $query = qq/
1209 UPDATE reserves
1210 SET priority = 0
1211 WHERE biblionumber = ?
1212 AND borrowernumber = ?
1213 AND found ='W'
1215 my $sth = $dbh->prepare($query);
1216 $sth->execute( $biblio, $borrowernumber );
1218 my @priority;
1219 my @reservedates;
1221 # get whats left
1222 # FIXME adding a new security in returned elements for changing priority,
1223 # now, we don't care anymore any reservations with itemnumber linked (suppose a waiting reserve)
1224 # This is wrong a waiting reserve has W set
1225 # The assumption that having an itemnumber set means waiting is wrong and should be corrected any place it occurs
1226 my $query = qq/
1227 SELECT borrowernumber, reservedate, constrainttype
1228 FROM reserves
1229 WHERE biblionumber = ?
1230 AND ((found <> 'W') or found is NULL)
1231 ORDER BY priority ASC
1233 my $sth = $dbh->prepare($query);
1234 $sth->execute($biblio);
1235 while ( my $line = $sth->fetchrow_hashref ) {
1236 push( @reservedates, $line );
1237 push( @priority, $line );
1240 # To find the matching index
1241 my $i;
1242 my $key = -1; # to allow for 0 to be a valid result
1243 for ( $i = 0 ; $i < @priority ; $i++ ) {
1244 if ( $borrowernumber == $priority[$i]->{'borrowernumber'} ) {
1245 $key = $i; # save the index
1246 last;
1250 # if index exists in array then move it to new position
1251 if ( $key > -1 && $rank ne 'del' && $rank > 0 ) {
1252 my $new_rank = $rank -
1253 1; # $new_rank is what you want the new index to be in the array
1254 my $moving_item = splice( @priority, $key, 1 );
1255 splice( @priority, $new_rank, 0, $moving_item );
1258 # now fix the priority on those that are left....
1259 $query = "
1260 UPDATE reserves
1261 SET priority = ?
1262 WHERE biblionumber = ?
1263 AND borrowernumber = ?
1264 AND reservedate = ?
1265 AND found IS NULL
1267 $sth = $dbh->prepare($query);
1268 for ( my $j = 0 ; $j < @priority ; $j++ ) {
1269 $sth->execute(
1270 $j + 1, $biblio,
1271 $priority[$j]->{'borrowernumber'},
1272 $priority[$j]->{'reservedate'}
1274 $sth->finish;
1278 =item _Findgroupreserve
1280 @results = &_Findgroupreserve($biblioitemnumber, $biblionumber, $itemnumber);
1282 ****** FIXME ******
1283 I don't know what this does, because I don't understand how reserve
1284 constraints work. I think the idea is that you reserve a particular
1285 biblio, and the constraint allows you to restrict it to a given
1286 biblioitem (e.g., if you want to borrow the audio book edition of "The
1287 Prophet", rather than the first available publication).
1289 C<&_Findgroupreserve> returns :
1290 C<@results> is an array of references-to-hash whose keys are mostly
1291 fields from the reserves table of the Koha database, plus
1292 C<biblioitemnumber>.
1294 =cut
1296 sub _Findgroupreserve {
1297 my ( $bibitem, $biblio, $itemnumber ) = @_;
1298 my $dbh = C4::Context->dbh;
1300 # check for exact targetted match
1301 my $item_level_target_query = qq/
1302 SELECT reserves.biblionumber AS biblionumber,
1303 reserves.borrowernumber AS borrowernumber,
1304 reserves.reservedate AS reservedate,
1305 reserves.branchcode AS branchcode,
1306 reserves.cancellationdate AS cancellationdate,
1307 reserves.found AS found,
1308 reserves.reservenotes AS reservenotes,
1309 reserves.priority AS priority,
1310 reserves.timestamp AS timestamp,
1311 biblioitems.biblioitemnumber AS biblioitemnumber,
1312 reserves.itemnumber AS itemnumber
1313 FROM reserves
1314 JOIN biblioitems USING (biblionumber)
1315 JOIN hold_fill_targets USING (biblionumber, borrowernumber, itemnumber)
1316 WHERE found IS NULL
1317 AND priority > 0
1318 AND item_level_request = 1
1319 AND itemnumber = ?
1321 my $sth = $dbh->prepare($item_level_target_query);
1322 $sth->execute($itemnumber);
1323 my @results;
1324 if ( my $data = $sth->fetchrow_hashref ) {
1325 push( @results, $data );
1327 return @results if @results;
1329 # check for title-level targetted match
1330 my $title_level_target_query = qq/
1331 SELECT reserves.biblionumber AS biblionumber,
1332 reserves.borrowernumber AS borrowernumber,
1333 reserves.reservedate AS reservedate,
1334 reserves.branchcode AS branchcode,
1335 reserves.cancellationdate AS cancellationdate,
1336 reserves.found AS found,
1337 reserves.reservenotes AS reservenotes,
1338 reserves.priority AS priority,
1339 reserves.timestamp AS timestamp,
1340 biblioitems.biblioitemnumber AS biblioitemnumber,
1341 reserves.itemnumber AS itemnumber
1342 FROM reserves
1343 JOIN biblioitems USING (biblionumber)
1344 JOIN hold_fill_targets USING (biblionumber, borrowernumber)
1345 WHERE found IS NULL
1346 AND priority > 0
1347 AND item_level_request = 0
1348 AND hold_fill_targets.itemnumber = ?
1350 $sth = $dbh->prepare($title_level_target_query);
1351 $sth->execute($itemnumber);
1352 @results = ();
1353 if ( my $data = $sth->fetchrow_hashref ) {
1354 push( @results, $data );
1356 return @results if @results;
1358 my $query = qq/
1359 SELECT reserves.biblionumber AS biblionumber,
1360 reserves.borrowernumber AS borrowernumber,
1361 reserves.reservedate AS reservedate,
1362 reserves.branchcode AS branchcode,
1363 reserves.cancellationdate AS cancellationdate,
1364 reserves.found AS found,
1365 reserves.reservenotes AS reservenotes,
1366 reserves.priority AS priority,
1367 reserves.timestamp AS timestamp,
1368 reserveconstraints.biblioitemnumber AS biblioitemnumber,
1369 reserves.itemnumber AS itemnumber
1370 FROM reserves
1371 LEFT JOIN reserveconstraints ON reserves.biblionumber = reserveconstraints.biblionumber
1372 WHERE reserves.biblionumber = ?
1373 AND ( ( reserveconstraints.biblioitemnumber = ?
1374 AND reserves.borrowernumber = reserveconstraints.borrowernumber
1375 AND reserves.reservedate =reserveconstraints.reservedate )
1376 OR reserves.constrainttype='a' )
1377 AND (reserves.itemnumber IS NULL OR reserves.itemnumber = ?)
1379 $sth = $dbh->prepare($query);
1380 $sth->execute( $biblio, $bibitem, $itemnumber );
1381 @results = ();
1382 while ( my $data = $sth->fetchrow_hashref ) {
1383 push( @results, $data );
1385 $sth->finish;
1386 return @results;
1389 =item _koha_notify_reserve
1391 =over 4
1393 _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
1395 =back
1397 Sends a notification to the patron that their hold has been filled (through
1398 ModReserveAffect, _not_ ModReserveFill)
1400 =cut
1402 sub _koha_notify_reserve {
1403 my ($itemnumber, $borrowernumber, $biblionumber) = @_;
1405 my $dbh = C4::Context->dbh;
1406 my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold Filled' } );
1408 return if ( !defined( $messagingprefs->{'letter_code'} ) );
1410 my $sth = $dbh->prepare("
1411 SELECT *
1412 FROM reserves
1413 WHERE borrowernumber = ?
1414 AND biblionumber = ?
1416 $sth->execute( $borrowernumber, $biblionumber );
1417 my $reserve = $sth->fetchrow_hashref;
1418 my $branch_details = GetBranchDetail( $reserve->{'branchcode'} );
1420 my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
1422 my $letter = getletter( 'reserves', $messagingprefs->{'letter_code'} );
1424 C4::Letters::parseletter( $letter, 'branches', $reserve->{'branchcode'} );
1425 C4::Letters::parseletter( $letter, 'borrowers', $reserve->{'borrowernumber'} );
1426 C4::Letters::parseletter( $letter, 'biblio', $reserve->{'biblionumber'} );
1427 C4::Letters::parseletter( $letter, 'reserves', $reserve->{'borrowernumber'}, $reserve->{'biblionumber'} );
1429 if ( $reserve->{'itemnumber'} ) {
1430 C4::Letters::parseletter( $letter, 'items', $reserve->{'itemnumber'} );
1432 $letter->{'content'} =~ s/<<[a-z0-9_]+\.[a-z0-9]+>>//g; #remove any stragglers
1434 if ( -1 != firstidx { $_ eq 'email' } @{$messagingprefs->{transports}} ) {
1435 # aka, 'email' in ->{'transports'}
1436 C4::Letters::EnqueueLetter(
1437 { letter => $letter,
1438 borrowernumber => $borrowernumber,
1439 message_transport_type => 'email',
1440 from_address => $admin_email_address,
1445 if ( -1 != firstidx { $_ eq 'sms' } @{$messagingprefs->{transports}} ) {
1446 C4::Letters::EnqueueLetter(
1447 { letter => $letter,
1448 borrowernumber => $borrowernumber,
1449 message_transport_type => 'sms',
1455 =back
1457 =head1 AUTHOR
1459 Koha Developement team <info@koha.org>
1461 =cut