Syndetics and Amazon bugfix enhancements
[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 $borr->{'dateenrolled'} = format_date( $borr->{'dateenrolled'} );
62 $borr->{'expiry'}       = format_date( $borr->{'expiry'} );
63 $borr->{'dateofbirth'}  = format_date( $borr->{'dateofbirth'} );
64 $borr->{'ethnicity'}    = fixEthnicity( $borr->{'ethnicity'} );
65
66 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
67     $borr->{'flagged'} = 1;
68 }
69 # $make flagged available everywhere in the template
70 my $patron_flagged = $borr->{'flagged'};
71 if ( $borr->{'amountoutstanding'} > 5 ) {
72     $borr->{'amountoverfive'} = 1;
73 }
74 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
75     $borr->{'amountoverzero'} = 1;
76 }
77 if ( $borr->{'amountoutstanding'} < 0 ) {
78     $borr->{'amountlessthanzero'} = 1;
79     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
80 }
81
82 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
83
84 my @bordat;
85 $bordat[0] = $borr;
86
87 $template->param(   BORROWER_INFO  => \@bordat,
88                     borrowernumber => $borrowernumber,
89                     patron_flagged => $patron_flagged,
90                 );
91
92 #get issued items ....
93 my ($issues) = GetPendingIssues($borrowernumber);
94 my @issue_list = sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues;
95
96 my $count          = 0;
97 my $toggle = 0;
98 my $overdues_count = 0;
99 my @overdues;
100 my @issuedat;
101 my $itemtypes = GetItemTypes();
102 foreach my $issue ( @issue_list ) {
103     if($count%2 eq 0){ $issue->{'toggle'} = 1; } else { $issue->{'toggle'} = 0; }
104     # check for reserves
105     my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
106     if ( $restype ) {
107         $issue->{'reserved'} = 1;
108     }
109     
110     my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
111     my $charges = 0;
112     foreach my $ac (@$accts) {
113         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
114             $charges += $ac->{'amountoutstanding'}
115               if $ac->{'accounttype'} eq 'F';
116             $charges += $ac->{'amountoutstanding'}
117               if $ac->{'accounttype'} eq 'L';
118         }
119     }
120     $issue->{'charges'} = $charges;
121
122     # get publictype for icon
123
124     my $publictype = $issue->{'publictype'};
125     $issue->{$publictype} = 1;
126
127     # check if item is renewable
128     my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
129     ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
130
131     $issue->{'status'} = $status || C4::Context->preference("OpacRenewalAllowed");
132
133     if ( $issue->{'overdue'} ) {
134         push @overdues, $issue;
135         $overdues_count++;
136         $issue->{'overdue'} = 1;
137     }
138     else {
139         $issue->{'issued'} = 1;
140     }
141     # imageurl:
142     my $itemtype = $issue->{'itemtype'};
143     if ( $itemtype ) {
144         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
145         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
146     }
147     $issue->{date_due} = format_date($issue->{date_due});
148     push @issuedat, $issue;
149     $count++;
150     
151     my $isbn = GetNormalizedISBN($issue->{'isbn'});
152     $issue->{normalized_isbn} = $isbn;
153 }
154
155 $template->param( ISSUES       => \@issuedat );
156 $template->param( issues_count => $count );
157
158 $template->param( OVERDUES       => \@overdues );
159 $template->param( overdues_count => $overdues_count );
160
161 # load the branches
162 my $branches = GetBranches();
163 my @branch_loop;
164 for my $branch_hash (sort keys %$branches ) {
165     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
166     push @branch_loop,
167       {
168         value      => "branch: $branch_hash",
169         branchname => $branches->{$branch_hash}->{'branchname'},
170         selected => $selected
171       };
172 }
173 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
174
175 # now the reserved items....
176 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
177 foreach my $res (@reserves) {
178     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
179     my $publictype = $res->{'publictype'};
180     $res->{$publictype} = 1;
181     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
182     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
183     my $biblioData = GetBiblioData($res->{'biblionumber'});
184     $res->{'reserves_title'} = $biblioData->{'title'};
185     if ($OPACDisplayRequestPriority) {
186         $res->{'priority'} = '' if $res->{'priority'} eq '0';
187     }
188 }
189
190 # use Data::Dumper;
191 # warn Dumper(@reserves);
192
193 $template->param( RESERVES       => \@reserves );
194 $template->param( reserves_count => $#reserves+1 );
195 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
196
197 my @waiting;
198 my $wcount = 0;
199 foreach my $res (@reserves) {
200     if ( $res->{'itemnumber'} ) {
201         my $item = GetItem( $res->{'itemnumber'});
202         $res->{'holdingbranch'} =
203           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
204         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
205         # get document reserve status
206         my $biblioData = GetBiblioData($res->{'biblionumber'});
207         $res->{'waiting_title'} = $biblioData->{'title'};
208         if ( ( $res->{'found'} eq 'W' ) ) {
209             my $item = $res->{'itemnumber'};
210             $item = GetBiblioFromItemNumber($item,undef);
211             $res->{'wait'}= 1; 
212             $res->{'holdingbranch'}=$item->{'holdingbranch'};
213             $res->{'biblionumber'}=$item->{'biblionumber'};
214             $res->{'barcodenumber'} = $item->{'barcode'};
215             $res->{'wbrcode'} = $res->{'branchcode'};
216             $res->{'itemnumber'}    = $res->{'itemnumber'};
217             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
218             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
219                 $res->{'atdestination'} = 1;
220             }
221             # set found to 1 if reserve is waiting for patron pickup
222             $res->{'found'} = 1 if $res->{'found'} eq 'W';
223         } else {
224             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
225             if ($transfertwhen) {
226                 $res->{intransit} = 1;
227                 $res->{datesent}   = format_date($transfertwhen);
228                 $res->{frombranch} = GetBranchName($transfertfrom);
229             }
230         }
231         push @waiting, $res;
232         $wcount++;
233     }
234 }
235
236 $template->param( WAITING => \@waiting );
237
238 # current alert subscriptions
239 my $alerts = getalert($borrowernumber);
240 foreach ( @$alerts ) {
241     $_->{ $_->{type} } = 1;
242     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
243 }
244
245 if (C4::Context->preference('BakerTaylorEnabled')) {
246     $template->param(
247         BakerTaylorEnabled  => 1,
248         BakerTaylorImageURL => &image_url(),
249         BakerTaylorLinkURL  => &link_url(),
250         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
251     );
252 }
253
254 if (C4::Context->preference("OPACAmazonCoverImages") or 
255     C4::Context->preference("GoogleJackets") or
256     C4::Context->preference("BakerTaylorEnabled") or
257         C4::Context->preference("SyndeticsCoverImages")) {
258         $template->param(JacketImages=>1);
259 }
260
261 $template->param(
262     waiting_count      => $wcount,
263     textmessaging      => $borr->{textmessaging},
264     patronupdate => $patronupdate,
265     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
266     userview => 1,
267     dateformat    => C4::Context->preference("dateformat"),
268 );
269
270 output_html_with_http_headers $query, $cookie, $template->output;
271