Bug 13918 [QA Followup] - Remove dead template code
[koha.git] / opac / opac-user.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 # parts copyright 2010 BibLibre
5 #
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.
10 #
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.
15 #
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>.
18
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use CGI qw ( -utf8 );
24
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);
39 use Koha::Holds;
40
41 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
42
43 use Date::Calc qw(
44   Today
45   Add_Delta_Days
46   Date_to_Days
47 );
48
49 my $query = new CGI;
50
51 BEGIN {
52     if (C4::Context->preference('BakerTaylorEnabled')) {
53         require C4::External::BakerTaylor;
54         import C4::External::BakerTaylor qw(&image_url &link_url);
55     }
56 }
57
58 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
59     {
60         template_name   => "opac-user.tt",
61         query           => $query,
62         type            => "opac",
63         authnotrequired => 0,
64         debug           => 1,
65     }
66 );
67
68 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
69
70 my $show_priority;
71 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
72     m/priority/ and $show_priority = 1;
73 }
74
75 my $patronupdate = $query->param('patronupdate');
76 my $canrenew = 1;
77
78 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
79
80 # get borrower information ....
81 my ( $borr ) = GetMemberDetails( $borrowernumber );
82
83 my (  $today_year,   $today_month,   $today_day) = Today();
84 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
85
86 my $debar = IsDebarred($borrowernumber);
87 my $userdebarred;
88
89 if ($debar) {
90     $userdebarred = 1;
91     $template->param( 'userdebarred' => $userdebarred );
92     if ( $debar ne "9999-12-31" ) {
93         $borr->{'userdebarreddate'} = $debar;
94     }
95 }
96
97 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
98     $borr->{'flagged'} = 1;
99     $canrenew = 0;
100 }
101
102 if ( $borr->{'amountoutstanding'} > 5 ) {
103     $borr->{'amountoverfive'} = 1;
104 }
105 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
106     $borr->{'amountoverzero'} = 1;
107 }
108 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
109 $no_renewal_amt ||= 0;
110
111 if (  C4::Context->preference( 'OpacRenewalAllowed' ) && $borr->{amountoutstanding} > $no_renewal_amt ) {
112     $borr->{'flagged'} = 1;
113     $canrenew = 0;
114     $template->param(
115         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
116         renewal_blocked_fines_amountoutstanding => sprintf( '%.02f', $borr->{amountoutstanding} ),
117     );
118 }
119
120 if ( $borr->{'amountoutstanding'} < 0 ) {
121     $borr->{'amountlessthanzero'} = 1;
122     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
123 }
124
125 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
126
127 # Warningdate is the date that the warning starts appearing
128 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
129     my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
130     if ( $days_to_expiry < 0 ) {
131         #borrower card has expired, warn the borrower
132         $borr->{'warnexpired'} = $borr->{'dateexpiry'};
133     } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
134         # borrower card soon to expire, warn the borrower
135         $borr->{'warndeparture'} = $borr->{dateexpiry};
136         if (C4::Context->preference('ReturnBeforeExpiry')){
137             $borr->{'returnbeforeexpiry'} = 1;
138         }
139     }
140 }
141
142 # pass on any renew errors to the template for displaying
143 my $renew_error = $query->param('renew_error');
144
145 $template->param(   BORROWER_INFO     => $borr,
146                     borrowernumber    => $borrowernumber,
147                     patron_flagged    => $borr->{flagged},
148                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
149                     surname           => $borr->{surname},
150                     RENEW_ERROR       => $renew_error,
151                     borrower          => $borr,
152                 );
153
154 #get issued items ....
155
156 my $count          = 0;
157 my $overdues_count = 0;
158 my @overdues;
159 my @issuedat;
160 my $itemtypes = GetItemTypes();
161 my $issues = GetPendingIssues($borrowernumber);
162 if ($issues){
163     foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
164         # check for reserves
165         my $restype = GetReserveStatus( $issue->{'itemnumber'} );
166         if ( $restype ) {
167             $issue->{'reserved'} = 1;
168         }
169
170         my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
171         my $charges = 0;
172         foreach my $ac (@$accts) {
173             if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
174                 $charges += $ac->{'amountoutstanding'}
175                   if $ac->{'accounttype'} eq 'F';
176                 $charges += $ac->{'amountoutstanding'}
177                   if $ac->{'accounttype'} eq 'FU';
178                 $charges += $ac->{'amountoutstanding'}
179                   if $ac->{'accounttype'} eq 'L';
180             }
181         }
182         $issue->{'charges'} = $charges;
183         my $marcrecord = GetMarcBiblio( $issue->{'biblionumber'} );
184         $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'}));
185         # check if item is renewable
186         my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
187         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
188         if($status && C4::Context->preference("OpacRenewalAllowed")){
189             $issue->{'status'} = $status;
190         }
191
192         $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
193
194         if ($renewerror) {
195             $issue->{'too_many'}       = 1 if $renewerror eq 'too_many';
196             $issue->{'on_reserve'}     = 1 if $renewerror eq 'on_reserve';
197             $issue->{'norenew_overdue'} = 1 if $renewerror eq 'overdue';
198             $issue->{'auto_renew'}     = 1 if $renewerror eq 'auto_renew';
199             $issue->{'auto_too_soon'}  = 1 if $renewerror eq 'auto_too_soon';
200
201             if ( $renewerror eq 'too_soon' ) {
202                 $issue->{'too_soon'}         = 1;
203                 $issue->{'soonestrenewdate'} = output_pref(
204                     C4::Circulation::GetSoonestRenewDate(
205                         $issue->{borrowernumber},
206                         $issue->{itemnumber}
207                     )
208                 );
209             }
210         }
211
212         if ( $issue->{'overdue'} ) {
213             push @overdues, $issue;
214             $overdues_count++;
215             $issue->{'overdue'} = 1;
216         }
217         else {
218             $issue->{'issued'} = 1;
219         }
220         # imageurl:
221         my $itemtype = $issue->{'itemtype'};
222         if ( $itemtype ) {
223             $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
224             $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
225         }
226         push @issuedat, $issue;
227         $count++;
228
229         my $isbn = GetNormalizedISBN($issue->{'isbn'});
230         $issue->{normalized_isbn} = $isbn;
231         $issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
232
233                 # My Summary HTML
234                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
235                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
236                     $issue->{title} =~ s/\/+$//; # remove trailing slash
237                     $issue->{title} =~ s/\s+$//; # remove trailing space
238                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
239                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
240                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
241                     $issue->{MySummaryHTML} = $my_summary_html;
242                 }
243     }
244 }
245 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
246 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count);
247 $template->param( ISSUES       => \@issuedat );
248 $template->param( issues_count => $count );
249 $template->param( canrenew     => $canrenew );
250 $template->param( OVERDUES       => \@overdues );
251 $template->param( overdues_count => $overdues_count );
252
253 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
254 if ($show_barcode) {
255     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
256     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
257 }
258 $template->param( show_barcode => 1 ) if $show_barcode;
259
260 # load the branches
261 my $branches = GetBranches();
262 my @branch_loop;
263 for my $branch_hash ( sort keys %{$branches} ) {
264     my $selected;
265     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
266         $selected =
267           ( C4::Context->userenv
268               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
269     }
270     push @branch_loop,
271       { value      => "branch: $branch_hash",
272         branchname => $branches->{$branch_hash}->{'branchname'},
273         selected   => $selected,
274       };
275 }
276 $template->param( branchloop => \@branch_loop );
277
278 # now the reserved items....
279 my $reserves = Koha::Holds->search( { borrowernumber => $borrowernumber } );
280
281 $template->param(
282     RESERVES       => $reserves,
283     showpriority   => $show_priority,
284 );
285
286 # current alert subscriptions
287 my $alerts = getalert($borrowernumber);
288 foreach ( @$alerts ) {
289     $_->{ $_->{type} } = 1;
290     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
291 }
292
293 if (C4::Context->preference('BakerTaylorEnabled')) {
294     $template->param(
295         BakerTaylorEnabled  => 1,
296         BakerTaylorImageURL => &image_url(),
297         BakerTaylorLinkURL  => &link_url(),
298         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
299     );
300 }
301
302 if (C4::Context->preference("OPACAmazonCoverImages") or 
303     C4::Context->preference("GoogleJackets") or
304     C4::Context->preference("BakerTaylorEnabled") or
305     C4::Context->preference("SyndeticsCoverImages")) {
306         $template->param(JacketImages=>1);
307 }
308
309 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
310     $template->param( bor_messages => 1 );
311 }
312
313 if ( $borr->{'opacnote'} ) {
314   $template->param( 
315     bor_messages => 1,
316     opacnote => $borr->{'opacnote'},
317   );
318 }
319
320 $template->param(
321     bor_messages_loop        => GetMessages( $borrowernumber, 'B', 'NONE' ),
322     waiting_count            => $wcount,
323     patronupdate             => $patronupdate,
324     OpacRenewalAllowed       => C4::Context->preference("OpacRenewalAllowed"),
325     userview                 => 1,
326     SuspendHoldsOpac         => C4::Context->preference('SuspendHoldsOpac'),
327     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
328     OpacHoldNotes            => C4::Context->preference('OpacHoldNotes'),
329     failed_holds             => $query->param('failed_holds'),
330 );
331
332 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };