}
# Add the reserve
- # $branch, $borrowernumber, $biblionumber, $constraint, $bibitems, $priority, $notes, $title, $checkitem, $found
- AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, 0, undef, $title, undef, undef );
+ # $branch, $borrowernumber, $biblionumber,
+ # $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
+ # $title, $checkitem, $found
+ my $priority= C4::Reserves::CalculatePriority( $biblionumber );
+ AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $priority, undef, undef, undef, $title, undef, undef );
# Hashref building
my $out;
my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
return { code => 'NotHoldable' } unless $canbookbereserved and $canitembereserved;
- my $branch;
-
# Pickup branch management
+ my $branch;
if ( $cgi->param('pickup_location') ) {
$branch = $cgi->param('pickup_location');
my $branches = GetBranches();
$branch = $$borrower{branchcode};
}
- my $rank;
- my $found;
-
- # Get rank and found
- $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
- if ( $item->{'holdingbranch'} eq $branch ) {
- $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
- }
-
# Add the reserve
- # $branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found
- AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $rank, '', '', '', $title, $itemnumber, $found );
+ # $branch, $borrowernumber, $biblionumber,
+ # $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
+ # $title, $checkitem, $found
+ my $priority= C4::Reserves::CalculatePriority( $biblionumber );
+ AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $priority, undef, undef, undef, $title, $itemnumber, undef );
# Hashref building
my $out;
return $branchcode;
}
+=head2 CalculatePriority
+
+ my $p = CalculatePriority($biblionumber, $resdate);
+
+Calculate priority for a new reserve on biblionumber.
+The reserve date parameter is optional. Plays a role if the preference
+AllowHoldDateInFuture is set.
+After calculation of this priority, it is recommended to call
+_ShiftPriorityByDateAndPriority. Note that this is currently done in
+AddReserves.
+
+=cut
+
+sub CalculatePriority {
+ my ( $biblionumber, $resdate ) = @_;
+
+ my $sql = qq{
+ SELECT COUNT(*) FROM reserves
+ WHERE biblionumber=? AND priority>0 AND
+ (found IS NULL or found='')
+ };
+ #skip found==W or found==T (waiting or transit holds)
+ if( $resdate ) {
+ $sql.= ' AND ( reservedate<=? )';
+ }
+ else {
+ $sql.= ' AND ( reservedate < NOW() )';
+ }
+ my $dbh = C4::Context->dbh();
+ my @row= $dbh->selectrow_array( $sql, undef, $resdate?
+ ($biblionumber, $resdate): ($biblionumber) );
+
+ return @row? $row[0]+1: 1;
+ #if @row does not contain anything, something went wrong..
+}
+
=head1 AUTHOR
Koha Development Team <http://koha-community.org/>