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