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