Merge remote branch 'kc/new/enh/bug_6003' 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->{'formattedwaitingdate'} = format_date($res->{'waitingdate'});
199     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
200     my $biblioData = GetBiblioData($res->{'biblionumber'});
201     $res->{'reserves_title'} = $biblioData->{'title'};
202     if ($OPACDisplayRequestPriority) {
203         $res->{'priority'} = '' if $res->{'priority'} eq '0';
204     }
205 }
206
207 # use Data::Dumper;
208 # warn Dumper(@reserves);
209
210 $template->param( RESERVES       => \@reserves );
211 $template->param( reserves_count => $#reserves+1 );
212 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
213
214 my @waiting;
215 my $wcount = 0;
216 foreach my $res (@reserves) {
217     if ( $res->{'itemnumber'} ) {
218         my $item = GetItem( $res->{'itemnumber'});
219         $res->{'holdingbranch'} =
220           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
221         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
222         # get document reserve status
223         my $biblioData = GetBiblioData($res->{'biblionumber'});
224         $res->{'waiting_title'} = $biblioData->{'title'};
225         if ( ( $res->{'found'} eq 'W' ) ) {
226             my $item = $res->{'itemnumber'};
227             $item = GetBiblioFromItemNumber($item,undef);
228             $res->{'wait'}= 1; 
229             $res->{'holdingbranch'}=$item->{'holdingbranch'};
230             $res->{'biblionumber'}=$item->{'biblionumber'};
231             $res->{'barcodenumber'} = $item->{'barcode'};
232             $res->{'wbrcode'} = $res->{'branchcode'};
233             $res->{'itemnumber'}    = $res->{'itemnumber'};
234             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
235             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
236                 $res->{'atdestination'} = 1;
237             }
238             # set found to 1 if reserve is waiting for patron pickup
239             $res->{'found'} = 1 if $res->{'found'} eq 'W';
240         } else {
241             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
242             if ($transfertwhen) {
243                 $res->{intransit} = 1;
244                 $res->{datesent}   = format_date($transfertwhen);
245                 $res->{frombranch} = GetBranchName($transfertfrom);
246             }
247         }
248         push @waiting, $res;
249         $wcount++;
250     }
251     # can be cancelled
252     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
253     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
254     
255 }
256
257 $template->param( WAITING => \@waiting );
258
259 # current alert subscriptions
260 my $alerts = getalert($borrowernumber);
261 foreach ( @$alerts ) {
262     $_->{ $_->{type} } = 1;
263     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
264 }
265
266 if (C4::Context->preference('BakerTaylorEnabled')) {
267     $template->param(
268         BakerTaylorEnabled  => 1,
269         BakerTaylorImageURL => &image_url(),
270         BakerTaylorLinkURL  => &link_url(),
271         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
272     );
273 }
274
275 if (C4::Context->preference("OPACAmazonCoverImages") or 
276     C4::Context->preference("GoogleJackets") or
277     C4::Context->preference("BakerTaylorEnabled") or
278         C4::Context->preference("SyndeticsCoverImages")) {
279         $template->param(JacketImages=>1);
280 }
281
282 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
283         $template->param( bor_messages => 1 );
284 }
285
286 if ( $borr->{'opacnote'} ) {
287   $template->param( 
288     bor_messages => 1,
289     opacnote => $borr->{'opacnote'},
290   );
291 }
292
293 $template->param(
294     bor_messages_loop   => GetMessages( $borrowernumber, 'B', 'NONE' ),
295     waiting_count      => $wcount,
296     textmessaging      => $borr->{textmessaging},
297     patronupdate => $patronupdate,
298     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
299     userview => 1,
300     dateformat    => C4::Context->preference("dateformat"),
301 );
302
303 output_html_with_http_headers $query, $cookie, $template->output;
304