Fix release notes typo
[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 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.
10 #
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.
14 #
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.
18
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use CGI;
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 C4::Branch; # GetBranches
37 use Koha::DateUtils;
38
39 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
40
41 use Date::Calc qw(
42   Today
43   Add_Delta_Days
44   Date_to_Days
45 );
46
47 my $query = new CGI;
48
49 BEGIN {
50     if (C4::Context->preference('BakerTaylorEnabled')) {
51         require C4::External::BakerTaylor;
52         import C4::External::BakerTaylor qw(&image_url &link_url);
53     }
54 }
55
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57     {
58         template_name   => "opac-user.tmpl",
59         query           => $query,
60         type            => "opac",
61         authnotrequired => 0,
62         flagsrequired   => { borrow => 1 },
63         debug           => 1,
64     }
65 );
66
67 my $show_priority;
68 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
69     m/priority/ and $show_priority = 1;
70 }
71
72 my $patronupdate = $query->param('patronupdate');
73 my $canrenew = 1;
74
75 # get borrower information ....
76 my ( $borr ) = GetMemberDetails( $borrowernumber );
77
78 my (  $today_year,   $today_month,   $today_day) = Today();
79 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
80
81 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
82
83 my $debar = $borr->{'debarred'};
84 my $userdebarred;
85
86 if ($debar) {
87     $userdebarred = 1;
88     $template->param( 'userdebarred' => $userdebarred );
89     if ( $debar ne "9999-12-31" ) {
90         $borr->{'userdebarreddate'} = $debar;
91     }
92 }
93
94 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
95     $borr->{'flagged'} = 1;
96     $canrenew = 0;
97 }
98
99 if ( $borr->{'amountoutstanding'} > 5 ) {
100     $borr->{'amountoverfive'} = 1;
101 }
102 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
103     $borr->{'amountoverzero'} = 1;
104 }
105 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
106 $no_renewal_amt ||= 0;
107
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} ),
114     );
115 }
116
117 if ( $borr->{'amountoutstanding'} < 0 ) {
118     $borr->{'amountlessthanzero'} = 1;
119     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
120 }
121
122 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
123
124 my @bordat;
125 $bordat[0] = $borr;
126
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;
138         }
139     }
140 }
141
142 # pass on any renew errors to the template for displaying
143 my $renew_error = $query->param('renew_error');
144
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,
153                 );
154
155 #get issued items ....
156
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;
169         }
170
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';
181             }
182         }
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;
190         }
191
192         if ($renewerror) {
193             $issue->{'too_many'}   = 1 if $renewerror eq 'too_many';
194             $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
195
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}
202                     )
203                 );
204             }
205         }
206
207         if ( $issue->{'overdue'} ) {
208             push @overdues, $issue;
209             $overdues_count++;
210             $issue->{'overdue'} = 1;
211         }
212         else {
213             $issue->{'issued'} = 1;
214         }
215         # imageurl:
216         my $itemtype = $issue->{'itemtype'};
217         if ( $itemtype ) {
218             $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
219             $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
220         }
221         push @issuedat, $issue;
222         $count++;
223
224         my $isbn = GetNormalizedISBN($issue->{'isbn'});
225         $issue->{normalized_isbn} = $isbn;
226
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;
236                 }
237     }
238 }
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 );
244
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;
249 }
250 $template->param( show_barcode => 1 ) if $show_barcode;
251
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} ) );
261     }
262     push @branch_loop,
263       { value      => "branch: $branch_hash",
264         branchname => $branches->{$branch_hash}->{'branchname'},
265         selected   => $selected,
266       };
267 }
268 $template->param( branchloop => \@branch_loop );
269
270 # now the reserved items....
271 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
272 foreach my $res (@reserves) {
273
274     if ( $res->{'expirationdate'} eq '0000-00-00' ) {
275       $res->{'expirationdate'} = '';
276     }
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     $res->{'author'} = $biblioData->{'author'};
283
284     if ($show_priority) {
285         $res->{'priority'} ||= '';
286     }
287     $res->{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref") if ( $res->{'suspend_until'} );
288 }
289
290 # use Data::Dumper;
291 # warn Dumper(@reserves);
292
293 $template->param( RESERVES       => \@reserves );
294 $template->param( reserves_count => $#reserves+1 );
295 $template->param( showpriority=>$show_priority );
296
297 my @waiting;
298 my $wcount = 0;
299 foreach my $res (@reserves) {
300     if ( $res->{'itemnumber'} ) {
301         my $item = GetItem( $res->{'itemnumber'});
302         $res->{'holdingbranch'} =
303           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
304         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
305         # get document reserve status
306         my $biblioData = GetBiblioData($res->{'biblionumber'});
307         $res->{'waiting_title'} = $biblioData->{'title'};
308         if ( ( $res->{'found'} eq 'W' ) ) {
309             my $item = $res->{'itemnumber'};
310             $item = GetBiblioFromItemNumber($item,undef);
311             $res->{'wait'}= 1;
312             $res->{'holdingbranch'}=$item->{'holdingbranch'};
313             $res->{'biblionumber'}=$item->{'biblionumber'};
314             $res->{'barcode'} = $item->{'barcode'};
315             $res->{'wbrcode'} = $res->{'branchcode'};
316             $res->{'itemnumber'}    = $res->{'itemnumber'};
317             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
318             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
319                 $res->{'atdestination'} = 1;
320             }
321             # set found to 1 if reserve is waiting for patron pickup
322             $res->{'found'} = 1 if $res->{'found'} eq 'W';
323         } else {
324             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
325             if ($transfertwhen) {
326                 $res->{intransit} = 1;
327                 $res->{datesent}   = $transfertwhen;
328                 $res->{frombranch} = GetBranchName($transfertfrom);
329             }
330         }
331         push @waiting, $res;
332         $wcount++;
333     }
334     # can be cancelled
335     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
336     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
337
338 }
339
340 $template->param( WAITING => \@waiting );
341
342 # current alert subscriptions
343 my $alerts = getalert($borrowernumber);
344 foreach ( @$alerts ) {
345     $_->{ $_->{type} } = 1;
346     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
347 }
348
349 if (C4::Context->preference('BakerTaylorEnabled')) {
350     $template->param(
351         BakerTaylorEnabled  => 1,
352         BakerTaylorImageURL => &image_url(),
353         BakerTaylorLinkURL  => &link_url(),
354         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
355     );
356 }
357
358 if (C4::Context->preference("OPACAmazonCoverImages") or 
359     C4::Context->preference("GoogleJackets") or
360     C4::Context->preference("BakerTaylorEnabled") or
361     C4::Context->preference("SyndeticsCoverImages")) {
362         $template->param(JacketImages=>1);
363 }
364
365 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
366     $template->param( bor_messages => 1 );
367 }
368
369 if ( $borr->{'opacnote'} ) {
370   $template->param( 
371     bor_messages => 1,
372     opacnote => $borr->{'opacnote'},
373   );
374 }
375
376 $template->param(
377     bor_messages_loop    => GetMessages( $borrowernumber, 'B', 'NONE' ),
378     waiting_count      => $wcount,
379     patronupdate => $patronupdate,
380     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
381     userview => 1,
382 );
383
384 $template->param(
385     SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
386     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
387     OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
388 );
389
390 output_html_with_http_headers $query, $cookie, $template->output;
391