Bug 12545: Add EditorConfig.org file to the source tree
[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.tt",
59         query           => $query,
60         type            => "opac",
61         authnotrequired => 0,
62         flagsrequired   => { borrow => 1 },
63         debug           => 1,
64     }
65 );
66
67 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
68
69 my $show_priority;
70 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
71     m/priority/ and $show_priority = 1;
72 }
73
74 my $patronupdate = $query->param('patronupdate');
75 my $canrenew = 1;
76
77 # get borrower information ....
78 my ( $borr ) = GetMemberDetails( $borrowernumber );
79
80 my (  $today_year,   $today_month,   $today_day) = Today();
81 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
82
83 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
84
85 my $debar = $borr->{'debarred'};
86 my $userdebarred;
87
88 if ($debar) {
89     $userdebarred = 1;
90     $template->param( 'userdebarred' => $userdebarred );
91     if ( $debar ne "9999-12-31" ) {
92         $borr->{'userdebarreddate'} = $debar;
93     }
94 }
95
96 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
97     $borr->{'flagged'} = 1;
98     $canrenew = 0;
99 }
100
101 if ( $borr->{'amountoutstanding'} > 5 ) {
102     $borr->{'amountoverfive'} = 1;
103 }
104 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
105     $borr->{'amountoverzero'} = 1;
106 }
107 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
108 $no_renewal_amt ||= 0;
109
110 if (  C4::Context->preference( 'OpacRenewalAllowed' ) && $borr->{amountoutstanding} > $no_renewal_amt ) {
111     $borr->{'flagged'} = 1;
112     $canrenew = 0;
113     $template->param(
114         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
115         renewal_blocked_fines_amountoutstanding => sprintf( '%.02f', $borr->{amountoutstanding} ),
116     );
117 }
118
119 if ( $borr->{'amountoutstanding'} < 0 ) {
120     $borr->{'amountlessthanzero'} = 1;
121     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
122 }
123
124 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
125
126 my @bordat;
127 $bordat[0] = $borr;
128
129 # Warningdate is the date that the warning starts appearing
130 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
131     my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
132     if ( $days_to_expiry < 0 ) {
133         #borrower card has expired, warn the borrower
134         $borr->{'warnexpired'} = $borr->{'dateexpiry'};
135     } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
136         # borrower card soon to expire, warn the borrower
137         $borr->{'warndeparture'} = $borr->{dateexpiry};
138         if (C4::Context->preference('ReturnBeforeExpiry')){
139             $borr->{'returnbeforeexpiry'} = 1;
140         }
141     }
142 }
143
144 # pass on any renew errors to the template for displaying
145 my $renew_error = $query->param('renew_error');
146
147 $template->param(   BORROWER_INFO     => \@bordat,
148                     borrowernumber    => $borrowernumber,
149                     patron_flagged    => $borr->{flagged},
150                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
151                     surname           => $borr->{surname},
152                     showname          => $borr->{showname},
153                     RENEW_ERROR       => $renew_error,
154                     borrower          => $borr,
155                 );
156
157 #get issued items ....
158
159 my $count          = 0;
160 my $overdues_count = 0;
161 my @overdues;
162 my @issuedat;
163 my $itemtypes = GetItemTypes();
164 my $issues = GetPendingIssues($borrowernumber);
165 if ($issues){
166     foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
167         # check for reserves
168         my $restype = GetReserveStatus( $issue->{'itemnumber'} );
169         if ( $restype ) {
170             $issue->{'reserved'} = 1;
171         }
172
173         my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
174         my $charges = 0;
175         foreach my $ac (@$accts) {
176             if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
177                 $charges += $ac->{'amountoutstanding'}
178                   if $ac->{'accounttype'} eq 'F';
179                 $charges += $ac->{'amountoutstanding'}
180                   if $ac->{'accounttype'} eq 'FU';
181                 $charges += $ac->{'amountoutstanding'}
182                   if $ac->{'accounttype'} eq 'L';
183             }
184         }
185         $issue->{'charges'} = $charges;
186         $issue->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($issue->{'biblionumber'}), GetFrameworkCode($issue->{'biblionumber'}));
187         # check if item is renewable
188         my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
189         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
190         if($status && C4::Context->preference("OpacRenewalAllowed")){
191             $issue->{'status'} = $status;
192         }
193
194         $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
195
196         if ($renewerror) {
197             $issue->{'too_many'}   = 1 if $renewerror eq 'too_many';
198             $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
199
200             if ( $renewerror eq 'too_soon' ) {
201                 $issue->{'too_soon'}         = 1;
202                 $issue->{'soonestrenewdate'} = output_pref(
203                     C4::Circulation::GetSoonestRenewDate(
204                         $issue->{borrowernumber},
205                         $issue->{itemnumber}
206                     )
207                 );
208             }
209         }
210
211         if ( $issue->{'overdue'} ) {
212             push @overdues, $issue;
213             $overdues_count++;
214             $issue->{'overdue'} = 1;
215         }
216         else {
217             $issue->{'issued'} = 1;
218         }
219         # imageurl:
220         my $itemtype = $issue->{'itemtype'};
221         if ( $itemtype ) {
222             $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
223             $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
224         }
225         push @issuedat, $issue;
226         $count++;
227
228         my $isbn = GetNormalizedISBN($issue->{'isbn'});
229         $issue->{normalized_isbn} = $isbn;
230
231                 # My Summary HTML
232                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
233                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
234                     $issue->{title} =~ s/\/+$//; # remove trailing slash
235                     $issue->{title} =~ s/\s+$//; # remove trailing space
236                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
237                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
238                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
239                     $issue->{MySummaryHTML} = $my_summary_html;
240                 }
241     }
242 }
243 $template->param( ISSUES       => \@issuedat );
244 $template->param( issues_count => $count );
245 $template->param( canrenew     => $canrenew );
246 $template->param( OVERDUES       => \@overdues );
247 $template->param( overdues_count => $overdues_count );
248
249 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
250 if ($show_barcode) {
251     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
252     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
253 }
254 $template->param( show_barcode => 1 ) if $show_barcode;
255
256 # load the branches
257 my $branches = GetBranches();
258 my @branch_loop;
259 for my $branch_hash ( sort keys %{$branches} ) {
260     my $selected;
261     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
262         $selected =
263           ( C4::Context->userenv
264               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
265     }
266     push @branch_loop,
267       { value      => "branch: $branch_hash",
268         branchname => $branches->{$branch_hash}->{'branchname'},
269         selected   => $selected,
270       };
271 }
272 $template->param( branchloop => \@branch_loop );
273
274 # now the reserved items....
275 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
276 foreach my $res (@reserves) {
277
278     if ( $res->{'expirationdate'} eq '0000-00-00' ) {
279       $res->{'expirationdate'} = '';
280     }
281     $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
282     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
283     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
284     my $biblioData = GetBiblioData($res->{'biblionumber'});
285     $res->{'reserves_title'} = $biblioData->{'title'};
286     $res->{'author'} = $biblioData->{'author'};
287
288     if ($show_priority) {
289         $res->{'priority'} ||= '';
290     }
291     $res->{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref") if ( $res->{'suspend_until'} );
292 }
293
294 # use Data::Dumper;
295 # warn Dumper(@reserves);
296
297 $template->param( RESERVES       => \@reserves );
298 $template->param( reserves_count => $#reserves+1 );
299 $template->param( showpriority=>$show_priority );
300
301 my @waiting;
302 my $wcount = 0;
303 foreach my $res (@reserves) {
304     if ( $res->{'itemnumber'} ) {
305         my $item = GetItem( $res->{'itemnumber'});
306         $res->{'holdingbranch'} =
307           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
308         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
309         # get document reserve status
310         my $biblioData = GetBiblioData($res->{'biblionumber'});
311         $res->{'waiting_title'} = $biblioData->{'title'};
312         if ( ( $res->{'found'} eq 'W' ) ) {
313             my $item = $res->{'itemnumber'};
314             $item = GetBiblioFromItemNumber($item,undef);
315             $res->{'wait'}= 1;
316             $res->{'holdingbranch'}=$item->{'holdingbranch'};
317             $res->{'biblionumber'}=$item->{'biblionumber'};
318             $res->{'barcode'} = $item->{'barcode'};
319             $res->{'wbrcode'} = $res->{'branchcode'};
320             $res->{'itemnumber'}    = $res->{'itemnumber'};
321             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
322             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
323                 $res->{'atdestination'} = 1;
324             }
325             # set found to 1 if reserve is waiting for patron pickup
326             $res->{'found'} = 1 if $res->{'found'} eq 'W';
327         } else {
328             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
329             if ($transfertwhen) {
330                 $res->{intransit} = 1;
331                 $res->{datesent}   = $transfertwhen;
332                 $res->{frombranch} = GetBranchName($transfertfrom);
333             }
334         }
335         push @waiting, $res;
336         $wcount++;
337     }
338     # can be cancelled
339     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
340     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
341
342 }
343
344 $template->param( WAITING => \@waiting );
345
346 # current alert subscriptions
347 my $alerts = getalert($borrowernumber);
348 foreach ( @$alerts ) {
349     $_->{ $_->{type} } = 1;
350     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
351 }
352
353 if (C4::Context->preference('BakerTaylorEnabled')) {
354     $template->param(
355         BakerTaylorEnabled  => 1,
356         BakerTaylorImageURL => &image_url(),
357         BakerTaylorLinkURL  => &link_url(),
358         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
359     );
360 }
361
362 if (C4::Context->preference("OPACAmazonCoverImages") or 
363     C4::Context->preference("GoogleJackets") or
364     C4::Context->preference("BakerTaylorEnabled") or
365     C4::Context->preference("SyndeticsCoverImages")) {
366         $template->param(JacketImages=>1);
367 }
368
369 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
370     $template->param( bor_messages => 1 );
371 }
372
373 if ( $borr->{'opacnote'} ) {
374   $template->param( 
375     bor_messages => 1,
376     opacnote => $borr->{'opacnote'},
377   );
378 }
379
380 $template->param(
381     bor_messages_loop    => GetMessages( $borrowernumber, 'B', 'NONE' ),
382     waiting_count      => $wcount,
383     patronupdate => $patronupdate,
384     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
385     userview => 1,
386 );
387
388 $template->param(
389     SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
390     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
391     OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
392 );
393
394 output_html_with_http_headers $query, $cookie, $template->output;
395