(bug #4062) add item index in zebra conf
[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 for (qw(dateenrolled dateexpiry dateofbirth)) {
62     ($borr->{$_}) and $borr->{$_} = format_date($borr->{$_});
63 }
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     $issue->{'status'} = $status && C4::Context->preference("OpacRenewalAllowed");
131     $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
132     $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
133
134     if ( $issue->{'overdue'} ) {
135         push @overdues, $issue;
136         $overdues_count++;
137         $issue->{'overdue'} = 1;
138     }
139     else {
140         $issue->{'issued'} = 1;
141     }
142     # imageurl:
143     my $itemtype = $issue->{'itemtype'};
144     if ( $itemtype ) {
145         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
146         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
147     }
148     $issue->{date_due} = format_date($issue->{date_due});
149     push @issuedat, $issue;
150     $count++;
151         
152                 # XISBN Stuff
153         my $xisbn=$issue->{'isbn'};
154         $xisbn =~ /(\d*[X]*)/;
155         $issue->{amazonisbn} = $1;              # FIXME: so it is OK if the ISBN = 'XXXXX' ?
156         my ($clean, $amazonisbn);
157         $amazonisbn = $1;
158         # these might be overkill, but they are better than the regexp above.
159         if (
160                 $amazonisbn =~ /\b(\d{13})\b/ or
161                 $amazonisbn =~ /\b(\d{10})\b/ or 
162                 $amazonisbn =~ /\b(\d{9}X)\b/i
163         ) {
164                 $clean = $1;
165                 $issue->{clean_isbn} = $1;
166         }
167         
168 }
169
170 $template->param( ISSUES       => \@issuedat );
171 $template->param( issues_count => $count );
172
173 $template->param( OVERDUES       => \@overdues );
174 $template->param( overdues_count => $overdues_count );
175
176 # load the branches
177 my $branches = GetBranches();
178 my @branch_loop;
179 for my $branch_hash (sort keys %$branches ) {
180     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
181     push @branch_loop,
182       {
183         value      => "branch: $branch_hash",
184         branchname => $branches->{$branch_hash}->{'branchname'},
185         selected => $selected
186       };
187 }
188 $template->param( branchloop => \@branch_loop );
189
190 # now the reserved items....
191 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
192 foreach my $res (@reserves) {
193     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
194     my $publictype = $res->{'publictype'};
195     $res->{$publictype} = 1;
196     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
197     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
198     my $biblioData = GetBiblioData($res->{'biblionumber'});
199     $res->{'reserves_title'} = $biblioData->{'title'};
200         if ($OPACDisplayRequestPriority) {
201                 $res->{'priority'} = '' if $res->{'priority'} eq '0';
202         }
203 }
204
205 # use Data::Dumper;
206 # warn Dumper(@reserves);
207
208 $template->param( RESERVES       => \@reserves );
209 $template->param( reserves_count => $#reserves+1 );
210 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
211
212 my @waiting;
213 my $wcount = 0;
214 foreach my $res (@reserves) {
215     if ( $res->{'itemnumber'} ) {
216         my $item = GetItem( $res->{'itemnumber'});
217         $res->{'holdingbranch'} =
218           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
219         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
220         # get document reserve status
221         my $biblioData = GetBiblioData($res->{'biblionumber'});
222         $res->{'waiting_title'} = $biblioData->{'title'};
223         if ( ( $res->{'found'} eq 'W' ) ) {
224             my $item = $res->{'itemnumber'};
225             $item = GetBiblioFromItemNumber($item,undef);
226             $res->{'wait'}= 1; 
227             $res->{'holdingbranch'}=$item->{'holdingbranch'};
228             $res->{'biblionumber'}=$item->{'biblionumber'};
229             $res->{'barcodenumber'}     = $item->{'barcode'};
230             $res->{'wbrcode'} = $res->{'branchcode'};
231             $res->{'itemnumber'}        = $res->{'itemnumber'};
232             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
233             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
234                 $res->{'atdestination'} = 1;
235             }
236             # set found to 1 if reserve is waiting for patron pickup
237             $res->{'found'} = 1 if $res->{'found'} eq 'W';
238         } else {
239             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
240             if ($transfertwhen) {
241                 $res->{intransit} = 1;
242                 $res->{datesent}   = format_date($transfertwhen);
243                 $res->{frombranch} = GetBranchName($transfertfrom);
244             }
245         }
246         push @waiting, $res;
247         $wcount++;
248     }
249     # can be cancelled
250     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
251     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
252     
253 }
254
255 $template->param( WAITING => \@waiting );
256
257 # current alert subscriptions
258 my $alerts = getalert($borrowernumber);
259 foreach ( @$alerts ) {
260     $_->{ $_->{type} } = 1;
261     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
262 }
263
264 if (C4::Context->preference('BakerTaylorEnabled')) {
265         $template->param(
266                 BakerTaylorEnabled  => 1,
267                 BakerTaylorImageURL => &image_url(),
268                 BakerTaylorLinkURL  => &link_url(),
269                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
270         );
271 }
272
273 if (C4::Context->preference("AmazonContent"     ) or 
274         C4::Context->preference("GoogleJackets"     ) or
275         C4::Context->preference("BakerTaylorEnabled")   ) {
276                 $template->param(JacketImages=>1);
277 }
278
279 $template->param(
280     waiting_count      => $wcount,
281     textmessaging      => $borr->{textmessaging},
282         patronupdate => $patronupdate,
283         OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
284         userview => 1,
285         dateformat    => C4::Context->preference("dateformat"),
286 );
287
288 output_html_with_http_headers $query, $cookie, $template->output;
289