Bug 6547: Add id attributes to messages in opac-reserve.tt
[koha.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
83 if ( $borr->{'amountoutstanding'} > C4::Context->preference( 'OPACFineNoRenewals' ) ) {
84     $borr->{'flagged'} = 1;
85     $template->param(
86         renewal_blocked_fines => sprintf( "%.02f", C4::Context->preference( 'OPACFineNoRenewals' ) ),
87     );
88 }
89
90 if ( $borr->{'amountoutstanding'} < 0 ) {
91     $borr->{'amountlessthanzero'} = 1;
92     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
93 }
94
95 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
96
97 my @bordat;
98 $bordat[0] = $borr;
99
100 $template->param(   BORROWER_INFO     => \@bordat,
101                     borrowernumber    => $borrowernumber,
102                     patron_flagged    => $borr->{flagged},
103                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
104                 );
105
106 #get issued items ....
107
108 my $count          = 0;
109 my $toggle = 0;
110 my $overdues_count = 0;
111 my @overdues;
112 my @issuedat;
113 my $itemtypes = GetItemTypes();
114 my ($issues) = GetPendingIssues($borrowernumber);
115 if ($issues){
116         foreach my $issue ( sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues ) {
117                 # check for reserves
118                 my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
119                 if ( $restype ) {
120                         $issue->{'reserved'} = 1;
121                 }
122                 
123                 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
124                 my $charges = 0;
125                 foreach my $ac (@$accts) {
126                         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
127                                 $charges += $ac->{'amountoutstanding'}
128                                   if $ac->{'accounttype'} eq 'F';
129                                 $charges += $ac->{'amountoutstanding'}
130                                   if $ac->{'accounttype'} eq 'L';
131                         }
132                 }
133                 $issue->{'charges'} = $charges;
134
135                 # get publictype for icon
136
137                 my $publictype = $issue->{'publictype'};
138                 $issue->{$publictype} = 1;
139
140                 # check if item is renewable
141                 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
142                 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
143                 $issue->{'status'} = $status && C4::Context->preference("OpacRenewalAllowed");
144                 $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
145                 $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
146
147                 if ( $issue->{'overdue'} ) {
148                         push @overdues, $issue;
149                         $overdues_count++;
150                         $issue->{'overdue'} = 1;
151                 }
152                 else {
153                         $issue->{'issued'} = 1;
154                 }
155                 # imageurl:
156                 my $itemtype = $issue->{'itemtype'};
157                 if ( $itemtype ) {
158                         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
159                         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
160                 }
161                 $issue->{date_due} = format_date($issue->{date_due});
162                 push @issuedat, $issue;
163                 $count++;
164                 
165                 my $isbn = GetNormalizedISBN($issue->{'isbn'});
166                 $issue->{normalized_isbn} = $isbn;
167
168                 # My Summary HTML
169                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
170                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
171                     $issue->{title} =~ s/\/+$//; # remove trailing slash
172                     $issue->{title} =~ s/\s+$//; # remove trailing space
173                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
174                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
175                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
176                     $issue->{MySummaryHTML} = $my_summary_html;
177                 }
178         }
179 }
180 $template->param( ISSUES       => \@issuedat );
181 $template->param( issues_count => $count );
182
183 $template->param( OVERDUES       => \@overdues );
184 $template->param( overdues_count => $overdues_count );
185
186 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
187 if ($show_barcode) {
188     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
189     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
190 }
191 $template->param( show_barcode => 1 ) if $show_barcode;
192
193 # load the branches
194 my $branches = GetBranches();
195 my @branch_loop;
196 for my $branch_hash ( sort keys %{$branches} ) {
197     my $selected;
198     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
199         $selected =
200           ( C4::Context->userenv
201               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
202     }
203     push @branch_loop,
204       { value      => "branch: $branch_hash",
205         branchname => $branches->{$branch_hash}->{'branchname'},
206         selected   => $selected,
207       };
208 }
209 $template->param( branchloop => \@branch_loop );
210
211 # now the reserved items....
212 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
213 foreach my $res (@reserves) {
214     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
215
216     if ( $res->{'expirationdate'} ne '0000-00-00' ) {
217       $res->{'expirationdate'} = format_date( $res->{'expirationdate'} ) 
218     } else {
219       $res->{'expirationdate'} = '';
220     }
221     
222     my $publictype = $res->{'publictype'};
223     $res->{$publictype} = 1;
224     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
225     $res->{'formattedwaitingdate'} = format_date($res->{'waitingdate'});
226     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
227     my $biblioData = GetBiblioData($res->{'biblionumber'});
228     $res->{'reserves_title'} = $biblioData->{'title'};
229     if ($OPACDisplayRequestPriority) {
230         $res->{'priority'} = '' if $res->{'priority'} eq '0';
231     }
232 }
233
234 # use Data::Dumper;
235 # warn Dumper(@reserves);
236
237 $template->param( RESERVES       => \@reserves );
238 $template->param( reserves_count => $#reserves+1 );
239 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
240
241 my @waiting;
242 my $wcount = 0;
243 foreach my $res (@reserves) {
244     if ( $res->{'itemnumber'} ) {
245         my $item = GetItem( $res->{'itemnumber'});
246         $res->{'holdingbranch'} =
247           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
248         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
249         # get document reserve status
250         my $biblioData = GetBiblioData($res->{'biblionumber'});
251         $res->{'waiting_title'} = $biblioData->{'title'};
252         if ( ( $res->{'found'} eq 'W' ) ) {
253             my $item = $res->{'itemnumber'};
254             $item = GetBiblioFromItemNumber($item,undef);
255             $res->{'wait'}= 1; 
256             $res->{'holdingbranch'}=$item->{'holdingbranch'};
257             $res->{'biblionumber'}=$item->{'biblionumber'};
258             $res->{'barcode'} = $item->{'barcode'};
259             $res->{'wbrcode'} = $res->{'branchcode'};
260             $res->{'itemnumber'}    = $res->{'itemnumber'};
261             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
262             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
263                 $res->{'atdestination'} = 1;
264             }
265             # set found to 1 if reserve is waiting for patron pickup
266             $res->{'found'} = 1 if $res->{'found'} eq 'W';
267         } else {
268             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
269             if ($transfertwhen) {
270                 $res->{intransit} = 1;
271                 $res->{datesent}   = format_date($transfertwhen);
272                 $res->{frombranch} = GetBranchName($transfertfrom);
273             }
274         }
275         push @waiting, $res;
276         $wcount++;
277     }
278     # can be cancelled
279     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
280     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
281     
282 }
283
284 $template->param( WAITING => \@waiting );
285
286 # current alert subscriptions
287 my $alerts = getalert($borrowernumber);
288 foreach ( @$alerts ) {
289     $_->{ $_->{type} } = 1;
290     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
291 }
292
293 if (C4::Context->preference('BakerTaylorEnabled')) {
294     $template->param(
295         BakerTaylorEnabled  => 1,
296         BakerTaylorImageURL => &image_url(),
297         BakerTaylorLinkURL  => &link_url(),
298         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
299     );
300 }
301
302 if (C4::Context->preference("OPACAmazonCoverImages") or 
303     C4::Context->preference("GoogleJackets") or
304     C4::Context->preference("BakerTaylorEnabled") or
305         C4::Context->preference("SyndeticsCoverImages")) {
306         $template->param(JacketImages=>1);
307 }
308
309 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
310         $template->param( bor_messages => 1 );
311 }
312
313 if ( $borr->{'opacnote'} ) {
314   $template->param( 
315     bor_messages => 1,
316     opacnote => $borr->{'opacnote'},
317   );
318 }
319
320 $template->param(
321     bor_messages_loop   => GetMessages( $borrowernumber, 'B', 'NONE' ),
322     waiting_count      => $wcount,
323     textmessaging      => $borr->{textmessaging},
324     patronupdate => $patronupdate,
325     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
326     userview => 1,
327     dateformat    => C4::Context->preference("dateformat"),
328 );
329
330 output_html_with_http_headers $query, $cookie, $template->output;
331