Bug 15344: Remove some other calls of GetMemberDetails from pl scripts
[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         debug           => 1,
64     }
65 );
66
67 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
68
69 my $show_priority;
70 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
71     m/priority/ and $show_priority = 1;
72 }
73
74 my $patronupdate = $query->param('patronupdate');
75 my $canrenew = 1;
76
77 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
78
79 # get borrower information ....
80 my ( $borr ) = GetMemberDetails( $borrowernumber );
81
82 my (  $today_year,   $today_month,   $today_day) = Today();
83 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
84
85 my $debar = IsDebarred($borrowernumber);
86 my $userdebarred;
87
88 if ($debar) {
89     $userdebarred = 1;
90     $template->param( 'userdebarred' => $userdebarred );
91     if ( $debar ne "9999-12-31" ) {
92         $borr->{'userdebarreddate'} = $debar;
93     }
94 }
95
96 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
97     $borr->{'flagged'} = 1;
98     $canrenew = 0;
99 }
100
101 if ( $borr->{'amountoutstanding'} > 5 ) {
102     $borr->{'amountoverfive'} = 1;
103 }
104 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
105     $borr->{'amountoverzero'} = 1;
106 }
107 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
108 $no_renewal_amt ||= 0;
109
110 if (  C4::Context->preference( 'OpacRenewalAllowed' ) && $borr->{amountoutstanding} > $no_renewal_amt ) {
111     $borr->{'flagged'} = 1;
112     $canrenew = 0;
113     $template->param(
114         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
115         renewal_blocked_fines_amountoutstanding => sprintf( '%.02f', $borr->{amountoutstanding} ),
116     );
117 }
118
119 if ( $borr->{'amountoutstanding'} < 0 ) {
120     $borr->{'amountlessthanzero'} = 1;
121     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
122 }
123
124 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
125
126 # Warningdate is the date that the warning starts appearing
127 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
128     my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
129     if ( $days_to_expiry < 0 ) {
130         #borrower card has expired, warn the borrower
131         $borr->{'warnexpired'} = $borr->{'dateexpiry'};
132     } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
133         # borrower card soon to expire, warn the borrower
134         $borr->{'warndeparture'} = $borr->{dateexpiry};
135         if (C4::Context->preference('ReturnBeforeExpiry')){
136             $borr->{'returnbeforeexpiry'} = 1;
137         }
138     }
139 }
140
141 # pass on any renew errors to the template for displaying
142 my $renew_error = $query->param('renew_error');
143
144 $template->param(   BORROWER_INFO     => $borr,
145                     borrowernumber    => $borrowernumber,
146                     patron_flagged    => $borr->{flagged},
147                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
148                     surname           => $borr->{surname},
149                     RENEW_ERROR       => $renew_error,
150                     borrower          => $borr,
151                 );
152
153 #get issued items ....
154
155 my $count          = 0;
156 my $overdues_count = 0;
157 my @overdues;
158 my @issuedat;
159 my $itemtypes = GetItemTypes();
160 my $issues = GetPendingIssues($borrowernumber);
161 if ($issues){
162     foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
163         # check for reserves
164         my $restype = GetReserveStatus( $issue->{'itemnumber'} );
165         if ( $restype ) {
166             $issue->{'reserved'} = 1;
167         }
168
169         my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
170         my $charges = 0;
171         foreach my $ac (@$accts) {
172             if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
173                 $charges += $ac->{'amountoutstanding'}
174                   if $ac->{'accounttype'} eq 'F';
175                 $charges += $ac->{'amountoutstanding'}
176                   if $ac->{'accounttype'} eq 'FU';
177                 $charges += $ac->{'amountoutstanding'}
178                   if $ac->{'accounttype'} eq 'L';
179             }
180         }
181         $issue->{'charges'} = $charges;
182         my $marcrecord = GetMarcBiblio( $issue->{'biblionumber'} );
183         $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'}));
184         # check if item is renewable
185         my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
186         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
187         if($status && C4::Context->preference("OpacRenewalAllowed")){
188             $issue->{'status'} = $status;
189         }
190
191         $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
192
193         if ($renewerror) {
194             $issue->{'too_many'}       = 1 if $renewerror eq 'too_many';
195             $issue->{'on_reserve'}     = 1 if $renewerror eq 'on_reserve';
196             $issue->{'norenew_overdue'} = 1 if $renewerror eq 'overdue';
197             $issue->{'auto_renew'}     = 1 if $renewerror eq 'auto_renew';
198             $issue->{'auto_too_soon'}  = 1 if $renewerror eq 'auto_too_soon';
199
200             if ( $renewerror eq 'too_soon' ) {
201                 $issue->{'too_soon'}         = 1;
202                 $issue->{'soonestrenewdate'} = output_pref(
203                     C4::Circulation::GetSoonestRenewDate(
204                         $issue->{borrowernumber},
205                         $issue->{itemnumber}
206                     )
207                 );
208             }
209         }
210
211         if ( $issue->{'overdue'} ) {
212             push @overdues, $issue;
213             $overdues_count++;
214             $issue->{'overdue'} = 1;
215         }
216         else {
217             $issue->{'issued'} = 1;
218         }
219         # imageurl:
220         my $itemtype = $issue->{'itemtype'};
221         if ( $itemtype ) {
222             $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
223             $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
224         }
225         push @issuedat, $issue;
226         $count++;
227
228         my $isbn = GetNormalizedISBN($issue->{'isbn'});
229         $issue->{normalized_isbn} = $isbn;
230         $issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
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 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
245 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count);
246 $template->param( ISSUES       => \@issuedat );
247 $template->param( issues_count => $count );
248 $template->param( canrenew     => $canrenew );
249 $template->param( OVERDUES       => \@overdues );
250 $template->param( overdues_count => $overdues_count );
251
252 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
253 if ($show_barcode) {
254     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
255     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
256 }
257 $template->param( show_barcode => 1 ) if $show_barcode;
258
259 # load the branches
260 my $branches = GetBranches();
261 my @branch_loop;
262 for my $branch_hash ( sort keys %{$branches} ) {
263     my $selected;
264     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
265         $selected =
266           ( C4::Context->userenv
267               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
268     }
269     push @branch_loop,
270       { value      => "branch: $branch_hash",
271         branchname => $branches->{$branch_hash}->{'branchname'},
272         selected   => $selected,
273       };
274 }
275 $template->param( branchloop => \@branch_loop );
276
277 # now the reserved items....
278 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
279 foreach my $res (@reserves) {
280
281     if ( $res->{'expirationdate'} eq '0000-00-00' ) {
282       $res->{'expirationdate'} = '';
283     }
284     $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
285     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
286     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
287     my $biblioData = GetBiblioData($res->{'biblionumber'});
288     $res->{'reserves_title'} = $biblioData->{'title'};
289     $res->{'author'} = $biblioData->{'author'};
290
291     if ($show_priority) {
292         $res->{'priority'} ||= '';
293     }
294     if ( $res->{'suspend_until'} ) {
295         $res->{'suspend_until'} = output_pref({ dt => dt_from_string( $res->{'suspend_until'} , 'iso' ), dateonly => 1 });
296     }
297 }
298
299 # use Data::Dumper;
300 # warn Dumper(@reserves);
301
302 $template->param( RESERVES       => \@reserves );
303 $template->param( reserves_count => $#reserves+1 );
304 $template->param( showpriority=>$show_priority );
305
306 my @waiting;
307 my $wcount = 0;
308 foreach my $res (@reserves) {
309     if ( $res->{'itemnumber'} ) {
310         my $item = GetItem( $res->{'itemnumber'});
311         $res->{'holdingbranch'} =
312           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
313         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
314         $res->{'enumchron'} = $item->{'enumchron'} if $item->{'enumchron'};
315         # get document reserve status
316         my $biblioData = GetBiblioData($res->{'biblionumber'});
317         $res->{'waiting_title'} = $biblioData->{'title'};
318         if ( ( $res->{'found'} eq 'W' ) ) {
319             my $item = $res->{'itemnumber'};
320             $item = GetBiblioFromItemNumber($item,undef);
321             $res->{'wait'}= 1;
322             $res->{'holdingbranch'}=$item->{'holdingbranch'};
323             $res->{'biblionumber'}=$item->{'biblionumber'};
324             $res->{'barcode'} = $item->{'barcode'};
325             $res->{'wbrcode'} = $res->{'branchcode'};
326             $res->{'itemnumber'}    = $res->{'itemnumber'};
327             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
328             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
329                 $res->{'atdestination'} = 1;
330             }
331             # set found to 1 if reserve is waiting for patron pickup
332             $res->{'found'} = 1 if $res->{'found'} eq 'W';
333         } else {
334             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
335             if ($transfertwhen) {
336                 $res->{intransit} = 1;
337                 $res->{datesent}   = $transfertwhen;
338                 $res->{frombranch} = GetBranchName($transfertfrom);
339             }
340         }
341         push @waiting, $res;
342         $wcount++;
343     }
344     # can be cancelled
345     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
346     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
347
348 }
349
350 $template->param( WAITING => \@waiting );
351
352 # current alert subscriptions
353 my $alerts = getalert($borrowernumber);
354 foreach ( @$alerts ) {
355     $_->{ $_->{type} } = 1;
356     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
357 }
358
359 if (C4::Context->preference('BakerTaylorEnabled')) {
360     $template->param(
361         BakerTaylorEnabled  => 1,
362         BakerTaylorImageURL => &image_url(),
363         BakerTaylorLinkURL  => &link_url(),
364         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
365     );
366 }
367
368 if (C4::Context->preference("OPACAmazonCoverImages") or 
369     C4::Context->preference("GoogleJackets") or
370     C4::Context->preference("BakerTaylorEnabled") or
371     C4::Context->preference("SyndeticsCoverImages")) {
372         $template->param(JacketImages=>1);
373 }
374
375 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
376     $template->param( bor_messages => 1 );
377 }
378
379 if ( $borr->{'opacnote'} ) {
380   $template->param( 
381     bor_messages => 1,
382     opacnote => $borr->{'opacnote'},
383   );
384 }
385
386 $template->param(
387     bor_messages_loop        => GetMessages( $borrowernumber, 'B', 'NONE' ),
388     waiting_count            => $wcount,
389     patronupdate             => $patronupdate,
390     OpacRenewalAllowed       => C4::Context->preference("OpacRenewalAllowed"),
391     userview                 => 1,
392     SuspendHoldsOpac         => C4::Context->preference('SuspendHoldsOpac'),
393     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
394     OpacHoldNotes            => C4::Context->preference('OpacHoldNotes'),
395     failed_holds             => $query->param('failed_holds'),
396 );
397
398 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };