rel_3_0 moved to HEAD (introducing new files)
[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::Circ2;
27 use C4::Reserves2;
28 use C4::Members;
29 use C4::Interface::CGI::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 ) = getpatroninformation( undef, $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
59 if ( $borr->{'amountoutstanding'} > 5 ) {
60     $borr->{'amountoverfive'} = 1;
61 }
62 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
63     $borr->{'amountoverzero'} = 1;
64 }
65 if ( $borr->{'amountoutstanding'} < 0 ) {
66     $borr->{'amountlessthanzero'} = 1;
67     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
68 }
69
70 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
71
72 my @bordat;
73 $bordat[0] = $borr;
74
75 $template->param( BORROWER_INFO  => \@bordat );
76 $template->param( borrowernumber => $borrowernumber );
77
78 #get issued items ....
79 my $issues = getissues($borr);
80
81 my $count          = 0;
82 my $overdues_count = 0;
83 my @overdues;
84 my @issuedat;
85 my $imgdir = getitemtypeimagesrc();
86 my $itemtypes = GetItemTypes();
87 foreach my $key ( keys %$issues ) {
88     my $issue = $issues->{$key};
89     $issue->{'date_due'} = format_date( $issue->{'date_due'} );
90
91     # check for reserves
92     my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
93     if ( $restype ) {
94         $issue->{'reserved'} = 1;
95     }
96     
97     my ( $numaccts, $accts, $total ) = getboracctrecord( undef, $borr );
98     my $charges = 0;
99     foreach my $ac (@$accts) {
100         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
101             $charges += $ac->{'amountoutstanding'}
102               if $ac->{'accounttype'} eq 'F';
103             $charges += $ac->{'amountoutstanding'}
104               if $ac->{'accounttype'} eq 'L';
105         }
106     }
107     $issue->{'charges'} = $charges;
108
109     # get publictype for icon
110
111     my $publictype = $issue->{'publictype'};
112     $issue->{$publictype} = 1;
113
114     # check if item is renewable
115     my %env;
116     my $status = renewstatus( \%env, $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 my $branches = GetBranches();
145
146 # now the reserved items....
147 my ( $rcount, $reserves ) = FindReserves( undef, $borrowernumber );
148 foreach my $res (@$reserves) {
149     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
150     my $publictype = $res->{'publictype'};
151     $res->{$publictype} = 1;
152     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
153     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
154     my $biblioData = GetBiblioData($res->{'biblionumber'});
155     $res->{'reserves_title'} = $biblioData->{'title'};
156 }
157
158 $template->param( RESERVES       => $reserves );
159 $template->param( reserves_count => $rcount );
160
161 my @waiting;
162 my $wcount = 0;
163 foreach my $res (@$reserves) {
164     if ( $res->{'itemnumber'} ) {
165         my $item = getiteminformation( $res->{'itemnumber'}, '' );
166         $res->{'holdingbranch'} =
167           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
168         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
169         if ( $res->{'holdingbranch'} eq $res->{'branch'} ) {
170             $res->{'atdestination'} = 1;
171         }
172         my $biblioData = GetBiblioData($res->{'biblionumber'});
173         $res->{'waiting_title'} = $biblioData->{'title'};
174         push @waiting, $res;
175         $wcount++;
176     }
177 }
178
179 $template->param( WAITING => \@waiting );
180
181 # current alert subscriptions
182 my $alerts = getalert($borrowernumber);
183 foreach ( @$alerts ) {
184     $_->{ $_->{type} } = 1;
185     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
186 }
187
188 $template->param(
189     waiting_count      => $wcount,
190     textmessaging      => $borr->{textmessaging},
191 );
192
193 output_html_with_http_headers $query, $cookie, $template->output;
194