Bug 15526: Update DB rev (3.22.04.002)
[koha.git] / opac / opac-user.pl
blobb7a5535ac6dfec278b1440b719340a9658b14bbe
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 C4::Branch; # GetBranches
37 use Koha::DateUtils;
38 use Koha::Borrower::Debarments qw(IsDebarred);
40 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
42 use Scalar::Util qw(looks_like_number);
43 use Date::Calc qw(
44 Today
45 Add_Delta_Days
46 Date_to_Days
49 my $query = new CGI;
51 BEGIN {
52 if (C4::Context->preference('BakerTaylorEnabled')) {
53 require C4::External::BakerTaylor;
54 import C4::External::BakerTaylor qw(&image_url &link_url);
58 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
60 template_name => "opac-user.tt",
61 query => $query,
62 type => "opac",
63 authnotrequired => 0,
64 debug => 1,
68 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
70 my $show_priority;
71 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
72 m/priority/ and $show_priority = 1;
75 my $patronupdate = $query->param('patronupdate');
76 my $canrenew = 1;
78 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
80 # get borrower information ....
81 my ( $borr ) = GetMemberDetails( $borrowernumber );
83 my ( $today_year, $today_month, $today_day) = Today();
84 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
86 my $debar = IsDebarred($borrowernumber);
87 my $userdebarred;
89 if ($debar) {
90 $userdebarred = 1;
91 $template->param( 'userdebarred' => $userdebarred );
92 if ( $debar ne "9999-12-31" ) {
93 $borr->{'userdebarreddate'} = $debar;
97 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
98 $borr->{'flagged'} = 1;
99 $canrenew = 0;
102 if ( $borr->{'amountoutstanding'} > 5 ) {
103 $borr->{'amountoverfive'} = 1;
105 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
106 $borr->{'amountoverzero'} = 1;
108 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
109 $no_renewal_amt = undef unless looks_like_number( $no_renewal_amt );
111 if ( C4::Context->preference('OpacRenewalAllowed')
112 && defined($no_renewal_amt)
113 && $borr->{amountoutstanding} > $no_renewal_amt )
115 $borr->{'flagged'} = 1;
116 $canrenew = 0;
117 $template->param(
118 renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
119 renewal_blocked_fines_amountoutstanding =>
120 sprintf( '%.02f', $borr->{amountoutstanding} ),
124 if ( $borr->{'amountoutstanding'} < 0 ) {
125 $borr->{'amountlessthanzero'} = 1;
126 $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
129 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
131 # Warningdate is the date that the warning starts appearing
132 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
133 my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
134 if ( $days_to_expiry < 0 ) {
135 #borrower card has expired, warn the borrower
136 $borr->{'warnexpired'} = $borr->{'dateexpiry'};
137 } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
138 # borrower card soon to expire, warn the borrower
139 $borr->{'warndeparture'} = $borr->{dateexpiry};
140 if (C4::Context->preference('ReturnBeforeExpiry')){
141 $borr->{'returnbeforeexpiry'} = 1;
146 # pass on any renew errors to the template for displaying
147 my $renew_error = $query->param('renew_error');
149 $template->param( BORROWER_INFO => $borr,
150 borrowernumber => $borrowernumber,
151 patron_flagged => $borr->{flagged},
152 OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
153 surname => $borr->{surname},
154 RENEW_ERROR => $renew_error,
155 borrower => $borr,
158 #get issued items ....
160 my $count = 0;
161 my $overdues_count = 0;
162 my @overdues;
163 my @issuedat;
164 my $itemtypes = GetItemTypes();
165 my $issues = GetPendingIssues($borrowernumber);
166 if ($issues){
167 foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
168 # check for reserves
169 my $restype = GetReserveStatus( $issue->{'itemnumber'} );
170 if ( $restype ) {
171 $issue->{'reserved'} = 1;
174 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
175 my $charges = 0;
176 foreach my $ac (@$accts) {
177 if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
178 $charges += $ac->{'amountoutstanding'}
179 if $ac->{'accounttype'} eq 'F';
180 $charges += $ac->{'amountoutstanding'}
181 if $ac->{'accounttype'} eq 'FU';
182 $charges += $ac->{'amountoutstanding'}
183 if $ac->{'accounttype'} eq 'L';
186 $issue->{'charges'} = $charges;
187 my $marcrecord = GetMarcBiblio( $issue->{'biblionumber'} );
188 $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'}));
189 # check if item is renewable
190 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
191 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
192 if($status && C4::Context->preference("OpacRenewalAllowed")){
193 $issue->{'status'} = $status;
196 $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
198 if ($renewerror) {
199 $issue->{'too_many'} = 1 if $renewerror eq 'too_many';
200 $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
201 $issue->{'norenew_overdue'} = 1 if $renewerror eq 'overdue';
202 $issue->{'auto_renew'} = 1 if $renewerror eq 'auto_renew';
203 $issue->{'auto_too_soon'} = 1 if $renewerror eq 'auto_too_soon';
205 if ( $renewerror eq 'too_soon' ) {
206 $issue->{'too_soon'} = 1;
207 $issue->{'soonestrenewdate'} = output_pref(
208 C4::Circulation::GetSoonestRenewDate(
209 $issue->{borrowernumber},
210 $issue->{itemnumber}
216 if ( $issue->{'overdue'} ) {
217 push @overdues, $issue;
218 $overdues_count++;
219 $issue->{'overdue'} = 1;
221 else {
222 $issue->{'issued'} = 1;
224 # imageurl:
225 my $itemtype = $issue->{'itemtype'};
226 if ( $itemtype ) {
227 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
228 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
230 push @issuedat, $issue;
231 $count++;
233 my $isbn = GetNormalizedISBN($issue->{'isbn'});
234 $issue->{normalized_isbn} = $isbn;
235 $issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
237 # My Summary HTML
238 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
239 $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
240 $issue->{title} =~ s/\/+$//; # remove trailing slash
241 $issue->{title} =~ s/\s+$//; # remove trailing space
242 $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
243 $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
244 $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
245 $issue->{MySummaryHTML} = $my_summary_html;
249 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
250 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count);
251 $template->param( ISSUES => \@issuedat );
252 $template->param( issues_count => $count );
253 $template->param( canrenew => $canrenew );
254 $template->param( OVERDUES => \@overdues );
255 $template->param( overdues_count => $overdues_count );
257 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
258 if ($show_barcode) {
259 my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
260 undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
262 $template->param( show_barcode => 1 ) if $show_barcode;
264 # load the branches
265 my $branches = GetBranches();
266 my @branch_loop;
267 for my $branch_hash ( sort keys %{$branches} ) {
268 my $selected;
269 if ( C4::Context->preference('SearchMyLibraryFirst') ) {
270 $selected =
271 ( C4::Context->userenv
272 && ( $branch_hash eq C4::Context->userenv->{branch} ) );
274 push @branch_loop,
275 { value => "branch: $branch_hash",
276 branchname => $branches->{$branch_hash}->{'branchname'},
277 selected => $selected,
280 $template->param( branchloop => \@branch_loop );
282 # now the reserved items....
283 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
284 foreach my $res (@reserves) {
286 if ( $res->{'expirationdate'} eq '0000-00-00' ) {
287 $res->{'expirationdate'} = '';
289 $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
290 $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
291 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
292 my $biblioData = GetBiblioData($res->{'biblionumber'});
293 $res->{'reserves_title'} = $biblioData->{'title'};
294 $res->{'author'} = $biblioData->{'author'};
296 if ($show_priority) {
297 $res->{'priority'} ||= '';
299 if ( $res->{'suspend_until'} ) {
300 $res->{'suspend_until'} = output_pref({ dt => dt_from_string( $res->{'suspend_until'} , 'iso' ), dateonly => 1 });
304 # use Data::Dumper;
305 # warn Dumper(@reserves);
307 $template->param( RESERVES => \@reserves );
308 $template->param( reserves_count => $#reserves+1 );
309 $template->param( showpriority=>$show_priority );
311 my @waiting;
312 my $wcount = 0;
313 foreach my $res (@reserves) {
314 if ( $res->{'itemnumber'} ) {
315 my $item = GetItem( $res->{'itemnumber'});
316 $res->{'holdingbranch'} =
317 $branches->{ $item->{'holdingbranch'} }->{'branchname'};
318 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
319 $res->{'enumchron'} = $item->{'enumchron'} if $item->{'enumchron'};
320 # get document reserve status
321 my $biblioData = GetBiblioData($res->{'biblionumber'});
322 $res->{'waiting_title'} = $biblioData->{'title'};
323 if ( ( $res->{'found'} eq 'W' ) ) {
324 my $item = $res->{'itemnumber'};
325 $item = GetBiblioFromItemNumber($item,undef);
326 $res->{'wait'}= 1;
327 $res->{'holdingbranch'}=$item->{'holdingbranch'};
328 $res->{'biblionumber'}=$item->{'biblionumber'};
329 $res->{'barcode'} = $item->{'barcode'};
330 $res->{'wbrcode'} = $res->{'branchcode'};
331 $res->{'itemnumber'} = $res->{'itemnumber'};
332 $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
333 if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
334 $res->{'atdestination'} = 1;
336 # set found to 1 if reserve is waiting for patron pickup
337 $res->{'found'} = 1 if $res->{'found'} eq 'W';
338 } else {
339 my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
340 if ($transfertwhen) {
341 $res->{intransit} = 1;
342 $res->{datesent} = $transfertwhen;
343 $res->{frombranch} = GetBranchName($transfertfrom);
346 push @waiting, $res;
347 $wcount++;
349 # can be cancelled
350 #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
351 $res->{'cancelable'} = 1 if ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
355 $template->param( WAITING => \@waiting );
357 # current alert subscriptions
358 my $alerts = getalert($borrowernumber);
359 foreach ( @$alerts ) {
360 $_->{ $_->{type} } = 1;
361 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
364 if (C4::Context->preference('BakerTaylorEnabled')) {
365 $template->param(
366 BakerTaylorEnabled => 1,
367 BakerTaylorImageURL => &image_url(),
368 BakerTaylorLinkURL => &link_url(),
369 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
373 if (C4::Context->preference("OPACAmazonCoverImages") or
374 C4::Context->preference("GoogleJackets") or
375 C4::Context->preference("BakerTaylorEnabled") or
376 C4::Context->preference("SyndeticsCoverImages")) {
377 $template->param(JacketImages=>1);
380 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
381 $template->param( bor_messages => 1 );
384 if ( $borr->{'opacnote'} ) {
385 $template->param(
386 bor_messages => 1,
387 opacnote => $borr->{'opacnote'},
391 $template->param(
392 bor_messages_loop => GetMessages( $borrowernumber, 'B', 'NONE' ),
393 waiting_count => $wcount,
394 patronupdate => $patronupdate,
395 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
396 userview => 1,
397 SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
398 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
399 OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
400 failed_holds => $query->param('failed_holds'),
403 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };