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