3 # This file is part of Koha.
4 # parts copyright 2010 BibLibre
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use C4::Auth qw( get_template_and_user );
27 getitemtypeimagelocation
30 GetNormalizedOCLCNumber
32 use C4::Circulation qw( CanBookBeRenewed GetRenewCount GetIssuingCharges );
33 use C4::External::BakerTaylor qw( image_url link_url );
34 use C4::Reserves qw( GetReserveStatus );
36 use C4::Output qw( output_html_with_http_headers );
37 use Koha::Account::Lines;
40 use Koha::DateUtils qw( output_pref );
44 use Koha::Patron::Attribute::Types;
46 use Koha::Patron::Messages;
47 use Koha::Patron::Discharge;
52 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
54 use Scalar::Util qw( looks_like_number );
55 use Date::Calc qw( Date_to_Days Today );
59 my $op = $query->param('op') || q{};
61 # CAS single logout handling
62 # Will print header and exit
63 if ( C4::Context->preference('casAuthentication') ) {
64 require C4::Auth_with_cas;
65 C4::Auth_with_cas::logout_if_required($query);
68 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
70 template_name => "opac-user.tt",
76 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') || '' );
79 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
80 m/priority/ and $show_priority = 1;
83 my $patronupdate = $query->param('patronupdate');
86 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
88 # get borrower information ....
89 my $patron = Koha::Patrons->find( $borrowernumber );
91 if( $op eq 'cud-update_arc' && C4::Context->preference("AllowPatronToControlAutorenewal") ){
92 my $autorenew_checkouts = $query->param('borrower_autorenew_checkouts');
93 $patron->autorenew_checkouts( $autorenew_checkouts )->store() if defined $autorenew_checkouts;
96 my $borr = $patron->unblessed;
98 my ( $today_year, $today_month, $today_day) = Today();
99 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
101 my $debar = Koha::Patrons->find( $borrowernumber )->is_debarred;
106 $template->param( 'userdebarred' => $userdebarred );
107 if ( $debar ne "9999-12-31" ) {
108 $borr->{'userdebarreddate'} = $debar;
110 # FIXME looks like $available is not needed
111 # If a user is discharged they have a validated discharge available
112 my $available = Koha::Patron::Discharge::count({
113 borrowernumber => $borrowernumber,
116 $template->param( 'discharge_available' => $available && Koha::Patron::Discharge::is_discharged({borrowernumber => $borrowernumber}) );
119 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
120 $borr->{'flagged'} = 1;
124 my $amountoutstanding = $patron->account->balance;
125 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
126 $no_renewal_amt = undef unless looks_like_number( $no_renewal_amt );
127 my $amountoutstandingfornewal =
128 C4::Context->preference("OPACFineNoRenewalsIncludeCredit")
130 : $patron->account->outstanding_debits->total_outstanding;
132 if ( C4::Context->preference('OpacRenewalAllowed')
133 && defined($no_renewal_amt)
134 && $amountoutstandingfornewal > $no_renewal_amt )
136 $borr->{'flagged'} = 1;
139 renewal_blocked_fines => $no_renewal_amt,
140 renewal_blocked_fines_amountoutstanding => $amountoutstandingfornewal,
144 my $maxoutstanding = C4::Context->preference('maxoutstanding');
145 if ( $amountoutstanding && ( $amountoutstanding > $maxoutstanding ) ){
146 $borr->{blockedonfines} = 1;
149 # Warningdate is the date that the warning starts appearing
150 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
151 my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
152 if ( $days_to_expiry < 0 ) {
153 #borrower card has expired, warn the borrower
154 $borr->{'warnexpired'} = $borr->{'dateexpiry'};
155 } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
156 # borrower card soon to expire, warn the borrower
157 $borr->{'warndeparture'} = $borr->{dateexpiry};
158 if (C4::Context->preference('ReturnBeforeExpiry')){
159 $borr->{'returnbeforeexpiry'} = 1;
164 my $saving_display = C4::Context->preference('OPACShowSavings');
165 if ( $saving_display =~ /user/ ) {
166 $template->param( savings => $patron->get_savings );
169 # pass on any renew errors to the template for displaying
170 my $renew_error = $query->param('renew_error');
173 amountoutstanding => $amountoutstanding,
174 borrowernumber => $borrowernumber,
175 patron_flagged => $borr->{flagged},
176 surname => $borr->{surname},
177 RENEW_ERROR => $renew_error,
181 #get issued items ....
184 my $overdues_count = 0;
187 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
188 my $pending_checkouts = $patron->pending_checkouts->search(
191 order_by => [ { -desc => 'date_due' }, { -asc => 'issue_id' } ],
195 my $are_renewable_items = 0;
196 if ( $pending_checkouts->count ) { # Useless test
197 while ( my $c = $pending_checkouts->next ) {
198 my $issue = $c->unblessed_all_relateds;
200 my $restype = GetReserveStatus( $issue->{'itemnumber'} );
202 $issue->{'reserved'} = 1;
205 # Must be moved in a module if reused
206 my $charges = Koha::Account::Lines->search(
208 borrowernumber => $patron->borrowernumber,
209 amountoutstanding => { '>' => 0 },
210 debit_type_code => [ 'OVERDUE', 'LOST' ],
211 itemnumber => $issue->{itemnumber}
214 $issue->{charges} = $charges->total_outstanding;
216 my $rental_fines = Koha::Account::Lines->search(
218 borrowernumber => $patron->borrowernumber,
219 amountoutstanding => { '>' => 0 },
220 debit_type_code => { 'LIKE' => 'RENT_%' },
221 itemnumber => $issue->{itemnumber}
224 $issue->{rentalfines} = $rental_fines->total_outstanding;
226 # check if item is renewable
227 my ($status, $renewerror, $info) = CanBookBeRenewed( $patron, $c );
229 $issue->{'renewcount'},
230 $issue->{'renewsallowed'},
231 $issue->{'renewsleft'},
232 $issue->{'unseencount'},
233 $issue->{'unseenallowed'},
234 $issue->{'unseenleft'}
235 ) = GetRenewCount($patron, $c->item);
236 ( $issue->{'renewalfee'}, $issue->{'renewalitemtype'} ) = GetIssuingCharges( $issue->{'itemnumber'}, $borrowernumber );
237 $issue->{itemtype_object} = Koha::ItemTypes->find( $c->item->effective_itemtype );
238 if($status && C4::Context->preference("OpacRenewalAllowed")){
239 $are_renewable_items = 1;
240 $issue->{'status'} = $status;
243 $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
246 $issue->{'too_many'} = 1 if $renewerror eq 'too_many';
247 $issue->{'too_unseen'} = 1 if $renewerror eq 'too_unseen';
248 $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
249 $issue->{'norenew_overdue'} = 1 if $renewerror eq 'overdue';
250 $issue->{'auto_renew'} = 1 if $renewerror eq 'auto_renew';
251 $issue->{'auto_too_soon'} = 1 if $renewerror eq 'auto_too_soon';
252 $issue->{'auto_too_late'} = 1 if $renewerror eq 'auto_too_late';
253 $issue->{'auto_too_much_oweing'} = 1 if $renewerror eq 'auto_too_much_oweing';
254 $issue->{'item_denied_renewal'} = 1 if $renewerror eq 'item_denied_renewal';
255 $issue->{'item_issued_to_other_patron'} = 1 if $renewerror eq 'item_issued_to_other_patron';
257 if ( $renewerror eq 'too_soon' ) {
258 $issue->{'too_soon'} = 1;
259 $issue->{'soonestrenewdate'} = $info->{soonest_renew_date};
263 if ( $c->is_overdue ) {
264 push @overdues, $issue;
266 $issue->{'overdue'} = 1;
269 $issue->{'issued'} = 1;
272 my $itemtype = $issue->{'itemtype'};
274 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
275 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
278 if ( C4::Context->preference('OpacStarRatings') eq 'all' ) {
279 my $ratings = Koha::Ratings->search({ biblionumber => $issue->{biblionumber} });
280 $issue->{ratings} = $ratings;
281 $issue->{my_rating} = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
284 my $biblio_object = Koha::Biblios->find($issue->{biblionumber});
285 $issue->{biblio_object} = $biblio_object;
286 push @issuedat, $issue;
289 my $isbn = GetNormalizedISBN($issue->{'isbn'});
290 $issue->{normalized_isbn} = $isbn;
292 if ( C4::Context->preference('BakerTaylorEnabled')
293 || C4::Context->preference('SyndeticsEnabled')
294 || C4::Context->preference('SyndeticsCoverImages') )
296 my $marcrecord = $biblio_object->metadata->record( { embed_items => 1, opac => 1, patron => $patron, } );
297 $issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
298 $issue->{normalized_oclc} = GetNormalizedOCLCNumber( $marcrecord, C4::Context->preference('marcflavour') );
301 if ( C4::Context->preference('UseRecalls') ) {
302 my $maybe_recalls = Koha::Recalls->search({ biblio_id => $issue->{biblionumber}, item_id => [ undef, $issue->{itemnumber} ], completed => 0 });
303 while( my $recall = $maybe_recalls->next ) {
304 if ( $recall->checkout and $recall->checkout->issue_id == $issue->{issue_id} ) {
305 $issue->{recall} = 1;
312 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
313 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count) || !$are_renewable_items;
315 $template->param( ISSUES => \@issuedat );
316 $template->param( issues_count => $count );
317 $template->param( canrenew => $canrenew );
318 $template->param( OVERDUES => \@overdues );
319 $template->param( overdues_count => $overdues_count );
321 my $show_barcode = Koha::Patron::Attribute::Types->search( # FIXME we should not need this search
322 { code => ATTRIBUTE_SHOW_BARCODE } )->count;
324 my $patron_show_barcode = $patron->get_extended_attribute(ATTRIBUTE_SHOW_BARCODE);
325 undef $show_barcode if $patron_show_barcode and not $patron_show_barcode->attribute;
327 $template->param( show_barcode => 1 ) if $show_barcode;
329 # now the reserved items....
330 my $reserves = $patron->holds->filter_out_has_cancellation_requests;
333 RESERVES => $reserves,
334 showpriority => $show_priority,
337 if ( C4::Context->preference('UseRecalls') ) {
338 my $recalls = Koha::Recalls->search( { patron_id => $borrowernumber, completed => 0 } );
339 $template->param( RECALLS => $recalls );
342 if (C4::Context->preference('BakerTaylorEnabled')) {
344 BakerTaylorEnabled => 1,
345 BakerTaylorImageURL => &image_url(),
346 BakerTaylorLinkURL => &link_url(),
347 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
351 if (C4::Context->preference("OPACAmazonCoverImages") or
352 C4::Context->preference("GoogleJackets") or
353 C4::Context->preference("BakerTaylorEnabled") or
354 C4::Context->preference("SyndeticsCoverImages") or
355 ( C4::Context->preference('OPACCustomCoverImages') and C4::Context->preference('CustomCoverImagesURL') )
357 $template->param(JacketImages=>1);
361 OverDriveCirculation => C4::Context->preference('OverDriveCirculation') || 0,
362 overdrive_error => scalar $query->param('overdrive_error') || undef,
363 overdrive_tab => scalar $query->param('overdrive_tab') || 0,
366 my $patron_messages = Koha::Patron::Messages->search(
368 borrowernumber => $borrowernumber,
373 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor')
374 || C4::Context->preference('AllowStaffToSetCheckoutsVisibilityForGuarantor') )
377 # Filter out guarantees that don't want guarantor to see checkouts
378 foreach my $gr ( $patron->guarantee_relationships->as_list ) {
379 my $g = $gr->guarantee;
380 push( @relatives, $g ) if $g->privacy_guarantor_checkouts;
382 $template->param( relatives => \@relatives );
385 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor')
386 || C4::Context->preference('AllowStaffToSetFinesVisibilityForGuarantor') )
388 my @relatives_with_fines;
389 # Filter out guarantees that don't want guarantor to see checkouts
390 foreach my $gr ( $patron->guarantee_relationships->as_list ) {
391 my $g = $gr->guarantee;
392 push( @relatives_with_fines, $g ) if $g->privacy_guarantor_fines;
394 $template->param( relatives_with_fines => \@relatives_with_fines );
397 if ( C4::Context->preference("ArticleRequests") ) {
399 current_article_requests => [$patron->article_requests->filter_by_current->as_list],
404 patron_messages => $patron_messages,
405 opacnote => $borr->{opacnote},
406 patronupdate => $patronupdate,
407 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
409 SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
410 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
411 OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
412 failed_holds => scalar $query->param('failed_holds'),
413 opac_user_holds => scalar $query->param('opac-user-holds') || 0,
414 opac_user_article_requests => scalar $query->param('opac-user-article-requests') || 0,
417 # if not an empty string this indicates to return
418 # back to the opac-results page
419 my $search_query = $query->param('has-search-query');
423 print $query->redirect(
424 -uri => "/cgi-bin/koha/opac-search.pl?$search_query",
429 # if not an empty string this indicates to return
430 # back to the page we triggered the login from
431 my $return = $query->param('return');
433 my $uri_syspref = C4::Context->preference('OPACBaseURL');
435 my $uri = URI->new($uri_syspref);
436 if ( $uri->isa('URI::http') && $uri->host() ){
437 my $return_uri = URI->new($return);
438 $return_uri->scheme( $uri->scheme() );
439 $return_uri->authority( $uri->authority() );
440 print $query->redirect(
441 -uri => "$return_uri",
448 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };