Bug 7413: OPAC bootstrap theme changes
[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 my $patronupdate = $query->param('patronupdate');
72 my $canrenew = 1;
73
74 # get borrower information ....
75 my ( $borr ) = GetMemberDetails( $borrowernumber );
76
77 my (  $today_year,   $today_month,   $today_day) = Today();
78 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
79
80 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
81
82 my $debar = $borr->{'debarred'};
83 my $userdebarred;
84
85 if ($debar) {
86     $userdebarred = 1;
87     $template->param( 'userdebarred' => $userdebarred );
88     if ( $debar ne "9999-12-31" ) {
89         $borr->{'userdebarreddate'} = $debar;
90     }
91 }
92
93 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
94     $borr->{'flagged'} = 1;
95     $canrenew = 0;
96 }
97
98 if ( $borr->{'amountoutstanding'} > 5 ) {
99     $borr->{'amountoverfive'} = 1;
100 }
101 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
102     $borr->{'amountoverzero'} = 1;
103 }
104 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
105 $no_renewal_amt ||= 0;
106
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} ),
113     );
114 }
115
116 if ( $borr->{'amountoutstanding'} < 0 ) {
117     $borr->{'amountlessthanzero'} = 1;
118     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
119 }
120
121 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
122
123 my @bordat;
124 $bordat[0] = $borr;
125
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;
129 }
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;
137         }
138 }
139
140
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},
147                 );
148
149 #get issued items ....
150
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;
163         }
164
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';
175             }
176         }
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;
184         }
185
186         if ($renewerror) {
187             $issue->{'too_many'}   = 1 if $renewerror eq 'too_many';
188             $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
189
190             if ( $renewerror eq 'too_soon' ) {
191                 $issue->{'too_soon'}         = 1;
192                 $issue->{'soonestrenewdate'} = output_pref(
193                     C4::Circulation::GetSoonestRenewDate(
194                         $issue->{borrowernumber},
195                         $issue->{itemnumber}
196                     )
197                 );
198             }
199         }
200
201         if ( $issue->{'overdue'} ) {
202             push @overdues, $issue;
203             $overdues_count++;
204             $issue->{'overdue'} = 1;
205         }
206         else {
207             $issue->{'issued'} = 1;
208         }
209         # imageurl:
210         my $itemtype = $issue->{'itemtype'};
211         if ( $itemtype ) {
212             $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
213             $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
214         }
215         push @issuedat, $issue;
216         $count++;
217
218         my $isbn = GetNormalizedISBN($issue->{'isbn'});
219         $issue->{normalized_isbn} = $isbn;
220
221                 # My Summary HTML
222                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
223                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
224                     $issue->{title} =~ s/\/+$//; # remove trailing slash
225                     $issue->{title} =~ s/\s+$//; # remove trailing space
226                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
227                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
228                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
229                     $issue->{MySummaryHTML} = $my_summary_html;
230                 }
231     }
232 }
233 $template->param( ISSUES       => \@issuedat );
234 $template->param( issues_count => $count );
235 $template->param( canrenew     => $canrenew );
236 $template->param( OVERDUES       => \@overdues );
237 $template->param( overdues_count => $overdues_count );
238
239 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
240 if ($show_barcode) {
241     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
242     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
243 }
244 $template->param( show_barcode => 1 ) if $show_barcode;
245
246 # load the branches
247 my $branches = GetBranches();
248 my @branch_loop;
249 for my $branch_hash ( sort keys %{$branches} ) {
250     my $selected;
251     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
252         $selected =
253           ( C4::Context->userenv
254               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
255     }
256     push @branch_loop,
257       { value      => "branch: $branch_hash",
258         branchname => $branches->{$branch_hash}->{'branchname'},
259         selected   => $selected,
260       };
261 }
262 $template->param( branchloop => \@branch_loop );
263
264 # now the reserved items....
265 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
266 foreach my $res (@reserves) {
267
268     if ( $res->{'expirationdate'} eq '0000-00-00' ) {
269       $res->{'expirationdate'} = '';
270     }
271     $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
272     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
273     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
274     my $biblioData = GetBiblioData($res->{'biblionumber'});
275     $res->{'reserves_title'} = $biblioData->{'title'};
276     if ($show_priority) {
277         $res->{'priority'} ||= '';
278     }
279     $res->{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref") if ( $res->{'suspend_until'} );
280 }
281
282 # use Data::Dumper;
283 # warn Dumper(@reserves);
284
285 $template->param( RESERVES       => \@reserves );
286 $template->param( reserves_count => $#reserves+1 );
287 $template->param( showpriority=>$show_priority );
288
289 my @waiting;
290 my $wcount = 0;
291 foreach my $res (@reserves) {
292     if ( $res->{'itemnumber'} ) {
293         my $item = GetItem( $res->{'itemnumber'});
294         $res->{'holdingbranch'} =
295           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
296         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
297         # get document reserve status
298         my $biblioData = GetBiblioData($res->{'biblionumber'});
299         $res->{'waiting_title'} = $biblioData->{'title'};
300         if ( ( $res->{'found'} eq 'W' ) ) {
301             my $item = $res->{'itemnumber'};
302             $item = GetBiblioFromItemNumber($item,undef);
303             $res->{'wait'}= 1;
304             $res->{'holdingbranch'}=$item->{'holdingbranch'};
305             $res->{'biblionumber'}=$item->{'biblionumber'};
306             $res->{'barcode'} = $item->{'barcode'};
307             $res->{'wbrcode'} = $res->{'branchcode'};
308             $res->{'itemnumber'}    = $res->{'itemnumber'};
309             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
310             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
311                 $res->{'atdestination'} = 1;
312             }
313             # set found to 1 if reserve is waiting for patron pickup
314             $res->{'found'} = 1 if $res->{'found'} eq 'W';
315         } else {
316             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
317             if ($transfertwhen) {
318                 $res->{intransit} = 1;
319                 $res->{datesent}   = $transfertwhen;
320                 $res->{frombranch} = GetBranchName($transfertfrom);
321             }
322         }
323         push @waiting, $res;
324         $wcount++;
325     }
326     # can be cancelled
327     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
328     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
329
330 }
331
332 $template->param( WAITING => \@waiting );
333
334 # current alert subscriptions
335 my $alerts = getalert($borrowernumber);
336 foreach ( @$alerts ) {
337     $_->{ $_->{type} } = 1;
338     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
339 }
340
341 if (C4::Context->preference('BakerTaylorEnabled')) {
342     $template->param(
343         BakerTaylorEnabled  => 1,
344         BakerTaylorImageURL => &image_url(),
345         BakerTaylorLinkURL  => &link_url(),
346         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
347     );
348 }
349
350 if (C4::Context->preference("OPACAmazonCoverImages") or 
351     C4::Context->preference("GoogleJackets") or
352     C4::Context->preference("BakerTaylorEnabled") or
353     C4::Context->preference("SyndeticsCoverImages")) {
354         $template->param(JacketImages=>1);
355 }
356
357 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
358     $template->param( bor_messages => 1 );
359 }
360
361 if ( $borr->{'opacnote'} ) {
362   $template->param( 
363     bor_messages => 1,
364     opacnote => $borr->{'opacnote'},
365   );
366 }
367
368 $template->param(
369     bor_messages_loop    => GetMessages( $borrowernumber, 'B', 'NONE' ),
370     waiting_count      => $wcount,
371     patronupdate => $patronupdate,
372     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
373     userview => 1,
374 );
375
376 $template->param(
377     SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
378     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
379     OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
380 );
381
382 output_html_with_http_headers $query, $cookie, $template->output;
383