Bug 11287: make patron import tool take new restrictions system into account
[koha.git] / opac / opac-user.pl
blobea363b106738c56c1ec89104787cac312d48c027
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 under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 #use warnings; FIXME - Bug 2505
23 use CGI;
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;
39 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
41 use Date::Calc qw(
42 Today
43 Add_Delta_Days
44 Date_to_Days
47 my $query = new CGI;
49 BEGIN {
50 if (C4::Context->preference('BakerTaylorEnabled')) {
51 require C4::External::BakerTaylor;
52 import C4::External::BakerTaylor qw(&image_url &link_url);
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58 template_name => "opac-user.tmpl",
59 query => $query,
60 type => "opac",
61 authnotrequired => 0,
62 flagsrequired => { borrow => 1 },
63 debug => 1,
67 my $show_priority;
68 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
69 m/priority/ and $show_priority = 1;
72 my $patronupdate = $query->param('patronupdate');
73 my $canrenew = 1;
75 # get borrower information ....
76 my ( $borr ) = GetMemberDetails( $borrowernumber );
78 my ( $today_year, $today_month, $today_day) = Today();
79 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
81 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
83 my $debar = $borr->{'debarred'};
84 my $userdebarred;
86 if ($debar) {
87 $userdebarred = 1;
88 $template->param( 'userdebarred' => $userdebarred );
89 if ( $debar ne "9999-12-31" ) {
90 $borr->{'userdebarreddate'} = $debar;
94 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
95 $borr->{'flagged'} = 1;
96 $canrenew = 0;
99 if ( $borr->{'amountoutstanding'} > 5 ) {
100 $borr->{'amountoverfive'} = 1;
102 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
103 $borr->{'amountoverzero'} = 1;
105 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
106 $no_renewal_amt ||= 0;
108 if ( C4::Context->preference( 'OpacRenewalAllowed' ) && $borr->{amountoutstanding} > $no_renewal_amt ) {
109 $borr->{'flagged'} = 1;
110 $canrenew = 0;
111 $template->param(
112 renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
113 renewal_blocked_fines_amountoutstanding => sprintf( '%.02f', $borr->{amountoutstanding} ),
117 if ( $borr->{'amountoutstanding'} < 0 ) {
118 $borr->{'amountlessthanzero'} = 1;
119 $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
122 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
124 my @bordat;
125 $bordat[0] = $borr;
127 # Warningdate is the date that the warning starts appearing
128 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
129 my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
130 if ( $days_to_expiry < 0 ) {
131 #borrower card has expired, warn the borrower
132 $borr->{'warnexpired'} = $borr->{'dateexpiry'};
133 } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
134 # borrower card soon to expire, warn the borrower
135 $borr->{'warndeparture'} = $borr->{dateexpiry};
136 if (C4::Context->preference('ReturnBeforeExpiry')){
137 $borr->{'returnbeforeexpiry'} = 1;
142 # pass on any renew errors to the template for displaying
143 my $renew_error = $query->param('renew_error');
145 $template->param( BORROWER_INFO => \@bordat,
146 borrowernumber => $borrowernumber,
147 patron_flagged => $borr->{flagged},
148 OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
149 surname => $borr->{surname},
150 showname => $borr->{showname},
151 RENEW_ERROR => $renew_error,
152 borrower => $borr,
155 #get issued items ....
157 my $count = 0;
158 my $overdues_count = 0;
159 my @overdues;
160 my @issuedat;
161 my $itemtypes = GetItemTypes();
162 my $issues = GetPendingIssues($borrowernumber);
163 if ($issues){
164 foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
165 # check for reserves
166 my $restype = GetReserveStatus( $issue->{'itemnumber'} );
167 if ( $restype ) {
168 $issue->{'reserved'} = 1;
171 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
172 my $charges = 0;
173 foreach my $ac (@$accts) {
174 if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
175 $charges += $ac->{'amountoutstanding'}
176 if $ac->{'accounttype'} eq 'F';
177 $charges += $ac->{'amountoutstanding'}
178 if $ac->{'accounttype'} eq 'FU';
179 $charges += $ac->{'amountoutstanding'}
180 if $ac->{'accounttype'} eq 'L';
183 $issue->{'charges'} = $charges;
184 $issue->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($issue->{'biblionumber'}), GetFrameworkCode($issue->{'biblionumber'}));
185 # check if item is renewable
186 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
187 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
188 if($status && C4::Context->preference("OpacRenewalAllowed")){
189 $issue->{'status'} = $status;
192 if ($renewerror) {
193 $issue->{'too_many'} = 1 if $renewerror eq 'too_many';
194 $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
196 if ( $renewerror eq 'too_soon' ) {
197 $issue->{'too_soon'} = 1;
198 $issue->{'soonestrenewdate'} = output_pref(
199 C4::Circulation::GetSoonestRenewDate(
200 $issue->{borrowernumber},
201 $issue->{itemnumber}
207 if ( $issue->{'overdue'} ) {
208 push @overdues, $issue;
209 $overdues_count++;
210 $issue->{'overdue'} = 1;
212 else {
213 $issue->{'issued'} = 1;
215 # imageurl:
216 my $itemtype = $issue->{'itemtype'};
217 if ( $itemtype ) {
218 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
219 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
221 push @issuedat, $issue;
222 $count++;
224 my $isbn = GetNormalizedISBN($issue->{'isbn'});
225 $issue->{normalized_isbn} = $isbn;
227 # My Summary HTML
228 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
229 $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
230 $issue->{title} =~ s/\/+$//; # remove trailing slash
231 $issue->{title} =~ s/\s+$//; # remove trailing space
232 $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
233 $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
234 $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
235 $issue->{MySummaryHTML} = $my_summary_html;
239 $template->param( ISSUES => \@issuedat );
240 $template->param( issues_count => $count );
241 $template->param( canrenew => $canrenew );
242 $template->param( OVERDUES => \@overdues );
243 $template->param( overdues_count => $overdues_count );
245 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
246 if ($show_barcode) {
247 my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
248 undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
250 $template->param( show_barcode => 1 ) if $show_barcode;
252 # load the branches
253 my $branches = GetBranches();
254 my @branch_loop;
255 for my $branch_hash ( sort keys %{$branches} ) {
256 my $selected;
257 if ( C4::Context->preference('SearchMyLibraryFirst') ) {
258 $selected =
259 ( C4::Context->userenv
260 && ( $branch_hash eq C4::Context->userenv->{branch} ) );
262 push @branch_loop,
263 { value => "branch: $branch_hash",
264 branchname => $branches->{$branch_hash}->{'branchname'},
265 selected => $selected,
268 $template->param( branchloop => \@branch_loop );
270 # now the reserved items....
271 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
272 foreach my $res (@reserves) {
274 if ( $res->{'expirationdate'} eq '0000-00-00' ) {
275 $res->{'expirationdate'} = '';
277 $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
278 $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
279 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
280 my $biblioData = GetBiblioData($res->{'biblionumber'});
281 $res->{'reserves_title'} = $biblioData->{'title'};
282 if ($show_priority) {
283 $res->{'priority'} ||= '';
285 $res->{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref") if ( $res->{'suspend_until'} );
288 # use Data::Dumper;
289 # warn Dumper(@reserves);
291 $template->param( RESERVES => \@reserves );
292 $template->param( reserves_count => $#reserves+1 );
293 $template->param( showpriority=>$show_priority );
295 my @waiting;
296 my $wcount = 0;
297 foreach my $res (@reserves) {
298 if ( $res->{'itemnumber'} ) {
299 my $item = GetItem( $res->{'itemnumber'});
300 $res->{'holdingbranch'} =
301 $branches->{ $item->{'holdingbranch'} }->{'branchname'};
302 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
303 # get document reserve status
304 my $biblioData = GetBiblioData($res->{'biblionumber'});
305 $res->{'waiting_title'} = $biblioData->{'title'};
306 if ( ( $res->{'found'} eq 'W' ) ) {
307 my $item = $res->{'itemnumber'};
308 $item = GetBiblioFromItemNumber($item,undef);
309 $res->{'wait'}= 1;
310 $res->{'holdingbranch'}=$item->{'holdingbranch'};
311 $res->{'biblionumber'}=$item->{'biblionumber'};
312 $res->{'barcode'} = $item->{'barcode'};
313 $res->{'wbrcode'} = $res->{'branchcode'};
314 $res->{'itemnumber'} = $res->{'itemnumber'};
315 $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
316 if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
317 $res->{'atdestination'} = 1;
319 # set found to 1 if reserve is waiting for patron pickup
320 $res->{'found'} = 1 if $res->{'found'} eq 'W';
321 } else {
322 my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
323 if ($transfertwhen) {
324 $res->{intransit} = 1;
325 $res->{datesent} = $transfertwhen;
326 $res->{frombranch} = GetBranchName($transfertfrom);
329 push @waiting, $res;
330 $wcount++;
332 # can be cancelled
333 #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
334 $res->{'cancelable'} = 1 if ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
338 $template->param( WAITING => \@waiting );
340 # current alert subscriptions
341 my $alerts = getalert($borrowernumber);
342 foreach ( @$alerts ) {
343 $_->{ $_->{type} } = 1;
344 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
347 if (C4::Context->preference('BakerTaylorEnabled')) {
348 $template->param(
349 BakerTaylorEnabled => 1,
350 BakerTaylorImageURL => &image_url(),
351 BakerTaylorLinkURL => &link_url(),
352 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
356 if (C4::Context->preference("OPACAmazonCoverImages") or
357 C4::Context->preference("GoogleJackets") or
358 C4::Context->preference("BakerTaylorEnabled") or
359 C4::Context->preference("SyndeticsCoverImages")) {
360 $template->param(JacketImages=>1);
363 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
364 $template->param( bor_messages => 1 );
367 if ( $borr->{'opacnote'} ) {
368 $template->param(
369 bor_messages => 1,
370 opacnote => $borr->{'opacnote'},
374 $template->param(
375 bor_messages_loop => GetMessages( $borrowernumber, 'B', 'NONE' ),
376 waiting_count => $wcount,
377 patronupdate => $patronupdate,
378 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
379 userview => 1,
382 $template->param(
383 SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
384 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
385 OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
388 output_html_with_http_headers $query, $cookie, $template->output;