use C4::Letters;
use C4::Log;
+use Koha::Biblios;
use Koha::DateUtils;
use Koha::Calendar;
use Koha::Database;
use Koha::Hold;
+use Koha::Old::Hold;
use Koha::Holds;
use Koha::Libraries;
+use Koha::IssuingRules;
use Koha::Items;
use Koha::ItemTypes;
+use Koha::Patrons;
use List::MoreUtils qw( firstidx any );
use Carp;
&AddReserve
&GetReserve
- &GetReservesFromItemnumber
- &GetReservesFromBiblionumber
- &GetReservesFromBorrowernumber
&GetReservesForBranch
- &GetReservesToBranch
- &GetReserveCount
- &GetReserveInfo
&GetReserveStatus
&GetOtherReserves
&GetReservesControlBranch
IsItemOnHoldAndFound
+
+ GetMaxPatronHoldsForRecord
);
@EXPORT_OK = qw( MergeHolds );
}
biblio
biblioitems
items
+ reserves
=cut
$title, $checkitem, $found, $itemtype
) = @_;
- if ( Koha::Holds->search( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } )->count() > 0 ) {
- carp("AddReserve: borrower $borrowernumber already has a hold for biblionumber $biblionumber");
- return;
- }
-
- my $dbh = C4::Context->dbh;
-
$resdate = output_pref( { str => dt_from_string( $resdate ), dateonly => 1, dateformat => 'iso' })
or output_pref({ dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
my $reserve_id = $hold->id();
# add a reserve fee if needed
- my $fee = GetReserveFee( $borrowernumber, $biblionumber );
- ChargeReserveFee( $borrowernumber, $fee, $title );
+ if ( C4::Context->preference('HoldFeeMode') ne 'any_time_is_collected' ) {
+ my $reserve_fee = GetReserveFee( $borrowernumber, $biblionumber );
+ ChargeReserveFee( $borrowernumber, $reserve_fee, $title );
+ }
_FixPriority({ biblionumber => $biblionumber});
# Send e-mail to librarian if syspref is active
if(C4::Context->preference("emailLibrarianWhenHoldIsPlaced")){
- my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
- my $library = Koha::Libraries->find($borrower->{branchcode})->unblessed;
+ my $patron = Koha::Patrons->find( $borrowernumber );
+ my $library = $patron->library;
if ( my $letter = C4::Letters::GetPreparedLetter (
module => 'reserves',
letter_code => 'HOLDPLACED',
branchcode => $branch,
+ lang => $patron->lang,
tables => {
- 'branches' => $library,
- 'borrowers' => $borrower,
+ 'branches' => $library->unblessed,
+ 'borrowers' => $patron->unblessed,
'biblio' => $biblionumber,
'biblioitems' => $biblionumber,
'items' => $checkitem,
+ 'reserves' => $hold->unblessed,
},
) ) {
- my $admin_email_address = $library->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
+ my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
C4::Letters::EnqueueLetter(
{ letter => $letter,
return $sth->fetchrow_hashref();
}
-=head2 GetReservesFromBiblionumber
-
- my $reserves = GetReservesFromBiblionumber({
- biblionumber => $biblionumber,
- [ itemnumber => $itemnumber, ]
- [ all_dates => 1|0 ]
- });
-
-This function gets the list of reservations for one C<$biblionumber>,
-returning an arrayref pointing to the reserves for C<$biblionumber>.
-
-By default, only reserves whose start date falls before the current
-time are returned. To return all reserves, including future ones,
-the C<all_dates> parameter can be included and set to a true value.
-
-If the C<itemnumber> parameter is supplied, reserves must be targeted
-to that item or not targeted to any item at all; otherwise, they
-are excluded from the list.
-
-=cut
-
-sub GetReservesFromBiblionumber {
- my ( $params ) = @_;
- my $biblionumber = $params->{biblionumber} or return [];
- my $itemnumber = $params->{itemnumber};
- my $all_dates = $params->{all_dates} // 0;
- my $dbh = C4::Context->dbh;
-
- # Find the desired items in the reserves
- my @params;
- my $query = "
- SELECT reserve_id,
- branchcode,
- timestamp AS rtimestamp,
- priority,
- biblionumber,
- borrowernumber,
- reservedate,
- found,
- itemnumber,
- reservenotes,
- expirationdate,
- lowestPriority,
- suspend,
- suspend_until,
- itemtype
- FROM reserves
- WHERE biblionumber = ? ";
- push( @params, $biblionumber );
- unless ( $all_dates ) {
- $query .= " AND reservedate <= CAST(NOW() AS DATE) ";
- }
- if ( $itemnumber ) {
- $query .= " AND ( itemnumber IS NULL OR itemnumber = ? )";
- push( @params, $itemnumber );
- }
- $query .= "ORDER BY priority";
- my $sth = $dbh->prepare($query);
- $sth->execute( @params );
- my @results;
- while ( my $data = $sth->fetchrow_hashref ) {
- push @results, $data;
- }
- return \@results;
-}
-
-=head2 GetReservesFromItemnumber
-
- ( $reservedate, $borrowernumber, $branchcode, $reserve_id, $waitingdate ) = GetReservesFromItemnumber($itemnumber);
-
-Get the first reserve for a specific item number (based on priority). Returns the abovementioned values for that reserve.
-
-The routine does not look at future reserves (read: item level holds), but DOES include future waits (a confirmed future hold).
-
-=cut
-
-sub GetReservesFromItemnumber {
- my ($itemnumber) = @_;
-
- my $schema = Koha::Database->new()->schema();
-
- my $r = $schema->resultset('Reserve')->search(
- {
- itemnumber => $itemnumber,
- suspend => 0,
- -or => [
- reservedate => \'<= CAST( NOW() AS DATE )',
- waitingdate => { '!=', undef }
- ]
- },
- {
- order_by => 'priority',
- }
- )->first();
-
- return unless $r;
-
- return (
- $r->reservedate(),
- $r->get_column('borrowernumber'),
- $r->get_column('branchcode'),
- $r->reserve_id(),
- $r->waitingdate(),
- );
-}
-
-=head2 GetReservesFromBorrowernumber
-
- $borrowerreserv = GetReservesFromBorrowernumber($borrowernumber,$tatus);
-
-TODO :: Descritpion
-
-=cut
-
-sub GetReservesFromBorrowernumber {
- my ( $borrowernumber, $status ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth;
- if ($status) {
- $sth = $dbh->prepare("
- SELECT *
- FROM reserves
- WHERE borrowernumber=?
- AND found =?
- ORDER BY reservedate
- ");
- $sth->execute($borrowernumber,$status);
- } else {
- $sth = $dbh->prepare("
- SELECT *
- FROM reserves
- WHERE borrowernumber=?
- ORDER BY reservedate
- ");
- $sth->execute($borrowernumber);
- }
- my $data = $sth->fetchall_arrayref({});
- return @$data;
-}
-
=head2 CanBookBeReserved
$canReserve = &CanBookBeReserved($borrowernumber, $biblionumber)
=cut
-sub CanItemBeReserved{
- my ($borrowernumber, $itemnumber) = @_;
+sub CanItemBeReserved {
+ my ( $borrowernumber, $itemnumber ) = @_;
+
+ my $dbh = C4::Context->dbh;
+ my $ruleitemtype; # itemtype of the matching issuing rule
+ my $allowedreserves = 0; # Total number of holds allowed across all records
+ my $holds_per_record = 1; # Total number of holds allowed for this one given record
- my $dbh = C4::Context->dbh;
- my $ruleitemtype; # itemtype of the matching issuing rule
- my $allowedreserves = 0;
-
# we retrieve borrowers and items informations #
# item->{itype} will come for biblioitems if necessery
- my $item = GetItem($itemnumber);
- my $biblioData = C4::Biblio::GetBiblioData( $item->{biblionumber} );
- my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
+ my $item = GetItem($itemnumber);
+ my $biblio = Koha::Biblios->find( $item->{biblionumber} );
+ my $patron = Koha::Patrons->find( $borrowernumber );
+ my $borrower = $patron->unblessed;
# If an item is damaged and we don't allow holds on damaged items, we can stop right here
- return 'damaged' if ( $item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems') );
+ return 'damaged'
+ if ( $item->{damaged}
+ && !C4::Context->preference('AllowHoldsOnDamagedItems') );
- #Check for the age restriction
- my ($ageRestriction, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction( $biblioData->{agerestriction}, $borrower );
+ # Check for the age restriction
+ my ( $ageRestriction, $daysToAgeRestriction ) =
+ C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction, $borrower );
return 'ageRestricted' if $daysToAgeRestriction && $daysToAgeRestriction > 0;
+ # Check that the patron doesn't have an item level hold on this item already
+ return 'itemAlreadyOnHold'
+ if Koha::Holds->search( { borrowernumber => $borrowernumber, itemnumber => $itemnumber } )->count();
+
my $controlbranch = C4::Context->preference('ReservesControlBranch');
- # we retrieve user rights on this itemtype and branchcode
- my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
- FROM issuingrules
- WHERE (categorycode in (?,'*') )
- AND (itemtype IN (?,'*'))
- AND (branchcode IN (?,'*'))
- ORDER BY
- categorycode DESC,
- itemtype DESC,
- branchcode DESC;"
- );
-
- my $querycount ="SELECT
- count(*) as count
- FROM reserves
- LEFT JOIN items USING (itemnumber)
- LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
- LEFT JOIN borrowers USING (borrowernumber)
- WHERE borrowernumber = ?
- ";
-
-
- my $branchcode = "";
- my $branchfield = "reserves.branchcode";
-
- if( $controlbranch eq "ItemHomeLibrary" ){
+ my $querycount = q{
+ SELECT count(*) AS count
+ FROM reserves
+ LEFT JOIN items USING (itemnumber)
+ LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
+ LEFT JOIN borrowers USING (borrowernumber)
+ WHERE borrowernumber = ?
+ };
+
+ my $branchcode = "";
+ my $branchfield = "reserves.branchcode";
+
+ if ( $controlbranch eq "ItemHomeLibrary" ) {
$branchfield = "items.homebranch";
- $branchcode = $item->{homebranch};
- }elsif( $controlbranch eq "PatronLibrary" ){
+ $branchcode = $item->{homebranch};
+ }
+ elsif ( $controlbranch eq "PatronLibrary" ) {
$branchfield = "borrowers.branchcode";
- $branchcode = $borrower->{branchcode};
- }
-
- # we retrieve rights
- $sth->execute($borrower->{'categorycode'}, $item->{'itype'}, $branchcode);
- if(my $rights = $sth->fetchrow_hashref()){
- $ruleitemtype = $rights->{itemtype};
- $allowedreserves = $rights->{reservesallowed};
- }else{
+ $branchcode = $borrower->{branchcode};
+ }
+
+ # we retrieve rights
+ if ( my $rights = GetHoldRule( $borrower->{'categorycode'}, $item->{'itype'}, $branchcode ) ) {
+ $ruleitemtype = $rights->{itemtype};
+ $allowedreserves = $rights->{reservesallowed};
+ $holds_per_record = $rights->{holds_per_record};
+ }
+ else {
$ruleitemtype = '*';
}
+ $item = Koha::Items->find( $itemnumber );
+ my $holds = Koha::Holds->search(
+ {
+ borrowernumber => $borrowernumber,
+ biblionumber => $item->biblionumber,
+ found => undef, # Found holds don't count against a patron's holds limit
+ }
+ );
+ if ( $holds->count() >= $holds_per_record ) {
+ return "tooManyHoldsForThisRecord";
+ }
+
# we retrieve count
$querycount .= "AND $branchfield = ?";
-
+
# If using item-level itypes, fall back to the record
# level itemtype if the hold has no associated item
$querycount .=
if ( $ruleitemtype ne "*" );
my $sthcount = $dbh->prepare($querycount);
-
- if($ruleitemtype eq "*"){
- $sthcount->execute($borrowernumber, $branchcode);
- }else{
- $sthcount->execute($borrowernumber, $branchcode, $ruleitemtype);
+
+ if ( $ruleitemtype eq "*" ) {
+ $sthcount->execute( $borrowernumber, $branchcode );
+ }
+ else {
+ $sthcount->execute( $borrowernumber, $branchcode, $ruleitemtype );
}
my $reservecount = "0";
- if(my $rowcount = $sthcount->fetchrow_hashref()){
+ if ( my $rowcount = $sthcount->fetchrow_hashref() ) {
$reservecount = $rowcount->{count};
}
+
# we check if it's ok or not
- if( $reservecount >= $allowedreserves ){
+ if ( $reservecount >= $allowedreserves ) {
return 'tooManyReserves';
}
- my $circ_control_branch = C4::Circulation::_GetCircControlBranch($item,
- $borrower);
- my $branchitemrule = C4::Circulation::GetBranchItemRule($circ_control_branch,
- $item->{itype});
+ my $circ_control_branch =
+ C4::Circulation::_GetCircControlBranch( $item->unblessed(), $borrower );
+ my $branchitemrule =
+ C4::Circulation::GetBranchItemRule( $circ_control_branch, $item->itype );
if ( $branchitemrule->{holdallowed} == 0 ) {
return 'notReservable';
}
if ( $branchitemrule->{holdallowed} == 1
- && $borrower->{branchcode} ne $item->{homebranch} )
+ && $borrower->{branchcode} ne $item->homebranch )
{
- return 'cannotReserveFromOtherBranches';
+ return 'cannotReserveFromOtherBranches';
}
# If reservecount is ok, we check item branch if IndependentBranches is ON
if ( C4::Context->preference('IndependentBranches')
and !C4::Context->preference('canreservefromotherbranches') )
{
- my $itembranch = $item->{homebranch};
- if ($itembranch ne $borrower->{branchcode}) {
+ my $itembranch = $item->homebranch;
+ if ( $itembranch ne $borrower->{branchcode} ) {
return 'cannotReserveFromOtherBranches';
}
}
}
-=head2 GetReserveCount
-
- $number = &GetReserveCount($borrowernumber);
-
-this function returns the number of reservation for a borrower given on input arg.
-
-=cut
-
-sub GetReserveCount {
- my ($borrowernumber) = @_;
-
- my $dbh = C4::Context->dbh;
-
- my $query = "
- SELECT COUNT(*) AS counter
- FROM reserves
- WHERE borrowernumber = ?
- ";
- my $sth = $dbh->prepare($query);
- $sth->execute($borrowernumber);
- my $row = $sth->fetchrow_hashref;
- return $row->{counter};
-}
-
=head2 GetOtherReserves
($messages,$nextreservinfo)=$GetOtherReserves(itemnumber);
my $dbh = C4::Context->dbh;
my ( $fee ) = $dbh->selectrow_array( $borquery, undef, ($borrowernumber) );
my $hold_fee_mode = C4::Context->preference('HoldFeeMode') || 'not_always';
- if( $fee and $fee > 0 and $hold_fee_mode ne 'always' ) {
+ if( $fee and $fee > 0 and $hold_fee_mode eq 'not_always' ) {
# This is a reconstruction of the old code:
# Compare number of items with items issued, and optionally check holds
# If not all items are issued and there are no holds: charge no fee
return $fee;
}
-=head2 GetReservesToBranch
-
- @transreserv = GetReservesToBranch( $frombranch );
-
-Get reserve list for a given branch
-
-=cut
-
-sub GetReservesToBranch {
- my ( $frombranch ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare(
- "SELECT reserve_id,borrowernumber,reservedate,itemnumber,timestamp
- FROM reserves
- WHERE priority='0'
- AND branchcode=?"
- );
- $sth->execute( $frombranch );
- my @transreserv;
- my $i = 0;
- while ( my $data = $sth->fetchrow_hashref ) {
- $transreserv[$i] = $data;
- $i++;
- }
- return (@transreserv);
-}
-
=head2 GetReservesForBranch
@transreserv = GetReservesForBranch($frombranch);
my $dbh = C4::Context->dbh;
my $query = "
- SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate
- FROM reserves
+ SELECT reserve_id,borrowernumber,reservedate,itemnumber,waitingdate, expirationdate
+ FROM reserves
WHERE priority='0'
AND found='W'
";
if ( $res->{'itemnumber'} == $itemnumber && $res->{'priority'} == 0) {
return ( "Waiting", $res, \@reserves ); # Found it
} else {
- my $borrowerinfo;
+ my $patron;
my $iteminfo;
my $local_hold_match;
if ($LocalHoldsPriority) {
- $borrowerinfo = C4::Members::GetMember( borrowernumber => $res->{'borrowernumber'} );
+ $patron = Koha::Patrons->find( $res->{borrowernumber} );
$iteminfo = C4::Items::GetItem($itemnumber);
my $local_holds_priority_item_branchcode =
( $LocalHoldsPriorityPatronControl eq 'PickupLibrary' )
? $res->{branchcode}
: ( $LocalHoldsPriorityPatronControl eq 'HomeLibrary' )
- ? $borrowerinfo->{branchcode}
+ ? $patron->branchcode
: undef;
$local_hold_match =
$local_holds_priority_item_branchcode eq
if ( ( $res->{'priority'} && $res->{'priority'} < $priority ) || $local_hold_match ) {
$iteminfo ||= C4::Items::GetItem($itemnumber);
next if $res->{itemtype} && $res->{itemtype} ne _get_itype( $iteminfo );
- $borrowerinfo ||= C4::Members::GetMember( borrowernumber => $res->{'borrowernumber'} );
- my $branch = GetReservesControlBranch( $iteminfo, $borrowerinfo );
+ $patron ||= Koha::Patrons->find( $res->{borrowernumber} );
+ my $branch = GetReservesControlBranch( $iteminfo, $patron->unblessed );
my $branchitemrule = C4::Circulation::GetBranchItemRule($branch,$iteminfo->{'itype'});
next if ($branchitemrule->{'holdallowed'} == 0);
- next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $borrowerinfo->{'branchcode'}));
+ next if (($branchitemrule->{'holdallowed'} == 1) && ($branch ne $patron->branchcode));
next if ( ($branchitemrule->{hold_fulfillment_policy} ne 'any') && ($res->{branchcode} ne $iteminfo->{ $branchitemrule->{hold_fulfillment_policy} }) );
$priority = $res->{'priority'};
$highest = $res;
sub CancelExpiredReserves {
- # Cancel reserves that have passed their expiration date.
+ my $today = dt_from_string();
+ my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
+
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare( "
SELECT * FROM reserves WHERE DATE(expirationdate) < DATE( CURDATE() )
AND expirationdate IS NOT NULL
- AND found IS NULL
" );
$sth->execute();
while ( my $res = $sth->fetchrow_hashref() ) {
- CancelReserve({ reserve_id => $res->{'reserve_id'} });
- }
-
- # Cancel reserves that have been waiting too long
- if ( C4::Context->preference("ExpireReservesMaxPickUpDelay") ) {
- my $max_pickup_delay = C4::Context->preference("ReservesMaxPickUpDelay");
- my $cancel_on_holidays = C4::Context->preference('ExpireReservesOnHolidays');
-
- my $today = dt_from_string();
-
- my $query = "SELECT * FROM reserves WHERE TO_DAYS( NOW() ) - TO_DAYS( waitingdate ) > ? AND found = 'W' AND priority = 0";
- $sth = $dbh->prepare( $query );
- $sth->execute( $max_pickup_delay );
+ my $calendar = Koha::Calendar->new( branchcode => $res->{'branchcode'} );
+ my $cancel_params = { reserve_id => $res->{'reserve_id'} };
- while ( my $res = $sth->fetchrow_hashref ) {
- my $do_cancel = 1;
- unless ( $cancel_on_holidays ) {
- my $calendar = Koha::Calendar->new( branchcode => $res->{'branchcode'} );
- my $is_holiday = $calendar->is_holiday( $today );
-
- if ( $is_holiday ) {
- $do_cancel = 0;
- }
- }
+ next if !$cancel_on_holidays && $calendar->is_holiday( $today );
- if ( $do_cancel ) {
- CancelReserve({ reserve_id => $res->{'reserve_id'}, charge_cancel_fee => 1 });
- }
+ if ( $res->{found} eq 'W' ) {
+ $cancel_params->{charge_cancel_fee} = 1;
}
- }
+ CancelReserve($cancel_params);
+ }
}
=head2 AutoUnsuspendReserves
return unless ( $reserve_id || ( $borrowernumber && ( $biblionumber || $itemnumber ) ) );
$reserve_id = GetReserveId({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, itemnumber => $itemnumber }) unless ( $reserve_id );
- my $dbh = C4::Context->dbh;
if ( $rank eq "del" ) {
CancelReserve({ reserve_id => $reserve_id });
}
sub ModReserveFill {
my ($res) = @_;
- my $dbh = C4::Context->dbh;
- # fill in a reserve record....
my $reserve_id = $res->{'reserve_id'};
- my $biblionumber = $res->{'biblionumber'};
- my $borrowernumber = $res->{'borrowernumber'};
- my $resdate = $res->{'reservedate'};
+
+ my $hold = Koha::Holds->find($reserve_id);
# get the priority on this record....
- my $priority;
- my $query = "SELECT priority
- FROM reserves
- WHERE biblionumber = ?
- AND borrowernumber = ?
- AND reservedate = ?";
- my $sth = $dbh->prepare($query);
- $sth->execute( $biblionumber, $borrowernumber, $resdate );
- ($priority) = $sth->fetchrow_array;
+ my $priority = $hold->priority;
- # update the database...
- $query = "UPDATE reserves
- SET found = 'F',
- priority = 0
- WHERE biblionumber = ?
- AND reservedate = ?
- AND borrowernumber = ?
- ";
- $sth = $dbh->prepare($query);
- $sth->execute( $biblionumber, $resdate, $borrowernumber );
-
- # move to old_reserves
- $query = "INSERT INTO old_reserves
- SELECT * FROM reserves
- WHERE biblionumber = ?
- AND reservedate = ?
- AND borrowernumber = ?
- ";
- $sth = $dbh->prepare($query);
- $sth->execute( $biblionumber, $resdate, $borrowernumber );
- $query = "DELETE FROM reserves
- WHERE biblionumber = ?
- AND reservedate = ?
- AND borrowernumber = ?
- ";
- $sth = $dbh->prepare($query);
- $sth->execute( $biblionumber, $resdate, $borrowernumber );
+ # update the hold statuses, no need to store it though, we will be deleting it anyway
+ $hold->set(
+ {
+ found => 'F',
+ priority => 0,
+ }
+ );
+
+ Koha::Old::Hold->new( $hold->unblessed() )->store();
+
+ $hold->delete();
+
+ if ( C4::Context->preference('HoldFeeMode') eq 'any_time_is_collected' ) {
+ my $reserve_fee = GetReserveFee( $hold->borrowernumber, $hold->biblionumber );
+ ChargeReserveFee( $hold->borrowernumber, $reserve_fee, $hold->biblio->title );
+ }
# now fix the priority on the others (if the priority wasn't
# already sorted!)....
unless ( $priority == 0 ) {
- _FixPriority({ reserve_id => $reserve_id, biblionumber => $biblionumber });
+ _FixPriority( { reserve_id => $reserve_id, biblionumber => $hold->biblionumber } );
}
}
=head2 ModReserveAffect
- &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend);
+ &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend,$reserve_id);
-This function affect an item and a status for a given reserve
-The itemnumber parameter is used to find the biblionumber.
-with the biblionumber & the borrowernumber, we can affect the itemnumber
-to the correct reserve.
+This function affect an item and a status for a given reserve, either fetched directly
+by record_id, or by borrowernumber and itemnumber or biblionumber. If only biblionumber
+is given, only first reserve returned is affected, which is ok for anything but
+multi-item holds.
if $transferToDo is not set, then the status is set to "Waiting" as well.
otherwise, a transfer is on the way, and the end of the transfer will
=cut
sub ModReserveAffect {
- my ( $itemnumber, $borrowernumber,$transferToDo ) = @_;
+ my ( $itemnumber, $borrowernumber, $transferToDo, $reserve_id ) = @_;
my $dbh = C4::Context->dbh;
# we want to attach $itemnumber to $borrowernumber, find the biblionumber
# get request - need to find out if item is already
# waiting in order to not send duplicate hold filled notifications
- my $reserve_id = GetReserveId({
- borrowernumber => $borrowernumber,
- biblionumber => $biblionumber,
- });
- return unless defined $reserve_id;
- my $request = GetReserveInfo($reserve_id);
- my $already_on_shelf = ($request && $request->{found} eq 'W') ? 1 : 0;
-
- # If we affect a reserve that has to be transferred, don't set to Waiting
- my $query;
- if ($transferToDo) {
- $query = "
- UPDATE reserves
- SET priority = 0,
- itemnumber = ?,
- found = 'T'
- WHERE borrowernumber = ?
- AND biblionumber = ?
- ";
- }
- else {
- # affect the reserve to Waiting as well.
- $query = "
- UPDATE reserves
- SET priority = 0,
- found = 'W',
- waitingdate = NOW(),
- itemnumber = ?
- WHERE borrowernumber = ?
- AND biblionumber = ?
- ";
- }
- $sth = $dbh->prepare($query);
- $sth->execute( $itemnumber, $borrowernumber,$biblionumber);
- _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber ) if ( !$transferToDo && !$already_on_shelf );
+
+ my $hold;
+ # Find hold by id if we have it
+ $hold = Koha::Holds->find( $reserve_id ) if $reserve_id;
+ # Find item level hold for this item if there is one
+ $hold ||= Koha::Holds->search( { borrowernumber => $borrowernumber, itemnumber => $itemnumber } )->next();
+ # Find record level hold if there is no item level hold
+ $hold ||= Koha::Holds->search( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } )->next();
+
+ return unless $hold;
+
+ my $already_on_shelf = $hold->found && $hold->found eq 'W';
+
+ $hold->itemnumber($itemnumber);
+ $hold->set_waiting($transferToDo);
+
+ _koha_notify_reserve( $hold->reserve_id )
+ if ( !$transferToDo && !$already_on_shelf );
+
_FixPriority( { biblionumber => $biblionumber } );
+
if ( C4::Context->preference("ReturnToShelvingCart") ) {
- CartToShelf( $itemnumber );
+ CartToShelf($itemnumber);
}
return;
my $dbh = C4::Context->dbh;
my $query = "
UPDATE reserves
- SET priority = 0 , itemnumber = ?
+ SET priority = 0 , itemnumber = ?
WHERE reserve_id = ?
";
my $sth_upd = $dbh->prepare($query);
_FixPriority({ reserve_id => $reserve_id, rank => '0' });
}
-=head2 GetReserveInfo
-
- &GetReserveInfo($reserve_id);
-
-Get item and borrower details for a current hold.
-Current implementation this query should have a single result.
-
-=cut
-
-sub GetReserveInfo {
- my ( $reserve_id ) = @_;
- my $dbh = C4::Context->dbh;
- my $strsth="SELECT
- reserve_id,
- reservedate,
- reservenotes,
- reserves.borrowernumber,
- reserves.biblionumber,
- reserves.branchcode,
- reserves.waitingdate,
- notificationdate,
- reminderdate,
- priority,
- found,
- firstname,
- surname,
- phone,
- email,
- address,
- address2,
- cardnumber,
- city,
- zipcode,
- biblio.title,
- biblio.author,
- items.holdingbranch,
- items.itemcallnumber,
- items.itemnumber,
- items.location,
- barcode,
- notes
- FROM reserves
- LEFT JOIN items USING(itemnumber)
- LEFT JOIN borrowers USING(borrowernumber)
- LEFT JOIN biblio ON (reserves.biblionumber=biblio.biblionumber)
- WHERE reserves.reserve_id = ?";
- my $sth = $dbh->prepare($strsth);
- $sth->execute($reserve_id);
-
- my $data = $sth->fetchrow_hashref;
- return $data;
-}
-
=head2 IsAvailableForItemLevelRequest
my $is_available = IsAvailableForItemLevelRequest($item_record,$borrower_record);
foreach my $i (@items) {
$any_available = 1
unless $i->itemlost
- || $i->{notforloan} > 0
+ || $i->notforloan > 0
|| $i->withdrawn
|| $i->onloan
|| IsItemOnHoldAndFound( $i->id )
sub _OnShelfHoldsAllowed {
my ($itype,$borrowercategory,$branchcode) = @_;
- my $rule = C4::Circulation::GetIssuingRule($borrowercategory, $itype, $branchcode);
- return $rule->{onshelfholds};
+ my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowercategory, itemtype => $itype, branchcode => $branchcode });
+ return $issuing_rule ? $issuing_rule->onshelfholds : undef;
}
=head2 AlterPriority
sub AlterPriority {
my ( $where, $reserve_id ) = @_;
- my $dbh = C4::Context->dbh;
-
my $reserve = GetReserve( $reserve_id );
if ( $reserve->{cancellationdate} ) {
my $sth = $dbh->prepare( "UPDATE reserves SET lowestPriority = NOT lowestPriority WHERE reserve_id = ?");
$sth->execute( $reserve_id );
-
+
_FixPriority({ reserve_id => $reserve_id, rank => '999999' });
}
$priority[$j]->{'reserve_id'}
);
}
-
+
$sth = $dbh->prepare( "SELECT reserve_id FROM reserves WHERE lowestPriority = 1 ORDER BY priority" );
$sth->execute();
=head2 _koha_notify_reserve
- _koha_notify_reserve( $itemnumber, $borrowernumber, $biblionumber );
+ _koha_notify_reserve( $hold->reserve_id );
Sends a notification to the patron that their hold has been filled (through
ModReserveAffect, _not_ ModReserveFill)
=cut
sub _koha_notify_reserve {
- my ($itemnumber, $borrowernumber, $biblionumber) = @_;
+ my $reserve_id = shift;
+ my $hold = Koha::Holds->find($reserve_id);
+ my $borrowernumber = $hold->borrowernumber;
- my $dbh = C4::Context->dbh;
- my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
+ my $patron = Koha::Patrons->find( $borrowernumber );
# Try to get the borrower's email address
my $to_address = C4::Members::GetNoticeEmailAddress($borrowernumber);
message_name => 'Hold_Filled'
} );
- my $sth = $dbh->prepare("
- SELECT *
- FROM reserves
- WHERE borrowernumber = ?
- AND biblionumber = ?
- ");
- $sth->execute( $borrowernumber, $biblionumber );
- my $reserve = $sth->fetchrow_hashref;
- my $library = Koha::Libraries->find( $reserve->{branchcode} )->unblessed;
+ my $library = Koha::Libraries->find( $hold->branchcode )->unblessed;
my $admin_email_address = $library->{branchemail} || C4::Context->preference('KohaAdminEmailAddress');
my %letter_params = (
module => 'reserves',
- branchcode => $reserve->{branchcode},
+ branchcode => $hold->branchcode,
+ lang => $patron->lang,
tables => {
'branches' => $library,
- 'borrowers' => $borrower,
- 'biblio' => $biblionumber,
- 'biblioitems' => $biblionumber,
- 'reserves' => $reserve,
- 'items', $reserve->{'itemnumber'},
+ 'borrowers' => $patron->unblessed,
+ 'biblio' => $hold->biblionumber,
+ 'biblioitems' => $hold->biblionumber,
+ 'reserves' => $hold->unblessed,
+ 'items' => $hold->itemnumber,
},
- substitute => { today => output_pref( { dt => dt_from_string, dateonly => 1 } ) },
);
my $notification_sent = 0; #Keeping track if a Hold_filled message is sent. If no message can be sent, then default to a print message.
while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports} } ) {
next if (
( $mtt eq 'email' and not $to_address ) # No email address
- or ( $mtt eq 'sms' and not $borrower->{smsalertnumber} ) # No SMS number
+ or ( $mtt eq 'sms' and not $patron->smsalertnumber ) # No SMS number
or ( $mtt eq 'phone' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl
);
if (! $notification_sent) {
&$send_notification('print', 'HOLD');
}
-
+
}
=head2 _ShiftPriorityByDateAndPriority
return unless $res;
my $biblionumber = $res->{biblionumber};
- my $biblioitemnumber = $res->{biblioitemnumber};
if ($res->{borrowernumber} == $borrowernumber) {
ModReserveFill($res);
return unless ( ( $params->{'biblionumber'} || $params->{'itemnumber'} ) && $params->{'borrowernumber'} );
- my $dbh = C4::Context->dbh();
-
- my $sql = "SELECT reserve_id FROM reserves WHERE ";
-
- my @params;
- my @limits;
foreach my $key ( keys %$params ) {
- if ( defined( $params->{$key} ) ) {
- push( @limits, "$key = ?" );
- push( @params, $params->{$key} );
- }
+ delete $params->{$key} unless defined( $params->{$key} );
}
- $sql .= join( " AND ", @limits );
+ my $hold = Koha::Holds->search( $params )->next();
- my $sth = $dbh->prepare( $sql );
- $sth->execute( @params );
- my $row = $sth->fetchrow_hashref();
+ return unless $hold;
- return $row->{'reserve_id'};
+ return $hold->id();
}
=head2 ReserveSlip
my ($branch, $borrowernumber, $biblionumber) = @_;
# return unless ( C4::Context->boolean_preference('printreserveslips') );
+ my $patron = Koha::Patrons->find( $borrowernumber );
my $reserve_id = GetReserveId({
biblionumber => $biblionumber,
borrowernumber => $borrowernumber
}) or return;
- my $reserve = GetReserveInfo($reserve_id) or return;
+ my $reserve = Koha::Holds->find($reserve_id) or return;
+ $reserve = $reserve->unblessed;
return C4::Letters::GetPreparedLetter (
module => 'circulation',
letter_code => 'HOLD_SLIP',
branchcode => $branch,
+ lang => $patron->lang,
tables => {
'reserves' => $reserve,
'branches' => $reserve->{branchcode},
return $found;
}
+=head2 GetMaxPatronHoldsForRecord
+
+my $holds_per_record = ReservesControlBranch( $borrowernumber, $biblionumber );
+
+For multiple holds on a given record for a given patron, the max
+number of record level holds that a patron can be placed is the highest
+value of the holds_per_record rule for each item if the record for that
+patron. This subroutine finds and returns the highest holds_per_record
+rule value for a given patron id and record id.
+
+=cut
+
+sub GetMaxPatronHoldsForRecord {
+ my ( $borrowernumber, $biblionumber ) = @_;
+
+ my $patron = Koha::Patrons->find($borrowernumber);
+ my @items = Koha::Items->search( { biblionumber => $biblionumber } );
+
+ my $controlbranch = C4::Context->preference('ReservesControlBranch');
+
+ my $categorycode = $patron->categorycode;
+ my $branchcode;
+ $branchcode = $patron->branchcode if ( $controlbranch eq "PatronLibrary" );
+
+ my $max = 0;
+ foreach my $item (@items) {
+ my $itemtype = $item->effective_itemtype();
+
+ $branchcode = $item->homebranch if ( $controlbranch eq "ItemHomeLibrary" );
+
+ my $rule = GetHoldRule( $categorycode, $itemtype, $branchcode );
+ my $holds_per_record = $rule ? $rule->{holds_per_record} : 0;
+ $max = $holds_per_record if $holds_per_record > $max;
+ }
+
+ return $max;
+}
+
+=head2 GetHoldRule
+
+my $rule = GetHoldRule( $categorycode, $itemtype, $branchcode );
+
+Returns the matching hold related issuingrule fields for a given
+patron category, itemtype, and library.
+
+=cut
+
+sub GetHoldRule {
+ my ( $categorycode, $itemtype, $branchcode ) = @_;
+
+ my $dbh = C4::Context->dbh;
+
+ my $sth = $dbh->prepare(
+ q{
+ SELECT categorycode, itemtype, branchcode, reservesallowed, holds_per_record
+ FROM issuingrules
+ WHERE (categorycode in (?,'*') )
+ AND (itemtype IN (?,'*'))
+ AND (branchcode IN (?,'*'))
+ ORDER BY categorycode DESC,
+ itemtype DESC,
+ branchcode DESC
+ }
+ );
+
+ $sth->execute( $categorycode, $itemtype, $branchcode );
+
+ return $sth->fetchrow_hashref();
+}
+
=head1 AUTHOR
Koha Development Team <http://koha-community.org/>