Bug 6679 : Tidying changed parts of file
[koha.git] / opac / opac-user.pl
blob03c9e810fbc9a14a7f14bc9758e63530d6b25fc9
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 with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 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::Dates qw/format_date/;
36 use C4::Letters;
37 use C4::Branch; # GetBranches
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 $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
68 my $patronupdate = $query->param('patronupdate');
70 # get borrower information ....
71 my ( $borr ) = GetMemberDetails( $borrowernumber );
73 my ( $today_year, $today_month, $today_day) = Today();
74 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
76 for (qw(dateenrolled dateexpiry dateofbirth)) {
77 ($borr->{$_}) and $borr->{$_} = format_date($borr->{$_});
79 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
81 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
82 $borr->{'flagged'} = 1;
85 if ( $borr->{'amountoutstanding'} > 5 ) {
86 $borr->{'amountoverfive'} = 1;
88 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
89 $borr->{'amountoverzero'} = 1;
91 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
92 $no_renewal_amt ||= 0;
94 if ( $borr->{amountoutstanding} > $no_renewal_amt ) {
95 $borr->{'flagged'} = 1;
96 $template->param(
97 renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
101 if ( $borr->{'amountoutstanding'} < 0 ) {
102 $borr->{'amountlessthanzero'} = 1;
103 $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
106 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
108 my @bordat;
109 $bordat[0] = $borr;
111 # Warningdate is the date that the warning starts appearing
112 if ( C4::Context->preference('NotifyBorrowerDeparture') &&
113 Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
114 Date_to_Days( $today_year, $today_month, $today_day ) )
116 # borrower card soon to expire, warn the borrower
117 $borr->{'warndeparture'} = $borr->{dateexpiry};
118 if (C4::Context->preference('ReturnBeforeExpiry')){
119 $borr->{'returnbeforeexpiry'} = 1;
123 $template->param( BORROWER_INFO => \@bordat,
124 borrowernumber => $borrowernumber,
125 patron_flagged => $borr->{flagged},
126 OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
129 #get issued items ....
131 my $count = 0;
132 my $toggle = 0;
133 my $overdues_count = 0;
134 my @overdues;
135 my @issuedat;
136 my $itemtypes = GetItemTypes();
137 my ($issues) = GetPendingIssues($borrowernumber);
138 my $canrenew = 0;
139 if ($issues){
140 foreach my $issue ( sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues ) {
141 # check for reserves
142 my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
143 if ( $restype ) {
144 $issue->{'reserved'} = 1;
147 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
148 my $charges = 0;
149 foreach my $ac (@$accts) {
150 if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
151 $charges += $ac->{'amountoutstanding'}
152 if $ac->{'accounttype'} eq 'F';
153 $charges += $ac->{'amountoutstanding'}
154 if $ac->{'accounttype'} eq 'L';
157 $issue->{'charges'} = $charges;
159 # get publictype for icon
161 my $publictype = $issue->{'publictype'};
162 $issue->{$publictype} = 1;
164 # check if item is renewable
165 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
166 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
167 if($status && C4::Context->preference("OpacRenewalAllowed")){
168 $issue->{'status'} = $status;
169 $canrenew = 1;
171 $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
172 $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
174 if ( $issue->{'overdue'} ) {
175 push @overdues, $issue;
176 $overdues_count++;
177 $issue->{'overdue'} = 1;
179 else {
180 $issue->{'issued'} = 1;
182 # imageurl:
183 my $itemtype = $issue->{'itemtype'};
184 if ( $itemtype ) {
185 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
186 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
188 $issue->{date_due} = format_date($issue->{date_due});
189 push @issuedat, $issue;
190 $count++;
192 my $isbn = GetNormalizedISBN($issue->{'isbn'});
193 $issue->{normalized_isbn} = $isbn;
195 # My Summary HTML
196 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
197 $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
198 $issue->{title} =~ s/\/+$//; # remove trailing slash
199 $issue->{title} =~ s/\s+$//; # remove trailing space
200 $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
201 $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
202 $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
203 $issue->{MySummaryHTML} = $my_summary_html;
207 $template->param( ISSUES => \@issuedat );
208 $template->param( issues_count => $count );
209 $template->param( canrenew => $canrenew );
210 $template->param( OVERDUES => \@overdues );
211 $template->param( overdues_count => $overdues_count );
213 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
214 if ($show_barcode) {
215 my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
216 undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
218 $template->param( show_barcode => 1 ) if $show_barcode;
220 # load the branches
221 my $branches = GetBranches();
222 my @branch_loop;
223 for my $branch_hash ( sort keys %{$branches} ) {
224 my $selected;
225 if ( C4::Context->preference('SearchMyLibraryFirst') ) {
226 $selected =
227 ( C4::Context->userenv
228 && ( $branch_hash eq C4::Context->userenv->{branch} ) );
230 push @branch_loop,
231 { value => "branch: $branch_hash",
232 branchname => $branches->{$branch_hash}->{'branchname'},
233 selected => $selected,
236 $template->param( branchloop => \@branch_loop );
238 # now the reserved items....
239 my @reserves = GetReservesFromBorrowernumber( $borrowernumber );
240 foreach my $res (@reserves) {
241 $res->{'reservedate'} = format_date( $res->{'reservedate'} );
243 if ( $res->{'expirationdate'} ne '0000-00-00' ) {
244 $res->{'expirationdate'} = format_date( $res->{'expirationdate'} )
245 } else {
246 $res->{'expirationdate'} = '';
249 my $publictype = $res->{'publictype'};
250 $res->{$publictype} = 1;
251 $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
252 $res->{'formattedwaitingdate'} = format_date($res->{'waitingdate'});
253 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
254 my $biblioData = GetBiblioData($res->{'biblionumber'});
255 $res->{'reserves_title'} = $biblioData->{'title'};
256 if ($OPACDisplayRequestPriority) {
257 $res->{'priority'} = '' if $res->{'priority'} eq '0';
261 # use Data::Dumper;
262 # warn Dumper(@reserves);
264 $template->param( RESERVES => \@reserves );
265 $template->param( reserves_count => $#reserves+1 );
266 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
268 my @waiting;
269 my $wcount = 0;
270 foreach my $res (@reserves) {
271 if ( $res->{'itemnumber'} ) {
272 my $item = GetItem( $res->{'itemnumber'});
273 $res->{'holdingbranch'} =
274 $branches->{ $item->{'holdingbranch'} }->{'branchname'};
275 $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
276 # get document reserve status
277 my $biblioData = GetBiblioData($res->{'biblionumber'});
278 $res->{'waiting_title'} = $biblioData->{'title'};
279 if ( ( $res->{'found'} eq 'W' ) ) {
280 my $item = $res->{'itemnumber'};
281 $item = GetBiblioFromItemNumber($item,undef);
282 $res->{'wait'}= 1;
283 $res->{'holdingbranch'}=$item->{'holdingbranch'};
284 $res->{'biblionumber'}=$item->{'biblionumber'};
285 $res->{'barcode'} = $item->{'barcode'};
286 $res->{'wbrcode'} = $res->{'branchcode'};
287 $res->{'itemnumber'} = $res->{'itemnumber'};
288 $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
289 if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
290 $res->{'atdestination'} = 1;
292 # set found to 1 if reserve is waiting for patron pickup
293 $res->{'found'} = 1 if $res->{'found'} eq 'W';
294 } else {
295 my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
296 if ($transfertwhen) {
297 $res->{intransit} = 1;
298 $res->{datesent} = format_date($transfertwhen);
299 $res->{frombranch} = GetBranchName($transfertfrom);
302 push @waiting, $res;
303 $wcount++;
305 # can be cancelled
306 #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
307 $res->{'cancelable'} = 1 if ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
311 $template->param( WAITING => \@waiting );
313 # current alert subscriptions
314 my $alerts = getalert($borrowernumber);
315 foreach ( @$alerts ) {
316 $_->{ $_->{type} } = 1;
317 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
320 if (C4::Context->preference('BakerTaylorEnabled')) {
321 $template->param(
322 BakerTaylorEnabled => 1,
323 BakerTaylorImageURL => &image_url(),
324 BakerTaylorLinkURL => &link_url(),
325 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
329 if (C4::Context->preference("OPACAmazonCoverImages") or
330 C4::Context->preference("GoogleJackets") or
331 C4::Context->preference("BakerTaylorEnabled") or
332 C4::Context->preference("SyndeticsCoverImages")) {
333 $template->param(JacketImages=>1);
336 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
337 $template->param( bor_messages => 1 );
340 if ( $borr->{'opacnote'} ) {
341 $template->param(
342 bor_messages => 1,
343 opacnote => $borr->{'opacnote'},
347 $template->param(
348 bor_messages_loop => GetMessages( $borrowernumber, 'B', 'NONE' ),
349 waiting_count => $wcount,
350 textmessaging => $borr->{textmessaging},
351 patronupdate => $patronupdate,
352 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
353 userview => 1,
354 dateformat => C4::Context->preference("dateformat"),
357 output_html_with_http_headers $query, $cookie, $template->output;