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