Bug 21301: Remove patron informations in issues for GetRecords ILS-DI service
[koha-ffzg.git] / C4 / ILSDI / Services.pm
index f38365c..30a03bc 100644 (file)
@@ -33,7 +33,6 @@ use HTML::Entities;
 use CGI qw ( -utf8 );
 use DateTime;
 use C4::Auth;
-use C4::Members::Attributes qw(GetBorrowerAttributes);
 use Koha::DateUtils;
 
 use Koha::Biblios;
@@ -229,7 +228,14 @@ sub GetRecords {
         # Get most of the needed data
         my $biblioitemnumber = $biblioitem->{'biblioitemnumber'};
         my $holds  = $biblio->current_holds->unblessed;
-        my $issues           = GetBiblioIssues($biblionumber);
+        my $checkouts = Koha::Checkouts->search(
+            { biblionumber => $biblionumber },
+            {
+                join => 'item',
+                '+select' => ['item.barcode'],
+                '+as'     => ['barcode'],
+            }
+        )->unblessed;
         my @items            = $biblio->items->as_list;
 
         $biblioitem->{items}->{item} = [];
@@ -247,6 +253,20 @@ sub GetRecords {
             $item{'homebranchname'}    = $home_library    ? $home_library->branchname    : '';
             $item{'holdingbranchname'} = $holding_library ? $holding_library->branchname : '';
 
+            if ($item->location) {
+                my $authorised_value = Koha::AuthorisedValues->find_by_koha_field({ kohafield => 'items.location', authorised_value => $item->location });
+                if ($authorised_value) {
+                    $item{location_description} = $authorised_value->opac_description;
+                }
+            }
+
+            if ($item->itype) {
+                my $itemtype = Koha::ItemTypes->find($item->itype);
+                if ($itemtype) {
+                    $item{itype_description} = $itemtype->description;
+                }
+            }
+
             my $transfer = $item->get_transfer;
             if ($transfer) {
                 $item{transfer} = {
@@ -261,7 +281,7 @@ sub GetRecords {
 
         # Hashref building...
         $biblioitem->{'reserves'}->{'reserve'} = $holds;
-        $biblioitem->{'issues'}->{'issue'}     = $issues;
+        $biblioitem->{'issues'}->{'issue'}     = $checkouts;
 
         push @records, $biblioitem;
     }
@@ -478,7 +498,21 @@ 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
@@ -489,9 +523,22 @@ sub GetPatronInfo {
         $borrower->{'loans'}->{'loan'} = \@checkouts;
     }
 
-    if ( $cgi->param('show_attributes') eq "1" ) {
-        my $attrs = GetBorrowerAttributes( $borrowernumber, 1 );
-        $borrower->{'attributes'} = $attrs;
+    my $show_attributes = $cgi->param('show_attributes');
+    if ( $show_attributes && $show_attributes eq "1" ) {
+        # FIXME Regression expected here, we do not retrieve the same field as previously
+        # Waiting for answer on bug 14257 comment 15
+        $borrower->{'attributes'} = [
+            map {
+                $_->type->opac_display
+                  ? {
+                    %{ $_->unblessed },
+                    %{ $_->type->unblessed },
+                    value             => $_->attribute,   # Backward compatibility
+                    value_description => $_->description, # Awkward retro-compability...
+                  }
+                  : ()
+            } $patron->extended_attributes->search
+        ];
     }
 
     # Add is expired information
@@ -629,7 +676,7 @@ sub RenewLoan {
 
     # Add renewal if possible
     my @renewal = CanBookBeRenewed( $borrowernumber, $itemnumber );
-    if ( $renewal[0] ) { AddRenewal( $borrowernumber, $itemnumber ); }
+    if ( $renewal[0] ) { AddRenewal( $borrowernumber, $itemnumber, undef, undef, undef, undef, 0 ); }
 
     my $issue = $item->checkout;
     return unless $issue; # FIXME should be handled
@@ -658,9 +705,9 @@ Parameters:
     IP address where the end user request is being placed
   - pickup_location (Optional)
     a branch code indicating the location to which to deliver the item for pickup
-  - needed_before_date (Optional)
-    date after which hold request is no longer needed
-  - pickup_expiry_date (Optional)
+  - start_date (Optional)
+    date after which hold request is no longer needed if the document has not been made available
+  - expiry_date (Optional)
     date after which item returned to shelf if item is not picked up
 
 =cut
@@ -673,6 +720,9 @@ sub HoldTitle {
     my $patron = Koha::Patrons->find( $borrowernumber );
     return { code => 'PatronNotFound' } unless $patron;
 
+    # If borrower is restricted return an error code
+    return { code => 'PatronRestricted' } if $patron->is_debarred;
+
     # Get the biblio record, or return an error code
     my $biblionumber = $cgi->param('bib_id');
     my $biblio = Koha::Biblios->find( $biblionumber );
@@ -710,12 +760,32 @@ sub HoldTitle {
     return { code => 'libraryNotPickupLocation' } unless $destination->pickup_location;
     return { code => 'cannotBeTransferred' } unless $biblio->can_be_transferred({ to => $destination });
 
+    my $resdate;
+    if ( $cgi->param('start_date') ) {
+        $resdate = $cgi->param('start_date');
+    }
+
+    my $expdate;
+    if ( $cgi->param('expiry_date') ) {
+        $expdate = $cgi->param('expiry_date');
+    }
+
     # Add the reserve
     #    $branch,    $borrowernumber, $biblionumber,
     #    $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
     #    $title,      $checkitem, $found
     my $priority= C4::Reserves::CalculatePriority( $biblionumber );
-    AddReserve( $branch, $borrowernumber, $biblionumber, undef, $priority, undef, undef, undef, $title, undef, undef );
+    AddReserve(
+        {
+            branchcode       => $branch,
+            borrowernumber   => $borrowernumber,
+            biblionumber     => $biblionumber,
+            priority         => $priority,
+            reservation_date => $resdate,
+            expiration_date  => $expdate,
+            title            => $title,
+        }
+    );
 
     # Hashref building
     my $out;
@@ -743,9 +813,9 @@ Parameters:
     an itemnumber
   - pickup_location (Optional)
     a branch code indicating the location to which to deliver the item for pickup
-  - needed_before_date (Optional)
-    date after which hold request is no longer needed
-  - pickup_expiry_date (Optional)
+  - start_date (Optional)
+    date after which hold request is no longer needed if the item has not been made available
+  - expiry_date (Optional)
     date after which item returned to shelf if item is not picked up
 
 =cut
@@ -758,6 +828,9 @@ sub HoldItem {
     my $patron = Koha::Patrons->find( $borrowernumber );
     return { code => 'PatronNotFound' } unless $patron;
 
+    # If borrower is restricted return an error code
+    return { code => 'PatronRestricted' } if $patron->is_debarred;
+
     # Get the biblio or return an error code
     my $biblionumber = $cgi->param('bib_id');
     my $biblio = Koha::Biblios->find( $biblionumber );
@@ -786,12 +859,30 @@ sub HoldItem {
     my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber, $branch )->{status};
     return { code => $canitembereserved } unless $canitembereserved eq 'OK';
 
+    my $resdate;
+    if ( $cgi->param('start_date') ) {
+        $resdate = $cgi->param('start_date');
+    }
+
+    my $expdate;
+    if ( $cgi->param('expiry_date') ) {
+        $expdate = $cgi->param('expiry_date');
+    }
+
     # Add the reserve
-    #    $branch,    $borrowernumber, $biblionumber,
-    #    $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
-    #    $title,      $checkitem, $found
-    my $priority= C4::Reserves::CalculatePriority( $biblionumber );
-    AddReserve( $branch, $borrowernumber, $biblionumber, undef, $priority, undef, undef, undef, $title, $itemnumber, undef );
+    my $priority = C4::Reserves::CalculatePriority($biblionumber);
+    AddReserve(
+        {
+            branchcode       => $branch,
+            borrowernumber   => $borrowernumber,
+            biblionumber     => $biblionumber,
+            priority         => $priority,
+            reservation_date => $resdate,
+            expiration_date  => $expdate,
+            title            => $title,
+            itemnumber       => $itemnumber,
+        }
+    );
 
     # Hashref building
     my $out;