Bug 23156: Add pagination to checkouts in ILS-DI GetPatronInfo service
[koha-equinox.git] / C4 / ILSDI / Services.pm
index 53465ca..049ddcf 100644 (file)
@@ -230,23 +230,36 @@ sub GetRecords {
         my $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
         my $holds  = $biblio->current_holds->unblessed;
         my $issues           = GetBiblioIssues($biblionumber);
-        my $items            = $biblio->items->unblessed;
+        my @items            = $biblio->items->as_list;
+
+        $biblioitem->{items}->{item} = [];
 
         # We loop over the items to clean them
-        foreach my $item (@$items) {
+        foreach my $item (@items) {
+            my %item = %{ $item->unblessed };
 
             # This hides additionnal XML subfields, we don't need these info
-            delete $item->{'more_subfields_xml'};
+            delete $item{'more_subfields_xml'};
 
             # Display branch names instead of branch codes
-            my $home_library    = Koha::Libraries->find( $item->{homebranch} );
-            my $holding_library = Koha::Libraries->find( $item->{holdingbranch} );
-            $item->{'homebranchname'}    = $home_library    ? $home_library->branchname    : '';
-            $item->{'holdingbranchname'} = $holding_library ? $holding_library->branchname : '';
+            my $home_library    = $item->home_branch;
+            my $holding_library = $item->holding_branch;
+            $item{'homebranchname'}    = $home_library    ? $home_library->branchname    : '';
+            $item{'holdingbranchname'} = $holding_library ? $holding_library->branchname : '';
+
+            my $transfer = $item->get_transfer;
+            if ($transfer) {
+                $item{transfer} = {
+                    datesent => $transfer->datesent,
+                    frombranch => $transfer->frombranch,
+                    tobranch => $transfer->tobranch,
+                };
+            }
+
+            push @{ $biblioitem->{items}->{item} }, \%item;
         }
 
         # Hashref building...
-        $biblioitem->{'items'}->{'item'}       = $items;
         $biblioitem->{'reserves'}->{'reserve'} = $holds;
         $biblioitem->{'issues'}->{'issue'}     = $issues;
 
@@ -425,11 +438,7 @@ sub GetPatronInfo {
 
     # Fines management
     if ( $cgi->param('show_fines') && $cgi->param('show_fines') eq "1" ) {
-
-        my $account_lines = $patron->account->lines;
-        while (my $line = $account_lines->next ) {
-            push @{ $borrower->{fines}{fine} }, $line->unblessed;
-        }
+        $borrower->{fines}{fine} = $patron->account->lines->unblessed;
     }
 
     # Reserves management
@@ -469,21 +478,40 @@ sub GetPatronInfo {
 
     # Issues management
     if ( $cgi->param('show_loans') && $cgi->param('show_loans') eq "1" ) {
+        my $per_page = $cgi->param('loans_per_page');
+        my $page = $cgi->param('loans_page');
+
         my $pending_checkouts = $patron->pending_checkouts;
+
+        if ($page || $per_page) {
+            $page ||= 1;
+            $per_page ||= 10;
+            $borrower->{total_loans} = $pending_checkouts->count();
+            $pending_checkouts = $pending_checkouts->search(undef, {
+                rows => $per_page,
+                page => $page,
+            });
+        }
+
         my @checkouts;
         while ( my $c = $pending_checkouts->next ) {
             # FIXME We should only retrieve what is needed in the template
             my $issue = $c->unblessed_all_relateds;
+            delete $issue->{'more_subfields_xml'};
             push @checkouts, $issue
         }
         $borrower->{'loans'}->{'loan'} = \@checkouts;
     }
 
-    if ( $cgi->param('show_attributes') eq "1" ) {
+    my $show_attributes = $cgi->param('show_attributes');
+    if ( $show_attributes && $show_attributes eq "1" ) {
         my $attrs = GetBorrowerAttributes( $borrowernumber, 1 );
         $borrower->{'attributes'} = $attrs;
     }
 
+    # Add is expired information
+    $borrower->{'is_expired'} = $patron->is_expired ? 1 : 0;
+
     return $borrower;
 }
 
@@ -539,17 +567,17 @@ sub GetServices {
     my $borrower = $patron->unblessed;
     # Get the item, or return an error code if not found
     my $itemnumber = $cgi->param('item_id');
-    my $item = GetItem( $itemnumber );
-    return { code => 'RecordNotFound' } unless $$item{itemnumber};
+    my $item = Koha::Items->find($itemnumber);
+    return { code => 'RecordNotFound' } unless $item;
 
     my @availablefor;
 
     # Reserve level management
-    my $biblionumber = $item->{'biblionumber'};
+    my $biblionumber = $item->biblionumber;
     my $canbookbereserved = CanBookBeReserved( $borrower, $biblionumber );
     if ($canbookbereserved->{status} eq 'OK') {
         push @availablefor, 'title level hold';
-        my $canitembereserved = IsAvailableForItemLevelRequest($item, $borrower);
+        my $canitembereserved = IsAvailableForItemLevelRequest($item, $patron);
         if ($canitembereserved) {
             push @availablefor, 'item level hold';
         }
@@ -572,7 +600,7 @@ sub GetServices {
     }
 
     # Issuing management
-    my $barcode = $item->{'barcode'} || '';
+    my $barcode = $item->barcode || '';
     $barcode = barcodedecode($barcode) if ( $barcode && C4::Context->preference('itemBarcodeInputFilter') );
     if ($barcode) {
         my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $barcode );
@@ -611,19 +639,20 @@ sub RenewLoan {
 
     # Get the item, or return an error code
     my $itemnumber = $cgi->param('item_id');
-    my $item = GetItem( $itemnumber );
-    return { code => 'RecordNotFound' } unless $$item{itemnumber};
+    my $item = Koha::Items->find($itemnumber);
+    return { code => 'RecordNotFound' } unless $item;
 
     # Add renewal if possible
     my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
     if ( $renewal[0] ) { AddRenewal( $borrowernumber, $itemnumber ); }
 
-    my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } ) or return; # FIXME should be handled
+    my $issue = $item->checkout;
+    return unless $issue; # FIXME should be handled
 
     # Hashref building
     my $out;
     $out->{'renewals'} = $issue->renewals;
-    $out->{date_due}   = dt_from_string($issue->date_due)->strftime('%Y-%m-%d %H:%S');
+    $out->{date_due}   = dt_from_string($issue->date_due)->strftime('%Y-%m-%d %H:%M');
     $out->{'success'}  = $renewal[0];
     $out->{'error'}    = $renewal[1];
 
@@ -664,10 +693,23 @@ sub HoldTitle {
     my $biblio = Koha::Biblios->find( $biblionumber );
     return { code => 'RecordNotFound' } unless $biblio;
 
+    my @hostitems = get_hostitemnumbers_of($biblionumber);
+    my @itemnumbers;
+    if (@hostitems){
+        push(@itemnumbers, @hostitems);
+    }
+
+    my $items = Koha::Items->search({ -or => { biblionumber => $biblionumber, itemnumber => { in => \@itemnumbers } } });
+
+    unless ( $items->count ) {
+        return { code => 'NoItems' };
+    }
+
     my $title = $biblio ? $biblio->title : '';
 
     # Check if the biblio can be reserved
-    return { code => 'NotHoldable' } unless CanBookBeReserved( $borrowernumber, $biblionumber )->{status} eq 'OK';
+    my $code = CanBookBeReserved( $borrowernumber, $biblionumber )->{status};
+    return { code => $code } unless ( $code eq 'OK' );
 
     my $branch;
 
@@ -679,6 +721,10 @@ sub HoldTitle {
         $branch = $patron->branchcode;
     }
 
+    my $destination = Koha::Libraries->find($branch);
+    return { code => 'libraryNotPickupLocation' } unless $destination->pickup_location;
+    return { code => 'cannotBeTransferred' } unless $biblio->can_be_transferred({ to => $destination });
+
     # Add the reserve
     #    $branch,    $borrowernumber, $biblionumber,
     #    $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
@@ -736,16 +782,11 @@ sub HoldItem {
 
     # Get the item or return an error code
     my $itemnumber = $cgi->param('item_id');
-    my $item = GetItem( $itemnumber );
-    return { code => 'RecordNotFound' } unless $$item{itemnumber};
+    my $item = Koha::Items->find($itemnumber);
+    return { code => 'RecordNotFound' } unless $item;
 
     # If the biblio does not match the item, return an error code
-    return { code => 'RecordNotFound' } if $$item{biblionumber} ne $biblio->biblionumber;
-
-    # Check for item disponibility
-    my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber );
-    my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
-    return { code => 'NotHoldable' } unless $canbookbereserved->{status} eq 'OK' and $canitembereserved->{status} eq 'OK';
+    return { code => 'RecordNotFound' } if $item->biblionumber ne $biblio->biblionumber;
 
     # Pickup branch management
     my $branch;
@@ -756,6 +797,10 @@ sub HoldItem {
         $branch = $patron->branchcode;
     }
 
+    # Check for item disponibility
+    my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber, $branch )->{status};
+    return { code => $canitembereserved } unless $canitembereserved eq 'OK';
+
     # Add the reserve
     #    $branch,    $borrowernumber, $biblionumber,
     #    $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
@@ -815,25 +860,25 @@ Returns, for an itemnumber, an array containing availability information.
 
 sub _availability {
     my ($itemnumber) = @_;
-    my $item = GetItem( $itemnumber, undef, undef );
+    my $item = Koha::Items->find($itemnumber);
 
-    if ( not $item->{'itemnumber'} ) {
+    unless ( $item ) {
         return ( undef, 'unknown', 'Error: could not retrieve availability for this ID', undef );
     }
 
-    my $biblionumber = $item->{'biblioitemnumber'};
-    my $library = Koha::Libraries->find( $item->{holdingbranch} );
+    my $biblionumber = $item->biblioitemnumber;
+    my $library = Koha::Libraries->find( $item->holdingbranch );
     my $location = $library ? $library->branchname : '';
 
-    if ( $item->{'notforloan'} ) {
+    if ( $item->notforloan ) {
         return ( $biblionumber, 'not available', 'Not for loan', $location );
-    } elsif ( $item->{'onloan'} ) {
+    } elsif ( $item->onloan ) {
         return ( $biblionumber, 'not available', 'Checked out', $location );
-    } elsif ( $item->{'itemlost'} ) {
+    } elsif ( $item->itemlost ) {
         return ( $biblionumber, 'not available', 'Item lost', $location );
-    } elsif ( $item->{'withdrawn'} ) {
+    } elsif ( $item->withdrawn ) {
         return ( $biblionumber, 'not available', 'Item withdrawn', $location );
-    } elsif ( $item->{'damaged'} ) {
+    } elsif ( $item->damaged ) {
         return ( $biblionumber, 'not available', 'Item damaged', $location );
     } else {
         return ( $biblionumber, 'available', undef, $location );