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