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