Bug 15217 Remove redundant duplicate var declaration
[koha.git] / opac / opac-user.pl
blob54c929578824eb415ae74e600dea23588f078c7d
1 #!/usr/bin/perl
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>.
20 use strict;
21 #use warnings; FIXME - Bug 2505
23 use CGI qw ( -utf8 );
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Circulation;
28 use C4::Reserves;
29 use C4::Members;
30 use C4::Members::AttributeTypes;
31 use C4::Members::Attributes qw/GetBorrowerAttributeValue/;
32 use C4::Output;
33 use C4::Biblio;
34 use C4::Items;
35 use C4::Letters;
36 use C4::Branch; # GetBranches
37 use Koha::DateUtils;
38 use Koha::Borrower::Debarments qw(IsDebarred);
40 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
42 use Date::Calc qw(
43 Today
44 Add_Delta_Days
45 Date_to_Days
48 my $query = new CGI;
50 BEGIN {
51 if (C4::Context->preference('BakerTaylorEnabled')) {
52 require C4::External::BakerTaylor;
53 import C4::External::BakerTaylor qw(&image_url &link_url);
57 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
59 template_name => "opac-user.tt",
60 query => $query,
61 type => "opac",
62 authnotrequired => 0,
63 debug => 1,
67 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
69 my $show_priority;
70 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
71 m/priority/ and $show_priority = 1;
74 my $patronupdate = $query->param('patronupdate');
75 my $canrenew = 1;
77 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
79 # get borrower information ....
80 my ( $borr ) = GetMemberDetails( $borrowernumber );
82 my ( $today_year, $today_month, $today_day) = Today();
83 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
85 my $debar = IsDebarred($borrowernumber);
86 my $userdebarred;
88 if ($debar) {
89 $userdebarred = 1;
90 $template->param( 'userdebarred' => $userdebarred );
91 if ( $debar ne "9999-12-31" ) {
92 $borr->{'userdebarreddate'} = $debar;
96 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
97 $borr->{'flagged'} = 1;
98 $canrenew = 0;
101 if ( $borr->{'amountoutstanding'} > 5 ) {
102 $borr->{'amountoverfive'} = 1;
104 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
105 $borr->{'amountoverzero'} = 1;
107 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
108 $no_renewal_amt ||= 0;
110 if ( C4::Context->preference( 'OpacRenewalAllowed' ) && $borr->{amountoutstanding} > $no_renewal_amt ) {
111 $borr->{'flagged'} = 1;
112 $canrenew = 0;
113 $template->param(
114 renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
115 renewal_blocked_fines_amountoutstanding => sprintf( '%.02f', $borr->{amountoutstanding} ),
119 if ( $borr->{'amountoutstanding'} < 0 ) {
120 $borr->{'amountlessthanzero'} = 1;
121 $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
124 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
126 my @bordat;
127 $bordat[0] = $borr;
129 # Warningdate is the date that the warning starts appearing
130 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
131 my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
132 if ( $days_to_expiry < 0 ) {
133 #borrower card has expired, warn the borrower
134 $borr->{'warnexpired'} = $borr->{'dateexpiry'};
135 } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
136 # borrower card soon to expire, warn the borrower
137 $borr->{'warndeparture'} = $borr->{dateexpiry};
138 if (C4::Context->preference('ReturnBeforeExpiry')){
139 $borr->{'returnbeforeexpiry'} = 1;
144 # pass on any renew errors to the template for displaying
145 my $renew_error = $query->param('renew_error');
147 $template->param( BORROWER_INFO => \@bordat,
148 borrowernumber => $borrowernumber,
149 patron_flagged => $borr->{flagged},
150 OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
151 surname => $borr->{surname},
152 showname => $borr->{showname},
153 RENEW_ERROR => $renew_error,
154 borrower => $borr,
157 #get issued items ....
159 my $count = 0;
160 my $overdues_count = 0;
161 my @overdues;
162 my @issuedat;
163 my $itemtypes = GetItemTypes();
164 my $issues = GetPendingIssues($borrowernumber);
165 if ($issues){
166 foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
167 # check for reserves
168 my $restype = GetReserveStatus( $issue->{'itemnumber'} );
169 if ( $restype ) {
170 $issue->{'reserved'} = 1;
173 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
174 my $charges = 0;
175 foreach my $ac (@$accts) {
176 if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
177 $charges += $ac->{'amountoutstanding'}
178 if $ac->{'accounttype'} eq 'F';
179 $charges += $ac->{'amountoutstanding'}
180 if $ac->{'accounttype'} eq 'FU';
181 $charges += $ac->{'amountoutstanding'}
182 if $ac->{'accounttype'} eq 'L';
185 $issue->{'charges'} = $charges;
186 my $marcrecord = GetMarcBiblio( $issue->{'biblionumber'} );
187 $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'}));
188 # check if item is renewable
189 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
190 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
191 if($status && C4::Context->preference("OpacRenewalAllowed")){
192 $issue->{'status'} = $status;
195 $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
197 if ($renewerror) {
198 $issue->{'too_many'} = 1 if $renewerror eq 'too_many';
199 $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
200 $issue->{'norenew_overdue'} = 1 if $renewerror eq 'overdue';
201 $issue->{'auto_renew'} = 1 if $renewerror eq 'auto_renew';
202 $issue->{'auto_too_soon'} = 1 if $renewerror eq 'auto_too_soon';
204 if ( $renewerror eq 'too_soon' ) {
205 $issue->{'too_soon'} = 1;
206 $issue->{'soonestrenewdate'} = output_pref(
207 C4::Circulation::GetSoonestRenewDate(
208 $issue->{borrowernumber},
209 $issue->{itemnumber}
215 if ( $issue->{'overdue'} ) {
216 push @overdues, $issue;
217 $overdues_count++;
218 $issue->{'overdue'} = 1;
220 else {
221 $issue->{'issued'} = 1;
223 # imageurl:
224 my $itemtype = $issue->{'itemtype'};
225 if ( $itemtype ) {
226 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
227 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
229 push @issuedat, $issue;
230 $count++;
232 my $isbn = GetNormalizedISBN($issue->{'isbn'});
233 $issue->{normalized_isbn} = $isbn;
234 $issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
236 # My Summary HTML
237 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
238 $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
239 $issue->{title} =~ s/\/+$//; # remove trailing slash
240 $issue->{title} =~ s/\s+$//; # remove trailing space
241 $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
242 $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
243 $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
244 $issue->{MySummaryHTML} = $my_summary_html;
248 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
249 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count);
250 $template->param( ISSUES => \@issuedat );
251 $template->param( issues_count => $count );
252 $template->param( canrenew => $canrenew );
253 $template->param( OVERDUES => \@overdues );
254 $template->param( overdues_count => $overdues_count );
256 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
257 if ($show_barcode) {
258 my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
259 undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
261 $template->param( show_barcode => 1 ) if $show_barcode;
263 # load the branches
264 my $branches = GetBranches();
265 my @branch_loop;
266 for my $branch_hash ( sort keys %{$branches} ) {
267 my $selected;
268 if ( C4::Context->preference('SearchMyLibraryFirst') ) {
269 $selected =
270 ( C4::Context->userenv
271 && ( $branch_hash eq C4::Context->userenv->{branch} ) );
273 push @branch_loop,
274 { value => "branch: $branch_hash",
275 branchname => $branches->{$branch_hash}->{'branchname'},
276 selected => $selected,
279 $template->param( branchloop => \@branch_loop );
281 # now the reserved items....
282 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
283 foreach my $res (@reserves) {
285 if ( $res->{'expirationdate'} eq '0000-00-00' ) {
286 $res->{'expirationdate'} = '';
288 $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
289 $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
290 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
291 my $biblioData = GetBiblioData($res->{'biblionumber'});
292 $res->{'reserves_title'} = $biblioData->{'title'};
293 $res->{'author'} = $biblioData->{'author'};
295 if ($show_priority) {
296 $res->{'priority'} ||= '';
298 if ( $res->{'suspend_until'} ) {
299 $res->{'suspend_until'} = output_pref({ dt => dt_from_string( $res->{'suspend_until'} , 'iso' ), dateonly => 1 });
303 # use Data::Dumper;
304 # warn Dumper(@reserves);
306 $template->param( RESERVES => \@reserves );
307 $template->param( reserves_count => $#reserves+1 );
308 $template->param( showpriority=>$show_priority );
310 my @waiting;
311 my $wcount = 0;
312 foreach my $res (@reserves) {
313 if ( $res->{'itemnumber'} ) {
314 my $item = GetItem( $res->{'itemnumber'});
315 $res->{'holdingbranch'} =
316 $branches->{ $item->{'holdingbranch'} }->{'branchname'};
317 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
318 $res->{'enumchron'} = $item->{'enumchron'} if $item->{'enumchron'};
319 # get document reserve status
320 my $biblioData = GetBiblioData($res->{'biblionumber'});
321 $res->{'waiting_title'} = $biblioData->{'title'};
322 if ( ( $res->{'found'} eq 'W' ) ) {
323 my $item = $res->{'itemnumber'};
324 $item = GetBiblioFromItemNumber($item,undef);
325 $res->{'wait'}= 1;
326 $res->{'holdingbranch'}=$item->{'holdingbranch'};
327 $res->{'biblionumber'}=$item->{'biblionumber'};
328 $res->{'barcode'} = $item->{'barcode'};
329 $res->{'wbrcode'} = $res->{'branchcode'};
330 $res->{'itemnumber'} = $res->{'itemnumber'};
331 $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
332 if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
333 $res->{'atdestination'} = 1;
335 # set found to 1 if reserve is waiting for patron pickup
336 $res->{'found'} = 1 if $res->{'found'} eq 'W';
337 } else {
338 my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
339 if ($transfertwhen) {
340 $res->{intransit} = 1;
341 $res->{datesent} = $transfertwhen;
342 $res->{frombranch} = GetBranchName($transfertfrom);
345 push @waiting, $res;
346 $wcount++;
348 # can be cancelled
349 #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
350 $res->{'cancelable'} = 1 if ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
354 $template->param( WAITING => \@waiting );
356 # current alert subscriptions
357 my $alerts = getalert($borrowernumber);
358 foreach ( @$alerts ) {
359 $_->{ $_->{type} } = 1;
360 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
363 if (C4::Context->preference('BakerTaylorEnabled')) {
364 $template->param(
365 BakerTaylorEnabled => 1,
366 BakerTaylorImageURL => &image_url(),
367 BakerTaylorLinkURL => &link_url(),
368 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
372 if (C4::Context->preference("OPACAmazonCoverImages") or
373 C4::Context->preference("GoogleJackets") or
374 C4::Context->preference("BakerTaylorEnabled") or
375 C4::Context->preference("SyndeticsCoverImages")) {
376 $template->param(JacketImages=>1);
379 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
380 $template->param( bor_messages => 1 );
383 if ( $borr->{'opacnote'} ) {
384 $template->param(
385 bor_messages => 1,
386 opacnote => $borr->{'opacnote'},
390 $template->param(
391 bor_messages_loop => GetMessages( $borrowernumber, 'B', 'NONE' ),
392 waiting_count => $wcount,
393 patronupdate => $patronupdate,
394 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
395 userview => 1,
396 SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
397 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
398 OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
399 failed_holds => $query->param('failed_holds'),
402 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };