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