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