Bumping database version to 3.05.00.015
[wip/koha-chris_n.git] / opac / opac-user.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 # parts copyright 2010 BibLibre
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA  02111-1307 USA
18
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use CGI;
24
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Circulation;
28 use C4::Reserves;
29 use C4::Members;
30 use C4::Members::AttributeTypes;
31 use C4::Members::Attributes qw/GetBorrowerAttributeValue/;
32 use C4::Output;
33 use C4::Biblio;
34 use C4::Items;
35 use C4::Dates qw/format_date/;
36 use C4::Letters;
37 use C4::Branch; # GetBranches
38
39 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
40
41 my $query = new CGI;
42
43 BEGIN {
44     if (C4::Context->preference('BakerTaylorEnabled')) {
45         require C4::External::BakerTaylor;
46         import C4::External::BakerTaylor qw(&image_url &link_url);
47     }
48 }
49
50 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
51     {
52         template_name   => "opac-user.tmpl",
53         query           => $query,
54         type            => "opac",
55         authnotrequired => 0,
56         flagsrequired   => { borrow => 1 },
57         debug           => 1,
58     }
59 );
60
61 my $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
62 my $patronupdate = $query->param('patronupdate');
63
64 # get borrower information ....
65 my ( $borr ) = GetMemberDetails( $borrowernumber );
66
67 for (qw(dateenrolled dateexpiry dateofbirth)) {
68     ($borr->{$_}) and $borr->{$_} = format_date($borr->{$_});
69 }
70 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
71
72 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
73     $borr->{'flagged'} = 1;
74 }
75
76 if ( $borr->{'amountoutstanding'} > 5 ) {
77     $borr->{'amountoverfive'} = 1;
78 }
79 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
80     $borr->{'amountoverzero'} = 1;
81 }
82 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
83 $no_renewal_amt ||= 0;
84
85 if ( $borr->{amountoutstanding} > $no_renewal_amt ) {
86     $borr->{'flagged'} = 1;
87     $template->param(
88         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
89     );
90 }
91
92 if ( $borr->{'amountoutstanding'} < 0 ) {
93     $borr->{'amountlessthanzero'} = 1;
94     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
95 }
96
97 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
98
99 my @bordat;
100 $bordat[0] = $borr;
101
102 $template->param(   BORROWER_INFO     => \@bordat,
103                     borrowernumber    => $borrowernumber,
104                     patron_flagged    => $borr->{flagged},
105                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
106                 );
107
108 #get issued items ....
109
110 my $count          = 0;
111 my $toggle = 0;
112 my $overdues_count = 0;
113 my @overdues;
114 my @issuedat;
115 my $itemtypes = GetItemTypes();
116 my ($issues) = GetPendingIssues($borrowernumber);
117 my $canrenew = 0;
118 if ($issues){
119         foreach my $issue ( sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues ) {
120                 # check for reserves
121                 my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
122                 if ( $restype ) {
123                         $issue->{'reserved'} = 1;
124                 }
125                 
126                 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
127                 my $charges = 0;
128                 foreach my $ac (@$accts) {
129                         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
130                                 $charges += $ac->{'amountoutstanding'}
131                                   if $ac->{'accounttype'} eq 'F';
132                                 $charges += $ac->{'amountoutstanding'}
133                                   if $ac->{'accounttype'} eq 'L';
134                         }
135                 }
136                 $issue->{'charges'} = $charges;
137
138                 # get publictype for icon
139
140                 my $publictype = $issue->{'publictype'};
141                 $issue->{$publictype} = 1;
142
143                 # check if item is renewable
144                 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
145                 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
146         if($status && C4::Context->preference("OpacRenewalAllowed")){
147             $issue->{'status'} = $status;
148             $canrenew = 1;
149         }
150                 $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
151                 $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
152
153                 if ( $issue->{'overdue'} ) {
154                         push @overdues, $issue;
155                         $overdues_count++;
156                         $issue->{'overdue'} = 1;
157                 }
158                 else {
159                         $issue->{'issued'} = 1;
160                 }
161                 # imageurl:
162                 my $itemtype = $issue->{'itemtype'};
163                 if ( $itemtype ) {
164                         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
165                         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
166                 }
167                 $issue->{date_due} = format_date($issue->{date_due});
168                 push @issuedat, $issue;
169                 $count++;
170                 
171                 my $isbn = GetNormalizedISBN($issue->{'isbn'});
172                 $issue->{normalized_isbn} = $isbn;
173
174                 # My Summary HTML
175                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
176                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
177                     $issue->{title} =~ s/\/+$//; # remove trailing slash
178                     $issue->{title} =~ s/\s+$//; # remove trailing space
179                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
180                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
181                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
182                     $issue->{MySummaryHTML} = $my_summary_html;
183                 }
184         }
185 }
186 $template->param( ISSUES       => \@issuedat );
187 $template->param( issues_count => $count );
188 $template->param( canrenew     => $canrenew );
189 $template->param( OVERDUES       => \@overdues );
190 $template->param( overdues_count => $overdues_count );
191
192 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
193 if ($show_barcode) {
194     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
195     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
196 }
197 $template->param( show_barcode => 1 ) if $show_barcode;
198
199 # load the branches
200 my $branches = GetBranches();
201 my @branch_loop;
202 for my $branch_hash ( sort keys %{$branches} ) {
203     my $selected;
204     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
205         $selected =
206           ( C4::Context->userenv
207               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
208     }
209     push @branch_loop,
210       { value      => "branch: $branch_hash",
211         branchname => $branches->{$branch_hash}->{'branchname'},
212         selected   => $selected,
213       };
214 }
215 $template->param( branchloop => \@branch_loop );
216
217 # now the reserved items....
218 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
219 foreach my $res (@reserves) {
220     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
221
222     if ( $res->{'expirationdate'} ne '0000-00-00' ) {
223       $res->{'expirationdate'} = format_date( $res->{'expirationdate'} ) 
224     } else {
225       $res->{'expirationdate'} = '';
226     }
227     
228     my $publictype = $res->{'publictype'};
229     $res->{$publictype} = 1;
230     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
231     $res->{'formattedwaitingdate'} = format_date($res->{'waitingdate'});
232     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
233     my $biblioData = GetBiblioData($res->{'biblionumber'});
234     $res->{'reserves_title'} = $biblioData->{'title'};
235     if ($OPACDisplayRequestPriority) {
236         $res->{'priority'} = '' if $res->{'priority'} eq '0';
237     }
238 }
239
240 # use Data::Dumper;
241 # warn Dumper(@reserves);
242
243 $template->param( RESERVES       => \@reserves );
244 $template->param( reserves_count => $#reserves+1 );
245 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
246
247 my @waiting;
248 my $wcount = 0;
249 foreach my $res (@reserves) {
250     if ( $res->{'itemnumber'} ) {
251         my $item = GetItem( $res->{'itemnumber'});
252         $res->{'holdingbranch'} =
253           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
254         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
255         # get document reserve status
256         my $biblioData = GetBiblioData($res->{'biblionumber'});
257         $res->{'waiting_title'} = $biblioData->{'title'};
258         if ( ( $res->{'found'} eq 'W' ) ) {
259             my $item = $res->{'itemnumber'};
260             $item = GetBiblioFromItemNumber($item,undef);
261             $res->{'wait'}= 1; 
262             $res->{'holdingbranch'}=$item->{'holdingbranch'};
263             $res->{'biblionumber'}=$item->{'biblionumber'};
264             $res->{'barcode'} = $item->{'barcode'};
265             $res->{'wbrcode'} = $res->{'branchcode'};
266             $res->{'itemnumber'}    = $res->{'itemnumber'};
267             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
268             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
269                 $res->{'atdestination'} = 1;
270             }
271             # set found to 1 if reserve is waiting for patron pickup
272             $res->{'found'} = 1 if $res->{'found'} eq 'W';
273         } else {
274             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
275             if ($transfertwhen) {
276                 $res->{intransit} = 1;
277                 $res->{datesent}   = format_date($transfertwhen);
278                 $res->{frombranch} = GetBranchName($transfertfrom);
279             }
280         }
281         push @waiting, $res;
282         $wcount++;
283     }
284     # can be cancelled
285     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
286     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
287     
288 }
289
290 $template->param( WAITING => \@waiting );
291
292 # current alert subscriptions
293 my $alerts = getalert($borrowernumber);
294 foreach ( @$alerts ) {
295     $_->{ $_->{type} } = 1;
296     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
297 }
298
299 if (C4::Context->preference('BakerTaylorEnabled')) {
300     $template->param(
301         BakerTaylorEnabled  => 1,
302         BakerTaylorImageURL => &image_url(),
303         BakerTaylorLinkURL  => &link_url(),
304         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
305     );
306 }
307
308 if (C4::Context->preference("OPACAmazonCoverImages") or 
309     C4::Context->preference("GoogleJackets") or
310     C4::Context->preference("BakerTaylorEnabled") or
311         C4::Context->preference("SyndeticsCoverImages")) {
312         $template->param(JacketImages=>1);
313 }
314
315 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
316         $template->param( bor_messages => 1 );
317 }
318
319 if ( $borr->{'opacnote'} ) {
320   $template->param( 
321     bor_messages => 1,
322     opacnote => $borr->{'opacnote'},
323   );
324 }
325
326 $template->param(
327     bor_messages_loop   => GetMessages( $borrowernumber, 'B', 'NONE' ),
328     waiting_count      => $wcount,
329     textmessaging      => $borr->{textmessaging},
330     patronupdate => $patronupdate,
331     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
332     userview => 1,
333     dateformat    => C4::Context->preference("dateformat"),
334 );
335
336 output_html_with_http_headers $query, $cookie, $template->output;
337