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