This patch add a function in C4::Acquisition that return the number of items in 
order for a biblionumber.
Then, It permit to show to the user, the number of items in order for "empty" 
records in search results.
---
 C4/Acquisition.pm |   22 +++++-
 C4/Search.pm      |  261 +++++++++++++++++++++++++++--------------------------
 2 files changed, 155 insertions(+), 128 deletions(-)

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index f334edc..0a92d39 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -39,7 +39,7 @@ BEGIN {
                &GetOrderNumber &GetLateOrders &NewOrder &DelOrder
                &SearchOrder &GetHistory &GetRecentAcqui
                &ModOrder &ModReceiveOrder &ModOrderBiblioNumber
-               &GetParcels &GetParcel
+               &GetParcels &GetParcel &GetOrderItemCountFromBiblionumber
        );
 }
 
@@ -1217,6 +1217,26 @@ sub GetRecentAcqui {
     return \...@results;
 }
 
+=head2 GetOrderItemCountFromBiblionumber
+
+    $count = GetOrderItemCountFromBiblionumber($biblionumber)
+
+    C<$result> is the count of items in order for a biblionumber
+    
+=cut
+sub GetOrderItemCountFromBiblionumber{
+    my $biblionumber = shift;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("SELECT quantity FROM aqorders WHERE datereceived 
IS NULL AND biblionumber = ? ");
+    $sth->execute($biblionumber);
+    if(my $data = $sth->fetchrow_hashref){
+        return $data->{quantity};
+    }else{
+        return 0;
+    }
+}
+
+
 1;
 __END__
 
diff --git a/C4/Search.pm b/C4/Search.pm
index 21eb767..da475a5 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -19,6 +19,7 @@ use strict;
 # use warnings; # FIXME
 require Exporter;
 use C4::Context;
+use C4::Acquisition qw/GetOrderItemCountFromBiblionumber/;
 use C4::Biblio;    # GetMarcFromKohaField
 use C4::Koha;      # getFacets
 use C4::Search::PazPar2;
@@ -1251,12 +1252,14 @@ sub searchResults {
     # loop through all of the records we've retrieved
     for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) {
         my $marcrecord = MARC::File::USMARC::decode( $marcresults[$i] );
+        my $biblionumber;
         
         if ($bibliotag<10){
-            $fw = GetFrameworkCode($marcrecord->field($bibliotag)->data);
+            $biblionumber = $marcrecord->field($bibliotag)->data;
         }else{
-            $fw = 
GetFrameworkCode($marcrecord->subfield($bibliotag,$bibliosubf));
+            $biblionumber = $marcrecord->subfield($bibliotag,$bibliosubf);
         }
+        $fw = GetFrameworkCode($biblionumber);
         
         my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, $fw );
         $oldbiblio->{subtitle} = GetRecordValue('subtitle', $marcrecord, $fw);
@@ -1395,138 +1398,142 @@ sub searchResults {
           ? C4::Context->preference('maxItemsinSearchResults') - 1
           : 1;
 
-        # loop through every item
-        foreach my $field (@fields) {
-            my $item;
-
-            # populate the items hash
-            foreach my $code ( keys %subfieldstosearch ) {
-                $item->{$code} = $field->subfield( $subfieldstosearch{$code} );
-            }
-                       my $hbranch     = 
C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 'homebranch'   
 : 'holdingbranch';
-                       my $otherbranch = 
C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 
'holdingbranch' : 'homebranch';
-            # set item's branch name, use HomeOrHoldingBranch syspref first, 
fall back to the other one
-            if ($item->{$hbranch}) {
-                $item->{'branchname'} = $branches{$item->{$hbranch}};
-            }
-            elsif ($item->{$otherbranch}) {    # Last resort
-                $item->{'branchname'} = $branches{$item->{$otherbranch}}; 
-            }
-            
-            ($item->{'reserved'}) = 
C4::Reserves::CheckReserves($item->{itemnumber});
-            
-                       my $prefix = $item->{$hbranch} . '--' . 
$item->{location} . $item->{itype} . $item->{itemcallnumber};
-# For each grouping of items (onloan, available, unavailable), we build a key 
to store relevant info about that item
-            if ( $item->{onloan} or $item->{reserved} ) {
-                $onloan_count++;
-                               my $key = $prefix . $item->{onloan} . 
$item->{barcode};
-                               $onloan_items->{$key}->{due_date} = 
format_date($item->{onloan});
-                               $onloan_items->{$key}->{count}++ if 
$item->{$hbranch};
-                               $onloan_items->{$key}->{branchname} = 
$item->{branchname};
-                               $onloan_items->{$key}->{location} = 
$shelflocations->{ $item->{location} };
-                               $onloan_items->{$key}->{itemcallnumber} = 
$item->{itemcallnumber};
-                               $onloan_items->{$key}->{imageurl} = 
getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
-                               $onloan_items->{$key}->{barcode} = 
$item->{barcode};
-                # if something's checked out and lost, mark it as 'long 
overdue'
-                if ( $item->{itemlost} ) {
-                    $onloan_items->{$prefix}->{longoverdue}++;
-                    $longoverdue_count++;
-                } else {       # can place holds as long as item isn't lost
-                    $can_place_holds = 1;
+        if(not $items_count){
+            $ordered_count = GetOrderItemCountFromBiblionumber($biblionumber);
+        }else{
+            # loop through every item
+            foreach my $field (@fields) {
+                my $item;
+    
+                # populate the items hash
+                foreach my $code ( keys %subfieldstosearch ) {
+                    $item->{$code} = $field->subfield( 
$subfieldstosearch{$code} );
                 }
-            }
-
-         # items not on loan, but still unavailable ( lost, withdrawn, damaged 
)
-            else {
-
-                # item is on order
-                if ( $item->{notforloan} == -1 ) {
-                    $ordered_count++;
+                       my $hbranch     = 
C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 'homebranch'   
 : 'holdingbranch';
+                       my $otherbranch = 
C4::Context->preference('HomeOrHoldingBranch') eq 'homebranch' ? 
'holdingbranch' : 'homebranch';
+                # set item's branch name, use HomeOrHoldingBranch syspref 
first, fall back to the other one
+                if ($item->{$hbranch}) {
+                    $item->{'branchname'} = $branches{$item->{$hbranch}};
                 }
-
-                # is item in transit?
-                my $transfertwhen = '';
-                my ($transfertfrom, $transfertto);
-                
-                unless ($item->{wthdrawn}
-                        || $item->{itemlost}
-                        || $item->{damaged}
-                        || $item->{notforloan}
-                        || $items_count > 20) {
-
-                    # A couple heuristics to limit how many times
-                    # we query the database for item transfer information, 
sacrificing
-                    # accuracy in some cases for speed;
-                    #
-                    # 1. don't query if item has one of the other statuses
-                    # 2. don't check transit status if the bib has
-                    #    more than 20 items
-                    #
-                    # FIXME: to avoid having the query the database like this, 
and to make
-                    #        the in transit status count as unavailable for 
search limiting,
-                    #        should map transit status to record indexed in 
Zebra.
-                    #
-                    ($transfertwhen, $transfertfrom, $transfertto) = 
C4::Circulation::GetTransfers($item->{itemnumber});
+                elsif ($item->{$otherbranch}) {        # Last resort
+                    $item->{'branchname'} = $branches{$item->{$otherbranch}}; 
                 }
-
-                # item is withdrawn, lost or damaged
-                if (   $item->{wthdrawn}
-                    || $item->{itemlost}
-                    || $item->{damaged}
-                    || $item->{notforloan} 
-                    || $item->{reserved}
-                    || ($transfertwhen ne ''))
-                {
-                    $wthdrawn_count++        if $item->{wthdrawn};
-                    $itemlost_count++        if $item->{itemlost};
-                    $itemdamaged_count++     if $item->{damaged};
-                    $item_in_transit_count++ if $transfertwhen ne '';
-                    $item->{status} = $item->{wthdrawn} . "-" . 
$item->{itemlost} . "-" . $item->{damaged} . "-" . $item->{notforloan};
-
-                                       my $key = $prefix . $item->{status};
-                                       
-                                       foreach (qw(wthdrawn itemlost damaged 
branchname itemcallnumber)) {
-                                           if($item->{notforloan} == 1){
-                                               $notforloan_items->{$key}->{$_} 
= $item->{$_};
-                                           }else{
-                          $other_items->{$key}->{$_} = $item->{$_};
-                                           }
-                                       }
-
-                                       if($item->{notforloan} == 1){
-                        $notforloan_count++;
-
-                        $notforloan_items->{$key}->{intransit} = 
($transfertwhen ne '') ? 1 : 0;
-                                       $notforloan_items->{$key}->{notforloan} 
= 
GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value)
 if $notforloan_authorised_value;
-                                       $notforloan_items->{$key}->{count}++ if 
$item->{$hbranch};
-                                       $notforloan_items->{$key}->{location} = 
$shelflocations->{ $item->{location} };
-                                       $notforloan_items->{$key}->{imageurl} = 
getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
-                                       $notforloan_items->{$key}->{barcode} = 
$item->{barcode};
-                    }else{
-                        $other_count++;
-                                       
-                        $other_items->{$key}->{intransit} = ($transfertwhen ne 
'') ? 1 : 0;
-                                       $other_items->{$key}->{notforloan} = 
GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value)
 if $notforloan_authorised_value;
-                                       $other_items->{$key}->{count}++ if 
$item->{$hbranch};
-                                       $other_items->{$key}->{location} = 
$shelflocations->{ $item->{location} };
-                                       $other_items->{$key}->{imageurl} = 
getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
-                                       $other_items->{$key}->{barcode} = 
$item->{barcode};
+                
+                ($item->{'reserved'}) = 
C4::Reserves::CheckReserves($item->{itemnumber});
+                
+                       my $prefix = $item->{$hbranch} . '--' . 
$item->{location} . $item->{itype} . $item->{itemcallnumber};
+    # For each grouping of items (onloan, available, unavailable), we build a 
key to store relevant info about that item
+                if ( $item->{onloan} or $item->{reserved} ) {
+                    $onloan_count++;
+                               my $key = $prefix . $item->{onloan} . 
$item->{barcode};
+                               $onloan_items->{$key}->{due_date} = 
format_date($item->{onloan});
+                               $onloan_items->{$key}->{count}++ if 
$item->{$hbranch};
+                               $onloan_items->{$key}->{branchname} = 
$item->{branchname};
+                               $onloan_items->{$key}->{location} = 
$shelflocations->{ $item->{location} };
+                               $onloan_items->{$key}->{itemcallnumber} = 
$item->{itemcallnumber};
+                               $onloan_items->{$key}->{imageurl} = 
getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
+                               $onloan_items->{$key}->{barcode} = 
$item->{barcode};
+                    # if something's checked out and lost, mark it as 'long 
overdue'
+                    if ( $item->{itemlost} ) {
+                        $onloan_items->{$prefix}->{longoverdue}++;
+                        $longoverdue_count++;
+                    } else {   # can place holds as long as item isn't lost
+                        $can_place_holds = 1;
                     }
-
                 }
-                # item is available
+    
+             # items not on loan, but still unavailable ( lost, withdrawn, 
damaged )
                 else {
-                    $can_place_holds = 1;
-                    $available_count++;
-                                       $available_items->{$prefix}->{count}++ 
if $item->{$hbranch};
-                                       foreach (qw(branchname itemcallnumber 
barcode)) {
-                       $available_items->{$prefix}->{$_} = $item->{$_};
-                                       }
-                                       $available_items->{$prefix}->{location} 
= $shelflocations->{ $item->{location} };
-                                       $available_items->{$prefix}->{imageurl} 
= getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
+    
+                    # item is on order
+                    if ( $item->{notforloan} == -1 ) {
+                        $ordered_count++;
+                    }
+    
+                    # is item in transit?
+                    my $transfertwhen = '';
+                    my ($transfertfrom, $transfertto);
+                    
+                    unless ($item->{wthdrawn}
+                            || $item->{itemlost}
+                            || $item->{damaged}
+                            || $item->{notforloan}
+                            || $items_count > 20) {
+    
+                        # A couple heuristics to limit how many times
+                        # we query the database for item transfer information, 
sacrificing
+                        # accuracy in some cases for speed;
+                        #
+                        # 1. don't query if item has one of the other statuses
+                        # 2. don't check transit status if the bib has
+                        #    more than 20 items
+                        #
+                        # FIXME: to avoid having the query the database like 
this, and to make
+                        #        the in transit status count as unavailable 
for search limiting,
+                        #        should map transit status to record indexed 
in Zebra.
+                        #
+                        ($transfertwhen, $transfertfrom, $transfertto) = 
C4::Circulation::GetTransfers($item->{itemnumber});
+                    }
+    
+                    # item is withdrawn, lost or damaged
+                    if (   $item->{wthdrawn}
+                        || $item->{itemlost}
+                        || $item->{damaged}
+                        || $item->{notforloan} 
+                        || $item->{reserved}
+                        || ($transfertwhen ne ''))
+                    {
+                        $wthdrawn_count++        if $item->{wthdrawn};
+                        $itemlost_count++        if $item->{itemlost};
+                        $itemdamaged_count++     if $item->{damaged};
+                        $item_in_transit_count++ if $transfertwhen ne '';
+                        $item->{status} = $item->{wthdrawn} . "-" . 
$item->{itemlost} . "-" . $item->{damaged} . "-" . $item->{notforloan};
+    
+                                       my $key = $prefix . $item->{status};
+                                       
+                                       foreach (qw(wthdrawn itemlost damaged 
branchname itemcallnumber)) {
+                                           if($item->{notforloan} == 1){
+                                               $notforloan_items->{$key}->{$_} 
= $item->{$_};
+                                           }else{
+                                  $other_items->{$key}->{$_} = $item->{$_};
+                                           }
+                                       }
+    
+                                       if($item->{notforloan} == 1){
+                            $notforloan_count++;
+    
+                            $notforloan_items->{$key}->{intransit} = 
($transfertwhen ne '') ? 1 : 0;
+                                               
$notforloan_items->{$key}->{notforloan} = 
GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value)
 if $notforloan_authorised_value;
+                                               
$notforloan_items->{$key}->{count}++ if $item->{$hbranch};
+                                               
$notforloan_items->{$key}->{location} = $shelflocations->{ $item->{location} };
+                                               
$notforloan_items->{$key}->{imageurl} = getitemtypeimagelocation( 'opac', 
$itemtypes{ $item->{itype} }->{imageurl} );
+                                               
$notforloan_items->{$key}->{barcode} = $item->{barcode};
+                        }else{
+                            $other_count++;
+                                       
+                            $other_items->{$key}->{intransit} = 
($transfertwhen ne '') ? 1 : 0;
+                                               
$other_items->{$key}->{notforloan} = 
GetAuthorisedValueDesc('','',$item->{notforloan},'','',$notforloan_authorised_value)
 if $notforloan_authorised_value;
+                                               $other_items->{$key}->{count}++ 
if $item->{$hbranch};
+                                               
$other_items->{$key}->{location} = $shelflocations->{ $item->{location} };
+                                               
$other_items->{$key}->{imageurl} = getitemtypeimagelocation( 'opac', 
$itemtypes{ $item->{itype} }->{imageurl} );
+                                               $other_items->{$key}->{barcode} 
= $item->{barcode};
+                        }
+    
+                    }
+                    # item is available
+                    else {
+                        $can_place_holds = 1;
+                        $available_count++;
+                                       $available_items->{$prefix}->{count}++ 
if $item->{$hbranch};
+                                       foreach (qw(branchname itemcallnumber 
barcode)) {
+                               $available_items->{$prefix}->{$_} = $item->{$_};
+                                       }
+                                       $available_items->{$prefix}->{location} 
= $shelflocations->{ $item->{location} };
+                                       $available_items->{$prefix}->{imageurl} 
= getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
+                    }
                 }
-            }
-        }    # notforloan, item level and biblioitem level
+            }    # notforloan, item level and biblioitem level
+        }
         my ( $availableitemscount, $onloanitemscount, 
$notforloanitemscount,$otheritemscount );
         $maxitems =
           ( C4::Context->preference('maxItemsinSearchResults') )
-- 
1.6.0.4

_______________________________________________
Koha-patches mailing list
[email protected]
http://lists.koha.org/mailman/listinfo/koha-patches

Reply via email to