Bug 23156: Add pagination to checkouts in ILS-DI GetPatronInfo service
[koha-equinox.git] / C4 / ILSDI / Services.pm
index 2b291b8..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;
 
@@ -465,17 +478,33 @@ 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;
     }
@@ -548,7 +577,7 @@ sub GetServices {
     my $canbookbereserved = CanBookBeReserved( $borrower, $biblionumber );
     if ($canbookbereserved->{status} eq 'OK') {
         push @availablefor, 'title level hold';
-        my $canitembereserved = IsAvailableForItemLevelRequest($item->unblessed, $borrower);
+        my $canitembereserved = IsAvailableForItemLevelRequest($item, $patron);
         if ($canitembereserved) {
             push @availablefor, 'item level hold';
         }
@@ -617,15 +646,13 @@ sub RenewLoan {
     my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
     if ( $renewal[0] ) { AddRenewal( $borrowernumber, $itemnumber ); }
 
-    return unless $item; # FIXME should be handled
-
     my $issue = $item->checkout;
-    return $issue; # FIXME should be handled
+    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];
 
@@ -694,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,
@@ -757,10 +788,6 @@ sub HoldItem {
     # 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 )->{status};
-    return { code => $canitembereserved } unless $canitembereserved eq 'OK';
-
     # Pickup branch management
     my $branch;
     if ( $cgi->param('pickup_location') ) {
@@ -770,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,