installer: added (in some cases back) module deps
[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 require Exporter;
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::Dates qw/format_date/;
31 use C4::Letters;
32 use C4::Branch; # GetBranches
33
34 my $query = new CGI;
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "opac-user.tmpl",
38         query           => $query,
39         type            => "opac",
40         authnotrequired => 0,
41         flagsrequired   => { borrow => 1 },
42         debug           => 1,
43     }
44 );
45
46 # get borrower information ....
47 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
48
49 $borr->{'dateenrolled'} = format_date( $borr->{'dateenrolled'} );
50 $borr->{'expiry'}       = format_date( $borr->{'expiry'} );
51 $borr->{'dateofbirth'}  = format_date( $borr->{'dateofbirth'} );
52 $borr->{'ethnicity'}    = fixEthnicity( $borr->{'ethnicity'} );
53
54 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
55     $borr->{'flagged'} = 1;
56 }
57 # $make flagged available everywhere in the template
58 my $patron_flagged = $borr->{'flagged'};
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                     borrowernumber => $borrowernumber,
77                     patron_flagged => $patron_flagged,
78                 );
79
80 #get issued items ....
81 my ($countissues,$issues) = GetPendingIssues($borrowernumber);
82
83 my $count          = 0;
84 my $toggle = 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         if($count%2 eq 0){ $issue->{'toggle'} = 1; } else { $issue->{'toggle'} = 0; }
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         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
118
119     $issue->{'status'} = $status;
120
121     if ( $issue->{'overdue'} ) {
122         push @overdues, $issue;
123         $overdues_count++;
124         $issue->{'overdue'} = 1;
125     }
126     else {
127         $issue->{'issued'} = 1;
128     }
129     # imageurl:
130     my $itemtype = $issue->{'itemtype'};
131     if ( $itemtype ) {
132         $issue->{'imageurl'}    = $imgdir."/".$itemtypes->{$itemtype}->{'imageurl'};
133         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
134     }
135     push @issuedat, $issue;
136     $count++;
137 }
138
139 $template->param( ISSUES       => \@issuedat );
140 $template->param( issues_count => $count );
141
142 $template->param( OVERDUES       => \@overdues );
143 $template->param( overdues_count => $overdues_count );
144
145 # load the branches
146 my $branches = GetBranches();
147 my @branch_loop;
148 for my $branch_hash (sort keys %$branches ) {
149     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
150     push @branch_loop,
151       {
152         value      => "branch: $branch_hash",
153         branchname => $branches->{$branch_hash}->{'branchname'},
154         selected => $selected
155       };
156 }
157 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
158
159 # now the reserved items....
160 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
161 foreach my $res (@reserves) {
162     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
163     my $publictype = $res->{'publictype'};
164     $res->{$publictype} = 1;
165     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
166     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
167     my $biblioData = GetBiblioData($res->{'biblionumber'});
168     $res->{'reserves_title'} = $biblioData->{'title'};
169 }
170
171 # use Data::Dumper;
172 # warn Dumper(@reserves);
173
174 $template->param( RESERVES       => \@reserves );
175 $template->param( reserves_count => $#reserves+1 );
176
177 my @waiting;
178 my $wcount = 0;
179 foreach my $res (@reserves) {
180     if ( $res->{'itemnumber'} ) {
181         my $item = GetItem( $res->{'itemnumber'});
182         $res->{'holdingbranch'} =
183           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
184         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
185         # get document reserve status
186         my $biblioData = GetBiblioData($res->{'biblionumber'});
187         $res->{'waiting_title'} = $biblioData->{'title'};
188         if ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) ) {
189             my $item = $res->{'itemnumber'};
190             $item = GetBiblioFromItemNumber($item,undef);
191             $res->{'wait'}= 1; 
192             $res->{'holdingbranch'}=$item->{'holdingbranch'};
193             $res->{'biblionumber'}=$item->{'biblionumber'};
194             $res->{'barcodenumber'}     = $item->{'barcode'};
195             $res->{'wbrcode'} = $res->{'branchcode'};
196             $res->{'itemnumber'}        = $res->{'itemnumber'};
197             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
198             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
199                 $res->{'atdestination'} = 1;
200             }
201             # set found to 1 if reserve is waiting for patron pickup
202             $res->{'found'} = 1 if $res->{'found'} eq 'W';
203         }
204         push @waiting, $res;
205         $wcount++;
206     }
207 }
208
209 $template->param( WAITING => \@waiting );
210
211 # current alert subscriptions
212 my $alerts = getalert($borrowernumber);
213 foreach ( @$alerts ) {
214     $_->{ $_->{type} } = 1;
215     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
216 }
217
218 $template->param(
219     waiting_count      => $wcount,
220     textmessaging      => $borr->{textmessaging},
221 );
222
223 output_html_with_http_headers $query, $cookie, $template->output;
224