Merge branch 'bug_9105' into 3.12-master
[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::Overdues qw/CheckBorrowerDebarred/;
34 use C4::Biblio;
35 use C4::Items;
36 use C4::Letters;
37 use C4::Branch; # GetBranches
38 use Koha::DateUtils;
39
40 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
41
42 use Date::Calc qw(
43   Today
44   Add_Delta_Days
45   Date_to_Days
46 );
47
48 my $query = new CGI;
49
50 BEGIN {
51     if (C4::Context->preference('BakerTaylorEnabled')) {
52         require C4::External::BakerTaylor;
53         import C4::External::BakerTaylor qw(&image_url &link_url);
54     }
55 }
56
57 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58     {
59         template_name   => "opac-user.tmpl",
60         query           => $query,
61         type            => "opac",
62         authnotrequired => 0,
63         flagsrequired   => { borrow => 1 },
64         debug           => 1,
65     }
66 );
67
68 my $show_priority;
69 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
70     m/priority/ and $show_priority = 1;
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 = CheckBorrowerDebarred($borrowernumber);
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 ( $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} && Date_to_Days( $today_year, $today_month, $today_day ) > Date_to_Days( $warning_year, $warning_month, $warning_day ) ) {
129     $borr->{'warnexpired'} = 1;
130 }
131 elsif ( $borr->{dateexpiry} && C4::Context->preference('NotifyBorrowerDeparture') &&
132         Date_to_Days(Add_Delta_Days($warning_year, $warning_month, $warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
133         Date_to_Days( $today_year, $today_month, $today_day ) ) {
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 $template->param(   BORROWER_INFO     => \@bordat,
143                     borrowernumber    => $borrowernumber,
144                     patron_flagged    => $borr->{flagged},
145                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
146                     surname           => $borr->{surname},
147                     showname          => $borr->{showname},
148                 );
149
150 #get issued items ....
151
152 my $count          = 0;
153 my $overdues_count = 0;
154 my @overdues;
155 my @issuedat;
156 my $itemtypes = GetItemTypes();
157 my $issues = GetPendingIssues($borrowernumber);
158 if ($issues){
159     foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
160         # check for reserves
161         my ( $restype, $res, undef ) = CheckReserves( $issue->{'itemnumber'} );
162         if ( $restype ) {
163             $issue->{'reserved'} = 1;
164         }
165
166         my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
167         my $charges = 0;
168         foreach my $ac (@$accts) {
169             if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
170                 $charges += $ac->{'amountoutstanding'}
171                   if $ac->{'accounttype'} eq 'F';
172                 $charges += $ac->{'amountoutstanding'}
173                   if $ac->{'accounttype'} eq 'L';
174             }
175         }
176         $issue->{'charges'} = $charges;
177
178         # check if item is renewable
179         my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
180         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
181         if($status && C4::Context->preference("OpacRenewalAllowed")){
182             $issue->{'status'} = $status;
183         }
184         $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
185         $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
186
187         if ( $issue->{'overdue'} ) {
188             push @overdues, $issue;
189             $overdues_count++;
190             $issue->{'overdue'} = 1;
191         }
192         else {
193             $issue->{'issued'} = 1;
194         }
195         # imageurl:
196         my $itemtype = $issue->{'itemtype'};
197         if ( $itemtype ) {
198             $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
199             $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
200         }
201         push @issuedat, $issue;
202         $count++;
203
204         my $isbn = GetNormalizedISBN($issue->{'isbn'});
205         $issue->{normalized_isbn} = $isbn;
206
207                 # My Summary HTML
208                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
209                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
210                     $issue->{title} =~ s/\/+$//; # remove trailing slash
211                     $issue->{title} =~ s/\s+$//; # remove trailing space
212                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
213                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
214                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
215                     $issue->{MySummaryHTML} = $my_summary_html;
216                 }
217     }
218 }
219 $template->param( ISSUES       => \@issuedat );
220 $template->param( issues_count => $count );
221 $template->param( canrenew     => $canrenew );
222 $template->param( OVERDUES       => \@overdues );
223 $template->param( overdues_count => $overdues_count );
224
225 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
226 if ($show_barcode) {
227     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
228     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
229 }
230 $template->param( show_barcode => 1 ) if $show_barcode;
231
232 # load the branches
233 my $branches = GetBranches();
234 my @branch_loop;
235 for my $branch_hash ( sort keys %{$branches} ) {
236     my $selected;
237     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
238         $selected =
239           ( C4::Context->userenv
240               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
241     }
242     push @branch_loop,
243       { value      => "branch: $branch_hash",
244         branchname => $branches->{$branch_hash}->{'branchname'},
245         selected   => $selected,
246       };
247 }
248 $template->param( branchloop => \@branch_loop );
249
250 # now the reserved items....
251 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
252 foreach my $res (@reserves) {
253
254     if ( $res->{'expirationdate'} eq '0000-00-00' ) {
255       $res->{'expirationdate'} = '';
256     }
257
258     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
259     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
260     my $biblioData = GetBiblioData($res->{'biblionumber'});
261     $res->{'reserves_title'} = $biblioData->{'title'};
262     if ($show_priority) {
263         $res->{'priority'} ||= '';
264     }
265     $res->{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref") if ( $res->{'suspend_until'} );
266 }
267
268 # use Data::Dumper;
269 # warn Dumper(@reserves);
270
271 $template->param( RESERVES       => \@reserves );
272 $template->param( reserves_count => $#reserves+1 );
273 $template->param( showpriority=>$show_priority );
274
275 my @waiting;
276 my $wcount = 0;
277 foreach my $res (@reserves) {
278     if ( $res->{'itemnumber'} ) {
279         my $item = GetItem( $res->{'itemnumber'});
280         $res->{'holdingbranch'} =
281           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
282         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
283         # get document reserve status
284         my $biblioData = GetBiblioData($res->{'biblionumber'});
285         $res->{'waiting_title'} = $biblioData->{'title'};
286         if ( ( $res->{'found'} eq 'W' ) ) {
287             my $item = $res->{'itemnumber'};
288             $item = GetBiblioFromItemNumber($item,undef);
289             $res->{'wait'}= 1;
290             $res->{'holdingbranch'}=$item->{'holdingbranch'};
291             $res->{'biblionumber'}=$item->{'biblionumber'};
292             $res->{'barcode'} = $item->{'barcode'};
293             $res->{'wbrcode'} = $res->{'branchcode'};
294             $res->{'itemnumber'}    = $res->{'itemnumber'};
295             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
296             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
297                 $res->{'atdestination'} = 1;
298             }
299             # set found to 1 if reserve is waiting for patron pickup
300             $res->{'found'} = 1 if $res->{'found'} eq 'W';
301         } else {
302             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
303             if ($transfertwhen) {
304                 $res->{intransit} = 1;
305                 $res->{datesent}   = $transfertwhen;
306                 $res->{frombranch} = GetBranchName($transfertfrom);
307             }
308         }
309         push @waiting, $res;
310         $wcount++;
311     }
312     # can be cancelled
313     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
314     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
315
316 }
317
318 $template->param( WAITING => \@waiting );
319
320 # current alert subscriptions
321 my $alerts = getalert($borrowernumber);
322 foreach ( @$alerts ) {
323     $_->{ $_->{type} } = 1;
324     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
325 }
326
327 if (C4::Context->preference('BakerTaylorEnabled')) {
328     $template->param(
329         BakerTaylorEnabled  => 1,
330         BakerTaylorImageURL => &image_url(),
331         BakerTaylorLinkURL  => &link_url(),
332         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
333     );
334 }
335
336 if (C4::Context->preference("OPACAmazonCoverImages") or 
337     C4::Context->preference("GoogleJackets") or
338     C4::Context->preference("BakerTaylorEnabled") or
339     C4::Context->preference("SyndeticsCoverImages")) {
340         $template->param(JacketImages=>1);
341 }
342
343 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
344     $template->param( bor_messages => 1 );
345 }
346
347 if ( $borr->{'opacnote'} ) {
348   $template->param( 
349     bor_messages => 1,
350     opacnote => $borr->{'opacnote'},
351   );
352 }
353
354 $template->param(
355     bor_messages_loop    => GetMessages( $borrowernumber, 'B', 'NONE' ),
356     waiting_count      => $wcount,
357     patronupdate => $patronupdate,
358     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
359     userview => 1,
360 );
361
362 $template->param(
363     SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
364     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
365 );
366
367 output_html_with_http_headers $query, $cookie, $template->output;
368