Bug 17722: Make PatronLists.t run inside a transaction
[koha.git] / opac / opac-user.pl
blob84ee6604dd4fba8afd756bf50ffe0f2d827d3026
1 #!/usr/bin/perl
3 # This file is part of Koha.
4 # parts copyright 2010 BibLibre
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.
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.
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>.
20 use strict;
21 #use warnings; FIXME - Bug 2505
23 use CGI qw ( -utf8 );
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 Koha::DateUtils;
37 use Koha::Holds;
38 use Koha::Database;
39 use Koha::Patron::Messages;
40 use Koha::Patron::Discharge;
41 use Koha::Patrons;
43 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
45 use Scalar::Util qw(looks_like_number);
46 use Date::Calc qw(
47 Today
48 Add_Delta_Days
49 Date_to_Days
52 my $query = new CGI;
54 BEGIN {
55 if (C4::Context->preference('BakerTaylorEnabled')) {
56 require C4::External::BakerTaylor;
57 import C4::External::BakerTaylor qw(&image_url &link_url);
61 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
63 template_name => "opac-user.tt",
64 query => $query,
65 type => "opac",
66 authnotrequired => 0,
67 debug => 1,
71 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
73 my $show_priority;
74 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
75 m/priority/ and $show_priority = 1;
78 my $patronupdate = $query->param('patronupdate');
79 my $canrenew = 1;
81 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
83 if (!$borrowernumber) {
84 $template->param( adminWarning => 1 );
87 # get borrower information ....
88 my ( $borr ) = GetMemberDetails( $borrowernumber );
90 my ( $today_year, $today_month, $today_day) = Today();
91 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
93 my $debar = Koha::Patrons->find( $borrowernumber )->is_debarred;
94 my $userdebarred;
96 if ($debar) {
97 $userdebarred = 1;
98 $template->param( 'userdebarred' => $userdebarred );
99 if ( $debar ne "9999-12-31" ) {
100 $borr->{'userdebarreddate'} = $debar;
102 # FIXME looks like $available is not needed
103 # If a patron is discharged he has a validated discharge available
104 my $available = Koha::Patron::Discharge::count({
105 borrowernumber => $borrowernumber,
106 validated => 1,
108 $template->param( 'discharge_available' => $available && Koha::Patron::Discharge::is_discharged({borrowernumber => $borrowernumber}) );
111 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
112 $borr->{'flagged'} = 1;
113 $canrenew = 0;
116 if ( $borr->{'amountoutstanding'} > 5 ) {
117 $borr->{'amountoverfive'} = 1;
119 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
120 $borr->{'amountoverzero'} = 1;
122 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
123 $no_renewal_amt = undef unless looks_like_number( $no_renewal_amt );
125 if ( C4::Context->preference('OpacRenewalAllowed')
126 && defined($no_renewal_amt)
127 && $borr->{amountoutstanding} > $no_renewal_amt )
129 $borr->{'flagged'} = 1;
130 $canrenew = 0;
131 $template->param(
132 renewal_blocked_fines => $no_renewal_amt,
133 renewal_blocked_fines_amountoutstanding => $borr->{amountoutstanding},
137 if ( $borr->{'amountoutstanding'} < 0 ) {
138 $borr->{'amountlessthanzero'} = 1;
139 $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
142 # Warningdate is the date that the warning starts appearing
143 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
144 my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
145 if ( $days_to_expiry < 0 ) {
146 #borrower card has expired, warn the borrower
147 $borr->{'warnexpired'} = $borr->{'dateexpiry'};
148 } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
149 # borrower card soon to expire, warn the borrower
150 $borr->{'warndeparture'} = $borr->{dateexpiry};
151 if (C4::Context->preference('ReturnBeforeExpiry')){
152 $borr->{'returnbeforeexpiry'} = 1;
157 # pass on any renew errors to the template for displaying
158 my $renew_error = $query->param('renew_error');
160 $template->param( BORROWER_INFO => $borr,
161 borrowernumber => $borrowernumber,
162 patron_flagged => $borr->{flagged},
163 OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
164 surname => $borr->{surname},
165 RENEW_ERROR => $renew_error,
166 borrower => $borr,
169 #get issued items ....
171 my $count = 0;
172 my $overdues_count = 0;
173 my @overdues;
174 my @issuedat;
175 my $itemtypes = GetItemTypes();
176 my $issues = GetPendingIssues($borrowernumber);
177 if ($issues){
178 foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
179 # check for reserves
180 my $restype = GetReserveStatus( $issue->{'itemnumber'} );
181 if ( $restype ) {
182 $issue->{'reserved'} = 1;
185 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
186 my $charges = 0;
187 foreach my $ac (@$accts) {
188 if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
189 $charges += $ac->{'amountoutstanding'}
190 if $ac->{'accounttype'} eq 'F';
191 $charges += $ac->{'amountoutstanding'}
192 if $ac->{'accounttype'} eq 'FU';
193 $charges += $ac->{'amountoutstanding'}
194 if $ac->{'accounttype'} eq 'L';
197 $issue->{'charges'} = $charges;
198 my $marcrecord = GetMarcBiblio( $issue->{'biblionumber'} );
199 $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'}));
200 # check if item is renewable
201 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
202 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
203 if($status && C4::Context->preference("OpacRenewalAllowed")){
204 $issue->{'status'} = $status;
207 $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
209 if ($renewerror) {
210 $issue->{'too_many'} = 1 if $renewerror eq 'too_many';
211 $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
212 $issue->{'norenew_overdue'} = 1 if $renewerror eq 'overdue';
213 $issue->{'auto_renew'} = 1 if $renewerror eq 'auto_renew';
214 $issue->{'auto_too_soon'} = 1 if $renewerror eq 'auto_too_soon';
215 $issue->{'auto_too_late'} = 1 if $renewerror eq 'auto_too_late';
217 if ( $renewerror eq 'too_soon' ) {
218 $issue->{'too_soon'} = 1;
219 $issue->{'soonestrenewdate'} = output_pref(
220 C4::Circulation::GetSoonestRenewDate(
221 $issue->{borrowernumber},
222 $issue->{itemnumber}
228 if ( $issue->{'overdue'} ) {
229 push @overdues, $issue;
230 $overdues_count++;
231 $issue->{'overdue'} = 1;
233 else {
234 $issue->{'issued'} = 1;
236 # imageurl:
237 my $itemtype = $issue->{'itemtype'};
238 if ( $itemtype ) {
239 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
240 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
242 push @issuedat, $issue;
243 $count++;
245 my $isbn = GetNormalizedISBN($issue->{'isbn'});
246 $issue->{normalized_isbn} = $isbn;
247 $issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
249 # My Summary HTML
250 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
251 $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
252 $issue->{title} =~ s/\/+$//; # remove trailing slash
253 $issue->{title} =~ s/\s+$//; # remove trailing space
254 $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
255 $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
256 $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
257 $issue->{MySummaryHTML} = $my_summary_html;
261 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
262 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count);
263 $template->param( ISSUES => \@issuedat );
264 $template->param( issues_count => $count );
265 $template->param( canrenew => $canrenew );
266 $template->param( OVERDUES => \@overdues );
267 $template->param( overdues_count => $overdues_count );
269 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
270 if ($show_barcode) {
271 my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
272 undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
274 $template->param( show_barcode => 1 ) if $show_barcode;
276 # now the reserved items....
277 my $reserves = Koha::Holds->search( { borrowernumber => $borrowernumber } );
279 $template->param(
280 RESERVES => $reserves,
281 showpriority => $show_priority,
284 # current alert subscriptions
285 my $alerts = getalert($borrowernumber);
286 foreach ( @$alerts ) {
287 $_->{ $_->{type} } = 1;
288 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
291 if (C4::Context->preference('BakerTaylorEnabled')) {
292 $template->param(
293 BakerTaylorEnabled => 1,
294 BakerTaylorImageURL => &image_url(),
295 BakerTaylorLinkURL => &link_url(),
296 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
300 if (C4::Context->preference("OPACAmazonCoverImages") or
301 C4::Context->preference("GoogleJackets") or
302 C4::Context->preference("BakerTaylorEnabled") or
303 C4::Context->preference("SyndeticsCoverImages")) {
304 $template->param(JacketImages=>1);
307 my $patron_messages = Koha::Patron::Messages->search(
309 borrowernumber => $borrowernumber,
310 message_type => 'B',
314 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor')
315 || C4::Context->preference('AllowStaffToSetCheckoutsVisibilityForGuarantor') )
317 my @relatives =
318 Koha::Database->new()->schema()->resultset("Borrower")->search(
320 privacy_guarantor_checkouts => 1,
321 'me.guarantorid' => $borrowernumber
323 { prefetch => [ { 'issues' => { 'item' => 'biblio' } } ] }
325 $template->param( relatives => \@relatives );
328 $template->param(
329 borrower => Koha::Patrons->find($borrowernumber),
330 patron_messages => $patron_messages,
331 opacnote => $borr->{opacnote},
332 patronupdate => $patronupdate,
333 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
334 userview => 1,
335 SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
336 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
337 OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
338 failed_holds => scalar $query->param('failed_holds'),
341 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };