+=head2 GetReservesControlBranch
+
+ my $reserves_control_branch = GetReservesControlBranch($item, $borrower);
+
+ Return the branchcode to be used to determine which reserves
+ policy applies to a transaction.
+
+ C<$item> is a hashref for an item. Only 'homebranch' is used.
+
+ C<$borrower> is a hashref to borrower. Only 'branchcode' is used.
+
+=cut
+
+sub GetReservesControlBranch {
+ my ( $item, $borrower ) = @_;
+
+ my $reserves_control = C4::Context->preference('ReservesControlBranch');
+
+ my $branchcode =
+ ( $reserves_control eq 'ItemHomeLibrary' ) ? $item->{'homebranch'}
+ : ( $reserves_control eq 'PatronLibrary' ) ? $borrower->{'branchcode'}
+ : undef;
+
+ return $branchcode;
+}
+
+=head2 CalculatePriority
+
+ my $p = CalculatePriority($biblionumber, $resdate);
+
+Calculate priority for a new reserve on biblionumber, placing it at
+the end of the line of all holds whose start date falls before
+the current system time and that are neither on the hold shelf
+or in transit.
+
+The reserve date parameter is optional; if it is supplied, the
+priority is based on the set of holds whose start date falls before
+the parameter value.
+
+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 = q{
+ 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;
+}
+