Bug 7262 - No calendar present in holidays module when there are quotes in title...
[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 use Date::Calc qw(
42   Today
43   Add_Delta_Days
44   Date_to_Days
45 );
46
47 my $query = new CGI;
48
49 BEGIN {
50     if (C4::Context->preference('BakerTaylorEnabled')) {
51         require C4::External::BakerTaylor;
52         import C4::External::BakerTaylor qw(&image_url &link_url);
53     }
54 }
55
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57     {
58         template_name   => "opac-user.tmpl",
59         query           => $query,
60         type            => "opac",
61         authnotrequired => 0,
62         flagsrequired   => { borrow => 1 },
63         debug           => 1,
64     }
65 );
66
67 my $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
68 my $patronupdate = $query->param('patronupdate');
69
70 # get borrower information ....
71 my ( $borr ) = GetMemberDetails( $borrowernumber );
72
73 my (  $today_year,   $today_month,   $today_day) = Today();
74 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
75
76 for (qw(dateenrolled dateexpiry dateofbirth)) {
77     ($borr->{$_}) and $borr->{$_} = format_date($borr->{$_});
78 }
79 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
80
81 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
82     $borr->{'flagged'} = 1;
83 }
84
85 if ( $borr->{'amountoutstanding'} > 5 ) {
86     $borr->{'amountoverfive'} = 1;
87 }
88 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
89     $borr->{'amountoverzero'} = 1;
90 }
91 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
92 $no_renewal_amt ||= 0;
93
94 if ( $borr->{amountoutstanding} > $no_renewal_amt ) {
95     $borr->{'flagged'} = 1;
96     $template->param(
97         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
98     );
99 }
100
101 if ( $borr->{'amountoutstanding'} < 0 ) {
102     $borr->{'amountlessthanzero'} = 1;
103     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
104 }
105
106 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
107 $borr->{'debarred'} = C4::Dates->new($borr->{'debarred'},'iso')->output;
108
109 my @bordat;
110 $bordat[0] = $borr;
111
112 # Warningdate is the date that the warning starts appearing
113 if ( C4::Context->preference('NotifyBorrowerDeparture') &&
114     Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
115     Date_to_Days( $today_year, $today_month, $today_day ) ) 
116 {
117     # borrower card soon to expire, warn the borrower
118     $borr->{'warndeparture'} = $borr->{dateexpiry};
119     if (C4::Context->preference('ReturnBeforeExpiry')){
120         $borr->{'returnbeforeexpiry'} = 1;
121     }
122 }
123
124 $template->param(   BORROWER_INFO     => \@bordat,
125                     borrowernumber    => $borrowernumber,
126                     patron_flagged    => $borr->{flagged},
127                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
128                     surname           => $borr->{surname},
129                     showname          => $borr->{showname},
130                 );
131
132 #get issued items ....
133
134 my $count          = 0;
135 my $toggle = 0;
136 my $overdues_count = 0;
137 my @overdues;
138 my @issuedat;
139 my $itemtypes = GetItemTypes();
140 my ($issues) = GetPendingIssues($borrowernumber);
141 my $canrenew = 0;
142 if ($issues){
143         foreach my $issue ( sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues ) {
144                 # check for reserves
145                 my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
146                 if ( $restype ) {
147                         $issue->{'reserved'} = 1;
148                 }
149                 
150                 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
151                 my $charges = 0;
152                 foreach my $ac (@$accts) {
153                         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
154                                 $charges += $ac->{'amountoutstanding'}
155                                   if $ac->{'accounttype'} eq 'F';
156                                 $charges += $ac->{'amountoutstanding'}
157                                   if $ac->{'accounttype'} eq 'L';
158                         }
159                 }
160                 $issue->{'charges'} = $charges;
161
162                 # get publictype for icon
163
164                 my $publictype = $issue->{'publictype'};
165                 $issue->{$publictype} = 1;
166
167                 # check if item is renewable
168                 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
169                 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
170         if($status && C4::Context->preference("OpacRenewalAllowed")){
171             $issue->{'status'} = $status;
172             $canrenew = 1;
173         }
174                 $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
175                 $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
176
177                 if ( $issue->{'overdue'} ) {
178                         push @overdues, $issue;
179                         $overdues_count++;
180                         $issue->{'overdue'} = 1;
181                 }
182                 else {
183                         $issue->{'issued'} = 1;
184                 }
185                 # imageurl:
186                 my $itemtype = $issue->{'itemtype'};
187                 if ( $itemtype ) {
188                         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
189                         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
190                 }
191                 $issue->{date_due} = format_date($issue->{date_due});
192                 push @issuedat, $issue;
193                 $count++;
194                 
195                 my $isbn = GetNormalizedISBN($issue->{'isbn'});
196                 $issue->{normalized_isbn} = $isbn;
197
198                 # My Summary HTML
199                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
200                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
201                     $issue->{title} =~ s/\/+$//; # remove trailing slash
202                     $issue->{title} =~ s/\s+$//; # remove trailing space
203                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
204                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
205                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
206                     $issue->{MySummaryHTML} = $my_summary_html;
207                 }
208         }
209 }
210 $template->param( ISSUES       => \@issuedat );
211 $template->param( issues_count => $count );
212 $template->param( canrenew     => $canrenew );
213 $template->param( OVERDUES       => \@overdues );
214 $template->param( overdues_count => $overdues_count );
215
216 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
217 if ($show_barcode) {
218     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
219     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
220 }
221 $template->param( show_barcode => 1 ) if $show_barcode;
222
223 # load the branches
224 my $branches = GetBranches();
225 my @branch_loop;
226 for my $branch_hash ( sort keys %{$branches} ) {
227     my $selected;
228     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
229         $selected =
230           ( C4::Context->userenv
231               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
232     }
233     push @branch_loop,
234       { value      => "branch: $branch_hash",
235         branchname => $branches->{$branch_hash}->{'branchname'},
236         selected   => $selected,
237       };
238 }
239 $template->param( branchloop => \@branch_loop );
240
241 # now the reserved items....
242 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
243 foreach my $res (@reserves) {
244     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
245
246     if ( $res->{'expirationdate'} ne '0000-00-00' ) {
247       $res->{'expirationdate'} = format_date( $res->{'expirationdate'} ) 
248     } else {
249       $res->{'expirationdate'} = '';
250     }
251     
252     my $publictype = $res->{'publictype'};
253     $res->{$publictype} = 1;
254     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
255     $res->{'formattedwaitingdate'} = format_date($res->{'waitingdate'});
256     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
257     my $biblioData = GetBiblioData($res->{'biblionumber'});
258     $res->{'reserves_title'} = $biblioData->{'title'};
259     if ($OPACDisplayRequestPriority) {
260         $res->{'priority'} = '' if $res->{'priority'} eq '0';
261     }
262 }
263
264 # use Data::Dumper;
265 # warn Dumper(@reserves);
266
267 $template->param( RESERVES       => \@reserves );
268 $template->param( reserves_count => $#reserves+1 );
269 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
270
271 my @waiting;
272 my $wcount = 0;
273 foreach my $res (@reserves) {
274     if ( $res->{'itemnumber'} ) {
275         my $item = GetItem( $res->{'itemnumber'});
276         $res->{'holdingbranch'} =
277           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
278         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
279         # get document reserve status
280         my $biblioData = GetBiblioData($res->{'biblionumber'});
281         $res->{'waiting_title'} = $biblioData->{'title'};
282         if ( ( $res->{'found'} eq 'W' ) ) {
283             my $item = $res->{'itemnumber'};
284             $item = GetBiblioFromItemNumber($item,undef);
285             $res->{'wait'}= 1; 
286             $res->{'holdingbranch'}=$item->{'holdingbranch'};
287             $res->{'biblionumber'}=$item->{'biblionumber'};
288             $res->{'barcode'} = $item->{'barcode'};
289             $res->{'wbrcode'} = $res->{'branchcode'};
290             $res->{'itemnumber'}    = $res->{'itemnumber'};
291             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
292             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
293                 $res->{'atdestination'} = 1;
294             }
295             # set found to 1 if reserve is waiting for patron pickup
296             $res->{'found'} = 1 if $res->{'found'} eq 'W';
297         } else {
298             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
299             if ($transfertwhen) {
300                 $res->{intransit} = 1;
301                 $res->{datesent}   = format_date($transfertwhen);
302                 $res->{frombranch} = GetBranchName($transfertfrom);
303             }
304         }
305         push @waiting, $res;
306         $wcount++;
307     }
308     # can be cancelled
309     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
310     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
311     
312 }
313
314 $template->param( WAITING => \@waiting );
315
316 # current alert subscriptions
317 my $alerts = getalert($borrowernumber);
318 foreach ( @$alerts ) {
319     $_->{ $_->{type} } = 1;
320     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
321 }
322
323 if (C4::Context->preference('BakerTaylorEnabled')) {
324     $template->param(
325         BakerTaylorEnabled  => 1,
326         BakerTaylorImageURL => &image_url(),
327         BakerTaylorLinkURL  => &link_url(),
328         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
329     );
330 }
331
332 if (C4::Context->preference("OPACAmazonCoverImages") or 
333     C4::Context->preference("GoogleJackets") or
334     C4::Context->preference("BakerTaylorEnabled") or
335         C4::Context->preference("SyndeticsCoverImages")) {
336         $template->param(JacketImages=>1);
337 }
338
339 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
340         $template->param( bor_messages => 1 );
341 }
342
343 if ( $borr->{'opacnote'} ) {
344   $template->param( 
345     bor_messages => 1,
346     opacnote => $borr->{'opacnote'},
347   );
348 }
349
350 $template->param(
351     bor_messages_loop   => GetMessages( $borrowernumber, 'B', 'NONE' ),
352     waiting_count      => $wcount,
353     textmessaging      => $borr->{textmessaging},
354     patronupdate => $patronupdate,
355     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
356     userview => 1,
357     dateformat    => C4::Context->preference("dateformat"),
358 );
359
360 output_html_with_http_headers $query, $cookie, $template->output;
361