Bug 29609: Centralized code to build the link to a biblio detail page
[srvgit] / circ / pendingreserves.pl
index 69477bf..85bcffe 100755 (executable)
@@ -21,16 +21,17 @@ use Modern::Perl;
 
 use constant PULL_INTERVAL => 2;
 use List::MoreUtils qw( uniq );
+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::Debug;
-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;
 
@@ -48,7 +49,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         query           => $input,
         type            => "intranet",
         flagsrequired   => { circulate => "circulate_remaining_permissions" },
-        debug           => 1,
     }
 );
 
@@ -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 ) {
@@ -109,7 +109,7 @@ if ( $op eq 'cancel_reserve' and $reserve_id ) {
         if ( my $yaml = C4::Context->preference('UpdateItemWhenLostFromHoldList') ) {
             $yaml = "$yaml\n\n";  # YAML is anal on ending \n. Surplus does not hurt
             my $assignments;
-            eval { $assignments = YAML::Load($yaml); };
+            eval { $assignments = YAML::XS::Load(Encode::encode_utf8($yaml)); };
             if ($@) {
                 warn "Unable to parse UpdateItemWhenLostFromHoldList syspref : $@" if $@;
             }
@@ -155,8 +155,9 @@ unless ( $enddate ) {
 
 # building query parameters
 my %where = (
-    'reserve.found' => undef,
-    'reserve.suspend' => 0,
+    'me.found' => undef,
+    'me.priority' => 1,
+    'me.suspend' => 0,
     'itembib.itemlost' => 0,
     'itembib.withdrawn' => 0,
     'itembib.notforloan' => 0,
@@ -168,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
@@ -185,28 +186,17 @@ if ( C4::Context->preference('IndependentBranches') ){
 }
 
 # get all distinct unfulfilled reserves
-my @biblionumbers = Koha::Holds->search(
+my $holds = Koha::Holds->search(
     { %where },
-    { join => 'itembib', alias => 'reserve', distinct  => 1, columns => qw[me.biblionumber] }
-)->get_column('biblionumber');
-
-my @branchtransfers = map { $_->itemnumber } Koha::Item::Transfers->search({ datearrived => undef }, { columns => [ 'itemnumber' ], collapse => 1 });
-my @waiting_holds = map { $_->itemnumber } Koha::Holds->search({'found' => 'W'}, { columns => [ 'itemnumber' ], collapse => 1 });
-
-my @all_items = Koha::Items->search(
-    {
-        biblionumber => { in => \@biblionumbers },
-        itemlost     => 0,
-        withdrawn    => 0,
-        notforloan   => 0,
-        onloan       => undef,
-        itemnumber   => { -not_in => [ @branchtransfers, @waiting_holds ] },
-    }
+    { join => 'itembib', distinct  => 1, columns => qw[me.biblionumber] }
 );
+my @biblionumbers = $holds->get_column('biblionumber');
 
 my $all_items;
-foreach my $item ( @all_items ) {
-    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
@@ -222,12 +212,38 @@ my $patrons_count = {
     }
 };
 
+my $holds_biblios_map = {
+    map { $_->{biblionumber} => $_->{reserve_id} } @{ Koha::Holds->search(
+            {%where},
+            {
+                join    => ['itembib', 'biblio'],
+                select  => ['me.biblionumber', 'me.reserve_id'],
+            }
+        )->unblessed
+    }
+};
+
+my $all_holds = {
+    map { $_->biblionumber => $_ } @{ Koha::Holds->search(
+            { reserve_id => [ values %$holds_biblios_map ]},
+            {
+                prefetch => [ 'borrowernumber', 'itembib', 'biblio' ],
+            }
+        )->as_list
+    }
+};
+
 # make final holds_info array and fill with info
 my @holds_info;
 foreach my $bibnum ( @biblionumbers ){
 
     my $hold_info;
     my $items = $all_items->{$bibnum};
+    my $items_count = defined $items ? scalar @$items : 0;
+    my $pull_count = $items_count <= $patrons_count->{$bibnum} ? $items_count : $patrons_count->{$bibnum};
+    if ( $pull_count == 0 ) {
+        next;
+    }
 
     # get available item types for each biblio
     my @res_itemtypes;
@@ -243,45 +259,34 @@ foreach my $bibnum ( @biblionumbers ){
     }
     $hold_info->{itemtypes} = \@res_itemtypes;
 
-    # get available locations for each biblio
-    $hold_info->{locations} = [ uniq map { defined $_->location ? $_->location : () } @$items ];
-
-    # get available callnumbers for each biblio
-    $hold_info->{callnumbers} = [ uniq map { defined $_->itemcallnumber ? $_->itemcallnumber : () } @$items ];
-
-    # get available enumchrons for each biblio
-    $hold_info->{enumchrons} = [ uniq map { defined $_->enumchron ? $_->enumchron : () } @$items ];
-
-    # get available copynumbers for each biblio
-    $hold_info->{copynumbers} = [ uniq map { defined $_->copynumber ? $_->copynumber : () } @$items ];
-
-    # get available barcodes for each biblio
-    $hold_info->{barcodes} = [ uniq map { defined $_->barcode ? $_->barcode : () } @$items ];
-
-    # get available holding branches for each biblio
-    $hold_info->{holdingbranches} = [ uniq map { defined $_->holdingbranch ? $_->holdingbranch : () } @$items ];
+    # get available values for each biblio
+    my $fields = {
+        locations       => 'location',
+        callnumbers     => 'itemcallnumber',
+        enumchrons      => 'enumchron',
+        copynumbers     => 'copynumber',
+        barcodes        => 'barcode',
+        holdingbranches => 'holdingbranch'
+    };
+
+    while (
+        my ( $key, $field ) = each %$fields )
+    {
+        $hold_info->{$key} =
+          [ uniq map { defined $_->$field ? $_->$field : () } @$items ];
+    }
 
     # items available
-    my $items_count = scalar @$items;
     $hold_info->{items_count} = $items_count;
 
     # patrons with holds
     $hold_info->{patrons_count} = $patrons_count->{$bibnum};
 
-    my $pull_count = $items_count <= $patrons_count->{$bibnum} ? $items_count : $patrons_count->{$bibnum};
-    if ( $pull_count == 0 ) {
-        next;
-    }
+    # number of items to pull
     $hold_info->{pull_count} = $pull_count;
 
     # get other relevant information
-    my $res_info = Koha::Holds->search(
-        { 'reserve.biblionumber' => $bibnum, %where },
-        { prefetch => [ 'borrowernumber', 'itembib', 'biblio' ],
-          order_by => 'priority',
-          alias => 'reserve'
-        }
-    )->next; # get first item in results
+    my $res_info = $all_holds->{$bibnum};
     $hold_info->{patron} = $res_info->patron;
     $hold_info->{item}   = $res_info->item;
     $hold_info->{biblio} = $res_info->biblio;
@@ -295,7 +300,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,