Bug 22456: (follow-up) Cancel holds at checkin
[koha-ffzg.git] / circ / pendingreserves.pl
index 9c73023..a438557 100755 (executable)
@@ -25,13 +25,13 @@ use YAML::XS;
 use Encode;
 
 use C4::Context;
-use C4::Output;
+use C4::Output qw( output_html_with_http_headers );
 use CGI qw ( -utf8 );
-use C4::Auth;
-use C4::Items qw( ModItemTransfer );
+use C4::Auth qw( get_template_and_user );
+use C4::Items;
 use C4::Reserves qw( ModReserveCancelAll );
 use Koha::Biblios;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string );
 use Koha::Holds;
 use DateTime::Duration;
 
@@ -84,13 +84,13 @@ if ( $op eq 'cancel_reserve' and $reserve_id ) {
                 },
             );
             if ( $letter ) {
-                my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
+                my $from_address = $library->from_email_address;
 
                 C4::Letters::EnqueueLetter(
                     {   letter                 => $letter,
                         borrowernumber         => $patron->borrowernumber,
                         message_transport_type => 'email',
-                        from_address           => $admin_email_address,
+                        from_address           => $from_address,
                     }
                 );
                 unless ( $patron->notice_email_address ) {
@@ -155,13 +155,13 @@ unless ( $enddate ) {
 
 # building query parameters
 my %where = (
-    'reserve.found' => undef,
-    'reserve.priority' => 1,
-    'reserve.suspend' => 0,
+    'me.found' => undef,
+    'me.priority' => { '!=' => 0 },
+    'me.suspend' => 0,
     'itembib.itemlost' => 0,
     'itembib.withdrawn' => 0,
     'itembib.notforloan' => 0,
-    'itembib.itemnumber' => { -not_in => \'SELECT itemnumber FROM branchtransfers WHERE datearrived IS NULL' }
+    'itembib.itemnumber' => { -not_in => \'SELECT itemnumber FROM branchtransfers WHERE datearrived IS NULL AND datecancelled IS NULL' }
 );
 
 # date boundaries
@@ -169,11 +169,11 @@ my $dtf = Koha::Database->new->schema->storage->datetime_parser;
 my $startdate_iso = $dtf->format_date($startdate);
 my $enddate_iso   = $dtf->format_date($enddate);
 if ( $startdate_iso && $enddate_iso ){
-    $where{'reserve.reservedate'} = [ -and => { '>=', $startdate_iso }, { '<=', $enddate_iso } ];
+    $where{'me.reservedate'} = [ -and => { '>=', $startdate_iso }, { '<=', $enddate_iso } ];
 } elsif ( $startdate_iso ){
-    $where{'reserve.reservedate'} = { '>=', $startdate_iso };
+    $where{'me.reservedate'} = { '>=', $startdate_iso };
 } elsif ( $enddate_iso ){
-    $where{'reserve.reservedate'} = { '<=', $enddate_iso };
+    $where{'me.reservedate'} = { '<=', $enddate_iso };
 }
 
 # Bug 21320
@@ -188,19 +188,22 @@ if ( C4::Context->preference('IndependentBranches') ){
 # get all distinct unfulfilled reserves
 my $holds = Koha::Holds->search(
     { %where },
-    { join => 'itembib', alias => 'reserve', distinct  => 1, columns => qw[me.biblionumber] }
+    { join => 'itembib', distinct => 1, columns => qw[me.biblionumber] }
 );
+
 my @biblionumbers = $holds->get_column('biblionumber');
 
 my $all_items;
-foreach my $item ( $holds->get_items_that_can_fill ) {
-    push @{$all_items->{$item->biblionumber}}, $item;
+if ( $holds->count ) {
+    foreach my $item ( $holds->get_items_that_can_fill->as_list ) {
+        push @{ $all_items->{ $item->biblionumber } }, $item;
+    }
 }
 
 # patrons count per biblio
 my $patrons_count = {
     map { $_->{biblionumber} => $_->{patrons_count} } @{ Koha::Holds->search(
-            {},
+            { 'suspend' => 0 },
             {
                 select   => [ 'biblionumber', { count => { distinct => 'borrowernumber' } } ],
                 as       => [qw( biblionumber patrons_count )],
@@ -215,8 +218,8 @@ my $holds_biblios_map = {
             {%where},
             {
                 join    => ['itembib', 'biblio'],
-                alias   => 'reserve',
-                select  => ['reserve.biblionumber', 'reserve.reserve_id'],
+                select  => ['me.biblionumber', 'me.reserve_id'],
+                order_by => { -desc => 'priority' }
             }
         )->unblessed
     }
@@ -227,7 +230,6 @@ my $all_holds = {
             { reserve_id => [ values %$holds_biblios_map ]},
             {
                 prefetch => [ 'borrowernumber', 'itembib', 'biblio' ],
-                alias    => 'reserve',
             }
         )->as_list
     }
@@ -235,7 +237,11 @@ my $all_holds = {
 
 # make final holds_info array and fill with info
 my @holds_info;
+my $seen = {};
 foreach my $bibnum ( @biblionumbers ){
+    # Skip this record if it's already been handled
+    next if $seen->{$bibnum};
+    $seen->{$bibnum} = 1;
 
     my $hold_info;
     my $items = $all_items->{$bibnum};
@@ -300,7 +306,6 @@ $template->param(
     from                => $startdate,
     to                  => $enddate,
     holds_info          => \@holds_info,
-    "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
     HoldsToPullStartDate => C4::Context->preference('HoldsToPullStartDate') || PULL_INTERVAL,
     HoldsToPullEndDate  => C4::Context->preference('ConfirmFutureHolds') || 0,
     messages            => \@messages,