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