Bug 32426: Changes for opac-registration-verify
[koha-ffzg.git] / reserve / modrequest.pl
index 7e2c189..3e9e563 100755 (executable)
 
 use Modern::Perl;
 use CGI qw ( -utf8 );
+use URI;
+use List::MoreUtils qw( uniq );
+use Try::Tiny;
+
 use C4::Output;
-use C4::Reserves;
-use C4::Auth;
+use C4::Reserves qw( ModReserve ModReserveCancelAll );
+use C4::Auth qw( checkauth );
+use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue;
 
-my $query = new CGI;
-my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
-    {   
-        template_name   => "about.tt",
-        query           => $query,
-        type            => "intranet",
-        authnotrequired => 0,
-        flagsrequired   => { catalogue => 1 },
-        debug           => 1,
-    }
-);
+my $query = CGI->new;
+
+checkauth($query, 0, { reserveforothers => '*' }, 'intranet');
 
 my @reserve_id = $query->multi_param('reserve_id');
 my @rank = $query->multi_param('rank-request');
-my @biblionumber = $query->multi_param('biblionumber');
 my @borrower = $query->multi_param('borrowernumber');
 my @reservedates = $query->multi_param('reservedate');
 my @expirationdates = $query->multi_param('expirationdate');
 my @branch = $query->multi_param('pickup');
 my @itemnumber = $query->multi_param('itemnumber');
-my @suspend_until=$query->multi_param('suspend_until');
-my $multi_hold = $query->param('multi_hold');
-my $biblionumbers = $query->param('biblionumbers');
+my @biblionumber = $query->multi_param('biblionumber');
 my $count=@rank;
 
+@biblionumber = uniq @biblionumber;
+
 my $CancelBiblioNumber = $query->param('CancelBiblioNumber');
 my $CancelBorrowerNumber = $query->param('CancelBorrowerNumber');
 my $CancelItemnumber = $query->param('CancelItemnumber');
@@ -69,16 +65,37 @@ if ($CancelBorrowerNumber) {
 else {
     for (my $i=0;$i<$count;$i++){
         undef $itemnumber[$i] if !$itemnumber[$i];
-        ModReserve({
+        my $suspend_until = $query->param( "suspend_until_" . $reserve_id[$i] );
+        my $cancellation_reason = $query->param("cancellation-reason");
+        my $params = {
             rank => $rank[$i],
             reserve_id => $reserve_id[$i],
-            reservedate => $reservedates[$i],
-            expirationdate => $expirationdates[$i],
+            expirationdate => $expirationdates[$i] || undef,
             branchcode => $branch[$i],
             itemnumber => $itemnumber[$i],
-            suspend_until => $suspend_until[$i]
-        });
+            defined $suspend_until ? ( suspend_until => $suspend_until ) : (),
+            cancellation_reason => $cancellation_reason,
+        };
+        if (C4::Context->preference('AllowHoldDateInFuture')) {
+            $params->{reservedate} = $reservedates[$i] || undef;
+        }
+
+        try {
+            ModReserve($params);
+        } catch {
+            if ($_->isa('Koha::Exceptions::ObjectNotFound')){
+                warn $_;
+            } else {
+                $_->rethrow;
+            }
+        }
     }
+    my @biblio_ids = uniq @biblionumber;
+    Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
+        {
+            biblio_ids => \@biblio_ids
+        }
+    );
 }
 
 my $from=$query->param('from');
@@ -88,11 +105,7 @@ if ( $from eq 'borrower'){
 } elsif ( $from eq 'circ'){
     print $query->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrower[0]");
 } else {
-     my $url = "/cgi-bin/koha/reserve/request.pl?";
-     if ($multi_hold) {
-         $url .= "multi_hold=1&biblionumbers=$biblionumbers";
-     } else {
-         $url .= "biblionumber=$biblionumber[0]";
-     }
+     my $url = URI->new("/cgi-bin/koha/reserve/request.pl");
+     $url->query_form( biblionumber => [@biblionumber]);
      print $query->redirect($url);
 }