Bug 18336: DBIC Schema changes
[koha.git] / opac / opac-user.pl
blob55c04a3a7874deb37d5f09d930ce8aa9b491315f
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::Libraries;
37 use Koha::DateUtils;
38 use Koha::Holds;
39 use Koha::Database;
40 use Koha::ItemTypes;
41 use Koha::Patron::Attribute::Types;
42 use Koha::Patron::Messages;
43 use Koha::Patron::Discharge;
44 use Koha::Patrons;
46 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
48 use Scalar::Util qw(looks_like_number);
49 use Date::Calc qw(
50 Today
51 Add_Delta_Days
52 Date_to_Days
55 my $query = new CGI;
57 BEGIN {
58 if (C4::Context->preference('BakerTaylorEnabled')) {
59 require C4::External::BakerTaylor;
60 import C4::External::BakerTaylor qw(&image_url &link_url);
64 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
66 template_name => "opac-user.tt",
67 query => $query,
68 type => "opac",
69 authnotrequired => 0,
70 debug => 1,
74 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
76 my $show_priority;
77 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
78 m/priority/ and $show_priority = 1;
81 my $patronupdate = $query->param('patronupdate');
82 my $canrenew = 1;
84 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
86 if (!$borrowernumber) {
87 $template->param( adminWarning => 1 );
90 # get borrower information ....
91 my $borr = Koha::Patrons->find( $borrowernumber )->unblessed;
93 my ( $today_year, $today_month, $today_day) = Today();
94 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
96 my $debar = Koha::Patrons->find( $borrowernumber )->is_debarred;
97 my $userdebarred;
99 if ($debar) {
100 $userdebarred = 1;
101 $template->param( 'userdebarred' => $userdebarred );
102 if ( $debar ne "9999-12-31" ) {
103 $borr->{'userdebarreddate'} = $debar;
105 # FIXME looks like $available is not needed
106 # If a user is discharged they have a validated discharge available
107 my $available = Koha::Patron::Discharge::count({
108 borrowernumber => $borrowernumber,
109 validated => 1,
111 $template->param( 'discharge_available' => $available && Koha::Patron::Discharge::is_discharged({borrowernumber => $borrowernumber}) );
114 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
115 $borr->{'flagged'} = 1;
116 $canrenew = 0;
119 my ( $amountoutstanding ) = GetMemberAccountRecords($borrowernumber);
120 if ( $amountoutstanding > 5 ) {
121 $borr->{'amountoverfive'} = 1;
123 if ( 5 >= $amountoutstanding && $amountoutstanding > 0 ) {
124 $borr->{'amountoverzero'} = 1;
126 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
127 $no_renewal_amt = undef unless looks_like_number( $no_renewal_amt );
129 if ( C4::Context->preference('OpacRenewalAllowed')
130 && defined($no_renewal_amt)
131 && $amountoutstanding > $no_renewal_amt )
133 $borr->{'flagged'} = 1;
134 $canrenew = 0;
135 $template->param(
136 renewal_blocked_fines => $no_renewal_amt,
137 renewal_blocked_fines_amountoutstanding => $amountoutstanding,
141 if ( $amountoutstanding < 0 ) {
142 $borr->{'amountlessthanzero'} = 1;
143 $amountoutstanding = -1 * ( $amountoutstanding );
146 # Warningdate is the date that the warning starts appearing
147 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
148 my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
149 if ( $days_to_expiry < 0 ) {
150 #borrower card has expired, warn the borrower
151 $borr->{'warnexpired'} = $borr->{'dateexpiry'};
152 } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
153 # borrower card soon to expire, warn the borrower
154 $borr->{'warndeparture'} = $borr->{dateexpiry};
155 if (C4::Context->preference('ReturnBeforeExpiry')){
156 $borr->{'returnbeforeexpiry'} = 1;
161 # pass on any renew errors to the template for displaying
162 my $renew_error = $query->param('renew_error');
164 $template->param( BORROWER_INFO => $borr,
165 amountoutstanding => $amountoutstanding,
166 borrowernumber => $borrowernumber,
167 patron_flagged => $borr->{flagged},
168 OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
169 surname => $borr->{surname},
170 RENEW_ERROR => $renew_error,
171 borrower => $borr,
174 #get issued items ....
176 my $count = 0;
177 my $overdues_count = 0;
178 my @overdues;
179 my @issuedat;
180 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
181 my $issues = GetPendingIssues($borrowernumber);
182 if ($issues){
183 foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
184 # check for reserves
185 my $restype = GetReserveStatus( $issue->{'itemnumber'} );
186 if ( $restype ) {
187 $issue->{'reserved'} = 1;
190 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
191 my $charges = 0;
192 my $rentalfines = 0;
193 foreach my $ac (@$accts) {
194 if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
195 $charges += $ac->{'amountoutstanding'}
196 if $ac->{'accounttype'} eq 'F';
197 $charges += $ac->{'amountoutstanding'}
198 if $ac->{'accounttype'} eq 'FU';
199 $charges += $ac->{'amountoutstanding'}
200 if $ac->{'accounttype'} eq 'L';
201 $rentalfines += $ac->{'amountoutstanding'}
202 if $ac->{'accounttype'} eq 'Rent';
205 $issue->{'charges'} = $charges;
206 $issue->{'rentalfines'} = $rentalfines;
207 my $marcrecord = GetMarcBiblio({ biblionumber => $issue->{'biblionumber'} });
208 $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'}));
209 # check if item is renewable
210 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
211 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
212 ( $issue->{'renewalfee'}, $issue->{'renewalitemtype'} ) = GetIssuingCharges( $issue->{'itemnumber'}, $borrowernumber );
213 if($status && C4::Context->preference("OpacRenewalAllowed")){
214 $issue->{'status'} = $status;
217 $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
219 if ($renewerror) {
220 $issue->{'too_many'} = 1 if $renewerror eq 'too_many';
221 $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
222 $issue->{'norenew_overdue'} = 1 if $renewerror eq 'overdue';
223 $issue->{'auto_renew'} = 1 if $renewerror eq 'auto_renew';
224 $issue->{'auto_too_soon'} = 1 if $renewerror eq 'auto_too_soon';
225 $issue->{'auto_too_late'} = 1 if $renewerror eq 'auto_too_late';
226 $issue->{'auto_too_much_oweing'} = 1 if $renewerror eq 'auto_too_much_oweing';
228 if ( $renewerror eq 'too_soon' ) {
229 $issue->{'too_soon'} = 1;
230 $issue->{'soonestrenewdate'} = output_pref(
231 C4::Circulation::GetSoonestRenewDate(
232 $issue->{borrowernumber},
233 $issue->{itemnumber}
239 if ( $issue->{'overdue'} ) {
240 push @overdues, $issue;
241 $overdues_count++;
242 $issue->{'overdue'} = 1;
244 else {
245 $issue->{'issued'} = 1;
247 # imageurl:
248 my $itemtype = $issue->{'itemtype'};
249 if ( $itemtype ) {
250 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
251 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
253 push @issuedat, $issue;
254 $count++;
256 my $isbn = GetNormalizedISBN($issue->{'isbn'});
257 $issue->{normalized_isbn} = $isbn;
258 $issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
260 # My Summary HTML
261 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
262 $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
263 $issue->{title} =~ s/\/+$//; # remove trailing slash
264 $issue->{title} =~ s/\s+$//; # remove trailing space
265 $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
266 $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
267 $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
268 $issue->{MySummaryHTML} = $my_summary_html;
272 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
273 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count);
275 $template->param( ISSUES => \@issuedat );
276 $template->param( issues_count => $count );
277 $template->param( canrenew => $canrenew );
278 $template->param( OVERDUES => \@overdues );
279 $template->param( overdues_count => $overdues_count );
281 my $show_barcode = Koha::Patron::Attribute::Types->search(
282 { code => ATTRIBUTE_SHOW_BARCODE } )->count;
283 if ($show_barcode) {
284 my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
285 undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
287 $template->param( show_barcode => 1 ) if $show_barcode;
289 # now the reserved items....
290 my $reserves = Koha::Holds->search( { borrowernumber => $borrowernumber } );
292 $template->param(
293 RESERVES => $reserves,
294 showpriority => $show_priority,
297 # current alert subscriptions
298 my $alerts = getalert($borrowernumber);
299 foreach ( @$alerts ) {
300 $_->{ $_->{type} } = 1;
301 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
304 if (C4::Context->preference('BakerTaylorEnabled')) {
305 $template->param(
306 BakerTaylorEnabled => 1,
307 BakerTaylorImageURL => &image_url(),
308 BakerTaylorLinkURL => &link_url(),
309 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
313 if (C4::Context->preference("OPACAmazonCoverImages") or
314 C4::Context->preference("GoogleJackets") or
315 C4::Context->preference("BakerTaylorEnabled") or
316 C4::Context->preference("SyndeticsCoverImages")) {
317 $template->param(JacketImages=>1);
320 $template->param(
321 OverDriveCirculation => C4::Context->preference('OverDriveCirculation') || 0,
322 overdrive_error => scalar $query->param('overdrive_error') || undef,
323 overdrive_tab => scalar $query->param('overdrive_tab') || 0,
326 my $patron_messages = Koha::Patron::Messages->search(
328 borrowernumber => $borrowernumber,
329 message_type => 'B',
333 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor')
334 || C4::Context->preference('AllowStaffToSetCheckoutsVisibilityForGuarantor') )
336 my @relatives =
337 Koha::Database->new()->schema()->resultset("Borrower")->search(
339 privacy_guarantor_checkouts => 1,
340 'me.guarantorid' => $borrowernumber
342 { prefetch => [ { 'issues' => { 'item' => 'biblio' } } ] }
344 $template->param( relatives => \@relatives );
347 $template->param(
348 borrower => scalar Koha::Patrons->find($borrowernumber),
349 patron_messages => $patron_messages,
350 opacnote => $borr->{opacnote},
351 patronupdate => $patronupdate,
352 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
353 userview => 1,
354 SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
355 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
356 OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
357 failed_holds => scalar $query->param('failed_holds'),
360 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };