adding export feature to 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 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::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 $overdues_count = 0;
85 my @overdues;
86 my @issuedat;
87 my $imgdir = getitemtypeimagesrc();
88 my $itemtypes = GetItemTypes();
89 foreach my $issue ( @$issues ) {
90
91     # check for reserves
92     my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
93     if ( $restype ) {
94         $issue->{'reserved'} = 1;
95     }
96     
97     my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
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 $status = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
116
117     $issue->{'status'} = $status;
118
119     if ( $issue->{'overdue'} ) {
120         push @overdues, $issue;
121         $overdues_count++;
122         $issue->{'overdue'} = 1;
123     }
124     else {
125         $issue->{'issued'} = 1;
126     }
127     # imageurl:
128     my $itemtype = $issue->{'itemtype'};
129     if ( $itemtype ) {
130         $issue->{'imageurl'}    = $imgdir."/".$itemtypes->{$itemtype}->{'imageurl'};
131         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
132     }
133     push @issuedat, $issue;
134     $count++;
135 }
136
137 $template->param( ISSUES       => \@issuedat );
138 $template->param( issues_count => $count );
139
140 $template->param( OVERDUES       => \@overdues );
141 $template->param( overdues_count => $overdues_count );
142
143 # load the branches
144 my $branches = GetBranches();
145 my @branch_loop;
146 for my $branch_hash (sort keys %$branches ) {
147     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
148     push @branch_loop,
149       {
150         value      => "branch: $branch_hash",
151         branchname => $branches->{$branch_hash}->{'branchname'},
152         selected => $selected
153       };
154 }
155 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
156
157 # now the reserved items....
158 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
159 foreach my $res (@reserves) {
160     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
161     my $publictype = $res->{'publictype'};
162     $res->{$publictype} = 1;
163     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
164     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
165     my $biblioData = GetBiblioData($res->{'biblionumber'});
166     $res->{'reserves_title'} = $biblioData->{'title'};
167 }
168
169 # use Data::Dumper;
170 # warn Dumper(@reserves);
171
172 $template->param( RESERVES       => \@reserves );
173 $template->param( reserves_count => $#reserves+1 );
174
175 my @waiting;
176 my $wcount = 0;
177 foreach my $res (@reserves) {
178     if ( $res->{'itemnumber'} ) {
179         my $item = GetItem( $res->{'itemnumber'});
180         $res->{'holdingbranch'} =
181           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
182         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
183         # get document reserve status
184         my $biblioData = GetBiblioData($res->{'biblionumber'});
185         $res->{'waiting_title'} = $biblioData->{'title'};
186         if ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) ) {
187             my $item = $res->{'itemnumber'};
188             $item = GetBiblioFromItemNumber($item,undef);
189             $res->{'wait'}= 1; 
190             $res->{'holdingbranch'}=$item->{'holdingbranch'};
191             $res->{'biblionumber'}=$item->{'biblionumber'};
192             $res->{'barcodenumber'}     = $item->{'barcode'};
193             $res->{'wbrcode'} = $res->{'branchcode'};
194             $res->{'itemnumber'}        = $res->{'itemnumber'};
195             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
196             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
197                 $res->{'atdestination'} = 1;
198             }
199             # set found to 1 if reserve is waiting for patron pickup
200             $res->{'found'} = 1 if $res->{'found'} eq 'W';
201         }
202         push @waiting, $res;
203         $wcount++;
204     }
205 }
206
207 $template->param( WAITING => \@waiting );
208
209 # current alert subscriptions
210 my $alerts = getalert($borrowernumber);
211 foreach ( @$alerts ) {
212     $_->{ $_->{type} } = 1;
213     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
214 }
215
216 $template->param(
217     waiting_count      => $wcount,
218     textmessaging      => $borr->{textmessaging},
219 );
220
221 output_html_with_http_headers $query, $cookie, $template->output;
222