Bug 23116: AllowHoldPolicyOverride allows a librarian to almost place a hold on an...
[koha-equinox.git] / reserve / placerequest.pl
index ee916ea..89c591d 100755 (executable)
@@ -21,8 +21,7 @@
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 use C4::Biblio;
@@ -33,6 +32,9 @@ use C4::Circulation;
 use C4::Members;
 use C4::Auth qw/checkauth/;
 
+use Koha::Items;
+use Koha::Patrons;
+
 my $input = CGI->new();
 
 checkauth($input, 0, { reserveforothers => 'place_holds' }, 'intranet');
@@ -51,7 +53,8 @@ my $checkitem      = $input->param('checkitem');
 my $expirationdate = $input->param('expiration_date');
 my $itemtype       = $input->param('itemtype') || undef;
 
-my $borrower = GetMember( 'borrowernumber' => $borrowernumber );
+my $borrower = Koha::Patrons->find( $borrowernumber );
+$borrower = $borrower->unblessed if $borrower;
 
 my $multi_hold = $input->param('multi_hold');
 my $biblionumbers = $multi_hold ? $input->param('biblionumbers') : ($biblionumber . '/');
@@ -69,18 +72,6 @@ foreach my $bibnum (@biblionumbers) {
 
 my $found;
 
-# if we have an item selectionned, and the pickup branch is the same as the holdingbranch
-# of the document, we force the value $rank and $found .
-if (defined $checkitem && $checkitem ne ''){
-    $holds_to_place_count = 1;
-    $rank[0] = '0' unless C4::Context->preference('ReservesNeedReturns');
-    my $item = $checkitem;
-    $item = GetItem($item);
-    if ( $item->{'holdingbranch'} eq $branch ){
-        $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
-    }
-}
-
 if ( $type eq 'str8' && $borrower ) {
 
     foreach my $biblionumber ( keys %bibinfos ) {
@@ -97,23 +88,37 @@ if ( $type eq 'str8' && $borrower ) {
             }
         }
 
+        my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
         if ( defined $checkitem && $checkitem ne '' ) {
-            my $item = GetItem($checkitem);
-            if ( $item->{'biblionumber'} ne $biblionumber ) {
-                $biblionumber = $item->{'biblionumber'};
+
+            my $item = Koha::Items->find($checkitem);
+
+            if ( $item->biblionumber ne $biblionumber ) {
+                $biblionumber = $item->biblionumber;
             }
-        }
 
-        if ($multi_hold) {
+            my $can_item_be_reserved = CanItemBeReserved($borrower->{'borrowernumber'}, $item->itemnumber, $branch)->{status};
+
+            if ( $can_item_be_reserved eq 'OK' || ( $can_item_be_reserved ne 'itemAlreadyOnHold' && $can_override ) ) {
+                AddReserve( $branch, $borrower->{'borrowernumber'},
+                    $biblionumber, \@realbi, $rank[0], $startdate, $expirationdate, $notes, $title,
+                    $checkitem, $found, $itemtype );
+            }
+
+        } elsif ($multi_hold) {
             my $bibinfo = $bibinfos{$biblionumber};
-            AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,[$biblionumber],
-                       $bibinfo->{rank},$startdate,$expirationdate,$notes,$bibinfo->{title},$checkitem,$found);
+            if ( $can_override || CanBookBeReserved($borrower->{'borrowernumber'}, $biblionumber)->{status} eq 'OK' ) {
+                AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,[$biblionumber],
+                           $bibinfo->{rank},$startdate,$expirationdate,$notes,$bibinfo->{title},$checkitem,$found);
+            }
         } else {
             # place a request on 1st available
             for ( my $i = 0 ; $i < $holds_to_place_count ; $i++ ) {
-                AddReserve( $branch, $borrower->{'borrowernumber'},
-                    $biblionumber, \@realbi, $rank[0], $startdate, $expirationdate, $notes, $title,
-                    $checkitem, $found, $itemtype );
+                if ( $can_override || CanBookBeReserved($borrower->{'borrowernumber'}, $biblionumber)->{status} eq 'OK' ) {
+                    AddReserve( $branch, $borrower->{'borrowernumber'},
+                        $biblionumber, \@realbi, $rank[0], $startdate, $expirationdate, $notes, $title,
+                        $checkitem, $found, $itemtype );
+                }
             }
         }
     }
@@ -128,11 +133,11 @@ if ( $type eq 'str8' && $borrower ) {
         print $input->redirect("request.pl?biblionumber=$biblionumber");
     }
 }
-elsif ( $borrower eq '' ) {
+elsif ( $borrowernumber eq '' ) {
     print $input->header();
     print "Invalid borrower number please try again";
 
     # Not sure that Dump() does HTML escaping. Use firebug or something to trace
     # instead.
-    #  print $input->Dump;
+    #print $input->Dump;
 }