Bug 11032: Check a valid MARC::Record passed to Biblio
[koha.git] / opac / opac-user.pl
blob02002a3bef997964ad9ab5320517e0b023090ad7
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;
71 my $patronupdate = $query->param('patronupdate');
72 my $canrenew = 1;
74 # get borrower information ....
75 my ( $borr ) = GetMemberDetails( $borrowernumber );
77 my ( $today_year, $today_month, $today_day) = Today();
78 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
80 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
82 my $debar = $borr->{'debarred'};
83 my $userdebarred;
85 if ($debar) {
86 $userdebarred = 1;
87 $template->param( 'userdebarred' => $userdebarred );
88 if ( $debar ne "9999-12-31" ) {
89 $borr->{'userdebarreddate'} = $debar;
93 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
94 $borr->{'flagged'} = 1;
95 $canrenew = 0;
98 if ( $borr->{'amountoutstanding'} > 5 ) {
99 $borr->{'amountoverfive'} = 1;
101 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
102 $borr->{'amountoverzero'} = 1;
104 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
105 $no_renewal_amt ||= 0;
107 if ( C4::Context->preference( 'OpacRenewalAllowed' ) && $borr->{amountoutstanding} > $no_renewal_amt ) {
108 $borr->{'flagged'} = 1;
109 $canrenew = 0;
110 $template->param(
111 renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
112 renewal_blocked_fines_amountoutstanding => sprintf( '%.02f', $borr->{amountoutstanding} ),
116 if ( $borr->{'amountoutstanding'} < 0 ) {
117 $borr->{'amountlessthanzero'} = 1;
118 $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
121 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
123 my @bordat;
124 $bordat[0] = $borr;
126 # Warningdate is the date that the warning starts appearing
127 if ( $borr->{dateexpiry} && Date_to_Days( $today_year, $today_month, $today_day ) > Date_to_Days( $warning_year, $warning_month, $warning_day ) ) {
128 $borr->{'warnexpired'} = 1;
130 elsif ( $borr->{dateexpiry} && C4::Context->preference('NotifyBorrowerDeparture') &&
131 Date_to_Days(Add_Delta_Days($warning_year, $warning_month, $warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
132 Date_to_Days( $today_year, $today_month, $today_day ) ) {
133 # borrower card soon to expire, warn the borrower
134 $borr->{'warndeparture'} = $borr->{dateexpiry};
135 if (C4::Context->preference('ReturnBeforeExpiry')){
136 $borr->{'returnbeforeexpiry'} = 1;
141 $template->param( BORROWER_INFO => \@bordat,
142 borrowernumber => $borrowernumber,
143 patron_flagged => $borr->{flagged},
144 OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
145 surname => $borr->{surname},
146 showname => $borr->{showname},
149 #get issued items ....
151 my $count = 0;
152 my $overdues_count = 0;
153 my @overdues;
154 my @issuedat;
155 my $itemtypes = GetItemTypes();
156 my $issues = GetPendingIssues($borrowernumber);
157 if ($issues){
158 foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
159 # check for reserves
160 my $restype = GetReserveStatus( $issue->{'itemnumber'} );
161 if ( $restype ) {
162 $issue->{'reserved'} = 1;
165 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
166 my $charges = 0;
167 foreach my $ac (@$accts) {
168 if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
169 $charges += $ac->{'amountoutstanding'}
170 if $ac->{'accounttype'} eq 'F';
171 $charges += $ac->{'amountoutstanding'}
172 if $ac->{'accounttype'} eq 'FU';
173 $charges += $ac->{'amountoutstanding'}
174 if $ac->{'accounttype'} eq 'L';
177 $issue->{'charges'} = $charges;
178 $issue->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($issue->{'biblionumber'}), GetFrameworkCode($issue->{'biblionumber'}));
179 # check if item is renewable
180 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
181 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
182 if($status && C4::Context->preference("OpacRenewalAllowed")){
183 $issue->{'status'} = $status;
185 $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
186 $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
188 if ( $issue->{'overdue'} ) {
189 push @overdues, $issue;
190 $overdues_count++;
191 $issue->{'overdue'} = 1;
193 else {
194 $issue->{'issued'} = 1;
196 # imageurl:
197 my $itemtype = $issue->{'itemtype'};
198 if ( $itemtype ) {
199 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
200 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
202 push @issuedat, $issue;
203 $count++;
205 my $isbn = GetNormalizedISBN($issue->{'isbn'});
206 $issue->{normalized_isbn} = $isbn;
208 # My Summary HTML
209 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
210 $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
211 $issue->{title} =~ s/\/+$//; # remove trailing slash
212 $issue->{title} =~ s/\s+$//; # remove trailing space
213 $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
214 $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
215 $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
216 $issue->{MySummaryHTML} = $my_summary_html;
220 $template->param( ISSUES => \@issuedat );
221 $template->param( issues_count => $count );
222 $template->param( canrenew => $canrenew );
223 $template->param( OVERDUES => \@overdues );
224 $template->param( overdues_count => $overdues_count );
226 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
227 if ($show_barcode) {
228 my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
229 undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
231 $template->param( show_barcode => 1 ) if $show_barcode;
233 # load the branches
234 my $branches = GetBranches();
235 my @branch_loop;
236 for my $branch_hash ( sort keys %{$branches} ) {
237 my $selected;
238 if ( C4::Context->preference('SearchMyLibraryFirst') ) {
239 $selected =
240 ( C4::Context->userenv
241 && ( $branch_hash eq C4::Context->userenv->{branch} ) );
243 push @branch_loop,
244 { value => "branch: $branch_hash",
245 branchname => $branches->{$branch_hash}->{'branchname'},
246 selected => $selected,
249 $template->param( branchloop => \@branch_loop );
251 # now the reserved items....
252 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
253 foreach my $res (@reserves) {
255 if ( $res->{'expirationdate'} eq '0000-00-00' ) {
256 $res->{'expirationdate'} = '';
258 $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
259 $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
260 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
261 my $biblioData = GetBiblioData($res->{'biblionumber'});
262 $res->{'reserves_title'} = $biblioData->{'title'};
263 if ($show_priority) {
264 $res->{'priority'} ||= '';
266 $res->{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref") if ( $res->{'suspend_until'} );
269 # use Data::Dumper;
270 # warn Dumper(@reserves);
272 $template->param( RESERVES => \@reserves );
273 $template->param( reserves_count => $#reserves+1 );
274 $template->param( showpriority=>$show_priority );
276 my @waiting;
277 my $wcount = 0;
278 foreach my $res (@reserves) {
279 if ( $res->{'itemnumber'} ) {
280 my $item = GetItem( $res->{'itemnumber'});
281 $res->{'holdingbranch'} =
282 $branches->{ $item->{'holdingbranch'} }->{'branchname'};
283 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
284 # get document reserve status
285 my $biblioData = GetBiblioData($res->{'biblionumber'});
286 $res->{'waiting_title'} = $biblioData->{'title'};
287 if ( ( $res->{'found'} eq 'W' ) ) {
288 my $item = $res->{'itemnumber'};
289 $item = GetBiblioFromItemNumber($item,undef);
290 $res->{'wait'}= 1;
291 $res->{'holdingbranch'}=$item->{'holdingbranch'};
292 $res->{'biblionumber'}=$item->{'biblionumber'};
293 $res->{'barcode'} = $item->{'barcode'};
294 $res->{'wbrcode'} = $res->{'branchcode'};
295 $res->{'itemnumber'} = $res->{'itemnumber'};
296 $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
297 if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
298 $res->{'atdestination'} = 1;
300 # set found to 1 if reserve is waiting for patron pickup
301 $res->{'found'} = 1 if $res->{'found'} eq 'W';
302 } else {
303 my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
304 if ($transfertwhen) {
305 $res->{intransit} = 1;
306 $res->{datesent} = $transfertwhen;
307 $res->{frombranch} = GetBranchName($transfertfrom);
310 push @waiting, $res;
311 $wcount++;
313 # can be cancelled
314 #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
315 $res->{'cancelable'} = 1 if ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
319 $template->param( WAITING => \@waiting );
321 # current alert subscriptions
322 my $alerts = getalert($borrowernumber);
323 foreach ( @$alerts ) {
324 $_->{ $_->{type} } = 1;
325 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
328 if (C4::Context->preference('BakerTaylorEnabled')) {
329 $template->param(
330 BakerTaylorEnabled => 1,
331 BakerTaylorImageURL => &image_url(),
332 BakerTaylorLinkURL => &link_url(),
333 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
337 if (C4::Context->preference("OPACAmazonCoverImages") or
338 C4::Context->preference("GoogleJackets") or
339 C4::Context->preference("BakerTaylorEnabled") or
340 C4::Context->preference("SyndeticsCoverImages")) {
341 $template->param(JacketImages=>1);
344 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
345 $template->param( bor_messages => 1 );
348 if ( $borr->{'opacnote'} ) {
349 $template->param(
350 bor_messages => 1,
351 opacnote => $borr->{'opacnote'},
355 $template->param(
356 bor_messages_loop => GetMessages( $borrowernumber, 'B', 'NONE' ),
357 waiting_count => $wcount,
358 patronupdate => $patronupdate,
359 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
360 userview => 1,
363 $template->param(
364 SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
365 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
366 OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
369 output_html_with_http_headers $query, $cookie, $template->output;