Bug 32923: (follow-up) Fix x-koha-embed from EHoldings/*/*
[koha-ffzg.git] / circ / pendingreserves.pl
index 1f85355..21016bc 100755 (executable)
@@ -161,7 +161,7 @@ my %where = (
     '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
@@ -181,8 +181,8 @@ if ( !C4::Context->preference('AllowHoldsOnDamagedItems') ){
     $where{'itembib.damaged'} = 0;
 }
 
-if ( C4::Context->preference('IndependentBranches') ){
-    $where{'itembib.holdingbranch'} = C4::Context->userenv->{'branch'};
+if ( C4::Context->only_my_library() ){
+    $where{'me.branchcode'} = C4::Context->userenv->{'branch'};
 }
 
 # get all distinct unfulfilled reserves
@@ -190,6 +190,7 @@ my $holds = Koha::Holds->search(
     { %where },
     { join => 'itembib', distinct => 1, columns => qw[me.biblionumber] }
 );
+
 my @biblionumbers = $holds->get_column('biblionumber');
 
 my $all_items;
@@ -218,6 +219,7 @@ my $holds_biblios_map = {
             {
                 join    => ['itembib', 'biblio'],
                 select  => ['me.biblionumber', 'me.reserve_id'],
+                order_by => { -desc => 'priority' }
             }
         )->unblessed
     }
@@ -227,7 +229,7 @@ my $all_holds = {
     map { $_->biblionumber => $_ } @{ Koha::Holds->search(
             { reserve_id => [ values %$holds_biblios_map ]},
             {
-                prefetch => [ 'borrowernumber', 'itembib', 'biblio' ],
+                prefetch => [ 'borrowernumber', 'itembib', 'biblio', 'item_group' ],
             }
         )->as_list
     }
@@ -263,8 +265,11 @@ foreach my $bibnum ( @biblionumbers ){
     }
     $hold_info->{itemtypes} = \@res_itemtypes;
 
+    my $res_info = $all_holds->{$bibnum};
+
     # get available values for each biblio
     my $fields = {
+        collections     => 'ccode',
         locations       => 'location',
         callnumbers     => 'itemcallnumber',
         enumchrons      => 'enumchron',
@@ -280,6 +285,10 @@ foreach my $bibnum ( @biblionumbers ){
           [ uniq map { defined $_->$field ? $_->$field : () } @$items ];
     }
 
+    if ( $res_info->item_group ) {
+       $hold_info->{barcodes} = [ uniq map { defined $_->barcode && $_->item_group && ( $_->item_group->id == $res_info->item_group_id )  ? $_->barcode : () } @$items ];
+    }
+
     # items available
     $hold_info->{items_count} = $items_count;
 
@@ -290,11 +299,11 @@ foreach my $bibnum ( @biblionumbers ){
     $hold_info->{pull_count} = $pull_count;
 
     # get other relevant information
-    my $res_info = $all_holds->{$bibnum};
     $hold_info->{patron} = $res_info->patron;
     $hold_info->{item}   = $res_info->item;
     $hold_info->{biblio} = $res_info->biblio;
     $hold_info->{hold}   = $res_info;
+    $hold_info->{item_group} = $res_info->item_group;
 
     push @holds_info, $hold_info;
 }