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