Bug 21206: Replace C4::Items::GetItem
[koha.git] / opac / opac-user.pl
index 9a04174..d3ff888 100755 (executable)
@@ -17,8 +17,7 @@
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 
-use strict;
-#use warnings; FIXME - Bug 2505
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 
@@ -33,9 +32,12 @@ use C4::Output;
 use C4::Biblio;
 use C4::Items;
 use C4::Letters;
+use Koha::Account::Lines;
+use Koha::Libraries;
 use Koha::DateUtils;
 use Koha::Holds;
 use Koha::Database;
+use Koha::ItemTypes;
 use Koha::Patron::Attribute::Types;
 use Koha::Patron::Messages;
 use Koha::Patron::Discharge;
@@ -59,6 +61,10 @@ BEGIN {
     }
 }
 
+# CAS single logout handling
+# Will print header and exit
+C4::Context->preference('casAuthentication') and C4::Auth_with_cas::logout_if_required($query);
+
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {
         template_name   => "opac-user.tt",
@@ -69,7 +75,7 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     }
 );
 
-my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
+my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') || '' );
 
 my $show_priority;
 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
@@ -81,12 +87,11 @@ my $canrenew = 1;
 
 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
 
-if (!$borrowernumber) {
-    $template->param( adminWarning => 1 );
-}
-
 # get borrower information ....
-my ( $borr ) = GetMember( borrowernumber => $borrowernumber );
+my $patron = Koha::Patrons->find( $borrowernumber );
+my $borr = $patron->unblessed;
+# unblessed is a hash vs. object/undef. Hence the use of curly braces here.
+my $borcat = $borr ? $borr->{categorycode} : q{};
 
 my (  $today_year,   $today_month,   $today_day) = Today();
 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
@@ -101,7 +106,7 @@ if ($debar) {
         $borr->{'userdebarreddate'} = $debar;
     }
     # FIXME looks like $available is not needed
-    # If a patron is discharged he has a validated discharge available
+    # If a user is discharged they have a validated discharge available
     my $available = Koha::Patron::Discharge::count({
         borrowernumber => $borrowernumber,
         validated      => 1,
@@ -114,7 +119,7 @@ if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
     $canrenew = 0;
 }
 
-my ( $amountoutstanding ) = GetMemberAccountRecords($borrowernumber);
+my $amountoutstanding = $patron->account->balance;
 if ( $amountoutstanding > 5 ) {
     $borr->{'amountoverfive'} = 1;
 }
@@ -159,7 +164,7 @@ if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture')
 # pass on any renew errors to the template for displaying
 my $renew_error = $query->param('renew_error');
 
-$template->param(   BORROWER_INFO     => $borr,
+$template->param(
                     amountoutstanding => $amountoutstanding,
                     borrowernumber    => $borrowernumber,
                     patron_flagged    => $borr->{flagged},
@@ -175,34 +180,48 @@ my $count          = 0;
 my $overdues_count = 0;
 my @overdues;
 my @issuedat;
-my $itemtypes = GetItemTypes();
-my $issues = GetPendingIssues($borrowernumber);
-if ($issues){
-    foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
+my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
+my $pending_checkouts = $patron->pending_checkouts->search({}, { order_by => [ { -desc => 'date_due' }, { -asc => 'issue_id' } ] });
+if ( $pending_checkouts->count ) { # Useless test
+    while ( my $c = $pending_checkouts->next ) {
+        my $issue = $c->unblessed_all_relateds;
         # check for reserves
         my $restype = GetReserveStatus( $issue->{'itemnumber'} );
         if ( $restype ) {
             $issue->{'reserved'} = 1;
         }
 
-        my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
-        my $charges = 0;
-        foreach my $ac (@$accts) {
-            if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
-                $charges += $ac->{'amountoutstanding'}
-                  if $ac->{'accounttype'} eq 'F';
-                $charges += $ac->{'amountoutstanding'}
-                  if $ac->{'accounttype'} eq 'FU';
-                $charges += $ac->{'amountoutstanding'}
-                  if $ac->{'accounttype'} eq 'L';
+        # Must be moved in a module if reused
+        my $charges = Koha::Account::Lines->search(
+            {
+                borrowernumber    => $patron->borrowernumber,
+                amountoutstanding => { '>' => 0 },
+                accounttype       => [ 'F', 'FU', 'L' ],
+                itemnumber        => $issue->{itemnumber}
+            },
+        );
+        $issue->{charges} = $charges->total_outstanding;
+
+        my $rental_fines = Koha::Account::Lines->search(
+            {
+                borrowernumber    => $patron->borrowernumber,
+                amountoutstanding => { '>' => 0 },
+                accounttype       => 'Rent',
+                itemnumber        => $issue->{itemnumber}
             }
-        }
-        $issue->{'charges'} = $charges;
-        my $marcrecord = GetMarcBiblio( $issue->{'biblionumber'} );
+        );
+        $issue->{rentalfines} = $rental_fines->total_outstanding;
+
+        my $marcrecord = GetMarcBiblio({
+            biblionumber => $issue->{'biblionumber'},
+            embed_items  => 1,
+            opac         => 1,
+            borcat       => $borcat });
         $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'}));
         # check if item is renewable
         my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
+        ( $issue->{'renewalfee'}, $issue->{'renewalitemtype'} ) = GetIssuingCharges( $issue->{'itemnumber'}, $borrowernumber );
         if($status && C4::Context->preference("OpacRenewalAllowed")){
             $issue->{'status'} = $status;
         }
@@ -216,6 +235,8 @@ if ($issues){
             $issue->{'auto_renew'}     = 1 if $renewerror eq 'auto_renew';
             $issue->{'auto_too_soon'}  = 1 if $renewerror eq 'auto_too_soon';
             $issue->{'auto_too_late'}  = 1 if $renewerror eq 'auto_too_late';
+            $issue->{'auto_too_much_oweing'}  = 1 if $renewerror eq 'auto_too_much_oweing';
+            $issue->{'item_denied_renewal'}  = 1 if $renewerror eq 'item_denied_renewal';
 
             if ( $renewerror eq 'too_soon' ) {
                 $issue->{'too_soon'}         = 1;
@@ -228,7 +249,7 @@ if ($issues){
             }
         }
 
-        if ( $issue->{'overdue'} ) {
+        if ( $c->is_overdue ) {
             push @overdues, $issue;
             $overdues_count++;
             $issue->{'overdue'} = 1;
@@ -263,6 +284,7 @@ if ($issues){
 }
 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count);
+
 $template->param( ISSUES       => \@issuedat );
 $template->param( issues_count => $count );
 $template->param( canrenew     => $canrenew );
@@ -285,13 +307,6 @@ $template->param(
     showpriority   => $show_priority,
 );
 
-# current alert subscriptions
-my $alerts = getalert($borrowernumber);
-foreach ( @$alerts ) {
-    $_->{ $_->{type} } = 1;
-    $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
-}
-
 if (C4::Context->preference('BakerTaylorEnabled')) {
     $template->param(
         BakerTaylorEnabled  => 1,
@@ -310,8 +325,9 @@ if (C4::Context->preference("OPACAmazonCoverImages") or
 
 $template->param(
     OverDriveCirculation => C4::Context->preference('OverDriveCirculation') || 0,
-    overdrive_error      => $query->param('overdrive_error') || undef,
-    overdrive_tab        => $query->param('overdrive_tab') || 0,
+    overdrive_error      => scalar $query->param('overdrive_error') || undef,
+    overdrive_tab        => scalar $query->param('overdrive_tab') || 0,
+    RecordedBooksCirculation => C4::Context->preference('RecordedBooksClientSecret') && C4::Context->preference('RecordedBooksLibraryID'),
 );
 
 my $patron_messages = Koha::Patron::Messages->search(
@@ -336,7 +352,6 @@ if (   C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor'
 }
 
 $template->param(
-    borrower                 => Koha::Patrons->find($borrowernumber),
     patron_messages          => $patron_messages,
     opacnote                 => $borr->{opacnote},
     patronupdate             => $patronupdate,
@@ -348,4 +363,16 @@ $template->param(
     failed_holds             => scalar $query->param('failed_holds'),
 );
 
+# if not an empty string this indicates to return
+# back to the opac-results page
+my $search_query = $query->param('has-search-query');
+
+if ($search_query) {
+
+    print $query->redirect(
+        -uri    => "/cgi-bin/koha/opac-search.pl?$search_query",
+        -cookie => $cookie,
+    );
+}
+
 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };