Merge commit 'pianohacker-koha/prefs-submit' into master
[koha.git] / opac / opac-user.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19 use strict;
20
21 use CGI;
22
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Circulation;
26 use C4::Reserves;
27 use C4::Members;
28 use C4::Output;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Dates qw/format_date/;
32 use C4::Letters;
33 use C4::Branch; # GetBranches
34
35 my $query = new CGI;
36
37 BEGIN {
38     if (C4::Context->preference('BakerTaylorEnabled')) {
39         require C4::External::BakerTaylor;
40         import C4::External::BakerTaylor qw(&image_url &link_url);
41     }
42 }
43
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45     {
46         template_name   => "opac-user.tmpl",
47         query           => $query,
48         type            => "opac",
49         authnotrequired => 0,
50         flagsrequired   => { borrow => 1 },
51         debug           => 1,
52     }
53 );
54
55 my $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
56 my $patronupdate = $query->param('patronupdate');
57
58 # get borrower information ....
59 my ( $borr ) = GetMemberDetails( $borrowernumber );
60
61 for (qw(dateenrolled dateexpiry dateofbirth)) {
62     ($borr->{$_}) and $borr->{$_} = format_date($borr->{$_});
63 }
64 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
65
66 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
67     $borr->{'flagged'} = 1;
68 }
69
70 if ( $borr->{'amountoutstanding'} > 5 ) {
71     $borr->{'amountoverfive'} = 1;
72 }
73 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
74     $borr->{'amountoverzero'} = 1;
75 }
76
77 if ( $borr->{'amountoutstanding'} > C4::Context->preference( 'OPACFineNoRenewals' ) ) {
78     $borr->{'flagged'} = 1;
79     $template->param(
80         renewal_blocked_fines => sprintf( "%.02f", C4::Context->preference( 'OPACFineNoRenewals' ) ),
81     );
82 }
83
84 if ( $borr->{'amountoutstanding'} < 0 ) {
85     $borr->{'amountlessthanzero'} = 1;
86     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
87 }
88
89 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
90
91 my @bordat;
92 $bordat[0] = $borr;
93
94 $template->param(   BORROWER_INFO  => \@bordat,
95                     borrowernumber => $borrowernumber,
96                     patron_flagged => $borr->{flagged},
97                 );
98
99 #get issued items ....
100
101 my $count          = 0;
102 my $toggle = 0;
103 my $overdues_count = 0;
104 my @overdues;
105 my @issuedat;
106 my $itemtypes = GetItemTypes();
107 my ($issues) = GetPendingIssues($borrowernumber);
108 if ($issues){
109         foreach my $issue ( sort sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues ) {
110                 # check for reserves
111                 my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
112                 if ( $restype ) {
113                         $issue->{'reserved'} = 1;
114                 }
115                 
116                 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
117                 my $charges = 0;
118                 foreach my $ac (@$accts) {
119                         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
120                                 $charges += $ac->{'amountoutstanding'}
121                                   if $ac->{'accounttype'} eq 'F';
122                                 $charges += $ac->{'amountoutstanding'}
123                                   if $ac->{'accounttype'} eq 'L';
124                         }
125                 }
126                 $issue->{'charges'} = $charges;
127
128                 # get publictype for icon
129
130                 my $publictype = $issue->{'publictype'};
131                 $issue->{$publictype} = 1;
132
133                 # check if item is renewable
134                 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
135                 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
136                 $issue->{'status'} = $status && C4::Context->preference("OpacRenewalAllowed");
137                 $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
138                 $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
139
140                 if ( $issue->{'overdue'} ) {
141                         push @overdues, $issue;
142                         $overdues_count++;
143                         $issue->{'overdue'} = 1;
144                 }
145                 else {
146                         $issue->{'issued'} = 1;
147                 }
148                 # imageurl:
149                 my $itemtype = $issue->{'itemtype'};
150                 if ( $itemtype ) {
151                         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
152                         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
153                 }
154                 $issue->{date_due} = format_date($issue->{date_due});
155                 push @issuedat, $issue;
156                 $count++;
157                 
158                 my $isbn = GetNormalizedISBN($issue->{'isbn'});
159                 $issue->{normalized_isbn} = $isbn;
160         }
161 }
162 $template->param( ISSUES       => \@issuedat );
163 $template->param( issues_count => $count );
164
165 $template->param( OVERDUES       => \@overdues );
166 $template->param( overdues_count => $overdues_count );
167
168 # load the branches
169 my $branches = GetBranches();
170 my @branch_loop;
171 for my $branch_hash (sort keys %$branches ) {
172     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
173     push @branch_loop,
174       {
175         value      => "branch: $branch_hash",
176         branchname => $branches->{$branch_hash}->{'branchname'},
177         selected => $selected
178       };
179 }
180 $template->param( branchloop => \@branch_loop );
181
182 # now the reserved items....
183 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
184 foreach my $res (@reserves) {
185     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
186     my $publictype = $res->{'publictype'};
187     $res->{$publictype} = 1;
188     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
189     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
190     my $biblioData = GetBiblioData($res->{'biblionumber'});
191     $res->{'reserves_title'} = $biblioData->{'title'};
192     if ($OPACDisplayRequestPriority) {
193         $res->{'priority'} = '' if $res->{'priority'} eq '0';
194     }
195 }
196
197 # use Data::Dumper;
198 # warn Dumper(@reserves);
199
200 $template->param( RESERVES       => \@reserves );
201 $template->param( reserves_count => $#reserves+1 );
202 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
203
204 my @waiting;
205 my $wcount = 0;
206 foreach my $res (@reserves) {
207     if ( $res->{'itemnumber'} ) {
208         my $item = GetItem( $res->{'itemnumber'});
209         $res->{'holdingbranch'} =
210           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
211         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
212         # get document reserve status
213         my $biblioData = GetBiblioData($res->{'biblionumber'});
214         $res->{'waiting_title'} = $biblioData->{'title'};
215         if ( ( $res->{'found'} eq 'W' ) ) {
216             my $item = $res->{'itemnumber'};
217             $item = GetBiblioFromItemNumber($item,undef);
218             $res->{'wait'}= 1; 
219             $res->{'holdingbranch'}=$item->{'holdingbranch'};
220             $res->{'biblionumber'}=$item->{'biblionumber'};
221             $res->{'barcodenumber'} = $item->{'barcode'};
222             $res->{'wbrcode'} = $res->{'branchcode'};
223             $res->{'itemnumber'}    = $res->{'itemnumber'};
224             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
225             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
226                 $res->{'atdestination'} = 1;
227             }
228             # set found to 1 if reserve is waiting for patron pickup
229             $res->{'found'} = 1 if $res->{'found'} eq 'W';
230         } else {
231             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
232             if ($transfertwhen) {
233                 $res->{intransit} = 1;
234                 $res->{datesent}   = format_date($transfertwhen);
235                 $res->{frombranch} = GetBranchName($transfertfrom);
236             }
237         }
238         push @waiting, $res;
239         $wcount++;
240     }
241     # can be cancelled
242     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
243     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
244     
245 }
246
247 $template->param( WAITING => \@waiting );
248
249 # current alert subscriptions
250 my $alerts = getalert($borrowernumber);
251 foreach ( @$alerts ) {
252     $_->{ $_->{type} } = 1;
253     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
254 }
255
256 if (C4::Context->preference('BakerTaylorEnabled')) {
257     $template->param(
258         BakerTaylorEnabled  => 1,
259         BakerTaylorImageURL => &image_url(),
260         BakerTaylorLinkURL  => &link_url(),
261         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
262     );
263 }
264
265 if (C4::Context->preference("OPACAmazonCoverImages") or 
266     C4::Context->preference("GoogleJackets") or
267     C4::Context->preference("BakerTaylorEnabled") or
268         C4::Context->preference("SyndeticsCoverImages")) {
269         $template->param(JacketImages=>1);
270 }
271
272 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
273         $template->param( bor_messages => 1 );
274 }
275
276 if ( $borr->{'opacnote'} ) {
277   $template->param( 
278     bor_messages => 1,
279     opacnote => $borr->{'opacnote'},
280   );
281 }
282
283 $template->param(
284     bor_messages_loop   => GetMessages( $borrowernumber, 'B', 'NONE' ),
285     waiting_count      => $wcount,
286     textmessaging      => $borr->{textmessaging},
287     patronupdate => $patronupdate,
288     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
289     userview => 1,
290     dateformat    => C4::Context->preference("dateformat"),
291 );
292
293 output_html_with_http_headers $query, $cookie, $template->output;
294