Bug 14930 - Leaving OpacFineNoRenewals blank blocks renewals, but should disable...
[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
40 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
41
42 use Date::Calc qw(
43   Today
44   Add_Delta_Days
45   Date_to_Days
46 );
47
48 my $query = new CGI;
49
50 BEGIN {
51     if (C4::Context->preference('BakerTaylorEnabled')) {
52         require C4::External::BakerTaylor;
53         import C4::External::BakerTaylor qw(&image_url &link_url);
54     }
55 }
56
57 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58     {
59         template_name   => "opac-user.tt",
60         query           => $query,
61         type            => "opac",
62         authnotrequired => 0,
63         flagsrequired   => { borrow => 1 },
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 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
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;
112
113 if (  C4::Context->preference( 'OpacRenewalAllowed' ) && $no_renewal_amt && $borr->{amountoutstanding} > $no_renewal_amt ) {
114     $borr->{'flagged'} = 1;
115     $canrenew = 0;
116     $template->param(
117         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
118         renewal_blocked_fines_amountoutstanding => sprintf( '%.02f', $borr->{amountoutstanding} ),
119     );
120 }
121
122 if ( $borr->{'amountoutstanding'} < 0 ) {
123     $borr->{'amountlessthanzero'} = 1;
124     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
125 }
126
127 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
128
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;
140         }
141     }
142 }
143
144 # pass on any renew errors to the template for displaying
145 my $renew_error = $query->param('renew_error');
146
147 $template->param(   BORROWER_INFO     => $borr,
148                     borrowernumber    => $borrowernumber,
149                     patron_flagged    => $borr->{flagged},
150                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
151                     surname           => $borr->{surname},
152                     RENEW_ERROR       => $renew_error,
153                     borrower          => $borr,
154                 );
155
156 #get issued items ....
157
158 my $count          = 0;
159 my $overdues_count = 0;
160 my @overdues;
161 my @issuedat;
162 my $itemtypes = GetItemTypes();
163 my $issues = GetPendingIssues($borrowernumber);
164 if ($issues){
165     foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
166         # check for reserves
167         my $restype = GetReserveStatus( $issue->{'itemnumber'} );
168         if ( $restype ) {
169             $issue->{'reserved'} = 1;
170         }
171
172         my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
173         my $charges = 0;
174         foreach my $ac (@$accts) {
175             if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
176                 $charges += $ac->{'amountoutstanding'}
177                   if $ac->{'accounttype'} eq 'F';
178                 $charges += $ac->{'amountoutstanding'}
179                   if $ac->{'accounttype'} eq 'FU';
180                 $charges += $ac->{'amountoutstanding'}
181                   if $ac->{'accounttype'} eq 'L';
182             }
183         }
184         $issue->{'charges'} = $charges;
185         $issue->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($issue->{'biblionumber'}), GetFrameworkCode($issue->{'biblionumber'}));
186         # check if item is renewable
187         my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
188         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
189         if($status && C4::Context->preference("OpacRenewalAllowed")){
190             $issue->{'status'} = $status;
191         }
192
193         $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
194
195         if ($renewerror) {
196             $issue->{'too_many'}       = 1 if $renewerror eq 'too_many';
197             $issue->{'on_reserve'}     = 1 if $renewerror eq 'on_reserve';
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
232                 # My Summary HTML
233                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
234                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
235                     $issue->{title} =~ s/\/+$//; # remove trailing slash
236                     $issue->{title} =~ s/\s+$//; # remove trailing space
237                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
238                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
239                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
240                     $issue->{MySummaryHTML} = $my_summary_html;
241                 }
242     }
243 }
244 $template->param( ISSUES       => \@issuedat );
245 $template->param( issues_count => $count );
246 $template->param( canrenew     => $canrenew );
247 $template->param( OVERDUES       => \@overdues );
248 $template->param( overdues_count => $overdues_count );
249
250 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
251 if ($show_barcode) {
252     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
253     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
254 }
255 $template->param( show_barcode => 1 ) if $show_barcode;
256
257 # load the branches
258 my $branches = GetBranches();
259 my @branch_loop;
260 for my $branch_hash ( sort keys %{$branches} ) {
261     my $selected;
262     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
263         $selected =
264           ( C4::Context->userenv
265               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
266     }
267     push @branch_loop,
268       { value      => "branch: $branch_hash",
269         branchname => $branches->{$branch_hash}->{'branchname'},
270         selected   => $selected,
271       };
272 }
273 $template->param( branchloop => \@branch_loop );
274
275 # now the reserved items....
276 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
277 foreach my $res (@reserves) {
278
279     if ( $res->{'expirationdate'} eq '0000-00-00' ) {
280       $res->{'expirationdate'} = '';
281     }
282     $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
283     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
284     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
285     my $biblioData = GetBiblioData($res->{'biblionumber'});
286     $res->{'reserves_title'} = $biblioData->{'title'};
287     $res->{'author'} = $biblioData->{'author'};
288
289     if ($show_priority) {
290         $res->{'priority'} ||= '';
291     }
292     $res->{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref") if ( $res->{'suspend_until'} );
293 }
294
295 # use Data::Dumper;
296 # warn Dumper(@reserves);
297
298 $template->param( RESERVES       => \@reserves );
299 $template->param( reserves_count => $#reserves+1 );
300 $template->param( showpriority=>$show_priority );
301
302 my @waiting;
303 my $wcount = 0;
304 foreach my $res (@reserves) {
305     if ( $res->{'itemnumber'} ) {
306         my $item = GetItem( $res->{'itemnumber'});
307         $res->{'holdingbranch'} =
308           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
309         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
310         $res->{'enumchron'} = $item->{'enumchron'} if $item->{'enumchron'};
311         # get document reserve status
312         my $biblioData = GetBiblioData($res->{'biblionumber'});
313         $res->{'waiting_title'} = $biblioData->{'title'};
314         if ( ( $res->{'found'} eq 'W' ) ) {
315             my $item = $res->{'itemnumber'};
316             $item = GetBiblioFromItemNumber($item,undef);
317             $res->{'wait'}= 1;
318             $res->{'holdingbranch'}=$item->{'holdingbranch'};
319             $res->{'biblionumber'}=$item->{'biblionumber'};
320             $res->{'barcode'} = $item->{'barcode'};
321             $res->{'wbrcode'} = $res->{'branchcode'};
322             $res->{'itemnumber'}    = $res->{'itemnumber'};
323             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
324             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
325                 $res->{'atdestination'} = 1;
326             }
327             # set found to 1 if reserve is waiting for patron pickup
328             $res->{'found'} = 1 if $res->{'found'} eq 'W';
329         } else {
330             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
331             if ($transfertwhen) {
332                 $res->{intransit} = 1;
333                 $res->{datesent}   = $transfertwhen;
334                 $res->{frombranch} = GetBranchName($transfertfrom);
335             }
336         }
337         push @waiting, $res;
338         $wcount++;
339     }
340     # can be cancelled
341     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
342     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
343
344 }
345
346 $template->param( WAITING => \@waiting );
347
348 # current alert subscriptions
349 my $alerts = getalert($borrowernumber);
350 foreach ( @$alerts ) {
351     $_->{ $_->{type} } = 1;
352     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
353 }
354
355 if (C4::Context->preference('BakerTaylorEnabled')) {
356     $template->param(
357         BakerTaylorEnabled  => 1,
358         BakerTaylorImageURL => &image_url(),
359         BakerTaylorLinkURL  => &link_url(),
360         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
361     );
362 }
363
364 if (C4::Context->preference("OPACAmazonCoverImages") or 
365     C4::Context->preference("GoogleJackets") or
366     C4::Context->preference("BakerTaylorEnabled") or
367     C4::Context->preference("SyndeticsCoverImages")) {
368         $template->param(JacketImages=>1);
369 }
370
371 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
372     $template->param( bor_messages => 1 );
373 }
374
375 if ( $borr->{'opacnote'} ) {
376   $template->param( 
377     bor_messages => 1,
378     opacnote => $borr->{'opacnote'},
379   );
380 }
381
382 $template->param(
383     bor_messages_loop    => GetMessages( $borrowernumber, 'B', 'NONE' ),
384     waiting_count      => $wcount,
385     patronupdate => $patronupdate,
386     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
387     userview => 1,
388 );
389
390 $template->param(
391     SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
392     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
393     OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
394 );
395
396 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };