Prevent OPAC renewals if fines are over limit
[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 # $make flagged available everywhere in the template
70 my $patron_flagged = $borr->{'flagged'};
71 if ( $borr->{'amountoutstanding'} > 5 ) {
72     $borr->{'amountoverfive'} = 1;
73 }
74 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
75     $borr->{'amountoverzero'} = 1;
76 }
77
78 if ( $borr->{'amountoutstanding'} > C4::Context->preference( 'OPACFineNoRenewals' ) ) {
79     $borr->{'flagged'} = 1;
80     $template->param(
81         renewal_blocked_fines => sprintf( "%.02f", C4::Context->preference( 'OPACFineNoRenewals' ) ),
82     );
83 }
84
85 if ( $borr->{'amountoutstanding'} < 0 ) {
86     $borr->{'amountlessthanzero'} = 1;
87     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
88 }
89
90 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
91
92 my @bordat;
93 $bordat[0] = $borr;
94
95 $template->param(   BORROWER_INFO  => \@bordat,
96                     borrowernumber => $borrowernumber,
97                     patron_flagged => $patron_flagged,
98                 );
99
100 #get issued items ....
101 my ($issues) = GetPendingIssues($borrowernumber);
102 my @issue_list = sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues;
103
104 my $count          = 0;
105 my $toggle = 0;
106 my $overdues_count = 0;
107 my @overdues;
108 my @issuedat;
109 my $itemtypes = GetItemTypes();
110 foreach my $issue ( @issue_list ) {
111     if($count%2 eq 0){ $issue->{'toggle'} = 1; } else { $issue->{'toggle'} = 0; }
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     my $publictype = $res->{'publictype'};
189     $res->{$publictype} = 1;
190     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
191     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
192     my $biblioData = GetBiblioData($res->{'biblionumber'});
193     $res->{'reserves_title'} = $biblioData->{'title'};
194     if ($OPACDisplayRequestPriority) {
195         $res->{'priority'} = '' if $res->{'priority'} eq '0';
196     }
197 }
198
199 # use Data::Dumper;
200 # warn Dumper(@reserves);
201
202 $template->param( RESERVES       => \@reserves );
203 $template->param( reserves_count => $#reserves+1 );
204 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
205
206 my @waiting;
207 my $wcount = 0;
208 foreach my $res (@reserves) {
209     if ( $res->{'itemnumber'} ) {
210         my $item = GetItem( $res->{'itemnumber'});
211         $res->{'holdingbranch'} =
212           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
213         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
214         # get document reserve status
215         my $biblioData = GetBiblioData($res->{'biblionumber'});
216         $res->{'waiting_title'} = $biblioData->{'title'};
217         if ( ( $res->{'found'} eq 'W' ) ) {
218             my $item = $res->{'itemnumber'};
219             $item = GetBiblioFromItemNumber($item,undef);
220             $res->{'wait'}= 1; 
221             $res->{'holdingbranch'}=$item->{'holdingbranch'};
222             $res->{'biblionumber'}=$item->{'biblionumber'};
223             $res->{'barcodenumber'} = $item->{'barcode'};
224             $res->{'wbrcode'} = $res->{'branchcode'};
225             $res->{'itemnumber'}    = $res->{'itemnumber'};
226             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
227             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
228                 $res->{'atdestination'} = 1;
229             }
230             # set found to 1 if reserve is waiting for patron pickup
231             $res->{'found'} = 1 if $res->{'found'} eq 'W';
232         } else {
233             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
234             if ($transfertwhen) {
235                 $res->{intransit} = 1;
236                 $res->{datesent}   = format_date($transfertwhen);
237                 $res->{frombranch} = GetBranchName($transfertfrom);
238             }
239         }
240         push @waiting, $res;
241         $wcount++;
242     }
243 }
244
245 $template->param( WAITING => \@waiting );
246
247 # current alert subscriptions
248 my $alerts = getalert($borrowernumber);
249 foreach ( @$alerts ) {
250     $_->{ $_->{type} } = 1;
251     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
252 }
253
254 if (C4::Context->preference('BakerTaylorEnabled')) {
255     $template->param(
256         BakerTaylorEnabled  => 1,
257         BakerTaylorImageURL => &image_url(),
258         BakerTaylorLinkURL  => &link_url(),
259         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
260     );
261 }
262
263 if (C4::Context->preference("OPACAmazonCoverImages") or 
264     C4::Context->preference("GoogleJackets") or
265     C4::Context->preference("BakerTaylorEnabled") or
266         C4::Context->preference("SyndeticsCoverImages")) {
267         $template->param(JacketImages=>1);
268 }
269
270 $template->param(
271     waiting_count      => $wcount,
272     textmessaging      => $borr->{textmessaging},
273     patronupdate => $patronupdate,
274     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
275     userview => 1,
276     dateformat    => C4::Context->preference("dateformat"),
277 );
278
279 output_html_with_http_headers $query, $cookie, $template->output;
280