#1255 : flagged patron can't renew
[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 # $Id$
19
20 use strict;
21 require Exporter;
22 use CGI;
23
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Circulation;
27 use C4::Reserves;
28 use C4::Members;
29 use C4::Output;
30 use C4::Biblio;
31 use C4::Date;
32 use C4::Letters;
33 use C4::Branch; # GetBranches
34
35 my $query = new CGI;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-user.tmpl",
39         query           => $query,
40         type            => "opac",
41         authnotrequired => 0,
42         flagsrequired   => { borrow => 1 },
43         debug           => 1,
44     }
45 );
46
47 # get borrower information ....
48 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
49
50 $borr->{'dateenrolled'} = format_date( $borr->{'dateenrolled'} );
51 $borr->{'expiry'}       = format_date( $borr->{'expiry'} );
52 $borr->{'dateofbirth'}  = format_date( $borr->{'dateofbirth'} );
53 $borr->{'ethnicity'}    = fixEthnicity( $borr->{'ethnicity'} );
54
55 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
56     $borr->{'flagged'} = 1;
57 }
58 # $make flagged available everywhere in the template
59 my $patron_flagged = $borr->{'flagged'};
60 if ( $borr->{'amountoutstanding'} > 5 ) {
61     $borr->{'amountoverfive'} = 1;
62 }
63 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
64     $borr->{'amountoverzero'} = 1;
65 }
66 if ( $borr->{'amountoutstanding'} < 0 ) {
67     $borr->{'amountlessthanzero'} = 1;
68     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
69 }
70
71 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
72
73 my @bordat;
74 $bordat[0] = $borr;
75
76 $template->param(   BORROWER_INFO  => \@bordat,
77                     borrowernumber => $borrowernumber,
78                     patron_flagged => $patron_flagged,
79                 );
80
81 #get issued items ....
82 my ($countissues,$issues) = GetPendingIssues($borrowernumber);
83
84 my $count          = 0;
85 my $overdues_count = 0;
86 my @overdues;
87 my @issuedat;
88 my $imgdir = getitemtypeimagesrc();
89 my $itemtypes = GetItemTypes();
90 foreach my $issue ( @$issues ) {
91
92     # check for reserves
93     my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
94     if ( $restype ) {
95         $issue->{'reserved'} = 1;
96     }
97     
98     my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
99     my $charges = 0;
100     foreach my $ac (@$accts) {
101         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
102             $charges += $ac->{'amountoutstanding'}
103               if $ac->{'accounttype'} eq 'F';
104             $charges += $ac->{'amountoutstanding'}
105               if $ac->{'accounttype'} eq 'L';
106         }
107     }
108     $issue->{'charges'} = $charges;
109
110     # get publictype for icon
111
112     my $publictype = $issue->{'publictype'};
113     $issue->{$publictype} = 1;
114
115     # check if item is renewable
116     my $status = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
117
118     $issue->{'status'} = $status;
119
120     if ( $issue->{'overdue'} ) {
121         push @overdues, $issue;
122         $overdues_count++;
123         $issue->{'overdue'} = 1;
124     }
125     else {
126         $issue->{'issued'} = 1;
127     }
128     # imageurl:
129     my $itemtype = $issue->{'itemtype'};
130     if ( $itemtype ) {
131         $issue->{'imageurl'}    = $imgdir."/".$itemtypes->{$itemtype}->{'imageurl'};
132         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
133     }
134     push @issuedat, $issue;
135     $count++;
136 }
137
138 $template->param( ISSUES       => \@issuedat );
139 $template->param( issues_count => $count );
140
141 $template->param( OVERDUES       => \@overdues );
142 $template->param( overdues_count => $overdues_count );
143
144 # load the branches
145 my $branches = GetBranches();
146 my @branch_loop;
147 for my $branch_hash (sort keys %$branches ) {
148     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
149     push @branch_loop,
150       {
151         value      => "branch: $branch_hash",
152         branchname => $branches->{$branch_hash}->{'branchname'},
153         selected => $selected
154       };
155 }
156 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
157
158 # now the reserved items....
159 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
160 foreach my $res (@reserves) {
161     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
162     my $publictype = $res->{'publictype'};
163     $res->{$publictype} = 1;
164     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
165     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
166     my $biblioData = GetBiblioData($res->{'biblionumber'});
167     $res->{'reserves_title'} = $biblioData->{'title'};
168 }
169
170 # use Data::Dumper;
171 # warn Dumper(@reserves);
172
173 $template->param( RESERVES       => \@reserves );
174 $template->param( reserves_count => $#reserves+1 );
175
176 my @waiting;
177 my $wcount = 0;
178 foreach my $res (@reserves) {
179     if ( $res->{'itemnumber'} ) {
180         my $item = GetItem( $res->{'itemnumber'});
181         $res->{'holdingbranch'} =
182           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
183         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
184         # get document reserve status
185         my $biblioData = GetBiblioData($res->{'biblionumber'});
186         $res->{'waiting_title'} = $biblioData->{'title'};
187         if ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) ) {
188             my $item = $res->{'itemnumber'};
189             $item = GetBiblioFromItemNumber($item,undef);
190             $res->{'wait'}= 1; 
191             $res->{'holdingbranch'}=$item->{'holdingbranch'};
192             $res->{'biblionumber'}=$item->{'biblionumber'};
193             $res->{'barcodenumber'}     = $item->{'barcode'};
194             $res->{'wbrcode'} = $res->{'branchcode'};
195             $res->{'itemnumber'}        = $res->{'itemnumber'};
196             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
197             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
198                 $res->{'atdestination'} = 1;
199             }
200             # set found to 1 if reserve is waiting for patron pickup
201             $res->{'found'} = 1 if $res->{'found'} eq 'W';
202         }
203         push @waiting, $res;
204         $wcount++;
205     }
206 }
207
208 $template->param( WAITING => \@waiting );
209
210 # current alert subscriptions
211 my $alerts = getalert($borrowernumber);
212 foreach ( @$alerts ) {
213     $_->{ $_->{type} } = 1;
214     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
215 }
216
217 $template->param(
218     waiting_count      => $wcount,
219     textmessaging      => $borr->{textmessaging},
220 );
221
222 output_html_with_http_headers $query, $cookie, $template->output;
223